LCOV - code coverage report
Current view: top level - gcc/fortran - trans-expr.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.8 % 7377 6991
Test Date: 2026-11-14 16:39:50 Functions: 96.3 % 164 158
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Expression translation
       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-expr.cc-- generate GENERIC trees for gfc_expr.  */
      23              : 
      24              : #define INCLUDE_MEMORY
      25              : #include "config.h"
      26              : #include "system.h"
      27              : #include "coretypes.h"
      28              : #include "options.h"
      29              : #include "tree.h"
      30              : #include "gfortran.h"
      31              : #include "trans.h"
      32              : #include "stringpool.h"
      33              : #include "diagnostic-core.h"  /* For fatal_error.  */
      34              : #include "fold-const.h"
      35              : #include "langhooks.h"
      36              : #include "arith.h"
      37              : #include "constructor.h"
      38              : #include "trans-const.h"
      39              : #include "trans-types.h"
      40              : #include "trans-array.h"
      41              : #include "trans-descriptor.h"
      42              : /* Only for gfc_trans_assign and gfc_trans_pointer_assign.  */
      43              : #include "trans-stmt.h"
      44              : #include "dependency.h"
      45              : #include "gimplify.h"
      46              : #include "tm.h"               /* For CHAR_TYPE_SIZE.  */
      47              : 
      48              : 
      49              : /* Calculate the number of characters in a string.  */
      50              : 
      51              : static tree
      52        36564 : gfc_get_character_len (tree type)
      53              : {
      54        36564 :   tree len;
      55              : 
      56        36564 :   gcc_assert (type && TREE_CODE (type) == ARRAY_TYPE
      57              :               && TYPE_STRING_FLAG (type));
      58              : 
      59        36564 :   len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
      60        36564 :   len = (len) ? (len) : (integer_zero_node);
      61        36564 :   return fold_convert (gfc_charlen_type_node, len);
      62              : }
      63              : 
      64              : 
      65              : 
      66              : /* Calculate the number of bytes in a string.  */
      67              : 
      68              : tree
      69        36564 : gfc_get_character_len_in_bytes (tree type)
      70              : {
      71        36564 :   tree tmp, len;
      72              : 
      73        36564 :   gcc_assert (type && TREE_CODE (type) == ARRAY_TYPE
      74              :               && TYPE_STRING_FLAG (type));
      75              : 
      76        36564 :   tmp = TYPE_SIZE_UNIT (TREE_TYPE (type));
      77        73128 :   tmp = (tmp && !integer_zerop (tmp))
      78        73128 :     ? (fold_convert (gfc_charlen_type_node, tmp)) : (NULL_TREE);
      79        36564 :   len = gfc_get_character_len (type);
      80        36564 :   if (tmp && len && !integer_zerop (len))
      81        35750 :     len = fold_build2_loc (input_location, MULT_EXPR,
      82              :                            gfc_charlen_type_node, len, tmp);
      83        36564 :   return len;
      84              : }
      85              : 
      86              : 
      87              : tree
      88         6508 : gfc_conv_scalar_to_descriptor (gfc_se *se, tree scalar, symbol_attribute attr)
      89              : {
      90         6508 :   tree desc, type;
      91              : 
      92         6508 :   type = gfc_get_scalar_to_descriptor_type (TREE_TYPE (scalar), attr);
      93         6508 :   desc = gfc_create_var (type, "desc");
      94         6508 :   DECL_ARTIFICIAL (desc) = 1;
      95              : 
      96         6508 :   if (CONSTANT_CLASS_P (scalar))
      97              :     {
      98            0 :       tree tmp;
      99            0 :       tmp = gfc_create_var (TREE_TYPE (scalar), "scalar");
     100            0 :       gfc_add_modify (&se->pre, tmp, scalar);
     101            0 :       scalar = tmp;
     102              :     }
     103              : 
     104         6508 :   gfc_set_descriptor_from_scalar (&se->pre, desc, scalar);
     105              : 
     106              :   /* Copy pointer address back - but only if it could have changed and
     107              :      if the actual argument is a pointer and not, e.g., NULL().  */
     108         6508 :   if ((attr.pointer || attr.allocatable) && attr.intent != INTENT_IN)
     109         2400 :     gfc_add_modify (&se->post, scalar,
     110         1200 :                     fold_convert (TREE_TYPE (scalar),
     111              :                                   gfc_conv_descriptor_data_get (desc)));
     112         6508 :   return desc;
     113              : }
     114              : 
     115              : 
     116              : /* Get the coarray token from the ultimate array or component ref.
     117              :    Returns a NULL_TREE, when the ref object is not allocatable or pointer.  */
     118              : 
     119              : tree
     120          580 : gfc_get_ultimate_alloc_ptr_comps_caf_token (gfc_se *outerse, gfc_expr *expr)
     121              : {
     122          580 :   gfc_symbol *sym = expr->symtree->n.sym;
     123         1160 :   bool is_coarray = sym->ts.type == BT_CLASS
     124          580 :                       ? CLASS_DATA (sym)->attr.codimension
     125          531 :                       : sym->attr.codimension;
     126          580 :   gfc_expr *caf_expr = gfc_copy_expr (expr);
     127          580 :   gfc_ref *ref = caf_expr->ref, *last_caf_ref = NULL;
     128              : 
     129         1784 :   while (ref)
     130              :     {
     131         1204 :       if (ref->type == REF_COMPONENT
     132          439 :           && (ref->u.c.component->attr.allocatable
     133          104 :               || ref->u.c.component->attr.pointer)
     134          437 :           && (is_coarray || ref->u.c.component->attr.codimension))
     135         1204 :           last_caf_ref = ref;
     136         1204 :       ref = ref->next;
     137              :     }
     138              : 
     139          580 :   if (last_caf_ref == NULL)
     140              :     {
     141          226 :       gfc_free_expr (caf_expr);
     142          226 :       return NULL_TREE;
     143              :     }
     144              : 
     145          143 :   tree comp = last_caf_ref->u.c.component->caf_token
     146          354 :                 ? gfc_comp_caf_token (last_caf_ref->u.c.component)
     147              :                 : NULL_TREE,
     148              :        caf;
     149          354 :   gfc_se se;
     150          354 :   bool comp_ref = !last_caf_ref->u.c.component->attr.dimension;
     151          354 :   if (comp == NULL_TREE && comp_ref)
     152              :     {
     153           62 :       gfc_free_expr (caf_expr);
     154           62 :       return NULL_TREE;
     155              :     }
     156          292 :   gfc_init_se (&se, outerse);
     157          292 :   gfc_free_ref_list (last_caf_ref->next);
     158          292 :   last_caf_ref->next = NULL;
     159          292 :   caf_expr->rank = comp_ref ? 0 : last_caf_ref->u.c.component->as->rank;
     160          584 :   caf_expr->corank = last_caf_ref->u.c.component->as
     161          292 :                        ? last_caf_ref->u.c.component->as->corank
     162              :                        : expr->corank;
     163          292 :   se.want_pointer = comp_ref;
     164          292 :   gfc_conv_expr (&se, caf_expr);
     165          292 :   gfc_add_block_to_block (&outerse->pre, &se.pre);
     166              : 
     167          292 :   if (TREE_CODE (se.expr) == COMPONENT_REF && comp_ref)
     168          143 :     se.expr = TREE_OPERAND (se.expr, 0);
     169          292 :   gfc_free_expr (caf_expr);
     170              : 
     171          292 :   if (comp_ref)
     172          143 :     caf = fold_build3_loc (input_location, COMPONENT_REF,
     173          143 :                            TREE_TYPE (comp), se.expr, comp, NULL_TREE);
     174              :   else
     175          149 :     caf = gfc_conv_descriptor_token (se.expr);
     176          292 :   return gfc_build_addr_expr (NULL_TREE, caf);
     177              : }
     178              : 
     179              : 
     180              : /* This is the seed for an eventual trans-class.c
     181              : 
     182              :    The following parameters should not be used directly since they might
     183              :    in future implementations.  Use the corresponding APIs.  */
     184              : #define CLASS_DATA_FIELD 0
     185              : #define CLASS_VPTR_FIELD 1
     186              : #define CLASS_LEN_FIELD 2
     187              : #define VTABLE_HASH_FIELD 0
     188              : #define VTABLE_SIZE_FIELD 1
     189              : #define VTABLE_EXTENDS_FIELD 2
     190              : #define VTABLE_DEF_INIT_FIELD 3
     191              : #define VTABLE_COPY_FIELD 4
     192              : #define VTABLE_FINAL_FIELD 5
     193              : #define VTABLE_DEALLOCATE_FIELD 6
     194              : 
     195              : 
     196              : tree
     197           40 : gfc_class_set_static_fields (tree decl, tree vptr, tree data)
     198              : {
     199           40 :   tree tmp;
     200           40 :   tree field;
     201           40 :   vec<constructor_elt, va_gc> *init = NULL;
     202              : 
     203           40 :   field = TYPE_FIELDS (TREE_TYPE (decl));
     204           40 :   tmp = gfc_advance_chain (field, CLASS_DATA_FIELD);
     205           40 :   CONSTRUCTOR_APPEND_ELT (init, tmp, data);
     206              : 
     207           40 :   tmp = gfc_advance_chain (field, CLASS_VPTR_FIELD);
     208           40 :   CONSTRUCTOR_APPEND_ELT (init, tmp, vptr);
     209              : 
     210           40 :   return build_constructor (TREE_TYPE (decl), init);
     211              : }
     212              : 
     213              : 
     214              : tree
     215        34701 : gfc_class_data_get (tree decl)
     216              : {
     217        34701 :   tree data;
     218        34701 :   if (POINTER_TYPE_P (TREE_TYPE (decl)))
     219         5687 :     decl = build_fold_indirect_ref_loc (input_location, decl);
     220        34701 :   data = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
     221              :                             CLASS_DATA_FIELD);
     222        34701 :   return fold_build3_loc (input_location, COMPONENT_REF,
     223        34701 :                           TREE_TYPE (data), decl, data,
     224        34701 :                           NULL_TREE);
     225              : }
     226              : 
     227              : 
     228              : tree
     229        49633 : gfc_class_vptr_get (tree decl)
     230              : {
     231        49633 :   tree vptr;
     232              :   /* For class arrays decl may be a temporary descriptor handle, the vptr is
     233              :      then available through the saved descriptor.  */
     234        29710 :   if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
     235        51568 :       && GFC_DECL_SAVED_DESCRIPTOR (decl))
     236         1264 :     decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
     237        49633 :   if (POINTER_TYPE_P (TREE_TYPE (decl)))
     238         2349 :     decl = build_fold_indirect_ref_loc (input_location, decl);
     239        49633 :   vptr = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
     240              :                             CLASS_VPTR_FIELD);
     241        49633 :   return fold_build3_loc (input_location, COMPONENT_REF,
     242        49633 :                           TREE_TYPE (vptr), decl, vptr,
     243        49633 :                           NULL_TREE);
     244              : }
     245              : 
     246              : 
     247              : tree
     248         7315 : gfc_class_len_get (tree decl)
     249              : {
     250         7315 :   tree len;
     251              :   /* For class arrays decl may be a temporary descriptor handle, the len is
     252              :      then available through the saved descriptor.  */
     253         5189 :   if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
     254         7618 :       && GFC_DECL_SAVED_DESCRIPTOR (decl))
     255          127 :     decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
     256         7315 :   if (POINTER_TYPE_P (TREE_TYPE (decl)))
     257          716 :     decl = build_fold_indirect_ref_loc (input_location, decl);
     258         7315 :   len = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
     259              :                            CLASS_LEN_FIELD);
     260         7315 :   return fold_build3_loc (input_location, COMPONENT_REF,
     261         7315 :                           TREE_TYPE (len), decl, len,
     262         7315 :                           NULL_TREE);
     263              : }
     264              : 
     265              : 
     266              : /* Try to get the _len component of a class.  When the class is not unlimited
     267              :    poly, i.e. no _len field exists, then return a zero node.  */
     268              : 
     269              : static tree
     270         8998 : gfc_class_len_or_zero_get (tree decl)
     271              : {
     272         8998 :   tree len;
     273              :   /* For class arrays decl may be a temporary descriptor handle, the vptr is
     274              :      then available through the saved descriptor.  */
     275         4417 :   if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
     276         9172 :       && GFC_DECL_SAVED_DESCRIPTOR (decl))
     277            0 :     decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
     278         8998 :   if (POINTER_TYPE_P (TREE_TYPE (decl)))
     279           18 :     decl = build_fold_indirect_ref_loc (input_location, decl);
     280         8998 :   len = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
     281              :                            CLASS_LEN_FIELD);
     282        11475 :   return len != NULL_TREE ? fold_build3_loc (input_location, COMPONENT_REF,
     283         2477 :                                              TREE_TYPE (len), decl, len,
     284              :                                              NULL_TREE)
     285         6521 :     : build_zero_cst (gfc_charlen_type_node);
     286              : }
     287              : 
     288              : 
     289              : tree
     290         8816 : gfc_resize_class_size_with_len (stmtblock_t * block, tree class_expr, tree size)
     291              : {
     292         8816 :   tree tmp;
     293         8816 :   tree tmp2;
     294         8816 :   tree type;
     295              : 
     296         8816 :   tmp = gfc_class_len_or_zero_get (class_expr);
     297              : 
     298              :   /* Include the len value in the element size if present.  */
     299         8816 :   if (!integer_zerop (tmp))
     300              :     {
     301         2295 :       type = TREE_TYPE (size);
     302         2295 :       if (block)
     303              :         {
     304         1144 :           size = gfc_evaluate_now (size, block);
     305         1144 :           tmp = gfc_evaluate_now (fold_convert (type , tmp), block);
     306              :         }
     307              :       else
     308         1151 :         tmp = fold_convert (type , tmp);
     309         2295 :       tmp2 = fold_build2_loc (input_location, MULT_EXPR,
     310              :                               type, size, tmp);
     311         2295 :       tmp = fold_build2_loc (input_location, GT_EXPR,
     312              :                              logical_type_node, tmp,
     313              :                              build_zero_cst (type));
     314         2295 :       size = fold_build3_loc (input_location, COND_EXPR,
     315              :                               type, tmp, tmp2, size);
     316              :     }
     317              :   else
     318              :     return size;
     319              : 
     320         2295 :   if (block)
     321         1144 :     size = gfc_evaluate_now (size, block);
     322              : 
     323              :   return size;
     324              : }
     325              : 
     326              : 
     327              : /* Get the specified FIELD from the VPTR.  */
     328              : 
     329              : static tree
     330        23441 : vptr_field_get (tree vptr, int fieldno)
     331              : {
     332        23441 :   tree field;
     333        23441 :   vptr = build_fold_indirect_ref_loc (input_location, vptr);
     334        23441 :   field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (vptr)),
     335              :                              fieldno);
     336        23441 :   field = fold_build3_loc (input_location, COMPONENT_REF,
     337        23441 :                            TREE_TYPE (field), vptr, field,
     338              :                            NULL_TREE);
     339        23441 :   gcc_assert (field);
     340        23441 :   return field;
     341              : }
     342              : 
     343              : 
     344              : /* Get the field from the class' vptr.  */
     345              : 
     346              : static tree
     347        11136 : class_vtab_field_get (tree decl, int fieldno)
     348              : {
     349        11136 :   tree vptr;
     350        11136 :   vptr = gfc_class_vptr_get (decl);
     351        11136 :   return vptr_field_get (vptr, fieldno);
     352              : }
     353              : 
     354              : 
     355              : /* Define a macro for creating the class_vtab_* and vptr_* accessors in
     356              :    unison.  */
     357              : #define VTAB_GET_FIELD_GEN(name, field) tree \
     358              : gfc_class_vtab_## name ##_get (tree cl) \
     359              : { \
     360              :   return class_vtab_field_get (cl, field); \
     361              : } \
     362              :  \
     363              : tree \
     364              : gfc_vptr_## name ##_get (tree vptr) \
     365              : { \
     366              :   return vptr_field_get (vptr, field); \
     367              : }
     368              : 
     369          183 : VTAB_GET_FIELD_GEN (hash, VTABLE_HASH_FIELD)
     370            0 : VTAB_GET_FIELD_GEN (extends, VTABLE_EXTENDS_FIELD)
     371            0 : VTAB_GET_FIELD_GEN (def_init, VTABLE_DEF_INIT_FIELD)
     372         4849 : VTAB_GET_FIELD_GEN (copy, VTABLE_COPY_FIELD)
     373         2059 : VTAB_GET_FIELD_GEN (final, VTABLE_FINAL_FIELD)
     374         1089 : VTAB_GET_FIELD_GEN (deallocate, VTABLE_DEALLOCATE_FIELD)
     375              : #undef VTAB_GET_FIELD_GEN
     376              : 
     377              : /* The size field is returned as an array index type.  Therefore treat
     378              :    it and only it specially.  */
     379              : 
     380              : tree
     381         8820 : gfc_class_vtab_size_get (tree cl)
     382              : {
     383         8820 :   tree size;
     384         8820 :   size = class_vtab_field_get (cl, VTABLE_SIZE_FIELD);
     385              :   /* Always return size as an array index type.  */
     386         8820 :   size = fold_convert (gfc_array_index_type, size);
     387         8820 :   gcc_assert (size);
     388         8820 :   return size;
     389              : }
     390              : 
     391              : tree
     392         6441 : gfc_vptr_size_get (tree vptr)
     393              : {
     394         6441 :   tree size;
     395         6441 :   size = vptr_field_get (vptr, VTABLE_SIZE_FIELD);
     396              :   /* Always return size as an array index type.  */
     397         6441 :   size = fold_convert (gfc_array_index_type, size);
     398         6441 :   gcc_assert (size);
     399         6441 :   return size;
     400              : }
     401              : 
     402              : 
     403              : #undef CLASS_DATA_FIELD
     404              : #undef CLASS_VPTR_FIELD
     405              : #undef CLASS_LEN_FIELD
     406              : #undef VTABLE_HASH_FIELD
     407              : #undef VTABLE_SIZE_FIELD
     408              : #undef VTABLE_EXTENDS_FIELD
     409              : #undef VTABLE_DEF_INIT_FIELD
     410              : #undef VTABLE_COPY_FIELD
     411              : #undef VTABLE_FINAL_FIELD
     412              : 
     413              : 
     414              : /* IF ts is null (default), search for the last _class ref in the chain
     415              :    of references of the expression and cut the chain there.  Although
     416              :    this routine is similar to class.cc:gfc_add_component_ref (), there
     417              :    is a significant difference: gfc_add_component_ref () concentrates
     418              :    on an array ref that is the last ref in the chain and is oblivious
     419              :    to the kind of refs following.
     420              :    ELSE IF ts is non-null the cut is at the class entity or component
     421              :    that is followed by an array reference, which is not an element.
     422              :    These calls come from trans-array.cc:build_class_array_ref, which
     423              :    handles scalarized class array references.*/
     424              : 
     425              : gfc_expr *
     426        10377 : gfc_find_and_cut_at_last_class_ref (gfc_expr *e, bool is_mold,
     427              :                                     gfc_typespec **ts)
     428              : {
     429        10377 :   gfc_expr *base_expr;
     430        10377 :   gfc_ref *ref, *class_ref, *tail = NULL, *array_ref;
     431              : 
     432              :   /* Find the last class reference.  */
     433        10377 :   class_ref = NULL;
     434        10377 :   array_ref = NULL;
     435              : 
     436        10377 :   if (ts)
     437              :     {
     438          501 :       if (e->symtree
     439          476 :           && e->symtree->n.sym->ts.type == BT_CLASS)
     440          476 :         *ts = &e->symtree->n.sym->ts;
     441              :       else
     442           25 :         *ts = NULL;
     443              :     }
     444              : 
     445        26115 :   for (ref = e->ref; ref; ref = ref->next)
     446              :     {
     447        16230 :       if (ts)
     448              :         {
     449         1176 :           if (ref->type == REF_COMPONENT
     450          562 :               && ref->u.c.component->ts.type == BT_CLASS
     451            0 :               && ref->next && ref->next->type == REF_COMPONENT
     452            0 :               && !strcmp (ref->next->u.c.component->name, "_data")
     453            0 :               && ref->next->next
     454            0 :               && ref->next->next->type == REF_ARRAY
     455            0 :               && ref->next->next->u.ar.type != AR_ELEMENT)
     456              :             {
     457            0 :               *ts = &ref->u.c.component->ts;
     458            0 :               class_ref = ref;
     459            0 :               break;
     460              :             }
     461              : 
     462         1176 :           if (ref->next == NULL)
     463              :             break;
     464              :         }
     465              :       else
     466              :         {
     467        15054 :           if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
     468        15054 :             array_ref = ref;
     469              : 
     470        15054 :           if (ref->type == REF_COMPONENT
     471         9076 :               && ref->u.c.component->ts.type == BT_CLASS)
     472              :             {
     473              :               /* Component to the right of a part reference with nonzero
     474              :                  rank must not have the ALLOCATABLE attribute.  If attempts
     475              :                  are made to reference such a component reference, an error
     476              :                  results followed by an ICE.  */
     477         1805 :               if (array_ref
     478           16 :                   && CLASS_DATA (ref->u.c.component)->attr.allocatable)
     479              :                 return NULL;
     480              :               class_ref = ref;
     481              :             }
     482              :         }
     483              :     }
     484              : 
     485        10361 :   if (ts && *ts == NULL)
     486              :     return NULL;
     487              : 
     488              :   /* Remove and store all subsequent references after the
     489              :      CLASS reference.  */
     490        10336 :   if (class_ref)
     491              :     {
     492         1585 :       tail = class_ref->next;
     493         1585 :       class_ref->next = NULL;
     494              :     }
     495         8751 :   else if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
     496              :     {
     497         8751 :       tail = e->ref;
     498         8751 :       e->ref = NULL;
     499              :     }
     500              : 
     501        10336 :   if (is_mold)
     502           61 :     base_expr = gfc_expr_to_initialize (e);
     503              :   else
     504        10275 :     base_expr = gfc_copy_expr (e);
     505              : 
     506              :   /* Restore the original tail expression.  */
     507        10336 :   if (class_ref)
     508              :     {
     509         1585 :       gfc_free_ref_list (class_ref->next);
     510         1585 :       class_ref->next = tail;
     511              :     }
     512         8751 :   else if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
     513              :     {
     514         8751 :       gfc_free_ref_list (e->ref);
     515         8751 :       e->ref = tail;
     516              :     }
     517              :   return base_expr;
     518              : }
     519              : 
     520              : /* Reset the vptr to the declared type, e.g. after deallocation.
     521              :    Use the variable in CLASS_CONTAINER if available.  Otherwise, recreate
     522              :    one with e or class_type.  At least one of the two has to be set.  The
     523              :    generated assignment code is added at the end of BLOCK.  */
     524              : 
     525              : void
     526        11973 : gfc_reset_vptr (stmtblock_t *block, gfc_expr *e, tree class_container,
     527              :                 gfc_symbol *class_type)
     528              : {
     529        11973 :   tree vptr = NULL_TREE;
     530              : 
     531        11973 :   if (class_container != NULL_TREE)
     532         7089 :     vptr = gfc_get_vptr_from_expr (class_container);
     533              : 
     534         7089 :   if (vptr == NULL_TREE)
     535              :     {
     536         4891 :       gfc_se se;
     537         4891 :       gcc_assert (e);
     538              : 
     539              :       /* Evaluate the expression and obtain the vptr from it.  */
     540         4891 :       gfc_init_se (&se, NULL);
     541         4891 :       if (e->rank)
     542         2428 :         gfc_conv_expr_descriptor (&se, e);
     543              :       else
     544         2463 :         gfc_conv_expr (&se, e);
     545         4891 :       gfc_add_block_to_block (block, &se.pre);
     546              : 
     547         4891 :       vptr = gfc_get_vptr_from_expr (se.expr);
     548              :     }
     549              : 
     550              :   /* If a vptr is not found, we can do nothing more.  */
     551         4891 :   if (vptr == NULL_TREE)
     552              :     return;
     553              : 
     554        11957 :   if (UNLIMITED_POLY (e)
     555        10861 :       || UNLIMITED_POLY (class_type)
     556              :       /* When the class_type's source is not a symbol (e.g. a component's ts),
     557              :          then look at the _data-components type.  */
     558         1721 :       || (class_type != NULL && class_type->ts.type == BT_UNKNOWN
     559         1721 :           && class_type->components && class_type->components->ts.u.derived
     560         1715 :           && class_type->components->ts.u.derived->attr.unlimited_polymorphic))
     561         1282 :     gfc_add_modify (block, vptr, build_int_cst (TREE_TYPE (vptr), 0));
     562              :   else
     563              :     {
     564        10675 :       gfc_symbol *vtab, *type = nullptr;
     565        10675 :       tree vtable;
     566              : 
     567        10675 :       if (e)
     568         9140 :         type = e->ts.u.derived;
     569         1535 :       else if (class_type)
     570              :         {
     571         1535 :           if (class_type->ts.type == BT_CLASS)
     572            0 :             type = CLASS_DATA (class_type)->ts.u.derived;
     573              :           else
     574              :             type = class_type;
     575              :         }
     576         9140 :       gcc_assert (type);
     577              :       /* Return the vptr to the address of the declared type.  */
     578        10675 :       vtab = gfc_find_derived_vtab (type);
     579        10675 :       vtable = vtab->backend_decl;
     580        10675 :       if (vtable == NULL_TREE)
     581          106 :         vtable = gfc_get_symbol_decl (vtab);
     582        10675 :       vtable = gfc_build_addr_expr (NULL, vtable);
     583        10675 :       vtable = fold_convert (TREE_TYPE (vptr), vtable);
     584        10675 :       gfc_add_modify (block, vptr, vtable);
     585              :     }
     586              : }
     587              : 
     588              : /* Set the vptr of a class in to from the type given in from.  If from is NULL,
     589              :    then reset the vptr to the default or to.  */
     590              : 
     591              : void
     592          234 : gfc_class_set_vptr (stmtblock_t *block, tree to, tree from)
     593              : {
     594          234 :   tree tmp, vptr_ref;
     595          234 :   gfc_symbol *type;
     596              : 
     597          234 :   vptr_ref = gfc_get_vptr_from_expr (to);
     598          276 :   if (POINTER_TYPE_P (TREE_TYPE (from))
     599          234 :       && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (from))))
     600              :     {
     601           44 :       gfc_add_modify (block, vptr_ref,
     602           22 :                       fold_convert (TREE_TYPE (vptr_ref),
     603              :                                     gfc_get_vptr_from_expr (from)));
     604          256 :       return;
     605              :     }
     606          212 :   tmp = gfc_get_vptr_from_expr (from);
     607          212 :   if (tmp)
     608              :     {
     609          170 :       gfc_add_modify (block, vptr_ref,
     610          170 :                       fold_convert (TREE_TYPE (vptr_ref), tmp));
     611          170 :       return;
     612              :     }
     613           42 :   if (VAR_P (from)
     614           42 :       && strncmp (IDENTIFIER_POINTER (DECL_NAME (from)), "__vtab", 6) == 0)
     615              :     {
     616           42 :       gfc_add_modify (block, vptr_ref,
     617           42 :                       gfc_build_addr_expr (TREE_TYPE (vptr_ref), from));
     618           42 :       return;
     619              :     }
     620            0 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (from)))
     621            0 :       && GFC_CLASS_TYPE_P (
     622              :         TREE_TYPE (TREE_OPERAND (TREE_OPERAND (from, 0), 0))))
     623              :     {
     624            0 :       gfc_add_modify (block, vptr_ref,
     625            0 :                       fold_convert (TREE_TYPE (vptr_ref),
     626              :                                     gfc_get_vptr_from_expr (TREE_OPERAND (
     627              :                                       TREE_OPERAND (from, 0), 0))));
     628            0 :       return;
     629              :     }
     630              : 
     631              :   /* If nothing of the above matches, set the vtype according to the type.  */
     632            0 :   tmp = TREE_TYPE (from);
     633            0 :   if (POINTER_TYPE_P (tmp))
     634            0 :     tmp = TREE_TYPE (tmp);
     635            0 :   gfc_find_symbol (IDENTIFIER_POINTER (TYPE_NAME (tmp)), gfc_current_ns, 1,
     636              :                    &type);
     637            0 :   tmp = gfc_find_derived_vtab (type)->backend_decl;
     638            0 :   gcc_assert (tmp);
     639            0 :   gfc_add_modify (block, vptr_ref,
     640            0 :                   gfc_build_addr_expr (TREE_TYPE (vptr_ref), tmp));
     641              : }
     642              : 
     643              : /* Reset the len for unlimited polymorphic objects.  */
     644              : 
     645              : void
     646          663 : gfc_reset_len (stmtblock_t *block, gfc_expr *expr)
     647              : {
     648          663 :   gfc_expr *e;
     649          663 :   gfc_se se_len;
     650          663 :   e = gfc_find_and_cut_at_last_class_ref (expr);
     651          663 :   if (e == NULL)
     652              :     return;
     653          663 :   gfc_add_len_component (e);
     654          663 :   gfc_init_se (&se_len, NULL);
     655          663 :   gfc_conv_expr (&se_len, e);
     656          663 :   gfc_add_modify (block, se_len.expr,
     657          663 :                   fold_convert (TREE_TYPE (se_len.expr), integer_zero_node));
     658          663 :   gfc_free_expr (e);
     659              : }
     660              : 
     661              : 
     662              : /* Obtain the last class reference in a gfc_expr. Return NULL_TREE if no class
     663              :    reference is found. Note that it is up to the caller to avoid using this
     664              :    for expressions other than variables.  */
     665              : 
     666              : tree
     667         1649 : gfc_get_class_from_gfc_expr (gfc_expr *e)
     668              : {
     669         1649 :   gfc_expr *class_expr;
     670         1649 :   gfc_se cse;
     671         1649 :   class_expr = gfc_find_and_cut_at_last_class_ref (e);
     672         1649 :   if (class_expr == NULL)
     673              :     return NULL_TREE;
     674         1649 :   gfc_init_se (&cse, NULL);
     675         1649 :   gfc_conv_expr (&cse, class_expr);
     676         1649 :   gfc_free_expr (class_expr);
     677         1649 :   return cse.expr;
     678              : }
     679              : 
     680              : 
     681              : /* Obtain the last class reference in an expression.
     682              :    Return NULL_TREE if no class reference is found.  */
     683              : 
     684              : tree
     685       112914 : gfc_get_class_from_expr (tree expr)
     686              : {
     687       112914 :   tree tmp;
     688       112914 :   tree type;
     689       112914 :   bool array_descr_found = false;
     690       112914 :   bool comp_after_descr_found = false;
     691              : 
     692       290493 :   for (tmp = expr; tmp; tmp = TREE_OPERAND (tmp, 0))
     693              :     {
     694       290493 :       if (CONSTANT_CLASS_P (tmp))
     695              :         return NULL_TREE;
     696              : 
     697       290456 :       type = TREE_TYPE (tmp);
     698       336626 :       while (type)
     699              :         {
     700       328598 :           if (GFC_CLASS_TYPE_P (type))
     701              :             return tmp;
     702       307384 :           if (GFC_DESCRIPTOR_TYPE_P (type))
     703        36713 :             array_descr_found = true;
     704       307384 :           if (type != TYPE_CANONICAL (type))
     705        46170 :             type = TYPE_CANONICAL (type);
     706              :           else
     707              :             type = NULL_TREE;
     708              :         }
     709       269242 :       if (VAR_P (tmp) || TREE_CODE (tmp) == PARM_DECL)
     710              :         break;
     711              : 
     712              :       /* Avoid walking up the reference chain too far.  For class arrays, the
     713              :          array descriptor is a direct component (through a pointer) of the class
     714              :          container.  So there is exactly one COMPONENT_REF between a class
     715              :          container and its child array descriptor.  After seeing an array
     716              :          descriptor, we can give up on the second COMPONENT_REF we see, if no
     717              :          class container was found until that point.  */
     718       177579 :       if (array_descr_found)
     719              :         {
     720         7927 :           if (comp_after_descr_found)
     721              :             {
     722           12 :               if (TREE_CODE (tmp) == COMPONENT_REF)
     723              :                 return NULL_TREE;
     724              :             }
     725         7915 :           else if (TREE_CODE (tmp) == COMPONENT_REF)
     726         7927 :             comp_after_descr_found = true;
     727              :         }
     728              :     }
     729              : 
     730        91663 :   if (POINTER_TYPE_P (TREE_TYPE (tmp)))
     731        61570 :     tmp = build_fold_indirect_ref_loc (input_location, tmp);
     732              : 
     733        91663 :   if (GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
     734           20 :     return tmp;
     735              : 
     736              :   return NULL_TREE;
     737              : }
     738              : 
     739              : 
     740              : /* Obtain the vptr of the last class reference in an expression.
     741              :    Return NULL_TREE if no class reference is found.  */
     742              : 
     743              : tree
     744        12640 : gfc_get_vptr_from_expr (tree expr)
     745              : {
     746        12640 :   tree tmp;
     747              : 
     748        12640 :   tmp = gfc_get_class_from_expr (expr);
     749              : 
     750        12640 :   if (tmp != NULL_TREE)
     751        12563 :     return gfc_class_vptr_get (tmp);
     752              : 
     753              :   return NULL_TREE;
     754              : }
     755              : 
     756              : void
     757         2002 : gfc_class_array_data_assign (stmtblock_t *block, tree lhs_desc, tree rhs_desc,
     758              :                              bool lhs_type)
     759              : {
     760         2002 :   tree lhs_dim, rhs_dim, type;
     761              : 
     762         2002 :   gfc_conv_descriptor_data_set (block, lhs_desc,
     763              :                                 gfc_conv_descriptor_data_get (rhs_desc));
     764         2002 :   gfc_conv_descriptor_offset_set (block, lhs_desc,
     765              :                                   gfc_conv_descriptor_offset_get (rhs_desc));
     766              : 
     767         2002 :   gfc_conv_descriptor_dtype_set (block, lhs_desc,
     768              :                                  gfc_conv_descriptor_dtype_get (rhs_desc));
     769         2002 :   gfc_conv_descriptor_span_set (block, lhs_desc,
     770              :                                 gfc_conv_descriptor_span_get (rhs_desc));
     771              : 
     772              :   /* Assign the dimension as range-ref.  */
     773         2002 :   lhs_dim = gfc_get_descriptor_dimension (lhs_desc);
     774         2002 :   rhs_dim = gfc_get_descriptor_dimension (rhs_desc);
     775              : 
     776         2002 :   type = lhs_type ? TREE_TYPE (lhs_dim) : TREE_TYPE (rhs_dim);
     777         2002 :   lhs_dim = build4_loc (input_location, ARRAY_RANGE_REF, type, lhs_dim,
     778              :                         gfc_index_zero_node, NULL_TREE, NULL_TREE);
     779         2002 :   rhs_dim = build4_loc (input_location, ARRAY_RANGE_REF, type, rhs_dim,
     780              :                         gfc_index_zero_node, NULL_TREE, NULL_TREE);
     781         2002 :   gfc_add_modify (block, lhs_dim, rhs_dim);
     782              : 
     783              :   /* The corank dimensions are not copied by the ARRAY_RANGE_REF.  */
     784         2002 :   gfc_copy_coarray_desc_part (block, lhs_desc, rhs_desc);
     785         2002 : }
     786              : 
     787              : /* Takes a derived type expression and returns the address of a temporary
     788              :    class object of the 'declared' type.  If opt_vptr_src is not NULL, this is
     789              :    used for the temporary class object.
     790              :    optional_alloc_ptr is false when the dummy is neither allocatable
     791              :    nor a pointer; that's only relevant for the optional handling.
     792              :    The optional argument 'derived_array' is used to preserve the parmse
     793              :    expression for deallocation of allocatable components. Assumed rank
     794              :    formal arguments made this necessary.  */
     795              : void
     796         5362 : gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym,
     797              :                            tree opt_vptr_src, bool optional,
     798              :                            bool optional_alloc_ptr, const char *proc_name,
     799              :                            tree *derived_array)
     800              : {
     801         5362 :   tree cond_optional = NULL_TREE;
     802         5362 :   gfc_ss *ss;
     803         5362 :   tree ctree;
     804         5362 :   tree var;
     805         5362 :   tree tmp;
     806         5362 :   tree packed = NULL_TREE;
     807              : 
     808              :   /* The derived type needs to be converted to a temporary CLASS object.  */
     809         5362 :   tmp = gfc_typenode_for_spec (&fsym->ts);
     810         5362 :   var = gfc_create_var (tmp, "class");
     811              : 
     812              :   /* Set the vptr.  */
     813         5362 :   if (opt_vptr_src)
     814          128 :     gfc_class_set_vptr (&parmse->pre, var, opt_vptr_src);
     815              :   else
     816         5234 :     gfc_reset_vptr (&parmse->pre, e, var);
     817              : 
     818              :   /* Now set the data field.  */
     819         5362 :   ctree = gfc_class_data_get (var);
     820              : 
     821         5362 :   if (flag_coarray == GFC_FCOARRAY_LIB && CLASS_DATA (fsym)->attr.codimension)
     822              :     {
     823            4 :       tree token;
     824            4 :       tmp = gfc_get_tree_for_caf_expr (e);
     825            4 :       if (POINTER_TYPE_P (TREE_TYPE (tmp)))
     826            2 :         tmp = build_fold_indirect_ref (tmp);
     827            4 :       gfc_get_caf_token_offset (parmse, &token, nullptr, tmp, NULL_TREE, e);
     828            4 :       gfc_conv_descriptor_token_set (&parmse->pre, ctree, token);
     829              :     }
     830              : 
     831         5362 :   if (optional)
     832          576 :     cond_optional = gfc_conv_expr_present (e->symtree->n.sym);
     833              : 
     834              :   /* Set the _len as early as possible.  */
     835         5362 :   if (fsym->ts.u.derived->components->ts.type == BT_DERIVED
     836         5362 :       && fsym->ts.u.derived->components->ts.u.derived->attr
     837         5362 :            .unlimited_polymorphic)
     838              :     {
     839              :       /* Take care about initializing the _len component correctly.  */
     840          392 :       tree len_tree = gfc_class_len_get (var);
     841          392 :       if (UNLIMITED_POLY (e))
     842              :         {
     843           12 :           gfc_expr *len;
     844           12 :           gfc_se se;
     845              : 
     846           12 :           len = gfc_find_and_cut_at_last_class_ref (e);
     847           12 :           gfc_add_len_component (len);
     848           12 :           gfc_init_se (&se, NULL);
     849           12 :           gfc_conv_expr (&se, len);
     850           12 :           if (optional)
     851            0 :             tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se.expr),
     852              :                               cond_optional, se.expr,
     853            0 :                               fold_convert (TREE_TYPE (se.expr),
     854              :                                             integer_zero_node));
     855              :           else
     856           12 :             tmp = se.expr;
     857           12 :           gfc_free_expr (len);
     858           12 :         }
     859              :       else
     860          380 :         tmp = integer_zero_node;
     861          392 :       gfc_add_modify (&parmse->pre, len_tree,
     862          392 :                       fold_convert (TREE_TYPE (len_tree), tmp));
     863              :     }
     864              : 
     865         5362 :   if (parmse->expr && POINTER_TYPE_P (TREE_TYPE (parmse->expr)))
     866              :     {
     867              :       /* If there is a ready made pointer to a derived type, use it
     868              :          rather than evaluating the expression again.  */
     869          535 :       tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
     870          535 :       gfc_add_modify (&parmse->pre, ctree, tmp);
     871              :     }
     872         4827 :   else if (parmse->ss && parmse->ss->info && parmse->ss->info->useflags)
     873              :     {
     874              :       /* For an array reference in an elemental procedure call we need
     875              :          to retain the ss to provide the scalarized array reference.  */
     876          445 :       gfc_conv_expr_reference (parmse, e);
     877          445 :       tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
     878          445 :       if (optional)
     879            0 :         tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
     880              :                           cond_optional, tmp,
     881            0 :                           fold_convert (TREE_TYPE (tmp), null_pointer_node));
     882          445 :       gfc_add_modify (&parmse->pre, ctree, tmp);
     883              :     }
     884              :   else
     885              :     {
     886         4382 :       ss = gfc_walk_expr (e);
     887         4382 :       if (ss == gfc_ss_terminator)
     888              :         {
     889         3091 :           parmse->ss = NULL;
     890         3091 :           gfc_conv_expr_reference (parmse, e);
     891              : 
     892              :           /* Scalar to an assumed-rank array.  */
     893         3091 :           if (fsym->ts.u.derived->components->as)
     894          334 :             gfc_set_descriptor_from_scalar (&parmse->pre, ctree,
     895              :                                             parmse->expr, e, cond_optional);
     896              :           else
     897              :             {
     898         2757 :               tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
     899         2757 :               if (optional)
     900          132 :                 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
     901              :                                   cond_optional, tmp,
     902          132 :                                   fold_convert (TREE_TYPE (tmp),
     903              :                                                 null_pointer_node));
     904         2757 :               gfc_add_modify (&parmse->pre, ctree, tmp);
     905              :             }
     906              :         }
     907              :       else
     908              :         {
     909         1291 :           stmtblock_t block;
     910         1291 :           gfc_init_block (&block);
     911         1291 :           gfc_ref *ref;
     912         1291 :           int dim;
     913         1291 :           tree lbshift = NULL_TREE;
     914              : 
     915              :           /* Array refs with sections indicate, that a for a formal argument
     916              :              expecting contiguous repacking needs to be done.  */
     917         2437 :           for (ref = e->ref; ref; ref = ref->next)
     918         1296 :             if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
     919              :               break;
     920         1291 :           if (IS_CLASS_ARRAY (fsym)
     921         1171 :               && (CLASS_DATA (fsym)->as->type == AS_EXPLICIT
     922          913 :                   || CLASS_DATA (fsym)->as->type == AS_ASSUMED_SIZE)
     923          354 :               && (ref || e->rank != fsym->ts.u.derived->components->as->rank))
     924          144 :             fsym->attr.contiguous = 1;
     925              : 
     926              :           /* Detect any array references with vector subscripts.  */
     927         2581 :           for (ref = e->ref; ref; ref = ref->next)
     928         1296 :             if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT
     929         1248 :                 && ref->u.ar.type != AR_FULL)
     930              :               {
     931          336 :                 for (dim = 0; dim < ref->u.ar.dimen; dim++)
     932          192 :                   if (ref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
     933              :                     break;
     934          150 :                 if (dim < ref->u.ar.dimen)
     935              :                   break;
     936              :               }
     937              :           /* Array references with vector subscripts and non-variable
     938              :              expressions need be converted to a one-based descriptor.  */
     939         1291 :           if (ref || e->expr_type != EXPR_VARIABLE)
     940           49 :             lbshift = gfc_index_one_node;
     941              : 
     942         1291 :           parmse->expr = var;
     943         1291 :           gfc_conv_array_parameter (parmse, e, false, fsym, proc_name, nullptr,
     944              :                                     &lbshift, &packed);
     945              : 
     946         1291 :           if (derived_array && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (parmse->expr)))
     947              :             {
     948         1195 :               *derived_array
     949         1195 :                 = gfc_create_var (TREE_TYPE (parmse->expr), "array");
     950         1195 :               if (e->rank == -1)
     951              :                 {
     952              :                   /* Assumed-rank actual: parmse->expr physically holds only
     953              :                      dtype.rank dims; a full struct assign reads past the end.
     954              :                      Copy field-by-field with a runtime-sized dim[] memcpy.
     955              :                      PR fortran/60576.  */
     956           78 :                   tree rank, dim_field, dim_size, copy_size, dst_ptr, src_ptr;
     957              : 
     958           78 :                   gfc_conv_descriptor_data_set
     959           78 :                     (&block, *derived_array,
     960              :                      gfc_conv_descriptor_data_get (parmse->expr));
     961           78 :                   gfc_conv_descriptor_offset_set
     962           78 :                     (&block, *derived_array,
     963              :                      gfc_conv_descriptor_offset_get (parmse->expr));
     964           78 :                   tree dtype_val = gfc_conv_descriptor_dtype_get (parmse->expr);
     965           78 :                   gfc_conv_descriptor_dtype_set (&block, *derived_array,
     966              :                                                  dtype_val);
     967           78 :                   rank = gfc_conv_descriptor_rank_get (parmse->expr);
     968           78 :                   rank = fold_convert (size_type_node, rank);
     969           78 :                   dim_field = gfc_get_descriptor_dimension (parmse->expr);
     970           78 :                   dim_size = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (dim_field)));
     971           78 :                   copy_size = fold_build2_loc (input_location, MULT_EXPR,
     972              :                                                size_type_node, rank, dim_size);
     973           78 :                   dst_ptr = gfc_build_addr_expr
     974           78 :                     (pvoid_type_node, gfc_get_descriptor_dimension (*derived_array));
     975           78 :                   src_ptr = gfc_build_addr_expr (pvoid_type_node, dim_field);
     976           78 :                   gfc_add_expr_to_block (&block,
     977              :                       build_call_expr_loc (input_location,
     978              :                                            builtin_decl_explicit (BUILT_IN_MEMCPY),
     979              :                                            3, dst_ptr, src_ptr, copy_size));
     980              :                 }
     981              :               else
     982         1117 :                 gfc_add_modify (&block, *derived_array, parmse->expr);
     983              :             }
     984              : 
     985         1291 :           if (optional)
     986              :             {
     987          348 :               tmp = gfc_finish_block (&block);
     988              : 
     989          348 :               gfc_init_block (&block);
     990          348 :               gfc_init_absent_descriptor (&block, ctree);
     991          348 :               if (derived_array && *derived_array != NULL_TREE)
     992          348 :                 gfc_init_absent_descriptor (&block, *derived_array);
     993              : 
     994          348 :               tmp = build3_v (COND_EXPR, cond_optional, tmp,
     995              :                               gfc_finish_block (&block));
     996          348 :               gfc_add_expr_to_block (&parmse->pre, tmp);
     997              :             }
     998              :           else
     999          943 :             gfc_add_block_to_block (&parmse->pre, &block);
    1000              :         }
    1001              :     }
    1002              : 
    1003              :   /* Pass the address of the class object.  */
    1004         5362 :   if (packed)
    1005              :     parmse->expr = packed;
    1006              :   else
    1007         5266 :     parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
    1008              : 
    1009         5362 :   if (optional && optional_alloc_ptr)
    1010           84 :     parmse->expr
    1011           84 :       = build3_loc (input_location, COND_EXPR, TREE_TYPE (parmse->expr),
    1012              :                     cond_optional, parmse->expr,
    1013           84 :                     fold_convert (TREE_TYPE (parmse->expr), null_pointer_node));
    1014         5362 : }
    1015              : 
    1016              : /* Create a new class container, which is required as scalar coarrays
    1017              :    have an array descriptor while normal scalars haven't. Optionally,
    1018              :    NULL pointer checks are added if the argument is OPTIONAL.  */
    1019              : 
    1020              : static void
    1021           48 : class_scalar_coarray_to_class (gfc_se *parmse, gfc_expr *e,
    1022              :                                gfc_typespec class_ts, bool optional)
    1023              : {
    1024           48 :   tree var, ctree, tmp;
    1025           48 :   stmtblock_t block;
    1026           48 :   gfc_ref *ref;
    1027           48 :   gfc_ref *class_ref;
    1028              : 
    1029           48 :   gfc_init_block (&block);
    1030              : 
    1031           48 :   class_ref = NULL;
    1032          144 :   for (ref = e->ref; ref; ref = ref->next)
    1033              :     {
    1034           96 :       if (ref->type == REF_COMPONENT
    1035           48 :             && ref->u.c.component->ts.type == BT_CLASS)
    1036           96 :         class_ref = ref;
    1037              :     }
    1038              : 
    1039           48 :   if (class_ref == NULL
    1040           48 :         && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
    1041           48 :     tmp = e->symtree->n.sym->backend_decl;
    1042              :   else
    1043              :     {
    1044              :       /* Remove everything after the last class reference, convert the
    1045              :          expression and then recover its tailend once more.  */
    1046            0 :       gfc_se tmpse;
    1047            0 :       ref = class_ref->next;
    1048            0 :       class_ref->next = NULL;
    1049            0 :       gfc_init_se (&tmpse, NULL);
    1050            0 :       gfc_conv_expr (&tmpse, e);
    1051            0 :       class_ref->next = ref;
    1052            0 :       tmp = tmpse.expr;
    1053              :     }
    1054              : 
    1055           48 :   var = gfc_typenode_for_spec (&class_ts);
    1056           48 :   var = gfc_create_var (var, "class");
    1057              : 
    1058           48 :   ctree = gfc_class_vptr_get (var);
    1059           96 :   gfc_add_modify (&block, ctree,
    1060           48 :                   fold_convert (TREE_TYPE (ctree), gfc_class_vptr_get (tmp)));
    1061              : 
    1062           48 :   ctree = gfc_class_data_get (var);
    1063           48 :   tmp = gfc_conv_descriptor_data_get (
    1064           48 :     gfc_class_data_get (GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (tmp)))
    1065              :                           ? tmp
    1066           24 :                           : GFC_DECL_SAVED_DESCRIPTOR (tmp)));
    1067           48 :   gfc_add_modify (&block, ctree, fold_convert (TREE_TYPE (ctree), tmp));
    1068              : 
    1069              :   /* Pass the address of the class object.  */
    1070           48 :   parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
    1071              : 
    1072           48 :   if (optional)
    1073              :     {
    1074           48 :       tree cond = gfc_conv_expr_present (e->symtree->n.sym);
    1075           48 :       tree tmp2;
    1076              : 
    1077           48 :       tmp = gfc_finish_block (&block);
    1078              : 
    1079           48 :       gfc_init_block (&block);
    1080           48 :       tmp2 = gfc_class_data_get (var);
    1081           48 :       gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
    1082              :                                                   null_pointer_node));
    1083           48 :       tmp2 = gfc_finish_block (&block);
    1084              : 
    1085           48 :       tmp = build3_loc (input_location, COND_EXPR, void_type_node,
    1086              :                         cond, tmp, tmp2);
    1087           48 :       gfc_add_expr_to_block (&parmse->pre, tmp);
    1088              :     }
    1089              :   else
    1090            0 :     gfc_add_block_to_block (&parmse->pre, &block);
    1091           48 : }
    1092              : 
    1093              : 
    1094              : /* Takes an intrinsic type expression and returns the address of a temporary
    1095              :    class object of the 'declared' type.  */
    1096              : void
    1097          930 : gfc_conv_intrinsic_to_class (gfc_se *parmse, gfc_expr *e,
    1098              :                              gfc_typespec class_ts)
    1099              : {
    1100          930 :   gfc_symbol *vtab;
    1101          930 :   gfc_ss *ss;
    1102          930 :   tree ctree;
    1103          930 :   tree var;
    1104          930 :   tree tmp;
    1105          930 :   int dim;
    1106          930 :   bool unlimited_poly;
    1107              : 
    1108         1860 :   unlimited_poly = class_ts.type == BT_CLASS
    1109          930 :                    && class_ts.u.derived->components->ts.type == BT_DERIVED
    1110          930 :                    && class_ts.u.derived->components->ts.u.derived
    1111          930 :                                                 ->attr.unlimited_polymorphic;
    1112              : 
    1113              :   /* The intrinsic type needs to be converted to a temporary
    1114              :      CLASS object.  */
    1115          930 :   tmp = gfc_typenode_for_spec (&class_ts);
    1116          930 :   var = gfc_create_var (tmp, "class");
    1117              : 
    1118              :   /* Force a temporary for component or substring references.  */
    1119          930 :   if (unlimited_poly
    1120          930 :       && class_ts.u.derived->components->attr.dimension
    1121          671 :       && !class_ts.u.derived->components->attr.allocatable
    1122          671 :       && !class_ts.u.derived->components->attr.class_pointer
    1123         1601 :       && is_subref_array (e))
    1124           17 :     parmse->force_tmp = 1;
    1125              : 
    1126              :   /* Set the vptr.  */
    1127          930 :   ctree = gfc_class_vptr_get (var);
    1128              : 
    1129          930 :   vtab = gfc_find_vtab (&e->ts);
    1130          930 :   gcc_assert (vtab);
    1131          930 :   tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
    1132          930 :   gfc_add_modify (&parmse->pre, ctree,
    1133          930 :                   fold_convert (TREE_TYPE (ctree), tmp));
    1134              : 
    1135              :   /* Now set the data field.  */
    1136          930 :   ctree = gfc_class_data_get (var);
    1137          930 :   if (parmse->ss && parmse->ss->info->useflags)
    1138              :     {
    1139              :       /* For an array reference in an elemental procedure call we need
    1140              :          to retain the ss to provide the scalarized array reference.  */
    1141           36 :       gfc_conv_expr_reference (parmse, e);
    1142           36 :       tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
    1143           36 :       gfc_add_modify (&parmse->pre, ctree, tmp);
    1144              :     }
    1145              :   else
    1146              :     {
    1147          894 :       ss = gfc_walk_expr (e);
    1148          894 :       if (ss == gfc_ss_terminator)
    1149              :         {
    1150          247 :           parmse->ss = NULL;
    1151          247 :           gfc_conv_expr_reference (parmse, e);
    1152          247 :           if (class_ts.u.derived->components->as
    1153           24 :               && class_ts.u.derived->components->as->type == AS_ASSUMED_RANK)
    1154              :             {
    1155           24 :               tmp = gfc_conv_scalar_to_descriptor (parmse, parmse->expr,
    1156              :                                                    gfc_expr_attr (e));
    1157           24 :               tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
    1158           24 :                                      TREE_TYPE (ctree), tmp);
    1159              :             }
    1160              :           else
    1161          223 :               tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
    1162          247 :           gfc_add_modify (&parmse->pre, ctree, tmp);
    1163              :         }
    1164              :       else
    1165              :         {
    1166          647 :           parmse->ss = ss;
    1167          647 :           gfc_conv_expr_descriptor (parmse, e);
    1168              : 
    1169              :           /* Array references with vector subscripts and non-variable expressions
    1170              :              need be converted to a one-based descriptor.  */
    1171          647 :           if (e->expr_type != EXPR_VARIABLE)
    1172              :             {
    1173          416 :               for (dim = 0; dim < e->rank; ++dim)
    1174          217 :                 gfc_conv_shift_descriptor_lbound (&parmse->pre, parmse->expr,
    1175              :                                                   dim, gfc_index_one_node);
    1176              :             }
    1177              : 
    1178          647 :           if (class_ts.u.derived->components->as->rank != e->rank)
    1179              :             {
    1180           49 :               tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
    1181           49 :                                      TREE_TYPE (ctree), parmse->expr);
    1182           49 :               gfc_add_modify (&parmse->pre, ctree, tmp);
    1183              :             }
    1184              :           else
    1185          598 :             gfc_add_modify (&parmse->pre, ctree, parmse->expr);
    1186              :         }
    1187              :     }
    1188              : 
    1189          930 :   gcc_assert (class_ts.type == BT_CLASS);
    1190          930 :   if (unlimited_poly)
    1191              :     {
    1192          930 :       ctree = gfc_class_len_get (var);
    1193              :       /* When the actual arg is a char array, then set the _len component of the
    1194              :          unlimited polymorphic entity to the length of the string.  */
    1195          930 :       if (e->ts.type == BT_CHARACTER)
    1196              :         {
    1197              :           /* Start with parmse->string_length because this seems to be set to a
    1198              :            correct value more often.  */
    1199          175 :           if (parmse->string_length)
    1200              :             tmp = parmse->string_length;
    1201              :           /* When the string_length is not yet set, then try the backend_decl of
    1202              :            the cl.  */
    1203            0 :           else if (e->ts.u.cl->backend_decl)
    1204              :             tmp = e->ts.u.cl->backend_decl;
    1205              :           /* If both of the above approaches fail, then try to generate an
    1206              :            expression from the input, which is only feasible currently, when the
    1207              :            expression can be evaluated to a constant one.  */
    1208              :           else
    1209              :             {
    1210              :               /* Try to simplify the expression.  */
    1211            0 :               gfc_simplify_expr (e, 0);
    1212            0 :               if (e->expr_type == EXPR_CONSTANT && !e->ts.u.cl->resolved)
    1213              :                 {
    1214              :                   /* Amazingly all data is present to compute the length of a
    1215              :                    constant string, but the expression is not yet there.  */
    1216            0 :                   e->ts.u.cl->length = gfc_get_constant_expr (BT_INTEGER,
    1217              :                                                               gfc_charlen_int_kind,
    1218              :                                                               &e->where);
    1219            0 :                   mpz_set_ui (e->ts.u.cl->length->value.integer,
    1220            0 :                               e->value.character.length);
    1221            0 :                   gfc_conv_const_charlen (e->ts.u.cl);
    1222            0 :                   e->ts.u.cl->resolved = 1;
    1223            0 :                   tmp = e->ts.u.cl->backend_decl;
    1224              :                 }
    1225              :               else
    1226              :                 {
    1227            0 :                   gfc_error ("Cannot compute the length of the char array "
    1228              :                              "at %L.", &e->where);
    1229              :                 }
    1230              :             }
    1231              :         }
    1232              :       else
    1233          755 :         tmp = integer_zero_node;
    1234              : 
    1235          930 :       gfc_add_modify (&parmse->pre, ctree, fold_convert (TREE_TYPE (ctree), tmp));
    1236              :     }
    1237              : 
    1238              :   /* Pass the address of the class object.  */
    1239          930 :   parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
    1240          930 : }
    1241              : 
    1242              : 
    1243              : /* Takes a scalarized class array expression and returns the
    1244              :    address of a temporary scalar class object of the 'declared'
    1245              :    type.
    1246              :    OOP-TODO: This could be improved by adding code that branched on
    1247              :    the dynamic type being the same as the declared type. In this case
    1248              :    the original class expression can be passed directly.
    1249              :    optional_alloc_ptr is false when the dummy is neither allocatable
    1250              :    nor a pointer; that's relevant for the optional handling.
    1251              :    Set copyback to true if class container's _data and _vtab pointers
    1252              :    might get modified.  */
    1253              : 
    1254              : void
    1255         3818 : gfc_conv_class_to_class (gfc_se *parmse, gfc_expr *e, gfc_typespec class_ts,
    1256              :                          bool elemental, bool copyback, bool optional,
    1257              :                          bool optional_alloc_ptr)
    1258              : {
    1259         3818 :   tree ctree;
    1260         3818 :   tree var;
    1261         3818 :   tree tmp;
    1262         3818 :   tree vptr;
    1263         3818 :   tree cond = NULL_TREE;
    1264         3818 :   tree slen = NULL_TREE;
    1265         3818 :   gfc_ref *ref;
    1266         3818 :   gfc_ref *class_ref;
    1267         3818 :   stmtblock_t block;
    1268         3818 :   bool full_array = false;
    1269              : 
    1270              :   /* If this is the data field of a class temporary, the class expression
    1271              :      can be obtained and returned directly.  */
    1272         3818 :   if (e->expr_type != EXPR_VARIABLE
    1273          198 :       && TREE_CODE (parmse->expr) == COMPONENT_REF
    1274           54 :       && !GFC_CLASS_TYPE_P (TREE_TYPE (parmse->expr))
    1275         3872 :       && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (parmse->expr, 0))))
    1276              :     {
    1277           54 :       parmse->expr = TREE_OPERAND (parmse->expr, 0);
    1278           54 :       if (!VAR_P (parmse->expr))
    1279            0 :         parmse->expr = gfc_evaluate_now (parmse->expr, &parmse->pre);
    1280           54 :       parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
    1281          192 :       return;
    1282              :     }
    1283              : 
    1284         3764 :   gfc_init_block (&block);
    1285              : 
    1286         3764 :   class_ref = NULL;
    1287         7703 :   for (ref = e->ref; ref; ref = ref->next)
    1288              :     {
    1289         7327 :       if (ref->type == REF_COMPONENT
    1290         3948 :             && ref->u.c.component->ts.type == BT_CLASS)
    1291         7327 :         class_ref = ref;
    1292              : 
    1293         7327 :       if (ref->next == NULL)
    1294              :         break;
    1295              :     }
    1296              : 
    1297         3764 :   if ((ref == NULL || class_ref == ref)
    1298          488 :       && !(gfc_is_class_array_function (e) && parmse->class_vptr != NULL_TREE)
    1299         4234 :       && (!class_ts.u.derived->components->as
    1300          379 :           || class_ts.u.derived->components->as->rank != -1))
    1301              :     return;
    1302              : 
    1303              :   /* Test for FULL_ARRAY.  */
    1304         3626 :   if (e->rank == 0
    1305         3982 :       && ((gfc_expr_attr (e).codimension && gfc_expr_attr (e).dimension)
    1306          512 :           || (class_ts.u.derived->components->as
    1307          366 :               && class_ts.u.derived->components->as->type == AS_ASSUMED_RANK)))
    1308          411 :     full_array = true;
    1309              :   else
    1310         3215 :     gfc_is_class_array_ref (e, &full_array);
    1311              : 
    1312              :   /* The derived type needs to be converted to a temporary
    1313              :      CLASS object.  */
    1314         3626 :   tmp = gfc_typenode_for_spec (&class_ts);
    1315         3626 :   var = gfc_create_var (tmp, "class");
    1316              : 
    1317              :   /* Set the data.  */
    1318         3626 :   ctree = gfc_class_data_get (var);
    1319         3626 :   if (class_ts.u.derived->components->as
    1320         3318 :       && e->rank != class_ts.u.derived->components->as->rank)
    1321              :     {
    1322          977 :       if (e->rank == 0)
    1323          356 :         gfc_set_descriptor_from_scalar_class (&block, ctree, parmse->expr, e);
    1324              :       else
    1325          621 :         gfc_class_array_data_assign (&block, ctree, parmse->expr, false);
    1326              :     }
    1327              :   else
    1328              :     {
    1329         2649 :       if (TREE_TYPE (parmse->expr) != TREE_TYPE (ctree))
    1330         1542 :         parmse->expr = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
    1331         1542 :                                         TREE_TYPE (ctree), parmse->expr);
    1332         2649 :       gfc_add_modify (&block, ctree, parmse->expr);
    1333              :     }
    1334              : 
    1335              :   /* Return the data component, except in the case of scalarized array
    1336              :      references, where nullification of the cannot occur and so there
    1337              :      is no need.  */
    1338         3626 :   if (!elemental && full_array && copyback)
    1339              :     {
    1340         1214 :       if (class_ts.u.derived->components->as
    1341         1214 :           && e->rank != class_ts.u.derived->components->as->rank)
    1342              :         {
    1343          270 :           if (e->rank == 0)
    1344              :             {
    1345          102 :               tmp = gfc_class_data_get (parmse->expr);
    1346          204 :               gfc_add_modify (&parmse->post, tmp,
    1347          102 :                               fold_convert (TREE_TYPE (tmp),
    1348              :                                          gfc_conv_descriptor_data_get (ctree)));
    1349              :             }
    1350              :           else
    1351          168 :             gfc_class_array_data_assign (&parmse->post, parmse->expr, ctree,
    1352              :                                          true);
    1353              :         }
    1354              :       else
    1355          944 :         gfc_add_modify (&parmse->post, parmse->expr, ctree);
    1356              :     }
    1357              : 
    1358              :   /* Set the vptr.  */
    1359         3626 :   ctree = gfc_class_vptr_get (var);
    1360              : 
    1361              :   /* The vptr is the second field of the actual argument.
    1362              :      First we have to find the corresponding class reference.  */
    1363              : 
    1364         3626 :   tmp = NULL_TREE;
    1365         3626 :   if (gfc_is_class_array_function (e)
    1366         3626 :       && parmse->class_vptr != NULL_TREE)
    1367              :     tmp = parmse->class_vptr;
    1368         3608 :   else if (class_ref == NULL
    1369         3067 :            && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
    1370              :     {
    1371         3067 :       tmp = e->symtree->n.sym->backend_decl;
    1372              : 
    1373         3067 :       if (TREE_CODE (tmp) == FUNCTION_DECL)
    1374            6 :         tmp = gfc_get_fake_result_decl (e->symtree->n.sym, 0);
    1375              : 
    1376         3067 :       if (DECL_LANG_SPECIFIC (tmp) && GFC_DECL_SAVED_DESCRIPTOR (tmp))
    1377          398 :         tmp = GFC_DECL_SAVED_DESCRIPTOR (tmp);
    1378              : 
    1379         3067 :       slen = build_zero_cst (size_type_node);
    1380              :     }
    1381          541 :   else if (parmse->class_container != NULL_TREE)
    1382              :     /* Don't redundantly evaluate the expression if the required information
    1383              :        is already available.  */
    1384              :     tmp = parmse->class_container;
    1385              :   else
    1386              :     {
    1387              :       /* Remove everything after the last class reference, convert the
    1388              :          expression and then recover its tailend once more.  */
    1389           24 :       gfc_se tmpse;
    1390           24 :       ref = class_ref->next;
    1391           24 :       class_ref->next = NULL;
    1392           24 :       gfc_init_se (&tmpse, NULL);
    1393           24 :       gfc_conv_expr (&tmpse, e);
    1394           24 :       class_ref->next = ref;
    1395           24 :       tmp = tmpse.expr;
    1396           24 :       slen = tmpse.string_length;
    1397              :     }
    1398              : 
    1399         3626 :   gcc_assert (tmp != NULL_TREE);
    1400              : 
    1401              :   /* Dereference if needs be.  */
    1402         3626 :   if (TREE_CODE (TREE_TYPE (tmp)) == REFERENCE_TYPE)
    1403          352 :     tmp = build_fold_indirect_ref_loc (input_location, tmp);
    1404              : 
    1405         3626 :   if (!(gfc_is_class_array_function (e) && parmse->class_vptr))
    1406         3608 :     vptr = gfc_class_vptr_get (tmp);
    1407              :   else
    1408              :     vptr = tmp;
    1409              : 
    1410         3626 :   gfc_add_modify (&block, ctree,
    1411         3626 :                   fold_convert (TREE_TYPE (ctree), vptr));
    1412              : 
    1413              :   /* Return the vptr component, except in the case of scalarized array
    1414              :      references, where the dynamic type cannot change.  */
    1415         3626 :   if (!elemental && full_array && copyback)
    1416         1214 :     gfc_add_modify (&parmse->post, vptr,
    1417         1214 :                     fold_convert (TREE_TYPE (vptr), ctree));
    1418              : 
    1419              :   /* For unlimited polymorphic objects also set the _len component.  */
    1420         3626 :   if (class_ts.type == BT_CLASS
    1421         3626 :       && class_ts.u.derived->components
    1422         3626 :       && class_ts.u.derived->components->ts.u
    1423         3626 :                       .derived->attr.unlimited_polymorphic)
    1424              :     {
    1425         1236 :       ctree = gfc_class_len_get (var);
    1426         1236 :       if (UNLIMITED_POLY (e))
    1427         1033 :         tmp = gfc_class_len_get (tmp);
    1428          203 :       else if (e->ts.type == BT_CHARACTER)
    1429              :         {
    1430            0 :           gcc_assert (slen != NULL_TREE);
    1431              :           tmp = slen;
    1432              :         }
    1433              :       else
    1434          203 :         tmp = build_zero_cst (size_type_node);
    1435         1236 :       gfc_add_modify (&parmse->pre, ctree,
    1436         1236 :                       fold_convert (TREE_TYPE (ctree), tmp));
    1437              : 
    1438              :       /* Return the len component, except in the case of scalarized array
    1439              :         references, where the dynamic type cannot change.  */
    1440         1236 :       if (!elemental && full_array && copyback
    1441          477 :           && (UNLIMITED_POLY (e) || VAR_P (tmp)))
    1442          464 :           gfc_add_modify (&parmse->post, tmp,
    1443          464 :                           fold_convert (TREE_TYPE (tmp), ctree));
    1444              :     }
    1445              : 
    1446         3626 :   if (optional)
    1447              :     {
    1448          510 :       tree tmp2;
    1449              : 
    1450          510 :       cond = gfc_conv_expr_present (e->symtree->n.sym);
    1451              :       /* parmse->pre may contain some preparatory instructions for the
    1452              :          temporary array descriptor.  Those may only be executed when the
    1453              :          optional argument is set, therefore add parmse->pre's instructions
    1454              :          to block, which is later guarded by an if (optional_arg_given).  */
    1455          510 :       gfc_add_block_to_block (&parmse->pre, &block);
    1456          510 :       block.head = parmse->pre.head;
    1457          510 :       parmse->pre.head = NULL_TREE;
    1458          510 :       tmp = gfc_finish_block (&block);
    1459              : 
    1460          510 :       if (optional_alloc_ptr)
    1461          102 :         tmp2 = build_empty_stmt (input_location);
    1462              :       else
    1463              :         {
    1464          408 :           gfc_init_block (&block);
    1465          408 :           gfc_conv_descriptor_data_set (&block, gfc_class_data_get (var),
    1466              :                                         null_pointer_node);
    1467          408 :           tmp2 = gfc_finish_block (&block);
    1468              :         }
    1469              : 
    1470          510 :       tmp = build3_loc (input_location, COND_EXPR, void_type_node,
    1471              :                         cond, tmp, tmp2);
    1472          510 :       gfc_add_expr_to_block (&parmse->pre, tmp);
    1473              : 
    1474          510 :       if (!elemental && full_array && copyback)
    1475              :         {
    1476           30 :           tmp2 = build_empty_stmt (input_location);
    1477           30 :           tmp = gfc_finish_block (&parmse->post);
    1478           30 :           tmp = build3_loc (input_location, COND_EXPR, void_type_node,
    1479              :                             cond, tmp, tmp2);
    1480           30 :           gfc_add_expr_to_block (&parmse->post, tmp);
    1481              :         }
    1482              :     }
    1483              :   else
    1484         3116 :     gfc_add_block_to_block (&parmse->pre, &block);
    1485              : 
    1486              :   /* Pass the address of the class object.  */
    1487         3626 :   parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
    1488              : 
    1489         3626 :   if (optional && optional_alloc_ptr)
    1490          204 :     parmse->expr = build3_loc (input_location, COND_EXPR,
    1491          102 :                                TREE_TYPE (parmse->expr),
    1492              :                                cond, parmse->expr,
    1493          102 :                                fold_convert (TREE_TYPE (parmse->expr),
    1494              :                                              null_pointer_node));
    1495              : }
    1496              : 
    1497              : 
    1498              : /* Given a class array declaration and an index, returns the address
    1499              :    of the referenced element.  */
    1500              : 
    1501              : static tree
    1502          880 : gfc_get_class_array_ref (tree index, tree class_decl, tree data_comp,
    1503              :                          bool unlimited)
    1504              : {
    1505          880 :   tree data, size, tmp, ctmp, offset, ptr;
    1506              : 
    1507          880 :   data = data_comp != NULL_TREE ? data_comp :
    1508            0 :                                   gfc_class_data_get (class_decl);
    1509          880 :   size = gfc_class_vtab_size_get (class_decl);
    1510              : 
    1511          880 :   if (unlimited)
    1512              :     {
    1513          244 :       tmp = fold_convert (gfc_array_index_type,
    1514              :                           gfc_class_len_get (class_decl));
    1515          244 :       ctmp = fold_build2_loc (input_location, MULT_EXPR,
    1516              :                               gfc_array_index_type, size, tmp);
    1517          244 :       tmp = fold_build2_loc (input_location, GT_EXPR,
    1518              :                              logical_type_node, tmp,
    1519          244 :                              build_zero_cst (TREE_TYPE (tmp)));
    1520          244 :       size = fold_build3_loc (input_location, COND_EXPR,
    1521              :                               gfc_array_index_type, tmp, ctmp, size);
    1522              :     }
    1523              : 
    1524          880 :   offset = fold_build2_loc (input_location, MULT_EXPR,
    1525              :                             gfc_array_index_type,
    1526              :                             index, size);
    1527              : 
    1528          880 :   data = gfc_conv_descriptor_data_get (data);
    1529          880 :   ptr = fold_convert (pvoid_type_node, data);
    1530          880 :   ptr = fold_build_pointer_plus_loc (input_location, ptr, offset);
    1531          880 :   return fold_convert (TREE_TYPE (data), ptr);
    1532              : }
    1533              : 
    1534              : 
    1535              : /* Copies one class expression to another, assuming that if either
    1536              :    'to' or 'from' are arrays they are packed.  Should 'from' be
    1537              :    NULL_TREE, the initialization expression for 'to' is used, assuming
    1538              :    that the _vptr is set.  */
    1539              : 
    1540              : tree
    1541          896 : gfc_copy_class_to_class (tree from, tree to, tree nelems, bool unlimited)
    1542              : {
    1543          896 :   tree fcn;
    1544          896 :   tree fcn_type;
    1545          896 :   tree from_data;
    1546          896 :   tree from_len;
    1547          896 :   tree to_data;
    1548          896 :   tree to_len;
    1549          896 :   tree to_ref;
    1550          896 :   tree from_ref;
    1551          896 :   vec<tree, va_gc> *args;
    1552          896 :   tree tmp;
    1553          896 :   tree stdcopy;
    1554          896 :   tree extcopy;
    1555          896 :   tree index;
    1556          896 :   bool is_from_desc = false, is_to_class = false;
    1557              : 
    1558          896 :   args = NULL;
    1559              :   /* To prevent warnings on uninitialized variables.  */
    1560          896 :   from_len = to_len = NULL_TREE;
    1561              : 
    1562          896 :   if (from != NULL_TREE)
    1563          896 :     fcn = gfc_class_vtab_copy_get (from);
    1564              :   else
    1565            0 :     fcn = gfc_class_vtab_copy_get (to);
    1566              : 
    1567          896 :   fcn_type = TREE_TYPE (TREE_TYPE (fcn));
    1568              : 
    1569          896 :   if (from != NULL_TREE)
    1570              :     {
    1571          896 :       is_from_desc = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from));
    1572          896 :       if (is_from_desc)
    1573              :         {
    1574            0 :           from_data = from;
    1575            0 :           from = GFC_DECL_SAVED_DESCRIPTOR (from);
    1576              :         }
    1577              :       else
    1578              :         {
    1579              :           /* Check that from is a class.  When the class is part of a coarray,
    1580              :              then from is a common pointer and is to be used as is.  */
    1581         1792 :           tmp = POINTER_TYPE_P (TREE_TYPE (from))
    1582          896 :               ? build_fold_indirect_ref (from) : from;
    1583         1792 :           from_data =
    1584          896 :               (GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
    1585            0 :                || (DECL_P (tmp) && GFC_DECL_CLASS (tmp)))
    1586          896 :               ? gfc_class_data_get (from) : from;
    1587          896 :           is_from_desc = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data));
    1588              :         }
    1589              :      }
    1590              :   else
    1591            0 :     from_data = gfc_class_vtab_def_init_get (to);
    1592              : 
    1593          896 :   if (unlimited)
    1594              :     {
    1595          182 :       if (from != NULL_TREE && unlimited)
    1596          182 :         from_len = gfc_class_len_or_zero_get (from);
    1597              :       else
    1598            0 :         from_len = build_zero_cst (size_type_node);
    1599              :     }
    1600              : 
    1601          896 :   if (GFC_CLASS_TYPE_P (TREE_TYPE (to)))
    1602              :     {
    1603          896 :       is_to_class = true;
    1604          896 :       to_data = gfc_class_data_get (to);
    1605          896 :       if (unlimited)
    1606          182 :         to_len = gfc_class_len_get (to);
    1607              :     }
    1608              :   else
    1609              :     /* When to is a BT_DERIVED and not a BT_CLASS, then to_data == to.  */
    1610            0 :     to_data = to;
    1611              : 
    1612          896 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (to_data)))
    1613              :     {
    1614          440 :       stmtblock_t loopbody;
    1615          440 :       stmtblock_t body;
    1616          440 :       stmtblock_t ifbody;
    1617          440 :       gfc_loopinfo loop;
    1618              : 
    1619          440 :       gfc_init_block (&body);
    1620          440 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
    1621              :                              gfc_array_index_type, nelems,
    1622              :                              gfc_index_one_node);
    1623          440 :       nelems = gfc_evaluate_now (tmp, &body);
    1624          440 :       index = gfc_create_var (gfc_array_index_type, "S");
    1625              : 
    1626          440 :       if (is_from_desc)
    1627              :         {
    1628          440 :           from_ref = gfc_get_class_array_ref (index, from, from_data,
    1629              :                                               unlimited);
    1630          440 :           vec_safe_push (args, from_ref);
    1631              :         }
    1632              :       else
    1633            0 :         vec_safe_push (args, from_data);
    1634              : 
    1635          440 :       if (is_to_class)
    1636          440 :         to_ref = gfc_get_class_array_ref (index, to, to_data, unlimited);
    1637              :       else
    1638              :         {
    1639            0 :           tmp = gfc_conv_array_data (to);
    1640            0 :           tmp = build_fold_indirect_ref_loc (input_location, tmp);
    1641            0 :           to_ref = gfc_build_addr_expr (NULL_TREE,
    1642              :                                         gfc_build_array_ref (tmp, index, to));
    1643              :         }
    1644          440 :       vec_safe_push (args, to_ref);
    1645              : 
    1646              :       /* Add bounds check.  */
    1647          440 :       if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) > 0 && is_from_desc)
    1648              :         {
    1649           25 :           const char *name = "<<unknown>>";
    1650           25 :           int dim, rank;
    1651              : 
    1652           25 :           if (DECL_P (to))
    1653            0 :             name = IDENTIFIER_POINTER (DECL_NAME (to));
    1654              : 
    1655           25 :           rank = GFC_TYPE_ARRAY_RANK (TREE_TYPE (from_data));
    1656           55 :           for (dim = 1; dim <= rank; dim++)
    1657              :             {
    1658           30 :               tree from_len, to_len, cond;
    1659           30 :               char *msg;
    1660              : 
    1661           30 :               from_len = gfc_conv_descriptor_size (from_data, dim);
    1662           30 :               from_len = fold_convert (long_integer_type_node, from_len);
    1663           30 :               to_len = gfc_conv_descriptor_size (to_data, dim);
    1664           30 :               to_len = fold_convert (long_integer_type_node, to_len);
    1665           30 :               msg = xasprintf ("Array bound mismatch for dimension %d "
    1666              :                                "of array '%s' (%%ld/%%ld)",
    1667              :                                dim, name);
    1668           30 :               cond = fold_build2_loc (input_location, NE_EXPR,
    1669              :                                       logical_type_node, from_len, to_len);
    1670           30 :               gfc_trans_runtime_check (true, false, cond, &body,
    1671              :                                        NULL, msg, to_len, from_len);
    1672           30 :               free (msg);
    1673              :             }
    1674              :         }
    1675              : 
    1676          440 :       tmp = build_call_vec (fcn_type, fcn, args);
    1677              : 
    1678              :       /* Build the body of the loop.  */
    1679          440 :       gfc_init_block (&loopbody);
    1680          440 :       gfc_add_expr_to_block (&loopbody, tmp);
    1681              : 
    1682              :       /* Build the loop and return.  */
    1683          440 :       gfc_init_loopinfo (&loop);
    1684          440 :       loop.dimen = 1;
    1685          440 :       loop.from[0] = gfc_index_zero_node;
    1686          440 :       loop.loopvar[0] = index;
    1687          440 :       loop.to[0] = nelems;
    1688          440 :       gfc_trans_scalarizing_loops (&loop, &loopbody);
    1689          440 :       gfc_init_block (&ifbody);
    1690          440 :       gfc_add_block_to_block (&ifbody, &loop.pre);
    1691          440 :       stdcopy = gfc_finish_block (&ifbody);
    1692              :       /* In initialization mode from_len is a constant zero.  */
    1693          440 :       if (unlimited && !integer_zerop (from_len))
    1694              :         {
    1695          122 :           vec_safe_push (args, from_len);
    1696          122 :           vec_safe_push (args, to_len);
    1697          122 :           tmp = build_call_vec (fcn_type, fcn, args);
    1698              :           /* Build the body of the loop.  */
    1699          122 :           gfc_init_block (&loopbody);
    1700          122 :           gfc_add_expr_to_block (&loopbody, tmp);
    1701              : 
    1702              :           /* Build the loop and return.  */
    1703          122 :           gfc_init_loopinfo (&loop);
    1704          122 :           loop.dimen = 1;
    1705          122 :           loop.from[0] = gfc_index_zero_node;
    1706          122 :           loop.loopvar[0] = index;
    1707          122 :           loop.to[0] = nelems;
    1708          122 :           gfc_trans_scalarizing_loops (&loop, &loopbody);
    1709          122 :           gfc_init_block (&ifbody);
    1710          122 :           gfc_add_block_to_block (&ifbody, &loop.pre);
    1711          122 :           extcopy = gfc_finish_block (&ifbody);
    1712              : 
    1713          122 :           tmp = fold_build2_loc (input_location, GT_EXPR,
    1714              :                                  logical_type_node, from_len,
    1715          122 :                                  build_zero_cst (TREE_TYPE (from_len)));
    1716          122 :           tmp = fold_build3_loc (input_location, COND_EXPR,
    1717              :                                  void_type_node, tmp, extcopy, stdcopy);
    1718          122 :           gfc_add_expr_to_block (&body, tmp);
    1719          122 :           tmp = gfc_finish_block (&body);
    1720              :         }
    1721              :       else
    1722              :         {
    1723          318 :           gfc_add_expr_to_block (&body, stdcopy);
    1724          318 :           tmp = gfc_finish_block (&body);
    1725              :         }
    1726          440 :       gfc_cleanup_loop (&loop);
    1727              :     }
    1728              :   else
    1729              :     {
    1730          456 :       gcc_assert (!is_from_desc);
    1731          456 :       vec_safe_push (args, from_data);
    1732          456 :       vec_safe_push (args, to_data);
    1733          456 :       stdcopy = build_call_vec (fcn_type, fcn, args);
    1734              : 
    1735              :       /* In initialization mode from_len is a constant zero.  */
    1736          456 :       if (unlimited && !integer_zerop (from_len))
    1737              :         {
    1738           60 :           vec_safe_push (args, from_len);
    1739           60 :           vec_safe_push (args, to_len);
    1740           60 :           extcopy = build_call_vec (fcn_type, unshare_expr (fcn), args);
    1741           60 :           tmp = fold_build2_loc (input_location, GT_EXPR,
    1742              :                                  logical_type_node, from_len,
    1743           60 :                                  build_zero_cst (TREE_TYPE (from_len)));
    1744           60 :           tmp = fold_build3_loc (input_location, COND_EXPR,
    1745              :                                  void_type_node, tmp, extcopy, stdcopy);
    1746              :         }
    1747              :       else
    1748              :         tmp = stdcopy;
    1749              :     }
    1750              : 
    1751              :   /* Only copy _def_init to to_data, when it is not a NULL-pointer.  */
    1752          896 :   if (from == NULL_TREE)
    1753              :     {
    1754            0 :       tree cond;
    1755            0 :       cond = fold_build2_loc (input_location, NE_EXPR,
    1756              :                               logical_type_node,
    1757              :                               from_data, null_pointer_node);
    1758            0 :       tmp = fold_build3_loc (input_location, COND_EXPR,
    1759              :                              void_type_node, cond,
    1760              :                              tmp, build_empty_stmt (input_location));
    1761              :     }
    1762              : 
    1763          896 :   return tmp;
    1764              : }
    1765              : 
    1766              : 
    1767              : static tree
    1768          106 : gfc_trans_class_array_init_assign (gfc_expr *rhs, gfc_expr *lhs, gfc_expr *obj)
    1769              : {
    1770          106 :   gfc_actual_arglist *actual;
    1771          106 :   gfc_expr *ppc;
    1772          106 :   gfc_code *ppc_code;
    1773          106 :   tree res;
    1774              : 
    1775          106 :   actual = gfc_get_actual_arglist ();
    1776          106 :   actual->expr = gfc_copy_expr (rhs);
    1777          106 :   actual->next = gfc_get_actual_arglist ();
    1778          106 :   actual->next->expr = gfc_copy_expr (lhs);
    1779          106 :   ppc = gfc_copy_expr (obj);
    1780          106 :   gfc_add_vptr_component (ppc);
    1781          106 :   gfc_add_component_ref (ppc, "_copy");
    1782          106 :   ppc_code = gfc_get_code (EXEC_CALL);
    1783          106 :   ppc_code->resolved_sym = ppc->symtree->n.sym;
    1784              :   /* Although '_copy' is set to be elemental in class.cc, it is
    1785              :      not staying that way.  Find out why, sometime....  */
    1786          106 :   ppc_code->resolved_sym->attr.elemental = 1;
    1787          106 :   ppc_code->ext.actual = actual;
    1788          106 :   ppc_code->expr1 = ppc;
    1789              :   /* Since '_copy' is elemental, the scalarizer will take care
    1790              :      of arrays in gfc_trans_call.  */
    1791          106 :   res = gfc_trans_call (ppc_code, false, NULL, NULL, false);
    1792          106 :   gfc_free_statements (ppc_code);
    1793              : 
    1794          106 :   if (UNLIMITED_POLY(obj))
    1795              :     {
    1796              :       /* Check if rhs is non-NULL. */
    1797           24 :       gfc_se src;
    1798           24 :       gfc_init_se (&src, NULL);
    1799           24 :       gfc_conv_expr (&src, rhs);
    1800           24 :       src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
    1801           24 :       tree cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    1802           24 :                                    src.expr, fold_convert (TREE_TYPE (src.expr),
    1803              :                                                            null_pointer_node));
    1804           24 :       res = build3_loc (input_location, COND_EXPR, TREE_TYPE (res), cond, res,
    1805              :                         build_empty_stmt (input_location));
    1806              :     }
    1807              : 
    1808          106 :   return res;
    1809              : }
    1810              : 
    1811              : /* Special case for initializing a polymorphic dummy with INTENT(OUT).
    1812              :    A MEMCPY is needed to copy the full data from the default initializer
    1813              :    of the dynamic type.  */
    1814              : 
    1815              : tree
    1816          491 : gfc_trans_class_init_assign (gfc_code *code)
    1817              : {
    1818          491 :   stmtblock_t block;
    1819          491 :   tree tmp;
    1820          491 :   bool cmp_flag = true;
    1821          491 :   gfc_se dst,src,memsz;
    1822          491 :   gfc_expr *lhs, *rhs, *sz;
    1823          491 :   gfc_component *cmp;
    1824          491 :   gfc_symbol *sym;
    1825          491 :   gfc_ref *ref;
    1826              : 
    1827          491 :   gfc_start_block (&block);
    1828              : 
    1829          491 :   lhs = gfc_copy_expr (code->expr1);
    1830              : 
    1831          491 :   rhs = gfc_copy_expr (code->expr1);
    1832          491 :   gfc_add_vptr_component (rhs);
    1833              : 
    1834              :   /* Make sure that the component backend_decls have been built, which
    1835              :      will not have happened if the derived types concerned have not
    1836              :      been referenced.  */
    1837          491 :   gfc_get_derived_type (rhs->ts.u.derived);
    1838          491 :   gfc_add_def_init_component (rhs);
    1839              :   /* The _def_init is always scalar.  */
    1840          491 :   rhs->rank = 0;
    1841              : 
    1842              :   /* Check def_init for initializers.  If this is an INTENT(OUT) dummy with all
    1843              :      default initializer components NULL, use the passed value even though
    1844              :      F2018(8.5.10) asserts that it should considered to be undefined. This is
    1845              :      needed for consistency with other brands.  */
    1846          491 :   sym = code->expr1->expr_type == EXPR_VARIABLE ? code->expr1->symtree->n.sym
    1847              :                                                 : NULL;
    1848          491 :   if (code->op != EXEC_ALLOCATE
    1849          430 :       && sym && sym->attr.dummy
    1850          430 :       && sym->attr.intent == INTENT_OUT)
    1851              :     {
    1852          430 :       ref = rhs->ref;
    1853          860 :       while (ref && ref->next)
    1854              :         ref = ref->next;
    1855          430 :       cmp = ref->u.c.component->ts.u.derived->components;
    1856          665 :       for (; cmp; cmp = cmp->next)
    1857              :         {
    1858          458 :           if (cmp->initializer)
    1859              :             break;
    1860          235 :           else if (!cmp->next)
    1861          170 :             cmp_flag = false;
    1862              :         }
    1863              :     }
    1864              : 
    1865          491 :   if (code->expr1->ts.type == BT_CLASS
    1866          468 :       && CLASS_DATA (code->expr1)->attr.dimension)
    1867              :     {
    1868          106 :       gfc_array_spec *tmparr = gfc_get_array_spec ();
    1869          106 :       *tmparr = *CLASS_DATA (code->expr1)->as;
    1870              :       /* Adding the array ref to the class expression results in correct
    1871              :          indexing to the dynamic type.  */
    1872          106 :       gfc_add_full_array_ref (lhs, tmparr);
    1873          106 :       tmp = gfc_trans_class_array_init_assign (rhs, lhs, code->expr1);
    1874          106 :     }
    1875          385 :   else if (cmp_flag)
    1876              :     {
    1877              :       /* Scalar initialization needs the _data component.  */
    1878          228 :       gfc_add_data_component (lhs);
    1879          228 :       sz = gfc_copy_expr (code->expr1);
    1880          228 :       gfc_add_vptr_component (sz);
    1881          228 :       gfc_add_size_component (sz);
    1882              : 
    1883          228 :       gfc_init_se (&dst, NULL);
    1884          228 :       gfc_init_se (&src, NULL);
    1885          228 :       gfc_init_se (&memsz, NULL);
    1886          228 :       gfc_conv_expr (&dst, lhs);
    1887          228 :       gfc_conv_expr (&src, rhs);
    1888          228 :       gfc_conv_expr (&memsz, sz);
    1889          228 :       gfc_add_block_to_block (&block, &src.pre);
    1890          228 :       src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
    1891              : 
    1892          228 :       tmp = gfc_build_memcpy_call (dst.expr, src.expr, memsz.expr);
    1893              : 
    1894          228 :       if (UNLIMITED_POLY(code->expr1))
    1895              :         {
    1896              :           /* Check if _def_init is non-NULL. */
    1897            7 :           tree cond = fold_build2_loc (input_location, NE_EXPR,
    1898              :                                        logical_type_node, src.expr,
    1899            7 :                                        fold_convert (TREE_TYPE (src.expr),
    1900              :                                                      null_pointer_node));
    1901            7 :           tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
    1902              :                             tmp, build_empty_stmt (input_location));
    1903              :         }
    1904              :     }
    1905              :   else
    1906          157 :     tmp = build_empty_stmt (input_location);
    1907              : 
    1908          491 :   if (code->expr1->symtree->n.sym->attr.dummy
    1909          440 :       && (code->expr1->symtree->n.sym->attr.optional
    1910          434 :           || code->expr1->symtree->n.sym->ns->proc_name->attr.entry_master))
    1911              :     {
    1912            6 :       tree present = gfc_conv_expr_present (code->expr1->symtree->n.sym);
    1913            6 :       tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
    1914              :                         present, tmp,
    1915              :                         build_empty_stmt (input_location));
    1916              :     }
    1917              : 
    1918          491 :   gfc_add_expr_to_block (&block, tmp);
    1919          491 :   gfc_free_expr (lhs);
    1920          491 :   gfc_free_expr (rhs);
    1921              : 
    1922          491 :   return gfc_finish_block (&block);
    1923              : }
    1924              : 
    1925              : 
    1926              : /* Class valued elemental function calls or class array elements arriving
    1927              :    in gfc_trans_scalar_assign come here.  Wherever possible the vptr copy
    1928              :    is used to ensure that the rhs dynamic type is assigned to the lhs.  */
    1929              : 
    1930              : static bool
    1931          785 : trans_scalar_class_assign (stmtblock_t *block, gfc_se *lse, gfc_se *rse)
    1932              : {
    1933          785 :   tree fcn;
    1934          785 :   tree rse_expr;
    1935          785 :   tree class_data;
    1936          785 :   tree tmp;
    1937          785 :   tree zero;
    1938          785 :   tree cond;
    1939          785 :   tree final_cond;
    1940          785 :   stmtblock_t inner_block;
    1941          785 :   bool is_descriptor;
    1942          785 :   bool not_call_expr = TREE_CODE (rse->expr) != CALL_EXPR;
    1943          785 :   bool not_lhs_array_type;
    1944              : 
    1945              :   /* Temporaries arising from dependencies in assignment get cast as a
    1946              :      character type of the dynamic size of the rhs. Use the vptr copy
    1947              :      for this case.  */
    1948          785 :   tmp = TREE_TYPE (lse->expr);
    1949          785 :   not_lhs_array_type = !(tmp && TREE_CODE (tmp) == ARRAY_TYPE
    1950            0 :                          && TYPE_MAX_VALUE (TYPE_DOMAIN (tmp)) != NULL_TREE);
    1951              : 
    1952              :   /* Use ordinary assignment if the rhs is not a call expression or
    1953              :      the lhs is not a class entity or an array(ie. character) type.  */
    1954          749 :   if ((not_call_expr && gfc_get_class_from_expr (lse->expr) == NULL_TREE)
    1955         1066 :       && not_lhs_array_type)
    1956              :     return false;
    1957              : 
    1958              :   /* Ordinary assignment can be used if both sides are class expressions
    1959              :      since the dynamic type is preserved by copying the vptr.  This
    1960              :      should only occur, where temporaries are involved.  */
    1961          504 :   if (GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
    1962          504 :       && GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
    1963              :     return false;
    1964              : 
    1965              :   /* Fix the class expression and the class data of the rhs.  */
    1966          504 :   if (!GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr))
    1967          504 :       || not_call_expr)
    1968              :     {
    1969          504 :       if (rse->class_container != NULL_TREE)
    1970              :         tmp = rse->class_container;
    1971              :       else
    1972          407 :         tmp = gfc_get_class_from_expr (rse->expr);
    1973          504 :       if (tmp == NULL_TREE)
    1974              :         return false;
    1975          182 :       rse_expr = gfc_evaluate_now (tmp, block);
    1976              :     }
    1977              :   else
    1978            0 :     rse_expr = gfc_evaluate_now (rse->expr, block);
    1979              : 
    1980          182 :   class_data = gfc_class_data_get (rse_expr);
    1981              : 
    1982              :   /* Check that the rhs data is not null.  */
    1983          182 :   is_descriptor = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (class_data));
    1984          182 :   if (is_descriptor)
    1985          182 :     class_data = gfc_conv_descriptor_data_get (class_data);
    1986          182 :   class_data = gfc_evaluate_now (class_data, block);
    1987              : 
    1988              :   /* Copy the rhs to the lhs.  */
    1989          182 :   fcn = gfc_vptr_copy_get (gfc_class_vptr_get (rse_expr));
    1990          182 :   fcn = build_fold_indirect_ref_loc (input_location, fcn);
    1991          182 :   tmp = gfc_evaluate_now (gfc_build_addr_expr (NULL, rse->expr), block);
    1992          182 :   tmp = is_descriptor ? tmp : class_data;
    1993          182 :   tmp = build_call_expr_loc (input_location, fcn, 2, tmp,
    1994              :                              gfc_build_addr_expr (NULL, lse->expr));
    1995          182 :   gfc_add_expr_to_block (block, tmp);
    1996              : 
    1997              :   /* Only elemental function results need to be finalised and freed.  */
    1998          182 :   if (not_call_expr)
    1999              :     return true;
    2000              : 
    2001            0 :   zero = build_int_cst (TREE_TYPE (class_data), 0);
    2002            0 :   cond = fold_build2_loc (input_location, NE_EXPR,
    2003              :                           logical_type_node,
    2004              :                           class_data, zero);
    2005              : 
    2006              :   /* Finalize the class data if needed.  */
    2007            0 :   gfc_init_block (&inner_block);
    2008            0 :   fcn = gfc_vptr_final_get (gfc_class_vptr_get (rse_expr));
    2009            0 :   zero = build_int_cst (TREE_TYPE (fcn), 0);
    2010            0 :   final_cond = fold_build2_loc (input_location, NE_EXPR,
    2011              :                                 logical_type_node, fcn, zero);
    2012            0 :   fcn = build_fold_indirect_ref_loc (input_location, fcn);
    2013            0 :   tmp = build_call_expr_loc (input_location, fcn, 1, class_data);
    2014            0 :   tmp = build3_v (COND_EXPR, final_cond,
    2015              :                   tmp, build_empty_stmt (input_location));
    2016            0 :   gfc_add_expr_to_block (&inner_block, tmp);
    2017              : 
    2018              :   /* Free the class data.  */
    2019            0 :   tmp = gfc_call_free (class_data);
    2020            0 :   tmp = build3_v (COND_EXPR, cond, tmp,
    2021              :                   build_empty_stmt (input_location));
    2022            0 :   gfc_add_expr_to_block (&inner_block, tmp);
    2023              : 
    2024              :   /* Finish the inner block and subject it to the condition on the
    2025              :      class data being non-zero.  */
    2026            0 :   tmp = gfc_finish_block (&inner_block);
    2027            0 :   tmp = build3_v (COND_EXPR, cond, tmp,
    2028              :                   build_empty_stmt (input_location));
    2029            0 :   gfc_add_expr_to_block (block, tmp);
    2030              : 
    2031            0 :   return true;
    2032              : }
    2033              : 
    2034              : /* End of prototype trans-class.c  */
    2035              : 
    2036              : 
    2037              : static void
    2038        13153 : realloc_lhs_warning (bt type, bool array, locus *where)
    2039              : {
    2040        13153 :   if (array && type != BT_CLASS && type != BT_DERIVED && warn_realloc_lhs)
    2041           25 :     gfc_warning (OPT_Wrealloc_lhs,
    2042              :                  "Code for reallocating the allocatable array at %L will "
    2043              :                  "be added", where);
    2044        13128 :   else if (warn_realloc_lhs_all)
    2045            4 :     gfc_warning (OPT_Wrealloc_lhs_all,
    2046              :                  "Code for reallocating the allocatable variable at %L "
    2047              :                  "will be added", where);
    2048        13153 : }
    2049              : 
    2050              : 
    2051              : static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
    2052              :                                                  gfc_expr *);
    2053              : 
    2054              : /* Copy the scalarization loop variables.  */
    2055              : 
    2056              : static void
    2057      1311465 : gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
    2058              : {
    2059      1311465 :   dest->ss = src->ss;
    2060      1311465 :   dest->loop = src->loop;
    2061            0 : }
    2062              : 
    2063              : 
    2064              : /* Initialize a simple expression holder.
    2065              : 
    2066              :    Care must be taken when multiple se are created with the same parent.
    2067              :    The child se must be kept in sync.  The easiest way is to delay creation
    2068              :    of a child se until after the previous se has been translated.  */
    2069              : 
    2070              : void
    2071      4773663 : gfc_init_se (gfc_se * se, gfc_se * parent)
    2072              : {
    2073      4773663 :   memset (se, 0, sizeof (gfc_se));
    2074      4773663 :   gfc_init_block (&se->pre);
    2075      4773663 :   gfc_init_block (&se->finalblock);
    2076      4773663 :   gfc_init_block (&se->post);
    2077              : 
    2078      4773663 :   se->parent = parent;
    2079              : 
    2080      4773663 :   if (parent)
    2081      1311465 :     gfc_copy_se_loopvars (se, parent);
    2082      4773663 : }
    2083              : 
    2084              : 
    2085              : /* Advances to the next SS in the chain.  Use this rather than setting
    2086              :    se->ss = se->ss->next because all the parents needs to be kept in sync.
    2087              :    See gfc_init_se.  */
    2088              : 
    2089              : void
    2090       250216 : gfc_advance_se_ss_chain (gfc_se * se)
    2091              : {
    2092       250216 :   gfc_se *p;
    2093              : 
    2094       250216 :   gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
    2095              : 
    2096              :   p = se;
    2097              :   /* Walk down the parent chain.  */
    2098       655408 :   while (p != NULL)
    2099              :     {
    2100              :       /* Simple consistency check.  */
    2101       405192 :       gcc_assert (p->parent == NULL || p->parent->ss == p->ss
    2102              :                   || p->parent->ss->nested_ss == p->ss);
    2103              : 
    2104       405192 :       p->ss = p->ss->next;
    2105              : 
    2106       405192 :       p = p->parent;
    2107              :     }
    2108       250216 : }
    2109              : 
    2110              : 
    2111              : /* Ensures the result of the expression as either a temporary variable
    2112              :    or a constant so that it can be used repeatedly.  */
    2113              : 
    2114              : void
    2115         8392 : gfc_make_safe_expr (gfc_se * se)
    2116              : {
    2117         8392 :   tree var;
    2118              : 
    2119         8392 :   if (CONSTANT_CLASS_P (se->expr))
    2120              :     return;
    2121              : 
    2122              :   /* We need a temporary for this result.  */
    2123          274 :   var = gfc_create_var (TREE_TYPE (se->expr), NULL);
    2124          274 :   gfc_add_modify (&se->pre, var, se->expr);
    2125          274 :   se->expr = var;
    2126              : }
    2127              : 
    2128              : 
    2129              : /* Return an expression which determines if a dummy parameter is present.
    2130              :    Also used for arguments to procedures with multiple entry points.  */
    2131              : 
    2132              : tree
    2133        11952 : gfc_conv_expr_present (gfc_symbol * sym, bool use_saved_desc)
    2134              : {
    2135        11952 :   tree decl, orig_decl, cond;
    2136              : 
    2137        11952 :   gcc_assert (sym->attr.dummy);
    2138        11952 :   orig_decl = decl = gfc_get_symbol_decl (sym);
    2139              : 
    2140              :   /* Intrinsic scalars and derived types with VALUE attribute which are passed
    2141              :      by value use a hidden argument to denote the presence status.  */
    2142        11952 :   if (sym->attr.value && !sym->attr.dimension && sym->ts.type != BT_CLASS)
    2143              :     {
    2144         1082 :       char name[GFC_MAX_SYMBOL_LEN + 2];
    2145         1082 :       tree tree_name;
    2146              : 
    2147         1082 :       gcc_assert (TREE_CODE (decl) == PARM_DECL);
    2148         1082 :       name[0] = '.';
    2149         1082 :       strcpy (&name[1], sym->name);
    2150         1082 :       tree_name = get_identifier (name);
    2151              : 
    2152              :       /* Walk function argument list to find hidden arg.  */
    2153         1082 :       cond = DECL_ARGUMENTS (DECL_CONTEXT (decl));
    2154         5428 :       for ( ; cond != NULL_TREE; cond = TREE_CHAIN (cond))
    2155         5428 :         if (DECL_NAME (cond) == tree_name
    2156         5428 :             && DECL_ARTIFICIAL (cond))
    2157              :           break;
    2158              : 
    2159         1082 :       gcc_assert (cond);
    2160         1082 :       return cond;
    2161              :     }
    2162              : 
    2163              :   /* Assumed-shape arrays use a local variable for the array data;
    2164              :      the actual PARAM_DECL is in a saved decl.  As the local variable
    2165              :      is NULL, it can be checked instead, unless use_saved_desc is
    2166              :      requested.  */
    2167              : 
    2168        10870 :   if (use_saved_desc && TREE_CODE (decl) != PARM_DECL)
    2169              :     {
    2170          888 :       gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
    2171              :              || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
    2172          888 :       decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
    2173              :     }
    2174              : 
    2175        10870 :   cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, decl,
    2176        10870 :                           fold_convert (TREE_TYPE (decl), null_pointer_node));
    2177              : 
    2178              :   /* Fortran 2008 allows to pass null pointers and non-associated pointers
    2179              :      as actual argument to denote absent dummies. For array descriptors,
    2180              :      we thus also need to check the array descriptor.  For BT_CLASS, it
    2181              :      can also occur for scalars and F2003 due to type->class wrapping and
    2182              :      class->class wrapping.  Note further that BT_CLASS always uses an
    2183              :      array descriptor for arrays, also for explicit-shape/assumed-size.
    2184              :      For assumed-rank arrays, no local variable is generated, hence,
    2185              :      the following also applies with !use_saved_desc.  */
    2186              : 
    2187        10870 :   if ((use_saved_desc || TREE_CODE (orig_decl) == PARM_DECL)
    2188         7715 :       && !sym->attr.allocatable
    2189         6485 :       && ((sym->ts.type != BT_CLASS && !sym->attr.pointer)
    2190         2302 :           || (sym->ts.type == BT_CLASS
    2191         1047 :               && !CLASS_DATA (sym)->attr.allocatable
    2192          573 :               && !CLASS_DATA (sym)->attr.class_pointer))
    2193         4396 :       && ((gfc_option.allow_std & GFC_STD_F2008) != 0
    2194            6 :           || sym->ts.type == BT_CLASS))
    2195              :     {
    2196         4390 :       tree tmp;
    2197              : 
    2198         4390 :       if ((sym->as && (sym->as->type == AS_ASSUMED_SHAPE
    2199         1525 :                        || sym->as->type == AS_ASSUMED_RANK
    2200         1437 :                        || sym->attr.codimension))
    2201         3462 :           || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
    2202              :         {
    2203         1105 :           tmp = build_fold_indirect_ref_loc (input_location, decl);
    2204         1105 :           if (sym->ts.type == BT_CLASS)
    2205          177 :             tmp = gfc_class_data_get (tmp);
    2206         1105 :           tmp = gfc_conv_array_data (tmp);
    2207              :         }
    2208         3285 :       else if (sym->ts.type == BT_CLASS)
    2209           36 :         tmp = gfc_class_data_get (decl);
    2210              :       else
    2211              :         tmp = NULL_TREE;
    2212              : 
    2213         1141 :       if (tmp != NULL_TREE)
    2214              :         {
    2215         1141 :           tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
    2216         1141 :                                  fold_convert (TREE_TYPE (tmp), null_pointer_node));
    2217         1141 :           cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    2218              :                                   logical_type_node, cond, tmp);
    2219              :         }
    2220              :     }
    2221              : 
    2222              :   return cond;
    2223              : }
    2224              : 
    2225              : 
    2226              : /* Converts a missing, dummy argument into a null or zero.  */
    2227              : 
    2228              : void
    2229          886 : gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
    2230              : {
    2231          886 :   tree present;
    2232          886 :   tree tmp;
    2233              : 
    2234          886 :   present = gfc_conv_expr_present (arg->symtree->n.sym);
    2235              : 
    2236          886 :   if (kind > 0)
    2237              :     {
    2238              :       /* Create a temporary and convert it to the correct type.  */
    2239           54 :       tmp = gfc_get_int_type (kind);
    2240           54 :       tmp = fold_convert (tmp, build_fold_indirect_ref_loc (input_location,
    2241              :                                                         se->expr));
    2242              : 
    2243              :       /* Test for a NULL value.  */
    2244           54 :       tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
    2245           54 :                         tmp, fold_convert (TREE_TYPE (tmp), integer_one_node));
    2246           54 :       tmp = gfc_evaluate_now (tmp, &se->pre);
    2247           54 :       se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
    2248              :     }
    2249              :   else
    2250              :     {
    2251          832 :       tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
    2252              :                         present, se->expr,
    2253          832 :                         build_zero_cst (TREE_TYPE (se->expr)));
    2254          832 :       tmp = gfc_evaluate_now (tmp, &se->pre);
    2255          832 :       se->expr = tmp;
    2256              :     }
    2257              : 
    2258          886 :   if (ts.type == BT_CHARACTER)
    2259              :     {
    2260              :       /* Handle deferred-length dummies that pass the character length by
    2261              :          reference so that the value can be returned.  */
    2262          262 :       if (ts.deferred && INDIRECT_REF_P (se->string_length))
    2263              :         {
    2264           18 :           tmp = gfc_build_addr_expr (NULL_TREE, se->string_length);
    2265           18 :           tmp = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
    2266              :                                  present, tmp, null_pointer_node);
    2267           18 :           tmp = gfc_evaluate_now (tmp, &se->pre);
    2268           18 :           tmp = build_fold_indirect_ref_loc (input_location, tmp);
    2269              :         }
    2270              :       else
    2271              :         {
    2272          244 :           tmp = build_int_cst (gfc_charlen_type_node, 0);
    2273          244 :           tmp = fold_build3_loc (input_location, COND_EXPR,
    2274              :                                  gfc_charlen_type_node,
    2275              :                                  present, se->string_length, tmp);
    2276          244 :           tmp = gfc_evaluate_now (tmp, &se->pre);
    2277              :         }
    2278          262 :       se->string_length = tmp;
    2279              :     }
    2280          886 :   return;
    2281              : }
    2282              : 
    2283              : 
    2284              : /* Get the character length of an expression, looking through gfc_refs
    2285              :    if necessary.  */
    2286              : 
    2287              : tree
    2288        20461 : gfc_get_expr_charlen (gfc_expr *e)
    2289              : {
    2290        20461 :   gfc_ref *r;
    2291        20461 :   tree length;
    2292        20461 :   tree previous = NULL_TREE;
    2293        20461 :   gfc_se se;
    2294              : 
    2295        20461 :   gcc_assert (e->expr_type == EXPR_VARIABLE
    2296              :               && e->ts.type == BT_CHARACTER);
    2297              : 
    2298        20461 :   length = NULL; /* To silence compiler warning.  */
    2299              : 
    2300        20461 :   if (is_subref_array (e) && e->ts.u.cl->length)
    2301              :     {
    2302          839 :       gfc_se tmpse;
    2303          839 :       gfc_init_se (&tmpse, NULL);
    2304          839 :       gfc_conv_expr_type (&tmpse, e->ts.u.cl->length, gfc_charlen_type_node);
    2305          839 :       e->ts.u.cl->backend_decl = tmpse.expr;
    2306          839 :       return tmpse.expr;
    2307              :     }
    2308              : 
    2309              :   /* First candidate: if the variable is of type CHARACTER, the
    2310              :      expression's length could be the length of the character
    2311              :      variable.  */
    2312        19622 :   if (e->symtree->n.sym->ts.type == BT_CHARACTER)
    2313        19310 :     length = e->symtree->n.sym->ts.u.cl->backend_decl;
    2314              : 
    2315              :   /* Look through the reference chain for component references.  */
    2316        39270 :   for (r = e->ref; r; r = r->next)
    2317              :     {
    2318        19648 :       previous = length;
    2319        19648 :       switch (r->type)
    2320              :         {
    2321          312 :         case REF_COMPONENT:
    2322          312 :           if (r->u.c.component->ts.type == BT_CHARACTER)
    2323          312 :             length = r->u.c.component->ts.u.cl->backend_decl;
    2324              :           break;
    2325              : 
    2326              :         case REF_ARRAY:
    2327              :           /* Do nothing.  */
    2328              :           break;
    2329              : 
    2330           20 :         case REF_SUBSTRING:
    2331           20 :           gfc_init_se (&se, NULL);
    2332           20 :           gfc_conv_expr_type (&se, r->u.ss.start, gfc_charlen_type_node);
    2333           20 :           length = se.expr;
    2334           20 :           if (r->u.ss.end)
    2335            0 :             gfc_conv_expr_type (&se, r->u.ss.end, gfc_charlen_type_node);
    2336              :           else
    2337           20 :             se.expr = previous;
    2338           20 :           length = fold_build2_loc (input_location, MINUS_EXPR,
    2339              :                                     gfc_charlen_type_node,
    2340              :                                     se.expr, length);
    2341           20 :           length = fold_build2_loc (input_location, PLUS_EXPR,
    2342              :                                     gfc_charlen_type_node, length,
    2343              :                                     gfc_index_one_node);
    2344           20 :           break;
    2345              : 
    2346            0 :         default:
    2347            0 :           gcc_unreachable ();
    2348        19648 :           break;
    2349              :         }
    2350              :     }
    2351              : 
    2352        19622 :   gcc_assert (length != NULL);
    2353              :   return length;
    2354              : }
    2355              : 
    2356              : 
    2357              : /* Return for an expression the backend decl of the coarray.  */
    2358              : 
    2359              : tree
    2360         2170 : gfc_get_tree_for_caf_expr (gfc_expr *expr)
    2361              : {
    2362         2170 :   tree caf_decl;
    2363         2170 :   bool found = false;
    2364         2170 :   gfc_ref *ref;
    2365              : 
    2366         2170 :   gcc_assert (expr && expr->expr_type == EXPR_VARIABLE);
    2367              : 
    2368              :   /* Not-implemented diagnostic.  */
    2369         2170 :   if (expr->symtree->n.sym->ts.type == BT_CLASS
    2370           39 :       && UNLIMITED_POLY (expr->symtree->n.sym)
    2371            0 :       && CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
    2372            0 :     gfc_error ("Sorry, coindexed access to an unlimited polymorphic object at "
    2373              :                "%L is not supported", &expr->where);
    2374              : 
    2375         4601 :   for (ref = expr->ref; ref; ref = ref->next)
    2376         2431 :     if (ref->type == REF_COMPONENT)
    2377              :       {
    2378          225 :         if (ref->u.c.component->ts.type == BT_CLASS
    2379            0 :             && UNLIMITED_POLY (ref->u.c.component)
    2380            0 :             && CLASS_DATA (ref->u.c.component)->attr.codimension)
    2381            0 :           gfc_error ("Sorry, coindexed access to an unlimited polymorphic "
    2382              :                      "component at %L is not supported", &expr->where);
    2383              :       }
    2384              : 
    2385              :   /* Make sure the backend_decl is present before accessing it.  */
    2386         2170 :   caf_decl = expr->symtree->n.sym->backend_decl == NULL_TREE
    2387         2170 :       ? gfc_get_symbol_decl (expr->symtree->n.sym)
    2388              :       : expr->symtree->n.sym->backend_decl;
    2389              : 
    2390         2170 :   if (expr->symtree->n.sym->ts.type == BT_CLASS)
    2391              :     {
    2392           39 :       if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
    2393           45 :           && GFC_DECL_SAVED_DESCRIPTOR (caf_decl))
    2394            6 :         caf_decl = GFC_DECL_SAVED_DESCRIPTOR (caf_decl);
    2395              : 
    2396           39 :       if (expr->ref && expr->ref->type == REF_ARRAY)
    2397              :         {
    2398           28 :           caf_decl = gfc_class_data_get (caf_decl);
    2399           28 :           if (CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
    2400              :             return caf_decl;
    2401              :         }
    2402           11 :       else if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
    2403            2 :                && GFC_DECL_TOKEN (caf_decl)
    2404           13 :                && CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
    2405              :         return caf_decl;
    2406              : 
    2407           23 :       for (ref = expr->ref; ref; ref = ref->next)
    2408              :         {
    2409           18 :           if (ref->type == REF_COMPONENT
    2410            9 :               && strcmp (ref->u.c.component->name, "_data") != 0)
    2411              :             {
    2412            0 :               caf_decl = gfc_class_data_get (caf_decl);
    2413            0 :               if (CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
    2414              :                 return caf_decl;
    2415              :               break;
    2416              :             }
    2417           18 :           else if (ref->type == REF_ARRAY && ref->u.ar.dimen)
    2418              :             break;
    2419              :         }
    2420              :     }
    2421         2140 :   if (expr->symtree->n.sym->attr.codimension)
    2422              :     return caf_decl;
    2423              : 
    2424              :   /* The following code assumes that the coarray is a component reachable via
    2425              :      only scalar components/variables; the Fortran standard guarantees this.  */
    2426              : 
    2427           76 :   for (ref = expr->ref; ref; ref = ref->next)
    2428           76 :     if (ref->type == REF_COMPONENT)
    2429              :       {
    2430           76 :         gfc_component *comp = ref->u.c.component;
    2431              : 
    2432           76 :         if (POINTER_TYPE_P (TREE_TYPE (caf_decl)))
    2433            0 :           caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
    2434           76 :         caf_decl = fold_build3_loc (input_location, COMPONENT_REF,
    2435           76 :                                     TREE_TYPE (comp->backend_decl), caf_decl,
    2436              :                                     comp->backend_decl, NULL_TREE);
    2437           76 :         if (comp->ts.type == BT_CLASS)
    2438              :           {
    2439            0 :             caf_decl = gfc_class_data_get (caf_decl);
    2440            0 :             if (CLASS_DATA (comp)->attr.codimension)
    2441              :               {
    2442              :                 found = true;
    2443              :                 break;
    2444              :               }
    2445              :           }
    2446           76 :         if (comp->attr.codimension)
    2447              :           {
    2448              :             found = true;
    2449              :             break;
    2450              :           }
    2451              :       }
    2452           76 :   gcc_assert (found && caf_decl);
    2453              :   return caf_decl;
    2454              : }
    2455              : 
    2456              : 
    2457              : /* Obtain the Coarray token - and optionally also the offset.  */
    2458              : 
    2459              : void
    2460         2041 : gfc_get_caf_token_offset (gfc_se *se, tree *token, tree *offset, tree caf_decl,
    2461              :                           tree se_expr, gfc_expr *expr)
    2462              : {
    2463         2041 :   tree tmp;
    2464              : 
    2465         2041 :   gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
    2466              : 
    2467              :   /* Coarray token.  */
    2468         2041 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
    2469          652 :       *token = gfc_conv_descriptor_token (caf_decl);
    2470         1387 :   else if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
    2471         1588 :            && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
    2472            6 :     *token = GFC_DECL_TOKEN (caf_decl);
    2473              :   else
    2474              :     {
    2475         1383 :       gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (caf_decl))
    2476              :                   && GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl)) != NULL_TREE);
    2477         1383 :       *token = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl));
    2478              :     }
    2479              : 
    2480         2041 :   if (offset == NULL)
    2481              :     return;
    2482              : 
    2483              :   /* Offset between the coarray base address and the address wanted.  */
    2484          187 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl))
    2485          187 :       && (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_ALLOCATABLE
    2486            0 :           || GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_POINTER))
    2487            0 :     *offset = build_int_cst (gfc_array_index_type, 0);
    2488          187 :   else if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
    2489          187 :            && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
    2490            0 :     *offset = GFC_DECL_CAF_OFFSET (caf_decl);
    2491          187 :   else if (GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl)) != NULL_TREE)
    2492            0 :     *offset = GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl));
    2493              :   else
    2494          187 :     *offset = build_int_cst (gfc_array_index_type, 0);
    2495              : 
    2496          187 :   if (POINTER_TYPE_P (TREE_TYPE (se_expr))
    2497          187 :       && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se_expr))))
    2498              :     {
    2499            0 :       tmp = build_fold_indirect_ref_loc (input_location, se_expr);
    2500            0 :       tmp = gfc_conv_descriptor_data_get (tmp);
    2501              :     }
    2502          187 :   else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se_expr)))
    2503            0 :     tmp = gfc_conv_descriptor_data_get (se_expr);
    2504              :   else
    2505              :     {
    2506          187 :       gcc_assert (POINTER_TYPE_P (TREE_TYPE (se_expr)));
    2507              :       tmp = se_expr;
    2508              :     }
    2509              : 
    2510          187 :   *offset = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    2511              :                              *offset, fold_convert (gfc_array_index_type, tmp));
    2512              : 
    2513          187 :   if (expr->symtree->n.sym->ts.type == BT_DERIVED
    2514            0 :       && expr->symtree->n.sym->attr.codimension
    2515            0 :       && expr->symtree->n.sym->ts.u.derived->attr.alloc_comp)
    2516              :     {
    2517            0 :       gfc_expr *base_expr = gfc_copy_expr (expr);
    2518            0 :       gfc_ref *ref = base_expr->ref;
    2519            0 :       gfc_se base_se;
    2520              : 
    2521              :       // Iterate through the refs until the last one.
    2522            0 :       while (ref->next)
    2523              :           ref = ref->next;
    2524              : 
    2525            0 :       if (ref->type == REF_ARRAY
    2526            0 :           && ref->u.ar.type != AR_FULL)
    2527              :         {
    2528            0 :           const int ranksum = ref->u.ar.dimen + ref->u.ar.codimen;
    2529            0 :           int i;
    2530            0 :           for (i = 0; i < ranksum; ++i)
    2531              :             {
    2532            0 :               ref->u.ar.start[i] = NULL;
    2533            0 :               ref->u.ar.end[i] = NULL;
    2534              :             }
    2535            0 :           ref->u.ar.type = AR_FULL;
    2536              :         }
    2537            0 :       gfc_init_se (&base_se, NULL);
    2538            0 :       if (gfc_caf_attr (base_expr).dimension)
    2539              :         {
    2540            0 :           gfc_conv_expr_descriptor (&base_se, base_expr);
    2541            0 :           tmp = gfc_conv_descriptor_data_get (base_se.expr);
    2542              :         }
    2543              :       else
    2544              :         {
    2545            0 :           gfc_conv_expr (&base_se, base_expr);
    2546            0 :           tmp = base_se.expr;
    2547              :         }
    2548              : 
    2549            0 :       gfc_free_expr (base_expr);
    2550            0 :       gfc_add_block_to_block (&se->pre, &base_se.pre);
    2551            0 :       gfc_add_block_to_block (&se->post, &base_se.post);
    2552            0 :     }
    2553          187 :   else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
    2554            0 :     tmp = gfc_conv_descriptor_data_get (caf_decl);
    2555          187 :   else if (INDIRECT_REF_P (caf_decl))
    2556            0 :     tmp = TREE_OPERAND (caf_decl, 0);
    2557              :   else
    2558              :     {
    2559          187 :       gcc_assert (POINTER_TYPE_P (TREE_TYPE (caf_decl)));
    2560              :       tmp = caf_decl;
    2561              :     }
    2562              : 
    2563          187 :   *offset = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    2564              :                             fold_convert (gfc_array_index_type, *offset),
    2565              :                             fold_convert (gfc_array_index_type, tmp));
    2566              : }
    2567              : 
    2568              : 
    2569              : /* Convert the coindex of a coarray into an image index; the result is
    2570              :    image_num =  (idx(1)-lcobound(1)+1) + (idx(2)-lcobound(2))*extent(1)
    2571              :               + (idx(3)-lcobound(3))*extend(1)*extent(2) + ...  */
    2572              : 
    2573              : tree
    2574         1748 : gfc_caf_get_image_index (stmtblock_t *block, gfc_expr *e, tree desc)
    2575              : {
    2576         1748 :   gfc_ref *ref;
    2577         1748 :   tree lbound, ubound, extent, tmp, img_idx;
    2578         1748 :   gfc_se se;
    2579         1748 :   int i;
    2580              : 
    2581         1809 :   for (ref = e->ref; ref; ref = ref->next)
    2582         1809 :     if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
    2583              :       break;
    2584         1748 :   gcc_assert (ref != NULL);
    2585              : 
    2586         1748 :   if (ref->u.ar.dimen_type[ref->u.ar.dimen] == DIMEN_THIS_IMAGE)
    2587          193 :     return build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
    2588          193 :                                 null_pointer_node);
    2589              : 
    2590         1555 :   img_idx = build_zero_cst (gfc_array_index_type);
    2591         1555 :   extent = build_one_cst (gfc_array_index_type);
    2592         1555 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
    2593          642 :     for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
    2594              :       {
    2595          327 :         gfc_init_se (&se, NULL);
    2596          327 :         gfc_conv_expr_type (&se, ref->u.ar.start[i], gfc_array_index_type);
    2597          327 :         gfc_add_block_to_block (block, &se.pre);
    2598          327 :         lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
    2599          327 :         tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2600          327 :                                TREE_TYPE (lbound), se.expr, lbound);
    2601          327 :         tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
    2602              :                                extent, tmp);
    2603          327 :         img_idx = fold_build2_loc (input_location, PLUS_EXPR,
    2604          327 :                                    TREE_TYPE (tmp), img_idx, tmp);
    2605          327 :         if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
    2606              :           {
    2607           12 :             ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
    2608           12 :             tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
    2609           12 :             extent = fold_build2_loc (input_location, MULT_EXPR,
    2610           12 :                                       TREE_TYPE (tmp), extent, tmp);
    2611              :           }
    2612              :       }
    2613              :   else
    2614         2496 :     for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
    2615              :       {
    2616         1256 :         gfc_init_se (&se, NULL);
    2617         1256 :         gfc_conv_expr_type (&se, ref->u.ar.start[i], gfc_array_index_type);
    2618         1256 :         gfc_add_block_to_block (block, &se.pre);
    2619         1256 :         lbound = GFC_TYPE_ARRAY_LBOUND (TREE_TYPE (desc), i);
    2620         1256 :         tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2621         1256 :                                TREE_TYPE (lbound), se.expr, lbound);
    2622         1256 :         tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
    2623              :                                extent, tmp);
    2624         1256 :         img_idx = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
    2625              :                                    img_idx, tmp);
    2626         1256 :         if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
    2627              :           {
    2628           16 :             ubound = GFC_TYPE_ARRAY_UBOUND (TREE_TYPE (desc), i);
    2629           16 :             tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2630           16 :                                    TREE_TYPE (ubound), ubound, lbound);
    2631           16 :             tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
    2632           16 :                                    tmp, build_one_cst (TREE_TYPE (tmp)));
    2633           16 :             extent = fold_build2_loc (input_location, MULT_EXPR,
    2634           16 :                                       TREE_TYPE (tmp), extent, tmp);
    2635              :           }
    2636              :       }
    2637         1555 :   img_idx = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (img_idx),
    2638         1555 :                              img_idx, build_one_cst (TREE_TYPE (img_idx)));
    2639         1555 :   return fold_convert (integer_type_node, img_idx);
    2640              : }
    2641              : 
    2642              : 
    2643              : /* For each character array constructor subexpression without a ts.u.cl->length,
    2644              :    replace it by its first element (if there aren't any elements, the length
    2645              :    should already be set to zero).  */
    2646              : 
    2647              : static void
    2648          110 : flatten_array_ctors_without_strlen (gfc_expr* e)
    2649              : {
    2650          110 :   gfc_actual_arglist* arg;
    2651          110 :   gfc_constructor* c;
    2652              : 
    2653          110 :   if (!e)
    2654              :     return;
    2655              : 
    2656          110 :   switch (e->expr_type)
    2657              :     {
    2658              : 
    2659            0 :     case EXPR_OP:
    2660            0 :       flatten_array_ctors_without_strlen (e->value.op.op1);
    2661            0 :       flatten_array_ctors_without_strlen (e->value.op.op2);
    2662            0 :       break;
    2663              : 
    2664            0 :     case EXPR_COMPCALL:
    2665              :       /* TODO: Implement as with EXPR_FUNCTION when needed.  */
    2666            0 :       gcc_unreachable ();
    2667              : 
    2668           13 :     case EXPR_FUNCTION:
    2669           40 :       for (arg = e->value.function.actual; arg; arg = arg->next)
    2670           27 :         flatten_array_ctors_without_strlen (arg->expr);
    2671              :       break;
    2672              : 
    2673            0 :     case EXPR_ARRAY:
    2674              : 
    2675              :       /* We've found what we're looking for.  */
    2676            0 :       if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
    2677              :         {
    2678            0 :           gfc_constructor *c;
    2679            0 :           gfc_expr* new_expr;
    2680              : 
    2681            0 :           gcc_assert (e->value.constructor);
    2682              : 
    2683            0 :           c = gfc_constructor_first (e->value.constructor);
    2684            0 :           new_expr = c->expr;
    2685            0 :           c->expr = NULL;
    2686              : 
    2687            0 :           flatten_array_ctors_without_strlen (new_expr);
    2688            0 :           gfc_replace_expr (e, new_expr);
    2689            0 :           break;
    2690              :         }
    2691              : 
    2692              :       /* Otherwise, fall through to handle constructor elements.  */
    2693            0 :       gcc_fallthrough ();
    2694            0 :     case EXPR_STRUCTURE:
    2695            0 :       for (c = gfc_constructor_first (e->value.constructor);
    2696            0 :            c; c = gfc_constructor_next (c))
    2697            0 :         flatten_array_ctors_without_strlen (c->expr);
    2698              :       break;
    2699              : 
    2700              :     default:
    2701              :       break;
    2702              : 
    2703              :     }
    2704              : }
    2705              : 
    2706              : 
    2707              : /* Generate code to initialize a string length variable. Returns the
    2708              :    value.  For array constructors, cl->length might be NULL and in this case,
    2709              :    the first element of the constructor is needed.  expr is the original
    2710              :    expression so we can access it but can be NULL if this is not needed.  */
    2711              : 
    2712              : void
    2713         3921 : gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
    2714              : {
    2715         3921 :   gfc_se se;
    2716              : 
    2717         3921 :   gfc_init_se (&se, NULL);
    2718              : 
    2719         3921 :   if (!cl->length && cl->backend_decl && VAR_P (cl->backend_decl))
    2720         1379 :     return;
    2721              : 
    2722              :   /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
    2723              :      "flatten" array constructors by taking their first element; all elements
    2724              :      should be the same length or a cl->length should be present.  */
    2725         2635 :   if (!cl->length)
    2726              :     {
    2727          176 :       gfc_expr* expr_flat;
    2728          176 :       if (!expr)
    2729              :         return;
    2730           83 :       expr_flat = gfc_copy_expr (expr);
    2731           83 :       flatten_array_ctors_without_strlen (expr_flat);
    2732           83 :       gfc_resolve_expr (expr_flat);
    2733           83 :       if (expr_flat->rank)
    2734           13 :         gfc_conv_expr_descriptor (&se, expr_flat);
    2735              :       else
    2736           70 :         gfc_conv_expr (&se, expr_flat);
    2737           83 :       if (expr_flat->expr_type != EXPR_VARIABLE)
    2738           77 :         gfc_add_block_to_block (pblock, &se.pre);
    2739           83 :       se.expr = convert (gfc_charlen_type_node, se.string_length);
    2740           83 :       gfc_add_block_to_block (pblock, &se.post);
    2741           83 :       gfc_free_expr (expr_flat);
    2742              :     }
    2743              :   else
    2744              :     {
    2745              :       /* Convert cl->length.  */
    2746         2459 :       gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
    2747         2459 :       se.expr = fold_build2_loc (input_location, MAX_EXPR,
    2748              :                                  gfc_charlen_type_node, se.expr,
    2749         2459 :                                  build_zero_cst (TREE_TYPE (se.expr)));
    2750         2459 :       gfc_add_block_to_block (pblock, &se.pre);
    2751              :     }
    2752              : 
    2753         2542 :   if (cl->backend_decl && VAR_P (cl->backend_decl))
    2754         1630 :     gfc_add_modify (pblock, cl->backend_decl, se.expr);
    2755              :   else
    2756          912 :     cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
    2757              : }
    2758              : 
    2759              : 
    2760              : static void
    2761         7252 : gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
    2762              :                     const char *name, locus *where)
    2763              : {
    2764         7252 :   tree tmp;
    2765         7252 :   tree type;
    2766         7252 :   tree fault;
    2767         7252 :   gfc_se start;
    2768         7252 :   gfc_se end;
    2769         7252 :   char *msg;
    2770         7252 :   mpz_t length;
    2771              : 
    2772         7252 :   type = gfc_get_character_type (kind, ref->u.ss.length);
    2773         7252 :   type = build_pointer_type (type);
    2774              : 
    2775         7252 :   gfc_init_se (&start, se);
    2776         7252 :   gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
    2777         7252 :   gfc_add_block_to_block (&se->pre, &start.pre);
    2778              : 
    2779         7252 :   if (integer_onep (start.expr))
    2780         2790 :     gfc_conv_string_parameter (se);
    2781              :   else
    2782              :     {
    2783         4462 :       tmp = start.expr;
    2784         4462 :       STRIP_NOPS (tmp);
    2785              :       /* Avoid multiple evaluation of substring start.  */
    2786         4462 :       if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
    2787         1585 :         start.expr = gfc_evaluate_now (start.expr, &se->pre);
    2788              : 
    2789              :       /* Change the start of the string.  */
    2790         4462 :       if (((TREE_CODE (TREE_TYPE (se->expr)) == ARRAY_TYPE
    2791         1112 :             || TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE)
    2792         3470 :            && TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
    2793         5454 :           || (POINTER_TYPE_P (TREE_TYPE (se->expr))
    2794          992 :               && TREE_CODE (TREE_TYPE (TREE_TYPE (se->expr))) != ARRAY_TYPE))
    2795              :         tmp = se->expr;
    2796              :       else
    2797          984 :         tmp = build_fold_indirect_ref_loc (input_location,
    2798              :                                        se->expr);
    2799              :       /* For BIND(C), a BT_CHARACTER is not an ARRAY_TYPE.  */
    2800         4462 :       if (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE)
    2801              :         {
    2802         4334 :           tmp = gfc_build_array_ref (tmp, start.expr, NULL_TREE, true);
    2803         4334 :           se->expr = gfc_build_addr_expr (type, tmp);
    2804              :         }
    2805          128 :       else if (POINTER_TYPE_P (TREE_TYPE (tmp)))
    2806              :         {
    2807            8 :           tree diff;
    2808            8 :           diff = fold_build2 (MINUS_EXPR, gfc_charlen_type_node, start.expr,
    2809              :                               build_one_cst (gfc_charlen_type_node));
    2810            8 :           diff = fold_convert (size_type_node, diff);
    2811            8 :           se->expr
    2812            8 :             = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (tmp), tmp, diff);
    2813              :         }
    2814              :     }
    2815              : 
    2816              :   /* Length = end + 1 - start.  */
    2817         7252 :   gfc_init_se (&end, se);
    2818         7252 :   if (ref->u.ss.end == NULL)
    2819          202 :     end.expr = se->string_length;
    2820              :   else
    2821              :     {
    2822         7050 :       gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
    2823         7050 :       gfc_add_block_to_block (&se->pre, &end.pre);
    2824              :     }
    2825         7252 :   tmp = end.expr;
    2826         7252 :   STRIP_NOPS (tmp);
    2827         7252 :   if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
    2828         2193 :     end.expr = gfc_evaluate_now (end.expr, &se->pre);
    2829              : 
    2830         7252 :   if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    2831          455 :       && !gfc_contains_implied_index_p (ref->u.ss.start)
    2832         7707 :       && !gfc_contains_implied_index_p (ref->u.ss.end))
    2833              :     {
    2834          455 :       tree nonempty = fold_build2_loc (input_location, LE_EXPR,
    2835              :                                        logical_type_node, start.expr,
    2836              :                                        end.expr);
    2837              : 
    2838              :       /* Check lower bound.  */
    2839          455 :       fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    2840              :                                start.expr,
    2841          455 :                                build_one_cst (TREE_TYPE (start.expr)));
    2842          455 :       fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    2843              :                                logical_type_node, nonempty, fault);
    2844          455 :       if (name)
    2845          454 :         msg = xasprintf ("Substring out of bounds: lower bound (%%ld) of '%s' "
    2846              :                          "is less than one", name);
    2847              :       else
    2848            1 :         msg = xasprintf ("Substring out of bounds: lower bound (%%ld) "
    2849              :                          "is less than one");
    2850          455 :       gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
    2851              :                                fold_convert (long_integer_type_node,
    2852              :                                              start.expr));
    2853          455 :       free (msg);
    2854              : 
    2855              :       /* Check upper bound.  */
    2856          455 :       fault = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    2857              :                                end.expr, se->string_length);
    2858          455 :       fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    2859              :                                logical_type_node, nonempty, fault);
    2860          455 :       if (name)
    2861          454 :         msg = xasprintf ("Substring out of bounds: upper bound (%%ld) of '%s' "
    2862              :                          "exceeds string length (%%ld)", name);
    2863              :       else
    2864            1 :         msg = xasprintf ("Substring out of bounds: upper bound (%%ld) "
    2865              :                          "exceeds string length (%%ld)");
    2866          455 :       gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
    2867              :                                fold_convert (long_integer_type_node, end.expr),
    2868              :                                fold_convert (long_integer_type_node,
    2869              :                                              se->string_length));
    2870          455 :       free (msg);
    2871              :     }
    2872              : 
    2873              :   /* Try to calculate the length from the start and end expressions.  */
    2874         7252 :   if (ref->u.ss.end
    2875         7252 :       && gfc_dep_difference (ref->u.ss.end, ref->u.ss.start, &length))
    2876              :     {
    2877         6020 :       HOST_WIDE_INT i_len;
    2878              : 
    2879         6020 :       i_len = gfc_mpz_get_hwi (length) + 1;
    2880         6020 :       if (i_len < 0)
    2881              :         i_len = 0;
    2882              : 
    2883         6020 :       tmp = build_int_cst (gfc_charlen_type_node, i_len);
    2884         6020 :       mpz_clear (length);  /* Was initialized by gfc_dep_difference.  */
    2885              :     }
    2886              :   else
    2887              :     {
    2888         1232 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
    2889              :                              fold_convert (gfc_charlen_type_node, end.expr),
    2890              :                              fold_convert (gfc_charlen_type_node, start.expr));
    2891         1232 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
    2892              :                              build_int_cst (gfc_charlen_type_node, 1), tmp);
    2893         1232 :       tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
    2894              :                              tmp, build_int_cst (gfc_charlen_type_node, 0));
    2895              :     }
    2896              : 
    2897         7252 :   se->string_length = tmp;
    2898         7252 : }
    2899              : 
    2900              : 
    2901              : /* Convert a derived type component reference.  */
    2902              : 
    2903              : void
    2904       189554 : gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
    2905              : {
    2906       189554 :   gfc_component *c;
    2907       189554 :   tree tmp;
    2908       189554 :   tree decl;
    2909       189554 :   tree field;
    2910       189554 :   tree context;
    2911              : 
    2912       189554 :   c = ref->u.c.component;
    2913              : 
    2914       189554 :   if (c->backend_decl == NULL_TREE
    2915            6 :       && ref->u.c.sym != NULL)
    2916            6 :     gfc_get_derived_type (ref->u.c.sym);
    2917              : 
    2918       189554 :   field = c->backend_decl;
    2919       189554 :   gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
    2920       189554 :   decl = se->expr;
    2921       189554 :   context = DECL_FIELD_CONTEXT (field);
    2922              : 
    2923              :   /* Components can correspond to fields of different containing
    2924              :      types, as components are created without context, whereas
    2925              :      a concrete use of a component has the type of decl as context.
    2926              :      So, if the type doesn't match, we search the corresponding
    2927              :      FIELD_DECL in the parent type.  To not waste too much time
    2928              :      we cache this result in norestrict_decl.
    2929              :      On the other hand, if the context is a UNION or a MAP (a
    2930              :      RECORD_TYPE within a UNION_TYPE) always use the given FIELD_DECL.  */
    2931              : 
    2932       189554 :   if (context != TREE_TYPE (decl)
    2933       189554 :       && !(   TREE_CODE (TREE_TYPE (field)) == UNION_TYPE /* Field is union */
    2934        14500 :            || TREE_CODE (context) == UNION_TYPE))         /* Field is map */
    2935              :     {
    2936        14500 :       tree f2 = c->norestrict_decl;
    2937        24429 :       if (!f2 || DECL_FIELD_CONTEXT (f2) != TREE_TYPE (decl))
    2938         9056 :         for (f2 = TYPE_FIELDS (TREE_TYPE (decl)); f2; f2 = DECL_CHAIN (f2))
    2939         9056 :           if (TREE_CODE (f2) == FIELD_DECL
    2940         9056 :               && DECL_NAME (f2) == DECL_NAME (field))
    2941              :             break;
    2942        14500 :       gcc_assert (f2);
    2943        14500 :       c->norestrict_decl = f2;
    2944        14500 :       field = f2;
    2945              :     }
    2946              : 
    2947       189554 :   if (ref->u.c.sym && ref->u.c.sym->ts.type == BT_CLASS
    2948            0 :       && strcmp ("_data", c->name) == 0)
    2949              :     {
    2950              :       /* Found a ref to the _data component.  Store the associated ref to
    2951              :          the vptr in se->class_vptr.  */
    2952            0 :       se->class_vptr = gfc_class_vptr_get (decl);
    2953              :     }
    2954              :   else
    2955       189554 :     se->class_vptr = NULL_TREE;
    2956              : 
    2957       189554 :   tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
    2958              :                          decl, field, NULL_TREE);
    2959              : 
    2960       189554 :   se->expr = tmp;
    2961              : 
    2962              :   /* Allocatable deferred char arrays are to be handled by the gfc_deferred_
    2963              :      strlen () conditional below.  */
    2964       189554 :   if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
    2965         9294 :       && !c->ts.deferred
    2966         6016 :       && !c->attr.pdt_string)
    2967              :     {
    2968         5638 :       tmp = c->ts.u.cl->backend_decl;
    2969              :       /* Components must always be constant length.  */
    2970         5638 :       gcc_assert (tmp && INTEGER_CST_P (tmp));
    2971         5638 :       se->string_length = tmp;
    2972              :     }
    2973              : 
    2974       189554 :   if (gfc_deferred_strlen (c, &field))
    2975              :     {
    2976         3656 :       tmp = fold_build3_loc (input_location, COMPONENT_REF,
    2977         3656 :                              TREE_TYPE (field),
    2978              :                              decl, field, NULL_TREE);
    2979         3656 :       se->string_length = tmp;
    2980              :     }
    2981              : 
    2982       189554 :   if (((c->attr.pointer || c->attr.allocatable)
    2983       110032 :        && (!c->attr.dimension && !c->attr.codimension)
    2984        59437 :        && c->ts.type != BT_CHARACTER)
    2985       132424 :       || c->attr.proc_pointer)
    2986        63919 :     se->expr = build_fold_indirect_ref_loc (input_location,
    2987              :                                         se->expr);
    2988       189554 : }
    2989              : 
    2990              : 
    2991              : /* This function deals with component references to components of the
    2992              :    parent type for derived type extensions.  */
    2993              : void
    2994        69841 : conv_parent_component_references (gfc_se * se, gfc_ref * ref)
    2995              : {
    2996        69841 :   gfc_component *c;
    2997        69841 :   gfc_component *cmp;
    2998        69841 :   gfc_symbol *dt;
    2999        69841 :   gfc_ref parent;
    3000              : 
    3001        69841 :   dt = ref->u.c.sym;
    3002        69841 :   c = ref->u.c.component;
    3003              : 
    3004              :   /* Return if the component is in this type, i.e. not in the parent type.  */
    3005       122251 :   for (cmp = dt->components; cmp; cmp = cmp->next)
    3006       110814 :     if (c == cmp)
    3007        58404 :       return;
    3008              : 
    3009              :   /* Build a gfc_ref to recursively call gfc_conv_component_ref.  */
    3010        11437 :   parent.type = REF_COMPONENT;
    3011        11437 :   parent.next = NULL;
    3012        11437 :   parent.u.c.sym = dt;
    3013        11437 :   parent.u.c.component = dt->components;
    3014              : 
    3015        11437 :   if (dt->backend_decl == NULL)
    3016            0 :     gfc_get_derived_type (dt);
    3017              : 
    3018              :   /* Build the reference and call self.  */
    3019        11437 :   gfc_conv_component_ref (se, &parent);
    3020        11437 :   parent.u.c.sym = dt->components->ts.u.derived;
    3021        11437 :   parent.u.c.component = c;
    3022        11437 :   conv_parent_component_references (se, &parent);
    3023              : }
    3024              : 
    3025              : 
    3026              : static void
    3027          639 : conv_inquiry (gfc_se * se, gfc_ref * ref, gfc_expr *expr, gfc_typespec *ts)
    3028              : {
    3029          639 :   tree res = se->expr;
    3030              : 
    3031          639 :   switch (ref->u.i)
    3032              :     {
    3033          265 :     case INQUIRY_RE:
    3034          530 :       res = fold_build1_loc (input_location, REALPART_EXPR,
    3035          265 :                              TREE_TYPE (TREE_TYPE (res)), res);
    3036          265 :       break;
    3037              : 
    3038          245 :     case INQUIRY_IM:
    3039          490 :       res = fold_build1_loc (input_location, IMAGPART_EXPR,
    3040          245 :                              TREE_TYPE (TREE_TYPE (res)), res);
    3041          245 :       break;
    3042              : 
    3043            7 :     case INQUIRY_KIND:
    3044            7 :       res = build_int_cst (gfc_typenode_for_spec (&expr->ts),
    3045            7 :                            ts->kind);
    3046            7 :       se->string_length = NULL_TREE;
    3047            7 :       break;
    3048              : 
    3049          122 :     case INQUIRY_LEN:
    3050          122 :       res = fold_convert (gfc_typenode_for_spec (&expr->ts),
    3051              :                           se->string_length);
    3052          122 :       se->string_length = NULL_TREE;
    3053          122 :       break;
    3054              : 
    3055            0 :     default:
    3056            0 :       gcc_unreachable ();
    3057              :     }
    3058          639 :   se->expr = res;
    3059          639 : }
    3060              : 
    3061              : /* Dereference VAR where needed if it is a pointer, reference, etc.
    3062              :    according to Fortran semantics.  */
    3063              : 
    3064              : tree
    3065      1493246 : gfc_maybe_dereference_var (gfc_symbol *sym, tree var, bool descriptor_only_p,
    3066              :                            bool is_classarray)
    3067              : {
    3068      1493246 :   if (!POINTER_TYPE_P (TREE_TYPE (var)))
    3069              :     return var;
    3070       306654 :   if (is_CFI_desc (sym, NULL))
    3071        11892 :     return build_fold_indirect_ref_loc (input_location, var);
    3072              : 
    3073              :   /* Characters are entirely different from other types, they are treated
    3074              :      separately.  */
    3075       294762 :   if (sym->ts.type == BT_CHARACTER)
    3076              :     {
    3077              :       /* Dereference character pointer dummy arguments
    3078              :          or results.  */
    3079        33597 :       if ((sym->attr.pointer || sym->attr.allocatable
    3080        19387 :            || (sym->as && sym->as->type == AS_ASSUMED_RANK))
    3081        14546 :           && (sym->attr.dummy
    3082        11230 :               || sym->attr.function
    3083        10832 :               || sym->attr.result))
    3084         4423 :         var = build_fold_indirect_ref_loc (input_location, var);
    3085              :     }
    3086       261165 :   else if (!sym->attr.value)
    3087              :     {
    3088              :       /* Dereference temporaries for class array dummy arguments.  */
    3089       179254 :       if (sym->attr.dummy && is_classarray
    3090       268322 :           && GFC_ARRAY_TYPE_P (TREE_TYPE (var)))
    3091              :         {
    3092         5755 :           if (!descriptor_only_p)
    3093         2984 :             var = GFC_DECL_SAVED_DESCRIPTOR (var);
    3094              : 
    3095         5755 :           var = build_fold_indirect_ref_loc (input_location, var);
    3096              :         }
    3097              : 
    3098              :       /* Dereference non-character scalar dummy arguments.  */
    3099       260067 :       if (sym->attr.dummy && !sym->attr.dimension
    3100       108488 :           && !(sym->attr.codimension && sym->attr.allocatable)
    3101       108415 :           && (sym->ts.type != BT_CLASS
    3102        20871 :               || (!CLASS_DATA (sym)->attr.dimension
    3103        12092 :                   && !(CLASS_DATA (sym)->attr.codimension
    3104          283 :                        && CLASS_DATA (sym)->attr.allocatable))))
    3105        99495 :         var = build_fold_indirect_ref_loc (input_location, var);
    3106              : 
    3107              :       /* Dereference scalar hidden result.  */
    3108       260067 :       if (flag_f2c && sym->ts.type == BT_COMPLEX
    3109          286 :           && (sym->attr.function || sym->attr.result)
    3110          108 :           && !sym->attr.dimension && !sym->attr.pointer
    3111           60 :           && !sym->attr.always_explicit)
    3112           36 :         var = build_fold_indirect_ref_loc (input_location, var);
    3113              : 
    3114              :       /* Dereference non-character, non-class pointer variables.
    3115              :          These must be dummies, results, or scalars.  */
    3116       260067 :       if (!is_classarray
    3117       251364 :           && (sym->attr.pointer || sym->attr.allocatable
    3118       199839 :               || gfc_is_associate_pointer (sym)
    3119       194652 :               || (sym->as && sym->as->type == AS_ASSUMED_RANK))
    3120       341337 :           && (sym->attr.dummy
    3121        38473 :               || sym->attr.function
    3122        37543 :               || sym->attr.result
    3123        36389 :               || (!sym->attr.dimension
    3124        36384 :                   && (!sym->attr.codimension || !sym->attr.allocatable))))
    3125        81265 :         var = build_fold_indirect_ref_loc (input_location, var);
    3126              :       /* Now treat the class array pointer variables accordingly.  */
    3127       178802 :       else if (sym->ts.type == BT_CLASS
    3128        21319 :                && sym->attr.dummy
    3129        20871 :                && (CLASS_DATA (sym)->attr.dimension
    3130        12092 :                    || CLASS_DATA (sym)->attr.codimension)
    3131         9062 :                && ((CLASS_DATA (sym)->as
    3132         9062 :                     && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
    3133         7999 :                    || CLASS_DATA (sym)->attr.allocatable
    3134         6530 :                    || CLASS_DATA (sym)->attr.class_pointer))
    3135         3165 :         var = build_fold_indirect_ref_loc (input_location, var);
    3136              :       /* And the case where a non-dummy, non-result, non-function,
    3137              :          non-allocable and non-pointer classarray is present.  This case was
    3138              :          previously covered by the first if, but with introducing the
    3139              :          condition !is_classarray there, that case has to be covered
    3140              :          explicitly.  */
    3141       175637 :       else if (sym->ts.type == BT_CLASS
    3142        18154 :                && !sym->attr.dummy
    3143          448 :                && !sym->attr.function
    3144          448 :                && !sym->attr.result
    3145          448 :                && (CLASS_DATA (sym)->attr.dimension
    3146            4 :                    || CLASS_DATA (sym)->attr.codimension)
    3147          448 :                && (sym->assoc
    3148            0 :                    || !CLASS_DATA (sym)->attr.allocatable)
    3149          448 :                && !CLASS_DATA (sym)->attr.class_pointer)
    3150          448 :         var = build_fold_indirect_ref_loc (input_location, var);
    3151              :     }
    3152              : 
    3153              :   return var;
    3154              : }
    3155              : 
    3156              : /* Return the contents of a variable. Also handles reference/pointer
    3157              :    variables (all Fortran pointer references are implicit).  */
    3158              : 
    3159              : static void
    3160      1651092 : gfc_conv_variable (gfc_se * se, gfc_expr * expr)
    3161              : {
    3162      1651092 :   gfc_ss *ss;
    3163      1651092 :   gfc_ref *ref;
    3164      1651092 :   gfc_symbol *sym;
    3165      1651092 :   tree parent_decl = NULL_TREE;
    3166      1651092 :   int parent_flag;
    3167      1651092 :   bool return_value;
    3168      1651092 :   bool alternate_entry;
    3169      1651092 :   bool entry_master;
    3170      1651092 :   bool is_classarray;
    3171      1651092 :   bool first_time = true;
    3172              : 
    3173      1651092 :   sym = expr->symtree->n.sym;
    3174      1651092 :   is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
    3175      1651092 :   ss = se->ss;
    3176      1651092 :   if (ss != NULL)
    3177              :     {
    3178       137045 :       gfc_ss_info *ss_info = ss->info;
    3179              : 
    3180              :       /* Check that something hasn't gone horribly wrong.  */
    3181       137045 :       gcc_assert (ss != gfc_ss_terminator);
    3182       137045 :       gcc_assert (ss_info->expr == expr);
    3183              : 
    3184              :       /* A scalarized term.  We already know the descriptor.  */
    3185       137045 :       se->expr = ss_info->data.array.descriptor;
    3186       137045 :       se->string_length = ss_info->string_length;
    3187       137045 :       ref = ss_info->data.array.ref;
    3188       137045 :       if (ref)
    3189       136691 :         gcc_assert (ref->type == REF_ARRAY
    3190              :                     && ref->u.ar.type != AR_ELEMENT);
    3191              :       else
    3192          354 :         gfc_conv_tmp_array_ref (se);
    3193       137045 :       se->class_container = ss_info->class_container;
    3194              :     }
    3195              :   else
    3196              :     {
    3197      1514047 :       tree se_expr = NULL_TREE;
    3198              : 
    3199      1514047 :       se->expr = gfc_get_symbol_decl (sym);
    3200              : 
    3201              :       /* Deal with references to a parent results or entries by storing
    3202              :          the current_function_decl and moving to the parent_decl.  */
    3203      1514047 :       return_value = sym->attr.function && sym->result == sym;
    3204        19856 :       alternate_entry = sym->attr.function && sym->attr.entry
    3205      1515186 :                         && sym->result == sym;
    3206      3028094 :       entry_master = sym->attr.result
    3207        15639 :                      && sym->ns->proc_name->attr.entry_master
    3208      1514428 :                      && !gfc_return_by_reference (sym->ns->proc_name);
    3209      1514047 :       if (current_function_decl)
    3210      1494531 :         parent_decl = DECL_CONTEXT (current_function_decl);
    3211              : 
    3212      1514047 :       if ((se->expr == parent_decl && return_value)
    3213      1513930 :            || (sym->ns && sym->ns->proc_name
    3214      1508356 :                && parent_decl
    3215      1488840 :                && sym->ns->proc_name->backend_decl == parent_decl
    3216        39565 :                && (alternate_entry || entry_master)))
    3217              :         parent_flag = 1;
    3218              :       else
    3219      1513897 :         parent_flag = 0;
    3220              : 
    3221              :       /* Special case for assigning the return value of a function.
    3222              :          Self recursive functions must have an explicit return value.  */
    3223      1514047 :       if (return_value && (se->expr == current_function_decl || parent_flag))
    3224        10666 :         se_expr = gfc_get_fake_result_decl (sym, parent_flag);
    3225              : 
    3226              :       /* Similarly for alternate entry points.  */
    3227      1503381 :       else if (alternate_entry
    3228         1106 :                && (sym->ns->proc_name->backend_decl == current_function_decl
    3229            0 :                    || parent_flag))
    3230              :         {
    3231         1106 :           gfc_entry_list *el = NULL;
    3232              : 
    3233         1705 :           for (el = sym->ns->entries; el; el = el->next)
    3234         1705 :             if (sym == el->sym)
    3235              :               {
    3236         1106 :                 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
    3237         1106 :                 break;
    3238              :               }
    3239              :         }
    3240              : 
    3241      1502275 :       else if (entry_master
    3242          295 :                && (sym->ns->proc_name->backend_decl == current_function_decl
    3243            0 :                    || parent_flag))
    3244          295 :         se_expr = gfc_get_fake_result_decl (sym, parent_flag);
    3245              : 
    3246        12067 :       if (se_expr)
    3247        12067 :         se->expr = se_expr;
    3248              : 
    3249              :       /* Procedure actual arguments.  Look out for temporary variables
    3250              :          with the same attributes as function values.  */
    3251      1501980 :       else if (!sym->attr.temporary
    3252      1501780 :                && sym->attr.flavor == FL_PROCEDURE
    3253        22952 :                && se->expr != current_function_decl)
    3254              :         {
    3255        22879 :           if (!sym->attr.dummy && !sym->attr.proc_pointer)
    3256              :             {
    3257        21167 :               gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
    3258        21167 :               se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
    3259              :             }
    3260              :           return;
    3261              :         }
    3262              : 
    3263      1491168 :       if (sym->ts.type == BT_CLASS
    3264        77245 :           && sym->attr.class_ok
    3265        77003 :           && sym->ts.u.derived->attr.is_class)
    3266              :         {
    3267        29568 :           if (is_classarray && DECL_LANG_SPECIFIC (se->expr)
    3268        85215 :               && GFC_DECL_SAVED_DESCRIPTOR (se->expr))
    3269         5897 :             se->class_container = GFC_DECL_SAVED_DESCRIPTOR (se->expr);
    3270              :           else
    3271        71106 :             se->class_container = se->expr;
    3272              :         }
    3273              : 
    3274              :       /* Dereference the expression, where needed.  */
    3275      1491168 :       if (se->class_container && CLASS_DATA (sym)->attr.codimension
    3276         2113 :           && !CLASS_DATA (sym)->attr.dimension)
    3277          910 :         se->expr
    3278          910 :           = gfc_maybe_dereference_var (sym, se->class_container,
    3279          910 :                                        se->descriptor_only, is_classarray);
    3280              :       else
    3281      1490258 :         se->expr
    3282      1490258 :           = gfc_maybe_dereference_var (sym, se->expr, se->descriptor_only,
    3283              :                                        is_classarray);
    3284              : 
    3285      1491168 :       ref = expr->ref;
    3286              :     }
    3287              : 
    3288              :   /* For character variables, also get the length.  */
    3289      1628213 :   if (sym->ts.type == BT_CHARACTER)
    3290              :     {
    3291              :       /* If the character length of an entry isn't set, get the length from
    3292              :          the master function instead.  */
    3293       168597 :       if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
    3294            0 :         se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
    3295              :       else
    3296       168597 :         se->string_length = sym->ts.u.cl->backend_decl;
    3297       168597 :       gcc_assert (se->string_length);
    3298              : 
    3299              :       /* For coarray strings return the pointer to the data and not the
    3300              :          descriptor.  */
    3301         5143 :       if (sym->attr.codimension && sym->attr.associate_var
    3302            6 :           && !se->descriptor_only
    3303       168603 :           && TREE_CODE (TREE_TYPE (se->expr)) != ARRAY_TYPE)
    3304            6 :         se->expr = gfc_conv_descriptor_data_get (se->expr);
    3305              :     }
    3306              : 
    3307              :   /* F202Y: Runtime warning that an assumed rank object is associated
    3308              :      with an assumed size object.  */
    3309      1628213 :   if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    3310        90899 :       && (gfc_option.allow_std & GFC_STD_F202Y)
    3311      1628447 :       && expr->rank == -1 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr)))
    3312              :     {
    3313           60 :       tree dim, lower, upper, cond;
    3314           60 :       char *msg;
    3315              : 
    3316           60 :       dim = fold_convert (gfc_array_dim_rank_type,
    3317              :                           gfc_conv_descriptor_rank_get (se->expr));
    3318           60 :       dim = fold_build2_loc (input_location, MINUS_EXPR,
    3319              :                              gfc_array_dim_rank_type, dim, gfc_rank_cst[1]);
    3320           60 :       lower = gfc_conv_descriptor_lbound_get (se->expr, dim);
    3321           60 :       upper = gfc_conv_descriptor_ubound_get (se->expr, dim);
    3322              : 
    3323           60 :       msg = xasprintf ("Assumed rank object %s is associated with an "
    3324              :                        "assumed size object", sym->name);
    3325           60 :       cond = fold_build2_loc (input_location, LT_EXPR,
    3326              :                               logical_type_node, upper, lower);
    3327           60 :       gfc_trans_runtime_check (false, true, cond, &se->pre,
    3328              :                                &gfc_current_locus, msg);
    3329           60 :       free (msg);
    3330              :     }
    3331              : 
    3332      1628213 :   gfc_typespec *ts = &sym->ts;
    3333      2084220 :   while (ref)
    3334              :     {
    3335       816791 :       switch (ref->type)
    3336              :         {
    3337       631089 :         case REF_ARRAY:
    3338              :           /* Return the descriptor if that's what we want and this is an array
    3339              :              section reference.  */
    3340       631089 :           if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
    3341              :             return;
    3342              : /* TODO: Pointers to single elements of array sections, eg elemental subs.  */
    3343              :           /* Return the descriptor for array pointers and allocations.  */
    3344       280097 :           if (se->want_pointer
    3345        25202 :               && ref->next == NULL && (se->descriptor_only))
    3346              :             return;
    3347              : 
    3348       270305 :           gfc_conv_array_ref (se, &ref->u.ar, expr, &expr->where);
    3349              :           /* Return a pointer to an element.  */
    3350       270305 :           break;
    3351              : 
    3352       178069 :         case REF_COMPONENT:
    3353       178069 :           ts = &ref->u.c.component->ts;
    3354       178069 :           if (first_time && IS_CLASS_ARRAY (sym) && sym->attr.dummy
    3355         6251 :               && se->descriptor_only && !CLASS_DATA (sym)->attr.allocatable
    3356         3292 :               && !CLASS_DATA (sym)->attr.class_pointer && CLASS_DATA (sym)->as
    3357         3292 :               && CLASS_DATA (sym)->as->type != AS_ASSUMED_RANK
    3358         2771 :               && strcmp ("_data", ref->u.c.component->name) == 0)
    3359              :             /* Skip the first ref of a _data component, because for class
    3360              :                arrays that one is already done by introducing a temporary
    3361              :                array descriptor.  */
    3362              :             break;
    3363              : 
    3364       175298 :           if (ref->u.c.sym->attr.extension)
    3365        58313 :             conv_parent_component_references (se, ref);
    3366              : 
    3367       175298 :           gfc_conv_component_ref (se, ref);
    3368              : 
    3369       175298 :           if (ref->u.c.component->ts.type == BT_CLASS
    3370        13522 :               && ref->u.c.component->attr.class_ok
    3371        13522 :               && ref->u.c.component->ts.u.derived->attr.is_class)
    3372        13522 :             se->class_container = se->expr;
    3373       161776 :           else if (!(ref->u.c.sym->attr.flavor == FL_DERIVED
    3374       159282 :                      && ref->u.c.sym->attr.is_class))
    3375        88696 :             se->class_container = NULL_TREE;
    3376              : 
    3377       175298 :           if (!ref->next && ref->u.c.sym->attr.codimension
    3378            0 :               && se->want_pointer && se->descriptor_only)
    3379              :             return;
    3380              : 
    3381              :           break;
    3382              : 
    3383         6994 :         case REF_SUBSTRING:
    3384         6994 :           gfc_conv_substring (se, ref, expr->ts.kind,
    3385         6994 :                               expr->symtree->name, &expr->where);
    3386         6994 :           break;
    3387              : 
    3388          639 :         case REF_INQUIRY:
    3389          639 :           conv_inquiry (se, ref, expr, ts);
    3390          639 :           break;
    3391              : 
    3392            0 :         default:
    3393            0 :           gcc_unreachable ();
    3394       456007 :           break;
    3395              :         }
    3396       456007 :       first_time = false;
    3397       456007 :       ref = ref->next;
    3398              :     }
    3399              :   /* Pointer assignment, allocation or pass by reference.  Arrays are handled
    3400              :      separately.  */
    3401      1267429 :   if (se->want_pointer)
    3402              :     {
    3403       139165 :       if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr))
    3404         8235 :         gfc_conv_string_parameter (se);
    3405              :       else
    3406       130930 :         se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
    3407              :     }
    3408              : }
    3409              : 
    3410              : 
    3411              : /* Unary ops are easy... Or they would be if ! was a valid op.  */
    3412              : 
    3413              : static void
    3414        29011 : gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
    3415              : {
    3416        29011 :   gfc_se operand;
    3417        29011 :   tree type;
    3418              : 
    3419        29011 :   gcc_assert (expr->ts.type != BT_CHARACTER);
    3420              :   /* Initialize the operand.  */
    3421        29011 :   gfc_init_se (&operand, se);
    3422        29011 :   gfc_conv_expr_val (&operand, expr->value.op.op1);
    3423        29011 :   gfc_add_block_to_block (&se->pre, &operand.pre);
    3424              : 
    3425        29011 :   type = gfc_typenode_for_spec (&expr->ts);
    3426              : 
    3427              :   /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
    3428              :      We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
    3429              :      All other unary operators have an equivalent GIMPLE unary operator.  */
    3430        29011 :   if (code == TRUTH_NOT_EXPR)
    3431        20404 :     se->expr = fold_build2_loc (input_location, EQ_EXPR, type, operand.expr,
    3432              :                                 build_int_cst (type, 0));
    3433              :   else
    3434         8607 :     se->expr = fold_build1_loc (input_location, code, type, operand.expr);
    3435              : 
    3436        29011 : }
    3437              : 
    3438              : /* Expand power operator to optimal multiplications when a value is raised
    3439              :    to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
    3440              :    Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
    3441              :    Programming", 3rd Edition, 1998.  */
    3442              : 
    3443              : /* This code is mostly duplicated from expand_powi in the backend.
    3444              :    We establish the "optimal power tree" lookup table with the defined size.
    3445              :    The items in the table are the exponents used to calculate the index
    3446              :    exponents. Any integer n less than the value can get an "addition chain",
    3447              :    with the first node being one.  */
    3448              : #define POWI_TABLE_SIZE 256
    3449              : 
    3450              : /* The table is from builtins.cc.  */
    3451              : static const unsigned char powi_table[POWI_TABLE_SIZE] =
    3452              :   {
    3453              :       0,   1,   1,   2,   2,   3,   3,   4,  /*   0 -   7 */
    3454              :       4,   6,   5,   6,   6,  10,   7,   9,  /*   8 -  15 */
    3455              :       8,  16,   9,  16,  10,  12,  11,  13,  /*  16 -  23 */
    3456              :      12,  17,  13,  18,  14,  24,  15,  26,  /*  24 -  31 */
    3457              :      16,  17,  17,  19,  18,  33,  19,  26,  /*  32 -  39 */
    3458              :      20,  25,  21,  40,  22,  27,  23,  44,  /*  40 -  47 */
    3459              :      24,  32,  25,  34,  26,  29,  27,  44,  /*  48 -  55 */
    3460              :      28,  31,  29,  34,  30,  60,  31,  36,  /*  56 -  63 */
    3461              :      32,  64,  33,  34,  34,  46,  35,  37,  /*  64 -  71 */
    3462              :      36,  65,  37,  50,  38,  48,  39,  69,  /*  72 -  79 */
    3463              :      40,  49,  41,  43,  42,  51,  43,  58,  /*  80 -  87 */
    3464              :      44,  64,  45,  47,  46,  59,  47,  76,  /*  88 -  95 */
    3465              :      48,  65,  49,  66,  50,  67,  51,  66,  /*  96 - 103 */
    3466              :      52,  70,  53,  74,  54, 104,  55,  74,  /* 104 - 111 */
    3467              :      56,  64,  57,  69,  58,  78,  59,  68,  /* 112 - 119 */
    3468              :      60,  61,  61,  80,  62,  75,  63,  68,  /* 120 - 127 */
    3469              :      64,  65,  65, 128,  66, 129,  67,  90,  /* 128 - 135 */
    3470              :      68,  73,  69, 131,  70,  94,  71,  88,  /* 136 - 143 */
    3471              :      72, 128,  73,  98,  74, 132,  75, 121,  /* 144 - 151 */
    3472              :      76, 102,  77, 124,  78, 132,  79, 106,  /* 152 - 159 */
    3473              :      80,  97,  81, 160,  82,  99,  83, 134,  /* 160 - 167 */
    3474              :      84,  86,  85,  95,  86, 160,  87, 100,  /* 168 - 175 */
    3475              :      88, 113,  89,  98,  90, 107,  91, 122,  /* 176 - 183 */
    3476              :      92, 111,  93, 102,  94, 126,  95, 150,  /* 184 - 191 */
    3477              :      96, 128,  97, 130,  98, 133,  99, 195,  /* 192 - 199 */
    3478              :     100, 128, 101, 123, 102, 164, 103, 138,  /* 200 - 207 */
    3479              :     104, 145, 105, 146, 106, 109, 107, 149,  /* 208 - 215 */
    3480              :     108, 200, 109, 146, 110, 170, 111, 157,  /* 216 - 223 */
    3481              :     112, 128, 113, 130, 114, 182, 115, 132,  /* 224 - 231 */
    3482              :     116, 200, 117, 132, 118, 158, 119, 206,  /* 232 - 239 */
    3483              :     120, 240, 121, 162, 122, 147, 123, 152,  /* 240 - 247 */
    3484              :     124, 166, 125, 214, 126, 138, 127, 153,  /* 248 - 255 */
    3485              :   };
    3486              : 
    3487              : /* If n is larger than lookup table's max index, we use the "window
    3488              :    method".  */
    3489              : #define POWI_WINDOW_SIZE 3
    3490              : 
    3491              : /* Recursive function to expand the power operator. The temporary
    3492              :    values are put in tmpvar. The function returns tmpvar[1] ** n.  */
    3493              : static tree
    3494       178323 : gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
    3495              : {
    3496       178323 :   tree op0;
    3497       178323 :   tree op1;
    3498       178323 :   tree tmp;
    3499       178323 :   int digit;
    3500              : 
    3501       178323 :   if (n < POWI_TABLE_SIZE)
    3502              :     {
    3503       137336 :       if (tmpvar[n])
    3504              :         return tmpvar[n];
    3505              : 
    3506        56612 :       op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
    3507        56612 :       op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
    3508              :     }
    3509        40987 :   else if (n & 1)
    3510              :     {
    3511        10015 :       digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
    3512        10015 :       op0 = gfc_conv_powi (se, n - digit, tmpvar);
    3513        10015 :       op1 = gfc_conv_powi (se, digit, tmpvar);
    3514              :     }
    3515              :   else
    3516              :     {
    3517        30972 :       op0 = gfc_conv_powi (se, n >> 1, tmpvar);
    3518        30972 :       op1 = op0;
    3519              :     }
    3520              : 
    3521        97599 :   tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
    3522        97599 :   tmp = gfc_evaluate_now (tmp, &se->pre);
    3523              : 
    3524        97599 :   if (n < POWI_TABLE_SIZE)
    3525        56612 :     tmpvar[n] = tmp;
    3526              : 
    3527              :   return tmp;
    3528              : }
    3529              : 
    3530              : 
    3531              : /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
    3532              :    return 1. Else return 0 and a call to runtime library functions
    3533              :    will have to be built.  */
    3534              : static int
    3535         3305 : gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
    3536              : {
    3537         3305 :   tree cond;
    3538         3305 :   tree tmp;
    3539         3305 :   tree type;
    3540         3305 :   tree vartmp[POWI_TABLE_SIZE];
    3541         3305 :   HOST_WIDE_INT m;
    3542         3305 :   unsigned HOST_WIDE_INT n;
    3543         3305 :   int sgn;
    3544         3305 :   wi::tree_to_wide_ref wrhs = wi::to_wide (rhs);
    3545              : 
    3546              :   /* If exponent is too large, we won't expand it anyway, so don't bother
    3547              :      with large integer values.  */
    3548         3305 :   if (!wi::fits_shwi_p (wrhs))
    3549              :     return 0;
    3550              : 
    3551         2945 :   m = wrhs.to_shwi ();
    3552              :   /* Use the wide_int's routine to reliably get the absolute value on all
    3553              :      platforms.  Then convert it to a HOST_WIDE_INT like above.  */
    3554         2945 :   n = wi::abs (wrhs).to_shwi ();
    3555              : 
    3556         2945 :   type = TREE_TYPE (lhs);
    3557         2945 :   sgn = tree_int_cst_sgn (rhs);
    3558              : 
    3559         2945 :   if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
    3560         5890 :        || optimize_size) && (m > 2 || m < -1))
    3561              :     return 0;
    3562              : 
    3563              :   /* rhs == 0  */
    3564         1639 :   if (sgn == 0)
    3565              :     {
    3566          282 :       se->expr = gfc_build_const (type, integer_one_node);
    3567          282 :       return 1;
    3568              :     }
    3569              : 
    3570              :   /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1.  */
    3571         1357 :   if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
    3572              :     {
    3573          220 :       tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    3574          220 :                              lhs, build_int_cst (TREE_TYPE (lhs), -1));
    3575          220 :       cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    3576          220 :                               lhs, build_int_cst (TREE_TYPE (lhs), 1));
    3577              : 
    3578              :       /* If rhs is even,
    3579              :          result = (lhs == 1 || lhs == -1) ? 1 : 0.  */
    3580          220 :       if ((n & 1) == 0)
    3581              :         {
    3582          104 :           tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    3583              :                                  logical_type_node, tmp, cond);
    3584          104 :           se->expr = fold_build3_loc (input_location, COND_EXPR, type,
    3585              :                                       tmp, build_int_cst (type, 1),
    3586              :                                       build_int_cst (type, 0));
    3587          104 :           return 1;
    3588              :         }
    3589              :       /* If rhs is odd,
    3590              :          result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0.  */
    3591          116 :       tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp,
    3592              :                              build_int_cst (type, -1),
    3593              :                              build_int_cst (type, 0));
    3594          116 :       se->expr = fold_build3_loc (input_location, COND_EXPR, type,
    3595              :                                   cond, build_int_cst (type, 1), tmp);
    3596          116 :       return 1;
    3597              :     }
    3598              : 
    3599         1137 :   memset (vartmp, 0, sizeof (vartmp));
    3600         1137 :   vartmp[1] = lhs;
    3601         1137 :   if (sgn == -1)
    3602              :     {
    3603          141 :       tmp = gfc_build_const (type, integer_one_node);
    3604          141 :       vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
    3605              :                                    vartmp[1]);
    3606              :     }
    3607              : 
    3608         1137 :   se->expr = gfc_conv_powi (se, n, vartmp);
    3609              : 
    3610         1137 :   return 1;
    3611              : }
    3612              : 
    3613              : /* Convert lhs**rhs, for constant rhs, when both are unsigned.
    3614              :    Method:
    3615              :    if (rhs == 0)      ! Checked here.
    3616              :      return 1;
    3617              :    if (lhs & 1 == 1)  ! odd_cnd
    3618              :      {
    3619              :        if (bit_size(rhs) < bit_size(lhs))  ! Checked here.
    3620              :          return lhs ** rhs;
    3621              : 
    3622              :        mask = 1 << (bit_size(a) - 1) / 2;
    3623              :        return lhs ** (n & rhs);
    3624              :      }
    3625              :    if (rhs > bit_size(lhs))  ! Checked here.
    3626              :      return 0;
    3627              : 
    3628              :    return lhs ** rhs;
    3629              : */
    3630              : 
    3631              : static int
    3632        15120 : gfc_conv_cst_uint_power (gfc_se * se, tree lhs, tree rhs)
    3633              : {
    3634        15120 :   tree type = TREE_TYPE (lhs);
    3635        15120 :   tree tmp, is_odd, odd_branch, even_branch;
    3636        15120 :   unsigned HOST_WIDE_INT lhs_prec, rhs_prec;
    3637        15120 :   wi::tree_to_wide_ref wrhs = wi::to_wide (rhs);
    3638        15120 :   unsigned HOST_WIDE_INT n, n_odd;
    3639        15120 :   tree vartmp_odd[POWI_TABLE_SIZE], vartmp_even[POWI_TABLE_SIZE];
    3640              : 
    3641              :   /* Anything ** 0 is one.  */
    3642        15120 :   if (integer_zerop (rhs))
    3643              :     {
    3644         1800 :       se->expr = build_int_cst (type, 1);
    3645         1800 :       return 1;
    3646              :     }
    3647              : 
    3648        13320 :   if (!wi::fits_uhwi_p (wrhs))
    3649              :     return 0;
    3650              : 
    3651        12960 :   n = wrhs.to_uhwi ();
    3652              : 
    3653              :   /* tmp = a & 1; . */
    3654        12960 :   tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type,
    3655              :                          lhs, build_int_cst (type, 1));
    3656        12960 :   is_odd = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    3657              :                             tmp, build_int_cst (type, 1));
    3658              : 
    3659        12960 :   lhs_prec = TYPE_PRECISION (type);
    3660        12960 :   rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs));
    3661              : 
    3662        12960 :   if (rhs_prec >= lhs_prec && lhs_prec <= HOST_BITS_PER_WIDE_INT)
    3663              :     {
    3664         7044 :       unsigned HOST_WIDE_INT mask = (HOST_WIDE_INT_1U << (lhs_prec - 1)) - 1;
    3665         7044 :       n_odd = n & mask;
    3666              :     }
    3667              :   else
    3668              :     n_odd = n;
    3669              : 
    3670        12960 :   memset (vartmp_odd, 0, sizeof (vartmp_odd));
    3671        12960 :   vartmp_odd[0] = build_int_cst (type, 1);
    3672        12960 :   vartmp_odd[1] = lhs;
    3673        12960 :   odd_branch = gfc_conv_powi (se, n_odd, vartmp_odd);
    3674        12960 :   even_branch = NULL_TREE;
    3675              : 
    3676        12960 :   if (n > lhs_prec)
    3677         4260 :     even_branch = build_int_cst (type, 0);
    3678              :   else
    3679              :     {
    3680         8700 :       if (n_odd != n)
    3681              :         {
    3682            0 :           memset (vartmp_even, 0, sizeof (vartmp_even));
    3683            0 :           vartmp_even[0] = build_int_cst (type, 1);
    3684            0 :           vartmp_even[1] = lhs;
    3685            0 :           even_branch = gfc_conv_powi (se, n, vartmp_even);
    3686              :         }
    3687              :     }
    3688         4260 :   if (even_branch != NULL_TREE)
    3689         4260 :     se->expr = fold_build3_loc (input_location, COND_EXPR, type, is_odd,
    3690              :                                 odd_branch, even_branch);
    3691              :   else
    3692         8700 :     se->expr = odd_branch;
    3693              : 
    3694              :   return 1;
    3695              : }
    3696              : 
    3697              : /* Power op (**).  Constant integer exponent and powers of 2 have special
    3698              :    handling.  */
    3699              : 
    3700              : static void
    3701        49186 : gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
    3702              : {
    3703        49186 :   tree gfc_int4_type_node;
    3704        49186 :   int kind;
    3705        49186 :   int ikind;
    3706        49186 :   int res_ikind_1, res_ikind_2;
    3707        49186 :   gfc_se lse;
    3708        49186 :   gfc_se rse;
    3709        49186 :   tree fndecl = NULL;
    3710              : 
    3711        49186 :   gfc_init_se (&lse, se);
    3712        49186 :   gfc_conv_expr_val (&lse, expr->value.op.op1);
    3713        49186 :   lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
    3714        49186 :   gfc_add_block_to_block (&se->pre, &lse.pre);
    3715              : 
    3716        49186 :   gfc_init_se (&rse, se);
    3717        49186 :   gfc_conv_expr_val (&rse, expr->value.op.op2);
    3718        49186 :   gfc_add_block_to_block (&se->pre, &rse.pre);
    3719              : 
    3720        49186 :   if (expr->value.op.op2->expr_type == EXPR_CONSTANT)
    3721              :     {
    3722        17563 :       if (expr->value.op.op2->ts.type == BT_INTEGER)
    3723              :         {
    3724         2292 :           if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
    3725        20486 :             return;
    3726              :         }
    3727        15271 :       else if (expr->value.op.op2->ts.type == BT_UNSIGNED)
    3728              :         {
    3729        15120 :           if (gfc_conv_cst_uint_power (se, lse.expr, rse.expr))
    3730              :             return;
    3731              :         }
    3732              :     }
    3733              : 
    3734        32787 :   if ((expr->value.op.op2->ts.type == BT_INTEGER
    3735        31468 :        || expr->value.op.op2->ts.type == BT_UNSIGNED)
    3736        31919 :       && expr->value.op.op2->expr_type == EXPR_CONSTANT)
    3737         1013 :     if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
    3738              :       return;
    3739              : 
    3740        32787 :   if (INTEGER_CST_P (lse.expr)
    3741        15380 :       && TREE_CODE (TREE_TYPE (rse.expr)) == INTEGER_TYPE
    3742        48167 :       && expr->value.op.op2->ts.type == BT_INTEGER)
    3743              :     {
    3744          260 :       wi::tree_to_wide_ref wlhs = wi::to_wide (lse.expr);
    3745          260 :       HOST_WIDE_INT v;
    3746          260 :       unsigned HOST_WIDE_INT w;
    3747          260 :       int kind, ikind, bit_size;
    3748              : 
    3749          260 :       v = wlhs.to_shwi ();
    3750          260 :       w = absu_hwi (v);
    3751              : 
    3752          260 :       kind = expr->value.op.op1->ts.kind;
    3753          260 :       ikind = gfc_validate_kind (BT_INTEGER, kind, false);
    3754          260 :       bit_size = gfc_integer_kinds[ikind].bit_size;
    3755              : 
    3756          260 :       if (v == 1)
    3757              :         {
    3758              :           /* 1**something is always 1.  */
    3759           35 :           se->expr = build_int_cst (TREE_TYPE (lse.expr), 1);
    3760          248 :           return;
    3761              :         }
    3762          225 :       else if (v == -1)
    3763              :         {
    3764              :           /* (-1)**n is 1 - ((n & 1) << 1) */
    3765           34 :           tree type;
    3766           34 :           tree tmp;
    3767              : 
    3768           34 :           type = TREE_TYPE (lse.expr);
    3769           34 :           tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type,
    3770              :                                  rse.expr, build_int_cst (type, 1));
    3771           34 :           tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
    3772              :                                  tmp, build_int_cst (type, 1));
    3773           34 :           tmp = fold_build2_loc (input_location, MINUS_EXPR, type,
    3774              :                                  build_int_cst (type, 1), tmp);
    3775           34 :           se->expr = tmp;
    3776           34 :           return;
    3777              :         }
    3778          191 :       else if (w > 0 && ((w & (w-1)) == 0) && ((w >> (bit_size-1)) == 0))
    3779              :         {
    3780              :           /* Here v is +/- 2**e.  The further simplification uses
    3781              :              2**n = 1<<n, 4**n = 1<<(n+n), 8**n = 1 <<(3*n), 16**n =
    3782              :              1<<(4*n), etc., but we have to make sure to return zero
    3783              :              if the number of bits is too large. */
    3784          179 :           tree lshift;
    3785          179 :           tree type;
    3786          179 :           tree shift;
    3787          179 :           tree ge;
    3788          179 :           tree cond;
    3789          179 :           tree num_bits;
    3790          179 :           tree cond2;
    3791          179 :           tree tmp1;
    3792              : 
    3793          179 :           type = TREE_TYPE (lse.expr);
    3794              : 
    3795          179 :           if (w == 2)
    3796          119 :             shift = rse.expr;
    3797           60 :           else if (w == 4)
    3798           12 :             shift = fold_build2_loc (input_location, PLUS_EXPR,
    3799           12 :                                      TREE_TYPE (rse.expr),
    3800              :                                        rse.expr, rse.expr);
    3801              :           else
    3802              :             {
    3803              :               /* use popcount for fast log2(w) */
    3804           48 :               int e = wi::popcount (w-1);
    3805           96 :               shift = fold_build2_loc (input_location, MULT_EXPR,
    3806           48 :                                        TREE_TYPE (rse.expr),
    3807           48 :                                        build_int_cst (TREE_TYPE (rse.expr), e),
    3808              :                                        rse.expr);
    3809              :             }
    3810              : 
    3811          179 :           lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type,
    3812              :                                     build_int_cst (type, 1), shift);
    3813          179 :           ge = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
    3814              :                                 rse.expr, build_int_cst (type, 0));
    3815          179 :           cond = fold_build3_loc (input_location, COND_EXPR, type, ge, lshift,
    3816              :                                  build_int_cst (type, 0));
    3817          179 :           num_bits = build_int_cst (TREE_TYPE (rse.expr), TYPE_PRECISION (type));
    3818          179 :           cond2 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
    3819              :                                    rse.expr, num_bits);
    3820          179 :           tmp1 = fold_build3_loc (input_location, COND_EXPR, type, cond2,
    3821              :                                   build_int_cst (type, 0), cond);
    3822          179 :           if (v > 0)
    3823              :             {
    3824              :               se->expr = tmp1;
    3825              :             }
    3826              :           else
    3827              :             {
    3828              :               /* for v < 0, calculate v**n = |v|**n * (-1)**n */
    3829           42 :               tree tmp2;
    3830           42 :               tmp2 = fold_build2_loc (input_location, BIT_AND_EXPR, type,
    3831              :                                       rse.expr, build_int_cst (type, 1));
    3832           42 :               tmp2 = fold_build2_loc (input_location, LSHIFT_EXPR, type,
    3833              :                                       tmp2, build_int_cst (type, 1));
    3834           42 :               tmp2 = fold_build2_loc (input_location, MINUS_EXPR, type,
    3835              :                                       build_int_cst (type, 1), tmp2);
    3836           42 :               se->expr = fold_build2_loc (input_location, MULT_EXPR, type,
    3837              :                                           tmp1, tmp2);
    3838              :             }
    3839          179 :           return;
    3840              :         }
    3841              :     }
    3842              :   /* Handle unsigned separate from signed above, things would be too
    3843              :      complicated otherwise.  */
    3844              : 
    3845        32539 :   if (INTEGER_CST_P (lse.expr) && expr->value.op.op1->ts.type == BT_UNSIGNED)
    3846              :     {
    3847        15120 :       gfc_expr * op1 = expr->value.op.op1;
    3848        15120 :       tree type;
    3849              : 
    3850        15120 :       type = TREE_TYPE (lse.expr);
    3851              : 
    3852        15120 :       if (mpz_cmp_ui (op1->value.integer, 1) == 0)
    3853              :         {
    3854              :           /* 1**something is always 1.  */
    3855         1260 :           se->expr = build_int_cst (type, 1);
    3856         1260 :           return;
    3857              :         }
    3858              : 
    3859              :       /* Simplify 2u**x to a shift, with the value set to zero if it falls
    3860              :        outside the range.  */
    3861        26460 :       if (mpz_popcount (op1->value.integer) == 1)
    3862              :         {
    3863         2520 :           tree prec_m1, lim, shift, lshift, cond, tmp;
    3864         2520 :           tree rtype = TREE_TYPE (rse.expr);
    3865         2520 :           int e = mpz_scan1 (op1->value.integer, 0);
    3866              : 
    3867         2520 :           shift = fold_build2_loc (input_location, MULT_EXPR,
    3868         2520 :                                    rtype, build_int_cst (rtype, e),
    3869              :                                    rse.expr);
    3870         2520 :           lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type,
    3871              :                                     build_int_cst (type, 1), shift);
    3872         5040 :           prec_m1 = fold_build2_loc (input_location, MINUS_EXPR, rtype,
    3873         2520 :                                      build_int_cst (rtype, TYPE_PRECISION (type)),
    3874              :                                      build_int_cst (rtype, 1));
    3875         2520 :           lim = fold_build2_loc (input_location, TRUNC_DIV_EXPR, rtype,
    3876         2520 :                                  prec_m1, build_int_cst (rtype, e));
    3877         2520 :           cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    3878              :                                   rse.expr, lim);
    3879         2520 :           tmp = fold_build3_loc (input_location, COND_EXPR, type, cond,
    3880              :                                  build_int_cst (type, 0), lshift);
    3881         2520 :           se->expr = tmp;
    3882         2520 :           return;
    3883              :         }
    3884              :     }
    3885              : 
    3886        28759 :   gfc_int4_type_node = gfc_get_int_type (4);
    3887              : 
    3888              :   /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
    3889              :      library routine.  But in the end, we have to convert the result back
    3890              :      if this case applies -- with res_ikind_K, we keep track whether operand K
    3891              :      falls into this case.  */
    3892        28759 :   res_ikind_1 = -1;
    3893        28759 :   res_ikind_2 = -1;
    3894              : 
    3895        28759 :   kind = expr->value.op.op1->ts.kind;
    3896        28759 :   switch (expr->value.op.op2->ts.type)
    3897              :     {
    3898         1071 :     case BT_INTEGER:
    3899         1071 :       ikind = expr->value.op.op2->ts.kind;
    3900         1071 :       switch (ikind)
    3901              :         {
    3902          168 :         case 1:
    3903          168 :         case 2:
    3904          168 :           rse.expr = convert (gfc_int4_type_node, rse.expr);
    3905          168 :           res_ikind_2 = ikind;
    3906              :           /* Fall through.  */
    3907              : 
    3908              :         case 4:
    3909              :           ikind = 0;
    3910              :           break;
    3911              : 
    3912          182 :         case 8:
    3913          182 :           ikind = 1;
    3914          182 :           break;
    3915              : 
    3916            6 :         case 16:
    3917            6 :           ikind = 2;
    3918            6 :           break;
    3919              : 
    3920            0 :         default:
    3921            0 :           gcc_unreachable ();
    3922              :         }
    3923         1071 :       switch (kind)
    3924              :         {
    3925            0 :         case 1:
    3926            0 :         case 2:
    3927            0 :           if (expr->value.op.op1->ts.type == BT_INTEGER)
    3928              :             {
    3929            0 :               lse.expr = convert (gfc_int4_type_node, lse.expr);
    3930            0 :               res_ikind_1 = kind;
    3931              :             }
    3932              :           else
    3933            0 :             gcc_unreachable ();
    3934              :           /* Fall through.  */
    3935              : 
    3936              :         case 4:
    3937              :           kind = 0;
    3938              :           break;
    3939              : 
    3940          212 :         case 8:
    3941          212 :           kind = 1;
    3942          212 :           break;
    3943              : 
    3944            6 :         case 10:
    3945            6 :           kind = 2;
    3946            6 :           break;
    3947              : 
    3948           18 :         case 16:
    3949           18 :           kind = 3;
    3950           18 :           break;
    3951              : 
    3952            0 :         default:
    3953            0 :           gcc_unreachable ();
    3954              :         }
    3955              : 
    3956         1071 :       switch (expr->value.op.op1->ts.type)
    3957              :         {
    3958          129 :         case BT_INTEGER:
    3959          129 :           if (kind == 3) /* Case 16 was not handled properly above.  */
    3960              :             kind = 2;
    3961          129 :           fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
    3962          129 :           break;
    3963              : 
    3964          710 :         case BT_REAL:
    3965              :           /* Use builtins for real ** int4.  */
    3966              : 
    3967          710 :           if (real_minus_onep (lse.expr))
    3968              :             {
    3969              :               /* (-1.0)**n is (real) (1 - ((n & 1) << 1)), see the integer case
    3970              :                  above.  */
    3971              : 
    3972           59 :               tree lhs_type, rhs_type;
    3973           59 :               tree tmp;
    3974           59 :               lhs_type = TREE_TYPE (lse.expr);
    3975           59 :               rhs_type = TREE_TYPE (rse.expr);
    3976           59 :               tmp = fold_build2_loc (input_location, BIT_AND_EXPR, rhs_type,
    3977              :                                      rse.expr, build_int_cst (rhs_type, 1));
    3978           59 :               tmp = fold_build2_loc (input_location, LSHIFT_EXPR, rhs_type,
    3979              :                                      tmp, build_int_cst (rhs_type, 1));
    3980           59 :               tmp = fold_build2_loc (input_location, MINUS_EXPR, rhs_type,
    3981              :                                      build_int_cst (rhs_type, 1), tmp);
    3982           59 :               se->expr = fold_convert (lhs_type, tmp);
    3983           59 :               return;
    3984              :             }
    3985              : 
    3986          651 :           if (ikind == 0)
    3987              :             {
    3988          555 :               switch (kind)
    3989              :                 {
    3990          391 :                 case 0:
    3991          391 :                   fndecl = builtin_decl_explicit (BUILT_IN_POWIF);
    3992          391 :                   break;
    3993              : 
    3994          146 :                 case 1:
    3995          146 :                   fndecl = builtin_decl_explicit (BUILT_IN_POWI);
    3996          146 :                   break;
    3997              : 
    3998            6 :                 case 2:
    3999            6 :                   fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
    4000            6 :                   break;
    4001              : 
    4002           12 :                 case 3:
    4003              :                   /* Use the __builtin_powil() only if real(kind=16) is
    4004              :                      actually the C long double type.  */
    4005           12 :                   if (!gfc_real16_is_float128)
    4006            0 :                     fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
    4007              :                   break;
    4008              : 
    4009              :                 default:
    4010              :                   gcc_unreachable ();
    4011              :                 }
    4012              :             }
    4013              : 
    4014              :           /* If we don't have a good builtin for this, go for the
    4015              :              library function.  */
    4016          543 :           if (!fndecl)
    4017          108 :             fndecl = gfor_fndecl_math_powi[kind][ikind].real;
    4018              :           break;
    4019              : 
    4020          232 :         case BT_COMPLEX:
    4021          232 :           fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
    4022          232 :           break;
    4023              : 
    4024            0 :         default:
    4025            0 :           gcc_unreachable ();
    4026              :         }
    4027              :       break;
    4028              : 
    4029          139 :     case BT_REAL:
    4030          139 :       fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
    4031          139 :       break;
    4032              : 
    4033          729 :     case BT_COMPLEX:
    4034          729 :       fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
    4035          729 :       break;
    4036              : 
    4037        26820 :     case BT_UNSIGNED:
    4038        26820 :       {
    4039              :         /* Valid kinds for unsigned are 1, 2, 4, 8, 16.  Instead of using a
    4040              :            large switch statement, let's just use __builtin_ctz.  */
    4041        26820 :         int base = __builtin_ctz (expr->value.op.op1->ts.kind);
    4042        26820 :         int expon = __builtin_ctz (expr->value.op.op2->ts.kind);
    4043        26820 :         fndecl = gfor_fndecl_unsigned_pow_list[base][expon];
    4044              :       }
    4045        26820 :       break;
    4046              : 
    4047            0 :     default:
    4048            0 :       gcc_unreachable ();
    4049        28700 :       break;
    4050              :     }
    4051              : 
    4052        28700 :   se->expr = build_call_expr_loc (input_location,
    4053              :                               fndecl, 2, lse.expr, rse.expr);
    4054              : 
    4055              :   /* Convert the result back if it is of wrong integer kind.  */
    4056        28700 :   if (res_ikind_1 != -1 && res_ikind_2 != -1)
    4057              :     {
    4058              :       /* We want the maximum of both operand kinds as result.  */
    4059            0 :       if (res_ikind_1 < res_ikind_2)
    4060            0 :         res_ikind_1 = res_ikind_2;
    4061            0 :       se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr);
    4062              :     }
    4063              : }
    4064              : 
    4065              : 
    4066              : /* Generate code to allocate a string temporary.  */
    4067              : 
    4068              : tree
    4069         5000 : gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
    4070              : {
    4071         5000 :   tree var;
    4072         5000 :   tree tmp;
    4073              : 
    4074         5000 :   if (gfc_can_put_var_on_stack (len))
    4075              :     {
    4076              :       /* Create a temporary variable to hold the result.  */
    4077         4754 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
    4078         2377 :                              TREE_TYPE (len), len,
    4079         2377 :                              build_int_cst (TREE_TYPE (len), 1));
    4080         2377 :       tmp = build_range_type (gfc_charlen_type_node, size_zero_node, tmp);
    4081              : 
    4082         2377 :       if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
    4083         2377 :         tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
    4084              :       else
    4085            0 :         tmp = build_array_type (TREE_TYPE (type), tmp);
    4086              : 
    4087         2377 :       var = gfc_create_var (tmp, "str");
    4088         2377 :       var = gfc_build_addr_expr (type, var);
    4089              :     }
    4090              :   else
    4091              :     {
    4092              :       /* Allocate a temporary to hold the result.  */
    4093         2623 :       var = gfc_create_var (type, "pstr");
    4094         2623 :       gcc_assert (POINTER_TYPE_P (type));
    4095         2623 :       tmp = TREE_TYPE (type);
    4096         2623 :       if (TREE_CODE (tmp) == ARRAY_TYPE)
    4097         2623 :         tmp = TREE_TYPE (tmp);
    4098         2623 :       tmp = TYPE_SIZE_UNIT (tmp);
    4099         2623 :       tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
    4100              :                             fold_convert (size_type_node, len),
    4101              :                             fold_convert (size_type_node, tmp));
    4102         2623 :       tmp = gfc_call_malloc (&se->pre, type, tmp);
    4103         2623 :       gfc_add_modify (&se->pre, var, tmp);
    4104              : 
    4105              :       /* Free the temporary afterwards.  */
    4106         2623 :       tmp = gfc_call_free (var);
    4107         2623 :       gfc_add_expr_to_block (&se->post, tmp);
    4108              :     }
    4109              : 
    4110         5000 :   return var;
    4111              : }
    4112              : 
    4113              : 
    4114              : /* Handle a string concatenation operation.  A temporary will be allocated to
    4115              :    hold the result.  */
    4116              : 
    4117              : static void
    4118         1348 : gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
    4119              : {
    4120         1348 :   gfc_se lse, rse;
    4121         1348 :   tree len, type, var, tmp, fndecl;
    4122              : 
    4123         1348 :   gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
    4124              :               && expr->value.op.op2->ts.type == BT_CHARACTER);
    4125         1348 :   gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
    4126              : 
    4127         1348 :   gfc_init_se (&lse, se);
    4128         1348 :   gfc_conv_expr (&lse, expr->value.op.op1);
    4129         1348 :   gfc_conv_string_parameter (&lse);
    4130         1348 :   gfc_init_se (&rse, se);
    4131         1348 :   gfc_conv_expr (&rse, expr->value.op.op2);
    4132         1348 :   gfc_conv_string_parameter (&rse);
    4133              : 
    4134         1348 :   gfc_add_block_to_block (&se->pre, &lse.pre);
    4135         1348 :   gfc_add_block_to_block (&se->pre, &rse.pre);
    4136              : 
    4137         1348 :   type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
    4138         1348 :   len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
    4139         1348 :   if (len == NULL_TREE)
    4140              :     {
    4141         1129 :       len = fold_build2_loc (input_location, PLUS_EXPR,
    4142              :                              gfc_charlen_type_node,
    4143              :                              fold_convert (gfc_charlen_type_node,
    4144              :                                            lse.string_length),
    4145              :                              fold_convert (gfc_charlen_type_node,
    4146              :                                            rse.string_length));
    4147              :     }
    4148              : 
    4149         1348 :   type = build_pointer_type (type);
    4150              : 
    4151         1348 :   var = gfc_conv_string_tmp (se, type, len);
    4152              : 
    4153              :   /* Do the actual concatenation.  */
    4154         1348 :   if (expr->ts.kind == 1)
    4155         1257 :     fndecl = gfor_fndecl_concat_string;
    4156           91 :   else if (expr->ts.kind == 4)
    4157           91 :     fndecl = gfor_fndecl_concat_string_char4;
    4158              :   else
    4159            0 :     gcc_unreachable ();
    4160              : 
    4161         1348 :   tmp = build_call_expr_loc (input_location,
    4162              :                          fndecl, 6, len, var, lse.string_length, lse.expr,
    4163              :                          rse.string_length, rse.expr);
    4164         1348 :   gfc_add_expr_to_block (&se->pre, tmp);
    4165              : 
    4166              :   /* Add the cleanup for the operands.  */
    4167         1348 :   gfc_add_block_to_block (&se->pre, &rse.post);
    4168         1348 :   gfc_add_block_to_block (&se->pre, &lse.post);
    4169              : 
    4170         1348 :   se->expr = var;
    4171         1348 :   se->string_length = len;
    4172         1348 : }
    4173              : 
    4174              : 
    4175              : /* Return the variable that E puts in parentheses, or NULL.  */
    4176              : 
    4177              : static gfc_expr *
    4178          719 : parenthesized_variable (gfc_expr *e)
    4179              : {
    4180            0 :   if (e->expr_type == EXPR_OP
    4181           48 :       && e->value.op.op == INTRINSIC_PARENTHESES
    4182           48 :       && e->value.op.op1->expr_type == EXPR_VARIABLE)
    4183            0 :     return e->value.op.op1;
    4184              :   return NULL;
    4185              : }
    4186              : 
    4187              : 
    4188              : /* Translates an op expression. Common (binary) cases are handled by this
    4189              :    function, others are passed on. Recursion is used in either case.
    4190              :    We use the fact that (op1.ts == op2.ts) (except for the power
    4191              :    operator **).
    4192              :    Operators need no special handling for scalarized expressions as long as
    4193              :    they call gfc_conv_simple_val to get their operands.
    4194              :    Character strings get special handling.  */
    4195              : 
    4196              : static void
    4197       517008 : gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
    4198              : {
    4199       517008 :   enum tree_code code;
    4200       517008 :   gfc_se lse;
    4201       517008 :   gfc_se rse;
    4202       517008 :   tree tmp, type;
    4203       517008 :   int lop;
    4204       517008 :   int checkstring;
    4205              : 
    4206       517008 :   checkstring = 0;
    4207       517008 :   lop = 0;
    4208       517008 :   switch (expr->value.op.op)
    4209              :     {
    4210        15478 :     case INTRINSIC_PARENTHESES:
    4211        15478 :       if ((expr->ts.type == BT_REAL || expr->ts.type == BT_COMPLEX)
    4212         3772 :           && flag_protect_parens)
    4213              :         {
    4214         3638 :           gfc_conv_unary_op (PAREN_EXPR, se, expr);
    4215         3638 :           gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
    4216        91391 :           return;
    4217              :         }
    4218              : 
    4219              :       /* Fallthrough.  */
    4220        11846 :     case INTRINSIC_UPLUS:
    4221        11846 :       gfc_conv_expr (se, expr->value.op.op1);
    4222        11846 :       return;
    4223              : 
    4224         4969 :     case INTRINSIC_UMINUS:
    4225         4969 :       gfc_conv_unary_op (NEGATE_EXPR, se, expr);
    4226         4969 :       return;
    4227              : 
    4228        20404 :     case INTRINSIC_NOT:
    4229        20404 :       gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
    4230        20404 :       return;
    4231              : 
    4232              :     case INTRINSIC_PLUS:
    4233              :       code = PLUS_EXPR;
    4234              :       break;
    4235              : 
    4236        30205 :     case INTRINSIC_MINUS:
    4237        30205 :       code = MINUS_EXPR;
    4238        30205 :       break;
    4239              : 
    4240        33905 :     case INTRINSIC_TIMES:
    4241        33905 :       code = MULT_EXPR;
    4242        33905 :       break;
    4243              : 
    4244         7246 :     case INTRINSIC_DIVIDE:
    4245              :       /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
    4246              :          an integer or unsigned, we must round towards zero, so we use a
    4247              :          TRUNC_DIV_EXPR.  */
    4248         7246 :       if (expr->ts.type == BT_INTEGER || expr->ts.type == BT_UNSIGNED)
    4249              :         code = TRUNC_DIV_EXPR;
    4250              :       else
    4251       425617 :         code = RDIV_EXPR;
    4252              :       break;
    4253              : 
    4254        49186 :     case INTRINSIC_POWER:
    4255        49186 :       gfc_conv_power_op (se, expr);
    4256        49186 :       return;
    4257              : 
    4258         1348 :     case INTRINSIC_CONCAT:
    4259         1348 :       gfc_conv_concat_op (se, expr);
    4260         1348 :       return;
    4261              : 
    4262         4901 :     case INTRINSIC_AND:
    4263         4901 :       code = flag_frontend_optimize ? TRUTH_ANDIF_EXPR : TRUTH_AND_EXPR;
    4264              :       lop = 1;
    4265              :       break;
    4266              : 
    4267        56331 :     case INTRINSIC_OR:
    4268        56331 :       code = flag_frontend_optimize ? TRUTH_ORIF_EXPR : TRUTH_OR_EXPR;
    4269              :       lop = 1;
    4270              :       break;
    4271              : 
    4272              :       /* EQV and NEQV only work on logicals, but since we represent them
    4273              :          as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE.  */
    4274        12877 :     case INTRINSIC_EQ:
    4275        12877 :     case INTRINSIC_EQ_OS:
    4276        12877 :     case INTRINSIC_EQV:
    4277        12877 :       code = EQ_EXPR;
    4278        12877 :       checkstring = 1;
    4279        12877 :       lop = 1;
    4280        12877 :       break;
    4281              : 
    4282       211875 :     case INTRINSIC_NE:
    4283       211875 :     case INTRINSIC_NE_OS:
    4284       211875 :     case INTRINSIC_NEQV:
    4285       211875 :       code = NE_EXPR;
    4286       211875 :       checkstring = 1;
    4287       211875 :       lop = 1;
    4288       211875 :       break;
    4289              : 
    4290        12227 :     case INTRINSIC_GT:
    4291        12227 :     case INTRINSIC_GT_OS:
    4292        12227 :       code = GT_EXPR;
    4293        12227 :       checkstring = 1;
    4294        12227 :       lop = 1;
    4295        12227 :       break;
    4296              : 
    4297         1677 :     case INTRINSIC_GE:
    4298         1677 :     case INTRINSIC_GE_OS:
    4299         1677 :       code = GE_EXPR;
    4300         1677 :       checkstring = 1;
    4301         1677 :       lop = 1;
    4302         1677 :       break;
    4303              : 
    4304         4411 :     case INTRINSIC_LT:
    4305         4411 :     case INTRINSIC_LT_OS:
    4306         4411 :       code = LT_EXPR;
    4307         4411 :       checkstring = 1;
    4308         4411 :       lop = 1;
    4309         4411 :       break;
    4310              : 
    4311         2612 :     case INTRINSIC_LE:
    4312         2612 :     case INTRINSIC_LE_OS:
    4313         2612 :       code = LE_EXPR;
    4314         2612 :       checkstring = 1;
    4315         2612 :       lop = 1;
    4316         2612 :       break;
    4317              : 
    4318            0 :     case INTRINSIC_USER:
    4319            0 :     case INTRINSIC_ASSIGN:
    4320              :       /* These should be converted into function calls by the frontend.  */
    4321            0 :       gcc_unreachable ();
    4322              : 
    4323            0 :     default:
    4324            0 :       fatal_error (input_location, "Unknown intrinsic op");
    4325       425617 :       return;
    4326              :     }
    4327              : 
    4328              :   /* The only exception to this is **, which is handled separately anyway.  */
    4329       425617 :   gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
    4330              : 
    4331       425617 :   if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
    4332       390985 :     checkstring = 0;
    4333              : 
    4334              :   /* lhs */
    4335       425617 :   gfc_init_se (&lse, se);
    4336       425617 :   gfc_conv_expr (&lse, expr->value.op.op1);
    4337       425617 :   gfc_add_block_to_block (&se->pre, &lse.pre);
    4338              : 
    4339              :   /* rhs */
    4340       425617 :   gfc_init_se (&rse, se);
    4341       425617 :   gfc_conv_expr (&rse, expr->value.op.op2);
    4342       425617 :   gfc_add_block_to_block (&se->pre, &rse.pre);
    4343              : 
    4344       425617 :   if (checkstring)
    4345              :     {
    4346        34632 :       gfc_conv_string_parameter (&lse);
    4347        34632 :       gfc_conv_string_parameter (&rse);
    4348              : 
    4349        69264 :       lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
    4350              :                                            rse.string_length, rse.expr,
    4351        34632 :                                            expr->value.op.op1->ts.kind,
    4352              :                                            code);
    4353        34632 :       rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
    4354        34632 :       gfc_add_block_to_block (&lse.post, &rse.post);
    4355              :     }
    4356              : 
    4357       425617 :   type = gfc_typenode_for_spec (&expr->ts);
    4358              : 
    4359       425617 :   if (lop)
    4360              :     {
    4361              :       // Inhibit overeager optimization of Cray pointer comparisons (PR106692).
    4362       306911 :       if (expr->value.op.op1->expr_type == EXPR_VARIABLE
    4363       173272 :           && expr->value.op.op1->ts.type == BT_INTEGER
    4364        75672 :           && expr->value.op.op1->symtree
    4365        75672 :           && expr->value.op.op1->symtree->n.sym->attr.cray_pointer)
    4366           12 :         TREE_THIS_VOLATILE (lse.expr) = 1;
    4367              : 
    4368       306911 :       if (expr->value.op.op2->expr_type == EXPR_VARIABLE
    4369        73052 :           && expr->value.op.op2->ts.type == BT_INTEGER
    4370        13505 :           && expr->value.op.op2->symtree
    4371        13505 :           && expr->value.op.op2->symtree->n.sym->attr.cray_pointer)
    4372           12 :         TREE_THIS_VOLATILE (rse.expr) = 1;
    4373              : 
    4374              :       /* The result of logical ops is always logical_type_node.  */
    4375       306911 :       tmp = fold_build2_loc (input_location, code, logical_type_node,
    4376              :                              lse.expr, rse.expr);
    4377       306911 :       se->expr = convert (type, tmp);
    4378              :     }
    4379              :   else
    4380       118706 :     se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
    4381              : 
    4382              :   /* Add the post blocks.  */
    4383       425617 :   gfc_add_block_to_block (&se->post, &rse.post);
    4384       425617 :   gfc_add_block_to_block (&se->post, &lse.post);
    4385              : }
    4386              : 
    4387              : static void
    4388          159 : gfc_conv_conditional_expr (gfc_se *se, gfc_expr *expr)
    4389              : {
    4390          159 :   gfc_se cond_se, true_se, false_se;
    4391          159 :   tree condition, true_val, false_val;
    4392          159 :   tree type;
    4393              : 
    4394          159 :   gfc_init_se (&cond_se, se);
    4395          159 :   gfc_init_se (&true_se, se);
    4396          159 :   gfc_init_se (&false_se, se);
    4397              : 
    4398          159 :   gfc_conv_expr (&cond_se, expr->value.conditional.condition);
    4399          159 :   gfc_add_block_to_block (&se->pre, &cond_se.pre);
    4400          159 :   condition = gfc_evaluate_now (cond_se.expr, &se->pre);
    4401              : 
    4402          159 :   true_se.want_pointer = se->want_pointer;
    4403          159 :   gfc_conv_expr (&true_se, expr->value.conditional.true_expr);
    4404          159 :   true_val = true_se.expr;
    4405          159 :   false_se.want_pointer = se->want_pointer;
    4406          159 :   gfc_conv_expr (&false_se, expr->value.conditional.false_expr);
    4407          159 :   false_val = false_se.expr;
    4408              : 
    4409          159 :   if (true_se.pre.head != NULL_TREE || false_se.pre.head != NULL_TREE)
    4410           24 :     gfc_add_expr_to_block (
    4411              :       &se->pre,
    4412              :       fold_build3_loc (input_location, COND_EXPR, void_type_node, condition,
    4413           24 :                        true_se.pre.head != NULL_TREE
    4414            6 :                          ? gfc_finish_block (&true_se.pre)
    4415           18 :                          : build_empty_stmt (input_location),
    4416           24 :                        false_se.pre.head != NULL_TREE
    4417           24 :                          ? gfc_finish_block (&false_se.pre)
    4418            0 :                          : build_empty_stmt (input_location)));
    4419              : 
    4420          159 :   if (true_se.post.head != NULL_TREE || false_se.post.head != NULL_TREE)
    4421            6 :     gfc_add_expr_to_block (
    4422              :       &se->post,
    4423              :       fold_build3_loc (input_location, COND_EXPR, void_type_node, condition,
    4424            6 :                        true_se.post.head != NULL_TREE
    4425            0 :                          ? gfc_finish_block (&true_se.post)
    4426            6 :                          : build_empty_stmt (input_location),
    4427            6 :                        false_se.post.head != NULL_TREE
    4428            6 :                          ? gfc_finish_block (&false_se.post)
    4429            0 :                          : build_empty_stmt (input_location)));
    4430              : 
    4431          159 :   type = gfc_typenode_for_spec (&expr->ts);
    4432          159 :   if (se->want_pointer)
    4433           18 :     type = build_pointer_type (type);
    4434              : 
    4435          159 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, condition,
    4436              :                               true_val, false_val);
    4437          159 :   if (expr->ts.type == BT_CHARACTER)
    4438           66 :     se->string_length
    4439           66 :       = fold_build3_loc (input_location, COND_EXPR, gfc_charlen_type_node,
    4440              :                          condition, true_se.string_length,
    4441              :                          false_se.string_length);
    4442          159 : }
    4443              : 
    4444              : /* If a string's length is one, we convert it to a single character.  */
    4445              : 
    4446              : tree
    4447       143684 : gfc_string_to_single_character (tree len, tree str, int kind)
    4448              : {
    4449              : 
    4450       143684 :   if (len == NULL
    4451       143684 :       || !tree_fits_uhwi_p (len)
    4452       263845 :       || !POINTER_TYPE_P (TREE_TYPE (str)))
    4453              :     return NULL_TREE;
    4454              : 
    4455       120109 :   if (TREE_INT_CST_LOW (len) == 1)
    4456              :     {
    4457        22947 :       str = fold_convert (gfc_get_pchar_type (kind), str);
    4458        22947 :       return build_fold_indirect_ref_loc (input_location, str);
    4459              :     }
    4460              : 
    4461        97162 :   if (kind == 1
    4462        79672 :       && TREE_CODE (str) == ADDR_EXPR
    4463        68482 :       && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
    4464        48885 :       && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
    4465        30269 :       && array_ref_low_bound (TREE_OPERAND (str, 0))
    4466        30269 :          == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
    4467        30269 :       && TREE_INT_CST_LOW (len) > 1
    4468       125573 :       && TREE_INT_CST_LOW (len)
    4469              :          == (unsigned HOST_WIDE_INT)
    4470        28411 :             TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
    4471              :     {
    4472        28411 :       tree ret = fold_convert (gfc_get_pchar_type (kind), str);
    4473        28411 :       ret = build_fold_indirect_ref_loc (input_location, ret);
    4474        28411 :       if (TREE_CODE (ret) == INTEGER_CST)
    4475              :         {
    4476        28411 :           tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
    4477        28411 :           int i, length = TREE_STRING_LENGTH (string_cst);
    4478        28411 :           const char *ptr = TREE_STRING_POINTER (string_cst);
    4479              : 
    4480        42545 :           for (i = 1; i < length; i++)
    4481        41847 :             if (ptr[i] != ' ')
    4482              :               return NULL_TREE;
    4483              : 
    4484              :           return ret;
    4485              :         }
    4486              :     }
    4487              : 
    4488              :   return NULL_TREE;
    4489              : }
    4490              : 
    4491              : 
    4492              : static void
    4493          220 : conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
    4494              : {
    4495          220 :   gcc_assert (expr);
    4496              : 
    4497              :   /* We used to modify the tree here. Now it is done earlier in
    4498              :      the front-end, so we only check it here to avoid regressions.  */
    4499          220 :   if (sym->backend_decl)
    4500              :     {
    4501           67 :       gcc_assert (TREE_CODE (TREE_TYPE (sym->backend_decl)) == INTEGER_TYPE);
    4502           67 :       gcc_assert (TYPE_UNSIGNED (TREE_TYPE (sym->backend_decl)) == 1);
    4503           67 :       gcc_assert (TYPE_PRECISION (TREE_TYPE (sym->backend_decl)) == CHAR_TYPE_SIZE);
    4504           67 :       gcc_assert (DECL_BY_REFERENCE (sym->backend_decl) == 0);
    4505              :     }
    4506              : 
    4507              :   /* If we have a constant character expression, make it into an
    4508              :       integer of type C char.  */
    4509          220 :   if ((*expr)->expr_type == EXPR_CONSTANT)
    4510              :     {
    4511          166 :       gfc_typespec ts;
    4512          166 :       gfc_clear_ts (&ts);
    4513              : 
    4514          332 :       gfc_expr *tmp = gfc_get_int_expr (gfc_default_character_kind, NULL,
    4515          166 :                                         (*expr)->value.character.string[0]);
    4516          166 :       gfc_replace_expr (*expr, tmp);
    4517              :     }
    4518           54 :   else if (se != NULL)
    4519              :     {
    4520           54 :       gfc_conv_expr (se, *expr);
    4521           54 :       gfc_conv_string_parameter (se);
    4522          108 :       se->expr = gfc_string_to_single_character (integer_one_node,
    4523           54 :                                                  se->expr, (*expr)->ts.kind);
    4524           54 :       se->string_length = integer_one_node;
    4525              :     }
    4526          220 : }
    4527              : 
    4528              : /* Helper function for gfc_build_compare_string.  Return LEN_TRIM value
    4529              :    if STR is a string literal, otherwise return -1.  */
    4530              : 
    4531              : static int
    4532        32842 : gfc_optimize_len_trim (tree len, tree str, int kind)
    4533              : {
    4534        32842 :   if (kind == 1
    4535        27750 :       && TREE_CODE (str) == ADDR_EXPR
    4536        24322 :       && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
    4537        15538 :       && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
    4538        10047 :       && array_ref_low_bound (TREE_OPERAND (str, 0))
    4539        10047 :          == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
    4540        10047 :       && tree_fits_uhwi_p (len)
    4541        10047 :       && tree_to_uhwi (len) >= 1
    4542        32842 :       && tree_to_uhwi (len)
    4543        10003 :          == (unsigned HOST_WIDE_INT)
    4544        10003 :             TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
    4545              :     {
    4546        10003 :       tree folded = fold_convert (gfc_get_pchar_type (kind), str);
    4547        10003 :       folded = build_fold_indirect_ref_loc (input_location, folded);
    4548        10003 :       if (TREE_CODE (folded) == INTEGER_CST)
    4549              :         {
    4550        10003 :           tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
    4551        10003 :           int length = TREE_STRING_LENGTH (string_cst);
    4552        10003 :           const char *ptr = TREE_STRING_POINTER (string_cst);
    4553              : 
    4554        14927 :           for (; length > 0; length--)
    4555        14927 :             if (ptr[length - 1] != ' ')
    4556              :               break;
    4557              : 
    4558              :           return length;
    4559              :         }
    4560              :     }
    4561              :   return -1;
    4562              : }
    4563              : 
    4564              : /* Helper to build a call to memcmp.  */
    4565              : 
    4566              : static tree
    4567        13393 : build_memcmp_call (tree s1, tree s2, tree n)
    4568              : {
    4569        13393 :   tree tmp;
    4570              : 
    4571        13393 :   if (!POINTER_TYPE_P (TREE_TYPE (s1)))
    4572            0 :     s1 = gfc_build_addr_expr (pvoid_type_node, s1);
    4573              :   else
    4574        13393 :     s1 = fold_convert (pvoid_type_node, s1);
    4575              : 
    4576        13393 :   if (!POINTER_TYPE_P (TREE_TYPE (s2)))
    4577            0 :     s2 = gfc_build_addr_expr (pvoid_type_node, s2);
    4578              :   else
    4579        13393 :     s2 = fold_convert (pvoid_type_node, s2);
    4580              : 
    4581        13393 :   n = fold_convert (size_type_node, n);
    4582              : 
    4583        13393 :   tmp = build_call_expr_loc (input_location,
    4584              :                              builtin_decl_explicit (BUILT_IN_MEMCMP),
    4585              :                              3, s1, s2, n);
    4586              : 
    4587        13393 :   return fold_convert (integer_type_node, tmp);
    4588              : }
    4589              : 
    4590              : /* Compare two strings. If they are all single characters, the result is the
    4591              :    subtraction of them. Otherwise, we build a library call.  */
    4592              : 
    4593              : tree
    4594        34731 : gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
    4595              :                           enum tree_code code)
    4596              : {
    4597        34731 :   tree sc1;
    4598        34731 :   tree sc2;
    4599        34731 :   tree fndecl;
    4600              : 
    4601        34731 :   gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
    4602        34731 :   gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
    4603              : 
    4604        34731 :   sc1 = gfc_string_to_single_character (len1, str1, kind);
    4605        34731 :   sc2 = gfc_string_to_single_character (len2, str2, kind);
    4606              : 
    4607        34731 :   if (sc1 != NULL_TREE && sc2 != NULL_TREE)
    4608              :     {
    4609              :       /* Deal with single character specially.  */
    4610         4887 :       sc1 = fold_convert (integer_type_node, sc1);
    4611         4887 :       sc2 = fold_convert (integer_type_node, sc2);
    4612         4887 :       return fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
    4613         4887 :                               sc1, sc2);
    4614              :     }
    4615              : 
    4616        29844 :   if ((code == EQ_EXPR || code == NE_EXPR)
    4617        29282 :       && optimize
    4618        24527 :       && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
    4619              :     {
    4620              :       /* If one string is a string literal with LEN_TRIM longer
    4621              :          than the length of the second string, the strings
    4622              :          compare unequal.  */
    4623        16421 :       int len = gfc_optimize_len_trim (len1, str1, kind);
    4624        16421 :       if (len > 0 && compare_tree_int (len2, len) < 0)
    4625            0 :         return integer_one_node;
    4626        16421 :       len = gfc_optimize_len_trim (len2, str2, kind);
    4627        16421 :       if (len > 0 && compare_tree_int (len1, len) < 0)
    4628            0 :         return integer_one_node;
    4629              :     }
    4630              : 
    4631              :   /* We can compare via memcpy if the strings are known to be equal
    4632              :      in length and they are
    4633              :      - kind=1
    4634              :      - kind=4 and the comparison is for (in)equality.  */
    4635              : 
    4636        20008 :   if (INTEGER_CST_P (len1) && INTEGER_CST_P (len2)
    4637        19670 :       && tree_int_cst_equal (len1, len2)
    4638        43297 :       && (kind == 1 || code == EQ_EXPR || code == NE_EXPR))
    4639              :     {
    4640        13393 :       tree tmp;
    4641        13393 :       tree chartype;
    4642              : 
    4643        13393 :       chartype = gfc_get_char_type (kind);
    4644        13393 :       tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE(len1),
    4645        13393 :                              fold_convert (TREE_TYPE(len1),
    4646              :                                            TYPE_SIZE_UNIT(chartype)),
    4647              :                              len1);
    4648        13393 :       return build_memcmp_call (str1, str2, tmp);
    4649              :     }
    4650              : 
    4651              :   /* Build a call for the comparison.  */
    4652        16451 :   if (kind == 1)
    4653        13596 :     fndecl = gfor_fndecl_compare_string;
    4654         2855 :   else if (kind == 4)
    4655         2855 :     fndecl = gfor_fndecl_compare_string_char4;
    4656              :   else
    4657            0 :     gcc_unreachable ();
    4658              : 
    4659        16451 :   return build_call_expr_loc (input_location, fndecl, 4,
    4660        16451 :                               len1, str1, len2, str2);
    4661              : }
    4662              : 
    4663              : 
    4664              : /* Return the backend_decl for a procedure pointer component.  */
    4665              : 
    4666              : static tree
    4667         1980 : get_proc_ptr_comp (gfc_expr *e)
    4668              : {
    4669         1980 :   gfc_se comp_se;
    4670         1980 :   gfc_expr *e2;
    4671         1980 :   expr_t old_type;
    4672              : 
    4673         1980 :   gfc_init_se (&comp_se, NULL);
    4674         1980 :   e2 = gfc_copy_expr (e);
    4675              :   /* We have to restore the expr type later so that gfc_free_expr frees
    4676              :      the exact same thing that was allocated.
    4677              :      TODO: This is ugly.  */
    4678         1980 :   old_type = e2->expr_type;
    4679         1980 :   e2->expr_type = EXPR_VARIABLE;
    4680         1980 :   gfc_conv_expr (&comp_se, e2);
    4681         1980 :   e2->expr_type = old_type;
    4682         1980 :   gfc_free_expr (e2);
    4683         1980 :   return build_fold_addr_expr_loc (input_location, comp_se.expr);
    4684              : }
    4685              : 
    4686              : 
    4687              : /* Convert a typebound function reference from a class object.  */
    4688              : static void
    4689           80 : conv_base_obj_fcn_val (gfc_se * se, tree base_object, gfc_expr * expr)
    4690              : {
    4691           80 :   gfc_ref *ref;
    4692           80 :   tree var;
    4693              : 
    4694           80 :   if (!VAR_P (base_object))
    4695              :     {
    4696            0 :       var = gfc_create_var (TREE_TYPE (base_object), NULL);
    4697            0 :       gfc_add_modify (&se->pre, var, base_object);
    4698              :     }
    4699           80 :   se->expr = gfc_class_vptr_get (base_object);
    4700           80 :   se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
    4701           80 :   ref = expr->ref;
    4702          308 :   while (ref && ref->next)
    4703              :     ref = ref->next;
    4704           80 :   gcc_assert (ref && ref->type == REF_COMPONENT);
    4705           80 :   if (ref->u.c.sym->attr.extension)
    4706            0 :     conv_parent_component_references (se, ref);
    4707           80 :   gfc_conv_component_ref (se, ref);
    4708           80 :   se->expr = build_fold_addr_expr_loc (input_location, se->expr);
    4709           80 : }
    4710              : 
    4711              : static tree
    4712       131175 : get_builtin_fn (gfc_symbol * sym)
    4713              : {
    4714       131175 :   if (!gfc_option.disable_omp_is_initial_device
    4715       131171 :       && flag_openmp && sym->attr.function && sym->ts.type == BT_LOGICAL
    4716          631 :       && !strcmp (sym->name, "omp_is_initial_device"))
    4717           41 :     return builtin_decl_explicit (BUILT_IN_OMP_IS_INITIAL_DEVICE);
    4718              : 
    4719       131134 :   if (!gfc_option.disable_omp_get_initial_device
    4720       131127 :       && flag_openmp && sym->attr.function && sym->ts.type == BT_INTEGER
    4721         4338 :       && !strcmp (sym->name, "omp_get_initial_device"))
    4722           29 :     return builtin_decl_explicit (BUILT_IN_OMP_GET_INITIAL_DEVICE);
    4723              : 
    4724       131105 :   if (!gfc_option.disable_omp_get_num_devices
    4725       131098 :       && flag_openmp && sym->attr.function && sym->ts.type == BT_INTEGER
    4726         4309 :       && !strcmp (sym->name, "omp_get_num_devices"))
    4727          109 :     return builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_DEVICES);
    4728              : 
    4729       130996 :   if (!gfc_option.disable_acc_on_device
    4730       130816 :       && flag_openacc && sym->attr.function && sym->ts.type == BT_LOGICAL
    4731         1169 :       && !strcmp (sym->name, "acc_on_device_h"))
    4732          390 :     return builtin_decl_explicit (BUILT_IN_ACC_ON_DEVICE);
    4733              : 
    4734              :   return NULL_TREE;
    4735              : }
    4736              : 
    4737              : static tree
    4738          569 : update_builtin_function (tree fn_call, gfc_symbol *sym)
    4739              : {
    4740          569 :   tree fn = TREE_OPERAND (CALL_EXPR_FN (fn_call), 0);
    4741              : 
    4742          569 :   if (DECL_FUNCTION_CODE (fn) == BUILT_IN_OMP_IS_INITIAL_DEVICE)
    4743              :      /* In Fortran omp_is_initial_device returns logical(4)
    4744              :         but the builtin uses 'int'.  */
    4745           41 :     return fold_convert (TREE_TYPE (TREE_TYPE (sym->backend_decl)), fn_call);
    4746              : 
    4747          528 :   else if (DECL_FUNCTION_CODE (fn) == BUILT_IN_ACC_ON_DEVICE)
    4748              :     {
    4749              :       /* Likewise for the return type; additionally, the argument it a
    4750              :          call-by-value int, Fortran has a by-reference 'integer(4)'.  */
    4751          390 :       tree arg = build_fold_indirect_ref_loc (input_location,
    4752          390 :                                               CALL_EXPR_ARG (fn_call, 0));
    4753          390 :       CALL_EXPR_ARG (fn_call, 0) = fold_convert (integer_type_node, arg);
    4754          390 :       return fold_convert (TREE_TYPE (TREE_TYPE (sym->backend_decl)), fn_call);
    4755              :     }
    4756              :   return fn_call;
    4757              : }
    4758              : 
    4759              : static void
    4760       133981 : conv_function_val (gfc_se * se, bool *is_builtin, gfc_symbol * sym,
    4761              :                    gfc_expr * expr, gfc_actual_arglist *actual_args)
    4762              : {
    4763       133981 :   tree tmp;
    4764              : 
    4765       133981 :   if (gfc_is_proc_ptr_comp (expr))
    4766         1980 :     tmp = get_proc_ptr_comp (expr);
    4767       132001 :   else if (sym->attr.dummy)
    4768              :     {
    4769          826 :       tmp = gfc_get_symbol_decl (sym);
    4770          826 :       if (sym->attr.proc_pointer)
    4771           89 :         tmp = build_fold_indirect_ref_loc (input_location,
    4772              :                                        tmp);
    4773          826 :       gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
    4774              :               && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
    4775              :     }
    4776              :   else
    4777              :     {
    4778       131175 :       if (!sym->backend_decl)
    4779        33088 :         sym->backend_decl = gfc_get_extern_function_decl (sym, actual_args);
    4780              : 
    4781       131175 :       if ((tmp = get_builtin_fn (sym)) != NULL_TREE)
    4782          569 :         *is_builtin = true;
    4783              :       else
    4784              :         {
    4785       130606 :           TREE_USED (sym->backend_decl) = 1;
    4786       130606 :           tmp = sym->backend_decl;
    4787              :         }
    4788              : 
    4789       131175 :       if (sym->attr.cray_pointee)
    4790              :         {
    4791              :           /* TODO - make the cray pointee a pointer to a procedure,
    4792              :              assign the pointer to it and use it for the call.  This
    4793              :              will do for now!  */
    4794           19 :           tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
    4795           19 :                          gfc_get_symbol_decl (sym->cp_pointer));
    4796           19 :           tmp = gfc_evaluate_now (tmp, &se->pre);
    4797              :         }
    4798              : 
    4799       131175 :       if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
    4800              :         {
    4801       130547 :           gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
    4802       130547 :           tmp = gfc_build_addr_expr (NULL_TREE, tmp);
    4803              :         }
    4804              :     }
    4805       133981 :   se->expr = tmp;
    4806       133981 : }
    4807              : 
    4808              : 
    4809              : /* Initialize MAPPING.  */
    4810              : 
    4811              : void
    4812       134098 : gfc_init_interface_mapping (gfc_interface_mapping * mapping)
    4813              : {
    4814       134098 :   mapping->syms = NULL;
    4815       134098 :   mapping->charlens = NULL;
    4816       134098 : }
    4817              : 
    4818              : 
    4819              : /* Free all memory held by MAPPING (but not MAPPING itself).  */
    4820              : 
    4821              : void
    4822       134098 : gfc_free_interface_mapping (gfc_interface_mapping * mapping)
    4823              : {
    4824       134098 :   gfc_interface_sym_mapping *sym;
    4825       134098 :   gfc_interface_sym_mapping *nextsym;
    4826       134098 :   gfc_charlen *cl;
    4827       134098 :   gfc_charlen *nextcl;
    4828              : 
    4829       174988 :   for (sym = mapping->syms; sym; sym = nextsym)
    4830              :     {
    4831        40890 :       nextsym = sym->next;
    4832        40890 :       sym->new_sym->n.sym->formal = NULL;
    4833        40890 :       gfc_free_symbol (sym->new_sym->n.sym);
    4834        40890 :       gfc_free_expr (sym->expr);
    4835        40890 :       free (sym->new_sym);
    4836        40890 :       free (sym);
    4837              :     }
    4838       138786 :   for (cl = mapping->charlens; cl; cl = nextcl)
    4839              :     {
    4840         4688 :       nextcl = cl->next;
    4841         4688 :       gfc_free_expr (cl->length);
    4842         4688 :       free (cl);
    4843              :     }
    4844       134098 : }
    4845              : 
    4846              : 
    4847              : /* Return a copy of gfc_charlen CL.  Add the returned structure to
    4848              :    MAPPING so that it will be freed by gfc_free_interface_mapping.  */
    4849              : 
    4850              : static gfc_charlen *
    4851         4688 : gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
    4852              :                                    gfc_charlen * cl)
    4853              : {
    4854         4688 :   gfc_charlen *new_charlen;
    4855              : 
    4856         4688 :   new_charlen = gfc_get_charlen ();
    4857         4688 :   new_charlen->next = mapping->charlens;
    4858         4688 :   new_charlen->length = gfc_copy_expr (cl->length);
    4859              : 
    4860         4688 :   mapping->charlens = new_charlen;
    4861         4688 :   return new_charlen;
    4862              : }
    4863              : 
    4864              : 
    4865              : /* A subroutine of gfc_add_interface_mapping.  Return a descriptorless
    4866              :    array variable that can be used as the actual argument for dummy
    4867              :    argument SYM, except in the case of assumed rank dummies of
    4868              :    non-intrinsic functions where the descriptor must be passed. Add any
    4869              :    initialization code to BLOCK. PACKED is as for gfc_get_nodesc_array_type
    4870              :    and DATA points to the first element in the passed array.  */
    4871              : 
    4872              : static tree
    4873         8482 : gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
    4874              :                                  gfc_packed packed, tree data, tree len,
    4875              :                                  bool assumed_rank_formal)
    4876              : {
    4877         8482 :   tree type;
    4878         8482 :   tree var;
    4879              : 
    4880         8482 :   if (len != NULL_TREE && (TREE_CONSTANT (len) || VAR_P (len)))
    4881           70 :     type = gfc_get_character_type_len (sym->ts.kind, len);
    4882              :   else
    4883         8412 :     type = gfc_typenode_for_spec (&sym->ts);
    4884              : 
    4885         8482 :   if (assumed_rank_formal)
    4886           13 :     type = TREE_TYPE (data);
    4887              :   else
    4888         8469 :     type = gfc_get_nodesc_array_type (type, sym->as, packed,
    4889         8469 :                                     !sym->attr.target && !sym->attr.pointer
    4890         8445 :                                     && !sym->attr.proc_pointer);
    4891              : 
    4892         8482 :   var = gfc_create_var (type, "ifm");
    4893         8482 :   gfc_add_modify (block, var, fold_convert (type, data));
    4894              : 
    4895         8482 :   return var;
    4896              : }
    4897              : 
    4898              : 
    4899              : /* A subroutine of gfc_add_interface_mapping.  Set the stride, upper bounds
    4900              :    and offset of descriptorless array type TYPE given that it has the same
    4901              :    size as DESC.  Add any set-up code to BLOCK.  */
    4902              : 
    4903              : static void
    4904         8152 : gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
    4905              : {
    4906         8152 :   int n;
    4907         8152 :   tree dim;
    4908         8152 :   tree offset;
    4909         8152 :   tree tmp;
    4910              : 
    4911         8152 :   offset = gfc_index_zero_node;
    4912         9266 :   for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
    4913              :     {
    4914         1114 :       dim = gfc_rank_cst[n];
    4915         1114 :       GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
    4916         1114 :       if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
    4917              :         {
    4918            1 :           GFC_TYPE_ARRAY_LBOUND (type, n)
    4919            1 :                 = gfc_conv_descriptor_lbound_get (desc, dim);
    4920            1 :           GFC_TYPE_ARRAY_UBOUND (type, n)
    4921            2 :                 = gfc_conv_descriptor_ubound_get (desc, dim);
    4922              :         }
    4923         1113 :       else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
    4924              :         {
    4925         1087 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    4926              :                                  gfc_array_index_type,
    4927              :                                  gfc_conv_descriptor_ubound_get (desc, dim),
    4928              :                                  gfc_conv_descriptor_lbound_get (desc, dim));
    4929         3261 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    4930              :                                  gfc_array_index_type,
    4931         1087 :                                  GFC_TYPE_ARRAY_LBOUND (type, n), tmp);
    4932         1087 :           tmp = gfc_evaluate_now (tmp, block);
    4933         1087 :           GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
    4934              :         }
    4935         4456 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    4936         1114 :                              GFC_TYPE_ARRAY_LBOUND (type, n),
    4937         1114 :                              GFC_TYPE_ARRAY_STRIDE (type, n));
    4938         1114 :       offset = fold_build2_loc (input_location, MINUS_EXPR,
    4939              :                                 gfc_array_index_type, offset, tmp);
    4940              :     }
    4941         8152 :   offset = gfc_evaluate_now (offset, block);
    4942         8152 :   GFC_TYPE_ARRAY_OFFSET (type) = offset;
    4943         8152 : }
    4944              : 
    4945              : 
    4946              : /* Extend MAPPING so that it maps dummy argument SYM to the value stored
    4947              :    in SE.  The caller may still use se->expr and se->string_length after
    4948              :    calling this function.  */
    4949              : 
    4950              : void
    4951        40890 : gfc_add_interface_mapping (gfc_interface_mapping * mapping,
    4952              :                            gfc_symbol * sym, gfc_se * se,
    4953              :                            gfc_expr *expr)
    4954              : {
    4955        40890 :   gfc_interface_sym_mapping *sm;
    4956        40890 :   tree desc;
    4957        40890 :   tree tmp;
    4958        40890 :   tree value;
    4959        40890 :   gfc_symbol *new_sym;
    4960        40890 :   gfc_symtree *root;
    4961        40890 :   gfc_symtree *new_symtree;
    4962              : 
    4963              :   /* Create a new symbol to represent the actual argument.  */
    4964        40890 :   new_sym = gfc_new_symbol (sym->name, NULL);
    4965        40890 :   new_sym->ts = sym->ts;
    4966        40890 :   new_sym->as = gfc_copy_array_spec (sym->as);
    4967        40890 :   new_sym->attr.referenced = 1;
    4968        40890 :   new_sym->attr.dimension = sym->attr.dimension;
    4969        40890 :   new_sym->attr.contiguous = sym->attr.contiguous;
    4970        40890 :   new_sym->attr.codimension = sym->attr.codimension;
    4971        40890 :   new_sym->attr.pointer = sym->attr.pointer;
    4972        40890 :   new_sym->attr.allocatable = sym->attr.allocatable;
    4973        40890 :   new_sym->attr.flavor = sym->attr.flavor;
    4974        40890 :   new_sym->attr.function = sym->attr.function;
    4975        40890 :   new_sym->attr.dummy = 0;
    4976              : 
    4977              :   /* Ensure that the interface is available and that
    4978              :      descriptors are passed for array actual arguments.  */
    4979        40890 :   if (sym->attr.flavor == FL_PROCEDURE)
    4980              :     {
    4981           36 :       new_sym->formal = expr->symtree->n.sym->formal;
    4982           36 :       new_sym->attr.always_explicit
    4983           36 :             = expr->symtree->n.sym->attr.always_explicit;
    4984              :     }
    4985              : 
    4986              :   /* Create a fake symtree for it.  */
    4987        40890 :   root = NULL;
    4988        40890 :   new_symtree = gfc_new_symtree (&root, sym->name);
    4989        40890 :   new_symtree->n.sym = new_sym;
    4990        40890 :   gcc_assert (new_symtree == root);
    4991              : 
    4992              :   /* Create a dummy->actual mapping.  */
    4993        40890 :   sm = XCNEW (gfc_interface_sym_mapping);
    4994        40890 :   sm->next = mapping->syms;
    4995        40890 :   sm->old = sym;
    4996        40890 :   sm->new_sym = new_symtree;
    4997        40890 :   sm->expr = gfc_copy_expr (expr);
    4998        40890 :   mapping->syms = sm;
    4999              : 
    5000              :   /* Stabilize the argument's value.  */
    5001        40890 :   if (!sym->attr.function && se)
    5002        40792 :     se->expr = gfc_evaluate_now (se->expr, &se->pre);
    5003              : 
    5004        40890 :   if (sym->ts.type == BT_CHARACTER)
    5005              :     {
    5006              :       /* Create a copy of the dummy argument's length.  */
    5007         2886 :       new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
    5008         2886 :       sm->expr->ts.u.cl = new_sym->ts.u.cl;
    5009              : 
    5010              :       /* If the length is specified as "*", record the length that
    5011              :          the caller is passing.  We should use the callee's length
    5012              :          in all other cases.  */
    5013         2886 :       if (!new_sym->ts.u.cl->length && se)
    5014              :         {
    5015         2646 :           se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
    5016         2646 :           new_sym->ts.u.cl->backend_decl = se->string_length;
    5017              :         }
    5018              :     }
    5019              : 
    5020        40876 :   if (!se)
    5021           62 :     return;
    5022              : 
    5023              :   /* Use the passed value as-is if the argument is a function.  */
    5024        40828 :   if (sym->attr.flavor == FL_PROCEDURE)
    5025           36 :     value = se->expr;
    5026              : 
    5027              :   /* If the argument is a pass-by-value scalar, use the value as is.  */
    5028        40792 :   else if (!sym->attr.dimension && sym->attr.value)
    5029           78 :     value = se->expr;
    5030              : 
    5031              :   /* If the argument is either a string or a pointer to a string,
    5032              :      convert it to a boundless character type.  */
    5033        40714 :   else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
    5034              :     {
    5035         1305 :       se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
    5036         1305 :       tmp = gfc_get_character_type_len (sym->ts.kind, se->string_length);
    5037         1305 :       tmp = build_pointer_type (tmp);
    5038         1305 :       if (sym->attr.pointer)
    5039          126 :         value = build_fold_indirect_ref_loc (input_location,
    5040              :                                          se->expr);
    5041              :       else
    5042         1179 :         value = se->expr;
    5043         1305 :       value = fold_convert (tmp, value);
    5044              :     }
    5045              : 
    5046              :   /* If the argument is a scalar, a pointer to an array or an allocatable,
    5047              :      dereference it.  */
    5048        39409 :   else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
    5049        29430 :     value = build_fold_indirect_ref_loc (input_location,
    5050              :                                      se->expr);
    5051              : 
    5052              :   /* For character(*), use the actual argument's descriptor.  */
    5053         9979 :   else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
    5054         1497 :     value = build_fold_indirect_ref_loc (input_location,
    5055              :                                          se->expr);
    5056              : 
    5057              :   /* If the argument is an array descriptor, use it to determine
    5058              :      information about the actual argument's shape.  */
    5059         8482 :   else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
    5060         8482 :            && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
    5061              :     {
    5062         8152 :       bool assumed_rank_formal = false;
    5063              : 
    5064              :       /* Get the actual argument's descriptor.  */
    5065         8152 :       desc = build_fold_indirect_ref_loc (input_location,
    5066              :                                       se->expr);
    5067              : 
    5068              :       /* Create the replacement variable.  */
    5069         8152 :       if (sym->as && sym->as->type == AS_ASSUMED_RANK
    5070         7362 :           && !(sym->ns && sym->ns->proc_name
    5071         7362 :                && sym->ns->proc_name->attr.proc == PROC_INTRINSIC))
    5072              :         {
    5073              :           assumed_rank_formal = true;
    5074              :           tmp = desc;
    5075              :         }
    5076              :       else
    5077         8139 :         tmp = gfc_conv_descriptor_data_get (desc);
    5078              : 
    5079         8152 :       value = gfc_get_interface_mapping_array (&se->pre, sym,
    5080              :                                                PACKED_NO, tmp,
    5081              :                                                se->string_length,
    5082              :                                                assumed_rank_formal);
    5083              : 
    5084              :       /* Use DESC to work out the upper bounds, strides and offset.  */
    5085         8152 :       gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
    5086              :     }
    5087              :   else
    5088              :     /* Otherwise we have a packed array.  */
    5089          330 :     value = gfc_get_interface_mapping_array (&se->pre, sym,
    5090              :                                              PACKED_FULL, se->expr,
    5091              :                                              se->string_length,
    5092              :                                              false);
    5093              : 
    5094        40828 :   new_sym->backend_decl = value;
    5095              : }
    5096              : 
    5097              : 
    5098              : /* Called once all dummy argument mappings have been added to MAPPING,
    5099              :    but before the mapping is used to evaluate expressions.  Pre-evaluate
    5100              :    the length of each argument, adding any initialization code to PRE and
    5101              :    any finalization code to POST.  */
    5102              : 
    5103              : static void
    5104       134061 : gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
    5105              :                               stmtblock_t * pre, stmtblock_t * post)
    5106              : {
    5107       134061 :   gfc_interface_sym_mapping *sym;
    5108       134061 :   gfc_expr *expr;
    5109       134061 :   gfc_se se;
    5110              : 
    5111       174889 :   for (sym = mapping->syms; sym; sym = sym->next)
    5112        40828 :     if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
    5113         2872 :         && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
    5114              :       {
    5115          226 :         expr = sym->new_sym->n.sym->ts.u.cl->length;
    5116          226 :         gfc_apply_interface_mapping_to_expr (mapping, expr);
    5117          226 :         gfc_init_se (&se, NULL);
    5118          226 :         gfc_conv_expr (&se, expr);
    5119          226 :         se.expr = fold_convert (gfc_charlen_type_node, se.expr);
    5120          226 :         se.expr = gfc_evaluate_now (se.expr, &se.pre);
    5121          226 :         gfc_add_block_to_block (pre, &se.pre);
    5122          226 :         gfc_add_block_to_block (post, &se.post);
    5123              : 
    5124          226 :         sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
    5125              :       }
    5126       134061 : }
    5127              : 
    5128              : 
    5129              : /* Like gfc_apply_interface_mapping_to_expr, but applied to
    5130              :    constructor C.  */
    5131              : 
    5132              : static void
    5133           47 : gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
    5134              :                                      gfc_constructor_base base)
    5135              : {
    5136           47 :   gfc_constructor *c;
    5137          428 :   for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
    5138              :     {
    5139          381 :       gfc_apply_interface_mapping_to_expr (mapping, c->expr);
    5140          381 :       if (c->iterator)
    5141              :         {
    5142            6 :           gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
    5143            6 :           gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
    5144            6 :           gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
    5145              :         }
    5146              :     }
    5147           47 : }
    5148              : 
    5149              : 
    5150              : /* Like gfc_apply_interface_mapping_to_expr, but applied to
    5151              :    reference REF.  */
    5152              : 
    5153              : static void
    5154        12783 : gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
    5155              :                                     gfc_ref * ref)
    5156              : {
    5157        12783 :   int n;
    5158              : 
    5159        14268 :   for (; ref; ref = ref->next)
    5160         1485 :     switch (ref->type)
    5161              :       {
    5162              :       case REF_ARRAY:
    5163         2915 :         for (n = 0; n < ref->u.ar.dimen; n++)
    5164              :           {
    5165         1650 :             gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
    5166         1650 :             gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
    5167         1650 :             gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
    5168              :           }
    5169              :         break;
    5170              : 
    5171              :       case REF_COMPONENT:
    5172              :       case REF_INQUIRY:
    5173              :         break;
    5174              : 
    5175           43 :       case REF_SUBSTRING:
    5176           43 :         gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
    5177           43 :         gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
    5178           43 :         break;
    5179              :       }
    5180        12783 : }
    5181              : 
    5182              : 
    5183              : /* Convert intrinsic function calls into result expressions.  */
    5184              : 
    5185              : static bool
    5186         2232 : gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
    5187              : {
    5188         2232 :   gfc_symbol *sym;
    5189         2232 :   gfc_expr *new_expr;
    5190         2232 :   gfc_expr *arg1;
    5191         2232 :   gfc_expr *arg2;
    5192         2232 :   int d, dup;
    5193              : 
    5194         2232 :   arg1 = expr->value.function.actual->expr;
    5195         2232 :   if (expr->value.function.actual->next)
    5196         2111 :     arg2 = expr->value.function.actual->next->expr;
    5197              :   else
    5198              :     arg2 = NULL;
    5199              : 
    5200         2232 :   sym = arg1->symtree->n.sym;
    5201              : 
    5202         2232 :   if (sym->attr.dummy)
    5203              :     return false;
    5204              : 
    5205         2208 :   new_expr = NULL;
    5206              : 
    5207         2208 :   switch (expr->value.function.isym->id)
    5208              :     {
    5209          947 :     case GFC_ISYM_LEN:
    5210              :       /* TODO figure out why this condition is necessary.  */
    5211          947 :       if (sym->attr.function
    5212           43 :           && (arg1->ts.u.cl->length == NULL
    5213           42 :               || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
    5214           42 :                   && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
    5215              :         return false;
    5216              : 
    5217          904 :       new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
    5218          904 :       break;
    5219              : 
    5220          228 :     case GFC_ISYM_LEN_TRIM:
    5221          228 :       new_expr = gfc_copy_expr (arg1);
    5222          228 :       gfc_apply_interface_mapping_to_expr (mapping, new_expr);
    5223              : 
    5224          228 :       if (!new_expr)
    5225              :         return false;
    5226              : 
    5227          228 :       gfc_replace_expr (arg1, new_expr);
    5228          228 :       return true;
    5229              : 
    5230          606 :     case GFC_ISYM_SIZE:
    5231          606 :       if (!sym->as || sym->as->rank == 0)
    5232              :         return false;
    5233              : 
    5234          530 :       if (arg2 && arg2->expr_type == EXPR_CONSTANT)
    5235              :         {
    5236          360 :           dup = mpz_get_si (arg2->value.integer);
    5237          360 :           d = dup - 1;
    5238              :         }
    5239              :       else
    5240              :         {
    5241          530 :           dup = sym->as->rank;
    5242          530 :           d = 0;
    5243              :         }
    5244              : 
    5245          542 :       for (; d < dup; d++)
    5246              :         {
    5247          530 :           gfc_expr *tmp;
    5248              : 
    5249          530 :           if (!sym->as->upper[d] || !sym->as->lower[d])
    5250              :             {
    5251          518 :               gfc_free_expr (new_expr);
    5252          518 :               return false;
    5253              :             }
    5254              : 
    5255           12 :           tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
    5256              :                                         gfc_get_int_expr (gfc_default_integer_kind,
    5257              :                                                           NULL, 1));
    5258           12 :           tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
    5259           12 :           if (new_expr)
    5260            0 :             new_expr = gfc_multiply (new_expr, tmp);
    5261              :           else
    5262              :             new_expr = tmp;
    5263              :         }
    5264              :       break;
    5265              : 
    5266           44 :     case GFC_ISYM_LBOUND:
    5267           44 :     case GFC_ISYM_UBOUND:
    5268              :         /* TODO These implementations of lbound and ubound do not limit if
    5269              :            the size < 0, according to F95's 13.14.53 and 13.14.113.  */
    5270              : 
    5271           44 :       if (!sym->as || sym->as->rank == 0)
    5272              :         return false;
    5273              : 
    5274           44 :       if (arg2 && arg2->expr_type == EXPR_CONSTANT)
    5275           38 :         d = mpz_get_si (arg2->value.integer) - 1;
    5276              :       else
    5277              :         return false;
    5278              : 
    5279           38 :       if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
    5280              :         {
    5281           23 :           if (sym->as->lower[d])
    5282           23 :             new_expr = gfc_copy_expr (sym->as->lower[d]);
    5283              :         }
    5284              :       else
    5285              :         {
    5286           15 :           if (sym->as->upper[d])
    5287            9 :             new_expr = gfc_copy_expr (sym->as->upper[d]);
    5288              :         }
    5289              :       break;
    5290              : 
    5291              :     default:
    5292              :       break;
    5293              :     }
    5294              : 
    5295         1337 :   gfc_apply_interface_mapping_to_expr (mapping, new_expr);
    5296         1337 :   if (!new_expr)
    5297              :     return false;
    5298              : 
    5299          113 :   gfc_replace_expr (expr, new_expr);
    5300          113 :   return true;
    5301              : }
    5302              : 
    5303              : 
    5304              : static void
    5305           24 : gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
    5306              :                               gfc_interface_mapping * mapping)
    5307              : {
    5308           24 :   gfc_formal_arglist *f;
    5309           24 :   gfc_actual_arglist *actual;
    5310              : 
    5311           24 :   actual = expr->value.function.actual;
    5312           24 :   f = gfc_sym_get_dummy_args (map_expr->symtree->n.sym);
    5313              : 
    5314           72 :   for (; f && actual; f = f->next, actual = actual->next)
    5315              :     {
    5316           24 :       if (!actual->expr)
    5317            0 :         continue;
    5318              : 
    5319           24 :       gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
    5320              :     }
    5321              : 
    5322           24 :   if (map_expr->symtree->n.sym->attr.dimension)
    5323              :     {
    5324            6 :       int d;
    5325            6 :       gfc_array_spec *as;
    5326              : 
    5327            6 :       as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
    5328              : 
    5329           18 :       for (d = 0; d < as->rank; d++)
    5330              :         {
    5331            6 :           gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
    5332            6 :           gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
    5333              :         }
    5334              : 
    5335            6 :       expr->value.function.esym->as = as;
    5336              :     }
    5337              : 
    5338           24 :   if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
    5339              :     {
    5340            0 :       expr->value.function.esym->ts.u.cl->length
    5341            0 :         = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
    5342              : 
    5343            0 :       gfc_apply_interface_mapping_to_expr (mapping,
    5344            0 :                         expr->value.function.esym->ts.u.cl->length);
    5345              :     }
    5346           24 : }
    5347              : 
    5348              : 
    5349              : /* EXPR is a copy of an expression that appeared in the interface
    5350              :    associated with MAPPING.  Walk it recursively looking for references to
    5351              :    dummy arguments that MAPPING maps to actual arguments.  Replace each such
    5352              :    reference with a reference to the associated actual argument.  */
    5353              : 
    5354              : static void
    5355        21367 : gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
    5356              :                                      gfc_expr * expr)
    5357              : {
    5358        22934 :   gfc_interface_sym_mapping *sym;
    5359        22934 :   gfc_actual_arglist *actual;
    5360              : 
    5361        22934 :   if (!expr)
    5362              :     return;
    5363              : 
    5364              :   /* Copying an expression does not copy its length, so do that here.  */
    5365        12783 :   if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
    5366              :     {
    5367         1802 :       expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
    5368         1802 :       gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
    5369              :     }
    5370              : 
    5371              :   /* Apply the mapping to any references.  */
    5372        12783 :   gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
    5373              : 
    5374              :   /* ...and to the expression's symbol, if it has one.  */
    5375              :   /* TODO Find out why the condition on expr->symtree had to be moved into
    5376              :      the loop rather than being outside it, as originally.  */
    5377        30236 :   for (sym = mapping->syms; sym; sym = sym->next)
    5378        17453 :     if (expr->symtree && !strcmp (sym->old->name, expr->symtree->n.sym->name))
    5379              :       {
    5380         3412 :         if (sym->new_sym->n.sym->backend_decl)
    5381         3368 :           expr->symtree = sym->new_sym;
    5382           44 :         else if (sym->expr)
    5383           44 :           gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
    5384              :       }
    5385              : 
    5386              :       /* ...and to subexpressions in expr->value.  */
    5387        12783 :   switch (expr->expr_type)
    5388              :     {
    5389              :     case EXPR_VARIABLE:
    5390              :     case EXPR_CONSTANT:
    5391              :     case EXPR_NULL:
    5392              :     case EXPR_SUBSTRING:
    5393              :       break;
    5394              : 
    5395         1567 :     case EXPR_OP:
    5396         1567 :       gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
    5397         1567 :       gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
    5398         1567 :       break;
    5399              : 
    5400            0 :     case EXPR_CONDITIONAL:
    5401            0 :       gfc_apply_interface_mapping_to_expr (mapping,
    5402            0 :                                            expr->value.conditional.true_expr);
    5403            0 :       gfc_apply_interface_mapping_to_expr (mapping,
    5404            0 :                                            expr->value.conditional.false_expr);
    5405            0 :       break;
    5406              : 
    5407         2976 :     case EXPR_FUNCTION:
    5408         9558 :       for (actual = expr->value.function.actual; actual; actual = actual->next)
    5409         6582 :         gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
    5410              : 
    5411         2976 :       if (expr->value.function.esym == NULL
    5412         2663 :             && expr->value.function.isym != NULL
    5413         2651 :             && expr->value.function.actual
    5414         2650 :             && expr->value.function.actual->expr
    5415         2650 :             && expr->value.function.actual->expr->symtree
    5416         5208 :             && gfc_map_intrinsic_function (expr, mapping))
    5417              :         break;
    5418              : 
    5419         6192 :       for (sym = mapping->syms; sym; sym = sym->next)
    5420         3557 :         if (sym->old == expr->value.function.esym)
    5421              :           {
    5422           24 :             expr->value.function.esym = sym->new_sym->n.sym;
    5423           24 :             gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
    5424           24 :             expr->value.function.esym->result = sym->new_sym->n.sym;
    5425              :           }
    5426              :       break;
    5427              : 
    5428           47 :     case EXPR_ARRAY:
    5429           47 :     case EXPR_STRUCTURE:
    5430           47 :       gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
    5431           47 :       break;
    5432              : 
    5433            0 :     case EXPR_COMPCALL:
    5434            0 :     case EXPR_PPC:
    5435            0 :     case EXPR_UNKNOWN:
    5436            0 :       gcc_unreachable ();
    5437              :       break;
    5438              :     }
    5439              : 
    5440              :   return;
    5441              : }
    5442              : 
    5443              : 
    5444              : /* Evaluate interface expression EXPR using MAPPING.  Store the result
    5445              :    in SE.  */
    5446              : 
    5447              : void
    5448         4178 : gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
    5449              :                              gfc_se * se, gfc_expr * expr)
    5450              : {
    5451         4178 :   expr = gfc_copy_expr (expr);
    5452         4178 :   gfc_apply_interface_mapping_to_expr (mapping, expr);
    5453         4178 :   gfc_conv_expr (se, expr);
    5454         4178 :   se->expr = gfc_evaluate_now (se->expr, &se->pre);
    5455         4178 :   gfc_free_expr (expr);
    5456         4178 : }
    5457              : 
    5458              : 
    5459              : /* Returns a reference to a temporary array into which a component of
    5460              :    an actual argument derived type array is copied and then returned
    5461              :    after the function call.  */
    5462              : void
    5463         2990 : gfc_conv_subref_array_arg (gfc_se *se, gfc_expr * expr, int g77,
    5464              :                            sym_intent intent, bool formal_ptr,
    5465              :                            const gfc_symbol *fsym, const char *proc_name,
    5466              :                            gfc_symbol *sym, bool check_contiguous,
    5467              :                            bool deep_copy, bool span_only)
    5468              : {
    5469         2990 :   gfc_se lse;
    5470         2990 :   gfc_se rse;
    5471         2990 :   gfc_ss *lss;
    5472         2990 :   gfc_ss *rss;
    5473         2990 :   gfc_loopinfo loop;
    5474         2990 :   gfc_loopinfo loop2;
    5475         2990 :   gfc_array_info *info;
    5476         2990 :   tree offset;
    5477         2990 :   tree tmp_index;
    5478         2990 :   tree tmp;
    5479         2990 :   tree base_type;
    5480         2990 :   tree size;
    5481         2990 :   stmtblock_t body;
    5482         2990 :   int n;
    5483         2990 :   int dimen;
    5484         2990 :   gfc_se work_se;
    5485         2990 :   gfc_se *parmse;
    5486         2990 :   bool pass_optional;
    5487         2990 :   bool readonly;
    5488              : 
    5489         2990 :   pass_optional = fsym && fsym->attr.optional && sym && sym->attr.optional;
    5490              : 
    5491         2949 :   if (pass_optional || check_contiguous)
    5492              :     {
    5493         1543 :       gfc_init_se (&work_se, NULL);
    5494         1543 :       parmse = &work_se;
    5495              :     }
    5496              :   else
    5497              :     parmse = se;
    5498              : 
    5499         2990 :   if (gfc_option.rtcheck & GFC_RTCHECK_ARRAY_TEMPS)
    5500              :     {
    5501              :       /* We will create a temporary array, so let us warn.  */
    5502          868 :       char * msg;
    5503              : 
    5504          868 :       if (fsym && proc_name)
    5505          868 :         msg = xasprintf ("An array temporary was created for argument "
    5506          868 :                          "'%s' of procedure '%s'", fsym->name, proc_name);
    5507              :       else
    5508            0 :         msg = xasprintf ("An array temporary was created");
    5509              : 
    5510          868 :       tmp = build_int_cst (logical_type_node, 1);
    5511          868 :       gfc_trans_runtime_check (false, true, tmp, &parmse->pre,
    5512              :                                &expr->where, msg);
    5513          868 :       free (msg);
    5514              :     }
    5515              : 
    5516         2990 :   gfc_init_se (&lse, NULL);
    5517         2990 :   gfc_init_se (&rse, NULL);
    5518              : 
    5519              :   /* Walk the argument expression.  */
    5520         2990 :   rss = gfc_walk_expr (expr);
    5521              : 
    5522         2990 :   gcc_assert (rss != gfc_ss_terminator);
    5523              : 
    5524              :   /* Initialize the scalarizer.  */
    5525         2990 :   gfc_init_loopinfo (&loop);
    5526         2990 :   gfc_add_ss_to_loop (&loop, rss);
    5527              : 
    5528              :   /* Calculate the bounds of the scalarization.  */
    5529         2990 :   gfc_conv_ss_startstride (&loop);
    5530              : 
    5531              :   /* Build an ss for the temporary.  */
    5532         2990 :   if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
    5533          136 :     gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
    5534              : 
    5535         2990 :   base_type = gfc_typenode_for_spec (&expr->ts);
    5536         2990 :   if (GFC_ARRAY_TYPE_P (base_type)
    5537         2990 :                 || GFC_DESCRIPTOR_TYPE_P (base_type))
    5538            0 :     base_type = gfc_get_element_type (base_type);
    5539              : 
    5540         2990 :   if (expr->ts.type == BT_CLASS)
    5541          129 :     base_type = gfc_typenode_for_spec (&CLASS_DATA (expr)->ts);
    5542              : 
    5543         4214 :   loop.temp_ss = gfc_get_temp_ss (base_type, ((expr->ts.type == BT_CHARACTER)
    5544         1224 :                                               ? expr->ts.u.cl->backend_decl
    5545              :                                               : NULL),
    5546              :                                   loop.dimen);
    5547              : 
    5548         2990 :   parmse->string_length = loop.temp_ss->info->string_length;
    5549              : 
    5550              :   /* Associate the SS with the loop.  */
    5551         2990 :   gfc_add_ss_to_loop (&loop, loop.temp_ss);
    5552              : 
    5553              :   /* Setup the scalarizing loops.  */
    5554         2990 :   gfc_conv_loop_setup (&loop, &expr->where);
    5555              : 
    5556              :   /* Pass the temporary descriptor back to the caller.  */
    5557         2990 :   info = &loop.temp_ss->info->data.array;
    5558         2990 :   parmse->expr = info->descriptor;
    5559              : 
    5560              :   /* Setup the gfc_se structures.  */
    5561         2990 :   gfc_copy_loopinfo_to_se (&lse, &loop);
    5562         2990 :   gfc_copy_loopinfo_to_se (&rse, &loop);
    5563              : 
    5564         2990 :   rse.ss = rss;
    5565         2990 :   lse.ss = loop.temp_ss;
    5566         2990 :   gfc_mark_ss_chain_used (rss, 1);
    5567         2990 :   gfc_mark_ss_chain_used (loop.temp_ss, 1);
    5568              : 
    5569              :   /* Start the scalarized loop body.  */
    5570         2990 :   gfc_start_scalarized_body (&loop, &body);
    5571              : 
    5572              :   /* Translate the expression.  */
    5573         2990 :   gfc_conv_expr (&rse, expr);
    5574              : 
    5575         2990 :   gfc_conv_tmp_array_ref (&lse);
    5576              : 
    5577         2990 :   if (intent != INTENT_OUT)
    5578              :     {
    5579         2946 :       tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, deep_copy, false);
    5580         2946 :       gfc_add_expr_to_block (&body, tmp);
    5581         2946 :       gcc_assert (rse.ss == gfc_ss_terminator);
    5582         2946 :       gfc_trans_scalarizing_loops (&loop, &body);
    5583              :     }
    5584              :   else
    5585              :     {
    5586              :       /* Make sure that the temporary declaration survives by merging
    5587              :        all the loop declarations into the current context.  */
    5588           97 :       for (n = 0; n < loop.dimen; n++)
    5589              :         {
    5590           53 :           gfc_merge_block_scope (&body);
    5591           53 :           body = loop.code[loop.order[n]];
    5592              :         }
    5593           44 :       gfc_merge_block_scope (&body);
    5594              :     }
    5595              : 
    5596              :   /* Add the post block after the second loop, so that any
    5597              :      freeing of allocated memory is done at the right time.  */
    5598         2990 :   gfc_add_block_to_block (&parmse->pre, &loop.pre);
    5599              : 
    5600              :   /**********Copy the temporary back again.*********/
    5601              : 
    5602         2990 :   gfc_init_se (&lse, NULL);
    5603         2990 :   gfc_init_se (&rse, NULL);
    5604              : 
    5605              :   /* Walk the argument expression.  */
    5606         2990 :   lss = gfc_walk_expr (expr);
    5607         2990 :   rse.ss = loop.temp_ss;
    5608         2990 :   lse.ss = lss;
    5609              : 
    5610              :   /* Initialize the scalarizer.  */
    5611         2990 :   gfc_init_loopinfo (&loop2);
    5612         2990 :   gfc_add_ss_to_loop (&loop2, lss);
    5613              : 
    5614         2990 :   dimen = rse.ss->dimen;
    5615              : 
    5616              :   /* Skip the write-out loop for this case.  */
    5617         2990 :   if (gfc_is_class_array_function (expr))
    5618           13 :     goto class_array_fcn;
    5619              : 
    5620              :   /* Calculate the bounds of the scalarization.  */
    5621         2977 :   gfc_conv_ss_startstride (&loop2);
    5622              : 
    5623              :   /* Setup the scalarizing loops.  */
    5624         2977 :   gfc_conv_loop_setup (&loop2, &expr->where);
    5625              : 
    5626         2977 :   gfc_copy_loopinfo_to_se (&lse, &loop2);
    5627         2977 :   gfc_copy_loopinfo_to_se (&rse, &loop2);
    5628              : 
    5629         2977 :   gfc_mark_ss_chain_used (lss, 1);
    5630         2977 :   gfc_mark_ss_chain_used (loop.temp_ss, 1);
    5631              : 
    5632              :   /* Declare the variable to hold the temporary offset and start the
    5633              :      scalarized loop body.  */
    5634         2977 :   offset = gfc_create_var (gfc_array_index_type, NULL);
    5635         2977 :   gfc_start_scalarized_body (&loop2, &body);
    5636              : 
    5637              :   /* Build the offsets for the temporary from the loop variables.  The
    5638              :      temporary array has lbounds of zero and strides of one in all
    5639              :      dimensions, so this is very simple.  The offset is only computed
    5640              :      outside the innermost loop, so the overall transfer could be
    5641              :      optimized further.  */
    5642         2977 :   info = &rse.ss->info->data.array;
    5643              : 
    5644         2977 :   tmp_index = gfc_index_zero_node;
    5645         4378 :   for (n = dimen - 1; n > 0; n--)
    5646              :     {
    5647         1401 :       tree tmp_str;
    5648         1401 :       tmp = rse.loop->loopvar[n];
    5649         1401 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    5650              :                              tmp, rse.loop->from[n]);
    5651         1401 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    5652              :                              tmp, tmp_index);
    5653              : 
    5654         2802 :       tmp_str = fold_build2_loc (input_location, MINUS_EXPR,
    5655              :                                  gfc_array_index_type,
    5656         1401 :                                  rse.loop->to[n-1], rse.loop->from[n-1]);
    5657         1401 :       tmp_str = fold_build2_loc (input_location, PLUS_EXPR,
    5658              :                                  gfc_array_index_type,
    5659              :                                  tmp_str, gfc_index_one_node);
    5660              : 
    5661         1401 :       tmp_index = fold_build2_loc (input_location, MULT_EXPR,
    5662              :                                    gfc_array_index_type, tmp, tmp_str);
    5663              :     }
    5664              : 
    5665         5954 :   tmp_index = fold_build2_loc (input_location, MINUS_EXPR,
    5666              :                                gfc_array_index_type,
    5667         2977 :                                tmp_index, rse.loop->from[0]);
    5668         2977 :   gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
    5669              : 
    5670         5954 :   tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
    5671              :                                gfc_array_index_type,
    5672         2977 :                                rse.loop->loopvar[0], offset);
    5673              : 
    5674              :   /* Now use the offset for the reference.  */
    5675         2977 :   tmp = build_fold_indirect_ref_loc (input_location,
    5676              :                                  info->data);
    5677         2977 :   rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
    5678              : 
    5679         2977 :   if (expr->ts.type == BT_CHARACTER)
    5680         1224 :     rse.string_length = expr->ts.u.cl->backend_decl;
    5681              : 
    5682         2977 :   gfc_conv_expr (&lse, expr);
    5683              : 
    5684         2977 :   gcc_assert (lse.ss == gfc_ss_terminator);
    5685              : 
    5686              :   /* Do not do deallocations when we are looking at a g77-style argument.  */
    5687              : 
    5688         2977 :   tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, !g77);
    5689         2977 :   gfc_add_expr_to_block (&body, tmp);
    5690              : 
    5691              :   /* Generate the copying loops.  */
    5692         2977 :   gfc_trans_scalarizing_loops (&loop2, &body);
    5693              : 
    5694              :   /* Wrap the whole thing up by adding the second loop to the post-block
    5695              :      and following it by the post-block of the first loop.  In this way,
    5696              :      if the temporary needs freeing, it is done after use!
    5697              :      If input expr is read-only, e.g. a PARAMETER array, copying back
    5698              :      modified values is undefined behavior.  */
    5699         5954 :   readonly = (expr->expr_type == EXPR_VARIABLE
    5700         2923 :               && expr->symtree
    5701         5900 :               && expr->symtree->n.sym->attr.flavor == FL_PARAMETER);
    5702              : 
    5703         2977 :   if ((intent != INTENT_IN) && !readonly)
    5704              :     {
    5705         1232 :       gfc_add_block_to_block (&parmse->post, &loop2.pre);
    5706         1232 :       gfc_add_block_to_block (&parmse->post, &loop2.post);
    5707              :     }
    5708              : 
    5709         1745 : class_array_fcn:
    5710              : 
    5711              :   /* A deep copy allocated fresh components for the temporary; free them
    5712              :      again once the call has returned, before the temporary itself goes.
    5713              :      Only INTENT_IN is supported, as writing the temporary back would leave
    5714              :      the actual argument holding the freed component pointers.  */
    5715         2990 :   gcc_assert (!deep_copy || intent == INTENT_IN);
    5716         2990 :   if (deep_copy && expr->ts.type == BT_DERIVED
    5717           24 :       && expr->ts.u.derived->attr.alloc_comp)
    5718              :     {
    5719           24 :       tmp = gfc_deallocate_alloc_comp (expr->ts.u.derived, parmse->expr,
    5720              :                                        dimen);
    5721           24 :       gfc_add_expr_to_block (&parmse->post, tmp);
    5722              :     }
    5723              : 
    5724         2990 :   gfc_add_block_to_block (&parmse->post, &loop.post);
    5725              : 
    5726         2990 :   gfc_cleanup_loop (&loop);
    5727         2990 :   gfc_cleanup_loop (&loop2);
    5728              : 
    5729              :   /* Pass the string length to the argument expression.  */
    5730         2990 :   if (expr->ts.type == BT_CHARACTER)
    5731         1224 :     parmse->string_length = expr->ts.u.cl->backend_decl;
    5732              : 
    5733              :   /* Determine the offset for pointer formal arguments and set the
    5734              :      lbounds to one.  */
    5735         2990 :   if (formal_ptr)
    5736              :     {
    5737           18 :       size = gfc_index_one_node;
    5738           18 :       offset = gfc_index_zero_node;
    5739           36 :       for (n = 0; n < dimen; n++)
    5740              :         {
    5741           18 :           tmp = gfc_conv_descriptor_ubound_get (parmse->expr,
    5742              :                                                 gfc_rank_cst[n]);
    5743           18 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    5744              :                                  gfc_array_index_type, tmp,
    5745              :                                  gfc_index_one_node);
    5746           18 :           gfc_conv_descriptor_ubound_set (&parmse->pre,
    5747              :                                           parmse->expr,
    5748              :                                           gfc_rank_cst[n],
    5749              :                                           tmp);
    5750           18 :           gfc_conv_descriptor_lbound_set (&parmse->pre,
    5751              :                                           parmse->expr,
    5752              :                                           gfc_rank_cst[n],
    5753              :                                           gfc_index_one_node);
    5754           18 :           size = gfc_evaluate_now (size, &parmse->pre);
    5755           18 :           offset = fold_build2_loc (input_location, MINUS_EXPR,
    5756              :                                     gfc_array_index_type,
    5757              :                                     offset, size);
    5758           18 :           offset = gfc_evaluate_now (offset, &parmse->pre);
    5759           36 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    5760              :                                  gfc_array_index_type,
    5761           18 :                                  rse.loop->to[n], rse.loop->from[n]);
    5762           18 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    5763              :                                  gfc_array_index_type,
    5764              :                                  tmp, gfc_index_one_node);
    5765           18 :           size = fold_build2_loc (input_location, MULT_EXPR,
    5766              :                                   gfc_array_index_type, size, tmp);
    5767              :         }
    5768              : 
    5769           18 :       gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
    5770              :                                       offset);
    5771              :     }
    5772              : 
    5773              :   /* We want either the address for the data or the address of the descriptor,
    5774              :      depending on the mode of passing array arguments.  */
    5775         2990 :   if (g77)
    5776          491 :     parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
    5777              :   else
    5778         2499 :     parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
    5779              : 
    5780              :   /* Basically make this into
    5781              : 
    5782              :      if (present)
    5783              :        {
    5784              :          if (contiguous)
    5785              :            {
    5786              :              pointer = a;
    5787              :            }
    5788              :          else
    5789              :            {
    5790              :              parmse->pre();
    5791              :              pointer = parmse->expr;
    5792              :            }
    5793              :        }
    5794              :      else
    5795              :        pointer = NULL;
    5796              : 
    5797              :      foo (pointer);
    5798              :      if (present && !contiguous)
    5799              :            se->post();
    5800              : 
    5801              :      */
    5802              : 
    5803         2990 :   if (pass_optional || check_contiguous)
    5804              :     {
    5805         1543 :       tree type;
    5806         1543 :       stmtblock_t else_block;
    5807         1543 :       tree pre_stmts, post_stmts;
    5808         1543 :       tree pointer;
    5809         1543 :       tree else_stmt;
    5810         1543 :       tree present_var = NULL_TREE;
    5811         1543 :       tree cont_var = NULL_TREE;
    5812         1543 :       tree post_cond;
    5813              : 
    5814         1543 :       type = TREE_TYPE (parmse->expr);
    5815         1543 :       if (POINTER_TYPE_P (type) && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
    5816         1177 :         type = TREE_TYPE (type);
    5817         1543 :       pointer = gfc_create_var (type, "arg_ptr");
    5818              : 
    5819         1543 :       if (check_contiguous)
    5820              :         {
    5821         1513 :           gfc_se cont_se, array_se;
    5822         1513 :           stmtblock_t if_block, else_block;
    5823         1513 :           tree if_stmt, else_stmt;
    5824         1513 :           mpz_t size;
    5825         1513 :           bool size_set;
    5826              : 
    5827         1513 :           cont_var = gfc_create_var (boolean_type_node, "contiguous");
    5828              : 
    5829              :           /* If the size is known to be one at compile-time, set
    5830              :              cont_var to true unconditionally.  This may look
    5831              :              inelegant, but we're only doing this during
    5832              :              optimization, so the statements will be optimized away,
    5833              :              and this saves complexity here.  */
    5834              : 
    5835         1513 :           size_set = gfc_array_size (expr, &size);
    5836         1513 :           if (size_set && mpz_cmp_ui (size, 1) == 0)
    5837              :             {
    5838            6 :               gfc_add_modify (&se->pre, cont_var,
    5839              :                               build_one_cst (boolean_type_node));
    5840              :             }
    5841              :           else
    5842              :             {
    5843              :               /* cont_var = is_contiguous (expr), or just that the span is the
    5844              :                  element length for a dummy that takes any stride.  */
    5845         1507 :               gfc_init_se (&cont_se, parmse);
    5846         1507 :               if (span_only)
    5847           90 :                 gfc_conv_span_is_elem_len (&cont_se, expr);
    5848              :               else
    5849         1417 :                 gfc_conv_is_contiguous_expr (&cont_se, expr);
    5850         1507 :               gfc_add_block_to_block (&se->pre, &(&cont_se)->pre);
    5851         1507 :               gfc_add_modify (&se->pre, cont_var, cont_se.expr);
    5852         1507 :               gfc_add_block_to_block (&se->pre, &(&cont_se)->post);
    5853              :             }
    5854              : 
    5855         1513 :           if (size_set)
    5856         1155 :             mpz_clear (size);
    5857              : 
    5858              :           /* arrayse->expr = descriptor of a.  */
    5859         1513 :           gfc_init_se (&array_se, se);
    5860         1513 :           gfc_conv_expr_descriptor (&array_se, expr);
    5861         1513 :           gfc_add_block_to_block (&se->pre, &(&array_se)->pre);
    5862         1513 :           gfc_add_block_to_block (&se->pre, &(&array_se)->post);
    5863              : 
    5864              :           /* if_stmt = { descriptor ? pointer = a : pointer = &a[0]; } .  */
    5865         1513 :           gfc_init_block (&if_block);
    5866         1513 :           if (GFC_DESCRIPTOR_TYPE_P (type))
    5867         1153 :             gfc_add_modify (&if_block, pointer, array_se.expr);
    5868              :           else
    5869              :             {
    5870          360 :               tmp = gfc_conv_array_data (array_se.expr);
    5871          360 :               tmp = fold_convert (type, tmp);
    5872          360 :               gfc_add_modify (&if_block, pointer, tmp);
    5873              :             }
    5874         1513 :           if_stmt = gfc_finish_block (&if_block);
    5875              : 
    5876              :           /* else_stmt = { parmse->pre(); pointer = parmse->expr; } .  */
    5877         1513 :           gfc_init_block (&else_block);
    5878         1513 :           gfc_add_block_to_block (&else_block, &parmse->pre);
    5879         1873 :           tmp = (GFC_DESCRIPTOR_TYPE_P (type)
    5880         1513 :                  ? build_fold_indirect_ref_loc (input_location, parmse->expr)
    5881              :                  : parmse->expr);
    5882         1513 :           gfc_add_modify (&else_block, pointer, tmp);
    5883         1513 :           else_stmt = gfc_finish_block (&else_block);
    5884              : 
    5885              :           /* And put the above into an if statement.  */
    5886         1513 :           pre_stmts = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    5887              :                                        gfc_likely (cont_var,
    5888              :                                                    PRED_FORTRAN_CONTIGUOUS),
    5889              :                                        if_stmt, else_stmt);
    5890              :         }
    5891              :       else
    5892              :         {
    5893              :           /* pointer = parmse->expr;  .  */
    5894           36 :           tmp = (GFC_DESCRIPTOR_TYPE_P (type)
    5895           30 :                  ? build_fold_indirect_ref_loc (input_location, parmse->expr)
    5896              :                  : parmse->expr);
    5897           30 :           gfc_add_modify (&parmse->pre, pointer, tmp);
    5898           30 :           pre_stmts = gfc_finish_block (&parmse->pre);
    5899              :         }
    5900              : 
    5901         1543 :       if (pass_optional)
    5902              :         {
    5903           41 :           present_var = gfc_create_var (boolean_type_node, "present");
    5904              : 
    5905              :           /* present_var = present(sym); .  */
    5906           41 :           tmp = gfc_conv_expr_present (sym);
    5907           41 :           tmp = fold_convert (boolean_type_node, tmp);
    5908           41 :           gfc_add_modify (&se->pre, present_var, tmp);
    5909              : 
    5910              :           /* else_stmt = { pointer = NULL; } .  */
    5911           41 :           gfc_init_block (&else_block);
    5912           41 :           if (GFC_DESCRIPTOR_TYPE_P (type))
    5913           24 :             gfc_conv_descriptor_data_set (&else_block, pointer,
    5914              :                                           null_pointer_node);
    5915              :           else
    5916           17 :             gfc_add_modify (&else_block, pointer, build_int_cst (type, 0));
    5917           41 :           else_stmt = gfc_finish_block (&else_block);
    5918              : 
    5919           41 :           tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    5920              :                                  gfc_likely (present_var,
    5921              :                                              PRED_FORTRAN_ABSENT_DUMMY),
    5922              :                                  pre_stmts, else_stmt);
    5923           41 :           gfc_add_expr_to_block (&se->pre, tmp);
    5924              :         }
    5925              :       else
    5926         1502 :         gfc_add_expr_to_block (&se->pre, pre_stmts);
    5927              : 
    5928         1543 :       post_stmts = gfc_finish_block (&parmse->post);
    5929              : 
    5930              :       /* Put together the post stuff, plus the optional
    5931              :          deallocation.  */
    5932         1543 :       if (check_contiguous)
    5933              :         {
    5934              :           /* !cont_var.  */
    5935         1513 :           tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    5936              :                                  cont_var,
    5937              :                                  build_zero_cst (boolean_type_node));
    5938         1513 :           tmp = gfc_unlikely (tmp, PRED_FORTRAN_CONTIGUOUS);
    5939              : 
    5940         1513 :           if (pass_optional)
    5941              :             {
    5942           11 :               tree present_likely = gfc_likely (present_var,
    5943              :                                                 PRED_FORTRAN_ABSENT_DUMMY);
    5944           11 :               post_cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    5945              :                                            boolean_type_node, present_likely,
    5946              :                                            tmp);
    5947              :             }
    5948              :           else
    5949              :             post_cond = tmp;
    5950              :         }
    5951              :       else
    5952              :         {
    5953           30 :           gcc_assert (pass_optional);
    5954              :           post_cond = present_var;
    5955              :         }
    5956              : 
    5957         1543 :       tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, post_cond,
    5958              :                              post_stmts, build_empty_stmt (input_location));
    5959         1543 :       gfc_add_expr_to_block (&se->post, tmp);
    5960         1543 :       if (GFC_DESCRIPTOR_TYPE_P (type))
    5961              :         {
    5962         1177 :           type = TREE_TYPE (parmse->expr);
    5963         1177 :           if (POINTER_TYPE_P (type))
    5964              :             {
    5965         1177 :               pointer = gfc_build_addr_expr (type, pointer);
    5966         1177 :               if (pass_optional)
    5967              :                 {
    5968           24 :                   tmp = gfc_likely (present_var, PRED_FORTRAN_ABSENT_DUMMY);
    5969           24 :                   pointer = fold_build3_loc (input_location, COND_EXPR, type,
    5970              :                                              tmp, pointer,
    5971              :                                              fold_convert (type,
    5972              :                                                            null_pointer_node));
    5973              :                 }
    5974              :             }
    5975              :           else
    5976            0 :             gcc_assert (!pass_optional);
    5977              :         }
    5978         1543 :       se->expr = pointer;
    5979         1543 :       se->string_length = parmse->string_length;
    5980              :     }
    5981              : 
    5982         2990 :   return;
    5983              : }
    5984              : 
    5985              : 
    5986              : /* Generate the code for argument list functions.  */
    5987              : 
    5988              : static void
    5989         5846 : conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
    5990              : {
    5991              :   /* Pass by value for g77 %VAL(arg), pass the address
    5992              :      indirectly for %LOC, else by reference.  Thus %REF
    5993              :      is a "do-nothing" and %LOC is the same as an F95
    5994              :      pointer.  */
    5995         5846 :   if (strcmp (name, "%VAL") == 0)
    5996         5834 :     gfc_conv_expr (se, expr);
    5997           12 :   else if (strcmp (name, "%LOC") == 0)
    5998              :     {
    5999            6 :       gfc_conv_expr_reference (se, expr);
    6000            6 :       se->expr = gfc_build_addr_expr (NULL, se->expr);
    6001              :     }
    6002            6 :   else if (strcmp (name, "%REF") == 0)
    6003            6 :     gfc_conv_expr_reference (se, expr);
    6004              :   else
    6005            0 :     gfc_error ("Unknown argument list function at %L", &expr->where);
    6006         5846 : }
    6007              : 
    6008              : 
    6009              : /* This function tells whether the middle-end representation of the expression
    6010              :    E given as input may point to data otherwise accessible through a variable
    6011              :    (sub-)reference.
    6012              :    It is assumed that the only expressions that may alias are variables,
    6013              :    and array constructors if ARRAY_MAY_ALIAS is true and some of its elements
    6014              :    may alias.
    6015              :    This function is used to decide whether freeing an expression's allocatable
    6016              :    components is safe or should be avoided.
    6017              : 
    6018              :    If ARRAY_MAY_ALIAS is true, an array constructor may alias if some of
    6019              :    its elements are copied from a variable.  This ARRAY_MAY_ALIAS trick
    6020              :    is necessary because for array constructors, aliasing depends on how
    6021              :    the array is used:
    6022              :     - If E is an array constructor used as argument to an elemental procedure,
    6023              :       the array, which is generated through shallow copy by the scalarizer,
    6024              :       is used directly and can alias the expressions it was copied from.
    6025              :     - If E is an array constructor used as argument to a non-elemental
    6026              :       procedure,the scalarizer is used in gfc_conv_expr_descriptor to generate
    6027              :       the array as in the previous case, but then that array is used
    6028              :       to initialize a new descriptor through deep copy.  There is no alias
    6029              :       possible in that case.
    6030              :    Thus, the ARRAY_MAY_ALIAS flag is necessary to distinguish the two cases
    6031              :    above.  */
    6032              : 
    6033              : static bool
    6034         7795 : expr_may_alias_variables (gfc_expr *e, bool array_may_alias)
    6035              : {
    6036         7795 :   gfc_constructor *c;
    6037              : 
    6038         7795 :   if (e->expr_type == EXPR_OP
    6039           48 :       && e->value.op.op == INTRINSIC_PARENTHESES
    6040           48 :       && e->value.op.op1->expr_type == EXPR_ARRAY)
    6041         7795 :     e = e->value.op.op1;
    6042              : 
    6043         7795 :   if (e->expr_type == EXPR_VARIABLE)
    6044              :     return true;
    6045          586 :   else if (e->expr_type == EXPR_FUNCTION)
    6046              :     {
    6047          161 :       gfc_symbol *proc_ifc = gfc_get_proc_ifc_for_expr (e);
    6048              : 
    6049          161 :       if (proc_ifc->result != NULL
    6050          161 :           && ((proc_ifc->result->ts.type == BT_CLASS
    6051           25 :                && proc_ifc->result->ts.u.derived->attr.is_class
    6052           25 :                && CLASS_DATA (proc_ifc->result)->attr.class_pointer)
    6053          161 :               || proc_ifc->result->attr.pointer))
    6054              :         return true;
    6055              :       else
    6056          160 :         return false;
    6057              :     }
    6058          425 :   else if (e->expr_type != EXPR_ARRAY || !array_may_alias)
    6059              :     return false;
    6060              : 
    6061           85 :   for (c = gfc_constructor_first (e->value.constructor);
    6062          239 :        c; c = gfc_constructor_next (c))
    6063          195 :     if (c->expr
    6064          195 :         && expr_may_alias_variables (c->expr, array_may_alias))
    6065              :       return true;
    6066              : 
    6067              :   return false;
    6068              : }
    6069              : 
    6070              : 
    6071              : /* A helper function to set the dtype for unallocated or unassociated
    6072              :    entities.  */
    6073              : 
    6074              : static void
    6075          891 : set_dtype_for_unallocated (gfc_se *parmse, gfc_expr *e)
    6076              : {
    6077          891 :   tree tmp;
    6078          891 :   tree desc;
    6079          891 :   tree cond;
    6080          891 :   tree type;
    6081          891 :   stmtblock_t block;
    6082              : 
    6083              :   /* TODO Figure out how to handle optional dummies.  */
    6084          891 :   if (e && e->expr_type == EXPR_VARIABLE
    6085          807 :       && e->symtree->n.sym->attr.optional)
    6086          108 :     return;
    6087              : 
    6088          819 :   desc = parmse->expr;
    6089          819 :   if (desc == NULL_TREE)
    6090              :     return;
    6091              : 
    6092          819 :   if (POINTER_TYPE_P (TREE_TYPE (desc)))
    6093          819 :     desc = build_fold_indirect_ref_loc (input_location, desc);
    6094          819 :   if (GFC_CLASS_TYPE_P (TREE_TYPE (desc)))
    6095          192 :     desc = gfc_class_data_get (desc);
    6096          819 :   if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
    6097              :     return;
    6098              : 
    6099          783 :   gfc_init_block (&block);
    6100          783 :   tmp = gfc_conv_descriptor_data_get (desc);
    6101          783 :   cond = fold_build2_loc (input_location, EQ_EXPR,
    6102              :                           logical_type_node, tmp,
    6103          783 :                           build_int_cst (TREE_TYPE (tmp), 0));
    6104          783 :   type = gfc_get_element_type (TREE_TYPE (desc));
    6105          783 :   gfc_conv_descriptor_dtype_set (&block, desc,
    6106              :                                  gfc_get_dtype_rank_type (e->rank, type));
    6107          783 :   cond = build3_v (COND_EXPR, cond,
    6108              :                    gfc_finish_block (&block),
    6109              :                    build_empty_stmt (input_location));
    6110          783 :   gfc_add_expr_to_block (&parmse->pre, cond);
    6111              : }
    6112              : 
    6113              : 
    6114              : 
    6115              : /* Provide an interface between gfortran array descriptors and the F2018:18.4
    6116              :    ISO_Fortran_binding array descriptors. */
    6117              : 
    6118              : static void
    6119         6537 : gfc_conv_gfc_desc_to_cfi_desc (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym)
    6120              : {
    6121         6537 :   stmtblock_t block, block2;
    6122         6537 :   tree cfi, gfc, tmp, tmp2;
    6123         6537 :   tree present = NULL;
    6124         6537 :   tree gfc_strlen = NULL;
    6125         6537 :   tree rank;
    6126         6537 :   gfc_se se;
    6127              : 
    6128         6537 :   if (fsym->attr.optional
    6129         1094 :       && e->expr_type == EXPR_VARIABLE
    6130         1094 :       && e->symtree->n.sym->attr.optional)
    6131          103 :     present = gfc_conv_expr_present (e->symtree->n.sym);
    6132              : 
    6133         6537 :   gfc_init_block (&block);
    6134              : 
    6135              :   /* Convert original argument to a tree. */
    6136         6537 :   gfc_init_se (&se, NULL);
    6137         6537 :   if (e->rank == 0)
    6138              :     {
    6139          687 :       se.want_pointer = 1;
    6140          687 :       gfc_conv_expr (&se, e);
    6141          687 :       gfc = se.expr;
    6142              :     }
    6143              :   else
    6144              :     {
    6145              :       /* If the actual argument can be noncontiguous, copy-in/out is required,
    6146              :          if the dummy has either the CONTIGUOUS attribute or is an assumed-
    6147              :          length assumed-length/assumed-size CHARACTER array.  This only
    6148              :          applies if the actual argument is a "variable"; if it's some
    6149              :          non-lvalue expression, we are going to evaluate it to a
    6150              :          temporary below anyway.  */
    6151         5850 :       se.force_no_tmp = 1;
    6152         5850 :       if ((fsym->attr.contiguous
    6153         4769 :            || (fsym->ts.type == BT_CHARACTER && !fsym->ts.u.cl->length
    6154         1375 :                && (fsym->as->type == AS_ASSUMED_SIZE
    6155          937 :                    || fsym->as->type == AS_EXPLICIT)))
    6156         2023 :           && !gfc_is_simply_contiguous (e, false, true)
    6157         6883 :           && gfc_expr_is_variable (e))
    6158              :         {
    6159         1027 :           bool optional = fsym->attr.optional;
    6160         1027 :           fsym->attr.optional = 0;
    6161         1027 :           gfc_conv_subref_array_arg (&se, e, false, fsym->attr.intent,
    6162         1027 :                                      fsym->attr.pointer, fsym,
    6163         1027 :                                      fsym->ns->proc_name->name, NULL,
    6164              :                                      /* check_contiguous= */ true);
    6165         1027 :           fsym->attr.optional = optional;
    6166              :         }
    6167              :       else
    6168         4823 :         gfc_conv_expr_descriptor (&se, e);
    6169         5850 :       gfc = se.expr;
    6170              :       /* For dt(:)%var, the base_addr is that of the subobject and elem_len is
    6171              :          its size, see below.  The descriptor built for a subreference of the
    6172              :          array provides both.  While sm is fine as it uses span*stride and not
    6173              :          elem_len.  */
    6174         5850 :       if (POINTER_TYPE_P (TREE_TYPE (gfc)))
    6175         1027 :         gfc = build_fold_indirect_ref_loc (input_location, gfc);
    6176              :     }
    6177         6537 :   if (e->ts.type == BT_CHARACTER)
    6178              :     {
    6179         3409 :       if (se.string_length)
    6180              :         gfc_strlen = se.string_length;
    6181            1 :       else if (e->ts.u.cl->backend_decl)
    6182              :         gfc_strlen = e->ts.u.cl->backend_decl;
    6183              :       else
    6184            0 :         gcc_unreachable ();
    6185              :     }
    6186         6537 :   gfc_add_block_to_block (&block, &se.pre);
    6187              : 
    6188              :   /* Create array descriptor and set version, rank, attribute, type. */
    6189        12769 :   cfi = gfc_create_var (gfc_get_cfi_type (e->rank < 0
    6190              :                                           ? GFC_MAX_DIMENSIONS : e->rank,
    6191              :                                           false), "cfi");
    6192              :   /* Convert to CFI_cdesc_t, which has dim[] to avoid TBAA issues,*/
    6193         6537 :   if (fsym->attr.dimension && fsym->as->type == AS_ASSUMED_RANK)
    6194              :     {
    6195         2516 :       tmp = gfc_get_cfi_type (-1, !fsym->attr.pointer && !fsym->attr.target);
    6196         2338 :       tmp = build_pointer_type (tmp);
    6197         2338 :       parmse->expr = cfi = gfc_build_addr_expr (tmp, cfi);
    6198         2338 :       cfi = build_fold_indirect_ref_loc (input_location, cfi);
    6199              :     }
    6200              :   else
    6201         4199 :     parmse->expr = gfc_build_addr_expr (NULL, cfi);
    6202              : 
    6203         6537 :   tmp = gfc_get_cfi_desc_version (cfi);
    6204         6537 :   gfc_add_modify (&block, tmp,
    6205         6537 :                   build_int_cst (TREE_TYPE (tmp), CFI_VERSION));
    6206         6537 :   if (e->rank < 0)
    6207          305 :     rank = gfc_conv_descriptor_rank_get (gfc);
    6208              :   else
    6209         6232 :     rank = gfc_rank_cst[e->rank];
    6210         6537 :   tmp = gfc_get_cfi_desc_rank (cfi);
    6211         6537 :   gfc_add_modify (&block, tmp,
    6212         6537 :                   fold_convert (TREE_TYPE (tmp), rank));
    6213         6537 :   int itype = CFI_type_other;
    6214         6537 :   if (e->ts.f90_type == BT_VOID)
    6215           96 :     itype = (e->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
    6216           96 :              ? CFI_type_cfunptr : CFI_type_cptr);
    6217              :   else
    6218              :     {
    6219         6441 :       if (e->expr_type == EXPR_NULL && e->ts.type == BT_UNKNOWN)
    6220            1 :         e->ts = fsym->ts;
    6221         6441 :       switch (e->ts.type)
    6222              :         {
    6223         2296 :         case BT_INTEGER:
    6224         2296 :         case BT_LOGICAL:
    6225         2296 :         case BT_REAL:
    6226         2296 :         case BT_COMPLEX:
    6227         2296 :           itype = CFI_type_from_type_kind (e->ts.type, e->ts.kind);
    6228         2296 :           break;
    6229         3410 :         case BT_CHARACTER:
    6230         3410 :           itype = CFI_type_from_type_kind (CFI_type_Character, e->ts.kind);
    6231         3410 :           break;
    6232              :         case BT_DERIVED:
    6233         6537 :           itype = CFI_type_struct;
    6234              :           break;
    6235            0 :         case BT_VOID:
    6236            0 :           itype = (e->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
    6237            0 :                    ? CFI_type_cfunptr : CFI_type_cptr);
    6238              :           break;
    6239              :         case BT_ASSUMED:
    6240              :           itype = CFI_type_other;  // FIXME: Or CFI_type_cptr ?
    6241              :           break;
    6242            1 :         case BT_CLASS:
    6243            1 :           if (fsym->ts.type == BT_ASSUMED)
    6244              :             {
    6245              :               // F2017: 7.3.2.2: "An entity that is declared using the TYPE(*)
    6246              :               // type specifier is assumed-type and is an unlimited polymorphic
    6247              :               //  entity." The actual argument _data component is passed.
    6248              :               itype = CFI_type_other;  // FIXME: Or CFI_type_cptr ?
    6249              :               break;
    6250              :             }
    6251              :           else
    6252            0 :             gcc_unreachable ();
    6253              : 
    6254            0 :         case BT_UNSIGNED:
    6255            0 :           gfc_internal_error ("Unsigned not yet implemented");
    6256              : 
    6257            0 :         case BT_PROCEDURE:
    6258            0 :         case BT_HOLLERITH:
    6259            0 :         case BT_UNION:
    6260            0 :         case BT_BOZ:
    6261            0 :         case BT_UNKNOWN:
    6262              :           // FIXME: Really unreachable? Or reachable for type(*) ? If so, CFI_type_other?
    6263            0 :           gcc_unreachable ();
    6264              :         }
    6265              :     }
    6266              : 
    6267         6537 :   tmp = gfc_get_cfi_desc_type (cfi);
    6268         6537 :   gfc_add_modify (&block, tmp,
    6269         6537 :                   build_int_cst (TREE_TYPE (tmp), itype));
    6270              : 
    6271         6537 :   int attr = CFI_attribute_other;
    6272         6537 :   if (fsym->attr.pointer)
    6273              :     attr = CFI_attribute_pointer;
    6274         5774 :   else if (fsym->attr.allocatable)
    6275          433 :     attr = CFI_attribute_allocatable;
    6276         6537 :   tmp = gfc_get_cfi_desc_attribute (cfi);
    6277         6537 :   gfc_add_modify (&block, tmp,
    6278         6537 :                   build_int_cst (TREE_TYPE (tmp), attr));
    6279              : 
    6280              :   /* The cfi-base_addr assignment could be skipped for 'pointer, intent(out)'.
    6281              :      That is very sensible for undefined pointers, but the C code might assume
    6282              :      that the pointer retains the value, in particular, if it was NULL.  */
    6283         6537 :   if (e->rank == 0)
    6284              :     {
    6285          687 :       tmp = gfc_get_cfi_desc_base_addr (cfi);
    6286          687 :       gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), gfc));
    6287              :     }
    6288              :   else
    6289              :     {
    6290         5850 :       tmp = gfc_get_cfi_desc_base_addr (cfi);
    6291         5850 :       tmp2 = gfc_conv_descriptor_data_get (gfc);
    6292         5850 :       gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), tmp2));
    6293              :     }
    6294              : 
    6295              :   /* Set elem_len if known - must be before the next if block.
    6296              :      Note that allocatable implies 'len=:'.  */
    6297         6537 :   if (e->ts.type != BT_ASSUMED && e->ts.type != BT_CHARACTER )
    6298              :     {
    6299              :       /* Length is known at compile time; use 'block' for it.  */
    6300         3073 :       tmp = size_in_bytes (gfc_typenode_for_spec (&e->ts));
    6301         3073 :       tmp2 = gfc_get_cfi_desc_elem_len (cfi);
    6302         3073 :       gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
    6303              :     }
    6304              : 
    6305         6537 :   if (fsym->attr.pointer && fsym->attr.intent == INTENT_OUT)
    6306           91 :     goto done;
    6307              : 
    6308              :   /* When allocatable + intent out, free the cfi descriptor.  */
    6309         6446 :   if (fsym->attr.allocatable && fsym->attr.intent == INTENT_OUT)
    6310              :     {
    6311           90 :       tmp = gfc_get_cfi_desc_base_addr (cfi);
    6312           90 :       tree call = builtin_decl_explicit (BUILT_IN_FREE);
    6313           90 :       call = build_call_expr_loc (input_location, call, 1, tmp);
    6314           90 :       gfc_add_expr_to_block (&block, fold_convert (void_type_node, call));
    6315           90 :       gfc_add_modify (&block, tmp,
    6316           90 :                       fold_convert (TREE_TYPE (tmp), null_pointer_node));
    6317           90 :       goto done;
    6318              :     }
    6319              : 
    6320              :   /* If not unallocated/unassociated. */
    6321         6356 :   gfc_init_block (&block2);
    6322              : 
    6323              :   /* Set elem_len, which may be only known at run time. */
    6324         6356 :   if (e->ts.type == BT_CHARACTER
    6325         3410 :       && (e->expr_type != EXPR_NULL || gfc_strlen != NULL_TREE))
    6326              :     {
    6327         3408 :       gcc_assert (gfc_strlen);
    6328         3409 :       tmp = gfc_strlen;
    6329         3409 :       if (e->ts.kind != 1)
    6330         1117 :         tmp = fold_build2_loc (input_location, MULT_EXPR,
    6331              :                                gfc_charlen_type_node, tmp,
    6332              :                                build_int_cst (gfc_charlen_type_node,
    6333         1117 :                                               e->ts.kind));
    6334         3409 :       tmp2 = gfc_get_cfi_desc_elem_len (cfi);
    6335         3409 :       gfc_add_modify (&block2, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
    6336              :     }
    6337         2947 :   else if (e->ts.type == BT_ASSUMED)
    6338              :     {
    6339           54 :       tmp = gfc_conv_descriptor_elem_len_get (gfc);
    6340           54 :       tmp2 = gfc_get_cfi_desc_elem_len (cfi);
    6341           54 :       gfc_add_modify (&block2, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
    6342              :     }
    6343              : 
    6344         6356 :   if (e->ts.type == BT_ASSUMED)
    6345              :     {
    6346              :       /* Note: type(*) implies assumed-shape/assumed-rank if fsym requires
    6347              :          an CFI descriptor.  Use the type in the descriptor as it provide
    6348              :          mode information. (Quality of implementation feature.)  */
    6349           54 :       tree cond;
    6350           54 :       tree ctype = gfc_get_cfi_desc_type (cfi);
    6351           54 :       tree type = fold_convert (TREE_TYPE (ctype),
    6352              :                                 gfc_conv_descriptor_type_get (gfc));
    6353           54 :       tree kind = fold_convert (TREE_TYPE (ctype),
    6354              :                                 gfc_conv_descriptor_elem_len_get (gfc));
    6355           54 :       kind = fold_build2_loc (input_location, LSHIFT_EXPR, TREE_TYPE (type),
    6356           54 :                               kind, build_int_cst (TREE_TYPE (type),
    6357              :                                                    CFI_type_kind_shift));
    6358              : 
    6359              :       /* if (BT_VOID) CFI_type_cptr else CFI_type_other  */
    6360              :       /* Note: BT_VOID is could also be CFI_type_funcptr, but assume c_ptr. */
    6361           54 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6362           54 :                               build_int_cst (TREE_TYPE (type), BT_VOID));
    6363           54 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, ctype,
    6364           54 :                              build_int_cst (TREE_TYPE (type), CFI_type_cptr));
    6365           54 :       tmp2 = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    6366              :                               ctype,
    6367           54 :                               build_int_cst (TREE_TYPE (type), CFI_type_other));
    6368           54 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    6369              :                               tmp, tmp2);
    6370              :       /* if (BT_DERIVED) CFI_type_struct else  < tmp2 >  */
    6371           54 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6372           54 :                               build_int_cst (TREE_TYPE (type), BT_DERIVED));
    6373           54 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, ctype,
    6374           54 :                              build_int_cst (TREE_TYPE (type), CFI_type_struct));
    6375           54 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    6376              :                               tmp, tmp2);
    6377              :       /* if (BT_CHARACTER) CFI_type_Character + kind=1 else  < tmp2 >  */
    6378              :       /* Note: could also be kind=4, with cfi->elem_len = gfc->elem_len*4.  */
    6379           54 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6380           54 :                               build_int_cst (TREE_TYPE (type), BT_CHARACTER));
    6381           54 :       tmp = build_int_cst (TREE_TYPE (type),
    6382              :                            CFI_type_from_type_kind (CFI_type_Character, 1));
    6383           54 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    6384              :                              ctype, tmp);
    6385           54 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    6386              :                               tmp, tmp2);
    6387              :       /* if (BT_COMPLEX) CFI_type_Complex + kind/2 else  < tmp2 >  */
    6388           54 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6389           54 :                               build_int_cst (TREE_TYPE (type), BT_COMPLEX));
    6390           54 :       tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR, TREE_TYPE (type),
    6391           54 :                              kind, build_int_cst (TREE_TYPE (type), 2));
    6392           54 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (type), tmp,
    6393           54 :                              build_int_cst (TREE_TYPE (type),
    6394              :                                             CFI_type_Complex));
    6395           54 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    6396              :                              ctype, tmp);
    6397           54 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    6398              :                               tmp, tmp2);
    6399              :       /* if (BT_INTEGER || BT_LOGICAL || BT_REAL) type + kind else  <tmp2>  */
    6400           54 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6401           54 :                               build_int_cst (TREE_TYPE (type), BT_INTEGER));
    6402           54 :       tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6403           54 :                               build_int_cst (TREE_TYPE (type), BT_LOGICAL));
    6404           54 :       cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
    6405              :                               cond, tmp);
    6406           54 :       tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6407           54 :                               build_int_cst (TREE_TYPE (type), BT_REAL));
    6408           54 :       cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
    6409              :                               cond, tmp);
    6410           54 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (type),
    6411              :                              type, kind);
    6412           54 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    6413              :                              ctype, tmp);
    6414           54 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    6415              :                               tmp, tmp2);
    6416           54 :       gfc_add_expr_to_block (&block2, tmp2);
    6417              :     }
    6418              : 
    6419         6356 :   if (e->rank != 0)
    6420              :     {
    6421              :       /* Loop: for (i = 0; i < rank; ++i).  */
    6422         5735 :       tree idx = gfc_create_var (gfc_array_dim_rank_type, "idx");
    6423              :       /* Loop body.  */
    6424         5735 :       stmtblock_t loop_body;
    6425         5735 :       gfc_init_block (&loop_body);
    6426              :       /* cfi->dim[i].lower_bound = (allocatable/pointer)
    6427              :                                    ? gfc->dim[i].lbound : 0 */
    6428         5735 :       if (fsym->attr.pointer || fsym->attr.allocatable)
    6429          648 :         tmp = gfc_conv_descriptor_lbound_get (gfc, idx);
    6430              :       else
    6431         5087 :         tmp = gfc_index_zero_node;
    6432         5735 :       gfc_add_modify (&loop_body, gfc_get_cfi_dim_lbound (cfi, idx), tmp);
    6433              :       /* cfi->dim[i].extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1.  */
    6434         5735 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    6435              :                              gfc_conv_descriptor_ubound_get (gfc, idx),
    6436              :                              gfc_conv_descriptor_lbound_get (gfc, idx));
    6437         5735 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    6438              :                              tmp, gfc_index_one_node);
    6439         5735 :       gfc_add_modify (&loop_body, gfc_get_cfi_dim_extent (cfi, idx), tmp);
    6440              :       /* d->dim[n].sm = gfc->dim[i].stride  * gfc->span); */
    6441         5735 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    6442              :                              gfc_conv_descriptor_stride_get (gfc, idx),
    6443              :                              gfc_conv_descriptor_span_get (gfc));
    6444         5735 :       gfc_add_modify (&loop_body, gfc_get_cfi_dim_sm (cfi, idx), tmp);
    6445              : 
    6446              :       /* Generate loop.  */
    6447         5735 :       gfc_simple_for_loop (&block2, idx, gfc_rank_cst[0], rank, LT_EXPR,
    6448              :                            gfc_rank_cst[1], gfc_finish_block (&loop_body));
    6449              : 
    6450         5735 :       if (e->expr_type == EXPR_VARIABLE
    6451         5573 :           && e->ref
    6452         5573 :           && e->ref->u.ar.type == AR_FULL
    6453         2732 :           && e->symtree->n.sym->attr.dummy
    6454          988 :           && e->symtree->n.sym->as
    6455          988 :           && e->symtree->n.sym->as->type == AS_ASSUMED_SIZE)
    6456              :         {
    6457          138 :           tmp = gfc_get_cfi_dim_extent (cfi, gfc_rank_cst[e->rank-1]),
    6458          138 :           gfc_add_modify (&block2, tmp, build_int_cst (TREE_TYPE (tmp), -1));
    6459              :         }
    6460              :     }
    6461              : 
    6462         6356 :   if (fsym->attr.allocatable || fsym->attr.pointer)
    6463              :     {
    6464         1015 :       tmp = gfc_get_cfi_desc_base_addr (cfi),
    6465         1015 :       tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    6466              :                              tmp, null_pointer_node);
    6467         1015 :       tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
    6468              :                       build_empty_stmt (input_location));
    6469         1015 :       gfc_add_expr_to_block (&block, tmp);
    6470              :     }
    6471              :   else
    6472         5341 :     gfc_add_block_to_block (&block, &block2);
    6473              : 
    6474              : 
    6475         6537 : done:
    6476         6537 :   if (present)
    6477              :     {
    6478          103 :       parmse->expr = build3_loc (input_location, COND_EXPR,
    6479          103 :                                  TREE_TYPE (parmse->expr),
    6480              :                                  present, parmse->expr, null_pointer_node);
    6481          103 :       tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block),
    6482              :                       build_empty_stmt (input_location));
    6483          103 :       gfc_add_expr_to_block (&parmse->pre, tmp);
    6484              :     }
    6485              :   else
    6486         6434 :     gfc_add_block_to_block (&parmse->pre, &block);
    6487              : 
    6488         6537 :   gfc_init_block (&block);
    6489              : 
    6490         6537 :   if ((!fsym->attr.allocatable && !fsym->attr.pointer)
    6491         1196 :       || fsym->attr.intent == INTENT_IN)
    6492         5550 :     goto post_call;
    6493              : 
    6494          987 :   gfc_init_block (&block2);
    6495          987 :   if (e->rank == 0)
    6496              :     {
    6497          428 :       tmp = gfc_get_cfi_desc_base_addr (cfi);
    6498          428 :       gfc_add_modify (&block, gfc, fold_convert (TREE_TYPE (gfc), tmp));
    6499              :     }
    6500              :   else
    6501              :     {
    6502          559 :       tmp = gfc_get_cfi_desc_base_addr (cfi);
    6503          559 :       gfc_conv_descriptor_data_set (&block, gfc, tmp);
    6504              : 
    6505          559 :       if (fsym->attr.allocatable)
    6506              :         {
    6507              :           /* gfc->span = cfi->elem_len.  */
    6508          252 :           tmp = fold_convert (gfc_array_index_type,
    6509              :                               gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]));
    6510              :         }
    6511              :       else
    6512              :         {
    6513              :           /* gfc->span = ((cfi->dim[0].sm % cfi->elem_len)
    6514              :                           ? cfi->dim[0].sm : cfi->elem_len).  */
    6515          307 :           tmp = gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]);
    6516          307 :           tmp2 = fold_convert (gfc_array_index_type,
    6517              :                                gfc_get_cfi_desc_elem_len (cfi));
    6518          307 :           tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR,
    6519              :                                  gfc_array_index_type, tmp, tmp2);
    6520          307 :           tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    6521              :                              tmp, gfc_index_zero_node);
    6522          307 :           tmp = build3_loc (input_location, COND_EXPR, gfc_array_index_type, tmp,
    6523              :                             gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]), tmp2);
    6524              :         }
    6525          559 :       gfc_conv_descriptor_span_set (&block2, gfc, tmp);
    6526              : 
    6527              :       /* Calculate offset + set lbound, ubound and stride.  */
    6528          559 :       gfc_conv_descriptor_offset_set (&block2, gfc, gfc_index_zero_node);
    6529              :       /* Loop: for (i = 0; i < rank; ++i).  */
    6530          559 :       tree idx = gfc_create_var (gfc_array_dim_rank_type, "idx");
    6531              :       /* Loop body.  */
    6532          559 :       stmtblock_t loop_body;
    6533          559 :       gfc_init_block (&loop_body);
    6534              :       /* gfc->dim[i].lbound = ... */
    6535          559 :       tmp = gfc_get_cfi_dim_lbound (cfi, idx);
    6536          559 :       gfc_conv_descriptor_lbound_set (&loop_body, gfc, idx, tmp);
    6537              : 
    6538              :       /* gfc->dim[i].ubound = gfc->dim[i].lbound + cfi->dim[i].extent - 1. */
    6539          559 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    6540              :                              gfc_conv_descriptor_lbound_get (gfc, idx),
    6541              :                              gfc_index_one_node);
    6542          559 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    6543              :                              gfc_get_cfi_dim_extent (cfi, idx), tmp);
    6544          559 :       gfc_conv_descriptor_ubound_set (&loop_body, gfc, idx, tmp);
    6545              : 
    6546              :       /* gfc->dim[i].stride = cfi->dim[i].sm / cfi>elem_len */
    6547          559 :       tmp = gfc_get_cfi_dim_sm (cfi, idx);
    6548          559 :       tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    6549              :                              gfc_array_index_type, tmp,
    6550              :                              fold_convert (gfc_array_index_type,
    6551              :                                            gfc_get_cfi_desc_elem_len (cfi)));
    6552          559 :       gfc_conv_descriptor_stride_set (&loop_body, gfc, idx, tmp);
    6553              : 
    6554              :       /* gfc->offset -= gfc->dim[i].stride * gfc->dim[i].lbound. */
    6555          559 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    6556              :                              gfc_conv_descriptor_stride_get (gfc, idx),
    6557              :                              gfc_conv_descriptor_lbound_get (gfc, idx));
    6558          559 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    6559              :                              gfc_conv_descriptor_offset_get (gfc), tmp);
    6560          559 :       gfc_conv_descriptor_offset_set (&loop_body, gfc, tmp);
    6561              :       /* Generate loop.  */
    6562          559 :       gfc_simple_for_loop (&block2, idx, gfc_rank_cst[0], rank, LT_EXPR,
    6563              :                            gfc_rank_cst[1], gfc_finish_block (&loop_body));
    6564              :     }
    6565              : 
    6566          987 :   if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
    6567              :     {
    6568           60 :       tmp = fold_convert (gfc_charlen_type_node,
    6569              :                           gfc_get_cfi_desc_elem_len (cfi));
    6570           60 :       if (e->ts.kind != 1)
    6571           24 :         tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    6572              :                                gfc_charlen_type_node, tmp,
    6573              :                                build_int_cst (gfc_charlen_type_node,
    6574           24 :                                               e->ts.kind));
    6575           60 :       gfc_add_modify (&block2, gfc_strlen, tmp);
    6576              :     }
    6577              : 
    6578          987 :   tmp = gfc_get_cfi_desc_base_addr (cfi),
    6579          987 :   tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    6580              :                          tmp, null_pointer_node);
    6581          987 :   tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
    6582              :                   build_empty_stmt (input_location));
    6583          987 :   gfc_add_expr_to_block (&block, tmp);
    6584              : 
    6585         6537 : post_call:
    6586         6537 :   gfc_add_block_to_block (&block, &se.post);
    6587         6537 :   if (present && block.head)
    6588              :     {
    6589            6 :       tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block),
    6590              :                       build_empty_stmt (input_location));
    6591            6 :       gfc_add_expr_to_block (&parmse->post, tmp);
    6592              :     }
    6593         6531 :   else if (block.head)
    6594         1564 :     gfc_add_block_to_block (&parmse->post, &block);
    6595         6537 : }
    6596              : 
    6597              : 
    6598              : /* Create "conditional temporary" to handle scalar dummy variables with the
    6599              :    OPTIONAL+VALUE attribute that shall not be dereferenced.  Use null value
    6600              :    as fallback.  Does not handle CLASS.  */
    6601              : 
    6602              : static void
    6603          234 : conv_cond_temp (gfc_se * parmse, gfc_expr * e, tree cond)
    6604              : {
    6605          234 :   tree temp;
    6606          234 :   gcc_assert (e && e->ts.type != BT_CLASS);
    6607          234 :   gcc_assert (e->rank == 0);
    6608          234 :   temp = gfc_create_var (TREE_TYPE (parmse->expr), "condtemp");
    6609          234 :   TREE_STATIC (temp) = 1;
    6610          234 :   TREE_CONSTANT (temp) = 1;
    6611          234 :   TREE_READONLY (temp) = 1;
    6612          234 :   DECL_INITIAL (temp) = build_zero_cst (TREE_TYPE (temp));
    6613          234 :   parmse->expr = fold_build3_loc (input_location, COND_EXPR,
    6614          234 :                                   TREE_TYPE (parmse->expr),
    6615              :                                   cond, parmse->expr, temp);
    6616          234 :   parmse->expr = gfc_evaluate_now (parmse->expr, &parmse->pre);
    6617          234 : }
    6618              : 
    6619              : 
    6620              : /* Returns true if the type specified in TS is a character type whose length
    6621              :    is constant.  Otherwise returns false.  */
    6622              : 
    6623              : static bool
    6624        22293 : gfc_const_length_character_type_p (gfc_typespec *ts)
    6625              : {
    6626        22293 :   return (ts->type == BT_CHARACTER
    6627          515 :           && ts->u.cl
    6628          515 :           && ts->u.cl->length
    6629          479 :           && ts->u.cl->length->expr_type == EXPR_CONSTANT
    6630        22760 :           && ts->u.cl->length->ts.type == BT_INTEGER);
    6631              : }
    6632              : 
    6633              : 
    6634              : /* Returns true if FORMAL contains an explicit-shape array dummy with the
    6635              :    VALUE attribute.  The bounds of such a dummy may have to be evaluated
    6636              :    on the caller side, which needs an interface mapping.  */
    6637              : 
    6638              : static bool
    6639       116634 : has_value_array_dummy (gfc_formal_arglist *formal)
    6640              : {
    6641       311672 :   for (; formal; formal = formal->next)
    6642       195104 :     if (formal->sym && formal->sym->attr.value && formal->sym->attr.dimension
    6643          174 :         && formal->sym->as && formal->sym->as->type == AS_EXPLICIT)
    6644              :       return true;
    6645              : 
    6646              :   return false;
    6647              : }
    6648              : 
    6649              : 
    6650              : /* Sequence association (F2023, 15.5.2.12) of a scalar actual argument E with
    6651              :    an explicit-shape array dummy FSYM that has the VALUE attribute.  Copy as
    6652              :    many elements as the dummy declares into a temporary and pass that.
    6653              :    MAPPING supplies the caller-side values of any dummy arguments appearing
    6654              :    in the bounds or the character length of FSYM.  */
    6655              : 
    6656              : static void
    6657           36 : conv_seq_assoc_value_arg (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym,
    6658              :                           gfc_interface_mapping *mapping)
    6659              : {
    6660           36 :   tree nelems, elem_type, elem_size, tmpvar, src, tmp;
    6661           36 :   gfc_se se;
    6662           36 :   int n;
    6663              : 
    6664           36 :   gcc_assert (fsym->as && fsym->as->type == AS_EXPLICIT);
    6665              : 
    6666              :   /* Address of the first element of the actual argument's sequence.  */
    6667           36 :   gfc_init_se (&se, NULL);
    6668           36 :   if (e->ts.type == BT_CHARACTER)
    6669              :     {
    6670           12 :       gfc_conv_expr (&se, e);
    6671           12 :       gfc_conv_string_parameter (&se);
    6672              :       /* The hidden length argument is that of the actual argument, as it
    6673              :          is for a dummy that does not have the VALUE attribute.  */
    6674           12 :       parmse->string_length = se.string_length;
    6675              :     }
    6676              :   else
    6677           24 :     gfc_conv_expr_reference (&se, e);
    6678           36 :   gfc_add_block_to_block (&parmse->pre, &se.pre);
    6679           36 :   gfc_add_block_to_block (&parmse->post, &se.post);
    6680           36 :   src = se.expr;
    6681              : 
    6682              :   /* Number of elements of the dummy.  */
    6683           36 :   nelems = gfc_index_one_node;
    6684           78 :   for (n = 0; n < fsym->as->rank; n++)
    6685              :     {
    6686           42 :       tree lbound, ubound, extent;
    6687              : 
    6688           42 :       gfc_init_se (&se, NULL);
    6689           42 :       gfc_apply_interface_mapping (mapping, &se, fsym->as->upper[n]);
    6690           42 :       gfc_add_block_to_block (&parmse->pre, &se.pre);
    6691           42 :       gfc_add_block_to_block (&parmse->post, &se.post);
    6692           42 :       ubound = fold_convert (gfc_array_index_type, se.expr);
    6693              : 
    6694           42 :       if (fsym->as->lower[n])
    6695              :         {
    6696           42 :           gfc_init_se (&se, NULL);
    6697           42 :           gfc_apply_interface_mapping (mapping, &se, fsym->as->lower[n]);
    6698           42 :           gfc_add_block_to_block (&parmse->pre, &se.pre);
    6699           42 :           gfc_add_block_to_block (&parmse->post, &se.post);
    6700           42 :           lbound = fold_convert (gfc_array_index_type, se.expr);
    6701              :         }
    6702              :       else
    6703            0 :         lbound = gfc_index_one_node;
    6704              : 
    6705           42 :       extent = fold_build2_loc (input_location, MINUS_EXPR,
    6706              :                                 gfc_array_index_type, ubound, lbound);
    6707           42 :       extent = fold_build2_loc (input_location, PLUS_EXPR,
    6708              :                                 gfc_array_index_type, extent,
    6709              :                                 gfc_index_one_node);
    6710           42 :       extent = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
    6711              :                                 extent, gfc_index_zero_node);
    6712           42 :       nelems = fold_build2_loc (input_location, MULT_EXPR,
    6713              :                                 gfc_array_index_type, nelems, extent);
    6714              :     }
    6715           36 :   nelems = gfc_evaluate_now (nelems, &parmse->pre);
    6716              : 
    6717              :   /* Element type and size of the dummy.  For characters the element
    6718              :      sequence is grouped by the character length of the dummy.  */
    6719           36 :   if (fsym->ts.type == BT_CHARACTER)
    6720              :     {
    6721           12 :       tree len;
    6722              : 
    6723           12 :       if (fsym->ts.u.cl->length)
    6724              :         {
    6725           12 :           gfc_init_se (&se, NULL);
    6726           12 :           gfc_apply_interface_mapping (mapping, &se, fsym->ts.u.cl->length);
    6727           12 :           gfc_add_block_to_block (&parmse->pre, &se.pre);
    6728           12 :           gfc_add_block_to_block (&parmse->post, &se.post);
    6729           12 :           len = fold_convert (gfc_charlen_type_node, se.expr);
    6730              :         }
    6731              :       else
    6732            0 :         len = fold_convert (gfc_charlen_type_node, parmse->string_length);
    6733              : 
    6734           12 :       tree char_size = TYPE_SIZE_UNIT (gfc_get_char_type (fsym->ts.kind));
    6735              : 
    6736           12 :       elem_type = gfc_get_character_type_len (fsym->ts.kind, len);
    6737           12 :       elem_size = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
    6738              :                                    fold_convert (size_type_node, len),
    6739              :                                    fold_convert (size_type_node, char_size));
    6740              :     }
    6741              :   else
    6742              :     {
    6743           24 :       elem_type = gfc_typenode_for_spec (&fsym->ts);
    6744           24 :       elem_size = fold_convert (size_type_node, TYPE_SIZE_UNIT (elem_type));
    6745              :     }
    6746              : 
    6747              :   /* The temporary holding the copy.  Allocate at least one element so that
    6748              :      a zero-sized dummy does not produce a degenerate array type.  */
    6749           36 :   tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_array_index_type,
    6750              :                          nelems, gfc_index_one_node);
    6751           36 :   tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    6752              :                          tmp, gfc_index_one_node);
    6753           36 :   tmp = build_array_type (elem_type,
    6754              :                           build_range_type (gfc_array_index_type,
    6755              :                                             gfc_index_zero_node, tmp));
    6756           36 :   tmpvar = gfc_create_var (tmp, "seq_copy");
    6757           36 :   gfc_add_expr_to_block (&parmse->pre,
    6758              :                          fold_build1_loc (input_location, DECL_EXPR, tmp,
    6759              :                                           tmpvar));
    6760              : 
    6761           36 :   tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
    6762              :                          fold_convert (size_type_node, nelems), elem_size);
    6763           36 :   tmp = gfc_build_memcpy_call (fold_convert (pvoid_type_node,
    6764              :                                              gfc_build_addr_expr (NULL_TREE,
    6765              :                                                                   tmpvar)),
    6766              :                                fold_convert (pvoid_type_node, src), tmp);
    6767           36 :   gfc_add_expr_to_block (&parmse->pre, tmp);
    6768              : 
    6769              :   /* The memcpy also copied the component pointers of a derived type, which
    6770              :      would leave the temporary sharing the actual argument's allocatable
    6771              :      components.  Give the copy components of its own and free them again
    6772              :      once the call has returned.  */
    6773           36 :   if (fsym->ts.type == BT_DERIVED && fsym->ts.u.derived->attr.alloc_comp)
    6774              :     {
    6775            6 :       tree src_ptr = fold_convert (build_pointer_type (elem_type), src);
    6776            6 :       tree elem_idx = gfc_create_var (gfc_array_index_type, "elem");
    6777            6 :       tree dest_elem = gfc_build_array_ref (tmpvar, elem_idx, NULL_TREE);
    6778            6 :       tree src_offset = fold_build2_loc (input_location, MULT_EXPR, sizetype,
    6779              :                                          fold_convert (sizetype, elem_idx),
    6780              :                                          elem_size);
    6781            6 :       tree src_elem
    6782            6 :         = build_fold_indirect_ref_loc (input_location,
    6783              :                                        fold_build_pointer_plus_loc
    6784              :                                        (input_location, src_ptr, src_offset));
    6785              : 
    6786            6 :       tmp = gfc_copy_alloc_comp (fsym->ts.u.derived, src_elem, dest_elem, 0, 0);
    6787            6 :       gfc_simple_for_loop (&parmse->pre, elem_idx, gfc_index_zero_node, nelems,
    6788              :                            LT_EXPR, gfc_index_one_node, tmp);
    6789              : 
    6790            6 :       tmp = gfc_deallocate_alloc_comp (fsym->ts.u.derived, dest_elem, 0);
    6791            6 :       gfc_simple_for_loop (&parmse->post, elem_idx, gfc_index_zero_node, nelems,
    6792              :                            LT_EXPR, gfc_index_one_node, tmp);
    6793              :     }
    6794              : 
    6795           36 :   if (fsym->ts.type == BT_CHARACTER)
    6796           12 :     parmse->expr
    6797           12 :       = gfc_build_addr_expr (build_pointer_type (gfc_get_char_type
    6798              :                                                  (fsym->ts.kind)), tmpvar);
    6799              :   else
    6800           24 :     parmse->expr = gfc_build_addr_expr (build_pointer_type (elem_type), tmpvar);
    6801           36 : }
    6802              : 
    6803              : 
    6804              : /* Helper function for the handling of (currently) scalar dummy variables
    6805              :    with the VALUE attribute.  Argument parmse should already be set up.  */
    6806              : static void
    6807        22739 : conv_dummy_value (gfc_se * parmse, gfc_expr * e, gfc_symbol * fsym,
    6808              :                   vec<tree, va_gc> *& optionalargs)
    6809              : {
    6810        22739 :   tree tmp;
    6811              : 
    6812        22739 :   gcc_assert (fsym && fsym->attr.value && !fsym->attr.dimension);
    6813              : 
    6814        22739 :   if (IS_PDT (e))
    6815              :     {
    6816            6 :       tmp = gfc_create_var (TREE_TYPE (parmse->expr), "PDT");
    6817            6 :       gfc_add_modify (&parmse->pre, tmp, parmse->expr);
    6818            6 :       gfc_add_expr_to_block (&parmse->pre,
    6819            6 :                              gfc_copy_alloc_comp (e->ts.u.derived,
    6820              :                                                   parmse->expr, tmp,
    6821              :                                                   e->rank, 0));
    6822            6 :       parmse->expr = tmp;
    6823            6 :       tmp = gfc_deallocate_pdt_comp (e->ts.u.derived, tmp, e->rank);
    6824            6 :       gfc_add_expr_to_block (&parmse->post, tmp);
    6825            6 :       return;
    6826              :     }
    6827              : 
    6828              :   /* Absent actual argument for optional scalar dummy.  */
    6829        22733 :   if ((e == NULL || e->expr_type == EXPR_NULL) && fsym->attr.optional)
    6830              :     {
    6831              :       /* For scalar arguments with VALUE attribute which are passed by
    6832              :          value, pass "0" and a hidden argument for the optional status.  */
    6833          440 :       if (fsym->ts.type == BT_CHARACTER)
    6834              :         {
    6835              :           /* Pass a NULL pointer for an absent CHARACTER arg and a length of
    6836              :              zero.  */
    6837          102 :           parmse->expr = null_pointer_node;
    6838          102 :           parmse->string_length = build_int_cst (gfc_charlen_type_node, 0);
    6839              :         }
    6840          338 :       else if (gfc_bt_struct (fsym->ts.type)
    6841           30 :                && !(fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING))
    6842              :         {
    6843              :           /* Pass null struct.  Types c_ptr and c_funptr from ISO_C_BINDING
    6844              :              are pointers and passed as such below.  */
    6845           24 :           tree temp = gfc_create_var (gfc_sym_type (fsym), "absent");
    6846           24 :           TREE_CONSTANT (temp) = 1;
    6847           24 :           TREE_READONLY (temp) = 1;
    6848           24 :           DECL_INITIAL (temp) = build_zero_cst (TREE_TYPE (temp));
    6849           24 :           parmse->expr = temp;
    6850           24 :         }
    6851              :       else
    6852          314 :         parmse->expr = fold_convert (gfc_sym_type (fsym),
    6853              :                                      integer_zero_node);
    6854          440 :       vec_safe_push (optionalargs, boolean_false_node);
    6855              : 
    6856          440 :       return;
    6857              :     }
    6858              : 
    6859              :   /* Assumed-length or non-constant-length CHARACTER VALUE dummy: copy
    6860              :      the actual argument and pass the copy.  */
    6861        22293 :   if (fsym->ts.type == BT_CHARACTER
    6862          515 :       && (!fsym->ts.u.cl || !fsym->ts.u.cl->length
    6863          479 :           || fsym->ts.u.cl->length->expr_type != EXPR_CONSTANT))
    6864              :     {
    6865              :       /* An optional actual argument that is absent has nothing to copy
    6866              :          from; pass a null pointer and a length of zero instead.  */
    6867           48 :       tree present = NULL_TREE;
    6868           48 :       if (fsym->attr.optional && e->expr_type == EXPR_VARIABLE
    6869           24 :           && e->symtree->n.sym->attr.optional)
    6870           12 :         present = gfc_conv_expr_present (e->symtree->n.sym);
    6871              : 
    6872           48 :       gfc_conv_string_parameter (parmse);
    6873           48 :       tree len = fold_convert (gfc_charlen_type_node, parmse->string_length);
    6874           48 :       if (present)
    6875              :         {
    6876           12 :           len = fold_build3_loc (input_location, COND_EXPR,
    6877              :                                  gfc_charlen_type_node, present, len,
    6878              :                                  build_zero_cst (gfc_charlen_type_node));
    6879           12 :           len = gfc_evaluate_now (len, &parmse->pre);
    6880           12 :           parmse->string_length = len;
    6881              :         }
    6882           48 :       tree chartype = gfc_get_character_type_len (fsym->ts.kind, len);
    6883           48 :       tree val_copy = gfc_create_var (chartype, "val_copy");
    6884           48 :       tmp = fold_build1_loc (input_location, DECL_EXPR, chartype, val_copy);
    6885           48 :       gfc_add_expr_to_block (&parmse->pre, tmp);
    6886              :       /* The copy size is in bytes, not in characters.  */
    6887           48 :       tree bytes
    6888           48 :         = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
    6889              :                            fold_convert (size_type_node, len),
    6890           48 :                            fold_convert (size_type_node,
    6891              :                                          TYPE_SIZE_UNIT (gfc_get_char_type
    6892              :                                                          (fsym->ts.kind))));
    6893           48 :       tmp = gfc_build_memcpy_call (
    6894              :         fold_convert (pvoid_type_node,
    6895              :                       gfc_build_addr_expr (NULL_TREE, val_copy)),
    6896              :         fold_convert (pvoid_type_node, parmse->expr), bytes);
    6897           48 :       if (present)
    6898           12 :         tmp = build3_v (COND_EXPR, present, tmp,
    6899              :                         build_empty_stmt (input_location));
    6900           48 :       gfc_add_expr_to_block (&parmse->pre, tmp);
    6901           48 :       parmse->expr = fold_convert (
    6902              :         build_pointer_type (gfc_get_char_type (fsym->ts.kind)),
    6903              :         gfc_build_addr_expr (NULL_TREE, val_copy));
    6904           48 :       if (present)
    6905           24 :         parmse->expr = fold_build3_loc (input_location, COND_EXPR,
    6906           12 :                                         TREE_TYPE (parmse->expr), present,
    6907              :                                         parmse->expr,
    6908           12 :                                         fold_convert (TREE_TYPE (parmse->expr),
    6909              :                                                       null_pointer_node));
    6910              :     }
    6911              : 
    6912              :   /* Truncate a too long constant character actual argument.  */
    6913        22293 :   if (gfc_const_length_character_type_p (&fsym->ts)
    6914          467 :       && e->expr_type == EXPR_CONSTANT
    6915        22376 :       && mpz_cmp_ui (fsym->ts.u.cl->length->value.integer,
    6916              :                      e->value.character.length) < 0)
    6917              :     {
    6918           17 :       gfc_charlen_t flen = mpz_get_ui (fsym->ts.u.cl->length->value.integer);
    6919              : 
    6920              :       /* Truncate actual string argument.  */
    6921           17 :       gfc_conv_expr (parmse, e);
    6922           34 :       parmse->expr = gfc_build_wide_string_const (e->ts.kind, flen,
    6923           17 :                                                   e->value.character.string);
    6924           17 :       parmse->string_length = build_int_cst (gfc_charlen_type_node, flen);
    6925              : 
    6926           17 :       if (flen == 1)
    6927              :         {
    6928           14 :           tree slen1 = build_int_cst (gfc_charlen_type_node, 1);
    6929           14 :           gfc_conv_string_parameter (parmse);
    6930           14 :           parmse->expr = gfc_string_to_single_character (slen1, parmse->expr,
    6931              :                                                          e->ts.kind);
    6932              :         }
    6933              : 
    6934              :       /* Indicate value,optional scalar dummy argument as present.  */
    6935           17 :       if (fsym->attr.optional)
    6936            1 :         vec_safe_push (optionalargs, boolean_true_node);
    6937              :       return;
    6938              :     }
    6939              : 
    6940              :   /* gfortran argument passing conventions:
    6941              :      actual arguments to CHARACTER(len=1),VALUE
    6942              :      dummy arguments are actually passed by value.
    6943              :      Strings are truncated to length 1.  */
    6944        22276 :   if (gfc_length_one_character_type_p (&fsym->ts))
    6945              :     {
    6946          378 :       if (e->expr_type == EXPR_CONSTANT
    6947           54 :           && e->value.character.length > 1)
    6948              :         {
    6949            0 :           e->value.character.length = 1;
    6950            0 :           gfc_conv_expr (parmse, e);
    6951              :         }
    6952              : 
    6953          378 :       tree slen1 = build_int_cst (gfc_charlen_type_node, 1);
    6954          378 :       gfc_conv_string_parameter (parmse);
    6955          378 :       parmse->expr = gfc_string_to_single_character (slen1, parmse->expr,
    6956              :                                                      e->ts.kind);
    6957              :       /* Truncate resulting string to length 1.  */
    6958          378 :       parmse->string_length = slen1;
    6959              :     }
    6960              : 
    6961        22276 :   if (fsym->attr.optional && fsym->ts.type != BT_CLASS)
    6962              :     {
    6963              :       /* F2018:15.5.2.12 Argument presence and
    6964              :          restrictions on arguments not present.  */
    6965          853 :       if (e->expr_type == EXPR_VARIABLE
    6966          674 :           && e->rank == 0
    6967         1473 :           && (gfc_expr_attr (e).allocatable
    6968          620 :               || gfc_expr_attr (e).pointer))
    6969              :         {
    6970          198 :           gfc_se argse;
    6971          198 :           tree cond;
    6972          198 :           gfc_init_se (&argse, NULL);
    6973          198 :           argse.want_pointer = 1;
    6974          198 :           gfc_conv_expr (&argse, e);
    6975          198 :           cond = fold_convert (TREE_TYPE (argse.expr), null_pointer_node);
    6976          198 :           cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    6977              :                                   argse.expr, cond);
    6978          198 :           if (e->symtree->n.sym->attr.dummy)
    6979           24 :             cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    6980              :                                     logical_type_node,
    6981              :                                     gfc_conv_expr_present (e->symtree->n.sym),
    6982              :                                     cond);
    6983          198 :           vec_safe_push (optionalargs, fold_convert (boolean_type_node, cond));
    6984              :           /* Create "conditional temporary".  */
    6985          198 :           conv_cond_temp (parmse, e, cond);
    6986              :         }
    6987          655 :       else if (e->expr_type != EXPR_VARIABLE
    6988          476 :                || !e->symtree->n.sym->attr.optional
    6989          272 :                || (e->ref != NULL && e->ref->type != REF_ARRAY))
    6990          383 :         vec_safe_push (optionalargs, boolean_true_node);
    6991              :       else
    6992              :         {
    6993          272 :           tmp = gfc_conv_expr_present (e->symtree->n.sym);
    6994          272 :           if (gfc_bt_struct (fsym->ts.type)
    6995           36 :               && !(fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING))
    6996           36 :             conv_cond_temp (parmse, e, tmp);
    6997          236 :           else if (e->ts.type != BT_CHARACTER && !e->symtree->n.sym->attr.value)
    6998           84 :             parmse->expr
    6999          168 :               = fold_build3_loc (input_location, COND_EXPR,
    7000           84 :                                  TREE_TYPE (parmse->expr),
    7001              :                                  tmp, parmse->expr,
    7002           84 :                                  fold_convert (TREE_TYPE (parmse->expr),
    7003              :                                                integer_zero_node));
    7004              : 
    7005          544 :           vec_safe_push (optionalargs,
    7006          272 :                          fold_convert (boolean_type_node, tmp));
    7007              :         }
    7008              :     }
    7009              : }
    7010              : 
    7011              : 
    7012              : /* Helper function for the handling of NULL() actual arguments associated with
    7013              :    non-optional dummy variables.  Argument parmse should already be set up.  */
    7014              : static void
    7015          444 : conv_null_actual (gfc_se * parmse, gfc_expr * e, gfc_symbol * fsym)
    7016              : {
    7017          444 :   gcc_assert (fsym && e->expr_type == EXPR_NULL);
    7018              : 
    7019              :   /* Obtain the character length for a NULL() actual with a character
    7020              :      MOLD argument.  Otherwise substitute a suitable dummy length.
    7021              :      Here we handle only non-optional dummies of non-bind(c) procedures.  */
    7022          444 :   if (fsym->ts.type == BT_CHARACTER)
    7023              :     {
    7024          216 :       if (e->ts.type == BT_CHARACTER
    7025          162 :           && e->symtree->n.sym->ts.type == BT_CHARACTER)
    7026              :         {
    7027              :           /* MOLD is present.  Substitute a temporary character NULL pointer.
    7028              :              For an assumed-rank dummy we need a descriptor that passes the
    7029              :              correct rank.  */
    7030          162 :           if (fsym->as && fsym->as->type == AS_ASSUMED_RANK)
    7031              :             {
    7032           54 :               tree tmp;
    7033           54 :               tmp = gfc_create_null_actual_descriptor (&parmse->pre, &e->ts,
    7034              :                                                        fsym->attr, e->rank);
    7035           54 :               parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
    7036           54 :             }
    7037              :           else
    7038              :             {
    7039          108 :               tree tmp = gfc_create_var (TREE_TYPE (parmse->expr), "null");
    7040          108 :               gfc_add_modify (&parmse->pre, tmp,
    7041          108 :                               build_zero_cst (TREE_TYPE (tmp)));
    7042          108 :               parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
    7043              :             }
    7044              : 
    7045              :           /* Ensure that a usable length is available.  */
    7046          162 :           if (parmse->string_length == NULL_TREE)
    7047              :             {
    7048          162 :               gfc_typespec *ts = &e->symtree->n.sym->ts;
    7049              : 
    7050          162 :               if (ts->u.cl->length != NULL
    7051          108 :                   && ts->u.cl->length->expr_type == EXPR_CONSTANT)
    7052          108 :                 gfc_conv_const_charlen (ts->u.cl);
    7053              : 
    7054          162 :               if (ts->u.cl->backend_decl)
    7055          162 :                 parmse->string_length = ts->u.cl->backend_decl;
    7056              :             }
    7057              :         }
    7058           54 :       else if (e->ts.type == BT_UNKNOWN && parmse->string_length == NULL_TREE)
    7059              :         {
    7060              :           /* MOLD is not present.  Pass length of associated dummy character
    7061              :              argument if constant, or zero.  */
    7062           54 :           if (fsym->ts.u.cl->length != NULL
    7063           18 :               && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
    7064              :             {
    7065           18 :               gfc_conv_const_charlen (fsym->ts.u.cl);
    7066           18 :               parmse->string_length = fsym->ts.u.cl->backend_decl;
    7067              :             }
    7068              :           else
    7069              :             {
    7070           36 :               parmse->string_length = gfc_create_var (gfc_charlen_type_node,
    7071              :                                                       "slen");
    7072           36 :               gfc_add_modify (&parmse->pre, parmse->string_length,
    7073              :                               build_zero_cst (gfc_charlen_type_node));
    7074              :             }
    7075              :         }
    7076              :     }
    7077          228 :   else if (fsym->ts.type == BT_DERIVED
    7078           18 :            || ((fsym->attr.allocatable || fsym->attr.pointer)
    7079           18 :                && fsym->ts.type != BT_CLASS))
    7080              :     {
    7081          228 :       if (e->ts.type != BT_UNKNOWN)
    7082              :         /* MOLD is present.  Pass a corresponding temporary NULL pointer.
    7083              :            For an assumed-rank dummy we provide a descriptor that passes
    7084              :            the correct rank.  */
    7085              :         {
    7086          138 :           tree tmp = gfc_create_null_actual_descriptor (&parmse->pre, &e->ts,
    7087              :                                                         fsym->attr, e->rank);
    7088          138 :           parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
    7089              :         }
    7090              :       else
    7091              :         /* MOLD is not present.  Use attributes from dummy argument, which is
    7092              :            not allowed to be assumed-rank.  */
    7093              :         {
    7094           90 :           int dummy_rank = fsym->as ? fsym->as->rank : 0;
    7095           90 :           tree tmp = gfc_create_null_actual_descriptor (&parmse->pre, &fsym->ts,
    7096              :                                                         fsym->attr, dummy_rank);
    7097           90 :           parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
    7098              :         }
    7099              :     }
    7100          444 : }
    7101              : 
    7102              : 
    7103              : /* Return true if a subobject of the elements of an array is referenced.  */
    7104              : 
    7105              : static bool
    7106           60 : is_subobject_ref (gfc_expr *e)
    7107              : {
    7108           60 :   bool seen_array = false;
    7109              : 
    7110          132 :   for (gfc_ref *ref = e->ref; ref; ref = ref->next)
    7111              :     {
    7112          120 :       if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
    7113              :         seen_array = true;
    7114           60 :       else if (seen_array)
    7115              :         return true;
    7116              :     }
    7117              : 
    7118              :   return false;
    7119              : }
    7120              : 
    7121              : 
    7122              : /* Return true if expr is a span addressed dummy that is passed on as a whole,
    7123              :    rather than a reference to a subobject of the elements of an array.  */
    7124              : 
    7125              : static bool
    7126          831 : is_whole_span_addressed_dummy (gfc_expr *e)
    7127              : {
    7128          831 :   return e->expr_type == EXPR_VARIABLE
    7129          831 :          && e->symtree && e->symtree->n.sym
    7130          831 :          && gfc_is_span_addressed_dummy (e->symtree->n.sym)
    7131          891 :          && !is_subobject_ref (e);
    7132              : }
    7133              : 
    7134              : 
    7135              : /* Return true if the dummy fsym has an array descriptor and so addresses its
    7136              :    elements by the strides held in it.  Such a dummy accepts an actual
    7137              :    argument of any stride; only a dummy without a descriptor, or one declared
    7138              :    CONTIGUOUS, needs it packed into contiguous storage.  */
    7139              : 
    7140              : static bool
    7141          157 : dummy_accepts_strided_arg (gfc_symbol *fsym, bool nodesc_arg)
    7142              : {
    7143          126 :   return fsym && !nodesc_arg && !fsym->attr.contiguous && fsym->as
    7144          247 :          && (fsym->as->type == AS_ASSUMED_SHAPE
    7145            0 :              || fsym->as->type == AS_ASSUMED_RANK
    7146            0 :              || fsym->as->type == AS_DEFERRED);
    7147              : }
    7148              : 
    7149              : 
    7150              : /* Return true if the actual argument expr for the dummy fsym may be passed as
    7151              :    a copy-in/copy-out temporary.  A pointer associated with a TARGET or POINTER
    7152              :    dummy must remain valid after the call, so the actual argument is passed
    7153              :    directly, with a descriptor whose span provides the element spacing.  An
    7154              :    actual argument with a vector subscript is not definable and its pointer
    7155              :    association is undefined on return, so it is still copied.  */
    7156              : 
    7157              : static bool
    7158         1397 : copy_in_out_allowed (gfc_symbol *fsym, gfc_expr *e, bool nodesc_arg)
    7159              : {
    7160         1397 :   if (fsym == NULL || nodesc_arg || gfc_has_vector_subscript (e))
    7161              :     return true;
    7162              : 
    7163         1259 :   if (gfc_dummy_requires_direct_arg (fsym))
    7164              :     return false;
    7165              : 
    7166          971 :   return !(fsym->attr.pointer && !fsym->attr.contiguous && fsym->as
    7167            6 :            && (fsym->as->type == AS_ASSUMED_SHAPE
    7168              :                || fsym->as->type == AS_ASSUMED_RANK
    7169              :                || fsym->as->type == AS_DEFERRED));
    7170              : }
    7171              : 
    7172              : 
    7173              : /* Return true if E is a reference to a function returning a pointer array,
    7174              :    whose elements may be spaced by more than the element size.  */
    7175              : 
    7176              : static bool
    7177        53466 : pointer_array_result_p (gfc_expr *e)
    7178              : {
    7179        53466 :   return e->expr_type == EXPR_FUNCTION
    7180         4058 :          && e->rank > 0
    7181         4058 :          && e->ts.type != BT_CLASS
    7182        57511 :          && gfc_expr_attr (e).pointer;
    7183              : }
    7184              : 
    7185              : 
    7186              : /* Evaluate the pointer array function reference E into a temporary pointer
    7187              :    in BLOCK and return a variable expression for that pointer.  */
    7188              : 
    7189              : static gfc_expr *
    7190          145 : pointer_result_var (stmtblock_t *block, gfc_expr *e)
    7191              : {
    7192          145 :   gfc_symtree *st = gfc_get_unique_symtree (NULL);
    7193          145 :   gfc_symbol *sym = gfc_new_symbol (st->name, NULL);
    7194          145 :   st->n.sym = sym;
    7195          145 :   sym->ts = e->ts;
    7196          145 :   sym->attr.flavor = FL_VARIABLE;
    7197          145 :   sym->attr.referenced = 1;
    7198          145 :   sym->attr.dimension = 1;
    7199          145 :   sym->attr.pointer = 1;
    7200          145 :   sym->as = gfc_get_array_spec ();
    7201          145 :   sym->as->rank = e->rank;
    7202          145 :   sym->as->type = AS_DEFERRED;
    7203              : 
    7204          145 :   gfc_se se;
    7205          145 :   gfc_init_se (&se, NULL);
    7206          145 :   gfc_conv_expr_descriptor (&se, e);
    7207          145 :   gfc_add_block_to_block (block, &se.pre);
    7208          145 :   gfc_add_block_to_block (block, &se.post);
    7209              : 
    7210          145 :   gcc_assert (VAR_P (se.expr) && GFC_DECL_PTR_ARRAY_P (se.expr));
    7211          145 :   sym->backend_decl = se.expr;
    7212              : 
    7213          145 :   if (sym->ts.type == BT_CHARACTER)
    7214              :     {
    7215           36 :       sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    7216           36 :       sym->ts.u.cl->backend_decl = gfc_evaluate_now (se.string_length, block);
    7217              :     }
    7218              : 
    7219          145 :   gfc_expr *var = gfc_get_variable_expr (st);
    7220          145 :   var->where = e->where;
    7221          145 :   return var;
    7222              : }
    7223              : 
    7224              : 
    7225              : /* Generate code for a procedure call.  Note can return se->post != NULL.
    7226              :    If se->direct_byref is set then se->expr contains the return parameter.
    7227              :    Return nonzero, if the call has alternate specifiers.
    7228              :    'expr' is only needed for procedure pointer components.  */
    7229              : 
    7230              : int
    7231       139905 : gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
    7232              :                          gfc_actual_arglist * args, gfc_expr * expr,
    7233              :                          vec<tree, va_gc> *append_args)
    7234              : {
    7235       139905 :   gfc_interface_mapping mapping;
    7236       139905 :   vec<tree, va_gc> *arglist;
    7237       139905 :   vec<tree, va_gc> *retargs;
    7238       139905 :   tree tmp;
    7239       139905 :   tree fntype;
    7240       139905 :   gfc_se parmse;
    7241       139905 :   gfc_array_info *info;
    7242       139905 :   int byref;
    7243       139905 :   int parm_kind;
    7244       139905 :   tree type;
    7245       139905 :   tree var;
    7246       139905 :   tree len;
    7247       139905 :   tree base_object;
    7248       139905 :   vec<tree, va_gc> *stringargs;
    7249       139905 :   vec<tree, va_gc> *optionalargs;
    7250       139905 :   tree result = NULL;
    7251       139905 :   gfc_formal_arglist *formal;
    7252       139905 :   gfc_actual_arglist *arg;
    7253       139905 :   int has_alternate_specifier = 0;
    7254       139905 :   bool need_interface_mapping;
    7255       139905 :   bool is_builtin;
    7256       139905 :   bool callee_alloc;
    7257       139905 :   bool ulim_copy;
    7258       139905 :   gfc_typespec ts;
    7259       139905 :   gfc_charlen cl;
    7260       139905 :   gfc_expr *e;
    7261       139905 :   gfc_symbol *fsym;
    7262       139905 :   enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
    7263       139905 :   gfc_component *comp = NULL;
    7264       139905 :   int arglen;
    7265       139905 :   unsigned int argc;
    7266       139905 :   tree arg1_cntnr = NULL_TREE;
    7267       139905 :   bool call_needed_for_length = true;
    7268       139905 :   arglist = NULL;
    7269       139905 :   retargs = NULL;
    7270       139905 :   stringargs = NULL;
    7271       139905 :   optionalargs = NULL;
    7272       139905 :   var = NULL_TREE;
    7273       139905 :   len = NULL_TREE;
    7274       139905 :   gfc_clear_ts (&ts);
    7275       139905 :   gfc_intrinsic_sym *isym = expr && expr->rank ?
    7276              :                             expr->value.function.isym : NULL;
    7277              : 
    7278       139905 :   comp = gfc_get_proc_ptr_comp (expr);
    7279              : 
    7280       279810 :   bool elemental_proc = (comp
    7281         2109 :                          && comp->ts.interface
    7282         2055 :                          && comp->ts.interface->attr.elemental)
    7283         1904 :                         || (comp && comp->attr.elemental)
    7284       141809 :                         || sym->attr.elemental;
    7285              : 
    7286       139905 :   if (se->ss != NULL)
    7287              :     {
    7288        25415 :       if (!elemental_proc)
    7289              :         {
    7290        21838 :           gcc_assert (se->ss->info->type == GFC_SS_FUNCTION);
    7291        21838 :           if (se->ss->info->useflags)
    7292              :             {
    7293         5844 :               gcc_assert ((!comp && gfc_return_by_reference (sym)
    7294              :                            && sym->result->attr.dimension)
    7295              :                           || (comp && comp->attr.dimension)
    7296              :                           || gfc_is_class_array_function (expr));
    7297         5844 :               gcc_assert (se->loop != NULL);
    7298              :               /* Access the previously obtained result.  */
    7299         5844 :               gfc_conv_tmp_array_ref (se);
    7300         5844 :               return 0;
    7301              :             }
    7302              :         }
    7303        19571 :       info = &se->ss->info->data.array;
    7304              :     }
    7305              :   else
    7306              :     info = NULL;
    7307              : 
    7308       134061 :   stmtblock_t post, clobbers, dealloc_blk;
    7309       134061 :   gfc_init_block (&post);
    7310       134061 :   gfc_init_block (&clobbers);
    7311       134061 :   gfc_init_block (&dealloc_blk);
    7312       134061 :   gfc_init_interface_mapping (&mapping);
    7313       134061 :   if (!comp)
    7314              :     {
    7315       132001 :       formal = gfc_sym_get_dummy_args (sym);
    7316       248260 :       need_interface_mapping = sym->attr.dimension ||
    7317       116259 :                                (sym->ts.type == BT_CHARACTER
    7318         3234 :                                 && sym->ts.u.cl->length
    7319         2482 :                                 && sym->ts.u.cl->length->expr_type
    7320              :                                    != EXPR_CONSTANT)
    7321       246663 :                                || has_value_array_dummy (formal);
    7322              :     }
    7323              :   else
    7324              :     {
    7325         2060 :       formal = comp->ts.interface ? comp->ts.interface->formal : NULL;
    7326         4051 :       need_interface_mapping = comp->attr.dimension ||
    7327         1991 :                                (comp->ts.type == BT_CHARACTER
    7328          229 :                                 && comp->ts.u.cl->length
    7329          220 :                                 && comp->ts.u.cl->length->expr_type
    7330              :                                    != EXPR_CONSTANT)
    7331         4032 :                                || has_value_array_dummy (formal);
    7332              :     }
    7333              : 
    7334       134061 :   base_object = NULL_TREE;
    7335              :   /* For _vprt->_copy () routines no formal symbol is present.  Nevertheless
    7336              :      is the third and fourth argument to such a function call a value
    7337              :      denoting the number of elements to copy (i.e., most of the time the
    7338              :      length of a deferred length string).  */
    7339       268122 :   ulim_copy = (formal == NULL)
    7340        32719 :                && UNLIMITED_POLY (sym)
    7341       134153 :                && comp && (strcmp ("_copy", comp->name) == 0);
    7342              : 
    7343              :   /* Scan for allocatable actual arguments passed to allocatable dummy
    7344              :      arguments with INTENT(OUT).  As the corresponding actual arguments are
    7345              :      deallocated before execution of the procedure, we evaluate actual
    7346              :      argument expressions to avoid problems with possible dependencies.  */
    7347       134061 :   bool force_eval_args = false;
    7348       134061 :   gfc_formal_arglist *tmp_formal;
    7349       408810 :   for (arg = args, tmp_formal = formal; arg != NULL;
    7350       241394 :        arg = arg->next, tmp_formal = tmp_formal ? tmp_formal->next : NULL)
    7351              :     {
    7352       275308 :       e = arg->expr;
    7353       275308 :       fsym = tmp_formal ? tmp_formal->sym : NULL;
    7354       261653 :       if (e && fsym
    7355       229725 :           && e->expr_type == EXPR_VARIABLE
    7356       101581 :           && fsym->attr.intent == INTENT_OUT
    7357         6606 :           && (fsym->ts.type == BT_CLASS && fsym->attr.class_ok
    7358         6606 :               ? CLASS_DATA (fsym)->attr.allocatable
    7359         4910 :               : fsym->attr.allocatable)
    7360          559 :           && e->symtree
    7361          559 :           && e->symtree->n.sym
    7362       536961 :           && gfc_variable_attr (e).allocatable)
    7363              :         {
    7364              :           force_eval_args = true;
    7365              :           break;
    7366              :         }
    7367              :     }
    7368              : 
    7369              :   /* Evaluate the arguments.  */
    7370       409783 :   for (arg = args, argc = 0; arg != NULL;
    7371       275722 :        arg = arg->next, formal = formal ? formal->next : NULL, ++argc)
    7372              :     {
    7373       275722 :       bool finalized = false;
    7374       275722 :       tree derived_array = NULL_TREE;
    7375       275722 :       symbol_attribute *attr;
    7376              : 
    7377       275722 :       e = arg->expr;
    7378       275722 :       fsym = formal ? formal->sym : NULL;
    7379       518089 :       parm_kind = MISSING;
    7380              : 
    7381       242367 :       attr = fsym ? &(fsym->ts.type == BT_CLASS ? CLASS_DATA (fsym)->attr
    7382              :                                                 : fsym->attr)
    7383              :                   : nullptr;
    7384              :       /* If the procedure requires an explicit interface, the actual
    7385              :          argument is passed according to the corresponding formal
    7386              :          argument.  If the corresponding formal argument is a POINTER,
    7387              :          ALLOCATABLE or assumed shape, we do not use g77's calling
    7388              :          convention, and pass the address of the array descriptor
    7389              :          instead.  Otherwise we use g77's calling convention, in other words
    7390              :          pass the array data pointer without descriptor.  */
    7391       242314 :       bool nodesc_arg = fsym != NULL
    7392       242314 :                         && !(fsym->attr.pointer || fsym->attr.allocatable)
    7393       232870 :                         && fsym->as
    7394        41924 :                         && fsym->as->type != AS_ASSUMED_SHAPE
    7395        25017 :                         && fsym->as->type != AS_ASSUMED_RANK;
    7396       275722 :       if (comp)
    7397         2827 :         nodesc_arg = nodesc_arg || !comp->attr.always_explicit;
    7398              :       else
    7399       272895 :         nodesc_arg
    7400              :           = nodesc_arg
    7401       272895 :             || !(sym->attr.always_explicit || (attr && attr->codimension));
    7402              : 
    7403              :       /* Class array expressions are sometimes coming completely unadorned
    7404              :          with either arrayspec or _data component.  Correct that here.
    7405              :          OOP-TODO: Move this to the frontend.  */
    7406       275722 :       if (e && e->expr_type == EXPR_VARIABLE
    7407       115708 :             && !e->ref
    7408        52550 :             && e->ts.type == BT_CLASS
    7409         2711 :             && (CLASS_DATA (e)->attr.codimension
    7410         2711 :                 || CLASS_DATA (e)->attr.dimension))
    7411              :         {
    7412            0 :           gfc_typespec temp_ts = e->ts;
    7413            0 :           gfc_add_class_array_ref (e);
    7414            0 :           e->ts = temp_ts;
    7415              :         }
    7416              : 
    7417       275722 :       if (e == NULL
    7418       262061 :           || (e->expr_type == EXPR_NULL
    7419          763 :               && fsym
    7420          763 :               && fsym->attr.value
    7421           72 :               && fsym->attr.optional
    7422           72 :               && !fsym->attr.dimension
    7423           72 :               && fsym->ts.type != BT_CLASS))
    7424              :         {
    7425        13733 :           if (se->ignore_optional)
    7426              :             {
    7427              :               /* Some intrinsics have already been resolved to the correct
    7428              :                  parameters.  */
    7429          446 :               continue;
    7430              :             }
    7431        13535 :           else if (arg->label)
    7432              :             {
    7433          224 :               has_alternate_specifier = 1;
    7434          224 :               continue;
    7435              :             }
    7436              :           else
    7437              :             {
    7438        13311 :               gfc_init_se (&parmse, NULL);
    7439              : 
    7440              :               /* For scalar arguments with VALUE attribute which are passed by
    7441              :                  value, pass "0" and a hidden argument gives the optional
    7442              :                  status.  */
    7443        13311 :               if (fsym && fsym->attr.optional && fsym->attr.value
    7444          476 :                   && !fsym->attr.dimension && fsym->ts.type != BT_CLASS)
    7445              :                 {
    7446          440 :                   conv_dummy_value (&parmse, e, fsym, optionalargs);
    7447              :                 }
    7448              :               else
    7449              :                 {
    7450              :                   /* Pass a NULL pointer for an absent arg.  */
    7451        12871 :                   parmse.expr = null_pointer_node;
    7452              : 
    7453              :                   /* Is it an absent character dummy?  */
    7454        12871 :                   bool absent_char = false;
    7455        12871 :                   gfc_dummy_arg * const dummy_arg = arg->associated_dummy;
    7456              : 
    7457              :                   /* Fall back to inferred type only if no formal.  */
    7458        12871 :                   if (fsym)
    7459        11813 :                     absent_char = (fsym->ts.type == BT_CHARACTER);
    7460         1058 :                   else if (dummy_arg)
    7461         1058 :                     absent_char = (gfc_dummy_arg_get_typespec (*dummy_arg).type
    7462              :                                    == BT_CHARACTER);
    7463        12871 :                   if (absent_char)
    7464         1133 :                     parmse.string_length = build_int_cst (gfc_charlen_type_node,
    7465              :                                                           0);
    7466              :                 }
    7467              :             }
    7468              :         }
    7469       261989 :       else if (e->expr_type == EXPR_NULL
    7470          691 :                && fsym && attr && (attr->pointer || attr->allocatable)
    7471          534 :                && ((fsym->ts.type == BT_DERIVED
    7472          210 :                     && (e->ts.type == BT_UNKNOWN || e->ts.type == BT_DERIVED))
    7473          324 :                    || (fsym->attr.allocatable && fsym->ts.type != BT_CLASS
    7474           18 :                        && fsym->ts.type != BT_CHARACTER)))
    7475              :         {
    7476          228 :           gfc_init_se (&parmse, NULL);
    7477          228 :           gfc_conv_expr_reference (&parmse, e);
    7478          228 :           conv_null_actual (&parmse, e, fsym);
    7479              :         }
    7480       261761 :       else if (arg->expr->expr_type == EXPR_NULL
    7481          463 :                && fsym && !fsym->attr.pointer
    7482          163 :                && (fsym->ts.type != BT_CLASS
    7483            6 :                    || !CLASS_DATA (fsym)->attr.class_pointer))
    7484              :         {
    7485              :           /* Pass a NULL pointer to denote an absent arg.  */
    7486          163 :           gcc_assert (fsym->attr.optional && !fsym->attr.allocatable
    7487              :                       && (fsym->ts.type != BT_CLASS
    7488              :                           || !CLASS_DATA (fsym)->attr.allocatable));
    7489          163 :           gfc_init_se (&parmse, NULL);
    7490          163 :           parmse.expr = null_pointer_node;
    7491          163 :           if (fsym->ts.type == BT_CHARACTER)
    7492           42 :             parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
    7493              :         }
    7494       261598 :       else if (fsym && fsym->ts.type == BT_CLASS
    7495        11652 :                  && e->ts.type == BT_DERIVED)
    7496              :         {
    7497              :           /* The derived type needs to be converted to a temporary
    7498              :              CLASS object.  */
    7499         4827 :           gfc_init_se (&parmse, se);
    7500         4827 :           gfc_conv_derived_to_class (&parmse, e, fsym, NULL_TREE,
    7501         4827 :                                      fsym->attr.optional
    7502         1008 :                                        && e->expr_type == EXPR_VARIABLE
    7503         1008 :                                        && e->symtree->n.sym->attr.optional,
    7504         4827 :                                      CLASS_DATA (fsym)->attr.class_pointer
    7505         4634 :                                        || CLASS_DATA (fsym)->attr.allocatable,
    7506              :                                      sym->name, &derived_array);
    7507              :         }
    7508       224843 :       else if (UNLIMITED_POLY (fsym) && e->ts.type != BT_CLASS
    7509          954 :                && e->ts.type != BT_PROCEDURE
    7510          930 :                && (gfc_expr_attr (e).flavor != FL_PROCEDURE
    7511          930 :                    || gfc_expr_attr (e).proc != PROC_UNKNOWN))
    7512              :         {
    7513              :           /* The intrinsic type needs to be converted to a temporary
    7514              :              CLASS object for the unlimited polymorphic formal.  */
    7515          930 :           gfc_find_vtab (&e->ts);
    7516          930 :           gfc_init_se (&parmse, se);
    7517          930 :           gfc_conv_intrinsic_to_class (&parmse, e, fsym->ts);
    7518              : 
    7519              :         }
    7520       255841 :       else if (se->ss && se->ss->info->useflags)
    7521              :         {
    7522         5861 :           gfc_ss *ss;
    7523              : 
    7524         5861 :           ss = se->ss;
    7525              : 
    7526              :           /* An elemental function inside a scalarized loop.  */
    7527         5861 :           gfc_init_se (&parmse, se);
    7528         5861 :           parm_kind = ELEMENTAL;
    7529              : 
    7530              :           /* When no fsym is present, ulim_copy is set and this is a third or
    7531              :              fourth argument, use call-by-value instead of by reference to
    7532              :              hand the length properties to the copy routine (i.e., most of the
    7533              :              time this will be a call to a __copy_character_* routine where the
    7534              :              third and fourth arguments are the lengths of a deferred length
    7535              :              char array).  */
    7536         5861 :           if ((fsym && fsym->attr.value)
    7537         5627 :               || (ulim_copy && (argc == 2 || argc == 3)))
    7538          234 :             gfc_conv_expr (&parmse, e);
    7539         5627 :           else if (e->expr_type == EXPR_ARRAY)
    7540              :             {
    7541          306 :               gfc_conv_expr (&parmse, e);
    7542          306 :               if (e->ts.type != BT_CHARACTER)
    7543          263 :                 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
    7544              :             }
    7545              :           else
    7546         5321 :             gfc_conv_expr_reference (&parmse, e);
    7547              : 
    7548         5861 :           if (e->ts.type == BT_CHARACTER && !e->rank
    7549          174 :               && e->expr_type == EXPR_FUNCTION)
    7550           12 :             parmse.expr = build_fold_indirect_ref_loc (input_location,
    7551              :                                                        parmse.expr);
    7552              : 
    7553         5811 :           if (fsym && fsym->ts.type == BT_DERIVED
    7554         7495 :               && gfc_is_class_container_ref (e))
    7555              :             {
    7556           24 :               parmse.expr = gfc_class_data_get (parmse.expr);
    7557              : 
    7558           24 :               if (fsym->attr.optional && e->expr_type == EXPR_VARIABLE
    7559           24 :                   && e->symtree->n.sym->attr.optional)
    7560              :                 {
    7561            0 :                   tree cond = gfc_conv_expr_present (e->symtree->n.sym);
    7562            0 :                   parmse.expr = build3_loc (input_location, COND_EXPR,
    7563            0 :                                         TREE_TYPE (parmse.expr),
    7564              :                                         cond, parmse.expr,
    7565            0 :                                         fold_convert (TREE_TYPE (parmse.expr),
    7566              :                                                       null_pointer_node));
    7567              :                 }
    7568              :             }
    7569              : 
    7570              :           /* Scalar dummy arguments of intrinsic type or derived type with
    7571              :              VALUE attribute.  */
    7572         5861 :           if (fsym
    7573         5811 :               && fsym->attr.value
    7574          234 :               && fsym->ts.type != BT_CLASS)
    7575          234 :             conv_dummy_value (&parmse, e, fsym, optionalargs);
    7576              : 
    7577              :           /* If we are passing an absent array as optional dummy to an
    7578              :              elemental procedure, make sure that we pass NULL when the data
    7579              :              pointer is NULL.  We need this extra conditional because of
    7580              :              scalarization which passes arrays elements to the procedure,
    7581              :              ignoring the fact that the array can be absent/unallocated/...  */
    7582         5627 :           else if (ss->info->can_be_null_ref
    7583          421 :                    && ss->info->type != GFC_SS_REFERENCE)
    7584              :             {
    7585          199 :               tree descriptor_data;
    7586              : 
    7587          199 :               descriptor_data = ss->info->data.array.data;
    7588          199 :               tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    7589              :                                      descriptor_data,
    7590          199 :                                      fold_convert (TREE_TYPE (descriptor_data),
    7591              :                                                    null_pointer_node));
    7592          199 :               parmse.expr
    7593          398 :                 = fold_build3_loc (input_location, COND_EXPR,
    7594          199 :                                    TREE_TYPE (parmse.expr),
    7595              :                                    gfc_unlikely (tmp, PRED_FORTRAN_ABSENT_DUMMY),
    7596          199 :                                    fold_convert (TREE_TYPE (parmse.expr),
    7597              :                                                  null_pointer_node),
    7598              :                                    parmse.expr);
    7599              :             }
    7600              : 
    7601              :           /* The scalarizer does not repackage the reference to a class
    7602              :              array - instead it returns a pointer to the data element.  */
    7603         5861 :           if (fsym && fsym->ts.type == BT_CLASS && e->ts.type == BT_CLASS)
    7604          216 :             gfc_conv_class_to_class (&parmse, e, fsym->ts, true,
    7605          192 :                                      fsym->attr.intent != INTENT_IN
    7606              :                                      && (CLASS_DATA (fsym)->attr.class_pointer
    7607           24 :                                          || CLASS_DATA (fsym)->attr.allocatable),
    7608          192 :                                      fsym->attr.optional
    7609            0 :                                      && e->expr_type == EXPR_VARIABLE
    7610            0 :                                      && e->symtree->n.sym->attr.optional,
    7611          192 :                                      CLASS_DATA (fsym)->attr.class_pointer
    7612          192 :                                      || CLASS_DATA (fsym)->attr.allocatable);
    7613              :         }
    7614              :       else
    7615              :         {
    7616       249980 :           bool scalar;
    7617       249980 :           gfc_ss *argss;
    7618              : 
    7619       249980 :           gfc_init_se (&parmse, NULL);
    7620              : 
    7621              :           /* Check whether the expression is a scalar or not; we cannot use
    7622              :              e->rank as it can be nonzero for functions arguments.  */
    7623       249980 :           argss = gfc_walk_expr (e);
    7624       249980 :           scalar = argss == gfc_ss_terminator;
    7625       249980 :           if (!scalar)
    7626        61824 :             gfc_free_ss_chain (argss);
    7627              : 
    7628              :           /* Special handling for passing scalar polymorphic coarrays;
    7629              :              otherwise one passes "class->_data.data" instead of "&class".  */
    7630       249980 :           if (e->rank == 0 && e->ts.type == BT_CLASS
    7631         3695 :               && fsym && fsym->ts.type == BT_CLASS
    7632         3273 :               && CLASS_DATA (fsym)->attr.codimension
    7633           55 :               && !CLASS_DATA (fsym)->attr.dimension)
    7634              :             {
    7635           55 :               gfc_add_class_array_ref (e);
    7636           55 :               parmse.want_coarray = 1;
    7637           55 :               scalar = false;
    7638              :             }
    7639              : 
    7640              :           /* A scalar or transformational function.  */
    7641       249980 :           if (scalar)
    7642              :             {
    7643       188101 :               if (e->expr_type == EXPR_VARIABLE
    7644        56237 :                     && e->symtree->n.sym->attr.cray_pointee
    7645          390 :                     && fsym && fsym->attr.flavor == FL_PROCEDURE)
    7646              :                 {
    7647              :                     /* The Cray pointer needs to be converted to a pointer to
    7648              :                        a type given by the expression.  */
    7649            6 :                     gfc_conv_expr (&parmse, e);
    7650            6 :                     type = build_pointer_type (TREE_TYPE (parmse.expr));
    7651            6 :                     tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
    7652            6 :                     parmse.expr = convert (type, tmp);
    7653              :                 }
    7654              : 
    7655       188095 :               else if (sym->attr.is_bind_c && e && is_CFI_desc (fsym, NULL))
    7656              :                 /* Implement F2018, 18.3.6, list item (5), bullet point 2.  */
    7657          687 :                 gfc_conv_gfc_desc_to_cfi_desc (&parmse, e, fsym);
    7658              : 
    7659       187408 :               else if (fsym && fsym->attr.value && fsym->attr.dimension)
    7660              :                 /* Scalar actual argument sequence associated with a VALUE
    7661              :                    array dummy.  */
    7662           36 :                 conv_seq_assoc_value_arg (&parmse, e, fsym, &mapping);
    7663              : 
    7664       159117 :               else if (fsym && fsym->attr.value)
    7665              :                 {
    7666        22285 :                   if (fsym->ts.type == BT_CHARACTER
    7667          639 :                       && fsym->ts.is_c_interop
    7668          229 :                       && fsym->ns->proc_name != NULL
    7669          229 :                       && fsym->ns->proc_name->attr.is_bind_c)
    7670              :                     {
    7671          220 :                       parmse.expr = NULL;
    7672          220 :                       conv_scalar_char_value (fsym, &parmse, &e);
    7673          220 :                       if (parmse.expr == NULL)
    7674          166 :                         gfc_conv_expr (&parmse, e);
    7675              :                     }
    7676              :                   else
    7677              :                     {
    7678        22065 :                       gfc_conv_expr (&parmse, e);
    7679        22065 :                       conv_dummy_value (&parmse, e, fsym, optionalargs);
    7680              :                     }
    7681              :                 }
    7682              : 
    7683       165087 :               else if (arg->name && arg->name[0] == '%')
    7684              :                 /* Argument list functions %VAL, %LOC and %REF are signalled
    7685              :                    through arg->name.  */
    7686         5846 :                 conv_arglist_function (&parmse, arg->expr, arg->name);
    7687       159241 :               else if ((e->expr_type == EXPR_FUNCTION)
    7688         8305 :                         && ((e->value.function.esym
    7689         2154 :                              && e->value.function.esym->result->attr.pointer)
    7690         8210 :                             || (!e->value.function.esym
    7691         6151 :                                 && e->symtree->n.sym->attr.pointer))
    7692           95 :                         && fsym && fsym->attr.target)
    7693              :                 /* Make sure the function only gets called once.  */
    7694            8 :                 gfc_conv_expr_reference (&parmse, e);
    7695       159233 :               else if (e->expr_type == EXPR_FUNCTION
    7696         8297 :                        && e->symtree->n.sym->result
    7697         7262 :                        && e->symtree->n.sym->result != e->symtree->n.sym
    7698          138 :                        && e->symtree->n.sym->result->attr.proc_pointer)
    7699              :                 {
    7700              :                   /* Functions returning procedure pointers.  */
    7701           18 :                   gfc_conv_expr (&parmse, e);
    7702           18 :                   if (fsym && fsym->attr.proc_pointer)
    7703            6 :                     parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
    7704              :                 }
    7705              : 
    7706              :               else
    7707              :                 {
    7708       159215 :                   bool defer_to_dealloc_blk = false;
    7709       159215 :                   if (e->ts.type == BT_CLASS && fsym
    7710         3628 :                       && fsym->ts.type == BT_CLASS
    7711         3206 :                       && (!CLASS_DATA (fsym)->as
    7712          356 :                           || CLASS_DATA (fsym)->as->type != AS_ASSUMED_RANK)
    7713         2850 :                       && CLASS_DATA (e)->attr.codimension)
    7714              :                     {
    7715           48 :                       gcc_assert (!CLASS_DATA (fsym)->attr.codimension);
    7716           48 :                       gcc_assert (!CLASS_DATA (fsym)->as);
    7717           48 :                       gfc_add_class_array_ref (e);
    7718           48 :                       parmse.want_coarray = 1;
    7719           48 :                       gfc_conv_expr_reference (&parmse, e);
    7720           48 :                       class_scalar_coarray_to_class (&parmse, e, fsym->ts,
    7721           48 :                                      fsym->attr.optional
    7722           48 :                                      && e->expr_type == EXPR_VARIABLE);
    7723              :                     }
    7724       159167 :                   else if (e->ts.type == BT_CLASS && fsym
    7725         3580 :                            && fsym->ts.type == BT_CLASS
    7726         3158 :                            && !CLASS_DATA (fsym)->as
    7727         2802 :                            && !CLASS_DATA (e)->as
    7728         2674 :                            && strcmp (fsym->ts.u.derived->name,
    7729              :                                       e->ts.u.derived->name))
    7730              :                     {
    7731         1679 :                       type = gfc_typenode_for_spec (&fsym->ts);
    7732         1679 :                       var = gfc_create_var (type, fsym->name);
    7733         1679 :                       gfc_conv_expr (&parmse, e);
    7734         1679 :                       if (fsym->attr.optional
    7735          153 :                           && e->expr_type == EXPR_VARIABLE
    7736          153 :                           && e->symtree->n.sym->attr.optional)
    7737              :                         {
    7738           66 :                           stmtblock_t block;
    7739           66 :                           tree cond;
    7740           66 :                           tmp = gfc_build_addr_expr (NULL_TREE, parmse.expr);
    7741           66 :                           cond = fold_build2_loc (input_location, NE_EXPR,
    7742              :                                                   logical_type_node, tmp,
    7743           66 :                                                   fold_convert (TREE_TYPE (tmp),
    7744              :                                                             null_pointer_node));
    7745           66 :                           gfc_start_block (&block);
    7746           66 :                           gfc_add_modify (&block, var,
    7747              :                                           fold_build1_loc (input_location,
    7748              :                                                            VIEW_CONVERT_EXPR,
    7749              :                                                            type, parmse.expr));
    7750           66 :                           gfc_add_expr_to_block (&parmse.pre,
    7751              :                                  fold_build3_loc (input_location,
    7752              :                                          COND_EXPR, void_type_node,
    7753              :                                          cond, gfc_finish_block (&block),
    7754              :                                          build_empty_stmt (input_location)));
    7755           66 :                           parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
    7756          132 :                           parmse.expr = build3_loc (input_location, COND_EXPR,
    7757           66 :                                          TREE_TYPE (parmse.expr),
    7758              :                                          cond, parmse.expr,
    7759           66 :                                          fold_convert (TREE_TYPE (parmse.expr),
    7760              :                                                        null_pointer_node));
    7761           66 :                         }
    7762              :                       else
    7763              :                         {
    7764              :                           /* Since the internal representation of unlimited
    7765              :                              polymorphic expressions includes an extra field
    7766              :                              that other class objects do not, a cast to the
    7767              :                              formal type does not work.  */
    7768         1613 :                           if (!UNLIMITED_POLY (e) && UNLIMITED_POLY (fsym))
    7769              :                             {
    7770           91 :                               tree efield;
    7771              : 
    7772              :                               /* Evaluate arguments just once, when they have
    7773              :                                  side effects.  */
    7774           91 :                               if (TREE_SIDE_EFFECTS (parmse.expr))
    7775              :                                 {
    7776           25 :                                   tree cldata, zero;
    7777              : 
    7778           25 :                                   parmse.expr = gfc_evaluate_now (parmse.expr,
    7779              :                                                                   &parmse.pre);
    7780              : 
    7781              :                                   /* Prevent memory leak, when old component
    7782              :                                      was allocated already.  */
    7783           25 :                                   cldata = gfc_class_data_get (parmse.expr);
    7784           25 :                                   zero = build_int_cst (TREE_TYPE (cldata),
    7785              :                                                         0);
    7786           25 :                                   tmp = fold_build2_loc (input_location, NE_EXPR,
    7787              :                                                          logical_type_node,
    7788              :                                                          cldata, zero);
    7789           25 :                                   tmp = build3_v (COND_EXPR, tmp,
    7790              :                                                   gfc_call_free (cldata),
    7791              :                                                   build_empty_stmt (
    7792              :                                                     input_location));
    7793           25 :                                   gfc_add_expr_to_block (&parmse.finalblock,
    7794              :                                                          tmp);
    7795           25 :                                   gfc_add_modify (&parmse.finalblock,
    7796              :                                                   cldata, zero);
    7797              :                                 }
    7798              : 
    7799              :                               /* Set the _data field.  */
    7800           91 :                               tmp = gfc_class_data_get (var);
    7801           91 :                               efield = fold_convert (TREE_TYPE (tmp),
    7802              :                                         gfc_class_data_get (parmse.expr));
    7803           91 :                               gfc_add_modify (&parmse.pre, tmp, efield);
    7804              : 
    7805              :                               /* Set the _vptr field.  */
    7806           91 :                               tmp = gfc_class_vptr_get (var);
    7807           91 :                               efield = fold_convert (TREE_TYPE (tmp),
    7808              :                                         gfc_class_vptr_get (parmse.expr));
    7809           91 :                               gfc_add_modify (&parmse.pre, tmp, efield);
    7810              : 
    7811              :                               /* Set the _len field.  */
    7812           91 :                               tmp = gfc_class_len_get (var);
    7813           91 :                               gfc_add_modify (&parmse.pre, tmp,
    7814           91 :                                               build_int_cst (TREE_TYPE (tmp), 0));
    7815           91 :                             }
    7816              :                           else
    7817              :                             {
    7818         1522 :                               tmp = fold_build1_loc (input_location,
    7819              :                                                      VIEW_CONVERT_EXPR,
    7820              :                                                      type, parmse.expr);
    7821         1522 :                               gfc_add_modify (&parmse.pre, var, tmp);
    7822         1613 :                                               ;
    7823              :                             }
    7824         1613 :                           parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
    7825              :                         }
    7826              :                     }
    7827              :                   else
    7828              :                     {
    7829       157488 :                       gfc_conv_expr_reference (&parmse, e);
    7830              : 
    7831       157488 :                       gfc_symbol *dsym = fsym;
    7832       157488 :                       gfc_dummy_arg *dummy;
    7833              : 
    7834              :                       /* Use associated dummy as fallback for formal
    7835              :                          argument if there is no explicit interface.  */
    7836       157488 :                       if (dsym == NULL
    7837        27443 :                           && (dummy = arg->associated_dummy)
    7838        24901 :                           && dummy->intrinsicness == GFC_NON_INTRINSIC_DUMMY_ARG
    7839       180982 :                           && dummy->u.non_intrinsic->sym)
    7840       157488 :                         dsym = dummy->u.non_intrinsic->sym;
    7841              : 
    7842       157488 :                       if (dsym
    7843       153539 :                           && dsym->attr.intent == INTENT_OUT
    7844         3357 :                           && !dsym->attr.allocatable
    7845         3208 :                           && !dsym->attr.pointer
    7846         3190 :                           && e->expr_type == EXPR_VARIABLE
    7847         3189 :                           && e->ref == NULL
    7848         3074 :                           && e->symtree
    7849         3074 :                           && e->symtree->n.sym
    7850         3074 :                           && !e->symtree->n.sym->attr.dimension
    7851         3074 :                           && e->ts.type != BT_CHARACTER
    7852         2972 :                           && e->ts.type != BT_CLASS
    7853         2724 :                           && (e->ts.type != BT_DERIVED
    7854          516 :                               || (dsym->ts.type == BT_DERIVED
    7855          516 :                                   && e->ts.u.derived == dsym->ts.u.derived
    7856              :                                   /* Types with allocatable components are
    7857              :                                      excluded from clobbering because we need
    7858              :                                      the unclobbered pointers to free the
    7859              :                                      allocatable components in the callee.
    7860              :                                      Same goes for finalizable types or types
    7861              :                                      with finalizable components, we need to
    7862              :                                      pass the unclobbered values to the
    7863              :                                      finalization routines.
    7864              :                                      For parameterized types, it's less clear
    7865              :                                      but they may not have a constant size
    7866              :                                      so better exclude them in any case.  */
    7867          501 :                                   && !e->ts.u.derived->attr.alloc_comp
    7868          375 :                                   && !e->ts.u.derived->attr.pdt_type
    7869          351 :                                   && !gfc_is_finalizable (e->ts.u.derived, NULL)))
    7870         2517 :                           && e->ts.type != BT_PROCEDURE
    7871       159969 :                           && !sym->attr.elemental)
    7872              :                         {
    7873         1148 :                           tree var;
    7874         1148 :                           var = build_fold_indirect_ref_loc (input_location,
    7875              :                                                              parmse.expr);
    7876         1148 :                           tree clobber = build_clobber (TREE_TYPE (var));
    7877         1148 :                           gfc_add_modify (&clobbers, var, clobber);
    7878              :                         }
    7879              :                     }
    7880              :                   /* Catch base objects that are not variables.  */
    7881       159215 :                   if (e->ts.type == BT_CLASS
    7882         3628 :                         && e->expr_type != EXPR_VARIABLE
    7883          306 :                         && expr && e == expr->base_expr)
    7884           80 :                     base_object = build_fold_indirect_ref_loc (input_location,
    7885              :                                                                parmse.expr);
    7886              : 
    7887              :                   /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
    7888              :                      allocated on entry, it must be deallocated.  */
    7889       131772 :                   if (fsym && fsym->attr.intent == INTENT_OUT
    7890         3286 :                       && (fsym->attr.allocatable
    7891         3137 :                           || (fsym->ts.type == BT_CLASS
    7892          277 :                               && CLASS_DATA (fsym)->attr.allocatable))
    7893       159531 :                       && !is_CFI_desc (fsym, NULL))
    7894              :                     {
    7895          316 :                       stmtblock_t block;
    7896          316 :                       tree ptr;
    7897              : 
    7898          316 :                       defer_to_dealloc_blk = true;
    7899              : 
    7900          316 :                       parmse.expr = gfc_evaluate_data_ref_now (parmse.expr,
    7901              :                                                                &parmse.pre);
    7902              : 
    7903          316 :                       if (parmse.class_container != NULL_TREE)
    7904          174 :                         parmse.class_container
    7905          174 :                             = gfc_evaluate_data_ref_now (parmse.class_container,
    7906              :                                                          &parmse.pre);
    7907              : 
    7908          316 :                       gfc_init_block  (&block);
    7909          316 :                       ptr = parmse.expr;
    7910          316 :                       if (e->ts.type == BT_CLASS)
    7911          174 :                         ptr = gfc_class_data_get (ptr);
    7912              : 
    7913          316 :                       tree cls = parmse.class_container;
    7914          316 :                       tmp = gfc_deallocate_scalar_with_status (ptr, NULL_TREE,
    7915              :                                                                NULL_TREE, true,
    7916              :                                                                e, e->ts, cls);
    7917          316 :                       gfc_add_expr_to_block (&block, tmp);
    7918          316 :                       gfc_add_modify (&block, ptr,
    7919          316 :                                       fold_convert (TREE_TYPE (ptr),
    7920              :                                                     null_pointer_node));
    7921              : 
    7922          316 :                       if (fsym->ts.type == BT_CLASS)
    7923          167 :                         gfc_reset_vptr (&block, nullptr,
    7924              :                                         build_fold_indirect_ref (parmse.expr),
    7925          167 :                                         fsym->ts.u.derived);
    7926              : 
    7927          316 :                       if (fsym->attr.optional
    7928           42 :                           && e->expr_type == EXPR_VARIABLE
    7929           42 :                           && e->symtree->n.sym->attr.optional)
    7930              :                         {
    7931           36 :                           tmp = fold_build3_loc (input_location, COND_EXPR,
    7932              :                                      void_type_node,
    7933           18 :                                      gfc_conv_expr_present (e->symtree->n.sym),
    7934              :                                             gfc_finish_block (&block),
    7935              :                                             build_empty_stmt (input_location));
    7936              :                         }
    7937              :                       else
    7938          298 :                         tmp = gfc_finish_block (&block);
    7939              : 
    7940          316 :                       gfc_add_expr_to_block (&dealloc_blk, tmp);
    7941              :                     }
    7942              : 
    7943              :                   /* A class array element needs converting back to be a
    7944              :                      class object, if the formal argument is a class object.  */
    7945       159215 :                   if (fsym && fsym->ts.type == BT_CLASS
    7946         3230 :                         && e->ts.type == BT_CLASS
    7947         3206 :                         && ((CLASS_DATA (fsym)->as
    7948          356 :                              && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
    7949         2850 :                             || CLASS_DATA (e)->attr.dimension))
    7950              :                     {
    7951          484 :                       gfc_se class_se = parmse;
    7952          484 :                       gfc_init_block (&class_se.pre);
    7953          484 :                       gfc_init_block (&class_se.post);
    7954              : 
    7955          751 :                       gfc_conv_class_to_class (&class_se, e, fsym->ts, false,
    7956          484 :                                      fsym->attr.intent != INTENT_IN
    7957              :                                      && (CLASS_DATA (fsym)->attr.class_pointer
    7958          267 :                                          || CLASS_DATA (fsym)->attr.allocatable),
    7959          484 :                                      fsym->attr.optional
    7960          198 :                                      && e->expr_type == EXPR_VARIABLE
    7961          198 :                                      && e->symtree->n.sym->attr.optional,
    7962          484 :                                      CLASS_DATA (fsym)->attr.class_pointer
    7963          448 :                                      || CLASS_DATA (fsym)->attr.allocatable);
    7964              : 
    7965          484 :                       parmse.expr = class_se.expr;
    7966          460 :                       stmtblock_t *class_pre_block = defer_to_dealloc_blk
    7967          484 :                                                      ? &dealloc_blk
    7968              :                                                      : &parmse.pre;
    7969          484 :                       gfc_add_block_to_block (class_pre_block, &class_se.pre);
    7970          484 :                       gfc_add_block_to_block (&parmse.post, &class_se.post);
    7971              :                     }
    7972              : 
    7973       131772 :                   if (fsym && (fsym->ts.type == BT_DERIVED
    7974       119694 :                                || fsym->ts.type == BT_ASSUMED)
    7975        12945 :                       && e->ts.type == BT_CLASS
    7976          410 :                       && !CLASS_DATA (e)->attr.dimension
    7977          374 :                       && !CLASS_DATA (e)->attr.codimension)
    7978              :                     {
    7979          374 :                       parmse.expr = gfc_class_data_get (parmse.expr);
    7980              :                       /* The result is a class temporary, whose _data component
    7981              :                          must be freed to avoid a memory leak.  */
    7982          374 :                       if (e->expr_type == EXPR_FUNCTION
    7983           23 :                           && CLASS_DATA (e)->attr.allocatable)
    7984              :                         {
    7985           19 :                           tree zero;
    7986              : 
    7987              :                           /* Finalize the expression.  */
    7988           19 :                           gfc_finalize_tree_expr (&parmse, NULL,
    7989           19 :                                                   gfc_expr_attr (e), e->rank);
    7990           19 :                           gfc_add_block_to_block (&parmse.post,
    7991              :                                                   &parmse.finalblock);
    7992              : 
    7993              :                           /* Then free the class _data.  */
    7994           19 :                           zero = build_int_cst (TREE_TYPE (parmse.expr), 0);
    7995           19 :                           tmp = fold_build2_loc (input_location, NE_EXPR,
    7996              :                                                  logical_type_node,
    7997              :                                                  parmse.expr, zero);
    7998           19 :                           tmp = build3_v (COND_EXPR, tmp,
    7999              :                                           gfc_call_free (parmse.expr),
    8000              :                                           build_empty_stmt (input_location));
    8001           19 :                           gfc_add_expr_to_block (&parmse.post, tmp);
    8002           19 :                           gfc_add_modify (&parmse.post, parmse.expr, zero);
    8003              :                         }
    8004              :                     }
    8005              : 
    8006              :                   /* Wrap scalar variable in a descriptor. We need to convert
    8007              :                      the address of a pointer back to the pointer itself before,
    8008              :                      we can assign it to the data field.  */
    8009              : 
    8010       131772 :                   if (fsym && fsym->as && fsym->as->type == AS_ASSUMED_RANK
    8011         1344 :                       && fsym->ts.type != BT_CLASS && e->expr_type != EXPR_NULL)
    8012              :                     {
    8013         1272 :                       tmp = parmse.expr;
    8014         1272 :                       if (TREE_CODE (tmp) == ADDR_EXPR)
    8015          754 :                         tmp = TREE_OPERAND (tmp, 0);
    8016         1272 :                       parmse.expr = gfc_conv_scalar_to_descriptor (&parmse, tmp,
    8017              :                                                                    fsym->attr);
    8018         1272 :                       parmse.expr = gfc_build_addr_expr (NULL_TREE,
    8019              :                                                          parmse.expr);
    8020              :                     }
    8021       130500 :                   else if (fsym && e->expr_type != EXPR_NULL
    8022       130202 :                       && ((fsym->attr.pointer
    8023         1740 :                            && fsym->attr.flavor != FL_PROCEDURE)
    8024       128468 :                           || (fsym->attr.proc_pointer
    8025          199 :                               && !(e->expr_type == EXPR_VARIABLE
    8026          199 :                                    && e->symtree->n.sym->attr.dummy))
    8027       128281 :                           || (fsym->attr.proc_pointer
    8028           12 :                               && e->expr_type == EXPR_VARIABLE
    8029           12 :                               && gfc_is_proc_ptr_comp (e))
    8030       128275 :                           || (fsym->attr.allocatable
    8031         1323 :                               && fsym->attr.flavor != FL_PROCEDURE)))
    8032              :                     {
    8033              :                       /* Scalar pointer dummy args require an extra level of
    8034              :                          indirection. The null pointer already contains
    8035              :                          this level of indirection.  */
    8036         3244 :                       parm_kind = SCALAR_POINTER;
    8037         3244 :                       parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
    8038              :                     }
    8039              :                 }
    8040              :             }
    8041        61879 :           else if (e->ts.type == BT_CLASS
    8042         2885 :                     && fsym && fsym->ts.type == BT_CLASS
    8043         2461 :                     && (CLASS_DATA (fsym)->attr.dimension
    8044           55 :                         || CLASS_DATA (fsym)->attr.codimension))
    8045              :             {
    8046              :               /* Pass a class array.  */
    8047         2461 :               gfc_conv_expr_descriptor (&parmse, e);
    8048         2461 :               bool defer_to_dealloc_blk = false;
    8049              : 
    8050         2461 :               if (fsym->attr.optional
    8051          798 :                   && e->expr_type == EXPR_VARIABLE
    8052          798 :                   && e->symtree->n.sym->attr.optional)
    8053              :                 {
    8054          438 :                   stmtblock_t block;
    8055              : 
    8056          438 :                   gfc_init_block (&block);
    8057          438 :                   gfc_add_block_to_block (&block, &parmse.pre);
    8058              : 
    8059          876 :                   tree t = fold_build3_loc (input_location, COND_EXPR,
    8060              :                              void_type_node,
    8061          438 :                              gfc_conv_expr_present (e->symtree->n.sym),
    8062              :                                     gfc_finish_block (&block),
    8063              :                                     build_empty_stmt (input_location));
    8064              : 
    8065          438 :                   gfc_add_expr_to_block (&parmse.pre, t);
    8066              :                 }
    8067              : 
    8068              :               /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
    8069              :                  allocated on entry, it must be deallocated.  */
    8070         2461 :               if (fsym->attr.intent == INTENT_OUT
    8071          165 :                   && CLASS_DATA (fsym)->attr.allocatable)
    8072              :                 {
    8073          134 :                   stmtblock_t block;
    8074          134 :                   tree ptr;
    8075              : 
    8076              :                   /* In case the data reference to deallocate is dependent on
    8077              :                      its own content, save the resulting pointer to a variable
    8078              :                      and only use that variable from now on, before the
    8079              :                      expression becomes invalid.  */
    8080          134 :                   parmse.expr = gfc_evaluate_data_ref_now (parmse.expr,
    8081              :                                                            &parmse.pre);
    8082              : 
    8083          134 :                   if (parmse.class_container != NULL_TREE)
    8084          134 :                     parmse.class_container
    8085          134 :                         = gfc_evaluate_data_ref_now (parmse.class_container,
    8086              :                                                      &parmse.pre);
    8087              : 
    8088          134 :                   gfc_init_block  (&block);
    8089          134 :                   ptr = parmse.expr;
    8090          134 :                   ptr = gfc_class_data_get (ptr);
    8091              : 
    8092          134 :                   tree cls = parmse.class_container;
    8093          134 :                   tmp = gfc_deallocate_with_status (ptr, NULL_TREE,
    8094              :                                                     NULL_TREE, NULL_TREE,
    8095              :                                                     NULL_TREE, true, e,
    8096              :                                                     GFC_CAF_COARRAY_NOCOARRAY,
    8097              :                                                     cls);
    8098          134 :                   gfc_add_expr_to_block (&block, tmp);
    8099          134 :                   tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    8100              :                                          void_type_node, ptr,
    8101              :                                          null_pointer_node);
    8102          134 :                   gfc_add_expr_to_block (&block, tmp);
    8103          134 :                   gfc_reset_vptr (&block, e, parmse.class_container);
    8104              : 
    8105          134 :                   if (fsym->attr.optional
    8106           30 :                       && e->expr_type == EXPR_VARIABLE
    8107           30 :                       && (!e->ref
    8108           30 :                           || (e->ref->type == REF_ARRAY
    8109            0 :                               && e->ref->u.ar.type != AR_FULL))
    8110            0 :                       && e->symtree->n.sym->attr.optional)
    8111              :                     {
    8112            0 :                       tmp = fold_build3_loc (input_location, COND_EXPR,
    8113              :                                     void_type_node,
    8114            0 :                                     gfc_conv_expr_present (e->symtree->n.sym),
    8115              :                                     gfc_finish_block (&block),
    8116              :                                     build_empty_stmt (input_location));
    8117              :                     }
    8118              :                   else
    8119          134 :                     tmp = gfc_finish_block (&block);
    8120              : 
    8121          134 :                   gfc_add_expr_to_block (&dealloc_blk, tmp);
    8122          134 :                   defer_to_dealloc_blk = true;
    8123              :                 }
    8124              : 
    8125         2461 :               gfc_se class_se = parmse;
    8126         2461 :               gfc_init_block (&class_se.pre);
    8127         2461 :               gfc_init_block (&class_se.post);
    8128              : 
    8129         2461 :               if (e->expr_type != EXPR_VARIABLE)
    8130              :                 {
    8131              :                   int n;
    8132              :                   /* Set the bounds and offset correctly.  */
    8133           84 :                   for (n = 0; n < e->rank; n++)
    8134           42 :                     gfc_conv_shift_descriptor_lbound (&class_se.pre,
    8135              :                                                       class_se.expr,
    8136              :                                                       n, gfc_index_one_node);
    8137              :                 }
    8138              : 
    8139              :               /* The conversion does not repackage the reference to a class
    8140              :                  array - _data descriptor.  */
    8141         3900 :               gfc_conv_class_to_class (&class_se, e, fsym->ts, false,
    8142         2461 :                                      fsym->attr.intent != INTENT_IN
    8143              :                                      && (CLASS_DATA (fsym)->attr.class_pointer
    8144         1253 :                                          || CLASS_DATA (fsym)->attr.allocatable),
    8145         2461 :                                      fsym->attr.optional
    8146          798 :                                      && e->expr_type == EXPR_VARIABLE
    8147          798 :                                      && e->symtree->n.sym->attr.optional,
    8148         2461 :                                      CLASS_DATA (fsym)->attr.class_pointer
    8149         2035 :                                      || CLASS_DATA (fsym)->attr.allocatable);
    8150              : 
    8151         2461 :               parmse.expr = class_se.expr;
    8152         2327 :               stmtblock_t *class_pre_block = defer_to_dealloc_blk
    8153         2461 :                                              ? &dealloc_blk
    8154              :                                              : &parmse.pre;
    8155         2461 :               gfc_add_block_to_block (class_pre_block, &class_se.pre);
    8156         2461 :               gfc_add_block_to_block (&parmse.post, &class_se.post);
    8157              : 
    8158         2461 :               if (e->expr_type == EXPR_OP
    8159           24 :                   && POINTER_TYPE_P (TREE_TYPE (parmse.expr))
    8160         2485 :                   && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (parmse.expr, 0))))
    8161              :                 {
    8162           24 :                   tree cond;
    8163           24 :                   tree dealloc_expr = gfc_finish_block (&parmse.post);
    8164           24 :                   tmp = TREE_OPERAND (parmse.expr, 0);
    8165           24 :                   gfc_init_block (&parmse.post);
    8166           24 :                   cond = gfc_class_data_get (tmp);
    8167           24 :                   tmp = gfc_deallocate_alloc_comp_no_caf (e->ts.u.derived,
    8168              :                                                           tmp, e->rank, true);
    8169           24 :                   gfc_add_expr_to_block (&parmse.post, tmp);
    8170           24 :                   cond = gfc_class_data_get (TREE_OPERAND (parmse.expr, 0));
    8171           24 :                   cond = gfc_conv_descriptor_data_get (cond);
    8172           24 :                   cond = fold_build2_loc (input_location, NE_EXPR,
    8173              :                                           logical_type_node, cond,
    8174           24 :                                           build_int_cst (TREE_TYPE (cond), 0));
    8175           24 :                   tmp = build3_v (COND_EXPR, cond, dealloc_expr,
    8176              :                                   build_empty_stmt (input_location));
    8177              : 
    8178              :                   /* This specific case should not be processed further and so
    8179              :                      bundle everything up and proceed to the next argument.  */
    8180           24 :                   if (fsym && need_interface_mapping && e)
    8181           12 :                     gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
    8182           24 :                   gfc_add_expr_to_block (&parmse.post, tmp);
    8183           24 :                   gfc_add_block_to_block (&se->pre, &parmse.pre);
    8184           24 :                   gfc_add_block_to_block (&post, &parmse.post);
    8185           24 :                   gfc_add_block_to_block (&se->finalblock, &parmse.finalblock);
    8186           24 :                   vec_safe_push (arglist, parmse.expr);
    8187           24 :                   continue;
    8188           24 :                 }
    8189         2437 :             }
    8190              :           else
    8191              :             {
    8192              :               /* If the argument is a function call that may not create
    8193              :                  a temporary for the result, we have to check that we
    8194              :                  can do it, i.e. that there is no alias between this
    8195              :                  argument and another one.  */
    8196        59418 :               if (gfc_get_noncopying_intrinsic_argument (e) != NULL)
    8197              :                 {
    8198          406 :                   gfc_expr *iarg;
    8199          406 :                   sym_intent intent;
    8200              : 
    8201          406 :                   if (fsym != NULL)
    8202          397 :                     intent = fsym->attr.intent;
    8203              :                   else
    8204              :                     intent = INTENT_UNKNOWN;
    8205              : 
    8206          406 :                   if (gfc_check_fncall_dependency (e, intent, sym, args,
    8207              :                                                    NOT_ELEMENTAL))
    8208           21 :                     parmse.force_tmp = 1;
    8209              : 
    8210          406 :                   iarg = e->value.function.actual->expr;
    8211              : 
    8212              :                   /* Temporary needed if aliasing due to host association.  */
    8213          406 :                   if (sym->attr.contained
    8214          168 :                         && !sym->attr.pure
    8215          168 :                         && !sym->attr.implicit_pure
    8216           84 :                         && !sym->attr.use_assoc
    8217           84 :                         && iarg->expr_type == EXPR_VARIABLE
    8218           84 :                         && sym->ns == iarg->symtree->n.sym->ns)
    8219           36 :                     parmse.force_tmp = 1;
    8220              : 
    8221              :                   /* Ditto within module.  */
    8222          406 :                   if (sym->attr.use_assoc
    8223            6 :                         && !sym->attr.pure
    8224            6 :                         && !sym->attr.implicit_pure
    8225            0 :                         && iarg->expr_type == EXPR_VARIABLE
    8226            0 :                         && sym->module == iarg->symtree->n.sym->module)
    8227            0 :                     parmse.force_tmp = 1;
    8228              :                 }
    8229              : 
    8230              :               /* Special case for assumed-rank arrays: when passing an
    8231              :                  argument to a nonallocatable/nonpointer dummy, the bounds have
    8232              :                  to be reset as otherwise a last-dim ubound of -1 is
    8233              :                  indistinguishable from an assumed-size array in the callee.  */
    8234        59418 :               if (!sym->attr.is_bind_c && e && fsym && fsym->as
    8235        36267 :                   && fsym->as->type == AS_ASSUMED_RANK
    8236        12013 :                   && e->rank != -1
    8237        11692 :                   && e->expr_type == EXPR_VARIABLE
    8238        11227 :                   && ((fsym->ts.type == BT_CLASS
    8239            0 :                        && !CLASS_DATA (fsym)->attr.class_pointer
    8240            0 :                        && !CLASS_DATA (fsym)->attr.allocatable)
    8241        11227 :                       || (fsym->ts.type != BT_CLASS
    8242        11227 :                           && !fsym->attr.pointer && !fsym->attr.allocatable)))
    8243              :                 {
    8244              :                   /* Change AR_FULL to a (:,:,:) ref to force bounds update. */
    8245        10684 :                   gfc_ref *ref;
    8246        10948 :                   for (ref = e->ref; ref->next; ref = ref->next)
    8247              :                     {
    8248          342 :                       if (ref->next->type == REF_INQUIRY)
    8249              :                         break;
    8250          294 :                       if (ref->type == REF_ARRAY
    8251           30 :                           && ref->u.ar.type != AR_ELEMENT)
    8252              :                         break;
    8253        10684 :                     };
    8254        10684 :                   if (ref->u.ar.type == AR_FULL
    8255         9934 :                       && ref->u.ar.as->type != AS_ASSUMED_SIZE)
    8256         9814 :                     ref->u.ar.type = AR_SECTION;
    8257              :                 }
    8258              : 
    8259        59418 :               if (sym->attr.is_bind_c && e && is_CFI_desc (fsym, NULL))
    8260              :                 /* Implement F2018, 18.3.6, list item (5), bullet point 2.  */
    8261         5850 :                 gfc_conv_gfc_desc_to_cfi_desc (&parmse, e, fsym);
    8262              : 
    8263        53568 :               else if (fsym && fsym->attr.value && fsym->attr.dimension
    8264          102 :                        && e->rank != -1)
    8265              :                 /* VALUE array dummy: pass a private copy of the actual
    8266              :                    argument.  Allocatable components are copied deeply, so
    8267              :                    that the callee cannot reach the actual argument's data.
    8268              :                    The symbol passed is that of the actual argument, so that
    8269              :                    the copy is suppressed and a null pointer passed when an
    8270              :                    optional actual argument is absent.  */
    8271          102 :                 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg, INTENT_IN,
    8272              :                                            false, fsym, sym->name,
    8273          102 :                                            e->expr_type == EXPR_VARIABLE
    8274          102 :                                            ? e->symtree->n.sym : NULL,
    8275              :                                            false, true);
    8276              : 
    8277        53466 :               else if (pointer_array_result_p (e)
    8278          157 :                        && !(fsym && fsym->attr.pointer)
    8279        53617 :                        && copy_in_out_allowed (fsym, e, nodesc_arg))
    8280              :                 /* A pointer function result is copied into a temporary when
    8281              :                    its elements are not spaced by the element size.  */
    8282              :                 {
    8283          145 :                   gfc_expr *var = pointer_result_var (&parmse.pre, e);
    8284          283 :                   gfc_conv_subref_array_arg (&parmse, var, nodesc_arg,
    8285          138 :                                 fsym ? fsym->attr.intent : INTENT_INOUT,
    8286              :                                 false, fsym, sym->name, NULL, true, false,
    8287              :                                 dummy_accepts_strided_arg (fsym, nodesc_arg));
    8288          145 :                   gfc_symtree *st = var->symtree;
    8289          145 :                   gfc_free_expr (var);
    8290          145 :                   gfc_free_symbol (st->n.sym);
    8291          145 :                   XDELETE (st);
    8292              :                 }
    8293              : 
    8294        53321 :               else if (e->expr_type == EXPR_VARIABLE
    8295        41660 :                     && is_subref_array (e)
    8296         1327 :                     && !(fsym && fsym->attr.pointer)
    8297        54362 :                     && copy_in_out_allowed (fsym, e, nodesc_arg))
    8298              :                 /* The actual argument is a component reference to an
    8299              :                    array of derived types.  In this case, the argument
    8300              :                    is converted to a temporary, which is passed and then
    8301              :                    written back after the procedure call.  The elements of
    8302              :                    a span addressed dummy passed on as a whole are usually
    8303              :                    contiguous, so the copy is made conditional.  A dummy that
    8304              :                    has a descriptor takes any stride, so for it the condition
    8305              :                    is only that the span be the element length.  */
    8306              :                 {
    8307          831 :                   bool whole_span = is_whole_span_addressed_dummy (e);
    8308         1632 :                   gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
    8309          789 :                                 fsym ? fsym->attr.intent : INTENT_INOUT,
    8310          789 :                                 fsym && fsym->attr.pointer, fsym, sym->name,
    8311              :                                 NULL, whole_span, false,
    8312              :                                 whole_span
    8313           12 :                                 && dummy_accepts_strided_arg (fsym,
    8314              :                                                               nodesc_arg));
    8315              :                 }
    8316              : 
    8317        52490 :               else if (e->ts.type == BT_CLASS && CLASS_DATA (e)->as
    8318          421 :                        && CLASS_DATA (e)->as->type == AS_ASSUMED_SIZE
    8319           18 :                        && nodesc_arg && fsym->ts.type == BT_DERIVED)
    8320              :                 /* An assumed size class actual argument being passed to
    8321              :                    a 'no descriptor' formal argument just requires the
    8322              :                    data pointer to be passed. For class dummy arguments
    8323              :                    this is stored in the symbol backend decl..  */
    8324            6 :                 parmse.expr = e->symtree->n.sym->backend_decl;
    8325              : 
    8326        52484 :               else if (gfc_is_class_array_ref (e, NULL)
    8327          414 :                        && fsym && fsym->ts.type == BT_DERIVED
    8328        52676 :                        && copy_in_out_allowed (fsym, e, nodesc_arg))
    8329              :                 /* The actual argument is a component reference to an
    8330              :                    array of derived types.  In this case, the argument
    8331              :                    is converted to a temporary, which is passed and then
    8332              :                    written back after the procedure call.  */
    8333          114 :                 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
    8334          114 :                                            fsym->attr.intent,
    8335          114 :                                            fsym->attr.pointer);
    8336              : 
    8337        52370 :               else if (gfc_is_class_array_function (e)
    8338           13 :                        && fsym && fsym->ts.type == BT_DERIVED
    8339        52383 :                        && copy_in_out_allowed (fsym, e, nodesc_arg))
    8340              :                 /* See previous comment.  For function actual argument,
    8341              :                    the write out is not needed so the intent is set as
    8342              :                    intent in.  */
    8343              :                 {
    8344           13 :                   e->must_finalize = 1;
    8345           13 :                   gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
    8346           13 :                                              INTENT_IN, fsym->attr.pointer);
    8347              :                 }
    8348        48783 :               else if (fsym && fsym->attr.contiguous
    8349           90 :                        && (fsym->attr.target
    8350         1751 :                            ? gfc_is_not_contiguous (e)
    8351         1661 :                            : !gfc_is_simply_contiguous (e, false, true))
    8352          351 :                        && gfc_expr_is_variable (e)
    8353        54447 :                        && e->rank != -1)
    8354              :                 {
    8355          327 :                   gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
    8356          327 :                                              fsym->attr.intent,
    8357          327 :                                              fsym->attr.pointer);
    8358              :                 }
    8359              :               else
    8360              :                 {
    8361              :                   /* Having declined copy-in/copy-out above, a subobject of an
    8362              :                      array is described by a spanned descriptor.  */
    8363        52030 :                   if (e->expr_type == EXPR_VARIABLE && is_subref_array (e))
    8364          496 :                     parmse.force_no_tmp = 1;
    8365              : 
    8366              :                   /* This is where we introduce a temporary to store the
    8367              :                      result of a non-lvalue array expression.  */
    8368        52030 :                   gfc_conv_array_parameter (&parmse, e, nodesc_arg, fsym,
    8369              :                                             sym->name, NULL);
    8370              :                 }
    8371              : 
    8372              :               /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
    8373              :                  allocated on entry, it must be deallocated.
    8374              :                  CFI descriptors are handled elsewhere.  */
    8375        55795 :               if (fsym && fsym->attr.allocatable
    8376         1799 :                   && fsym->attr.intent == INTENT_OUT
    8377        59173 :                   && !is_CFI_desc (fsym, NULL))
    8378              :                 {
    8379          167 :                   if (fsym->ts.type == BT_DERIVED
    8380           53 :                       && fsym->ts.u.derived->attr.alloc_comp)
    8381              :                   {
    8382              :                     // deallocate the components first
    8383           11 :                     tmp = gfc_deallocate_alloc_comp (fsym->ts.u.derived,
    8384              :                                                      parmse.expr, e->rank);
    8385              :                     /* But check whether dummy argument is optional.  */
    8386           11 :                     if (tmp != NULL_TREE
    8387           11 :                         && fsym->attr.optional
    8388            6 :                         && e->expr_type == EXPR_VARIABLE
    8389            6 :                         && e->symtree->n.sym->attr.optional)
    8390              :                       {
    8391            6 :                         tree present;
    8392            6 :                         present = gfc_conv_expr_present (e->symtree->n.sym);
    8393            6 :                         tmp = build3_v (COND_EXPR, present, tmp,
    8394              :                                         build_empty_stmt (input_location));
    8395              :                       }
    8396           11 :                     if (tmp != NULL_TREE)
    8397           11 :                       gfc_add_expr_to_block (&dealloc_blk, tmp);
    8398              :                   }
    8399              : 
    8400          167 :                   tmp = parmse.expr;
    8401              :                   /* With bind(C), the actual argument is replaced by a bind-C
    8402              :                      descriptor; in this case, the data component arrives here,
    8403              :                      which shall not be dereferenced, but still freed and
    8404              :                      nullified.  */
    8405          167 :                   if  (TREE_TYPE(tmp) != pvoid_type_node)
    8406          167 :                     tmp = build_fold_indirect_ref_loc (input_location,
    8407              :                                                        parmse.expr);
    8408          167 :                   tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
    8409              :                                                     NULL_TREE, NULL_TREE, true,
    8410              :                                                     e,
    8411              :                                                     GFC_CAF_COARRAY_NOCOARRAY);
    8412          167 :                   if (fsym->attr.optional
    8413           48 :                       && e->expr_type == EXPR_VARIABLE
    8414           48 :                       && e->symtree->n.sym->attr.optional)
    8415           48 :                     tmp = fold_build3_loc (input_location, COND_EXPR,
    8416              :                                      void_type_node,
    8417           24 :                                      gfc_conv_expr_present (e->symtree->n.sym),
    8418              :                                        tmp, build_empty_stmt (input_location));
    8419          167 :                   gfc_add_expr_to_block (&dealloc_blk, tmp);
    8420              :                 }
    8421              :             }
    8422              :         }
    8423              :       /* Special case for an assumed-rank dummy argument. */
    8424       275276 :       if (!sym->attr.is_bind_c && e && fsym && e->rank > 0
    8425        58247 :           && (fsym->ts.type == BT_CLASS
    8426        58247 :               ? (CLASS_DATA (fsym)->as
    8427         4757 :                  && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
    8428        53490 :               : (fsym->as && fsym->as->type == AS_ASSUMED_RANK)))
    8429              :         {
    8430        12843 :           if (fsym->ts.type == BT_CLASS
    8431        12843 :               ? (CLASS_DATA (fsym)->attr.class_pointer
    8432         1067 :                  || CLASS_DATA (fsym)->attr.allocatable)
    8433        11776 :               : (fsym->attr.pointer || fsym->attr.allocatable))
    8434              :             {
    8435              :               /* Unallocated allocatable arrays and unassociated pointer
    8436              :                  arrays need their dtype setting if they are argument
    8437              :                  associated with assumed rank dummies to set the rank.  */
    8438          891 :               set_dtype_for_unallocated (&parmse, e);
    8439              :             }
    8440        11952 :           else if (e->expr_type == EXPR_VARIABLE
    8441        11449 :                    && e->symtree->n.sym->attr.dummy
    8442          722 :                    && (e->ts.type == BT_CLASS
    8443          915 :                        ? (e->ref && e->ref->next
    8444          193 :                           && e->ref->next->type == REF_ARRAY
    8445          193 :                           && e->ref->next->u.ar.type == AR_FULL
    8446          386 :                           && e->ref->next->u.ar.as->type == AS_ASSUMED_SIZE)
    8447          529 :                        : (e->ref && e->ref->type == REF_ARRAY
    8448          529 :                           && e->ref->u.ar.type == AR_FULL
    8449          757 :                           && e->ref->u.ar.as->type == AS_ASSUMED_SIZE)))
    8450              :             {
    8451              :               /* Assumed-size actual to assumed-rank dummy requires
    8452              :                  dim[rank-1].ubound = -1. */
    8453          180 :               tree minus_one;
    8454          180 :               tmp = build_fold_indirect_ref_loc (input_location, parmse.expr);
    8455          180 :               if (fsym->ts.type == BT_CLASS)
    8456           60 :                 tmp = gfc_class_data_get (tmp);
    8457          180 :               minus_one = build_int_cst (gfc_array_index_type, -1);
    8458          180 :               gfc_conv_descriptor_ubound_set (&parmse.pre, tmp,
    8459          180 :                                               gfc_rank_cst[e->rank - 1],
    8460              :                                               minus_one);
    8461              :             }
    8462              :         }
    8463              : 
    8464              :       /* The case with fsym->attr.optional is that of a user subroutine
    8465              :          with an interface indicating an optional argument.  When we call
    8466              :          an intrinsic subroutine, however, fsym is NULL, but we might still
    8467              :          have an optional argument, so we proceed to the substitution
    8468              :          just in case.  Arguments passed to bind(c) procedures via CFI
    8469              :          descriptors are handled elsewhere.  */
    8470       262037 :       if (e && (fsym == NULL || fsym->attr.optional)
    8471       335987 :           && !(sym->attr.is_bind_c && is_CFI_desc (fsym, NULL)))
    8472              :         {
    8473              :           /* If an optional argument is itself an optional dummy argument,
    8474              :              check its presence and substitute a null if absent.  This is
    8475              :              only needed when passing an array to an elemental procedure
    8476              :              as then array elements are accessed - or no NULL pointer is
    8477              :              allowed and a "1" or "0" should be passed if not present.
    8478              :              When passing a non-array-descriptor full array to a
    8479              :              non-array-descriptor dummy, no check is needed. For
    8480              :              array-descriptor actual to array-descriptor dummy, see
    8481              :              PR 41911 for why a check has to be inserted.
    8482              :              fsym == NULL is checked as intrinsics required the descriptor
    8483              :              but do not always set fsym.
    8484              :              Also, it is necessary to pass a NULL pointer to library routines
    8485              :              which usually ignore optional arguments, so they can handle
    8486              :              these themselves.  */
    8487        59617 :           if (e->expr_type == EXPR_VARIABLE
    8488        26636 :               && e->symtree->n.sym->attr.optional
    8489         2469 :               && (((e->rank != 0 && elemental_proc)
    8490         2288 :                    || e->representation.length || e->ts.type == BT_CHARACTER
    8491         2044 :                    || (e->rank == 0 && e->symtree->n.sym->attr.value)
    8492         1934 :                    || (e->rank != 0
    8493         1094 :                        && (fsym == NULL
    8494         1058 :                            || (fsym->as
    8495          296 :                                && (fsym->as->type == AS_ASSUMED_SHAPE
    8496          241 :                                    || fsym->as->type == AS_ASSUMED_RANK
    8497          123 :                                    || fsym->as->type == AS_DEFERRED)))))
    8498         1691 :                   || se->ignore_optional))
    8499          806 :             gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
    8500          806 :                                     e->representation.length);
    8501              :         }
    8502              : 
    8503              :       /* Make the class container for the first argument available with class
    8504              :          valued transformational functions.  */
    8505       275276 :       if (argc == 0 && e && e->ts.type == BT_CLASS
    8506         5261 :           && isym && isym->transformational
    8507           84 :           && se->ss && se->ss->info)
    8508              :         {
    8509           84 :           arg1_cntnr = parmse.expr;
    8510           84 :           if (POINTER_TYPE_P (TREE_TYPE (arg1_cntnr)))
    8511           84 :             arg1_cntnr = build_fold_indirect_ref_loc (input_location, arg1_cntnr);
    8512           84 :           arg1_cntnr = gfc_get_class_from_expr (arg1_cntnr);
    8513           84 :           se->ss->info->class_container = arg1_cntnr;
    8514              :         }
    8515              : 
    8516              :       /* Obtain the character length of an assumed character length procedure
    8517              :          from the typespec of the actual argument.  */
    8518       275276 :       if (e
    8519       262037 :           && parmse.string_length == NULL_TREE
    8520       226205 :           && e->ts.type == BT_PROCEDURE
    8521         1941 :           && e->symtree->n.sym->ts.type == BT_CHARACTER
    8522           21 :           && e->symtree->n.sym->ts.u.cl->length != NULL
    8523           21 :           && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
    8524              :         {
    8525           13 :           gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
    8526           13 :           parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
    8527              :         }
    8528              : 
    8529       275276 :       if (fsym && e)
    8530              :         {
    8531              :           /* Obtain the character length for a NULL() actual with a character
    8532              :              MOLD argument.  Otherwise substitute a suitable dummy length.
    8533              :              Here we handle non-optional dummies of non-bind(c) procedures.  */
    8534       230109 :           if (e->expr_type == EXPR_NULL
    8535          763 :               && fsym->ts.type == BT_CHARACTER
    8536          296 :               && !fsym->attr.optional
    8537       230327 :               && !(sym->attr.is_bind_c && is_CFI_desc (fsym, NULL)))
    8538          216 :             conv_null_actual (&parmse, e, fsym);
    8539              :         }
    8540              : 
    8541              :       /* If any actual argument of the procedure is allocatable and passed
    8542              :          to an allocatable dummy with INTENT(OUT), we conservatively
    8543              :          evaluate actual argument expressions before deallocations are
    8544              :          performed and the procedure is executed.  May create temporaries.
    8545              :          This ensures we conform to F2023:15.5.3, 15.5.4.  */
    8546       262037 :       if (e && fsym && force_eval_args
    8547         1180 :           && fsym->attr.intent != INTENT_OUT
    8548       275703 :           && !gfc_is_constant_expr (e))
    8549          274 :         parmse.expr = gfc_evaluate_now (parmse.expr, &parmse.pre);
    8550              : 
    8551       275276 :       if (fsym && need_interface_mapping && e)
    8552        40816 :         gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
    8553              : 
    8554       275276 :       gfc_add_block_to_block (&se->pre, &parmse.pre);
    8555       275276 :       gfc_add_block_to_block (&post, &parmse.post);
    8556       275276 :       gfc_add_block_to_block (&se->finalblock, &parmse.finalblock);
    8557              : 
    8558              :       /* Allocated allocatable components of derived types must be
    8559              :          deallocated for non-variable scalars, array arguments to elemental
    8560              :          procedures, and array arguments with descriptor to non-elemental
    8561              :          procedures.  As bounds information for descriptorless arrays is no
    8562              :          longer available here, they are dealt with in trans-array.cc
    8563              :          (gfc_conv_array_parameter).  A derived-type temporary for a
    8564              :          parenthesized variable shares the components of the variable.  */
    8565       262037 :       if (e && (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS)
    8566        29281 :             && e->ts.u.derived->attr.alloc_comp
    8567         7744 :             && (e->rank == 0 || elemental_proc || !nodesc_arg)
    8568         7600 :             && !expr_may_alias_variables (e, elemental_proc)
    8569       275666 :             && !(e->ts.type == BT_DERIVED && parenthesized_variable (e)))
    8570              :         {
    8571          354 :           int parm_rank;
    8572              :           /* It is known the e returns a structure type with at least one
    8573              :              allocatable component.  When e is a function, ensure that the
    8574              :              function is called once only by using a temporary variable.  */
    8575          354 :           if (!DECL_P (parmse.expr) && e->expr_type == EXPR_FUNCTION)
    8576          140 :             parmse.expr = gfc_evaluate_now_loc (input_location,
    8577              :                                                 parmse.expr, &se->pre);
    8578              : 
    8579          354 :           if ((fsym && fsym->attr.value) || e->expr_type == EXPR_ARRAY)
    8580          152 :             tmp = parmse.expr;
    8581              :           else
    8582          202 :             tmp = build_fold_indirect_ref_loc (input_location,
    8583              :                                                parmse.expr);
    8584              : 
    8585          354 :           parm_rank = e->rank;
    8586          354 :           switch (parm_kind)
    8587              :             {
    8588              :             case (ELEMENTAL):
    8589              :             case (SCALAR):
    8590          354 :               parm_rank = 0;
    8591              :               break;
    8592              : 
    8593            0 :             case (SCALAR_POINTER):
    8594            0 :               tmp = build_fold_indirect_ref_loc (input_location,
    8595              :                                              tmp);
    8596            0 :               break;
    8597              :             }
    8598              : 
    8599          354 :           if (e->ts.type == BT_DERIVED && fsym && fsym->ts.type == BT_CLASS)
    8600              :             {
    8601              :               /* The derived type is passed to gfc_deallocate_alloc_comp.
    8602              :                  Therefore, class actuals can be handled correctly but derived
    8603              :                  types passed to class formals need the _data component.  */
    8604           70 :               tmp = gfc_class_data_get (tmp);
    8605           70 :               if (!CLASS_DATA (fsym)->attr.dimension)
    8606              :                 {
    8607           44 :                   if (UNLIMITED_POLY (fsym))
    8608              :                     {
    8609            6 :                       tree type = gfc_typenode_for_spec (&e->ts);
    8610            6 :                       type = build_pointer_type (type);
    8611            6 :                       tmp = fold_convert (type, tmp);
    8612              :                     }
    8613           44 :                   tmp = build_fold_indirect_ref_loc (input_location, tmp);
    8614              :                 }
    8615              :             }
    8616              : 
    8617          354 :           if (parenthesized_variable (e))
    8618              :             {
    8619            0 :               tree local_tmp;
    8620            0 :               local_tmp = gfc_evaluate_now (tmp, &se->pre);
    8621            0 :               local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp,
    8622              :                                                parm_rank, 0);
    8623            0 :               gfc_add_expr_to_block (&se->post, local_tmp);
    8624              :             }
    8625              : 
    8626              :           /* Items of array expressions passed to a polymorphic formal arguments
    8627              :              create their own clean up, so prevent double free.  */
    8628          354 :           if (!finalized && !e->must_finalize
    8629          353 :               && !(e->expr_type == EXPR_ARRAY && fsym
    8630           86 :                    && fsym->ts.type == BT_CLASS))
    8631              :             {
    8632          333 :               bool scalar_res_outside_loop;
    8633          987 :               scalar_res_outside_loop = e->expr_type == EXPR_FUNCTION
    8634          151 :                                         && parm_rank == 0
    8635          472 :                                         && parmse.loop;
    8636              : 
    8637              :               /* Scalars passed to an assumed rank argument are converted to
    8638              :                  a descriptor. Obtain the data field before deallocating any
    8639              :                  allocatable components.  */
    8640          274 :               if (parm_rank == 0 && e->expr_type != EXPR_ARRAY
    8641          570 :                   && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
    8642           19 :                 tmp = gfc_conv_descriptor_data_get (tmp);
    8643              : 
    8644          333 :               if (scalar_res_outside_loop)
    8645              :                 {
    8646              :                   /* Go through the ss chain to find the argument and use
    8647              :                      the stored value.  */
    8648           30 :                   gfc_ss *tmp_ss = parmse.loop->ss;
    8649           72 :                   for (; tmp_ss; tmp_ss = tmp_ss->next)
    8650           60 :                     if (tmp_ss->info
    8651           48 :                         && tmp_ss->info->expr == e
    8652           18 :                         && tmp_ss->info->data.scalar.value != NULL_TREE)
    8653              :                       {
    8654           18 :                         tmp = tmp_ss->info->data.scalar.value;
    8655           18 :                         break;
    8656              :                       }
    8657              :                 }
    8658              : 
    8659          333 :               STRIP_NOPS (tmp);
    8660              : 
    8661          333 :               if (derived_array != NULL_TREE)
    8662            0 :                 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived,
    8663              :                                                  derived_array,
    8664              :                                                  parm_rank);
    8665          333 :               else if ((e->ts.type == BT_CLASS
    8666           24 :                         && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
    8667          333 :                        || e->ts.type == BT_DERIVED)
    8668          333 :                 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp,
    8669              :                                                  parm_rank, 0, true);
    8670            0 :               else if (e->ts.type == BT_CLASS)
    8671            0 :                 tmp = gfc_deallocate_alloc_comp (CLASS_DATA (e)->ts.u.derived,
    8672              :                                                  tmp, parm_rank);
    8673              : 
    8674          333 :               if (scalar_res_outside_loop)
    8675           30 :                 gfc_add_expr_to_block (&parmse.loop->post, tmp);
    8676              :               else
    8677          303 :                 gfc_prepend_expr_to_block (&post, tmp);
    8678              :             }
    8679              :         }
    8680              : 
    8681              :       /* Add argument checking of passing an unallocated/NULL actual to
    8682              :          a nonallocatable/nonpointer dummy.  */
    8683              : 
    8684       275276 :       if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
    8685              :         {
    8686         6546 :           symbol_attribute attr;
    8687         6546 :           char *msg;
    8688         6546 :           tree cond;
    8689         6546 :           tree tmp;
    8690         6546 :           symbol_attribute fsym_attr;
    8691              : 
    8692         6546 :           if (fsym)
    8693              :             {
    8694         6385 :               if (fsym->ts.type == BT_CLASS)
    8695              :                 {
    8696          321 :                   fsym_attr = CLASS_DATA (fsym)->attr;
    8697          321 :                   fsym_attr.pointer = fsym_attr.class_pointer;
    8698              :                 }
    8699              :               else
    8700         6064 :                 fsym_attr = fsym->attr;
    8701              :             }
    8702              : 
    8703         6546 :           if (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
    8704         4094 :             attr = gfc_expr_attr (e);
    8705              :           else
    8706         6081 :             goto end_pointer_check;
    8707              : 
    8708              :           /*  In Fortran 2008 it's allowed to pass a NULL pointer/nonallocated
    8709              :               allocatable to an optional dummy, cf. 12.5.2.12.  */
    8710         4094 :           if (fsym != NULL && fsym->attr.optional && !attr.proc_pointer
    8711         1038 :               && (gfc_option.allow_std & GFC_STD_F2008) != 0)
    8712         1032 :             goto end_pointer_check;
    8713              : 
    8714         3062 :           if (attr.optional)
    8715              :             {
    8716              :               /* If the actual argument is an optional pointer/allocatable and
    8717              :                  the formal argument takes an nonpointer optional value,
    8718              :                  it is invalid to pass a non-present argument on, even
    8719              :                  though there is no technical reason for this in gfortran.
    8720              :                  See Fortran 2003, Section 12.4.1.6 item (7)+(8).  */
    8721           96 :               tree present, null_ptr, type;
    8722              : 
    8723           96 :               if (attr.allocatable
    8724           12 :                   && (fsym == NULL || !fsym_attr.allocatable))
    8725            0 :                 msg = xasprintf ("Allocatable actual argument '%s' is not "
    8726              :                                  "allocated or not present",
    8727            0 :                                  e->symtree->n.sym->name);
    8728           96 :               else if (attr.pointer
    8729           24 :                        && (fsym == NULL || !fsym_attr.pointer))
    8730           12 :                 msg = xasprintf ("Pointer actual argument '%s' is not "
    8731              :                                  "associated or not present",
    8732           12 :                                  e->symtree->n.sym->name);
    8733           84 :               else if (attr.proc_pointer && !e->value.function.actual
    8734            0 :                        && (fsym == NULL || !fsym_attr.proc_pointer))
    8735            0 :                 msg = xasprintf ("Proc-pointer actual argument '%s' is not "
    8736              :                                  "associated or not present",
    8737            0 :                                  e->symtree->n.sym->name);
    8738              :               else
    8739           84 :                 goto end_pointer_check;
    8740              : 
    8741           12 :               present = gfc_conv_expr_present (e->symtree->n.sym);
    8742           12 :               type = TREE_TYPE (present);
    8743           12 :               present = fold_build2_loc (input_location, EQ_EXPR,
    8744              :                                          logical_type_node, present,
    8745              :                                          fold_convert (type,
    8746              :                                                        null_pointer_node));
    8747           12 :               type = TREE_TYPE (parmse.expr);
    8748           12 :               null_ptr = fold_build2_loc (input_location, EQ_EXPR,
    8749              :                                           logical_type_node, parmse.expr,
    8750              :                                           fold_convert (type,
    8751              :                                                         null_pointer_node));
    8752           12 :               cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    8753              :                                       logical_type_node, present, null_ptr);
    8754              :             }
    8755              :           else
    8756              :             {
    8757         2966 :               if (attr.allocatable
    8758          244 :                   && (fsym == NULL || !fsym_attr.allocatable))
    8759          190 :                 msg = xasprintf ("Allocatable actual argument '%s' is not "
    8760          190 :                                  "allocated", e->symtree->n.sym->name);
    8761         2776 :               else if (attr.pointer
    8762          260 :                        && (fsym == NULL || !fsym_attr.pointer))
    8763          184 :                 msg = xasprintf ("Pointer actual argument '%s' is not "
    8764          184 :                                  "associated", e->symtree->n.sym->name);
    8765         2592 :               else if (attr.proc_pointer && !e->value.function.actual
    8766           80 :                        && (fsym == NULL
    8767           50 :                            || (!fsym_attr.proc_pointer && !fsym_attr.optional)))
    8768           79 :                 msg = xasprintf ("Proc-pointer actual argument '%s' is not "
    8769           79 :                                  "associated", e->symtree->n.sym->name);
    8770              :               else
    8771         2513 :                 goto end_pointer_check;
    8772              : 
    8773          453 :               tmp = parmse.expr;
    8774          453 :               if (fsym && fsym->ts.type == BT_CLASS && !attr.proc_pointer)
    8775              :                 {
    8776           76 :                   if (POINTER_TYPE_P (TREE_TYPE (tmp)))
    8777           70 :                     tmp = build_fold_indirect_ref_loc (input_location, tmp);
    8778           76 :                   tmp = gfc_class_data_get (tmp);
    8779           76 :                   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
    8780            3 :                     tmp = gfc_conv_descriptor_data_get (tmp);
    8781              :                 }
    8782              : 
    8783              :               /* If the argument is passed by value, we need to strip the
    8784              :                  INDIRECT_REF.  */
    8785          453 :               if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
    8786           12 :                 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
    8787              : 
    8788          453 :               cond = fold_build2_loc (input_location, EQ_EXPR,
    8789              :                                       logical_type_node, tmp,
    8790          453 :                                       fold_convert (TREE_TYPE (tmp),
    8791              :                                                     null_pointer_node));
    8792              :             }
    8793              : 
    8794          465 :           gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
    8795              :                                    msg);
    8796          465 :           free (msg);
    8797              :         }
    8798       268730 :       end_pointer_check:
    8799              : 
    8800              :       /* Deferred length dummies pass the character length by reference
    8801              :          so that the value can be returned.  */
    8802       275276 :       if (parmse.string_length && fsym && fsym->ts.deferred)
    8803              :         {
    8804          795 :           if (INDIRECT_REF_P (parmse.string_length))
    8805              :             {
    8806              :               /* In chains of functions/procedure calls the string_length already
    8807              :                  is a pointer to the variable holding the length.  Therefore
    8808              :                  remove the deref on call.  */
    8809           90 :               tmp = parmse.string_length;
    8810           90 :               parmse.string_length = TREE_OPERAND (parmse.string_length, 0);
    8811              :             }
    8812              :           else
    8813              :             {
    8814          705 :               tmp = parmse.string_length;
    8815          705 :               if (!VAR_P (tmp) && TREE_CODE (tmp) != COMPONENT_REF)
    8816           61 :                 tmp = gfc_evaluate_now (parmse.string_length, &se->pre);
    8817          705 :               parmse.string_length = gfc_build_addr_expr (NULL_TREE, tmp);
    8818              :             }
    8819              : 
    8820          795 :           if (e && e->expr_type == EXPR_VARIABLE
    8821          638 :               && fsym->attr.allocatable
    8822          368 :               && e->ts.u.cl->backend_decl
    8823          368 :               && VAR_P (e->ts.u.cl->backend_decl))
    8824              :             {
    8825          284 :               if (INDIRECT_REF_P (tmp))
    8826            0 :                 tmp = TREE_OPERAND (tmp, 0);
    8827          284 :               gfc_add_modify (&se->post, e->ts.u.cl->backend_decl,
    8828              :                               fold_convert (gfc_charlen_type_node, tmp));
    8829              :             }
    8830              :         }
    8831              : 
    8832              :       /* Character strings are passed as two parameters, a length and a
    8833              :          pointer - except for Bind(c) and c_ptrs which only pass the pointer.
    8834              :          An unlimited polymorphic formal argument likewise does not
    8835              :          need the length.  */
    8836       275276 :       if (parmse.string_length != NULL_TREE
    8837        37260 :           && !sym->attr.is_bind_c
    8838        36510 :           && !(fsym && fsym->ts.type == BT_DERIVED && fsym->ts.u.derived
    8839            6 :                && fsym->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
    8840            6 :                && fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING )
    8841        30625 :           && !(fsym && fsym->ts.type == BT_ASSUMED)
    8842        30516 :           && !(fsym && UNLIMITED_POLY (fsym)))
    8843        36220 :         vec_safe_push (stringargs, parmse.string_length);
    8844              : 
    8845              :       /* When calling __copy for character expressions to unlimited
    8846              :          polymorphic entities, the dst argument needs a string length.  */
    8847        52287 :       if (sym->name[0] == '_' && e && e->ts.type == BT_CHARACTER
    8848         5326 :           && startswith (sym->name, "__vtab_CHARACTER")
    8849            0 :           && arg->next && arg->next->expr
    8850            0 :           && (arg->next->expr->ts.type == BT_DERIVED
    8851            0 :               || arg->next->expr->ts.type == BT_CLASS)
    8852       275276 :           && arg->next->expr->ts.u.derived->attr.unlimited_polymorphic)
    8853            0 :         vec_safe_push (stringargs, parmse.string_length);
    8854              : 
    8855              :       /* For descriptorless coarrays and assumed-shape coarray dummies, we
    8856              :          pass the token and the offset as additional arguments.  */
    8857       275276 :       if (fsym && e == NULL && flag_coarray == GFC_FCOARRAY_LIB
    8858          144 :           && attr->codimension && !attr->allocatable)
    8859              :         {
    8860              :           /* Token and offset.  */
    8861            5 :           vec_safe_push (stringargs, null_pointer_node);
    8862            5 :           vec_safe_push (stringargs, build_int_cst (gfc_array_index_type, 0));
    8863            5 :           gcc_assert (fsym->attr.optional);
    8864              :         }
    8865       242285 :       else if (fsym && flag_coarray == GFC_FCOARRAY_LIB && attr->codimension
    8866          305 :                && !attr->allocatable)
    8867              :         {
    8868          123 :           tree caf_decl, caf_type, caf_desc = NULL_TREE;
    8869          123 :           tree offset, tmp2;
    8870              : 
    8871          123 :           caf_decl = gfc_get_tree_for_caf_expr (e);
    8872          123 :           caf_type = TREE_TYPE (caf_decl);
    8873          123 :           if (POINTER_TYPE_P (caf_type)
    8874          123 :               && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_type)))
    8875            3 :             caf_desc = TREE_TYPE (caf_type);
    8876          120 :           else if (GFC_DESCRIPTOR_TYPE_P (caf_type))
    8877              :             caf_desc = caf_type;
    8878              : 
    8879           51 :           if (caf_desc
    8880           51 :               && (GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_ALLOCATABLE
    8881            0 :                   || GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_POINTER))
    8882              :             {
    8883          102 :               tmp = POINTER_TYPE_P (TREE_TYPE (caf_decl))
    8884           54 :                       ? build_fold_indirect_ref (caf_decl)
    8885              :                       : caf_decl;
    8886           51 :               tmp = gfc_conv_descriptor_token (tmp);
    8887              :             }
    8888           72 :           else if (DECL_LANG_SPECIFIC (caf_decl)
    8889           72 :                    && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
    8890           12 :             tmp = GFC_DECL_TOKEN (caf_decl);
    8891              :           else
    8892              :             {
    8893           60 :               gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
    8894              :                           && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
    8895           60 :               tmp = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
    8896              :             }
    8897              : 
    8898          123 :           vec_safe_push (stringargs, tmp);
    8899              : 
    8900          123 :           if (caf_desc
    8901          123 :               && GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_ALLOCATABLE)
    8902           51 :             offset = build_int_cst (gfc_array_index_type, 0);
    8903           72 :           else if (DECL_LANG_SPECIFIC (caf_decl)
    8904           72 :                    && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
    8905           12 :             offset = GFC_DECL_CAF_OFFSET (caf_decl);
    8906           60 :           else if (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) != NULL_TREE)
    8907            0 :             offset = GFC_TYPE_ARRAY_CAF_OFFSET (caf_type);
    8908              :           else
    8909           60 :             offset = build_int_cst (gfc_array_index_type, 0);
    8910              : 
    8911          123 :           if (caf_desc)
    8912              :             {
    8913          102 :               tmp = POINTER_TYPE_P (TREE_TYPE (caf_decl))
    8914           54 :                       ? build_fold_indirect_ref (caf_decl)
    8915              :                       : caf_decl;
    8916           51 :               tmp = gfc_conv_descriptor_data_get (tmp);
    8917              :             }
    8918              :           else
    8919              :             {
    8920           72 :               gcc_assert (POINTER_TYPE_P (caf_type));
    8921           72 :               tmp = caf_decl;
    8922              :             }
    8923              : 
    8924          108 :           tmp2 = fsym->ts.type == BT_CLASS
    8925          123 :                  ? gfc_class_data_get (parmse.expr) : parmse.expr;
    8926          123 :           if ((fsym->ts.type != BT_CLASS
    8927          108 :                && (fsym->as->type == AS_ASSUMED_SHAPE
    8928           59 :                    || fsym->as->type == AS_ASSUMED_RANK))
    8929           74 :               || (fsym->ts.type == BT_CLASS
    8930           15 :                   && (CLASS_DATA (fsym)->as->type == AS_ASSUMED_SHAPE
    8931           10 :                       || CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)))
    8932              :             {
    8933           54 :               if (fsym->ts.type == BT_CLASS)
    8934            5 :                 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (tmp2)));
    8935              :               else
    8936              :                 {
    8937           49 :                   gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
    8938           49 :                   tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
    8939              :                 }
    8940           54 :               gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)));
    8941           54 :               tmp2 = gfc_conv_descriptor_data_get (tmp2);
    8942              :             }
    8943           69 :           else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
    8944           10 :             tmp2 = gfc_conv_descriptor_data_get (tmp2);
    8945              :           else
    8946              :             {
    8947           59 :               gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
    8948              :             }
    8949              : 
    8950          123 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    8951              :                                  gfc_array_index_type,
    8952              :                                  fold_convert (gfc_array_index_type, tmp2),
    8953              :                                  fold_convert (gfc_array_index_type, tmp));
    8954          123 :           offset = fold_build2_loc (input_location, PLUS_EXPR,
    8955              :                                     gfc_array_index_type, offset, tmp);
    8956              : 
    8957          123 :           vec_safe_push (stringargs, offset);
    8958          123 :         }
    8959       275148 :      else if (e && gfc_pdt_requires_hidden_arg (fsym))
    8960              :         {
    8961          202 :           gfc_actual_arglist *parms;
    8962          202 :           tree hidden_pdt;
    8963          202 :           gfc_symbol *derived;
    8964              : 
    8965              :           /* Build a temporary of the declared type.  */
    8966          202 :           if (IS_CLASS_PDT (e))
    8967              :             {
    8968           48 :               tmp = gfc_typenode_for_spec (&CLASS_DATA (e)->ts);
    8969           48 :               hidden_pdt = gfc_create_var (tmp, "pdt");
    8970           48 :               derived = CLASS_DATA (e)->ts.u.derived;
    8971              :             }
    8972              :           else
    8973              :             {
    8974          154 :               tmp = gfc_typenode_for_spec (&e->ts);
    8975          154 :               hidden_pdt = gfc_create_var (tmp, "pdt");
    8976          154 :               derived = e->ts.u.derived;
    8977              :             }
    8978              : 
    8979              :           /* Obtain the PDT parameter list.  */
    8980          202 :           parms = e->param_list ? e->param_list :
    8981          202 :                                   e->symtree->n.sym->param_list;
    8982              : 
    8983          202 :           if (gfc_spec_list_type (parms, derived) == SPEC_EXPLICIT)
    8984              :             {
    8985              :               /* If all the parameters are explcit, build the hidden PDT...  */
    8986           60 :               tmp = gfc_allocate_pdt_comp (derived, hidden_pdt,
    8987              :                                            0, parms);
    8988           60 :               gfc_add_expr_to_block (&se->pre, tmp);
    8989           60 :               tmp = gfc_build_addr_expr (NULL_TREE, hidden_pdt);
    8990           60 :               vec_safe_push (stringargs, tmp);
    8991              :               /* ...and deallocate it after the calls.  */
    8992           60 :               tmp = gfc_deallocate_pdt_comp (derived,
    8993              :                                              hidden_pdt, 0);
    8994           60 :               gfc_add_expr_to_block (&se->post, tmp);
    8995              :             }
    8996          142 :           else if (e->expr_type == EXPR_VARIABLE
    8997          142 :                    && e->symtree->n.sym->attr.dummy
    8998            6 :                    && e->symtree->n.sym->hidden_pdt_dummy_decl != NULL_TREE)
    8999              :             /* Otherwise hope that this was a dummy with a hidden arg.  */
    9000            6 :             vec_safe_push (stringargs,
    9001            6 :                            e->symtree->n.sym->hidden_pdt_dummy_decl);
    9002              :           else
    9003              :             {
    9004              :               /* Or else just add it without the parameter set here.  */
    9005          136 :               tmp = gfc_build_addr_expr (NULL_TREE, hidden_pdt);
    9006          136 :               vec_safe_push (stringargs, tmp);
    9007              :             }
    9008              :         }
    9009       275276 :       vec_safe_push (arglist, parmse.expr);
    9010              :     }
    9011              : 
    9012       134061 :   gfc_add_block_to_block (&se->pre, &dealloc_blk);
    9013       134061 :   gfc_add_block_to_block (&se->pre, &clobbers);
    9014       134061 :   gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
    9015              : 
    9016       134061 :   if (comp)
    9017         2060 :     ts = comp->ts;
    9018       132001 :   else if (sym->ts.type == BT_CLASS)
    9019          900 :     ts = CLASS_DATA (sym)->ts;
    9020              :   else
    9021       131101 :     ts = sym->ts;
    9022              : 
    9023       134061 :   if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
    9024          210 :     se->string_length = build_int_cst (gfc_charlen_type_node, 1);
    9025       133851 :   else if (ts.type == BT_CHARACTER)
    9026              :     {
    9027         5118 :       if (ts.u.cl->length == NULL)
    9028              :         {
    9029              :           /* Assumed character length results are not allowed by C418 of the 2003
    9030              :              standard and are trapped in resolve.cc; except in the case of SPREAD
    9031              :              (and other intrinsics?) and dummy functions.  In the case of SPREAD,
    9032              :              we take the character length of the first argument for the result.
    9033              :              For dummies, we have to look through the formal argument list for
    9034              :              this function and use the character length found there.
    9035              :              Likewise, we handle the case of deferred-length character dummy
    9036              :              arguments to intrinsics that determine the characteristics of
    9037              :              the result, which cannot be deferred-length.  */
    9038         2333 :           if (expr->value.function.isym)
    9039         1703 :             ts.deferred = false;
    9040         2333 :           if (ts.deferred)
    9041          623 :             cl.backend_decl = gfc_create_var (gfc_charlen_type_node, "slen");
    9042         1710 :           else if (!sym->attr.dummy)
    9043         1703 :             cl.backend_decl = (*stringargs)[0];
    9044              :           else
    9045              :             {
    9046            7 :               formal = gfc_sym_get_dummy_args (sym->ns->proc_name);
    9047           26 :               for (; formal; formal = formal->next)
    9048           12 :                 if (strcmp (formal->sym->name, sym->name) == 0)
    9049            7 :                   cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
    9050              :             }
    9051              :           len = cl.backend_decl;
    9052              :         }
    9053              :       else
    9054              :         {
    9055         2785 :           tree tmp;
    9056              : 
    9057              :           /* Calculate the length of the returned string.  */
    9058         2785 :           gfc_init_se (&parmse, NULL);
    9059         2785 :           if (need_interface_mapping)
    9060         1909 :             gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
    9061              :           else
    9062          876 :             gfc_conv_expr (&parmse, ts.u.cl->length);
    9063         2785 :           gfc_add_block_to_block (&se->pre, &parmse.pre);
    9064         2785 :           gfc_add_block_to_block (&se->post, &parmse.post);
    9065         2785 :           tmp = parmse.expr;
    9066              :           /* TODO: It would be better to have the charlens as
    9067              :              gfc_charlen_type_node already when the interface is
    9068              :              created instead of converting it here (see PR 84615).  */
    9069         2785 :           tmp = fold_build2_loc (input_location, MAX_EXPR,
    9070              :                                  gfc_charlen_type_node,
    9071              :                                  fold_convert (gfc_charlen_type_node, tmp),
    9072              :                                  build_zero_cst (gfc_charlen_type_node));
    9073         2785 :           cl.backend_decl = tmp;
    9074              : 
    9075              :           /* The length was fully computed above from the specification
    9076              :              expression, without needing the callee to actually run.  */
    9077         2785 :           call_needed_for_length = false;
    9078              :         }
    9079              : 
    9080              :       /* Set up a charlen structure for it.  */
    9081         5118 :       cl.next = NULL;
    9082         5118 :       cl.length = NULL;
    9083         5118 :       ts.u.cl = &cl;
    9084              : 
    9085         5118 :       len = cl.backend_decl;
    9086              :     }
    9087              : 
    9088         2060 :   byref = (comp && (comp->attr.dimension
    9089         1991 :            || (comp->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)))
    9090       134061 :            || (!comp && gfc_return_by_reference (sym));
    9091              : 
    9092              :   if (byref)
    9093              :     {
    9094        19125 :       if (se->direct_byref)
    9095              :         {
    9096              :           /* Sometimes, too much indirection can be applied; e.g. for
    9097              :              function_result = array_valued_recursive_function.  */
    9098         7047 :           if (TREE_TYPE (TREE_TYPE (se->expr))
    9099         7047 :                 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
    9100         7065 :                 && GFC_DESCRIPTOR_TYPE_P
    9101              :                         (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
    9102           18 :             se->expr = build_fold_indirect_ref_loc (input_location,
    9103              :                                                     se->expr);
    9104              : 
    9105              :           /* If the lhs of an assignment x = f(..) is allocatable and
    9106              :              f2003 is allowed, we must do the automatic reallocation.
    9107              :              TODO - deal with intrinsics, without using a temporary.  */
    9108         7047 :           if (flag_realloc_lhs
    9109         6972 :                 && se->ss && se->ss->loop_chain
    9110          203 :                 && se->ss->loop_chain->is_alloc_lhs
    9111          203 :                 && !expr->value.function.isym
    9112          203 :                 && sym->result->as != NULL)
    9113              :             {
    9114              :               /* Evaluate the bounds of the result, if known.  */
    9115          203 :               gfc_set_loop_bounds_from_array_spec (&mapping, se,
    9116              :                                                    sym->result->as);
    9117              : 
    9118              :               /* Perform the automatic reallocation.  */
    9119          203 :               tmp = gfc_alloc_allocatable_for_assignment (se->loop,
    9120              :                                                           expr, NULL);
    9121          203 :               gfc_add_expr_to_block (&se->pre, tmp);
    9122              : 
    9123              :               /* Pass the temporary as the first argument.  */
    9124          203 :               result = info->descriptor;
    9125              :             }
    9126              :           else
    9127         6844 :             result = build_fold_indirect_ref_loc (input_location,
    9128              :                                                   se->expr);
    9129         7047 :           vec_safe_push (retargs, se->expr);
    9130              :         }
    9131        12078 :       else if (comp && comp->attr.dimension)
    9132              :         {
    9133           66 :           gcc_assert (se->loop && info);
    9134              : 
    9135              :           /* Set the type of the array. vtable charlens are not always reliable.
    9136              :              Use the interface, if possible.  */
    9137           66 :           if (comp->ts.type == BT_CHARACTER
    9138            1 :               && expr->symtree->n.sym->ts.type == BT_CLASS
    9139            1 :               && comp->ts.interface && comp->ts.interface->result)
    9140            1 :             tmp = gfc_typenode_for_spec (&comp->ts.interface->result->ts);
    9141              :           else
    9142           65 :             tmp = gfc_typenode_for_spec (&comp->ts);
    9143           66 :           gcc_assert (se->ss->dimen == se->loop->dimen);
    9144              : 
    9145              :           /* Evaluate the bounds of the result, if known.  */
    9146           66 :           gfc_set_loop_bounds_from_array_spec (&mapping, se, comp->as);
    9147              : 
    9148              :           /* If the lhs of an assignment x = f(..) is allocatable and
    9149              :              f2003 is allowed, we must not generate the function call
    9150              :              here but should just send back the results of the mapping.
    9151              :              This is signalled by the function ss being flagged.  */
    9152           66 :           if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
    9153              :             {
    9154            0 :               gfc_free_interface_mapping (&mapping);
    9155            0 :               return has_alternate_specifier;
    9156              :             }
    9157              : 
    9158              :           /* Create a temporary to store the result.  In case the function
    9159              :              returns a pointer, the temporary will be a shallow copy and
    9160              :              mustn't be deallocated.  */
    9161           66 :           callee_alloc = comp->attr.allocatable || comp->attr.pointer;
    9162           66 :           gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
    9163              :                                        tmp, NULL_TREE, false,
    9164              :                                        !comp->attr.pointer, callee_alloc,
    9165           66 :                                        &se->ss->info->expr->where);
    9166              : 
    9167              :           /* Index a pointer result by the span the callee returns.  */
    9168           66 :           if (comp->attr.pointer && VAR_P (info->descriptor))
    9169            0 :             GFC_DECL_PTR_ARRAY_P (info->descriptor) = 1;
    9170              : 
    9171              :           /* Pass the temporary as the first argument.  */
    9172           66 :           result = info->descriptor;
    9173           66 :           tmp = gfc_build_addr_expr (NULL_TREE, result);
    9174           66 :           vec_safe_push (retargs, tmp);
    9175              :         }
    9176        11783 :       else if (!comp && sym->result->attr.dimension)
    9177              :         {
    9178         8698 :           gcc_assert (se->loop && info);
    9179              : 
    9180              :           /* Set the type of the array.  */
    9181         8698 :           tmp = gfc_typenode_for_spec (&ts);
    9182         8698 :           tmp = arg1_cntnr ? TREE_TYPE (arg1_cntnr) : tmp;
    9183         8698 :           gcc_assert (se->ss->dimen == se->loop->dimen);
    9184              : 
    9185              :           /* Evaluate the bounds of the result, if known.  */
    9186         8698 :           gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
    9187              : 
    9188              :           /* If the lhs of an assignment x = f(..) is allocatable and
    9189              :              f2003 is allowed, we must not generate the function call
    9190              :              here but should just send back the results of the mapping.
    9191              :              This is signalled by the function ss being flagged.  */
    9192         8698 :           if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
    9193              :             {
    9194            0 :               gfc_free_interface_mapping (&mapping);
    9195            0 :               return has_alternate_specifier;
    9196              :             }
    9197              : 
    9198              :           /* Create a temporary to store the result.  In case the function
    9199              :              returns a pointer, the temporary will be a shallow copy and
    9200              :              mustn't be deallocated.  */
    9201         8698 :           callee_alloc = sym->attr.allocatable || sym->attr.pointer;
    9202         8698 :           gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
    9203              :                                        tmp, NULL_TREE, false,
    9204              :                                        !sym->attr.pointer, callee_alloc,
    9205         8698 :                                        &se->ss->info->expr->where);
    9206              : 
    9207              :           /* Index a pointer result by the span the callee returns.  */
    9208         8698 :           if (sym->attr.pointer && VAR_P (info->descriptor))
    9209          356 :             GFC_DECL_PTR_ARRAY_P (info->descriptor) = 1;
    9210              : 
    9211              :           /* Pass the temporary as the first argument.  */
    9212         8698 :           result = info->descriptor;
    9213         8698 :           tmp = gfc_build_addr_expr (NULL_TREE, result);
    9214         8698 :           vec_safe_push (retargs, tmp);
    9215              :         }
    9216         3314 :       else if (ts.type == BT_CHARACTER)
    9217              :         {
    9218              :           /* Pass the string length.  */
    9219         3253 :           type = gfc_get_character_type (ts.kind, ts.u.cl);
    9220         3253 :           type = build_pointer_type (type);
    9221              : 
    9222              :           /* Emit a DECL_EXPR for the VLA type.  */
    9223         3253 :           tmp = TREE_TYPE (type);
    9224         3253 :           if (TYPE_SIZE (tmp)
    9225         3253 :               && TREE_CODE (TYPE_SIZE (tmp)) != INTEGER_CST)
    9226              :             {
    9227         1935 :               tmp = build_decl (input_location, TYPE_DECL, NULL_TREE, tmp);
    9228         1935 :               DECL_ARTIFICIAL (tmp) = 1;
    9229         1935 :               DECL_IGNORED_P (tmp) = 1;
    9230         1935 :               tmp = fold_build1_loc (input_location, DECL_EXPR,
    9231         1935 :                                      TREE_TYPE (tmp), tmp);
    9232         1935 :               gfc_add_expr_to_block (&se->pre, tmp);
    9233              :             }
    9234              : 
    9235              :           /* Return an address to a char[0:len-1]* temporary for
    9236              :              character pointers.  */
    9237         3253 :           if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
    9238          229 :                || (comp && (comp->attr.pointer || comp->attr.allocatable)))
    9239              :             {
    9240          648 :               var = gfc_create_var (type, "pstr");
    9241              : 
    9242          648 :               if ((!comp && sym->attr.allocatable)
    9243           21 :                   || (comp && comp->attr.allocatable))
    9244              :                 {
    9245          361 :                   gfc_add_modify (&se->pre, var,
    9246          361 :                                   fold_convert (TREE_TYPE (var),
    9247              :                                                 null_pointer_node));
    9248          361 :                   tmp = gfc_call_free (var);
    9249          361 :                   gfc_add_expr_to_block (&se->post, tmp);
    9250              :                 }
    9251              : 
    9252              :               /* Provide an address expression for the function arguments.  */
    9253          648 :               var = gfc_build_addr_expr (NULL_TREE, var);
    9254              :             }
    9255              :           else
    9256         2605 :             var = gfc_conv_string_tmp (se, type, len);
    9257              : 
    9258         3253 :           vec_safe_push (retargs, var);
    9259              :         }
    9260              :       else
    9261              :         {
    9262           61 :           gcc_assert (flag_f2c && ts.type == BT_COMPLEX);
    9263              : 
    9264           61 :           type = gfc_get_complex_type (ts.kind);
    9265           61 :           var = gfc_build_addr_expr (NULL_TREE, gfc_create_var (type, "cmplx"));
    9266           61 :           vec_safe_push (retargs, var);
    9267              :         }
    9268              : 
    9269              :       /* Add the string length to the argument list.  */
    9270        19125 :       if (ts.type == BT_CHARACTER && ts.deferred)
    9271              :         {
    9272          623 :           tmp = len;
    9273          623 :           if (!VAR_P (tmp))
    9274            0 :             tmp = gfc_evaluate_now (len, &se->pre);
    9275          623 :           TREE_STATIC (tmp) = 1;
    9276          623 :           gfc_add_modify (&se->pre, tmp,
    9277          623 :                           build_int_cst (TREE_TYPE (tmp), 0));
    9278          623 :           tmp = gfc_build_addr_expr (NULL_TREE, tmp);
    9279          623 :           vec_safe_push (retargs, tmp);
    9280              :         }
    9281        18502 :       else if (ts.type == BT_CHARACTER)
    9282         4495 :         vec_safe_push (retargs, len);
    9283              :     }
    9284              : 
    9285       134061 :   gfc_free_interface_mapping (&mapping);
    9286              : 
    9287              :   /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS.  */
    9288       249295 :   arglen = (vec_safe_length (arglist) + vec_safe_length (optionalargs)
    9289       159884 :             + vec_safe_length (stringargs) + vec_safe_length (append_args));
    9290       134061 :   vec_safe_reserve (retargs, arglen);
    9291              : 
    9292              :   /* Add the return arguments.  */
    9293       134061 :   vec_safe_splice (retargs, arglist);
    9294              : 
    9295              :   /* Add the hidden present status for optional+value to the arguments.  */
    9296       134061 :   vec_safe_splice (retargs, optionalargs);
    9297              : 
    9298              :   /* Add the hidden string length parameters to the arguments.  */
    9299       134061 :   vec_safe_splice (retargs, stringargs);
    9300              : 
    9301              :   /* We may want to append extra arguments here.  This is used e.g. for
    9302              :      calls to libgfortran_matmul_??, which need extra information.  */
    9303       134061 :   vec_safe_splice (retargs, append_args);
    9304              : 
    9305       134061 :   arglist = retargs;
    9306              : 
    9307              :   /* Generate the actual call.  */
    9308       134061 :   is_builtin = false;
    9309       134061 :   if (base_object == NULL_TREE)
    9310       133981 :     conv_function_val (se, &is_builtin, sym, expr, args);
    9311              :   else
    9312           80 :     conv_base_obj_fcn_val (se, base_object, expr);
    9313              : 
    9314              :   /* If there are alternate return labels, function type should be
    9315              :      integer.  Can't modify the type in place though, since it can be shared
    9316              :      with other functions.  For dummy arguments, the typing is done to
    9317              :      this result, even if it has to be repeated for each call.  */
    9318       134061 :   if (has_alternate_specifier
    9319       134061 :       && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
    9320              :     {
    9321            7 :       if (!sym->attr.dummy)
    9322              :         {
    9323            0 :           TREE_TYPE (sym->backend_decl)
    9324            0 :                 = build_function_type (integer_type_node,
    9325            0 :                       TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
    9326            0 :           se->expr = gfc_build_addr_expr (NULL_TREE, sym->backend_decl);
    9327              :         }
    9328              :       else
    9329            7 :         TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
    9330              :     }
    9331              : 
    9332       134061 :   fntype = TREE_TYPE (TREE_TYPE (se->expr));
    9333       134061 :   se->expr = build_call_vec (TREE_TYPE (fntype), se->expr, arglist);
    9334              : 
    9335       134061 :   if (is_builtin)
    9336          569 :     se->expr = update_builtin_function (se->expr, sym);
    9337              : 
    9338              :   /* Allocatable scalar function results must be freed and nullified
    9339              :      after use. This necessitates the creation of a temporary to
    9340              :      hold the result to prevent duplicate calls.  */
    9341       134061 :   symbol_attribute attr =  comp ? comp->attr : sym->attr;
    9342       134061 :   bool allocatable = attr.allocatable && !attr.dimension;
    9343       137563 :   gfc_symbol *der = comp ?
    9344         2060 :                     comp->ts.type == BT_DERIVED ? comp->ts.u.derived : NULL
    9345              :                          :
    9346       132001 :                     sym->ts.type == BT_DERIVED ? sym->ts.u.derived : NULL;
    9347         3502 :   bool finalizable = der != NULL && der->ns->proc_name
    9348         7001 :                             && gfc_is_finalizable (der, NULL);
    9349              : 
    9350       134061 :   if (!byref && finalizable)
    9351          188 :     gfc_finalize_tree_expr (se, der, attr, expr->rank);
    9352              : 
    9353       134061 :   if (!byref && sym->ts.type != BT_CHARACTER
    9354       114726 :       && allocatable && !finalizable)
    9355              :     {
    9356          236 :       tmp = gfc_create_var (TREE_TYPE (se->expr), NULL);
    9357          236 :       gfc_add_modify (&se->pre, tmp, se->expr);
    9358          236 :       se->expr = tmp;
    9359          236 :       tmp = gfc_call_free (tmp);
    9360          236 :       gfc_add_expr_to_block (&post, tmp);
    9361          236 :       gfc_add_modify (&post, se->expr, build_int_cst (TREE_TYPE (se->expr), 0));
    9362              :     }
    9363              : 
    9364              :   /* If we have a pointer function, but we don't want a pointer, e.g.
    9365              :      something like
    9366              :         x = f()
    9367              :      where f is pointer valued, we have to dereference the result.  */
    9368       134061 :   if (!se->want_pointer && !byref
    9369       114328 :       && ((!comp && (sym->attr.pointer || sym->attr.allocatable))
    9370         1718 :           || (comp && (comp->attr.pointer || comp->attr.allocatable))))
    9371          462 :     se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
    9372              : 
    9373              :   /* f2c calling conventions require a scalar default real function to
    9374              :      return a double precision result.  Convert this back to default
    9375              :      real.  We only care about the cases that can happen in Fortran 77.
    9376              :   */
    9377       134061 :   if (flag_f2c && sym->ts.type == BT_REAL
    9378           98 :       && sym->ts.kind == gfc_default_real_kind
    9379           74 :       && !sym->attr.pointer
    9380           55 :       && !sym->attr.allocatable
    9381           43 :       && !sym->attr.always_explicit)
    9382           43 :     se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
    9383              : 
    9384              :   /* A pure function may still have side-effects - it may modify its
    9385              :      parameters.  */
    9386       134061 :   TREE_SIDE_EFFECTS (se->expr) = 1;
    9387              : #if 0
    9388              :   if (!sym->attr.pure)
    9389              :     TREE_SIDE_EFFECTS (se->expr) = 1;
    9390              : #endif
    9391              : 
    9392       134061 :   if (byref)
    9393              :     {
    9394              :       /* Add the function call to the pre chain.  There is no expression.  */
    9395        19125 :       if (!se->no_function_call || call_needed_for_length)
    9396        19093 :         gfc_add_expr_to_block (&se->pre, se->expr);
    9397              : 
    9398        19125 :       se->expr = NULL_TREE;
    9399              : 
    9400        19125 :       if (!se->direct_byref)
    9401              :         {
    9402        12078 :           if ((sym->attr.dimension && !comp) || (comp && comp->attr.dimension))
    9403              :             {
    9404         8764 :               if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    9405              :                 {
    9406              :                   /* Check the data pointer hasn't been modified.  This would
    9407              :                      happen in a function returning a pointer.  */
    9408          251 :                   tmp = gfc_conv_descriptor_data_get (info->descriptor);
    9409          251 :                   tmp = fold_build2_loc (input_location, NE_EXPR,
    9410              :                                          logical_type_node,
    9411              :                                          tmp, info->data);
    9412          251 :                   gfc_trans_runtime_check (true, false, tmp, &se->pre, NULL,
    9413              :                                            gfc_msg_fault);
    9414              :                 }
    9415         8764 :               se->expr = info->descriptor;
    9416              :               /* Bundle in the string length.  */
    9417         8764 :               se->string_length = len;
    9418              : 
    9419         8764 :               if (finalizable)
    9420            6 :                 gfc_finalize_tree_expr (se, der, attr, expr->rank);
    9421              :             }
    9422         3314 :           else if (ts.type == BT_CHARACTER)
    9423              :             {
    9424              :               /* Dereference for character pointer results.  */
    9425         3253 :               if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
    9426          229 :                   || (comp && (comp->attr.pointer || comp->attr.allocatable)))
    9427          648 :                 se->expr = build_fold_indirect_ref_loc (input_location, var);
    9428              :               else
    9429         2605 :                 se->expr = var;
    9430              : 
    9431         3253 :               se->string_length = len;
    9432              :             }
    9433              :           else
    9434              :             {
    9435           61 :               gcc_assert (ts.type == BT_COMPLEX && flag_f2c);
    9436           61 :               se->expr = build_fold_indirect_ref_loc (input_location, var);
    9437              :             }
    9438              :         }
    9439              :     }
    9440              : 
    9441              :   /* Associate the rhs class object's meta-data with the result, when the
    9442              :      result is a temporary.  */
    9443       115239 :   if (args && args->expr && args->expr->ts.type == BT_CLASS
    9444         5285 :       && sym->ts.type == BT_CLASS && result != NULL_TREE && DECL_P (result)
    9445       134093 :       && !GFC_CLASS_TYPE_P (TREE_TYPE (result)))
    9446              :     {
    9447           32 :       gfc_se parmse;
    9448           32 :       gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (args->expr);
    9449              : 
    9450           32 :       gfc_init_se (&parmse, NULL);
    9451           32 :       parmse.data_not_needed = 1;
    9452           32 :       gfc_conv_expr (&parmse, class_expr);
    9453           32 :       if (!DECL_LANG_SPECIFIC (result))
    9454           32 :         gfc_allocate_lang_decl (result);
    9455           32 :       GFC_DECL_SAVED_DESCRIPTOR (result) = parmse.expr;
    9456           32 :       gfc_free_expr (class_expr);
    9457              :       /* -fcheck= can add diagnostic code, which has to be placed before
    9458              :          the call. */
    9459           32 :       if (parmse.pre.head != NULL)
    9460           12 :           gfc_add_expr_to_block (&se->pre, parmse.pre.head);
    9461           32 :       gcc_assert (parmse.post.head == NULL_TREE);
    9462              :     }
    9463              : 
    9464              :   /* Follow the function call with the argument post block.  */
    9465       134061 :   if (byref)
    9466              :     {
    9467              :       /* Transformational functions of derived types with allocatable
    9468              :          components must have the result allocatable components copied
    9469              :          BEFORE the argument post block is appended.  Copying the result
    9470              :          first, then freeing the argument, gives the correct order.  */
    9471        19125 :       arg = expr->value.function.actual;
    9472        19125 :       if (result && arg && expr->rank
    9473        14760 :           && isym && isym->transformational
    9474        13173 :           && isym->id != GFC_ISYM_REDUCE
    9475        13047 :           && arg->expr
    9476        12987 :           && arg->expr->ts.type == BT_DERIVED
    9477          271 :           && (arg->expr->ts.u.derived->attr.alloc_comp
    9478          223 :               || arg->expr->ts.u.derived->attr.pdt_type))
    9479              :         {
    9480           78 :           tree tmp2;
    9481              :           /* Copy the allocatable components.  We have to use a
    9482              :              temporary here to prevent source allocatable components
    9483              :              from being corrupted.  */
    9484           78 :           tmp2 = gfc_evaluate_now (result, &se->pre);
    9485              : 
    9486           78 :           tmp = gfc_copy_alloc_comp (arg->expr->ts.u.derived,
    9487              :                                      result, tmp2, expr->rank, 0);
    9488           78 :           gfc_add_expr_to_block (&se->pre, tmp);
    9489           78 :           tmp = gfc_copy_allocatable_data (result, tmp2, TREE_TYPE(tmp2),
    9490              :                                            expr->rank);
    9491           78 :           gfc_add_expr_to_block (&se->pre, tmp);
    9492              : 
    9493              :           /* Free the result PST components if not direct_byref.  */
    9494           78 :           if (!se->direct_byref && arg->expr->ts.u.derived->attr.pdt_type)
    9495              :             {
    9496            6 :               tmp = gfc_deallocate_pdt_comp (arg->expr->ts.u.derived, result,
    9497              :                                              expr->rank);
    9498            6 :               gfc_add_expr_to_block (&se->pre, tmp);
    9499              :             }
    9500              :           /* Finally free the temporary's data field.  */
    9501           78 :           tmp = gfc_conv_descriptor_data_get (tmp2);
    9502           78 :           tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
    9503              :                                             NULL_TREE, NULL_TREE, true,
    9504              :                                             NULL, GFC_CAF_COARRAY_NOCOARRAY);
    9505           78 :           gfc_add_expr_to_block (&se->pre, tmp);
    9506              :         }
    9507              : 
    9508        19125 :       gfc_add_block_to_block (&se->pre, &post);
    9509              :     }
    9510              :   else
    9511              :     {
    9512              :       /* For a function with a class array result, save the result as
    9513              :          a temporary, set the info fields needed by the scalarizer and
    9514              :          call the finalization function of the temporary. Note that the
    9515              :          nullification of allocatable components needed by the result
    9516              :          is done in gfc_trans_assignment_1.  */
    9517        35896 :       if (expr && (gfc_is_class_array_function (expr)
    9518        35562 :                    || gfc_is_alloc_class_scalar_function (expr))
    9519          890 :           && se->expr && GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
    9520       115814 :           && expr->must_finalize)
    9521              :         {
    9522              :           /* TODO Eliminate the doubling of temporaries.  This
    9523              :              one is necessary to ensure no memory leakage.  */
    9524          333 :           se->expr = gfc_evaluate_now (se->expr, &se->pre);
    9525              : 
    9526              :           /* Finalize the result, if necessary.  */
    9527          666 :           attr = expr->value.function.esym
    9528          333 :                  ? CLASS_DATA (expr->value.function.esym->result)->attr
    9529           14 :                  : CLASS_DATA (expr)->attr;
    9530          333 :           if (!((gfc_is_class_array_function (expr)
    9531          120 :                  || gfc_is_alloc_class_scalar_function (expr))
    9532          333 :                 && attr.pointer))
    9533          288 :             gfc_finalize_tree_expr (se, NULL, attr, expr->rank);
    9534              :         }
    9535       114936 :       gfc_add_block_to_block (&se->post, &post);
    9536              :     }
    9537              : 
    9538              :   return has_alternate_specifier;
    9539              : }
    9540              : 
    9541              : 
    9542              : /* Fill a character string with spaces.  */
    9543              : 
    9544              : static tree
    9545        31615 : fill_with_spaces (tree start, tree type, tree size)
    9546              : {
    9547        31615 :   stmtblock_t block, loop;
    9548        31615 :   tree i, el, exit_label, cond, tmp;
    9549              : 
    9550              :   /* For a simple char type, we can call memset().  */
    9551        31615 :   if (compare_tree_int (TYPE_SIZE_UNIT (type), 1) == 0)
    9552        52606 :     return build_call_expr_loc (input_location,
    9553              :                             builtin_decl_explicit (BUILT_IN_MEMSET),
    9554              :                             3, start,
    9555              :                             build_int_cst (gfc_get_int_type (gfc_c_int_kind),
    9556        26303 :                                            lang_hooks.to_target_charset (' ')),
    9557              :                                 fold_convert (size_type_node, size));
    9558              : 
    9559              :   /* Otherwise, we use a loop:
    9560              :         for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
    9561              :           *el = (type) ' ';
    9562              :    */
    9563              : 
    9564              :   /* Initialize variables.  */
    9565         5312 :   gfc_init_block (&block);
    9566         5312 :   i = gfc_create_var (sizetype, "i");
    9567         5312 :   gfc_add_modify (&block, i, fold_convert (sizetype, size));
    9568         5312 :   el = gfc_create_var (build_pointer_type (type), "el");
    9569         5312 :   gfc_add_modify (&block, el, fold_convert (TREE_TYPE (el), start));
    9570         5312 :   exit_label = gfc_build_label_decl (NULL_TREE);
    9571         5312 :   TREE_USED (exit_label) = 1;
    9572              : 
    9573              : 
    9574              :   /* Loop body.  */
    9575         5312 :   gfc_init_block (&loop);
    9576              : 
    9577              :   /* Exit condition.  */
    9578         5312 :   cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, i,
    9579              :                           build_zero_cst (sizetype));
    9580         5312 :   tmp = build1_v (GOTO_EXPR, exit_label);
    9581         5312 :   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
    9582              :                          build_empty_stmt (input_location));
    9583         5312 :   gfc_add_expr_to_block (&loop, tmp);
    9584              : 
    9585              :   /* Assignment.  */
    9586         5312 :   gfc_add_modify (&loop,
    9587              :                   fold_build1_loc (input_location, INDIRECT_REF, type, el),
    9588         5312 :                   build_int_cst (type, lang_hooks.to_target_charset (' ')));
    9589              : 
    9590              :   /* Increment loop variables.  */
    9591         5312 :   gfc_add_modify (&loop, i,
    9592              :                   fold_build2_loc (input_location, MINUS_EXPR, sizetype, i,
    9593         5312 :                                    TYPE_SIZE_UNIT (type)));
    9594         5312 :   gfc_add_modify (&loop, el,
    9595              :                   fold_build_pointer_plus_loc (input_location,
    9596         5312 :                                                el, TYPE_SIZE_UNIT (type)));
    9597              : 
    9598              :   /* Making the loop... actually loop!  */
    9599         5312 :   tmp = gfc_finish_block (&loop);
    9600         5312 :   tmp = build1_v (LOOP_EXPR, tmp);
    9601         5312 :   gfc_add_expr_to_block (&block, tmp);
    9602              : 
    9603              :   /* The exit label.  */
    9604         5312 :   tmp = build1_v (LABEL_EXPR, exit_label);
    9605         5312 :   gfc_add_expr_to_block (&block, tmp);
    9606              : 
    9607              : 
    9608         5312 :   return gfc_finish_block (&block);
    9609              : }
    9610              : 
    9611              : 
    9612              : /* Generate code to copy a string.  */
    9613              : 
    9614              : void
    9615        36880 : gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
    9616              :                        int dkind, tree slength, tree src, int skind)
    9617              : {
    9618        36880 :   tree tmp, dlen, slen;
    9619        36880 :   tree dsc;
    9620        36880 :   tree ssc;
    9621        36880 :   tree cond;
    9622        36880 :   tree cond2;
    9623        36880 :   tree tmp2;
    9624        36880 :   tree tmp3;
    9625        36880 :   tree tmp4;
    9626        36880 :   tree chartype;
    9627        36880 :   stmtblock_t tempblock;
    9628              : 
    9629        36880 :   gcc_assert (dkind == skind);
    9630              : 
    9631        36880 :   if (slength != NULL_TREE)
    9632              :     {
    9633        36880 :       slen = gfc_evaluate_now (fold_convert (gfc_charlen_type_node, slength), block);
    9634        36880 :       ssc = gfc_string_to_single_character (slen, src, skind);
    9635              :     }
    9636              :   else
    9637              :     {
    9638            0 :       slen = build_one_cst (gfc_charlen_type_node);
    9639            0 :       ssc =  src;
    9640              :     }
    9641              : 
    9642        36880 :   if (dlength != NULL_TREE)
    9643              :     {
    9644        36880 :       dlen = gfc_evaluate_now (fold_convert (gfc_charlen_type_node, dlength), block);
    9645        36880 :       dsc = gfc_string_to_single_character (dlen, dest, dkind);
    9646              :     }
    9647              :   else
    9648              :     {
    9649            0 :       dlen = build_one_cst (gfc_charlen_type_node);
    9650            0 :       dsc =  dest;
    9651              :     }
    9652              : 
    9653              :   /* Assign directly if the types are compatible.  */
    9654        36880 :   if (dsc != NULL_TREE && ssc != NULL_TREE
    9655        36880 :       && TREE_TYPE (dsc) == TREE_TYPE (ssc))
    9656              :     {
    9657         5265 :       gfc_add_modify (block, dsc, ssc);
    9658         5265 :       return;
    9659              :     }
    9660              : 
    9661              :   /* The string copy algorithm below generates code like
    9662              : 
    9663              :      if (destlen > 0)
    9664              :        {
    9665              :          if (srclen < destlen)
    9666              :            {
    9667              :              memmove (dest, src, srclen);
    9668              :              // Pad with spaces.
    9669              :              memset (&dest[srclen], ' ', destlen - srclen);
    9670              :            }
    9671              :          else
    9672              :            {
    9673              :              // Truncate if too long.
    9674              :              memmove (dest, src, destlen);
    9675              :            }
    9676              :        }
    9677              :   */
    9678              : 
    9679              :   /* Do nothing if the destination length is zero.  */
    9680        31615 :   cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node, dlen,
    9681        31615 :                           build_zero_cst (TREE_TYPE (dlen)));
    9682              : 
    9683              :   /* For non-default character kinds, we have to multiply the string
    9684              :      length by the base type size.  */
    9685        31615 :   chartype = gfc_get_char_type (dkind);
    9686        31615 :   slen = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (slen),
    9687              :                           slen,
    9688        31615 :                           fold_convert (TREE_TYPE (slen),
    9689              :                                         TYPE_SIZE_UNIT (chartype)));
    9690        31615 :   dlen = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (dlen),
    9691              :                           dlen,
    9692        31615 :                           fold_convert (TREE_TYPE (dlen),
    9693              :                                         TYPE_SIZE_UNIT (chartype)));
    9694              : 
    9695        31615 :   if (dlength && POINTER_TYPE_P (TREE_TYPE (dest)))
    9696        31567 :     dest = fold_convert (pvoid_type_node, dest);
    9697              :   else
    9698           48 :     dest = gfc_build_addr_expr (pvoid_type_node, dest);
    9699              : 
    9700        31615 :   if (slength && POINTER_TYPE_P (TREE_TYPE (src)))
    9701        31611 :     src = fold_convert (pvoid_type_node, src);
    9702              :   else
    9703            4 :     src = gfc_build_addr_expr (pvoid_type_node, src);
    9704              : 
    9705              :   /* Truncate string if source is too long.  */
    9706        31615 :   cond2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node, slen,
    9707              :                            dlen);
    9708              : 
    9709              :   /* Pre-evaluate pointers unless one of the IF arms will be optimized away.  */
    9710        31615 :   if (!CONSTANT_CLASS_P (cond2))
    9711              :     {
    9712         9808 :       dest = gfc_evaluate_now (dest, block);
    9713         9808 :       src = gfc_evaluate_now (src, block);
    9714              :     }
    9715              : 
    9716              :   /* Copy and pad with spaces.  */
    9717        31615 :   tmp3 = build_call_expr_loc (input_location,
    9718              :                               builtin_decl_explicit (BUILT_IN_MEMMOVE),
    9719              :                               3, dest, src,
    9720              :                               fold_convert (size_type_node, slen));
    9721              : 
    9722              :   /* Wstringop-overflow appears at -O3 even though this warning is not
    9723              :      explicitly available in fortran nor can it be switched off. If the
    9724              :      source length is a constant, its negative appears as a very large
    9725              :      positive number and triggers the warning in BUILTIN_MEMSET. Fixing
    9726              :      the result of the MINUS_EXPR suppresses this spurious warning.  */
    9727        31615 :   tmp = fold_build2_loc (input_location, MINUS_EXPR,
    9728        31615 :                          TREE_TYPE(dlen), dlen, slen);
    9729        31615 :   if (slength && TREE_CONSTANT (slength))
    9730        27903 :     tmp = gfc_evaluate_now (tmp, block);
    9731              : 
    9732        31615 :   tmp4 = fold_build_pointer_plus_loc (input_location, dest, slen);
    9733        31615 :   tmp4 = fill_with_spaces (tmp4, chartype, tmp);
    9734              : 
    9735        31615 :   gfc_init_block (&tempblock);
    9736        31615 :   gfc_add_expr_to_block (&tempblock, tmp3);
    9737        31615 :   gfc_add_expr_to_block (&tempblock, tmp4);
    9738        31615 :   tmp3 = gfc_finish_block (&tempblock);
    9739              : 
    9740              :   /* The truncated memmove if the slen >= dlen.  */
    9741        31615 :   tmp2 = build_call_expr_loc (input_location,
    9742              :                               builtin_decl_explicit (BUILT_IN_MEMMOVE),
    9743              :                               3, dest, src,
    9744              :                               fold_convert (size_type_node, dlen));
    9745              : 
    9746              :   /* The whole copy_string function is there.  */
    9747        31615 :   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
    9748              :                          tmp3, tmp2);
    9749        31615 :   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
    9750              :                          build_empty_stmt (input_location));
    9751        31615 :   gfc_add_expr_to_block (block, tmp);
    9752              : }
    9753              : 
    9754              : 
    9755              : /* Translate a statement function.
    9756              :    The value of a statement function reference is obtained by evaluating the
    9757              :    expression using the values of the actual arguments for the values of the
    9758              :    corresponding dummy arguments.  */
    9759              : 
    9760              : static void
    9761          269 : gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
    9762              : {
    9763          269 :   gfc_symbol *sym;
    9764          269 :   gfc_symbol *fsym;
    9765          269 :   gfc_formal_arglist *fargs;
    9766          269 :   gfc_actual_arglist *args;
    9767          269 :   gfc_se lse;
    9768          269 :   gfc_se rse;
    9769          269 :   gfc_saved_var *saved_vars;
    9770          269 :   tree *temp_vars;
    9771          269 :   tree type;
    9772          269 :   tree tmp;
    9773          269 :   int n;
    9774              : 
    9775          269 :   sym = expr->symtree->n.sym;
    9776          269 :   args = expr->value.function.actual;
    9777          269 :   gfc_init_se (&lse, NULL);
    9778          269 :   gfc_init_se (&rse, NULL);
    9779              : 
    9780          269 :   n = 0;
    9781          727 :   for (fargs = gfc_sym_get_dummy_args (sym); fargs; fargs = fargs->next)
    9782          458 :     n++;
    9783          269 :   saved_vars = XCNEWVEC (gfc_saved_var, n);
    9784          269 :   temp_vars = XCNEWVEC (tree, n);
    9785              : 
    9786          727 :   for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
    9787          458 :        fargs = fargs->next, n++)
    9788              :     {
    9789              :       /* Each dummy shall be specified, explicitly or implicitly, to be
    9790              :          scalar.  */
    9791          458 :       gcc_assert (fargs->sym->attr.dimension == 0);
    9792          458 :       fsym = fargs->sym;
    9793              : 
    9794          458 :       if (fsym->ts.type == BT_CHARACTER)
    9795              :         {
    9796              :           /* Copy string arguments.  */
    9797           48 :           tree arglen;
    9798              : 
    9799           48 :           gcc_assert (fsym->ts.u.cl && fsym->ts.u.cl->length
    9800              :                       && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT);
    9801              : 
    9802              :           /* Create a temporary to hold the value.  */
    9803           48 :           if (fsym->ts.u.cl->backend_decl == NULL_TREE)
    9804            1 :              fsym->ts.u.cl->backend_decl
    9805            1 :                 = gfc_conv_constant_to_tree (fsym->ts.u.cl->length);
    9806              : 
    9807           48 :           type = gfc_get_character_type (fsym->ts.kind, fsym->ts.u.cl);
    9808           48 :           temp_vars[n] = gfc_create_var (type, fsym->name);
    9809              : 
    9810           48 :           arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
    9811              : 
    9812           48 :           gfc_conv_expr (&rse, args->expr);
    9813           48 :           gfc_conv_string_parameter (&rse);
    9814           48 :           gfc_add_block_to_block (&se->pre, &lse.pre);
    9815           48 :           gfc_add_block_to_block (&se->pre, &rse.pre);
    9816              : 
    9817           48 :           gfc_trans_string_copy (&se->pre, arglen, temp_vars[n], fsym->ts.kind,
    9818              :                                  rse.string_length, rse.expr, fsym->ts.kind);
    9819           48 :           gfc_add_block_to_block (&se->pre, &lse.post);
    9820           48 :           gfc_add_block_to_block (&se->pre, &rse.post);
    9821              :         }
    9822              :       else
    9823              :         {
    9824              :           /* For everything else, just evaluate the expression.  */
    9825              : 
    9826              :           /* Create a temporary to hold the value.  */
    9827          410 :           type = gfc_typenode_for_spec (&fsym->ts);
    9828          410 :           temp_vars[n] = gfc_create_var (type, fsym->name);
    9829              : 
    9830          410 :           gfc_conv_expr (&lse, args->expr);
    9831              : 
    9832          410 :           gfc_add_block_to_block (&se->pre, &lse.pre);
    9833          410 :           gfc_add_modify (&se->pre, temp_vars[n], lse.expr);
    9834          410 :           gfc_add_block_to_block (&se->pre, &lse.post);
    9835              :         }
    9836              : 
    9837          458 :       args = args->next;
    9838              :     }
    9839              : 
    9840              :   /* Use the temporary variables in place of the real ones.  */
    9841          727 :   for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
    9842          458 :        fargs = fargs->next, n++)
    9843          458 :     gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
    9844              : 
    9845          269 :   gfc_conv_expr (se, sym->value);
    9846              : 
    9847          269 :   if (sym->ts.type == BT_CHARACTER)
    9848              :     {
    9849           55 :       gfc_conv_const_charlen (sym->ts.u.cl);
    9850              : 
    9851              :       /* Force the expression to the correct length.  */
    9852           55 :       if (!INTEGER_CST_P (se->string_length)
    9853          101 :           || tree_int_cst_lt (se->string_length,
    9854           46 :                               sym->ts.u.cl->backend_decl))
    9855              :         {
    9856           31 :           type = gfc_get_character_type (sym->ts.kind, sym->ts.u.cl);
    9857           31 :           tmp = gfc_create_var (type, sym->name);
    9858           31 :           tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
    9859           31 :           gfc_trans_string_copy (&se->pre, sym->ts.u.cl->backend_decl, tmp,
    9860              :                                  sym->ts.kind, se->string_length, se->expr,
    9861              :                                  sym->ts.kind);
    9862           31 :           se->expr = tmp;
    9863              :         }
    9864           55 :       se->string_length = sym->ts.u.cl->backend_decl;
    9865              :     }
    9866              : 
    9867              :   /* Restore the original variables.  */
    9868          727 :   for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
    9869          458 :        fargs = fargs->next, n++)
    9870          458 :     gfc_restore_sym (fargs->sym, &saved_vars[n]);
    9871          269 :   free (temp_vars);
    9872          269 :   free (saved_vars);
    9873          269 : }
    9874              : 
    9875              : 
    9876              : /* Translate a function expression.  */
    9877              : 
    9878              : static void
    9879       323277 : gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
    9880              : {
    9881       323277 :   gfc_symbol *sym;
    9882              : 
    9883       323277 :   if (expr->value.function.isym)
    9884              :     {
    9885       271371 :       gfc_conv_intrinsic_function (se, expr);
    9886       271371 :       return;
    9887              :     }
    9888              : 
    9889              :   /* expr.value.function.esym is the resolved (specific) function symbol for
    9890              :      most functions.  However this isn't set for dummy procedures.  */
    9891        51906 :   sym = expr->value.function.esym;
    9892        51906 :   if (!sym)
    9893         1688 :     sym = expr->symtree->n.sym;
    9894              : 
    9895              :   /* The IEEE_ARITHMETIC functions are caught here. */
    9896        51906 :   if (sym->from_intmod == INTMOD_IEEE_ARITHMETIC)
    9897        13939 :     if (gfc_conv_ieee_arithmetic_function (se, expr))
    9898              :       return;
    9899              : 
    9900              :   /* We distinguish statement functions from general functions to improve
    9901              :      runtime performance.  */
    9902        39449 :   if (sym->attr.proc == PROC_ST_FUNCTION)
    9903              :     {
    9904          269 :       gfc_conv_statement_function (se, expr);
    9905          269 :       return;
    9906              :     }
    9907              : 
    9908        39180 :   gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
    9909              :                            NULL);
    9910              : }
    9911              : 
    9912              : 
    9913              : /* Determine whether the given EXPR_CONSTANT is a zero initializer.  */
    9914              : 
    9915              : static bool
    9916        40762 : is_zero_initializer_p (gfc_expr * expr)
    9917              : {
    9918        40762 :   if (expr->expr_type != EXPR_CONSTANT)
    9919              :     return false;
    9920              : 
    9921              :   /* We ignore constants with prescribed memory representations for now.  */
    9922        11698 :   if (expr->representation.string)
    9923              :     return false;
    9924              : 
    9925        11680 :   switch (expr->ts.type)
    9926              :     {
    9927         5431 :     case BT_INTEGER:
    9928         5431 :       return mpz_cmp_si (expr->value.integer, 0) == 0;
    9929              : 
    9930         4857 :     case BT_REAL:
    9931         4857 :       return mpfr_zero_p (expr->value.real)
    9932         4857 :              && MPFR_SIGN (expr->value.real) >= 0;
    9933              : 
    9934          931 :     case BT_LOGICAL:
    9935          931 :       return expr->value.logical == 0;
    9936              : 
    9937          243 :     case BT_COMPLEX:
    9938          243 :       return mpfr_zero_p (mpc_realref (expr->value.complex))
    9939          155 :              && MPFR_SIGN (mpc_realref (expr->value.complex)) >= 0
    9940          155 :              && mpfr_zero_p (mpc_imagref (expr->value.complex))
    9941          386 :              && MPFR_SIGN (mpc_imagref (expr->value.complex)) >= 0;
    9942              : 
    9943              :     default:
    9944              :       break;
    9945              :     }
    9946              :   return false;
    9947              : }
    9948              : 
    9949              : 
    9950              : static void
    9951        37073 : gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
    9952              : {
    9953        37073 :   gfc_ss *ss;
    9954              : 
    9955        37073 :   ss = se->ss;
    9956        37073 :   gcc_assert (ss != NULL && ss != gfc_ss_terminator);
    9957        37073 :   gcc_assert (ss->info->expr == expr && ss->info->type == GFC_SS_CONSTRUCTOR);
    9958              : 
    9959        37073 :   gfc_conv_tmp_array_ref (se);
    9960        37073 : }
    9961              : 
    9962              : 
    9963              : /* Build a static initializer.  EXPR is the expression for the initial value.
    9964              :    The other parameters describe the variable of the component being
    9965              :    initialized. EXPR may be null.  */
    9966              : 
    9967              : tree
    9968       142972 : gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
    9969              :                       bool array, bool pointer, bool procptr)
    9970              : {
    9971       142972 :   gfc_se se;
    9972              : 
    9973       142972 :   if (flag_coarray != GFC_FCOARRAY_LIB && ts->type == BT_DERIVED
    9974        45090 :       && ts->u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    9975          171 :       && ts->u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
    9976           59 :     return build_constructor (type, NULL);
    9977              : 
    9978       142913 :   if (!(expr || pointer || procptr))
    9979              :     return NULL_TREE;
    9980              : 
    9981              :   /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
    9982              :      (these are the only two iso_c_binding derived types that can be
    9983              :      used as initialization expressions).  If so, we need to modify
    9984              :      the 'expr' to be that for a (void *).  */
    9985       134388 :   if (expr != NULL && expr->ts.type == BT_DERIVED
    9986        40735 :       && expr->ts.is_iso_c && expr->ts.u.derived)
    9987              :     {
    9988          186 :       if (TREE_CODE (type) == ARRAY_TYPE)
    9989            4 :         return build_constructor (type, NULL);
    9990          182 :       else if (POINTER_TYPE_P (type))
    9991          182 :         return build_int_cst (type, 0);
    9992              :       else
    9993            0 :         gcc_unreachable ();
    9994              :     }
    9995              : 
    9996       134202 :   if (array && !procptr)
    9997              :     {
    9998         9221 :       tree ctor;
    9999              :       /* Arrays need special handling.  */
   10000         9221 :       if (pointer)
   10001          821 :         ctor = gfc_build_null_descriptor (type);
   10002              :       /* Special case assigning an array to zero.  */
   10003         8400 :       else if (is_zero_initializer_p (expr))
   10004          226 :         ctor = build_constructor (type, NULL);
   10005              :       else
   10006         8174 :         ctor = gfc_conv_array_initializer (type, expr);
   10007         9221 :       TREE_STATIC (ctor) = 1;
   10008         9221 :       return ctor;
   10009              :     }
   10010       124981 :   else if (pointer || procptr)
   10011              :     {
   10012        58777 :       if (ts->type == BT_CLASS && !procptr)
   10013              :         {
   10014         1822 :           gfc_init_se (&se, NULL);
   10015         1822 :           gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
   10016         1822 :           gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
   10017         1822 :           TREE_STATIC (se.expr) = 1;
   10018         1822 :           return se.expr;
   10019              :         }
   10020        56955 :       else if (!expr || expr->expr_type == EXPR_NULL)
   10021        30341 :         return fold_convert (type, null_pointer_node);
   10022              :       else
   10023              :         {
   10024        26614 :           gfc_init_se (&se, NULL);
   10025        26614 :           se.want_pointer = 1;
   10026        26614 :           gfc_conv_expr (&se, expr);
   10027        26614 :           gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
   10028              :           return se.expr;
   10029              :         }
   10030              :     }
   10031              :   else
   10032              :     {
   10033        66204 :       switch (ts->type)
   10034              :         {
   10035        19674 :         case_bt_struct:
   10036        19674 :         case BT_CLASS:
   10037        19674 :           gfc_init_se (&se, NULL);
   10038        19674 :           if (ts->type == BT_CLASS && expr->expr_type == EXPR_NULL)
   10039          889 :             gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
   10040              :           else
   10041        18785 :             gfc_conv_structure (&se, expr, 1);
   10042        19674 :           gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
   10043        19674 :           TREE_STATIC (se.expr) = 1;
   10044        19674 :           return se.expr;
   10045              : 
   10046         2812 :         case BT_CHARACTER:
   10047         2812 :           if (expr->expr_type == EXPR_CONSTANT)
   10048              :             {
   10049         2811 :               tree ctor = gfc_conv_string_init (ts->u.cl->backend_decl, expr);
   10050         2811 :               TREE_STATIC (ctor) = 1;
   10051         2811 :               return ctor;
   10052              :             }
   10053              : 
   10054              :           /* Fallthrough.  */
   10055        43719 :         default:
   10056        43719 :           gfc_init_se (&se, NULL);
   10057        43719 :           gfc_conv_constant (&se, expr);
   10058        43719 :           gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
   10059              :           return se.expr;
   10060              :         }
   10061              :     }
   10062              : }
   10063              : 
   10064              : static tree
   10065         1094 : gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
   10066              : {
   10067         1094 :   gfc_se rse;
   10068         1094 :   gfc_se lse;
   10069         1094 :   gfc_ss *rss;
   10070         1094 :   gfc_ss *lss;
   10071         1094 :   gfc_array_info *lss_array;
   10072         1094 :   stmtblock_t body;
   10073         1094 :   stmtblock_t block;
   10074         1094 :   gfc_loopinfo loop;
   10075         1094 :   int n;
   10076         1094 :   tree tmp;
   10077              : 
   10078         1094 :   gfc_start_block (&block);
   10079              : 
   10080              :   /* Initialize the scalarizer.  */
   10081         1094 :   gfc_init_loopinfo (&loop);
   10082              : 
   10083         1094 :   gfc_init_se (&lse, NULL);
   10084         1094 :   gfc_init_se (&rse, NULL);
   10085              : 
   10086              :   /* Walk the rhs.  */
   10087         1094 :   rss = gfc_walk_expr (expr);
   10088         1094 :   if (rss == gfc_ss_terminator)
   10089              :     /* The rhs is scalar.  Add a ss for the expression.  */
   10090          310 :     rss = gfc_get_scalar_ss (gfc_ss_terminator, expr);
   10091              : 
   10092              :   /* Create a SS for the destination.  */
   10093         1094 :   lss = gfc_get_array_ss (gfc_ss_terminator, NULL, cm->as->rank,
   10094              :                           GFC_SS_COMPONENT);
   10095         1094 :   lss_array = &lss->info->data.array;
   10096         1094 :   lss_array->shape = gfc_get_shape (cm->as->rank);
   10097         1094 :   lss_array->descriptor = dest;
   10098         1094 :   lss_array->data = gfc_conv_array_data (dest);
   10099         1094 :   lss_array->offset = gfc_conv_array_offset (dest);
   10100         2263 :   for (n = 0; n < cm->as->rank; n++)
   10101              :     {
   10102         1169 :       lss_array->start[n] = gfc_conv_array_lbound (dest, n);
   10103         1169 :       lss_array->stride[n] = gfc_index_one_node;
   10104              : 
   10105         1169 :       mpz_init (lss_array->shape[n]);
   10106         1169 :       mpz_sub (lss_array->shape[n], cm->as->upper[n]->value.integer,
   10107         1169 :                cm->as->lower[n]->value.integer);
   10108         1169 :       mpz_add_ui (lss_array->shape[n], lss_array->shape[n], 1);
   10109              :     }
   10110              : 
   10111              :   /* Associate the SS with the loop.  */
   10112         1094 :   gfc_add_ss_to_loop (&loop, lss);
   10113         1094 :   gfc_add_ss_to_loop (&loop, rss);
   10114              : 
   10115              :   /* Calculate the bounds of the scalarization.  */
   10116         1094 :   gfc_conv_ss_startstride (&loop);
   10117              : 
   10118              :   /* Setup the scalarizing loops.  */
   10119         1094 :   gfc_conv_loop_setup (&loop, &expr->where);
   10120              : 
   10121              :   /* Setup the gfc_se structures.  */
   10122         1094 :   gfc_copy_loopinfo_to_se (&lse, &loop);
   10123         1094 :   gfc_copy_loopinfo_to_se (&rse, &loop);
   10124              : 
   10125         1094 :   rse.ss = rss;
   10126         1094 :   gfc_mark_ss_chain_used (rss, 1);
   10127         1094 :   lse.ss = lss;
   10128         1094 :   gfc_mark_ss_chain_used (lss, 1);
   10129              : 
   10130              :   /* Start the scalarized loop body.  */
   10131         1094 :   gfc_start_scalarized_body (&loop, &body);
   10132              : 
   10133         1094 :   gfc_conv_tmp_array_ref (&lse);
   10134         1094 :   if (cm->ts.type == BT_CHARACTER)
   10135          206 :     lse.string_length = cm->ts.u.cl->backend_decl;
   10136              : 
   10137         1094 :   gfc_conv_expr (&rse, expr);
   10138              : 
   10139         1094 :   tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false);
   10140         1094 :   gfc_add_expr_to_block (&body, tmp);
   10141              : 
   10142         1094 :   gcc_assert (rse.ss == gfc_ss_terminator);
   10143              : 
   10144              :   /* Generate the copying loops.  */
   10145         1094 :   gfc_trans_scalarizing_loops (&loop, &body);
   10146              : 
   10147              :   /* Wrap the whole thing up.  */
   10148         1094 :   gfc_add_block_to_block (&block, &loop.pre);
   10149         1094 :   gfc_add_block_to_block (&block, &loop.post);
   10150              : 
   10151         1094 :   gcc_assert (lss_array->shape != NULL);
   10152         1094 :   gfc_free_shape (&lss_array->shape, cm->as->rank);
   10153         1094 :   gfc_cleanup_loop (&loop);
   10154              : 
   10155         1094 :   return gfc_finish_block (&block);
   10156              : }
   10157              : 
   10158              : 
   10159              : static stmtblock_t *final_block;
   10160              : 
   10161              : 
   10162              : /* Get the address of element index of contiguous character array data whose elements
   10163              :    are len characters of ksize bytes each.  */
   10164              : 
   10165              : static tree
   10166          196 : gfc_char_elem_addr (tree char_ptr, tree data, tree idx, tree len, tree ksize)
   10167              : {
   10168          196 :   tree offset = fold_build2_loc (input_location, MULT_EXPR,
   10169              :                                  gfc_array_index_type, len, ksize);
   10170          196 :   offset = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
   10171              :                             idx, offset);
   10172          196 :   return fold_build_pointer_plus_loc (input_location,
   10173          196 :                                       fold_convert (char_ptr, data), offset);
   10174              : }
   10175              : 
   10176              : 
   10177              : /* Copy a deferred-shape allocatable character array component in a structure
   10178              :    constructor when the source element length (SRC_LEN) may differ from the
   10179              :    component's declared length.  Like gfc_duplicate_allocatable, a
   10180              :    contiguous source layout is assumed.  DEST and SRC are array descriptors;
   10181              :    DEST already carries the source's bounds.  */
   10182              : 
   10183              : static tree
   10184           98 : gfc_trans_alloc_char_subarray_assign (tree dest, gfc_component *cm, tree src,
   10185              :                                       tree src_len, int rank)
   10186              : {
   10187           98 :   stmtblock_t block, body;
   10188           98 :   tree dlen, slen, ksize, nelems, idx, size, tmp, pchar, cond;
   10189              : 
   10190           98 :   gfc_init_block (&block);
   10191              : 
   10192           98 :   pchar = gfc_get_pchar_type (cm->ts.kind);
   10193           98 :   ksize = fold_convert (gfc_array_index_type,
   10194              :                         TYPE_SIZE_UNIT (gfc_get_char_type (cm->ts.kind)));
   10195           98 :   dlen = fold_convert (gfc_array_index_type, cm->ts.u.cl->backend_decl);
   10196           98 :   slen = fold_convert (gfc_array_index_type, src_len);
   10197           98 :   nelems = gfc_full_array_size (&block, src, rank);
   10198           98 :   nelems = gfc_evaluate_now (nelems, &block);
   10199              : 
   10200              :   /* Allocate the destination data: nelems elements of the component length.  */
   10201           98 :   size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
   10202              :                           nelems, dlen);
   10203           98 :   size = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
   10204              :                           size, ksize);
   10205           98 :   tmp = GFC_TYPE_ARRAY_DATAPTR_TYPE (TREE_TYPE (dest));
   10206           98 :   gfc_conv_descriptor_data_set (&block, dest,
   10207              :                                 gfc_call_malloc (&block, tmp, size));
   10208              : 
   10209              :   /* Copy element IDX, padding or truncating to the component length.  */
   10210           98 :   idx = gfc_create_var (gfc_array_index_type, "idx");
   10211           98 :   gfc_init_block (&body);
   10212           98 :   gfc_trans_string_copy (&body, cm->ts.u.cl->backend_decl,
   10213              :                          gfc_char_elem_addr (pchar,
   10214              :                                              gfc_conv_descriptor_data_get (dest),
   10215              :                                              idx, dlen, ksize),
   10216              :                          cm->ts.kind, src_len,
   10217              :                          gfc_char_elem_addr (pchar,
   10218              :                                              gfc_conv_descriptor_data_get (src),
   10219              :                                              idx, slen, ksize),
   10220              :                          cm->ts.kind);
   10221           98 :   gfc_simple_for_loop (&block, idx, gfc_index_zero_node, nelems, LT_EXPR,
   10222              :                        gfc_index_one_node, gfc_finish_block (&body));
   10223              : 
   10224           98 :   tmp = gfc_finish_block (&block);
   10225              : 
   10226              :   /* Null the destination if the source is unallocated.  */
   10227           98 :   gfc_init_block (&body);
   10228           98 :   gfc_conv_descriptor_data_set (&body, dest, null_pointer_node);
   10229           98 :   cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
   10230              :                           fold_convert (pvoid_type_node,
   10231              :                                         gfc_conv_descriptor_data_get (src)),
   10232              :                           null_pointer_node);
   10233           98 :   return build3_v (COND_EXPR, cond, tmp, gfc_finish_block (&body));
   10234              : }
   10235              : 
   10236              : 
   10237              : static tree
   10238         1408 : gfc_trans_alloc_subarray_assign (tree dest, gfc_component * cm,
   10239              :                                  gfc_expr * expr)
   10240              : {
   10241         1408 :   gfc_se se;
   10242         1408 :   stmtblock_t block;
   10243         1408 :   tree offset;
   10244         1408 :   int n;
   10245         1408 :   tree tmp;
   10246         1408 :   tree tmp2;
   10247         1408 :   gfc_array_spec *as;
   10248         1408 :   gfc_expr *arg = NULL;
   10249              : 
   10250         1408 :   gfc_start_block (&block);
   10251         1408 :   gfc_init_se (&se, NULL);
   10252              : 
   10253              :   /* Get the descriptor for the expressions.  */
   10254         1408 :   se.want_pointer = 0;
   10255         1408 :   gfc_conv_expr_descriptor (&se, expr);
   10256         1408 :   gfc_add_block_to_block (&block, &se.pre);
   10257         1408 :   gfc_add_modify (&block, dest, se.expr);
   10258         1408 :   if (cm->ts.type == BT_CHARACTER
   10259         1408 :       && gfc_deferred_strlen (cm, &tmp))
   10260              :     {
   10261           30 :       tmp = fold_build3_loc (input_location, COMPONENT_REF,
   10262           30 :                              TREE_TYPE (tmp),
   10263           30 :                              TREE_OPERAND (dest, 0),
   10264              :                              tmp, NULL_TREE);
   10265           30 :       gfc_add_modify (&block, tmp,
   10266           30 :                               fold_convert (TREE_TYPE (tmp),
   10267              :                               se.string_length));
   10268           30 :       cm->ts.u.cl->backend_decl = gfc_create_var (gfc_charlen_type_node,
   10269              :                                                   "slen");
   10270           30 :       gfc_add_modify (&block, cm->ts.u.cl->backend_decl, se.string_length);
   10271              :     }
   10272              : 
   10273              :   /* Deal with arrays of derived types with allocatable components.  */
   10274         1408 :   if (gfc_bt_struct (cm->ts.type)
   10275          199 :         && cm->ts.u.derived->attr.alloc_comp)
   10276              :     // TODO: Fix caf_mode
   10277          113 :     tmp = gfc_copy_alloc_comp (cm->ts.u.derived,
   10278              :                                se.expr, dest,
   10279          113 :                                cm->as->rank, 0);
   10280         1295 :   else if (cm->ts.type == BT_CLASS && expr->ts.type == BT_DERIVED
   10281           36 :            && CLASS_DATA(cm)->attr.allocatable)
   10282              :     {
   10283           36 :       if (cm->ts.u.derived->attr.alloc_comp)
   10284              :         // TODO: Fix caf_mode
   10285            0 :         tmp = gfc_copy_alloc_comp (expr->ts.u.derived,
   10286              :                                    se.expr, dest,
   10287              :                                    expr->rank, 0);
   10288              :       else
   10289              :         {
   10290           36 :           tmp = TREE_TYPE (dest);
   10291           36 :           tmp = gfc_duplicate_allocatable (dest, se.expr,
   10292              :                                            tmp, expr->rank, NULL_TREE);
   10293              :         }
   10294              :     }
   10295         1259 :   else if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
   10296           30 :     tmp = gfc_duplicate_allocatable (dest, se.expr,
   10297              :                                      gfc_typenode_for_spec (&cm->ts),
   10298           30 :                                      cm->as->rank, NULL_TREE);
   10299         1229 :   else if (cm->ts.type == BT_CHARACTER)
   10300              :     /* Explicit-length character: the source element length may differ from
   10301              :        the component length, so a bitwise duplicate would copy the wrong
   10302              :        bytes.  Copy element by element with padding/truncation.  */
   10303           98 :     tmp = gfc_trans_alloc_char_subarray_assign (dest, cm, se.expr,
   10304              :                                                 se.string_length,
   10305           98 :                                                 cm->as->rank);
   10306              :   else
   10307         1131 :     tmp = gfc_duplicate_allocatable (dest, se.expr,
   10308         1131 :                                      TREE_TYPE(cm->backend_decl),
   10309         1131 :                                      cm->as->rank, NULL_TREE);
   10310              : 
   10311              : 
   10312         1408 :   gfc_add_expr_to_block (&block, tmp);
   10313         1408 :   gfc_add_block_to_block (&block, &se.post);
   10314              : 
   10315         1408 :   if (final_block && !cm->attr.allocatable
   10316          108 :       && expr->expr_type == EXPR_ARRAY)
   10317              :     {
   10318          108 :       tree data_ptr;
   10319          108 :       data_ptr = gfc_conv_descriptor_data_get (dest);
   10320          108 :       gfc_add_expr_to_block (final_block, gfc_call_free (data_ptr));
   10321          108 :     }
   10322         1300 :   else if (final_block && cm->attr.allocatable)
   10323          162 :     gfc_add_block_to_block (final_block, &se.finalblock);
   10324              : 
   10325         1408 :   if (expr->expr_type != EXPR_VARIABLE)
   10326              :     {
   10327         1269 :       if (gfc_bt_struct (cm->ts.type) && cm->ts.u.derived->attr.alloc_comp)
   10328              :         {
   10329          214 :           tmp = gfc_deallocate_alloc_comp_no_caf (cm->ts.u.derived,
   10330          107 :                                                   se.expr, cm->as->rank, true);
   10331          107 :           gfc_add_expr_to_block (&block, tmp);
   10332              :         }
   10333         1269 :       gfc_conv_descriptor_data_set (&block, se.expr, null_pointer_node);
   10334              :     }
   10335              : 
   10336              :   /* We need to know if the argument of a conversion function is a
   10337              :      variable, so that the correct lower bound can be used.  */
   10338         1408 :   if (expr->expr_type == EXPR_FUNCTION
   10339           68 :         && expr->value.function.isym
   10340           56 :         && expr->value.function.isym->conversion
   10341           56 :         && expr->value.function.actual->expr
   10342           56 :         && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE)
   10343           56 :     arg = expr->value.function.actual->expr;
   10344              : 
   10345              :   /* Obtain the array spec of full array references.  */
   10346           56 :   if (arg)
   10347           56 :     as = gfc_get_full_arrayspec_from_expr (arg);
   10348              :   else
   10349         1352 :     as = gfc_get_full_arrayspec_from_expr (expr);
   10350              : 
   10351              :   /* Shift the lbound and ubound of temporaries to being unity,
   10352              :      rather than zero, based. Always calculate the offset.  */
   10353         1408 :   gfc_conv_descriptor_offset_set (&block, dest, gfc_index_zero_node);
   10354         1408 :   offset = gfc_conv_descriptor_offset_get (dest);
   10355         1408 :   tmp2 =gfc_create_var (gfc_array_index_type, NULL);
   10356              : 
   10357         4280 :   for (n = 0; n < expr->rank; n++)
   10358              :     {
   10359         1464 :       tree span;
   10360         1464 :       tree lbound;
   10361              : 
   10362              :       /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
   10363              :          TODO It looks as if gfc_conv_expr_descriptor should return
   10364              :          the correct bounds and that the following should not be
   10365              :          necessary.  This would simplify gfc_conv_intrinsic_bound
   10366              :          as well.  */
   10367         1464 :       if (as && as->lower[n])
   10368              :         {
   10369           92 :           gfc_se lbse;
   10370           92 :           gfc_init_se (&lbse, NULL);
   10371           92 :           gfc_conv_expr (&lbse, as->lower[n]);
   10372           92 :           gfc_add_block_to_block (&block, &lbse.pre);
   10373           92 :           lbound = gfc_evaluate_now (lbse.expr, &block);
   10374           92 :         }
   10375         1372 :       else if (as && arg)
   10376              :         {
   10377           34 :           tmp = gfc_get_symbol_decl (arg->symtree->n.sym);
   10378           34 :           lbound = gfc_conv_descriptor_lbound_get (tmp,
   10379              :                                         gfc_rank_cst[n]);
   10380              :         }
   10381         1338 :       else if (as)
   10382           82 :         lbound = gfc_conv_descriptor_lbound_get (dest,
   10383              :                                                 gfc_rank_cst[n]);
   10384              :       else
   10385         1256 :         lbound = gfc_index_one_node;
   10386              : 
   10387         1464 :       lbound = fold_convert (gfc_array_index_type, lbound);
   10388              : 
   10389              :       /* Shift the bounds and set the offset accordingly.  */
   10390         1464 :       tmp = gfc_conv_descriptor_ubound_get (dest, gfc_rank_cst[n]);
   10391         1464 :       span = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
   10392              :                 tmp, gfc_conv_descriptor_lbound_get (dest, gfc_rank_cst[n]));
   10393         1464 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
   10394              :                              span, lbound);
   10395         1464 :       gfc_conv_descriptor_ubound_set (&block, dest,
   10396              :                                       gfc_rank_cst[n], tmp);
   10397         1464 :       gfc_conv_descriptor_lbound_set (&block, dest,
   10398              :                                       gfc_rank_cst[n], lbound);
   10399              : 
   10400         1464 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
   10401              :                          gfc_conv_descriptor_lbound_get (dest,
   10402              :                                                          gfc_rank_cst[n]),
   10403              :                          gfc_conv_descriptor_stride_get (dest,
   10404              :                                                          gfc_rank_cst[n]));
   10405         1464 :       gfc_add_modify (&block, tmp2, tmp);
   10406         1464 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
   10407              :                              offset, tmp2);
   10408         1464 :       gfc_conv_descriptor_offset_set (&block, dest, tmp);
   10409              :     }
   10410              : 
   10411         1408 :   if (arg)
   10412              :     {
   10413              :       /* If a conversion expression has a null data pointer
   10414              :          argument, nullify the allocatable component.  */
   10415           56 :       tree non_null_expr;
   10416           56 :       tree null_expr;
   10417              : 
   10418           56 :       if (arg->symtree->n.sym->attr.allocatable
   10419           24 :             || arg->symtree->n.sym->attr.pointer)
   10420              :         {
   10421           32 :           non_null_expr = gfc_finish_block (&block);
   10422           32 :           gfc_start_block (&block);
   10423           32 :           gfc_conv_descriptor_data_set (&block, dest,
   10424              :                                         null_pointer_node);
   10425           32 :           null_expr = gfc_finish_block (&block);
   10426           32 :           tmp = gfc_conv_descriptor_data_get (arg->symtree->n.sym->backend_decl);
   10427           32 :           tmp = build2_loc (input_location, EQ_EXPR, logical_type_node, tmp,
   10428           32 :                             fold_convert (TREE_TYPE (tmp), null_pointer_node));
   10429           32 :           return build3_v (COND_EXPR, tmp,
   10430              :                            null_expr, non_null_expr);
   10431              :         }
   10432              :     }
   10433              : 
   10434         1376 :   return gfc_finish_block (&block);
   10435              : }
   10436              : 
   10437              : 
   10438              : /* Allocate or reallocate scalar component, as necessary.  */
   10439              : 
   10440              : static void
   10441          464 : alloc_scalar_allocatable_subcomponent (stmtblock_t *block, tree comp,
   10442              :                                        gfc_component *cm, gfc_expr *expr2,
   10443              :                                        tree slen)
   10444              : {
   10445          464 :   tree tmp;
   10446          464 :   tree ptr;
   10447          464 :   tree size;
   10448          464 :   tree size_in_bytes;
   10449          464 :   tree lhs_cl_size = NULL_TREE;
   10450          464 :   gfc_se se;
   10451              : 
   10452          464 :   if (!comp)
   10453            0 :     return;
   10454              : 
   10455          464 :   if (!expr2 || expr2->rank)
   10456              :     return;
   10457              : 
   10458          464 :   realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
   10459              : 
   10460          464 :   if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
   10461              :     {
   10462          145 :       gcc_assert (expr2->ts.type == BT_CHARACTER);
   10463          145 :       size = expr2->ts.u.cl->backend_decl;
   10464          145 :       if (!size || !VAR_P (size))
   10465          145 :         size = gfc_create_var (TREE_TYPE (slen), "slen");
   10466          145 :       gfc_add_modify (block, size, slen);
   10467              : 
   10468          145 :       gfc_deferred_strlen (cm, &tmp);
   10469          145 :       lhs_cl_size = fold_build3_loc (input_location, COMPONENT_REF,
   10470              :                                      gfc_charlen_type_node,
   10471          145 :                                      TREE_OPERAND (comp, 0),
   10472              :                                      tmp, NULL_TREE);
   10473              : 
   10474          145 :       tmp = TREE_TYPE (gfc_typenode_for_spec (&cm->ts));
   10475          145 :       tmp = TYPE_SIZE_UNIT (tmp);
   10476          290 :       size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
   10477          145 :                                        TREE_TYPE (tmp), tmp,
   10478          145 :                                        fold_convert (TREE_TYPE (tmp), size));
   10479              :     }
   10480          319 :   else if (cm->ts.type == BT_CLASS)
   10481              :     {
   10482          121 :       if (expr2->ts.type != BT_CLASS)
   10483              :         {
   10484          121 :           if (expr2->ts.type == BT_CHARACTER)
   10485              :             {
   10486           24 :               gfc_init_se (&se, NULL);
   10487           24 :               gfc_conv_expr (&se, expr2);
   10488           24 :               size = build_int_cst (gfc_charlen_type_node, expr2->ts.kind);
   10489           24 :               size = fold_build2_loc (input_location, MULT_EXPR,
   10490              :                                       gfc_charlen_type_node,
   10491              :                                       se.string_length, size);
   10492           24 :               size = fold_convert (size_type_node, size);
   10493              :             }
   10494              :           else
   10495              :             {
   10496           97 :               tmp = gfc_typenode_for_spec (&expr2->ts);
   10497           97 :               size = TYPE_SIZE_UNIT (tmp);
   10498              :             }
   10499              :         }
   10500              :       else
   10501              :         {
   10502            0 :           gfc_expr *e2vtab;
   10503            0 :           e2vtab = gfc_find_and_cut_at_last_class_ref (expr2);
   10504            0 :           gfc_add_vptr_component (e2vtab);
   10505            0 :           gfc_add_size_component (e2vtab);
   10506            0 :           gfc_init_se (&se, NULL);
   10507            0 :           gfc_conv_expr (&se, e2vtab);
   10508            0 :           gfc_add_block_to_block (block, &se.pre);
   10509            0 :           size = fold_convert (size_type_node, se.expr);
   10510            0 :           gfc_free_expr (e2vtab);
   10511              :         }
   10512              :       size_in_bytes = size;
   10513              :     }
   10514              :   else
   10515              :     {
   10516              :       /* Otherwise use the length in bytes of the rhs.  */
   10517          198 :       size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&cm->ts));
   10518          198 :       size_in_bytes = size;
   10519              :     }
   10520              : 
   10521          464 :   size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
   10522              :                                    size_in_bytes, size_one_node);
   10523              : 
   10524          464 :   if (cm->ts.type == BT_DERIVED && cm->ts.u.derived->attr.alloc_comp)
   10525              :     {
   10526            6 :       tmp = build_call_expr_loc (input_location,
   10527              :                                  builtin_decl_explicit (BUILT_IN_CALLOC),
   10528              :                                  2, build_one_cst (size_type_node),
   10529              :                                  size_in_bytes);
   10530            6 :       tmp = fold_convert (TREE_TYPE (comp), tmp);
   10531            6 :       gfc_add_modify (block, comp, tmp);
   10532              :     }
   10533              :   else
   10534              :     {
   10535          458 :       tmp = build_call_expr_loc (input_location,
   10536              :                                  builtin_decl_explicit (BUILT_IN_MALLOC),
   10537              :                                  1, size_in_bytes);
   10538          458 :       if (GFC_CLASS_TYPE_P (TREE_TYPE (comp)))
   10539          121 :         ptr = gfc_class_data_get (comp);
   10540              :       else
   10541              :         ptr = comp;
   10542          458 :       tmp = fold_convert (TREE_TYPE (ptr), tmp);
   10543          458 :       gfc_add_modify (block, ptr, tmp);
   10544              :     }
   10545              : 
   10546          464 :   if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
   10547              :     /* Update the lhs character length.  */
   10548          145 :     gfc_add_modify (block, lhs_cl_size,
   10549          145 :                     fold_convert (TREE_TYPE (lhs_cl_size), size));
   10550              : }
   10551              : 
   10552              : 
   10553              : /* Assign a single component of a derived type constructor.  */
   10554              : 
   10555              : static tree
   10556        32761 : gfc_trans_subcomponent_assign (tree dest, gfc_component * cm,
   10557              :                                gfc_expr * expr, bool init)
   10558              : {
   10559        32761 :   gfc_se se;
   10560        32761 :   gfc_se lse;
   10561        32761 :   stmtblock_t block;
   10562        32761 :   tree tmp;
   10563        32761 :   tree vtab;
   10564              : 
   10565        32761 :   gfc_start_block (&block);
   10566              : 
   10567        32761 :   if (cm->attr.pointer || cm->attr.proc_pointer)
   10568              :     {
   10569              :       /* Only care about pointers here, not about allocatables.  */
   10570         2807 :       gfc_init_se (&se, NULL);
   10571              :       /* Pointer component.  */
   10572         2807 :       if ((cm->attr.dimension || cm->attr.codimension)
   10573          688 :           && !cm->attr.proc_pointer)
   10574              :         {
   10575              :           /* Array pointer.  */
   10576          672 :           if (expr->expr_type == EXPR_NULL)
   10577          666 :             gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
   10578              :           else
   10579              :             {
   10580            6 :               se.direct_byref = 1;
   10581            6 :               se.expr = dest;
   10582            6 :               gfc_conv_expr_descriptor (&se, expr);
   10583            6 :               gfc_add_block_to_block (&block, &se.pre);
   10584            6 :               gfc_add_block_to_block (&block, &se.post);
   10585              :             }
   10586              :         }
   10587              :       else
   10588              :         {
   10589              :           /* Scalar pointers.  */
   10590         2135 :           se.want_pointer = 1;
   10591         2135 :           gfc_conv_expr (&se, expr);
   10592         2135 :           gfc_add_block_to_block (&block, &se.pre);
   10593              : 
   10594         2135 :           if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
   10595           12 :               && expr->symtree->n.sym->attr.dummy)
   10596           12 :             se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
   10597              : 
   10598         2135 :           gfc_add_modify (&block, dest,
   10599         2135 :                                fold_convert (TREE_TYPE (dest), se.expr));
   10600         2135 :           gfc_add_block_to_block (&block, &se.post);
   10601              :         }
   10602              :     }
   10603        29954 :   else if (cm->ts.type == BT_CLASS && expr->expr_type == EXPR_NULL)
   10604              :     {
   10605              :       /* NULL initialization for CLASS components.  */
   10606         1055 :       tmp = gfc_trans_structure_assign (dest,
   10607              :                                         gfc_class_initializer (&cm->ts, expr),
   10608              :                                         false);
   10609         1055 :       gfc_add_expr_to_block (&block, tmp);
   10610              :     }
   10611        28899 :   else if (cm->ts.type == BT_CLASS && CLASS_DATA (cm)->attr.class_pointer
   10612           30 :            && expr->ts.type != BT_CLASS)
   10613              :     {
   10614              :       /* Associate a CLASS pointer component with a non-class target.  */
   10615           30 :       gfc_init_se (&se, NULL);
   10616           30 :       if (CLASS_DATA (cm)->attr.dimension)
   10617              :         {
   10618           12 :           se.direct_byref = 1;
   10619           12 :           se.expr = gfc_class_data_get (dest);
   10620           12 :           gfc_conv_expr_descriptor (&se, expr);
   10621           12 :           gfc_add_block_to_block (&block, &se.pre);
   10622              :         }
   10623              :       else
   10624              :         {
   10625           18 :           gfc_conv_expr_reference (&se, expr);
   10626           18 :           gfc_add_block_to_block (&block, &se.pre);
   10627           18 :           tmp = gfc_class_data_get (dest);
   10628           18 :           gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), se.expr));
   10629              :         }
   10630           30 :       vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
   10631           30 :       vtab = gfc_build_addr_expr (NULL_TREE, vtab);
   10632           30 :       tmp = gfc_class_vptr_get (dest);
   10633           30 :       gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), vtab));
   10634           30 :       if (UNLIMITED_POLY (cm))
   10635              :         {
   10636           18 :           tmp = gfc_class_len_get (dest);
   10637           18 :           gfc_add_modify (&block, tmp,
   10638           18 :                           expr->ts.type == BT_CHARACTER
   10639           12 :                           ? fold_convert (TREE_TYPE (tmp), se.string_length)
   10640            6 :                           : build_zero_cst (TREE_TYPE (tmp)));
   10641              :         }
   10642           30 :       gfc_add_block_to_block (&block, &se.post);
   10643              :     }
   10644        28869 :   else if ((cm->attr.dimension || cm->attr.codimension)
   10645              :            && !cm->attr.proc_pointer)
   10646              :     {
   10647         5415 :       if (cm->attr.allocatable && expr->expr_type == EXPR_NULL)
   10648              :         {
   10649         2949 :           gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
   10650         2949 :           if (cm->attr.codimension && flag_coarray == GFC_FCOARRAY_LIB)
   10651            2 :             gfc_conv_descriptor_token_set (&block, dest, null_pointer_node);
   10652              :         }
   10653         2466 :       else if (cm->attr.allocatable || cm->attr.pdt_array)
   10654              :         {
   10655         1372 :           tmp = gfc_trans_alloc_subarray_assign (dest, cm, expr);
   10656         1372 :           gfc_add_expr_to_block (&block, tmp);
   10657              :         }
   10658              :       else
   10659              :         {
   10660         1094 :           tmp = gfc_trans_subarray_assign (dest, cm, expr);
   10661         1094 :           gfc_add_expr_to_block (&block, tmp);
   10662              :         }
   10663              :     }
   10664        23454 :   else if (cm->ts.type == BT_CLASS
   10665          169 :            && CLASS_DATA (cm)->attr.dimension
   10666           36 :            && CLASS_DATA (cm)->attr.allocatable
   10667           36 :            && expr->ts.type == BT_DERIVED)
   10668              :     {
   10669           36 :       vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
   10670           36 :       vtab = gfc_build_addr_expr (NULL_TREE, vtab);
   10671           36 :       tmp = gfc_class_vptr_get (dest);
   10672           36 :       gfc_add_modify (&block, tmp,
   10673           36 :                       fold_convert (TREE_TYPE (tmp), vtab));
   10674           36 :       tmp = gfc_class_data_get (dest);
   10675           36 :       tmp = gfc_trans_alloc_subarray_assign (tmp, cm, expr);
   10676           36 :       gfc_add_expr_to_block (&block, tmp);
   10677              :     }
   10678        23418 :   else if (cm->attr.allocatable && expr->expr_type == EXPR_NULL
   10679         1874 :            && (init
   10680         1747 :                || (cm->ts.type == BT_CHARACTER
   10681          131 :                    && !(cm->ts.deferred || cm->attr.pdt_string))))
   10682              :     {
   10683              :       /* NULL initialization for allocatable components.
   10684              :          Deferred-length character is dealt with later.  */
   10685          151 :       gfc_add_modify (&block, dest, fold_convert (TREE_TYPE (dest),
   10686              :                                                   null_pointer_node));
   10687              :     }
   10688        23267 :   else if (init && (cm->attr.allocatable
   10689        14588 :            || (cm->ts.type == BT_CLASS && CLASS_DATA (cm)->attr.allocatable
   10690          133 :                && expr->ts.type != BT_CLASS)))
   10691              :     {
   10692          464 :       tree size;
   10693          464 :       tree tmp2;
   10694              : 
   10695          464 :       gfc_init_se (&se, NULL);
   10696          464 :       gfc_conv_expr (&se, expr);
   10697              : 
   10698              :       /* The remainder of these instructions follow the if (cm->attr.pointer)
   10699              :          if (!cm->attr.dimension) part above.  */
   10700          464 :       gfc_add_block_to_block (&block, &se.pre);
   10701              :       /* Take care about non-array allocatable components here.  The alloc_*
   10702              :          routine below is motivated by the alloc_scalar_allocatable_for_
   10703              :          assignment() routine, but with the realloc portions removed and
   10704              :          different input.  */
   10705          464 :       alloc_scalar_allocatable_subcomponent (&block, dest, cm, expr,
   10706              :                                              se.string_length);
   10707              : 
   10708          464 :       if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
   10709            0 :           && expr->symtree->n.sym->attr.dummy)
   10710            0 :         se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
   10711              : 
   10712          464 :       if (cm->ts.type == BT_CLASS)
   10713              :         {
   10714          121 :           tmp = gfc_class_data_get (dest);
   10715          121 :           tmp = build_fold_indirect_ref_loc (input_location, tmp);
   10716          121 :           vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
   10717          121 :           vtab = gfc_build_addr_expr (NULL_TREE, vtab);
   10718          121 :           gfc_add_modify (&block, gfc_class_vptr_get (dest),
   10719          121 :                  fold_convert (TREE_TYPE (gfc_class_vptr_get (dest)), vtab));
   10720              :         }
   10721              :       else
   10722          343 :         tmp = build_fold_indirect_ref_loc (input_location, dest);
   10723              : 
   10724              :       /* For deferred strings insert a memcpy.  */
   10725          464 :       if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
   10726              :         {
   10727          145 :           gcc_assert (se.string_length || expr->ts.u.cl->backend_decl);
   10728          145 :           size = size_of_string_in_bytes (cm->ts.kind, se.string_length
   10729              :                                                 ? se.string_length
   10730            0 :                                                 : expr->ts.u.cl->backend_decl);
   10731          145 :           tmp = gfc_build_memcpy_call (tmp, se.expr, size);
   10732          145 :           gfc_add_expr_to_block (&block, tmp);
   10733              :         }
   10734          319 :       else if (cm->ts.type == BT_CLASS)
   10735              :         {
   10736              :           /* Fix the expression for memcpy.  */
   10737          121 :           if (expr->expr_type != EXPR_VARIABLE)
   10738           85 :             se.expr = gfc_evaluate_now (se.expr, &block);
   10739              : 
   10740          121 :           if (expr->ts.type == BT_CHARACTER)
   10741              :             {
   10742           24 :               size = build_int_cst (gfc_charlen_type_node, expr->ts.kind);
   10743           24 :               size = fold_build2_loc (input_location, MULT_EXPR,
   10744              :                                       gfc_charlen_type_node,
   10745              :                                       se.string_length, size);
   10746           24 :               size = fold_convert (size_type_node, size);
   10747              :             }
   10748              :           else
   10749           97 :             size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr->ts));
   10750              : 
   10751              :           /* Now copy the expression to the constructor component _data.  */
   10752          121 :           gfc_add_expr_to_block (&block,
   10753              :                                  gfc_build_memcpy_call (tmp, se.expr, size));
   10754              : 
   10755          121 :           if (expr->ts.type == BT_DERIVED
   10756           66 :               && expr->ts.u.derived->attr.alloc_comp
   10757            6 :               && expr->expr_type != EXPR_NULL)
   10758              :             {
   10759            6 :               tmp2 = gfc_class_data_get (dest);
   10760            6 :               tmp2 = gfc_copy_alloc_comp (expr->ts.u.derived, tmp2,
   10761              :                                           gfc_class_data_get (dest),
   10762              :                                           expr->rank, 0);
   10763            6 :               gfc_add_expr_to_block (&block, tmp2);
   10764              :             }
   10765              : 
   10766              :           /* Fill the unlimited polymorphic _len field.  */
   10767          121 :           if (UNLIMITED_POLY (cm) && expr->ts.type == BT_CHARACTER)
   10768              :             {
   10769           24 :               tmp = gfc_class_len_get (gfc_get_class_from_expr (tmp));
   10770           24 :               gfc_add_modify (&block, tmp,
   10771           24 :                               fold_convert (TREE_TYPE (tmp),
   10772              :                               se.string_length));
   10773              :             }
   10774              :         }
   10775          198 :       else if (cm->ts.type == BT_CHARACTER)
   10776              :         {
   10777              :           /* Copy into the explicit-length allocatable character component.  */
   10778           24 :           gfc_conv_string_parameter (&se);
   10779           24 :           gfc_trans_string_copy (&block, cm->ts.u.cl->backend_decl, dest,
   10780              :                                  cm->ts.kind, se.string_length, se.expr,
   10781              :                                  expr->ts.kind);
   10782              :         }
   10783              :       else
   10784              :         {
   10785          174 :           gfc_add_modify (&block, tmp,
   10786          174 :                           fold_convert (TREE_TYPE (tmp), se.expr));
   10787          174 :           if (expr->ts.type == BT_DERIVED
   10788           32 :               && expr->ts.u.derived->attr.alloc_comp
   10789            6 :               && expr->expr_type != EXPR_NULL)
   10790              :             {
   10791            6 :               tmp2 = build_fold_indirect_ref_loc (input_location, dest);
   10792            6 :               tmp2 = gfc_copy_alloc_comp (cm->ts.u.derived, tmp2,
   10793              :                                           se.expr, expr->rank, 0);
   10794            6 :               gfc_add_expr_to_block (&block, tmp2);
   10795              :             }
   10796              :         }
   10797              : 
   10798          464 :       gfc_add_block_to_block (&block, &se.post);
   10799          464 :     }
   10800        22803 :   else if (expr->ts.type == BT_UNION)
   10801              :     {
   10802           13 :       tree tmp;
   10803           13 :       gfc_constructor *c = gfc_constructor_first (expr->value.constructor);
   10804              :       /* We mark that the entire union should be initialized with a contrived
   10805              :          EXPR_NULL expression at the beginning.  */
   10806           13 :       if (c != NULL && c->n.component == NULL
   10807            7 :           && c->expr != NULL && c->expr->expr_type == EXPR_NULL)
   10808              :         {
   10809            6 :           tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
   10810            6 :                             dest, build_constructor (TREE_TYPE (dest), NULL));
   10811            6 :           gfc_add_expr_to_block (&block, tmp);
   10812            6 :           c = gfc_constructor_next (c);
   10813              :         }
   10814              :       /* The following constructor expression, if any, represents a specific
   10815              :          map initializer, as given by the user.  */
   10816           13 :       if (c != NULL && c->expr != NULL)
   10817              :         {
   10818            6 :           gcc_assert (expr->expr_type == EXPR_STRUCTURE);
   10819            6 :           tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
   10820            6 :           gfc_add_expr_to_block (&block, tmp);
   10821              :         }
   10822              :     }
   10823        22790 :   else if (expr->ts.type == BT_DERIVED && expr->ts.f90_type != BT_VOID)
   10824              :     {
   10825         3711 :       if (expr->expr_type != EXPR_STRUCTURE)
   10826              :         {
   10827          494 :           tree dealloc = NULL_TREE;
   10828          494 :           gfc_init_se (&se, NULL);
   10829          494 :           gfc_conv_expr (&se, expr);
   10830          494 :           gfc_add_block_to_block (&block, &se.pre);
   10831              :           /* Prevent repeat evaluations in gfc_copy_alloc_comp by fixing the
   10832              :              expression in  a temporary variable and deallocate the allocatable
   10833              :              components. Then we can the copy the expression to the result.  */
   10834          494 :           if (cm->ts.u.derived->attr.alloc_comp
   10835          372 :               && expr->expr_type != EXPR_VARIABLE)
   10836              :             {
   10837          336 :               se.expr = gfc_evaluate_now (se.expr, &block);
   10838          336 :               dealloc = gfc_deallocate_alloc_comp (cm->ts.u.derived, se.expr,
   10839              :                                                    expr->rank);
   10840              :             }
   10841          494 :           gfc_add_modify (&block, dest,
   10842          494 :                           fold_convert (TREE_TYPE (dest), se.expr));
   10843          494 :           if (cm->ts.u.derived->attr.alloc_comp
   10844          372 :               && expr->expr_type != EXPR_NULL)
   10845              :             {
   10846              :               // TODO: Fix caf_mode
   10847           54 :               tmp = gfc_copy_alloc_comp (cm->ts.u.derived, se.expr,
   10848              :                                          dest, expr->rank, 0);
   10849           54 :               gfc_add_expr_to_block (&block, tmp);
   10850           54 :               if (dealloc != NULL_TREE)
   10851           18 :                 gfc_add_expr_to_block (&block, dealloc);
   10852              :             }
   10853          494 :           gfc_add_block_to_block (&block, &se.post);
   10854              :         }
   10855              :       else
   10856              :         {
   10857              :           /* Nested constructors.  */
   10858         3217 :           tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
   10859         3217 :           gfc_add_expr_to_block (&block, tmp);
   10860              :         }
   10861              :     }
   10862        19079 :   else if (gfc_deferred_strlen (cm, &tmp))
   10863              :     {
   10864          125 :       tree strlen;
   10865          125 :       strlen = tmp;
   10866          125 :       gcc_assert (strlen);
   10867          125 :       strlen = fold_build3_loc (input_location, COMPONENT_REF,
   10868          125 :                                 TREE_TYPE (strlen),
   10869          125 :                                 TREE_OPERAND (dest, 0),
   10870              :                                 strlen, NULL_TREE);
   10871              : 
   10872          125 :       if (expr->expr_type == EXPR_NULL)
   10873              :         {
   10874          107 :           tmp = build_int_cst (TREE_TYPE (cm->backend_decl), 0);
   10875          107 :           gfc_add_modify (&block, dest, tmp);
   10876          107 :           tmp = build_int_cst (TREE_TYPE (strlen), 0);
   10877          107 :           gfc_add_modify (&block, strlen, tmp);
   10878              :         }
   10879              :       else
   10880              :         {
   10881           18 :           tree size;
   10882           18 :           gfc_init_se (&se, NULL);
   10883           18 :           gfc_conv_expr (&se, expr);
   10884           18 :           size = size_of_string_in_bytes (cm->ts.kind, se.string_length);
   10885           18 :           size = fold_convert (size_type_node, size);
   10886           18 :           tmp = build_call_expr_loc (input_location,
   10887              :                                      builtin_decl_explicit (BUILT_IN_MALLOC),
   10888              :                                      1, size);
   10889           18 :           gfc_add_modify (&block, dest,
   10890           18 :                           fold_convert (TREE_TYPE (dest), tmp));
   10891           18 :           gfc_add_modify (&block, strlen,
   10892           18 :                           fold_convert (TREE_TYPE (strlen), se.string_length));
   10893           18 :           tmp = gfc_build_memcpy_call (dest, se.expr, size);
   10894           18 :           gfc_add_expr_to_block (&block, tmp);
   10895              :         }
   10896              :     }
   10897        18954 :   else if (cm->ts.type == BT_CLASS
   10898           12 :            && !CLASS_DATA (cm)->as
   10899           12 :            && expr->ts.type == BT_CLASS)
   10900              :     {
   10901           12 :       tree vptr1, vptr2;
   10902           12 :       tree data1, data2;
   10903           12 :       tree size, fcn;
   10904              : 
   10905           12 :       gfc_init_se (&se, NULL);
   10906              : 
   10907           12 :       gfc_conv_expr (&se, expr);
   10908              : 
   10909              :       /* Copy the _vptr to the destination....  */
   10910           12 :       vptr1 = gfc_class_vptr_get (dest);
   10911           12 :       vptr2 = gfc_class_vptr_get (se.expr);
   10912           12 :       gfc_add_modify (&block, vptr1,
   10913           12 :                       fold_convert (TREE_TYPE (vptr1), vptr2));
   10914              : 
   10915              :       /* ....and the _len field if necessary.  */
   10916           12 :       size = gfc_vptr_size_get (vptr2);
   10917           12 :       if (UNLIMITED_POLY (cm) && UNLIMITED_POLY (expr))
   10918              :         {
   10919            0 :           gfc_add_modify (&block, gfc_class_len_get (dest),
   10920              :                           gfc_class_len_get (se.expr));
   10921            0 :           size = gfc_resize_class_size_with_len (&block, se.expr, size);
   10922              :         }
   10923              : 
   10924              :       /* Allocate the destination data.  */
   10925           12 :       data1 = gfc_class_data_get (dest);
   10926           12 :       data2 = gfc_class_data_get (se.expr);
   10927           12 :       tmp = gfc_call_malloc (&block, TREE_TYPE (data1), size);
   10928           12 :       gfc_add_modify (&block, data1, tmp);
   10929              : 
   10930              :       /* Now call the copy function. */
   10931           12 :       fcn = gfc_vptr_copy_get (vptr2);
   10932           12 :       if (POINTER_TYPE_P (TREE_TYPE (fcn)))
   10933           12 :         fcn = build_fold_indirect_ref_loc (input_location, fcn);
   10934           12 :       tmp = build_call_expr_loc (input_location, fcn, 2,
   10935              :                                  data2, data1);
   10936           12 :       gfc_add_expr_to_block (&block, tmp);
   10937           12 :     }
   10938        18942 :   else if (!cm->attr.artificial)
   10939              :     {
   10940              :       /* Scalar component (excluding deferred parameters).  */
   10941        18821 :       gfc_init_se (&se, NULL);
   10942        18821 :       gfc_init_se (&lse, NULL);
   10943              : 
   10944        18821 :       gfc_conv_expr (&se, expr);
   10945        18821 :       if (cm->ts.type == BT_CHARACTER)
   10946         1105 :         lse.string_length = cm->ts.u.cl->backend_decl;
   10947        18821 :       lse.expr = dest;
   10948        18821 :       tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, false, false);
   10949        18821 :       gfc_add_expr_to_block (&block, tmp);
   10950              :     }
   10951        32761 :   return gfc_finish_block (&block);
   10952              : }
   10953              : 
   10954              : /* Assign a derived type constructor to a variable.  */
   10955              : 
   10956              : tree
   10957        22368 : gfc_trans_structure_assign (tree dest, gfc_expr * expr, bool init, bool coarray)
   10958              : {
   10959        22368 :   gfc_constructor *c;
   10960        22368 :   gfc_component *cm;
   10961        22368 :   stmtblock_t block;
   10962        22368 :   tree field;
   10963        22368 :   tree tmp;
   10964        22368 :   gfc_se se;
   10965              : 
   10966        22368 :   gfc_start_block (&block);
   10967              : 
   10968        22368 :   if (expr->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
   10969          179 :       && (expr->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
   10970           13 :           || expr->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
   10971              :     {
   10972          179 :       gfc_se lse;
   10973              : 
   10974          179 :       gfc_init_se (&se, NULL);
   10975          179 :       gfc_init_se (&lse, NULL);
   10976          179 :       gfc_conv_expr (&se, gfc_constructor_first (expr->value.constructor)->expr);
   10977          179 :       lse.expr = dest;
   10978          179 :       gfc_add_modify (&block, lse.expr,
   10979          179 :                       fold_convert (TREE_TYPE (lse.expr), se.expr));
   10980              : 
   10981          179 :       return gfc_finish_block (&block);
   10982              :     }
   10983              : 
   10984              :   /* Make sure that the derived type has been completely built.  */
   10985        22189 :   if (!expr->ts.u.derived->backend_decl
   10986        22189 :       || !TYPE_FIELDS (expr->ts.u.derived->backend_decl))
   10987              :     {
   10988          230 :       tmp = gfc_typenode_for_spec (&expr->ts);
   10989          230 :       gcc_assert (tmp);
   10990              :     }
   10991              : 
   10992        22189 :   cm = expr->ts.u.derived->components;
   10993              : 
   10994              : 
   10995        22189 :   if (coarray)
   10996          225 :     gfc_init_se (&se, NULL);
   10997              : 
   10998        22189 :   for (c = gfc_constructor_first (expr->value.constructor);
   10999        58250 :        c; c = gfc_constructor_next (c), cm = cm->next)
   11000              :     {
   11001              :       /* Skip absent members in default initializers.  */
   11002        36061 :       if (!c->expr && !cm->attr.allocatable)
   11003         3300 :         continue;
   11004              : 
   11005              :       /* Register the component with the caf-lib before it is initialized.
   11006              :          Register only allocatable components, that are not coarray'ed
   11007              :          components (%comp[*]).  Only register when the constructor is the
   11008              :          null-expression.  */
   11009        32761 :       if (coarray && !cm->attr.codimension
   11010          515 :           && (cm->attr.allocatable || cm->attr.pointer)
   11011          179 :           && (!c->expr || c->expr->expr_type == EXPR_NULL))
   11012              :         {
   11013          177 :           tree token, desc, size;
   11014          354 :           bool is_array = cm->ts.type == BT_CLASS
   11015          177 :               ? CLASS_DATA (cm)->attr.dimension : cm->attr.dimension;
   11016              : 
   11017          177 :           field = cm->backend_decl;
   11018          177 :           field = fold_build3_loc (input_location, COMPONENT_REF,
   11019          177 :                                    TREE_TYPE (field), dest, field, NULL_TREE);
   11020          177 :           if (cm->ts.type == BT_CLASS)
   11021            0 :             field = gfc_class_data_get (field);
   11022              : 
   11023          177 :           token
   11024              :             = is_array
   11025          177 :                 ? gfc_conv_descriptor_token (field)
   11026           52 :                 : fold_build3_loc (input_location, COMPONENT_REF,
   11027           52 :                                    TREE_TYPE (gfc_comp_caf_token (cm)), dest,
   11028           52 :                                    gfc_comp_caf_token (cm), NULL_TREE);
   11029              : 
   11030          177 :           if (is_array)
   11031              :             {
   11032              :               /* The _caf_register routine looks at the rank of the array
   11033              :                  descriptor to decide whether the data registered is an array
   11034              :                  or not.  */
   11035          125 :               int rank = cm->ts.type == BT_CLASS ? CLASS_DATA (cm)->as->rank
   11036          125 :                                                  : cm->as->rank;
   11037              :               /* When the rank is not known just set a positive rank, which
   11038              :                  suffices to recognize the data as array.  */
   11039          125 :               if (rank < 0)
   11040            0 :                 rank = 1;
   11041          125 :               size = build_zero_cst (size_type_node);
   11042          125 :               desc = field;
   11043          125 :               gfc_conv_descriptor_rank_set (&block, desc, rank);
   11044              :             }
   11045              :           else
   11046              :             {
   11047           52 :               desc = gfc_conv_scalar_to_descriptor (&se, field,
   11048           52 :                                                     cm->ts.type == BT_CLASS
   11049           52 :                                                     ? CLASS_DATA (cm)->attr
   11050              :                                                     : cm->attr);
   11051           52 :               size = TYPE_SIZE_UNIT (TREE_TYPE (field));
   11052              :             }
   11053          177 :           gfc_add_block_to_block (&block, &se.pre);
   11054          177 :           tmp =  build_call_expr_loc (input_location, gfor_fndecl_caf_register,
   11055              :                                       7, size, build_int_cst (
   11056              :                                         integer_type_node,
   11057              :                                         GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY),
   11058              :                                       gfc_build_addr_expr (pvoid_type_node,
   11059              :                                                            token),
   11060              :                                       gfc_build_addr_expr (NULL_TREE, desc),
   11061              :                                       null_pointer_node, null_pointer_node,
   11062              :                                       integer_zero_node);
   11063          177 :           gfc_add_expr_to_block (&block, tmp);
   11064              :         }
   11065        32761 :       field = cm->backend_decl;
   11066        32761 :       gcc_assert(field);
   11067        32761 :       tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
   11068              :                              dest, field, NULL_TREE);
   11069        32761 :       if (!c->expr)
   11070              :         {
   11071            0 :           gfc_expr *e = gfc_get_null_expr (NULL);
   11072            0 :           tmp = gfc_trans_subcomponent_assign (tmp, cm, e, init);
   11073            0 :           gfc_free_expr (e);
   11074              :         }
   11075              :       else
   11076        32761 :         tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr, init);
   11077        32761 :       gfc_add_expr_to_block (&block, tmp);
   11078              :     }
   11079        22189 :   return gfc_finish_block (&block);
   11080              : }
   11081              : 
   11082              : static void
   11083           21 : gfc_conv_union_initializer (vec<constructor_elt, va_gc> *&v,
   11084              :                             gfc_component *un, gfc_expr *init)
   11085              : {
   11086           21 :   gfc_constructor *ctor;
   11087              : 
   11088           21 :   if (un->ts.type != BT_UNION || un == NULL || init == NULL)
   11089              :     return;
   11090              : 
   11091           21 :   ctor = gfc_constructor_first (init->value.constructor);
   11092              : 
   11093           21 :   if (ctor == NULL || ctor->expr == NULL)
   11094              :     return;
   11095              : 
   11096           21 :   gcc_assert (init->expr_type == EXPR_STRUCTURE);
   11097              : 
   11098              :   /* If we have an 'initialize all' constructor, do it first.  */
   11099           21 :   if (ctor->expr->expr_type == EXPR_NULL)
   11100              :     {
   11101            9 :       tree union_type = TREE_TYPE (un->backend_decl);
   11102            9 :       tree val = build_constructor (union_type, NULL);
   11103            9 :       CONSTRUCTOR_APPEND_ELT (v, un->backend_decl, val);
   11104            9 :       ctor = gfc_constructor_next (ctor);
   11105              :     }
   11106              : 
   11107              :   /* Add the map initializer on top.  */
   11108           21 :   if (ctor != NULL && ctor->expr != NULL)
   11109              :     {
   11110           12 :       gcc_assert (ctor->expr->expr_type == EXPR_STRUCTURE);
   11111           12 :       tree val = gfc_conv_initializer (ctor->expr, &un->ts,
   11112           12 :                                        TREE_TYPE (un->backend_decl),
   11113           12 :                                        un->attr.dimension, un->attr.pointer,
   11114           12 :                                        un->attr.proc_pointer);
   11115           12 :       CONSTRUCTOR_APPEND_ELT (v, un->backend_decl, val);
   11116              :     }
   11117              : }
   11118              : 
   11119              : /* Build an expression for a constructor. If init is nonzero then
   11120              :    this is part of a static variable initializer.  */
   11121              : 
   11122              : void
   11123        41094 : gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
   11124              : {
   11125        41094 :   gfc_constructor *c;
   11126        41094 :   gfc_component *cm;
   11127        41094 :   tree val;
   11128        41094 :   tree type;
   11129        41094 :   tree tmp;
   11130        41094 :   vec<constructor_elt, va_gc> *v = NULL;
   11131              : 
   11132        41094 :   gcc_assert (se->ss == NULL);
   11133        41094 :   gcc_assert (expr->expr_type == EXPR_STRUCTURE);
   11134        41094 :   type = gfc_typenode_for_spec (&expr->ts);
   11135              : 
   11136        41094 :   if (!init)
   11137              :     {
   11138        17179 :       if (IS_PDT (expr) && expr->must_finalize)
   11139          288 :         final_block = &se->finalblock;
   11140              : 
   11141              :       /* Create a temporary variable and fill it in.  */
   11142        17179 :       se->expr = gfc_create_var (type, expr->ts.u.derived->name);
   11143              :       /* The symtree in expr is NULL, if the code to generate is for
   11144              :          initializing the static members only.  */
   11145        34358 :       tmp = gfc_trans_structure_assign (se->expr, expr, expr->symtree != NULL,
   11146        17179 :                                         se->want_coarray);
   11147        17179 :       gfc_add_expr_to_block (&se->pre, tmp);
   11148        17179 :       final_block = NULL;
   11149        17179 :       return;
   11150              :     }
   11151              : 
   11152        23915 :   cm = expr->ts.u.derived->components;
   11153              : 
   11154        23915 :   for (c = gfc_constructor_first (expr->value.constructor);
   11155       122129 :        c && cm; c = gfc_constructor_next (c), cm = cm->next)
   11156              :     {
   11157              :       /* Skip absent members in default initializers and allocatable
   11158              :          components.  Although the latter have a default initializer
   11159              :          of EXPR_NULL,... by default, the static nullify is not needed
   11160              :          since this is done every time we come into scope.  */
   11161       107360 :       if (!c->expr
   11162        95755 :           || (cm->attr.allocatable && cm->attr.flavor != FL_PROCEDURE)
   11163       187474 :           || (IS_PDT (cm) && has_parameterized_comps (cm->ts.u.derived)))
   11164         9146 :         continue;
   11165              : 
   11166        89068 :       if (cm->initializer && cm->initializer->expr_type != EXPR_NULL
   11167        51825 :           && strcmp (cm->name, "_extends") == 0
   11168         1442 :           && cm->initializer->symtree)
   11169              :         {
   11170         1442 :           tree vtab;
   11171         1442 :           gfc_symbol *vtabs;
   11172         1442 :           vtabs = cm->initializer->symtree->n.sym;
   11173         1442 :           vtab = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtabs));
   11174         1442 :           vtab = unshare_expr_without_location (vtab);
   11175         1442 :           CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, vtab);
   11176         1442 :         }
   11177        87626 :       else if (cm->ts.u.derived && strcmp (cm->name, "_size") == 0)
   11178              :         {
   11179         9493 :           val = TYPE_SIZE_UNIT (gfc_get_derived_type (cm->ts.u.derived));
   11180         9493 :           CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
   11181              :                                   fold_convert (TREE_TYPE (cm->backend_decl),
   11182              :                                                 val));
   11183         9493 :         }
   11184        78133 :       else if (cm->ts.type == BT_INTEGER && strcmp (cm->name, "_len") == 0)
   11185          449 :         CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
   11186              :                                 fold_convert (TREE_TYPE (cm->backend_decl),
   11187          449 :                                               integer_zero_node));
   11188        77684 :       else if (cm->ts.type == BT_UNION)
   11189           21 :         gfc_conv_union_initializer (v, cm, c->expr);
   11190              :       else
   11191              :         {
   11192        77663 :           val = gfc_conv_initializer (c->expr, &cm->ts,
   11193        77663 :                                       TREE_TYPE (cm->backend_decl),
   11194        77663 :                                       cm->attr.dimension, cm->attr.pointer,
   11195        77663 :                                       cm->attr.proc_pointer);
   11196        77663 :           val = unshare_expr_without_location (val);
   11197              : 
   11198              :           /* Append it to the constructor list.  */
   11199       175877 :           CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
   11200              :         }
   11201              :     }
   11202              : 
   11203        23915 :   se->expr = build_constructor (type, v);
   11204        23915 :   if (init)
   11205        23915 :     TREE_CONSTANT (se->expr) = 1;
   11206              : }
   11207              : 
   11208              : 
   11209              : /* Translate a substring expression.  */
   11210              : 
   11211              : static void
   11212          258 : gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
   11213              : {
   11214          258 :   gfc_ref *ref;
   11215              : 
   11216          258 :   ref = expr->ref;
   11217              : 
   11218          258 :   gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
   11219              : 
   11220          516 :   se->expr = gfc_build_wide_string_const (expr->ts.kind,
   11221          258 :                                           expr->value.character.length,
   11222          258 :                                           expr->value.character.string);
   11223              : 
   11224          258 :   se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
   11225          258 :   TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
   11226              : 
   11227          258 :   if (ref)
   11228          258 :     gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
   11229          258 : }
   11230              : 
   11231              : 
   11232              : /* Entry point for expression translation.  Evaluates a scalar quantity.
   11233              :    EXPR is the expression to be translated, and SE is the state structure if
   11234              :    called from within the scalarized.  */
   11235              : 
   11236              : void
   11237      3753138 : gfc_conv_expr (gfc_se * se, gfc_expr * expr)
   11238              : {
   11239      3753138 :   gfc_ss *ss;
   11240              : 
   11241      3753138 :   ss = se->ss;
   11242      3753138 :   if (ss && ss->info->expr == expr
   11243       245516 :       && (ss->info->type == GFC_SS_SCALAR
   11244              :           || ss->info->type == GFC_SS_REFERENCE))
   11245              :     {
   11246        41524 :       gfc_ss_info *ss_info;
   11247              : 
   11248        41524 :       ss_info = ss->info;
   11249              :       /* Substitute a scalar expression evaluated outside the scalarization
   11250              :          loop.  */
   11251        41524 :       se->expr = ss_info->data.scalar.value;
   11252        41524 :       if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
   11253          874 :         se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
   11254              : 
   11255        41524 :       se->string_length = ss_info->string_length;
   11256        41524 :       gfc_advance_se_ss_chain (se);
   11257        41524 :       return;
   11258              :     }
   11259              : 
   11260              :   /* We need to convert the expressions for the iso_c_binding derived types.
   11261              :      C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
   11262              :      null_pointer_node.  C_PTR and C_FUNPTR are converted to match the
   11263              :      typespec for the C_PTR and C_FUNPTR symbols, which has already been
   11264              :      updated to be an integer with a kind equal to the size of a (void *).  */
   11265      3711614 :   if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->ts.f90_type == BT_VOID
   11266        14938 :       && expr->ts.u.derived->attr.is_bind_c)
   11267              :     {
   11268        14029 :       if (expr->expr_type == EXPR_VARIABLE
   11269         9572 :           && (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
   11270         9572 :               || expr->symtree->n.sym->intmod_sym_id
   11271              :                  == ISOCBINDING_NULL_FUNPTR))
   11272              :         {
   11273              :           /* Set expr_type to EXPR_NULL, which will result in
   11274              :              null_pointer_node being used below.  */
   11275            0 :           expr->expr_type = EXPR_NULL;
   11276              :         }
   11277              :       else
   11278              :         {
   11279              :           /* Update the type/kind of the expression to be what the new
   11280              :              type/kind are for the updated symbols of C_PTR/C_FUNPTR.  */
   11281        14029 :           expr->ts.type = BT_INTEGER;
   11282        14029 :           expr->ts.f90_type = BT_VOID;
   11283        14029 :           expr->ts.kind = gfc_index_integer_kind;
   11284              :         }
   11285              :     }
   11286              : 
   11287      3711614 :   gfc_fix_class_refs (expr);
   11288              : 
   11289      3711614 :   switch (expr->expr_type)
   11290              :     {
   11291       517008 :     case EXPR_OP:
   11292       517008 :       gfc_conv_expr_op (se, expr);
   11293       517008 :       break;
   11294              : 
   11295          159 :     case EXPR_CONDITIONAL:
   11296          159 :       gfc_conv_conditional_expr (se, expr);
   11297          159 :       break;
   11298              : 
   11299       316337 :     case EXPR_FUNCTION:
   11300       316337 :       gfc_conv_function_expr (se, expr);
   11301       316337 :       break;
   11302              : 
   11303      1168140 :     case EXPR_CONSTANT:
   11304      1168140 :       gfc_conv_constant (se, expr);
   11305      1168140 :       break;
   11306              : 
   11307      1651092 :     case EXPR_VARIABLE:
   11308      1651092 :       gfc_conv_variable (se, expr);
   11309      1651092 :       break;
   11310              : 
   11311         4368 :     case EXPR_NULL:
   11312         4368 :       se->expr = null_pointer_node;
   11313         4368 :       break;
   11314              : 
   11315          258 :     case EXPR_SUBSTRING:
   11316          258 :       gfc_conv_substring_expr (se, expr);
   11317          258 :       break;
   11318              : 
   11319        17179 :     case EXPR_STRUCTURE:
   11320        17179 :       gfc_conv_structure (se, expr, 0);
   11321              :       /* F2008 4.5.6.3 para 5: If an executable construct references a
   11322              :          structure constructor or array constructor, the entity created by
   11323              :          the constructor is finalized after execution of the innermost
   11324              :          executable construct containing the reference. This, in fact,
   11325              :          was later deleted by the Combined Technical Corrigenda 1 TO 4 for
   11326              :          fortran 2008 (f08/0011).  */
   11327        17179 :       if ((gfc_option.allow_std & (GFC_STD_F2008 | GFC_STD_F2003))
   11328        17179 :           && !(gfc_option.allow_std & GFC_STD_GNU)
   11329          143 :           && expr->must_finalize
   11330        17191 :           && gfc_may_be_finalized (expr->ts))
   11331              :         {
   11332           12 :           locus loc;
   11333           12 :           gfc_locus_from_location (&loc, input_location);
   11334           12 :           gfc_warning (0, "The structure constructor at %L has been"
   11335              :                          " finalized. This feature was removed by f08/0011."
   11336              :                          " Use -std=f2018 or -std=gnu to eliminate the"
   11337              :                          " finalization.", &loc);
   11338           12 :           symbol_attribute attr;
   11339           12 :           attr.allocatable = attr.pointer = 0;
   11340           12 :           gfc_finalize_tree_expr (se, expr->ts.u.derived, attr, 0);
   11341           12 :           gfc_add_block_to_block (&se->post, &se->finalblock);
   11342              :         }
   11343              :       break;
   11344              : 
   11345        37073 :     case EXPR_ARRAY:
   11346        37073 :       gfc_conv_array_constructor_expr (se, expr);
   11347        37073 :       gfc_add_block_to_block (&se->post, &se->finalblock);
   11348        37073 :       break;
   11349              : 
   11350            0 :     default:
   11351            0 :       gcc_unreachable ();
   11352      3753138 :       break;
   11353              :     }
   11354              : }
   11355              : 
   11356              : /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
   11357              :    of an assignment.  */
   11358              : void
   11359       385162 : gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
   11360              : {
   11361       385162 :   gfc_conv_expr (se, expr);
   11362              :   /* All numeric lvalues should have empty post chains.  If not we need to
   11363              :      figure out a way of rewriting an lvalue so that it has no post chain.  */
   11364       385162 :   gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
   11365       385162 : }
   11366              : 
   11367              : /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
   11368              :    numeric expressions.  Used for scalar values where inserting cleanup code
   11369              :    is inconvenient.  */
   11370              : void
   11371      1062730 : gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
   11372              : {
   11373      1062730 :   tree val;
   11374              : 
   11375      1062730 :   gcc_assert (expr->ts.type != BT_CHARACTER);
   11376      1062730 :   gfc_conv_expr (se, expr);
   11377      1062730 :   if (se->post.head)
   11378              :     {
   11379         2633 :       val = gfc_create_var (TREE_TYPE (se->expr), NULL);
   11380         2633 :       gfc_add_modify (&se->pre, val, se->expr);
   11381         2633 :       se->expr = val;
   11382         2633 :       gfc_add_block_to_block (&se->pre, &se->post);
   11383              :     }
   11384      1062730 : }
   11385              : 
   11386              : /* Helper to translate an expression and convert it to a particular type.  */
   11387              : void
   11388       302856 : gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
   11389              : {
   11390       302856 :   gfc_conv_expr_val (se, expr);
   11391       302856 :   se->expr = convert (type, se->expr);
   11392       302856 : }
   11393              : 
   11394              : 
   11395              : /* Converts an expression so that it can be passed by reference.  Scalar
   11396              :    values only.  */
   11397              : 
   11398              : void
   11399       232327 : gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
   11400              : {
   11401       232327 :   gfc_ss *ss;
   11402       232327 :   tree var;
   11403              : 
   11404       232327 :   ss = se->ss;
   11405       232327 :   if (ss && ss->info->expr == expr
   11406         8071 :       && ss->info->type == GFC_SS_REFERENCE)
   11407              :     {
   11408              :       /* Returns a reference to the scalar evaluated outside the loop
   11409              :          for this case.  */
   11410          907 :       gfc_conv_expr (se, expr);
   11411              : 
   11412          907 :       if (expr->ts.type == BT_CHARACTER
   11413          114 :           && expr->expr_type != EXPR_FUNCTION)
   11414          102 :         gfc_conv_string_parameter (se);
   11415              :      else
   11416          805 :         se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
   11417              : 
   11418              :       return;
   11419              :     }
   11420              : 
   11421       231420 :   if (expr->ts.type == BT_CHARACTER)
   11422              :     {
   11423        50184 :       gfc_conv_expr (se, expr);
   11424        50184 :       gfc_conv_string_parameter (se);
   11425        50184 :       return;
   11426              :     }
   11427              : 
   11428       181236 :   if (expr->expr_type == EXPR_VARIABLE)
   11429              :     {
   11430        72651 :       se->want_pointer = 1;
   11431        72651 :       gfc_conv_expr (se, expr);
   11432        72651 :       if (se->post.head)
   11433              :         {
   11434            0 :           var = gfc_create_var (TREE_TYPE (se->expr), NULL);
   11435            0 :           gfc_add_modify (&se->pre, var, se->expr);
   11436            0 :           gfc_add_block_to_block (&se->pre, &se->post);
   11437            0 :           se->expr = var;
   11438              :         }
   11439              :       return;
   11440              :     }
   11441              : 
   11442       108585 :   if (expr->expr_type == EXPR_CONDITIONAL)
   11443              :     {
   11444           18 :       se->want_pointer = 1;
   11445           18 :       gfc_conv_expr (se, expr);
   11446           18 :       return;
   11447              :     }
   11448              : 
   11449       108567 :   if (expr->expr_type == EXPR_FUNCTION
   11450        14037 :       && ((expr->value.function.esym
   11451         2107 :            && expr->value.function.esym->result
   11452         2106 :            && expr->value.function.esym->result->attr.pointer
   11453           83 :            && !expr->value.function.esym->result->attr.dimension)
   11454        13960 :           || (!expr->value.function.esym && !expr->ref
   11455        11824 :               && expr->symtree->n.sym->attr.pointer
   11456            0 :               && !expr->symtree->n.sym->attr.dimension)))
   11457              :     {
   11458           77 :       se->want_pointer = 1;
   11459           77 :       gfc_conv_expr (se, expr);
   11460           77 :       var = gfc_create_var (TREE_TYPE (se->expr), NULL);
   11461           77 :       gfc_add_modify (&se->pre, var, se->expr);
   11462           77 :       se->expr = var;
   11463           77 :       return;
   11464              :     }
   11465              : 
   11466       108490 :   gfc_conv_expr (se, expr);
   11467              : 
   11468              :   /* Create a temporary var to hold the value.  */
   11469       108490 :   if (TREE_CONSTANT (se->expr))
   11470              :     {
   11471              :       tree tmp = se->expr;
   11472        85702 :       STRIP_TYPE_NOPS (tmp);
   11473        85702 :       var = build_decl (input_location,
   11474        85702 :                         CONST_DECL, NULL, TREE_TYPE (tmp));
   11475        85702 :       DECL_INITIAL (var) = tmp;
   11476        85702 :       TREE_STATIC (var) = 1;
   11477        85702 :       pushdecl (var);
   11478              :     }
   11479              :   else
   11480              :     {
   11481        22788 :       var = gfc_create_var (TREE_TYPE (se->expr), NULL);
   11482        22788 :       gfc_add_modify (&se->pre, var, se->expr);
   11483              :     }
   11484              : 
   11485       108490 :   if (!expr->must_finalize)
   11486       108394 :     gfc_add_block_to_block (&se->pre, &se->post);
   11487              : 
   11488              :   /* Take the address of that value.  */
   11489       108490 :   se->expr = gfc_build_addr_expr (NULL_TREE, var);
   11490              : }
   11491              : 
   11492              : 
   11493              : /* Get the _len component for an unlimited polymorphic expression.  */
   11494              : 
   11495              : static tree
   11496         1962 : trans_get_upoly_len (stmtblock_t *block, gfc_expr *expr)
   11497              : {
   11498         1962 :   gfc_se se;
   11499         1962 :   gfc_ref *ref = expr->ref;
   11500              : 
   11501         1962 :   gfc_init_se (&se, NULL);
   11502         4038 :   while (ref && ref->next)
   11503              :     ref = ref->next;
   11504         1962 :   gfc_add_len_component (expr);
   11505         1962 :   gfc_conv_expr (&se, expr);
   11506         1962 :   gfc_add_block_to_block (block, &se.pre);
   11507         1962 :   gcc_assert (se.post.head == NULL_TREE);
   11508         1962 :   if (ref)
   11509              :     {
   11510          298 :       gfc_free_ref_list (ref->next);
   11511          298 :       ref->next = NULL;
   11512              :     }
   11513              :   else
   11514              :     {
   11515         1664 :       gfc_free_ref_list (expr->ref);
   11516         1664 :       expr->ref = NULL;
   11517              :     }
   11518         1962 :   return se.expr;
   11519              : }
   11520              : 
   11521              : 
   11522              : /* Assign _vptr and _len components as appropriate.  BLOCK should be a
   11523              :    statement-list outside of the scalarizer-loop.  When code is generated, that
   11524              :    depends on the scalarized expression, it is added to RSE.PRE.
   11525              :    Returns le's _vptr tree and when set the len expressions in to_lenp and
   11526              :    from_lenp to form a le%_vptr%_copy (re, le, [from_lenp, to_lenp])
   11527              :    expression.  */
   11528              : 
   11529              : static tree
   11530         4914 : trans_class_vptr_len_assignment (stmtblock_t *block, gfc_expr * le,
   11531              :                                  gfc_expr * re, gfc_se *rse,
   11532              :                                  tree * to_lenp, tree * from_lenp,
   11533              :                                  tree * from_vptrp)
   11534              : {
   11535         4914 :   gfc_se se;
   11536         4914 :   gfc_expr * vptr_expr;
   11537         4914 :   tree tmp, to_len = NULL_TREE, from_len = NULL_TREE, lhs_vptr;
   11538         4914 :   bool set_vptr = false, temp_rhs = false;
   11539         4914 :   stmtblock_t *pre = block;
   11540         4914 :   tree class_expr = NULL_TREE;
   11541         4914 :   tree from_vptr = NULL_TREE;
   11542              : 
   11543              :   /* Create a temporary for complicated expressions.  */
   11544         4914 :   if (re->expr_type != EXPR_VARIABLE && re->expr_type != EXPR_NULL
   11545         1402 :       && rse->expr != NULL_TREE)
   11546              :     {
   11547         1402 :       if (!DECL_P (rse->expr))
   11548              :         {
   11549          410 :           if (re->ts.type == BT_CLASS && !GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
   11550           37 :             class_expr = gfc_get_class_from_expr (rse->expr);
   11551              : 
   11552          410 :           if (rse->loop)
   11553          159 :             pre = &rse->loop->pre;
   11554              :           else
   11555          251 :             pre = &rse->pre;
   11556              : 
   11557          410 :           if (class_expr != NULL_TREE && UNLIMITED_POLY (re))
   11558           37 :               tmp = gfc_evaluate_now (TREE_OPERAND (rse->expr, 0), &rse->pre);
   11559              :           else
   11560          373 :               tmp = gfc_evaluate_now (rse->expr, &rse->pre);
   11561              : 
   11562          410 :           rse->expr = tmp;
   11563              :         }
   11564              :       else
   11565          992 :         pre = &rse->pre;
   11566              : 
   11567              :       temp_rhs = true;
   11568              :     }
   11569              : 
   11570              :   /* Get the _vptr for the left-hand side expression.  */
   11571         4914 :   gfc_init_se (&se, NULL);
   11572         4914 :   vptr_expr = gfc_find_and_cut_at_last_class_ref (le);
   11573         4914 :   if (vptr_expr != NULL && gfc_expr_attr (vptr_expr).class_ok)
   11574              :     {
   11575              :       /* Care about _len for unlimited polymorphic entities.  */
   11576         4914 :       if (UNLIMITED_POLY (vptr_expr)
   11577         3822 :           || (vptr_expr->ts.type == BT_DERIVED
   11578         2684 :               && vptr_expr->ts.u.derived->attr.unlimited_polymorphic))
   11579         1630 :         to_len = trans_get_upoly_len (block, vptr_expr);
   11580         4914 :       gfc_add_vptr_component (vptr_expr);
   11581         4914 :       set_vptr = true;
   11582              :     }
   11583              :   else
   11584            0 :     vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&le->ts));
   11585         4914 :   se.want_pointer = 1;
   11586         4914 :   gfc_conv_expr (&se, vptr_expr);
   11587         4914 :   gfc_free_expr (vptr_expr);
   11588         4914 :   gfc_add_block_to_block (block, &se.pre);
   11589         4914 :   gcc_assert (se.post.head == NULL_TREE);
   11590         4914 :   lhs_vptr = se.expr;
   11591         4914 :   STRIP_NOPS (lhs_vptr);
   11592              : 
   11593              :   /* Set the _vptr only when the left-hand side of the assignment is a
   11594              :      class-object.  */
   11595         4914 :   if (set_vptr)
   11596              :     {
   11597              :       /* Get the vptr from the rhs expression only, when it is variable.
   11598              :          Functions are expected to be assigned to a temporary beforehand.  */
   11599         3383 :       vptr_expr = (re->expr_type == EXPR_VARIABLE && re->ts.type == BT_CLASS)
   11600         5815 :           ? gfc_find_and_cut_at_last_class_ref (re)
   11601              :           : NULL;
   11602          901 :       if (vptr_expr != NULL && vptr_expr->ts.type == BT_CLASS)
   11603              :         {
   11604          901 :           if (to_len != NULL_TREE)
   11605              :             {
   11606              :               /* Get the _len information from the rhs.  */
   11607          347 :               if (UNLIMITED_POLY (vptr_expr)
   11608              :                   || (vptr_expr->ts.type == BT_DERIVED
   11609              :                       && vptr_expr->ts.u.derived->attr.unlimited_polymorphic))
   11610          320 :                 from_len = trans_get_upoly_len (block, vptr_expr);
   11611              :             }
   11612          901 :           gfc_add_vptr_component (vptr_expr);
   11613              :         }
   11614              :       else
   11615              :         {
   11616         4013 :           if (re->expr_type == EXPR_VARIABLE
   11617         2482 :               && DECL_P (re->symtree->n.sym->backend_decl)
   11618         2482 :               && DECL_LANG_SPECIFIC (re->symtree->n.sym->backend_decl)
   11619          834 :               && GFC_DECL_SAVED_DESCRIPTOR (re->symtree->n.sym->backend_decl)
   11620         4080 :               && GFC_CLASS_TYPE_P (TREE_TYPE (GFC_DECL_SAVED_DESCRIPTOR (
   11621              :                                            re->symtree->n.sym->backend_decl))))
   11622              :             {
   11623           43 :               vptr_expr = NULL;
   11624           43 :               se.expr = gfc_class_vptr_get (GFC_DECL_SAVED_DESCRIPTOR (
   11625              :                                              re->symtree->n.sym->backend_decl));
   11626           43 :               if (to_len && UNLIMITED_POLY (re))
   11627            0 :                 from_len = gfc_class_len_get (GFC_DECL_SAVED_DESCRIPTOR (
   11628              :                                              re->symtree->n.sym->backend_decl));
   11629              :             }
   11630         3970 :           else if (temp_rhs && re->ts.type == BT_CLASS)
   11631              :             {
   11632          245 :               vptr_expr = NULL;
   11633          245 :               if (class_expr)
   11634              :                 tmp = class_expr;
   11635          208 :               else if (!GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
   11636            0 :                 tmp = gfc_get_class_from_expr (rse->expr);
   11637              :               else
   11638              :                 tmp = rse->expr;
   11639              : 
   11640          245 :               se.expr = gfc_class_vptr_get (tmp);
   11641          245 :               from_vptr = se.expr;
   11642          245 :               if (UNLIMITED_POLY (re))
   11643           86 :                 from_len = gfc_class_len_get (tmp);
   11644              : 
   11645              :             }
   11646         3725 :           else if (re->expr_type != EXPR_NULL)
   11647              :             /* Only when rhs is non-NULL use its declared type for vptr
   11648              :                initialisation.  */
   11649         3596 :             vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&re->ts));
   11650              :           else
   11651              :             /* When the rhs is NULL use the vtab of lhs' declared type.  */
   11652          129 :             vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&le->ts));
   11653              :         }
   11654              : 
   11655         4712 :       if (vptr_expr)
   11656              :         {
   11657         4626 :           gfc_init_se (&se, NULL);
   11658         4626 :           se.want_pointer = 1;
   11659         4626 :           gfc_conv_expr (&se, vptr_expr);
   11660         4626 :           gfc_free_expr (vptr_expr);
   11661         4626 :           gfc_add_block_to_block (block, &se.pre);
   11662         4626 :           gcc_assert (se.post.head == NULL_TREE);
   11663         4626 :           from_vptr = se.expr;
   11664              :         }
   11665         4914 :       gfc_add_modify (pre, lhs_vptr, fold_convert (TREE_TYPE (lhs_vptr),
   11666              :                                                 se.expr));
   11667              : 
   11668         4914 :       if (to_len != NULL_TREE)
   11669              :         {
   11670              :           /* The _len component needs to be set.  Figure how to get the
   11671              :              value of the right-hand side.  */
   11672         1630 :           if (from_len == NULL_TREE)
   11673              :             {
   11674         1224 :               if (rse->string_length != NULL_TREE)
   11675              :                 from_len = rse->string_length;
   11676          766 :               else if (re->ts.type == BT_CHARACTER && re->ts.u.cl->length)
   11677              :                 {
   11678            0 :                   gfc_init_se (&se, NULL);
   11679            0 :                   gfc_conv_expr (&se, re->ts.u.cl->length);
   11680            0 :                   gfc_add_block_to_block (block, &se.pre);
   11681            0 :                   gcc_assert (se.post.head == NULL_TREE);
   11682            0 :                   from_len = gfc_evaluate_now (se.expr, block);
   11683              :                 }
   11684              :               else
   11685          766 :                 from_len = build_zero_cst (gfc_charlen_type_node);
   11686              :             }
   11687         1630 :           gfc_add_modify (pre, to_len, fold_convert (TREE_TYPE (to_len),
   11688              :                                                      from_len));
   11689              :         }
   11690              :     }
   11691              : 
   11692              :   /* Return the _len and _vptr trees only, when requested.  */
   11693         4914 :   if (to_lenp)
   11694         3627 :     *to_lenp = to_len;
   11695         4914 :   if (from_lenp)
   11696         3627 :     *from_lenp = from_len;
   11697         4914 :   if (from_vptrp)
   11698         3627 :     *from_vptrp = from_vptr;
   11699         4914 :   return lhs_vptr;
   11700              : }
   11701              : 
   11702              : 
   11703              : /* Assign tokens for pointer components.  */
   11704              : 
   11705              : static void
   11706           12 : trans_caf_token_assign (gfc_se *lse, gfc_se *rse, gfc_expr *expr1,
   11707              :                         gfc_expr *expr2)
   11708              : {
   11709           12 :   symbol_attribute lhs_attr, rhs_attr;
   11710           12 :   tree tmp, lhs_tok, rhs_tok;
   11711              :   /* Flag to indicated component refs on the rhs.  */
   11712           12 :   bool rhs_cr;
   11713              : 
   11714           12 :   lhs_attr = gfc_caf_attr (expr1);
   11715           12 :   if (expr2->expr_type != EXPR_NULL)
   11716              :     {
   11717            8 :       rhs_attr = gfc_caf_attr (expr2, false, &rhs_cr);
   11718            8 :       if (lhs_attr.codimension && rhs_attr.codimension)
   11719              :         {
   11720            4 :           lhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (lse, expr1);
   11721            4 :           lhs_tok = build_fold_indirect_ref (lhs_tok);
   11722              : 
   11723            4 :           if (rhs_cr)
   11724            0 :             rhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (rse, expr2);
   11725              :           else
   11726              :             {
   11727            4 :               tree caf_decl;
   11728            4 :               caf_decl = gfc_get_tree_for_caf_expr (expr2);
   11729            4 :               gfc_get_caf_token_offset (rse, &rhs_tok, NULL, caf_decl,
   11730              :                                         NULL_TREE, NULL);
   11731              :             }
   11732            4 :           tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
   11733              :                             lhs_tok,
   11734            4 :                             fold_convert (TREE_TYPE (lhs_tok), rhs_tok));
   11735            4 :           gfc_prepend_expr_to_block (&lse->post, tmp);
   11736              :         }
   11737              :     }
   11738            4 :   else if (lhs_attr.codimension)
   11739              :     {
   11740            4 :       lhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (lse, expr1);
   11741            4 :       if (!lhs_tok)
   11742              :         {
   11743            2 :           lhs_tok = gfc_get_tree_for_caf_expr (expr1);
   11744            2 :           lhs_tok = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (lhs_tok));
   11745              :         }
   11746              :       else
   11747            2 :         lhs_tok = build_fold_indirect_ref (lhs_tok);
   11748            4 :       tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
   11749              :                         lhs_tok, null_pointer_node);
   11750            4 :       gfc_prepend_expr_to_block (&lse->post, tmp);
   11751              :     }
   11752           12 : }
   11753              : 
   11754              : 
   11755              : /* Do everything that is needed for a CLASS function expr2.  */
   11756              : 
   11757              : static tree
   11758           18 : trans_class_pointer_fcn (stmtblock_t *block, gfc_se *lse, gfc_se *rse,
   11759              :                          gfc_expr *expr1, gfc_expr *expr2)
   11760              : {
   11761           18 :   tree expr1_vptr = NULL_TREE;
   11762           18 :   tree tmp;
   11763              : 
   11764           18 :   gfc_conv_function_expr (rse, expr2);
   11765           18 :   rse->expr = gfc_evaluate_now (rse->expr, &rse->pre);
   11766              : 
   11767           18 :   if (expr1->ts.type != BT_CLASS)
   11768           12 :       rse->expr = gfc_class_data_get (rse->expr);
   11769              :   else
   11770              :     {
   11771            6 :       expr1_vptr = trans_class_vptr_len_assignment (block, expr1,
   11772              :                                                     expr2, rse,
   11773              :                                                     NULL, NULL, NULL);
   11774            6 :       gfc_add_block_to_block (block, &rse->pre);
   11775            6 :       tmp = gfc_create_var (TREE_TYPE (rse->expr), "ptrtemp");
   11776            6 :       gfc_add_modify (&lse->pre, tmp, rse->expr);
   11777              : 
   11778           12 :       gfc_add_modify (&lse->pre, expr1_vptr,
   11779            6 :                       fold_convert (TREE_TYPE (expr1_vptr),
   11780              :                       gfc_class_vptr_get (tmp)));
   11781            6 :       rse->expr = gfc_class_data_get (tmp);
   11782              :     }
   11783              : 
   11784           18 :   return expr1_vptr;
   11785              : }
   11786              : 
   11787              : 
   11788              : tree
   11789        10456 : gfc_trans_pointer_assign (gfc_code * code)
   11790              : {
   11791        10456 :   return gfc_trans_pointer_assignment (code->expr1, code->expr2);
   11792              : }
   11793              : 
   11794              : 
   11795              : /* Generate code for a pointer assignment.  */
   11796              : 
   11797              : tree
   11798        10511 : gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
   11799              : {
   11800        10511 :   gfc_se lse;
   11801        10511 :   gfc_se rse;
   11802        10511 :   stmtblock_t block;
   11803        10511 :   tree desc;
   11804        10511 :   tree tmp;
   11805        10511 :   tree expr1_vptr = NULL_TREE;
   11806        10511 :   bool scalar, non_proc_ptr_assign;
   11807        10511 :   gfc_ss *ss;
   11808              : 
   11809        10511 :   gfc_start_block (&block);
   11810              : 
   11811        10511 :   gfc_init_se (&lse, NULL);
   11812              : 
   11813              :   /* Usually testing whether this is not a proc pointer assignment.  */
   11814        10511 :   non_proc_ptr_assign
   11815        10511 :     = !(gfc_expr_attr (expr1).proc_pointer
   11816         1231 :         && ((expr2->expr_type == EXPR_VARIABLE
   11817          993 :              && expr2->symtree->n.sym->attr.flavor == FL_PROCEDURE)
   11818          288 :             || expr2->expr_type == EXPR_NULL));
   11819              : 
   11820              :   /* Check whether the expression is a scalar or not; we cannot use
   11821              :      expr1->rank as it can be nonzero for proc pointers.  */
   11822        10511 :   ss = gfc_walk_expr (expr1);
   11823        10511 :   scalar = ss == gfc_ss_terminator;
   11824        10511 :   if (!scalar)
   11825         4611 :     gfc_free_ss_chain (ss);
   11826              : 
   11827        10511 :   if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS
   11828           96 :       && expr2->expr_type != EXPR_FUNCTION && non_proc_ptr_assign)
   11829              :     {
   11830           72 :       gfc_add_data_component (expr2);
   11831              :       /* The following is required as gfc_add_data_component doesn't
   11832              :          update ts.type if there is a trailing REF_ARRAY.  */
   11833           72 :       expr2->ts.type = BT_DERIVED;
   11834              :     }
   11835              : 
   11836        10511 :   if (scalar)
   11837              :     {
   11838              :       /* Scalar pointers.  */
   11839         5900 :       lse.want_pointer = 1;
   11840         5900 :       gfc_conv_expr (&lse, expr1);
   11841         5900 :       gfc_init_se (&rse, NULL);
   11842         5900 :       rse.want_pointer = 1;
   11843         5900 :       if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
   11844            6 :         trans_class_pointer_fcn (&block, &lse, &rse, expr1, expr2);
   11845              :       else
   11846         5894 :         gfc_conv_expr (&rse, expr2);
   11847              : 
   11848         5900 :       if (non_proc_ptr_assign && expr1->ts.type == BT_CLASS)
   11849              :         {
   11850          769 :           trans_class_vptr_len_assignment (&block, expr1, expr2, &rse, NULL,
   11851              :                                            NULL, NULL);
   11852          769 :           lse.expr = gfc_class_data_get (lse.expr);
   11853              :         }
   11854              : 
   11855         5900 :       if (expr1->symtree->n.sym->attr.proc_pointer
   11856          863 :           && expr1->symtree->n.sym->attr.dummy)
   11857           49 :         lse.expr = build_fold_indirect_ref_loc (input_location,
   11858              :                                                 lse.expr);
   11859              : 
   11860         5900 :       if (expr2->symtree && expr2->symtree->n.sym->attr.proc_pointer
   11861           47 :           && expr2->symtree->n.sym->attr.dummy)
   11862           20 :         rse.expr = build_fold_indirect_ref_loc (input_location,
   11863              :                                                 rse.expr);
   11864              : 
   11865         5900 :       gfc_add_block_to_block (&block, &lse.pre);
   11866         5900 :       gfc_add_block_to_block (&block, &rse.pre);
   11867              : 
   11868              :       /* Check character lengths if character expression.  The test is only
   11869              :          really added if -fbounds-check is enabled.  Exclude deferred
   11870              :          character length lefthand sides.  */
   11871          960 :       if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL
   11872          786 :           && !expr1->ts.deferred
   11873          371 :           && !expr1->symtree->n.sym->attr.proc_pointer
   11874         6264 :           && !gfc_is_proc_ptr_comp (expr1))
   11875              :         {
   11876          345 :           gcc_assert (expr2->ts.type == BT_CHARACTER);
   11877          345 :           gcc_assert (lse.string_length && rse.string_length);
   11878          345 :           gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
   11879              :                                        lse.string_length, rse.string_length,
   11880              :                                        &block);
   11881              :         }
   11882              : 
   11883              :       /* The assignment to an deferred character length sets the string
   11884              :          length to that of the rhs.  */
   11885         5900 :       if (expr1->ts.deferred)
   11886              :         {
   11887          530 :           if (expr2->expr_type != EXPR_NULL && lse.string_length != NULL)
   11888          413 :             gfc_add_modify (&block, lse.string_length,
   11889          413 :                             fold_convert (TREE_TYPE (lse.string_length),
   11890              :                                           rse.string_length));
   11891          117 :           else if (lse.string_length != NULL)
   11892          115 :             gfc_add_modify (&block, lse.string_length,
   11893          115 :                             build_zero_cst (TREE_TYPE (lse.string_length)));
   11894              :         }
   11895              : 
   11896         5900 :       gfc_add_modify (&block, lse.expr,
   11897         5900 :                       fold_convert (TREE_TYPE (lse.expr), rse.expr));
   11898              : 
   11899         5900 :       if (flag_coarray == GFC_FCOARRAY_LIB)
   11900              :         {
   11901          354 :           if (expr1->ref)
   11902              :             /* Also set the tokens for pointer components in derived typed
   11903              :                coarrays.  */
   11904           12 :             trans_caf_token_assign (&lse, &rse, expr1, expr2);
   11905          342 :           else if (gfc_caf_attr (expr1).codimension)
   11906              :             {
   11907            0 :               tree lhs_caf_decl, rhs_caf_decl, lhs_tok, rhs_tok;
   11908              : 
   11909            0 :               lhs_caf_decl = gfc_get_tree_for_caf_expr (expr1);
   11910            0 :               rhs_caf_decl = gfc_get_tree_for_caf_expr (expr2);
   11911            0 :               gfc_get_caf_token_offset (&lse, &lhs_tok, nullptr, lhs_caf_decl,
   11912              :                                         NULL_TREE, expr1);
   11913            0 :               gfc_get_caf_token_offset (&rse, &rhs_tok, nullptr, rhs_caf_decl,
   11914              :                                         NULL_TREE, expr2);
   11915            0 :               gfc_add_modify (&block, lhs_tok, rhs_tok);
   11916              :             }
   11917              :         }
   11918              : 
   11919         5900 :       gfc_add_block_to_block (&block, &rse.post);
   11920         5900 :       gfc_add_block_to_block (&block, &lse.post);
   11921              :     }
   11922              :   else
   11923              :     {
   11924         4611 :       gfc_ref* remap;
   11925         4611 :       bool rank_remap;
   11926         4611 :       tree strlen_lhs;
   11927         4611 :       tree strlen_rhs = NULL_TREE;
   11928              : 
   11929              :       /* Array pointer.  Find the last reference on the LHS and if it is an
   11930              :          array section ref, we're dealing with bounds remapping.  In this case,
   11931              :          set it to AR_FULL so that gfc_conv_expr_descriptor does
   11932              :          not see it and process the bounds remapping afterwards explicitly.  */
   11933        10267 :       for (remap = expr1->ref; remap; remap = remap->next)
   11934         6039 :         if (!remap->next && remap->type == REF_ARRAY
   11935         4611 :             && remap->u.ar.type == AR_SECTION)
   11936              :           break;
   11937         4611 :       rank_remap = (remap && remap->u.ar.end[0]);
   11938              : 
   11939          383 :       if (remap && expr2->expr_type == EXPR_NULL)
   11940              :         {
   11941            2 :           gfc_error ("If bounds remapping is specified at %L, "
   11942              :                      "the pointer target shall not be NULL", &expr1->where);
   11943            2 :           return NULL_TREE;
   11944              :         }
   11945              : 
   11946         4609 :       gfc_init_se (&lse, NULL);
   11947         4609 :       if (remap)
   11948          381 :         lse.descriptor_only = 1;
   11949         4609 :       gfc_conv_expr_descriptor (&lse, expr1);
   11950         4609 :       strlen_lhs = lse.string_length;
   11951         4609 :       desc = lse.expr;
   11952              : 
   11953         4609 :       if (expr2->expr_type == EXPR_NULL)
   11954              :         {
   11955              :           /* Just set the data pointer to null.  */
   11956          692 :           gfc_nullify_descriptor (&lse.pre, lse.expr);
   11957              :         }
   11958         3917 :       else if (rank_remap)
   11959              :         {
   11960              :           /* If we are rank-remapping, just get the RHS's descriptor and
   11961              :              process this later on.  */
   11962          258 :           gfc_init_se (&rse, NULL);
   11963          258 :           rse.direct_byref = 1;
   11964          258 :           rse.byref_noassign = 1;
   11965              : 
   11966          258 :           if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
   11967           12 :             expr1_vptr = trans_class_pointer_fcn (&block, &lse, &rse,
   11968              :                                                   expr1, expr2);
   11969          246 :           else if (expr2->expr_type == EXPR_FUNCTION)
   11970              :             {
   11971              :               tree bound[GFC_MAX_DIMENSIONS];
   11972              :               int i;
   11973              : 
   11974           26 :               for (i = 0; i < expr2->rank; i++)
   11975           13 :                 bound[i] = NULL_TREE;
   11976           13 :               tmp = gfc_typenode_for_spec (&expr2->ts);
   11977           13 :               tmp = gfc_get_array_type_bounds (tmp, expr2->rank, 0,
   11978              :                                                bound, bound, 0,
   11979              :                                                GFC_ARRAY_POINTER_CONT, false);
   11980           13 :               tmp = gfc_create_var (tmp, "ptrtemp");
   11981           13 :               rse.descriptor_only = 0;
   11982           13 :               rse.expr = tmp;
   11983           13 :               rse.direct_byref = 1;
   11984           13 :               gfc_conv_expr_descriptor (&rse, expr2);
   11985           13 :               strlen_rhs = rse.string_length;
   11986           13 :               rse.expr = tmp;
   11987              :             }
   11988              :           else
   11989              :             {
   11990          233 :               gfc_conv_expr_descriptor (&rse, expr2);
   11991          233 :               strlen_rhs = rse.string_length;
   11992          233 :               if (expr1->ts.type == BT_CLASS)
   11993           64 :                 expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
   11994              :                                                               expr2, &rse,
   11995              :                                                               NULL, NULL,
   11996              :                                                               NULL);
   11997              :             }
   11998              :         }
   11999         3659 :       else if (expr2->expr_type == EXPR_VARIABLE)
   12000              :         {
   12001              :           /* Assign directly to the LHS's descriptor.  */
   12002         3509 :           lse.descriptor_only = 0;
   12003         3509 :           lse.direct_byref = 1;
   12004         3509 :           gfc_conv_expr_descriptor (&lse, expr2);
   12005         3509 :           strlen_rhs = lse.string_length;
   12006         3509 :           gfc_init_se (&rse, NULL);
   12007              : 
   12008         3509 :           if (expr1->ts.type == BT_CLASS)
   12009              :             {
   12010          429 :               rse.expr = NULL_TREE;
   12011          429 :               rse.string_length = strlen_rhs;
   12012          429 :               trans_class_vptr_len_assignment (&block, expr1, expr2, &rse,
   12013              :                                                NULL, NULL, NULL);
   12014              :             }
   12015              : 
   12016         3509 :           if (remap == NULL)
   12017              :             {
   12018              :               /* If the target is not a whole array, use the target array
   12019              :                  reference for remap.  */
   12020         7120 :               for (remap = expr2->ref; remap; remap = remap->next)
   12021         3992 :                 if (remap->type == REF_ARRAY
   12022         3446 :                     && remap->u.ar.type == AR_FULL
   12023         2747 :                     && remap->next)
   12024              :                   break;
   12025              :             }
   12026              :         }
   12027          150 :       else if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
   12028              :         {
   12029           31 :           gfc_init_se (&rse, NULL);
   12030           31 :           rse.want_pointer = 1;
   12031           31 :           gfc_conv_function_expr (&rse, expr2);
   12032           31 :           if (expr1->ts.type != BT_CLASS)
   12033              :             {
   12034           12 :               rse.expr = gfc_class_data_get (rse.expr);
   12035           12 :               gfc_add_modify (&lse.pre, desc, rse.expr);
   12036              :             }
   12037              :           else
   12038              :             {
   12039           19 :               expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
   12040              :                                                             expr2, &rse, NULL,
   12041              :                                                             NULL, NULL);
   12042           19 :               gfc_add_block_to_block (&block, &rse.pre);
   12043           19 :               tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
   12044           19 :               gfc_add_modify (&lse.pre, tmp, rse.expr);
   12045              : 
   12046           38 :               gfc_add_modify (&lse.pre, expr1_vptr,
   12047           19 :                               fold_convert (TREE_TYPE (expr1_vptr),
   12048              :                                         gfc_class_vptr_get (tmp)));
   12049           19 :               rse.expr = gfc_class_data_get (tmp);
   12050           19 :               gfc_add_modify (&lse.pre, desc, rse.expr);
   12051              :             }
   12052              :         }
   12053              :       else
   12054              :         {
   12055              :           /* Assign to a temporary descriptor and then copy that
   12056              :              temporary to the pointer.  */
   12057          119 :           tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
   12058          119 :           lse.descriptor_only = 0;
   12059          119 :           lse.expr = tmp;
   12060          119 :           lse.direct_byref = 1;
   12061          119 :           gfc_conv_expr_descriptor (&lse, expr2);
   12062          119 :           strlen_rhs = lse.string_length;
   12063          119 :           gfc_add_modify (&lse.pre, desc, tmp);
   12064              :         }
   12065              : 
   12066         4609 :       if (expr1->ts.type == BT_CHARACTER
   12067          620 :           && expr1->ts.deferred)
   12068              :         {
   12069          344 :           gfc_symbol *psym = expr1->symtree->n.sym;
   12070          344 :           tmp = NULL_TREE;
   12071          344 :           if (psym->ts.type == BT_CHARACTER
   12072          343 :               && psym->ts.u.cl->backend_decl)
   12073          343 :             tmp = psym->ts.u.cl->backend_decl;
   12074            1 :           else if (expr1->ts.u.cl->backend_decl
   12075            1 :                    && VAR_P (expr1->ts.u.cl->backend_decl))
   12076            0 :             tmp = expr1->ts.u.cl->backend_decl;
   12077            1 :           else if (TREE_CODE (lse.expr) == COMPONENT_REF)
   12078              :             {
   12079            1 :               gfc_ref *ref = expr1->ref;
   12080            3 :               for (;ref; ref = ref->next)
   12081              :                 {
   12082            2 :                   if (ref->type == REF_COMPONENT
   12083            1 :                       && ref->u.c.component->ts.type == BT_CHARACTER
   12084            3 :                       && gfc_deferred_strlen (ref->u.c.component, &tmp))
   12085            1 :                     tmp = fold_build3_loc (input_location, COMPONENT_REF,
   12086            1 :                                            TREE_TYPE (tmp),
   12087            1 :                                            TREE_OPERAND (lse.expr, 0),
   12088              :                                            tmp, NULL_TREE);
   12089              :                 }
   12090              :             }
   12091              : 
   12092          344 :           gcc_assert (tmp);
   12093              : 
   12094          344 :           if (expr2->expr_type != EXPR_NULL)
   12095          332 :             gfc_add_modify (&block, tmp,
   12096          332 :                             fold_convert (TREE_TYPE (tmp), strlen_rhs));
   12097              :           else
   12098           12 :             gfc_add_modify (&block, tmp, build_zero_cst (TREE_TYPE (tmp)));
   12099              :         }
   12100              : 
   12101         4609 :       gfc_add_block_to_block (&block, &lse.pre);
   12102         4609 :       if (rank_remap)
   12103          258 :         gfc_add_block_to_block (&block, &rse.pre);
   12104              : 
   12105              :       /* If we do bounds remapping, update LHS descriptor accordingly.  */
   12106         4609 :       if (remap)
   12107              :         {
   12108          639 :           int dim;
   12109          639 :           gcc_assert (remap->u.ar.dimen == expr1->rank);
   12110              : 
   12111              :           /* Always set dtype.  */
   12112          639 :           gfc_conv_descriptor_dtype_set (&block, desc,
   12113          639 :                                          gfc_get_dtype (TREE_TYPE (desc)));
   12114              : 
   12115              :           /* For unlimited polymorphic LHS use elem_len from RHS.  */
   12116          639 :           if (UNLIMITED_POLY (expr1) && expr2->ts.type != BT_CLASS)
   12117              :             {
   12118           66 :               tree elem_len;
   12119           66 :               tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
   12120           66 :               elem_len = fold_convert (gfc_array_index_type, tmp);
   12121           66 :               elem_len = gfc_evaluate_now (elem_len, &block);
   12122           66 :               gfc_conv_descriptor_elem_len_set (&block, desc, elem_len);
   12123              :             }
   12124              : 
   12125          639 :           if (rank_remap)
   12126              :             {
   12127              :               /* Do rank remapping.  We already have the RHS's descriptor
   12128              :                  converted in rse and now have to build the correct LHS
   12129              :                  descriptor for it.  */
   12130              : 
   12131          258 :               tree data, span;
   12132          258 :               tree offs, stride;
   12133          258 :               tree lbound, ubound;
   12134              : 
   12135              :               /* Copy data pointer.  */
   12136          258 :               data = gfc_conv_descriptor_data_get (rse.expr);
   12137          258 :               gfc_conv_descriptor_data_set (&block, desc, data);
   12138              : 
   12139              :               /* Copy the span.  */
   12140          258 :               if (VAR_P (rse.expr)
   12141          258 :                   && GFC_DECL_PTR_ARRAY_P (rse.expr))
   12142           12 :                 span = gfc_conv_descriptor_span_get (rse.expr);
   12143              :               else
   12144              :                 {
   12145          246 :                   tmp = TREE_TYPE (rse.expr);
   12146          246 :                   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (tmp));
   12147          246 :                   span = fold_convert (gfc_array_index_type, tmp);
   12148              :                 }
   12149          258 :               gfc_conv_descriptor_span_set (&block, desc, span);
   12150              : 
   12151              :               /* Copy offset but adjust it such that it would correspond
   12152              :                  to a lbound of zero.  */
   12153          258 :               if (expr2->rank == -1)
   12154           42 :                 gfc_conv_descriptor_offset_set (&block, desc,
   12155              :                                                 gfc_index_zero_node);
   12156              :               else
   12157              :                 {
   12158          216 :                   offs = gfc_conv_descriptor_offset_get (rse.expr);
   12159          670 :                   for (dim = 0; dim < expr2->rank; ++dim)
   12160              :                     {
   12161          238 :                       stride = gfc_conv_descriptor_stride_get (rse.expr,
   12162              :                                                         gfc_rank_cst[dim]);
   12163          238 :                       lbound = gfc_conv_descriptor_lbound_get (rse.expr,
   12164              :                                                         gfc_rank_cst[dim]);
   12165          238 :                       tmp = fold_build2_loc (input_location, MULT_EXPR,
   12166              :                                              gfc_array_index_type, stride,
   12167              :                                              lbound);
   12168          238 :                       offs = fold_build2_loc (input_location, PLUS_EXPR,
   12169              :                                               gfc_array_index_type, offs, tmp);
   12170              :                     }
   12171          216 :                   gfc_conv_descriptor_offset_set (&block, desc, offs);
   12172              :                 }
   12173              :               /* Set the bounds as declared for the LHS and calculate strides as
   12174              :                  well as another offset update accordingly.  */
   12175          258 :               stride = gfc_conv_descriptor_stride_get (rse.expr,
   12176              :                                                        gfc_rank_cst[0]);
   12177          907 :               for (dim = 0; dim < expr1->rank; ++dim)
   12178              :                 {
   12179          391 :                   gfc_se lower_se;
   12180          391 :                   gfc_se upper_se;
   12181              : 
   12182          391 :                   gcc_assert (remap->u.ar.start[dim] && remap->u.ar.end[dim]);
   12183              : 
   12184          391 :                   if (remap->u.ar.start[dim]->expr_type != EXPR_CONSTANT
   12185          391 :                       && remap->u.ar.start[dim]->expr_type != EXPR_VARIABLE)
   12186            0 :                     gfc_resolve_expr (remap->u.ar.start[dim]);
   12187          391 :                   if (remap->u.ar.end[dim]->expr_type != EXPR_CONSTANT
   12188          391 :                       && remap->u.ar.end[dim]->expr_type != EXPR_VARIABLE)
   12189           96 :                     gfc_resolve_expr (remap->u.ar.end[dim]);
   12190              : 
   12191              :                   /* Convert declared bounds.  */
   12192          391 :                   gfc_init_se (&lower_se, NULL);
   12193          391 :                   gfc_init_se (&upper_se, NULL);
   12194          391 :                   gfc_conv_expr (&lower_se, remap->u.ar.start[dim]);
   12195          391 :                   gfc_conv_expr (&upper_se, remap->u.ar.end[dim]);
   12196              : 
   12197          391 :                   gfc_add_block_to_block (&block, &lower_se.pre);
   12198          391 :                   gfc_add_block_to_block (&block, &upper_se.pre);
   12199              : 
   12200          391 :                   lbound = fold_convert (gfc_array_index_type, lower_se.expr);
   12201          391 :                   ubound = fold_convert (gfc_array_index_type, upper_se.expr);
   12202              : 
   12203          391 :                   lbound = gfc_evaluate_now (lbound, &block);
   12204          391 :                   ubound = gfc_evaluate_now (ubound, &block);
   12205              : 
   12206          391 :                   gfc_add_block_to_block (&block, &lower_se.post);
   12207          391 :                   gfc_add_block_to_block (&block, &upper_se.post);
   12208              : 
   12209              :                   /* Set bounds in descriptor.  */
   12210          391 :                   gfc_conv_descriptor_lbound_set (&block, desc,
   12211              :                                                   gfc_rank_cst[dim], lbound);
   12212          391 :                   gfc_conv_descriptor_ubound_set (&block, desc,
   12213              :                                                   gfc_rank_cst[dim], ubound);
   12214              : 
   12215              :                   /* Set stride.  */
   12216          391 :                   stride = gfc_evaluate_now (stride, &block);
   12217          391 :                   gfc_conv_descriptor_stride_set (&block, desc,
   12218              :                                                   gfc_rank_cst[dim], stride);
   12219              : 
   12220              :                   /* Update offset.  */
   12221          391 :                   offs = gfc_conv_descriptor_offset_get (desc);
   12222          391 :                   tmp = fold_build2_loc (input_location, MULT_EXPR,
   12223              :                                          gfc_array_index_type, lbound, stride);
   12224          391 :                   offs = fold_build2_loc (input_location, MINUS_EXPR,
   12225              :                                           gfc_array_index_type, offs, tmp);
   12226          391 :                   offs = gfc_evaluate_now (offs, &block);
   12227          391 :                   gfc_conv_descriptor_offset_set (&block, desc, offs);
   12228              : 
   12229              :                   /* Update stride.  */
   12230          391 :                   tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
   12231          391 :                   stride = fold_build2_loc (input_location, MULT_EXPR,
   12232              :                                             gfc_array_index_type, stride, tmp);
   12233              :                 }
   12234              :             }
   12235              :           else
   12236              :             {
   12237              :               /* Bounds remapping.  Just shift the lower bounds.  */
   12238              : 
   12239          381 :               gcc_assert (expr1->rank == expr2->rank);
   12240              : 
   12241          876 :               for (dim = 0; dim < remap->u.ar.dimen; ++dim)
   12242              :                 {
   12243          495 :                   gfc_se lbound_se;
   12244              : 
   12245          495 :                   gcc_assert (!remap->u.ar.end[dim]);
   12246          495 :                   gfc_init_se (&lbound_se, NULL);
   12247          495 :                   if (remap->u.ar.start[dim])
   12248              :                     {
   12249          225 :                       gfc_conv_expr (&lbound_se, remap->u.ar.start[dim]);
   12250          225 :                       gfc_add_block_to_block (&block, &lbound_se.pre);
   12251              :                     }
   12252              :                   else
   12253              :                     /* This remap arises from a target that is not a whole
   12254              :                        array. The start expressions will be NULL but we need
   12255              :                        the lbounds to be one.  */
   12256          270 :                     lbound_se.expr = gfc_index_one_node;
   12257          495 :                   gfc_conv_shift_descriptor_lbound (&block, desc,
   12258              :                                                     dim, lbound_se.expr);
   12259          495 :                   gfc_add_block_to_block (&block, &lbound_se.post);
   12260              :                 }
   12261              :             }
   12262              :         }
   12263              : 
   12264              :       /* If rank remapping was done, check with -fcheck=bounds that
   12265              :          the target is at least as large as the pointer.  */
   12266         4609 :       if (rank_remap && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
   12267           72 :           && expr2->rank != -1)
   12268              :         {
   12269           54 :           tree lsize, rsize;
   12270           54 :           tree fault;
   12271           54 :           const char* msg;
   12272              : 
   12273           54 :           lsize = gfc_conv_descriptor_size (lse.expr, expr1->rank);
   12274           54 :           rsize = gfc_conv_descriptor_size (rse.expr, expr2->rank);
   12275              : 
   12276           54 :           lsize = gfc_evaluate_now (lsize, &block);
   12277           54 :           rsize = gfc_evaluate_now (rsize, &block);
   12278           54 :           fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
   12279              :                                    rsize, lsize);
   12280              : 
   12281           54 :           msg = _("Target of rank remapping is too small (%ld < %ld)");
   12282           54 :           gfc_trans_runtime_check (true, false, fault, &block, &expr2->where,
   12283              :                                    msg, rsize, lsize);
   12284              :         }
   12285              : 
   12286              :       /* Check string lengths if applicable.  The check is only really added
   12287              :          to the output code if -fbounds-check is enabled.  */
   12288         4609 :       if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
   12289              :         {
   12290          554 :           gcc_assert (expr2->ts.type == BT_CHARACTER);
   12291          554 :           gcc_assert (strlen_lhs && strlen_rhs);
   12292          554 :           gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
   12293              :                                        strlen_lhs, strlen_rhs, &block);
   12294              :         }
   12295              : 
   12296         4609 :       gfc_add_block_to_block (&block, &lse.post);
   12297         4609 :       if (rank_remap)
   12298          258 :         gfc_add_block_to_block (&block, &rse.post);
   12299              :     }
   12300              : 
   12301        10509 :   return gfc_finish_block (&block);
   12302              : }
   12303              : 
   12304              : 
   12305              : /* Makes sure se is suitable for passing as a function string parameter.  */
   12306              : /* TODO: Need to check all callers of this function.  It may be abused.  */
   12307              : 
   12308              : void
   12309       250145 : gfc_conv_string_parameter (gfc_se * se)
   12310              : {
   12311       250145 :   tree type;
   12312              : 
   12313       250145 :   if (TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE
   12314       250145 :       && integer_onep (se->string_length))
   12315              :     {
   12316          697 :       se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
   12317          697 :       return;
   12318              :     }
   12319              : 
   12320       249448 :   if (TREE_CODE (se->expr) == STRING_CST)
   12321              :     {
   12322       104340 :       type = TREE_TYPE (TREE_TYPE (se->expr));
   12323       104340 :       se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
   12324       104340 :       return;
   12325              :     }
   12326              : 
   12327       145108 :   if (TREE_CODE (se->expr) == COND_EXPR)
   12328              :     {
   12329           62 :       tree cond = TREE_OPERAND (se->expr, 0);
   12330           62 :       tree lhs = TREE_OPERAND (se->expr, 1);
   12331           62 :       tree rhs = TREE_OPERAND (se->expr, 2);
   12332              : 
   12333           62 :       gfc_se lse, rse;
   12334           62 :       gfc_init_se (&lse, NULL);
   12335           62 :       gfc_init_se (&rse, NULL);
   12336              : 
   12337           62 :       lse.expr = lhs;
   12338           62 :       lse.string_length = se->string_length;
   12339           62 :       gfc_conv_string_parameter (&lse);
   12340              : 
   12341           62 :       rse.expr = rhs;
   12342           62 :       rse.string_length = se->string_length;
   12343           62 :       gfc_conv_string_parameter (&rse);
   12344              : 
   12345           62 :       se->expr
   12346           62 :         = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (lse.expr),
   12347              :                            cond, lse.expr, rse.expr);
   12348              :     }
   12349              : 
   12350       145108 :   if ((TREE_CODE (TREE_TYPE (se->expr)) == ARRAY_TYPE
   12351        46694 :        || TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE)
   12352       145483 :       && TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
   12353              :     {
   12354        98789 :       type = TREE_TYPE (se->expr);
   12355        98789 :       if (TREE_CODE (se->expr) != INDIRECT_REF)
   12356        83804 :         se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
   12357              :       else
   12358              :         {
   12359        14985 :           if (TREE_CODE (type) == ARRAY_TYPE)
   12360        14706 :             type = TREE_TYPE (type);
   12361        14985 :           type = gfc_get_character_type_len_for_eltype (type,
   12362              :                                                         se->string_length);
   12363        14985 :           type = build_pointer_type (type);
   12364        14985 :           se->expr = gfc_build_addr_expr (type, se->expr);
   12365              :         }
   12366              :     }
   12367              : 
   12368       145108 :   gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
   12369              : }
   12370              : 
   12371              : 
   12372              : /* Generate code for assignment of scalar variables.  Includes character
   12373              :    strings and derived types with allocatable components.
   12374              :    If you know that the LHS has no allocations, set dealloc to false.
   12375              : 
   12376              :    DEEP_COPY has no effect if the typespec TS is not a derived type with
   12377              :    allocatable components.  Otherwise, if it is set, an explicit copy of each
   12378              :    allocatable component is made.  This is necessary as a simple copy of the
   12379              :    whole object would copy array descriptors as is, so that the lhs's
   12380              :    allocatable components would point to the rhs's after the assignment.
   12381              :    Typically, setting DEEP_COPY is necessary if the rhs is a variable, and not
   12382              :    necessary if the rhs is a non-pointer function, as the allocatable components
   12383              :    are not accessible by other means than the function's result after the
   12384              :    function has returned.  It is even more subtle when temporaries are involved,
   12385              :    as the two following examples show:
   12386              :     1.  When we evaluate an array constructor, a temporary is created.  Thus
   12387              :       there is theoretically no alias possible.  However, no deep copy is
   12388              :       made for this temporary, so that if the constructor is made of one or
   12389              :       more variable with allocatable components, those components still point
   12390              :       to the variable's: DEEP_COPY should be set for the assignment from the
   12391              :       temporary to the lhs in that case.
   12392              :     2.  When assigning a scalar to an array, we evaluate the scalar value out
   12393              :       of the loop, store it into a temporary variable, and assign from that.
   12394              :       In that case, deep copying when assigning to the temporary would be a
   12395              :       waste of resources; however deep copies should happen when assigning from
   12396              :       the temporary to each array element: again DEEP_COPY should be set for
   12397              :       the assignment from the temporary to the lhs.  */
   12398              : 
   12399              : tree
   12400       349181 : gfc_trans_scalar_assign (gfc_se *lse, gfc_se *rse, gfc_typespec ts,
   12401              :                          bool deep_copy, bool dealloc, bool in_coarray,
   12402              :                          bool assoc_assign)
   12403              : {
   12404       349181 :   stmtblock_t block;
   12405       349181 :   tree tmp;
   12406       349181 :   tree cond;
   12407       349181 :   int caf_mode;
   12408              : 
   12409       349181 :   gfc_init_block (&block);
   12410              : 
   12411       349181 :   if (ts.type == BT_CHARACTER)
   12412              :     {
   12413        34207 :       tree rlen = NULL;
   12414        34207 :       tree llen = NULL;
   12415              : 
   12416        34207 :       if (lse->string_length != NULL_TREE)
   12417              :         {
   12418        34207 :           gfc_conv_string_parameter (lse);
   12419        34207 :           gfc_add_block_to_block (&block, &lse->pre);
   12420        34207 :           llen = lse->string_length;
   12421              :         }
   12422              : 
   12423        34207 :       if (rse->string_length != NULL_TREE)
   12424              :         {
   12425        34207 :           gfc_conv_string_parameter (rse);
   12426        34207 :           gfc_add_block_to_block (&block, &rse->pre);
   12427        34207 :           rlen = rse->string_length;
   12428              :         }
   12429              : 
   12430        34207 :       gfc_trans_string_copy (&block, llen, lse->expr, ts.kind, rlen,
   12431              :                              rse->expr, ts.kind);
   12432              :     }
   12433       294250 :   else if (gfc_bt_struct (ts.type)
   12434       314974 :            && (ts.u.derived->attr.alloc_comp
   12435        13496 :                || (deep_copy && has_parameterized_comps (ts.u.derived))))
   12436              :     {
   12437         7552 :       tree tmp_var = NULL_TREE;
   12438         7552 :       cond = NULL_TREE;
   12439              : 
   12440              :       /* Are the rhs and the lhs the same?  */
   12441         7552 :       if (deep_copy)
   12442              :         {
   12443         4598 :           if (!TREE_CONSTANT (rse->expr) && !VAR_P (rse->expr))
   12444         3340 :             rse->expr = gfc_evaluate_now (rse->expr, &rse->pre);
   12445         4598 :           cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   12446              :                                   gfc_build_addr_expr (NULL_TREE, lse->expr),
   12447              :                                   gfc_build_addr_expr (NULL_TREE, rse->expr));
   12448         4598 :           cond = gfc_evaluate_now (cond, &lse->pre);
   12449              :         }
   12450              : 
   12451              :       /* Deallocate the lhs allocated components as long as it is not
   12452              :          the same as the rhs.  This must be done following the assignment
   12453              :          to prevent deallocating data that could be used in the rhs
   12454              :          expression.  */
   12455         7552 :       if (dealloc)
   12456              :         {
   12457         2145 :           tmp_var = gfc_evaluate_now (lse->expr, &lse->pre);
   12458         2145 :           tmp = gfc_deallocate_alloc_comp_no_caf (ts.u.derived, tmp_var,
   12459              :                                                   0, gfc_may_be_finalized (ts));
   12460         2145 :           if (deep_copy)
   12461          893 :             tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
   12462              :                             tmp);
   12463         2145 :           gfc_add_expr_to_block (&lse->post, tmp);
   12464              :         }
   12465              : 
   12466         7552 :       gfc_add_block_to_block (&block, &rse->pre);
   12467              : 
   12468              :       /* Skip finalization for self-assignment.  */
   12469         7552 :       if (deep_copy && lse->finalblock.head)
   12470              :         {
   12471           24 :           tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
   12472              :                           gfc_finish_block (&lse->finalblock));
   12473           24 :           gfc_add_expr_to_block (&block, tmp);
   12474              :         }
   12475              :       else
   12476         7528 :         gfc_add_block_to_block (&block, &lse->finalblock);
   12477              : 
   12478         7552 :       gfc_add_block_to_block (&block, &lse->pre);
   12479              : 
   12480         7552 :       if (TYPE_MAIN_VARIANT (TREE_TYPE (lse->expr))
   12481         7552 :           == TYPE_MAIN_VARIANT (TREE_TYPE (rse->expr)))
   12482         7204 :         gfc_add_modify (&block, lse->expr,
   12483         7204 :                         fold_convert (TREE_TYPE (lse->expr), rse->expr));
   12484              :       else
   12485              :         {
   12486          348 :           tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
   12487          348 :                                  TREE_TYPE (lse->expr), rse->expr);
   12488          348 :           gfc_add_modify (&block, lse->expr, tmp);
   12489              :         }
   12490              : 
   12491              :       /* Restore pointer address of coarray components.  */
   12492         7552 :       if (ts.u.derived->attr.coarray_comp && deep_copy && tmp_var != NULL_TREE)
   12493              :         {
   12494            5 :           tmp = gfc_reassign_alloc_comp_caf (ts.u.derived, tmp_var, lse->expr);
   12495            5 :           tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
   12496              :                           tmp);
   12497            5 :           gfc_add_expr_to_block (&block, tmp);
   12498              :         }
   12499              : 
   12500              :       /* Do a deep copy if the rhs is a variable, if it is not the
   12501              :          same as the lhs.  */
   12502         7552 :       if (deep_copy)
   12503              :         {
   12504         4598 :           caf_mode = in_coarray ? (GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
   12505              :                                        | GFC_STRUCTURE_CAF_MODE_IN_COARRAY) : 0;
   12506         4598 :           tmp = gfc_copy_alloc_comp (ts.u.derived, rse->expr, lse->expr, 0,
   12507              :                                      caf_mode);
   12508         4598 :           tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
   12509              :                           tmp);
   12510         4598 :           gfc_add_expr_to_block (&block, tmp);
   12511              :         }
   12512              :     }
   12513       307422 :   else if (gfc_bt_struct (ts.type))
   12514              :     {
   12515        13172 :       gfc_add_block_to_block (&block, &rse->pre);
   12516        13172 :       gfc_add_block_to_block (&block, &lse->finalblock);
   12517        13172 :       gfc_add_block_to_block (&block, &lse->pre);
   12518        13172 :       tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
   12519        13172 :                              TREE_TYPE (lse->expr), rse->expr);
   12520        13172 :       gfc_add_modify (&block, lse->expr, tmp);
   12521              :     }
   12522              :   /* If possible use the rhs vptr copy with trans_scalar_class_assign....  */
   12523       294250 :   else if (ts.type == BT_CLASS)
   12524              :     {
   12525          785 :       gfc_add_block_to_block (&block, &lse->pre);
   12526          785 :       gfc_add_block_to_block (&block, &rse->pre);
   12527          785 :       gfc_add_block_to_block (&block, &lse->finalblock);
   12528              : 
   12529          785 :       if (!trans_scalar_class_assign (&block, lse, rse))
   12530              :         {
   12531              :           /* ..otherwise assignment suffices. Note the use of VIEW_CONVERT_EXPR
   12532              :           for the lhs which ensures that class data rhs cast as a string
   12533              :           assigns correctly.  */
   12534          603 :           tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
   12535          603 :                                  TREE_TYPE (rse->expr), lse->expr);
   12536          603 :           gfc_add_modify (&block, tmp, rse->expr);
   12537              : 
   12538              :           /* Copy allocatable components but guard against class pointer
   12539              :              assign, which arrives here.  */
   12540              : #define DATA_DT ts.u.derived->components->ts.u.derived
   12541          603 :           if (deep_copy
   12542          158 :               && !(GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
   12543            0 :                    && GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
   12544          158 :               && ts.u.derived->components
   12545          761 :               && DATA_DT && DATA_DT->attr.alloc_comp)
   12546              :             {
   12547            6 :               caf_mode = in_coarray ? (GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
   12548              :                                        | GFC_STRUCTURE_CAF_MODE_IN_COARRAY)
   12549              :                                     : 0;
   12550            6 :               tmp = gfc_copy_alloc_comp (DATA_DT, rse->expr, lse->expr, 0,
   12551              :                                          caf_mode);
   12552            6 :               gfc_add_expr_to_block (&block, tmp);
   12553              :             }
   12554              : #undef DATA_DT
   12555              :         }
   12556              :     }
   12557       293465 :   else if (ts.type != BT_CLASS)
   12558              :     {
   12559       293465 :       gfc_add_block_to_block (&block, &lse->pre);
   12560       293465 :       gfc_add_block_to_block (&block, &rse->pre);
   12561              : 
   12562       293465 :       if (in_coarray)
   12563              :         {
   12564          907 :           if (flag_coarray == GFC_FCOARRAY_LIB && assoc_assign)
   12565              :             {
   12566            0 :               tree rtype = TREE_TYPE (TREE_TYPE (rse->expr));
   12567            0 :               tree rtoken = TYPE_LANG_SPECIFIC (rtype)->caf_token;
   12568            0 :               gfc_conv_descriptor_token_set (&block, lse->expr, rtoken);
   12569              :             }
   12570          907 :           if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (lse->expr)))
   12571            0 :             lse->expr = gfc_conv_array_data (lse->expr);
   12572          294 :           if (flag_coarray == GFC_FCOARRAY_SINGLE && assoc_assign
   12573          907 :               && !POINTER_TYPE_P (TREE_TYPE (rse->expr)))
   12574            0 :             rse->expr = gfc_build_addr_expr (NULL_TREE, rse->expr);
   12575              :         }
   12576       293465 :       gfc_add_modify (&block, lse->expr,
   12577       293465 :                       fold_convert (TREE_TYPE (lse->expr), rse->expr));
   12578              :     }
   12579              : 
   12580       349181 :   gfc_add_block_to_block (&block, &lse->post);
   12581       349181 :   gfc_add_block_to_block (&block, &rse->post);
   12582              : 
   12583       349181 :   return gfc_finish_block (&block);
   12584              : }
   12585              : 
   12586              : 
   12587              : /* There are quite a lot of restrictions on the optimisation in using an
   12588              :    array function assign without a temporary.  */
   12589              : 
   12590              : static bool
   12591        14574 : arrayfunc_assign_needs_temporary (gfc_expr * expr1, gfc_expr * expr2)
   12592              : {
   12593        14574 :   gfc_ref * ref;
   12594        14574 :   bool seen_array_ref;
   12595        14574 :   bool c = false;
   12596        14574 :   gfc_symbol *sym = expr1->symtree->n.sym;
   12597              : 
   12598              :   /* Play it safe with class functions assigned to a derived type.  */
   12599        14574 :   if (gfc_is_class_array_function (expr2)
   12600        14574 :       && expr1->ts.type == BT_DERIVED)
   12601              :     return true;
   12602              : 
   12603              :   /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION.  */
   12604        14550 :   if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
   12605              :     return true;
   12606              : 
   12607              :   /* Elemental functions are scalarized so that they don't need a
   12608              :      temporary in gfc_trans_assignment_1, so return a true.  Otherwise,
   12609              :      they would need special treatment in gfc_trans_arrayfunc_assign.  */
   12610         8561 :   if (expr2->value.function.esym != NULL
   12611         1595 :       && expr2->value.function.esym->attr.elemental)
   12612              :     return true;
   12613              : 
   12614              :   /* Need a temporary if rhs is not FULL or a contiguous section.  */
   12615         8196 :   if (expr1->ref && !(gfc_full_array_ref_p (expr1->ref, &c) || c))
   12616              :     return true;
   12617              : 
   12618              :   /* Need a temporary if EXPR1 can't be expressed as a descriptor.  */
   12619         7946 :   if (gfc_ref_needs_temporary_p (expr1->ref))
   12620              :     return true;
   12621              : 
   12622              :   /* Functions returning pointers or allocatables need temporaries.  */
   12623         7934 :   if (gfc_expr_attr (expr2).pointer
   12624         7934 :       || gfc_expr_attr (expr2).allocatable)
   12625              :     return true;
   12626              : 
   12627              :   /* Character array functions need temporaries unless the
   12628              :      character lengths are the same.  */
   12629         7558 :   if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
   12630              :     {
   12631          562 :       if (UNLIMITED_POLY (expr1))
   12632              :         return true;
   12633              : 
   12634          556 :       if (expr1->ts.u.cl->length == NULL
   12635          507 :             || expr1->ts.u.cl->length->expr_type != EXPR_CONSTANT)
   12636              :         return true;
   12637              : 
   12638          493 :       if (expr2->ts.u.cl->length == NULL
   12639          487 :             || expr2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
   12640              :         return true;
   12641              : 
   12642          475 :       if (mpz_cmp (expr1->ts.u.cl->length->value.integer,
   12643          475 :                      expr2->ts.u.cl->length->value.integer) != 0)
   12644              :         return true;
   12645              :     }
   12646              : 
   12647              :   /* Check that no LHS component references appear during an array
   12648              :      reference. This is needed because we do not have the means to
   12649              :      span any arbitrary stride with an array descriptor. This check
   12650              :      is not needed for the rhs because the function result has to be
   12651              :      a complete type.  */
   12652         7465 :   seen_array_ref = false;
   12653        14930 :   for (ref = expr1->ref; ref; ref = ref->next)
   12654              :     {
   12655         7478 :       if (ref->type == REF_ARRAY)
   12656              :         seen_array_ref= true;
   12657           13 :       else if (ref->type == REF_COMPONENT && seen_array_ref)
   12658              :         return true;
   12659              :     }
   12660              : 
   12661              :   /* Check for a dependency.  */
   12662         7452 :   if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
   12663              :                                    expr2->value.function.esym,
   12664              :                                    expr2->value.function.actual,
   12665              :                                    NOT_ELEMENTAL))
   12666              :     return true;
   12667              : 
   12668              :   /* If we have reached here with an intrinsic function, we do not
   12669              :      need a temporary except in the particular case that reallocation
   12670              :      on assignment is active and the lhs is allocatable and a target,
   12671              :      or a pointer which may be a subref pointer.  FIXME: The last
   12672              :      condition can go away when we use span in the intrinsics
   12673              :      directly.*/
   12674         7015 :   if (expr2->value.function.isym)
   12675         6131 :     return (flag_realloc_lhs && sym->attr.allocatable && sym->attr.target)
   12676        12316 :       || (sym->attr.pointer && sym->attr.subref_array_pointer);
   12677              : 
   12678              :   /* If the LHS is a dummy, we need a temporary if it is not
   12679              :      INTENT(OUT).  */
   12680          809 :   if (sym->attr.dummy && sym->attr.intent != INTENT_OUT)
   12681              :     return true;
   12682              : 
   12683              :   /* If the lhs has been host_associated, is in common, a pointer or is
   12684              :      a target and the function is not using a RESULT variable, aliasing
   12685              :      can occur and a temporary is needed.  */
   12686          803 :   if ((sym->attr.host_assoc
   12687          749 :            || sym->attr.in_common
   12688          743 :            || sym->attr.pointer
   12689          737 :            || sym->attr.cray_pointee
   12690          737 :            || sym->attr.target)
   12691           66 :         && expr2->symtree != NULL
   12692           66 :         && expr2->symtree->n.sym == expr2->symtree->n.sym->result)
   12693              :     return true;
   12694              : 
   12695              :   /* A PURE function can unconditionally be called without a temporary.  */
   12696          761 :   if (expr2->value.function.esym != NULL
   12697          730 :       && expr2->value.function.esym->attr.pure)
   12698              :     return false;
   12699              : 
   12700              :   /* Implicit_pure functions are those which could legally be declared
   12701              :      to be PURE.  */
   12702          733 :   if (expr2->value.function.esym != NULL
   12703          702 :       && expr2->value.function.esym->attr.implicit_pure)
   12704              :     return false;
   12705              : 
   12706          450 :   if (!sym->attr.use_assoc
   12707          450 :         && !sym->attr.in_common
   12708          450 :         && !sym->attr.pointer
   12709          444 :         && !sym->attr.target
   12710          444 :         && !sym->attr.cray_pointee
   12711          444 :         && expr2->value.function.esym)
   12712              :     {
   12713              :       /* A temporary is not needed if the function is not contained and
   12714              :          the variable is local or host associated and not a pointer or
   12715              :          a target.  */
   12716          413 :       if (!expr2->value.function.esym->attr.contained)
   12717              :         return false;
   12718              : 
   12719              :       /* A temporary is not needed if the lhs has never been host
   12720              :          associated and the procedure is contained.  */
   12721          164 :       else if (!sym->attr.host_assoc)
   12722              :         return false;
   12723              : 
   12724              :       /* A temporary is not needed if the variable is local and not
   12725              :          a pointer, a target or a result.  */
   12726            6 :       if (sym->ns->parent
   12727            0 :             && expr2->value.function.esym->ns == sym->ns->parent)
   12728            0 :         return false;
   12729              :     }
   12730              : 
   12731              :   /* Default to temporary use.  */
   12732              :   return true;
   12733              : }
   12734              : 
   12735              : 
   12736              : /* Provide the loop info so that the lhs descriptor can be built for
   12737              :    reallocatable assignments from extrinsic function calls.  */
   12738              : 
   12739              : static void
   12740          203 : realloc_lhs_loop_for_fcn_call (gfc_se *se, locus *where, gfc_ss **ss,
   12741              :                                gfc_loopinfo *loop)
   12742              : {
   12743              :   /* Signal that the function call should not be made by
   12744              :      gfc_conv_loop_setup.  */
   12745          203 :   se->ss->is_alloc_lhs = 1;
   12746          203 :   gfc_init_loopinfo (loop);
   12747          203 :   gfc_add_ss_to_loop (loop, *ss);
   12748          203 :   gfc_add_ss_to_loop (loop, se->ss);
   12749          203 :   gfc_conv_ss_startstride (loop);
   12750          203 :   gfc_conv_loop_setup (loop, where);
   12751          203 :   gfc_copy_loopinfo_to_se (se, loop);
   12752          203 :   gfc_add_block_to_block (&se->pre, &loop->pre);
   12753          203 :   gfc_add_block_to_block (&se->pre, &loop->post);
   12754          203 :   se->ss->is_alloc_lhs = 0;
   12755          203 : }
   12756              : 
   12757              : 
   12758              : /* For assignment to a reallocatable lhs from intrinsic functions,
   12759              :    replace the se.expr (ie. the result) with a temporary descriptor.
   12760              :    Null the data field so that the library allocates space for the
   12761              :    result. Free the data of the original descriptor after the function,
   12762              :    in case it appears in an argument expression and transfer the
   12763              :    result to the original descriptor.  */
   12764              : 
   12765              : static void
   12766         2137 : fcncall_realloc_result (gfc_se *se, int rank, tree dtype)
   12767              : {
   12768         2137 :   tree desc;
   12769         2137 :   tree res_desc;
   12770         2137 :   tree tmp;
   12771         2137 :   tree offset;
   12772         2137 :   tree zero_cond;
   12773         2137 :   tree not_same_shape;
   12774         2137 :   stmtblock_t shape_block;
   12775         2137 :   int n;
   12776              : 
   12777              :   /* Use the allocation done by the library.  Substitute the lhs
   12778              :      descriptor with a copy, whose data field is nulled.*/
   12779         2137 :   desc = build_fold_indirect_ref_loc (input_location, se->expr);
   12780         2137 :   if (POINTER_TYPE_P (TREE_TYPE (desc)))
   12781            9 :     desc = build_fold_indirect_ref_loc (input_location, desc);
   12782              : 
   12783         2137 :   res_desc = gfc_create_unallocated_library_result_descriptor (&se->pre, desc,
   12784              :                                                                dtype);
   12785         2137 :   se->expr = gfc_build_addr_expr (NULL_TREE, res_desc);
   12786              : 
   12787              :   /* Free the lhs after the function call and copy the result data to
   12788              :      the lhs descriptor.  */
   12789         2137 :   tmp = gfc_conv_descriptor_data_get (desc);
   12790         2137 :   zero_cond = fold_build2_loc (input_location, EQ_EXPR,
   12791              :                                logical_type_node, tmp,
   12792         2137 :                                build_int_cst (TREE_TYPE (tmp), 0));
   12793         2137 :   zero_cond = gfc_evaluate_now (zero_cond, &se->post);
   12794         2137 :   tmp = gfc_call_free (tmp);
   12795         2137 :   gfc_add_expr_to_block (&se->post, tmp);
   12796              : 
   12797         2137 :   tmp = gfc_conv_descriptor_data_get (res_desc);
   12798         2137 :   gfc_conv_descriptor_data_set (&se->post, desc, tmp);
   12799              : 
   12800              :   /* Check that the shapes are the same between lhs and expression.
   12801              :      The evaluation of the shape is done in 'shape_block' to avoid
   12802              :      uninitialized warnings from the lhs bounds. */
   12803         2137 :   not_same_shape = boolean_false_node;
   12804         2137 :   gfc_start_block (&shape_block);
   12805         9015 :   for (n = 0 ; n < rank; n++)
   12806              :     {
   12807         4741 :       tree tmp1;
   12808         4741 :       tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
   12809         4741 :       tmp1 = gfc_conv_descriptor_lbound_get (res_desc, gfc_rank_cst[n]);
   12810         4741 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
   12811              :                              gfc_array_index_type, tmp, tmp1);
   12812         4741 :       tmp1 = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
   12813         4741 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
   12814              :                              gfc_array_index_type, tmp, tmp1);
   12815         4741 :       tmp1 = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
   12816         4741 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
   12817              :                              gfc_array_index_type, tmp, tmp1);
   12818         4741 :       tmp = fold_build2_loc (input_location, NE_EXPR,
   12819              :                              logical_type_node, tmp,
   12820              :                              gfc_index_zero_node);
   12821         4741 :       tmp = gfc_evaluate_now (tmp, &shape_block);
   12822         4741 :       if (n == 0)
   12823              :         not_same_shape = tmp;
   12824              :       else
   12825         2604 :         not_same_shape = fold_build2_loc (input_location, TRUTH_OR_EXPR,
   12826              :                                           logical_type_node, tmp,
   12827              :                                           not_same_shape);
   12828              :     }
   12829              : 
   12830              :   /* 'zero_cond' being true is equal to lhs not being allocated or the
   12831              :      shapes being different.  */
   12832         2137 :   tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR, logical_type_node,
   12833              :                          zero_cond, not_same_shape);
   12834         2137 :   gfc_add_modify (&shape_block, zero_cond, tmp);
   12835         2137 :   tmp = gfc_finish_block (&shape_block);
   12836         2137 :   tmp = build3_v (COND_EXPR, zero_cond,
   12837              :                   build_empty_stmt (input_location), tmp);
   12838         2137 :   gfc_add_expr_to_block (&se->post, tmp);
   12839              : 
   12840              :   /* Now reset the bounds returned from the function call to bounds based
   12841              :      on the lhs lbounds, except where the lhs is not allocated or the shapes
   12842              :      of 'variable and 'expr' are different. Set the offset accordingly.  */
   12843         2137 :   offset = gfc_index_zero_node;
   12844         6878 :   for (n = 0 ; n < rank; n++)
   12845              :     {
   12846         4741 :       tree lbound;
   12847              : 
   12848         4741 :       lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
   12849         4741 :       lbound = fold_build3_loc (input_location, COND_EXPR,
   12850              :                                 gfc_array_index_type, zero_cond,
   12851              :                                 gfc_index_one_node, lbound);
   12852         4741 :       lbound = gfc_evaluate_now (lbound, &se->post);
   12853              : 
   12854         4741 :       tmp = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
   12855         4741 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
   12856              :                              gfc_array_index_type, tmp, lbound);
   12857         4741 :       gfc_conv_descriptor_lbound_set (&se->post, desc,
   12858              :                                       gfc_rank_cst[n], lbound);
   12859         4741 :       gfc_conv_descriptor_ubound_set (&se->post, desc,
   12860              :                                       gfc_rank_cst[n], tmp);
   12861              : 
   12862              :       /* Set stride and accumulate the offset.  */
   12863         4741 :       tmp = gfc_conv_descriptor_stride_get (res_desc, gfc_rank_cst[n]);
   12864         4741 :       gfc_conv_descriptor_stride_set (&se->post, desc,
   12865              :                                       gfc_rank_cst[n], tmp);
   12866         4741 :       tmp = fold_build2_loc (input_location, MULT_EXPR,
   12867              :                              gfc_array_index_type, lbound, tmp);
   12868         4741 :       offset = fold_build2_loc (input_location, MINUS_EXPR,
   12869              :                                 gfc_array_index_type, offset, tmp);
   12870         4741 :       offset = gfc_evaluate_now (offset, &se->post);
   12871              :     }
   12872              : 
   12873         2137 :   gfc_conv_descriptor_offset_set (&se->post, desc, offset);
   12874         2137 : }
   12875              : 
   12876              : 
   12877              : 
   12878              : /* Try to translate array(:) = func (...), where func is a transformational
   12879              :    array function, without using a temporary.  Returns NULL if this isn't the
   12880              :    case.  */
   12881              : 
   12882              : static tree
   12883        14614 : gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
   12884              : {
   12885        14614 :   gfc_se se;
   12886        14614 :   gfc_ss *ss = NULL;
   12887        14614 :   gfc_component *comp = NULL;
   12888        14614 :   gfc_loopinfo loop;
   12889        14614 :   tree tmp;
   12890        14614 :   tree lhs;
   12891        14614 :   gfc_se final_se;
   12892        14614 :   gfc_symbol *sym = expr1->symtree->n.sym;
   12893        14614 :   bool finalizable =  gfc_may_be_finalized (expr1->ts);
   12894              : 
   12895              :   /* If the symbol is host associated and has not been referenced in its name
   12896              :      space, it might be lacking a backend_decl and vtable.  */
   12897        14614 :   if (sym->backend_decl == NULL_TREE)
   12898              :     return NULL_TREE;
   12899              : 
   12900        14574 :   if (arrayfunc_assign_needs_temporary (expr1, expr2))
   12901              :     return NULL_TREE;
   12902              : 
   12903              :   /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
   12904              :      functions.  */
   12905         6891 :   comp = gfc_get_proc_ptr_comp (expr2);
   12906              : 
   12907         6891 :   if (!(expr2->value.function.isym
   12908          718 :               || (comp && comp->attr.dimension)
   12909          718 :               || (!comp && gfc_return_by_reference (expr2->value.function.esym)
   12910          718 :                   && expr2->value.function.esym->result->attr.dimension)))
   12911              :     return NULL_TREE;
   12912              : 
   12913         6891 :   gfc_init_se (&se, NULL);
   12914         6891 :   gfc_start_block (&se.pre);
   12915         6891 :   se.want_pointer = 1;
   12916              : 
   12917              :   /* First the lhs must be finalized, if necessary. We use a copy of the symbol
   12918              :      backend decl, stash the original away for the finalization so that the
   12919              :      value used is that before the assignment. This is necessary because
   12920              :      evaluation of the rhs expression using direct by reference can change
   12921              :      the value. However, the standard mandates that the finalization must occur
   12922              :      after evaluation of the rhs.  */
   12923         6891 :   gfc_init_se (&final_se, NULL);
   12924              : 
   12925         6891 :   if (finalizable)
   12926              :     {
   12927           45 :       tmp = sym->backend_decl;
   12928           45 :       lhs = sym->backend_decl;
   12929           45 :       if (INDIRECT_REF_P (tmp))
   12930            0 :         tmp = TREE_OPERAND (tmp, 0);
   12931           45 :       sym->backend_decl = gfc_create_var (TREE_TYPE (tmp), "lhs");
   12932           45 :       gfc_add_modify (&se.pre, sym->backend_decl, tmp);
   12933           45 :       if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
   12934              :         {
   12935            0 :           tmp = gfc_copy_alloc_comp (expr1->ts.u.derived, tmp, sym->backend_decl,
   12936              :                                      expr1->rank, 0);
   12937            0 :           gfc_add_expr_to_block (&final_se.pre, tmp);
   12938              :         }
   12939              :     }
   12940              : 
   12941           45 :   if (finalizable && gfc_assignment_finalizer_call (&final_se, expr1, false))
   12942              :     {
   12943           45 :       gfc_add_block_to_block (&se.pre, &final_se.pre);
   12944           45 :       gfc_add_block_to_block (&se.post, &final_se.finalblock);
   12945              :     }
   12946              : 
   12947         6891 :   if (finalizable)
   12948           45 :     sym->backend_decl = lhs;
   12949              : 
   12950         6891 :   gfc_conv_array_parameter (&se, expr1, false, NULL, NULL, NULL);
   12951              : 
   12952         6891 :   if (expr1->ts.type == BT_DERIVED
   12953          288 :         && expr1->ts.u.derived->attr.alloc_comp)
   12954              :     {
   12955          110 :       tmp = build_fold_indirect_ref_loc (input_location, se.expr);
   12956          110 :       tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, tmp,
   12957              :                                               expr1->rank);
   12958          110 :       gfc_add_expr_to_block (&se.pre, tmp);
   12959              :     }
   12960         6781 :   else if (expr1->ts.type == BT_DERIVED
   12961          178 :            && has_parameterized_comps (expr1->ts.u.derived) && expr1->rank > 0
   12962           24 :            && expr2->value.function.isym
   12963           24 :            && expr2->value.function.isym->transformational
   12964         6805 :            && expr2->value.function.isym->id != GFC_ISYM_REDUCE)
   12965              :     {
   12966           24 :       tmp = build_fold_indirect_ref_loc (input_location, se.expr);
   12967           24 :       tmp = gfc_deallocate_pdt_comp (expr1->ts.u.derived, tmp, expr1->rank);
   12968           24 :       gfc_add_expr_to_block (&se.pre, tmp);
   12969              :     }
   12970              : 
   12971         6891 :   se.direct_byref = 1;
   12972         6891 :   se.ss = gfc_walk_expr (expr2);
   12973         6891 :   gcc_assert (se.ss != gfc_ss_terminator);
   12974              : 
   12975              :   /* Since this is a direct by reference call, references to the lhs can be
   12976              :      used for finalization of the function result just as long as the blocks
   12977              :      from final_se are added at the right time.  */
   12978         6891 :   gfc_init_se (&final_se, NULL);
   12979         6891 :   if (finalizable && expr2->value.function.esym)
   12980              :     {
   12981           32 :       final_se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
   12982           32 :       gfc_finalize_tree_expr (&final_se, expr2->ts.u.derived,
   12983           32 :                                     expr2->value.function.esym->attr,
   12984              :                                     expr2->rank);
   12985              :     }
   12986              : 
   12987              :   /* Reallocate on assignment needs the loopinfo for extrinsic functions.
   12988              :      This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
   12989              :      Clearly, this cannot be done for an allocatable function result, since
   12990              :      the shape of the result is unknown and, in any case, the function must
   12991              :      correctly take care of the reallocation internally. For intrinsic
   12992              :      calls, the array data is freed and the library takes care of allocation.
   12993              :      TODO: Add logic of trans-array.cc: gfc_alloc_allocatable_for_assignment
   12994              :      to the library.  */
   12995         6891 :   if (flag_realloc_lhs
   12996         6816 :         && gfc_is_reallocatable_lhs (expr1)
   12997         9231 :         && !gfc_expr_attr (expr1).codimension
   12998         2340 :         && !gfc_is_coindexed (expr1)
   12999         9231 :         && !(expr2->value.function.esym
   13000          203 :             && expr2->value.function.esym->result->attr.allocatable))
   13001              :     {
   13002         2340 :       realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
   13003              : 
   13004         2340 :       if (!expr2->value.function.isym)
   13005              :         {
   13006          203 :           ss = gfc_walk_expr (expr1);
   13007          203 :           gcc_assert (ss != gfc_ss_terminator);
   13008              : 
   13009          203 :           realloc_lhs_loop_for_fcn_call (&se, &expr1->where, &ss, &loop);
   13010          203 :           ss->is_alloc_lhs = 1;
   13011              :         }
   13012              :       else
   13013              :         {
   13014         2137 :           tree dtype = NULL_TREE;
   13015         2137 :           tree type = gfc_typenode_for_spec (&expr2->ts);
   13016         2137 :           if (expr1->ts.type == BT_CLASS)
   13017              :             {
   13018           13 :               tmp = gfc_class_vptr_get (sym->backend_decl);
   13019           13 :               tree tmp2 = gfc_get_symbol_decl (gfc_find_vtab (&expr2->ts));
   13020           13 :               tmp2 = gfc_build_addr_expr (TREE_TYPE (tmp), tmp2);
   13021           13 :               gfc_add_modify (&se.pre, tmp, tmp2);
   13022           13 :               dtype = gfc_get_dtype_rank_type (expr1->rank,type);
   13023              :             }
   13024         2137 :           fcncall_realloc_result (&se, expr1->rank, dtype);
   13025              :         }
   13026              :     }
   13027              : 
   13028         6891 :   gfc_conv_function_expr (&se, expr2);
   13029              : 
   13030              :   /* Fix the result.  */
   13031         6891 :   gfc_add_block_to_block (&se.pre, &se.post);
   13032         6891 :   if (finalizable)
   13033           45 :     gfc_add_block_to_block (&se.pre, &final_se.pre);
   13034              : 
   13035              :   /* Do the finalization, including final calls from function arguments.  */
   13036           45 :   if (finalizable)
   13037              :     {
   13038           45 :       gfc_add_block_to_block (&se.pre, &final_se.post);
   13039           45 :       gfc_add_block_to_block (&se.pre, &se.finalblock);
   13040           45 :       gfc_add_block_to_block (&se.pre, &final_se.finalblock);
   13041              :    }
   13042              : 
   13043         6891 :   if (ss)
   13044          203 :     gfc_cleanup_loop (&loop);
   13045              :   else
   13046         6688 :     gfc_free_ss_chain (se.ss);
   13047              : 
   13048         6891 :   return gfc_finish_block (&se.pre);
   13049              : }
   13050              : 
   13051              : 
   13052              : /* Try to efficiently translate array(:) = 0.  Return NULL if this
   13053              :    can't be done.  */
   13054              : 
   13055              : static tree
   13056         4060 : gfc_trans_zero_assign (gfc_expr * expr)
   13057              : {
   13058         4060 :   tree dest, len, type;
   13059         4060 :   tree tmp;
   13060         4060 :   gfc_symbol *sym;
   13061              : 
   13062         4060 :   sym = expr->symtree->n.sym;
   13063         4060 :   dest = gfc_get_symbol_decl (sym);
   13064              : 
   13065         4060 :   type = TREE_TYPE (dest);
   13066         4060 :   if (POINTER_TYPE_P (type))
   13067          255 :     type = TREE_TYPE (type);
   13068         4060 :   if (GFC_ARRAY_TYPE_P (type))
   13069              :     {
   13070              :       /* Determine the length of the array.  */
   13071         2850 :       len = GFC_TYPE_ARRAY_SIZE (type);
   13072         2850 :       if (!len || TREE_CODE (len) != INTEGER_CST)
   13073              :         return NULL_TREE;
   13074              :     }
   13075         1210 :   else if (GFC_DESCRIPTOR_TYPE_P (type)
   13076         1210 :           && gfc_is_simply_contiguous (expr, false, false))
   13077              :     {
   13078         1098 :       if (POINTER_TYPE_P (TREE_TYPE (dest)))
   13079            4 :         dest = build_fold_indirect_ref_loc (input_location, dest);
   13080         1098 :       len = gfc_conv_descriptor_size (dest, GFC_TYPE_ARRAY_RANK (type));
   13081         1098 :       dest = gfc_conv_descriptor_data_get (dest);
   13082              :     }
   13083              :   else
   13084              :     return NULL_TREE;
   13085              : 
   13086              :   /* If we are zeroing a local array avoid taking its address by emitting
   13087              :      a = {} instead.  */
   13088         3763 :   if (!POINTER_TYPE_P (TREE_TYPE (dest)))
   13089         2622 :     return build2_loc (input_location, MODIFY_EXPR, void_type_node,
   13090         2622 :                        dest, build_constructor (TREE_TYPE (dest),
   13091         2622 :                                               NULL));
   13092              : 
   13093              :   /* Multiply len by element size.  */
   13094         1141 :   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
   13095         1141 :   len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
   13096              :                          len, fold_convert (gfc_array_index_type, tmp));
   13097              : 
   13098              :   /* Convert arguments to the correct types.  */
   13099         1141 :   dest = fold_convert (pvoid_type_node, dest);
   13100         1141 :   len = fold_convert (size_type_node, len);
   13101              : 
   13102              :   /* Construct call to __builtin_memset.  */
   13103         1141 :   tmp = build_call_expr_loc (input_location,
   13104              :                              builtin_decl_explicit (BUILT_IN_MEMSET),
   13105              :                              3, dest, integer_zero_node, len);
   13106         1141 :   return fold_convert (void_type_node, tmp);
   13107              : }
   13108              : 
   13109              : 
   13110              : /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
   13111              :    that constructs the call to __builtin_memcpy.  */
   13112              : 
   13113              : tree
   13114         8214 : gfc_build_memcpy_call (tree dst, tree src, tree len)
   13115              : {
   13116         8214 :   tree tmp;
   13117              : 
   13118              :   /* Convert arguments to the correct types.  */
   13119         8214 :   if (!POINTER_TYPE_P (TREE_TYPE (dst)))
   13120         7799 :     dst = gfc_build_addr_expr (pvoid_type_node, dst);
   13121              :   else
   13122          415 :     dst = fold_convert (pvoid_type_node, dst);
   13123              : 
   13124         8214 :   if (!POINTER_TYPE_P (TREE_TYPE (src)))
   13125         7716 :     src = gfc_build_addr_expr (pvoid_type_node, src);
   13126              :   else
   13127          498 :     src = fold_convert (pvoid_type_node, src);
   13128              : 
   13129         8214 :   len = fold_convert (size_type_node, len);
   13130              : 
   13131              :   /* Construct call to __builtin_memcpy.  */
   13132         8214 :   tmp = build_call_expr_loc (input_location,
   13133              :                              builtin_decl_explicit (BUILT_IN_MEMCPY),
   13134              :                              3, dst, src, len);
   13135         8214 :   return fold_convert (void_type_node, tmp);
   13136              : }
   13137              : 
   13138              : 
   13139              : /* Try to efficiently translate dst(:) = src(:).  Return NULL if this
   13140              :    can't be done.  EXPR1 is the destination/lhs and EXPR2 is the
   13141              :    source/rhs, both are gfc_full_array_ref_p which have been checked for
   13142              :    dependencies.  */
   13143              : 
   13144              : static tree
   13145         2585 : gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
   13146              : {
   13147         2585 :   tree dst, dlen, dtype;
   13148         2585 :   tree src, slen, stype;
   13149         2585 :   tree tmp;
   13150              : 
   13151         2585 :   dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
   13152         2585 :   src = gfc_get_symbol_decl (expr2->symtree->n.sym);
   13153              : 
   13154         2585 :   dtype = TREE_TYPE (dst);
   13155         2585 :   if (POINTER_TYPE_P (dtype))
   13156          265 :     dtype = TREE_TYPE (dtype);
   13157         2585 :   stype = TREE_TYPE (src);
   13158         2585 :   if (POINTER_TYPE_P (stype))
   13159          293 :     stype = TREE_TYPE (stype);
   13160              : 
   13161         2585 :   if (!GFC_ARRAY_TYPE_P (dtype) || !GFC_ARRAY_TYPE_P (stype))
   13162              :     return NULL_TREE;
   13163              : 
   13164              :   /* Determine the lengths of the arrays.  */
   13165         1581 :   dlen = GFC_TYPE_ARRAY_SIZE (dtype);
   13166         1581 :   if (!dlen || TREE_CODE (dlen) != INTEGER_CST)
   13167              :     return NULL_TREE;
   13168         1492 :   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
   13169         1492 :   dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
   13170              :                           dlen, fold_convert (gfc_array_index_type, tmp));
   13171              : 
   13172         1492 :   slen = GFC_TYPE_ARRAY_SIZE (stype);
   13173         1492 :   if (!slen || TREE_CODE (slen) != INTEGER_CST)
   13174              :     return NULL_TREE;
   13175         1486 :   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (stype));
   13176         1486 :   slen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
   13177              :                           slen, fold_convert (gfc_array_index_type, tmp));
   13178              : 
   13179              :   /* Sanity check that they are the same.  This should always be
   13180              :      the case, as we should already have checked for conformance.  */
   13181         1486 :   if (!tree_int_cst_equal (slen, dlen))
   13182              :     return NULL_TREE;
   13183              : 
   13184         1486 :   return gfc_build_memcpy_call (dst, src, dlen);
   13185              : }
   13186              : 
   13187              : 
   13188              : /* Try to efficiently translate array(:) = (/ ... /).  Return NULL if
   13189              :    this can't be done.  EXPR1 is the destination/lhs for which
   13190              :    gfc_full_array_ref_p is true, and EXPR2 is the source/rhs.  */
   13191              : 
   13192              : static tree
   13193         8380 : gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
   13194              : {
   13195         8380 :   unsigned HOST_WIDE_INT nelem;
   13196         8380 :   tree dst, dtype;
   13197         8380 :   tree src, stype;
   13198         8380 :   tree len;
   13199         8380 :   tree tmp;
   13200              : 
   13201         8380 :   nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
   13202         8380 :   if (nelem == 0)
   13203              :     return NULL_TREE;
   13204              : 
   13205         6923 :   dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
   13206         6923 :   dtype = TREE_TYPE (dst);
   13207         6923 :   if (POINTER_TYPE_P (dtype))
   13208          271 :     dtype = TREE_TYPE (dtype);
   13209         6923 :   if (!GFC_ARRAY_TYPE_P (dtype))
   13210              :     return NULL_TREE;
   13211              : 
   13212              :   /* Determine the lengths of the array.  */
   13213         6063 :   len = GFC_TYPE_ARRAY_SIZE (dtype);
   13214         6063 :   if (!len || TREE_CODE (len) != INTEGER_CST)
   13215              :     return NULL_TREE;
   13216              : 
   13217              :   /* Confirm that the constructor is the same size.  */
   13218         5959 :   if (compare_tree_int (len, nelem) != 0)
   13219              :     return NULL_TREE;
   13220              : 
   13221         5959 :   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
   13222         5959 :   len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
   13223              :                          fold_convert (gfc_array_index_type, tmp));
   13224              : 
   13225         5959 :   stype = gfc_typenode_for_spec (&expr2->ts);
   13226         5959 :   src = gfc_build_constant_array_constructor (expr2, stype);
   13227              : 
   13228         5959 :   return gfc_build_memcpy_call (dst, src, len);
   13229              : }
   13230              : 
   13231              : 
   13232              : /* Tells whether the expression is to be treated as a variable reference.  */
   13233              : 
   13234              : bool
   13235       323121 : gfc_expr_is_variable (gfc_expr *expr)
   13236              : {
   13237       323399 :   gfc_expr *arg;
   13238       323399 :   gfc_component *comp;
   13239       323399 :   gfc_symbol *func_ifc;
   13240              : 
   13241       323399 :   if (expr->expr_type == EXPR_VARIABLE)
   13242              :     return true;
   13243              : 
   13244       286530 :   arg = gfc_get_noncopying_intrinsic_argument (expr);
   13245       286530 :   if (arg)
   13246              :     {
   13247          278 :       gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
   13248              :       return gfc_expr_is_variable (arg);
   13249              :     }
   13250              : 
   13251              :   /* A data-pointer-returning function should be considered as a variable
   13252              :      too.  */
   13253       286252 :   if (expr->expr_type == EXPR_FUNCTION
   13254        38221 :       && expr->ref == NULL)
   13255              :     {
   13256        37790 :       if (expr->value.function.isym != NULL)
   13257              :         return false;
   13258              : 
   13259         9892 :       if (expr->value.function.esym != NULL)
   13260              :         {
   13261         9883 :           func_ifc = expr->value.function.esym;
   13262         9883 :           goto found_ifc;
   13263              :         }
   13264            9 :       gcc_assert (expr->symtree);
   13265            9 :       func_ifc = expr->symtree->n.sym;
   13266            9 :       goto found_ifc;
   13267              :     }
   13268              : 
   13269       248462 :   comp = gfc_get_proc_ptr_comp (expr);
   13270       248462 :   if ((expr->expr_type == EXPR_PPC || expr->expr_type == EXPR_FUNCTION)
   13271          431 :       && comp)
   13272              :     {
   13273          317 :       func_ifc = comp->ts.interface;
   13274          317 :       goto found_ifc;
   13275              :     }
   13276              : 
   13277       248145 :   if (expr->expr_type == EXPR_COMPCALL)
   13278              :     {
   13279            0 :       gcc_assert (!expr->value.compcall.tbp->is_generic);
   13280            0 :       func_ifc = expr->value.compcall.tbp->u.specific->n.sym;
   13281            0 :       goto found_ifc;
   13282              :     }
   13283              : 
   13284              :   return false;
   13285              : 
   13286        10209 : found_ifc:
   13287        10209 :   gcc_assert (func_ifc->attr.function
   13288              :               && func_ifc->result != NULL);
   13289        10209 :   return func_ifc->result->attr.pointer;
   13290              : }
   13291              : 
   13292              : 
   13293              : /* Is the lhs OK for automatic reallocation?  */
   13294              : 
   13295              : static bool
   13296       272882 : is_scalar_reallocatable_lhs (gfc_expr *expr)
   13297              : {
   13298       272882 :   gfc_ref * ref;
   13299              : 
   13300              :   /* An allocatable variable with no reference.  */
   13301       272882 :   if (expr->symtree->n.sym->attr.allocatable
   13302         6917 :         && !expr->ref)
   13303              :     return true;
   13304              : 
   13305              :   /* All that can be left are allocatable components.  However, we do
   13306              :      not check for allocatable components here because the expression
   13307              :      could be an allocatable component of a pointer component.  */
   13308       270007 :   if (expr->symtree->n.sym->ts.type != BT_DERIVED
   13309       245838 :         && expr->symtree->n.sym->ts.type != BT_CLASS)
   13310              :     return false;
   13311              : 
   13312              :   /* Find an allocatable component ref last.  */
   13313        42680 :   for (ref = expr->ref; ref; ref = ref->next)
   13314        17453 :     if (ref->type == REF_COMPONENT
   13315        12881 :           && !ref->next
   13316         9863 :           && ref->u.c.component->attr.allocatable)
   13317              :       return true;
   13318              : 
   13319              :   return false;
   13320              : }
   13321              : 
   13322              : 
   13323              : /* Allocate or reallocate scalar lhs, as necessary.  */
   13324              : 
   13325              : static void
   13326         3757 : alloc_scalar_allocatable_for_assignment (stmtblock_t *block,
   13327              :                                          tree string_length,
   13328              :                                          gfc_expr *expr1,
   13329              :                                          gfc_expr *expr2)
   13330              : 
   13331              : {
   13332         3757 :   tree cond;
   13333         3757 :   tree tmp;
   13334         3757 :   tree size;
   13335         3757 :   tree size_in_bytes;
   13336         3757 :   tree jump_label1;
   13337         3757 :   tree jump_label2;
   13338         3757 :   gfc_se lse;
   13339         3757 :   gfc_ref *ref;
   13340              : 
   13341         3757 :   if (!expr1 || expr1->rank)
   13342            0 :     return;
   13343              : 
   13344         3757 :   if (!expr2 || expr2->rank)
   13345              :     return;
   13346              : 
   13347         5265 :   for (ref = expr1->ref; ref; ref = ref->next)
   13348         1508 :     if (ref->type == REF_SUBSTRING)
   13349              :       return;
   13350              : 
   13351         3757 :   realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
   13352              : 
   13353              :   /* Since this is a scalar lhs, we can afford to do this.  That is,
   13354              :      there is no risk of side effects being repeated.  */
   13355         3757 :   gfc_init_se (&lse, NULL);
   13356         3757 :   lse.want_pointer = 1;
   13357         3757 :   gfc_conv_expr (&lse, expr1);
   13358              : 
   13359         3757 :   jump_label1 = gfc_build_label_decl (NULL_TREE);
   13360         3757 :   jump_label2 = gfc_build_label_decl (NULL_TREE);
   13361              : 
   13362              :   /* Do the allocation if the lhs is NULL. Otherwise go to label 1.  */
   13363         3757 :   tmp = build_int_cst (TREE_TYPE (lse.expr), 0);
   13364         3757 :   cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
   13365              :                           lse.expr, tmp);
   13366         3757 :   tmp = build3_v (COND_EXPR, cond,
   13367              :                   build1_v (GOTO_EXPR, jump_label1),
   13368              :                   build_empty_stmt (input_location));
   13369         3757 :   gfc_add_expr_to_block (block, tmp);
   13370              : 
   13371         3757 :   if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   13372              :     {
   13373              :       /* Use the rhs string length and the lhs element size. Note that 'size' is
   13374              :          used below for the string-length comparison, only.  */
   13375         1578 :       size = string_length;
   13376         1578 :       tmp = TYPE_SIZE_UNIT (gfc_get_char_type (expr1->ts.kind));
   13377         3156 :       size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
   13378         1578 :                                        TREE_TYPE (tmp), tmp,
   13379         1578 :                                        fold_convert (TREE_TYPE (tmp), size));
   13380              :     }
   13381              :   else
   13382              :     {
   13383              :       /* Otherwise use the length in bytes of the rhs.  */
   13384         2179 :       size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr1->ts));
   13385         2179 :       size_in_bytes = size;
   13386              :     }
   13387              : 
   13388         3757 :   size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
   13389              :                                    size_in_bytes, size_one_node);
   13390              : 
   13391         3757 :   if (gfc_caf_attr (expr1).codimension && flag_coarray == GFC_FCOARRAY_LIB)
   13392              :     {
   13393           32 :       tree caf_decl, token;
   13394           32 :       gfc_se caf_se;
   13395           32 :       symbol_attribute attr;
   13396              : 
   13397           32 :       gfc_clear_attr (&attr);
   13398           32 :       gfc_init_se (&caf_se, NULL);
   13399              : 
   13400           32 :       caf_decl = gfc_get_tree_for_caf_expr (expr1);
   13401           32 :       gfc_get_caf_token_offset (&caf_se, &token, NULL, caf_decl, NULL_TREE,
   13402              :                                 NULL);
   13403           32 :       gfc_add_block_to_block (block, &caf_se.pre);
   13404           32 :       gfc_allocate_allocatable (block, lse.expr, size_in_bytes,
   13405              :                                 gfc_build_addr_expr (NULL_TREE, token),
   13406              :                                 NULL_TREE, NULL_TREE, NULL_TREE, jump_label1,
   13407              :                                 expr1, 1);
   13408              :     }
   13409         3725 :   else if (expr1->ts.type == BT_DERIVED
   13410         3725 :            && (expr1->ts.u.derived->attr.alloc_comp
   13411          244 :                || has_parameterized_comps (expr1->ts.u.derived)))
   13412              :     {
   13413          152 :       tmp = build_call_expr_loc (input_location,
   13414              :                                  builtin_decl_explicit (BUILT_IN_CALLOC),
   13415              :                                  2, build_one_cst (size_type_node),
   13416              :                                  size_in_bytes);
   13417          152 :       tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
   13418          152 :       gfc_add_modify (block, lse.expr, tmp);
   13419              :     }
   13420              :   else
   13421              :     {
   13422         3573 :       tmp = build_call_expr_loc (input_location,
   13423              :                                  builtin_decl_explicit (BUILT_IN_MALLOC),
   13424              :                                  1, size_in_bytes);
   13425         3573 :       tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
   13426         3573 :       gfc_add_modify (block, lse.expr, tmp);
   13427              :     }
   13428              : 
   13429         3757 :   if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   13430              :     {
   13431              :       /* Deferred characters need checking for lhs and rhs string
   13432              :          length.  Other deferred parameter variables will have to
   13433              :          come here too.  */
   13434         1578 :       tmp = build1_v (GOTO_EXPR, jump_label2);
   13435         1578 :       gfc_add_expr_to_block (block, tmp);
   13436              :     }
   13437         3757 :   tmp = build1_v (LABEL_EXPR, jump_label1);
   13438         3757 :   gfc_add_expr_to_block (block, tmp);
   13439              : 
   13440              :   /* For a deferred length character, reallocate if lengths of lhs and
   13441              :      rhs are different.  */
   13442         3757 :   if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   13443              :     {
   13444         1578 :       cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   13445              :                               lse.string_length,
   13446         1578 :                               fold_convert (TREE_TYPE (lse.string_length),
   13447              :                                             size));
   13448              :       /* Jump past the realloc if the lengths are the same.  */
   13449         1578 :       tmp = build3_v (COND_EXPR, cond,
   13450              :                       build1_v (GOTO_EXPR, jump_label2),
   13451              :                       build_empty_stmt (input_location));
   13452         1578 :       gfc_add_expr_to_block (block, tmp);
   13453         1578 :       tmp = build_call_expr_loc (input_location,
   13454              :                                  builtin_decl_explicit (BUILT_IN_REALLOC),
   13455              :                                  2, fold_convert (pvoid_type_node, lse.expr),
   13456              :                                  size_in_bytes);
   13457         1578 :       tree omp_cond = NULL_TREE;
   13458         1578 :       if (flag_openmp_allocators)
   13459              :         {
   13460            1 :           tree omp_tmp;
   13461            1 :           omp_cond = gfc_omp_call_is_alloc (lse.expr);
   13462            1 :           omp_cond = gfc_evaluate_now (omp_cond, block);
   13463              : 
   13464            1 :           omp_tmp = builtin_decl_explicit (BUILT_IN_GOMP_REALLOC);
   13465            1 :           omp_tmp = build_call_expr_loc (input_location, omp_tmp, 4,
   13466              :                                          fold_convert (pvoid_type_node,
   13467              :                                                        lse.expr), size_in_bytes,
   13468              :                                          build_zero_cst (ptr_type_node),
   13469              :                                          build_zero_cst (ptr_type_node));
   13470            1 :           tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
   13471              :                             omp_cond, omp_tmp, tmp);
   13472              :         }
   13473         1578 :       tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
   13474         1578 :       gfc_add_modify (block, lse.expr, tmp);
   13475         1578 :       if (omp_cond)
   13476            1 :         gfc_add_expr_to_block (block,
   13477              :                                build3_loc (input_location, COND_EXPR,
   13478              :                                void_type_node, omp_cond,
   13479              :                                gfc_omp_call_add_alloc (lse.expr),
   13480              :                                build_empty_stmt (input_location)));
   13481         1578 :       tmp = build1_v (LABEL_EXPR, jump_label2);
   13482         1578 :       gfc_add_expr_to_block (block, tmp);
   13483              : 
   13484              :       /* Update the lhs character length.  */
   13485         1578 :       size = string_length;
   13486         1578 :       gfc_add_modify (block, lse.string_length,
   13487         1578 :                       fold_convert (TREE_TYPE (lse.string_length), size));
   13488              :     }
   13489              : }
   13490              : 
   13491              : /* Check for assignments of the type
   13492              : 
   13493              :    a = a + 4
   13494              : 
   13495              :    to make sure we do not check for reallocation unnecessarily.  */
   13496              : 
   13497              : 
   13498              : /* Strip parentheses from an expression to get the underlying variable.
   13499              :    This is needed for self-assignment detection since (a) creates a
   13500              :    parentheses operator node.  */
   13501              : 
   13502              : static gfc_expr *
   13503         8112 : strip_parentheses (gfc_expr *expr)
   13504              : {
   13505            0 :   while (expr->expr_type == EXPR_OP
   13506       324377 :          && expr->value.op.op == INTRINSIC_PARENTHESES)
   13507          605 :     expr = expr->value.op.op1;
   13508       323101 :   return expr;
   13509              : }
   13510              : 
   13511              : 
   13512              : static bool
   13513         7635 : is_runtime_conformable (gfc_expr *expr1, gfc_expr *expr2)
   13514              : {
   13515         8112 :   gfc_actual_arglist *a;
   13516         8112 :   gfc_expr *e1, *e2;
   13517              : 
   13518              :   /* Strip parentheses to handle cases like a = (a).  */
   13519        16275 :   expr1 = strip_parentheses (expr1);
   13520         8112 :   expr2 = strip_parentheses (expr2);
   13521              : 
   13522         8112 :   switch (expr2->expr_type)
   13523              :     {
   13524         2218 :     case EXPR_VARIABLE:
   13525         2218 :       return gfc_dep_compare_expr (expr1, expr2) == 0;
   13526              : 
   13527         2845 :     case EXPR_FUNCTION:
   13528         2845 :       if (expr2->value.function.esym
   13529          305 :           && expr2->value.function.esym->attr.elemental)
   13530              :         {
   13531           75 :           for (a = expr2->value.function.actual; a != NULL; a = a->next)
   13532              :             {
   13533           74 :               e1 = a->expr;
   13534           74 :               if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
   13535              :                 return false;
   13536              :             }
   13537              :           return true;
   13538              :         }
   13539         2783 :       else if (expr2->value.function.isym
   13540         2526 :                && expr2->value.function.isym->elemental)
   13541              :         {
   13542          332 :           for (a = expr2->value.function.actual; a != NULL; a = a->next)
   13543              :             {
   13544          322 :               e1 = a->expr;
   13545          322 :               if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
   13546              :                 return false;
   13547              :             }
   13548              :           return true;
   13549              :         }
   13550              : 
   13551              :       break;
   13552              : 
   13553          671 :     case EXPR_OP:
   13554          671 :       switch (expr2->value.op.op)
   13555              :         {
   13556           19 :         case INTRINSIC_NOT:
   13557           19 :         case INTRINSIC_UPLUS:
   13558           19 :         case INTRINSIC_UMINUS:
   13559           19 :         case INTRINSIC_PARENTHESES:
   13560           19 :           return is_runtime_conformable (expr1, expr2->value.op.op1);
   13561              : 
   13562          627 :         case INTRINSIC_PLUS:
   13563          627 :         case INTRINSIC_MINUS:
   13564          627 :         case INTRINSIC_TIMES:
   13565          627 :         case INTRINSIC_DIVIDE:
   13566          627 :         case INTRINSIC_POWER:
   13567          627 :         case INTRINSIC_AND:
   13568          627 :         case INTRINSIC_OR:
   13569          627 :         case INTRINSIC_EQV:
   13570          627 :         case INTRINSIC_NEQV:
   13571          627 :         case INTRINSIC_EQ:
   13572          627 :         case INTRINSIC_NE:
   13573          627 :         case INTRINSIC_GT:
   13574          627 :         case INTRINSIC_GE:
   13575          627 :         case INTRINSIC_LT:
   13576          627 :         case INTRINSIC_LE:
   13577          627 :         case INTRINSIC_EQ_OS:
   13578          627 :         case INTRINSIC_NE_OS:
   13579          627 :         case INTRINSIC_GT_OS:
   13580          627 :         case INTRINSIC_GE_OS:
   13581          627 :         case INTRINSIC_LT_OS:
   13582          627 :         case INTRINSIC_LE_OS:
   13583              : 
   13584          627 :           e1 = expr2->value.op.op1;
   13585          627 :           e2 = expr2->value.op.op2;
   13586              : 
   13587          627 :           if (e1->rank == 0 && e2->rank > 0)
   13588              :             return is_runtime_conformable (expr1, e2);
   13589          569 :           else if (e1->rank > 0 && e2->rank == 0)
   13590              :             return is_runtime_conformable (expr1, e1);
   13591          169 :           else if (e1->rank > 0 && e2->rank > 0)
   13592          169 :             return is_runtime_conformable (expr1, e1)
   13593          169 :               && is_runtime_conformable (expr1, e2);
   13594              :           break;
   13595              : 
   13596              :         default:
   13597              :           break;
   13598              : 
   13599              :         }
   13600              : 
   13601              :       break;
   13602              : 
   13603              :     default:
   13604              :       break;
   13605              :     }
   13606              :   return false;
   13607              : }
   13608              : 
   13609              : 
   13610              : static tree
   13611         3627 : trans_class_assignment (stmtblock_t *block, gfc_expr *lhs, gfc_expr *rhs,
   13612              :                         gfc_se *lse, gfc_se *rse, bool use_vptr_copy,
   13613              :                         bool class_realloc)
   13614              : {
   13615         3627 :   tree tmp, fcn, stdcopy, to_len, from_len, vptr, old_vptr, rhs_vptr;
   13616         3627 :   vec<tree, va_gc> *args = NULL;
   13617         3627 :   bool final_expr;
   13618              : 
   13619         3627 :   final_expr = gfc_assignment_finalizer_call (lse, lhs, false);
   13620         3627 :   if (final_expr)
   13621              :     {
   13622          521 :       if (rse->loop)
   13623          244 :         gfc_prepend_expr_to_block (&rse->loop->pre,
   13624              :                                    gfc_finish_block (&lse->finalblock));
   13625              :       else
   13626          277 :         gfc_add_block_to_block (block, &lse->finalblock);
   13627              :     }
   13628              : 
   13629              :   /* Store the old vptr so that dynamic types can be compared for
   13630              :      reallocation to occur or not.  */
   13631         3627 :   if (class_realloc)
   13632              :     {
   13633          313 :       tmp = lse->expr;
   13634          313 :       if (!GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
   13635            0 :         tmp = gfc_get_class_from_expr (tmp);
   13636              :     }
   13637              : 
   13638         3627 :   vptr = trans_class_vptr_len_assignment (block, lhs, rhs, rse, &to_len,
   13639              :                                           &from_len, &rhs_vptr);
   13640         3627 :   if (rhs_vptr == NULL_TREE)
   13641           43 :     rhs_vptr = vptr;
   13642              : 
   13643              :   /* Generate (re)allocation of the lhs.  */
   13644         3627 :   if (class_realloc)
   13645              :     {
   13646          313 :       stmtblock_t alloc, re_alloc;
   13647          313 :       tree class_han, re, size;
   13648              : 
   13649          313 :       if (tmp && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
   13650          313 :         old_vptr = gfc_evaluate_now (gfc_class_vptr_get (tmp), block);
   13651              :       else
   13652            0 :         old_vptr = build_int_cst (TREE_TYPE (vptr), 0);
   13653              : 
   13654          313 :       size = gfc_vptr_size_get (rhs_vptr);
   13655              : 
   13656              :       /* Take into account _len of unlimited polymorphic entities.
   13657              :          TODO: handle class(*) allocatable function results on rhs.  */
   13658          313 :       if (UNLIMITED_POLY (rhs))
   13659              :         {
   13660           18 :           tree len;
   13661           18 :           if (rhs->expr_type == EXPR_VARIABLE)
   13662           12 :             len = trans_get_upoly_len (block, rhs);
   13663              :           else
   13664            6 :             len = gfc_class_len_get (tmp);
   13665           18 :           len = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
   13666              :                                  fold_convert (size_type_node, len),
   13667              :                                  size_one_node);
   13668           18 :           size = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (size),
   13669           18 :                                   size, fold_convert (TREE_TYPE (size), len));
   13670           18 :         }
   13671          295 :       else if (rhs->ts.type == BT_CHARACTER && rse->string_length)
   13672           27 :         size = fold_build2_loc (input_location, MULT_EXPR,
   13673              :                                 gfc_charlen_type_node, size,
   13674              :                                 rse->string_length);
   13675              : 
   13676              : 
   13677          313 :       tmp = lse->expr;
   13678          313 :       class_han = GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
   13679          313 :           ? gfc_class_data_get (tmp) : tmp;
   13680              : 
   13681          313 :       if (!POINTER_TYPE_P (TREE_TYPE (class_han)))
   13682            0 :         class_han = gfc_build_addr_expr (NULL_TREE, class_han);
   13683              : 
   13684              :       /* Allocate block.  */
   13685          313 :       gfc_init_block (&alloc);
   13686          313 :       gfc_allocate_using_malloc (&alloc, class_han, size, NULL_TREE);
   13687              : 
   13688              :       /* Reallocate if dynamic types are different. */
   13689          313 :       gfc_init_block (&re_alloc);
   13690          313 :       if (UNLIMITED_POLY (lhs) && rhs->ts.type == BT_CHARACTER)
   13691              :         {
   13692           27 :           gfc_add_expr_to_block (&re_alloc, gfc_call_free (class_han));
   13693           27 :           gfc_allocate_using_malloc (&re_alloc, class_han, size, NULL_TREE);
   13694              :         }
   13695              :       else
   13696              :         {
   13697          286 :           tmp = fold_convert (pvoid_type_node, class_han);
   13698          286 :           re = build_call_expr_loc (input_location,
   13699              :                                     builtin_decl_explicit (BUILT_IN_REALLOC),
   13700              :                                     2, tmp, size);
   13701          286 :           re = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (tmp),
   13702              :                                 tmp, re);
   13703          286 :           tmp = fold_build2_loc (input_location, NE_EXPR,
   13704              :                                  logical_type_node, rhs_vptr, old_vptr);
   13705          286 :           re = fold_build3_loc (input_location, COND_EXPR, void_type_node,
   13706              :                                 tmp, re, build_empty_stmt (input_location));
   13707          286 :           gfc_add_expr_to_block (&re_alloc, re);
   13708              :         }
   13709          313 :       tree realloc_expr = lhs->ts.type == BT_CLASS ?
   13710          313 :                                           gfc_finish_block (&re_alloc) :
   13711            0 :                                           build_empty_stmt (input_location);
   13712              : 
   13713              :       /* Allocate if _data is NULL, reallocate otherwise.  */
   13714          313 :       tmp = fold_build2_loc (input_location, EQ_EXPR,
   13715              :                              logical_type_node, class_han,
   13716              :                              build_int_cst (prvoid_type_node, 0));
   13717          313 :       tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
   13718              :                              gfc_unlikely (tmp,
   13719              :                                            PRED_FORTRAN_FAIL_ALLOC),
   13720              :                              gfc_finish_block (&alloc),
   13721              :                              realloc_expr);
   13722          313 :       gfc_add_expr_to_block (&lse->pre, tmp);
   13723              :     }
   13724              : 
   13725         3627 :   fcn = gfc_vptr_copy_get (vptr);
   13726              : 
   13727         3627 :   tmp = GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr))
   13728         3627 :       ? gfc_class_data_get (rse->expr) : rse->expr;
   13729         3627 :   if (use_vptr_copy)
   13730              :     {
   13731         6036 :       if (!POINTER_TYPE_P (TREE_TYPE (tmp))
   13732          578 :           || INDIRECT_REF_P (tmp)
   13733          421 :           || (rhs->ts.type == BT_DERIVED
   13734            0 :               && rhs->ts.u.derived->attr.unlimited_polymorphic
   13735            0 :               && !rhs->ts.u.derived->attr.pointer
   13736            0 :               && !rhs->ts.u.derived->attr.allocatable)
   13737         3725 :           || (UNLIMITED_POLY (rhs)
   13738          134 :               && !CLASS_DATA (rhs)->attr.pointer
   13739           43 :               && !CLASS_DATA (rhs)->attr.allocatable))
   13740         2883 :         vec_safe_push (args, gfc_build_addr_expr (NULL_TREE, tmp));
   13741              :       else
   13742          421 :         vec_safe_push (args, tmp);
   13743         3304 :       tmp = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
   13744         3304 :           ? gfc_class_data_get (lse->expr) : lse->expr;
   13745         5762 :       if (!POINTER_TYPE_P (TREE_TYPE (tmp))
   13746          846 :           || INDIRECT_REF_P (tmp)
   13747          313 :           || (lhs->ts.type == BT_DERIVED
   13748            0 :               && lhs->ts.u.derived->attr.unlimited_polymorphic
   13749            0 :               && !lhs->ts.u.derived->attr.pointer
   13750            0 :               && !lhs->ts.u.derived->attr.allocatable)
   13751         3617 :           || (UNLIMITED_POLY (lhs)
   13752          125 :               && !CLASS_DATA (lhs)->attr.pointer
   13753          125 :               && !CLASS_DATA (lhs)->attr.allocatable))
   13754         2991 :         vec_safe_push (args, gfc_build_addr_expr (NULL_TREE, tmp));
   13755              :       else
   13756          313 :         vec_safe_push (args, tmp);
   13757              : 
   13758         3304 :       stdcopy = build_call_vec (TREE_TYPE (TREE_TYPE (fcn)), fcn, args);
   13759              : 
   13760         3304 :       if (to_len != NULL_TREE && !integer_zerop (from_len))
   13761              :         {
   13762          442 :           tree extcopy;
   13763          442 :           vec_safe_push (args, from_len);
   13764          442 :           vec_safe_push (args, to_len);
   13765          442 :           extcopy = build_call_vec (TREE_TYPE (TREE_TYPE (fcn)), fcn, args);
   13766              : 
   13767          442 :           tmp = fold_build2_loc (input_location, GT_EXPR,
   13768              :                                  logical_type_node, from_len,
   13769          442 :                                  build_zero_cst (TREE_TYPE (from_len)));
   13770          442 :           return fold_build3_loc (input_location, COND_EXPR,
   13771              :                                   void_type_node, tmp,
   13772          442 :                                   extcopy, stdcopy);
   13773              :         }
   13774              :       else
   13775              :         return stdcopy;
   13776              :     }
   13777              :   else
   13778              :     {
   13779          323 :       tree rhst = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
   13780          323 :           ? gfc_class_data_get (lse->expr) : lse->expr;
   13781          323 :       stmtblock_t tblock;
   13782          323 :       gfc_init_block (&tblock);
   13783          323 :       if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
   13784            0 :         tmp = gfc_build_addr_expr (NULL_TREE, tmp);
   13785          323 :       if (!POINTER_TYPE_P (TREE_TYPE (rhst)))
   13786            0 :         rhst = gfc_build_addr_expr (NULL_TREE, rhst);
   13787              :       /* When coming from a ptr_copy lhs and rhs are swapped.  */
   13788          323 :       gfc_add_modify_loc (input_location, &tblock, rhst,
   13789          323 :                           fold_convert (TREE_TYPE (rhst), tmp));
   13790          323 :       return gfc_finish_block (&tblock);
   13791              :     }
   13792              : }
   13793              : 
   13794              : bool
   13795       317163 : is_assoc_assign (gfc_expr *lhs, gfc_expr *rhs)
   13796              : {
   13797       317163 :   if (lhs->expr_type != EXPR_VARIABLE || rhs->expr_type != EXPR_VARIABLE)
   13798              :     return false;
   13799              : 
   13800        33276 :   return lhs->symtree->n.sym->assoc
   13801        33276 :          && lhs->symtree->n.sym->assoc->target == rhs;
   13802              : }
   13803              : 
   13804              : /* Subroutine of gfc_trans_assignment that actually scalarizes the
   13805              :    assignment.  EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
   13806              :    init_flag indicates initialization expressions and dealloc that no
   13807              :    deallocate prior assignment is needed (if in doubt, set true).
   13808              :    When PTR_COPY is set and expr1 is a class type, then use the _vptr-copy
   13809              :    routine instead of a pointer assignment.  Alias resolution is only done,
   13810              :    when MAY_ALIAS is set (the default).  This flag is used by ALLOCATE()
   13811              :    where it is known, that newly allocated memory on the lhs can never be
   13812              :    an alias of the rhs.  */
   13813              : 
   13814              : static tree
   13815       317163 : gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
   13816              :                         bool dealloc, bool use_vptr_copy, bool may_alias)
   13817              : {
   13818       317163 :   gfc_se lse;
   13819       317163 :   gfc_se rse;
   13820       317163 :   gfc_ss *lss;
   13821       317163 :   gfc_ss *lss_section;
   13822       317163 :   gfc_ss *rss;
   13823       317163 :   gfc_loopinfo loop;
   13824       317163 :   tree tmp;
   13825       317163 :   stmtblock_t block;
   13826       317163 :   stmtblock_t body;
   13827       317163 :   bool final_expr;
   13828       317163 :   bool l_is_temp;
   13829       317163 :   bool scalar_to_array;
   13830       317163 :   tree string_length;
   13831       317163 :   int n;
   13832       317163 :   bool maybe_workshare = false, lhs_refs_comp = false, rhs_refs_comp = false;
   13833       317163 :   symbol_attribute lhs_caf_attr, rhs_caf_attr, lhs_attr, rhs_attr;
   13834       317163 :   bool is_poly_assign;
   13835       317163 :   bool realloc_flag;
   13836       317163 :   bool assoc_assign = false;
   13837       317163 :   bool dummy_class_array_copy;
   13838              : 
   13839              :   /* Assignment of the form lhs = rhs.  */
   13840       317163 :   gfc_start_block (&block);
   13841              : 
   13842       317163 :   gfc_init_se (&lse, NULL);
   13843       317163 :   gfc_init_se (&rse, NULL);
   13844              : 
   13845       317163 :   gfc_fix_class_refs (expr1);
   13846              : 
   13847       634326 :   realloc_flag = flag_realloc_lhs
   13848       310735 :                  && gfc_is_reallocatable_lhs (expr1)
   13849         8468 :                  && expr2->rank
   13850       324138 :                  && !is_runtime_conformable (expr1, expr2);
   13851              : 
   13852              :   /* Walk the lhs.  */
   13853       317163 :   lss = gfc_walk_expr (expr1);
   13854       317163 :   if (realloc_flag)
   13855              :     {
   13856         6592 :       lss->no_bounds_check = 1;
   13857         6592 :       lss->is_alloc_lhs = 1;
   13858              :     }
   13859              :   else
   13860       310571 :     lss->no_bounds_check = expr1->no_bounds_check;
   13861              : 
   13862       317163 :   rss = NULL;
   13863              : 
   13864       317163 :   if (expr2->expr_type != EXPR_VARIABLE
   13865       317163 :       && expr2->expr_type != EXPR_CONSTANT
   13866       317163 :       && (expr2->ts.type == BT_CLASS || gfc_may_be_finalized (expr2->ts)))
   13867              :     {
   13868          924 :       expr2->must_finalize = 1;
   13869              :       /* F2023 7.5.6.3: If an executable construct references a nonpointer
   13870              :          function, the result is finalized after execution of the innermost
   13871              :          executable construct containing the reference.  */
   13872          924 :       if (expr2->expr_type == EXPR_FUNCTION
   13873          924 :           && (gfc_expr_attr (expr2).pointer
   13874          310 :               || (expr2->ts.type == BT_CLASS && CLASS_DATA (expr2)->attr.class_pointer)))
   13875          147 :         expr2->must_finalize = 0;
   13876              :       /* F2008 4.5.6.3 para 5: If an executable construct references a
   13877              :          structure constructor or array constructor, the entity created by
   13878              :          the constructor is finalized after execution of the innermost
   13879              :          executable construct containing the reference.
   13880              :          These finalizations were later deleted by the Combined Technical
   13881              :          Corrigenda 1 TO 4 for fortran 2008 (f08/0011).  */
   13882          777 :       else if (gfc_notification_std (GFC_STD_F2018_DEL)
   13883          777 :           && (expr2->expr_type == EXPR_STRUCTURE
   13884          734 :               || expr2->expr_type == EXPR_ARRAY))
   13885          405 :         expr2->must_finalize = 0;
   13886              :     }
   13887              : 
   13888              : 
   13889              :   /* Checking whether a class assignment is desired is quite complicated and
   13890              :      needed at two locations, so do it once only before the information is
   13891              :      needed.  */
   13892       317163 :   lhs_attr = gfc_expr_attr (expr1);
   13893       317163 :   rhs_attr = gfc_expr_attr (expr2);
   13894       317163 :   dummy_class_array_copy
   13895       634326 :     = (expr2->expr_type == EXPR_VARIABLE
   13896        33276 :        && expr2->rank > 0
   13897         8546 :        && expr2->symtree != NULL
   13898         8546 :        && expr2->symtree->n.sym->attr.dummy
   13899         1513 :        && expr2->ts.type == BT_CLASS
   13900          169 :        && !rhs_attr.pointer
   13901          163 :        && !rhs_attr.allocatable
   13902          150 :        && !CLASS_DATA (expr2)->attr.class_pointer
   13903       317313 :        && !CLASS_DATA (expr2)->attr.allocatable);
   13904              : 
   13905              :   /* What can be sent to trans_class_assignment includes all the obvious
   13906              :      candidates but scalar assignment of a class expression to a derived type
   13907              :      must be done using gfc_trans_scalar_assign; partly because it is simpler
   13908              :      and partly because some cases fail, eg. class assignment to derived_type
   13909              :      select type temporaries.  */
   13910       317163 :   is_poly_assign
   13911       317163 :     = (use_vptr_copy
   13912       299019 :        || ((lhs_attr.pointer || lhs_attr.allocatable) && !lhs_attr.dimension))
   13913        24226 :       && (expr1->ts.type == BT_CLASS || gfc_is_class_array_ref (expr1, NULL)
   13914        22025 :           || gfc_is_class_scalar_expr (expr1)
   13915        20587 :           || gfc_is_class_array_ref (expr2, NULL)
   13916        20587 :           || (gfc_is_class_scalar_expr (expr2)
   13917           42 :               && !(expr1->ts.type == BT_DERIVED && !lhs_attr.dimension)))
   13918       320802 :       && lhs_attr.flavor != FL_PROCEDURE;
   13919              : 
   13920       317163 :   assoc_assign = is_assoc_assign (expr1, expr2);
   13921              : 
   13922              :   /* Only analyze the expressions for coarray properties, when in coarray-lib
   13923              :      mode.  Avoid false-positive uninitialized diagnostics with initializing
   13924              :      the codimension flag unconditionally.  */
   13925       317163 :   lhs_caf_attr.codimension = false;
   13926       317163 :   rhs_caf_attr.codimension = false;
   13927       317163 :   if (flag_coarray == GFC_FCOARRAY_LIB)
   13928              :     {
   13929         7039 :       lhs_caf_attr = gfc_caf_attr (expr1, false, &lhs_refs_comp);
   13930         7039 :       rhs_caf_attr = gfc_caf_attr (expr2, false, &rhs_refs_comp);
   13931              :     }
   13932              : 
   13933       317163 :   tree reallocation = NULL_TREE;
   13934       317163 :   if (lss != gfc_ss_terminator)
   13935              :     {
   13936              :       /* The assignment needs scalarization.  */
   13937              :       lss_section = lss;
   13938              : 
   13939              :       /* Find a non-scalar SS from the lhs.  */
   13940              :       while (lss_section != gfc_ss_terminator
   13941        41319 :              && lss_section->info->type != GFC_SS_SECTION)
   13942            0 :         lss_section = lss_section->next;
   13943              : 
   13944        41319 :       gcc_assert (lss_section != gfc_ss_terminator);
   13945              : 
   13946              :       /* Initialize the scalarizer.  */
   13947        41319 :       gfc_init_loopinfo (&loop);
   13948              : 
   13949              :       /* Walk the rhs.  */
   13950        41319 :       rss = gfc_walk_expr (expr2);
   13951        41319 :       if (rss == gfc_ss_terminator)
   13952              :         {
   13953              :           /* The rhs is scalar.  Add a ss for the expression.  */
   13954        15539 :           rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
   13955        15539 :           lss->is_alloc_lhs = 0;
   13956              :         }
   13957              : 
   13958              :       /* When doing a class assign, then the handle to the rhs needs to be a
   13959              :          pointer to allow for polymorphism.  */
   13960        41319 :       if (is_poly_assign && expr2->rank == 0 && !UNLIMITED_POLY (expr2))
   13961          557 :         rss->info->type = GFC_SS_REFERENCE;
   13962              : 
   13963        41319 :       rss->no_bounds_check = expr2->no_bounds_check;
   13964              :       /* Associate the SS with the loop.  */
   13965        41319 :       gfc_add_ss_to_loop (&loop, lss);
   13966        41319 :       gfc_add_ss_to_loop (&loop, rss);
   13967              : 
   13968              :       /* Calculate the bounds of the scalarization.  */
   13969        41319 :       gfc_conv_ss_startstride (&loop);
   13970              :       /* Enable loop reversal.  */
   13971       702423 :       for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
   13972       619785 :         loop.reverse[n] = GFC_ENABLE_REVERSE;
   13973              :       /* Resolve any data dependencies in the statement.  */
   13974        41319 :       if (may_alias)
   13975        38956 :         gfc_conv_resolve_dependencies (&loop, lss, rss);
   13976              :       /* Setup the scalarizing loops.  */
   13977        41319 :       gfc_conv_loop_setup (&loop, &expr2->where);
   13978              : 
   13979              :       /* Setup the gfc_se structures.  */
   13980        41319 :       gfc_copy_loopinfo_to_se (&lse, &loop);
   13981        41319 :       gfc_copy_loopinfo_to_se (&rse, &loop);
   13982              : 
   13983        41319 :       rse.ss = rss;
   13984        41319 :       gfc_mark_ss_chain_used (rss, 1);
   13985        41319 :       if (loop.temp_ss == NULL)
   13986              :         {
   13987        40198 :           lse.ss = lss;
   13988        40198 :           gfc_mark_ss_chain_used (lss, 1);
   13989              :         }
   13990              :       else
   13991              :         {
   13992         1121 :           lse.ss = loop.temp_ss;
   13993         1121 :           gfc_mark_ss_chain_used (lss, 3);
   13994         1121 :           gfc_mark_ss_chain_used (loop.temp_ss, 3);
   13995              :         }
   13996              : 
   13997              :       /* Allow the scalarizer to workshare array assignments.  */
   13998        41319 :       if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_BODY))
   13999              :           == OMPWS_WORKSHARE_FLAG
   14000           85 :           && loop.temp_ss == NULL)
   14001              :         {
   14002           73 :           maybe_workshare = true;
   14003           73 :           ompws_flags |= OMPWS_SCALARIZER_WS | OMPWS_SCALARIZER_BODY;
   14004              :         }
   14005              : 
   14006              :       /* F2003: Allocate or reallocate lhs of allocatable array.  */
   14007        41319 :       if (realloc_flag)
   14008              :         {
   14009         6592 :           realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
   14010         6592 :           ompws_flags &= ~OMPWS_SCALARIZER_WS;
   14011         6592 :           reallocation = gfc_alloc_allocatable_for_assignment (&loop, expr1,
   14012              :                                                                expr2);
   14013              :         }
   14014              : 
   14015              :       /* Start the scalarized loop body.  */
   14016        41319 :       gfc_start_scalarized_body (&loop, &body);
   14017              :     }
   14018              :   else
   14019       275844 :     gfc_init_block (&body);
   14020              : 
   14021       317163 :   l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
   14022              : 
   14023              :   /* Translate the expression.  */
   14024       634326 :   rse.want_coarray = flag_coarray == GFC_FCOARRAY_LIB
   14025       317163 :                      && (init_flag || assoc_assign) && lhs_caf_attr.codimension;
   14026       317163 :   rse.want_pointer = rse.want_coarray && !init_flag && !lhs_caf_attr.dimension;
   14027       317163 :   gfc_conv_expr (&rse, expr2);
   14028              : 
   14029              :   /* Deal with the case of a scalar class function assigned to a derived type.
   14030              :    */
   14031       317163 :   if (gfc_is_alloc_class_scalar_function (expr2)
   14032       317163 :       && expr1->ts.type == BT_DERIVED)
   14033              :     {
   14034           60 :       rse.expr = gfc_class_data_get (rse.expr);
   14035           60 :       rse.expr = build_fold_indirect_ref_loc (input_location, rse.expr);
   14036              :     }
   14037              : 
   14038              :   /* Stabilize a string length for temporaries.  */
   14039       317163 :   if (expr2->ts.type == BT_CHARACTER && !expr1->ts.deferred
   14040        25148 :       && !(VAR_P (rse.string_length)
   14041              :            || TREE_CODE (rse.string_length) == PARM_DECL
   14042              :            || INDIRECT_REF_P (rse.string_length)))
   14043        24260 :     string_length = gfc_evaluate_now (rse.string_length, &rse.pre);
   14044       292903 :   else if (expr2->ts.type == BT_CHARACTER)
   14045              :     {
   14046         4502 :       if (expr1->ts.deferred
   14047         7001 :           && gfc_expr_attr (expr1).allocatable
   14048         7121 :           && gfc_check_dependency (expr1, expr2, true))
   14049          120 :         rse.string_length =
   14050          120 :           gfc_evaluate_now_function_scope (rse.string_length, &rse.pre);
   14051         4502 :       string_length = rse.string_length;
   14052              :     }
   14053              :   else
   14054              :     string_length = NULL_TREE;
   14055              : 
   14056       317163 :   if (l_is_temp)
   14057              :     {
   14058         1121 :       gfc_conv_tmp_array_ref (&lse);
   14059         1121 :       if (expr2->ts.type == BT_CHARACTER)
   14060          123 :         lse.string_length = string_length;
   14061              :     }
   14062              :   else
   14063              :     {
   14064       316042 :       gfc_conv_expr (&lse, expr1);
   14065              :       /* For some expression (e.g. complex numbers) fold_convert uses a
   14066              :          SAVE_EXPR, which is hazardous on the lhs, because the value is
   14067              :          not updated when assigned to.  */
   14068       316042 :       if (TREE_CODE (lse.expr) == SAVE_EXPR)
   14069            8 :         lse.expr = TREE_OPERAND (lse.expr, 0);
   14070              : 
   14071         6183 :       if (gfc_option.rtcheck & GFC_RTCHECK_MEM && !init_flag
   14072       322225 :           && gfc_expr_attr (expr1).allocatable && expr1->rank && !expr2->rank)
   14073              :         {
   14074           36 :           tree cond;
   14075           36 :           const char* msg;
   14076              : 
   14077           36 :           tmp = INDIRECT_REF_P (lse.expr)
   14078           36 :               ? gfc_build_addr_expr (NULL_TREE, lse.expr) : lse.expr;
   14079           36 :           STRIP_NOPS (tmp);
   14080              : 
   14081              :           /* We should only get array references here.  */
   14082           36 :           gcc_assert (TREE_CODE (tmp) == POINTER_PLUS_EXPR
   14083              :                       || TREE_CODE (tmp) == ARRAY_REF);
   14084              : 
   14085              :           /* 'tmp' is either the pointer to the array(POINTER_PLUS_EXPR)
   14086              :              or the array itself(ARRAY_REF).  */
   14087           36 :           tmp = TREE_OPERAND (tmp, 0);
   14088              : 
   14089              :           /* Provide the address of the array.  */
   14090           36 :           if (TREE_CODE (lse.expr) == ARRAY_REF)
   14091           18 :             tmp = gfc_build_addr_expr (NULL_TREE, tmp);
   14092              : 
   14093           36 :           cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   14094           36 :                                   tmp, build_int_cst (TREE_TYPE (tmp), 0));
   14095           36 :           msg = _("Assignment of scalar to unallocated array");
   14096           36 :           gfc_trans_runtime_check (true, false, cond, &loop.pre,
   14097              :                                    &expr1->where, msg);
   14098              :         }
   14099              : 
   14100              :       /* Deallocate the lhs parameterized components if required.  */
   14101       316042 :       if (dealloc
   14102       296314 :           && !expr1->symtree->n.sym->attr.associate_var
   14103       294200 :           && expr2->expr_type != EXPR_ARRAY
   14104       287834 :           && (IS_PDT (expr1) || IS_CLASS_PDT (expr1)))
   14105              :         {
   14106          505 :           bool pdt_dep = gfc_check_dependency (expr1, expr2, true);
   14107              : 
   14108          505 :           tmp = lse.expr;
   14109          505 :           if (pdt_dep)
   14110              :             {
   14111              :               /* Create a temporary for deallocation after assignment.  */
   14112          240 :               tmp = gfc_create_var (TREE_TYPE (lse.expr), "pdt_tmp");
   14113          240 :               gfc_add_modify (&lse.pre, tmp, lse.expr);
   14114              :             }
   14115              : 
   14116          505 :           if (expr1->ts.type == BT_DERIVED)
   14117          505 :             tmp = gfc_deallocate_pdt_comp (expr1->ts.u.derived, tmp,
   14118              :                                            expr1->rank);
   14119            0 :           else if (expr1->ts.type == BT_CLASS)
   14120              :             {
   14121            0 :               tmp = gfc_class_data_get (tmp);
   14122            0 :               tmp = gfc_deallocate_pdt_comp (CLASS_DATA (expr1)->ts.u.derived,
   14123              :                                              tmp, expr1->rank);
   14124              :             }
   14125              : 
   14126          505 :           if (tmp && pdt_dep)
   14127          128 :             gfc_add_expr_to_block (&rse.post, tmp);
   14128          377 :           else if (tmp)
   14129          133 :             gfc_add_expr_to_block (&lse.pre, tmp);
   14130              :         }
   14131              :     }
   14132              : 
   14133              :   /* Assignments of scalar derived types with allocatable components
   14134              :      to arrays must be done with a deep copy and the rhs temporary
   14135              :      must have its components deallocated afterwards.  */
   14136       634326 :   scalar_to_array = (expr2->ts.type == BT_DERIVED
   14137        21027 :                        && expr2->ts.u.derived->attr.alloc_comp
   14138         7261 :                        && !gfc_expr_is_variable (expr2)
   14139       321122 :                        && expr1->rank && !expr2->rank);
   14140       634326 :   scalar_to_array |= (expr1->ts.type == BT_DERIVED
   14141        21316 :                                     && expr1->rank
   14142         4063 :                                     && expr1->ts.u.derived->attr.alloc_comp
   14143       318649 :                                     && gfc_is_alloc_class_scalar_function (expr2));
   14144       317163 :   if (scalar_to_array && dealloc)
   14145              :     {
   14146           59 :       tmp = gfc_deallocate_alloc_comp_no_caf (expr2->ts.u.derived, rse.expr, 0);
   14147           59 :       gfc_prepend_expr_to_block (&loop.post, tmp);
   14148              :     }
   14149              : 
   14150              :   /* When assigning a character function result to a deferred-length variable,
   14151              :      the function call must happen before the (re)allocation of the lhs -
   14152              :      otherwise the character length of the result is not known.
   14153              :      NOTE 1: This relies on having the exact dependence of the length type
   14154              :      parameter available to the caller; gfortran saves it in the .mod files.
   14155              :      NOTE 2: Vector array references generate an index temporary that must
   14156              :      not go outside the loop. Otherwise, variables should not generate
   14157              :      a pre block.
   14158              :      NOTE 3: The concatenation operation generates a temporary pointer,
   14159              :      whose allocation must go to the innermost loop.
   14160              :      NOTE 4: Elemental functions may generate a temporary, too.  */
   14161       317163 :   if (flag_realloc_lhs
   14162       310735 :       && expr2->ts.type == BT_CHARACTER && expr1->ts.deferred
   14163         3086 :       && !(lss != gfc_ss_terminator
   14164          952 :            && rss != gfc_ss_terminator
   14165          952 :            && ((expr2->expr_type == EXPR_VARIABLE && expr2->rank)
   14166          759 :                || (expr2->expr_type == EXPR_FUNCTION
   14167          160 :                    && expr2->value.function.esym != NULL
   14168           26 :                    && expr2->value.function.esym->attr.elemental)
   14169          746 :                || (expr2->expr_type == EXPR_FUNCTION
   14170          147 :                    && expr2->value.function.isym != NULL
   14171          134 :                    && expr2->value.function.isym->elemental)
   14172          690 :                || (expr2->expr_type == EXPR_OP
   14173           31 :                    && expr2->value.op.op == INTRINSIC_CONCAT))))
   14174         2799 :     gfc_add_block_to_block (&block, &rse.pre);
   14175              : 
   14176              :   /* Nullify the allocatable components corresponding to those of the lhs
   14177              :      derived type, so that the finalization of the function result does not
   14178              :      affect the lhs of the assignment. Prepend is used to ensure that the
   14179              :      nullification occurs before the call to the finalizer. In the case of
   14180              :      a scalar to array assignment, this is done in gfc_trans_scalar_assign
   14181              :      as part of the deep copy.  */
   14182       316290 :   if (!scalar_to_array && expr1->ts.type == BT_DERIVED
   14183       337606 :                        && (gfc_is_class_array_function (expr2)
   14184        20419 :                            || gfc_is_alloc_class_scalar_function (expr2)))
   14185              :     {
   14186           78 :       tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, rse.expr, 0);
   14187           78 :       gfc_prepend_expr_to_block (&rse.post, tmp);
   14188           78 :       if (lss != gfc_ss_terminator && rss == gfc_ss_terminator)
   14189            0 :         gfc_add_block_to_block (&loop.post, &rse.post);
   14190              :     }
   14191              : 
   14192       317163 :   tmp = NULL_TREE;
   14193              : 
   14194       317163 :   if (is_poly_assign)
   14195              :     {
   14196        10881 :       tmp = trans_class_assignment (&body, expr1, expr2, &lse, &rse,
   14197          636 :                                     use_vptr_copy || (lhs_attr.allocatable
   14198          313 :                                                       && !lhs_attr.dimension),
   14199         3353 :                                     !realloc_flag && flag_realloc_lhs
   14200          636 :                                     && !lhs_attr.pointer);
   14201         3627 :       if (expr2->expr_type == EXPR_FUNCTION
   14202          244 :           && expr2->ts.type == BT_DERIVED
   14203           18 :           && expr2->ts.u.derived->attr.alloc_comp)
   14204              :         {
   14205           18 :           tree tmp2 = gfc_deallocate_alloc_comp (expr2->ts.u.derived,
   14206              :                                                  rse.expr, expr2->rank);
   14207           18 :           if (lss == gfc_ss_terminator)
   14208           18 :             gfc_add_expr_to_block (&rse.post, tmp2);
   14209              :           else
   14210            0 :             gfc_add_expr_to_block (&loop.post, tmp2);
   14211              :         }
   14212              : 
   14213         3627 :       expr1->must_finalize = 0;
   14214              :     }
   14215       313536 :   else if (!is_poly_assign
   14216       313536 :            && expr1->ts.type == BT_CLASS
   14217          393 :            && expr2->ts.type == BT_CLASS
   14218          200 :            && (expr2->must_finalize || dummy_class_array_copy))
   14219              :     {
   14220              :       /* This case comes about when the scalarizer provides array element
   14221              :          references to class temporaries or nonpointer dummy arrays. Use the
   14222              :          vptr copy function, since this does a deep copy of allocatable
   14223              :          components.  */
   14224          132 :       tmp = gfc_get_vptr_from_expr (rse.expr);
   14225          132 :       if (tmp == NULL_TREE && dummy_class_array_copy)
   14226           12 :         tmp = gfc_get_vptr_from_expr (gfc_get_class_from_gfc_expr (expr2));
   14227          132 :       if (tmp != NULL_TREE)
   14228              :         {
   14229          132 :           tree fcn = gfc_vptr_copy_get (tmp);
   14230          132 :           if (POINTER_TYPE_P (TREE_TYPE (fcn)))
   14231          132 :             fcn = build_fold_indirect_ref_loc (input_location, fcn);
   14232          132 :           tmp = build_call_expr_loc (input_location,
   14233              :                                      fcn, 2,
   14234              :                                      gfc_build_addr_expr (NULL, rse.expr),
   14235              :                                      gfc_build_addr_expr (NULL, lse.expr));
   14236              :         }
   14237              :     }
   14238              : 
   14239              :   /* Comply with F2018 (7.5.6.3). Make sure that any finalization code is added
   14240              :      after evaluation of the rhs and before reallocation.
   14241              :      Skip finalization for self-assignment to avoid use-after-free.
   14242              :      Strip parentheses from both sides to handle cases like a = (a).  */
   14243       317163 :   final_expr = gfc_assignment_finalizer_call (&lse, expr1, init_flag);
   14244       317163 :   if (final_expr
   14245          702 :       && gfc_dep_compare_expr (strip_parentheses (expr1),
   14246              :                                strip_parentheses (expr2)) != 0
   14247       317841 :       && !(strip_parentheses (expr2)->expr_type == EXPR_VARIABLE
   14248          229 :            && strip_parentheses (expr2)->symtree->n.sym->attr.artificial))
   14249              :     {
   14250          678 :       if (lss == gfc_ss_terminator)
   14251              :         {
   14252          201 :           gfc_add_block_to_block (&block, &rse.pre);
   14253          201 :           gfc_add_block_to_block (&block, &lse.finalblock);
   14254              :         }
   14255              :       else
   14256              :         {
   14257          477 :           gfc_add_block_to_block (&body, &rse.pre);
   14258          477 :           gfc_add_block_to_block (&loop.code[expr1->rank - 1],
   14259              :                                   &lse.finalblock);
   14260              :         }
   14261              :     }
   14262              :   else
   14263       316485 :     gfc_add_block_to_block (&body, &rse.pre);
   14264              : 
   14265       317163 :   if (flag_coarray != GFC_FCOARRAY_NONE && expr1->ts.type == BT_CHARACTER
   14266         3004 :       && assoc_assign)
   14267            0 :     tmp = gfc_trans_pointer_assignment (expr1, expr2);
   14268              : 
   14269              :   /* The finalization above is all that is wanted: the structure copy is done
   14270              :      component by component in generate_component_assignments.  */
   14271       317163 :   if (expr1->finalize_only)
   14272           24 :     tmp = build_empty_stmt (input_location);
   14273              : 
   14274              :   /* If nothing else works, do it the old fashioned way!  */
   14275       317163 :   if (tmp == NULL_TREE)
   14276              :     {
   14277              :       /* Strip parentheses to detect cases like a = (a) which need deep_copy.  */
   14278       313380 :       gfc_expr *expr2_stripped = strip_parentheses (expr2);
   14279       313380 :       tmp
   14280       626760 :         = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
   14281       313380 :                                    gfc_expr_is_variable (expr2_stripped)
   14282       282032 :                                      || scalar_to_array
   14283       281255 :                                      || expr2->expr_type == EXPR_ARRAY,
   14284              :                                    !(l_is_temp || init_flag) && dealloc,
   14285       313380 :                                    expr1->symtree->n.sym->attr.codimension,
   14286              :                                    assoc_assign);
   14287              :     }
   14288              : 
   14289              :   /* Add the lse pre block to the body  */
   14290       317163 :   gfc_add_block_to_block (&body, &lse.pre);
   14291       317163 :   gfc_add_expr_to_block (&body, tmp);
   14292              : 
   14293              :   /* Add the post blocks to the body.  Scalar finalization must appear before
   14294              :      the post block in case any dellocations are done.  */
   14295       317163 :   if (rse.finalblock.head
   14296       317163 :       && (!l_is_temp || (expr2->expr_type == EXPR_FUNCTION
   14297          154 :                          && gfc_expr_attr (expr2).elemental)))
   14298              :     {
   14299          154 :       gfc_add_block_to_block (&body, &rse.finalblock);
   14300          154 :       gfc_add_block_to_block (&body, &rse.post);
   14301              :     }
   14302              :   else
   14303       317009 :     gfc_add_block_to_block (&body, &rse.post);
   14304              : 
   14305       317163 :   gfc_add_block_to_block (&body, &lse.post);
   14306              : 
   14307       317163 :   if (lss == gfc_ss_terminator)
   14308              :     {
   14309              :       /* F2003: Add the code for reallocation on assignment.  */
   14310       272882 :       if (flag_realloc_lhs && is_scalar_reallocatable_lhs (expr1)
   14311       279601 :           && !is_poly_assign)
   14312         3757 :         alloc_scalar_allocatable_for_assignment (&block, string_length,
   14313              :                                                  expr1, expr2);
   14314              : 
   14315              :       /* Use the scalar assignment as is.  */
   14316       275844 :       gfc_add_block_to_block (&block, &body);
   14317              :     }
   14318              :   else
   14319              :     {
   14320        41319 :       gcc_assert (lse.ss == gfc_ss_terminator
   14321              :                   && rse.ss == gfc_ss_terminator);
   14322              : 
   14323        41319 :       if (l_is_temp)
   14324              :         {
   14325         1121 :           gfc_trans_scalarized_loop_boundary (&loop, &body);
   14326              : 
   14327              :           /* We need to copy the temporary to the actual lhs.  */
   14328         1121 :           gfc_init_se (&lse, NULL);
   14329         1121 :           gfc_init_se (&rse, NULL);
   14330         1121 :           gfc_copy_loopinfo_to_se (&lse, &loop);
   14331         1121 :           gfc_copy_loopinfo_to_se (&rse, &loop);
   14332              : 
   14333         1121 :           rse.ss = loop.temp_ss;
   14334         1121 :           lse.ss = lss;
   14335              : 
   14336         1121 :           gfc_conv_tmp_array_ref (&rse);
   14337         1121 :           gfc_conv_expr (&lse, expr1);
   14338              : 
   14339         1121 :           gcc_assert (lse.ss == gfc_ss_terminator
   14340              :                       && rse.ss == gfc_ss_terminator);
   14341              : 
   14342         1121 :           if (expr2->ts.type == BT_CHARACTER)
   14343          123 :             rse.string_length = string_length;
   14344              : 
   14345         1121 :           tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
   14346              :                                          false, dealloc);
   14347         1121 :           gfc_add_expr_to_block (&body, tmp);
   14348              :         }
   14349              : 
   14350        41319 :       if (reallocation != NULL_TREE)
   14351         6592 :         gfc_add_expr_to_block (&loop.code[loop.dimen - 1], reallocation);
   14352              : 
   14353        41319 :       if (maybe_workshare)
   14354           73 :         ompws_flags &= ~OMPWS_SCALARIZER_BODY;
   14355              : 
   14356              :       /* Generate the copying loops.  */
   14357        41319 :       gfc_trans_scalarizing_loops (&loop, &body);
   14358              : 
   14359              :       /* Wrap the whole thing up.  */
   14360        41319 :       gfc_add_block_to_block (&block, &loop.pre);
   14361        41319 :       gfc_add_block_to_block (&block, &loop.post);
   14362              : 
   14363        41319 :       gfc_cleanup_loop (&loop);
   14364              :     }
   14365              : 
   14366              :   /* Since parameterized components cannot have default initializers,
   14367              :      the default PDT constructor leaves them unallocated. Do the
   14368              :      allocation now.  */
   14369       317163 :   if (init_flag && IS_PDT (expr1)
   14370          515 :       && !expr1->symtree->n.sym->attr.allocatable
   14371          497 :       && !expr1->symtree->n.sym->attr.dummy)
   14372              :     {
   14373           79 :       gfc_symbol *sym = expr1->symtree->n.sym;
   14374           79 :       tmp = gfc_allocate_pdt_comp (sym->ts.u.derived,
   14375              :                                    sym->backend_decl,
   14376           79 :                                    sym->as ? sym->as->rank : 0,
   14377           79 :                                              sym->param_list);
   14378           79 :       gfc_add_expr_to_block (&block, tmp);
   14379              :     }
   14380              : 
   14381       317163 :   return gfc_finish_block (&block);
   14382              : }
   14383              : 
   14384              : 
   14385              : /* Check whether EXPR is a copyable array.  */
   14386              : 
   14387              : static bool
   14388      1004700 : copyable_array_p (gfc_expr * expr)
   14389              : {
   14390      1004700 :   if (expr->expr_type != EXPR_VARIABLE)
   14391              :     return false;
   14392              : 
   14393              :   /* First check it's an array.  */
   14394       980506 :   if (expr->rank < 1 || !expr->ref || expr->ref->next)
   14395              :     return false;
   14396              : 
   14397       151741 :   if (!gfc_full_array_ref_p (expr->ref, NULL))
   14398              :     return false;
   14399              : 
   14400              :   /* Next check that it's of a simple enough type.  */
   14401       118361 :   switch (expr->ts.type)
   14402              :     {
   14403              :     case BT_INTEGER:
   14404              :     case BT_REAL:
   14405              :     case BT_COMPLEX:
   14406              :     case BT_LOGICAL:
   14407              :       return true;
   14408              : 
   14409              :     case BT_CHARACTER:
   14410              :       return false;
   14411              : 
   14412         7067 :     case_bt_struct:
   14413         7067 :       return (!expr->ts.u.derived->attr.alloc_comp
   14414         7067 :               && !expr->ts.u.derived->attr.pdt_type);
   14415              : 
   14416              :     default:
   14417              :       break;
   14418              :     }
   14419              : 
   14420              :   return false;
   14421              : }
   14422              : 
   14423              : /* Translate an assignment.  */
   14424              : 
   14425              : tree
   14426       335262 : gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
   14427              :                       bool dealloc, bool use_vptr_copy, bool may_alias)
   14428              : {
   14429       335262 :   tree tmp;
   14430              : 
   14431              :   /* Special case a single function returning an array.  */
   14432       335262 :   if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
   14433              :     {
   14434        14614 :       tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
   14435        14614 :       if (tmp)
   14436              :         return tmp;
   14437              :     }
   14438              : 
   14439              :   /* Special case assigning an array to zero.  */
   14440       328371 :   if (copyable_array_p (expr1)
   14441       328371 :       && is_zero_initializer_p (expr2))
   14442              :     {
   14443         4060 :       tmp = gfc_trans_zero_assign (expr1);
   14444         4060 :       if (tmp)
   14445              :         return tmp;
   14446              :     }
   14447              : 
   14448              :   /* Special case copying one array to another.  */
   14449       324608 :   if (copyable_array_p (expr1)
   14450        28599 :       && copyable_array_p (expr2)
   14451         2675 :       && gfc_compare_types (&expr1->ts, &expr2->ts)
   14452       327283 :       && !gfc_check_dependency (expr1, expr2, 0))
   14453              :     {
   14454         2585 :       tmp = gfc_trans_array_copy (expr1, expr2);
   14455         2585 :       if (tmp)
   14456              :         return tmp;
   14457              :     }
   14458              : 
   14459              :   /* Special case initializing an array from a constant array constructor.  */
   14460       323122 :   if (copyable_array_p (expr1)
   14461        27113 :       && expr2->expr_type == EXPR_ARRAY
   14462       331502 :       && gfc_compare_types (&expr1->ts, &expr2->ts))
   14463              :     {
   14464         8380 :       tmp = gfc_trans_array_constructor_copy (expr1, expr2);
   14465         8380 :       if (tmp)
   14466              :         return tmp;
   14467              :     }
   14468              : 
   14469       317163 :   if (UNLIMITED_POLY (expr1) && expr1->rank)
   14470       317163 :     use_vptr_copy = true;
   14471              : 
   14472              :   /* Fallback to the scalarizer to generate explicit loops.  */
   14473       317163 :   return gfc_trans_assignment_1 (expr1, expr2, init_flag, dealloc,
   14474       317163 :                                  use_vptr_copy, may_alias);
   14475              : }
   14476              : 
   14477              : tree
   14478        14129 : gfc_trans_init_assign (gfc_code * code)
   14479              : {
   14480        14129 :   return gfc_trans_assignment (code->expr1, code->expr2, true, false, true);
   14481              : }
   14482              : 
   14483              : tree
   14484       312406 : gfc_trans_assign (gfc_code * code)
   14485              : {
   14486       312406 :   return gfc_trans_assignment (code->expr1, code->expr2, false, true);
   14487              : }
   14488              : 
   14489              : /* Generate a simple loop for internal use of the form
   14490              :    for (var = begin; var <cond> end; var += step)
   14491              :       body;  */
   14492              : void
   14493        12281 : gfc_simple_for_loop (stmtblock_t *block, tree var, tree begin, tree end,
   14494              :                      enum tree_code cond, tree step, tree body)
   14495              : {
   14496        12281 :   tree tmp;
   14497              : 
   14498              :   /* var = begin. */
   14499        12281 :   gfc_add_modify (block, var, begin);
   14500              : 
   14501              :   /* Loop: for (var = begin; var <cond> end; var += step).  */
   14502        12281 :   tree label_loop = gfc_build_label_decl (NULL_TREE);
   14503        12281 :   tree label_cond = gfc_build_label_decl (NULL_TREE);
   14504        12281 :   TREE_USED (label_loop) = 1;
   14505        12281 :   TREE_USED (label_cond) = 1;
   14506              : 
   14507        12281 :   gfc_add_expr_to_block (block, build1_v (GOTO_EXPR, label_cond));
   14508        12281 :   gfc_add_expr_to_block (block, build1_v (LABEL_EXPR, label_loop));
   14509              : 
   14510              :   /* Loop body.  */
   14511        12281 :   gfc_add_expr_to_block (block, body);
   14512              : 
   14513              :   /* End of loop body.  */
   14514        12281 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (var), var, step);
   14515        12281 :   gfc_add_modify (block, var, tmp);
   14516        12281 :   gfc_add_expr_to_block (block, build1_v (LABEL_EXPR, label_cond));
   14517        12281 :   tmp = fold_build2_loc (input_location, cond, boolean_type_node, var, end);
   14518        12281 :   tmp = build3_v (COND_EXPR, tmp, build1_v (GOTO_EXPR, label_loop),
   14519              :                   build_empty_stmt (input_location));
   14520        12281 :   gfc_add_expr_to_block (block, tmp);
   14521        12281 : }
        

Generated by: LCOV version 2.5.0-beta

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