LCOV - code coverage report
Current view: top level - gcc/fortran - trans-expr.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.7 % 7087 6713
Test Date: 2026-04-20 14:57:17 Functions: 96.1 % 155 149
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              : /* Only for gfc_trans_assign and gfc_trans_pointer_assign.  */
      42              : #include "trans-stmt.h"
      43              : #include "dependency.h"
      44              : #include "gimplify.h"
      45              : #include "tm.h"               /* For CHAR_TYPE_SIZE.  */
      46              : 
      47              : 
      48              : /* Calculate the number of characters in a string.  */
      49              : 
      50              : static tree
      51        36078 : gfc_get_character_len (tree type)
      52              : {
      53        36078 :   tree len;
      54              : 
      55        36078 :   gcc_assert (type && TREE_CODE (type) == ARRAY_TYPE
      56              :               && TYPE_STRING_FLAG (type));
      57              : 
      58        36078 :   len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
      59        36078 :   len = (len) ? (len) : (integer_zero_node);
      60        36078 :   return fold_convert (gfc_charlen_type_node, len);
      61              : }
      62              : 
      63              : 
      64              : 
      65              : /* Calculate the number of bytes in a string.  */
      66              : 
      67              : tree
      68        36078 : gfc_get_character_len_in_bytes (tree type)
      69              : {
      70        36078 :   tree tmp, len;
      71              : 
      72        36078 :   gcc_assert (type && TREE_CODE (type) == ARRAY_TYPE
      73              :               && TYPE_STRING_FLAG (type));
      74              : 
      75        36078 :   tmp = TYPE_SIZE_UNIT (TREE_TYPE (type));
      76        72156 :   tmp = (tmp && !integer_zerop (tmp))
      77        72156 :     ? (fold_convert (gfc_charlen_type_node, tmp)) : (NULL_TREE);
      78        36078 :   len = gfc_get_character_len (type);
      79        36078 :   if (tmp && len && !integer_zerop (len))
      80        35306 :     len = fold_build2_loc (input_location, MULT_EXPR,
      81              :                            gfc_charlen_type_node, len, tmp);
      82        36078 :   return len;
      83              : }
      84              : 
      85              : 
      86              : /* Convert a scalar to an array descriptor. To be used for assumed-rank
      87              :    arrays.  */
      88              : 
      89              : static tree
      90         6269 : get_scalar_to_descriptor_type (tree scalar, symbol_attribute attr)
      91              : {
      92         6269 :   enum gfc_array_kind akind;
      93         6269 :   tree *lbound = NULL, *ubound = NULL;
      94         6269 :   int codim = 0;
      95              : 
      96         6269 :   if (attr.pointer)
      97              :     akind = GFC_ARRAY_POINTER_CONT;
      98         5917 :   else if (attr.allocatable)
      99              :     akind = GFC_ARRAY_ALLOCATABLE;
     100              :   else
     101         5148 :     akind = GFC_ARRAY_ASSUMED_SHAPE_CONT;
     102              : 
     103         6269 :   if (POINTER_TYPE_P (TREE_TYPE (scalar)))
     104         5322 :     scalar = TREE_TYPE (scalar);
     105         6269 :   if (TYPE_LANG_SPECIFIC (TREE_TYPE (scalar)))
     106              :     {
     107         4727 :       struct lang_type *lang_specific = TYPE_LANG_SPECIFIC (TREE_TYPE (scalar));
     108         4727 :       codim = lang_specific->corank;
     109         4727 :       lbound = lang_specific->lbound;
     110         4727 :       ubound = lang_specific->ubound;
     111              :     }
     112         6269 :   return gfc_get_array_type_bounds (TREE_TYPE (scalar), 0, codim, lbound,
     113              :                                     ubound, 1, akind,
     114         6269 :                                     !(attr.pointer || attr.target));
     115              : }
     116              : 
     117              : tree
     118         5591 : gfc_conv_scalar_to_descriptor (gfc_se *se, tree scalar, symbol_attribute attr)
     119              : {
     120         5591 :   tree desc, type, etype;
     121              : 
     122         5591 :   type = get_scalar_to_descriptor_type (scalar, attr);
     123         5591 :   etype = TREE_TYPE (scalar);
     124         5591 :   desc = gfc_create_var (type, "desc");
     125         5591 :   DECL_ARTIFICIAL (desc) = 1;
     126              : 
     127         5591 :   if (CONSTANT_CLASS_P (scalar))
     128              :     {
     129           54 :       tree tmp;
     130           54 :       tmp = gfc_create_var (TREE_TYPE (scalar), "scalar");
     131           54 :       gfc_add_modify (&se->pre, tmp, scalar);
     132           54 :       scalar = tmp;
     133              :     }
     134         5591 :   if (!POINTER_TYPE_P (TREE_TYPE (scalar)))
     135          947 :     scalar = gfc_build_addr_expr (NULL_TREE, scalar);
     136         4644 :   else if (TREE_TYPE (etype) && TREE_CODE (TREE_TYPE (etype)) == ARRAY_TYPE)
     137          158 :     etype = TREE_TYPE (etype);
     138         5591 :   gfc_add_modify (&se->pre, gfc_conv_descriptor_dtype (desc),
     139              :                   gfc_get_dtype_rank_type (0, etype));
     140         5591 :   gfc_conv_descriptor_data_set (&se->pre, desc, scalar);
     141         5591 :   gfc_conv_descriptor_span_set (&se->pre, desc,
     142              :                                 gfc_conv_descriptor_elem_len (desc));
     143              : 
     144              :   /* Copy pointer address back - but only if it could have changed and
     145              :      if the actual argument is a pointer and not, e.g., NULL().  */
     146         5591 :   if ((attr.pointer || attr.allocatable) && attr.intent != INTENT_IN)
     147          846 :     gfc_add_modify (&se->post, scalar,
     148          423 :                     fold_convert (TREE_TYPE (scalar),
     149              :                                   gfc_conv_descriptor_data_get (desc)));
     150         5591 :   return desc;
     151              : }
     152              : 
     153              : 
     154              : /* Get the coarray token from the ultimate array or component ref.
     155              :    Returns a NULL_TREE, when the ref object is not allocatable or pointer.  */
     156              : 
     157              : tree
     158          508 : gfc_get_ultimate_alloc_ptr_comps_caf_token (gfc_se *outerse, gfc_expr *expr)
     159              : {
     160          508 :   gfc_symbol *sym = expr->symtree->n.sym;
     161         1016 :   bool is_coarray = sym->ts.type == BT_CLASS
     162          508 :                       ? CLASS_DATA (sym)->attr.codimension
     163          463 :                       : sym->attr.codimension;
     164          508 :   gfc_expr *caf_expr = gfc_copy_expr (expr);
     165          508 :   gfc_ref *ref = caf_expr->ref, *last_caf_ref = NULL;
     166              : 
     167         1610 :   while (ref)
     168              :     {
     169         1102 :       if (ref->type == REF_COMPONENT
     170          415 :           && (ref->u.c.component->attr.allocatable
     171          104 :               || ref->u.c.component->attr.pointer)
     172          413 :           && (is_coarray || ref->u.c.component->attr.codimension))
     173         1102 :           last_caf_ref = ref;
     174         1102 :       ref = ref->next;
     175              :     }
     176              : 
     177          508 :   if (last_caf_ref == NULL)
     178              :     {
     179          178 :       gfc_free_expr (caf_expr);
     180          178 :       return NULL_TREE;
     181              :     }
     182              : 
     183          143 :   tree comp = last_caf_ref->u.c.component->caf_token
     184          330 :                 ? gfc_comp_caf_token (last_caf_ref->u.c.component)
     185              :                 : NULL_TREE,
     186              :        caf;
     187          330 :   gfc_se se;
     188          330 :   bool comp_ref = !last_caf_ref->u.c.component->attr.dimension;
     189          330 :   if (comp == NULL_TREE && comp_ref)
     190              :     {
     191           46 :       gfc_free_expr (caf_expr);
     192           46 :       return NULL_TREE;
     193              :     }
     194          284 :   gfc_init_se (&se, outerse);
     195          284 :   gfc_free_ref_list (last_caf_ref->next);
     196          284 :   last_caf_ref->next = NULL;
     197          284 :   caf_expr->rank = comp_ref ? 0 : last_caf_ref->u.c.component->as->rank;
     198          568 :   caf_expr->corank = last_caf_ref->u.c.component->as
     199          284 :                        ? last_caf_ref->u.c.component->as->corank
     200              :                        : expr->corank;
     201          284 :   se.want_pointer = comp_ref;
     202          284 :   gfc_conv_expr (&se, caf_expr);
     203          284 :   gfc_add_block_to_block (&outerse->pre, &se.pre);
     204              : 
     205          284 :   if (TREE_CODE (se.expr) == COMPONENT_REF && comp_ref)
     206          143 :     se.expr = TREE_OPERAND (se.expr, 0);
     207          284 :   gfc_free_expr (caf_expr);
     208              : 
     209          284 :   if (comp_ref)
     210          143 :     caf = fold_build3_loc (input_location, COMPONENT_REF,
     211          143 :                            TREE_TYPE (comp), se.expr, comp, NULL_TREE);
     212              :   else
     213          141 :     caf = gfc_conv_descriptor_token (se.expr);
     214          284 :   return gfc_build_addr_expr (NULL_TREE, caf);
     215              : }
     216              : 
     217              : 
     218              : /* This is the seed for an eventual trans-class.c
     219              : 
     220              :    The following parameters should not be used directly since they might
     221              :    in future implementations.  Use the corresponding APIs.  */
     222              : #define CLASS_DATA_FIELD 0
     223              : #define CLASS_VPTR_FIELD 1
     224              : #define CLASS_LEN_FIELD 2
     225              : #define VTABLE_HASH_FIELD 0
     226              : #define VTABLE_SIZE_FIELD 1
     227              : #define VTABLE_EXTENDS_FIELD 2
     228              : #define VTABLE_DEF_INIT_FIELD 3
     229              : #define VTABLE_COPY_FIELD 4
     230              : #define VTABLE_FINAL_FIELD 5
     231              : #define VTABLE_DEALLOCATE_FIELD 6
     232              : 
     233              : 
     234              : tree
     235           40 : gfc_class_set_static_fields (tree decl, tree vptr, tree data)
     236              : {
     237           40 :   tree tmp;
     238           40 :   tree field;
     239           40 :   vec<constructor_elt, va_gc> *init = NULL;
     240              : 
     241           40 :   field = TYPE_FIELDS (TREE_TYPE (decl));
     242           40 :   tmp = gfc_advance_chain (field, CLASS_DATA_FIELD);
     243           40 :   CONSTRUCTOR_APPEND_ELT (init, tmp, data);
     244              : 
     245           40 :   tmp = gfc_advance_chain (field, CLASS_VPTR_FIELD);
     246           40 :   CONSTRUCTOR_APPEND_ELT (init, tmp, vptr);
     247              : 
     248           40 :   return build_constructor (TREE_TYPE (decl), init);
     249              : }
     250              : 
     251              : 
     252              : tree
     253        32120 : gfc_class_data_get (tree decl)
     254              : {
     255        32120 :   tree data;
     256        32120 :   if (POINTER_TYPE_P (TREE_TYPE (decl)))
     257         5418 :     decl = build_fold_indirect_ref_loc (input_location, decl);
     258        32120 :   data = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
     259              :                             CLASS_DATA_FIELD);
     260        32120 :   return fold_build3_loc (input_location, COMPONENT_REF,
     261        32120 :                           TREE_TYPE (data), decl, data,
     262        32120 :                           NULL_TREE);
     263              : }
     264              : 
     265              : 
     266              : tree
     267        45482 : gfc_class_vptr_get (tree decl)
     268              : {
     269        45482 :   tree vptr;
     270              :   /* For class arrays decl may be a temporary descriptor handle, the vptr is
     271              :      then available through the saved descriptor.  */
     272        28107 :   if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
     273        47282 :       && GFC_DECL_SAVED_DESCRIPTOR (decl))
     274         1297 :     decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
     275        45482 :   if (POINTER_TYPE_P (TREE_TYPE (decl)))
     276         2362 :     decl = build_fold_indirect_ref_loc (input_location, decl);
     277        45482 :   vptr = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
     278              :                             CLASS_VPTR_FIELD);
     279        45482 :   return fold_build3_loc (input_location, COMPONENT_REF,
     280        45482 :                           TREE_TYPE (vptr), decl, vptr,
     281        45482 :                           NULL_TREE);
     282              : }
     283              : 
     284              : 
     285              : tree
     286         6662 : gfc_class_len_get (tree decl)
     287              : {
     288         6662 :   tree len;
     289              :   /* For class arrays decl may be a temporary descriptor handle, the len is
     290              :      then available through the saved descriptor.  */
     291         4791 :   if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
     292         6911 :       && GFC_DECL_SAVED_DESCRIPTOR (decl))
     293           85 :     decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
     294         6662 :   if (POINTER_TYPE_P (TREE_TYPE (decl)))
     295          662 :     decl = build_fold_indirect_ref_loc (input_location, decl);
     296         6662 :   len = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
     297              :                            CLASS_LEN_FIELD);
     298         6662 :   return fold_build3_loc (input_location, COMPONENT_REF,
     299         6662 :                           TREE_TYPE (len), decl, len,
     300         6662 :                           NULL_TREE);
     301              : }
     302              : 
     303              : 
     304              : /* Try to get the _len component of a class.  When the class is not unlimited
     305              :    poly, i.e. no _len field exists, then return a zero node.  */
     306              : 
     307              : static tree
     308         4985 : gfc_class_len_or_zero_get (tree decl)
     309              : {
     310         4985 :   tree len;
     311              :   /* For class arrays decl may be a temporary descriptor handle, the vptr is
     312              :      then available through the saved descriptor.  */
     313         2969 :   if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
     314         5033 :       && GFC_DECL_SAVED_DESCRIPTOR (decl))
     315            0 :     decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
     316         4985 :   if (POINTER_TYPE_P (TREE_TYPE (decl)))
     317           12 :     decl = build_fold_indirect_ref_loc (input_location, decl);
     318         4985 :   len = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
     319              :                            CLASS_LEN_FIELD);
     320         6844 :   return len != NULL_TREE ? fold_build3_loc (input_location, COMPONENT_REF,
     321         1859 :                                              TREE_TYPE (len), decl, len,
     322              :                                              NULL_TREE)
     323         3126 :     : build_zero_cst (gfc_charlen_type_node);
     324              : }
     325              : 
     326              : 
     327              : tree
     328         4825 : gfc_resize_class_size_with_len (stmtblock_t * block, tree class_expr, tree size)
     329              : {
     330         4825 :   tree tmp;
     331         4825 :   tree tmp2;
     332         4825 :   tree type;
     333              : 
     334         4825 :   tmp = gfc_class_len_or_zero_get (class_expr);
     335              : 
     336              :   /* Include the len value in the element size if present.  */
     337         4825 :   if (!integer_zerop (tmp))
     338              :     {
     339         1699 :       type = TREE_TYPE (size);
     340         1699 :       if (block)
     341              :         {
     342          986 :           size = gfc_evaluate_now (size, block);
     343          986 :           tmp = gfc_evaluate_now (fold_convert (type , tmp), block);
     344              :         }
     345              :       else
     346          713 :         tmp = fold_convert (type , tmp);
     347         1699 :       tmp2 = fold_build2_loc (input_location, MULT_EXPR,
     348              :                               type, size, tmp);
     349         1699 :       tmp = fold_build2_loc (input_location, GT_EXPR,
     350              :                              logical_type_node, tmp,
     351              :                              build_zero_cst (type));
     352         1699 :       size = fold_build3_loc (input_location, COND_EXPR,
     353              :                               type, tmp, tmp2, size);
     354              :     }
     355              :   else
     356              :     return size;
     357              : 
     358         1699 :   if (block)
     359          986 :     size = gfc_evaluate_now (size, block);
     360              : 
     361              :   return size;
     362              : }
     363              : 
     364              : 
     365              : /* Get the specified FIELD from the VPTR.  */
     366              : 
     367              : static tree
     368        21219 : vptr_field_get (tree vptr, int fieldno)
     369              : {
     370        21219 :   tree field;
     371        21219 :   vptr = build_fold_indirect_ref_loc (input_location, vptr);
     372        21219 :   field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (vptr)),
     373              :                              fieldno);
     374        21219 :   field = fold_build3_loc (input_location, COMPONENT_REF,
     375        21219 :                            TREE_TYPE (field), vptr, field,
     376              :                            NULL_TREE);
     377        21219 :   gcc_assert (field);
     378        21219 :   return field;
     379              : }
     380              : 
     381              : 
     382              : /* Get the field from the class' vptr.  */
     383              : 
     384              : static tree
     385         9866 : class_vtab_field_get (tree decl, int fieldno)
     386              : {
     387         9866 :   tree vptr;
     388         9866 :   vptr = gfc_class_vptr_get (decl);
     389         9866 :   return vptr_field_get (vptr, fieldno);
     390              : }
     391              : 
     392              : 
     393              : /* Define a macro for creating the class_vtab_* and vptr_* accessors in
     394              :    unison.  */
     395              : #define VTAB_GET_FIELD_GEN(name, field) tree \
     396              : gfc_class_vtab_## name ##_get (tree cl) \
     397              : { \
     398              :   return class_vtab_field_get (cl, field); \
     399              : } \
     400              :  \
     401              : tree \
     402              : gfc_vptr_## name ##_get (tree vptr) \
     403              : { \
     404              :   return vptr_field_get (vptr, field); \
     405              : }
     406              : 
     407          183 : VTAB_GET_FIELD_GEN (hash, VTABLE_HASH_FIELD)
     408            0 : VTAB_GET_FIELD_GEN (extends, VTABLE_EXTENDS_FIELD)
     409            0 : VTAB_GET_FIELD_GEN (def_init, VTABLE_DEF_INIT_FIELD)
     410         4354 : VTAB_GET_FIELD_GEN (copy, VTABLE_COPY_FIELD)
     411         1812 : VTAB_GET_FIELD_GEN (final, VTABLE_FINAL_FIELD)
     412         1023 : VTAB_GET_FIELD_GEN (deallocate, VTABLE_DEALLOCATE_FIELD)
     413              : #undef VTAB_GET_FIELD_GEN
     414              : 
     415              : /* The size field is returned as an array index type.  Therefore treat
     416              :    it and only it specially.  */
     417              : 
     418              : tree
     419         7892 : gfc_class_vtab_size_get (tree cl)
     420              : {
     421         7892 :   tree size;
     422         7892 :   size = class_vtab_field_get (cl, VTABLE_SIZE_FIELD);
     423              :   /* Always return size as an array index type.  */
     424         7892 :   size = fold_convert (gfc_array_index_type, size);
     425         7892 :   gcc_assert (size);
     426         7892 :   return size;
     427              : }
     428              : 
     429              : tree
     430         5955 : gfc_vptr_size_get (tree vptr)
     431              : {
     432         5955 :   tree size;
     433         5955 :   size = vptr_field_get (vptr, VTABLE_SIZE_FIELD);
     434              :   /* Always return size as an array index type.  */
     435         5955 :   size = fold_convert (gfc_array_index_type, size);
     436         5955 :   gcc_assert (size);
     437         5955 :   return size;
     438              : }
     439              : 
     440              : 
     441              : #undef CLASS_DATA_FIELD
     442              : #undef CLASS_VPTR_FIELD
     443              : #undef CLASS_LEN_FIELD
     444              : #undef VTABLE_HASH_FIELD
     445              : #undef VTABLE_SIZE_FIELD
     446              : #undef VTABLE_EXTENDS_FIELD
     447              : #undef VTABLE_DEF_INIT_FIELD
     448              : #undef VTABLE_COPY_FIELD
     449              : #undef VTABLE_FINAL_FIELD
     450              : 
     451              : 
     452              : /* IF ts is null (default), search for the last _class ref in the chain
     453              :    of references of the expression and cut the chain there.  Although
     454              :    this routine is similiar to class.cc:gfc_add_component_ref (), there
     455              :    is a significant difference: gfc_add_component_ref () concentrates
     456              :    on an array ref that is the last ref in the chain and is oblivious
     457              :    to the kind of refs following.
     458              :    ELSE IF ts is non-null the cut is at the class entity or component
     459              :    that is followed by an array reference, which is not an element.
     460              :    These calls come from trans-array.cc:build_class_array_ref, which
     461              :    handles scalarized class array references.*/
     462              : 
     463              : gfc_expr *
     464         9444 : gfc_find_and_cut_at_last_class_ref (gfc_expr *e, bool is_mold,
     465              :                                     gfc_typespec **ts)
     466              : {
     467         9444 :   gfc_expr *base_expr;
     468         9444 :   gfc_ref *ref, *class_ref, *tail = NULL, *array_ref;
     469              : 
     470              :   /* Find the last class reference.  */
     471         9444 :   class_ref = NULL;
     472         9444 :   array_ref = NULL;
     473              : 
     474         9444 :   if (ts)
     475              :     {
     476          435 :       if (e->symtree
     477          410 :           && e->symtree->n.sym->ts.type == BT_CLASS)
     478          410 :         *ts = &e->symtree->n.sym->ts;
     479              :       else
     480           25 :         *ts = NULL;
     481              :     }
     482              : 
     483        23721 :   for (ref = e->ref; ref; ref = ref->next)
     484              :     {
     485        14697 :       if (ts)
     486              :         {
     487         1038 :           if (ref->type == REF_COMPONENT
     488          490 :               && ref->u.c.component->ts.type == BT_CLASS
     489            0 :               && ref->next && ref->next->type == REF_COMPONENT
     490            0 :               && !strcmp (ref->next->u.c.component->name, "_data")
     491            0 :               && ref->next->next
     492            0 :               && ref->next->next->type == REF_ARRAY
     493            0 :               && ref->next->next->u.ar.type != AR_ELEMENT)
     494              :             {
     495            0 :               *ts = &ref->u.c.component->ts;
     496            0 :               class_ref = ref;
     497            0 :               break;
     498              :             }
     499              : 
     500         1038 :           if (ref->next == NULL)
     501              :             break;
     502              :         }
     503              :       else
     504              :         {
     505        13659 :           if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
     506        13659 :             array_ref = ref;
     507              : 
     508        13659 :           if (ref->type == REF_COMPONENT
     509         8217 :               && ref->u.c.component->ts.type == BT_CLASS)
     510              :             {
     511              :               /* Component to the right of a part reference with nonzero
     512              :                  rank must not have the ALLOCATABLE attribute.  If attempts
     513              :                  are made to reference such a component reference, an error
     514              :                  results followed by an ICE.  */
     515         1609 :               if (array_ref
     516           10 :                   && CLASS_DATA (ref->u.c.component)->attr.allocatable)
     517              :                 return NULL;
     518              :               class_ref = ref;
     519              :             }
     520              :         }
     521              :     }
     522              : 
     523         9434 :   if (ts && *ts == NULL)
     524              :     return NULL;
     525              : 
     526              :   /* Remove and store all subsequent references after the
     527              :      CLASS reference.  */
     528         9409 :   if (class_ref)
     529              :     {
     530         1407 :       tail = class_ref->next;
     531         1407 :       class_ref->next = NULL;
     532              :     }
     533         8002 :   else if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
     534              :     {
     535         7984 :       tail = e->ref;
     536         7984 :       e->ref = NULL;
     537              :     }
     538              : 
     539         9409 :   if (is_mold)
     540           61 :     base_expr = gfc_expr_to_initialize (e);
     541              :   else
     542         9348 :     base_expr = gfc_copy_expr (e);
     543              : 
     544              :   /* Restore the original tail expression.  */
     545         9409 :   if (class_ref)
     546              :     {
     547         1407 :       gfc_free_ref_list (class_ref->next);
     548         1407 :       class_ref->next = tail;
     549              :     }
     550         8002 :   else if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
     551              :     {
     552         7984 :       gfc_free_ref_list (e->ref);
     553         7984 :       e->ref = tail;
     554              :     }
     555              :   return base_expr;
     556              : }
     557              : 
     558              : /* Reset the vptr to the declared type, e.g. after deallocation.
     559              :    Use the variable in CLASS_CONTAINER if available.  Otherwise, recreate
     560              :    one with e or class_type.  At least one of the two has to be set.  The
     561              :    generated assignment code is added at the end of BLOCK.  */
     562              : 
     563              : void
     564        11118 : gfc_reset_vptr (stmtblock_t *block, gfc_expr *e, tree class_container,
     565              :                 gfc_symbol *class_type)
     566              : {
     567        11118 :   tree vptr = NULL_TREE;
     568              : 
     569        11118 :   if (class_container != NULL_TREE)
     570         6644 :     vptr = gfc_get_vptr_from_expr (class_container);
     571              : 
     572         6644 :   if (vptr == NULL_TREE)
     573              :     {
     574         4481 :       gfc_se se;
     575         4481 :       gcc_assert (e);
     576              : 
     577              :       /* Evaluate the expression and obtain the vptr from it.  */
     578         4481 :       gfc_init_se (&se, NULL);
     579         4481 :       if (e->rank)
     580         2239 :         gfc_conv_expr_descriptor (&se, e);
     581              :       else
     582         2242 :         gfc_conv_expr (&se, e);
     583         4481 :       gfc_add_block_to_block (block, &se.pre);
     584              : 
     585         4481 :       vptr = gfc_get_vptr_from_expr (se.expr);
     586              :     }
     587              : 
     588              :   /* If a vptr is not found, we can do nothing more.  */
     589         4481 :   if (vptr == NULL_TREE)
     590              :     return;
     591              : 
     592        11108 :   if (UNLIMITED_POLY (e)
     593        10082 :       || UNLIMITED_POLY (class_type)
     594              :       /* When the class_type's source is not a symbol (e.g. a component's ts),
     595              :          then look at the _data-components type.  */
     596         1515 :       || (class_type != NULL && class_type->ts.type == BT_UNKNOWN
     597         1515 :           && class_type->components && class_type->components->ts.u.derived
     598         1509 :           && class_type->components->ts.u.derived->attr.unlimited_polymorphic))
     599         1194 :     gfc_add_modify (block, vptr, build_int_cst (TREE_TYPE (vptr), 0));
     600              :   else
     601              :     {
     602         9914 :       gfc_symbol *vtab, *type = nullptr;
     603         9914 :       tree vtable;
     604              : 
     605         9914 :       if (e)
     606         8567 :         type = e->ts.u.derived;
     607         1347 :       else if (class_type)
     608              :         {
     609         1347 :           if (class_type->ts.type == BT_CLASS)
     610            0 :             type = CLASS_DATA (class_type)->ts.u.derived;
     611              :           else
     612              :             type = class_type;
     613              :         }
     614         8567 :       gcc_assert (type);
     615              :       /* Return the vptr to the address of the declared type.  */
     616         9914 :       vtab = gfc_find_derived_vtab (type);
     617         9914 :       vtable = vtab->backend_decl;
     618         9914 :       if (vtable == NULL_TREE)
     619           76 :         vtable = gfc_get_symbol_decl (vtab);
     620         9914 :       vtable = gfc_build_addr_expr (NULL, vtable);
     621         9914 :       vtable = fold_convert (TREE_TYPE (vptr), vtable);
     622         9914 :       gfc_add_modify (block, vptr, vtable);
     623              :     }
     624              : }
     625              : 
     626              : /* Set the vptr of a class in to from the type given in from.  If from is NULL,
     627              :    then reset the vptr to the default or to.  */
     628              : 
     629              : void
     630          216 : gfc_class_set_vptr (stmtblock_t *block, tree to, tree from)
     631              : {
     632          216 :   tree tmp, vptr_ref;
     633          216 :   gfc_symbol *type;
     634              : 
     635          216 :   vptr_ref = gfc_get_vptr_from_expr (to);
     636          252 :   if (POINTER_TYPE_P (TREE_TYPE (from))
     637          216 :       && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (from))))
     638              :     {
     639           44 :       gfc_add_modify (block, vptr_ref,
     640           22 :                       fold_convert (TREE_TYPE (vptr_ref),
     641              :                                     gfc_get_vptr_from_expr (from)));
     642          238 :       return;
     643              :     }
     644          194 :   tmp = gfc_get_vptr_from_expr (from);
     645          194 :   if (tmp)
     646              :     {
     647          158 :       gfc_add_modify (block, vptr_ref,
     648          158 :                       fold_convert (TREE_TYPE (vptr_ref), tmp));
     649          158 :       return;
     650              :     }
     651           36 :   if (VAR_P (from)
     652           36 :       && strncmp (IDENTIFIER_POINTER (DECL_NAME (from)), "__vtab", 6) == 0)
     653              :     {
     654           36 :       gfc_add_modify (block, vptr_ref,
     655           36 :                       gfc_build_addr_expr (TREE_TYPE (vptr_ref), from));
     656           36 :       return;
     657              :     }
     658            0 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (from)))
     659            0 :       && GFC_CLASS_TYPE_P (
     660              :         TREE_TYPE (TREE_OPERAND (TREE_OPERAND (from, 0), 0))))
     661              :     {
     662            0 :       gfc_add_modify (block, vptr_ref,
     663            0 :                       fold_convert (TREE_TYPE (vptr_ref),
     664              :                                     gfc_get_vptr_from_expr (TREE_OPERAND (
     665              :                                       TREE_OPERAND (from, 0), 0))));
     666            0 :       return;
     667              :     }
     668              : 
     669              :   /* If nothing of the above matches, set the vtype according to the type.  */
     670            0 :   tmp = TREE_TYPE (from);
     671            0 :   if (POINTER_TYPE_P (tmp))
     672            0 :     tmp = TREE_TYPE (tmp);
     673            0 :   gfc_find_symbol (IDENTIFIER_POINTER (TYPE_NAME (tmp)), gfc_current_ns, 1,
     674              :                    &type);
     675            0 :   tmp = gfc_find_derived_vtab (type)->backend_decl;
     676            0 :   gcc_assert (tmp);
     677            0 :   gfc_add_modify (block, vptr_ref,
     678            0 :                   gfc_build_addr_expr (TREE_TYPE (vptr_ref), tmp));
     679              : }
     680              : 
     681              : /* Reset the len for unlimited polymorphic objects.  */
     682              : 
     683              : void
     684          630 : gfc_reset_len (stmtblock_t *block, gfc_expr *expr)
     685              : {
     686          630 :   gfc_expr *e;
     687          630 :   gfc_se se_len;
     688          630 :   e = gfc_find_and_cut_at_last_class_ref (expr);
     689          630 :   if (e == NULL)
     690            0 :     return;
     691          630 :   gfc_add_len_component (e);
     692          630 :   gfc_init_se (&se_len, NULL);
     693          630 :   gfc_conv_expr (&se_len, e);
     694          630 :   gfc_add_modify (block, se_len.expr,
     695          630 :                   fold_convert (TREE_TYPE (se_len.expr), integer_zero_node));
     696          630 :   gfc_free_expr (e);
     697              : }
     698              : 
     699              : 
     700              : /* Obtain the last class reference in a gfc_expr. Return NULL_TREE if no class
     701              :    reference is found. Note that it is up to the caller to avoid using this
     702              :    for expressions other than variables.  */
     703              : 
     704              : tree
     705         1451 : gfc_get_class_from_gfc_expr (gfc_expr *e)
     706              : {
     707         1451 :   gfc_expr *class_expr;
     708         1451 :   gfc_se cse;
     709         1451 :   class_expr = gfc_find_and_cut_at_last_class_ref (e);
     710         1451 :   if (class_expr == NULL)
     711              :     return NULL_TREE;
     712         1451 :   gfc_init_se (&cse, NULL);
     713         1451 :   gfc_conv_expr (&cse, class_expr);
     714         1451 :   gfc_free_expr (class_expr);
     715         1451 :   return cse.expr;
     716              : }
     717              : 
     718              : 
     719              : /* Obtain the last class reference in an expression.
     720              :    Return NULL_TREE if no class reference is found.  */
     721              : 
     722              : tree
     723       106874 : gfc_get_class_from_expr (tree expr)
     724              : {
     725       106874 :   tree tmp;
     726       106874 :   tree type;
     727       106874 :   bool array_descr_found = false;
     728       106874 :   bool comp_after_descr_found = false;
     729              : 
     730       275709 :   for (tmp = expr; tmp; tmp = TREE_OPERAND (tmp, 0))
     731              :     {
     732       275709 :       if (CONSTANT_CLASS_P (tmp))
     733              :         return NULL_TREE;
     734              : 
     735       275672 :       type = TREE_TYPE (tmp);
     736       319774 :       while (type)
     737              :         {
     738       311896 :           if (GFC_CLASS_TYPE_P (type))
     739              :             return tmp;
     740       292112 :           if (GFC_DESCRIPTOR_TYPE_P (type))
     741        35001 :             array_descr_found = true;
     742       292112 :           if (type != TYPE_CANONICAL (type))
     743        44102 :             type = TYPE_CANONICAL (type);
     744              :           else
     745              :             type = NULL_TREE;
     746              :         }
     747       255888 :       if (VAR_P (tmp) || TREE_CODE (tmp) == PARM_DECL)
     748              :         break;
     749              : 
     750              :       /* Avoid walking up the reference chain too far.  For class arrays, the
     751              :          array descriptor is a direct component (through a pointer) of the class
     752              :          container.  So there is exactly one COMPONENT_REF between a class
     753              :          container and its child array descriptor.  After seeing an array
     754              :          descriptor, we can give up on the second COMPONENT_REF we see, if no
     755              :          class container was found until that point.  */
     756       168835 :       if (array_descr_found)
     757              :         {
     758         7427 :           if (comp_after_descr_found)
     759              :             {
     760           12 :               if (TREE_CODE (tmp) == COMPONENT_REF)
     761              :                 return NULL_TREE;
     762              :             }
     763         7415 :           else if (TREE_CODE (tmp) == COMPONENT_REF)
     764         7427 :             comp_after_descr_found = true;
     765              :         }
     766              :     }
     767              : 
     768        87053 :   if (POINTER_TYPE_P (TREE_TYPE (tmp)))
     769        58348 :     tmp = build_fold_indirect_ref_loc (input_location, tmp);
     770              : 
     771        87053 :   if (GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
     772              :     return tmp;
     773              : 
     774              :   return NULL_TREE;
     775              : }
     776              : 
     777              : 
     778              : /* Obtain the vptr of the last class reference in an expression.
     779              :    Return NULL_TREE if no class reference is found.  */
     780              : 
     781              : tree
     782        11749 : gfc_get_vptr_from_expr (tree expr)
     783              : {
     784        11749 :   tree tmp;
     785              : 
     786        11749 :   tmp = gfc_get_class_from_expr (expr);
     787              : 
     788        11749 :   if (tmp != NULL_TREE)
     789        11684 :     return gfc_class_vptr_get (tmp);
     790              : 
     791              :   return NULL_TREE;
     792              : }
     793              : 
     794              : static void
     795         2347 : copy_coarray_desc_part (stmtblock_t *block, tree dest, tree src)
     796              : {
     797         2347 :   tree src_type = TREE_TYPE (src);
     798         2347 :   if (TYPE_LANG_SPECIFIC (src_type) && TYPE_LANG_SPECIFIC (src_type)->corank)
     799              :     {
     800          135 :       struct lang_type *lang_specific = TYPE_LANG_SPECIFIC (src_type);
     801          270 :       for (int c = 0; c < lang_specific->corank; ++c)
     802              :         {
     803          135 :           int dim = lang_specific->rank + c;
     804          135 :           tree codim = gfc_rank_cst[dim];
     805              : 
     806          135 :           if (lang_specific->lbound[dim])
     807           54 :             gfc_conv_descriptor_lbound_set (block, dest, codim,
     808              :                                             lang_specific->lbound[dim]);
     809              :           else
     810           81 :             gfc_conv_descriptor_lbound_set (
     811              :               block, dest, codim, gfc_conv_descriptor_lbound_get (src, codim));
     812          135 :           if (dim + 1 < lang_specific->corank)
     813              :             {
     814            0 :               if (lang_specific->ubound[dim])
     815            0 :                 gfc_conv_descriptor_ubound_set (block, dest, codim,
     816              :                                                 lang_specific->ubound[dim]);
     817              :               else
     818            0 :                 gfc_conv_descriptor_ubound_set (
     819              :                   block, dest, codim,
     820              :                   gfc_conv_descriptor_ubound_get (src, codim));
     821              :             }
     822              :         }
     823              :     }
     824         2347 : }
     825              : 
     826              : void
     827         2025 : gfc_class_array_data_assign (stmtblock_t *block, tree lhs_desc, tree rhs_desc,
     828              :                              bool lhs_type)
     829              : {
     830         2025 :   tree lhs_dim, rhs_dim, type;
     831              : 
     832         2025 :   gfc_conv_descriptor_data_set (block, lhs_desc,
     833              :                                 gfc_conv_descriptor_data_get (rhs_desc));
     834         2025 :   gfc_conv_descriptor_offset_set (block, lhs_desc,
     835              :                                   gfc_conv_descriptor_offset_get (rhs_desc));
     836              : 
     837         2025 :   gfc_add_modify (block, gfc_conv_descriptor_dtype (lhs_desc),
     838              :                   gfc_conv_descriptor_dtype (rhs_desc));
     839              : 
     840              :   /* Assign the dimension as range-ref.  */
     841         2025 :   lhs_dim = gfc_get_descriptor_dimension (lhs_desc);
     842         2025 :   rhs_dim = gfc_get_descriptor_dimension (rhs_desc);
     843              : 
     844         2025 :   type = lhs_type ? TREE_TYPE (lhs_dim) : TREE_TYPE (rhs_dim);
     845         2025 :   lhs_dim = build4_loc (input_location, ARRAY_RANGE_REF, type, lhs_dim,
     846              :                         gfc_index_zero_node, NULL_TREE, NULL_TREE);
     847         2025 :   rhs_dim = build4_loc (input_location, ARRAY_RANGE_REF, type, rhs_dim,
     848              :                         gfc_index_zero_node, NULL_TREE, NULL_TREE);
     849         2025 :   gfc_add_modify (block, lhs_dim, rhs_dim);
     850              : 
     851              :   /* The corank dimensions are not copied by the ARRAY_RANGE_REF.  */
     852         2025 :   copy_coarray_desc_part (block, lhs_desc, rhs_desc);
     853         2025 : }
     854              : 
     855              : /* Takes a derived type expression and returns the address of a temporary
     856              :    class object of the 'declared' type.  If opt_vptr_src is not NULL, this is
     857              :    used for the temporary class object.
     858              :    optional_alloc_ptr is false when the dummy is neither allocatable
     859              :    nor a pointer; that's only relevant for the optional handling.
     860              :    The optional argument 'derived_array' is used to preserve the parmse
     861              :    expression for deallocation of allocatable components. Assumed rank
     862              :    formal arguments made this necessary.  */
     863              : void
     864         5135 : gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym,
     865              :                            tree opt_vptr_src, bool optional,
     866              :                            bool optional_alloc_ptr, const char *proc_name,
     867              :                            tree *derived_array)
     868              : {
     869         5135 :   tree cond_optional = NULL_TREE;
     870         5135 :   gfc_ss *ss;
     871         5135 :   tree ctree;
     872         5135 :   tree var;
     873         5135 :   tree tmp;
     874         5135 :   tree packed = NULL_TREE;
     875              : 
     876              :   /* The derived type needs to be converted to a temporary CLASS object.  */
     877         5135 :   tmp = gfc_typenode_for_spec (&fsym->ts);
     878         5135 :   var = gfc_create_var (tmp, "class");
     879              : 
     880              :   /* Set the vptr.  */
     881         5135 :   if (opt_vptr_src)
     882          116 :     gfc_class_set_vptr (&parmse->pre, var, opt_vptr_src);
     883              :   else
     884         5019 :     gfc_reset_vptr (&parmse->pre, e, var);
     885              : 
     886              :   /* Now set the data field.  */
     887         5135 :   ctree = gfc_class_data_get (var);
     888              : 
     889         5135 :   if (flag_coarray == GFC_FCOARRAY_LIB && CLASS_DATA (fsym)->attr.codimension)
     890              :     {
     891            4 :       tree token;
     892            4 :       tmp = gfc_get_tree_for_caf_expr (e);
     893            4 :       if (POINTER_TYPE_P (TREE_TYPE (tmp)))
     894            2 :         tmp = build_fold_indirect_ref (tmp);
     895            4 :       gfc_get_caf_token_offset (parmse, &token, nullptr, tmp, NULL_TREE, e);
     896            4 :       gfc_add_modify (&parmse->pre, gfc_conv_descriptor_token (ctree), token);
     897              :     }
     898              : 
     899         5135 :   if (optional)
     900          576 :     cond_optional = gfc_conv_expr_present (e->symtree->n.sym);
     901              : 
     902              :   /* Set the _len as early as possible.  */
     903         5135 :   if (fsym->ts.u.derived->components->ts.type == BT_DERIVED
     904         5135 :       && fsym->ts.u.derived->components->ts.u.derived->attr
     905         5135 :            .unlimited_polymorphic)
     906              :     {
     907              :       /* Take care about initializing the _len component correctly.  */
     908          386 :       tree len_tree = gfc_class_len_get (var);
     909          386 :       if (UNLIMITED_POLY (e))
     910              :         {
     911           12 :           gfc_expr *len;
     912           12 :           gfc_se se;
     913              : 
     914           12 :           len = gfc_find_and_cut_at_last_class_ref (e);
     915           12 :           gfc_add_len_component (len);
     916           12 :           gfc_init_se (&se, NULL);
     917           12 :           gfc_conv_expr (&se, len);
     918           12 :           if (optional)
     919            0 :             tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se.expr),
     920              :                               cond_optional, se.expr,
     921            0 :                               fold_convert (TREE_TYPE (se.expr),
     922              :                                             integer_zero_node));
     923              :           else
     924           12 :             tmp = se.expr;
     925           12 :           gfc_free_expr (len);
     926           12 :         }
     927              :       else
     928          374 :         tmp = integer_zero_node;
     929          386 :       gfc_add_modify (&parmse->pre, len_tree,
     930          386 :                       fold_convert (TREE_TYPE (len_tree), tmp));
     931              :     }
     932              : 
     933         5135 :   if (parmse->expr && POINTER_TYPE_P (TREE_TYPE (parmse->expr)))
     934              :     {
     935              :       /* If there is a ready made pointer to a derived type, use it
     936              :          rather than evaluating the expression again.  */
     937          522 :       tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
     938          522 :       gfc_add_modify (&parmse->pre, ctree, tmp);
     939              :     }
     940         4613 :   else if (parmse->ss && parmse->ss->info && parmse->ss->info->useflags)
     941              :     {
     942              :       /* For an array reference in an elemental procedure call we need
     943              :          to retain the ss to provide the scalarized array reference.  */
     944          445 :       gfc_conv_expr_reference (parmse, e);
     945          445 :       tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
     946          445 :       if (optional)
     947            0 :         tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
     948              :                           cond_optional, tmp,
     949            0 :                           fold_convert (TREE_TYPE (tmp), null_pointer_node));
     950          445 :       gfc_add_modify (&parmse->pre, ctree, tmp);
     951              :     }
     952              :   else
     953              :     {
     954         4168 :       ss = gfc_walk_expr (e);
     955         4168 :       if (ss == gfc_ss_terminator)
     956              :         {
     957         2920 :           parmse->ss = NULL;
     958         2920 :           gfc_conv_expr_reference (parmse, e);
     959              : 
     960              :           /* Scalar to an assumed-rank array.  */
     961         2920 :           if (fsym->ts.u.derived->components->as)
     962              :             {
     963          322 :               tree type;
     964          322 :               type = get_scalar_to_descriptor_type (parmse->expr,
     965              :                                                     gfc_expr_attr (e));
     966          322 :               gfc_add_modify (&parmse->pre, gfc_conv_descriptor_dtype (ctree),
     967              :                               gfc_get_dtype (type));
     968          322 :               copy_coarray_desc_part (&parmse->pre, ctree, parmse->expr);
     969          322 :               if (optional)
     970          192 :                 parmse->expr = build3_loc (input_location, COND_EXPR,
     971           96 :                                            TREE_TYPE (parmse->expr),
     972              :                                            cond_optional, parmse->expr,
     973           96 :                                            fold_convert (TREE_TYPE (parmse->expr),
     974              :                                                          null_pointer_node));
     975          322 :               gfc_conv_descriptor_data_set (&parmse->pre, ctree, parmse->expr);
     976              :             }
     977              :           else
     978              :             {
     979         2598 :               tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
     980         2598 :               if (optional)
     981          132 :                 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
     982              :                                   cond_optional, tmp,
     983          132 :                                   fold_convert (TREE_TYPE (tmp),
     984              :                                                 null_pointer_node));
     985         2598 :               gfc_add_modify (&parmse->pre, ctree, tmp);
     986              :             }
     987              :         }
     988              :       else
     989              :         {
     990         1248 :           stmtblock_t block;
     991         1248 :           gfc_init_block (&block);
     992         1248 :           gfc_ref *ref;
     993         1248 :           int dim;
     994         1248 :           tree lbshift = NULL_TREE;
     995              : 
     996              :           /* Array refs with sections indicate, that a for a formal argument
     997              :              expecting contiguous repacking needs to be done.  */
     998         2345 :           for (ref = e->ref; ref; ref = ref->next)
     999         1247 :             if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
    1000              :               break;
    1001         1248 :           if (IS_CLASS_ARRAY (fsym)
    1002         1140 :               && (CLASS_DATA (fsym)->as->type == AS_EXPLICIT
    1003          882 :                   || CLASS_DATA (fsym)->as->type == AS_ASSUMED_SIZE)
    1004          354 :               && (ref || e->rank != fsym->ts.u.derived->components->as->rank))
    1005          144 :             fsym->attr.contiguous = 1;
    1006              : 
    1007              :           /* Detect any array references with vector subscripts.  */
    1008         2489 :           for (ref = e->ref; ref; ref = ref->next)
    1009         1247 :             if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT
    1010         1205 :                 && ref->u.ar.type != AR_FULL)
    1011              :               {
    1012          336 :                 for (dim = 0; dim < ref->u.ar.dimen; dim++)
    1013          192 :                   if (ref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
    1014              :                     break;
    1015          150 :                 if (dim < ref->u.ar.dimen)
    1016              :                   break;
    1017              :               }
    1018              :           /* Array references with vector subscripts and non-variable
    1019              :              expressions need be converted to a one-based descriptor.  */
    1020         1248 :           if (ref || e->expr_type != EXPR_VARIABLE)
    1021           49 :             lbshift = gfc_index_one_node;
    1022              : 
    1023         1248 :           parmse->expr = var;
    1024         1248 :           gfc_conv_array_parameter (parmse, e, false, fsym, proc_name, nullptr,
    1025              :                                     &lbshift, &packed);
    1026              : 
    1027         1248 :           if (derived_array && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (parmse->expr)))
    1028              :             {
    1029         1152 :               *derived_array
    1030         1152 :                 = gfc_create_var (TREE_TYPE (parmse->expr), "array");
    1031         1152 :               gfc_add_modify (&block, *derived_array, parmse->expr);
    1032              :             }
    1033              : 
    1034         1248 :           if (optional)
    1035              :             {
    1036          348 :               tmp = gfc_finish_block (&block);
    1037              : 
    1038          348 :               gfc_init_block (&block);
    1039          348 :               gfc_conv_descriptor_data_set (&block, ctree, null_pointer_node);
    1040          348 :               if (derived_array && *derived_array != NULL_TREE)
    1041          348 :                 gfc_conv_descriptor_data_set (&block, *derived_array,
    1042              :                                               null_pointer_node);
    1043              : 
    1044          348 :               tmp = build3_v (COND_EXPR, cond_optional, tmp,
    1045              :                               gfc_finish_block (&block));
    1046          348 :               gfc_add_expr_to_block (&parmse->pre, tmp);
    1047              :             }
    1048              :           else
    1049          900 :             gfc_add_block_to_block (&parmse->pre, &block);
    1050              :         }
    1051              :     }
    1052              : 
    1053              :   /* Pass the address of the class object.  */
    1054         5135 :   if (packed)
    1055           96 :     parmse->expr = packed;
    1056              :   else
    1057         5039 :     parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
    1058              : 
    1059         5135 :   if (optional && optional_alloc_ptr)
    1060           84 :     parmse->expr
    1061           84 :       = build3_loc (input_location, COND_EXPR, TREE_TYPE (parmse->expr),
    1062              :                     cond_optional, parmse->expr,
    1063           84 :                     fold_convert (TREE_TYPE (parmse->expr), null_pointer_node));
    1064         5135 : }
    1065              : 
    1066              : /* Create a new class container, which is required as scalar coarrays
    1067              :    have an array descriptor while normal scalars haven't. Optionally,
    1068              :    NULL pointer checks are added if the argument is OPTIONAL.  */
    1069              : 
    1070              : static void
    1071           48 : class_scalar_coarray_to_class (gfc_se *parmse, gfc_expr *e,
    1072              :                                gfc_typespec class_ts, bool optional)
    1073              : {
    1074           48 :   tree var, ctree, tmp;
    1075           48 :   stmtblock_t block;
    1076           48 :   gfc_ref *ref;
    1077           48 :   gfc_ref *class_ref;
    1078              : 
    1079           48 :   gfc_init_block (&block);
    1080              : 
    1081           48 :   class_ref = NULL;
    1082          144 :   for (ref = e->ref; ref; ref = ref->next)
    1083              :     {
    1084           96 :       if (ref->type == REF_COMPONENT
    1085           48 :             && ref->u.c.component->ts.type == BT_CLASS)
    1086           96 :         class_ref = ref;
    1087              :     }
    1088              : 
    1089           48 :   if (class_ref == NULL
    1090           48 :         && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
    1091           48 :     tmp = e->symtree->n.sym->backend_decl;
    1092              :   else
    1093              :     {
    1094              :       /* Remove everything after the last class reference, convert the
    1095              :          expression and then recover its tailend once more.  */
    1096            0 :       gfc_se tmpse;
    1097            0 :       ref = class_ref->next;
    1098            0 :       class_ref->next = NULL;
    1099            0 :       gfc_init_se (&tmpse, NULL);
    1100            0 :       gfc_conv_expr (&tmpse, e);
    1101            0 :       class_ref->next = ref;
    1102            0 :       tmp = tmpse.expr;
    1103              :     }
    1104              : 
    1105           48 :   var = gfc_typenode_for_spec (&class_ts);
    1106           48 :   var = gfc_create_var (var, "class");
    1107              : 
    1108           48 :   ctree = gfc_class_vptr_get (var);
    1109           96 :   gfc_add_modify (&block, ctree,
    1110           48 :                   fold_convert (TREE_TYPE (ctree), gfc_class_vptr_get (tmp)));
    1111              : 
    1112           48 :   ctree = gfc_class_data_get (var);
    1113           48 :   tmp = gfc_conv_descriptor_data_get (
    1114           48 :     gfc_class_data_get (GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (tmp)))
    1115              :                           ? tmp
    1116           24 :                           : GFC_DECL_SAVED_DESCRIPTOR (tmp)));
    1117           48 :   gfc_add_modify (&block, ctree, fold_convert (TREE_TYPE (ctree), tmp));
    1118              : 
    1119              :   /* Pass the address of the class object.  */
    1120           48 :   parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
    1121              : 
    1122           48 :   if (optional)
    1123              :     {
    1124           48 :       tree cond = gfc_conv_expr_present (e->symtree->n.sym);
    1125           48 :       tree tmp2;
    1126              : 
    1127           48 :       tmp = gfc_finish_block (&block);
    1128              : 
    1129           48 :       gfc_init_block (&block);
    1130           48 :       tmp2 = gfc_class_data_get (var);
    1131           48 :       gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
    1132              :                                                   null_pointer_node));
    1133           48 :       tmp2 = gfc_finish_block (&block);
    1134              : 
    1135           48 :       tmp = build3_loc (input_location, COND_EXPR, void_type_node,
    1136              :                         cond, tmp, tmp2);
    1137           48 :       gfc_add_expr_to_block (&parmse->pre, tmp);
    1138              :     }
    1139              :   else
    1140            0 :     gfc_add_block_to_block (&parmse->pre, &block);
    1141           48 : }
    1142              : 
    1143              : 
    1144              : /* Takes an intrinsic type expression and returns the address of a temporary
    1145              :    class object of the 'declared' type.  */
    1146              : void
    1147          882 : gfc_conv_intrinsic_to_class (gfc_se *parmse, gfc_expr *e,
    1148              :                              gfc_typespec class_ts)
    1149              : {
    1150          882 :   gfc_symbol *vtab;
    1151          882 :   gfc_ss *ss;
    1152          882 :   tree ctree;
    1153          882 :   tree var;
    1154          882 :   tree tmp;
    1155          882 :   int dim;
    1156          882 :   bool unlimited_poly;
    1157              : 
    1158         1764 :   unlimited_poly = class_ts.type == BT_CLASS
    1159          882 :                    && class_ts.u.derived->components->ts.type == BT_DERIVED
    1160          882 :                    && class_ts.u.derived->components->ts.u.derived
    1161          882 :                                                 ->attr.unlimited_polymorphic;
    1162              : 
    1163              :   /* The intrinsic type needs to be converted to a temporary
    1164              :      CLASS object.  */
    1165          882 :   tmp = gfc_typenode_for_spec (&class_ts);
    1166          882 :   var = gfc_create_var (tmp, "class");
    1167              : 
    1168              :   /* Force a temporary for component or substring references.  */
    1169          882 :   if (unlimited_poly
    1170          882 :       && class_ts.u.derived->components->attr.dimension
    1171          623 :       && !class_ts.u.derived->components->attr.allocatable
    1172          623 :       && !class_ts.u.derived->components->attr.class_pointer
    1173         1505 :       && is_subref_array (e))
    1174           17 :     parmse->force_tmp = 1;
    1175              : 
    1176              :   /* Set the vptr.  */
    1177          882 :   ctree = gfc_class_vptr_get (var);
    1178              : 
    1179          882 :   vtab = gfc_find_vtab (&e->ts);
    1180          882 :   gcc_assert (vtab);
    1181          882 :   tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
    1182          882 :   gfc_add_modify (&parmse->pre, ctree,
    1183          882 :                   fold_convert (TREE_TYPE (ctree), tmp));
    1184              : 
    1185              :   /* Now set the data field.  */
    1186          882 :   ctree = gfc_class_data_get (var);
    1187          882 :   if (parmse->ss && parmse->ss->info->useflags)
    1188              :     {
    1189              :       /* For an array reference in an elemental procedure call we need
    1190              :          to retain the ss to provide the scalarized array reference.  */
    1191           36 :       gfc_conv_expr_reference (parmse, e);
    1192           36 :       tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
    1193           36 :       gfc_add_modify (&parmse->pre, ctree, tmp);
    1194              :     }
    1195              :   else
    1196              :     {
    1197          846 :       ss = gfc_walk_expr (e);
    1198          846 :       if (ss == gfc_ss_terminator)
    1199              :         {
    1200          247 :           parmse->ss = NULL;
    1201          247 :           gfc_conv_expr_reference (parmse, e);
    1202          247 :           if (class_ts.u.derived->components->as
    1203           24 :               && class_ts.u.derived->components->as->type == AS_ASSUMED_RANK)
    1204              :             {
    1205           24 :               tmp = gfc_conv_scalar_to_descriptor (parmse, parmse->expr,
    1206              :                                                    gfc_expr_attr (e));
    1207           24 :               tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
    1208           24 :                                      TREE_TYPE (ctree), tmp);
    1209              :             }
    1210              :           else
    1211          223 :               tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
    1212          247 :           gfc_add_modify (&parmse->pre, ctree, tmp);
    1213              :         }
    1214              :       else
    1215              :         {
    1216          599 :           parmse->ss = ss;
    1217          599 :           gfc_conv_expr_descriptor (parmse, e);
    1218              : 
    1219              :           /* Array references with vector subscripts and non-variable expressions
    1220              :              need be converted to a one-based descriptor.  */
    1221          599 :           if (e->expr_type != EXPR_VARIABLE)
    1222              :             {
    1223          368 :               for (dim = 0; dim < e->rank; ++dim)
    1224          193 :                 gfc_conv_shift_descriptor_lbound (&parmse->pre, parmse->expr,
    1225              :                                                   dim, gfc_index_one_node);
    1226              :             }
    1227              : 
    1228          599 :           if (class_ts.u.derived->components->as->rank != e->rank)
    1229              :             {
    1230           49 :               tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
    1231           49 :                                      TREE_TYPE (ctree), parmse->expr);
    1232           49 :               gfc_add_modify (&parmse->pre, ctree, tmp);
    1233              :             }
    1234              :           else
    1235          550 :             gfc_add_modify (&parmse->pre, ctree, parmse->expr);
    1236              :         }
    1237              :     }
    1238              : 
    1239          882 :   gcc_assert (class_ts.type == BT_CLASS);
    1240          882 :   if (unlimited_poly)
    1241              :     {
    1242          882 :       ctree = gfc_class_len_get (var);
    1243              :       /* When the actual arg is a char array, then set the _len component of the
    1244              :          unlimited polymorphic entity to the length of the string.  */
    1245          882 :       if (e->ts.type == BT_CHARACTER)
    1246              :         {
    1247              :           /* Start with parmse->string_length because this seems to be set to a
    1248              :            correct value more often.  */
    1249          175 :           if (parmse->string_length)
    1250              :             tmp = parmse->string_length;
    1251              :           /* When the string_length is not yet set, then try the backend_decl of
    1252              :            the cl.  */
    1253            0 :           else if (e->ts.u.cl->backend_decl)
    1254              :             tmp = e->ts.u.cl->backend_decl;
    1255              :           /* If both of the above approaches fail, then try to generate an
    1256              :            expression from the input, which is only feasible currently, when the
    1257              :            expression can be evaluated to a constant one.  */
    1258              :           else
    1259              :             {
    1260              :               /* Try to simplify the expression.  */
    1261            0 :               gfc_simplify_expr (e, 0);
    1262            0 :               if (e->expr_type == EXPR_CONSTANT && !e->ts.u.cl->resolved)
    1263              :                 {
    1264              :                   /* Amazingly all data is present to compute the length of a
    1265              :                    constant string, but the expression is not yet there.  */
    1266            0 :                   e->ts.u.cl->length = gfc_get_constant_expr (BT_INTEGER,
    1267              :                                                               gfc_charlen_int_kind,
    1268              :                                                               &e->where);
    1269            0 :                   mpz_set_ui (e->ts.u.cl->length->value.integer,
    1270            0 :                               e->value.character.length);
    1271            0 :                   gfc_conv_const_charlen (e->ts.u.cl);
    1272            0 :                   e->ts.u.cl->resolved = 1;
    1273            0 :                   tmp = e->ts.u.cl->backend_decl;
    1274              :                 }
    1275              :               else
    1276              :                 {
    1277            0 :                   gfc_error ("Cannot compute the length of the char array "
    1278              :                              "at %L.", &e->where);
    1279              :                 }
    1280              :             }
    1281              :         }
    1282              :       else
    1283          707 :         tmp = integer_zero_node;
    1284              : 
    1285          882 :       gfc_add_modify (&parmse->pre, ctree, fold_convert (TREE_TYPE (ctree), tmp));
    1286              :     }
    1287              : 
    1288              :   /* Pass the address of the class object.  */
    1289          882 :   parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
    1290          882 : }
    1291              : 
    1292              : 
    1293              : /* Takes a scalarized class array expression and returns the
    1294              :    address of a temporary scalar class object of the 'declared'
    1295              :    type.
    1296              :    OOP-TODO: This could be improved by adding code that branched on
    1297              :    the dynamic type being the same as the declared type. In this case
    1298              :    the original class expression can be passed directly.
    1299              :    optional_alloc_ptr is false when the dummy is neither allocatable
    1300              :    nor a pointer; that's relevant for the optional handling.
    1301              :    Set copyback to true if class container's _data and _vtab pointers
    1302              :    might get modified.  */
    1303              : 
    1304              : void
    1305         3609 : gfc_conv_class_to_class (gfc_se *parmse, gfc_expr *e, gfc_typespec class_ts,
    1306              :                          bool elemental, bool copyback, bool optional,
    1307              :                          bool optional_alloc_ptr)
    1308              : {
    1309         3609 :   tree ctree;
    1310         3609 :   tree var;
    1311         3609 :   tree tmp;
    1312         3609 :   tree vptr;
    1313         3609 :   tree cond = NULL_TREE;
    1314         3609 :   tree slen = NULL_TREE;
    1315         3609 :   gfc_ref *ref;
    1316         3609 :   gfc_ref *class_ref;
    1317         3609 :   stmtblock_t block;
    1318         3609 :   bool full_array = false;
    1319              : 
    1320              :   /* If this is the data field of a class temporary, the class expression
    1321              :      can be obtained and returned directly.  */
    1322         3609 :   if (e->expr_type != EXPR_VARIABLE
    1323          180 :       && TREE_CODE (parmse->expr) == COMPONENT_REF
    1324           36 :       && !GFC_CLASS_TYPE_P (TREE_TYPE (parmse->expr))
    1325         3645 :       && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (parmse->expr, 0))))
    1326              :     {
    1327           36 :       parmse->expr = TREE_OPERAND (parmse->expr, 0);
    1328           36 :       if (!VAR_P (parmse->expr))
    1329            0 :         parmse->expr = gfc_evaluate_now (parmse->expr, &parmse->pre);
    1330           36 :       parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
    1331          174 :       return;
    1332              :     }
    1333              : 
    1334         3573 :   gfc_init_block (&block);
    1335              : 
    1336         3573 :   class_ref = NULL;
    1337         7162 :   for (ref = e->ref; ref; ref = ref->next)
    1338              :     {
    1339         6786 :       if (ref->type == REF_COMPONENT
    1340         3623 :             && ref->u.c.component->ts.type == BT_CLASS)
    1341         6786 :         class_ref = ref;
    1342              : 
    1343         6786 :       if (ref->next == NULL)
    1344              :         break;
    1345              :     }
    1346              : 
    1347         3573 :   if ((ref == NULL || class_ref == ref)
    1348          488 :       && !(gfc_is_class_array_function (e) && parmse->class_vptr != NULL_TREE)
    1349         4043 :       && (!class_ts.u.derived->components->as
    1350          379 :           || class_ts.u.derived->components->as->rank != -1))
    1351              :     return;
    1352              : 
    1353              :   /* Test for FULL_ARRAY.  */
    1354         3435 :   if (e->rank == 0
    1355         3435 :       && ((gfc_expr_attr (e).codimension && gfc_expr_attr (e).dimension)
    1356          494 :           || (class_ts.u.derived->components->as
    1357          366 :               && class_ts.u.derived->components->as->type == AS_ASSUMED_RANK)))
    1358          411 :     full_array = true;
    1359              :   else
    1360         3024 :     gfc_is_class_array_ref (e, &full_array);
    1361              : 
    1362              :   /* The derived type needs to be converted to a temporary
    1363              :      CLASS object.  */
    1364         3435 :   tmp = gfc_typenode_for_spec (&class_ts);
    1365         3435 :   var = gfc_create_var (tmp, "class");
    1366              : 
    1367              :   /* Set the data.  */
    1368         3435 :   ctree = gfc_class_data_get (var);
    1369         3435 :   if (class_ts.u.derived->components->as
    1370         3151 :       && e->rank != class_ts.u.derived->components->as->rank)
    1371              :     {
    1372          965 :       if (e->rank == 0)
    1373              :         {
    1374          356 :           tree type = get_scalar_to_descriptor_type (parmse->expr,
    1375              :                                                      gfc_expr_attr (e));
    1376          356 :           gfc_add_modify (&block, gfc_conv_descriptor_dtype (ctree),
    1377              :                           gfc_get_dtype (type));
    1378              : 
    1379          356 :           tmp = gfc_class_data_get (parmse->expr);
    1380          356 :           if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
    1381           12 :             tmp = gfc_build_addr_expr (NULL_TREE, tmp);
    1382              : 
    1383          356 :           gfc_conv_descriptor_data_set (&block, ctree, tmp);
    1384              :         }
    1385              :       else
    1386          609 :         gfc_class_array_data_assign (&block, ctree, parmse->expr, false);
    1387              :     }
    1388              :   else
    1389              :     {
    1390         2470 :       if (TREE_TYPE (parmse->expr) != TREE_TYPE (ctree))
    1391         1418 :         parmse->expr = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
    1392         1418 :                                         TREE_TYPE (ctree), parmse->expr);
    1393         2470 :       gfc_add_modify (&block, ctree, parmse->expr);
    1394              :     }
    1395              : 
    1396              :   /* Return the data component, except in the case of scalarized array
    1397              :      references, where nullification of the cannot occur and so there
    1398              :      is no need.  */
    1399         3435 :   if (!elemental && full_array && copyback)
    1400              :     {
    1401         1149 :       if (class_ts.u.derived->components->as
    1402         1149 :           && e->rank != class_ts.u.derived->components->as->rank)
    1403              :         {
    1404          270 :           if (e->rank == 0)
    1405              :             {
    1406          102 :               tmp = gfc_class_data_get (parmse->expr);
    1407          204 :               gfc_add_modify (&parmse->post, tmp,
    1408          102 :                               fold_convert (TREE_TYPE (tmp),
    1409              :                                          gfc_conv_descriptor_data_get (ctree)));
    1410              :             }
    1411              :           else
    1412          168 :             gfc_class_array_data_assign (&parmse->post, parmse->expr, ctree,
    1413              :                                          true);
    1414              :         }
    1415              :       else
    1416          879 :         gfc_add_modify (&parmse->post, parmse->expr, ctree);
    1417              :     }
    1418              : 
    1419              :   /* Set the vptr.  */
    1420         3435 :   ctree = gfc_class_vptr_get (var);
    1421              : 
    1422              :   /* The vptr is the second field of the actual argument.
    1423              :      First we have to find the corresponding class reference.  */
    1424              : 
    1425         3435 :   tmp = NULL_TREE;
    1426         3435 :   if (gfc_is_class_array_function (e)
    1427         3435 :       && parmse->class_vptr != NULL_TREE)
    1428              :     tmp = parmse->class_vptr;
    1429         3417 :   else if (class_ref == NULL
    1430         2973 :            && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
    1431              :     {
    1432         2973 :       tmp = e->symtree->n.sym->backend_decl;
    1433              : 
    1434         2973 :       if (TREE_CODE (tmp) == FUNCTION_DECL)
    1435            6 :         tmp = gfc_get_fake_result_decl (e->symtree->n.sym, 0);
    1436              : 
    1437         2973 :       if (DECL_LANG_SPECIFIC (tmp) && GFC_DECL_SAVED_DESCRIPTOR (tmp))
    1438          397 :         tmp = GFC_DECL_SAVED_DESCRIPTOR (tmp);
    1439              : 
    1440         2973 :       slen = build_zero_cst (size_type_node);
    1441              :     }
    1442          444 :   else if (parmse->class_container != NULL_TREE)
    1443              :     /* Don't redundantly evaluate the expression if the required information
    1444              :        is already available.  */
    1445              :     tmp = parmse->class_container;
    1446              :   else
    1447              :     {
    1448              :       /* Remove everything after the last class reference, convert the
    1449              :          expression and then recover its tailend once more.  */
    1450           18 :       gfc_se tmpse;
    1451           18 :       ref = class_ref->next;
    1452           18 :       class_ref->next = NULL;
    1453           18 :       gfc_init_se (&tmpse, NULL);
    1454           18 :       gfc_conv_expr (&tmpse, e);
    1455           18 :       class_ref->next = ref;
    1456           18 :       tmp = tmpse.expr;
    1457           18 :       slen = tmpse.string_length;
    1458              :     }
    1459              : 
    1460         3435 :   gcc_assert (tmp != NULL_TREE);
    1461              : 
    1462              :   /* Dereference if needs be.  */
    1463         3435 :   if (TREE_CODE (TREE_TYPE (tmp)) == REFERENCE_TYPE)
    1464          345 :     tmp = build_fold_indirect_ref_loc (input_location, tmp);
    1465              : 
    1466         3435 :   if (!(gfc_is_class_array_function (e) && parmse->class_vptr))
    1467         3417 :     vptr = gfc_class_vptr_get (tmp);
    1468              :   else
    1469              :     vptr = tmp;
    1470              : 
    1471         3435 :   gfc_add_modify (&block, ctree,
    1472         3435 :                   fold_convert (TREE_TYPE (ctree), vptr));
    1473              : 
    1474              :   /* Return the vptr component, except in the case of scalarized array
    1475              :      references, where the dynamic type cannot change.  */
    1476         3435 :   if (!elemental && full_array && copyback)
    1477         1149 :     gfc_add_modify (&parmse->post, vptr,
    1478         1149 :                     fold_convert (TREE_TYPE (vptr), ctree));
    1479              : 
    1480              :   /* For unlimited polymorphic objects also set the _len component.  */
    1481         3435 :   if (class_ts.type == BT_CLASS
    1482         3435 :       && class_ts.u.derived->components
    1483         3435 :       && class_ts.u.derived->components->ts.u
    1484         3435 :                       .derived->attr.unlimited_polymorphic)
    1485              :     {
    1486         1109 :       ctree = gfc_class_len_get (var);
    1487         1109 :       if (UNLIMITED_POLY (e))
    1488          913 :         tmp = gfc_class_len_get (tmp);
    1489          196 :       else if (e->ts.type == BT_CHARACTER)
    1490              :         {
    1491            0 :           gcc_assert (slen != NULL_TREE);
    1492              :           tmp = slen;
    1493              :         }
    1494              :       else
    1495          196 :         tmp = build_zero_cst (size_type_node);
    1496         1109 :       gfc_add_modify (&parmse->pre, ctree,
    1497         1109 :                       fold_convert (TREE_TYPE (ctree), tmp));
    1498              : 
    1499              :       /* Return the len component, except in the case of scalarized array
    1500              :         references, where the dynamic type cannot change.  */
    1501         1109 :       if (!elemental && full_array && copyback
    1502          440 :           && (UNLIMITED_POLY (e) || VAR_P (tmp)))
    1503          428 :           gfc_add_modify (&parmse->post, tmp,
    1504          428 :                           fold_convert (TREE_TYPE (tmp), ctree));
    1505              :     }
    1506              : 
    1507         3435 :   if (optional)
    1508              :     {
    1509          510 :       tree tmp2;
    1510              : 
    1511          510 :       cond = gfc_conv_expr_present (e->symtree->n.sym);
    1512              :       /* parmse->pre may contain some preparatory instructions for the
    1513              :          temporary array descriptor.  Those may only be executed when the
    1514              :          optional argument is set, therefore add parmse->pre's instructions
    1515              :          to block, which is later guarded by an if (optional_arg_given).  */
    1516          510 :       gfc_add_block_to_block (&parmse->pre, &block);
    1517          510 :       block.head = parmse->pre.head;
    1518          510 :       parmse->pre.head = NULL_TREE;
    1519          510 :       tmp = gfc_finish_block (&block);
    1520              : 
    1521          510 :       if (optional_alloc_ptr)
    1522          102 :         tmp2 = build_empty_stmt (input_location);
    1523              :       else
    1524              :         {
    1525          408 :           gfc_init_block (&block);
    1526              : 
    1527          408 :           tmp2 = gfc_conv_descriptor_data_get (gfc_class_data_get (var));
    1528          408 :           gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
    1529              :                                                       null_pointer_node));
    1530          408 :           tmp2 = gfc_finish_block (&block);
    1531              :         }
    1532              : 
    1533          510 :       tmp = build3_loc (input_location, COND_EXPR, void_type_node,
    1534              :                         cond, tmp, tmp2);
    1535          510 :       gfc_add_expr_to_block (&parmse->pre, tmp);
    1536              : 
    1537          510 :       if (!elemental && full_array && copyback)
    1538              :         {
    1539           30 :           tmp2 = build_empty_stmt (input_location);
    1540           30 :           tmp = gfc_finish_block (&parmse->post);
    1541           30 :           tmp = build3_loc (input_location, COND_EXPR, void_type_node,
    1542              :                             cond, tmp, tmp2);
    1543           30 :           gfc_add_expr_to_block (&parmse->post, tmp);
    1544              :         }
    1545              :     }
    1546              :   else
    1547         2925 :     gfc_add_block_to_block (&parmse->pre, &block);
    1548              : 
    1549              :   /* Pass the address of the class object.  */
    1550         3435 :   parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
    1551              : 
    1552         3435 :   if (optional && optional_alloc_ptr)
    1553          204 :     parmse->expr = build3_loc (input_location, COND_EXPR,
    1554          102 :                                TREE_TYPE (parmse->expr),
    1555              :                                cond, parmse->expr,
    1556          102 :                                fold_convert (TREE_TYPE (parmse->expr),
    1557              :                                              null_pointer_node));
    1558              : }
    1559              : 
    1560              : 
    1561              : /* Given a class array declaration and an index, returns the address
    1562              :    of the referenced element.  */
    1563              : 
    1564              : static tree
    1565          712 : gfc_get_class_array_ref (tree index, tree class_decl, tree data_comp,
    1566              :                          bool unlimited)
    1567              : {
    1568          712 :   tree data, size, tmp, ctmp, offset, ptr;
    1569              : 
    1570          712 :   data = data_comp != NULL_TREE ? data_comp :
    1571            0 :                                   gfc_class_data_get (class_decl);
    1572          712 :   size = gfc_class_vtab_size_get (class_decl);
    1573              : 
    1574          712 :   if (unlimited)
    1575              :     {
    1576          200 :       tmp = fold_convert (gfc_array_index_type,
    1577              :                           gfc_class_len_get (class_decl));
    1578          200 :       ctmp = fold_build2_loc (input_location, MULT_EXPR,
    1579              :                               gfc_array_index_type, size, tmp);
    1580          200 :       tmp = fold_build2_loc (input_location, GT_EXPR,
    1581              :                              logical_type_node, tmp,
    1582          200 :                              build_zero_cst (TREE_TYPE (tmp)));
    1583          200 :       size = fold_build3_loc (input_location, COND_EXPR,
    1584              :                               gfc_array_index_type, tmp, ctmp, size);
    1585              :     }
    1586              : 
    1587          712 :   offset = fold_build2_loc (input_location, MULT_EXPR,
    1588              :                             gfc_array_index_type,
    1589              :                             index, size);
    1590              : 
    1591          712 :   data = gfc_conv_descriptor_data_get (data);
    1592          712 :   ptr = fold_convert (pvoid_type_node, data);
    1593          712 :   ptr = fold_build_pointer_plus_loc (input_location, ptr, offset);
    1594          712 :   return fold_convert (TREE_TYPE (data), ptr);
    1595              : }
    1596              : 
    1597              : 
    1598              : /* Copies one class expression to another, assuming that if either
    1599              :    'to' or 'from' are arrays they are packed.  Should 'from' be
    1600              :    NULL_TREE, the initialization expression for 'to' is used, assuming
    1601              :    that the _vptr is set.  */
    1602              : 
    1603              : tree
    1604          758 : gfc_copy_class_to_class (tree from, tree to, tree nelems, bool unlimited)
    1605              : {
    1606          758 :   tree fcn;
    1607          758 :   tree fcn_type;
    1608          758 :   tree from_data;
    1609          758 :   tree from_len;
    1610          758 :   tree to_data;
    1611          758 :   tree to_len;
    1612          758 :   tree to_ref;
    1613          758 :   tree from_ref;
    1614          758 :   vec<tree, va_gc> *args;
    1615          758 :   tree tmp;
    1616          758 :   tree stdcopy;
    1617          758 :   tree extcopy;
    1618          758 :   tree index;
    1619          758 :   bool is_from_desc = false, is_to_class = false;
    1620              : 
    1621          758 :   args = NULL;
    1622              :   /* To prevent warnings on uninitialized variables.  */
    1623          758 :   from_len = to_len = NULL_TREE;
    1624              : 
    1625          758 :   if (from != NULL_TREE)
    1626          758 :     fcn = gfc_class_vtab_copy_get (from);
    1627              :   else
    1628            0 :     fcn = gfc_class_vtab_copy_get (to);
    1629              : 
    1630          758 :   fcn_type = TREE_TYPE (TREE_TYPE (fcn));
    1631              : 
    1632          758 :   if (from != NULL_TREE)
    1633              :     {
    1634          758 :       is_from_desc = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from));
    1635          758 :       if (is_from_desc)
    1636              :         {
    1637            0 :           from_data = from;
    1638            0 :           from = GFC_DECL_SAVED_DESCRIPTOR (from);
    1639              :         }
    1640              :       else
    1641              :         {
    1642              :           /* Check that from is a class.  When the class is part of a coarray,
    1643              :              then from is a common pointer and is to be used as is.  */
    1644         1516 :           tmp = POINTER_TYPE_P (TREE_TYPE (from))
    1645          758 :               ? build_fold_indirect_ref (from) : from;
    1646         1516 :           from_data =
    1647          758 :               (GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
    1648            0 :                || (DECL_P (tmp) && GFC_DECL_CLASS (tmp)))
    1649          758 :               ? gfc_class_data_get (from) : from;
    1650          758 :           is_from_desc = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data));
    1651              :         }
    1652              :      }
    1653              :   else
    1654            0 :     from_data = gfc_class_vtab_def_init_get (to);
    1655              : 
    1656          758 :   if (unlimited)
    1657              :     {
    1658          160 :       if (from != NULL_TREE && unlimited)
    1659          160 :         from_len = gfc_class_len_or_zero_get (from);
    1660              :       else
    1661            0 :         from_len = build_zero_cst (size_type_node);
    1662              :     }
    1663              : 
    1664          758 :   if (GFC_CLASS_TYPE_P (TREE_TYPE (to)))
    1665              :     {
    1666          758 :       is_to_class = true;
    1667          758 :       to_data = gfc_class_data_get (to);
    1668          758 :       if (unlimited)
    1669          160 :         to_len = gfc_class_len_get (to);
    1670              :     }
    1671              :   else
    1672              :     /* When to is a BT_DERIVED and not a BT_CLASS, then to_data == to.  */
    1673            0 :     to_data = to;
    1674              : 
    1675          758 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (to_data)))
    1676              :     {
    1677          356 :       stmtblock_t loopbody;
    1678          356 :       stmtblock_t body;
    1679          356 :       stmtblock_t ifbody;
    1680          356 :       gfc_loopinfo loop;
    1681              : 
    1682          356 :       gfc_init_block (&body);
    1683          356 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
    1684              :                              gfc_array_index_type, nelems,
    1685              :                              gfc_index_one_node);
    1686          356 :       nelems = gfc_evaluate_now (tmp, &body);
    1687          356 :       index = gfc_create_var (gfc_array_index_type, "S");
    1688              : 
    1689          356 :       if (is_from_desc)
    1690              :         {
    1691          356 :           from_ref = gfc_get_class_array_ref (index, from, from_data,
    1692              :                                               unlimited);
    1693          356 :           vec_safe_push (args, from_ref);
    1694              :         }
    1695              :       else
    1696            0 :         vec_safe_push (args, from_data);
    1697              : 
    1698          356 :       if (is_to_class)
    1699          356 :         to_ref = gfc_get_class_array_ref (index, to, to_data, unlimited);
    1700              :       else
    1701              :         {
    1702            0 :           tmp = gfc_conv_array_data (to);
    1703            0 :           tmp = build_fold_indirect_ref_loc (input_location, tmp);
    1704            0 :           to_ref = gfc_build_addr_expr (NULL_TREE,
    1705              :                                         gfc_build_array_ref (tmp, index, to));
    1706              :         }
    1707          356 :       vec_safe_push (args, to_ref);
    1708              : 
    1709              :       /* Add bounds check.  */
    1710          356 :       if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) > 0 && is_from_desc)
    1711              :         {
    1712           25 :           const char *name = "<<unknown>>";
    1713           25 :           int dim, rank;
    1714              : 
    1715           25 :           if (DECL_P (to))
    1716            0 :             name = IDENTIFIER_POINTER (DECL_NAME (to));
    1717              : 
    1718           25 :           rank = GFC_TYPE_ARRAY_RANK (TREE_TYPE (from_data));
    1719           55 :           for (dim = 1; dim <= rank; dim++)
    1720              :             {
    1721           30 :               tree from_len, to_len, cond;
    1722           30 :               char *msg;
    1723              : 
    1724           30 :               from_len = gfc_conv_descriptor_size (from_data, dim);
    1725           30 :               from_len = fold_convert (long_integer_type_node, from_len);
    1726           30 :               to_len = gfc_conv_descriptor_size (to_data, dim);
    1727           30 :               to_len = fold_convert (long_integer_type_node, to_len);
    1728           30 :               msg = xasprintf ("Array bound mismatch for dimension %d "
    1729              :                                "of array '%s' (%%ld/%%ld)",
    1730              :                                dim, name);
    1731           30 :               cond = fold_build2_loc (input_location, NE_EXPR,
    1732              :                                       logical_type_node, from_len, to_len);
    1733           30 :               gfc_trans_runtime_check (true, false, cond, &body,
    1734              :                                        NULL, msg, to_len, from_len);
    1735           30 :               free (msg);
    1736              :             }
    1737              :         }
    1738              : 
    1739          356 :       tmp = build_call_vec (fcn_type, fcn, args);
    1740              : 
    1741              :       /* Build the body of the loop.  */
    1742          356 :       gfc_init_block (&loopbody);
    1743          356 :       gfc_add_expr_to_block (&loopbody, tmp);
    1744              : 
    1745              :       /* Build the loop and return.  */
    1746          356 :       gfc_init_loopinfo (&loop);
    1747          356 :       loop.dimen = 1;
    1748          356 :       loop.from[0] = gfc_index_zero_node;
    1749          356 :       loop.loopvar[0] = index;
    1750          356 :       loop.to[0] = nelems;
    1751          356 :       gfc_trans_scalarizing_loops (&loop, &loopbody);
    1752          356 :       gfc_init_block (&ifbody);
    1753          356 :       gfc_add_block_to_block (&ifbody, &loop.pre);
    1754          356 :       stdcopy = gfc_finish_block (&ifbody);
    1755              :       /* In initialization mode from_len is a constant zero.  */
    1756          356 :       if (unlimited && !integer_zerop (from_len))
    1757              :         {
    1758          100 :           vec_safe_push (args, from_len);
    1759          100 :           vec_safe_push (args, to_len);
    1760          100 :           tmp = build_call_vec (fcn_type, fcn, args);
    1761              :           /* Build the body of the loop.  */
    1762          100 :           gfc_init_block (&loopbody);
    1763          100 :           gfc_add_expr_to_block (&loopbody, tmp);
    1764              : 
    1765              :           /* Build the loop and return.  */
    1766          100 :           gfc_init_loopinfo (&loop);
    1767          100 :           loop.dimen = 1;
    1768          100 :           loop.from[0] = gfc_index_zero_node;
    1769          100 :           loop.loopvar[0] = index;
    1770          100 :           loop.to[0] = nelems;
    1771          100 :           gfc_trans_scalarizing_loops (&loop, &loopbody);
    1772          100 :           gfc_init_block (&ifbody);
    1773          100 :           gfc_add_block_to_block (&ifbody, &loop.pre);
    1774          100 :           extcopy = gfc_finish_block (&ifbody);
    1775              : 
    1776          100 :           tmp = fold_build2_loc (input_location, GT_EXPR,
    1777              :                                  logical_type_node, from_len,
    1778          100 :                                  build_zero_cst (TREE_TYPE (from_len)));
    1779          100 :           tmp = fold_build3_loc (input_location, COND_EXPR,
    1780              :                                  void_type_node, tmp, extcopy, stdcopy);
    1781          100 :           gfc_add_expr_to_block (&body, tmp);
    1782          100 :           tmp = gfc_finish_block (&body);
    1783              :         }
    1784              :       else
    1785              :         {
    1786          256 :           gfc_add_expr_to_block (&body, stdcopy);
    1787          256 :           tmp = gfc_finish_block (&body);
    1788              :         }
    1789          356 :       gfc_cleanup_loop (&loop);
    1790              :     }
    1791              :   else
    1792              :     {
    1793          402 :       gcc_assert (!is_from_desc);
    1794          402 :       vec_safe_push (args, from_data);
    1795          402 :       vec_safe_push (args, to_data);
    1796          402 :       stdcopy = build_call_vec (fcn_type, fcn, args);
    1797              : 
    1798              :       /* In initialization mode from_len is a constant zero.  */
    1799          402 :       if (unlimited && !integer_zerop (from_len))
    1800              :         {
    1801           60 :           vec_safe_push (args, from_len);
    1802           60 :           vec_safe_push (args, to_len);
    1803           60 :           extcopy = build_call_vec (fcn_type, unshare_expr (fcn), args);
    1804           60 :           tmp = fold_build2_loc (input_location, GT_EXPR,
    1805              :                                  logical_type_node, from_len,
    1806           60 :                                  build_zero_cst (TREE_TYPE (from_len)));
    1807           60 :           tmp = fold_build3_loc (input_location, COND_EXPR,
    1808              :                                  void_type_node, tmp, extcopy, stdcopy);
    1809              :         }
    1810              :       else
    1811              :         tmp = stdcopy;
    1812              :     }
    1813              : 
    1814              :   /* Only copy _def_init to to_data, when it is not a NULL-pointer.  */
    1815          758 :   if (from == NULL_TREE)
    1816              :     {
    1817            0 :       tree cond;
    1818            0 :       cond = fold_build2_loc (input_location, NE_EXPR,
    1819              :                               logical_type_node,
    1820              :                               from_data, null_pointer_node);
    1821            0 :       tmp = fold_build3_loc (input_location, COND_EXPR,
    1822              :                              void_type_node, cond,
    1823              :                              tmp, build_empty_stmt (input_location));
    1824              :     }
    1825              : 
    1826          758 :   return tmp;
    1827              : }
    1828              : 
    1829              : 
    1830              : static tree
    1831          106 : gfc_trans_class_array_init_assign (gfc_expr *rhs, gfc_expr *lhs, gfc_expr *obj)
    1832              : {
    1833          106 :   gfc_actual_arglist *actual;
    1834          106 :   gfc_expr *ppc;
    1835          106 :   gfc_code *ppc_code;
    1836          106 :   tree res;
    1837              : 
    1838          106 :   actual = gfc_get_actual_arglist ();
    1839          106 :   actual->expr = gfc_copy_expr (rhs);
    1840          106 :   actual->next = gfc_get_actual_arglist ();
    1841          106 :   actual->next->expr = gfc_copy_expr (lhs);
    1842          106 :   ppc = gfc_copy_expr (obj);
    1843          106 :   gfc_add_vptr_component (ppc);
    1844          106 :   gfc_add_component_ref (ppc, "_copy");
    1845          106 :   ppc_code = gfc_get_code (EXEC_CALL);
    1846          106 :   ppc_code->resolved_sym = ppc->symtree->n.sym;
    1847              :   /* Although '_copy' is set to be elemental in class.cc, it is
    1848              :      not staying that way.  Find out why, sometime....  */
    1849          106 :   ppc_code->resolved_sym->attr.elemental = 1;
    1850          106 :   ppc_code->ext.actual = actual;
    1851          106 :   ppc_code->expr1 = ppc;
    1852              :   /* Since '_copy' is elemental, the scalarizer will take care
    1853              :      of arrays in gfc_trans_call.  */
    1854          106 :   res = gfc_trans_call (ppc_code, false, NULL, NULL, false);
    1855          106 :   gfc_free_statements (ppc_code);
    1856              : 
    1857          106 :   if (UNLIMITED_POLY(obj))
    1858              :     {
    1859              :       /* Check if rhs is non-NULL. */
    1860           24 :       gfc_se src;
    1861           24 :       gfc_init_se (&src, NULL);
    1862           24 :       gfc_conv_expr (&src, rhs);
    1863           24 :       src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
    1864           24 :       tree cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    1865           24 :                                    src.expr, fold_convert (TREE_TYPE (src.expr),
    1866              :                                                            null_pointer_node));
    1867           24 :       res = build3_loc (input_location, COND_EXPR, TREE_TYPE (res), cond, res,
    1868              :                         build_empty_stmt (input_location));
    1869              :     }
    1870              : 
    1871          106 :   return res;
    1872              : }
    1873              : 
    1874              : /* Special case for initializing a polymorphic dummy with INTENT(OUT).
    1875              :    A MEMCPY is needed to copy the full data from the default initializer
    1876              :    of the dynamic type.  */
    1877              : 
    1878              : tree
    1879          461 : gfc_trans_class_init_assign (gfc_code *code)
    1880              : {
    1881          461 :   stmtblock_t block;
    1882          461 :   tree tmp;
    1883          461 :   bool cmp_flag = true;
    1884          461 :   gfc_se dst,src,memsz;
    1885          461 :   gfc_expr *lhs, *rhs, *sz;
    1886          461 :   gfc_component *cmp;
    1887          461 :   gfc_symbol *sym;
    1888          461 :   gfc_ref *ref;
    1889              : 
    1890          461 :   gfc_start_block (&block);
    1891              : 
    1892          461 :   lhs = gfc_copy_expr (code->expr1);
    1893              : 
    1894          461 :   rhs = gfc_copy_expr (code->expr1);
    1895          461 :   gfc_add_vptr_component (rhs);
    1896              : 
    1897              :   /* Make sure that the component backend_decls have been built, which
    1898              :      will not have happened if the derived types concerned have not
    1899              :      been referenced.  */
    1900          461 :   gfc_get_derived_type (rhs->ts.u.derived);
    1901          461 :   gfc_add_def_init_component (rhs);
    1902              :   /* The _def_init is always scalar.  */
    1903          461 :   rhs->rank = 0;
    1904              : 
    1905              :   /* Check def_init for initializers.  If this is an INTENT(OUT) dummy with all
    1906              :      default initializer components NULL, use the passed value even though
    1907              :      F2018(8.5.10) asserts that it should considered to be undefined. This is
    1908              :      needed for consistency with other brands.  */
    1909          461 :   sym = code->expr1->expr_type == EXPR_VARIABLE ? code->expr1->symtree->n.sym
    1910              :                                                 : NULL;
    1911          461 :   if (code->op != EXEC_ALLOCATE
    1912          400 :       && sym && sym->attr.dummy
    1913          400 :       && sym->attr.intent == INTENT_OUT)
    1914              :     {
    1915          400 :       ref = rhs->ref;
    1916          800 :       while (ref && ref->next)
    1917              :         ref = ref->next;
    1918          400 :       cmp = ref->u.c.component->ts.u.derived->components;
    1919          611 :       for (; cmp; cmp = cmp->next)
    1920              :         {
    1921          428 :           if (cmp->initializer)
    1922              :             break;
    1923          211 :           else if (!cmp->next)
    1924          146 :             cmp_flag = false;
    1925              :         }
    1926              :     }
    1927              : 
    1928          461 :   if (code->expr1->ts.type == BT_CLASS
    1929          438 :       && CLASS_DATA (code->expr1)->attr.dimension)
    1930              :     {
    1931          106 :       gfc_array_spec *tmparr = gfc_get_array_spec ();
    1932          106 :       *tmparr = *CLASS_DATA (code->expr1)->as;
    1933              :       /* Adding the array ref to the class expression results in correct
    1934              :          indexing to the dynamic type.  */
    1935          106 :       gfc_add_full_array_ref (lhs, tmparr);
    1936          106 :       tmp = gfc_trans_class_array_init_assign (rhs, lhs, code->expr1);
    1937          106 :     }
    1938          355 :   else if (cmp_flag)
    1939              :     {
    1940              :       /* Scalar initialization needs the _data component.  */
    1941          222 :       gfc_add_data_component (lhs);
    1942          222 :       sz = gfc_copy_expr (code->expr1);
    1943          222 :       gfc_add_vptr_component (sz);
    1944          222 :       gfc_add_size_component (sz);
    1945              : 
    1946          222 :       gfc_init_se (&dst, NULL);
    1947          222 :       gfc_init_se (&src, NULL);
    1948          222 :       gfc_init_se (&memsz, NULL);
    1949          222 :       gfc_conv_expr (&dst, lhs);
    1950          222 :       gfc_conv_expr (&src, rhs);
    1951          222 :       gfc_conv_expr (&memsz, sz);
    1952          222 :       gfc_add_block_to_block (&block, &src.pre);
    1953          222 :       src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
    1954              : 
    1955          222 :       tmp = gfc_build_memcpy_call (dst.expr, src.expr, memsz.expr);
    1956              : 
    1957          222 :       if (UNLIMITED_POLY(code->expr1))
    1958              :         {
    1959              :           /* Check if _def_init is non-NULL. */
    1960            7 :           tree cond = fold_build2_loc (input_location, NE_EXPR,
    1961              :                                        logical_type_node, src.expr,
    1962            7 :                                        fold_convert (TREE_TYPE (src.expr),
    1963              :                                                      null_pointer_node));
    1964            7 :           tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
    1965              :                             tmp, build_empty_stmt (input_location));
    1966              :         }
    1967              :     }
    1968              :   else
    1969          133 :     tmp = build_empty_stmt (input_location);
    1970              : 
    1971          461 :   if (code->expr1->symtree->n.sym->attr.dummy
    1972          410 :       && (code->expr1->symtree->n.sym->attr.optional
    1973          404 :           || code->expr1->symtree->n.sym->ns->proc_name->attr.entry_master))
    1974              :     {
    1975            6 :       tree present = gfc_conv_expr_present (code->expr1->symtree->n.sym);
    1976            6 :       tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
    1977              :                         present, tmp,
    1978              :                         build_empty_stmt (input_location));
    1979              :     }
    1980              : 
    1981          461 :   gfc_add_expr_to_block (&block, tmp);
    1982          461 :   gfc_free_expr (lhs);
    1983          461 :   gfc_free_expr (rhs);
    1984              : 
    1985          461 :   return gfc_finish_block (&block);
    1986              : }
    1987              : 
    1988              : 
    1989              : /* Class valued elemental function calls or class array elements arriving
    1990              :    in gfc_trans_scalar_assign come here.  Wherever possible the vptr copy
    1991              :    is used to ensure that the rhs dynamic type is assigned to the lhs.  */
    1992              : 
    1993              : static bool
    1994          788 : trans_scalar_class_assign (stmtblock_t *block, gfc_se *lse, gfc_se *rse)
    1995              : {
    1996          788 :   tree fcn;
    1997          788 :   tree rse_expr;
    1998          788 :   tree class_data;
    1999          788 :   tree tmp;
    2000          788 :   tree zero;
    2001          788 :   tree cond;
    2002          788 :   tree final_cond;
    2003          788 :   stmtblock_t inner_block;
    2004          788 :   bool is_descriptor;
    2005          788 :   bool not_call_expr = TREE_CODE (rse->expr) != CALL_EXPR;
    2006          788 :   bool not_lhs_array_type;
    2007              : 
    2008              :   /* Temporaries arising from dependencies in assignment get cast as a
    2009              :      character type of the dynamic size of the rhs. Use the vptr copy
    2010              :      for this case.  */
    2011          788 :   tmp = TREE_TYPE (lse->expr);
    2012          788 :   not_lhs_array_type = !(tmp && TREE_CODE (tmp) == ARRAY_TYPE
    2013            0 :                          && TYPE_MAX_VALUE (TYPE_DOMAIN (tmp)) != NULL_TREE);
    2014              : 
    2015              :   /* Use ordinary assignment if the rhs is not a call expression or
    2016              :      the lhs is not a class entity or an array(ie. character) type.  */
    2017          740 :   if ((not_call_expr && gfc_get_class_from_expr (lse->expr) == NULL_TREE)
    2018         1061 :       && not_lhs_array_type)
    2019              :     return false;
    2020              : 
    2021              :   /* Ordinary assignment can be used if both sides are class expressions
    2022              :      since the dynamic type is preserved by copying the vptr.  This
    2023              :      should only occur, where temporaries are involved.  */
    2024          515 :   if (GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
    2025          515 :       && GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
    2026              :     return false;
    2027              : 
    2028              :   /* Fix the class expression and the class data of the rhs.  */
    2029          454 :   if (!GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr))
    2030          454 :       || not_call_expr)
    2031              :     {
    2032          454 :       tmp = gfc_get_class_from_expr (rse->expr);
    2033          454 :       if (tmp == NULL_TREE)
    2034              :         return false;
    2035          146 :       rse_expr = gfc_evaluate_now (tmp, block);
    2036              :     }
    2037              :   else
    2038            0 :     rse_expr = gfc_evaluate_now (rse->expr, block);
    2039              : 
    2040          146 :   class_data = gfc_class_data_get (rse_expr);
    2041              : 
    2042              :   /* Check that the rhs data is not null.  */
    2043          146 :   is_descriptor = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (class_data));
    2044          146 :   if (is_descriptor)
    2045          146 :     class_data = gfc_conv_descriptor_data_get (class_data);
    2046          146 :   class_data = gfc_evaluate_now (class_data, block);
    2047              : 
    2048          146 :   zero = build_int_cst (TREE_TYPE (class_data), 0);
    2049          146 :   cond = fold_build2_loc (input_location, NE_EXPR,
    2050              :                           logical_type_node,
    2051              :                           class_data, zero);
    2052              : 
    2053              :   /* Copy the rhs to the lhs.  */
    2054          146 :   fcn = gfc_vptr_copy_get (gfc_class_vptr_get (rse_expr));
    2055          146 :   fcn = build_fold_indirect_ref_loc (input_location, fcn);
    2056          146 :   tmp = gfc_evaluate_now (gfc_build_addr_expr (NULL, rse->expr), block);
    2057          146 :   tmp = is_descriptor ? tmp : class_data;
    2058          146 :   tmp = build_call_expr_loc (input_location, fcn, 2, tmp,
    2059              :                              gfc_build_addr_expr (NULL, lse->expr));
    2060          146 :   gfc_add_expr_to_block (block, tmp);
    2061              : 
    2062              :   /* Only elemental function results need to be finalised and freed.  */
    2063          146 :   if (not_call_expr)
    2064              :     return true;
    2065              : 
    2066              :   /* Finalize the class data if needed.  */
    2067            0 :   gfc_init_block (&inner_block);
    2068            0 :   fcn = gfc_vptr_final_get (gfc_class_vptr_get (rse_expr));
    2069            0 :   zero = build_int_cst (TREE_TYPE (fcn), 0);
    2070            0 :   final_cond = fold_build2_loc (input_location, NE_EXPR,
    2071              :                                 logical_type_node, fcn, zero);
    2072            0 :   fcn = build_fold_indirect_ref_loc (input_location, fcn);
    2073            0 :   tmp = build_call_expr_loc (input_location, fcn, 1, class_data);
    2074            0 :   tmp = build3_v (COND_EXPR, final_cond,
    2075              :                   tmp, build_empty_stmt (input_location));
    2076            0 :   gfc_add_expr_to_block (&inner_block, tmp);
    2077              : 
    2078              :   /* Free the class data.  */
    2079            0 :   tmp = gfc_call_free (class_data);
    2080            0 :   tmp = build3_v (COND_EXPR, cond, tmp,
    2081              :                   build_empty_stmt (input_location));
    2082            0 :   gfc_add_expr_to_block (&inner_block, tmp);
    2083              : 
    2084              :   /* Finish the inner block and subject it to the condition on the
    2085              :      class data being non-zero.  */
    2086            0 :   tmp = gfc_finish_block (&inner_block);
    2087            0 :   tmp = build3_v (COND_EXPR, cond, tmp,
    2088              :                   build_empty_stmt (input_location));
    2089            0 :   gfc_add_expr_to_block (block, tmp);
    2090              : 
    2091            0 :   return true;
    2092              : }
    2093              : 
    2094              : /* End of prototype trans-class.c  */
    2095              : 
    2096              : 
    2097              : static void
    2098        12645 : realloc_lhs_warning (bt type, bool array, locus *where)
    2099              : {
    2100        12645 :   if (array && type != BT_CLASS && type != BT_DERIVED && warn_realloc_lhs)
    2101           25 :     gfc_warning (OPT_Wrealloc_lhs,
    2102              :                  "Code for reallocating the allocatable array at %L will "
    2103              :                  "be added", where);
    2104        12620 :   else if (warn_realloc_lhs_all)
    2105            4 :     gfc_warning (OPT_Wrealloc_lhs_all,
    2106              :                  "Code for reallocating the allocatable variable at %L "
    2107              :                  "will be added", where);
    2108        12645 : }
    2109              : 
    2110              : 
    2111              : static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
    2112              :                                                  gfc_expr *);
    2113              : 
    2114              : /* Copy the scalarization loop variables.  */
    2115              : 
    2116              : static void
    2117      1271632 : gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
    2118              : {
    2119      1271632 :   dest->ss = src->ss;
    2120      1271632 :   dest->loop = src->loop;
    2121      1271632 : }
    2122              : 
    2123              : 
    2124              : /* Initialize a simple expression holder.
    2125              : 
    2126              :    Care must be taken when multiple se are created with the same parent.
    2127              :    The child se must be kept in sync.  The easiest way is to delay creation
    2128              :    of a child se until after the previous se has been translated.  */
    2129              : 
    2130              : void
    2131      4596936 : gfc_init_se (gfc_se * se, gfc_se * parent)
    2132              : {
    2133      4596936 :   memset (se, 0, sizeof (gfc_se));
    2134      4596936 :   gfc_init_block (&se->pre);
    2135      4596936 :   gfc_init_block (&se->finalblock);
    2136      4596936 :   gfc_init_block (&se->post);
    2137              : 
    2138      4596936 :   se->parent = parent;
    2139              : 
    2140      4596936 :   if (parent)
    2141      1271632 :     gfc_copy_se_loopvars (se, parent);
    2142      4596936 : }
    2143              : 
    2144              : 
    2145              : /* Advances to the next SS in the chain.  Use this rather than setting
    2146              :    se->ss = se->ss->next because all the parents needs to be kept in sync.
    2147              :    See gfc_init_se.  */
    2148              : 
    2149              : void
    2150       240906 : gfc_advance_se_ss_chain (gfc_se * se)
    2151              : {
    2152       240906 :   gfc_se *p;
    2153              : 
    2154       240906 :   gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
    2155              : 
    2156              :   p = se;
    2157              :   /* Walk down the parent chain.  */
    2158       632570 :   while (p != NULL)
    2159              :     {
    2160              :       /* Simple consistency check.  */
    2161       391664 :       gcc_assert (p->parent == NULL || p->parent->ss == p->ss
    2162              :                   || p->parent->ss->nested_ss == p->ss);
    2163              : 
    2164       391664 :       p->ss = p->ss->next;
    2165              : 
    2166       391664 :       p = p->parent;
    2167              :     }
    2168       240906 : }
    2169              : 
    2170              : 
    2171              : /* Ensures the result of the expression as either a temporary variable
    2172              :    or a constant so that it can be used repeatedly.  */
    2173              : 
    2174              : void
    2175         8046 : gfc_make_safe_expr (gfc_se * se)
    2176              : {
    2177         8046 :   tree var;
    2178              : 
    2179         8046 :   if (CONSTANT_CLASS_P (se->expr))
    2180              :     return;
    2181              : 
    2182              :   /* We need a temporary for this result.  */
    2183          208 :   var = gfc_create_var (TREE_TYPE (se->expr), NULL);
    2184          208 :   gfc_add_modify (&se->pre, var, se->expr);
    2185          208 :   se->expr = var;
    2186              : }
    2187              : 
    2188              : 
    2189              : /* Return an expression which determines if a dummy parameter is present.
    2190              :    Also used for arguments to procedures with multiple entry points.  */
    2191              : 
    2192              : tree
    2193        11592 : gfc_conv_expr_present (gfc_symbol * sym, bool use_saved_desc)
    2194              : {
    2195        11592 :   tree decl, orig_decl, cond;
    2196              : 
    2197        11592 :   gcc_assert (sym->attr.dummy);
    2198        11592 :   orig_decl = decl = gfc_get_symbol_decl (sym);
    2199              : 
    2200              :   /* Intrinsic scalars and derived types with VALUE attribute which are passed
    2201              :      by value use a hidden argument to denote the presence status.  */
    2202        11592 :   if (sym->attr.value && !sym->attr.dimension && sym->ts.type != BT_CLASS)
    2203              :     {
    2204         1052 :       char name[GFC_MAX_SYMBOL_LEN + 2];
    2205         1052 :       tree tree_name;
    2206              : 
    2207         1052 :       gcc_assert (TREE_CODE (decl) == PARM_DECL);
    2208         1052 :       name[0] = '.';
    2209         1052 :       strcpy (&name[1], sym->name);
    2210         1052 :       tree_name = get_identifier (name);
    2211              : 
    2212              :       /* Walk function argument list to find hidden arg.  */
    2213         1052 :       cond = DECL_ARGUMENTS (DECL_CONTEXT (decl));
    2214         5320 :       for ( ; cond != NULL_TREE; cond = TREE_CHAIN (cond))
    2215         5320 :         if (DECL_NAME (cond) == tree_name
    2216         5320 :             && DECL_ARTIFICIAL (cond))
    2217              :           break;
    2218              : 
    2219         1052 :       gcc_assert (cond);
    2220         1052 :       return cond;
    2221              :     }
    2222              : 
    2223              :   /* Assumed-shape arrays use a local variable for the array data;
    2224              :      the actual PARAM_DECL is in a saved decl.  As the local variable
    2225              :      is NULL, it can be checked instead, unless use_saved_desc is
    2226              :      requested.  */
    2227              : 
    2228        10540 :   if (use_saved_desc && TREE_CODE (decl) != PARM_DECL)
    2229              :     {
    2230          822 :       gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
    2231              :              || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
    2232          822 :       decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
    2233              :     }
    2234              : 
    2235        10540 :   cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, decl,
    2236        10540 :                           fold_convert (TREE_TYPE (decl), null_pointer_node));
    2237              : 
    2238              :   /* Fortran 2008 allows to pass null pointers and non-associated pointers
    2239              :      as actual argument to denote absent dummies. For array descriptors,
    2240              :      we thus also need to check the array descriptor.  For BT_CLASS, it
    2241              :      can also occur for scalars and F2003 due to type->class wrapping and
    2242              :      class->class wrapping.  Note further that BT_CLASS always uses an
    2243              :      array descriptor for arrays, also for explicit-shape/assumed-size.
    2244              :      For assumed-rank arrays, no local variable is generated, hence,
    2245              :      the following also applies with !use_saved_desc.  */
    2246              : 
    2247        10540 :   if ((use_saved_desc || TREE_CODE (orig_decl) == PARM_DECL)
    2248         7499 :       && !sym->attr.allocatable
    2249         6287 :       && ((sym->ts.type != BT_CLASS && !sym->attr.pointer)
    2250         2296 :           || (sym->ts.type == BT_CLASS
    2251         1041 :               && !CLASS_DATA (sym)->attr.allocatable
    2252          567 :               && !CLASS_DATA (sym)->attr.class_pointer))
    2253         4198 :       && ((gfc_option.allow_std & GFC_STD_F2008) != 0
    2254            6 :           || sym->ts.type == BT_CLASS))
    2255              :     {
    2256         4192 :       tree tmp;
    2257              : 
    2258         4192 :       if ((sym->as && (sym->as->type == AS_ASSUMED_SHAPE
    2259         1495 :                        || sym->as->type == AS_ASSUMED_RANK
    2260         1407 :                        || sym->attr.codimension))
    2261         3324 :           || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
    2262              :         {
    2263         1039 :           tmp = build_fold_indirect_ref_loc (input_location, decl);
    2264         1039 :           if (sym->ts.type == BT_CLASS)
    2265          171 :             tmp = gfc_class_data_get (tmp);
    2266         1039 :           tmp = gfc_conv_array_data (tmp);
    2267              :         }
    2268         3153 :       else if (sym->ts.type == BT_CLASS)
    2269           36 :         tmp = gfc_class_data_get (decl);
    2270              :       else
    2271              :         tmp = NULL_TREE;
    2272              : 
    2273         1075 :       if (tmp != NULL_TREE)
    2274              :         {
    2275         1075 :           tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
    2276         1075 :                                  fold_convert (TREE_TYPE (tmp), null_pointer_node));
    2277         1075 :           cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    2278              :                                   logical_type_node, cond, tmp);
    2279              :         }
    2280              :     }
    2281              : 
    2282              :   return cond;
    2283              : }
    2284              : 
    2285              : 
    2286              : /* Converts a missing, dummy argument into a null or zero.  */
    2287              : 
    2288              : void
    2289          844 : gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
    2290              : {
    2291          844 :   tree present;
    2292          844 :   tree tmp;
    2293              : 
    2294          844 :   present = gfc_conv_expr_present (arg->symtree->n.sym);
    2295              : 
    2296          844 :   if (kind > 0)
    2297              :     {
    2298              :       /* Create a temporary and convert it to the correct type.  */
    2299           54 :       tmp = gfc_get_int_type (kind);
    2300           54 :       tmp = fold_convert (tmp, build_fold_indirect_ref_loc (input_location,
    2301              :                                                         se->expr));
    2302              : 
    2303              :       /* Test for a NULL value.  */
    2304           54 :       tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
    2305           54 :                         tmp, fold_convert (TREE_TYPE (tmp), integer_one_node));
    2306           54 :       tmp = gfc_evaluate_now (tmp, &se->pre);
    2307           54 :       se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
    2308              :     }
    2309              :   else
    2310              :     {
    2311          790 :       tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
    2312              :                         present, se->expr,
    2313          790 :                         build_zero_cst (TREE_TYPE (se->expr)));
    2314          790 :       tmp = gfc_evaluate_now (tmp, &se->pre);
    2315          790 :       se->expr = tmp;
    2316              :     }
    2317              : 
    2318          844 :   if (ts.type == BT_CHARACTER)
    2319              :     {
    2320              :       /* Handle deferred-length dummies that pass the character length by
    2321              :          reference so that the value can be returned.  */
    2322          244 :       if (ts.deferred && INDIRECT_REF_P (se->string_length))
    2323              :         {
    2324           18 :           tmp = gfc_build_addr_expr (NULL_TREE, se->string_length);
    2325           18 :           tmp = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
    2326              :                                  present, tmp, null_pointer_node);
    2327           18 :           tmp = gfc_evaluate_now (tmp, &se->pre);
    2328           18 :           tmp = build_fold_indirect_ref_loc (input_location, tmp);
    2329              :         }
    2330              :       else
    2331              :         {
    2332          226 :           tmp = build_int_cst (gfc_charlen_type_node, 0);
    2333          226 :           tmp = fold_build3_loc (input_location, COND_EXPR,
    2334              :                                  gfc_charlen_type_node,
    2335              :                                  present, se->string_length, tmp);
    2336          226 :           tmp = gfc_evaluate_now (tmp, &se->pre);
    2337              :         }
    2338          244 :       se->string_length = tmp;
    2339              :     }
    2340          844 :   return;
    2341              : }
    2342              : 
    2343              : 
    2344              : /* Get the character length of an expression, looking through gfc_refs
    2345              :    if necessary.  */
    2346              : 
    2347              : tree
    2348        20117 : gfc_get_expr_charlen (gfc_expr *e)
    2349              : {
    2350        20117 :   gfc_ref *r;
    2351        20117 :   tree length;
    2352        20117 :   tree previous = NULL_TREE;
    2353        20117 :   gfc_se se;
    2354              : 
    2355        20117 :   gcc_assert (e->expr_type == EXPR_VARIABLE
    2356              :               && e->ts.type == BT_CHARACTER);
    2357              : 
    2358        20117 :   length = NULL; /* To silence compiler warning.  */
    2359              : 
    2360        20117 :   if (is_subref_array (e) && e->ts.u.cl->length)
    2361              :     {
    2362          767 :       gfc_se tmpse;
    2363          767 :       gfc_init_se (&tmpse, NULL);
    2364          767 :       gfc_conv_expr_type (&tmpse, e->ts.u.cl->length, gfc_charlen_type_node);
    2365          767 :       e->ts.u.cl->backend_decl = tmpse.expr;
    2366          767 :       return tmpse.expr;
    2367              :     }
    2368              : 
    2369              :   /* First candidate: if the variable is of type CHARACTER, the
    2370              :      expression's length could be the length of the character
    2371              :      variable.  */
    2372        19350 :   if (e->symtree->n.sym->ts.type == BT_CHARACTER)
    2373        19062 :     length = e->symtree->n.sym->ts.u.cl->backend_decl;
    2374              : 
    2375              :   /* Look through the reference chain for component references.  */
    2376        38831 :   for (r = e->ref; r; r = r->next)
    2377              :     {
    2378        19481 :       previous = length;
    2379        19481 :       switch (r->type)
    2380              :         {
    2381          288 :         case REF_COMPONENT:
    2382          288 :           if (r->u.c.component->ts.type == BT_CHARACTER)
    2383          288 :             length = r->u.c.component->ts.u.cl->backend_decl;
    2384              :           break;
    2385              : 
    2386              :         case REF_ARRAY:
    2387              :           /* Do nothing.  */
    2388              :           break;
    2389              : 
    2390           20 :         case REF_SUBSTRING:
    2391           20 :           gfc_init_se (&se, NULL);
    2392           20 :           gfc_conv_expr_type (&se, r->u.ss.start, gfc_charlen_type_node);
    2393           20 :           length = se.expr;
    2394           20 :           if (r->u.ss.end)
    2395            0 :             gfc_conv_expr_type (&se, r->u.ss.end, gfc_charlen_type_node);
    2396              :           else
    2397           20 :             se.expr = previous;
    2398           20 :           length = fold_build2_loc (input_location, MINUS_EXPR,
    2399              :                                     gfc_charlen_type_node,
    2400              :                                     se.expr, length);
    2401           20 :           length = fold_build2_loc (input_location, PLUS_EXPR,
    2402              :                                     gfc_charlen_type_node, length,
    2403              :                                     gfc_index_one_node);
    2404           20 :           break;
    2405              : 
    2406            0 :         default:
    2407            0 :           gcc_unreachable ();
    2408        19481 :           break;
    2409              :         }
    2410              :     }
    2411              : 
    2412        19350 :   gcc_assert (length != NULL);
    2413              :   return length;
    2414              : }
    2415              : 
    2416              : 
    2417              : /* Return for an expression the backend decl of the coarray.  */
    2418              : 
    2419              : tree
    2420         2046 : gfc_get_tree_for_caf_expr (gfc_expr *expr)
    2421              : {
    2422         2046 :   tree caf_decl;
    2423         2046 :   bool found = false;
    2424         2046 :   gfc_ref *ref;
    2425              : 
    2426         2046 :   gcc_assert (expr && expr->expr_type == EXPR_VARIABLE);
    2427              : 
    2428              :   /* Not-implemented diagnostic.  */
    2429         2046 :   if (expr->symtree->n.sym->ts.type == BT_CLASS
    2430           39 :       && UNLIMITED_POLY (expr->symtree->n.sym)
    2431            0 :       && CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
    2432            0 :     gfc_error ("Sorry, coindexed access to an unlimited polymorphic object at "
    2433              :                "%L is not supported", &expr->where);
    2434              : 
    2435         4323 :   for (ref = expr->ref; ref; ref = ref->next)
    2436         2277 :     if (ref->type == REF_COMPONENT)
    2437              :       {
    2438          195 :         if (ref->u.c.component->ts.type == BT_CLASS
    2439            0 :             && UNLIMITED_POLY (ref->u.c.component)
    2440            0 :             && CLASS_DATA (ref->u.c.component)->attr.codimension)
    2441            0 :           gfc_error ("Sorry, coindexed access to an unlimited polymorphic "
    2442              :                      "component at %L is not supported", &expr->where);
    2443              :       }
    2444              : 
    2445              :   /* Make sure the backend_decl is present before accessing it.  */
    2446         2046 :   caf_decl = expr->symtree->n.sym->backend_decl == NULL_TREE
    2447         2046 :       ? gfc_get_symbol_decl (expr->symtree->n.sym)
    2448              :       : expr->symtree->n.sym->backend_decl;
    2449              : 
    2450         2046 :   if (expr->symtree->n.sym->ts.type == BT_CLASS)
    2451              :     {
    2452           39 :       if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
    2453           45 :           && GFC_DECL_SAVED_DESCRIPTOR (caf_decl))
    2454            6 :         caf_decl = GFC_DECL_SAVED_DESCRIPTOR (caf_decl);
    2455              : 
    2456           39 :       if (expr->ref && expr->ref->type == REF_ARRAY)
    2457              :         {
    2458           28 :           caf_decl = gfc_class_data_get (caf_decl);
    2459           28 :           if (CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
    2460              :             return caf_decl;
    2461              :         }
    2462           11 :       else if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
    2463            2 :                && GFC_DECL_TOKEN (caf_decl)
    2464           13 :                && CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
    2465              :         return caf_decl;
    2466              : 
    2467           23 :       for (ref = expr->ref; ref; ref = ref->next)
    2468              :         {
    2469           18 :           if (ref->type == REF_COMPONENT
    2470            9 :               && strcmp (ref->u.c.component->name, "_data") != 0)
    2471              :             {
    2472            0 :               caf_decl = gfc_class_data_get (caf_decl);
    2473            0 :               if (CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
    2474              :                 return caf_decl;
    2475              :               break;
    2476              :             }
    2477           18 :           else if (ref->type == REF_ARRAY && ref->u.ar.dimen)
    2478              :             break;
    2479              :         }
    2480              :     }
    2481         2016 :   if (expr->symtree->n.sym->attr.codimension)
    2482              :     return caf_decl;
    2483              : 
    2484              :   /* The following code assumes that the coarray is a component reachable via
    2485              :      only scalar components/variables; the Fortran standard guarantees this.  */
    2486              : 
    2487           46 :   for (ref = expr->ref; ref; ref = ref->next)
    2488           46 :     if (ref->type == REF_COMPONENT)
    2489              :       {
    2490           46 :         gfc_component *comp = ref->u.c.component;
    2491              : 
    2492           46 :         if (POINTER_TYPE_P (TREE_TYPE (caf_decl)))
    2493            0 :           caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
    2494           46 :         caf_decl = fold_build3_loc (input_location, COMPONENT_REF,
    2495           46 :                                     TREE_TYPE (comp->backend_decl), caf_decl,
    2496              :                                     comp->backend_decl, NULL_TREE);
    2497           46 :         if (comp->ts.type == BT_CLASS)
    2498              :           {
    2499            0 :             caf_decl = gfc_class_data_get (caf_decl);
    2500            0 :             if (CLASS_DATA (comp)->attr.codimension)
    2501              :               {
    2502              :                 found = true;
    2503              :                 break;
    2504              :               }
    2505              :           }
    2506           46 :         if (comp->attr.codimension)
    2507              :           {
    2508              :             found = true;
    2509              :             break;
    2510              :           }
    2511              :       }
    2512           46 :   gcc_assert (found && caf_decl);
    2513              :   return caf_decl;
    2514              : }
    2515              : 
    2516              : 
    2517              : /* Obtain the Coarray token - and optionally also the offset.  */
    2518              : 
    2519              : void
    2520         1917 : gfc_get_caf_token_offset (gfc_se *se, tree *token, tree *offset, tree caf_decl,
    2521              :                           tree se_expr, gfc_expr *expr)
    2522              : {
    2523         1917 :   tree tmp;
    2524              : 
    2525         1917 :   gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
    2526              : 
    2527              :   /* Coarray token.  */
    2528         1917 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
    2529          546 :       *token = gfc_conv_descriptor_token (caf_decl);
    2530         1369 :   else if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
    2531         1570 :            && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
    2532            6 :     *token = GFC_DECL_TOKEN (caf_decl);
    2533              :   else
    2534              :     {
    2535         1365 :       gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (caf_decl))
    2536              :                   && GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl)) != NULL_TREE);
    2537         1365 :       *token = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl));
    2538              :     }
    2539              : 
    2540         1917 :   if (offset == NULL)
    2541              :     return;
    2542              : 
    2543              :   /* Offset between the coarray base address and the address wanted.  */
    2544          179 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl))
    2545          179 :       && (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_ALLOCATABLE
    2546            0 :           || GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_POINTER))
    2547            0 :     *offset = build_int_cst (gfc_array_index_type, 0);
    2548          179 :   else if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
    2549          179 :            && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
    2550            0 :     *offset = GFC_DECL_CAF_OFFSET (caf_decl);
    2551          179 :   else if (GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl)) != NULL_TREE)
    2552            0 :     *offset = GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl));
    2553              :   else
    2554          179 :     *offset = build_int_cst (gfc_array_index_type, 0);
    2555              : 
    2556          179 :   if (POINTER_TYPE_P (TREE_TYPE (se_expr))
    2557          179 :       && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se_expr))))
    2558              :     {
    2559            0 :       tmp = build_fold_indirect_ref_loc (input_location, se_expr);
    2560            0 :       tmp = gfc_conv_descriptor_data_get (tmp);
    2561              :     }
    2562          179 :   else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se_expr)))
    2563            0 :     tmp = gfc_conv_descriptor_data_get (se_expr);
    2564              :   else
    2565              :     {
    2566          179 :       gcc_assert (POINTER_TYPE_P (TREE_TYPE (se_expr)));
    2567              :       tmp = se_expr;
    2568              :     }
    2569              : 
    2570          179 :   *offset = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    2571              :                              *offset, fold_convert (gfc_array_index_type, tmp));
    2572              : 
    2573          179 :   if (expr->symtree->n.sym->ts.type == BT_DERIVED
    2574            0 :       && expr->symtree->n.sym->attr.codimension
    2575            0 :       && expr->symtree->n.sym->ts.u.derived->attr.alloc_comp)
    2576              :     {
    2577            0 :       gfc_expr *base_expr = gfc_copy_expr (expr);
    2578            0 :       gfc_ref *ref = base_expr->ref;
    2579            0 :       gfc_se base_se;
    2580              : 
    2581              :       // Iterate through the refs until the last one.
    2582            0 :       while (ref->next)
    2583              :           ref = ref->next;
    2584              : 
    2585            0 :       if (ref->type == REF_ARRAY
    2586            0 :           && ref->u.ar.type != AR_FULL)
    2587              :         {
    2588            0 :           const int ranksum = ref->u.ar.dimen + ref->u.ar.codimen;
    2589            0 :           int i;
    2590            0 :           for (i = 0; i < ranksum; ++i)
    2591              :             {
    2592            0 :               ref->u.ar.start[i] = NULL;
    2593            0 :               ref->u.ar.end[i] = NULL;
    2594              :             }
    2595            0 :           ref->u.ar.type = AR_FULL;
    2596              :         }
    2597            0 :       gfc_init_se (&base_se, NULL);
    2598            0 :       if (gfc_caf_attr (base_expr).dimension)
    2599              :         {
    2600            0 :           gfc_conv_expr_descriptor (&base_se, base_expr);
    2601            0 :           tmp = gfc_conv_descriptor_data_get (base_se.expr);
    2602              :         }
    2603              :       else
    2604              :         {
    2605            0 :           gfc_conv_expr (&base_se, base_expr);
    2606            0 :           tmp = base_se.expr;
    2607              :         }
    2608              : 
    2609            0 :       gfc_free_expr (base_expr);
    2610            0 :       gfc_add_block_to_block (&se->pre, &base_se.pre);
    2611            0 :       gfc_add_block_to_block (&se->post, &base_se.post);
    2612            0 :     }
    2613          179 :   else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
    2614            0 :     tmp = gfc_conv_descriptor_data_get (caf_decl);
    2615          179 :   else if (INDIRECT_REF_P (caf_decl))
    2616            0 :     tmp = TREE_OPERAND (caf_decl, 0);
    2617              :   else
    2618              :     {
    2619          179 :       gcc_assert (POINTER_TYPE_P (TREE_TYPE (caf_decl)));
    2620              :       tmp = caf_decl;
    2621              :     }
    2622              : 
    2623          179 :   *offset = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    2624              :                             fold_convert (gfc_array_index_type, *offset),
    2625              :                             fold_convert (gfc_array_index_type, tmp));
    2626              : }
    2627              : 
    2628              : 
    2629              : /* Convert the coindex of a coarray into an image index; the result is
    2630              :    image_num =  (idx(1)-lcobound(1)+1) + (idx(2)-lcobound(2))*extent(1)
    2631              :               + (idx(3)-lcobound(3))*extend(1)*extent(2) + ...  */
    2632              : 
    2633              : tree
    2634         1628 : gfc_caf_get_image_index (stmtblock_t *block, gfc_expr *e, tree desc)
    2635              : {
    2636         1628 :   gfc_ref *ref;
    2637         1628 :   tree lbound, ubound, extent, tmp, img_idx;
    2638         1628 :   gfc_se se;
    2639         1628 :   int i;
    2640              : 
    2641         1659 :   for (ref = e->ref; ref; ref = ref->next)
    2642         1659 :     if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
    2643              :       break;
    2644         1628 :   gcc_assert (ref != NULL);
    2645              : 
    2646         1628 :   if (ref->u.ar.dimen_type[ref->u.ar.dimen] == DIMEN_THIS_IMAGE)
    2647           95 :     return build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
    2648           95 :                                 null_pointer_node);
    2649              : 
    2650         1533 :   img_idx = build_zero_cst (gfc_array_index_type);
    2651         1533 :   extent = build_one_cst (gfc_array_index_type);
    2652         1533 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
    2653          626 :     for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
    2654              :       {
    2655          319 :         gfc_init_se (&se, NULL);
    2656          319 :         gfc_conv_expr_type (&se, ref->u.ar.start[i], gfc_array_index_type);
    2657          319 :         gfc_add_block_to_block (block, &se.pre);
    2658          319 :         lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
    2659          319 :         tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2660          319 :                                TREE_TYPE (lbound), se.expr, lbound);
    2661          319 :         tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
    2662              :                                extent, tmp);
    2663          319 :         img_idx = fold_build2_loc (input_location, PLUS_EXPR,
    2664          319 :                                    TREE_TYPE (tmp), img_idx, tmp);
    2665          319 :         if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
    2666              :           {
    2667           12 :             ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
    2668           12 :             tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
    2669           12 :             extent = fold_build2_loc (input_location, MULT_EXPR,
    2670           12 :                                       TREE_TYPE (tmp), extent, tmp);
    2671              :           }
    2672              :       }
    2673              :   else
    2674         2468 :     for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
    2675              :       {
    2676         1242 :         gfc_init_se (&se, NULL);
    2677         1242 :         gfc_conv_expr_type (&se, ref->u.ar.start[i], gfc_array_index_type);
    2678         1242 :         gfc_add_block_to_block (block, &se.pre);
    2679         1242 :         lbound = GFC_TYPE_ARRAY_LBOUND (TREE_TYPE (desc), i);
    2680         1242 :         tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2681         1242 :                                TREE_TYPE (lbound), se.expr, lbound);
    2682         1242 :         tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
    2683              :                                extent, tmp);
    2684         1242 :         img_idx = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
    2685              :                                    img_idx, tmp);
    2686         1242 :         if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
    2687              :           {
    2688           16 :             ubound = GFC_TYPE_ARRAY_UBOUND (TREE_TYPE (desc), i);
    2689           16 :             tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2690           16 :                                    TREE_TYPE (ubound), ubound, lbound);
    2691           16 :             tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
    2692           16 :                                    tmp, build_one_cst (TREE_TYPE (tmp)));
    2693           16 :             extent = fold_build2_loc (input_location, MULT_EXPR,
    2694           16 :                                       TREE_TYPE (tmp), extent, tmp);
    2695              :           }
    2696              :       }
    2697         1533 :   img_idx = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (img_idx),
    2698         1533 :                              img_idx, build_one_cst (TREE_TYPE (img_idx)));
    2699         1533 :   return fold_convert (integer_type_node, img_idx);
    2700              : }
    2701              : 
    2702              : 
    2703              : /* For each character array constructor subexpression without a ts.u.cl->length,
    2704              :    replace it by its first element (if there aren't any elements, the length
    2705              :    should already be set to zero).  */
    2706              : 
    2707              : static void
    2708          110 : flatten_array_ctors_without_strlen (gfc_expr* e)
    2709              : {
    2710          110 :   gfc_actual_arglist* arg;
    2711          110 :   gfc_constructor* c;
    2712              : 
    2713          110 :   if (!e)
    2714              :     return;
    2715              : 
    2716          110 :   switch (e->expr_type)
    2717              :     {
    2718              : 
    2719            0 :     case EXPR_OP:
    2720            0 :       flatten_array_ctors_without_strlen (e->value.op.op1);
    2721            0 :       flatten_array_ctors_without_strlen (e->value.op.op2);
    2722            0 :       break;
    2723              : 
    2724            0 :     case EXPR_COMPCALL:
    2725              :       /* TODO: Implement as with EXPR_FUNCTION when needed.  */
    2726            0 :       gcc_unreachable ();
    2727              : 
    2728           13 :     case EXPR_FUNCTION:
    2729           40 :       for (arg = e->value.function.actual; arg; arg = arg->next)
    2730           27 :         flatten_array_ctors_without_strlen (arg->expr);
    2731              :       break;
    2732              : 
    2733            0 :     case EXPR_ARRAY:
    2734              : 
    2735              :       /* We've found what we're looking for.  */
    2736            0 :       if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
    2737              :         {
    2738            0 :           gfc_constructor *c;
    2739            0 :           gfc_expr* new_expr;
    2740              : 
    2741            0 :           gcc_assert (e->value.constructor);
    2742              : 
    2743            0 :           c = gfc_constructor_first (e->value.constructor);
    2744            0 :           new_expr = c->expr;
    2745            0 :           c->expr = NULL;
    2746              : 
    2747            0 :           flatten_array_ctors_without_strlen (new_expr);
    2748            0 :           gfc_replace_expr (e, new_expr);
    2749            0 :           break;
    2750              :         }
    2751              : 
    2752              :       /* Otherwise, fall through to handle constructor elements.  */
    2753            0 :       gcc_fallthrough ();
    2754            0 :     case EXPR_STRUCTURE:
    2755            0 :       for (c = gfc_constructor_first (e->value.constructor);
    2756            0 :            c; c = gfc_constructor_next (c))
    2757            0 :         flatten_array_ctors_without_strlen (c->expr);
    2758              :       break;
    2759              : 
    2760              :     default:
    2761              :       break;
    2762              : 
    2763              :     }
    2764              : }
    2765              : 
    2766              : 
    2767              : /* Generate code to initialize a string length variable. Returns the
    2768              :    value.  For array constructors, cl->length might be NULL and in this case,
    2769              :    the first element of the constructor is needed.  expr is the original
    2770              :    expression so we can access it but can be NULL if this is not needed.  */
    2771              : 
    2772              : void
    2773         3843 : gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
    2774              : {
    2775         3843 :   gfc_se se;
    2776              : 
    2777         3843 :   gfc_init_se (&se, NULL);
    2778              : 
    2779         3843 :   if (!cl->length && cl->backend_decl && VAR_P (cl->backend_decl))
    2780         1361 :     return;
    2781              : 
    2782              :   /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
    2783              :      "flatten" array constructors by taking their first element; all elements
    2784              :      should be the same length or a cl->length should be present.  */
    2785         2575 :   if (!cl->length)
    2786              :     {
    2787          176 :       gfc_expr* expr_flat;
    2788          176 :       if (!expr)
    2789              :         return;
    2790           83 :       expr_flat = gfc_copy_expr (expr);
    2791           83 :       flatten_array_ctors_without_strlen (expr_flat);
    2792           83 :       gfc_resolve_expr (expr_flat);
    2793           83 :       if (expr_flat->rank)
    2794           13 :         gfc_conv_expr_descriptor (&se, expr_flat);
    2795              :       else
    2796           70 :         gfc_conv_expr (&se, expr_flat);
    2797           83 :       if (expr_flat->expr_type != EXPR_VARIABLE)
    2798           77 :         gfc_add_block_to_block (pblock, &se.pre);
    2799           83 :       se.expr = convert (gfc_charlen_type_node, se.string_length);
    2800           83 :       gfc_add_block_to_block (pblock, &se.post);
    2801           83 :       gfc_free_expr (expr_flat);
    2802              :     }
    2803              :   else
    2804              :     {
    2805              :       /* Convert cl->length.  */
    2806         2399 :       gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
    2807         2399 :       se.expr = fold_build2_loc (input_location, MAX_EXPR,
    2808              :                                  gfc_charlen_type_node, se.expr,
    2809         2399 :                                  build_zero_cst (TREE_TYPE (se.expr)));
    2810         2399 :       gfc_add_block_to_block (pblock, &se.pre);
    2811              :     }
    2812              : 
    2813         2482 :   if (cl->backend_decl && VAR_P (cl->backend_decl))
    2814         1564 :     gfc_add_modify (pblock, cl->backend_decl, se.expr);
    2815              :   else
    2816          918 :     cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
    2817              : }
    2818              : 
    2819              : 
    2820              : static void
    2821         6845 : gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
    2822              :                     const char *name, locus *where)
    2823              : {
    2824         6845 :   tree tmp;
    2825         6845 :   tree type;
    2826         6845 :   tree fault;
    2827         6845 :   gfc_se start;
    2828         6845 :   gfc_se end;
    2829         6845 :   char *msg;
    2830         6845 :   mpz_t length;
    2831              : 
    2832         6845 :   type = gfc_get_character_type (kind, ref->u.ss.length);
    2833         6845 :   type = build_pointer_type (type);
    2834              : 
    2835         6845 :   gfc_init_se (&start, se);
    2836         6845 :   gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
    2837         6845 :   gfc_add_block_to_block (&se->pre, &start.pre);
    2838              : 
    2839         6845 :   if (integer_onep (start.expr))
    2840         2319 :     gfc_conv_string_parameter (se);
    2841              :   else
    2842              :     {
    2843         4526 :       tmp = start.expr;
    2844         4526 :       STRIP_NOPS (tmp);
    2845              :       /* Avoid multiple evaluation of substring start.  */
    2846         4526 :       if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
    2847         1697 :         start.expr = gfc_evaluate_now (start.expr, &se->pre);
    2848              : 
    2849              :       /* Change the start of the string.  */
    2850         4526 :       if (((TREE_CODE (TREE_TYPE (se->expr)) == ARRAY_TYPE
    2851         1194 :             || TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE)
    2852         3452 :            && TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
    2853         5600 :           || (POINTER_TYPE_P (TREE_TYPE (se->expr))
    2854         1074 :               && TREE_CODE (TREE_TYPE (TREE_TYPE (se->expr))) != ARRAY_TYPE))
    2855              :         tmp = se->expr;
    2856              :       else
    2857         1066 :         tmp = build_fold_indirect_ref_loc (input_location,
    2858              :                                        se->expr);
    2859              :       /* For BIND(C), a BT_CHARACTER is not an ARRAY_TYPE.  */
    2860         4526 :       if (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE)
    2861              :         {
    2862         4398 :           tmp = gfc_build_array_ref (tmp, start.expr, NULL_TREE, true);
    2863         4398 :           se->expr = gfc_build_addr_expr (type, tmp);
    2864              :         }
    2865          128 :       else if (POINTER_TYPE_P (TREE_TYPE (tmp)))
    2866              :         {
    2867            8 :           tree diff;
    2868            8 :           diff = fold_build2 (MINUS_EXPR, gfc_charlen_type_node, start.expr,
    2869              :                               build_one_cst (gfc_charlen_type_node));
    2870            8 :           diff = fold_convert (size_type_node, diff);
    2871            8 :           se->expr
    2872            8 :             = fold_build2 (POINTER_PLUS_EXPR, TREE_TYPE (tmp), tmp, diff);
    2873              :         }
    2874              :     }
    2875              : 
    2876              :   /* Length = end + 1 - start.  */
    2877         6845 :   gfc_init_se (&end, se);
    2878         6845 :   if (ref->u.ss.end == NULL)
    2879          202 :     end.expr = se->string_length;
    2880              :   else
    2881              :     {
    2882         6643 :       gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
    2883         6643 :       gfc_add_block_to_block (&se->pre, &end.pre);
    2884              :     }
    2885         6845 :   tmp = end.expr;
    2886         6845 :   STRIP_NOPS (tmp);
    2887         6845 :   if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
    2888         2301 :     end.expr = gfc_evaluate_now (end.expr, &se->pre);
    2889              : 
    2890         6845 :   if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    2891          474 :       && !gfc_contains_implied_index_p (ref->u.ss.start)
    2892         7300 :       && !gfc_contains_implied_index_p (ref->u.ss.end))
    2893              :     {
    2894          455 :       tree nonempty = fold_build2_loc (input_location, LE_EXPR,
    2895              :                                        logical_type_node, start.expr,
    2896              :                                        end.expr);
    2897              : 
    2898              :       /* Check lower bound.  */
    2899          455 :       fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    2900              :                                start.expr,
    2901          455 :                                build_one_cst (TREE_TYPE (start.expr)));
    2902          455 :       fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    2903              :                                logical_type_node, nonempty, fault);
    2904          455 :       if (name)
    2905          454 :         msg = xasprintf ("Substring out of bounds: lower bound (%%ld) of '%s' "
    2906              :                          "is less than one", name);
    2907              :       else
    2908            1 :         msg = xasprintf ("Substring out of bounds: lower bound (%%ld) "
    2909              :                          "is less than one");
    2910          455 :       gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
    2911              :                                fold_convert (long_integer_type_node,
    2912              :                                              start.expr));
    2913          455 :       free (msg);
    2914              : 
    2915              :       /* Check upper bound.  */
    2916          455 :       fault = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    2917              :                                end.expr, se->string_length);
    2918          455 :       fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    2919              :                                logical_type_node, nonempty, fault);
    2920          455 :       if (name)
    2921          454 :         msg = xasprintf ("Substring out of bounds: upper bound (%%ld) of '%s' "
    2922              :                          "exceeds string length (%%ld)", name);
    2923              :       else
    2924            1 :         msg = xasprintf ("Substring out of bounds: upper bound (%%ld) "
    2925              :                          "exceeds string length (%%ld)");
    2926          455 :       gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
    2927              :                                fold_convert (long_integer_type_node, end.expr),
    2928              :                                fold_convert (long_integer_type_node,
    2929              :                                              se->string_length));
    2930          455 :       free (msg);
    2931              :     }
    2932              : 
    2933              :   /* Try to calculate the length from the start and end expressions.  */
    2934         6845 :   if (ref->u.ss.end
    2935         6845 :       && gfc_dep_difference (ref->u.ss.end, ref->u.ss.start, &length))
    2936              :     {
    2937         5626 :       HOST_WIDE_INT i_len;
    2938              : 
    2939         5626 :       i_len = gfc_mpz_get_hwi (length) + 1;
    2940         5626 :       if (i_len < 0)
    2941              :         i_len = 0;
    2942              : 
    2943         5626 :       tmp = build_int_cst (gfc_charlen_type_node, i_len);
    2944         5626 :       mpz_clear (length);  /* Was initialized by gfc_dep_difference.  */
    2945              :     }
    2946              :   else
    2947              :     {
    2948         1219 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
    2949              :                              fold_convert (gfc_charlen_type_node, end.expr),
    2950              :                              fold_convert (gfc_charlen_type_node, start.expr));
    2951         1219 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
    2952              :                              build_int_cst (gfc_charlen_type_node, 1), tmp);
    2953         1219 :       tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
    2954              :                              tmp, build_int_cst (gfc_charlen_type_node, 0));
    2955              :     }
    2956              : 
    2957         6845 :   se->string_length = tmp;
    2958         6845 : }
    2959              : 
    2960              : 
    2961              : /* Convert a derived type component reference.  */
    2962              : 
    2963              : void
    2964       174984 : gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
    2965              : {
    2966       174984 :   gfc_component *c;
    2967       174984 :   tree tmp;
    2968       174984 :   tree decl;
    2969       174984 :   tree field;
    2970       174984 :   tree context;
    2971              : 
    2972       174984 :   c = ref->u.c.component;
    2973              : 
    2974       174984 :   if (c->backend_decl == NULL_TREE
    2975            6 :       && ref->u.c.sym != NULL)
    2976            6 :     gfc_get_derived_type (ref->u.c.sym);
    2977              : 
    2978       174984 :   field = c->backend_decl;
    2979       174984 :   gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
    2980       174984 :   decl = se->expr;
    2981       174984 :   context = DECL_FIELD_CONTEXT (field);
    2982              : 
    2983              :   /* Components can correspond to fields of different containing
    2984              :      types, as components are created without context, whereas
    2985              :      a concrete use of a component has the type of decl as context.
    2986              :      So, if the type doesn't match, we search the corresponding
    2987              :      FIELD_DECL in the parent type.  To not waste too much time
    2988              :      we cache this result in norestrict_decl.
    2989              :      On the other hand, if the context is a UNION or a MAP (a
    2990              :      RECORD_TYPE within a UNION_TYPE) always use the given FIELD_DECL.  */
    2991              : 
    2992       174984 :   if (context != TREE_TYPE (decl)
    2993       174984 :       && !(   TREE_CODE (TREE_TYPE (field)) == UNION_TYPE /* Field is union */
    2994        12063 :            || TREE_CODE (context) == UNION_TYPE))         /* Field is map */
    2995              :     {
    2996        12063 :       tree f2 = c->norestrict_decl;
    2997        20459 :       if (!f2 || DECL_FIELD_CONTEXT (f2) != TREE_TYPE (decl))
    2998         7286 :         for (f2 = TYPE_FIELDS (TREE_TYPE (decl)); f2; f2 = DECL_CHAIN (f2))
    2999         7286 :           if (TREE_CODE (f2) == FIELD_DECL
    3000         7286 :               && DECL_NAME (f2) == DECL_NAME (field))
    3001              :             break;
    3002        12063 :       gcc_assert (f2);
    3003        12063 :       c->norestrict_decl = f2;
    3004        12063 :       field = f2;
    3005              :     }
    3006              : 
    3007       174984 :   if (ref->u.c.sym && ref->u.c.sym->ts.type == BT_CLASS
    3008            0 :       && strcmp ("_data", c->name) == 0)
    3009              :     {
    3010              :       /* Found a ref to the _data component.  Store the associated ref to
    3011              :          the vptr in se->class_vptr.  */
    3012            0 :       se->class_vptr = gfc_class_vptr_get (decl);
    3013              :     }
    3014              :   else
    3015       174984 :     se->class_vptr = NULL_TREE;
    3016              : 
    3017       174984 :   tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
    3018              :                          decl, field, NULL_TREE);
    3019              : 
    3020       174984 :   se->expr = tmp;
    3021              : 
    3022              :   /* Allocatable deferred char arrays are to be handled by the gfc_deferred_
    3023              :      strlen () conditional below.  */
    3024       174984 :   if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
    3025         8741 :       && !c->ts.deferred
    3026         5602 :       && !c->attr.pdt_string)
    3027              :     {
    3028         5428 :       tmp = c->ts.u.cl->backend_decl;
    3029              :       /* Components must always be constant length.  */
    3030         5428 :       gcc_assert (tmp && INTEGER_CST_P (tmp));
    3031         5428 :       se->string_length = tmp;
    3032              :     }
    3033              : 
    3034       174984 :   if (gfc_deferred_strlen (c, &field))
    3035              :     {
    3036         3313 :       tmp = fold_build3_loc (input_location, COMPONENT_REF,
    3037         3313 :                              TREE_TYPE (field),
    3038              :                              decl, field, NULL_TREE);
    3039         3313 :       se->string_length = tmp;
    3040              :     }
    3041              : 
    3042       174984 :   if (((c->attr.pointer || c->attr.allocatable)
    3043       102215 :        && (!c->attr.dimension && !c->attr.codimension)
    3044        55303 :        && c->ts.type != BT_CHARACTER)
    3045       121885 :       || c->attr.proc_pointer)
    3046        59379 :     se->expr = build_fold_indirect_ref_loc (input_location,
    3047              :                                         se->expr);
    3048       174984 : }
    3049              : 
    3050              : 
    3051              : /* This function deals with component references to components of the
    3052              :    parent type for derived type extensions.  */
    3053              : void
    3054        63483 : conv_parent_component_references (gfc_se * se, gfc_ref * ref)
    3055              : {
    3056        63483 :   gfc_component *c;
    3057        63483 :   gfc_component *cmp;
    3058        63483 :   gfc_symbol *dt;
    3059        63483 :   gfc_ref parent;
    3060              : 
    3061        63483 :   dt = ref->u.c.sym;
    3062        63483 :   c = ref->u.c.component;
    3063              : 
    3064              :   /* Return if the component is in this type, i.e. not in the parent type.  */
    3065       109336 :   for (cmp = dt->components; cmp; cmp = cmp->next)
    3066        99031 :     if (c == cmp)
    3067        53178 :       return;
    3068              : 
    3069              :   /* Build a gfc_ref to recursively call gfc_conv_component_ref.  */
    3070        10305 :   parent.type = REF_COMPONENT;
    3071        10305 :   parent.next = NULL;
    3072        10305 :   parent.u.c.sym = dt;
    3073        10305 :   parent.u.c.component = dt->components;
    3074              : 
    3075        10305 :   if (dt->backend_decl == NULL)
    3076            0 :     gfc_get_derived_type (dt);
    3077              : 
    3078              :   /* Build the reference and call self.  */
    3079        10305 :   gfc_conv_component_ref (se, &parent);
    3080        10305 :   parent.u.c.sym = dt->components->ts.u.derived;
    3081        10305 :   parent.u.c.component = c;
    3082        10305 :   conv_parent_component_references (se, &parent);
    3083              : }
    3084              : 
    3085              : 
    3086              : static void
    3087          537 : conv_inquiry (gfc_se * se, gfc_ref * ref, gfc_expr *expr, gfc_typespec *ts)
    3088              : {
    3089          537 :   tree res = se->expr;
    3090              : 
    3091          537 :   switch (ref->u.i)
    3092              :     {
    3093          259 :     case INQUIRY_RE:
    3094          518 :       res = fold_build1_loc (input_location, REALPART_EXPR,
    3095          259 :                              TREE_TYPE (TREE_TYPE (res)), res);
    3096          259 :       break;
    3097              : 
    3098          233 :     case INQUIRY_IM:
    3099          466 :       res = fold_build1_loc (input_location, IMAGPART_EXPR,
    3100          233 :                              TREE_TYPE (TREE_TYPE (res)), res);
    3101          233 :       break;
    3102              : 
    3103            7 :     case INQUIRY_KIND:
    3104            7 :       res = build_int_cst (gfc_typenode_for_spec (&expr->ts),
    3105            7 :                            ts->kind);
    3106            7 :       se->string_length = NULL_TREE;
    3107            7 :       break;
    3108              : 
    3109           38 :     case INQUIRY_LEN:
    3110           38 :       res = fold_convert (gfc_typenode_for_spec (&expr->ts),
    3111              :                           se->string_length);
    3112           38 :       se->string_length = NULL_TREE;
    3113           38 :       break;
    3114              : 
    3115            0 :     default:
    3116            0 :       gcc_unreachable ();
    3117              :     }
    3118          537 :   se->expr = res;
    3119          537 : }
    3120              : 
    3121              : /* Dereference VAR where needed if it is a pointer, reference, etc.
    3122              :    according to Fortran semantics.  */
    3123              : 
    3124              : tree
    3125      1439871 : gfc_maybe_dereference_var (gfc_symbol *sym, tree var, bool descriptor_only_p,
    3126              :                            bool is_classarray)
    3127              : {
    3128      1439871 :   if (!POINTER_TYPE_P (TREE_TYPE (var)))
    3129              :     return var;
    3130       290005 :   if (is_CFI_desc (sym, NULL))
    3131        11892 :     return build_fold_indirect_ref_loc (input_location, var);
    3132              : 
    3133              :   /* Characters are entirely different from other types, they are treated
    3134              :      separately.  */
    3135       278113 :   if (sym->ts.type == BT_CHARACTER)
    3136              :     {
    3137              :       /* Dereference character pointer dummy arguments
    3138              :          or results.  */
    3139        32527 :       if ((sym->attr.pointer || sym->attr.allocatable
    3140        18879 :            || (sym->as && sym->as->type == AS_ASSUMED_RANK))
    3141        13984 :           && (sym->attr.dummy
    3142        10686 :               || sym->attr.function
    3143        10312 :               || sym->attr.result))
    3144         4336 :         var = build_fold_indirect_ref_loc (input_location, var);
    3145              :     }
    3146       245586 :   else if (!sym->attr.value)
    3147              :     {
    3148              :       /* Dereference temporaries for class array dummy arguments.  */
    3149       169492 :       if (sym->attr.dummy && is_classarray
    3150       252397 :           && GFC_ARRAY_TYPE_P (TREE_TYPE (var)))
    3151              :         {
    3152         5313 :           if (!descriptor_only_p)
    3153         2704 :             var = GFC_DECL_SAVED_DESCRIPTOR (var);
    3154              : 
    3155         5313 :           var = build_fold_indirect_ref_loc (input_location, var);
    3156              :         }
    3157              : 
    3158              :       /* Dereference non-character scalar dummy arguments.  */
    3159       244782 :       if (sym->attr.dummy && !sym->attr.dimension
    3160       103505 :           && !(sym->attr.codimension && sym->attr.allocatable)
    3161       103439 :           && (sym->ts.type != BT_CLASS
    3162        19338 :               || (!CLASS_DATA (sym)->attr.dimension
    3163        11253 :                   && !(CLASS_DATA (sym)->attr.codimension
    3164          283 :                        && CLASS_DATA (sym)->attr.allocatable))))
    3165        95213 :         var = build_fold_indirect_ref_loc (input_location, var);
    3166              : 
    3167              :       /* Dereference scalar hidden result.  */
    3168       244782 :       if (flag_f2c && sym->ts.type == BT_COMPLEX
    3169          286 :           && (sym->attr.function || sym->attr.result)
    3170          108 :           && !sym->attr.dimension && !sym->attr.pointer
    3171           60 :           && !sym->attr.always_explicit)
    3172           36 :         var = build_fold_indirect_ref_loc (input_location, var);
    3173              : 
    3174              :       /* Dereference non-character, non-class pointer variables.
    3175              :          These must be dummies, results, or scalars.  */
    3176       244782 :       if (!is_classarray
    3177       236724 :           && (sym->attr.pointer || sym->attr.allocatable
    3178       188241 :               || gfc_is_associate_pointer (sym)
    3179       183600 :               || (sym->as && sym->as->type == AS_ASSUMED_RANK))
    3180       319806 :           && (sym->attr.dummy
    3181        35288 :               || sym->attr.function
    3182        34358 :               || sym->attr.result
    3183        33264 :               || (!sym->attr.dimension
    3184        33259 :                   && (!sym->attr.codimension || !sym->attr.allocatable))))
    3185        75019 :         var = build_fold_indirect_ref_loc (input_location, var);
    3186              :       /* Now treat the class array pointer variables accordingly.  */
    3187       169763 :       else if (sym->ts.type == BT_CLASS
    3188        19781 :                && sym->attr.dummy
    3189        19338 :                && (CLASS_DATA (sym)->attr.dimension
    3190        11253 :                    || CLASS_DATA (sym)->attr.codimension)
    3191         8368 :                && ((CLASS_DATA (sym)->as
    3192         8368 :                     && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
    3193         7371 :                    || CLASS_DATA (sym)->attr.allocatable
    3194         6046 :                    || CLASS_DATA (sym)->attr.class_pointer))
    3195         2913 :         var = build_fold_indirect_ref_loc (input_location, var);
    3196              :       /* And the case where a non-dummy, non-result, non-function,
    3197              :          non-allocable and non-pointer classarray is present.  This case was
    3198              :          previously covered by the first if, but with introducing the
    3199              :          condition !is_classarray there, that case has to be covered
    3200              :          explicitly.  */
    3201       166850 :       else if (sym->ts.type == BT_CLASS
    3202        16868 :                && !sym->attr.dummy
    3203          443 :                && !sym->attr.function
    3204          443 :                && !sym->attr.result
    3205          443 :                && (CLASS_DATA (sym)->attr.dimension
    3206            4 :                    || CLASS_DATA (sym)->attr.codimension)
    3207          443 :                && (sym->assoc
    3208            0 :                    || !CLASS_DATA (sym)->attr.allocatable)
    3209          443 :                && !CLASS_DATA (sym)->attr.class_pointer)
    3210          443 :         var = build_fold_indirect_ref_loc (input_location, var);
    3211              :     }
    3212              : 
    3213              :   return var;
    3214              : }
    3215              : 
    3216              : /* Return the contents of a variable. Also handles reference/pointer
    3217              :    variables (all Fortran pointer references are implicit).  */
    3218              : 
    3219              : static void
    3220      1592215 : gfc_conv_variable (gfc_se * se, gfc_expr * expr)
    3221              : {
    3222      1592215 :   gfc_ss *ss;
    3223      1592215 :   gfc_ref *ref;
    3224      1592215 :   gfc_symbol *sym;
    3225      1592215 :   tree parent_decl = NULL_TREE;
    3226      1592215 :   int parent_flag;
    3227      1592215 :   bool return_value;
    3228      1592215 :   bool alternate_entry;
    3229      1592215 :   bool entry_master;
    3230      1592215 :   bool is_classarray;
    3231      1592215 :   bool first_time = true;
    3232              : 
    3233      1592215 :   sym = expr->symtree->n.sym;
    3234      1592215 :   is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
    3235      1592215 :   ss = se->ss;
    3236      1592215 :   if (ss != NULL)
    3237              :     {
    3238       131914 :       gfc_ss_info *ss_info = ss->info;
    3239              : 
    3240              :       /* Check that something hasn't gone horribly wrong.  */
    3241       131914 :       gcc_assert (ss != gfc_ss_terminator);
    3242       131914 :       gcc_assert (ss_info->expr == expr);
    3243              : 
    3244              :       /* A scalarized term.  We already know the descriptor.  */
    3245       131914 :       se->expr = ss_info->data.array.descriptor;
    3246       131914 :       se->string_length = ss_info->string_length;
    3247       131914 :       ref = ss_info->data.array.ref;
    3248       131914 :       if (ref)
    3249       131560 :         gcc_assert (ref->type == REF_ARRAY
    3250              :                     && ref->u.ar.type != AR_ELEMENT);
    3251              :       else
    3252          354 :         gfc_conv_tmp_array_ref (se);
    3253              :     }
    3254              :   else
    3255              :     {
    3256      1460301 :       tree se_expr = NULL_TREE;
    3257              : 
    3258      1460301 :       se->expr = gfc_get_symbol_decl (sym);
    3259              : 
    3260              :       /* Deal with references to a parent results or entries by storing
    3261              :          the current_function_decl and moving to the parent_decl.  */
    3262      1460301 :       return_value = sym->attr.function && sym->result == sym;
    3263        18882 :       alternate_entry = sym->attr.function && sym->attr.entry
    3264      1461440 :                         && sym->result == sym;
    3265      2920602 :       entry_master = sym->attr.result
    3266        14274 :                      && sym->ns->proc_name->attr.entry_master
    3267      1460682 :                      && !gfc_return_by_reference (sym->ns->proc_name);
    3268      1460301 :       if (current_function_decl)
    3269      1440099 :         parent_decl = DECL_CONTEXT (current_function_decl);
    3270              : 
    3271      1460301 :       if ((se->expr == parent_decl && return_value)
    3272      1460190 :            || (sym->ns && sym->ns->proc_name
    3273      1455270 :                && parent_decl
    3274      1435068 :                && sym->ns->proc_name->backend_decl == parent_decl
    3275        37649 :                && (alternate_entry || entry_master)))
    3276              :         parent_flag = 1;
    3277              :       else
    3278      1460157 :         parent_flag = 0;
    3279              : 
    3280              :       /* Special case for assigning the return value of a function.
    3281              :          Self recursive functions must have an explicit return value.  */
    3282      1460301 :       if (return_value && (se->expr == current_function_decl || parent_flag))
    3283        10246 :         se_expr = gfc_get_fake_result_decl (sym, parent_flag);
    3284              : 
    3285              :       /* Similarly for alternate entry points.  */
    3286      1450055 :       else if (alternate_entry
    3287         1106 :                && (sym->ns->proc_name->backend_decl == current_function_decl
    3288            0 :                    || parent_flag))
    3289              :         {
    3290         1106 :           gfc_entry_list *el = NULL;
    3291              : 
    3292         1705 :           for (el = sym->ns->entries; el; el = el->next)
    3293         1705 :             if (sym == el->sym)
    3294              :               {
    3295         1106 :                 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
    3296         1106 :                 break;
    3297              :               }
    3298              :         }
    3299              : 
    3300      1448949 :       else if (entry_master
    3301          295 :                && (sym->ns->proc_name->backend_decl == current_function_decl
    3302            0 :                    || parent_flag))
    3303          295 :         se_expr = gfc_get_fake_result_decl (sym, parent_flag);
    3304              : 
    3305        11647 :       if (se_expr)
    3306        11647 :         se->expr = se_expr;
    3307              : 
    3308              :       /* Procedure actual arguments.  Look out for temporary variables
    3309              :          with the same attributes as function values.  */
    3310      1448654 :       else if (!sym->attr.temporary
    3311      1448586 :                && sym->attr.flavor == FL_PROCEDURE
    3312        22541 :                && se->expr != current_function_decl)
    3313              :         {
    3314        22474 :           if (!sym->attr.dummy && !sym->attr.proc_pointer)
    3315              :             {
    3316        20774 :               gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
    3317        20774 :               se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
    3318              :             }
    3319        22474 :           return;
    3320              :         }
    3321              : 
    3322      1437827 :       if (sym->ts.type == BT_CLASS
    3323        71861 :           && sym->attr.class_ok
    3324        71619 :           && sym->ts.u.derived->attr.is_class)
    3325              :         {
    3326        27895 :           if (is_classarray && DECL_LANG_SPECIFIC (se->expr)
    3327        79087 :               && GFC_DECL_SAVED_DESCRIPTOR (se->expr))
    3328         5455 :             se->class_container = GFC_DECL_SAVED_DESCRIPTOR (se->expr);
    3329              :           else
    3330        66164 :             se->class_container = se->expr;
    3331              :         }
    3332              : 
    3333              :       /* Dereference the expression, where needed.  */
    3334      1437827 :       if (se->class_container && CLASS_DATA (sym)->attr.codimension
    3335         2042 :           && !CLASS_DATA (sym)->attr.dimension)
    3336          877 :         se->expr
    3337          877 :           = gfc_maybe_dereference_var (sym, se->class_container,
    3338          877 :                                        se->descriptor_only, is_classarray);
    3339              :       else
    3340      1436950 :         se->expr
    3341      1436950 :           = gfc_maybe_dereference_var (sym, se->expr, se->descriptor_only,
    3342              :                                        is_classarray);
    3343              : 
    3344      1437827 :       ref = expr->ref;
    3345              :     }
    3346              : 
    3347              :   /* For character variables, also get the length.  */
    3348      1569741 :   if (sym->ts.type == BT_CHARACTER)
    3349              :     {
    3350              :       /* If the character length of an entry isn't set, get the length from
    3351              :          the master function instead.  */
    3352       164545 :       if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
    3353            0 :         se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
    3354              :       else
    3355       164545 :         se->string_length = sym->ts.u.cl->backend_decl;
    3356       164545 :       gcc_assert (se->string_length);
    3357              : 
    3358              :       /* For coarray strings return the pointer to the data and not the
    3359              :          descriptor.  */
    3360         5143 :       if (sym->attr.codimension && sym->attr.associate_var
    3361            6 :           && !se->descriptor_only
    3362       164551 :           && TREE_CODE (TREE_TYPE (se->expr)) != ARRAY_TYPE)
    3363            6 :         se->expr = gfc_conv_descriptor_data_get (se->expr);
    3364              :     }
    3365              : 
    3366              :   /* F202Y: Runtime warning that an assumed rank object is associated
    3367              :      with an assumed size object.  */
    3368      1569741 :   if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    3369        90227 :       && (gfc_option.allow_std & GFC_STD_F202Y)
    3370      1569975 :       && expr->rank == -1 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr)))
    3371              :     {
    3372           60 :       tree dim, lower, upper, cond;
    3373           60 :       char *msg;
    3374              : 
    3375           60 :       dim = fold_convert (signed_char_type_node,
    3376              :                           gfc_conv_descriptor_rank (se->expr));
    3377           60 :       dim = fold_build2_loc (input_location, MINUS_EXPR, signed_char_type_node,
    3378              :                              dim, build_int_cst (signed_char_type_node, 1));
    3379           60 :       lower = gfc_conv_descriptor_lbound_get (se->expr, dim);
    3380           60 :       upper = gfc_conv_descriptor_ubound_get (se->expr, dim);
    3381              : 
    3382           60 :       msg = xasprintf ("Assumed rank object %s is associated with an "
    3383              :                        "assumed size object", sym->name);
    3384           60 :       cond = fold_build2_loc (input_location, LT_EXPR,
    3385              :                               logical_type_node, upper, lower);
    3386           60 :       gfc_trans_runtime_check (false, true, cond, &se->pre,
    3387              :                                &gfc_current_locus, msg);
    3388           60 :       free (msg);
    3389              :     }
    3390              : 
    3391              :   /* Some expressions leak through that haven't been fixed up.  */
    3392      1569741 :   if (IS_INFERRED_TYPE (expr) && expr->ref)
    3393          404 :     gfc_fixup_inferred_type_refs (expr);
    3394              : 
    3395      1569741 :   gfc_typespec *ts = &sym->ts;
    3396      1999839 :   while (ref)
    3397              :     {
    3398       776538 :       switch (ref->type)
    3399              :         {
    3400       604911 :         case REF_ARRAY:
    3401              :           /* Return the descriptor if that's what we want and this is an array
    3402              :              section reference.  */
    3403       604911 :           if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
    3404              :             return;
    3405              : /* TODO: Pointers to single elements of array sections, eg elemental subs.  */
    3406              :           /* Return the descriptor for array pointers and allocations.  */
    3407       267746 :           if (se->want_pointer
    3408        23832 :               && ref->next == NULL && (se->descriptor_only))
    3409              :             return;
    3410              : 
    3411       258471 :           gfc_conv_array_ref (se, &ref->u.ar, expr, &expr->where);
    3412              :           /* Return a pointer to an element.  */
    3413       258471 :           break;
    3414              : 
    3415       164503 :         case REF_COMPONENT:
    3416       164503 :           ts = &ref->u.c.component->ts;
    3417       164503 :           if (first_time && IS_CLASS_ARRAY (sym) && sym->attr.dummy
    3418         5757 :               && se->descriptor_only && !CLASS_DATA (sym)->attr.allocatable
    3419         3076 :               && !CLASS_DATA (sym)->attr.class_pointer && CLASS_DATA (sym)->as
    3420         3076 :               && CLASS_DATA (sym)->as->type != AS_ASSUMED_RANK
    3421         2609 :               && strcmp ("_data", ref->u.c.component->name) == 0)
    3422              :             /* Skip the first ref of a _data component, because for class
    3423              :                arrays that one is already done by introducing a temporary
    3424              :                array descriptor.  */
    3425              :             break;
    3426              : 
    3427       161894 :           if (ref->u.c.sym->attr.extension)
    3428        53087 :             conv_parent_component_references (se, ref);
    3429              : 
    3430       161894 :           gfc_conv_component_ref (se, ref);
    3431              : 
    3432       161894 :           if (ref->u.c.component->ts.type == BT_CLASS
    3433        11801 :               && ref->u.c.component->attr.class_ok
    3434        11801 :               && ref->u.c.component->ts.u.derived->attr.is_class)
    3435        11801 :             se->class_container = se->expr;
    3436       150093 :           else if (!(ref->u.c.sym->attr.flavor == FL_DERIVED
    3437       147599 :                      && ref->u.c.sym->attr.is_class))
    3438        82809 :             se->class_container = NULL_TREE;
    3439              : 
    3440       161894 :           if (!ref->next && ref->u.c.sym->attr.codimension
    3441            0 :               && se->want_pointer && se->descriptor_only)
    3442              :             return;
    3443              : 
    3444              :           break;
    3445              : 
    3446         6587 :         case REF_SUBSTRING:
    3447         6587 :           gfc_conv_substring (se, ref, expr->ts.kind,
    3448         6587 :                               expr->symtree->name, &expr->where);
    3449         6587 :           break;
    3450              : 
    3451          537 :         case REF_INQUIRY:
    3452          537 :           conv_inquiry (se, ref, expr, ts);
    3453          537 :           break;
    3454              : 
    3455            0 :         default:
    3456            0 :           gcc_unreachable ();
    3457       430098 :           break;
    3458              :         }
    3459       430098 :       first_time = false;
    3460       430098 :       ref = ref->next;
    3461              :     }
    3462              :   /* Pointer assignment, allocation or pass by reference.  Arrays are handled
    3463              :      separately.  */
    3464      1223301 :   if (se->want_pointer)
    3465              :     {
    3466       132905 :       if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr))
    3467         7979 :         gfc_conv_string_parameter (se);
    3468              :       else
    3469       124926 :         se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
    3470              :     }
    3471              : }
    3472              : 
    3473              : 
    3474              : /* Unary ops are easy... Or they would be if ! was a valid op.  */
    3475              : 
    3476              : static void
    3477        28792 : gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
    3478              : {
    3479        28792 :   gfc_se operand;
    3480        28792 :   tree type;
    3481              : 
    3482        28792 :   gcc_assert (expr->ts.type != BT_CHARACTER);
    3483              :   /* Initialize the operand.  */
    3484        28792 :   gfc_init_se (&operand, se);
    3485        28792 :   gfc_conv_expr_val (&operand, expr->value.op.op1);
    3486        28792 :   gfc_add_block_to_block (&se->pre, &operand.pre);
    3487              : 
    3488        28792 :   type = gfc_typenode_for_spec (&expr->ts);
    3489              : 
    3490              :   /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
    3491              :      We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
    3492              :      All other unary operators have an equivalent GIMPLE unary operator.  */
    3493        28792 :   if (code == TRUTH_NOT_EXPR)
    3494        20189 :     se->expr = fold_build2_loc (input_location, EQ_EXPR, type, operand.expr,
    3495              :                                 build_int_cst (type, 0));
    3496              :   else
    3497         8603 :     se->expr = fold_build1_loc (input_location, code, type, operand.expr);
    3498              : 
    3499        28792 : }
    3500              : 
    3501              : /* Expand power operator to optimal multiplications when a value is raised
    3502              :    to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
    3503              :    Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
    3504              :    Programming", 3rd Edition, 1998.  */
    3505              : 
    3506              : /* This code is mostly duplicated from expand_powi in the backend.
    3507              :    We establish the "optimal power tree" lookup table with the defined size.
    3508              :    The items in the table are the exponents used to calculate the index
    3509              :    exponents. Any integer n less than the value can get an "addition chain",
    3510              :    with the first node being one.  */
    3511              : #define POWI_TABLE_SIZE 256
    3512              : 
    3513              : /* The table is from builtins.cc.  */
    3514              : static const unsigned char powi_table[POWI_TABLE_SIZE] =
    3515              :   {
    3516              :       0,   1,   1,   2,   2,   3,   3,   4,  /*   0 -   7 */
    3517              :       4,   6,   5,   6,   6,  10,   7,   9,  /*   8 -  15 */
    3518              :       8,  16,   9,  16,  10,  12,  11,  13,  /*  16 -  23 */
    3519              :      12,  17,  13,  18,  14,  24,  15,  26,  /*  24 -  31 */
    3520              :      16,  17,  17,  19,  18,  33,  19,  26,  /*  32 -  39 */
    3521              :      20,  25,  21,  40,  22,  27,  23,  44,  /*  40 -  47 */
    3522              :      24,  32,  25,  34,  26,  29,  27,  44,  /*  48 -  55 */
    3523              :      28,  31,  29,  34,  30,  60,  31,  36,  /*  56 -  63 */
    3524              :      32,  64,  33,  34,  34,  46,  35,  37,  /*  64 -  71 */
    3525              :      36,  65,  37,  50,  38,  48,  39,  69,  /*  72 -  79 */
    3526              :      40,  49,  41,  43,  42,  51,  43,  58,  /*  80 -  87 */
    3527              :      44,  64,  45,  47,  46,  59,  47,  76,  /*  88 -  95 */
    3528              :      48,  65,  49,  66,  50,  67,  51,  66,  /*  96 - 103 */
    3529              :      52,  70,  53,  74,  54, 104,  55,  74,  /* 104 - 111 */
    3530              :      56,  64,  57,  69,  58,  78,  59,  68,  /* 112 - 119 */
    3531              :      60,  61,  61,  80,  62,  75,  63,  68,  /* 120 - 127 */
    3532              :      64,  65,  65, 128,  66, 129,  67,  90,  /* 128 - 135 */
    3533              :      68,  73,  69, 131,  70,  94,  71,  88,  /* 136 - 143 */
    3534              :      72, 128,  73,  98,  74, 132,  75, 121,  /* 144 - 151 */
    3535              :      76, 102,  77, 124,  78, 132,  79, 106,  /* 152 - 159 */
    3536              :      80,  97,  81, 160,  82,  99,  83, 134,  /* 160 - 167 */
    3537              :      84,  86,  85,  95,  86, 160,  87, 100,  /* 168 - 175 */
    3538              :      88, 113,  89,  98,  90, 107,  91, 122,  /* 176 - 183 */
    3539              :      92, 111,  93, 102,  94, 126,  95, 150,  /* 184 - 191 */
    3540              :      96, 128,  97, 130,  98, 133,  99, 195,  /* 192 - 199 */
    3541              :     100, 128, 101, 123, 102, 164, 103, 138,  /* 200 - 207 */
    3542              :     104, 145, 105, 146, 106, 109, 107, 149,  /* 208 - 215 */
    3543              :     108, 200, 109, 146, 110, 170, 111, 157,  /* 216 - 223 */
    3544              :     112, 128, 113, 130, 114, 182, 115, 132,  /* 224 - 231 */
    3545              :     116, 200, 117, 132, 118, 158, 119, 206,  /* 232 - 239 */
    3546              :     120, 240, 121, 162, 122, 147, 123, 152,  /* 240 - 247 */
    3547              :     124, 166, 125, 214, 126, 138, 127, 153,  /* 248 - 255 */
    3548              :   };
    3549              : 
    3550              : /* If n is larger than lookup table's max index, we use the "window
    3551              :    method".  */
    3552              : #define POWI_WINDOW_SIZE 3
    3553              : 
    3554              : /* Recursive function to expand the power operator. The temporary
    3555              :    values are put in tmpvar. The function returns tmpvar[1] ** n.  */
    3556              : static tree
    3557       178323 : gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
    3558              : {
    3559       178323 :   tree op0;
    3560       178323 :   tree op1;
    3561       178323 :   tree tmp;
    3562       178323 :   int digit;
    3563              : 
    3564       178323 :   if (n < POWI_TABLE_SIZE)
    3565              :     {
    3566       137336 :       if (tmpvar[n])
    3567              :         return tmpvar[n];
    3568              : 
    3569        56612 :       op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
    3570        56612 :       op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
    3571              :     }
    3572        40987 :   else if (n & 1)
    3573              :     {
    3574        10015 :       digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
    3575        10015 :       op0 = gfc_conv_powi (se, n - digit, tmpvar);
    3576        10015 :       op1 = gfc_conv_powi (se, digit, tmpvar);
    3577              :     }
    3578              :   else
    3579              :     {
    3580        30972 :       op0 = gfc_conv_powi (se, n >> 1, tmpvar);
    3581        30972 :       op1 = op0;
    3582              :     }
    3583              : 
    3584        97599 :   tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
    3585        97599 :   tmp = gfc_evaluate_now (tmp, &se->pre);
    3586              : 
    3587        97599 :   if (n < POWI_TABLE_SIZE)
    3588        56612 :     tmpvar[n] = tmp;
    3589              : 
    3590              :   return tmp;
    3591              : }
    3592              : 
    3593              : 
    3594              : /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
    3595              :    return 1. Else return 0 and a call to runtime library functions
    3596              :    will have to be built.  */
    3597              : static int
    3598         3305 : gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
    3599              : {
    3600         3305 :   tree cond;
    3601         3305 :   tree tmp;
    3602         3305 :   tree type;
    3603         3305 :   tree vartmp[POWI_TABLE_SIZE];
    3604         3305 :   HOST_WIDE_INT m;
    3605         3305 :   unsigned HOST_WIDE_INT n;
    3606         3305 :   int sgn;
    3607         3305 :   wi::tree_to_wide_ref wrhs = wi::to_wide (rhs);
    3608              : 
    3609              :   /* If exponent is too large, we won't expand it anyway, so don't bother
    3610              :      with large integer values.  */
    3611         3305 :   if (!wi::fits_shwi_p (wrhs))
    3612              :     return 0;
    3613              : 
    3614         2945 :   m = wrhs.to_shwi ();
    3615              :   /* Use the wide_int's routine to reliably get the absolute value on all
    3616              :      platforms.  Then convert it to a HOST_WIDE_INT like above.  */
    3617         2945 :   n = wi::abs (wrhs).to_shwi ();
    3618              : 
    3619         2945 :   type = TREE_TYPE (lhs);
    3620         2945 :   sgn = tree_int_cst_sgn (rhs);
    3621              : 
    3622         2945 :   if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
    3623         5890 :        || optimize_size) && (m > 2 || m < -1))
    3624              :     return 0;
    3625              : 
    3626              :   /* rhs == 0  */
    3627         1639 :   if (sgn == 0)
    3628              :     {
    3629          282 :       se->expr = gfc_build_const (type, integer_one_node);
    3630          282 :       return 1;
    3631              :     }
    3632              : 
    3633              :   /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1.  */
    3634         1357 :   if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
    3635              :     {
    3636          220 :       tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    3637          220 :                              lhs, build_int_cst (TREE_TYPE (lhs), -1));
    3638          220 :       cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    3639          220 :                               lhs, build_int_cst (TREE_TYPE (lhs), 1));
    3640              : 
    3641              :       /* If rhs is even,
    3642              :          result = (lhs == 1 || lhs == -1) ? 1 : 0.  */
    3643          220 :       if ((n & 1) == 0)
    3644              :         {
    3645          104 :           tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    3646              :                                  logical_type_node, tmp, cond);
    3647          104 :           se->expr = fold_build3_loc (input_location, COND_EXPR, type,
    3648              :                                       tmp, build_int_cst (type, 1),
    3649              :                                       build_int_cst (type, 0));
    3650          104 :           return 1;
    3651              :         }
    3652              :       /* If rhs is odd,
    3653              :          result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0.  */
    3654          116 :       tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp,
    3655              :                              build_int_cst (type, -1),
    3656              :                              build_int_cst (type, 0));
    3657          116 :       se->expr = fold_build3_loc (input_location, COND_EXPR, type,
    3658              :                                   cond, build_int_cst (type, 1), tmp);
    3659          116 :       return 1;
    3660              :     }
    3661              : 
    3662         1137 :   memset (vartmp, 0, sizeof (vartmp));
    3663         1137 :   vartmp[1] = lhs;
    3664         1137 :   if (sgn == -1)
    3665              :     {
    3666          141 :       tmp = gfc_build_const (type, integer_one_node);
    3667          141 :       vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
    3668              :                                    vartmp[1]);
    3669              :     }
    3670              : 
    3671         1137 :   se->expr = gfc_conv_powi (se, n, vartmp);
    3672              : 
    3673         1137 :   return 1;
    3674              : }
    3675              : 
    3676              : /* Convert lhs**rhs, for constant rhs, when both are unsigned.
    3677              :    Method:
    3678              :    if (rhs == 0)      ! Checked here.
    3679              :      return 1;
    3680              :    if (lhs & 1 == 1)  ! odd_cnd
    3681              :      {
    3682              :        if (bit_size(rhs) < bit_size(lhs))  ! Checked here.
    3683              :          return lhs ** rhs;
    3684              : 
    3685              :        mask = 1 << (bit_size(a) - 1) / 2;
    3686              :        return lhs ** (n & rhs);
    3687              :      }
    3688              :    if (rhs > bit_size(lhs))  ! Checked here.
    3689              :      return 0;
    3690              : 
    3691              :    return lhs ** rhs;
    3692              : */
    3693              : 
    3694              : static int
    3695        15120 : gfc_conv_cst_uint_power (gfc_se * se, tree lhs, tree rhs)
    3696              : {
    3697        15120 :   tree type = TREE_TYPE (lhs);
    3698        15120 :   tree tmp, is_odd, odd_branch, even_branch;
    3699        15120 :   unsigned HOST_WIDE_INT lhs_prec, rhs_prec;
    3700        15120 :   wi::tree_to_wide_ref wrhs = wi::to_wide (rhs);
    3701        15120 :   unsigned HOST_WIDE_INT n, n_odd;
    3702        15120 :   tree vartmp_odd[POWI_TABLE_SIZE], vartmp_even[POWI_TABLE_SIZE];
    3703              : 
    3704              :   /* Anything ** 0 is one.  */
    3705        15120 :   if (integer_zerop (rhs))
    3706              :     {
    3707         1800 :       se->expr = build_int_cst (type, 1);
    3708         1800 :       return 1;
    3709              :     }
    3710              : 
    3711        13320 :   if (!wi::fits_uhwi_p (wrhs))
    3712              :     return 0;
    3713              : 
    3714        12960 :   n = wrhs.to_uhwi ();
    3715              : 
    3716              :   /* tmp = a & 1; . */
    3717        12960 :   tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type,
    3718              :                          lhs, build_int_cst (type, 1));
    3719        12960 :   is_odd = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    3720              :                             tmp, build_int_cst (type, 1));
    3721              : 
    3722        12960 :   lhs_prec = TYPE_PRECISION (type);
    3723        12960 :   rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs));
    3724              : 
    3725        12960 :   if (rhs_prec >= lhs_prec && lhs_prec <= HOST_BITS_PER_WIDE_INT)
    3726              :     {
    3727         7044 :       unsigned HOST_WIDE_INT mask = (HOST_WIDE_INT_1U << (lhs_prec - 1)) - 1;
    3728         7044 :       n_odd = n & mask;
    3729              :     }
    3730              :   else
    3731              :     n_odd = n;
    3732              : 
    3733        12960 :   memset (vartmp_odd, 0, sizeof (vartmp_odd));
    3734        12960 :   vartmp_odd[0] = build_int_cst (type, 1);
    3735        12960 :   vartmp_odd[1] = lhs;
    3736        12960 :   odd_branch = gfc_conv_powi (se, n_odd, vartmp_odd);
    3737        12960 :   even_branch = NULL_TREE;
    3738              : 
    3739        12960 :   if (n > lhs_prec)
    3740         4260 :     even_branch = build_int_cst (type, 0);
    3741              :   else
    3742              :     {
    3743         8700 :       if (n_odd != n)
    3744              :         {
    3745            0 :           memset (vartmp_even, 0, sizeof (vartmp_even));
    3746            0 :           vartmp_even[0] = build_int_cst (type, 1);
    3747            0 :           vartmp_even[1] = lhs;
    3748            0 :           even_branch = gfc_conv_powi (se, n, vartmp_even);
    3749              :         }
    3750              :     }
    3751         4260 :   if (even_branch != NULL_TREE)
    3752         4260 :     se->expr = fold_build3_loc (input_location, COND_EXPR, type, is_odd,
    3753              :                                 odd_branch, even_branch);
    3754              :   else
    3755         8700 :     se->expr = odd_branch;
    3756              : 
    3757              :   return 1;
    3758              : }
    3759              : 
    3760              : /* Power op (**).  Constant integer exponent and powers of 2 have special
    3761              :    handling.  */
    3762              : 
    3763              : static void
    3764        49129 : gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
    3765              : {
    3766        49129 :   tree gfc_int4_type_node;
    3767        49129 :   int kind;
    3768        49129 :   int ikind;
    3769        49129 :   int res_ikind_1, res_ikind_2;
    3770        49129 :   gfc_se lse;
    3771        49129 :   gfc_se rse;
    3772        49129 :   tree fndecl = NULL;
    3773              : 
    3774        49129 :   gfc_init_se (&lse, se);
    3775        49129 :   gfc_conv_expr_val (&lse, expr->value.op.op1);
    3776        49129 :   lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
    3777        49129 :   gfc_add_block_to_block (&se->pre, &lse.pre);
    3778              : 
    3779        49129 :   gfc_init_se (&rse, se);
    3780        49129 :   gfc_conv_expr_val (&rse, expr->value.op.op2);
    3781        49129 :   gfc_add_block_to_block (&se->pre, &rse.pre);
    3782              : 
    3783        49129 :   if (expr->value.op.op2->expr_type == EXPR_CONSTANT)
    3784              :     {
    3785        17563 :       if (expr->value.op.op2->ts.type == BT_INTEGER)
    3786              :         {
    3787         2292 :           if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
    3788        20418 :             return;
    3789              :         }
    3790        15271 :       else if (expr->value.op.op2->ts.type == BT_UNSIGNED)
    3791              :         {
    3792        15120 :           if (gfc_conv_cst_uint_power (se, lse.expr, rse.expr))
    3793              :             return;
    3794              :         }
    3795              :     }
    3796              : 
    3797        32730 :   if ((expr->value.op.op2->ts.type == BT_INTEGER
    3798        31468 :        || expr->value.op.op2->ts.type == BT_UNSIGNED)
    3799        31862 :       && expr->value.op.op2->expr_type == EXPR_CONSTANT)
    3800         1013 :     if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
    3801              :       return;
    3802              : 
    3803        32730 :   if (INTEGER_CST_P (lse.expr)
    3804        15371 :       && TREE_CODE (TREE_TYPE (rse.expr)) == INTEGER_TYPE
    3805        48101 :       && expr->value.op.op2->ts.type == BT_INTEGER)
    3806              :     {
    3807          251 :       wi::tree_to_wide_ref wlhs = wi::to_wide (lse.expr);
    3808          251 :       HOST_WIDE_INT v;
    3809          251 :       unsigned HOST_WIDE_INT w;
    3810          251 :       int kind, ikind, bit_size;
    3811              : 
    3812          251 :       v = wlhs.to_shwi ();
    3813          251 :       w = absu_hwi (v);
    3814              : 
    3815          251 :       kind = expr->value.op.op1->ts.kind;
    3816          251 :       ikind = gfc_validate_kind (BT_INTEGER, kind, false);
    3817          251 :       bit_size = gfc_integer_kinds[ikind].bit_size;
    3818              : 
    3819          251 :       if (v == 1)
    3820              :         {
    3821              :           /* 1**something is always 1.  */
    3822           35 :           se->expr = build_int_cst (TREE_TYPE (lse.expr), 1);
    3823          239 :           return;
    3824              :         }
    3825          216 :       else if (v == -1)
    3826              :         {
    3827              :           /* (-1)**n is 1 - ((n & 1) << 1) */
    3828           34 :           tree type;
    3829           34 :           tree tmp;
    3830              : 
    3831           34 :           type = TREE_TYPE (lse.expr);
    3832           34 :           tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type,
    3833              :                                  rse.expr, build_int_cst (type, 1));
    3834           34 :           tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
    3835              :                                  tmp, build_int_cst (type, 1));
    3836           34 :           tmp = fold_build2_loc (input_location, MINUS_EXPR, type,
    3837              :                                  build_int_cst (type, 1), tmp);
    3838           34 :           se->expr = tmp;
    3839           34 :           return;
    3840              :         }
    3841          182 :       else if (w > 0 && ((w & (w-1)) == 0) && ((w >> (bit_size-1)) == 0))
    3842              :         {
    3843              :           /* Here v is +/- 2**e.  The further simplification uses
    3844              :              2**n = 1<<n, 4**n = 1<<(n+n), 8**n = 1 <<(3*n), 16**n =
    3845              :              1<<(4*n), etc., but we have to make sure to return zero
    3846              :              if the number of bits is too large. */
    3847          170 :           tree lshift;
    3848          170 :           tree type;
    3849          170 :           tree shift;
    3850          170 :           tree ge;
    3851          170 :           tree cond;
    3852          170 :           tree num_bits;
    3853          170 :           tree cond2;
    3854          170 :           tree tmp1;
    3855              : 
    3856          170 :           type = TREE_TYPE (lse.expr);
    3857              : 
    3858          170 :           if (w == 2)
    3859          110 :             shift = rse.expr;
    3860           60 :           else if (w == 4)
    3861           12 :             shift = fold_build2_loc (input_location, PLUS_EXPR,
    3862           12 :                                      TREE_TYPE (rse.expr),
    3863              :                                        rse.expr, rse.expr);
    3864              :           else
    3865              :             {
    3866              :               /* use popcount for fast log2(w) */
    3867           48 :               int e = wi::popcount (w-1);
    3868           96 :               shift = fold_build2_loc (input_location, MULT_EXPR,
    3869           48 :                                        TREE_TYPE (rse.expr),
    3870           48 :                                        build_int_cst (TREE_TYPE (rse.expr), e),
    3871              :                                        rse.expr);
    3872              :             }
    3873              : 
    3874          170 :           lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type,
    3875              :                                     build_int_cst (type, 1), shift);
    3876          170 :           ge = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
    3877              :                                 rse.expr, build_int_cst (type, 0));
    3878          170 :           cond = fold_build3_loc (input_location, COND_EXPR, type, ge, lshift,
    3879              :                                  build_int_cst (type, 0));
    3880          170 :           num_bits = build_int_cst (TREE_TYPE (rse.expr), TYPE_PRECISION (type));
    3881          170 :           cond2 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
    3882              :                                    rse.expr, num_bits);
    3883          170 :           tmp1 = fold_build3_loc (input_location, COND_EXPR, type, cond2,
    3884              :                                   build_int_cst (type, 0), cond);
    3885          170 :           if (v > 0)
    3886              :             {
    3887          128 :               se->expr = tmp1;
    3888              :             }
    3889              :           else
    3890              :             {
    3891              :               /* for v < 0, calculate v**n = |v|**n * (-1)**n */
    3892           42 :               tree tmp2;
    3893           42 :               tmp2 = fold_build2_loc (input_location, BIT_AND_EXPR, type,
    3894              :                                       rse.expr, build_int_cst (type, 1));
    3895           42 :               tmp2 = fold_build2_loc (input_location, LSHIFT_EXPR, type,
    3896              :                                       tmp2, build_int_cst (type, 1));
    3897           42 :               tmp2 = fold_build2_loc (input_location, MINUS_EXPR, type,
    3898              :                                       build_int_cst (type, 1), tmp2);
    3899           42 :               se->expr = fold_build2_loc (input_location, MULT_EXPR, type,
    3900              :                                           tmp1, tmp2);
    3901              :             }
    3902          170 :           return;
    3903              :         }
    3904              :     }
    3905              :   /* Handle unsigned separate from signed above, things would be too
    3906              :      complicated otherwise.  */
    3907              : 
    3908        32491 :   if (INTEGER_CST_P (lse.expr) && expr->value.op.op1->ts.type == BT_UNSIGNED)
    3909              :     {
    3910        15120 :       gfc_expr * op1 = expr->value.op.op1;
    3911        15120 :       tree type;
    3912              : 
    3913        15120 :       type = TREE_TYPE (lse.expr);
    3914              : 
    3915        15120 :       if (mpz_cmp_ui (op1->value.integer, 1) == 0)
    3916              :         {
    3917              :           /* 1**something is always 1.  */
    3918         1260 :           se->expr = build_int_cst (type, 1);
    3919         1260 :           return;
    3920              :         }
    3921              : 
    3922              :       /* Simplify 2u**x to a shift, with the value set to zero if it falls
    3923              :        outside the range.  */
    3924        26460 :       if (mpz_popcount (op1->value.integer) == 1)
    3925              :         {
    3926         2520 :           tree prec_m1, lim, shift, lshift, cond, tmp;
    3927         2520 :           tree rtype = TREE_TYPE (rse.expr);
    3928         2520 :           int e = mpz_scan1 (op1->value.integer, 0);
    3929              : 
    3930         2520 :           shift = fold_build2_loc (input_location, MULT_EXPR,
    3931         2520 :                                    rtype, build_int_cst (rtype, e),
    3932              :                                    rse.expr);
    3933         2520 :           lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type,
    3934              :                                     build_int_cst (type, 1), shift);
    3935         5040 :           prec_m1 = fold_build2_loc (input_location, MINUS_EXPR, rtype,
    3936         2520 :                                      build_int_cst (rtype, TYPE_PRECISION (type)),
    3937              :                                      build_int_cst (rtype, 1));
    3938         2520 :           lim = fold_build2_loc (input_location, TRUNC_DIV_EXPR, rtype,
    3939         2520 :                                  prec_m1, build_int_cst (rtype, e));
    3940         2520 :           cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    3941              :                                   rse.expr, lim);
    3942         2520 :           tmp = fold_build3_loc (input_location, COND_EXPR, type, cond,
    3943              :                                  build_int_cst (type, 0), lshift);
    3944         2520 :           se->expr = tmp;
    3945         2520 :           return;
    3946              :         }
    3947              :     }
    3948              : 
    3949        28711 :   gfc_int4_type_node = gfc_get_int_type (4);
    3950              : 
    3951              :   /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
    3952              :      library routine.  But in the end, we have to convert the result back
    3953              :      if this case applies -- with res_ikind_K, we keep track whether operand K
    3954              :      falls into this case.  */
    3955        28711 :   res_ikind_1 = -1;
    3956        28711 :   res_ikind_2 = -1;
    3957              : 
    3958        28711 :   kind = expr->value.op.op1->ts.kind;
    3959        28711 :   switch (expr->value.op.op2->ts.type)
    3960              :     {
    3961         1023 :     case BT_INTEGER:
    3962         1023 :       ikind = expr->value.op.op2->ts.kind;
    3963         1023 :       switch (ikind)
    3964              :         {
    3965          144 :         case 1:
    3966          144 :         case 2:
    3967          144 :           rse.expr = convert (gfc_int4_type_node, rse.expr);
    3968          144 :           res_ikind_2 = ikind;
    3969              :           /* Fall through.  */
    3970              : 
    3971              :         case 4:
    3972              :           ikind = 0;
    3973              :           break;
    3974              : 
    3975              :         case 8:
    3976              :           ikind = 1;
    3977              :           break;
    3978              : 
    3979            6 :         case 16:
    3980            6 :           ikind = 2;
    3981            6 :           break;
    3982              : 
    3983            0 :         default:
    3984            0 :           gcc_unreachable ();
    3985              :         }
    3986         1023 :       switch (kind)
    3987              :         {
    3988            0 :         case 1:
    3989            0 :         case 2:
    3990            0 :           if (expr->value.op.op1->ts.type == BT_INTEGER)
    3991              :             {
    3992            0 :               lse.expr = convert (gfc_int4_type_node, lse.expr);
    3993            0 :               res_ikind_1 = kind;
    3994              :             }
    3995              :           else
    3996            0 :             gcc_unreachable ();
    3997              :           /* Fall through.  */
    3998              : 
    3999              :         case 4:
    4000              :           kind = 0;
    4001              :           break;
    4002              : 
    4003              :         case 8:
    4004              :           kind = 1;
    4005              :           break;
    4006              : 
    4007            6 :         case 10:
    4008            6 :           kind = 2;
    4009            6 :           break;
    4010              : 
    4011           18 :         case 16:
    4012           18 :           kind = 3;
    4013           18 :           break;
    4014              : 
    4015            0 :         default:
    4016            0 :           gcc_unreachable ();
    4017              :         }
    4018              : 
    4019         1023 :       switch (expr->value.op.op1->ts.type)
    4020              :         {
    4021          129 :         case BT_INTEGER:
    4022          129 :           if (kind == 3) /* Case 16 was not handled properly above.  */
    4023              :             kind = 2;
    4024          129 :           fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
    4025          129 :           break;
    4026              : 
    4027          662 :         case BT_REAL:
    4028              :           /* Use builtins for real ** int4.  */
    4029          662 :           if (ikind == 0)
    4030              :             {
    4031          565 :               switch (kind)
    4032              :                 {
    4033          392 :                 case 0:
    4034          392 :                   fndecl = builtin_decl_explicit (BUILT_IN_POWIF);
    4035          392 :                   break;
    4036              : 
    4037          155 :                 case 1:
    4038          155 :                   fndecl = builtin_decl_explicit (BUILT_IN_POWI);
    4039          155 :                   break;
    4040              : 
    4041            6 :                 case 2:
    4042            6 :                   fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
    4043            6 :                   break;
    4044              : 
    4045           12 :                 case 3:
    4046              :                   /* Use the __builtin_powil() only if real(kind=16) is
    4047              :                      actually the C long double type.  */
    4048           12 :                   if (!gfc_real16_is_float128)
    4049            0 :                     fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
    4050              :                   break;
    4051              : 
    4052              :                 default:
    4053              :                   gcc_unreachable ();
    4054              :                 }
    4055              :             }
    4056              : 
    4057              :           /* If we don't have a good builtin for this, go for the
    4058              :              library function.  */
    4059          553 :           if (!fndecl)
    4060          109 :             fndecl = gfor_fndecl_math_powi[kind][ikind].real;
    4061              :           break;
    4062              : 
    4063          232 :         case BT_COMPLEX:
    4064          232 :           fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
    4065          232 :           break;
    4066              : 
    4067            0 :         default:
    4068            0 :           gcc_unreachable ();
    4069              :         }
    4070              :       break;
    4071              : 
    4072          139 :     case BT_REAL:
    4073          139 :       fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
    4074          139 :       break;
    4075              : 
    4076          729 :     case BT_COMPLEX:
    4077          729 :       fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
    4078          729 :       break;
    4079              : 
    4080        26820 :     case BT_UNSIGNED:
    4081        26820 :       {
    4082              :         /* Valid kinds for unsigned are 1, 2, 4, 8, 16.  Instead of using a
    4083              :            large switch statement, let's just use __builtin_ctz.  */
    4084        26820 :         int base = __builtin_ctz (expr->value.op.op1->ts.kind);
    4085        26820 :         int expon = __builtin_ctz (expr->value.op.op2->ts.kind);
    4086        26820 :         fndecl = gfor_fndecl_unsigned_pow_list[base][expon];
    4087              :       }
    4088        26820 :       break;
    4089              : 
    4090            0 :     default:
    4091            0 :       gcc_unreachable ();
    4092        28711 :       break;
    4093              :     }
    4094              : 
    4095        28711 :   se->expr = build_call_expr_loc (input_location,
    4096              :                               fndecl, 2, lse.expr, rse.expr);
    4097              : 
    4098              :   /* Convert the result back if it is of wrong integer kind.  */
    4099        28711 :   if (res_ikind_1 != -1 && res_ikind_2 != -1)
    4100              :     {
    4101              :       /* We want the maximum of both operand kinds as result.  */
    4102            0 :       if (res_ikind_1 < res_ikind_2)
    4103            0 :         res_ikind_1 = res_ikind_2;
    4104            0 :       se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr);
    4105              :     }
    4106              : }
    4107              : 
    4108              : 
    4109              : /* Generate code to allocate a string temporary.  */
    4110              : 
    4111              : tree
    4112         4939 : gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
    4113              : {
    4114         4939 :   tree var;
    4115         4939 :   tree tmp;
    4116              : 
    4117         4939 :   if (gfc_can_put_var_on_stack (len))
    4118              :     {
    4119              :       /* Create a temporary variable to hold the result.  */
    4120         4632 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
    4121         2316 :                              TREE_TYPE (len), len,
    4122         2316 :                              build_int_cst (TREE_TYPE (len), 1));
    4123         2316 :       tmp = build_range_type (gfc_charlen_type_node, size_zero_node, tmp);
    4124              : 
    4125         2316 :       if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
    4126         2286 :         tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
    4127              :       else
    4128           30 :         tmp = build_array_type (TREE_TYPE (type), tmp);
    4129              : 
    4130         2316 :       var = gfc_create_var (tmp, "str");
    4131         2316 :       var = gfc_build_addr_expr (type, var);
    4132              :     }
    4133              :   else
    4134              :     {
    4135              :       /* Allocate a temporary to hold the result.  */
    4136         2623 :       var = gfc_create_var (type, "pstr");
    4137         2623 :       gcc_assert (POINTER_TYPE_P (type));
    4138         2623 :       tmp = TREE_TYPE (type);
    4139         2623 :       if (TREE_CODE (tmp) == ARRAY_TYPE)
    4140         2581 :         tmp = TREE_TYPE (tmp);
    4141         2623 :       tmp = TYPE_SIZE_UNIT (tmp);
    4142         2623 :       tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
    4143              :                             fold_convert (size_type_node, len),
    4144              :                             fold_convert (size_type_node, tmp));
    4145         2623 :       tmp = gfc_call_malloc (&se->pre, type, tmp);
    4146         2623 :       gfc_add_modify (&se->pre, var, tmp);
    4147              : 
    4148              :       /* Free the temporary afterwards.  */
    4149         2623 :       tmp = gfc_call_free (var);
    4150         2623 :       gfc_add_expr_to_block (&se->post, tmp);
    4151              :     }
    4152              : 
    4153         4939 :   return var;
    4154              : }
    4155              : 
    4156              : 
    4157              : /* Handle a string concatenation operation.  A temporary will be allocated to
    4158              :    hold the result.  */
    4159              : 
    4160              : static void
    4161         1282 : gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
    4162              : {
    4163         1282 :   gfc_se lse, rse;
    4164         1282 :   tree len, type, var, tmp, fndecl;
    4165              : 
    4166         1282 :   gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
    4167              :               && expr->value.op.op2->ts.type == BT_CHARACTER);
    4168         1282 :   gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
    4169              : 
    4170         1282 :   gfc_init_se (&lse, se);
    4171         1282 :   gfc_conv_expr (&lse, expr->value.op.op1);
    4172         1282 :   gfc_conv_string_parameter (&lse);
    4173         1282 :   gfc_init_se (&rse, se);
    4174         1282 :   gfc_conv_expr (&rse, expr->value.op.op2);
    4175         1282 :   gfc_conv_string_parameter (&rse);
    4176              : 
    4177         1282 :   gfc_add_block_to_block (&se->pre, &lse.pre);
    4178         1282 :   gfc_add_block_to_block (&se->pre, &rse.pre);
    4179              : 
    4180         1282 :   type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
    4181         1282 :   len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
    4182         1282 :   if (len == NULL_TREE)
    4183              :     {
    4184         1063 :       len = fold_build2_loc (input_location, PLUS_EXPR,
    4185              :                              gfc_charlen_type_node,
    4186              :                              fold_convert (gfc_charlen_type_node,
    4187              :                                            lse.string_length),
    4188              :                              fold_convert (gfc_charlen_type_node,
    4189              :                                            rse.string_length));
    4190              :     }
    4191              : 
    4192         1282 :   type = build_pointer_type (type);
    4193              : 
    4194         1282 :   var = gfc_conv_string_tmp (se, type, len);
    4195              : 
    4196              :   /* Do the actual concatenation.  */
    4197         1282 :   if (expr->ts.kind == 1)
    4198         1191 :     fndecl = gfor_fndecl_concat_string;
    4199           91 :   else if (expr->ts.kind == 4)
    4200           91 :     fndecl = gfor_fndecl_concat_string_char4;
    4201              :   else
    4202            0 :     gcc_unreachable ();
    4203              : 
    4204         1282 :   tmp = build_call_expr_loc (input_location,
    4205              :                          fndecl, 6, len, var, lse.string_length, lse.expr,
    4206              :                          rse.string_length, rse.expr);
    4207         1282 :   gfc_add_expr_to_block (&se->pre, tmp);
    4208              : 
    4209              :   /* Add the cleanup for the operands.  */
    4210         1282 :   gfc_add_block_to_block (&se->pre, &rse.post);
    4211         1282 :   gfc_add_block_to_block (&se->pre, &lse.post);
    4212              : 
    4213         1282 :   se->expr = var;
    4214         1282 :   se->string_length = len;
    4215         1282 : }
    4216              : 
    4217              : /* Translates an op expression. Common (binary) cases are handled by this
    4218              :    function, others are passed on. Recursion is used in either case.
    4219              :    We use the fact that (op1.ts == op2.ts) (except for the power
    4220              :    operator **).
    4221              :    Operators need no special handling for scalarized expressions as long as
    4222              :    they call gfc_conv_simple_val to get their operands.
    4223              :    Character strings get special handling.  */
    4224              : 
    4225              : static void
    4226       503582 : gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
    4227              : {
    4228       503582 :   enum tree_code code;
    4229       503582 :   gfc_se lse;
    4230       503582 :   gfc_se rse;
    4231       503582 :   tree tmp, type;
    4232       503582 :   int lop;
    4233       503582 :   int checkstring;
    4234              : 
    4235       503582 :   checkstring = 0;
    4236       503582 :   lop = 0;
    4237       503582 :   switch (expr->value.op.op)
    4238              :     {
    4239        15501 :     case INTRINSIC_PARENTHESES:
    4240        15501 :       if ((expr->ts.type == BT_REAL || expr->ts.type == BT_COMPLEX)
    4241         3801 :           && flag_protect_parens)
    4242              :         {
    4243         3668 :           gfc_conv_unary_op (PAREN_EXPR, se, expr);
    4244         3668 :           gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
    4245        91042 :           return;
    4246              :         }
    4247              : 
    4248              :       /* Fallthrough.  */
    4249        11839 :     case INTRINSIC_UPLUS:
    4250        11839 :       gfc_conv_expr (se, expr->value.op.op1);
    4251        11839 :       return;
    4252              : 
    4253         4935 :     case INTRINSIC_UMINUS:
    4254         4935 :       gfc_conv_unary_op (NEGATE_EXPR, se, expr);
    4255         4935 :       return;
    4256              : 
    4257        20189 :     case INTRINSIC_NOT:
    4258        20189 :       gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
    4259        20189 :       return;
    4260              : 
    4261              :     case INTRINSIC_PLUS:
    4262              :       code = PLUS_EXPR;
    4263              :       break;
    4264              : 
    4265        28658 :     case INTRINSIC_MINUS:
    4266        28658 :       code = MINUS_EXPR;
    4267        28658 :       break;
    4268              : 
    4269        32154 :     case INTRINSIC_TIMES:
    4270        32154 :       code = MULT_EXPR;
    4271        32154 :       break;
    4272              : 
    4273         6801 :     case INTRINSIC_DIVIDE:
    4274              :       /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
    4275              :          an integer or unsigned, we must round towards zero, so we use a
    4276              :          TRUNC_DIV_EXPR.  */
    4277         6801 :       if (expr->ts.type == BT_INTEGER || expr->ts.type == BT_UNSIGNED)
    4278              :         code = TRUNC_DIV_EXPR;
    4279              :       else
    4280       412540 :         code = RDIV_EXPR;
    4281              :       break;
    4282              : 
    4283        49129 :     case INTRINSIC_POWER:
    4284        49129 :       gfc_conv_power_op (se, expr);
    4285        49129 :       return;
    4286              : 
    4287         1282 :     case INTRINSIC_CONCAT:
    4288         1282 :       gfc_conv_concat_op (se, expr);
    4289         1282 :       return;
    4290              : 
    4291         4786 :     case INTRINSIC_AND:
    4292         4786 :       code = flag_frontend_optimize ? TRUTH_ANDIF_EXPR : TRUTH_AND_EXPR;
    4293              :       lop = 1;
    4294              :       break;
    4295              : 
    4296        55834 :     case INTRINSIC_OR:
    4297        55834 :       code = flag_frontend_optimize ? TRUTH_ORIF_EXPR : TRUTH_OR_EXPR;
    4298              :       lop = 1;
    4299              :       break;
    4300              : 
    4301              :       /* EQV and NEQV only work on logicals, but since we represent them
    4302              :          as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE.  */
    4303        12609 :     case INTRINSIC_EQ:
    4304        12609 :     case INTRINSIC_EQ_OS:
    4305        12609 :     case INTRINSIC_EQV:
    4306        12609 :       code = EQ_EXPR;
    4307        12609 :       checkstring = 1;
    4308        12609 :       lop = 1;
    4309        12609 :       break;
    4310              : 
    4311       205406 :     case INTRINSIC_NE:
    4312       205406 :     case INTRINSIC_NE_OS:
    4313       205406 :     case INTRINSIC_NEQV:
    4314       205406 :       code = NE_EXPR;
    4315       205406 :       checkstring = 1;
    4316       205406 :       lop = 1;
    4317       205406 :       break;
    4318              : 
    4319        11877 :     case INTRINSIC_GT:
    4320        11877 :     case INTRINSIC_GT_OS:
    4321        11877 :       code = GT_EXPR;
    4322        11877 :       checkstring = 1;
    4323        11877 :       lop = 1;
    4324        11877 :       break;
    4325              : 
    4326         1661 :     case INTRINSIC_GE:
    4327         1661 :     case INTRINSIC_GE_OS:
    4328         1661 :       code = GE_EXPR;
    4329         1661 :       checkstring = 1;
    4330         1661 :       lop = 1;
    4331         1661 :       break;
    4332              : 
    4333         4331 :     case INTRINSIC_LT:
    4334         4331 :     case INTRINSIC_LT_OS:
    4335         4331 :       code = LT_EXPR;
    4336         4331 :       checkstring = 1;
    4337         4331 :       lop = 1;
    4338         4331 :       break;
    4339              : 
    4340         2596 :     case INTRINSIC_LE:
    4341         2596 :     case INTRINSIC_LE_OS:
    4342         2596 :       code = LE_EXPR;
    4343         2596 :       checkstring = 1;
    4344         2596 :       lop = 1;
    4345         2596 :       break;
    4346              : 
    4347            0 :     case INTRINSIC_USER:
    4348            0 :     case INTRINSIC_ASSIGN:
    4349              :       /* These should be converted into function calls by the frontend.  */
    4350            0 :       gcc_unreachable ();
    4351              : 
    4352            0 :     default:
    4353            0 :       fatal_error (input_location, "Unknown intrinsic op");
    4354       412540 :       return;
    4355              :     }
    4356              : 
    4357              :   /* The only exception to this is **, which is handled separately anyway.  */
    4358       412540 :   gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
    4359              : 
    4360       412540 :   if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
    4361       379130 :     checkstring = 0;
    4362              : 
    4363              :   /* lhs */
    4364       412540 :   gfc_init_se (&lse, se);
    4365       412540 :   gfc_conv_expr (&lse, expr->value.op.op1);
    4366       412540 :   gfc_add_block_to_block (&se->pre, &lse.pre);
    4367              : 
    4368              :   /* rhs */
    4369       412540 :   gfc_init_se (&rse, se);
    4370       412540 :   gfc_conv_expr (&rse, expr->value.op.op2);
    4371       412540 :   gfc_add_block_to_block (&se->pre, &rse.pre);
    4372              : 
    4373       412540 :   if (checkstring)
    4374              :     {
    4375        33410 :       gfc_conv_string_parameter (&lse);
    4376        33410 :       gfc_conv_string_parameter (&rse);
    4377              : 
    4378        66820 :       lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
    4379              :                                            rse.string_length, rse.expr,
    4380        33410 :                                            expr->value.op.op1->ts.kind,
    4381              :                                            code);
    4382        33410 :       rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
    4383        33410 :       gfc_add_block_to_block (&lse.post, &rse.post);
    4384              :     }
    4385              : 
    4386       412540 :   type = gfc_typenode_for_spec (&expr->ts);
    4387              : 
    4388       412540 :   if (lop)
    4389              :     {
    4390              :       // Inhibit overeager optimization of Cray pointer comparisons (PR106692).
    4391       299100 :       if (expr->value.op.op1->expr_type == EXPR_VARIABLE
    4392       168599 :           && expr->value.op.op1->ts.type == BT_INTEGER
    4393        72655 :           && expr->value.op.op1->symtree
    4394        72655 :           && expr->value.op.op1->symtree->n.sym->attr.cray_pointer)
    4395           12 :         TREE_THIS_VOLATILE (lse.expr) = 1;
    4396              : 
    4397       299100 :       if (expr->value.op.op2->expr_type == EXPR_VARIABLE
    4398        72092 :           && expr->value.op.op2->ts.type == BT_INTEGER
    4399        12797 :           && expr->value.op.op2->symtree
    4400        12797 :           && expr->value.op.op2->symtree->n.sym->attr.cray_pointer)
    4401           12 :         TREE_THIS_VOLATILE (rse.expr) = 1;
    4402              : 
    4403              :       /* The result of logical ops is always logical_type_node.  */
    4404       299100 :       tmp = fold_build2_loc (input_location, code, logical_type_node,
    4405              :                              lse.expr, rse.expr);
    4406       299100 :       se->expr = convert (type, tmp);
    4407              :     }
    4408              :   else
    4409       113440 :     se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
    4410              : 
    4411              :   /* Add the post blocks.  */
    4412       412540 :   gfc_add_block_to_block (&se->post, &rse.post);
    4413       412540 :   gfc_add_block_to_block (&se->post, &lse.post);
    4414              : }
    4415              : 
    4416              : static void
    4417          139 : gfc_conv_conditional_expr (gfc_se *se, gfc_expr *expr)
    4418              : {
    4419          139 :   gfc_se cond_se, true_se, false_se;
    4420          139 :   tree condition, true_val, false_val;
    4421          139 :   tree type;
    4422              : 
    4423          139 :   gfc_init_se (&cond_se, se);
    4424          139 :   gfc_init_se (&true_se, se);
    4425          139 :   gfc_init_se (&false_se, se);
    4426              : 
    4427          139 :   gfc_conv_expr (&cond_se, expr->value.conditional.condition);
    4428          139 :   gfc_add_block_to_block (&se->pre, &cond_se.pre);
    4429          139 :   condition = gfc_evaluate_now (cond_se.expr, &se->pre);
    4430              : 
    4431          139 :   true_se.want_pointer = se->want_pointer;
    4432          139 :   gfc_conv_expr (&true_se, expr->value.conditional.true_expr);
    4433          139 :   true_val = true_se.expr;
    4434          139 :   false_se.want_pointer = se->want_pointer;
    4435          139 :   gfc_conv_expr (&false_se, expr->value.conditional.false_expr);
    4436          139 :   false_val = false_se.expr;
    4437              : 
    4438          139 :   if (true_se.pre.head != NULL_TREE || false_se.pre.head != NULL_TREE)
    4439           24 :     gfc_add_expr_to_block (
    4440              :       &se->pre,
    4441              :       fold_build3_loc (input_location, COND_EXPR, void_type_node, condition,
    4442           24 :                        true_se.pre.head != NULL_TREE
    4443            6 :                          ? gfc_finish_block (&true_se.pre)
    4444           18 :                          : build_empty_stmt (input_location),
    4445           24 :                        false_se.pre.head != NULL_TREE
    4446           24 :                          ? gfc_finish_block (&false_se.pre)
    4447            0 :                          : build_empty_stmt (input_location)));
    4448              : 
    4449          139 :   if (true_se.post.head != NULL_TREE || false_se.post.head != NULL_TREE)
    4450            6 :     gfc_add_expr_to_block (
    4451              :       &se->post,
    4452              :       fold_build3_loc (input_location, COND_EXPR, void_type_node, condition,
    4453            6 :                        true_se.post.head != NULL_TREE
    4454            0 :                          ? gfc_finish_block (&true_se.post)
    4455            6 :                          : build_empty_stmt (input_location),
    4456            6 :                        false_se.post.head != NULL_TREE
    4457            6 :                          ? gfc_finish_block (&false_se.post)
    4458            0 :                          : build_empty_stmt (input_location)));
    4459              : 
    4460          139 :   type = gfc_typenode_for_spec (&expr->ts);
    4461          139 :   if (se->want_pointer)
    4462           18 :     type = build_pointer_type (type);
    4463              : 
    4464          139 :   se->expr = fold_build3_loc (input_location, COND_EXPR, type, condition,
    4465              :                               true_val, false_val);
    4466          139 :   if (expr->ts.type == BT_CHARACTER)
    4467           54 :     se->string_length
    4468           54 :       = fold_build3_loc (input_location, COND_EXPR, gfc_charlen_type_node,
    4469              :                          condition, true_se.string_length,
    4470              :                          false_se.string_length);
    4471          139 : }
    4472              : 
    4473              : /* If a string's length is one, we convert it to a single character.  */
    4474              : 
    4475              : tree
    4476       138536 : gfc_string_to_single_character (tree len, tree str, int kind)
    4477              : {
    4478              : 
    4479       138536 :   if (len == NULL
    4480       138536 :       || !tree_fits_uhwi_p (len)
    4481       254422 :       || !POINTER_TYPE_P (TREE_TYPE (str)))
    4482              :     return NULL_TREE;
    4483              : 
    4484       115834 :   if (TREE_INT_CST_LOW (len) == 1)
    4485              :     {
    4486        22345 :       str = fold_convert (gfc_get_pchar_type (kind), str);
    4487        22345 :       return build_fold_indirect_ref_loc (input_location, str);
    4488              :     }
    4489              : 
    4490        93489 :   if (kind == 1
    4491        76119 :       && TREE_CODE (str) == ADDR_EXPR
    4492        65480 :       && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
    4493        47037 :       && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
    4494        28657 :       && array_ref_low_bound (TREE_OPERAND (str, 0))
    4495        28657 :          == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
    4496        28657 :       && TREE_INT_CST_LOW (len) > 1
    4497       120390 :       && TREE_INT_CST_LOW (len)
    4498              :          == (unsigned HOST_WIDE_INT)
    4499        26901 :             TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
    4500              :     {
    4501        26901 :       tree ret = fold_convert (gfc_get_pchar_type (kind), str);
    4502        26901 :       ret = build_fold_indirect_ref_loc (input_location, ret);
    4503        26901 :       if (TREE_CODE (ret) == INTEGER_CST)
    4504              :         {
    4505        26901 :           tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
    4506        26901 :           int i, length = TREE_STRING_LENGTH (string_cst);
    4507        26901 :           const char *ptr = TREE_STRING_POINTER (string_cst);
    4508              : 
    4509        40054 :           for (i = 1; i < length; i++)
    4510        39381 :             if (ptr[i] != ' ')
    4511              :               return NULL_TREE;
    4512              : 
    4513              :           return ret;
    4514              :         }
    4515              :     }
    4516              : 
    4517              :   return NULL_TREE;
    4518              : }
    4519              : 
    4520              : 
    4521              : static void
    4522          172 : conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
    4523              : {
    4524          172 :   gcc_assert (expr);
    4525              : 
    4526              :   /* We used to modify the tree here. Now it is done earlier in
    4527              :      the front-end, so we only check it here to avoid regressions.  */
    4528          172 :   if (sym->backend_decl)
    4529              :     {
    4530           67 :       gcc_assert (TREE_CODE (TREE_TYPE (sym->backend_decl)) == INTEGER_TYPE);
    4531           67 :       gcc_assert (TYPE_UNSIGNED (TREE_TYPE (sym->backend_decl)) == 1);
    4532           67 :       gcc_assert (TYPE_PRECISION (TREE_TYPE (sym->backend_decl)) == CHAR_TYPE_SIZE);
    4533           67 :       gcc_assert (DECL_BY_REFERENCE (sym->backend_decl) == 0);
    4534              :     }
    4535              : 
    4536              :   /* If we have a constant character expression, make it into an
    4537              :       integer of type C char.  */
    4538          172 :   if ((*expr)->expr_type == EXPR_CONSTANT)
    4539              :     {
    4540          166 :       gfc_typespec ts;
    4541          166 :       gfc_clear_ts (&ts);
    4542              : 
    4543          332 :       gfc_expr *tmp = gfc_get_int_expr (gfc_default_character_kind, NULL,
    4544          166 :                                         (*expr)->value.character.string[0]);
    4545          166 :       gfc_replace_expr (*expr, tmp);
    4546              :     }
    4547            6 :   else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
    4548              :     {
    4549            6 :       if ((*expr)->ref == NULL)
    4550              :         {
    4551            6 :           se->expr = gfc_string_to_single_character
    4552            6 :             (integer_one_node,
    4553            6 :               gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
    4554              :                                   gfc_get_symbol_decl
    4555            6 :                                   ((*expr)->symtree->n.sym)),
    4556              :               (*expr)->ts.kind);
    4557              :         }
    4558              :       else
    4559              :         {
    4560            0 :           gfc_conv_variable (se, *expr);
    4561            0 :           se->expr = gfc_string_to_single_character
    4562            0 :             (integer_one_node,
    4563              :               gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
    4564              :                                   se->expr),
    4565            0 :               (*expr)->ts.kind);
    4566              :         }
    4567              :     }
    4568          172 : }
    4569              : 
    4570              : /* Helper function for gfc_build_compare_string.  Return LEN_TRIM value
    4571              :    if STR is a string literal, otherwise return -1.  */
    4572              : 
    4573              : static int
    4574        31438 : gfc_optimize_len_trim (tree len, tree str, int kind)
    4575              : {
    4576        31438 :   if (kind == 1
    4577        26396 :       && TREE_CODE (str) == ADDR_EXPR
    4578        23067 :       && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
    4579        14811 :       && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
    4580         9389 :       && array_ref_low_bound (TREE_OPERAND (str, 0))
    4581         9389 :          == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
    4582         9389 :       && tree_fits_uhwi_p (len)
    4583         9389 :       && tree_to_uhwi (len) >= 1
    4584        31438 :       && tree_to_uhwi (len)
    4585         9345 :          == (unsigned HOST_WIDE_INT)
    4586         9345 :             TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
    4587              :     {
    4588         9345 :       tree folded = fold_convert (gfc_get_pchar_type (kind), str);
    4589         9345 :       folded = build_fold_indirect_ref_loc (input_location, folded);
    4590         9345 :       if (TREE_CODE (folded) == INTEGER_CST)
    4591              :         {
    4592         9345 :           tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
    4593         9345 :           int length = TREE_STRING_LENGTH (string_cst);
    4594         9345 :           const char *ptr = TREE_STRING_POINTER (string_cst);
    4595              : 
    4596        14254 :           for (; length > 0; length--)
    4597        14254 :             if (ptr[length - 1] != ' ')
    4598              :               break;
    4599              : 
    4600              :           return length;
    4601              :         }
    4602              :     }
    4603              :   return -1;
    4604              : }
    4605              : 
    4606              : /* Helper to build a call to memcmp.  */
    4607              : 
    4608              : static tree
    4609        12703 : build_memcmp_call (tree s1, tree s2, tree n)
    4610              : {
    4611        12703 :   tree tmp;
    4612              : 
    4613        12703 :   if (!POINTER_TYPE_P (TREE_TYPE (s1)))
    4614            0 :     s1 = gfc_build_addr_expr (pvoid_type_node, s1);
    4615              :   else
    4616        12703 :     s1 = fold_convert (pvoid_type_node, s1);
    4617              : 
    4618        12703 :   if (!POINTER_TYPE_P (TREE_TYPE (s2)))
    4619            0 :     s2 = gfc_build_addr_expr (pvoid_type_node, s2);
    4620              :   else
    4621        12703 :     s2 = fold_convert (pvoid_type_node, s2);
    4622              : 
    4623        12703 :   n = fold_convert (size_type_node, n);
    4624              : 
    4625        12703 :   tmp = build_call_expr_loc (input_location,
    4626              :                              builtin_decl_explicit (BUILT_IN_MEMCMP),
    4627              :                              3, s1, s2, n);
    4628              : 
    4629        12703 :   return fold_convert (integer_type_node, tmp);
    4630              : }
    4631              : 
    4632              : /* Compare two strings. If they are all single characters, the result is the
    4633              :    subtraction of them. Otherwise, we build a library call.  */
    4634              : 
    4635              : tree
    4636        33509 : gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
    4637              :                           enum tree_code code)
    4638              : {
    4639        33509 :   tree sc1;
    4640        33509 :   tree sc2;
    4641        33509 :   tree fndecl;
    4642              : 
    4643        33509 :   gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
    4644        33509 :   gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
    4645              : 
    4646        33509 :   sc1 = gfc_string_to_single_character (len1, str1, kind);
    4647        33509 :   sc2 = gfc_string_to_single_character (len2, str2, kind);
    4648              : 
    4649        33509 :   if (sc1 != NULL_TREE && sc2 != NULL_TREE)
    4650              :     {
    4651              :       /* Deal with single character specially.  */
    4652         4803 :       sc1 = fold_convert (integer_type_node, sc1);
    4653         4803 :       sc2 = fold_convert (integer_type_node, sc2);
    4654         4803 :       return fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
    4655         4803 :                               sc1, sc2);
    4656              :     }
    4657              : 
    4658        28706 :   if ((code == EQ_EXPR || code == NE_EXPR)
    4659        28144 :       && optimize
    4660        23578 :       && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
    4661              :     {
    4662              :       /* If one string is a string literal with LEN_TRIM longer
    4663              :          than the length of the second string, the strings
    4664              :          compare unequal.  */
    4665        15719 :       int len = gfc_optimize_len_trim (len1, str1, kind);
    4666        15719 :       if (len > 0 && compare_tree_int (len2, len) < 0)
    4667            0 :         return integer_one_node;
    4668        15719 :       len = gfc_optimize_len_trim (len2, str2, kind);
    4669        15719 :       if (len > 0 && compare_tree_int (len1, len) < 0)
    4670            0 :         return integer_one_node;
    4671              :     }
    4672              : 
    4673              :   /* We can compare via memcpy if the strings are known to be equal
    4674              :      in length and they are
    4675              :      - kind=1
    4676              :      - kind=4 and the comparison is for (in)equality.  */
    4677              : 
    4678        19171 :   if (INTEGER_CST_P (len1) && INTEGER_CST_P (len2)
    4679        18833 :       && tree_int_cst_equal (len1, len2)
    4680        41469 :       && (kind == 1 || code == EQ_EXPR || code == NE_EXPR))
    4681              :     {
    4682        12703 :       tree tmp;
    4683        12703 :       tree chartype;
    4684              : 
    4685        12703 :       chartype = gfc_get_char_type (kind);
    4686        12703 :       tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE(len1),
    4687        12703 :                              fold_convert (TREE_TYPE(len1),
    4688              :                                            TYPE_SIZE_UNIT(chartype)),
    4689              :                              len1);
    4690        12703 :       return build_memcmp_call (str1, str2, tmp);
    4691              :     }
    4692              : 
    4693              :   /* Build a call for the comparison.  */
    4694        16003 :   if (kind == 1)
    4695        13160 :     fndecl = gfor_fndecl_compare_string;
    4696         2843 :   else if (kind == 4)
    4697         2843 :     fndecl = gfor_fndecl_compare_string_char4;
    4698              :   else
    4699            0 :     gcc_unreachable ();
    4700              : 
    4701        16003 :   return build_call_expr_loc (input_location, fndecl, 4,
    4702        16003 :                               len1, str1, len2, str2);
    4703              : }
    4704              : 
    4705              : 
    4706              : /* Return the backend_decl for a procedure pointer component.  */
    4707              : 
    4708              : static tree
    4709         1900 : get_proc_ptr_comp (gfc_expr *e)
    4710              : {
    4711         1900 :   gfc_se comp_se;
    4712         1900 :   gfc_expr *e2;
    4713         1900 :   expr_t old_type;
    4714              : 
    4715         1900 :   gfc_init_se (&comp_se, NULL);
    4716         1900 :   e2 = gfc_copy_expr (e);
    4717              :   /* We have to restore the expr type later so that gfc_free_expr frees
    4718              :      the exact same thing that was allocated.
    4719              :      TODO: This is ugly.  */
    4720         1900 :   old_type = e2->expr_type;
    4721         1900 :   e2->expr_type = EXPR_VARIABLE;
    4722         1900 :   gfc_conv_expr (&comp_se, e2);
    4723         1900 :   e2->expr_type = old_type;
    4724         1900 :   gfc_free_expr (e2);
    4725         1900 :   return build_fold_addr_expr_loc (input_location, comp_se.expr);
    4726              : }
    4727              : 
    4728              : 
    4729              : /* Convert a typebound function reference from a class object.  */
    4730              : static void
    4731           80 : conv_base_obj_fcn_val (gfc_se * se, tree base_object, gfc_expr * expr)
    4732              : {
    4733           80 :   gfc_ref *ref;
    4734           80 :   tree var;
    4735              : 
    4736           80 :   if (!VAR_P (base_object))
    4737              :     {
    4738            0 :       var = gfc_create_var (TREE_TYPE (base_object), NULL);
    4739            0 :       gfc_add_modify (&se->pre, var, base_object);
    4740              :     }
    4741           80 :   se->expr = gfc_class_vptr_get (base_object);
    4742           80 :   se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
    4743           80 :   ref = expr->ref;
    4744          308 :   while (ref && ref->next)
    4745              :     ref = ref->next;
    4746           80 :   gcc_assert (ref && ref->type == REF_COMPONENT);
    4747           80 :   if (ref->u.c.sym->attr.extension)
    4748            0 :     conv_parent_component_references (se, ref);
    4749           80 :   gfc_conv_component_ref (se, ref);
    4750           80 :   se->expr = build_fold_addr_expr_loc (input_location, se->expr);
    4751           80 : }
    4752              : 
    4753              : static tree
    4754       127159 : get_builtin_fn (gfc_symbol * sym)
    4755              : {
    4756       127159 :   if (!gfc_option.disable_omp_is_initial_device
    4757       127155 :       && flag_openmp && sym->attr.function && sym->ts.type == BT_LOGICAL
    4758          613 :       && !strcmp (sym->name, "omp_is_initial_device"))
    4759           23 :     return builtin_decl_explicit (BUILT_IN_OMP_IS_INITIAL_DEVICE);
    4760              : 
    4761       127136 :   if (!gfc_option.disable_omp_get_initial_device
    4762       127129 :       && flag_openmp && sym->attr.function && sym->ts.type == BT_INTEGER
    4763         4118 :       && !strcmp (sym->name, "omp_get_initial_device"))
    4764           29 :     return builtin_decl_explicit (BUILT_IN_OMP_GET_INITIAL_DEVICE);
    4765              : 
    4766       127107 :   if (!gfc_option.disable_omp_get_num_devices
    4767       127100 :       && flag_openmp && sym->attr.function && sym->ts.type == BT_INTEGER
    4768         4089 :       && !strcmp (sym->name, "omp_get_num_devices"))
    4769           80 :     return builtin_decl_explicit (BUILT_IN_OMP_GET_NUM_DEVICES);
    4770              : 
    4771       127027 :   if (!gfc_option.disable_acc_on_device
    4772       126847 :       && flag_openacc && sym->attr.function && sym->ts.type == BT_LOGICAL
    4773         1163 :       && !strcmp (sym->name, "acc_on_device_h"))
    4774          390 :     return builtin_decl_explicit (BUILT_IN_ACC_ON_DEVICE);
    4775              : 
    4776              :   return NULL_TREE;
    4777              : }
    4778              : 
    4779              : static tree
    4780          522 : update_builtin_function (tree fn_call, gfc_symbol *sym)
    4781              : {
    4782          522 :   tree fn = TREE_OPERAND (CALL_EXPR_FN (fn_call), 0);
    4783              : 
    4784          522 :   if (DECL_FUNCTION_CODE (fn) == BUILT_IN_OMP_IS_INITIAL_DEVICE)
    4785              :      /* In Fortran omp_is_initial_device returns logical(4)
    4786              :         but the builtin uses 'int'.  */
    4787           23 :     return fold_convert (TREE_TYPE (TREE_TYPE (sym->backend_decl)), fn_call);
    4788              : 
    4789          499 :   else if (DECL_FUNCTION_CODE (fn) == BUILT_IN_ACC_ON_DEVICE)
    4790              :     {
    4791              :       /* Likewise for the return type; additionally, the argument it a
    4792              :          call-by-value int, Fortran has a by-reference 'integer(4)'.  */
    4793          390 :       tree arg = build_fold_indirect_ref_loc (input_location,
    4794          390 :                                               CALL_EXPR_ARG (fn_call, 0));
    4795          390 :       CALL_EXPR_ARG (fn_call, 0) = fold_convert (integer_type_node, arg);
    4796          390 :       return fold_convert (TREE_TYPE (TREE_TYPE (sym->backend_decl)), fn_call);
    4797              :     }
    4798              :   return fn_call;
    4799              : }
    4800              : 
    4801              : static void
    4802       129863 : conv_function_val (gfc_se * se, bool *is_builtin, gfc_symbol * sym,
    4803              :                    gfc_expr * expr, gfc_actual_arglist *actual_args)
    4804              : {
    4805       129863 :   tree tmp;
    4806              : 
    4807       129863 :   if (gfc_is_proc_ptr_comp (expr))
    4808         1900 :     tmp = get_proc_ptr_comp (expr);
    4809       127963 :   else if (sym->attr.dummy)
    4810              :     {
    4811          804 :       tmp = gfc_get_symbol_decl (sym);
    4812          804 :       if (sym->attr.proc_pointer)
    4813           83 :         tmp = build_fold_indirect_ref_loc (input_location,
    4814              :                                        tmp);
    4815          804 :       gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
    4816              :               && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
    4817              :     }
    4818              :   else
    4819              :     {
    4820       127159 :       if (!sym->backend_decl)
    4821        31789 :         sym->backend_decl = gfc_get_extern_function_decl (sym, actual_args);
    4822              : 
    4823       127159 :       if ((tmp = get_builtin_fn (sym)) != NULL_TREE)
    4824          522 :         *is_builtin = true;
    4825              :       else
    4826              :         {
    4827       126637 :           TREE_USED (sym->backend_decl) = 1;
    4828       126637 :           tmp = sym->backend_decl;
    4829              :         }
    4830              : 
    4831       127159 :       if (sym->attr.cray_pointee)
    4832              :         {
    4833              :           /* TODO - make the cray pointee a pointer to a procedure,
    4834              :              assign the pointer to it and use it for the call.  This
    4835              :              will do for now!  */
    4836           19 :           tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
    4837           19 :                          gfc_get_symbol_decl (sym->cp_pointer));
    4838           19 :           tmp = gfc_evaluate_now (tmp, &se->pre);
    4839              :         }
    4840              : 
    4841       127159 :       if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
    4842              :         {
    4843       126537 :           gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
    4844       126537 :           tmp = gfc_build_addr_expr (NULL_TREE, tmp);
    4845              :         }
    4846              :     }
    4847       129863 :   se->expr = tmp;
    4848       129863 : }
    4849              : 
    4850              : 
    4851              : /* Initialize MAPPING.  */
    4852              : 
    4853              : void
    4854       129980 : gfc_init_interface_mapping (gfc_interface_mapping * mapping)
    4855              : {
    4856       129980 :   mapping->syms = NULL;
    4857       129980 :   mapping->charlens = NULL;
    4858       129980 : }
    4859              : 
    4860              : 
    4861              : /* Free all memory held by MAPPING (but not MAPPING itself).  */
    4862              : 
    4863              : void
    4864       129980 : gfc_free_interface_mapping (gfc_interface_mapping * mapping)
    4865              : {
    4866       129980 :   gfc_interface_sym_mapping *sym;
    4867       129980 :   gfc_interface_sym_mapping *nextsym;
    4868       129980 :   gfc_charlen *cl;
    4869       129980 :   gfc_charlen *nextcl;
    4870              : 
    4871       170526 :   for (sym = mapping->syms; sym; sym = nextsym)
    4872              :     {
    4873        40546 :       nextsym = sym->next;
    4874        40546 :       sym->new_sym->n.sym->formal = NULL;
    4875        40546 :       gfc_free_symbol (sym->new_sym->n.sym);
    4876        40546 :       gfc_free_expr (sym->expr);
    4877        40546 :       free (sym->new_sym);
    4878        40546 :       free (sym);
    4879              :     }
    4880       134620 :   for (cl = mapping->charlens; cl; cl = nextcl)
    4881              :     {
    4882         4640 :       nextcl = cl->next;
    4883         4640 :       gfc_free_expr (cl->length);
    4884         4640 :       free (cl);
    4885              :     }
    4886       129980 : }
    4887              : 
    4888              : 
    4889              : /* Return a copy of gfc_charlen CL.  Add the returned structure to
    4890              :    MAPPING so that it will be freed by gfc_free_interface_mapping.  */
    4891              : 
    4892              : static gfc_charlen *
    4893         4640 : gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
    4894              :                                    gfc_charlen * cl)
    4895              : {
    4896         4640 :   gfc_charlen *new_charlen;
    4897              : 
    4898         4640 :   new_charlen = gfc_get_charlen ();
    4899         4640 :   new_charlen->next = mapping->charlens;
    4900         4640 :   new_charlen->length = gfc_copy_expr (cl->length);
    4901              : 
    4902         4640 :   mapping->charlens = new_charlen;
    4903         4640 :   return new_charlen;
    4904              : }
    4905              : 
    4906              : 
    4907              : /* A subroutine of gfc_add_interface_mapping.  Return a descriptorless
    4908              :    array variable that can be used as the actual argument for dummy
    4909              :    argument SYM, except in the case of assumed rank dummies of
    4910              :    non-intrinsic functions where the descriptor must be passed. Add any
    4911              :    initialization code to BLOCK. PACKED is as for gfc_get_nodesc_array_type
    4912              :    and DATA points to the first element in the passed array.  */
    4913              : 
    4914              : static tree
    4915         8382 : gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
    4916              :                                  gfc_packed packed, tree data, tree len,
    4917              :                                  bool assumed_rank_formal)
    4918              : {
    4919         8382 :   tree type;
    4920         8382 :   tree var;
    4921              : 
    4922         8382 :   if (len != NULL_TREE && (TREE_CONSTANT (len) || VAR_P (len)))
    4923           58 :     type = gfc_get_character_type_len (sym->ts.kind, len);
    4924              :   else
    4925         8324 :     type = gfc_typenode_for_spec (&sym->ts);
    4926              : 
    4927         8382 :   if (assumed_rank_formal)
    4928           13 :     type = TREE_TYPE (data);
    4929              :   else
    4930         8369 :     type = gfc_get_nodesc_array_type (type, sym->as, packed,
    4931         8345 :                                     !sym->attr.target && !sym->attr.pointer
    4932        16714 :                                     && !sym->attr.proc_pointer);
    4933              : 
    4934         8382 :   var = gfc_create_var (type, "ifm");
    4935         8382 :   gfc_add_modify (block, var, fold_convert (type, data));
    4936              : 
    4937         8382 :   return var;
    4938              : }
    4939              : 
    4940              : 
    4941              : /* A subroutine of gfc_add_interface_mapping.  Set the stride, upper bounds
    4942              :    and offset of descriptorless array type TYPE given that it has the same
    4943              :    size as DESC.  Add any set-up code to BLOCK.  */
    4944              : 
    4945              : static void
    4946         8112 : gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
    4947              : {
    4948         8112 :   int n;
    4949         8112 :   tree dim;
    4950         8112 :   tree offset;
    4951         8112 :   tree tmp;
    4952              : 
    4953         8112 :   offset = gfc_index_zero_node;
    4954         9214 :   for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
    4955              :     {
    4956         1102 :       dim = gfc_rank_cst[n];
    4957         1102 :       GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
    4958         1102 :       if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
    4959              :         {
    4960            1 :           GFC_TYPE_ARRAY_LBOUND (type, n)
    4961            1 :                 = gfc_conv_descriptor_lbound_get (desc, dim);
    4962            1 :           GFC_TYPE_ARRAY_UBOUND (type, n)
    4963            2 :                 = gfc_conv_descriptor_ubound_get (desc, dim);
    4964              :         }
    4965         1101 :       else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
    4966              :         {
    4967         1075 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    4968              :                                  gfc_array_index_type,
    4969              :                                  gfc_conv_descriptor_ubound_get (desc, dim),
    4970              :                                  gfc_conv_descriptor_lbound_get (desc, dim));
    4971         3225 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    4972              :                                  gfc_array_index_type,
    4973         1075 :                                  GFC_TYPE_ARRAY_LBOUND (type, n), tmp);
    4974         1075 :           tmp = gfc_evaluate_now (tmp, block);
    4975         1075 :           GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
    4976              :         }
    4977         4408 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    4978         1102 :                              GFC_TYPE_ARRAY_LBOUND (type, n),
    4979         1102 :                              GFC_TYPE_ARRAY_STRIDE (type, n));
    4980         1102 :       offset = fold_build2_loc (input_location, MINUS_EXPR,
    4981              :                                 gfc_array_index_type, offset, tmp);
    4982              :     }
    4983         8112 :   offset = gfc_evaluate_now (offset, block);
    4984         8112 :   GFC_TYPE_ARRAY_OFFSET (type) = offset;
    4985         8112 : }
    4986              : 
    4987              : 
    4988              : /* Extend MAPPING so that it maps dummy argument SYM to the value stored
    4989              :    in SE.  The caller may still use se->expr and se->string_length after
    4990              :    calling this function.  */
    4991              : 
    4992              : void
    4993        40546 : gfc_add_interface_mapping (gfc_interface_mapping * mapping,
    4994              :                            gfc_symbol * sym, gfc_se * se,
    4995              :                            gfc_expr *expr)
    4996              : {
    4997        40546 :   gfc_interface_sym_mapping *sm;
    4998        40546 :   tree desc;
    4999        40546 :   tree tmp;
    5000        40546 :   tree value;
    5001        40546 :   gfc_symbol *new_sym;
    5002        40546 :   gfc_symtree *root;
    5003        40546 :   gfc_symtree *new_symtree;
    5004              : 
    5005              :   /* Create a new symbol to represent the actual argument.  */
    5006        40546 :   new_sym = gfc_new_symbol (sym->name, NULL);
    5007        40546 :   new_sym->ts = sym->ts;
    5008        40546 :   new_sym->as = gfc_copy_array_spec (sym->as);
    5009        40546 :   new_sym->attr.referenced = 1;
    5010        40546 :   new_sym->attr.dimension = sym->attr.dimension;
    5011        40546 :   new_sym->attr.contiguous = sym->attr.contiguous;
    5012        40546 :   new_sym->attr.codimension = sym->attr.codimension;
    5013        40546 :   new_sym->attr.pointer = sym->attr.pointer;
    5014        40546 :   new_sym->attr.allocatable = sym->attr.allocatable;
    5015        40546 :   new_sym->attr.flavor = sym->attr.flavor;
    5016        40546 :   new_sym->attr.function = sym->attr.function;
    5017        40546 :   new_sym->attr.dummy = 0;
    5018              : 
    5019              :   /* Ensure that the interface is available and that
    5020              :      descriptors are passed for array actual arguments.  */
    5021        40546 :   if (sym->attr.flavor == FL_PROCEDURE)
    5022              :     {
    5023           36 :       new_sym->formal = expr->symtree->n.sym->formal;
    5024           36 :       new_sym->attr.always_explicit
    5025           36 :             = expr->symtree->n.sym->attr.always_explicit;
    5026              :     }
    5027              : 
    5028              :   /* Create a fake symtree for it.  */
    5029        40546 :   root = NULL;
    5030        40546 :   new_symtree = gfc_new_symtree (&root, sym->name);
    5031        40546 :   new_symtree->n.sym = new_sym;
    5032        40546 :   gcc_assert (new_symtree == root);
    5033              : 
    5034              :   /* Create a dummy->actual mapping.  */
    5035        40546 :   sm = XCNEW (gfc_interface_sym_mapping);
    5036        40546 :   sm->next = mapping->syms;
    5037        40546 :   sm->old = sym;
    5038        40546 :   sm->new_sym = new_symtree;
    5039        40546 :   sm->expr = gfc_copy_expr (expr);
    5040        40546 :   mapping->syms = sm;
    5041              : 
    5042              :   /* Stabilize the argument's value.  */
    5043        40546 :   if (!sym->attr.function && se)
    5044        40448 :     se->expr = gfc_evaluate_now (se->expr, &se->pre);
    5045              : 
    5046        40546 :   if (sym->ts.type == BT_CHARACTER)
    5047              :     {
    5048              :       /* Create a copy of the dummy argument's length.  */
    5049         2856 :       new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
    5050         2856 :       sm->expr->ts.u.cl = new_sym->ts.u.cl;
    5051              : 
    5052              :       /* If the length is specified as "*", record the length that
    5053              :          the caller is passing.  We should use the callee's length
    5054              :          in all other cases.  */
    5055         2856 :       if (!new_sym->ts.u.cl->length && se)
    5056              :         {
    5057         2628 :           se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
    5058         2628 :           new_sym->ts.u.cl->backend_decl = se->string_length;
    5059              :         }
    5060              :     }
    5061              : 
    5062        40532 :   if (!se)
    5063           62 :     return;
    5064              : 
    5065              :   /* Use the passed value as-is if the argument is a function.  */
    5066        40484 :   if (sym->attr.flavor == FL_PROCEDURE)
    5067           36 :     value = se->expr;
    5068              : 
    5069              :   /* If the argument is a pass-by-value scalar, use the value as is.  */
    5070        40448 :   else if (!sym->attr.dimension && sym->attr.value)
    5071           78 :     value = se->expr;
    5072              : 
    5073              :   /* If the argument is either a string or a pointer to a string,
    5074              :      convert it to a boundless character type.  */
    5075        40370 :   else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
    5076              :     {
    5077         1287 :       se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
    5078         1287 :       tmp = gfc_get_character_type_len (sym->ts.kind, se->string_length);
    5079         1287 :       tmp = build_pointer_type (tmp);
    5080         1287 :       if (sym->attr.pointer)
    5081          126 :         value = build_fold_indirect_ref_loc (input_location,
    5082              :                                          se->expr);
    5083              :       else
    5084         1161 :         value = se->expr;
    5085         1287 :       value = fold_convert (tmp, value);
    5086              :     }
    5087              : 
    5088              :   /* If the argument is a scalar, a pointer to an array or an allocatable,
    5089              :      dereference it.  */
    5090        39083 :   else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
    5091        29204 :     value = build_fold_indirect_ref_loc (input_location,
    5092              :                                      se->expr);
    5093              : 
    5094              :   /* For character(*), use the actual argument's descriptor.  */
    5095         9879 :   else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
    5096         1497 :     value = build_fold_indirect_ref_loc (input_location,
    5097              :                                          se->expr);
    5098              : 
    5099              :   /* If the argument is an array descriptor, use it to determine
    5100              :      information about the actual argument's shape.  */
    5101         8382 :   else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
    5102         8382 :            && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
    5103              :     {
    5104         8112 :       bool assumed_rank_formal = false;
    5105              : 
    5106              :       /* Get the actual argument's descriptor.  */
    5107         8112 :       desc = build_fold_indirect_ref_loc (input_location,
    5108              :                                       se->expr);
    5109              : 
    5110              :       /* Create the replacement variable.  */
    5111         8112 :       if (sym->as && sym->as->type == AS_ASSUMED_RANK
    5112         7334 :           && !(sym->ns && sym->ns->proc_name
    5113         7334 :                && sym->ns->proc_name->attr.proc == PROC_INTRINSIC))
    5114              :         {
    5115              :           assumed_rank_formal = true;
    5116              :           tmp = desc;
    5117              :         }
    5118              :       else
    5119         8099 :         tmp = gfc_conv_descriptor_data_get (desc);
    5120              : 
    5121         8112 :       value = gfc_get_interface_mapping_array (&se->pre, sym,
    5122              :                                                PACKED_NO, tmp,
    5123              :                                                se->string_length,
    5124              :                                                assumed_rank_formal);
    5125              : 
    5126              :       /* Use DESC to work out the upper bounds, strides and offset.  */
    5127         8112 :       gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
    5128              :     }
    5129              :   else
    5130              :     /* Otherwise we have a packed array.  */
    5131          270 :     value = gfc_get_interface_mapping_array (&se->pre, sym,
    5132              :                                              PACKED_FULL, se->expr,
    5133              :                                              se->string_length,
    5134              :                                              false);
    5135              : 
    5136        40484 :   new_sym->backend_decl = value;
    5137              : }
    5138              : 
    5139              : 
    5140              : /* Called once all dummy argument mappings have been added to MAPPING,
    5141              :    but before the mapping is used to evaluate expressions.  Pre-evaluate
    5142              :    the length of each argument, adding any initialization code to PRE and
    5143              :    any finalization code to POST.  */
    5144              : 
    5145              : static void
    5146       129943 : gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
    5147              :                               stmtblock_t * pre, stmtblock_t * post)
    5148              : {
    5149       129943 :   gfc_interface_sym_mapping *sym;
    5150       129943 :   gfc_expr *expr;
    5151       129943 :   gfc_se se;
    5152              : 
    5153       170427 :   for (sym = mapping->syms; sym; sym = sym->next)
    5154        40484 :     if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
    5155         2842 :         && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
    5156              :       {
    5157          214 :         expr = sym->new_sym->n.sym->ts.u.cl->length;
    5158          214 :         gfc_apply_interface_mapping_to_expr (mapping, expr);
    5159          214 :         gfc_init_se (&se, NULL);
    5160          214 :         gfc_conv_expr (&se, expr);
    5161          214 :         se.expr = fold_convert (gfc_charlen_type_node, se.expr);
    5162          214 :         se.expr = gfc_evaluate_now (se.expr, &se.pre);
    5163          214 :         gfc_add_block_to_block (pre, &se.pre);
    5164          214 :         gfc_add_block_to_block (post, &se.post);
    5165              : 
    5166          214 :         sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
    5167              :       }
    5168       129943 : }
    5169              : 
    5170              : 
    5171              : /* Like gfc_apply_interface_mapping_to_expr, but applied to
    5172              :    constructor C.  */
    5173              : 
    5174              : static void
    5175           47 : gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
    5176              :                                      gfc_constructor_base base)
    5177              : {
    5178           47 :   gfc_constructor *c;
    5179          428 :   for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
    5180              :     {
    5181          381 :       gfc_apply_interface_mapping_to_expr (mapping, c->expr);
    5182          381 :       if (c->iterator)
    5183              :         {
    5184            6 :           gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
    5185            6 :           gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
    5186            6 :           gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
    5187              :         }
    5188              :     }
    5189           47 : }
    5190              : 
    5191              : 
    5192              : /* Like gfc_apply_interface_mapping_to_expr, but applied to
    5193              :    reference REF.  */
    5194              : 
    5195              : static void
    5196        12585 : gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
    5197              :                                     gfc_ref * ref)
    5198              : {
    5199        12585 :   int n;
    5200              : 
    5201        14070 :   for (; ref; ref = ref->next)
    5202         1485 :     switch (ref->type)
    5203              :       {
    5204              :       case REF_ARRAY:
    5205         2915 :         for (n = 0; n < ref->u.ar.dimen; n++)
    5206              :           {
    5207         1650 :             gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
    5208         1650 :             gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
    5209         1650 :             gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
    5210              :           }
    5211              :         break;
    5212              : 
    5213              :       case REF_COMPONENT:
    5214              :       case REF_INQUIRY:
    5215              :         break;
    5216              : 
    5217           43 :       case REF_SUBSTRING:
    5218           43 :         gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
    5219           43 :         gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
    5220           43 :         break;
    5221              :       }
    5222        12585 : }
    5223              : 
    5224              : 
    5225              : /* Convert intrinsic function calls into result expressions.  */
    5226              : 
    5227              : static bool
    5228         2214 : gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
    5229              : {
    5230         2214 :   gfc_symbol *sym;
    5231         2214 :   gfc_expr *new_expr;
    5232         2214 :   gfc_expr *arg1;
    5233         2214 :   gfc_expr *arg2;
    5234         2214 :   int d, dup;
    5235              : 
    5236         2214 :   arg1 = expr->value.function.actual->expr;
    5237         2214 :   if (expr->value.function.actual->next)
    5238         2093 :     arg2 = expr->value.function.actual->next->expr;
    5239              :   else
    5240              :     arg2 = NULL;
    5241              : 
    5242         2214 :   sym = arg1->symtree->n.sym;
    5243              : 
    5244         2214 :   if (sym->attr.dummy)
    5245              :     return false;
    5246              : 
    5247         2190 :   new_expr = NULL;
    5248              : 
    5249         2190 :   switch (expr->value.function.isym->id)
    5250              :     {
    5251          929 :     case GFC_ISYM_LEN:
    5252              :       /* TODO figure out why this condition is necessary.  */
    5253          929 :       if (sym->attr.function
    5254           43 :           && (arg1->ts.u.cl->length == NULL
    5255           42 :               || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
    5256           42 :                   && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
    5257              :         return false;
    5258              : 
    5259          886 :       new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
    5260          886 :       break;
    5261              : 
    5262          228 :     case GFC_ISYM_LEN_TRIM:
    5263          228 :       new_expr = gfc_copy_expr (arg1);
    5264          228 :       gfc_apply_interface_mapping_to_expr (mapping, new_expr);
    5265              : 
    5266          228 :       if (!new_expr)
    5267              :         return false;
    5268              : 
    5269          228 :       gfc_replace_expr (arg1, new_expr);
    5270          228 :       return true;
    5271              : 
    5272          606 :     case GFC_ISYM_SIZE:
    5273          606 :       if (!sym->as || sym->as->rank == 0)
    5274              :         return false;
    5275              : 
    5276          530 :       if (arg2 && arg2->expr_type == EXPR_CONSTANT)
    5277              :         {
    5278          360 :           dup = mpz_get_si (arg2->value.integer);
    5279          360 :           d = dup - 1;
    5280              :         }
    5281              :       else
    5282              :         {
    5283          530 :           dup = sym->as->rank;
    5284          530 :           d = 0;
    5285              :         }
    5286              : 
    5287          542 :       for (; d < dup; d++)
    5288              :         {
    5289          530 :           gfc_expr *tmp;
    5290              : 
    5291          530 :           if (!sym->as->upper[d] || !sym->as->lower[d])
    5292              :             {
    5293          518 :               gfc_free_expr (new_expr);
    5294          518 :               return false;
    5295              :             }
    5296              : 
    5297           12 :           tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
    5298              :                                         gfc_get_int_expr (gfc_default_integer_kind,
    5299              :                                                           NULL, 1));
    5300           12 :           tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
    5301           12 :           if (new_expr)
    5302            0 :             new_expr = gfc_multiply (new_expr, tmp);
    5303              :           else
    5304              :             new_expr = tmp;
    5305              :         }
    5306              :       break;
    5307              : 
    5308           44 :     case GFC_ISYM_LBOUND:
    5309           44 :     case GFC_ISYM_UBOUND:
    5310              :         /* TODO These implementations of lbound and ubound do not limit if
    5311              :            the size < 0, according to F95's 13.14.53 and 13.14.113.  */
    5312              : 
    5313           44 :       if (!sym->as || sym->as->rank == 0)
    5314              :         return false;
    5315              : 
    5316           44 :       if (arg2 && arg2->expr_type == EXPR_CONSTANT)
    5317           38 :         d = mpz_get_si (arg2->value.integer) - 1;
    5318              :       else
    5319              :         return false;
    5320              : 
    5321           38 :       if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
    5322              :         {
    5323           23 :           if (sym->as->lower[d])
    5324           23 :             new_expr = gfc_copy_expr (sym->as->lower[d]);
    5325              :         }
    5326              :       else
    5327              :         {
    5328           15 :           if (sym->as->upper[d])
    5329            9 :             new_expr = gfc_copy_expr (sym->as->upper[d]);
    5330              :         }
    5331              :       break;
    5332              : 
    5333              :     default:
    5334              :       break;
    5335              :     }
    5336              : 
    5337         1319 :   gfc_apply_interface_mapping_to_expr (mapping, new_expr);
    5338         1319 :   if (!new_expr)
    5339              :     return false;
    5340              : 
    5341          113 :   gfc_replace_expr (expr, new_expr);
    5342          113 :   return true;
    5343              : }
    5344              : 
    5345              : 
    5346              : static void
    5347           24 : gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
    5348              :                               gfc_interface_mapping * mapping)
    5349              : {
    5350           24 :   gfc_formal_arglist *f;
    5351           24 :   gfc_actual_arglist *actual;
    5352              : 
    5353           24 :   actual = expr->value.function.actual;
    5354           24 :   f = gfc_sym_get_dummy_args (map_expr->symtree->n.sym);
    5355              : 
    5356           72 :   for (; f && actual; f = f->next, actual = actual->next)
    5357              :     {
    5358           24 :       if (!actual->expr)
    5359            0 :         continue;
    5360              : 
    5361           24 :       gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
    5362              :     }
    5363              : 
    5364           24 :   if (map_expr->symtree->n.sym->attr.dimension)
    5365              :     {
    5366            6 :       int d;
    5367            6 :       gfc_array_spec *as;
    5368              : 
    5369            6 :       as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
    5370              : 
    5371           18 :       for (d = 0; d < as->rank; d++)
    5372              :         {
    5373            6 :           gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
    5374            6 :           gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
    5375              :         }
    5376              : 
    5377            6 :       expr->value.function.esym->as = as;
    5378              :     }
    5379              : 
    5380           24 :   if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
    5381              :     {
    5382            0 :       expr->value.function.esym->ts.u.cl->length
    5383            0 :         = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
    5384              : 
    5385            0 :       gfc_apply_interface_mapping_to_expr (mapping,
    5386            0 :                         expr->value.function.esym->ts.u.cl->length);
    5387              :     }
    5388           24 : }
    5389              : 
    5390              : 
    5391              : /* EXPR is a copy of an expression that appeared in the interface
    5392              :    associated with MAPPING.  Walk it recursively looking for references to
    5393              :    dummy arguments that MAPPING maps to actual arguments.  Replace each such
    5394              :    reference with a reference to the associated actual argument.  */
    5395              : 
    5396              : static void
    5397        21118 : gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
    5398              :                                      gfc_expr * expr)
    5399              : {
    5400        22683 :   gfc_interface_sym_mapping *sym;
    5401        22683 :   gfc_actual_arglist *actual;
    5402              : 
    5403        22683 :   if (!expr)
    5404              :     return;
    5405              : 
    5406              :   /* Copying an expression does not copy its length, so do that here.  */
    5407        12585 :   if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
    5408              :     {
    5409         1784 :       expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
    5410         1784 :       gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
    5411              :     }
    5412              : 
    5413              :   /* Apply the mapping to any references.  */
    5414        12585 :   gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
    5415              : 
    5416              :   /* ...and to the expression's symbol, if it has one.  */
    5417              :   /* TODO Find out why the condition on expr->symtree had to be moved into
    5418              :      the loop rather than being outside it, as originally.  */
    5419        29942 :   for (sym = mapping->syms; sym; sym = sym->next)
    5420        17357 :     if (expr->symtree && !strcmp (sym->old->name, expr->symtree->n.sym->name))
    5421              :       {
    5422         3370 :         if (sym->new_sym->n.sym->backend_decl)
    5423         3326 :           expr->symtree = sym->new_sym;
    5424           44 :         else if (sym->expr)
    5425           44 :           gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
    5426              :       }
    5427              : 
    5428              :       /* ...and to subexpressions in expr->value.  */
    5429        12585 :   switch (expr->expr_type)
    5430              :     {
    5431              :     case EXPR_VARIABLE:
    5432              :     case EXPR_CONSTANT:
    5433              :     case EXPR_NULL:
    5434              :     case EXPR_SUBSTRING:
    5435              :       break;
    5436              : 
    5437         1565 :     case EXPR_OP:
    5438         1565 :       gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
    5439         1565 :       gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
    5440         1565 :       break;
    5441              : 
    5442            0 :     case EXPR_CONDITIONAL:
    5443            0 :       gfc_apply_interface_mapping_to_expr (mapping,
    5444            0 :                                            expr->value.conditional.true_expr);
    5445            0 :       gfc_apply_interface_mapping_to_expr (mapping,
    5446            0 :                                            expr->value.conditional.false_expr);
    5447            0 :       break;
    5448              : 
    5449         2957 :     case EXPR_FUNCTION:
    5450         9502 :       for (actual = expr->value.function.actual; actual; actual = actual->next)
    5451         6545 :         gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
    5452              : 
    5453         2957 :       if (expr->value.function.esym == NULL
    5454         2644 :             && expr->value.function.isym != NULL
    5455         2632 :             && expr->value.function.actual
    5456         2631 :             && expr->value.function.actual->expr
    5457         2631 :             && expr->value.function.actual->expr->symtree
    5458         5171 :             && gfc_map_intrinsic_function (expr, mapping))
    5459              :         break;
    5460              : 
    5461         6154 :       for (sym = mapping->syms; sym; sym = sym->next)
    5462         3538 :         if (sym->old == expr->value.function.esym)
    5463              :           {
    5464           24 :             expr->value.function.esym = sym->new_sym->n.sym;
    5465           24 :             gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
    5466           24 :             expr->value.function.esym->result = sym->new_sym->n.sym;
    5467              :           }
    5468              :       break;
    5469              : 
    5470           47 :     case EXPR_ARRAY:
    5471           47 :     case EXPR_STRUCTURE:
    5472           47 :       gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
    5473           47 :       break;
    5474              : 
    5475            0 :     case EXPR_COMPCALL:
    5476            0 :     case EXPR_PPC:
    5477            0 :     case EXPR_UNKNOWN:
    5478            0 :       gcc_unreachable ();
    5479              :       break;
    5480              :     }
    5481              : 
    5482              :   return;
    5483              : }
    5484              : 
    5485              : 
    5486              : /* Evaluate interface expression EXPR using MAPPING.  Store the result
    5487              :    in SE.  */
    5488              : 
    5489              : void
    5490         4016 : gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
    5491              :                              gfc_se * se, gfc_expr * expr)
    5492              : {
    5493         4016 :   expr = gfc_copy_expr (expr);
    5494         4016 :   gfc_apply_interface_mapping_to_expr (mapping, expr);
    5495         4016 :   gfc_conv_expr (se, expr);
    5496         4016 :   se->expr = gfc_evaluate_now (se->expr, &se->pre);
    5497         4016 :   gfc_free_expr (expr);
    5498         4016 : }
    5499              : 
    5500              : 
    5501              : /* Returns a reference to a temporary array into which a component of
    5502              :    an actual argument derived type array is copied and then returned
    5503              :    after the function call.  */
    5504              : void
    5505         2612 : gfc_conv_subref_array_arg (gfc_se *se, gfc_expr * expr, int g77,
    5506              :                            sym_intent intent, bool formal_ptr,
    5507              :                            const gfc_symbol *fsym, const char *proc_name,
    5508              :                            gfc_symbol *sym, bool check_contiguous)
    5509              : {
    5510         2612 :   gfc_se lse;
    5511         2612 :   gfc_se rse;
    5512         2612 :   gfc_ss *lss;
    5513         2612 :   gfc_ss *rss;
    5514         2612 :   gfc_loopinfo loop;
    5515         2612 :   gfc_loopinfo loop2;
    5516         2612 :   gfc_array_info *info;
    5517         2612 :   tree offset;
    5518         2612 :   tree tmp_index;
    5519         2612 :   tree tmp;
    5520         2612 :   tree base_type;
    5521         2612 :   tree size;
    5522         2612 :   stmtblock_t body;
    5523         2612 :   int n;
    5524         2612 :   int dimen;
    5525         2612 :   gfc_se work_se;
    5526         2612 :   gfc_se *parmse;
    5527         2612 :   bool pass_optional;
    5528         2612 :   bool readonly;
    5529              : 
    5530         2612 :   pass_optional = fsym && fsym->attr.optional && sym && sym->attr.optional;
    5531              : 
    5532         2601 :   if (pass_optional || check_contiguous)
    5533              :     {
    5534         1359 :       gfc_init_se (&work_se, NULL);
    5535         1359 :       parmse = &work_se;
    5536              :     }
    5537              :   else
    5538              :     parmse = se;
    5539              : 
    5540         2612 :   if (gfc_option.rtcheck & GFC_RTCHECK_ARRAY_TEMPS)
    5541              :     {
    5542              :       /* We will create a temporary array, so let us warn.  */
    5543          868 :       char * msg;
    5544              : 
    5545          868 :       if (fsym && proc_name)
    5546          868 :         msg = xasprintf ("An array temporary was created for argument "
    5547          868 :                          "'%s' of procedure '%s'", fsym->name, proc_name);
    5548              :       else
    5549            0 :         msg = xasprintf ("An array temporary was created");
    5550              : 
    5551          868 :       tmp = build_int_cst (logical_type_node, 1);
    5552          868 :       gfc_trans_runtime_check (false, true, tmp, &parmse->pre,
    5553              :                                &expr->where, msg);
    5554          868 :       free (msg);
    5555              :     }
    5556              : 
    5557         2612 :   gfc_init_se (&lse, NULL);
    5558         2612 :   gfc_init_se (&rse, NULL);
    5559              : 
    5560              :   /* Walk the argument expression.  */
    5561         2612 :   rss = gfc_walk_expr (expr);
    5562              : 
    5563         2612 :   gcc_assert (rss != gfc_ss_terminator);
    5564              : 
    5565              :   /* Initialize the scalarizer.  */
    5566         2612 :   gfc_init_loopinfo (&loop);
    5567         2612 :   gfc_add_ss_to_loop (&loop, rss);
    5568              : 
    5569              :   /* Calculate the bounds of the scalarization.  */
    5570         2612 :   gfc_conv_ss_startstride (&loop);
    5571              : 
    5572              :   /* Build an ss for the temporary.  */
    5573         2612 :   if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
    5574          136 :     gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
    5575              : 
    5576         2612 :   base_type = gfc_typenode_for_spec (&expr->ts);
    5577         2612 :   if (GFC_ARRAY_TYPE_P (base_type)
    5578         2612 :                 || GFC_DESCRIPTOR_TYPE_P (base_type))
    5579            0 :     base_type = gfc_get_element_type (base_type);
    5580              : 
    5581         2612 :   if (expr->ts.type == BT_CLASS)
    5582          121 :     base_type = gfc_typenode_for_spec (&CLASS_DATA (expr)->ts);
    5583              : 
    5584         3776 :   loop.temp_ss = gfc_get_temp_ss (base_type, ((expr->ts.type == BT_CHARACTER)
    5585         1164 :                                               ? expr->ts.u.cl->backend_decl
    5586              :                                               : NULL),
    5587              :                                   loop.dimen);
    5588              : 
    5589         2612 :   parmse->string_length = loop.temp_ss->info->string_length;
    5590              : 
    5591              :   /* Associate the SS with the loop.  */
    5592         2612 :   gfc_add_ss_to_loop (&loop, loop.temp_ss);
    5593              : 
    5594              :   /* Setup the scalarizing loops.  */
    5595         2612 :   gfc_conv_loop_setup (&loop, &expr->where);
    5596              : 
    5597              :   /* Pass the temporary descriptor back to the caller.  */
    5598         2612 :   info = &loop.temp_ss->info->data.array;
    5599         2612 :   parmse->expr = info->descriptor;
    5600              : 
    5601              :   /* Setup the gfc_se structures.  */
    5602         2612 :   gfc_copy_loopinfo_to_se (&lse, &loop);
    5603         2612 :   gfc_copy_loopinfo_to_se (&rse, &loop);
    5604              : 
    5605         2612 :   rse.ss = rss;
    5606         2612 :   lse.ss = loop.temp_ss;
    5607         2612 :   gfc_mark_ss_chain_used (rss, 1);
    5608         2612 :   gfc_mark_ss_chain_used (loop.temp_ss, 1);
    5609              : 
    5610              :   /* Start the scalarized loop body.  */
    5611         2612 :   gfc_start_scalarized_body (&loop, &body);
    5612              : 
    5613              :   /* Translate the expression.  */
    5614         2612 :   gfc_conv_expr (&rse, expr);
    5615              : 
    5616         2612 :   gfc_conv_tmp_array_ref (&lse);
    5617              : 
    5618         2612 :   if (intent != INTENT_OUT)
    5619              :     {
    5620         2574 :       tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false);
    5621         2574 :       gfc_add_expr_to_block (&body, tmp);
    5622         2574 :       gcc_assert (rse.ss == gfc_ss_terminator);
    5623         2574 :       gfc_trans_scalarizing_loops (&loop, &body);
    5624              :     }
    5625              :   else
    5626              :     {
    5627              :       /* Make sure that the temporary declaration survives by merging
    5628              :        all the loop declarations into the current context.  */
    5629           85 :       for (n = 0; n < loop.dimen; n++)
    5630              :         {
    5631           47 :           gfc_merge_block_scope (&body);
    5632           47 :           body = loop.code[loop.order[n]];
    5633              :         }
    5634           38 :       gfc_merge_block_scope (&body);
    5635              :     }
    5636              : 
    5637              :   /* Add the post block after the second loop, so that any
    5638              :      freeing of allocated memory is done at the right time.  */
    5639         2612 :   gfc_add_block_to_block (&parmse->pre, &loop.pre);
    5640              : 
    5641              :   /**********Copy the temporary back again.*********/
    5642              : 
    5643         2612 :   gfc_init_se (&lse, NULL);
    5644         2612 :   gfc_init_se (&rse, NULL);
    5645              : 
    5646              :   /* Walk the argument expression.  */
    5647         2612 :   lss = gfc_walk_expr (expr);
    5648         2612 :   rse.ss = loop.temp_ss;
    5649         2612 :   lse.ss = lss;
    5650              : 
    5651              :   /* Initialize the scalarizer.  */
    5652         2612 :   gfc_init_loopinfo (&loop2);
    5653         2612 :   gfc_add_ss_to_loop (&loop2, lss);
    5654              : 
    5655         2612 :   dimen = rse.ss->dimen;
    5656              : 
    5657              :   /* Skip the write-out loop for this case.  */
    5658         2612 :   if (gfc_is_class_array_function (expr))
    5659           13 :     goto class_array_fcn;
    5660              : 
    5661              :   /* Calculate the bounds of the scalarization.  */
    5662         2599 :   gfc_conv_ss_startstride (&loop2);
    5663              : 
    5664              :   /* Setup the scalarizing loops.  */
    5665         2599 :   gfc_conv_loop_setup (&loop2, &expr->where);
    5666              : 
    5667         2599 :   gfc_copy_loopinfo_to_se (&lse, &loop2);
    5668         2599 :   gfc_copy_loopinfo_to_se (&rse, &loop2);
    5669              : 
    5670         2599 :   gfc_mark_ss_chain_used (lss, 1);
    5671         2599 :   gfc_mark_ss_chain_used (loop.temp_ss, 1);
    5672              : 
    5673              :   /* Declare the variable to hold the temporary offset and start the
    5674              :      scalarized loop body.  */
    5675         2599 :   offset = gfc_create_var (gfc_array_index_type, NULL);
    5676         2599 :   gfc_start_scalarized_body (&loop2, &body);
    5677              : 
    5678              :   /* Build the offsets for the temporary from the loop variables.  The
    5679              :      temporary array has lbounds of zero and strides of one in all
    5680              :      dimensions, so this is very simple.  The offset is only computed
    5681              :      outside the innermost loop, so the overall transfer could be
    5682              :      optimized further.  */
    5683         2599 :   info = &rse.ss->info->data.array;
    5684              : 
    5685         2599 :   tmp_index = gfc_index_zero_node;
    5686         3949 :   for (n = dimen - 1; n > 0; n--)
    5687              :     {
    5688         1350 :       tree tmp_str;
    5689         1350 :       tmp = rse.loop->loopvar[n];
    5690         1350 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    5691              :                              tmp, rse.loop->from[n]);
    5692         1350 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    5693              :                              tmp, tmp_index);
    5694              : 
    5695         2700 :       tmp_str = fold_build2_loc (input_location, MINUS_EXPR,
    5696              :                                  gfc_array_index_type,
    5697         1350 :                                  rse.loop->to[n-1], rse.loop->from[n-1]);
    5698         1350 :       tmp_str = fold_build2_loc (input_location, PLUS_EXPR,
    5699              :                                  gfc_array_index_type,
    5700              :                                  tmp_str, gfc_index_one_node);
    5701              : 
    5702         1350 :       tmp_index = fold_build2_loc (input_location, MULT_EXPR,
    5703              :                                    gfc_array_index_type, tmp, tmp_str);
    5704              :     }
    5705              : 
    5706         5198 :   tmp_index = fold_build2_loc (input_location, MINUS_EXPR,
    5707              :                                gfc_array_index_type,
    5708         2599 :                                tmp_index, rse.loop->from[0]);
    5709         2599 :   gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
    5710              : 
    5711         5198 :   tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
    5712              :                                gfc_array_index_type,
    5713         2599 :                                rse.loop->loopvar[0], offset);
    5714              : 
    5715              :   /* Now use the offset for the reference.  */
    5716         2599 :   tmp = build_fold_indirect_ref_loc (input_location,
    5717              :                                  info->data);
    5718         2599 :   rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
    5719              : 
    5720         2599 :   if (expr->ts.type == BT_CHARACTER)
    5721         1164 :     rse.string_length = expr->ts.u.cl->backend_decl;
    5722              : 
    5723         2599 :   gfc_conv_expr (&lse, expr);
    5724              : 
    5725         2599 :   gcc_assert (lse.ss == gfc_ss_terminator);
    5726              : 
    5727         2599 :   tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, true);
    5728         2599 :   gfc_add_expr_to_block (&body, tmp);
    5729              : 
    5730              :   /* Generate the copying loops.  */
    5731         2599 :   gfc_trans_scalarizing_loops (&loop2, &body);
    5732              : 
    5733              :   /* Wrap the whole thing up by adding the second loop to the post-block
    5734              :      and following it by the post-block of the first loop.  In this way,
    5735              :      if the temporary needs freeing, it is done after use!
    5736              :      If input expr is read-only, e.g. a PARAMETER array, copying back
    5737              :      modified values is undefined behavior.  */
    5738         5198 :   readonly = (expr->expr_type == EXPR_VARIABLE
    5739         2545 :               && expr->symtree
    5740         5144 :               && expr->symtree->n.sym->attr.flavor == FL_PARAMETER);
    5741              : 
    5742         2599 :   if ((intent != INTENT_IN) && !readonly)
    5743              :     {
    5744         1166 :       gfc_add_block_to_block (&parmse->post, &loop2.pre);
    5745         1166 :       gfc_add_block_to_block (&parmse->post, &loop2.post);
    5746              :     }
    5747              : 
    5748         1433 : class_array_fcn:
    5749              : 
    5750         2612 :   gfc_add_block_to_block (&parmse->post, &loop.post);
    5751              : 
    5752         2612 :   gfc_cleanup_loop (&loop);
    5753         2612 :   gfc_cleanup_loop (&loop2);
    5754              : 
    5755              :   /* Pass the string length to the argument expression.  */
    5756         2612 :   if (expr->ts.type == BT_CHARACTER)
    5757         1164 :     parmse->string_length = expr->ts.u.cl->backend_decl;
    5758              : 
    5759              :   /* Determine the offset for pointer formal arguments and set the
    5760              :      lbounds to one.  */
    5761         2612 :   if (formal_ptr)
    5762              :     {
    5763           18 :       size = gfc_index_one_node;
    5764           18 :       offset = gfc_index_zero_node;
    5765           36 :       for (n = 0; n < dimen; n++)
    5766              :         {
    5767           18 :           tmp = gfc_conv_descriptor_ubound_get (parmse->expr,
    5768              :                                                 gfc_rank_cst[n]);
    5769           18 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    5770              :                                  gfc_array_index_type, tmp,
    5771              :                                  gfc_index_one_node);
    5772           18 :           gfc_conv_descriptor_ubound_set (&parmse->pre,
    5773              :                                           parmse->expr,
    5774              :                                           gfc_rank_cst[n],
    5775              :                                           tmp);
    5776           18 :           gfc_conv_descriptor_lbound_set (&parmse->pre,
    5777              :                                           parmse->expr,
    5778              :                                           gfc_rank_cst[n],
    5779              :                                           gfc_index_one_node);
    5780           18 :           size = gfc_evaluate_now (size, &parmse->pre);
    5781           18 :           offset = fold_build2_loc (input_location, MINUS_EXPR,
    5782              :                                     gfc_array_index_type,
    5783              :                                     offset, size);
    5784           18 :           offset = gfc_evaluate_now (offset, &parmse->pre);
    5785           36 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    5786              :                                  gfc_array_index_type,
    5787           18 :                                  rse.loop->to[n], rse.loop->from[n]);
    5788           18 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    5789              :                                  gfc_array_index_type,
    5790              :                                  tmp, gfc_index_one_node);
    5791           18 :           size = fold_build2_loc (input_location, MULT_EXPR,
    5792              :                                   gfc_array_index_type, size, tmp);
    5793              :         }
    5794              : 
    5795           18 :       gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
    5796              :                                       offset);
    5797              :     }
    5798              : 
    5799              :   /* We want either the address for the data or the address of the descriptor,
    5800              :      depending on the mode of passing array arguments.  */
    5801         2612 :   if (g77)
    5802          437 :     parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
    5803              :   else
    5804         2175 :     parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
    5805              : 
    5806              :   /* Basically make this into
    5807              : 
    5808              :      if (present)
    5809              :        {
    5810              :          if (contiguous)
    5811              :            {
    5812              :              pointer = a;
    5813              :            }
    5814              :          else
    5815              :            {
    5816              :              parmse->pre();
    5817              :              pointer = parmse->expr;
    5818              :            }
    5819              :        }
    5820              :      else
    5821              :        pointer = NULL;
    5822              : 
    5823              :      foo (pointer);
    5824              :      if (present && !contiguous)
    5825              :            se->post();
    5826              : 
    5827              :      */
    5828              : 
    5829         2612 :   if (pass_optional || check_contiguous)
    5830              :     {
    5831         1359 :       tree type;
    5832         1359 :       stmtblock_t else_block;
    5833         1359 :       tree pre_stmts, post_stmts;
    5834         1359 :       tree pointer;
    5835         1359 :       tree else_stmt;
    5836         1359 :       tree present_var = NULL_TREE;
    5837         1359 :       tree cont_var = NULL_TREE;
    5838         1359 :       tree post_cond;
    5839              : 
    5840         1359 :       type = TREE_TYPE (parmse->expr);
    5841         1359 :       if (POINTER_TYPE_P (type) && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
    5842         1027 :         type = TREE_TYPE (type);
    5843         1359 :       pointer = gfc_create_var (type, "arg_ptr");
    5844              : 
    5845         1359 :       if (check_contiguous)
    5846              :         {
    5847         1359 :           gfc_se cont_se, array_se;
    5848         1359 :           stmtblock_t if_block, else_block;
    5849         1359 :           tree if_stmt, else_stmt;
    5850         1359 :           mpz_t size;
    5851         1359 :           bool size_set;
    5852              : 
    5853         1359 :           cont_var = gfc_create_var (boolean_type_node, "contiguous");
    5854              : 
    5855              :           /* If the size is known to be one at compile-time, set
    5856              :              cont_var to true unconditionally.  This may look
    5857              :              inelegant, but we're only doing this during
    5858              :              optimization, so the statements will be optimized away,
    5859              :              and this saves complexity here.  */
    5860              : 
    5861         1359 :           size_set = gfc_array_size (expr, &size);
    5862         1359 :           if (size_set && mpz_cmp_ui (size, 1) == 0)
    5863              :             {
    5864            6 :               gfc_add_modify (&se->pre, cont_var,
    5865              :                               build_one_cst (boolean_type_node));
    5866              :             }
    5867              :           else
    5868              :             {
    5869              :               /* cont_var = is_contiguous (expr); .  */
    5870         1353 :               gfc_init_se (&cont_se, parmse);
    5871         1353 :               gfc_conv_is_contiguous_expr (&cont_se, expr);
    5872         1353 :               gfc_add_block_to_block (&se->pre, &(&cont_se)->pre);
    5873         1353 :               gfc_add_modify (&se->pre, cont_var, cont_se.expr);
    5874         1353 :               gfc_add_block_to_block (&se->pre, &(&cont_se)->post);
    5875              :             }
    5876              : 
    5877         1359 :           if (size_set)
    5878         1145 :             mpz_clear (size);
    5879              : 
    5880              :           /* arrayse->expr = descriptor of a.  */
    5881         1359 :           gfc_init_se (&array_se, se);
    5882         1359 :           gfc_conv_expr_descriptor (&array_se, expr);
    5883         1359 :           gfc_add_block_to_block (&se->pre, &(&array_se)->pre);
    5884         1359 :           gfc_add_block_to_block (&se->pre, &(&array_se)->post);
    5885              : 
    5886              :           /* if_stmt = { descriptor ? pointer = a : pointer = &a[0]; } .  */
    5887         1359 :           gfc_init_block (&if_block);
    5888         1359 :           if (GFC_DESCRIPTOR_TYPE_P (type))
    5889         1027 :             gfc_add_modify (&if_block, pointer, array_se.expr);
    5890              :           else
    5891              :             {
    5892          332 :               tmp = gfc_conv_array_data (array_se.expr);
    5893          332 :               tmp = fold_convert (type, tmp);
    5894          332 :               gfc_add_modify (&if_block, pointer, tmp);
    5895              :             }
    5896         1359 :           if_stmt = gfc_finish_block (&if_block);
    5897              : 
    5898              :           /* else_stmt = { parmse->pre(); pointer = parmse->expr; } .  */
    5899         1359 :           gfc_init_block (&else_block);
    5900         1359 :           gfc_add_block_to_block (&else_block, &parmse->pre);
    5901         1691 :           tmp = (GFC_DESCRIPTOR_TYPE_P (type)
    5902         1359 :                  ? build_fold_indirect_ref_loc (input_location, parmse->expr)
    5903              :                  : parmse->expr);
    5904         1359 :           gfc_add_modify (&else_block, pointer, tmp);
    5905         1359 :           else_stmt = gfc_finish_block (&else_block);
    5906              : 
    5907              :           /* And put the above into an if statement.  */
    5908         1359 :           pre_stmts = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    5909              :                                        gfc_likely (cont_var,
    5910              :                                                    PRED_FORTRAN_CONTIGUOUS),
    5911              :                                        if_stmt, else_stmt);
    5912              :         }
    5913              :       else
    5914              :         {
    5915              :           /* pointer = pramse->expr;  .  */
    5916            0 :           gfc_add_modify (&parmse->pre, pointer, parmse->expr);
    5917            0 :           pre_stmts = gfc_finish_block (&parmse->pre);
    5918              :         }
    5919              : 
    5920         1359 :       if (pass_optional)
    5921              :         {
    5922           11 :           present_var = gfc_create_var (boolean_type_node, "present");
    5923              : 
    5924              :           /* present_var = present(sym); .  */
    5925           11 :           tmp = gfc_conv_expr_present (sym);
    5926           11 :           tmp = fold_convert (boolean_type_node, tmp);
    5927           11 :           gfc_add_modify (&se->pre, present_var, tmp);
    5928              : 
    5929              :           /* else_stmt = { pointer = NULL; } .  */
    5930           11 :           gfc_init_block (&else_block);
    5931           11 :           if (GFC_DESCRIPTOR_TYPE_P (type))
    5932            0 :             gfc_conv_descriptor_data_set (&else_block, pointer,
    5933              :                                           null_pointer_node);
    5934              :           else
    5935           11 :             gfc_add_modify (&else_block, pointer, build_int_cst (type, 0));
    5936           11 :           else_stmt = gfc_finish_block (&else_block);
    5937              : 
    5938           11 :           tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    5939              :                                  gfc_likely (present_var,
    5940              :                                              PRED_FORTRAN_ABSENT_DUMMY),
    5941              :                                  pre_stmts, else_stmt);
    5942           11 :           gfc_add_expr_to_block (&se->pre, tmp);
    5943              :         }
    5944              :       else
    5945         1348 :         gfc_add_expr_to_block (&se->pre, pre_stmts);
    5946              : 
    5947         1359 :       post_stmts = gfc_finish_block (&parmse->post);
    5948              : 
    5949              :       /* Put together the post stuff, plus the optional
    5950              :          deallocation.  */
    5951         1359 :       if (check_contiguous)
    5952              :         {
    5953              :           /* !cont_var.  */
    5954         1359 :           tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    5955              :                                  cont_var,
    5956              :                                  build_zero_cst (boolean_type_node));
    5957         1359 :           tmp = gfc_unlikely (tmp, PRED_FORTRAN_CONTIGUOUS);
    5958              : 
    5959         1359 :           if (pass_optional)
    5960              :             {
    5961           11 :               tree present_likely = gfc_likely (present_var,
    5962              :                                                 PRED_FORTRAN_ABSENT_DUMMY);
    5963           11 :               post_cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    5964              :                                            boolean_type_node, present_likely,
    5965              :                                            tmp);
    5966              :             }
    5967              :           else
    5968              :             post_cond = tmp;
    5969              :         }
    5970              :       else
    5971              :         {
    5972            0 :           gcc_assert (pass_optional);
    5973              :           post_cond = present_var;
    5974              :         }
    5975              : 
    5976         1359 :       tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, post_cond,
    5977              :                              post_stmts, build_empty_stmt (input_location));
    5978         1359 :       gfc_add_expr_to_block (&se->post, tmp);
    5979         1359 :       if (GFC_DESCRIPTOR_TYPE_P (type))
    5980              :         {
    5981         1027 :           type = TREE_TYPE (parmse->expr);
    5982         1027 :           if (POINTER_TYPE_P (type))
    5983              :             {
    5984         1027 :               pointer = gfc_build_addr_expr (type, pointer);
    5985         1027 :               if (pass_optional)
    5986              :                 {
    5987            0 :                   tmp = gfc_likely (present_var, PRED_FORTRAN_ABSENT_DUMMY);
    5988            0 :                   pointer = fold_build3_loc (input_location, COND_EXPR, type,
    5989              :                                              tmp, pointer,
    5990              :                                              fold_convert (type,
    5991              :                                                            null_pointer_node));
    5992              :                 }
    5993              :             }
    5994              :           else
    5995            0 :             gcc_assert (!pass_optional);
    5996              :         }
    5997         1359 :       se->expr = pointer;
    5998              :     }
    5999              : 
    6000         2612 :   return;
    6001              : }
    6002              : 
    6003              : 
    6004              : /* Generate the code for argument list functions.  */
    6005              : 
    6006              : static void
    6007         5822 : conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
    6008              : {
    6009              :   /* Pass by value for g77 %VAL(arg), pass the address
    6010              :      indirectly for %LOC, else by reference.  Thus %REF
    6011              :      is a "do-nothing" and %LOC is the same as an F95
    6012              :      pointer.  */
    6013         5822 :   if (strcmp (name, "%VAL") == 0)
    6014         5810 :     gfc_conv_expr (se, expr);
    6015           12 :   else if (strcmp (name, "%LOC") == 0)
    6016              :     {
    6017            6 :       gfc_conv_expr_reference (se, expr);
    6018            6 :       se->expr = gfc_build_addr_expr (NULL, se->expr);
    6019              :     }
    6020            6 :   else if (strcmp (name, "%REF") == 0)
    6021            6 :     gfc_conv_expr_reference (se, expr);
    6022              :   else
    6023            0 :     gfc_error ("Unknown argument list function at %L", &expr->where);
    6024         5822 : }
    6025              : 
    6026              : 
    6027              : /* This function tells whether the middle-end representation of the expression
    6028              :    E given as input may point to data otherwise accessible through a variable
    6029              :    (sub-)reference.
    6030              :    It is assumed that the only expressions that may alias are variables,
    6031              :    and array constructors if ARRAY_MAY_ALIAS is true and some of its elements
    6032              :    may alias.
    6033              :    This function is used to decide whether freeing an expression's allocatable
    6034              :    components is safe or should be avoided.
    6035              : 
    6036              :    If ARRAY_MAY_ALIAS is true, an array constructor may alias if some of
    6037              :    its elements are copied from a variable.  This ARRAY_MAY_ALIAS trick
    6038              :    is necessary because for array constructors, aliasing depends on how
    6039              :    the array is used:
    6040              :     - If E is an array constructor used as argument to an elemental procedure,
    6041              :       the array, which is generated through shallow copy by the scalarizer,
    6042              :       is used directly and can alias the expressions it was copied from.
    6043              :     - If E is an array constructor used as argument to a non-elemental
    6044              :       procedure,the scalarizer is used in gfc_conv_expr_descriptor to generate
    6045              :       the array as in the previous case, but then that array is used
    6046              :       to initialize a new descriptor through deep copy.  There is no alias
    6047              :       possible in that case.
    6048              :    Thus, the ARRAY_MAY_ALIAS flag is necessary to distinguish the two cases
    6049              :    above.  */
    6050              : 
    6051              : static bool
    6052         7593 : expr_may_alias_variables (gfc_expr *e, bool array_may_alias)
    6053              : {
    6054         7593 :   gfc_constructor *c;
    6055              : 
    6056         7593 :   if (e->expr_type == EXPR_VARIABLE)
    6057              :     return true;
    6058          544 :   else if (e->expr_type == EXPR_FUNCTION)
    6059              :     {
    6060          161 :       gfc_symbol *proc_ifc = gfc_get_proc_ifc_for_expr (e);
    6061              : 
    6062          161 :       if (proc_ifc->result != NULL
    6063          161 :           && ((proc_ifc->result->ts.type == BT_CLASS
    6064           25 :                && proc_ifc->result->ts.u.derived->attr.is_class
    6065           25 :                && CLASS_DATA (proc_ifc->result)->attr.class_pointer)
    6066          161 :               || proc_ifc->result->attr.pointer))
    6067              :         return true;
    6068              :       else
    6069              :         return false;
    6070              :     }
    6071          383 :   else if (e->expr_type != EXPR_ARRAY || !array_may_alias)
    6072              :     return false;
    6073              : 
    6074           79 :   for (c = gfc_constructor_first (e->value.constructor);
    6075          233 :        c; c = gfc_constructor_next (c))
    6076          189 :     if (c->expr
    6077          189 :         && expr_may_alias_variables (c->expr, array_may_alias))
    6078              :       return true;
    6079              : 
    6080              :   return false;
    6081              : }
    6082              : 
    6083              : 
    6084              : /* A helper function to set the dtype for unallocated or unassociated
    6085              :    entities.  */
    6086              : 
    6087              : static void
    6088          891 : set_dtype_for_unallocated (gfc_se *parmse, gfc_expr *e)
    6089              : {
    6090          891 :   tree tmp;
    6091          891 :   tree desc;
    6092          891 :   tree cond;
    6093          891 :   tree type;
    6094          891 :   stmtblock_t block;
    6095              : 
    6096              :   /* TODO Figure out how to handle optional dummies.  */
    6097          891 :   if (e && e->expr_type == EXPR_VARIABLE
    6098          807 :       && e->symtree->n.sym->attr.optional)
    6099          108 :     return;
    6100              : 
    6101          819 :   desc = parmse->expr;
    6102          819 :   if (desc == NULL_TREE)
    6103              :     return;
    6104              : 
    6105          819 :   if (POINTER_TYPE_P (TREE_TYPE (desc)))
    6106          819 :     desc = build_fold_indirect_ref_loc (input_location, desc);
    6107          819 :   if (GFC_CLASS_TYPE_P (TREE_TYPE (desc)))
    6108          192 :     desc = gfc_class_data_get (desc);
    6109          819 :   if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
    6110              :     return;
    6111              : 
    6112          783 :   gfc_init_block (&block);
    6113          783 :   tmp = gfc_conv_descriptor_data_get (desc);
    6114          783 :   cond = fold_build2_loc (input_location, EQ_EXPR,
    6115              :                           logical_type_node, tmp,
    6116          783 :                           build_int_cst (TREE_TYPE (tmp), 0));
    6117          783 :   tmp = gfc_conv_descriptor_dtype (desc);
    6118          783 :   type = gfc_get_element_type (TREE_TYPE (desc));
    6119         1566 :   tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    6120          783 :                          TREE_TYPE (tmp), tmp,
    6121              :                          gfc_get_dtype_rank_type (e->rank, type));
    6122          783 :   gfc_add_expr_to_block (&block, tmp);
    6123          783 :   cond = build3_v (COND_EXPR, cond,
    6124              :                    gfc_finish_block (&block),
    6125              :                    build_empty_stmt (input_location));
    6126          783 :   gfc_add_expr_to_block (&parmse->pre, cond);
    6127              : }
    6128              : 
    6129              : 
    6130              : 
    6131              : /* Provide an interface between gfortran array descriptors and the F2018:18.4
    6132              :    ISO_Fortran_binding array descriptors. */
    6133              : 
    6134              : static void
    6135         6537 : gfc_conv_gfc_desc_to_cfi_desc (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym)
    6136              : {
    6137         6537 :   stmtblock_t block, block2;
    6138         6537 :   tree cfi, gfc, tmp, tmp2;
    6139         6537 :   tree present = NULL;
    6140         6537 :   tree gfc_strlen = NULL;
    6141         6537 :   tree rank;
    6142         6537 :   gfc_se se;
    6143              : 
    6144         6537 :   if (fsym->attr.optional
    6145         1094 :       && e->expr_type == EXPR_VARIABLE
    6146         1094 :       && e->symtree->n.sym->attr.optional)
    6147          103 :     present = gfc_conv_expr_present (e->symtree->n.sym);
    6148              : 
    6149         6537 :   gfc_init_block (&block);
    6150              : 
    6151              :   /* Convert original argument to a tree. */
    6152         6537 :   gfc_init_se (&se, NULL);
    6153         6537 :   if (e->rank == 0)
    6154              :     {
    6155          687 :       se.want_pointer = 1;
    6156          687 :       gfc_conv_expr (&se, e);
    6157          687 :       gfc = se.expr;
    6158              :     }
    6159              :   else
    6160              :     {
    6161              :       /* If the actual argument can be noncontiguous, copy-in/out is required,
    6162              :          if the dummy has either the CONTIGUOUS attribute or is an assumed-
    6163              :          length assumed-length/assumed-size CHARACTER array.  This only
    6164              :          applies if the actual argument is a "variable"; if it's some
    6165              :          non-lvalue expression, we are going to evaluate it to a
    6166              :          temporary below anyway.  */
    6167         5850 :       se.force_no_tmp = 1;
    6168         5850 :       if ((fsym->attr.contiguous
    6169         4769 :            || (fsym->ts.type == BT_CHARACTER && !fsym->ts.u.cl->length
    6170         1375 :                && (fsym->as->type == AS_ASSUMED_SIZE
    6171          937 :                    || fsym->as->type == AS_EXPLICIT)))
    6172         2023 :           && !gfc_is_simply_contiguous (e, false, true)
    6173         6883 :           && gfc_expr_is_variable (e))
    6174              :         {
    6175         1027 :           bool optional = fsym->attr.optional;
    6176         1027 :           fsym->attr.optional = 0;
    6177         1027 :           gfc_conv_subref_array_arg (&se, e, false, fsym->attr.intent,
    6178         1027 :                                      fsym->attr.pointer, fsym,
    6179         1027 :                                      fsym->ns->proc_name->name, NULL,
    6180              :                                      /* check_contiguous= */ true);
    6181         1027 :           fsym->attr.optional = optional;
    6182              :         }
    6183              :       else
    6184         4823 :         gfc_conv_expr_descriptor (&se, e);
    6185         5850 :       gfc = se.expr;
    6186              :       /* For dt(:)%var the elem_len*stride != sm, hence, GFC uses
    6187              :          elem_len = sizeof(dt) and base_addr = dt(lb) instead.
    6188              :          gfc_get_dataptr_offset fixes the base_addr; for elem_len, see below.
    6189              :          While sm is fine as it uses span*stride and not elem_len.  */
    6190         5850 :       if (POINTER_TYPE_P (TREE_TYPE (gfc)))
    6191         1027 :         gfc = build_fold_indirect_ref_loc (input_location, gfc);
    6192         4823 :       else if (is_subref_array (e) && e->ts.type != BT_CHARACTER)
    6193           12 :          gfc_get_dataptr_offset (&se.pre, gfc, gfc, NULL, true, e);
    6194              :     }
    6195         6537 :   if (e->ts.type == BT_CHARACTER)
    6196              :     {
    6197         3409 :       if (se.string_length)
    6198              :         gfc_strlen = se.string_length;
    6199          883 :       else if (e->ts.u.cl->backend_decl)
    6200              :         gfc_strlen = e->ts.u.cl->backend_decl;
    6201              :       else
    6202            0 :         gcc_unreachable ();
    6203              :     }
    6204         6537 :   gfc_add_block_to_block (&block, &se.pre);
    6205              : 
    6206              :   /* Create array descriptor and set version, rank, attribute, type. */
    6207        12769 :   cfi = gfc_create_var (gfc_get_cfi_type (e->rank < 0
    6208              :                                           ? GFC_MAX_DIMENSIONS : e->rank,
    6209              :                                           false), "cfi");
    6210              :   /* Convert to CFI_cdesc_t, which has dim[] to avoid TBAA issues,*/
    6211         6537 :   if (fsym->attr.dimension && fsym->as->type == AS_ASSUMED_RANK)
    6212              :     {
    6213         2516 :       tmp = gfc_get_cfi_type (-1, !fsym->attr.pointer && !fsym->attr.target);
    6214         2338 :       tmp = build_pointer_type (tmp);
    6215         2338 :       parmse->expr = cfi = gfc_build_addr_expr (tmp, cfi);
    6216         2338 :       cfi = build_fold_indirect_ref_loc (input_location, cfi);
    6217              :     }
    6218              :   else
    6219         4199 :     parmse->expr = gfc_build_addr_expr (NULL, cfi);
    6220              : 
    6221         6537 :   tmp = gfc_get_cfi_desc_version (cfi);
    6222         6537 :   gfc_add_modify (&block, tmp,
    6223         6537 :                   build_int_cst (TREE_TYPE (tmp), CFI_VERSION));
    6224         6537 :   if (e->rank < 0)
    6225          305 :     rank = fold_convert (signed_char_type_node, gfc_conv_descriptor_rank (gfc));
    6226              :   else
    6227         6232 :     rank = build_int_cst (signed_char_type_node, e->rank);
    6228         6537 :   tmp = gfc_get_cfi_desc_rank (cfi);
    6229         6537 :   gfc_add_modify (&block, tmp, rank);
    6230         6537 :   int itype = CFI_type_other;
    6231         6537 :   if (e->ts.f90_type == BT_VOID)
    6232           96 :     itype = (e->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
    6233           96 :              ? CFI_type_cfunptr : CFI_type_cptr);
    6234              :   else
    6235              :     {
    6236         6441 :       if (e->expr_type == EXPR_NULL && e->ts.type == BT_UNKNOWN)
    6237            1 :         e->ts = fsym->ts;
    6238         6441 :       switch (e->ts.type)
    6239              :         {
    6240         2296 :         case BT_INTEGER:
    6241         2296 :         case BT_LOGICAL:
    6242         2296 :         case BT_REAL:
    6243         2296 :         case BT_COMPLEX:
    6244         2296 :           itype = CFI_type_from_type_kind (e->ts.type, e->ts.kind);
    6245         2296 :           break;
    6246         3410 :         case BT_CHARACTER:
    6247         3410 :           itype = CFI_type_from_type_kind (CFI_type_Character, e->ts.kind);
    6248         3410 :           break;
    6249              :         case BT_DERIVED:
    6250         6537 :           itype = CFI_type_struct;
    6251              :           break;
    6252            0 :         case BT_VOID:
    6253            0 :           itype = (e->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
    6254            0 :                    ? CFI_type_cfunptr : CFI_type_cptr);
    6255              :           break;
    6256              :         case BT_ASSUMED:
    6257              :           itype = CFI_type_other;  // FIXME: Or CFI_type_cptr ?
    6258              :           break;
    6259            1 :         case BT_CLASS:
    6260            1 :           if (fsym->ts.type == BT_ASSUMED)
    6261              :             {
    6262              :               // F2017: 7.3.2.2: "An entity that is declared using the TYPE(*)
    6263              :               // type specifier is assumed-type and is an unlimited polymorphic
    6264              :               //  entity." The actual argument _data component is passed.
    6265              :               itype = CFI_type_other;  // FIXME: Or CFI_type_cptr ?
    6266              :               break;
    6267              :             }
    6268              :           else
    6269            0 :             gcc_unreachable ();
    6270              : 
    6271            0 :         case BT_UNSIGNED:
    6272            0 :           gfc_internal_error ("Unsigned not yet implemented");
    6273              : 
    6274            0 :         case BT_PROCEDURE:
    6275            0 :         case BT_HOLLERITH:
    6276            0 :         case BT_UNION:
    6277            0 :         case BT_BOZ:
    6278            0 :         case BT_UNKNOWN:
    6279              :           // FIXME: Really unreachable? Or reachable for type(*) ? If so, CFI_type_other?
    6280            0 :           gcc_unreachable ();
    6281              :         }
    6282              :     }
    6283              : 
    6284         6537 :   tmp = gfc_get_cfi_desc_type (cfi);
    6285         6537 :   gfc_add_modify (&block, tmp,
    6286         6537 :                   build_int_cst (TREE_TYPE (tmp), itype));
    6287              : 
    6288         6537 :   int attr = CFI_attribute_other;
    6289         6537 :   if (fsym->attr.pointer)
    6290              :     attr = CFI_attribute_pointer;
    6291         5774 :   else if (fsym->attr.allocatable)
    6292          433 :     attr = CFI_attribute_allocatable;
    6293         6537 :   tmp = gfc_get_cfi_desc_attribute (cfi);
    6294         6537 :   gfc_add_modify (&block, tmp,
    6295         6537 :                   build_int_cst (TREE_TYPE (tmp), attr));
    6296              : 
    6297              :   /* The cfi-base_addr assignment could be skipped for 'pointer, intent(out)'.
    6298              :      That is very sensible for undefined pointers, but the C code might assume
    6299              :      that the pointer retains the value, in particular, if it was NULL.  */
    6300         6537 :   if (e->rank == 0)
    6301              :     {
    6302          687 :       tmp = gfc_get_cfi_desc_base_addr (cfi);
    6303          687 :       gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), gfc));
    6304              :     }
    6305              :   else
    6306              :     {
    6307         5850 :       tmp = gfc_get_cfi_desc_base_addr (cfi);
    6308         5850 :       tmp2 = gfc_conv_descriptor_data_get (gfc);
    6309         5850 :       gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), tmp2));
    6310              :     }
    6311              : 
    6312              :   /* Set elem_len if known - must be before the next if block.
    6313              :      Note that allocatable implies 'len=:'.  */
    6314         6537 :   if (e->ts.type != BT_ASSUMED && e->ts.type != BT_CHARACTER )
    6315              :     {
    6316              :       /* Length is known at compile time; use 'block' for it.  */
    6317         3073 :       tmp = size_in_bytes (gfc_typenode_for_spec (&e->ts));
    6318         3073 :       tmp2 = gfc_get_cfi_desc_elem_len (cfi);
    6319         3073 :       gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
    6320              :     }
    6321              : 
    6322         6537 :   if (fsym->attr.pointer && fsym->attr.intent == INTENT_OUT)
    6323           91 :     goto done;
    6324              : 
    6325              :   /* When allocatable + intent out, free the cfi descriptor.  */
    6326         6446 :   if (fsym->attr.allocatable && fsym->attr.intent == INTENT_OUT)
    6327              :     {
    6328           90 :       tmp = gfc_get_cfi_desc_base_addr (cfi);
    6329           90 :       tree call = builtin_decl_explicit (BUILT_IN_FREE);
    6330           90 :       call = build_call_expr_loc (input_location, call, 1, tmp);
    6331           90 :       gfc_add_expr_to_block (&block, fold_convert (void_type_node, call));
    6332           90 :       gfc_add_modify (&block, tmp,
    6333           90 :                       fold_convert (TREE_TYPE (tmp), null_pointer_node));
    6334           90 :       goto done;
    6335              :     }
    6336              : 
    6337              :   /* If not unallocated/unassociated. */
    6338         6356 :   gfc_init_block (&block2);
    6339              : 
    6340              :   /* Set elem_len, which may be only known at run time. */
    6341         6356 :   if (e->ts.type == BT_CHARACTER
    6342         3410 :       && (e->expr_type != EXPR_NULL || gfc_strlen != NULL_TREE))
    6343              :     {
    6344         3408 :       gcc_assert (gfc_strlen);
    6345         3409 :       tmp = gfc_strlen;
    6346         3409 :       if (e->ts.kind != 1)
    6347         1117 :         tmp = fold_build2_loc (input_location, MULT_EXPR,
    6348              :                                gfc_charlen_type_node, tmp,
    6349              :                                build_int_cst (gfc_charlen_type_node,
    6350         1117 :                                               e->ts.kind));
    6351         3409 :       tmp2 = gfc_get_cfi_desc_elem_len (cfi);
    6352         3409 :       gfc_add_modify (&block2, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
    6353              :     }
    6354         2947 :   else if (e->ts.type == BT_ASSUMED)
    6355              :     {
    6356           54 :       tmp = gfc_conv_descriptor_elem_len (gfc);
    6357           54 :       tmp2 = gfc_get_cfi_desc_elem_len (cfi);
    6358           54 :       gfc_add_modify (&block2, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
    6359              :     }
    6360              : 
    6361         6356 :   if (e->ts.type == BT_ASSUMED)
    6362              :     {
    6363              :       /* Note: type(*) implies assumed-shape/assumed-rank if fsym requires
    6364              :          an CFI descriptor.  Use the type in the descriptor as it provide
    6365              :          mode information. (Quality of implementation feature.)  */
    6366           54 :       tree cond;
    6367           54 :       tree ctype = gfc_get_cfi_desc_type (cfi);
    6368           54 :       tree type = fold_convert (TREE_TYPE (ctype),
    6369              :                                 gfc_conv_descriptor_type (gfc));
    6370           54 :       tree kind = fold_convert (TREE_TYPE (ctype),
    6371              :                                 gfc_conv_descriptor_elem_len (gfc));
    6372           54 :       kind = fold_build2_loc (input_location, LSHIFT_EXPR, TREE_TYPE (type),
    6373           54 :                               kind, build_int_cst (TREE_TYPE (type),
    6374              :                                                    CFI_type_kind_shift));
    6375              : 
    6376              :       /* if (BT_VOID) CFI_type_cptr else CFI_type_other  */
    6377              :       /* Note: BT_VOID is could also be CFI_type_funcptr, but assume c_ptr. */
    6378           54 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6379           54 :                               build_int_cst (TREE_TYPE (type), BT_VOID));
    6380           54 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, ctype,
    6381           54 :                              build_int_cst (TREE_TYPE (type), CFI_type_cptr));
    6382           54 :       tmp2 = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    6383              :                               ctype,
    6384           54 :                               build_int_cst (TREE_TYPE (type), CFI_type_other));
    6385           54 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    6386              :                               tmp, tmp2);
    6387              :       /* if (BT_DERIVED) CFI_type_struct 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_DERIVED));
    6390           54 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, ctype,
    6391           54 :                              build_int_cst (TREE_TYPE (type), CFI_type_struct));
    6392           54 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    6393              :                               tmp, tmp2);
    6394              :       /* if (BT_CHARACTER) CFI_type_Character + kind=1 else  < tmp2 >  */
    6395              :       /* Note: could also be kind=4, with cfi->elem_len = gfc->elem_len*4.  */
    6396           54 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6397           54 :                               build_int_cst (TREE_TYPE (type), BT_CHARACTER));
    6398           54 :       tmp = build_int_cst (TREE_TYPE (type),
    6399              :                            CFI_type_from_type_kind (CFI_type_Character, 1));
    6400           54 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    6401              :                              ctype, tmp);
    6402           54 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    6403              :                               tmp, tmp2);
    6404              :       /* if (BT_COMPLEX) CFI_type_Complex + kind/2 else  < tmp2 >  */
    6405           54 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6406           54 :                               build_int_cst (TREE_TYPE (type), BT_COMPLEX));
    6407           54 :       tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR, TREE_TYPE (type),
    6408           54 :                              kind, build_int_cst (TREE_TYPE (type), 2));
    6409           54 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (type), tmp,
    6410           54 :                              build_int_cst (TREE_TYPE (type),
    6411              :                                             CFI_type_Complex));
    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              :       /* if (BT_INTEGER || BT_LOGICAL || BT_REAL) type + kind else  <tmp2>  */
    6417           54 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6418           54 :                               build_int_cst (TREE_TYPE (type), BT_INTEGER));
    6419           54 :       tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6420           54 :                               build_int_cst (TREE_TYPE (type), BT_LOGICAL));
    6421           54 :       cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
    6422              :                               cond, tmp);
    6423           54 :       tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6424           54 :                               build_int_cst (TREE_TYPE (type), BT_REAL));
    6425           54 :       cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
    6426              :                               cond, tmp);
    6427           54 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (type),
    6428              :                              type, kind);
    6429           54 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    6430              :                              ctype, tmp);
    6431           54 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    6432              :                               tmp, tmp2);
    6433           54 :       gfc_add_expr_to_block (&block2, tmp2);
    6434              :     }
    6435              : 
    6436         6356 :   if (e->rank != 0)
    6437              :     {
    6438              :       /* Loop: for (i = 0; i < rank; ++i).  */
    6439         5735 :       tree idx = gfc_create_var (TREE_TYPE (rank), "idx");
    6440              :       /* Loop body.  */
    6441         5735 :       stmtblock_t loop_body;
    6442         5735 :       gfc_init_block (&loop_body);
    6443              :       /* cfi->dim[i].lower_bound = (allocatable/pointer)
    6444              :                                    ? gfc->dim[i].lbound : 0 */
    6445         5735 :       if (fsym->attr.pointer || fsym->attr.allocatable)
    6446          648 :         tmp = gfc_conv_descriptor_lbound_get (gfc, idx);
    6447              :       else
    6448         5087 :         tmp = gfc_index_zero_node;
    6449         5735 :       gfc_add_modify (&loop_body, gfc_get_cfi_dim_lbound (cfi, idx), tmp);
    6450              :       /* cfi->dim[i].extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1.  */
    6451         5735 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    6452              :                              gfc_conv_descriptor_ubound_get (gfc, idx),
    6453              :                              gfc_conv_descriptor_lbound_get (gfc, idx));
    6454         5735 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    6455              :                              tmp, gfc_index_one_node);
    6456         5735 :       gfc_add_modify (&loop_body, gfc_get_cfi_dim_extent (cfi, idx), tmp);
    6457              :       /* d->dim[n].sm = gfc->dim[i].stride  * gfc->span); */
    6458         5735 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    6459              :                              gfc_conv_descriptor_stride_get (gfc, idx),
    6460              :                              gfc_conv_descriptor_span_get (gfc));
    6461         5735 :       gfc_add_modify (&loop_body, gfc_get_cfi_dim_sm (cfi, idx), tmp);
    6462              : 
    6463              :       /* Generate loop.  */
    6464        11470 :       gfc_simple_for_loop (&block2, idx, build_int_cst (TREE_TYPE (idx), 0),
    6465         5735 :                            rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
    6466              :                            gfc_finish_block (&loop_body));
    6467              : 
    6468         5735 :       if (e->expr_type == EXPR_VARIABLE
    6469         5573 :           && e->ref
    6470         5573 :           && e->ref->u.ar.type == AR_FULL
    6471         2732 :           && e->symtree->n.sym->attr.dummy
    6472          988 :           && e->symtree->n.sym->as
    6473          988 :           && e->symtree->n.sym->as->type == AS_ASSUMED_SIZE)
    6474              :         {
    6475          138 :           tmp = gfc_get_cfi_dim_extent (cfi, gfc_rank_cst[e->rank-1]),
    6476          138 :           gfc_add_modify (&block2, tmp, build_int_cst (TREE_TYPE (tmp), -1));
    6477              :         }
    6478              :     }
    6479              : 
    6480         6356 :   if (fsym->attr.allocatable || fsym->attr.pointer)
    6481              :     {
    6482         1015 :       tmp = gfc_get_cfi_desc_base_addr (cfi),
    6483         1015 :       tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    6484              :                              tmp, null_pointer_node);
    6485         1015 :       tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
    6486              :                       build_empty_stmt (input_location));
    6487         1015 :       gfc_add_expr_to_block (&block, tmp);
    6488              :     }
    6489              :   else
    6490         5341 :     gfc_add_block_to_block (&block, &block2);
    6491              : 
    6492              : 
    6493         6537 : done:
    6494         6537 :   if (present)
    6495              :     {
    6496          103 :       parmse->expr = build3_loc (input_location, COND_EXPR,
    6497          103 :                                  TREE_TYPE (parmse->expr),
    6498              :                                  present, parmse->expr, null_pointer_node);
    6499          103 :       tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block),
    6500              :                       build_empty_stmt (input_location));
    6501          103 :       gfc_add_expr_to_block (&parmse->pre, tmp);
    6502              :     }
    6503              :   else
    6504         6434 :     gfc_add_block_to_block (&parmse->pre, &block);
    6505              : 
    6506         6537 :   gfc_init_block (&block);
    6507              : 
    6508         6537 :   if ((!fsym->attr.allocatable && !fsym->attr.pointer)
    6509         1196 :       || fsym->attr.intent == INTENT_IN)
    6510         5550 :     goto post_call;
    6511              : 
    6512          987 :   gfc_init_block (&block2);
    6513          987 :   if (e->rank == 0)
    6514              :     {
    6515          428 :       tmp = gfc_get_cfi_desc_base_addr (cfi);
    6516          428 :       gfc_add_modify (&block, gfc, fold_convert (TREE_TYPE (gfc), tmp));
    6517              :     }
    6518              :   else
    6519              :     {
    6520          559 :       tmp = gfc_get_cfi_desc_base_addr (cfi);
    6521          559 :       gfc_conv_descriptor_data_set (&block, gfc, tmp);
    6522              : 
    6523          559 :       if (fsym->attr.allocatable)
    6524              :         {
    6525              :           /* gfc->span = cfi->elem_len.  */
    6526          252 :           tmp = fold_convert (gfc_array_index_type,
    6527              :                               gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]));
    6528              :         }
    6529              :       else
    6530              :         {
    6531              :           /* gfc->span = ((cfi->dim[0].sm % cfi->elem_len)
    6532              :                           ? cfi->dim[0].sm : cfi->elem_len).  */
    6533          307 :           tmp = gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]);
    6534          307 :           tmp2 = fold_convert (gfc_array_index_type,
    6535              :                                gfc_get_cfi_desc_elem_len (cfi));
    6536          307 :           tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR,
    6537              :                                  gfc_array_index_type, tmp, tmp2);
    6538          307 :           tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    6539              :                              tmp, gfc_index_zero_node);
    6540          307 :           tmp = build3_loc (input_location, COND_EXPR, gfc_array_index_type, tmp,
    6541              :                             gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]), tmp2);
    6542              :         }
    6543          559 :       gfc_conv_descriptor_span_set (&block2, gfc, tmp);
    6544              : 
    6545              :       /* Calculate offset + set lbound, ubound and stride.  */
    6546          559 :       gfc_conv_descriptor_offset_set (&block2, gfc, gfc_index_zero_node);
    6547              :       /* Loop: for (i = 0; i < rank; ++i).  */
    6548          559 :       tree idx = gfc_create_var (TREE_TYPE (rank), "idx");
    6549              :       /* Loop body.  */
    6550          559 :       stmtblock_t loop_body;
    6551          559 :       gfc_init_block (&loop_body);
    6552              :       /* gfc->dim[i].lbound = ... */
    6553          559 :       tmp = gfc_get_cfi_dim_lbound (cfi, idx);
    6554          559 :       gfc_conv_descriptor_lbound_set (&loop_body, gfc, idx, tmp);
    6555              : 
    6556              :       /* gfc->dim[i].ubound = gfc->dim[i].lbound + cfi->dim[i].extent - 1. */
    6557          559 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    6558              :                              gfc_conv_descriptor_lbound_get (gfc, idx),
    6559              :                              gfc_index_one_node);
    6560          559 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    6561              :                              gfc_get_cfi_dim_extent (cfi, idx), tmp);
    6562          559 :       gfc_conv_descriptor_ubound_set (&loop_body, gfc, idx, tmp);
    6563              : 
    6564              :       /* gfc->dim[i].stride = cfi->dim[i].sm / cfi>elem_len */
    6565          559 :       tmp = gfc_get_cfi_dim_sm (cfi, idx);
    6566          559 :       tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    6567              :                              gfc_array_index_type, tmp,
    6568              :                              fold_convert (gfc_array_index_type,
    6569              :                                            gfc_get_cfi_desc_elem_len (cfi)));
    6570          559 :       gfc_conv_descriptor_stride_set (&loop_body, gfc, idx, tmp);
    6571              : 
    6572              :       /* gfc->offset -= gfc->dim[i].stride * gfc->dim[i].lbound. */
    6573          559 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    6574              :                              gfc_conv_descriptor_stride_get (gfc, idx),
    6575              :                              gfc_conv_descriptor_lbound_get (gfc, idx));
    6576          559 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    6577              :                              gfc_conv_descriptor_offset_get (gfc), tmp);
    6578          559 :       gfc_conv_descriptor_offset_set (&loop_body, gfc, tmp);
    6579              :       /* Generate loop.  */
    6580         1118 :       gfc_simple_for_loop (&block2, idx, build_int_cst (TREE_TYPE (idx), 0),
    6581          559 :                            rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
    6582              :                            gfc_finish_block (&loop_body));
    6583              :     }
    6584              : 
    6585          987 :   if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
    6586              :     {
    6587           60 :       tmp = fold_convert (gfc_charlen_type_node,
    6588              :                           gfc_get_cfi_desc_elem_len (cfi));
    6589           60 :       if (e->ts.kind != 1)
    6590           24 :         tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    6591              :                                gfc_charlen_type_node, tmp,
    6592              :                                build_int_cst (gfc_charlen_type_node,
    6593           24 :                                               e->ts.kind));
    6594           60 :       gfc_add_modify (&block2, gfc_strlen, tmp);
    6595              :     }
    6596              : 
    6597          987 :   tmp = gfc_get_cfi_desc_base_addr (cfi),
    6598          987 :   tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    6599              :                          tmp, null_pointer_node);
    6600          987 :   tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
    6601              :                   build_empty_stmt (input_location));
    6602          987 :   gfc_add_expr_to_block (&block, tmp);
    6603              : 
    6604         6537 : post_call:
    6605         6537 :   gfc_add_block_to_block (&block, &se.post);
    6606         6537 :   if (present && block.head)
    6607              :     {
    6608            6 :       tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block),
    6609              :                       build_empty_stmt (input_location));
    6610            6 :       gfc_add_expr_to_block (&parmse->post, tmp);
    6611              :     }
    6612         6531 :   else if (block.head)
    6613         1564 :     gfc_add_block_to_block (&parmse->post, &block);
    6614         6537 : }
    6615              : 
    6616              : 
    6617              : /* Create "conditional temporary" to handle scalar dummy variables with the
    6618              :    OPTIONAL+VALUE attribute that shall not be dereferenced.  Use null value
    6619              :    as fallback.  Does not handle CLASS.  */
    6620              : 
    6621              : static void
    6622          234 : conv_cond_temp (gfc_se * parmse, gfc_expr * e, tree cond)
    6623              : {
    6624          234 :   tree temp;
    6625          234 :   gcc_assert (e && e->ts.type != BT_CLASS);
    6626          234 :   gcc_assert (e->rank == 0);
    6627          234 :   temp = gfc_create_var (TREE_TYPE (parmse->expr), "condtemp");
    6628          234 :   TREE_STATIC (temp) = 1;
    6629          234 :   TREE_CONSTANT (temp) = 1;
    6630          234 :   TREE_READONLY (temp) = 1;
    6631          234 :   DECL_INITIAL (temp) = build_zero_cst (TREE_TYPE (temp));
    6632          234 :   parmse->expr = fold_build3_loc (input_location, COND_EXPR,
    6633          234 :                                   TREE_TYPE (parmse->expr),
    6634              :                                   cond, parmse->expr, temp);
    6635          234 :   parmse->expr = gfc_evaluate_now (parmse->expr, &parmse->pre);
    6636          234 : }
    6637              : 
    6638              : 
    6639              : /* Returns true if the type specified in TS is a character type whose length
    6640              :    is constant.  Otherwise returns false.  */
    6641              : 
    6642              : static bool
    6643        22016 : gfc_const_length_character_type_p (gfc_typespec *ts)
    6644              : {
    6645        22016 :   return (ts->type == BT_CHARACTER
    6646          467 :           && ts->u.cl
    6647          467 :           && ts->u.cl->length
    6648          467 :           && ts->u.cl->length->expr_type == EXPR_CONSTANT
    6649        22483 :           && ts->u.cl->length->ts.type == BT_INTEGER);
    6650              : }
    6651              : 
    6652              : 
    6653              : /* Helper function for the handling of (currently) scalar dummy variables
    6654              :    with the VALUE attribute.  Argument parmse should already be set up.  */
    6655              : static void
    6656        22449 : conv_dummy_value (gfc_se * parmse, gfc_expr * e, gfc_symbol * fsym,
    6657              :                   vec<tree, va_gc> *& optionalargs)
    6658              : {
    6659        22449 :   tree tmp;
    6660              : 
    6661        22449 :   gcc_assert (fsym && fsym->attr.value && !fsym->attr.dimension);
    6662              : 
    6663        22449 :   if (IS_PDT (e))
    6664              :     {
    6665            6 :       tmp = gfc_create_var (TREE_TYPE (parmse->expr), "PDT");
    6666            6 :       gfc_add_modify (&parmse->pre, tmp, parmse->expr);
    6667            6 :       gfc_add_expr_to_block (&parmse->pre,
    6668            6 :                              gfc_copy_alloc_comp (e->ts.u.derived,
    6669              :                                                   parmse->expr, tmp,
    6670              :                                                   e->rank, 0));
    6671            6 :       parmse->expr = tmp;
    6672            6 :       tmp = gfc_deallocate_pdt_comp (e->ts.u.derived, tmp, e->rank);
    6673            6 :       gfc_add_expr_to_block (&parmse->post, tmp);
    6674            6 :       return;
    6675              :     }
    6676              : 
    6677              :   /* Absent actual argument for optional scalar dummy.  */
    6678        22443 :   if ((e == NULL || e->expr_type == EXPR_NULL) && fsym->attr.optional)
    6679              :     {
    6680              :       /* For scalar arguments with VALUE attribute which are passed by
    6681              :          value, pass "0" and a hidden argument for the optional status.  */
    6682          427 :       if (fsym->ts.type == BT_CHARACTER)
    6683              :         {
    6684              :           /* Pass a NULL pointer for an absent CHARACTER arg and a length of
    6685              :              zero.  */
    6686           90 :           parmse->expr = null_pointer_node;
    6687           90 :           parmse->string_length = build_int_cst (gfc_charlen_type_node, 0);
    6688              :         }
    6689          337 :       else if (gfc_bt_struct (fsym->ts.type)
    6690           30 :                && !(fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING))
    6691              :         {
    6692              :           /* Pass null struct.  Types c_ptr and c_funptr from ISO_C_BINDING
    6693              :              are pointers and passed as such below.  */
    6694           24 :           tree temp = gfc_create_var (gfc_sym_type (fsym), "absent");
    6695           24 :           TREE_CONSTANT (temp) = 1;
    6696           24 :           TREE_READONLY (temp) = 1;
    6697           24 :           DECL_INITIAL (temp) = build_zero_cst (TREE_TYPE (temp));
    6698           24 :           parmse->expr = temp;
    6699           24 :         }
    6700              :       else
    6701          313 :         parmse->expr = fold_convert (gfc_sym_type (fsym),
    6702              :                                      integer_zero_node);
    6703          427 :       vec_safe_push (optionalargs, boolean_false_node);
    6704              : 
    6705          427 :       return;
    6706              :     }
    6707              : 
    6708              :   /* Truncate a too long constant character actual argument.  */
    6709        22016 :   if (gfc_const_length_character_type_p (&fsym->ts)
    6710          467 :       && e->expr_type == EXPR_CONSTANT
    6711        22099 :       && mpz_cmp_ui (fsym->ts.u.cl->length->value.integer,
    6712              :                      e->value.character.length) < 0)
    6713              :     {
    6714           17 :       gfc_charlen_t flen = mpz_get_ui (fsym->ts.u.cl->length->value.integer);
    6715              : 
    6716              :       /* Truncate actual string argument.  */
    6717           17 :       gfc_conv_expr (parmse, e);
    6718           34 :       parmse->expr = gfc_build_wide_string_const (e->ts.kind, flen,
    6719           17 :                                                   e->value.character.string);
    6720           17 :       parmse->string_length = build_int_cst (gfc_charlen_type_node, flen);
    6721              : 
    6722           17 :       if (flen == 1)
    6723              :         {
    6724           14 :           tree slen1 = build_int_cst (gfc_charlen_type_node, 1);
    6725           14 :           gfc_conv_string_parameter (parmse);
    6726           14 :           parmse->expr = gfc_string_to_single_character (slen1, parmse->expr,
    6727              :                                                          e->ts.kind);
    6728              :         }
    6729              : 
    6730              :       /* Indicate value,optional scalar dummy argument as present.  */
    6731           17 :       if (fsym->attr.optional)
    6732            1 :         vec_safe_push (optionalargs, boolean_true_node);
    6733           17 :       return;
    6734              :     }
    6735              : 
    6736              :   /* gfortran argument passing conventions:
    6737              :      actual arguments to CHARACTER(len=1),VALUE
    6738              :      dummy arguments are actually passed by value.
    6739              :      Strings are truncated to length 1.  */
    6740        21999 :   if (gfc_length_one_character_type_p (&fsym->ts))
    6741              :     {
    6742          378 :       if (e->expr_type == EXPR_CONSTANT
    6743           54 :           && e->value.character.length > 1)
    6744              :         {
    6745            0 :           e->value.character.length = 1;
    6746            0 :           gfc_conv_expr (parmse, e);
    6747              :         }
    6748              : 
    6749          378 :       tree slen1 = build_int_cst (gfc_charlen_type_node, 1);
    6750          378 :       gfc_conv_string_parameter (parmse);
    6751          378 :       parmse->expr = gfc_string_to_single_character (slen1, parmse->expr,
    6752              :                                                      e->ts.kind);
    6753              :       /* Truncate resulting string to length 1.  */
    6754          378 :       parmse->string_length = slen1;
    6755              :     }
    6756              : 
    6757        21999 :   if (fsym->attr.optional && fsym->ts.type != BT_CLASS)
    6758              :     {
    6759              :       /* F2018:15.5.2.12 Argument presence and
    6760              :          restrictions on arguments not present.  */
    6761          823 :       if (e->expr_type == EXPR_VARIABLE
    6762          650 :           && e->rank == 0
    6763         1419 :           && (gfc_expr_attr (e).allocatable
    6764          482 :               || gfc_expr_attr (e).pointer))
    6765              :         {
    6766          198 :           gfc_se argse;
    6767          198 :           tree cond;
    6768          198 :           gfc_init_se (&argse, NULL);
    6769          198 :           argse.want_pointer = 1;
    6770          198 :           gfc_conv_expr (&argse, e);
    6771          198 :           cond = fold_convert (TREE_TYPE (argse.expr), null_pointer_node);
    6772          198 :           cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    6773              :                                   argse.expr, cond);
    6774          198 :           if (e->symtree->n.sym->attr.dummy)
    6775           24 :             cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    6776              :                                     logical_type_node,
    6777              :                                     gfc_conv_expr_present (e->symtree->n.sym),
    6778              :                                     cond);
    6779          198 :           vec_safe_push (optionalargs, fold_convert (boolean_type_node, cond));
    6780              :           /* Create "conditional temporary".  */
    6781          198 :           conv_cond_temp (parmse, e, cond);
    6782              :         }
    6783          625 :       else if (e->expr_type != EXPR_VARIABLE
    6784          452 :                || !e->symtree->n.sym->attr.optional
    6785          260 :                || (e->ref != NULL && e->ref->type != REF_ARRAY))
    6786          365 :         vec_safe_push (optionalargs, boolean_true_node);
    6787              :       else
    6788              :         {
    6789          260 :           tmp = gfc_conv_expr_present (e->symtree->n.sym);
    6790          260 :           if (gfc_bt_struct (fsym->ts.type)
    6791           36 :               && !(fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING))
    6792           36 :             conv_cond_temp (parmse, e, tmp);
    6793          224 :           else if (e->ts.type != BT_CHARACTER && !e->symtree->n.sym->attr.value)
    6794           84 :             parmse->expr
    6795          168 :               = fold_build3_loc (input_location, COND_EXPR,
    6796           84 :                                  TREE_TYPE (parmse->expr),
    6797              :                                  tmp, parmse->expr,
    6798           84 :                                  fold_convert (TREE_TYPE (parmse->expr),
    6799              :                                                integer_zero_node));
    6800              : 
    6801          520 :           vec_safe_push (optionalargs,
    6802          260 :                          fold_convert (boolean_type_node, tmp));
    6803              :         }
    6804              :     }
    6805              : }
    6806              : 
    6807              : 
    6808              : /* Helper function for the handling of NULL() actual arguments associated with
    6809              :    non-optional dummy variables.  Argument parmse should already be set up.  */
    6810              : static void
    6811          426 : conv_null_actual (gfc_se * parmse, gfc_expr * e, gfc_symbol * fsym)
    6812              : {
    6813          426 :   gcc_assert (fsym && e->expr_type == EXPR_NULL);
    6814              : 
    6815              :   /* Obtain the character length for a NULL() actual with a character
    6816              :      MOLD argument.  Otherwise substitute a suitable dummy length.
    6817              :      Here we handle only non-optional dummies of non-bind(c) procedures.  */
    6818          426 :   if (fsym->ts.type == BT_CHARACTER)
    6819              :     {
    6820          216 :       if (e->ts.type == BT_CHARACTER
    6821          162 :           && e->symtree->n.sym->ts.type == BT_CHARACTER)
    6822              :         {
    6823              :           /* MOLD is present.  Substitute a temporary character NULL pointer.
    6824              :              For an assumed-rank dummy we need a descriptor that passes the
    6825              :              correct rank.  */
    6826          162 :           if (fsym->as && fsym->as->type == AS_ASSUMED_RANK)
    6827              :             {
    6828           54 :               tree rank;
    6829           54 :               tree tmp = parmse->expr;
    6830           54 :               tmp = gfc_conv_scalar_to_descriptor (parmse, tmp, fsym->attr);
    6831           54 :               rank = gfc_conv_descriptor_rank (tmp);
    6832           54 :               gfc_add_modify (&parmse->pre, rank,
    6833           54 :                               build_int_cst (TREE_TYPE (rank), e->rank));
    6834           54 :               parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
    6835           54 :             }
    6836              :           else
    6837              :             {
    6838          108 :               tree tmp = gfc_create_var (TREE_TYPE (parmse->expr), "null");
    6839          108 :               gfc_add_modify (&parmse->pre, tmp,
    6840          108 :                               build_zero_cst (TREE_TYPE (tmp)));
    6841          108 :               parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
    6842              :             }
    6843              : 
    6844              :           /* Ensure that a usable length is available.  */
    6845          162 :           if (parmse->string_length == NULL_TREE)
    6846              :             {
    6847          162 :               gfc_typespec *ts = &e->symtree->n.sym->ts;
    6848              : 
    6849          162 :               if (ts->u.cl->length != NULL
    6850          108 :                   && ts->u.cl->length->expr_type == EXPR_CONSTANT)
    6851          108 :                 gfc_conv_const_charlen (ts->u.cl);
    6852              : 
    6853          162 :               if (ts->u.cl->backend_decl)
    6854          162 :                 parmse->string_length = ts->u.cl->backend_decl;
    6855              :             }
    6856              :         }
    6857           54 :       else if (e->ts.type == BT_UNKNOWN && parmse->string_length == NULL_TREE)
    6858              :         {
    6859              :           /* MOLD is not present.  Pass length of associated dummy character
    6860              :              argument if constant, or zero.  */
    6861           54 :           if (fsym->ts.u.cl->length != NULL
    6862           18 :               && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
    6863              :             {
    6864           18 :               gfc_conv_const_charlen (fsym->ts.u.cl);
    6865           18 :               parmse->string_length = fsym->ts.u.cl->backend_decl;
    6866              :             }
    6867              :           else
    6868              :             {
    6869           36 :               parmse->string_length = gfc_create_var (gfc_charlen_type_node,
    6870              :                                                       "slen");
    6871           36 :               gfc_add_modify (&parmse->pre, parmse->string_length,
    6872              :                               build_zero_cst (gfc_charlen_type_node));
    6873              :             }
    6874              :         }
    6875              :     }
    6876          210 :   else if (fsym->ts.type == BT_DERIVED)
    6877              :     {
    6878          210 :       if (e->ts.type != BT_UNKNOWN)
    6879              :         /* MOLD is present.  Pass a corresponding temporary NULL pointer.
    6880              :            For an assumed-rank dummy we provide a descriptor that passes
    6881              :            the correct rank.  */
    6882              :         {
    6883          138 :           tree rank;
    6884          138 :           tree tmp = parmse->expr;
    6885              : 
    6886          138 :           tmp = gfc_conv_scalar_to_descriptor (parmse, tmp, gfc_expr_attr (e));
    6887          138 :           rank = gfc_conv_descriptor_rank (tmp);
    6888          138 :           gfc_add_modify (&parmse->pre, rank,
    6889          138 :                           build_int_cst (TREE_TYPE (rank), e->rank));
    6890          138 :           gfc_conv_descriptor_data_set (&parmse->pre, tmp, null_pointer_node);
    6891          138 :           parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
    6892              :         }
    6893              :       else
    6894              :         /* MOLD is not present.  Use attributes from dummy argument, which is
    6895              :            not allowed to be assumed-rank.  */
    6896              :         {
    6897           72 :           int dummy_rank;
    6898           72 :           tree tmp = parmse->expr;
    6899              : 
    6900           72 :           if ((fsym->attr.allocatable || fsym->attr.pointer)
    6901           72 :               && fsym->attr.intent == INTENT_UNKNOWN)
    6902           36 :             fsym->attr.intent = INTENT_IN;
    6903           72 :           tmp = gfc_conv_scalar_to_descriptor (parmse, tmp, fsym->attr);
    6904           72 :           dummy_rank = fsym->as ? fsym->as->rank : 0;
    6905           24 :           if (dummy_rank > 0)
    6906              :             {
    6907           24 :               tree rank = gfc_conv_descriptor_rank (tmp);
    6908           24 :               gfc_add_modify (&parmse->pre, rank,
    6909           24 :                               build_int_cst (TREE_TYPE (rank), dummy_rank));
    6910              :             }
    6911           72 :           gfc_conv_descriptor_data_set (&parmse->pre, tmp, null_pointer_node);
    6912           72 :           parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
    6913              :         }
    6914              :     }
    6915          426 : }
    6916              : 
    6917              : 
    6918              : /* Generate code for a procedure call.  Note can return se->post != NULL.
    6919              :    If se->direct_byref is set then se->expr contains the return parameter.
    6920              :    Return nonzero, if the call has alternate specifiers.
    6921              :    'expr' is only needed for procedure pointer components.  */
    6922              : 
    6923              : int
    6924       135709 : gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
    6925              :                          gfc_actual_arglist * args, gfc_expr * expr,
    6926              :                          vec<tree, va_gc> *append_args)
    6927              : {
    6928       135709 :   gfc_interface_mapping mapping;
    6929       135709 :   vec<tree, va_gc> *arglist;
    6930       135709 :   vec<tree, va_gc> *retargs;
    6931       135709 :   tree tmp;
    6932       135709 :   tree fntype;
    6933       135709 :   gfc_se parmse;
    6934       135709 :   gfc_array_info *info;
    6935       135709 :   int byref;
    6936       135709 :   int parm_kind;
    6937       135709 :   tree type;
    6938       135709 :   tree var;
    6939       135709 :   tree len;
    6940       135709 :   tree base_object;
    6941       135709 :   vec<tree, va_gc> *stringargs;
    6942       135709 :   vec<tree, va_gc> *optionalargs;
    6943       135709 :   tree result = NULL;
    6944       135709 :   gfc_formal_arglist *formal;
    6945       135709 :   gfc_actual_arglist *arg;
    6946       135709 :   int has_alternate_specifier = 0;
    6947       135709 :   bool need_interface_mapping;
    6948       135709 :   bool is_builtin;
    6949       135709 :   bool callee_alloc;
    6950       135709 :   bool ulim_copy;
    6951       135709 :   gfc_typespec ts;
    6952       135709 :   gfc_charlen cl;
    6953       135709 :   gfc_expr *e;
    6954       135709 :   gfc_symbol *fsym;
    6955       135709 :   enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
    6956       135709 :   gfc_component *comp = NULL;
    6957       135709 :   int arglen;
    6958       135709 :   unsigned int argc;
    6959       135709 :   tree arg1_cntnr = NULL_TREE;
    6960       135709 :   arglist = NULL;
    6961       135709 :   retargs = NULL;
    6962       135709 :   stringargs = NULL;
    6963       135709 :   optionalargs = NULL;
    6964       135709 :   var = NULL_TREE;
    6965       135709 :   len = NULL_TREE;
    6966       135709 :   gfc_clear_ts (&ts);
    6967       135709 :   gfc_intrinsic_sym *isym = expr && expr->rank ?
    6968              :                             expr->value.function.isym : NULL;
    6969              : 
    6970       135709 :   comp = gfc_get_proc_ptr_comp (expr);
    6971              : 
    6972       271418 :   bool elemental_proc = (comp
    6973         2029 :                          && comp->ts.interface
    6974         1975 :                          && comp->ts.interface->attr.elemental)
    6975         1830 :                         || (comp && comp->attr.elemental)
    6976       137539 :                         || sym->attr.elemental;
    6977              : 
    6978       135709 :   if (se->ss != NULL)
    6979              :     {
    6980        25010 :       if (!elemental_proc)
    6981              :         {
    6982        21457 :           gcc_assert (se->ss->info->type == GFC_SS_FUNCTION);
    6983        21457 :           if (se->ss->info->useflags)
    6984              :             {
    6985         5766 :               gcc_assert ((!comp && gfc_return_by_reference (sym)
    6986              :                            && sym->result->attr.dimension)
    6987              :                           || (comp && comp->attr.dimension)
    6988              :                           || gfc_is_class_array_function (expr));
    6989         5766 :               gcc_assert (se->loop != NULL);
    6990              :               /* Access the previously obtained result.  */
    6991         5766 :               gfc_conv_tmp_array_ref (se);
    6992         5766 :               return 0;
    6993              :             }
    6994              :         }
    6995        19244 :       info = &se->ss->info->data.array;
    6996              :     }
    6997              :   else
    6998              :     info = NULL;
    6999              : 
    7000       129943 :   stmtblock_t post, clobbers, dealloc_blk;
    7001       129943 :   gfc_init_block (&post);
    7002       129943 :   gfc_init_block (&clobbers);
    7003       129943 :   gfc_init_block (&dealloc_blk);
    7004       129943 :   gfc_init_interface_mapping (&mapping);
    7005       129943 :   if (!comp)
    7006              :     {
    7007       127963 :       formal = gfc_sym_get_dummy_args (sym);
    7008       127963 :       need_interface_mapping = sym->attr.dimension ||
    7009       112524 :                                (sym->ts.type == BT_CHARACTER
    7010         3166 :                                 && sym->ts.u.cl->length
    7011         2427 :                                 && sym->ts.u.cl->length->expr_type
    7012              :                                    != EXPR_CONSTANT);
    7013              :     }
    7014              :   else
    7015              :     {
    7016         1980 :       formal = comp->ts.interface ? comp->ts.interface->formal : NULL;
    7017         1980 :       need_interface_mapping = comp->attr.dimension ||
    7018         1911 :                                (comp->ts.type == BT_CHARACTER
    7019          229 :                                 && comp->ts.u.cl->length
    7020          220 :                                 && comp->ts.u.cl->length->expr_type
    7021              :                                    != EXPR_CONSTANT);
    7022              :     }
    7023              : 
    7024       129943 :   base_object = NULL_TREE;
    7025              :   /* For _vprt->_copy () routines no formal symbol is present.  Nevertheless
    7026              :      is the third and fourth argument to such a function call a value
    7027              :      denoting the number of elements to copy (i.e., most of the time the
    7028              :      length of a deferred length string).  */
    7029       259886 :   ulim_copy = (formal == NULL)
    7030        31669 :                && UNLIMITED_POLY (sym)
    7031       130023 :                && comp && (strcmp ("_copy", comp->name) == 0);
    7032              : 
    7033              :   /* Scan for allocatable actual arguments passed to allocatable dummy
    7034              :      arguments with INTENT(OUT).  As the corresponding actual arguments are
    7035              :      deallocated before execution of the procedure, we evaluate actual
    7036              :      argument expressions to avoid problems with possible dependencies.  */
    7037       129943 :   bool force_eval_args = false;
    7038       129943 :   gfc_formal_arglist *tmp_formal;
    7039       399783 :   for (arg = args, tmp_formal = formal; arg != NULL;
    7040       236546 :        arg = arg->next, tmp_formal = tmp_formal ? tmp_formal->next : NULL)
    7041              :     {
    7042       270340 :       e = arg->expr;
    7043       270340 :       fsym = tmp_formal ? tmp_formal->sym : NULL;
    7044       256954 :       if (e && fsym
    7045       225087 :           && e->expr_type == EXPR_VARIABLE
    7046        98921 :           && fsym->attr.intent == INTENT_OUT
    7047         6305 :           && (fsym->ts.type == BT_CLASS && fsym->attr.class_ok
    7048         6305 :               ? CLASS_DATA (fsym)->attr.allocatable
    7049         4777 :               : fsym->attr.allocatable)
    7050          500 :           && e->symtree
    7051          500 :           && e->symtree->n.sym
    7052       527294 :           && gfc_variable_attr (e, NULL).allocatable)
    7053              :         {
    7054              :           force_eval_args = true;
    7055              :           break;
    7056              :         }
    7057              :     }
    7058              : 
    7059              :   /* Evaluate the arguments.  */
    7060       400685 :   for (arg = args, argc = 0; arg != NULL;
    7061       270742 :        arg = arg->next, formal = formal ? formal->next : NULL, ++argc)
    7062              :     {
    7063       270742 :       bool finalized = false;
    7064       270742 :       tree derived_array = NULL_TREE;
    7065       270742 :       symbol_attribute *attr;
    7066              : 
    7067       270742 :       e = arg->expr;
    7068       270742 :       fsym = formal ? formal->sym : NULL;
    7069       508190 :       parm_kind = MISSING;
    7070              : 
    7071       237448 :       attr = fsym ? &(fsym->ts.type == BT_CLASS ? CLASS_DATA (fsym)->attr
    7072              :                                                 : fsym->attr)
    7073              :                   : nullptr;
    7074              :       /* If the procedure requires an explicit interface, the actual
    7075              :          argument is passed according to the corresponding formal
    7076              :          argument.  If the corresponding formal argument is a POINTER,
    7077              :          ALLOCATABLE or assumed shape, we do not use g77's calling
    7078              :          convention, and pass the address of the array descriptor
    7079              :          instead.  Otherwise we use g77's calling convention, in other words
    7080              :          pass the array data pointer without descriptor.  */
    7081       237395 :       bool nodesc_arg = fsym != NULL
    7082       237395 :                         && !(fsym->attr.pointer || fsym->attr.allocatable)
    7083       228311 :                         && fsym->as
    7084        40534 :                         && fsym->as->type != AS_ASSUMED_SHAPE
    7085        24686 :                         && fsym->as->type != AS_ASSUMED_RANK;
    7086       270742 :       if (comp)
    7087         2733 :         nodesc_arg = nodesc_arg || !comp->attr.always_explicit;
    7088              :       else
    7089       268009 :         nodesc_arg
    7090              :           = nodesc_arg
    7091       268009 :             || !(sym->attr.always_explicit || (attr && attr->codimension));
    7092              : 
    7093              :       /* Class array expressions are sometimes coming completely unadorned
    7094              :          with either arrayspec or _data component.  Correct that here.
    7095              :          OOP-TODO: Move this to the frontend.  */
    7096       270742 :       if (e && e->expr_type == EXPR_VARIABLE
    7097       113010 :             && !e->ref
    7098        51460 :             && e->ts.type == BT_CLASS
    7099         2603 :             && (CLASS_DATA (e)->attr.codimension
    7100         2603 :                 || CLASS_DATA (e)->attr.dimension))
    7101              :         {
    7102            0 :           gfc_typespec temp_ts = e->ts;
    7103            0 :           gfc_add_class_array_ref (e);
    7104            0 :           e->ts = temp_ts;
    7105              :         }
    7106              : 
    7107       270742 :       if (e == NULL
    7108       257350 :           || (e->expr_type == EXPR_NULL
    7109          745 :               && fsym
    7110          745 :               && fsym->attr.value
    7111           72 :               && fsym->attr.optional
    7112           72 :               && !fsym->attr.dimension
    7113           72 :               && fsym->ts.type != BT_CLASS))
    7114              :         {
    7115        13464 :           if (se->ignore_optional)
    7116              :             {
    7117              :               /* Some intrinsics have already been resolved to the correct
    7118              :                  parameters.  */
    7119          632 :               continue;
    7120              :             }
    7121        13266 :           else if (arg->label)
    7122              :             {
    7123          224 :               has_alternate_specifier = 1;
    7124          224 :               continue;
    7125              :             }
    7126              :           else
    7127              :             {
    7128        13042 :               gfc_init_se (&parmse, NULL);
    7129              : 
    7130              :               /* For scalar arguments with VALUE attribute which are passed by
    7131              :                  value, pass "0" and a hidden argument gives the optional
    7132              :                  status.  */
    7133        13042 :               if (fsym && fsym->attr.optional && fsym->attr.value
    7134          427 :                   && !fsym->attr.dimension && fsym->ts.type != BT_CLASS)
    7135              :                 {
    7136          427 :                   conv_dummy_value (&parmse, e, fsym, optionalargs);
    7137              :                 }
    7138              :               else
    7139              :                 {
    7140              :                   /* Pass a NULL pointer for an absent arg.  */
    7141        12615 :                   parmse.expr = null_pointer_node;
    7142              : 
    7143              :                   /* Is it an absent character dummy?  */
    7144        12615 :                   bool absent_char = false;
    7145        12615 :                   gfc_dummy_arg * const dummy_arg = arg->associated_dummy;
    7146              : 
    7147              :                   /* Fall back to inferred type only if no formal.  */
    7148        12615 :                   if (fsym)
    7149        11557 :                     absent_char = (fsym->ts.type == BT_CHARACTER);
    7150         1058 :                   else if (dummy_arg)
    7151         1058 :                     absent_char = (gfc_dummy_arg_get_typespec (*dummy_arg).type
    7152              :                                    == BT_CHARACTER);
    7153        12615 :                   if (absent_char)
    7154         1115 :                     parmse.string_length = build_int_cst (gfc_charlen_type_node,
    7155              :                                                           0);
    7156              :                 }
    7157              :             }
    7158              :         }
    7159       257278 :       else if (e->expr_type == EXPR_NULL
    7160          673 :                && (e->ts.type == BT_UNKNOWN || e->ts.type == BT_DERIVED)
    7161          371 :                && fsym && attr && (attr->pointer || attr->allocatable)
    7162          293 :                && fsym->ts.type == BT_DERIVED)
    7163              :         {
    7164          210 :           gfc_init_se (&parmse, NULL);
    7165          210 :           gfc_conv_expr_reference (&parmse, e);
    7166          210 :           conv_null_actual (&parmse, e, fsym);
    7167              :         }
    7168       257068 :       else if (arg->expr->expr_type == EXPR_NULL
    7169          463 :                && fsym && !fsym->attr.pointer
    7170          163 :                && (fsym->ts.type != BT_CLASS
    7171            6 :                    || !CLASS_DATA (fsym)->attr.class_pointer))
    7172              :         {
    7173              :           /* Pass a NULL pointer to denote an absent arg.  */
    7174          163 :           gcc_assert (fsym->attr.optional && !fsym->attr.allocatable
    7175              :                       && (fsym->ts.type != BT_CLASS
    7176              :                           || !CLASS_DATA (fsym)->attr.allocatable));
    7177          163 :           gfc_init_se (&parmse, NULL);
    7178          163 :           parmse.expr = null_pointer_node;
    7179          163 :           if (fsym->ts.type == BT_CHARACTER)
    7180           42 :             parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
    7181              :         }
    7182       256905 :       else if (fsym && fsym->ts.type == BT_CLASS
    7183        11126 :                  && e->ts.type == BT_DERIVED)
    7184              :         {
    7185              :           /* The derived type needs to be converted to a temporary
    7186              :              CLASS object.  */
    7187         4613 :           gfc_init_se (&parmse, se);
    7188         4613 :           gfc_conv_derived_to_class (&parmse, e, fsym, NULL_TREE,
    7189         4613 :                                      fsym->attr.optional
    7190         1008 :                                        && e->expr_type == EXPR_VARIABLE
    7191         5621 :                                        && e->symtree->n.sym->attr.optional,
    7192         4613 :                                      CLASS_DATA (fsym)->attr.class_pointer
    7193         4613 :                                        || CLASS_DATA (fsym)->attr.allocatable,
    7194              :                                      sym->name, &derived_array);
    7195              :         }
    7196       220425 :       else if (UNLIMITED_POLY (fsym) && e->ts.type != BT_CLASS
    7197          906 :                && e->ts.type != BT_PROCEDURE
    7198          882 :                && (gfc_expr_attr (e).flavor != FL_PROCEDURE
    7199           12 :                    || gfc_expr_attr (e).proc != PROC_UNKNOWN))
    7200              :         {
    7201              :           /* The intrinsic type needs to be converted to a temporary
    7202              :              CLASS object for the unlimited polymorphic formal.  */
    7203          882 :           gfc_find_vtab (&e->ts);
    7204          882 :           gfc_init_se (&parmse, se);
    7205          882 :           gfc_conv_intrinsic_to_class (&parmse, e, fsym->ts);
    7206              : 
    7207              :         }
    7208       251410 :       else if (se->ss && se->ss->info->useflags)
    7209              :         {
    7210         5831 :           gfc_ss *ss;
    7211              : 
    7212         5831 :           ss = se->ss;
    7213              : 
    7214              :           /* An elemental function inside a scalarized loop.  */
    7215         5831 :           gfc_init_se (&parmse, se);
    7216         5831 :           parm_kind = ELEMENTAL;
    7217              : 
    7218              :           /* When no fsym is present, ulim_copy is set and this is a third or
    7219              :              fourth argument, use call-by-value instead of by reference to
    7220              :              hand the length properties to the copy routine (i.e., most of the
    7221              :              time this will be a call to a __copy_character_* routine where the
    7222              :              third and fourth arguments are the lengths of a deferred length
    7223              :              char array).  */
    7224         5831 :           if ((fsym && fsym->attr.value)
    7225         5597 :               || (ulim_copy && (argc == 2 || argc == 3)))
    7226          234 :             gfc_conv_expr (&parmse, e);
    7227         5597 :           else if (e->expr_type == EXPR_ARRAY)
    7228              :             {
    7229          306 :               gfc_conv_expr (&parmse, e);
    7230          306 :               if (e->ts.type != BT_CHARACTER)
    7231          263 :                 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
    7232              :             }
    7233              :           else
    7234         5291 :             gfc_conv_expr_reference (&parmse, e);
    7235              : 
    7236         5831 :           if (e->ts.type == BT_CHARACTER && !e->rank
    7237          174 :               && e->expr_type == EXPR_FUNCTION)
    7238           12 :             parmse.expr = build_fold_indirect_ref_loc (input_location,
    7239              :                                                        parmse.expr);
    7240              : 
    7241         5781 :           if (fsym && fsym->ts.type == BT_DERIVED
    7242         7447 :               && gfc_is_class_container_ref (e))
    7243              :             {
    7244           24 :               parmse.expr = gfc_class_data_get (parmse.expr);
    7245              : 
    7246           24 :               if (fsym->attr.optional && e->expr_type == EXPR_VARIABLE
    7247           24 :                   && e->symtree->n.sym->attr.optional)
    7248              :                 {
    7249            0 :                   tree cond = gfc_conv_expr_present (e->symtree->n.sym);
    7250            0 :                   parmse.expr = build3_loc (input_location, COND_EXPR,
    7251            0 :                                         TREE_TYPE (parmse.expr),
    7252              :                                         cond, parmse.expr,
    7253            0 :                                         fold_convert (TREE_TYPE (parmse.expr),
    7254              :                                                       null_pointer_node));
    7255              :                 }
    7256              :             }
    7257              : 
    7258              :           /* Scalar dummy arguments of intrinsic type or derived type with
    7259              :              VALUE attribute.  */
    7260         5831 :           if (fsym
    7261         5781 :               && fsym->attr.value
    7262          234 :               && fsym->ts.type != BT_CLASS)
    7263          234 :             conv_dummy_value (&parmse, e, fsym, optionalargs);
    7264              : 
    7265              :           /* If we are passing an absent array as optional dummy to an
    7266              :              elemental procedure, make sure that we pass NULL when the data
    7267              :              pointer is NULL.  We need this extra conditional because of
    7268              :              scalarization which passes arrays elements to the procedure,
    7269              :              ignoring the fact that the array can be absent/unallocated/...  */
    7270         5597 :           else if (ss->info->can_be_null_ref
    7271          415 :                    && ss->info->type != GFC_SS_REFERENCE)
    7272              :             {
    7273          193 :               tree descriptor_data;
    7274              : 
    7275          193 :               descriptor_data = ss->info->data.array.data;
    7276          193 :               tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    7277              :                                      descriptor_data,
    7278          193 :                                      fold_convert (TREE_TYPE (descriptor_data),
    7279              :                                                    null_pointer_node));
    7280          193 :               parmse.expr
    7281          386 :                 = fold_build3_loc (input_location, COND_EXPR,
    7282          193 :                                    TREE_TYPE (parmse.expr),
    7283              :                                    gfc_unlikely (tmp, PRED_FORTRAN_ABSENT_DUMMY),
    7284          193 :                                    fold_convert (TREE_TYPE (parmse.expr),
    7285              :                                                  null_pointer_node),
    7286              :                                    parmse.expr);
    7287              :             }
    7288              : 
    7289              :           /* The scalarizer does not repackage the reference to a class
    7290              :              array - instead it returns a pointer to the data element.  */
    7291         5831 :           if (fsym && fsym->ts.type == BT_CLASS && e->ts.type == BT_CLASS)
    7292          186 :             gfc_conv_class_to_class (&parmse, e, fsym->ts, true,
    7293          186 :                                      fsym->attr.intent != INTENT_IN
    7294          186 :                                      && (CLASS_DATA (fsym)->attr.class_pointer
    7295           24 :                                          || CLASS_DATA (fsym)->attr.allocatable),
    7296          186 :                                      fsym->attr.optional
    7297            0 :                                      && e->expr_type == EXPR_VARIABLE
    7298          186 :                                      && e->symtree->n.sym->attr.optional,
    7299          186 :                                      CLASS_DATA (fsym)->attr.class_pointer
    7300          186 :                                      || CLASS_DATA (fsym)->attr.allocatable);
    7301              :         }
    7302              :       else
    7303              :         {
    7304       245579 :           bool scalar;
    7305       245579 :           gfc_ss *argss;
    7306              : 
    7307       245579 :           gfc_init_se (&parmse, NULL);
    7308              : 
    7309              :           /* Check whether the expression is a scalar or not; we cannot use
    7310              :              e->rank as it can be nonzero for functions arguments.  */
    7311       245579 :           argss = gfc_walk_expr (e);
    7312       245579 :           scalar = argss == gfc_ss_terminator;
    7313       245579 :           if (!scalar)
    7314        60258 :             gfc_free_ss_chain (argss);
    7315              : 
    7316              :           /* Special handling for passing scalar polymorphic coarrays;
    7317              :              otherwise one passes "class->_data.data" instead of "&class".  */
    7318       245579 :           if (e->rank == 0 && e->ts.type == BT_CLASS
    7319         3551 :               && fsym && fsym->ts.type == BT_CLASS
    7320         3129 :               && CLASS_DATA (fsym)->attr.codimension
    7321           55 :               && !CLASS_DATA (fsym)->attr.dimension)
    7322              :             {
    7323           55 :               gfc_add_class_array_ref (e);
    7324           55 :               parmse.want_coarray = 1;
    7325           55 :               scalar = false;
    7326              :             }
    7327              : 
    7328              :           /* A scalar or transformational function.  */
    7329       245579 :           if (scalar)
    7330              :             {
    7331       185266 :               if (e->expr_type == EXPR_VARIABLE
    7332        54911 :                     && e->symtree->n.sym->attr.cray_pointee
    7333          390 :                     && fsym && fsym->attr.flavor == FL_PROCEDURE)
    7334              :                 {
    7335              :                     /* The Cray pointer needs to be converted to a pointer to
    7336              :                        a type given by the expression.  */
    7337            6 :                     gfc_conv_expr (&parmse, e);
    7338            6 :                     type = build_pointer_type (TREE_TYPE (parmse.expr));
    7339            6 :                     tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
    7340            6 :                     parmse.expr = convert (type, tmp);
    7341              :                 }
    7342              : 
    7343       185260 :               else if (sym->attr.is_bind_c && e && is_CFI_desc (fsym, NULL))
    7344              :                 /* Implement F2018, 18.3.6, list item (5), bullet point 2.  */
    7345          687 :                 gfc_conv_gfc_desc_to_cfi_desc (&parmse, e, fsym);
    7346              : 
    7347       184573 :               else if (fsym && fsym->attr.value)
    7348              :                 {
    7349        21960 :                   if (fsym->ts.type == BT_CHARACTER
    7350          543 :                       && fsym->ts.is_c_interop
    7351          181 :                       && fsym->ns->proc_name != NULL
    7352          181 :                       && fsym->ns->proc_name->attr.is_bind_c)
    7353              :                     {
    7354          172 :                       parmse.expr = NULL;
    7355          172 :                       conv_scalar_char_value (fsym, &parmse, &e);
    7356          172 :                       if (parmse.expr == NULL)
    7357          166 :                         gfc_conv_expr (&parmse, e);
    7358              :                     }
    7359              :                   else
    7360              :                     {
    7361        21788 :                       gfc_conv_expr (&parmse, e);
    7362        21788 :                       conv_dummy_value (&parmse, e, fsym, optionalargs);
    7363              :                     }
    7364              :                 }
    7365              : 
    7366       162613 :               else if (arg->name && arg->name[0] == '%')
    7367              :                 /* Argument list functions %VAL, %LOC and %REF are signalled
    7368              :                    through arg->name.  */
    7369         5822 :                 conv_arglist_function (&parmse, arg->expr, arg->name);
    7370       156791 :               else if ((e->expr_type == EXPR_FUNCTION)
    7371         8289 :                         && ((e->value.function.esym
    7372         2154 :                              && e->value.function.esym->result->attr.pointer)
    7373         8194 :                             || (!e->value.function.esym
    7374         6135 :                                 && e->symtree->n.sym->attr.pointer))
    7375           95 :                         && fsym && fsym->attr.target)
    7376              :                 /* Make sure the function only gets called once.  */
    7377            8 :                 gfc_conv_expr_reference (&parmse, e);
    7378       156783 :               else if (e->expr_type == EXPR_FUNCTION
    7379         8281 :                        && e->symtree->n.sym->result
    7380         7246 :                        && e->symtree->n.sym->result != e->symtree->n.sym
    7381          138 :                        && e->symtree->n.sym->result->attr.proc_pointer)
    7382              :                 {
    7383              :                   /* Functions returning procedure pointers.  */
    7384           18 :                   gfc_conv_expr (&parmse, e);
    7385           18 :                   if (fsym && fsym->attr.proc_pointer)
    7386            6 :                     parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
    7387              :                 }
    7388              : 
    7389              :               else
    7390              :                 {
    7391       156765 :                   bool defer_to_dealloc_blk = false;
    7392       156765 :                   if (e->ts.type == BT_CLASS && fsym
    7393         3484 :                       && fsym->ts.type == BT_CLASS
    7394         3062 :                       && (!CLASS_DATA (fsym)->as
    7395          356 :                           || CLASS_DATA (fsym)->as->type != AS_ASSUMED_RANK)
    7396         2706 :                       && CLASS_DATA (e)->attr.codimension)
    7397              :                     {
    7398           48 :                       gcc_assert (!CLASS_DATA (fsym)->attr.codimension);
    7399           48 :                       gcc_assert (!CLASS_DATA (fsym)->as);
    7400           48 :                       gfc_add_class_array_ref (e);
    7401           48 :                       parmse.want_coarray = 1;
    7402           48 :                       gfc_conv_expr_reference (&parmse, e);
    7403           48 :                       class_scalar_coarray_to_class (&parmse, e, fsym->ts,
    7404           48 :                                      fsym->attr.optional
    7405           48 :                                      && e->expr_type == EXPR_VARIABLE);
    7406              :                     }
    7407       156717 :                   else if (e->ts.type == BT_CLASS && fsym
    7408         3436 :                            && fsym->ts.type == BT_CLASS
    7409         3014 :                            && !CLASS_DATA (fsym)->as
    7410         2658 :                            && !CLASS_DATA (e)->as
    7411         2548 :                            && strcmp (fsym->ts.u.derived->name,
    7412              :                                       e->ts.u.derived->name))
    7413              :                     {
    7414         1625 :                       type = gfc_typenode_for_spec (&fsym->ts);
    7415         1625 :                       var = gfc_create_var (type, fsym->name);
    7416         1625 :                       gfc_conv_expr (&parmse, e);
    7417         1625 :                       if (fsym->attr.optional
    7418          153 :                           && e->expr_type == EXPR_VARIABLE
    7419          153 :                           && e->symtree->n.sym->attr.optional)
    7420              :                         {
    7421           66 :                           stmtblock_t block;
    7422           66 :                           tree cond;
    7423           66 :                           tmp = gfc_build_addr_expr (NULL_TREE, parmse.expr);
    7424           66 :                           cond = fold_build2_loc (input_location, NE_EXPR,
    7425              :                                                   logical_type_node, tmp,
    7426           66 :                                                   fold_convert (TREE_TYPE (tmp),
    7427              :                                                             null_pointer_node));
    7428           66 :                           gfc_start_block (&block);
    7429           66 :                           gfc_add_modify (&block, var,
    7430              :                                           fold_build1_loc (input_location,
    7431              :                                                            VIEW_CONVERT_EXPR,
    7432              :                                                            type, parmse.expr));
    7433           66 :                           gfc_add_expr_to_block (&parmse.pre,
    7434              :                                  fold_build3_loc (input_location,
    7435              :                                          COND_EXPR, void_type_node,
    7436              :                                          cond, gfc_finish_block (&block),
    7437              :                                          build_empty_stmt (input_location)));
    7438           66 :                           parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
    7439          132 :                           parmse.expr = build3_loc (input_location, COND_EXPR,
    7440           66 :                                          TREE_TYPE (parmse.expr),
    7441              :                                          cond, parmse.expr,
    7442           66 :                                          fold_convert (TREE_TYPE (parmse.expr),
    7443              :                                                        null_pointer_node));
    7444           66 :                         }
    7445              :                       else
    7446              :                         {
    7447              :                           /* Since the internal representation of unlimited
    7448              :                              polymorphic expressions includes an extra field
    7449              :                              that other class objects do not, a cast to the
    7450              :                              formal type does not work.  */
    7451         1559 :                           if (!UNLIMITED_POLY (e) && UNLIMITED_POLY (fsym))
    7452              :                             {
    7453           91 :                               tree efield;
    7454              : 
    7455              :                               /* Evaluate arguments just once, when they have
    7456              :                                  side effects.  */
    7457           91 :                               if (TREE_SIDE_EFFECTS (parmse.expr))
    7458              :                                 {
    7459           25 :                                   tree cldata, zero;
    7460              : 
    7461           25 :                                   parmse.expr = gfc_evaluate_now (parmse.expr,
    7462              :                                                                   &parmse.pre);
    7463              : 
    7464              :                                   /* Prevent memory leak, when old component
    7465              :                                      was allocated already.  */
    7466           25 :                                   cldata = gfc_class_data_get (parmse.expr);
    7467           25 :                                   zero = build_int_cst (TREE_TYPE (cldata),
    7468              :                                                         0);
    7469           25 :                                   tmp = fold_build2_loc (input_location, NE_EXPR,
    7470              :                                                          logical_type_node,
    7471              :                                                          cldata, zero);
    7472           25 :                                   tmp = build3_v (COND_EXPR, tmp,
    7473              :                                                   gfc_call_free (cldata),
    7474              :                                                   build_empty_stmt (
    7475              :                                                     input_location));
    7476           25 :                                   gfc_add_expr_to_block (&parmse.finalblock,
    7477              :                                                          tmp);
    7478           25 :                                   gfc_add_modify (&parmse.finalblock,
    7479              :                                                   cldata, zero);
    7480              :                                 }
    7481              : 
    7482              :                               /* Set the _data field.  */
    7483           91 :                               tmp = gfc_class_data_get (var);
    7484           91 :                               efield = fold_convert (TREE_TYPE (tmp),
    7485              :                                         gfc_class_data_get (parmse.expr));
    7486           91 :                               gfc_add_modify (&parmse.pre, tmp, efield);
    7487              : 
    7488              :                               /* Set the _vptr field.  */
    7489           91 :                               tmp = gfc_class_vptr_get (var);
    7490           91 :                               efield = fold_convert (TREE_TYPE (tmp),
    7491              :                                         gfc_class_vptr_get (parmse.expr));
    7492           91 :                               gfc_add_modify (&parmse.pre, tmp, efield);
    7493              : 
    7494              :                               /* Set the _len field.  */
    7495           91 :                               tmp = gfc_class_len_get (var);
    7496           91 :                               gfc_add_modify (&parmse.pre, tmp,
    7497           91 :                                               build_int_cst (TREE_TYPE (tmp), 0));
    7498           91 :                             }
    7499              :                           else
    7500              :                             {
    7501         1468 :                               tmp = fold_build1_loc (input_location,
    7502              :                                                      VIEW_CONVERT_EXPR,
    7503              :                                                      type, parmse.expr);
    7504         1468 :                               gfc_add_modify (&parmse.pre, var, tmp);
    7505         1559 :                                               ;
    7506              :                             }
    7507         1559 :                           parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
    7508              :                         }
    7509              :                     }
    7510              :                   else
    7511              :                     {
    7512       155092 :                       gfc_conv_expr_reference (&parmse, e);
    7513              : 
    7514       155092 :                       gfc_symbol *dsym = fsym;
    7515       155092 :                       gfc_dummy_arg *dummy;
    7516              : 
    7517              :                       /* Use associated dummy as fallback for formal
    7518              :                          argument if there is no explicit interface.  */
    7519       155092 :                       if (dsym == NULL
    7520        27405 :                           && (dummy = arg->associated_dummy)
    7521        24885 :                           && dummy->intrinsicness == GFC_NON_INTRINSIC_DUMMY_ARG
    7522       178574 :                           && dummy->u.non_intrinsic->sym)
    7523              :                         dsym = dummy->u.non_intrinsic->sym;
    7524              : 
    7525       155092 :                       if (dsym
    7526       151169 :                           && dsym->attr.intent == INTENT_OUT
    7527         3246 :                           && !dsym->attr.allocatable
    7528         3104 :                           && !dsym->attr.pointer
    7529         3086 :                           && e->expr_type == EXPR_VARIABLE
    7530         3085 :                           && e->ref == NULL
    7531         2976 :                           && e->symtree
    7532         2976 :                           && e->symtree->n.sym
    7533         2976 :                           && !e->symtree->n.sym->attr.dimension
    7534         2976 :                           && e->ts.type != BT_CHARACTER
    7535         2874 :                           && e->ts.type != BT_CLASS
    7536         2644 :                           && (e->ts.type != BT_DERIVED
    7537          492 :                               || (dsym->ts.type == BT_DERIVED
    7538          492 :                                   && e->ts.u.derived == dsym->ts.u.derived
    7539              :                                   /* Types with allocatable components are
    7540              :                                      excluded from clobbering because we need
    7541              :                                      the unclobbered pointers to free the
    7542              :                                      allocatable components in the callee.
    7543              :                                      Same goes for finalizable types or types
    7544              :                                      with finalizable components, we need to
    7545              :                                      pass the unclobbered values to the
    7546              :                                      finalization routines.
    7547              :                                      For parameterized types, it's less clear
    7548              :                                      but they may not have a constant size
    7549              :                                      so better exclude them in any case.  */
    7550          477 :                                   && !e->ts.u.derived->attr.alloc_comp
    7551          351 :                                   && !e->ts.u.derived->attr.pdt_type
    7552          351 :                                   && !gfc_is_finalizable (e->ts.u.derived, NULL)))
    7553         2461 :                           && e->ts.type != BT_PROCEDURE
    7554       157517 :                           && !sym->attr.elemental)
    7555              :                         {
    7556         1092 :                           tree var;
    7557         1092 :                           var = build_fold_indirect_ref_loc (input_location,
    7558              :                                                              parmse.expr);
    7559         1092 :                           tree clobber = build_clobber (TREE_TYPE (var));
    7560         1092 :                           gfc_add_modify (&clobbers, var, clobber);
    7561              :                         }
    7562              :                     }
    7563              :                   /* Catch base objects that are not variables.  */
    7564       156765 :                   if (e->ts.type == BT_CLASS
    7565         3484 :                         && e->expr_type != EXPR_VARIABLE
    7566          306 :                         && expr && e == expr->base_expr)
    7567           80 :                     base_object = build_fold_indirect_ref_loc (input_location,
    7568              :                                                                parmse.expr);
    7569              : 
    7570              :                   /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
    7571              :                      allocated on entry, it must be deallocated.  */
    7572       129360 :                   if (fsym && fsym->attr.intent == INTENT_OUT
    7573         3175 :                       && (fsym->attr.allocatable
    7574         3033 :                           || (fsym->ts.type == BT_CLASS
    7575          259 :                               && CLASS_DATA (fsym)->attr.allocatable))
    7576       157056 :                       && !is_CFI_desc (fsym, NULL))
    7577              :                     {
    7578          291 :                       stmtblock_t block;
    7579          291 :                       tree ptr;
    7580              : 
    7581          291 :                       defer_to_dealloc_blk = true;
    7582              : 
    7583          291 :                       parmse.expr = gfc_evaluate_data_ref_now (parmse.expr,
    7584              :                                                                &parmse.pre);
    7585              : 
    7586          291 :                       if (parmse.class_container != NULL_TREE)
    7587          156 :                         parmse.class_container
    7588          156 :                             = gfc_evaluate_data_ref_now (parmse.class_container,
    7589              :                                                          &parmse.pre);
    7590              : 
    7591          291 :                       gfc_init_block  (&block);
    7592          291 :                       ptr = parmse.expr;
    7593          291 :                       if (e->ts.type == BT_CLASS)
    7594          156 :                         ptr = gfc_class_data_get (ptr);
    7595              : 
    7596          291 :                       tree cls = parmse.class_container;
    7597          291 :                       tmp = gfc_deallocate_scalar_with_status (ptr, NULL_TREE,
    7598              :                                                                NULL_TREE, true,
    7599              :                                                                e, e->ts, cls);
    7600          291 :                       gfc_add_expr_to_block (&block, tmp);
    7601          291 :                       gfc_add_modify (&block, ptr,
    7602          291 :                                       fold_convert (TREE_TYPE (ptr),
    7603              :                                                     null_pointer_node));
    7604              : 
    7605          291 :                       if (fsym->ts.type == BT_CLASS)
    7606          149 :                         gfc_reset_vptr (&block, nullptr,
    7607              :                                         build_fold_indirect_ref (parmse.expr),
    7608          149 :                                         fsym->ts.u.derived);
    7609              : 
    7610          291 :                       if (fsym->attr.optional
    7611           42 :                           && e->expr_type == EXPR_VARIABLE
    7612           42 :                           && e->symtree->n.sym->attr.optional)
    7613              :                         {
    7614           36 :                           tmp = fold_build3_loc (input_location, COND_EXPR,
    7615              :                                      void_type_node,
    7616           18 :                                      gfc_conv_expr_present (e->symtree->n.sym),
    7617              :                                             gfc_finish_block (&block),
    7618              :                                             build_empty_stmt (input_location));
    7619              :                         }
    7620              :                       else
    7621          273 :                         tmp = gfc_finish_block (&block);
    7622              : 
    7623          291 :                       gfc_add_expr_to_block (&dealloc_blk, tmp);
    7624              :                     }
    7625              : 
    7626              :                   /* A class array element needs converting back to be a
    7627              :                      class object, if the formal argument is a class object.  */
    7628       156765 :                   if (fsym && fsym->ts.type == BT_CLASS
    7629         3086 :                         && e->ts.type == BT_CLASS
    7630         3062 :                         && ((CLASS_DATA (fsym)->as
    7631          356 :                              && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
    7632         2706 :                             || CLASS_DATA (e)->attr.dimension))
    7633              :                     {
    7634          466 :                       gfc_se class_se = parmse;
    7635          466 :                       gfc_init_block (&class_se.pre);
    7636          466 :                       gfc_init_block (&class_se.post);
    7637              : 
    7638          466 :                       gfc_conv_class_to_class (&class_se, e, fsym->ts, false,
    7639          466 :                                      fsym->attr.intent != INTENT_IN
    7640          466 :                                      && (CLASS_DATA (fsym)->attr.class_pointer
    7641          267 :                                          || CLASS_DATA (fsym)->attr.allocatable),
    7642          466 :                                      fsym->attr.optional
    7643          198 :                                      && e->expr_type == EXPR_VARIABLE
    7644          664 :                                      && e->symtree->n.sym->attr.optional,
    7645          466 :                                      CLASS_DATA (fsym)->attr.class_pointer
    7646          466 :                                      || CLASS_DATA (fsym)->attr.allocatable);
    7647              : 
    7648          466 :                       parmse.expr = class_se.expr;
    7649          442 :                       stmtblock_t *class_pre_block = defer_to_dealloc_blk
    7650          466 :                                                      ? &dealloc_blk
    7651              :                                                      : &parmse.pre;
    7652          466 :                       gfc_add_block_to_block (class_pre_block, &class_se.pre);
    7653          466 :                       gfc_add_block_to_block (&parmse.post, &class_se.post);
    7654              :                     }
    7655              : 
    7656       129360 :                   if (fsym && (fsym->ts.type == BT_DERIVED
    7657       117508 :                                || fsym->ts.type == BT_ASSUMED)
    7658        12719 :                       && e->ts.type == BT_CLASS
    7659          410 :                       && !CLASS_DATA (e)->attr.dimension
    7660          374 :                       && !CLASS_DATA (e)->attr.codimension)
    7661              :                     {
    7662          374 :                       parmse.expr = gfc_class_data_get (parmse.expr);
    7663              :                       /* The result is a class temporary, whose _data component
    7664              :                          must be freed to avoid a memory leak.  */
    7665          374 :                       if (e->expr_type == EXPR_FUNCTION
    7666           23 :                           && CLASS_DATA (e)->attr.allocatable)
    7667              :                         {
    7668           19 :                           tree zero;
    7669              : 
    7670              :                           /* Finalize the expression.  */
    7671           19 :                           gfc_finalize_tree_expr (&parmse, NULL,
    7672           19 :                                                   gfc_expr_attr (e), e->rank);
    7673           19 :                           gfc_add_block_to_block (&parmse.post,
    7674              :                                                   &parmse.finalblock);
    7675              : 
    7676              :                           /* Then free the class _data.  */
    7677           19 :                           zero = build_int_cst (TREE_TYPE (parmse.expr), 0);
    7678           19 :                           tmp = fold_build2_loc (input_location, NE_EXPR,
    7679              :                                                  logical_type_node,
    7680              :                                                  parmse.expr, zero);
    7681           19 :                           tmp = build3_v (COND_EXPR, tmp,
    7682              :                                           gfc_call_free (parmse.expr),
    7683              :                                           build_empty_stmt (input_location));
    7684           19 :                           gfc_add_expr_to_block (&parmse.post, tmp);
    7685           19 :                           gfc_add_modify (&parmse.post, parmse.expr, zero);
    7686              :                         }
    7687              :                     }
    7688              : 
    7689              :                   /* Wrap scalar variable in a descriptor. We need to convert
    7690              :                      the address of a pointer back to the pointer itself before,
    7691              :                      we can assign it to the data field.  */
    7692              : 
    7693       129360 :                   if (fsym && fsym->as && fsym->as->type == AS_ASSUMED_RANK
    7694         1314 :                       && fsym->ts.type != BT_CLASS && e->expr_type != EXPR_NULL)
    7695              :                     {
    7696         1242 :                       tmp = parmse.expr;
    7697         1242 :                       if (TREE_CODE (tmp) == ADDR_EXPR)
    7698          736 :                         tmp = TREE_OPERAND (tmp, 0);
    7699         1242 :                       parmse.expr = gfc_conv_scalar_to_descriptor (&parmse, tmp,
    7700              :                                                                    fsym->attr);
    7701         1242 :                       parmse.expr = gfc_build_addr_expr (NULL_TREE,
    7702              :                                                          parmse.expr);
    7703              :                     }
    7704       128118 :                   else if (fsym && e->expr_type != EXPR_NULL
    7705       127820 :                       && ((fsym->attr.pointer
    7706         1740 :                            && fsym->attr.flavor != FL_PROCEDURE)
    7707       126086 :                           || (fsym->attr.proc_pointer
    7708          193 :                               && !(e->expr_type == EXPR_VARIABLE
    7709          193 :                                    && e->symtree->n.sym->attr.dummy))
    7710       125905 :                           || (fsym->attr.proc_pointer
    7711           12 :                               && e->expr_type == EXPR_VARIABLE
    7712           12 :                               && gfc_is_proc_ptr_comp (e))
    7713       125899 :                           || (fsym->attr.allocatable
    7714         1040 :                               && fsym->attr.flavor != FL_PROCEDURE)))
    7715              :                     {
    7716              :                       /* Scalar pointer dummy args require an extra level of
    7717              :                          indirection. The null pointer already contains
    7718              :                          this level of indirection.  */
    7719         2955 :                       parm_kind = SCALAR_POINTER;
    7720         2955 :                       parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
    7721              :                     }
    7722              :                 }
    7723              :             }
    7724        60313 :           else if (e->ts.type == BT_CLASS
    7725         2693 :                     && fsym && fsym->ts.type == BT_CLASS
    7726         2347 :                     && (CLASS_DATA (fsym)->attr.dimension
    7727           55 :                         || CLASS_DATA (fsym)->attr.codimension))
    7728              :             {
    7729              :               /* Pass a class array.  */
    7730         2347 :               gfc_conv_expr_descriptor (&parmse, e);
    7731         2347 :               bool defer_to_dealloc_blk = false;
    7732              : 
    7733         2347 :               if (fsym->attr.optional
    7734          798 :                   && e->expr_type == EXPR_VARIABLE
    7735          798 :                   && e->symtree->n.sym->attr.optional)
    7736              :                 {
    7737          438 :                   stmtblock_t block;
    7738              : 
    7739          438 :                   gfc_init_block (&block);
    7740          438 :                   gfc_add_block_to_block (&block, &parmse.pre);
    7741              : 
    7742          876 :                   tree t = fold_build3_loc (input_location, COND_EXPR,
    7743              :                              void_type_node,
    7744          438 :                              gfc_conv_expr_present (e->symtree->n.sym),
    7745              :                                     gfc_finish_block (&block),
    7746              :                                     build_empty_stmt (input_location));
    7747              : 
    7748          438 :                   gfc_add_expr_to_block (&parmse.pre, t);
    7749              :                 }
    7750              : 
    7751              :               /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
    7752              :                  allocated on entry, it must be deallocated.  */
    7753         2347 :               if (fsym->attr.intent == INTENT_OUT
    7754          141 :                   && CLASS_DATA (fsym)->attr.allocatable)
    7755              :                 {
    7756          110 :                   stmtblock_t block;
    7757          110 :                   tree ptr;
    7758              : 
    7759              :                   /* In case the data reference to deallocate is dependent on
    7760              :                      its own content, save the resulting pointer to a variable
    7761              :                      and only use that variable from now on, before the
    7762              :                      expression becomes invalid.  */
    7763          110 :                   parmse.expr = gfc_evaluate_data_ref_now (parmse.expr,
    7764              :                                                            &parmse.pre);
    7765              : 
    7766          110 :                   if (parmse.class_container != NULL_TREE)
    7767          110 :                     parmse.class_container
    7768          110 :                         = gfc_evaluate_data_ref_now (parmse.class_container,
    7769              :                                                      &parmse.pre);
    7770              : 
    7771          110 :                   gfc_init_block  (&block);
    7772          110 :                   ptr = parmse.expr;
    7773          110 :                   ptr = gfc_class_data_get (ptr);
    7774              : 
    7775          110 :                   tree cls = parmse.class_container;
    7776          110 :                   tmp = gfc_deallocate_with_status (ptr, NULL_TREE,
    7777              :                                                     NULL_TREE, NULL_TREE,
    7778              :                                                     NULL_TREE, true, e,
    7779              :                                                     GFC_CAF_COARRAY_NOCOARRAY,
    7780              :                                                     cls);
    7781          110 :                   gfc_add_expr_to_block (&block, tmp);
    7782          110 :                   tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    7783              :                                          void_type_node, ptr,
    7784              :                                          null_pointer_node);
    7785          110 :                   gfc_add_expr_to_block (&block, tmp);
    7786          110 :                   gfc_reset_vptr (&block, e, parmse.class_container);
    7787              : 
    7788          110 :                   if (fsym->attr.optional
    7789           30 :                       && e->expr_type == EXPR_VARIABLE
    7790           30 :                       && (!e->ref
    7791           30 :                           || (e->ref->type == REF_ARRAY
    7792            0 :                               && e->ref->u.ar.type != AR_FULL))
    7793            0 :                       && e->symtree->n.sym->attr.optional)
    7794              :                     {
    7795            0 :                       tmp = fold_build3_loc (input_location, COND_EXPR,
    7796              :                                     void_type_node,
    7797            0 :                                     gfc_conv_expr_present (e->symtree->n.sym),
    7798              :                                     gfc_finish_block (&block),
    7799              :                                     build_empty_stmt (input_location));
    7800              :                     }
    7801              :                   else
    7802          110 :                     tmp = gfc_finish_block (&block);
    7803              : 
    7804          110 :                   gfc_add_expr_to_block (&dealloc_blk, tmp);
    7805          110 :                   defer_to_dealloc_blk = true;
    7806              :                 }
    7807              : 
    7808         2347 :               gfc_se class_se = parmse;
    7809         2347 :               gfc_init_block (&class_se.pre);
    7810         2347 :               gfc_init_block (&class_se.post);
    7811              : 
    7812         2347 :               if (e->expr_type != EXPR_VARIABLE)
    7813              :                 {
    7814              :                   int n;
    7815              :                   /* Set the bounds and offset correctly.  */
    7816           60 :                   for (n = 0; n < e->rank; n++)
    7817           30 :                     gfc_conv_shift_descriptor_lbound (&class_se.pre,
    7818              :                                                       class_se.expr,
    7819              :                                                       n, gfc_index_one_node);
    7820              :                 }
    7821              : 
    7822              :               /* The conversion does not repackage the reference to a class
    7823              :                  array - _data descriptor.  */
    7824         2347 :               gfc_conv_class_to_class (&class_se, e, fsym->ts, false,
    7825         2347 :                                      fsym->attr.intent != INTENT_IN
    7826         2347 :                                      && (CLASS_DATA (fsym)->attr.class_pointer
    7827         1211 :                                          || CLASS_DATA (fsym)->attr.allocatable),
    7828         2347 :                                      fsym->attr.optional
    7829          798 :                                      && e->expr_type == EXPR_VARIABLE
    7830         3145 :                                      && e->symtree->n.sym->attr.optional,
    7831         2347 :                                      CLASS_DATA (fsym)->attr.class_pointer
    7832         2347 :                                      || CLASS_DATA (fsym)->attr.allocatable);
    7833              : 
    7834         2347 :               parmse.expr = class_se.expr;
    7835         2237 :               stmtblock_t *class_pre_block = defer_to_dealloc_blk
    7836         2347 :                                              ? &dealloc_blk
    7837              :                                              : &parmse.pre;
    7838         2347 :               gfc_add_block_to_block (class_pre_block, &class_se.pre);
    7839         2347 :               gfc_add_block_to_block (&parmse.post, &class_se.post);
    7840              : 
    7841         2347 :               if (e->expr_type == EXPR_OP
    7842           12 :                   && POINTER_TYPE_P (TREE_TYPE (parmse.expr))
    7843         2359 :                   && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_OPERAND (parmse.expr, 0))))
    7844              :                 {
    7845           12 :                   tree cond;
    7846           12 :                   tree dealloc_expr = gfc_finish_block (&parmse.post);
    7847           12 :                   tmp = TREE_OPERAND (parmse.expr, 0);
    7848           12 :                   gfc_init_block (&parmse.post);
    7849           12 :                   cond = gfc_class_data_get (tmp);
    7850           12 :                   tmp = gfc_deallocate_alloc_comp_no_caf (e->ts.u.derived,
    7851              :                                                           tmp, e->rank, true);
    7852           12 :                   gfc_add_expr_to_block (&parmse.post, tmp);
    7853           12 :                   cond = gfc_class_data_get (TREE_OPERAND (parmse.expr, 0));
    7854           12 :                   cond = gfc_conv_descriptor_data_get (cond);
    7855           12 :                   cond = fold_build2_loc (input_location, NE_EXPR,
    7856              :                                           logical_type_node, cond,
    7857           12 :                                           build_int_cst (TREE_TYPE (cond), 0));
    7858           12 :                   tmp = build3_v (COND_EXPR, cond, dealloc_expr,
    7859              :                                   build_empty_stmt (input_location));
    7860              : 
    7861              :                   /* This specific case should not be processed further and so
    7862              :                      bundle everything up and proceed to the next argument.  */
    7863           12 :                   if (fsym && need_interface_mapping && e)
    7864           12 :                     gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
    7865           12 :                   gfc_add_expr_to_block (&parmse.post, tmp);
    7866           12 :                   gfc_add_block_to_block (&se->pre, &parmse.pre);
    7867           12 :                   gfc_add_block_to_block (&post, &parmse.post);
    7868           12 :                   gfc_add_block_to_block (&se->finalblock, &parmse.finalblock);
    7869           12 :                   vec_safe_push (arglist, parmse.expr);
    7870           12 :                   continue;
    7871           12 :                 }
    7872         2335 :             }
    7873              :           else
    7874              :             {
    7875              :               /* If the argument is a function call that may not create
    7876              :                  a temporary for the result, we have to check that we
    7877              :                  can do it, i.e. that there is no alias between this
    7878              :                  argument and another one.  */
    7879        57966 :               if (gfc_get_noncopying_intrinsic_argument (e) != NULL)
    7880              :                 {
    7881          358 :                   gfc_expr *iarg;
    7882          358 :                   sym_intent intent;
    7883              : 
    7884          358 :                   if (fsym != NULL)
    7885          349 :                     intent = fsym->attr.intent;
    7886              :                   else
    7887              :                     intent = INTENT_UNKNOWN;
    7888              : 
    7889          358 :                   if (gfc_check_fncall_dependency (e, intent, sym, args,
    7890              :                                                    NOT_ELEMENTAL))
    7891           21 :                     parmse.force_tmp = 1;
    7892              : 
    7893          358 :                   iarg = e->value.function.actual->expr;
    7894              : 
    7895              :                   /* Temporary needed if aliasing due to host association.  */
    7896          358 :                   if (sym->attr.contained
    7897          114 :                         && !sym->attr.pure
    7898          114 :                         && !sym->attr.implicit_pure
    7899           36 :                         && !sym->attr.use_assoc
    7900           36 :                         && iarg->expr_type == EXPR_VARIABLE
    7901           36 :                         && sym->ns == iarg->symtree->n.sym->ns)
    7902           36 :                     parmse.force_tmp = 1;
    7903              : 
    7904              :                   /* Ditto within module.  */
    7905          358 :                   if (sym->attr.use_assoc
    7906            6 :                         && !sym->attr.pure
    7907            6 :                         && !sym->attr.implicit_pure
    7908            0 :                         && iarg->expr_type == EXPR_VARIABLE
    7909            0 :                         && sym->module == iarg->symtree->n.sym->module)
    7910            0 :                     parmse.force_tmp = 1;
    7911              :                 }
    7912              : 
    7913              :               /* Special case for assumed-rank arrays: when passing an
    7914              :                  argument to a nonallocatable/nonpointer dummy, the bounds have
    7915              :                  to be reset as otherwise a last-dim ubound of -1 is
    7916              :                  indistinguishable from an assumed-size array in the callee.  */
    7917        57966 :               if (!sym->attr.is_bind_c && e && fsym && fsym->as
    7918        34983 :                   && fsym->as->type == AS_ASSUMED_RANK
    7919        11905 :                   && e->rank != -1
    7920        11592 :                   && e->expr_type == EXPR_VARIABLE
    7921        11151 :                   && ((fsym->ts.type == BT_CLASS
    7922            0 :                        && !CLASS_DATA (fsym)->attr.class_pointer
    7923            0 :                        && !CLASS_DATA (fsym)->attr.allocatable)
    7924        11151 :                       || (fsym->ts.type != BT_CLASS
    7925        11151 :                           && !fsym->attr.pointer && !fsym->attr.allocatable)))
    7926              :                 {
    7927              :                   /* Change AR_FULL to a (:,:,:) ref to force bounds update. */
    7928        10608 :                   gfc_ref *ref;
    7929        10860 :                   for (ref = e->ref; ref->next; ref = ref->next)
    7930              :                     {
    7931          324 :                       if (ref->next->type == REF_INQUIRY)
    7932              :                         break;
    7933          276 :                       if (ref->type == REF_ARRAY
    7934           24 :                           && ref->u.ar.type != AR_ELEMENT)
    7935              :                         break;
    7936        10608 :                     };
    7937        10608 :                   if (ref->u.ar.type == AR_FULL
    7938         9858 :                       && ref->u.ar.as->type != AS_ASSUMED_SIZE)
    7939         9738 :                     ref->u.ar.type = AR_SECTION;
    7940              :                 }
    7941              : 
    7942        57966 :               if (sym->attr.is_bind_c && e && is_CFI_desc (fsym, NULL))
    7943              :                 /* Implement F2018, 18.3.6, list item (5), bullet point 2.  */
    7944         5850 :                 gfc_conv_gfc_desc_to_cfi_desc (&parmse, e, fsym);
    7945              : 
    7946        52116 :               else if (e->expr_type == EXPR_VARIABLE
    7947        40748 :                     && is_subref_array (e)
    7948        53096 :                     && !(fsym && fsym->attr.pointer))
    7949              :                 /* The actual argument is a component reference to an
    7950              :                    array of derived types.  In this case, the argument
    7951              :                    is converted to a temporary, which is passed and then
    7952              :                    written back after the procedure call.  */
    7953          727 :                 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
    7954          685 :                                 fsym ? fsym->attr.intent : INTENT_INOUT,
    7955          727 :                                 fsym && fsym->attr.pointer);
    7956              : 
    7957        51389 :               else if (e->ts.type == BT_CLASS && CLASS_DATA (e)->as
    7958          345 :                        && CLASS_DATA (e)->as->type == AS_ASSUMED_SIZE
    7959           18 :                        && nodesc_arg && fsym->ts.type == BT_DERIVED)
    7960              :                 /* An assumed size class actual argument being passed to
    7961              :                    a 'no descriptor' formal argument just requires the
    7962              :                    data pointer to be passed. For class dummy arguments
    7963              :                    this is stored in the symbol backend decl..  */
    7964            6 :                 parmse.expr = e->symtree->n.sym->backend_decl;
    7965              : 
    7966        51383 :               else if (gfc_is_class_array_ref (e, NULL)
    7967        51383 :                        && fsym && fsym->ts.type == BT_DERIVED)
    7968              :                 /* The actual argument is a component reference to an
    7969              :                    array of derived types.  In this case, the argument
    7970              :                    is converted to a temporary, which is passed and then
    7971              :                    written back after the procedure call.
    7972              :                    OOP-TODO: Insert code so that if the dynamic type is
    7973              :                    the same as the declared type, copy-in/copy-out does
    7974              :                    not occur.  */
    7975          108 :                 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
    7976          108 :                                            fsym->attr.intent,
    7977          108 :                                            fsym->attr.pointer);
    7978              : 
    7979        51275 :               else if (gfc_is_class_array_function (e)
    7980        51275 :                        && fsym && fsym->ts.type == BT_DERIVED)
    7981              :                 /* See previous comment.  For function actual argument,
    7982              :                    the write out is not needed so the intent is set as
    7983              :                    intent in.  */
    7984              :                 {
    7985           13 :                   e->must_finalize = 1;
    7986           13 :                   gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
    7987           13 :                                              INTENT_IN, fsym->attr.pointer);
    7988              :                 }
    7989        47700 :               else if (fsym && fsym->attr.contiguous
    7990           60 :                        && (fsym->attr.target
    7991         1695 :                            ? gfc_is_not_contiguous (e)
    7992         1635 :                            : !gfc_is_simply_contiguous (e, false, true))
    7993          327 :                        && gfc_expr_is_variable (e)
    7994        53272 :                        && e->rank != -1)
    7995              :                 {
    7996          303 :                   gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
    7997          303 :                                              fsym->attr.intent,
    7998          303 :                                              fsym->attr.pointer);
    7999              :                 }
    8000              :               else
    8001              :                 /* This is where we introduce a temporary to store the
    8002              :                    result of a non-lvalue array expression.  */
    8003        50959 :                 gfc_conv_array_parameter (&parmse, e, nodesc_arg, fsym,
    8004              :                                           sym->name, NULL);
    8005              : 
    8006              :               /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
    8007              :                  allocated on entry, it must be deallocated.
    8008              :                  CFI descriptors are handled elsewhere.  */
    8009        54362 :               if (fsym && fsym->attr.allocatable
    8010         1783 :                   && fsym->attr.intent == INTENT_OUT
    8011        57741 :                   && !is_CFI_desc (fsym, NULL))
    8012              :                 {
    8013          157 :                   if (fsym->ts.type == BT_DERIVED
    8014           45 :                       && fsym->ts.u.derived->attr.alloc_comp)
    8015              :                   {
    8016              :                     // deallocate the components first
    8017            9 :                     tmp = gfc_deallocate_alloc_comp (fsym->ts.u.derived,
    8018              :                                                      parmse.expr, e->rank);
    8019              :                     /* But check whether dummy argument is optional.  */
    8020            9 :                     if (tmp != NULL_TREE
    8021            9 :                         && fsym->attr.optional
    8022            6 :                         && e->expr_type == EXPR_VARIABLE
    8023            6 :                         && e->symtree->n.sym->attr.optional)
    8024              :                       {
    8025            6 :                         tree present;
    8026            6 :                         present = gfc_conv_expr_present (e->symtree->n.sym);
    8027            6 :                         tmp = build3_v (COND_EXPR, present, tmp,
    8028              :                                         build_empty_stmt (input_location));
    8029              :                       }
    8030            9 :                     if (tmp != NULL_TREE)
    8031            9 :                       gfc_add_expr_to_block (&dealloc_blk, tmp);
    8032              :                   }
    8033              : 
    8034          157 :                   tmp = parmse.expr;
    8035              :                   /* With bind(C), the actual argument is replaced by a bind-C
    8036              :                      descriptor; in this case, the data component arrives here,
    8037              :                      which shall not be dereferenced, but still freed and
    8038              :                      nullified.  */
    8039          157 :                   if  (TREE_TYPE(tmp) != pvoid_type_node)
    8040          157 :                     tmp = build_fold_indirect_ref_loc (input_location,
    8041              :                                                        parmse.expr);
    8042          157 :                   tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
    8043              :                                                     NULL_TREE, NULL_TREE, true,
    8044              :                                                     e,
    8045              :                                                     GFC_CAF_COARRAY_NOCOARRAY);
    8046          157 :                   if (fsym->attr.optional
    8047           48 :                       && e->expr_type == EXPR_VARIABLE
    8048           48 :                       && e->symtree->n.sym->attr.optional)
    8049           48 :                     tmp = fold_build3_loc (input_location, COND_EXPR,
    8050              :                                      void_type_node,
    8051           24 :                                      gfc_conv_expr_present (e->symtree->n.sym),
    8052              :                                        tmp, build_empty_stmt (input_location));
    8053          157 :                   gfc_add_expr_to_block (&dealloc_blk, tmp);
    8054              :                 }
    8055              :             }
    8056              :         }
    8057              :       /* Special case for an assumed-rank dummy argument. */
    8058       270308 :       if (!sym->attr.is_bind_c && e && fsym && e->rank > 0
    8059        56644 :           && (fsym->ts.type == BT_CLASS
    8060        56644 :               ? (CLASS_DATA (fsym)->as
    8061         4564 :                  && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
    8062        52080 :               : (fsym->as && fsym->as->type == AS_ASSUMED_RANK)))
    8063              :         {
    8064        12731 :           if (fsym->ts.type == BT_CLASS
    8065        12731 :               ? (CLASS_DATA (fsym)->attr.class_pointer
    8066         1055 :                  || CLASS_DATA (fsym)->attr.allocatable)
    8067        11676 :               : (fsym->attr.pointer || fsym->attr.allocatable))
    8068              :             {
    8069              :               /* Unallocated allocatable arrays and unassociated pointer
    8070              :                  arrays need their dtype setting if they are argument
    8071              :                  associated with assumed rank dummies to set the rank.  */
    8072          891 :               set_dtype_for_unallocated (&parmse, e);
    8073              :             }
    8074        11840 :           else if (e->expr_type == EXPR_VARIABLE
    8075        11361 :                    && e->symtree->n.sym->attr.dummy
    8076          698 :                    && (e->ts.type == BT_CLASS
    8077          891 :                        ? (e->ref && e->ref->next
    8078          193 :                           && e->ref->next->type == REF_ARRAY
    8079          193 :                           && e->ref->next->u.ar.type == AR_FULL
    8080          386 :                           && e->ref->next->u.ar.as->type == AS_ASSUMED_SIZE)
    8081          505 :                        : (e->ref && e->ref->type == REF_ARRAY
    8082          505 :                           && e->ref->u.ar.type == AR_FULL
    8083          733 :                           && e->ref->u.ar.as->type == AS_ASSUMED_SIZE)))
    8084              :             {
    8085              :               /* Assumed-size actual to assumed-rank dummy requires
    8086              :                  dim[rank-1].ubound = -1. */
    8087          180 :               tree minus_one;
    8088          180 :               tmp = build_fold_indirect_ref_loc (input_location, parmse.expr);
    8089          180 :               if (fsym->ts.type == BT_CLASS)
    8090           60 :                 tmp = gfc_class_data_get (tmp);
    8091          180 :               minus_one = build_int_cst (gfc_array_index_type, -1);
    8092          180 :               gfc_conv_descriptor_ubound_set (&parmse.pre, tmp,
    8093          180 :                                               gfc_rank_cst[e->rank - 1],
    8094              :                                               minus_one);
    8095              :             }
    8096              :         }
    8097              : 
    8098              :       /* The case with fsym->attr.optional is that of a user subroutine
    8099              :          with an interface indicating an optional argument.  When we call
    8100              :          an intrinsic subroutine, however, fsym is NULL, but we might still
    8101              :          have an optional argument, so we proceed to the substitution
    8102              :          just in case.  Arguments passed to bind(c) procedures via CFI
    8103              :          descriptors are handled elsewhere.  */
    8104       257338 :       if (e && (fsym == NULL || fsym->attr.optional)
    8105       330694 :           && !(sym->attr.is_bind_c && is_CFI_desc (fsym, NULL)))
    8106              :         {
    8107              :           /* If an optional argument is itself an optional dummy argument,
    8108              :              check its presence and substitute a null if absent.  This is
    8109              :              only needed when passing an array to an elemental procedure
    8110              :              as then array elements are accessed - or no NULL pointer is
    8111              :              allowed and a "1" or "0" should be passed if not present.
    8112              :              When passing a non-array-descriptor full array to a
    8113              :              non-array-descriptor dummy, no check is needed. For
    8114              :              array-descriptor actual to array-descriptor dummy, see
    8115              :              PR 41911 for why a check has to be inserted.
    8116              :              fsym == NULL is checked as intrinsics required the descriptor
    8117              :              but do not always set fsym.
    8118              :              Also, it is necessary to pass a NULL pointer to library routines
    8119              :              which usually ignore optional arguments, so they can handle
    8120              :              these themselves.  */
    8121        59292 :           if (e->expr_type == EXPR_VARIABLE
    8122        26416 :               && e->symtree->n.sym->attr.optional
    8123         2421 :               && (((e->rank != 0 && elemental_proc)
    8124         2246 :                    || e->representation.length || e->ts.type == BT_CHARACTER
    8125         2020 :                    || (e->rank == 0 && e->symtree->n.sym->attr.value)
    8126         1910 :                    || (e->rank != 0
    8127         1070 :                        && (fsym == NULL
    8128         1034 :                            || (fsym->as
    8129          272 :                                && (fsym->as->type == AS_ASSUMED_SHAPE
    8130          235 :                                    || fsym->as->type == AS_ASSUMED_RANK
    8131          117 :                                    || fsym->as->type == AS_DEFERRED)))))
    8132         1685 :                   || se->ignore_optional))
    8133          764 :             gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
    8134          764 :                                     e->representation.length);
    8135              :         }
    8136              : 
    8137              :       /* Make the class container for the first argument available with class
    8138              :          valued transformational functions.  */
    8139       270308 :       if (argc == 0 && e && e->ts.type == BT_CLASS
    8140         4949 :           && isym && isym->transformational
    8141           84 :           && se->ss && se->ss->info)
    8142              :         {
    8143           84 :           arg1_cntnr = parmse.expr;
    8144           84 :           if (POINTER_TYPE_P (TREE_TYPE (arg1_cntnr)))
    8145           84 :             arg1_cntnr = build_fold_indirect_ref_loc (input_location, arg1_cntnr);
    8146           84 :           arg1_cntnr = gfc_get_class_from_expr (arg1_cntnr);
    8147           84 :           se->ss->info->class_container = arg1_cntnr;
    8148              :         }
    8149              : 
    8150              :       /* Obtain the character length of an assumed character length procedure
    8151              :          from the typespec of the actual argument.  */
    8152       270308 :       if (e
    8153       257338 :           && parmse.string_length == NULL_TREE
    8154       221910 :           && e->ts.type == BT_PROCEDURE
    8155         1923 :           && e->symtree->n.sym->ts.type == BT_CHARACTER
    8156           21 :           && e->symtree->n.sym->ts.u.cl->length != NULL
    8157           21 :           && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
    8158              :         {
    8159           13 :           gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
    8160           13 :           parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
    8161              :         }
    8162              : 
    8163       270308 :       if (fsym && e)
    8164              :         {
    8165              :           /* Obtain the character length for a NULL() actual with a character
    8166              :              MOLD argument.  Otherwise substitute a suitable dummy length.
    8167              :              Here we handle non-optional dummies of non-bind(c) procedures.  */
    8168       225471 :           if (e->expr_type == EXPR_NULL
    8169          745 :               && fsym->ts.type == BT_CHARACTER
    8170          296 :               && !fsym->attr.optional
    8171       225689 :               && !(sym->attr.is_bind_c && is_CFI_desc (fsym, NULL)))
    8172          216 :             conv_null_actual (&parmse, e, fsym);
    8173              :         }
    8174              : 
    8175              :       /* If any actual argument of the procedure is allocatable and passed
    8176              :          to an allocatable dummy with INTENT(OUT), we conservatively
    8177              :          evaluate actual argument expressions before deallocations are
    8178              :          performed and the procedure is executed.  May create temporaries.
    8179              :          This ensures we conform to F2023:15.5.3, 15.5.4.  */
    8180       257338 :       if (e && fsym && force_eval_args
    8181         1103 :           && fsym->attr.intent != INTENT_OUT
    8182       270717 :           && !gfc_is_constant_expr (e))
    8183          268 :         parmse.expr = gfc_evaluate_now (parmse.expr, &parmse.pre);
    8184              : 
    8185       270308 :       if (fsym && need_interface_mapping && e)
    8186        40472 :         gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
    8187              : 
    8188       270308 :       gfc_add_block_to_block (&se->pre, &parmse.pre);
    8189       270308 :       gfc_add_block_to_block (&post, &parmse.post);
    8190       270308 :       gfc_add_block_to_block (&se->finalblock, &parmse.finalblock);
    8191              : 
    8192              :       /* Allocated allocatable components of derived types must be
    8193              :          deallocated for non-variable scalars, array arguments to elemental
    8194              :          procedures, and array arguments with descriptor to non-elemental
    8195              :          procedures.  As bounds information for descriptorless arrays is no
    8196              :          longer available here, they are dealt with in trans-array.cc
    8197              :          (gfc_conv_array_parameter).  */
    8198       257338 :       if (e && (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS)
    8199        28189 :             && e->ts.u.derived->attr.alloc_comp
    8200         7536 :             && (e->rank == 0 || elemental_proc || !nodesc_arg)
    8201       277712 :             && !expr_may_alias_variables (e, elemental_proc))
    8202              :         {
    8203          354 :           int parm_rank;
    8204              :           /* It is known the e returns a structure type with at least one
    8205              :              allocatable component.  When e is a function, ensure that the
    8206              :              function is called once only by using a temporary variable.  */
    8207          354 :           if (!DECL_P (parmse.expr) && e->expr_type == EXPR_FUNCTION)
    8208          140 :             parmse.expr = gfc_evaluate_now_loc (input_location,
    8209              :                                                 parmse.expr, &se->pre);
    8210              : 
    8211          354 :           if ((fsym && fsym->attr.value) || e->expr_type == EXPR_ARRAY)
    8212          140 :             tmp = parmse.expr;
    8213              :           else
    8214          214 :             tmp = build_fold_indirect_ref_loc (input_location,
    8215              :                                                parmse.expr);
    8216              : 
    8217          354 :           parm_rank = e->rank;
    8218          354 :           switch (parm_kind)
    8219              :             {
    8220              :             case (ELEMENTAL):
    8221              :             case (SCALAR):
    8222          354 :               parm_rank = 0;
    8223              :               break;
    8224              : 
    8225            0 :             case (SCALAR_POINTER):
    8226            0 :               tmp = build_fold_indirect_ref_loc (input_location,
    8227              :                                              tmp);
    8228            0 :               break;
    8229              :             }
    8230              : 
    8231          354 :           if (e->ts.type == BT_DERIVED && fsym && fsym->ts.type == BT_CLASS)
    8232              :             {
    8233              :               /* The derived type is passed to gfc_deallocate_alloc_comp.
    8234              :                  Therefore, class actuals can be handled correctly but derived
    8235              :                  types passed to class formals need the _data component.  */
    8236           82 :               tmp = gfc_class_data_get (tmp);
    8237           82 :               if (!CLASS_DATA (fsym)->attr.dimension)
    8238              :                 {
    8239           56 :                   if (UNLIMITED_POLY (fsym))
    8240              :                     {
    8241           12 :                       tree type = gfc_typenode_for_spec (&e->ts);
    8242           12 :                       type = build_pointer_type (type);
    8243           12 :                       tmp = fold_convert (type, tmp);
    8244              :                     }
    8245           56 :                   tmp = build_fold_indirect_ref_loc (input_location, tmp);
    8246              :                 }
    8247              :             }
    8248              : 
    8249          354 :           if (e->expr_type == EXPR_OP
    8250           24 :                 && e->value.op.op == INTRINSIC_PARENTHESES
    8251           24 :                 && e->value.op.op1->expr_type == EXPR_VARIABLE)
    8252              :             {
    8253           24 :               tree local_tmp;
    8254           24 :               local_tmp = gfc_evaluate_now (tmp, &se->pre);
    8255           24 :               local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp,
    8256              :                                                parm_rank, 0);
    8257           24 :               gfc_add_expr_to_block (&se->post, local_tmp);
    8258              :             }
    8259              : 
    8260              :           /* Items of array expressions passed to a polymorphic formal arguments
    8261              :              create their own clean up, so prevent double free.  */
    8262          354 :           if (!finalized && !e->must_finalize
    8263          353 :               && !(e->expr_type == EXPR_ARRAY && fsym
    8264           74 :                    && fsym->ts.type == BT_CLASS))
    8265              :             {
    8266          333 :               bool scalar_res_outside_loop;
    8267          987 :               scalar_res_outside_loop = e->expr_type == EXPR_FUNCTION
    8268          151 :                                         && parm_rank == 0
    8269          472 :                                         && parmse.loop;
    8270              : 
    8271              :               /* Scalars passed to an assumed rank argument are converted to
    8272              :                  a descriptor. Obtain the data field before deallocating any
    8273              :                  allocatable components.  */
    8274          292 :               if (parm_rank == 0 && e->expr_type != EXPR_ARRAY
    8275          588 :                   && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
    8276           19 :                 tmp = gfc_conv_descriptor_data_get (tmp);
    8277              : 
    8278          333 :               if (scalar_res_outside_loop)
    8279              :                 {
    8280              :                   /* Go through the ss chain to find the argument and use
    8281              :                      the stored value.  */
    8282           30 :                   gfc_ss *tmp_ss = parmse.loop->ss;
    8283           72 :                   for (; tmp_ss; tmp_ss = tmp_ss->next)
    8284           60 :                     if (tmp_ss->info
    8285           48 :                         && tmp_ss->info->expr == e
    8286           18 :                         && tmp_ss->info->data.scalar.value != NULL_TREE)
    8287              :                       {
    8288           18 :                         tmp = tmp_ss->info->data.scalar.value;
    8289           18 :                         break;
    8290              :                       }
    8291              :                 }
    8292              : 
    8293          333 :               STRIP_NOPS (tmp);
    8294              : 
    8295          333 :               if (derived_array != NULL_TREE)
    8296            0 :                 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived,
    8297              :                                                  derived_array,
    8298              :                                                  parm_rank);
    8299          333 :               else if ((e->ts.type == BT_CLASS
    8300           24 :                         && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
    8301          333 :                        || e->ts.type == BT_DERIVED)
    8302          333 :                 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp,
    8303              :                                                  parm_rank, 0, true);
    8304            0 :               else if (e->ts.type == BT_CLASS)
    8305            0 :                 tmp = gfc_deallocate_alloc_comp (CLASS_DATA (e)->ts.u.derived,
    8306              :                                                  tmp, parm_rank);
    8307              : 
    8308          333 :               if (scalar_res_outside_loop)
    8309           30 :                 gfc_add_expr_to_block (&parmse.loop->post, tmp);
    8310              :               else
    8311          303 :                 gfc_prepend_expr_to_block (&post, tmp);
    8312              :             }
    8313              :         }
    8314              : 
    8315              :       /* Add argument checking of passing an unallocated/NULL actual to
    8316              :          a nonallocatable/nonpointer dummy.  */
    8317              : 
    8318       270308 :       if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
    8319              :         {
    8320         6546 :           symbol_attribute attr;
    8321         6546 :           char *msg;
    8322         6546 :           tree cond;
    8323         6546 :           tree tmp;
    8324         6546 :           symbol_attribute fsym_attr;
    8325              : 
    8326         6546 :           if (fsym)
    8327              :             {
    8328         6385 :               if (fsym->ts.type == BT_CLASS)
    8329              :                 {
    8330          321 :                   fsym_attr = CLASS_DATA (fsym)->attr;
    8331          321 :                   fsym_attr.pointer = fsym_attr.class_pointer;
    8332              :                 }
    8333              :               else
    8334         6064 :                 fsym_attr = fsym->attr;
    8335              :             }
    8336              : 
    8337         6546 :           if (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
    8338         4094 :             attr = gfc_expr_attr (e);
    8339              :           else
    8340         6081 :             goto end_pointer_check;
    8341              : 
    8342              :           /*  In Fortran 2008 it's allowed to pass a NULL pointer/nonallocated
    8343              :               allocatable to an optional dummy, cf. 12.5.2.12.  */
    8344         4094 :           if (fsym != NULL && fsym->attr.optional && !attr.proc_pointer
    8345         1038 :               && (gfc_option.allow_std & GFC_STD_F2008) != 0)
    8346         1032 :             goto end_pointer_check;
    8347              : 
    8348         3062 :           if (attr.optional)
    8349              :             {
    8350              :               /* If the actual argument is an optional pointer/allocatable and
    8351              :                  the formal argument takes an nonpointer optional value,
    8352              :                  it is invalid to pass a non-present argument on, even
    8353              :                  though there is no technical reason for this in gfortran.
    8354              :                  See Fortran 2003, Section 12.4.1.6 item (7)+(8).  */
    8355           60 :               tree present, null_ptr, type;
    8356              : 
    8357           60 :               if (attr.allocatable
    8358            0 :                   && (fsym == NULL || !fsym_attr.allocatable))
    8359            0 :                 msg = xasprintf ("Allocatable actual argument '%s' is not "
    8360              :                                  "allocated or not present",
    8361            0 :                                  e->symtree->n.sym->name);
    8362           60 :               else if (attr.pointer
    8363           12 :                        && (fsym == NULL || !fsym_attr.pointer))
    8364           12 :                 msg = xasprintf ("Pointer actual argument '%s' is not "
    8365              :                                  "associated or not present",
    8366           12 :                                  e->symtree->n.sym->name);
    8367           48 :               else if (attr.proc_pointer && !e->value.function.actual
    8368            0 :                        && (fsym == NULL || !fsym_attr.proc_pointer))
    8369            0 :                 msg = xasprintf ("Proc-pointer actual argument '%s' is not "
    8370              :                                  "associated or not present",
    8371            0 :                                  e->symtree->n.sym->name);
    8372              :               else
    8373           48 :                 goto end_pointer_check;
    8374              : 
    8375           12 :               present = gfc_conv_expr_present (e->symtree->n.sym);
    8376           12 :               type = TREE_TYPE (present);
    8377           12 :               present = fold_build2_loc (input_location, EQ_EXPR,
    8378              :                                          logical_type_node, present,
    8379              :                                          fold_convert (type,
    8380              :                                                        null_pointer_node));
    8381           12 :               type = TREE_TYPE (parmse.expr);
    8382           12 :               null_ptr = fold_build2_loc (input_location, EQ_EXPR,
    8383              :                                           logical_type_node, parmse.expr,
    8384              :                                           fold_convert (type,
    8385              :                                                         null_pointer_node));
    8386           12 :               cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    8387              :                                       logical_type_node, present, null_ptr);
    8388              :             }
    8389              :           else
    8390              :             {
    8391         3002 :               if (attr.allocatable
    8392          256 :                   && (fsym == NULL || !fsym_attr.allocatable))
    8393          190 :                 msg = xasprintf ("Allocatable actual argument '%s' is not "
    8394          190 :                                  "allocated", e->symtree->n.sym->name);
    8395         2812 :               else if (attr.pointer
    8396          272 :                        && (fsym == NULL || !fsym_attr.pointer))
    8397          184 :                 msg = xasprintf ("Pointer actual argument '%s' is not "
    8398          184 :                                  "associated", e->symtree->n.sym->name);
    8399         2628 :               else if (attr.proc_pointer && !e->value.function.actual
    8400           80 :                        && (fsym == NULL
    8401           50 :                            || (!fsym_attr.proc_pointer && !fsym_attr.optional)))
    8402           79 :                 msg = xasprintf ("Proc-pointer actual argument '%s' is not "
    8403           79 :                                  "associated", e->symtree->n.sym->name);
    8404              :               else
    8405         2549 :                 goto end_pointer_check;
    8406              : 
    8407          453 :               tmp = parmse.expr;
    8408          453 :               if (fsym && fsym->ts.type == BT_CLASS && !attr.proc_pointer)
    8409              :                 {
    8410           76 :                   if (POINTER_TYPE_P (TREE_TYPE (tmp)))
    8411           70 :                     tmp = build_fold_indirect_ref_loc (input_location, tmp);
    8412           76 :                   tmp = gfc_class_data_get (tmp);
    8413           76 :                   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
    8414            3 :                     tmp = gfc_conv_descriptor_data_get (tmp);
    8415              :                 }
    8416              : 
    8417              :               /* If the argument is passed by value, we need to strip the
    8418              :                  INDIRECT_REF.  */
    8419          453 :               if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
    8420           12 :                 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
    8421              : 
    8422          453 :               cond = fold_build2_loc (input_location, EQ_EXPR,
    8423              :                                       logical_type_node, tmp,
    8424          453 :                                       fold_convert (TREE_TYPE (tmp),
    8425              :                                                     null_pointer_node));
    8426              :             }
    8427              : 
    8428          465 :           gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
    8429              :                                    msg);
    8430          465 :           free (msg);
    8431              :         }
    8432       263762 :       end_pointer_check:
    8433              : 
    8434              :       /* Deferred length dummies pass the character length by reference
    8435              :          so that the value can be returned.  */
    8436       270308 :       if (parmse.string_length && fsym && fsym->ts.deferred)
    8437              :         {
    8438          795 :           if (INDIRECT_REF_P (parmse.string_length))
    8439              :             {
    8440              :               /* In chains of functions/procedure calls the string_length already
    8441              :                  is a pointer to the variable holding the length.  Therefore
    8442              :                  remove the deref on call.  */
    8443           90 :               tmp = parmse.string_length;
    8444           90 :               parmse.string_length = TREE_OPERAND (parmse.string_length, 0);
    8445              :             }
    8446              :           else
    8447              :             {
    8448          705 :               tmp = parmse.string_length;
    8449          705 :               if (!VAR_P (tmp) && TREE_CODE (tmp) != COMPONENT_REF)
    8450           61 :                 tmp = gfc_evaluate_now (parmse.string_length, &se->pre);
    8451          705 :               parmse.string_length = gfc_build_addr_expr (NULL_TREE, tmp);
    8452              :             }
    8453              : 
    8454          795 :           if (e && e->expr_type == EXPR_VARIABLE
    8455          638 :               && fsym->attr.allocatable
    8456          368 :               && e->ts.u.cl->backend_decl
    8457          368 :               && VAR_P (e->ts.u.cl->backend_decl))
    8458              :             {
    8459          284 :               if (INDIRECT_REF_P (tmp))
    8460            0 :                 tmp = TREE_OPERAND (tmp, 0);
    8461          284 :               gfc_add_modify (&se->post, e->ts.u.cl->backend_decl,
    8462              :                               fold_convert (gfc_charlen_type_node, tmp));
    8463              :             }
    8464              :         }
    8465              : 
    8466              :       /* Character strings are passed as two parameters, a length and a
    8467              :          pointer - except for Bind(c) and c_ptrs which only pass the pointer.
    8468              :          An unlimited polymorphic formal argument likewise does not
    8469              :          need the length.  */
    8470       270308 :       if (parmse.string_length != NULL_TREE
    8471        36826 :           && !sym->attr.is_bind_c
    8472        36130 :           && !(fsym && fsym->ts.type == BT_DERIVED && fsym->ts.u.derived
    8473            6 :                && fsym->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
    8474            6 :                && fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING )
    8475        30250 :           && !(fsym && fsym->ts.type == BT_ASSUMED)
    8476        30141 :           && !(fsym && UNLIMITED_POLY (fsym)))
    8477        35840 :         vec_safe_push (stringargs, parmse.string_length);
    8478              : 
    8479              :       /* When calling __copy for character expressions to unlimited
    8480              :          polymorphic entities, the dst argument needs a string length.  */
    8481        51804 :       if (sym->name[0] == '_' && e && e->ts.type == BT_CHARACTER
    8482         5323 :           && startswith (sym->name, "__vtab_CHARACTER")
    8483            0 :           && arg->next && arg->next->expr
    8484            0 :           && (arg->next->expr->ts.type == BT_DERIVED
    8485            0 :               || arg->next->expr->ts.type == BT_CLASS)
    8486       270308 :           && arg->next->expr->ts.u.derived->attr.unlimited_polymorphic)
    8487            0 :         vec_safe_push (stringargs, parmse.string_length);
    8488              : 
    8489              :       /* For descriptorless coarrays and assumed-shape coarray dummies, we
    8490              :          pass the token and the offset as additional arguments.  */
    8491       270308 :       if (fsym && e == NULL && flag_coarray == GFC_FCOARRAY_LIB
    8492          122 :           && attr->codimension && !attr->allocatable)
    8493              :         {
    8494              :           /* Token and offset.  */
    8495            5 :           vec_safe_push (stringargs, null_pointer_node);
    8496            5 :           vec_safe_push (stringargs, build_int_cst (gfc_array_index_type, 0));
    8497            5 :           gcc_assert (fsym->attr.optional);
    8498              :         }
    8499       237378 :       else if (fsym && flag_coarray == GFC_FCOARRAY_LIB && attr->codimension
    8500          145 :                && !attr->allocatable)
    8501              :         {
    8502          123 :           tree caf_decl, caf_type, caf_desc = NULL_TREE;
    8503          123 :           tree offset, tmp2;
    8504              : 
    8505          123 :           caf_decl = gfc_get_tree_for_caf_expr (e);
    8506          123 :           caf_type = TREE_TYPE (caf_decl);
    8507          123 :           if (POINTER_TYPE_P (caf_type)
    8508          123 :               && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_type)))
    8509            3 :             caf_desc = TREE_TYPE (caf_type);
    8510          120 :           else if (GFC_DESCRIPTOR_TYPE_P (caf_type))
    8511              :             caf_desc = caf_type;
    8512              : 
    8513           51 :           if (caf_desc
    8514           51 :               && (GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_ALLOCATABLE
    8515            0 :                   || GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_POINTER))
    8516              :             {
    8517          102 :               tmp = POINTER_TYPE_P (TREE_TYPE (caf_decl))
    8518           54 :                       ? build_fold_indirect_ref (caf_decl)
    8519              :                       : caf_decl;
    8520           51 :               tmp = gfc_conv_descriptor_token (tmp);
    8521              :             }
    8522           72 :           else if (DECL_LANG_SPECIFIC (caf_decl)
    8523           72 :                    && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
    8524           12 :             tmp = GFC_DECL_TOKEN (caf_decl);
    8525              :           else
    8526              :             {
    8527           60 :               gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
    8528              :                           && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
    8529           60 :               tmp = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
    8530              :             }
    8531              : 
    8532          123 :           vec_safe_push (stringargs, tmp);
    8533              : 
    8534          123 :           if (caf_desc
    8535          123 :               && GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_ALLOCATABLE)
    8536           51 :             offset = build_int_cst (gfc_array_index_type, 0);
    8537           72 :           else if (DECL_LANG_SPECIFIC (caf_decl)
    8538           72 :                    && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
    8539           12 :             offset = GFC_DECL_CAF_OFFSET (caf_decl);
    8540           60 :           else if (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) != NULL_TREE)
    8541            0 :             offset = GFC_TYPE_ARRAY_CAF_OFFSET (caf_type);
    8542              :           else
    8543           60 :             offset = build_int_cst (gfc_array_index_type, 0);
    8544              : 
    8545          123 :           if (caf_desc)
    8546              :             {
    8547          102 :               tmp = POINTER_TYPE_P (TREE_TYPE (caf_decl))
    8548           54 :                       ? build_fold_indirect_ref (caf_decl)
    8549              :                       : caf_decl;
    8550           51 :               tmp = gfc_conv_descriptor_data_get (tmp);
    8551              :             }
    8552              :           else
    8553              :             {
    8554           72 :               gcc_assert (POINTER_TYPE_P (caf_type));
    8555           72 :               tmp = caf_decl;
    8556              :             }
    8557              : 
    8558          108 :           tmp2 = fsym->ts.type == BT_CLASS
    8559          123 :                  ? gfc_class_data_get (parmse.expr) : parmse.expr;
    8560          123 :           if ((fsym->ts.type != BT_CLASS
    8561          108 :                && (fsym->as->type == AS_ASSUMED_SHAPE
    8562           59 :                    || fsym->as->type == AS_ASSUMED_RANK))
    8563           74 :               || (fsym->ts.type == BT_CLASS
    8564           15 :                   && (CLASS_DATA (fsym)->as->type == AS_ASSUMED_SHAPE
    8565           10 :                       || CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)))
    8566              :             {
    8567           54 :               if (fsym->ts.type == BT_CLASS)
    8568            5 :                 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (tmp2)));
    8569              :               else
    8570              :                 {
    8571           49 :                   gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
    8572           49 :                   tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
    8573              :                 }
    8574           54 :               gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)));
    8575           54 :               tmp2 = gfc_conv_descriptor_data_get (tmp2);
    8576              :             }
    8577           69 :           else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
    8578           10 :             tmp2 = gfc_conv_descriptor_data_get (tmp2);
    8579              :           else
    8580              :             {
    8581           59 :               gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
    8582              :             }
    8583              : 
    8584          123 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    8585              :                                  gfc_array_index_type,
    8586              :                                  fold_convert (gfc_array_index_type, tmp2),
    8587              :                                  fold_convert (gfc_array_index_type, tmp));
    8588          123 :           offset = fold_build2_loc (input_location, PLUS_EXPR,
    8589              :                                     gfc_array_index_type, offset, tmp);
    8590              : 
    8591          123 :           vec_safe_push (stringargs, offset);
    8592              :         }
    8593              : 
    8594       270308 :       vec_safe_push (arglist, parmse.expr);
    8595              :     }
    8596              : 
    8597       129943 :   gfc_add_block_to_block (&se->pre, &dealloc_blk);
    8598       129943 :   gfc_add_block_to_block (&se->pre, &clobbers);
    8599       129943 :   gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
    8600              : 
    8601       129943 :   if (comp)
    8602         1980 :     ts = comp->ts;
    8603       127963 :   else if (sym->ts.type == BT_CLASS)
    8604          850 :     ts = CLASS_DATA (sym)->ts;
    8605              :   else
    8606       127113 :     ts = sym->ts;
    8607              : 
    8608       129943 :   if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
    8609          210 :     se->string_length = build_int_cst (gfc_charlen_type_node, 1);
    8610       129733 :   else if (ts.type == BT_CHARACTER)
    8611              :     {
    8612         5008 :       if (ts.u.cl->length == NULL)
    8613              :         {
    8614              :           /* Assumed character length results are not allowed by C418 of the 2003
    8615              :              standard and are trapped in resolve.cc; except in the case of SPREAD
    8616              :              (and other intrinsics?) and dummy functions.  In the case of SPREAD,
    8617              :              we take the character length of the first argument for the result.
    8618              :              For dummies, we have to look through the formal argument list for
    8619              :              this function and use the character length found there.
    8620              :              Likewise, we handle the case of deferred-length character dummy
    8621              :              arguments to intrinsics that determine the characteristics of
    8622              :              the result, which cannot be deferred-length.  */
    8623         2302 :           if (expr->value.function.isym)
    8624         1703 :             ts.deferred = false;
    8625         2302 :           if (ts.deferred)
    8626          592 :             cl.backend_decl = gfc_create_var (gfc_charlen_type_node, "slen");
    8627         1710 :           else if (!sym->attr.dummy)
    8628         1703 :             cl.backend_decl = (*stringargs)[0];
    8629              :           else
    8630              :             {
    8631            7 :               formal = gfc_sym_get_dummy_args (sym->ns->proc_name);
    8632           26 :               for (; formal; formal = formal->next)
    8633           12 :                 if (strcmp (formal->sym->name, sym->name) == 0)
    8634            7 :                   cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
    8635              :             }
    8636         2302 :           len = cl.backend_decl;
    8637              :         }
    8638              :       else
    8639              :         {
    8640         2706 :           tree tmp;
    8641              : 
    8642              :           /* Calculate the length of the returned string.  */
    8643         2706 :           gfc_init_se (&parmse, NULL);
    8644         2706 :           if (need_interface_mapping)
    8645         1867 :             gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
    8646              :           else
    8647          839 :             gfc_conv_expr (&parmse, ts.u.cl->length);
    8648         2706 :           gfc_add_block_to_block (&se->pre, &parmse.pre);
    8649         2706 :           gfc_add_block_to_block (&se->post, &parmse.post);
    8650         2706 :           tmp = parmse.expr;
    8651              :           /* TODO: It would be better to have the charlens as
    8652              :              gfc_charlen_type_node already when the interface is
    8653              :              created instead of converting it here (see PR 84615).  */
    8654         2706 :           tmp = fold_build2_loc (input_location, MAX_EXPR,
    8655              :                                  gfc_charlen_type_node,
    8656              :                                  fold_convert (gfc_charlen_type_node, tmp),
    8657              :                                  build_zero_cst (gfc_charlen_type_node));
    8658         2706 :           cl.backend_decl = tmp;
    8659              :         }
    8660              : 
    8661              :       /* Set up a charlen structure for it.  */
    8662         5008 :       cl.next = NULL;
    8663         5008 :       cl.length = NULL;
    8664         5008 :       ts.u.cl = &cl;
    8665              : 
    8666         5008 :       len = cl.backend_decl;
    8667              :     }
    8668              : 
    8669         1980 :   byref = (comp && (comp->attr.dimension
    8670         1911 :            || (comp->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)))
    8671       129943 :            || (!comp && gfc_return_by_reference (sym));
    8672              : 
    8673        18754 :   if (byref)
    8674              :     {
    8675        18754 :       if (se->direct_byref)
    8676              :         {
    8677              :           /* Sometimes, too much indirection can be applied; e.g. for
    8678              :              function_result = array_valued_recursive_function.  */
    8679         6986 :           if (TREE_TYPE (TREE_TYPE (se->expr))
    8680         6986 :                 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
    8681         7004 :                 && GFC_DESCRIPTOR_TYPE_P
    8682              :                         (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
    8683           18 :             se->expr = build_fold_indirect_ref_loc (input_location,
    8684              :                                                     se->expr);
    8685              : 
    8686              :           /* If the lhs of an assignment x = f(..) is allocatable and
    8687              :              f2003 is allowed, we must do the automatic reallocation.
    8688              :              TODO - deal with intrinsics, without using a temporary.  */
    8689         6986 :           if (flag_realloc_lhs
    8690         6911 :                 && se->ss && se->ss->loop_chain
    8691          203 :                 && se->ss->loop_chain->is_alloc_lhs
    8692          203 :                 && !expr->value.function.isym
    8693          203 :                 && sym->result->as != NULL)
    8694              :             {
    8695              :               /* Evaluate the bounds of the result, if known.  */
    8696          203 :               gfc_set_loop_bounds_from_array_spec (&mapping, se,
    8697              :                                                    sym->result->as);
    8698              : 
    8699              :               /* Perform the automatic reallocation.  */
    8700          203 :               tmp = gfc_alloc_allocatable_for_assignment (se->loop,
    8701              :                                                           expr, NULL);
    8702          203 :               gfc_add_expr_to_block (&se->pre, tmp);
    8703              : 
    8704              :               /* Pass the temporary as the first argument.  */
    8705          203 :               result = info->descriptor;
    8706              :             }
    8707              :           else
    8708         6783 :             result = build_fold_indirect_ref_loc (input_location,
    8709              :                                                   se->expr);
    8710         6986 :           vec_safe_push (retargs, se->expr);
    8711              :         }
    8712        11768 :       else if (comp && comp->attr.dimension)
    8713              :         {
    8714           66 :           gcc_assert (se->loop && info);
    8715              : 
    8716              :           /* Set the type of the array. vtable charlens are not always reliable.
    8717              :              Use the interface, if possible.  */
    8718           66 :           if (comp->ts.type == BT_CHARACTER
    8719            1 :               && expr->symtree->n.sym->ts.type == BT_CLASS
    8720            1 :               && comp->ts.interface && comp->ts.interface->result)
    8721            1 :             tmp = gfc_typenode_for_spec (&comp->ts.interface->result->ts);
    8722              :           else
    8723           65 :             tmp = gfc_typenode_for_spec (&comp->ts);
    8724           66 :           gcc_assert (se->ss->dimen == se->loop->dimen);
    8725              : 
    8726              :           /* Evaluate the bounds of the result, if known.  */
    8727           66 :           gfc_set_loop_bounds_from_array_spec (&mapping, se, comp->as);
    8728              : 
    8729              :           /* If the lhs of an assignment x = f(..) is allocatable and
    8730              :              f2003 is allowed, we must not generate the function call
    8731              :              here but should just send back the results of the mapping.
    8732              :              This is signalled by the function ss being flagged.  */
    8733           66 :           if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
    8734              :             {
    8735            0 :               gfc_free_interface_mapping (&mapping);
    8736            0 :               return has_alternate_specifier;
    8737              :             }
    8738              : 
    8739              :           /* Create a temporary to store the result.  In case the function
    8740              :              returns a pointer, the temporary will be a shallow copy and
    8741              :              mustn't be deallocated.  */
    8742           66 :           callee_alloc = comp->attr.allocatable || comp->attr.pointer;
    8743           66 :           gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
    8744              :                                        tmp, NULL_TREE, false,
    8745              :                                        !comp->attr.pointer, callee_alloc,
    8746           66 :                                        &se->ss->info->expr->where);
    8747              : 
    8748              :           /* Pass the temporary as the first argument.  */
    8749           66 :           result = info->descriptor;
    8750           66 :           tmp = gfc_build_addr_expr (NULL_TREE, result);
    8751           66 :           vec_safe_push (retargs, tmp);
    8752              :         }
    8753        11473 :       else if (!comp && sym->result->attr.dimension)
    8754              :         {
    8755         8456 :           gcc_assert (se->loop && info);
    8756              : 
    8757              :           /* Set the type of the array.  */
    8758         8456 :           tmp = gfc_typenode_for_spec (&ts);
    8759         8456 :           tmp = arg1_cntnr ? TREE_TYPE (arg1_cntnr) : tmp;
    8760         8456 :           gcc_assert (se->ss->dimen == se->loop->dimen);
    8761              : 
    8762              :           /* Evaluate the bounds of the result, if known.  */
    8763         8456 :           gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
    8764              : 
    8765              :           /* If the lhs of an assignment x = f(..) is allocatable and
    8766              :              f2003 is allowed, we must not generate the function call
    8767              :              here but should just send back the results of the mapping.
    8768              :              This is signalled by the function ss being flagged.  */
    8769         8456 :           if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
    8770              :             {
    8771            0 :               gfc_free_interface_mapping (&mapping);
    8772            0 :               return has_alternate_specifier;
    8773              :             }
    8774              : 
    8775              :           /* Create a temporary to store the result.  In case the function
    8776              :              returns a pointer, the temporary will be a shallow copy and
    8777              :              mustn't be deallocated.  */
    8778         8456 :           callee_alloc = sym->attr.allocatable || sym->attr.pointer;
    8779         8456 :           gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
    8780              :                                        tmp, NULL_TREE, false,
    8781              :                                        !sym->attr.pointer, callee_alloc,
    8782         8456 :                                        &se->ss->info->expr->where);
    8783              : 
    8784              :           /* Pass the temporary as the first argument.  */
    8785         8456 :           result = info->descriptor;
    8786         8456 :           tmp = gfc_build_addr_expr (NULL_TREE, result);
    8787         8456 :           vec_safe_push (retargs, tmp);
    8788              :         }
    8789         3246 :       else if (ts.type == BT_CHARACTER)
    8790              :         {
    8791              :           /* Pass the string length.  */
    8792         3185 :           type = gfc_get_character_type (ts.kind, ts.u.cl);
    8793         3185 :           type = build_pointer_type (type);
    8794              : 
    8795              :           /* Emit a DECL_EXPR for the VLA type.  */
    8796         3185 :           tmp = TREE_TYPE (type);
    8797         3185 :           if (TYPE_SIZE (tmp)
    8798         3185 :               && TREE_CODE (TYPE_SIZE (tmp)) != INTEGER_CST)
    8799              :             {
    8800         1922 :               tmp = build_decl (input_location, TYPE_DECL, NULL_TREE, tmp);
    8801         1922 :               DECL_ARTIFICIAL (tmp) = 1;
    8802         1922 :               DECL_IGNORED_P (tmp) = 1;
    8803         1922 :               tmp = fold_build1_loc (input_location, DECL_EXPR,
    8804         1922 :                                      TREE_TYPE (tmp), tmp);
    8805         1922 :               gfc_add_expr_to_block (&se->pre, tmp);
    8806              :             }
    8807              : 
    8808              :           /* Return an address to a char[0:len-1]* temporary for
    8809              :              character pointers.  */
    8810         3185 :           if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
    8811          229 :                || (comp && (comp->attr.pointer || comp->attr.allocatable)))
    8812              :             {
    8813          635 :               var = gfc_create_var (type, "pstr");
    8814              : 
    8815          635 :               if ((!comp && sym->attr.allocatable)
    8816           21 :                   || (comp && comp->attr.allocatable))
    8817              :                 {
    8818          348 :                   gfc_add_modify (&se->pre, var,
    8819          348 :                                   fold_convert (TREE_TYPE (var),
    8820              :                                                 null_pointer_node));
    8821          348 :                   tmp = gfc_call_free (var);
    8822          348 :                   gfc_add_expr_to_block (&se->post, tmp);
    8823              :                 }
    8824              : 
    8825              :               /* Provide an address expression for the function arguments.  */
    8826          635 :               var = gfc_build_addr_expr (NULL_TREE, var);
    8827              :             }
    8828              :           else
    8829         2550 :             var = gfc_conv_string_tmp (se, type, len);
    8830              : 
    8831         3185 :           vec_safe_push (retargs, var);
    8832              :         }
    8833              :       else
    8834              :         {
    8835           61 :           gcc_assert (flag_f2c && ts.type == BT_COMPLEX);
    8836              : 
    8837           61 :           type = gfc_get_complex_type (ts.kind);
    8838           61 :           var = gfc_build_addr_expr (NULL_TREE, gfc_create_var (type, "cmplx"));
    8839           61 :           vec_safe_push (retargs, var);
    8840              :         }
    8841              : 
    8842              :       /* Add the string length to the argument list.  */
    8843        18754 :       if (ts.type == BT_CHARACTER && ts.deferred)
    8844              :         {
    8845          592 :           tmp = len;
    8846          592 :           if (!VAR_P (tmp))
    8847            0 :             tmp = gfc_evaluate_now (len, &se->pre);
    8848          592 :           TREE_STATIC (tmp) = 1;
    8849          592 :           gfc_add_modify (&se->pre, tmp,
    8850          592 :                           build_int_cst (TREE_TYPE (tmp), 0));
    8851          592 :           tmp = gfc_build_addr_expr (NULL_TREE, tmp);
    8852          592 :           vec_safe_push (retargs, tmp);
    8853              :         }
    8854        18162 :       else if (ts.type == BT_CHARACTER)
    8855         4416 :         vec_safe_push (retargs, len);
    8856              :     }
    8857              : 
    8858       129943 :   gfc_free_interface_mapping (&mapping);
    8859              : 
    8860              :   /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS.  */
    8861       242067 :   arglen = (vec_safe_length (arglist) + vec_safe_length (optionalargs)
    8862       155232 :             + vec_safe_length (stringargs) + vec_safe_length (append_args));
    8863       129943 :   vec_safe_reserve (retargs, arglen);
    8864              : 
    8865              :   /* Add the return arguments.  */
    8866       129943 :   vec_safe_splice (retargs, arglist);
    8867              : 
    8868              :   /* Add the hidden present status for optional+value to the arguments.  */
    8869       129943 :   vec_safe_splice (retargs, optionalargs);
    8870              : 
    8871              :   /* Add the hidden string length parameters to the arguments.  */
    8872       129943 :   vec_safe_splice (retargs, stringargs);
    8873              : 
    8874              :   /* We may want to append extra arguments here.  This is used e.g. for
    8875              :      calls to libgfortran_matmul_??, which need extra information.  */
    8876       129943 :   vec_safe_splice (retargs, append_args);
    8877              : 
    8878       129943 :   arglist = retargs;
    8879              : 
    8880              :   /* Generate the actual call.  */
    8881       129943 :   is_builtin = false;
    8882       129943 :   if (base_object == NULL_TREE)
    8883       129863 :     conv_function_val (se, &is_builtin, sym, expr, args);
    8884              :   else
    8885           80 :     conv_base_obj_fcn_val (se, base_object, expr);
    8886              : 
    8887              :   /* If there are alternate return labels, function type should be
    8888              :      integer.  Can't modify the type in place though, since it can be shared
    8889              :      with other functions.  For dummy arguments, the typing is done to
    8890              :      this result, even if it has to be repeated for each call.  */
    8891       129943 :   if (has_alternate_specifier
    8892       129943 :       && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
    8893              :     {
    8894            7 :       if (!sym->attr.dummy)
    8895              :         {
    8896            0 :           TREE_TYPE (sym->backend_decl)
    8897            0 :                 = build_function_type (integer_type_node,
    8898            0 :                       TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
    8899            0 :           se->expr = gfc_build_addr_expr (NULL_TREE, sym->backend_decl);
    8900              :         }
    8901              :       else
    8902            7 :         TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
    8903              :     }
    8904              : 
    8905       129943 :   fntype = TREE_TYPE (TREE_TYPE (se->expr));
    8906       129943 :   se->expr = build_call_vec (TREE_TYPE (fntype), se->expr, arglist);
    8907              : 
    8908       129943 :   if (is_builtin)
    8909          522 :     se->expr = update_builtin_function (se->expr, sym);
    8910              : 
    8911              :   /* Allocatable scalar function results must be freed and nullified
    8912              :      after use. This necessitates the creation of a temporary to
    8913              :      hold the result to prevent duplicate calls.  */
    8914       129943 :   symbol_attribute attr =  comp ? comp->attr : sym->attr;
    8915       129943 :   bool allocatable = attr.allocatable && !attr.dimension;
    8916       133160 :   gfc_symbol *der = comp ?
    8917         1980 :                     comp->ts.type == BT_DERIVED ? comp->ts.u.derived : NULL
    8918              :                          :
    8919       127963 :                     sym->ts.type == BT_DERIVED ? sym->ts.u.derived : NULL;
    8920         3217 :   bool finalizable = der != NULL && der->ns->proc_name
    8921         6431 :                             && gfc_is_finalizable (der, NULL);
    8922              : 
    8923       129943 :   if (!byref && finalizable)
    8924          182 :     gfc_finalize_tree_expr (se, der, attr, expr->rank);
    8925              : 
    8926       129943 :   if (!byref && sym->ts.type != BT_CHARACTER
    8927       110979 :       && allocatable && !finalizable)
    8928              :     {
    8929          230 :       tmp = gfc_create_var (TREE_TYPE (se->expr), NULL);
    8930          230 :       gfc_add_modify (&se->pre, tmp, se->expr);
    8931          230 :       se->expr = tmp;
    8932          230 :       tmp = gfc_call_free (tmp);
    8933          230 :       gfc_add_expr_to_block (&post, tmp);
    8934          230 :       gfc_add_modify (&post, se->expr, build_int_cst (TREE_TYPE (se->expr), 0));
    8935              :     }
    8936              : 
    8937              :   /* If we have a pointer function, but we don't want a pointer, e.g.
    8938              :      something like
    8939              :         x = f()
    8940              :      where f is pointer valued, we have to dereference the result.  */
    8941       129943 :   if (!se->want_pointer && !byref
    8942       110587 :       && ((!comp && (sym->attr.pointer || sym->attr.allocatable))
    8943         1638 :           || (comp && (comp->attr.pointer || comp->attr.allocatable))))
    8944          456 :     se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
    8945              : 
    8946              :   /* f2c calling conventions require a scalar default real function to
    8947              :      return a double precision result.  Convert this back to default
    8948              :      real.  We only care about the cases that can happen in Fortran 77.
    8949              :   */
    8950       129943 :   if (flag_f2c && sym->ts.type == BT_REAL
    8951           98 :       && sym->ts.kind == gfc_default_real_kind
    8952           74 :       && !sym->attr.pointer
    8953           55 :       && !sym->attr.allocatable
    8954           43 :       && !sym->attr.always_explicit)
    8955           43 :     se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
    8956              : 
    8957              :   /* A pure function may still have side-effects - it may modify its
    8958              :      parameters.  */
    8959       129943 :   TREE_SIDE_EFFECTS (se->expr) = 1;
    8960              : #if 0
    8961              :   if (!sym->attr.pure)
    8962              :     TREE_SIDE_EFFECTS (se->expr) = 1;
    8963              : #endif
    8964              : 
    8965       129943 :   if (byref)
    8966              :     {
    8967              :       /* Add the function call to the pre chain.  There is no expression.  */
    8968        18754 :       gfc_add_expr_to_block (&se->pre, se->expr);
    8969        18754 :       se->expr = NULL_TREE;
    8970              : 
    8971        18754 :       if (!se->direct_byref)
    8972              :         {
    8973        11768 :           if ((sym->attr.dimension && !comp) || (comp && comp->attr.dimension))
    8974              :             {
    8975         8522 :               if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    8976              :                 {
    8977              :                   /* Check the data pointer hasn't been modified.  This would
    8978              :                      happen in a function returning a pointer.  */
    8979          251 :                   tmp = gfc_conv_descriptor_data_get (info->descriptor);
    8980          251 :                   tmp = fold_build2_loc (input_location, NE_EXPR,
    8981              :                                          logical_type_node,
    8982              :                                          tmp, info->data);
    8983          251 :                   gfc_trans_runtime_check (true, false, tmp, &se->pre, NULL,
    8984              :                                            gfc_msg_fault);
    8985              :                 }
    8986         8522 :               se->expr = info->descriptor;
    8987              :               /* Bundle in the string length.  */
    8988         8522 :               se->string_length = len;
    8989              : 
    8990         8522 :               if (finalizable)
    8991            6 :                 gfc_finalize_tree_expr (se, der, attr, expr->rank);
    8992              :             }
    8993         3246 :           else if (ts.type == BT_CHARACTER)
    8994              :             {
    8995              :               /* Dereference for character pointer results.  */
    8996         3185 :               if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
    8997          229 :                   || (comp && (comp->attr.pointer || comp->attr.allocatable)))
    8998          635 :                 se->expr = build_fold_indirect_ref_loc (input_location, var);
    8999              :               else
    9000         2550 :                 se->expr = var;
    9001              : 
    9002         3185 :               se->string_length = len;
    9003              :             }
    9004              :           else
    9005              :             {
    9006           61 :               gcc_assert (ts.type == BT_COMPLEX && flag_f2c);
    9007           61 :               se->expr = build_fold_indirect_ref_loc (input_location, var);
    9008              :             }
    9009              :         }
    9010              :     }
    9011              : 
    9012              :   /* Associate the rhs class object's meta-data with the result, when the
    9013              :      result is a temporary.  */
    9014       112129 :   if (args && args->expr && args->expr->ts.type == BT_CLASS
    9015         4961 :       && sym->ts.type == BT_CLASS && result != NULL_TREE && DECL_P (result)
    9016       129975 :       && !GFC_CLASS_TYPE_P (TREE_TYPE (result)))
    9017              :     {
    9018           32 :       gfc_se parmse;
    9019           32 :       gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (args->expr);
    9020              : 
    9021           32 :       gfc_init_se (&parmse, NULL);
    9022           32 :       parmse.data_not_needed = 1;
    9023           32 :       gfc_conv_expr (&parmse, class_expr);
    9024           32 :       if (!DECL_LANG_SPECIFIC (result))
    9025           32 :         gfc_allocate_lang_decl (result);
    9026           32 :       GFC_DECL_SAVED_DESCRIPTOR (result) = parmse.expr;
    9027           32 :       gfc_free_expr (class_expr);
    9028              :       /* -fcheck= can add diagnostic code, which has to be placed before
    9029              :          the call. */
    9030           32 :       if (parmse.pre.head != NULL)
    9031           12 :           gfc_add_expr_to_block (&se->pre, parmse.pre.head);
    9032           32 :       gcc_assert (parmse.post.head == NULL_TREE);
    9033              :     }
    9034              : 
    9035              :   /* Follow the function call with the argument post block.  */
    9036       129943 :   if (byref)
    9037              :     {
    9038        18754 :       gfc_add_block_to_block (&se->pre, &post);
    9039              : 
    9040              :       /* Transformational functions of derived types with allocatable
    9041              :          components must have the result allocatable components copied when the
    9042              :          argument is actually given.  This is unnecessry for REDUCE because the
    9043              :          wrapper for the OPERATION function takes care of this.  */
    9044        18754 :       arg = expr->value.function.actual;
    9045        18754 :       if (result && arg && expr->rank
    9046        14661 :           && isym && isym->transformational
    9047        13092 :           && isym->id != GFC_ISYM_REDUCE
    9048        12966 :           && arg->expr
    9049        12906 :           && arg->expr->ts.type == BT_DERIVED
    9050          229 :           && arg->expr->ts.u.derived->attr.alloc_comp)
    9051              :         {
    9052           36 :           tree tmp2;
    9053              :           /* Copy the allocatable components.  We have to use a
    9054              :              temporary here to prevent source allocatable components
    9055              :              from being corrupted.  */
    9056           36 :           tmp2 = gfc_evaluate_now (result, &se->pre);
    9057           36 :           tmp = gfc_copy_alloc_comp (arg->expr->ts.u.derived,
    9058              :                                      result, tmp2, expr->rank, 0);
    9059           36 :           gfc_add_expr_to_block (&se->pre, tmp);
    9060           36 :           tmp = gfc_copy_allocatable_data (result, tmp2, TREE_TYPE(tmp2),
    9061              :                                            expr->rank);
    9062           36 :           gfc_add_expr_to_block (&se->pre, tmp);
    9063              : 
    9064              :           /* Finally free the temporary's data field.  */
    9065           36 :           tmp = gfc_conv_descriptor_data_get (tmp2);
    9066           36 :           tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
    9067              :                                             NULL_TREE, NULL_TREE, true,
    9068              :                                             NULL, GFC_CAF_COARRAY_NOCOARRAY);
    9069           36 :           gfc_add_expr_to_block (&se->pre, tmp);
    9070              :         }
    9071              :     }
    9072              :   else
    9073              :     {
    9074              :       /* For a function with a class array result, save the result as
    9075              :          a temporary, set the info fields needed by the scalarizer and
    9076              :          call the finalization function of the temporary. Note that the
    9077              :          nullification of allocatable components needed by the result
    9078              :          is done in gfc_trans_assignment_1.  */
    9079        34522 :       if (expr && (gfc_is_class_array_function (expr)
    9080        34200 :                    || gfc_is_alloc_class_scalar_function (expr))
    9081          841 :           && se->expr && GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
    9082       112018 :           && expr->must_finalize)
    9083              :         {
    9084              :           /* TODO Eliminate the doubling of temporaries.  This
    9085              :              one is necessary to ensure no memory leakage.  */
    9086          321 :           se->expr = gfc_evaluate_now (se->expr, &se->pre);
    9087              : 
    9088              :           /* Finalize the result, if necessary.  */
    9089          642 :           attr = expr->value.function.esym
    9090          321 :                  ? CLASS_DATA (expr->value.function.esym->result)->attr
    9091           14 :                  : CLASS_DATA (expr)->attr;
    9092          321 :           if (!((gfc_is_class_array_function (expr)
    9093          108 :                  || gfc_is_alloc_class_scalar_function (expr))
    9094          321 :                 && attr.pointer))
    9095          276 :             gfc_finalize_tree_expr (se, NULL, attr, expr->rank);
    9096              :         }
    9097       111189 :       gfc_add_block_to_block (&se->post, &post);
    9098              :     }
    9099              : 
    9100              :   return has_alternate_specifier;
    9101              : }
    9102              : 
    9103              : 
    9104              : /* Fill a character string with spaces.  */
    9105              : 
    9106              : static tree
    9107        30450 : fill_with_spaces (tree start, tree type, tree size)
    9108              : {
    9109        30450 :   stmtblock_t block, loop;
    9110        30450 :   tree i, el, exit_label, cond, tmp;
    9111              : 
    9112              :   /* For a simple char type, we can call memset().  */
    9113        30450 :   if (compare_tree_int (TYPE_SIZE_UNIT (type), 1) == 0)
    9114        50312 :     return build_call_expr_loc (input_location,
    9115              :                             builtin_decl_explicit (BUILT_IN_MEMSET),
    9116              :                             3, start,
    9117              :                             build_int_cst (gfc_get_int_type (gfc_c_int_kind),
    9118        25156 :                                            lang_hooks.to_target_charset (' ')),
    9119              :                                 fold_convert (size_type_node, size));
    9120              : 
    9121              :   /* Otherwise, we use a loop:
    9122              :         for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
    9123              :           *el = (type) ' ';
    9124              :    */
    9125              : 
    9126              :   /* Initialize variables.  */
    9127         5294 :   gfc_init_block (&block);
    9128         5294 :   i = gfc_create_var (sizetype, "i");
    9129         5294 :   gfc_add_modify (&block, i, fold_convert (sizetype, size));
    9130         5294 :   el = gfc_create_var (build_pointer_type (type), "el");
    9131         5294 :   gfc_add_modify (&block, el, fold_convert (TREE_TYPE (el), start));
    9132         5294 :   exit_label = gfc_build_label_decl (NULL_TREE);
    9133         5294 :   TREE_USED (exit_label) = 1;
    9134              : 
    9135              : 
    9136              :   /* Loop body.  */
    9137         5294 :   gfc_init_block (&loop);
    9138              : 
    9139              :   /* Exit condition.  */
    9140         5294 :   cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, i,
    9141              :                           build_zero_cst (sizetype));
    9142         5294 :   tmp = build1_v (GOTO_EXPR, exit_label);
    9143         5294 :   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
    9144              :                          build_empty_stmt (input_location));
    9145         5294 :   gfc_add_expr_to_block (&loop, tmp);
    9146              : 
    9147              :   /* Assignment.  */
    9148         5294 :   gfc_add_modify (&loop,
    9149              :                   fold_build1_loc (input_location, INDIRECT_REF, type, el),
    9150         5294 :                   build_int_cst (type, lang_hooks.to_target_charset (' ')));
    9151              : 
    9152              :   /* Increment loop variables.  */
    9153         5294 :   gfc_add_modify (&loop, i,
    9154              :                   fold_build2_loc (input_location, MINUS_EXPR, sizetype, i,
    9155         5294 :                                    TYPE_SIZE_UNIT (type)));
    9156         5294 :   gfc_add_modify (&loop, el,
    9157              :                   fold_build_pointer_plus_loc (input_location,
    9158         5294 :                                                el, TYPE_SIZE_UNIT (type)));
    9159              : 
    9160              :   /* Making the loop... actually loop!  */
    9161         5294 :   tmp = gfc_finish_block (&loop);
    9162         5294 :   tmp = build1_v (LOOP_EXPR, tmp);
    9163         5294 :   gfc_add_expr_to_block (&block, tmp);
    9164              : 
    9165              :   /* The exit label.  */
    9166         5294 :   tmp = build1_v (LABEL_EXPR, exit_label);
    9167         5294 :   gfc_add_expr_to_block (&block, tmp);
    9168              : 
    9169              : 
    9170         5294 :   return gfc_finish_block (&block);
    9171              : }
    9172              : 
    9173              : 
    9174              : /* Generate code to copy a string.  */
    9175              : 
    9176              : void
    9177        35552 : gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
    9178              :                        int dkind, tree slength, tree src, int skind)
    9179              : {
    9180        35552 :   tree tmp, dlen, slen;
    9181        35552 :   tree dsc;
    9182        35552 :   tree ssc;
    9183        35552 :   tree cond;
    9184        35552 :   tree cond2;
    9185        35552 :   tree tmp2;
    9186        35552 :   tree tmp3;
    9187        35552 :   tree tmp4;
    9188        35552 :   tree chartype;
    9189        35552 :   stmtblock_t tempblock;
    9190              : 
    9191        35552 :   gcc_assert (dkind == skind);
    9192              : 
    9193        35552 :   if (slength != NULL_TREE)
    9194              :     {
    9195        35552 :       slen = gfc_evaluate_now (fold_convert (gfc_charlen_type_node, slength), block);
    9196        35552 :       ssc = gfc_string_to_single_character (slen, src, skind);
    9197              :     }
    9198              :   else
    9199              :     {
    9200            0 :       slen = build_one_cst (gfc_charlen_type_node);
    9201            0 :       ssc =  src;
    9202              :     }
    9203              : 
    9204        35552 :   if (dlength != NULL_TREE)
    9205              :     {
    9206        35552 :       dlen = gfc_evaluate_now (fold_convert (gfc_charlen_type_node, dlength), block);
    9207        35552 :       dsc = gfc_string_to_single_character (dlen, dest, dkind);
    9208              :     }
    9209              :   else
    9210              :     {
    9211            0 :       dlen = build_one_cst (gfc_charlen_type_node);
    9212            0 :       dsc =  dest;
    9213              :     }
    9214              : 
    9215              :   /* Assign directly if the types are compatible.  */
    9216        35552 :   if (dsc != NULL_TREE && ssc != NULL_TREE
    9217        35552 :       && TREE_TYPE (dsc) == TREE_TYPE (ssc))
    9218              :     {
    9219         5102 :       gfc_add_modify (block, dsc, ssc);
    9220         5102 :       return;
    9221              :     }
    9222              : 
    9223              :   /* The string copy algorithm below generates code like
    9224              : 
    9225              :      if (destlen > 0)
    9226              :        {
    9227              :          if (srclen < destlen)
    9228              :            {
    9229              :              memmove (dest, src, srclen);
    9230              :              // Pad with spaces.
    9231              :              memset (&dest[srclen], ' ', destlen - srclen);
    9232              :            }
    9233              :          else
    9234              :            {
    9235              :              // Truncate if too long.
    9236              :              memmove (dest, src, destlen);
    9237              :            }
    9238              :        }
    9239              :   */
    9240              : 
    9241              :   /* Do nothing if the destination length is zero.  */
    9242        30450 :   cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node, dlen,
    9243        30450 :                           build_zero_cst (TREE_TYPE (dlen)));
    9244              : 
    9245              :   /* For non-default character kinds, we have to multiply the string
    9246              :      length by the base type size.  */
    9247        30450 :   chartype = gfc_get_char_type (dkind);
    9248        30450 :   slen = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (slen),
    9249              :                           slen,
    9250        30450 :                           fold_convert (TREE_TYPE (slen),
    9251              :                                         TYPE_SIZE_UNIT (chartype)));
    9252        30450 :   dlen = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (dlen),
    9253              :                           dlen,
    9254        30450 :                           fold_convert (TREE_TYPE (dlen),
    9255              :                                         TYPE_SIZE_UNIT (chartype)));
    9256              : 
    9257        30450 :   if (dlength && POINTER_TYPE_P (TREE_TYPE (dest)))
    9258        30402 :     dest = fold_convert (pvoid_type_node, dest);
    9259              :   else
    9260           48 :     dest = gfc_build_addr_expr (pvoid_type_node, dest);
    9261              : 
    9262        30450 :   if (slength && POINTER_TYPE_P (TREE_TYPE (src)))
    9263        30446 :     src = fold_convert (pvoid_type_node, src);
    9264              :   else
    9265            4 :     src = gfc_build_addr_expr (pvoid_type_node, src);
    9266              : 
    9267              :   /* Truncate string if source is too long.  */
    9268        30450 :   cond2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node, slen,
    9269              :                            dlen);
    9270              : 
    9271              :   /* Pre-evaluate pointers unless one of the IF arms will be optimized away.  */
    9272        30450 :   if (!CONSTANT_CLASS_P (cond2))
    9273              :     {
    9274         9359 :       dest = gfc_evaluate_now (dest, block);
    9275         9359 :       src = gfc_evaluate_now (src, block);
    9276              :     }
    9277              : 
    9278              :   /* Copy and pad with spaces.  */
    9279        30450 :   tmp3 = build_call_expr_loc (input_location,
    9280              :                               builtin_decl_explicit (BUILT_IN_MEMMOVE),
    9281              :                               3, dest, src,
    9282              :                               fold_convert (size_type_node, slen));
    9283              : 
    9284              :   /* Wstringop-overflow appears at -O3 even though this warning is not
    9285              :      explicitly available in fortran nor can it be switched off. If the
    9286              :      source length is a constant, its negative appears as a very large
    9287              :      positive number and triggers the warning in BUILTIN_MEMSET. Fixing
    9288              :      the result of the MINUS_EXPR suppresses this spurious warning.  */
    9289        30450 :   tmp = fold_build2_loc (input_location, MINUS_EXPR,
    9290        30450 :                          TREE_TYPE(dlen), dlen, slen);
    9291        30450 :   if (slength && TREE_CONSTANT (slength))
    9292        26932 :     tmp = gfc_evaluate_now (tmp, block);
    9293              : 
    9294        30450 :   tmp4 = fold_build_pointer_plus_loc (input_location, dest, slen);
    9295        30450 :   tmp4 = fill_with_spaces (tmp4, chartype, tmp);
    9296              : 
    9297        30450 :   gfc_init_block (&tempblock);
    9298        30450 :   gfc_add_expr_to_block (&tempblock, tmp3);
    9299        30450 :   gfc_add_expr_to_block (&tempblock, tmp4);
    9300        30450 :   tmp3 = gfc_finish_block (&tempblock);
    9301              : 
    9302              :   /* The truncated memmove if the slen >= dlen.  */
    9303        30450 :   tmp2 = build_call_expr_loc (input_location,
    9304              :                               builtin_decl_explicit (BUILT_IN_MEMMOVE),
    9305              :                               3, dest, src,
    9306              :                               fold_convert (size_type_node, dlen));
    9307              : 
    9308              :   /* The whole copy_string function is there.  */
    9309        30450 :   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
    9310              :                          tmp3, tmp2);
    9311        30450 :   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
    9312              :                          build_empty_stmt (input_location));
    9313        30450 :   gfc_add_expr_to_block (block, tmp);
    9314              : }
    9315              : 
    9316              : 
    9317              : /* Translate a statement function.
    9318              :    The value of a statement function reference is obtained by evaluating the
    9319              :    expression using the values of the actual arguments for the values of the
    9320              :    corresponding dummy arguments.  */
    9321              : 
    9322              : static void
    9323          269 : gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
    9324              : {
    9325          269 :   gfc_symbol *sym;
    9326          269 :   gfc_symbol *fsym;
    9327          269 :   gfc_formal_arglist *fargs;
    9328          269 :   gfc_actual_arglist *args;
    9329          269 :   gfc_se lse;
    9330          269 :   gfc_se rse;
    9331          269 :   gfc_saved_var *saved_vars;
    9332          269 :   tree *temp_vars;
    9333          269 :   tree type;
    9334          269 :   tree tmp;
    9335          269 :   int n;
    9336              : 
    9337          269 :   sym = expr->symtree->n.sym;
    9338          269 :   args = expr->value.function.actual;
    9339          269 :   gfc_init_se (&lse, NULL);
    9340          269 :   gfc_init_se (&rse, NULL);
    9341              : 
    9342          269 :   n = 0;
    9343          727 :   for (fargs = gfc_sym_get_dummy_args (sym); fargs; fargs = fargs->next)
    9344          458 :     n++;
    9345          269 :   saved_vars = XCNEWVEC (gfc_saved_var, n);
    9346          269 :   temp_vars = XCNEWVEC (tree, n);
    9347              : 
    9348          727 :   for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
    9349          458 :        fargs = fargs->next, n++)
    9350              :     {
    9351              :       /* Each dummy shall be specified, explicitly or implicitly, to be
    9352              :          scalar.  */
    9353          458 :       gcc_assert (fargs->sym->attr.dimension == 0);
    9354          458 :       fsym = fargs->sym;
    9355              : 
    9356          458 :       if (fsym->ts.type == BT_CHARACTER)
    9357              :         {
    9358              :           /* Copy string arguments.  */
    9359           48 :           tree arglen;
    9360              : 
    9361           48 :           gcc_assert (fsym->ts.u.cl && fsym->ts.u.cl->length
    9362              :                       && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT);
    9363              : 
    9364              :           /* Create a temporary to hold the value.  */
    9365           48 :           if (fsym->ts.u.cl->backend_decl == NULL_TREE)
    9366            1 :              fsym->ts.u.cl->backend_decl
    9367            1 :                 = gfc_conv_constant_to_tree (fsym->ts.u.cl->length);
    9368              : 
    9369           48 :           type = gfc_get_character_type (fsym->ts.kind, fsym->ts.u.cl);
    9370           48 :           temp_vars[n] = gfc_create_var (type, fsym->name);
    9371              : 
    9372           48 :           arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
    9373              : 
    9374           48 :           gfc_conv_expr (&rse, args->expr);
    9375           48 :           gfc_conv_string_parameter (&rse);
    9376           48 :           gfc_add_block_to_block (&se->pre, &lse.pre);
    9377           48 :           gfc_add_block_to_block (&se->pre, &rse.pre);
    9378              : 
    9379           48 :           gfc_trans_string_copy (&se->pre, arglen, temp_vars[n], fsym->ts.kind,
    9380              :                                  rse.string_length, rse.expr, fsym->ts.kind);
    9381           48 :           gfc_add_block_to_block (&se->pre, &lse.post);
    9382           48 :           gfc_add_block_to_block (&se->pre, &rse.post);
    9383              :         }
    9384              :       else
    9385              :         {
    9386              :           /* For everything else, just evaluate the expression.  */
    9387              : 
    9388              :           /* Create a temporary to hold the value.  */
    9389          410 :           type = gfc_typenode_for_spec (&fsym->ts);
    9390          410 :           temp_vars[n] = gfc_create_var (type, fsym->name);
    9391              : 
    9392          410 :           gfc_conv_expr (&lse, args->expr);
    9393              : 
    9394          410 :           gfc_add_block_to_block (&se->pre, &lse.pre);
    9395          410 :           gfc_add_modify (&se->pre, temp_vars[n], lse.expr);
    9396          410 :           gfc_add_block_to_block (&se->pre, &lse.post);
    9397              :         }
    9398              : 
    9399          458 :       args = args->next;
    9400              :     }
    9401              : 
    9402              :   /* Use the temporary variables in place of the real ones.  */
    9403          727 :   for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
    9404          458 :        fargs = fargs->next, n++)
    9405          458 :     gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
    9406              : 
    9407          269 :   gfc_conv_expr (se, sym->value);
    9408              : 
    9409          269 :   if (sym->ts.type == BT_CHARACTER)
    9410              :     {
    9411           55 :       gfc_conv_const_charlen (sym->ts.u.cl);
    9412              : 
    9413              :       /* Force the expression to the correct length.  */
    9414           55 :       if (!INTEGER_CST_P (se->string_length)
    9415          101 :           || tree_int_cst_lt (se->string_length,
    9416           46 :                               sym->ts.u.cl->backend_decl))
    9417              :         {
    9418           31 :           type = gfc_get_character_type (sym->ts.kind, sym->ts.u.cl);
    9419           31 :           tmp = gfc_create_var (type, sym->name);
    9420           31 :           tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
    9421           31 :           gfc_trans_string_copy (&se->pre, sym->ts.u.cl->backend_decl, tmp,
    9422              :                                  sym->ts.kind, se->string_length, se->expr,
    9423              :                                  sym->ts.kind);
    9424           31 :           se->expr = tmp;
    9425              :         }
    9426           55 :       se->string_length = sym->ts.u.cl->backend_decl;
    9427              :     }
    9428              : 
    9429              :   /* Restore the original variables.  */
    9430          727 :   for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
    9431          458 :        fargs = fargs->next, n++)
    9432          458 :     gfc_restore_sym (fargs->sym, &saved_vars[n]);
    9433          269 :   free (temp_vars);
    9434          269 :   free (saved_vars);
    9435          269 : }
    9436              : 
    9437              : 
    9438              : /* Translate a function expression.  */
    9439              : 
    9440              : static void
    9441       309462 : gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
    9442              : {
    9443       309462 :   gfc_symbol *sym;
    9444              : 
    9445       309462 :   if (expr->value.function.isym)
    9446              :     {
    9447       259150 :       gfc_conv_intrinsic_function (se, expr);
    9448       259150 :       return;
    9449              :     }
    9450              : 
    9451              :   /* expr.value.function.esym is the resolved (specific) function symbol for
    9452              :      most functions.  However this isn't set for dummy procedures.  */
    9453        50312 :   sym = expr->value.function.esym;
    9454        50312 :   if (!sym)
    9455         1616 :     sym = expr->symtree->n.sym;
    9456              : 
    9457              :   /* The IEEE_ARITHMETIC functions are caught here. */
    9458        50312 :   if (sym->from_intmod == INTMOD_IEEE_ARITHMETIC)
    9459        13939 :     if (gfc_conv_ieee_arithmetic_function (se, expr))
    9460              :       return;
    9461              : 
    9462              :   /* We distinguish statement functions from general functions to improve
    9463              :      runtime performance.  */
    9464        37855 :   if (sym->attr.proc == PROC_ST_FUNCTION)
    9465              :     {
    9466          269 :       gfc_conv_statement_function (se, expr);
    9467          269 :       return;
    9468              :     }
    9469              : 
    9470        37586 :   gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
    9471              :                            NULL);
    9472              : }
    9473              : 
    9474              : 
    9475              : /* Determine whether the given EXPR_CONSTANT is a zero initializer.  */
    9476              : 
    9477              : static bool
    9478        39287 : is_zero_initializer_p (gfc_expr * expr)
    9479              : {
    9480        39287 :   if (expr->expr_type != EXPR_CONSTANT)
    9481              :     return false;
    9482              : 
    9483              :   /* We ignore constants with prescribed memory representations for now.  */
    9484        11373 :   if (expr->representation.string)
    9485              :     return false;
    9486              : 
    9487        11355 :   switch (expr->ts.type)
    9488              :     {
    9489         5237 :     case BT_INTEGER:
    9490         5237 :       return mpz_cmp_si (expr->value.integer, 0) == 0;
    9491              : 
    9492         4817 :     case BT_REAL:
    9493         4817 :       return mpfr_zero_p (expr->value.real)
    9494         4817 :              && MPFR_SIGN (expr->value.real) >= 0;
    9495              : 
    9496          925 :     case BT_LOGICAL:
    9497          925 :       return expr->value.logical == 0;
    9498              : 
    9499          242 :     case BT_COMPLEX:
    9500          242 :       return mpfr_zero_p (mpc_realref (expr->value.complex))
    9501          154 :              && MPFR_SIGN (mpc_realref (expr->value.complex)) >= 0
    9502          154 :              && mpfr_zero_p (mpc_imagref (expr->value.complex))
    9503          384 :              && MPFR_SIGN (mpc_imagref (expr->value.complex)) >= 0;
    9504              : 
    9505              :     default:
    9506              :       break;
    9507              :     }
    9508              :   return false;
    9509              : }
    9510              : 
    9511              : 
    9512              : static void
    9513        35325 : gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
    9514              : {
    9515        35325 :   gfc_ss *ss;
    9516              : 
    9517        35325 :   ss = se->ss;
    9518        35325 :   gcc_assert (ss != NULL && ss != gfc_ss_terminator);
    9519        35325 :   gcc_assert (ss->info->expr == expr && ss->info->type == GFC_SS_CONSTRUCTOR);
    9520              : 
    9521        35325 :   gfc_conv_tmp_array_ref (se);
    9522        35325 : }
    9523              : 
    9524              : 
    9525              : /* Build a static initializer.  EXPR is the expression for the initial value.
    9526              :    The other parameters describe the variable of the component being
    9527              :    initialized. EXPR may be null.  */
    9528              : 
    9529              : tree
    9530       140648 : gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
    9531              :                       bool array, bool pointer, bool procptr)
    9532              : {
    9533       140648 :   gfc_se se;
    9534              : 
    9535       140648 :   if (flag_coarray != GFC_FCOARRAY_LIB && ts->type == BT_DERIVED
    9536        45012 :       && ts->u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    9537          165 :       && ts->u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
    9538           57 :     return build_constructor (type, NULL);
    9539              : 
    9540       140591 :   if (!(expr || pointer || procptr))
    9541              :     return NULL_TREE;
    9542              : 
    9543              :   /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
    9544              :      (these are the only two iso_c_binding derived types that can be
    9545              :      used as initialization expressions).  If so, we need to modify
    9546              :      the 'expr' to be that for a (void *).  */
    9547       132227 :   if (expr != NULL && expr->ts.type == BT_DERIVED
    9548        40781 :       && expr->ts.is_iso_c && expr->ts.u.derived)
    9549              :     {
    9550          186 :       if (TREE_CODE (type) == ARRAY_TYPE)
    9551            4 :         return build_constructor (type, NULL);
    9552          182 :       else if (POINTER_TYPE_P (type))
    9553          182 :         return build_int_cst (type, 0);
    9554              :       else
    9555            0 :         gcc_unreachable ();
    9556              :     }
    9557              : 
    9558       132041 :   if (array && !procptr)
    9559              :     {
    9560         8648 :       tree ctor;
    9561              :       /* Arrays need special handling.  */
    9562         8648 :       if (pointer)
    9563          773 :         ctor = gfc_build_null_descriptor (type);
    9564              :       /* Special case assigning an array to zero.  */
    9565         7875 :       else if (is_zero_initializer_p (expr))
    9566          220 :         ctor = build_constructor (type, NULL);
    9567              :       else
    9568         7655 :         ctor = gfc_conv_array_initializer (type, expr);
    9569         8648 :       TREE_STATIC (ctor) = 1;
    9570         8648 :       return ctor;
    9571              :     }
    9572       123393 :   else if (pointer || procptr)
    9573              :     {
    9574        59955 :       if (ts->type == BT_CLASS && !procptr)
    9575              :         {
    9576         1744 :           gfc_init_se (&se, NULL);
    9577         1744 :           gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
    9578         1744 :           gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
    9579         1744 :           TREE_STATIC (se.expr) = 1;
    9580         1744 :           return se.expr;
    9581              :         }
    9582        58211 :       else if (!expr || expr->expr_type == EXPR_NULL)
    9583        31453 :         return fold_convert (type, null_pointer_node);
    9584              :       else
    9585              :         {
    9586        26758 :           gfc_init_se (&se, NULL);
    9587        26758 :           se.want_pointer = 1;
    9588        26758 :           gfc_conv_expr (&se, expr);
    9589        26758 :           gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
    9590              :           return se.expr;
    9591              :         }
    9592              :     }
    9593              :   else
    9594              :     {
    9595        63438 :       switch (ts->type)
    9596              :         {
    9597        19070 :         case_bt_struct:
    9598        19070 :         case BT_CLASS:
    9599        19070 :           gfc_init_se (&se, NULL);
    9600        19070 :           if (ts->type == BT_CLASS && expr->expr_type == EXPR_NULL)
    9601          757 :             gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
    9602              :           else
    9603        18313 :             gfc_conv_structure (&se, expr, 1);
    9604        19070 :           gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
    9605        19070 :           TREE_STATIC (se.expr) = 1;
    9606        19070 :           return se.expr;
    9607              : 
    9608         2679 :         case BT_CHARACTER:
    9609         2679 :           if (expr->expr_type == EXPR_CONSTANT)
    9610              :             {
    9611         2678 :               tree ctor = gfc_conv_string_init (ts->u.cl->backend_decl, expr);
    9612         2678 :               TREE_STATIC (ctor) = 1;
    9613         2678 :               return ctor;
    9614              :             }
    9615              : 
    9616              :           /* Fallthrough.  */
    9617        41690 :         default:
    9618        41690 :           gfc_init_se (&se, NULL);
    9619        41690 :           gfc_conv_constant (&se, expr);
    9620        41690 :           gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
    9621              :           return se.expr;
    9622              :         }
    9623              :     }
    9624              : }
    9625              : 
    9626              : static tree
    9627          950 : gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
    9628              : {
    9629          950 :   gfc_se rse;
    9630          950 :   gfc_se lse;
    9631          950 :   gfc_ss *rss;
    9632          950 :   gfc_ss *lss;
    9633          950 :   gfc_array_info *lss_array;
    9634          950 :   stmtblock_t body;
    9635          950 :   stmtblock_t block;
    9636          950 :   gfc_loopinfo loop;
    9637          950 :   int n;
    9638          950 :   tree tmp;
    9639              : 
    9640          950 :   gfc_start_block (&block);
    9641              : 
    9642              :   /* Initialize the scalarizer.  */
    9643          950 :   gfc_init_loopinfo (&loop);
    9644              : 
    9645          950 :   gfc_init_se (&lse, NULL);
    9646          950 :   gfc_init_se (&rse, NULL);
    9647              : 
    9648              :   /* Walk the rhs.  */
    9649          950 :   rss = gfc_walk_expr (expr);
    9650          950 :   if (rss == gfc_ss_terminator)
    9651              :     /* The rhs is scalar.  Add a ss for the expression.  */
    9652          208 :     rss = gfc_get_scalar_ss (gfc_ss_terminator, expr);
    9653              : 
    9654              :   /* Create a SS for the destination.  */
    9655          950 :   lss = gfc_get_array_ss (gfc_ss_terminator, NULL, cm->as->rank,
    9656              :                           GFC_SS_COMPONENT);
    9657          950 :   lss_array = &lss->info->data.array;
    9658          950 :   lss_array->shape = gfc_get_shape (cm->as->rank);
    9659          950 :   lss_array->descriptor = dest;
    9660          950 :   lss_array->data = gfc_conv_array_data (dest);
    9661          950 :   lss_array->offset = gfc_conv_array_offset (dest);
    9662         1957 :   for (n = 0; n < cm->as->rank; n++)
    9663              :     {
    9664         1007 :       lss_array->start[n] = gfc_conv_array_lbound (dest, n);
    9665         1007 :       lss_array->stride[n] = gfc_index_one_node;
    9666              : 
    9667         1007 :       mpz_init (lss_array->shape[n]);
    9668         1007 :       mpz_sub (lss_array->shape[n], cm->as->upper[n]->value.integer,
    9669         1007 :                cm->as->lower[n]->value.integer);
    9670         1007 :       mpz_add_ui (lss_array->shape[n], lss_array->shape[n], 1);
    9671              :     }
    9672              : 
    9673              :   /* Associate the SS with the loop.  */
    9674          950 :   gfc_add_ss_to_loop (&loop, lss);
    9675          950 :   gfc_add_ss_to_loop (&loop, rss);
    9676              : 
    9677              :   /* Calculate the bounds of the scalarization.  */
    9678          950 :   gfc_conv_ss_startstride (&loop);
    9679              : 
    9680              :   /* Setup the scalarizing loops.  */
    9681          950 :   gfc_conv_loop_setup (&loop, &expr->where);
    9682              : 
    9683              :   /* Setup the gfc_se structures.  */
    9684          950 :   gfc_copy_loopinfo_to_se (&lse, &loop);
    9685          950 :   gfc_copy_loopinfo_to_se (&rse, &loop);
    9686              : 
    9687          950 :   rse.ss = rss;
    9688          950 :   gfc_mark_ss_chain_used (rss, 1);
    9689          950 :   lse.ss = lss;
    9690          950 :   gfc_mark_ss_chain_used (lss, 1);
    9691              : 
    9692              :   /* Start the scalarized loop body.  */
    9693          950 :   gfc_start_scalarized_body (&loop, &body);
    9694              : 
    9695          950 :   gfc_conv_tmp_array_ref (&lse);
    9696          950 :   if (cm->ts.type == BT_CHARACTER)
    9697          176 :     lse.string_length = cm->ts.u.cl->backend_decl;
    9698              : 
    9699          950 :   gfc_conv_expr (&rse, expr);
    9700              : 
    9701          950 :   tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false);
    9702          950 :   gfc_add_expr_to_block (&body, tmp);
    9703              : 
    9704          950 :   gcc_assert (rse.ss == gfc_ss_terminator);
    9705              : 
    9706              :   /* Generate the copying loops.  */
    9707          950 :   gfc_trans_scalarizing_loops (&loop, &body);
    9708              : 
    9709              :   /* Wrap the whole thing up.  */
    9710          950 :   gfc_add_block_to_block (&block, &loop.pre);
    9711          950 :   gfc_add_block_to_block (&block, &loop.post);
    9712              : 
    9713          950 :   gcc_assert (lss_array->shape != NULL);
    9714          950 :   gfc_free_shape (&lss_array->shape, cm->as->rank);
    9715          950 :   gfc_cleanup_loop (&loop);
    9716              : 
    9717          950 :   return gfc_finish_block (&block);
    9718              : }
    9719              : 
    9720              : 
    9721              : static stmtblock_t *final_block;
    9722              : static tree
    9723         1280 : gfc_trans_alloc_subarray_assign (tree dest, gfc_component * cm,
    9724              :                                  gfc_expr * expr)
    9725              : {
    9726         1280 :   gfc_se se;
    9727         1280 :   stmtblock_t block;
    9728         1280 :   tree offset;
    9729         1280 :   int n;
    9730         1280 :   tree tmp;
    9731         1280 :   tree tmp2;
    9732         1280 :   gfc_array_spec *as;
    9733         1280 :   gfc_expr *arg = NULL;
    9734              : 
    9735         1280 :   gfc_start_block (&block);
    9736         1280 :   gfc_init_se (&se, NULL);
    9737              : 
    9738              :   /* Get the descriptor for the expressions.  */
    9739         1280 :   se.want_pointer = 0;
    9740         1280 :   gfc_conv_expr_descriptor (&se, expr);
    9741         1280 :   gfc_add_block_to_block (&block, &se.pre);
    9742         1280 :   gfc_add_modify (&block, dest, se.expr);
    9743         1280 :   if (cm->ts.type == BT_CHARACTER
    9744         1280 :       && gfc_deferred_strlen (cm, &tmp))
    9745              :     {
    9746           30 :       tmp = fold_build3_loc (input_location, COMPONENT_REF,
    9747           30 :                              TREE_TYPE (tmp),
    9748           30 :                              TREE_OPERAND (dest, 0),
    9749              :                              tmp, NULL_TREE);
    9750           30 :       gfc_add_modify (&block, tmp,
    9751           30 :                               fold_convert (TREE_TYPE (tmp),
    9752              :                               se.string_length));
    9753           30 :       cm->ts.u.cl->backend_decl = gfc_create_var (gfc_charlen_type_node,
    9754              :                                                   "slen");
    9755           30 :       gfc_add_modify (&block, cm->ts.u.cl->backend_decl, se.string_length);
    9756              :     }
    9757              : 
    9758              :   /* Deal with arrays of derived types with allocatable components.  */
    9759         1280 :   if (gfc_bt_struct (cm->ts.type)
    9760          187 :         && cm->ts.u.derived->attr.alloc_comp)
    9761              :     // TODO: Fix caf_mode
    9762          107 :     tmp = gfc_copy_alloc_comp (cm->ts.u.derived,
    9763              :                                se.expr, dest,
    9764          107 :                                cm->as->rank, 0);
    9765         1173 :   else if (cm->ts.type == BT_CLASS && expr->ts.type == BT_DERIVED
    9766           36 :            && CLASS_DATA(cm)->attr.allocatable)
    9767              :     {
    9768           36 :       if (cm->ts.u.derived->attr.alloc_comp)
    9769              :         // TODO: Fix caf_mode
    9770            0 :         tmp = gfc_copy_alloc_comp (expr->ts.u.derived,
    9771              :                                    se.expr, dest,
    9772              :                                    expr->rank, 0);
    9773              :       else
    9774              :         {
    9775           36 :           tmp = TREE_TYPE (dest);
    9776           36 :           tmp = gfc_duplicate_allocatable (dest, se.expr,
    9777              :                                            tmp, expr->rank, NULL_TREE);
    9778              :         }
    9779              :     }
    9780         1137 :   else if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
    9781           30 :     tmp = gfc_duplicate_allocatable (dest, se.expr,
    9782              :                                      gfc_typenode_for_spec (&cm->ts),
    9783           30 :                                      cm->as->rank, NULL_TREE);
    9784              :   else
    9785         1107 :     tmp = gfc_duplicate_allocatable (dest, se.expr,
    9786         1107 :                                      TREE_TYPE(cm->backend_decl),
    9787         1107 :                                      cm->as->rank, NULL_TREE);
    9788              : 
    9789              : 
    9790         1280 :   gfc_add_expr_to_block (&block, tmp);
    9791         1280 :   gfc_add_block_to_block (&block, &se.post);
    9792              : 
    9793         1280 :   if (final_block && !cm->attr.allocatable
    9794           96 :       && expr->expr_type == EXPR_ARRAY)
    9795              :     {
    9796           96 :       tree data_ptr;
    9797           96 :       data_ptr = gfc_conv_descriptor_data_get (dest);
    9798           96 :       gfc_add_expr_to_block (final_block, gfc_call_free (data_ptr));
    9799           96 :     }
    9800         1184 :   else if (final_block && cm->attr.allocatable)
    9801          162 :     gfc_add_block_to_block (final_block, &se.finalblock);
    9802              : 
    9803         1280 :   if (expr->expr_type != EXPR_VARIABLE)
    9804         1159 :     gfc_conv_descriptor_data_set (&block, se.expr,
    9805              :                                   null_pointer_node);
    9806              : 
    9807              :   /* We need to know if the argument of a conversion function is a
    9808              :      variable, so that the correct lower bound can be used.  */
    9809         1280 :   if (expr->expr_type == EXPR_FUNCTION
    9810           56 :         && expr->value.function.isym
    9811           44 :         && expr->value.function.isym->conversion
    9812           44 :         && expr->value.function.actual->expr
    9813           44 :         && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE)
    9814           44 :     arg = expr->value.function.actual->expr;
    9815              : 
    9816              :   /* Obtain the array spec of full array references.  */
    9817           44 :   if (arg)
    9818           44 :     as = gfc_get_full_arrayspec_from_expr (arg);
    9819              :   else
    9820         1236 :     as = gfc_get_full_arrayspec_from_expr (expr);
    9821              : 
    9822              :   /* Shift the lbound and ubound of temporaries to being unity,
    9823              :      rather than zero, based. Always calculate the offset.  */
    9824         1280 :   gfc_conv_descriptor_offset_set (&block, dest, gfc_index_zero_node);
    9825         1280 :   offset = gfc_conv_descriptor_offset_get (dest);
    9826         1280 :   tmp2 =gfc_create_var (gfc_array_index_type, NULL);
    9827              : 
    9828         2616 :   for (n = 0; n < expr->rank; n++)
    9829              :     {
    9830         1336 :       tree span;
    9831         1336 :       tree lbound;
    9832              : 
    9833              :       /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
    9834              :          TODO It looks as if gfc_conv_expr_descriptor should return
    9835              :          the correct bounds and that the following should not be
    9836              :          necessary.  This would simplify gfc_conv_intrinsic_bound
    9837              :          as well.  */
    9838         1336 :       if (as && as->lower[n])
    9839              :         {
    9840           80 :           gfc_se lbse;
    9841           80 :           gfc_init_se (&lbse, NULL);
    9842           80 :           gfc_conv_expr (&lbse, as->lower[n]);
    9843           80 :           gfc_add_block_to_block (&block, &lbse.pre);
    9844           80 :           lbound = gfc_evaluate_now (lbse.expr, &block);
    9845           80 :         }
    9846         1256 :       else if (as && arg)
    9847              :         {
    9848           34 :           tmp = gfc_get_symbol_decl (arg->symtree->n.sym);
    9849           34 :           lbound = gfc_conv_descriptor_lbound_get (tmp,
    9850              :                                         gfc_rank_cst[n]);
    9851              :         }
    9852         1222 :       else if (as)
    9853           64 :         lbound = gfc_conv_descriptor_lbound_get (dest,
    9854              :                                                 gfc_rank_cst[n]);
    9855              :       else
    9856         1158 :         lbound = gfc_index_one_node;
    9857              : 
    9858         1336 :       lbound = fold_convert (gfc_array_index_type, lbound);
    9859              : 
    9860              :       /* Shift the bounds and set the offset accordingly.  */
    9861         1336 :       tmp = gfc_conv_descriptor_ubound_get (dest, gfc_rank_cst[n]);
    9862         1336 :       span = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    9863              :                 tmp, gfc_conv_descriptor_lbound_get (dest, gfc_rank_cst[n]));
    9864         1336 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    9865              :                              span, lbound);
    9866         1336 :       gfc_conv_descriptor_ubound_set (&block, dest,
    9867              :                                       gfc_rank_cst[n], tmp);
    9868         1336 :       gfc_conv_descriptor_lbound_set (&block, dest,
    9869              :                                       gfc_rank_cst[n], lbound);
    9870              : 
    9871         1336 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    9872              :                          gfc_conv_descriptor_lbound_get (dest,
    9873              :                                                          gfc_rank_cst[n]),
    9874              :                          gfc_conv_descriptor_stride_get (dest,
    9875              :                                                          gfc_rank_cst[n]));
    9876         1336 :       gfc_add_modify (&block, tmp2, tmp);
    9877         1336 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    9878              :                              offset, tmp2);
    9879         1336 :       gfc_conv_descriptor_offset_set (&block, dest, tmp);
    9880              :     }
    9881              : 
    9882         1280 :   if (arg)
    9883              :     {
    9884              :       /* If a conversion expression has a null data pointer
    9885              :          argument, nullify the allocatable component.  */
    9886           44 :       tree non_null_expr;
    9887           44 :       tree null_expr;
    9888              : 
    9889           44 :       if (arg->symtree->n.sym->attr.allocatable
    9890           12 :             || arg->symtree->n.sym->attr.pointer)
    9891              :         {
    9892           32 :           non_null_expr = gfc_finish_block (&block);
    9893           32 :           gfc_start_block (&block);
    9894           32 :           gfc_conv_descriptor_data_set (&block, dest,
    9895              :                                         null_pointer_node);
    9896           32 :           null_expr = gfc_finish_block (&block);
    9897           32 :           tmp = gfc_conv_descriptor_data_get (arg->symtree->n.sym->backend_decl);
    9898           32 :           tmp = build2_loc (input_location, EQ_EXPR, logical_type_node, tmp,
    9899           32 :                             fold_convert (TREE_TYPE (tmp), null_pointer_node));
    9900           32 :           return build3_v (COND_EXPR, tmp,
    9901              :                            null_expr, non_null_expr);
    9902              :         }
    9903              :     }
    9904              : 
    9905         1248 :   return gfc_finish_block (&block);
    9906              : }
    9907              : 
    9908              : 
    9909              : /* Allocate or reallocate scalar component, as necessary.  */
    9910              : 
    9911              : static void
    9912          410 : alloc_scalar_allocatable_subcomponent (stmtblock_t *block, tree comp,
    9913              :                                        gfc_component *cm, gfc_expr *expr2,
    9914              :                                        tree slen)
    9915              : {
    9916          410 :   tree tmp;
    9917          410 :   tree ptr;
    9918          410 :   tree size;
    9919          410 :   tree size_in_bytes;
    9920          410 :   tree lhs_cl_size = NULL_TREE;
    9921          410 :   gfc_se se;
    9922              : 
    9923          410 :   if (!comp)
    9924            0 :     return;
    9925              : 
    9926          410 :   if (!expr2 || expr2->rank)
    9927              :     return;
    9928              : 
    9929          410 :   realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
    9930              : 
    9931          410 :   if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
    9932              :     {
    9933          139 :       gcc_assert (expr2->ts.type == BT_CHARACTER);
    9934          139 :       size = expr2->ts.u.cl->backend_decl;
    9935          139 :       if (!size || !VAR_P (size))
    9936          139 :         size = gfc_create_var (TREE_TYPE (slen), "slen");
    9937          139 :       gfc_add_modify (block, size, slen);
    9938              : 
    9939          139 :       gfc_deferred_strlen (cm, &tmp);
    9940          139 :       lhs_cl_size = fold_build3_loc (input_location, COMPONENT_REF,
    9941              :                                      gfc_charlen_type_node,
    9942          139 :                                      TREE_OPERAND (comp, 0),
    9943              :                                      tmp, NULL_TREE);
    9944              : 
    9945          139 :       tmp = TREE_TYPE (gfc_typenode_for_spec (&cm->ts));
    9946          139 :       tmp = TYPE_SIZE_UNIT (tmp);
    9947          278 :       size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
    9948          139 :                                        TREE_TYPE (tmp), tmp,
    9949          139 :                                        fold_convert (TREE_TYPE (tmp), size));
    9950              :     }
    9951          271 :   else if (cm->ts.type == BT_CLASS)
    9952              :     {
    9953          103 :       if (expr2->ts.type != BT_CLASS)
    9954              :         {
    9955          103 :           if (expr2->ts.type == BT_CHARACTER)
    9956              :             {
    9957           24 :               gfc_init_se (&se, NULL);
    9958           24 :               gfc_conv_expr (&se, expr2);
    9959           24 :               size = build_int_cst (gfc_charlen_type_node, expr2->ts.kind);
    9960           24 :               size = fold_build2_loc (input_location, MULT_EXPR,
    9961              :                                       gfc_charlen_type_node,
    9962              :                                       se.string_length, size);
    9963           24 :               size = fold_convert (size_type_node, size);
    9964              :             }
    9965              :           else
    9966              :             {
    9967           79 :               if (expr2->ts.type == BT_DERIVED)
    9968           48 :                 tmp = gfc_get_symbol_decl (expr2->ts.u.derived);
    9969              :               else
    9970           31 :                 tmp = gfc_typenode_for_spec (&expr2->ts);
    9971           79 :               size = TYPE_SIZE_UNIT (tmp);
    9972              :             }
    9973              :         }
    9974              :       else
    9975              :         {
    9976            0 :           gfc_expr *e2vtab;
    9977            0 :           e2vtab = gfc_find_and_cut_at_last_class_ref (expr2);
    9978            0 :           gfc_add_vptr_component (e2vtab);
    9979            0 :           gfc_add_size_component (e2vtab);
    9980            0 :           gfc_init_se (&se, NULL);
    9981            0 :           gfc_conv_expr (&se, e2vtab);
    9982            0 :           gfc_add_block_to_block (block, &se.pre);
    9983            0 :           size = fold_convert (size_type_node, se.expr);
    9984            0 :           gfc_free_expr (e2vtab);
    9985              :         }
    9986              :       size_in_bytes = size;
    9987              :     }
    9988              :   else
    9989              :     {
    9990              :       /* Otherwise use the length in bytes of the rhs.  */
    9991          168 :       size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&cm->ts));
    9992          168 :       size_in_bytes = size;
    9993              :     }
    9994              : 
    9995          410 :   size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
    9996              :                                    size_in_bytes, size_one_node);
    9997              : 
    9998          410 :   if (cm->ts.type == BT_DERIVED && cm->ts.u.derived->attr.alloc_comp)
    9999              :     {
   10000            0 :       tmp = build_call_expr_loc (input_location,
   10001              :                                  builtin_decl_explicit (BUILT_IN_CALLOC),
   10002              :                                  2, build_one_cst (size_type_node),
   10003              :                                  size_in_bytes);
   10004            0 :       tmp = fold_convert (TREE_TYPE (comp), tmp);
   10005            0 :       gfc_add_modify (block, comp, tmp);
   10006              :     }
   10007              :   else
   10008              :     {
   10009          410 :       tmp = build_call_expr_loc (input_location,
   10010              :                                  builtin_decl_explicit (BUILT_IN_MALLOC),
   10011              :                                  1, size_in_bytes);
   10012          410 :       if (GFC_CLASS_TYPE_P (TREE_TYPE (comp)))
   10013          103 :         ptr = gfc_class_data_get (comp);
   10014              :       else
   10015              :         ptr = comp;
   10016          410 :       tmp = fold_convert (TREE_TYPE (ptr), tmp);
   10017          410 :       gfc_add_modify (block, ptr, tmp);
   10018              :     }
   10019              : 
   10020          410 :   if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
   10021              :     /* Update the lhs character length.  */
   10022          139 :     gfc_add_modify (block, lhs_cl_size,
   10023          139 :                     fold_convert (TREE_TYPE (lhs_cl_size), size));
   10024              : }
   10025              : 
   10026              : 
   10027              : /* Assign a single component of a derived type constructor.  */
   10028              : 
   10029              : static tree
   10030        29103 : gfc_trans_subcomponent_assign (tree dest, gfc_component * cm,
   10031              :                                gfc_expr * expr, bool init)
   10032              : {
   10033        29103 :   gfc_se se;
   10034        29103 :   gfc_se lse;
   10035        29103 :   stmtblock_t block;
   10036        29103 :   tree tmp;
   10037        29103 :   tree vtab;
   10038              : 
   10039        29103 :   gfc_start_block (&block);
   10040              : 
   10041        29103 :   if (cm->attr.pointer || cm->attr.proc_pointer)
   10042              :     {
   10043              :       /* Only care about pointers here, not about allocatables.  */
   10044         2634 :       gfc_init_se (&se, NULL);
   10045              :       /* Pointer component.  */
   10046         2634 :       if ((cm->attr.dimension || cm->attr.codimension)
   10047          670 :           && !cm->attr.proc_pointer)
   10048              :         {
   10049              :           /* Array pointer.  */
   10050          654 :           if (expr->expr_type == EXPR_NULL)
   10051          648 :             gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
   10052              :           else
   10053              :             {
   10054            6 :               se.direct_byref = 1;
   10055            6 :               se.expr = dest;
   10056            6 :               gfc_conv_expr_descriptor (&se, expr);
   10057            6 :               gfc_add_block_to_block (&block, &se.pre);
   10058            6 :               gfc_add_block_to_block (&block, &se.post);
   10059              :             }
   10060              :         }
   10061              :       else
   10062              :         {
   10063              :           /* Scalar pointers.  */
   10064         1980 :           se.want_pointer = 1;
   10065         1980 :           gfc_conv_expr (&se, expr);
   10066         1980 :           gfc_add_block_to_block (&block, &se.pre);
   10067              : 
   10068         1980 :           if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
   10069           12 :               && expr->symtree->n.sym->attr.dummy)
   10070           12 :             se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
   10071              : 
   10072         1980 :           gfc_add_modify (&block, dest,
   10073         1980 :                                fold_convert (TREE_TYPE (dest), se.expr));
   10074         1980 :           gfc_add_block_to_block (&block, &se.post);
   10075              :         }
   10076              :     }
   10077        26469 :   else if (cm->ts.type == BT_CLASS && expr->expr_type == EXPR_NULL)
   10078              :     {
   10079              :       /* NULL initialization for CLASS components.  */
   10080          922 :       tmp = gfc_trans_structure_assign (dest,
   10081              :                                         gfc_class_initializer (&cm->ts, expr),
   10082              :                                         false);
   10083          922 :       gfc_add_expr_to_block (&block, tmp);
   10084              :     }
   10085        25547 :   else if ((cm->attr.dimension || cm->attr.codimension)
   10086              :            && !cm->attr.proc_pointer)
   10087              :     {
   10088         4847 :       if (cm->attr.allocatable && expr->expr_type == EXPR_NULL)
   10089              :         {
   10090         2653 :           gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
   10091         2653 :           if (cm->attr.codimension && flag_coarray == GFC_FCOARRAY_LIB)
   10092            2 :             gfc_add_modify (&block, gfc_conv_descriptor_token (dest),
   10093              :                             null_pointer_node);
   10094              :         }
   10095         2194 :       else if (cm->attr.allocatable || cm->attr.pdt_array)
   10096              :         {
   10097         1244 :           tmp = gfc_trans_alloc_subarray_assign (dest, cm, expr);
   10098         1244 :           gfc_add_expr_to_block (&block, tmp);
   10099              :         }
   10100              :       else
   10101              :         {
   10102          950 :           tmp = gfc_trans_subarray_assign (dest, cm, expr);
   10103          950 :           gfc_add_expr_to_block (&block, tmp);
   10104              :         }
   10105              :     }
   10106        20700 :   else if (cm->ts.type == BT_CLASS
   10107          145 :            && CLASS_DATA (cm)->attr.dimension
   10108           36 :            && CLASS_DATA (cm)->attr.allocatable
   10109           36 :            && expr->ts.type == BT_DERIVED)
   10110              :     {
   10111           36 :       vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
   10112           36 :       vtab = gfc_build_addr_expr (NULL_TREE, vtab);
   10113           36 :       tmp = gfc_class_vptr_get (dest);
   10114           36 :       gfc_add_modify (&block, tmp,
   10115           36 :                       fold_convert (TREE_TYPE (tmp), vtab));
   10116           36 :       tmp = gfc_class_data_get (dest);
   10117           36 :       tmp = gfc_trans_alloc_subarray_assign (tmp, cm, expr);
   10118           36 :       gfc_add_expr_to_block (&block, tmp);
   10119              :     }
   10120        20664 :   else if (cm->attr.allocatable && expr->expr_type == EXPR_NULL
   10121         1766 :            && (init
   10122         1639 :                || (cm->ts.type == BT_CHARACTER
   10123          131 :                    && !(cm->ts.deferred || cm->attr.pdt_string))))
   10124              :     {
   10125              :       /* NULL initialization for allocatable components.
   10126              :          Deferred-length character is dealt with later.  */
   10127          151 :       gfc_add_modify (&block, dest, fold_convert (TREE_TYPE (dest),
   10128              :                                                   null_pointer_node));
   10129              :     }
   10130        20513 :   else if (init && (cm->attr.allocatable
   10131        13425 :            || (cm->ts.type == BT_CLASS && CLASS_DATA (cm)->attr.allocatable
   10132          109 :                && expr->ts.type != BT_CLASS)))
   10133              :     {
   10134          410 :       tree size;
   10135              : 
   10136          410 :       gfc_init_se (&se, NULL);
   10137          410 :       gfc_conv_expr (&se, expr);
   10138              : 
   10139              :       /* The remainder of these instructions follow the if (cm->attr.pointer)
   10140              :          if (!cm->attr.dimension) part above.  */
   10141          410 :       gfc_add_block_to_block (&block, &se.pre);
   10142              :       /* Take care about non-array allocatable components here.  The alloc_*
   10143              :          routine below is motivated by the alloc_scalar_allocatable_for_
   10144              :          assignment() routine, but with the realloc portions removed and
   10145              :          different input.  */
   10146          410 :       alloc_scalar_allocatable_subcomponent (&block, dest, cm, expr,
   10147              :                                              se.string_length);
   10148              : 
   10149          410 :       if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
   10150            0 :           && expr->symtree->n.sym->attr.dummy)
   10151            0 :         se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
   10152              : 
   10153          410 :       if (cm->ts.type == BT_CLASS)
   10154              :         {
   10155          103 :           tmp = gfc_class_data_get (dest);
   10156          103 :           tmp = build_fold_indirect_ref_loc (input_location, tmp);
   10157          103 :           vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
   10158          103 :           vtab = gfc_build_addr_expr (NULL_TREE, vtab);
   10159          103 :           gfc_add_modify (&block, gfc_class_vptr_get (dest),
   10160          103 :                  fold_convert (TREE_TYPE (gfc_class_vptr_get (dest)), vtab));
   10161              :         }
   10162              :       else
   10163          307 :         tmp = build_fold_indirect_ref_loc (input_location, dest);
   10164              : 
   10165              :       /* For deferred strings insert a memcpy.  */
   10166          410 :       if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
   10167              :         {
   10168          139 :           gcc_assert (se.string_length || expr->ts.u.cl->backend_decl);
   10169          139 :           size = size_of_string_in_bytes (cm->ts.kind, se.string_length
   10170              :                                                 ? se.string_length
   10171            0 :                                                 : expr->ts.u.cl->backend_decl);
   10172          139 :           tmp = gfc_build_memcpy_call (tmp, se.expr, size);
   10173          139 :           gfc_add_expr_to_block (&block, tmp);
   10174              :         }
   10175          271 :       else if (cm->ts.type == BT_CLASS)
   10176              :         {
   10177              :           /* Fix the expression for memcpy.  */
   10178          103 :           if (expr->expr_type != EXPR_VARIABLE)
   10179           73 :             se.expr = gfc_evaluate_now (se.expr, &block);
   10180              : 
   10181          103 :           if (expr->ts.type == BT_CHARACTER)
   10182              :             {
   10183           24 :               size = build_int_cst (gfc_charlen_type_node, expr->ts.kind);
   10184           24 :               size = fold_build2_loc (input_location, MULT_EXPR,
   10185              :                                       gfc_charlen_type_node,
   10186              :                                       se.string_length, size);
   10187           24 :               size = fold_convert (size_type_node, size);
   10188              :             }
   10189              :           else
   10190           79 :             size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr->ts));
   10191              : 
   10192              :           /* Now copy the expression to the constructor component _data.  */
   10193          103 :           gfc_add_expr_to_block (&block,
   10194              :                                  gfc_build_memcpy_call (tmp, se.expr, size));
   10195              : 
   10196              :           /* Fill the unlimited polymorphic _len field.  */
   10197          103 :           if (UNLIMITED_POLY (cm) && expr->ts.type == BT_CHARACTER)
   10198              :             {
   10199           24 :               tmp = gfc_class_len_get (gfc_get_class_from_expr (tmp));
   10200           24 :               gfc_add_modify (&block, tmp,
   10201           24 :                               fold_convert (TREE_TYPE (tmp),
   10202              :                               se.string_length));
   10203              :             }
   10204              :         }
   10205              :       else
   10206          168 :         gfc_add_modify (&block, tmp,
   10207          168 :                         fold_convert (TREE_TYPE (tmp), se.expr));
   10208          410 :       gfc_add_block_to_block (&block, &se.post);
   10209          410 :     }
   10210        20103 :   else if (expr->ts.type == BT_UNION)
   10211              :     {
   10212           13 :       tree tmp;
   10213           13 :       gfc_constructor *c = gfc_constructor_first (expr->value.constructor);
   10214              :       /* We mark that the entire union should be initialized with a contrived
   10215              :          EXPR_NULL expression at the beginning.  */
   10216           13 :       if (c != NULL && c->n.component == NULL
   10217            7 :           && c->expr != NULL && c->expr->expr_type == EXPR_NULL)
   10218              :         {
   10219            6 :           tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
   10220            6 :                             dest, build_constructor (TREE_TYPE (dest), NULL));
   10221            6 :           gfc_add_expr_to_block (&block, tmp);
   10222            6 :           c = gfc_constructor_next (c);
   10223              :         }
   10224              :       /* The following constructor expression, if any, represents a specific
   10225              :          map intializer, as given by the user.  */
   10226           13 :       if (c != NULL && c->expr != NULL)
   10227              :         {
   10228            6 :           gcc_assert (expr->expr_type == EXPR_STRUCTURE);
   10229            6 :           tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
   10230            6 :           gfc_add_expr_to_block (&block, tmp);
   10231              :         }
   10232              :     }
   10233        20090 :   else if (expr->ts.type == BT_DERIVED && expr->ts.f90_type != BT_VOID)
   10234              :     {
   10235         3123 :       if (expr->expr_type != EXPR_STRUCTURE)
   10236              :         {
   10237          452 :           tree dealloc = NULL_TREE;
   10238          452 :           gfc_init_se (&se, NULL);
   10239          452 :           gfc_conv_expr (&se, expr);
   10240          452 :           gfc_add_block_to_block (&block, &se.pre);
   10241              :           /* Prevent repeat evaluations in gfc_copy_alloc_comp by fixing the
   10242              :              expression in  a temporary variable and deallocate the allocatable
   10243              :              components. Then we can the copy the expression to the result.  */
   10244          452 :           if (cm->ts.u.derived->attr.alloc_comp
   10245          330 :               && expr->expr_type != EXPR_VARIABLE)
   10246              :             {
   10247          300 :               se.expr = gfc_evaluate_now (se.expr, &block);
   10248          300 :               dealloc = gfc_deallocate_alloc_comp (cm->ts.u.derived, se.expr,
   10249              :                                                    expr->rank);
   10250              :             }
   10251          452 :           gfc_add_modify (&block, dest,
   10252          452 :                           fold_convert (TREE_TYPE (dest), se.expr));
   10253          452 :           if (cm->ts.u.derived->attr.alloc_comp
   10254          330 :               && expr->expr_type != EXPR_NULL)
   10255              :             {
   10256              :               // TODO: Fix caf_mode
   10257           48 :               tmp = gfc_copy_alloc_comp (cm->ts.u.derived, se.expr,
   10258              :                                          dest, expr->rank, 0);
   10259           48 :               gfc_add_expr_to_block (&block, tmp);
   10260           48 :               if (dealloc != NULL_TREE)
   10261           18 :                 gfc_add_expr_to_block (&block, dealloc);
   10262              :             }
   10263          452 :           gfc_add_block_to_block (&block, &se.post);
   10264              :         }
   10265              :       else
   10266              :         {
   10267              :           /* Nested constructors.  */
   10268         2671 :           tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
   10269         2671 :           gfc_add_expr_to_block (&block, tmp);
   10270              :         }
   10271              :     }
   10272        16967 :   else if (gfc_deferred_strlen (cm, &tmp))
   10273              :     {
   10274          125 :       tree strlen;
   10275          125 :       strlen = tmp;
   10276          125 :       gcc_assert (strlen);
   10277          125 :       strlen = fold_build3_loc (input_location, COMPONENT_REF,
   10278          125 :                                 TREE_TYPE (strlen),
   10279          125 :                                 TREE_OPERAND (dest, 0),
   10280              :                                 strlen, NULL_TREE);
   10281              : 
   10282          125 :       if (expr->expr_type == EXPR_NULL)
   10283              :         {
   10284          107 :           tmp = build_int_cst (TREE_TYPE (cm->backend_decl), 0);
   10285          107 :           gfc_add_modify (&block, dest, tmp);
   10286          107 :           tmp = build_int_cst (TREE_TYPE (strlen), 0);
   10287          107 :           gfc_add_modify (&block, strlen, tmp);
   10288              :         }
   10289              :       else
   10290              :         {
   10291           18 :           tree size;
   10292           18 :           gfc_init_se (&se, NULL);
   10293           18 :           gfc_conv_expr (&se, expr);
   10294           18 :           size = size_of_string_in_bytes (cm->ts.kind, se.string_length);
   10295           18 :           size = fold_convert (size_type_node, size);
   10296           18 :           tmp = build_call_expr_loc (input_location,
   10297              :                                      builtin_decl_explicit (BUILT_IN_MALLOC),
   10298              :                                      1, size);
   10299           18 :           gfc_add_modify (&block, dest,
   10300           18 :                           fold_convert (TREE_TYPE (dest), tmp));
   10301           18 :           gfc_add_modify (&block, strlen,
   10302           18 :                           fold_convert (TREE_TYPE (strlen), se.string_length));
   10303           18 :           tmp = gfc_build_memcpy_call (dest, se.expr, size);
   10304           18 :           gfc_add_expr_to_block (&block, tmp);
   10305              :         }
   10306              :     }
   10307        16842 :   else if (!cm->attr.artificial)
   10308              :     {
   10309              :       /* Scalar component (excluding deferred parameters).  */
   10310        16727 :       gfc_init_se (&se, NULL);
   10311        16727 :       gfc_init_se (&lse, NULL);
   10312              : 
   10313        16727 :       gfc_conv_expr (&se, expr);
   10314        16727 :       if (cm->ts.type == BT_CHARACTER)
   10315         1051 :         lse.string_length = cm->ts.u.cl->backend_decl;
   10316        16727 :       lse.expr = dest;
   10317        16727 :       tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, false, false);
   10318        16727 :       gfc_add_expr_to_block (&block, tmp);
   10319              :     }
   10320        29103 :   return gfc_finish_block (&block);
   10321              : }
   10322              : 
   10323              : /* Assign a derived type constructor to a variable.  */
   10324              : 
   10325              : tree
   10326        20294 : gfc_trans_structure_assign (tree dest, gfc_expr * expr, bool init, bool coarray)
   10327              : {
   10328        20294 :   gfc_constructor *c;
   10329        20294 :   gfc_component *cm;
   10330        20294 :   stmtblock_t block;
   10331        20294 :   tree field;
   10332        20294 :   tree tmp;
   10333        20294 :   gfc_se se;
   10334              : 
   10335        20294 :   gfc_start_block (&block);
   10336              : 
   10337        20294 :   if (expr->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
   10338          180 :       && (expr->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
   10339           13 :           || expr->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
   10340              :     {
   10341          180 :       gfc_se lse;
   10342              : 
   10343          180 :       gfc_init_se (&se, NULL);
   10344          180 :       gfc_init_se (&lse, NULL);
   10345          180 :       gfc_conv_expr (&se, gfc_constructor_first (expr->value.constructor)->expr);
   10346          180 :       lse.expr = dest;
   10347          180 :       gfc_add_modify (&block, lse.expr,
   10348          180 :                       fold_convert (TREE_TYPE (lse.expr), se.expr));
   10349              : 
   10350          180 :       return gfc_finish_block (&block);
   10351              :     }
   10352              : 
   10353              :   /* Make sure that the derived type has been completely built.  */
   10354        20114 :   if (!expr->ts.u.derived->backend_decl
   10355        20114 :       || !TYPE_FIELDS (expr->ts.u.derived->backend_decl))
   10356              :     {
   10357          224 :       tmp = gfc_typenode_for_spec (&expr->ts);
   10358          224 :       gcc_assert (tmp);
   10359              :     }
   10360              : 
   10361        20114 :   cm = expr->ts.u.derived->components;
   10362              : 
   10363              : 
   10364        20114 :   if (coarray)
   10365          223 :     gfc_init_se (&se, NULL);
   10366              : 
   10367        20114 :   for (c = gfc_constructor_first (expr->value.constructor);
   10368        52325 :        c; c = gfc_constructor_next (c), cm = cm->next)
   10369              :     {
   10370              :       /* Skip absent members in default initializers.  */
   10371        32211 :       if (!c->expr && !cm->attr.allocatable)
   10372         3108 :         continue;
   10373              : 
   10374              :       /* Register the component with the caf-lib before it is initialized.
   10375              :          Register only allocatable components, that are not coarray'ed
   10376              :          components (%comp[*]).  Only register when the constructor is the
   10377              :          null-expression.  */
   10378        29103 :       if (coarray && !cm->attr.codimension
   10379          513 :           && (cm->attr.allocatable || cm->attr.pointer)
   10380          177 :           && (!c->expr || c->expr->expr_type == EXPR_NULL))
   10381              :         {
   10382          175 :           tree token, desc, size;
   10383          350 :           bool is_array = cm->ts.type == BT_CLASS
   10384          175 :               ? CLASS_DATA (cm)->attr.dimension : cm->attr.dimension;
   10385              : 
   10386          175 :           field = cm->backend_decl;
   10387          175 :           field = fold_build3_loc (input_location, COMPONENT_REF,
   10388          175 :                                    TREE_TYPE (field), dest, field, NULL_TREE);
   10389          175 :           if (cm->ts.type == BT_CLASS)
   10390            0 :             field = gfc_class_data_get (field);
   10391              : 
   10392          175 :           token
   10393              :             = is_array
   10394          175 :                 ? gfc_conv_descriptor_token (field)
   10395           52 :                 : fold_build3_loc (input_location, COMPONENT_REF,
   10396           52 :                                    TREE_TYPE (gfc_comp_caf_token (cm)), dest,
   10397           52 :                                    gfc_comp_caf_token (cm), NULL_TREE);
   10398              : 
   10399          175 :           if (is_array)
   10400              :             {
   10401              :               /* The _caf_register routine looks at the rank of the array
   10402              :                  descriptor to decide whether the data registered is an array
   10403              :                  or not.  */
   10404          123 :               int rank = cm->ts.type == BT_CLASS ? CLASS_DATA (cm)->as->rank
   10405          123 :                                                  : cm->as->rank;
   10406              :               /* When the rank is not known just set a positive rank, which
   10407              :                  suffices to recognize the data as array.  */
   10408          123 :               if (rank < 0)
   10409            0 :                 rank = 1;
   10410          123 :               size = build_zero_cst (size_type_node);
   10411          123 :               desc = field;
   10412          123 :               gfc_add_modify (&block, gfc_conv_descriptor_rank (desc),
   10413          123 :                               build_int_cst (signed_char_type_node, rank));
   10414              :             }
   10415              :           else
   10416              :             {
   10417           52 :               desc = gfc_conv_scalar_to_descriptor (&se, field,
   10418           52 :                                                     cm->ts.type == BT_CLASS
   10419           52 :                                                     ? CLASS_DATA (cm)->attr
   10420              :                                                     : cm->attr);
   10421           52 :               size = TYPE_SIZE_UNIT (TREE_TYPE (field));
   10422              :             }
   10423          175 :           gfc_add_block_to_block (&block, &se.pre);
   10424          175 :           tmp =  build_call_expr_loc (input_location, gfor_fndecl_caf_register,
   10425              :                                       7, size, build_int_cst (
   10426              :                                         integer_type_node,
   10427              :                                         GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY),
   10428              :                                       gfc_build_addr_expr (pvoid_type_node,
   10429              :                                                            token),
   10430              :                                       gfc_build_addr_expr (NULL_TREE, desc),
   10431              :                                       null_pointer_node, null_pointer_node,
   10432              :                                       integer_zero_node);
   10433          175 :           gfc_add_expr_to_block (&block, tmp);
   10434              :         }
   10435        29103 :       field = cm->backend_decl;
   10436        29103 :       gcc_assert(field);
   10437        29103 :       tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
   10438              :                              dest, field, NULL_TREE);
   10439        29103 :       if (!c->expr)
   10440              :         {
   10441            0 :           gfc_expr *e = gfc_get_null_expr (NULL);
   10442            0 :           tmp = gfc_trans_subcomponent_assign (tmp, cm, e, init);
   10443            0 :           gfc_free_expr (e);
   10444              :         }
   10445              :       else
   10446        29103 :         tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr, init);
   10447        29103 :       gfc_add_expr_to_block (&block, tmp);
   10448              :     }
   10449        20114 :   return gfc_finish_block (&block);
   10450              : }
   10451              : 
   10452              : static void
   10453           21 : gfc_conv_union_initializer (vec<constructor_elt, va_gc> *&v,
   10454              :                             gfc_component *un, gfc_expr *init)
   10455              : {
   10456           21 :   gfc_constructor *ctor;
   10457              : 
   10458           21 :   if (un->ts.type != BT_UNION || un == NULL || init == NULL)
   10459              :     return;
   10460              : 
   10461           21 :   ctor = gfc_constructor_first (init->value.constructor);
   10462              : 
   10463           21 :   if (ctor == NULL || ctor->expr == NULL)
   10464              :     return;
   10465              : 
   10466           21 :   gcc_assert (init->expr_type == EXPR_STRUCTURE);
   10467              : 
   10468              :   /* If we have an 'initialize all' constructor, do it first.  */
   10469           21 :   if (ctor->expr->expr_type == EXPR_NULL)
   10470              :     {
   10471            9 :       tree union_type = TREE_TYPE (un->backend_decl);
   10472            9 :       tree val = build_constructor (union_type, NULL);
   10473            9 :       CONSTRUCTOR_APPEND_ELT (v, un->backend_decl, val);
   10474            9 :       ctor = gfc_constructor_next (ctor);
   10475              :     }
   10476              : 
   10477              :   /* Add the map initializer on top.  */
   10478           21 :   if (ctor != NULL && ctor->expr != NULL)
   10479              :     {
   10480           12 :       gcc_assert (ctor->expr->expr_type == EXPR_STRUCTURE);
   10481           12 :       tree val = gfc_conv_initializer (ctor->expr, &un->ts,
   10482           12 :                                        TREE_TYPE (un->backend_decl),
   10483           12 :                                        un->attr.dimension, un->attr.pointer,
   10484           12 :                                        un->attr.proc_pointer);
   10485           12 :       CONSTRUCTOR_APPEND_ELT (v, un->backend_decl, val);
   10486              :     }
   10487              : }
   10488              : 
   10489              : /* Build an expression for a constructor. If init is nonzero then
   10490              :    this is part of a static variable initializer.  */
   10491              : 
   10492              : void
   10493        38955 : gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
   10494              : {
   10495        38955 :   gfc_constructor *c;
   10496        38955 :   gfc_component *cm;
   10497        38955 :   tree val;
   10498        38955 :   tree type;
   10499        38955 :   tree tmp;
   10500        38955 :   vec<constructor_elt, va_gc> *v = NULL;
   10501              : 
   10502        38955 :   gcc_assert (se->ss == NULL);
   10503        38955 :   gcc_assert (expr->expr_type == EXPR_STRUCTURE);
   10504        38955 :   type = gfc_typenode_for_spec (&expr->ts);
   10505              : 
   10506        38955 :   if (!init)
   10507              :     {
   10508        15943 :       if (IS_PDT (expr) && expr->must_finalize)
   10509          276 :         final_block = &se->finalblock;
   10510              : 
   10511              :       /* Create a temporary variable and fill it in.  */
   10512        15943 :       se->expr = gfc_create_var (type, expr->ts.u.derived->name);
   10513              :       /* The symtree in expr is NULL, if the code to generate is for
   10514              :          initializing the static members only.  */
   10515        31886 :       tmp = gfc_trans_structure_assign (se->expr, expr, expr->symtree != NULL,
   10516        15943 :                                         se->want_coarray);
   10517        15943 :       gfc_add_expr_to_block (&se->pre, tmp);
   10518        15943 :       final_block = NULL;
   10519        15943 :       return;
   10520              :     }
   10521              : 
   10522        23012 :   cm = expr->ts.u.derived->components;
   10523              : 
   10524        23012 :   for (c = gfc_constructor_first (expr->value.constructor);
   10525       121248 :        c && cm; c = gfc_constructor_next (c), cm = cm->next)
   10526              :     {
   10527              :       /* Skip absent members in default initializers and allocatable
   10528              :          components.  Although the latter have a default initializer
   10529              :          of EXPR_NULL,... by default, the static nullify is not needed
   10530              :          since this is done every time we come into scope.  */
   10531       106764 :       if (!c->expr
   10532        95837 :           || (cm->attr.allocatable && cm->attr.flavor != FL_PROCEDURE)
   10533       188028 :           || (IS_PDT (cm) && has_parameterized_comps (cm->ts.u.derived)))
   10534         8528 :         continue;
   10535              : 
   10536        89708 :       if (cm->initializer && cm->initializer->expr_type != EXPR_NULL
   10537        51943 :           && strcmp (cm->name, "_extends") == 0
   10538         1294 :           && cm->initializer->symtree)
   10539              :         {
   10540         1294 :           tree vtab;
   10541         1294 :           gfc_symbol *vtabs;
   10542         1294 :           vtabs = cm->initializer->symtree->n.sym;
   10543         1294 :           vtab = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtabs));
   10544         1294 :           vtab = unshare_expr_without_location (vtab);
   10545         1294 :           CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, vtab);
   10546         1294 :         }
   10547        88414 :       else if (cm->ts.u.derived && strcmp (cm->name, "_size") == 0)
   10548              :         {
   10549         9832 :           val = TYPE_SIZE_UNIT (gfc_get_derived_type (cm->ts.u.derived));
   10550         9832 :           CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
   10551              :                                   fold_convert (TREE_TYPE (cm->backend_decl),
   10552              :                                                 val));
   10553         9832 :         }
   10554        78582 :       else if (cm->ts.type == BT_INTEGER && strcmp (cm->name, "_len") == 0)
   10555          403 :         CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
   10556              :                                 fold_convert (TREE_TYPE (cm->backend_decl),
   10557          403 :                                               integer_zero_node));
   10558        78179 :       else if (cm->ts.type == BT_UNION)
   10559           21 :         gfc_conv_union_initializer (v, cm, c->expr);
   10560              :       else
   10561              :         {
   10562        78158 :           val = gfc_conv_initializer (c->expr, &cm->ts,
   10563        78158 :                                       TREE_TYPE (cm->backend_decl),
   10564        78158 :                                       cm->attr.dimension, cm->attr.pointer,
   10565        78158 :                                       cm->attr.proc_pointer);
   10566        78158 :           val = unshare_expr_without_location (val);
   10567              : 
   10568              :           /* Append it to the constructor list.  */
   10569       176394 :           CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
   10570              :         }
   10571              :     }
   10572              : 
   10573        23012 :   se->expr = build_constructor (type, v);
   10574        23012 :   if (init)
   10575        23012 :     TREE_CONSTANT (se->expr) = 1;
   10576              : }
   10577              : 
   10578              : 
   10579              : /* Translate a substring expression.  */
   10580              : 
   10581              : static void
   10582          258 : gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
   10583              : {
   10584          258 :   gfc_ref *ref;
   10585              : 
   10586          258 :   ref = expr->ref;
   10587              : 
   10588          258 :   gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
   10589              : 
   10590          516 :   se->expr = gfc_build_wide_string_const (expr->ts.kind,
   10591          258 :                                           expr->value.character.length,
   10592          258 :                                           expr->value.character.string);
   10593              : 
   10594          258 :   se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
   10595          258 :   TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
   10596              : 
   10597          258 :   if (ref)
   10598          258 :     gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
   10599          258 : }
   10600              : 
   10601              : 
   10602              : /* Entry point for expression translation.  Evaluates a scalar quantity.
   10603              :    EXPR is the expression to be translated, and SE is the state structure if
   10604              :    called from within the scalarized.  */
   10605              : 
   10606              : void
   10607      3623364 : gfc_conv_expr (gfc_se * se, gfc_expr * expr)
   10608              : {
   10609      3623364 :   gfc_ss *ss;
   10610              : 
   10611      3623364 :   ss = se->ss;
   10612      3623364 :   if (ss && ss->info->expr == expr
   10613       236671 :       && (ss->info->type == GFC_SS_SCALAR
   10614              :           || ss->info->type == GFC_SS_REFERENCE))
   10615              :     {
   10616        40195 :       gfc_ss_info *ss_info;
   10617              : 
   10618        40195 :       ss_info = ss->info;
   10619              :       /* Substitute a scalar expression evaluated outside the scalarization
   10620              :          loop.  */
   10621        40195 :       se->expr = ss_info->data.scalar.value;
   10622        40195 :       if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
   10623          838 :         se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
   10624              : 
   10625        40195 :       se->string_length = ss_info->string_length;
   10626        40195 :       gfc_advance_se_ss_chain (se);
   10627        40195 :       return;
   10628              :     }
   10629              : 
   10630              :   /* We need to convert the expressions for the iso_c_binding derived types.
   10631              :      C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
   10632              :      null_pointer_node.  C_PTR and C_FUNPTR are converted to match the
   10633              :      typespec for the C_PTR and C_FUNPTR symbols, which has already been
   10634              :      updated to be an integer with a kind equal to the size of a (void *).  */
   10635      3583169 :   if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->ts.f90_type == BT_VOID
   10636        15951 :       && expr->ts.u.derived->attr.is_bind_c)
   10637              :     {
   10638        15112 :       if (expr->expr_type == EXPR_VARIABLE
   10639        10775 :           && (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
   10640        10775 :               || expr->symtree->n.sym->intmod_sym_id
   10641              :                  == ISOCBINDING_NULL_FUNPTR))
   10642              :         {
   10643              :           /* Set expr_type to EXPR_NULL, which will result in
   10644              :              null_pointer_node being used below.  */
   10645            0 :           expr->expr_type = EXPR_NULL;
   10646              :         }
   10647              :       else
   10648              :         {
   10649              :           /* Update the type/kind of the expression to be what the new
   10650              :              type/kind are for the updated symbols of C_PTR/C_FUNPTR.  */
   10651        15112 :           expr->ts.type = BT_INTEGER;
   10652        15112 :           expr->ts.f90_type = BT_VOID;
   10653        15112 :           expr->ts.kind = gfc_index_integer_kind;
   10654              :         }
   10655              :     }
   10656              : 
   10657      3583169 :   gfc_fix_class_refs (expr);
   10658              : 
   10659      3583169 :   switch (expr->expr_type)
   10660              :     {
   10661       503582 :     case EXPR_OP:
   10662       503582 :       gfc_conv_expr_op (se, expr);
   10663       503582 :       break;
   10664              : 
   10665          139 :     case EXPR_CONDITIONAL:
   10666          139 :       gfc_conv_conditional_expr (se, expr);
   10667          139 :       break;
   10668              : 
   10669       302559 :     case EXPR_FUNCTION:
   10670       302559 :       gfc_conv_function_expr (se, expr);
   10671       302559 :       break;
   10672              : 
   10673      1128949 :     case EXPR_CONSTANT:
   10674      1128949 :       gfc_conv_constant (se, expr);
   10675      1128949 :       break;
   10676              : 
   10677      1592215 :     case EXPR_VARIABLE:
   10678      1592215 :       gfc_conv_variable (se, expr);
   10679      1592215 :       break;
   10680              : 
   10681         4199 :     case EXPR_NULL:
   10682         4199 :       se->expr = null_pointer_node;
   10683         4199 :       break;
   10684              : 
   10685          258 :     case EXPR_SUBSTRING:
   10686          258 :       gfc_conv_substring_expr (se, expr);
   10687          258 :       break;
   10688              : 
   10689        15943 :     case EXPR_STRUCTURE:
   10690        15943 :       gfc_conv_structure (se, expr, 0);
   10691              :       /* F2008 4.5.6.3 para 5: If an executable construct references a
   10692              :          structure constructor or array constructor, the entity created by
   10693              :          the constructor is finalized after execution of the innermost
   10694              :          executable construct containing the reference. This, in fact,
   10695              :          was later deleted by the Combined Techical Corrigenda 1 TO 4 for
   10696              :          fortran 2008 (f08/0011).  */
   10697        15943 :       if ((gfc_option.allow_std & (GFC_STD_F2008 | GFC_STD_F2003))
   10698        15943 :           && !(gfc_option.allow_std & GFC_STD_GNU)
   10699          139 :           && expr->must_finalize
   10700        15955 :           && gfc_may_be_finalized (expr->ts))
   10701              :         {
   10702           12 :           locus loc;
   10703           12 :           gfc_locus_from_location (&loc, input_location);
   10704           12 :           gfc_warning (0, "The structure constructor at %L has been"
   10705              :                          " finalized. This feature was removed by f08/0011."
   10706              :                          " Use -std=f2018 or -std=gnu to eliminate the"
   10707              :                          " finalization.", &loc);
   10708           12 :           symbol_attribute attr;
   10709           12 :           attr.allocatable = attr.pointer = 0;
   10710           12 :           gfc_finalize_tree_expr (se, expr->ts.u.derived, attr, 0);
   10711           12 :           gfc_add_block_to_block (&se->post, &se->finalblock);
   10712              :         }
   10713              :       break;
   10714              : 
   10715        35325 :     case EXPR_ARRAY:
   10716        35325 :       gfc_conv_array_constructor_expr (se, expr);
   10717        35325 :       gfc_add_block_to_block (&se->post, &se->finalblock);
   10718        35325 :       break;
   10719              : 
   10720            0 :     default:
   10721            0 :       gcc_unreachable ();
   10722      3623364 :       break;
   10723              :     }
   10724              : }
   10725              : 
   10726              : /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
   10727              :    of an assignment.  */
   10728              : void
   10729       369781 : gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
   10730              : {
   10731       369781 :   gfc_conv_expr (se, expr);
   10732              :   /* All numeric lvalues should have empty post chains.  If not we need to
   10733              :      figure out a way of rewriting an lvalue so that it has no post chain.  */
   10734       369781 :   gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
   10735       369781 : }
   10736              : 
   10737              : /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
   10738              :    numeric expressions.  Used for scalar values where inserting cleanup code
   10739              :    is inconvenient.  */
   10740              : void
   10741      1025826 : gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
   10742              : {
   10743      1025826 :   tree val;
   10744              : 
   10745      1025826 :   gcc_assert (expr->ts.type != BT_CHARACTER);
   10746      1025826 :   gfc_conv_expr (se, expr);
   10747      1025826 :   if (se->post.head)
   10748              :     {
   10749         2558 :       val = gfc_create_var (TREE_TYPE (se->expr), NULL);
   10750         2558 :       gfc_add_modify (&se->pre, val, se->expr);
   10751         2558 :       se->expr = val;
   10752         2558 :       gfc_add_block_to_block (&se->pre, &se->post);
   10753              :     }
   10754      1025826 : }
   10755              : 
   10756              : /* Helper to translate an expression and convert it to a particular type.  */
   10757              : void
   10758       289127 : gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
   10759              : {
   10760       289127 :   gfc_conv_expr_val (se, expr);
   10761       289127 :   se->expr = convert (type, se->expr);
   10762       289127 : }
   10763              : 
   10764              : 
   10765              : /* Converts an expression so that it can be passed by reference.  Scalar
   10766              :    values only.  */
   10767              : 
   10768              : void
   10769       226395 : gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
   10770              : {
   10771       226395 :   gfc_ss *ss;
   10772       226395 :   tree var;
   10773              : 
   10774       226395 :   ss = se->ss;
   10775       226395 :   if (ss && ss->info->expr == expr
   10776         7987 :       && ss->info->type == GFC_SS_REFERENCE)
   10777              :     {
   10778              :       /* Returns a reference to the scalar evaluated outside the loop
   10779              :          for this case.  */
   10780          907 :       gfc_conv_expr (se, expr);
   10781              : 
   10782          907 :       if (expr->ts.type == BT_CHARACTER
   10783          114 :           && expr->expr_type != EXPR_FUNCTION)
   10784          102 :         gfc_conv_string_parameter (se);
   10785              :      else
   10786          805 :         se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
   10787              : 
   10788          907 :       return;
   10789              :     }
   10790              : 
   10791       225488 :   if (expr->ts.type == BT_CHARACTER)
   10792              :     {
   10793        49442 :       gfc_conv_expr (se, expr);
   10794        49442 :       gfc_conv_string_parameter (se);
   10795        49442 :       return;
   10796              :     }
   10797              : 
   10798       176046 :   if (expr->expr_type == EXPR_VARIABLE)
   10799              :     {
   10800        69812 :       se->want_pointer = 1;
   10801        69812 :       gfc_conv_expr (se, expr);
   10802        69812 :       if (se->post.head)
   10803              :         {
   10804            0 :           var = gfc_create_var (TREE_TYPE (se->expr), NULL);
   10805            0 :           gfc_add_modify (&se->pre, var, se->expr);
   10806            0 :           gfc_add_block_to_block (&se->pre, &se->post);
   10807            0 :           se->expr = var;
   10808              :         }
   10809        69812 :       return;
   10810              :     }
   10811              : 
   10812       106234 :   if (expr->expr_type == EXPR_CONDITIONAL)
   10813              :     {
   10814           18 :       se->want_pointer = 1;
   10815           18 :       gfc_conv_expr (se, expr);
   10816           18 :       return;
   10817              :     }
   10818              : 
   10819       106216 :   if (expr->expr_type == EXPR_FUNCTION
   10820        13581 :       && ((expr->value.function.esym
   10821         2089 :            && expr->value.function.esym->result
   10822         2088 :            && expr->value.function.esym->result->attr.pointer
   10823           83 :            && !expr->value.function.esym->result->attr.dimension)
   10824        13504 :           || (!expr->value.function.esym && !expr->ref
   10825        11386 :               && expr->symtree->n.sym->attr.pointer
   10826            0 :               && !expr->symtree->n.sym->attr.dimension)))
   10827              :     {
   10828           77 :       se->want_pointer = 1;
   10829           77 :       gfc_conv_expr (se, expr);
   10830           77 :       var = gfc_create_var (TREE_TYPE (se->expr), NULL);
   10831           77 :       gfc_add_modify (&se->pre, var, se->expr);
   10832           77 :       se->expr = var;
   10833           77 :       return;
   10834              :     }
   10835              : 
   10836       106139 :   gfc_conv_expr (se, expr);
   10837              : 
   10838              :   /* Create a temporary var to hold the value.  */
   10839       106139 :   if (TREE_CONSTANT (se->expr))
   10840              :     {
   10841              :       tree tmp = se->expr;
   10842        84085 :       STRIP_TYPE_NOPS (tmp);
   10843        84085 :       var = build_decl (input_location,
   10844        84085 :                         CONST_DECL, NULL, TREE_TYPE (tmp));
   10845        84085 :       DECL_INITIAL (var) = tmp;
   10846        84085 :       TREE_STATIC (var) = 1;
   10847        84085 :       pushdecl (var);
   10848              :     }
   10849              :   else
   10850              :     {
   10851        22054 :       var = gfc_create_var (TREE_TYPE (se->expr), NULL);
   10852        22054 :       gfc_add_modify (&se->pre, var, se->expr);
   10853              :     }
   10854              : 
   10855       106139 :   if (!expr->must_finalize)
   10856       106043 :     gfc_add_block_to_block (&se->pre, &se->post);
   10857              : 
   10858              :   /* Take the address of that value.  */
   10859       106139 :   se->expr = gfc_build_addr_expr (NULL_TREE, var);
   10860              : }
   10861              : 
   10862              : 
   10863              : /* Get the _len component for an unlimited polymorphic expression.  */
   10864              : 
   10865              : static tree
   10866         1788 : trans_get_upoly_len (stmtblock_t *block, gfc_expr *expr)
   10867              : {
   10868         1788 :   gfc_se se;
   10869         1788 :   gfc_ref *ref = expr->ref;
   10870              : 
   10871         1788 :   gfc_init_se (&se, NULL);
   10872         3690 :   while (ref && ref->next)
   10873              :     ref = ref->next;
   10874         1788 :   gfc_add_len_component (expr);
   10875         1788 :   gfc_conv_expr (&se, expr);
   10876         1788 :   gfc_add_block_to_block (block, &se.pre);
   10877         1788 :   gcc_assert (se.post.head == NULL_TREE);
   10878         1788 :   if (ref)
   10879              :     {
   10880          262 :       gfc_free_ref_list (ref->next);
   10881          262 :       ref->next = NULL;
   10882              :     }
   10883              :   else
   10884              :     {
   10885         1526 :       gfc_free_ref_list (expr->ref);
   10886         1526 :       expr->ref = NULL;
   10887              :     }
   10888         1788 :   return se.expr;
   10889              : }
   10890              : 
   10891              : 
   10892              : /* Assign _vptr and _len components as appropriate.  BLOCK should be a
   10893              :    statement-list outside of the scalarizer-loop.  When code is generated, that
   10894              :    depends on the scalarized expression, it is added to RSE.PRE.
   10895              :    Returns le's _vptr tree and when set the len expressions in to_lenp and
   10896              :    from_lenp to form a le%_vptr%_copy (re, le, [from_lenp, to_lenp])
   10897              :    expression.  */
   10898              : 
   10899              : static tree
   10900         4519 : trans_class_vptr_len_assignment (stmtblock_t *block, gfc_expr * le,
   10901              :                                  gfc_expr * re, gfc_se *rse,
   10902              :                                  tree * to_lenp, tree * from_lenp,
   10903              :                                  tree * from_vptrp)
   10904              : {
   10905         4519 :   gfc_se se;
   10906         4519 :   gfc_expr * vptr_expr;
   10907         4519 :   tree tmp, to_len = NULL_TREE, from_len = NULL_TREE, lhs_vptr;
   10908         4519 :   bool set_vptr = false, temp_rhs = false;
   10909         4519 :   stmtblock_t *pre = block;
   10910         4519 :   tree class_expr = NULL_TREE;
   10911         4519 :   tree from_vptr = NULL_TREE;
   10912              : 
   10913              :   /* Create a temporary for complicated expressions.  */
   10914         4519 :   if (re->expr_type != EXPR_VARIABLE && re->expr_type != EXPR_NULL
   10915         1262 :       && rse->expr != NULL_TREE)
   10916              :     {
   10917         1262 :       if (!DECL_P (rse->expr))
   10918              :         {
   10919          403 :           if (re->ts.type == BT_CLASS && !GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
   10920           37 :             class_expr = gfc_get_class_from_expr (rse->expr);
   10921              : 
   10922          403 :           if (rse->loop)
   10923          159 :             pre = &rse->loop->pre;
   10924              :           else
   10925          244 :             pre = &rse->pre;
   10926              : 
   10927          403 :           if (class_expr != NULL_TREE && UNLIMITED_POLY (re))
   10928           37 :               tmp = gfc_evaluate_now (TREE_OPERAND (rse->expr, 0), &rse->pre);
   10929              :           else
   10930          366 :               tmp = gfc_evaluate_now (rse->expr, &rse->pre);
   10931              : 
   10932          403 :           rse->expr = tmp;
   10933              :         }
   10934              :       else
   10935          859 :         pre = &rse->pre;
   10936              : 
   10937              :       temp_rhs = true;
   10938              :     }
   10939              : 
   10940              :   /* Get the _vptr for the left-hand side expression.  */
   10941         4519 :   gfc_init_se (&se, NULL);
   10942         4519 :   vptr_expr = gfc_find_and_cut_at_last_class_ref (le);
   10943         4519 :   if (vptr_expr != NULL && gfc_expr_attr (vptr_expr).class_ok)
   10944              :     {
   10945              :       /* Care about _len for unlimited polymorphic entities.  */
   10946         4501 :       if (UNLIMITED_POLY (vptr_expr)
   10947         3481 :           || (vptr_expr->ts.type == BT_DERIVED
   10948         2461 :               && vptr_expr->ts.u.derived->attr.unlimited_polymorphic))
   10949         1504 :         to_len = trans_get_upoly_len (block, vptr_expr);
   10950         4501 :       gfc_add_vptr_component (vptr_expr);
   10951         4501 :       set_vptr = true;
   10952              :     }
   10953              :   else
   10954           18 :     vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&le->ts));
   10955         4519 :   se.want_pointer = 1;
   10956         4519 :   gfc_conv_expr (&se, vptr_expr);
   10957         4519 :   gfc_free_expr (vptr_expr);
   10958         4519 :   gfc_add_block_to_block (block, &se.pre);
   10959         4519 :   gcc_assert (se.post.head == NULL_TREE);
   10960         4519 :   lhs_vptr = se.expr;
   10961         4519 :   STRIP_NOPS (lhs_vptr);
   10962              : 
   10963              :   /* Set the _vptr only when the left-hand side of the assignment is a
   10964              :      class-object.  */
   10965         4519 :   if (set_vptr)
   10966              :     {
   10967              :       /* Get the vptr from the rhs expression only, when it is variable.
   10968              :          Functions are expected to be assigned to a temporary beforehand.  */
   10969         3124 :       vptr_expr = (re->expr_type == EXPR_VARIABLE && re->ts.type == BT_CLASS)
   10970         5282 :           ? gfc_find_and_cut_at_last_class_ref (re)
   10971              :           : NULL;
   10972          781 :       if (vptr_expr != NULL && vptr_expr->ts.type == BT_CLASS)
   10973              :         {
   10974          781 :           if (to_len != NULL_TREE)
   10975              :             {
   10976              :               /* Get the _len information from the rhs.  */
   10977          299 :               if (UNLIMITED_POLY (vptr_expr)
   10978              :                   || (vptr_expr->ts.type == BT_DERIVED
   10979              :                       && vptr_expr->ts.u.derived->attr.unlimited_polymorphic))
   10980          272 :                 from_len = trans_get_upoly_len (block, vptr_expr);
   10981              :             }
   10982          781 :           gfc_add_vptr_component (vptr_expr);
   10983              :         }
   10984              :       else
   10985              :         {
   10986         3720 :           if (re->expr_type == EXPR_VARIABLE
   10987         2343 :               && DECL_P (re->symtree->n.sym->backend_decl)
   10988         2343 :               && DECL_LANG_SPECIFIC (re->symtree->n.sym->backend_decl)
   10989          821 :               && GFC_DECL_SAVED_DESCRIPTOR (re->symtree->n.sym->backend_decl)
   10990         3787 :               && GFC_CLASS_TYPE_P (TREE_TYPE (GFC_DECL_SAVED_DESCRIPTOR (
   10991              :                                            re->symtree->n.sym->backend_decl))))
   10992              :             {
   10993           43 :               vptr_expr = NULL;
   10994           43 :               se.expr = gfc_class_vptr_get (GFC_DECL_SAVED_DESCRIPTOR (
   10995              :                                              re->symtree->n.sym->backend_decl));
   10996           43 :               if (to_len && UNLIMITED_POLY (re))
   10997            0 :                 from_len = gfc_class_len_get (GFC_DECL_SAVED_DESCRIPTOR (
   10998              :                                              re->symtree->n.sym->backend_decl));
   10999              :             }
   11000         3677 :           else if (temp_rhs && re->ts.type == BT_CLASS)
   11001              :             {
   11002          214 :               vptr_expr = NULL;
   11003          214 :               if (class_expr)
   11004              :                 tmp = class_expr;
   11005          177 :               else if (!GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
   11006            0 :                 tmp = gfc_get_class_from_expr (rse->expr);
   11007              :               else
   11008              :                 tmp = rse->expr;
   11009              : 
   11010          214 :               se.expr = gfc_class_vptr_get (tmp);
   11011          214 :               from_vptr = se.expr;
   11012          214 :               if (UNLIMITED_POLY (re))
   11013           74 :                 from_len = gfc_class_len_get (tmp);
   11014              : 
   11015              :             }
   11016         3463 :           else if (re->expr_type != EXPR_NULL)
   11017              :             /* Only when rhs is non-NULL use its declared type for vptr
   11018              :                initialisation.  */
   11019         3336 :             vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&re->ts));
   11020              :           else
   11021              :             /* When the rhs is NULL use the vtab of lhs' declared type.  */
   11022          127 :             vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&le->ts));
   11023              :         }
   11024              : 
   11025         4318 :       if (vptr_expr)
   11026              :         {
   11027         4244 :           gfc_init_se (&se, NULL);
   11028         4244 :           se.want_pointer = 1;
   11029         4244 :           gfc_conv_expr (&se, vptr_expr);
   11030         4244 :           gfc_free_expr (vptr_expr);
   11031         4244 :           gfc_add_block_to_block (block, &se.pre);
   11032         4244 :           gcc_assert (se.post.head == NULL_TREE);
   11033         4244 :           from_vptr = se.expr;
   11034              :         }
   11035         4501 :       gfc_add_modify (pre, lhs_vptr, fold_convert (TREE_TYPE (lhs_vptr),
   11036              :                                                 se.expr));
   11037              : 
   11038         4501 :       if (to_len != NULL_TREE)
   11039              :         {
   11040              :           /* The _len component needs to be set.  Figure how to get the
   11041              :              value of the right-hand side.  */
   11042         1504 :           if (from_len == NULL_TREE)
   11043              :             {
   11044         1158 :               if (rse->string_length != NULL_TREE)
   11045              :                 from_len = rse->string_length;
   11046          712 :               else if (re->ts.type == BT_CHARACTER && re->ts.u.cl->length)
   11047              :                 {
   11048            0 :                   gfc_init_se (&se, NULL);
   11049            0 :                   gfc_conv_expr (&se, re->ts.u.cl->length);
   11050            0 :                   gfc_add_block_to_block (block, &se.pre);
   11051            0 :                   gcc_assert (se.post.head == NULL_TREE);
   11052            0 :                   from_len = gfc_evaluate_now (se.expr, block);
   11053              :                 }
   11054              :               else
   11055          712 :                 from_len = build_zero_cst (gfc_charlen_type_node);
   11056              :             }
   11057         1504 :           gfc_add_modify (pre, to_len, fold_convert (TREE_TYPE (to_len),
   11058              :                                                      from_len));
   11059              :         }
   11060              :     }
   11061              : 
   11062              :   /* Return the _len and _vptr trees only, when requested.  */
   11063         4519 :   if (to_lenp)
   11064         3318 :     *to_lenp = to_len;
   11065         4519 :   if (from_lenp)
   11066         3318 :     *from_lenp = from_len;
   11067         4519 :   if (from_vptrp)
   11068         3318 :     *from_vptrp = from_vptr;
   11069         4519 :   return lhs_vptr;
   11070              : }
   11071              : 
   11072              : 
   11073              : /* Assign tokens for pointer components.  */
   11074              : 
   11075              : static void
   11076           12 : trans_caf_token_assign (gfc_se *lse, gfc_se *rse, gfc_expr *expr1,
   11077              :                         gfc_expr *expr2)
   11078              : {
   11079           12 :   symbol_attribute lhs_attr, rhs_attr;
   11080           12 :   tree tmp, lhs_tok, rhs_tok;
   11081              :   /* Flag to indicated component refs on the rhs.  */
   11082           12 :   bool rhs_cr;
   11083              : 
   11084           12 :   lhs_attr = gfc_caf_attr (expr1);
   11085           12 :   if (expr2->expr_type != EXPR_NULL)
   11086              :     {
   11087            8 :       rhs_attr = gfc_caf_attr (expr2, false, &rhs_cr);
   11088            8 :       if (lhs_attr.codimension && rhs_attr.codimension)
   11089              :         {
   11090            4 :           lhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (lse, expr1);
   11091            4 :           lhs_tok = build_fold_indirect_ref (lhs_tok);
   11092              : 
   11093            4 :           if (rhs_cr)
   11094            0 :             rhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (rse, expr2);
   11095              :           else
   11096              :             {
   11097            4 :               tree caf_decl;
   11098            4 :               caf_decl = gfc_get_tree_for_caf_expr (expr2);
   11099            4 :               gfc_get_caf_token_offset (rse, &rhs_tok, NULL, caf_decl,
   11100              :                                         NULL_TREE, NULL);
   11101              :             }
   11102            4 :           tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
   11103              :                             lhs_tok,
   11104            4 :                             fold_convert (TREE_TYPE (lhs_tok), rhs_tok));
   11105            4 :           gfc_prepend_expr_to_block (&lse->post, tmp);
   11106              :         }
   11107              :     }
   11108            4 :   else if (lhs_attr.codimension)
   11109              :     {
   11110            4 :       lhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (lse, expr1);
   11111            4 :       if (!lhs_tok)
   11112              :         {
   11113            2 :           lhs_tok = gfc_get_tree_for_caf_expr (expr1);
   11114            2 :           lhs_tok = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (lhs_tok));
   11115              :         }
   11116              :       else
   11117            2 :         lhs_tok = build_fold_indirect_ref (lhs_tok);
   11118            4 :       tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
   11119              :                         lhs_tok, null_pointer_node);
   11120            4 :       gfc_prepend_expr_to_block (&lse->post, tmp);
   11121              :     }
   11122           12 : }
   11123              : 
   11124              : 
   11125              : /* Do everything that is needed for a CLASS function expr2.  */
   11126              : 
   11127              : static tree
   11128           18 : trans_class_pointer_fcn (stmtblock_t *block, gfc_se *lse, gfc_se *rse,
   11129              :                          gfc_expr *expr1, gfc_expr *expr2)
   11130              : {
   11131           18 :   tree expr1_vptr = NULL_TREE;
   11132           18 :   tree tmp;
   11133              : 
   11134           18 :   gfc_conv_function_expr (rse, expr2);
   11135           18 :   rse->expr = gfc_evaluate_now (rse->expr, &rse->pre);
   11136              : 
   11137           18 :   if (expr1->ts.type != BT_CLASS)
   11138           12 :       rse->expr = gfc_class_data_get (rse->expr);
   11139              :   else
   11140              :     {
   11141            6 :       expr1_vptr = trans_class_vptr_len_assignment (block, expr1,
   11142              :                                                     expr2, rse,
   11143              :                                                     NULL, NULL, NULL);
   11144            6 :       gfc_add_block_to_block (block, &rse->pre);
   11145            6 :       tmp = gfc_create_var (TREE_TYPE (rse->expr), "ptrtemp");
   11146            6 :       gfc_add_modify (&lse->pre, tmp, rse->expr);
   11147              : 
   11148           12 :       gfc_add_modify (&lse->pre, expr1_vptr,
   11149            6 :                       fold_convert (TREE_TYPE (expr1_vptr),
   11150              :                       gfc_class_vptr_get (tmp)));
   11151            6 :       rse->expr = gfc_class_data_get (tmp);
   11152              :     }
   11153              : 
   11154           18 :   return expr1_vptr;
   11155              : }
   11156              : 
   11157              : 
   11158              : tree
   11159        10097 : gfc_trans_pointer_assign (gfc_code * code)
   11160              : {
   11161        10097 :   return gfc_trans_pointer_assignment (code->expr1, code->expr2);
   11162              : }
   11163              : 
   11164              : 
   11165              : /* Generate code for a pointer assignment.  */
   11166              : 
   11167              : tree
   11168        10152 : gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
   11169              : {
   11170        10152 :   gfc_se lse;
   11171        10152 :   gfc_se rse;
   11172        10152 :   stmtblock_t block;
   11173        10152 :   tree desc;
   11174        10152 :   tree tmp;
   11175        10152 :   tree expr1_vptr = NULL_TREE;
   11176        10152 :   bool scalar, non_proc_ptr_assign;
   11177        10152 :   gfc_ss *ss;
   11178              : 
   11179        10152 :   gfc_start_block (&block);
   11180              : 
   11181        10152 :   gfc_init_se (&lse, NULL);
   11182              : 
   11183              :   /* Usually testing whether this is not a proc pointer assignment.  */
   11184        10152 :   non_proc_ptr_assign
   11185        10152 :     = !(gfc_expr_attr (expr1).proc_pointer
   11186         1187 :         && ((expr2->expr_type == EXPR_VARIABLE
   11187          955 :              && expr2->symtree->n.sym->attr.flavor == FL_PROCEDURE)
   11188          282 :             || expr2->expr_type == EXPR_NULL));
   11189              : 
   11190              :   /* Check whether the expression is a scalar or not; we cannot use
   11191              :      expr1->rank as it can be nonzero for proc pointers.  */
   11192        10152 :   ss = gfc_walk_expr (expr1);
   11193        10152 :   scalar = ss == gfc_ss_terminator;
   11194        10152 :   if (!scalar)
   11195         4360 :     gfc_free_ss_chain (ss);
   11196              : 
   11197        10152 :   if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS
   11198           90 :       && expr2->expr_type != EXPR_FUNCTION && non_proc_ptr_assign)
   11199              :     {
   11200           66 :       gfc_add_data_component (expr2);
   11201              :       /* The following is required as gfc_add_data_component doesn't
   11202              :          update ts.type if there is a trailing REF_ARRAY.  */
   11203           66 :       expr2->ts.type = BT_DERIVED;
   11204              :     }
   11205              : 
   11206        10152 :   if (scalar)
   11207              :     {
   11208              :       /* Scalar pointers.  */
   11209         5792 :       lse.want_pointer = 1;
   11210         5792 :       gfc_conv_expr (&lse, expr1);
   11211         5792 :       gfc_init_se (&rse, NULL);
   11212         5792 :       rse.want_pointer = 1;
   11213         5792 :       if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
   11214            6 :         trans_class_pointer_fcn (&block, &lse, &rse, expr1, expr2);
   11215              :       else
   11216         5786 :         gfc_conv_expr (&rse, expr2);
   11217              : 
   11218         5792 :       if (non_proc_ptr_assign && expr1->ts.type == BT_CLASS)
   11219              :         {
   11220          766 :           trans_class_vptr_len_assignment (&block, expr1, expr2, &rse, NULL,
   11221              :                                            NULL, NULL);
   11222          766 :           lse.expr = gfc_class_data_get (lse.expr);
   11223              :         }
   11224              : 
   11225         5792 :       if (expr1->symtree->n.sym->attr.proc_pointer
   11226          857 :           && expr1->symtree->n.sym->attr.dummy)
   11227           49 :         lse.expr = build_fold_indirect_ref_loc (input_location,
   11228              :                                                 lse.expr);
   11229              : 
   11230         5792 :       if (expr2->symtree && expr2->symtree->n.sym->attr.proc_pointer
   11231           47 :           && expr2->symtree->n.sym->attr.dummy)
   11232           20 :         rse.expr = build_fold_indirect_ref_loc (input_location,
   11233              :                                                 rse.expr);
   11234              : 
   11235         5792 :       gfc_add_block_to_block (&block, &lse.pre);
   11236         5792 :       gfc_add_block_to_block (&block, &rse.pre);
   11237              : 
   11238              :       /* Check character lengths if character expression.  The test is only
   11239              :          really added if -fbounds-check is enabled.  Exclude deferred
   11240              :          character length lefthand sides.  */
   11241          954 :       if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL
   11242          780 :           && !expr1->ts.deferred
   11243          365 :           && !expr1->symtree->n.sym->attr.proc_pointer
   11244         6150 :           && !gfc_is_proc_ptr_comp (expr1))
   11245              :         {
   11246          339 :           gcc_assert (expr2->ts.type == BT_CHARACTER);
   11247          339 :           gcc_assert (lse.string_length && rse.string_length);
   11248          339 :           gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
   11249              :                                        lse.string_length, rse.string_length,
   11250              :                                        &block);
   11251              :         }
   11252              : 
   11253              :       /* The assignment to an deferred character length sets the string
   11254              :          length to that of the rhs.  */
   11255         5792 :       if (expr1->ts.deferred)
   11256              :         {
   11257          530 :           if (expr2->expr_type != EXPR_NULL && lse.string_length != NULL)
   11258          413 :             gfc_add_modify (&block, lse.string_length,
   11259          413 :                             fold_convert (TREE_TYPE (lse.string_length),
   11260              :                                           rse.string_length));
   11261          117 :           else if (lse.string_length != NULL)
   11262          115 :             gfc_add_modify (&block, lse.string_length,
   11263          115 :                             build_zero_cst (TREE_TYPE (lse.string_length)));
   11264              :         }
   11265              : 
   11266         5792 :       gfc_add_modify (&block, lse.expr,
   11267         5792 :                       fold_convert (TREE_TYPE (lse.expr), rse.expr));
   11268              : 
   11269         5792 :       if (flag_coarray == GFC_FCOARRAY_LIB)
   11270              :         {
   11271          336 :           if (expr1->ref)
   11272              :             /* Also set the tokens for pointer components in derived typed
   11273              :                coarrays.  */
   11274           12 :             trans_caf_token_assign (&lse, &rse, expr1, expr2);
   11275          324 :           else if (gfc_caf_attr (expr1).codimension)
   11276              :             {
   11277            0 :               tree lhs_caf_decl, rhs_caf_decl, lhs_tok, rhs_tok;
   11278              : 
   11279            0 :               lhs_caf_decl = gfc_get_tree_for_caf_expr (expr1);
   11280            0 :               rhs_caf_decl = gfc_get_tree_for_caf_expr (expr2);
   11281            0 :               gfc_get_caf_token_offset (&lse, &lhs_tok, nullptr, lhs_caf_decl,
   11282              :                                         NULL_TREE, expr1);
   11283            0 :               gfc_get_caf_token_offset (&rse, &rhs_tok, nullptr, rhs_caf_decl,
   11284              :                                         NULL_TREE, expr2);
   11285            0 :               gfc_add_modify (&block, lhs_tok, rhs_tok);
   11286              :             }
   11287              :         }
   11288              : 
   11289         5792 :       gfc_add_block_to_block (&block, &rse.post);
   11290         5792 :       gfc_add_block_to_block (&block, &lse.post);
   11291              :     }
   11292              :   else
   11293              :     {
   11294         4360 :       gfc_ref* remap;
   11295         4360 :       bool rank_remap;
   11296         4360 :       tree strlen_lhs;
   11297         4360 :       tree strlen_rhs = NULL_TREE;
   11298              : 
   11299              :       /* Array pointer.  Find the last reference on the LHS and if it is an
   11300              :          array section ref, we're dealing with bounds remapping.  In this case,
   11301              :          set it to AR_FULL so that gfc_conv_expr_descriptor does
   11302              :          not see it and process the bounds remapping afterwards explicitly.  */
   11303        14046 :       for (remap = expr1->ref; remap; remap = remap->next)
   11304         5705 :         if (!remap->next && remap->type == REF_ARRAY
   11305         4360 :             && remap->u.ar.type == AR_SECTION)
   11306              :           break;
   11307         4360 :       rank_remap = (remap && remap->u.ar.end[0]);
   11308              : 
   11309          379 :       if (remap && expr2->expr_type == EXPR_NULL)
   11310              :         {
   11311            2 :           gfc_error ("If bounds remapping is specified at %L, "
   11312              :                      "the pointer target shall not be NULL", &expr1->where);
   11313            2 :           return NULL_TREE;
   11314              :         }
   11315              : 
   11316         4358 :       gfc_init_se (&lse, NULL);
   11317         4358 :       if (remap)
   11318          377 :         lse.descriptor_only = 1;
   11319         4358 :       gfc_conv_expr_descriptor (&lse, expr1);
   11320         4358 :       strlen_lhs = lse.string_length;
   11321         4358 :       desc = lse.expr;
   11322              : 
   11323         4358 :       if (expr2->expr_type == EXPR_NULL)
   11324              :         {
   11325              :           /* Just set the data pointer to null.  */
   11326          680 :           gfc_conv_descriptor_data_set (&lse.pre, lse.expr, null_pointer_node);
   11327              :         }
   11328         3678 :       else if (rank_remap)
   11329              :         {
   11330              :           /* If we are rank-remapping, just get the RHS's descriptor and
   11331              :              process this later on.  */
   11332          254 :           gfc_init_se (&rse, NULL);
   11333          254 :           rse.direct_byref = 1;
   11334          254 :           rse.byref_noassign = 1;
   11335              : 
   11336          254 :           if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
   11337           12 :             expr1_vptr = trans_class_pointer_fcn (&block, &lse, &rse,
   11338              :                                                   expr1, expr2);
   11339          242 :           else if (expr2->expr_type == EXPR_FUNCTION)
   11340              :             {
   11341              :               tree bound[GFC_MAX_DIMENSIONS];
   11342              :               int i;
   11343              : 
   11344           26 :               for (i = 0; i < expr2->rank; i++)
   11345           13 :                 bound[i] = NULL_TREE;
   11346           13 :               tmp = gfc_typenode_for_spec (&expr2->ts);
   11347           13 :               tmp = gfc_get_array_type_bounds (tmp, expr2->rank, 0,
   11348              :                                                bound, bound, 0,
   11349              :                                                GFC_ARRAY_POINTER_CONT, false);
   11350           13 :               tmp = gfc_create_var (tmp, "ptrtemp");
   11351           13 :               rse.descriptor_only = 0;
   11352           13 :               rse.expr = tmp;
   11353           13 :               rse.direct_byref = 1;
   11354           13 :               gfc_conv_expr_descriptor (&rse, expr2);
   11355           13 :               strlen_rhs = rse.string_length;
   11356           13 :               rse.expr = tmp;
   11357              :             }
   11358              :           else
   11359              :             {
   11360          229 :               gfc_conv_expr_descriptor (&rse, expr2);
   11361          229 :               strlen_rhs = rse.string_length;
   11362          229 :               if (expr1->ts.type == BT_CLASS)
   11363           60 :                 expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
   11364              :                                                               expr2, &rse,
   11365              :                                                               NULL, NULL,
   11366              :                                                               NULL);
   11367              :             }
   11368              :         }
   11369         3424 :       else if (expr2->expr_type == EXPR_VARIABLE)
   11370              :         {
   11371              :           /* Assign directly to the LHS's descriptor.  */
   11372         3292 :           lse.descriptor_only = 0;
   11373         3292 :           lse.direct_byref = 1;
   11374         3292 :           gfc_conv_expr_descriptor (&lse, expr2);
   11375         3292 :           strlen_rhs = lse.string_length;
   11376         3292 :           gfc_init_se (&rse, NULL);
   11377              : 
   11378         3292 :           if (expr1->ts.type == BT_CLASS)
   11379              :             {
   11380          356 :               rse.expr = NULL_TREE;
   11381          356 :               rse.string_length = strlen_rhs;
   11382          356 :               trans_class_vptr_len_assignment (&block, expr1, expr2, &rse,
   11383              :                                                NULL, NULL, NULL);
   11384              :             }
   11385              : 
   11386         3292 :           if (remap == NULL)
   11387              :             {
   11388              :               /* If the target is not a whole array, use the target array
   11389              :                  reference for remap.  */
   11390         6757 :               for (remap = expr2->ref; remap; remap = remap->next)
   11391         3738 :                 if (remap->type == REF_ARRAY
   11392         3229 :                     && remap->u.ar.type == AR_FULL
   11393         2536 :                     && remap->next)
   11394              :                   break;
   11395              :             }
   11396              :         }
   11397          132 :       else if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
   11398              :         {
   11399           25 :           gfc_init_se (&rse, NULL);
   11400           25 :           rse.want_pointer = 1;
   11401           25 :           gfc_conv_function_expr (&rse, expr2);
   11402           25 :           if (expr1->ts.type != BT_CLASS)
   11403              :             {
   11404           12 :               rse.expr = gfc_class_data_get (rse.expr);
   11405           12 :               gfc_add_modify (&lse.pre, desc, rse.expr);
   11406              :             }
   11407              :           else
   11408              :             {
   11409           13 :               expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
   11410              :                                                             expr2, &rse, NULL,
   11411              :                                                             NULL, NULL);
   11412           13 :               gfc_add_block_to_block (&block, &rse.pre);
   11413           13 :               tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
   11414           13 :               gfc_add_modify (&lse.pre, tmp, rse.expr);
   11415              : 
   11416           26 :               gfc_add_modify (&lse.pre, expr1_vptr,
   11417           13 :                               fold_convert (TREE_TYPE (expr1_vptr),
   11418              :                                         gfc_class_vptr_get (tmp)));
   11419           13 :               rse.expr = gfc_class_data_get (tmp);
   11420           13 :               gfc_add_modify (&lse.pre, desc, rse.expr);
   11421              :             }
   11422              :         }
   11423              :       else
   11424              :         {
   11425              :           /* Assign to a temporary descriptor and then copy that
   11426              :              temporary to the pointer.  */
   11427          107 :           tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
   11428          107 :           lse.descriptor_only = 0;
   11429          107 :           lse.expr = tmp;
   11430          107 :           lse.direct_byref = 1;
   11431          107 :           gfc_conv_expr_descriptor (&lse, expr2);
   11432          107 :           strlen_rhs = lse.string_length;
   11433          107 :           gfc_add_modify (&lse.pre, desc, tmp);
   11434              :         }
   11435              : 
   11436         4358 :       if (expr1->ts.type == BT_CHARACTER
   11437          596 :           && expr1->ts.deferred)
   11438              :         {
   11439          338 :           gfc_symbol *psym = expr1->symtree->n.sym;
   11440          338 :           tmp = NULL_TREE;
   11441          338 :           if (psym->ts.type == BT_CHARACTER
   11442          337 :               && psym->ts.u.cl->backend_decl)
   11443          337 :             tmp = psym->ts.u.cl->backend_decl;
   11444            1 :           else if (expr1->ts.u.cl->backend_decl
   11445            1 :                    && VAR_P (expr1->ts.u.cl->backend_decl))
   11446            0 :             tmp = expr1->ts.u.cl->backend_decl;
   11447            1 :           else if (TREE_CODE (lse.expr) == COMPONENT_REF)
   11448              :             {
   11449            1 :               gfc_ref *ref = expr1->ref;
   11450            3 :               for (;ref; ref = ref->next)
   11451              :                 {
   11452            2 :                   if (ref->type == REF_COMPONENT
   11453            1 :                       && ref->u.c.component->ts.type == BT_CHARACTER
   11454            3 :                       && gfc_deferred_strlen (ref->u.c.component, &tmp))
   11455            1 :                     tmp = fold_build3_loc (input_location, COMPONENT_REF,
   11456            1 :                                            TREE_TYPE (tmp),
   11457            1 :                                            TREE_OPERAND (lse.expr, 0),
   11458              :                                            tmp, NULL_TREE);
   11459              :                 }
   11460              :             }
   11461              : 
   11462          338 :           gcc_assert (tmp);
   11463              : 
   11464          338 :           if (expr2->expr_type != EXPR_NULL)
   11465          326 :             gfc_add_modify (&block, tmp,
   11466          326 :                             fold_convert (TREE_TYPE (tmp), strlen_rhs));
   11467              :           else
   11468           12 :             gfc_add_modify (&block, tmp, build_zero_cst (TREE_TYPE (tmp)));
   11469              :         }
   11470              : 
   11471         4358 :       gfc_add_block_to_block (&block, &lse.pre);
   11472         4358 :       if (rank_remap)
   11473          254 :         gfc_add_block_to_block (&block, &rse.pre);
   11474              : 
   11475              :       /* If we do bounds remapping, update LHS descriptor accordingly.  */
   11476         4358 :       if (remap)
   11477              :         {
   11478          527 :           int dim;
   11479          527 :           gcc_assert (remap->u.ar.dimen == expr1->rank);
   11480              : 
   11481              :           /* Always set dtype.  */
   11482          527 :           tree dtype = gfc_conv_descriptor_dtype (desc);
   11483          527 :           tmp = gfc_get_dtype (TREE_TYPE (desc));
   11484          527 :           gfc_add_modify (&block, dtype, tmp);
   11485              : 
   11486              :           /* For unlimited polymorphic LHS use elem_len from RHS.  */
   11487          527 :           if (UNLIMITED_POLY (expr1) && expr2->ts.type != BT_CLASS)
   11488              :             {
   11489           60 :               tree elem_len;
   11490           60 :               tmp = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr2->ts));
   11491           60 :               elem_len = fold_convert (gfc_array_index_type, tmp);
   11492           60 :               elem_len = gfc_evaluate_now (elem_len, &block);
   11493           60 :               tmp = gfc_conv_descriptor_elem_len (desc);
   11494           60 :               gfc_add_modify (&block, tmp,
   11495           60 :                               fold_convert (TREE_TYPE (tmp), elem_len));
   11496              :             }
   11497              : 
   11498          527 :           if (rank_remap)
   11499              :             {
   11500              :               /* Do rank remapping.  We already have the RHS's descriptor
   11501              :                  converted in rse and now have to build the correct LHS
   11502              :                  descriptor for it.  */
   11503              : 
   11504          254 :               tree data, span;
   11505          254 :               tree offs, stride;
   11506          254 :               tree lbound, ubound;
   11507              : 
   11508              :               /* Copy data pointer.  */
   11509          254 :               data = gfc_conv_descriptor_data_get (rse.expr);
   11510          254 :               gfc_conv_descriptor_data_set (&block, desc, data);
   11511              : 
   11512              :               /* Copy the span.  */
   11513          254 :               if (VAR_P (rse.expr)
   11514          254 :                   && GFC_DECL_PTR_ARRAY_P (rse.expr))
   11515           12 :                 span = gfc_conv_descriptor_span_get (rse.expr);
   11516              :               else
   11517              :                 {
   11518          242 :                   tmp = TREE_TYPE (rse.expr);
   11519          242 :                   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (tmp));
   11520          242 :                   span = fold_convert (gfc_array_index_type, tmp);
   11521              :                 }
   11522          254 :               gfc_conv_descriptor_span_set (&block, desc, span);
   11523              : 
   11524              :               /* Copy offset but adjust it such that it would correspond
   11525              :                  to a lbound of zero.  */
   11526          254 :               if (expr2->rank == -1)
   11527           42 :                 gfc_conv_descriptor_offset_set (&block, desc,
   11528              :                                                 gfc_index_zero_node);
   11529              :               else
   11530              :                 {
   11531          212 :                   offs = gfc_conv_descriptor_offset_get (rse.expr);
   11532          654 :                   for (dim = 0; dim < expr2->rank; ++dim)
   11533              :                     {
   11534          230 :                       stride = gfc_conv_descriptor_stride_get (rse.expr,
   11535              :                                                         gfc_rank_cst[dim]);
   11536          230 :                       lbound = gfc_conv_descriptor_lbound_get (rse.expr,
   11537              :                                                         gfc_rank_cst[dim]);
   11538          230 :                       tmp = fold_build2_loc (input_location, MULT_EXPR,
   11539              :                                              gfc_array_index_type, stride,
   11540              :                                              lbound);
   11541          230 :                       offs = fold_build2_loc (input_location, PLUS_EXPR,
   11542              :                                               gfc_array_index_type, offs, tmp);
   11543              :                     }
   11544          212 :                   gfc_conv_descriptor_offset_set (&block, desc, offs);
   11545              :                 }
   11546              :               /* Set the bounds as declared for the LHS and calculate strides as
   11547              :                  well as another offset update accordingly.  */
   11548          254 :               stride = gfc_conv_descriptor_stride_get (rse.expr,
   11549              :                                                        gfc_rank_cst[0]);
   11550          641 :               for (dim = 0; dim < expr1->rank; ++dim)
   11551              :                 {
   11552          387 :                   gfc_se lower_se;
   11553          387 :                   gfc_se upper_se;
   11554              : 
   11555          387 :                   gcc_assert (remap->u.ar.start[dim] && remap->u.ar.end[dim]);
   11556              : 
   11557          387 :                   if (remap->u.ar.start[dim]->expr_type != EXPR_CONSTANT
   11558              :                       || remap->u.ar.start[dim]->expr_type != EXPR_VARIABLE)
   11559          387 :                     gfc_resolve_expr (remap->u.ar.start[dim]);
   11560          387 :                   if (remap->u.ar.end[dim]->expr_type != EXPR_CONSTANT
   11561              :                       || remap->u.ar.end[dim]->expr_type != EXPR_VARIABLE)
   11562          387 :                     gfc_resolve_expr (remap->u.ar.end[dim]);
   11563              : 
   11564              :                   /* Convert declared bounds.  */
   11565          387 :                   gfc_init_se (&lower_se, NULL);
   11566          387 :                   gfc_init_se (&upper_se, NULL);
   11567          387 :                   gfc_conv_expr (&lower_se, remap->u.ar.start[dim]);
   11568          387 :                   gfc_conv_expr (&upper_se, remap->u.ar.end[dim]);
   11569              : 
   11570          387 :                   gfc_add_block_to_block (&block, &lower_se.pre);
   11571          387 :                   gfc_add_block_to_block (&block, &upper_se.pre);
   11572              : 
   11573          387 :                   lbound = fold_convert (gfc_array_index_type, lower_se.expr);
   11574          387 :                   ubound = fold_convert (gfc_array_index_type, upper_se.expr);
   11575              : 
   11576          387 :                   lbound = gfc_evaluate_now (lbound, &block);
   11577          387 :                   ubound = gfc_evaluate_now (ubound, &block);
   11578              : 
   11579          387 :                   gfc_add_block_to_block (&block, &lower_se.post);
   11580          387 :                   gfc_add_block_to_block (&block, &upper_se.post);
   11581              : 
   11582              :                   /* Set bounds in descriptor.  */
   11583          387 :                   gfc_conv_descriptor_lbound_set (&block, desc,
   11584              :                                                   gfc_rank_cst[dim], lbound);
   11585          387 :                   gfc_conv_descriptor_ubound_set (&block, desc,
   11586              :                                                   gfc_rank_cst[dim], ubound);
   11587              : 
   11588              :                   /* Set stride.  */
   11589          387 :                   stride = gfc_evaluate_now (stride, &block);
   11590          387 :                   gfc_conv_descriptor_stride_set (&block, desc,
   11591              :                                                   gfc_rank_cst[dim], stride);
   11592              : 
   11593              :                   /* Update offset.  */
   11594          387 :                   offs = gfc_conv_descriptor_offset_get (desc);
   11595          387 :                   tmp = fold_build2_loc (input_location, MULT_EXPR,
   11596              :                                          gfc_array_index_type, lbound, stride);
   11597          387 :                   offs = fold_build2_loc (input_location, MINUS_EXPR,
   11598              :                                           gfc_array_index_type, offs, tmp);
   11599          387 :                   offs = gfc_evaluate_now (offs, &block);
   11600          387 :                   gfc_conv_descriptor_offset_set (&block, desc, offs);
   11601              : 
   11602              :                   /* Update stride.  */
   11603          387 :                   tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
   11604          387 :                   stride = fold_build2_loc (input_location, MULT_EXPR,
   11605              :                                             gfc_array_index_type, stride, tmp);
   11606              :                 }
   11607              :             }
   11608              :           else
   11609              :             {
   11610              :               /* Bounds remapping.  Just shift the lower bounds.  */
   11611              : 
   11612          273 :               gcc_assert (expr1->rank == expr2->rank);
   11613              : 
   11614          654 :               for (dim = 0; dim < remap->u.ar.dimen; ++dim)
   11615              :                 {
   11616          381 :                   gfc_se lbound_se;
   11617              : 
   11618          381 :                   gcc_assert (!remap->u.ar.end[dim]);
   11619          381 :                   gfc_init_se (&lbound_se, NULL);
   11620          381 :                   if (remap->u.ar.start[dim])
   11621              :                     {
   11622          225 :                       gfc_conv_expr (&lbound_se, remap->u.ar.start[dim]);
   11623          225 :                       gfc_add_block_to_block (&block, &lbound_se.pre);
   11624              :                     }
   11625              :                   else
   11626              :                     /* This remap arises from a target that is not a whole
   11627              :                        array. The start expressions will be NULL but we need
   11628              :                        the lbounds to be one.  */
   11629          156 :                     lbound_se.expr = gfc_index_one_node;
   11630          381 :                   gfc_conv_shift_descriptor_lbound (&block, desc,
   11631              :                                                     dim, lbound_se.expr);
   11632          381 :                   gfc_add_block_to_block (&block, &lbound_se.post);
   11633              :                 }
   11634              :             }
   11635              :         }
   11636              : 
   11637              :       /* If rank remapping was done, check with -fcheck=bounds that
   11638              :          the target is at least as large as the pointer.  */
   11639         4358 :       if (rank_remap && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
   11640           72 :           && expr2->rank != -1)
   11641              :         {
   11642           54 :           tree lsize, rsize;
   11643           54 :           tree fault;
   11644           54 :           const char* msg;
   11645              : 
   11646           54 :           lsize = gfc_conv_descriptor_size (lse.expr, expr1->rank);
   11647           54 :           rsize = gfc_conv_descriptor_size (rse.expr, expr2->rank);
   11648              : 
   11649           54 :           lsize = gfc_evaluate_now (lsize, &block);
   11650           54 :           rsize = gfc_evaluate_now (rsize, &block);
   11651           54 :           fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
   11652              :                                    rsize, lsize);
   11653              : 
   11654           54 :           msg = _("Target of rank remapping is too small (%ld < %ld)");
   11655           54 :           gfc_trans_runtime_check (true, false, fault, &block, &expr2->where,
   11656              :                                    msg, rsize, lsize);
   11657              :         }
   11658              : 
   11659              :       /* Check string lengths if applicable.  The check is only really added
   11660              :          to the output code if -fbounds-check is enabled.  */
   11661         4358 :       if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
   11662              :         {
   11663          530 :           gcc_assert (expr2->ts.type == BT_CHARACTER);
   11664          530 :           gcc_assert (strlen_lhs && strlen_rhs);
   11665          530 :           gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
   11666              :                                        strlen_lhs, strlen_rhs, &block);
   11667              :         }
   11668              : 
   11669         4358 :       gfc_add_block_to_block (&block, &lse.post);
   11670         4358 :       if (rank_remap)
   11671          254 :         gfc_add_block_to_block (&block, &rse.post);
   11672              :     }
   11673              : 
   11674        10150 :   return gfc_finish_block (&block);
   11675              : }
   11676              : 
   11677              : 
   11678              : /* Makes sure se is suitable for passing as a function string parameter.  */
   11679              : /* TODO: Need to check all callers of this function.  It may be abused.  */
   11680              : 
   11681              : void
   11682       242263 : gfc_conv_string_parameter (gfc_se * se)
   11683              : {
   11684       242263 :   tree type;
   11685              : 
   11686       242263 :   if (TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE
   11687       242263 :       && integer_onep (se->string_length))
   11688              :     {
   11689          691 :       se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
   11690          691 :       return;
   11691              :     }
   11692              : 
   11693       241572 :   if (TREE_CODE (se->expr) == STRING_CST)
   11694              :     {
   11695       100494 :       type = TREE_TYPE (TREE_TYPE (se->expr));
   11696       100494 :       se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
   11697       100494 :       return;
   11698              :     }
   11699              : 
   11700       141078 :   if (TREE_CODE (se->expr) == COND_EXPR)
   11701              :     {
   11702          482 :       tree cond = TREE_OPERAND (se->expr, 0);
   11703          482 :       tree lhs = TREE_OPERAND (se->expr, 1);
   11704          482 :       tree rhs = TREE_OPERAND (se->expr, 2);
   11705              : 
   11706          482 :       gfc_se lse, rse;
   11707          482 :       gfc_init_se (&lse, NULL);
   11708          482 :       gfc_init_se (&rse, NULL);
   11709              : 
   11710          482 :       lse.expr = lhs;
   11711          482 :       lse.string_length = se->string_length;
   11712          482 :       gfc_conv_string_parameter (&lse);
   11713              : 
   11714          482 :       rse.expr = rhs;
   11715          482 :       rse.string_length = se->string_length;
   11716          482 :       gfc_conv_string_parameter (&rse);
   11717              : 
   11718          482 :       se->expr
   11719          482 :         = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (lse.expr),
   11720              :                            cond, lse.expr, rse.expr);
   11721              :     }
   11722              : 
   11723       141078 :   if ((TREE_CODE (TREE_TYPE (se->expr)) == ARRAY_TYPE
   11724        55251 :        || TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE)
   11725       141174 :       && TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
   11726              :     {
   11727        85923 :       type = TREE_TYPE (se->expr);
   11728        85923 :       if (TREE_CODE (se->expr) != INDIRECT_REF)
   11729        80872 :         se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
   11730              :       else
   11731              :         {
   11732         5051 :           if (TREE_CODE (type) == ARRAY_TYPE)
   11733         5051 :             type = TREE_TYPE (type);
   11734         5051 :           type = gfc_get_character_type_len_for_eltype (type,
   11735              :                                                         se->string_length);
   11736         5051 :           type = build_pointer_type (type);
   11737         5051 :           se->expr = gfc_build_addr_expr (type, se->expr);
   11738              :         }
   11739              :     }
   11740              : 
   11741       141078 :   gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
   11742              : }
   11743              : 
   11744              : 
   11745              : /* Generate code for assignment of scalar variables.  Includes character
   11746              :    strings and derived types with allocatable components.
   11747              :    If you know that the LHS has no allocations, set dealloc to false.
   11748              : 
   11749              :    DEEP_COPY has no effect if the typespec TS is not a derived type with
   11750              :    allocatable components.  Otherwise, if it is set, an explicit copy of each
   11751              :    allocatable component is made.  This is necessary as a simple copy of the
   11752              :    whole object would copy array descriptors as is, so that the lhs's
   11753              :    allocatable components would point to the rhs's after the assignment.
   11754              :    Typically, setting DEEP_COPY is necessary if the rhs is a variable, and not
   11755              :    necessary if the rhs is a non-pointer function, as the allocatable components
   11756              :    are not accessible by other means than the function's result after the
   11757              :    function has returned.  It is even more subtle when temporaries are involved,
   11758              :    as the two following examples show:
   11759              :     1.  When we evaluate an array constructor, a temporary is created.  Thus
   11760              :       there is theoretically no alias possible.  However, no deep copy is
   11761              :       made for this temporary, so that if the constructor is made of one or
   11762              :       more variable with allocatable components, those components still point
   11763              :       to the variable's: DEEP_COPY should be set for the assignment from the
   11764              :       temporary to the lhs in that case.
   11765              :     2.  When assigning a scalar to an array, we evaluate the scalar value out
   11766              :       of the loop, store it into a temporary variable, and assign from that.
   11767              :       In that case, deep copying when assigning to the temporary would be a
   11768              :       waste of resources; however deep copies should happen when assigning from
   11769              :       the temporary to each array element: again DEEP_COPY should be set for
   11770              :       the assignment from the temporary to the lhs.  */
   11771              : 
   11772              : tree
   11773       335847 : gfc_trans_scalar_assign (gfc_se *lse, gfc_se *rse, gfc_typespec ts,
   11774              :                          bool deep_copy, bool dealloc, bool in_coarray,
   11775              :                          bool assoc_assign)
   11776              : {
   11777       335847 :   stmtblock_t block;
   11778       335847 :   tree tmp;
   11779       335847 :   tree cond;
   11780       335847 :   int caf_mode;
   11781              : 
   11782       335847 :   gfc_init_block (&block);
   11783              : 
   11784       335847 :   if (ts.type == BT_CHARACTER)
   11785              :     {
   11786        33144 :       tree rlen = NULL;
   11787        33144 :       tree llen = NULL;
   11788              : 
   11789        33144 :       if (lse->string_length != NULL_TREE)
   11790              :         {
   11791        33144 :           gfc_conv_string_parameter (lse);
   11792        33144 :           gfc_add_block_to_block (&block, &lse->pre);
   11793        33144 :           llen = lse->string_length;
   11794              :         }
   11795              : 
   11796        33144 :       if (rse->string_length != NULL_TREE)
   11797              :         {
   11798        33144 :           gfc_conv_string_parameter (rse);
   11799        33144 :           gfc_add_block_to_block (&block, &rse->pre);
   11800        33144 :           rlen = rse->string_length;
   11801              :         }
   11802              : 
   11803        33144 :       gfc_trans_string_copy (&block, llen, lse->expr, ts.kind, rlen,
   11804              :                              rse->expr, ts.kind);
   11805              :     }
   11806       283933 :   else if (gfc_bt_struct (ts.type)
   11807       302703 :            && (ts.u.derived->attr.alloc_comp
   11808        12332 :                || (deep_copy && has_parameterized_comps (ts.u.derived))))
   11809              :     {
   11810         6582 :       tree tmp_var = NULL_TREE;
   11811         6582 :       cond = NULL_TREE;
   11812              : 
   11813              :       /* Are the rhs and the lhs the same?  */
   11814         6582 :       if (deep_copy)
   11815              :         {
   11816         3953 :           if (!TREE_CONSTANT (rse->expr) && !VAR_P (rse->expr))
   11817         2837 :             rse->expr = gfc_evaluate_now (rse->expr, &rse->pre);
   11818         3953 :           cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   11819              :                                   gfc_build_addr_expr (NULL_TREE, lse->expr),
   11820              :                                   gfc_build_addr_expr (NULL_TREE, rse->expr));
   11821         3953 :           cond = gfc_evaluate_now (cond, &lse->pre);
   11822              :         }
   11823              : 
   11824              :       /* Deallocate the lhs allocated components as long as it is not
   11825              :          the same as the rhs.  This must be done following the assignment
   11826              :          to prevent deallocating data that could be used in the rhs
   11827              :          expression.  */
   11828         6582 :       if (dealloc)
   11829              :         {
   11830         1858 :           tmp_var = gfc_evaluate_now (lse->expr, &lse->pre);
   11831         1858 :           tmp = gfc_deallocate_alloc_comp_no_caf (ts.u.derived, tmp_var,
   11832         1858 :                                                   0, gfc_may_be_finalized (ts));
   11833         1858 :           if (deep_copy)
   11834          779 :             tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
   11835              :                             tmp);
   11836         1858 :           gfc_add_expr_to_block (&lse->post, tmp);
   11837              :         }
   11838              : 
   11839         6582 :       gfc_add_block_to_block (&block, &rse->pre);
   11840              : 
   11841              :       /* Skip finalization for self-assignment.  */
   11842         6582 :       if (deep_copy && lse->finalblock.head)
   11843              :         {
   11844           24 :           tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
   11845              :                           gfc_finish_block (&lse->finalblock));
   11846           24 :           gfc_add_expr_to_block (&block, tmp);
   11847              :         }
   11848              :       else
   11849         6558 :         gfc_add_block_to_block (&block, &lse->finalblock);
   11850              : 
   11851         6582 :       gfc_add_block_to_block (&block, &lse->pre);
   11852              : 
   11853         6582 :       gfc_add_modify (&block, lse->expr,
   11854         6582 :                            fold_convert (TREE_TYPE (lse->expr), rse->expr));
   11855              : 
   11856              :       /* Restore pointer address of coarray components.  */
   11857         6582 :       if (ts.u.derived->attr.coarray_comp && deep_copy && tmp_var != NULL_TREE)
   11858              :         {
   11859            5 :           tmp = gfc_reassign_alloc_comp_caf (ts.u.derived, tmp_var, lse->expr);
   11860            5 :           tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
   11861              :                           tmp);
   11862            5 :           gfc_add_expr_to_block (&block, tmp);
   11863              :         }
   11864              : 
   11865              :       /* Do a deep copy if the rhs is a variable, if it is not the
   11866              :          same as the lhs.  */
   11867         6582 :       if (deep_copy)
   11868              :         {
   11869         3953 :           caf_mode = in_coarray ? (GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
   11870              :                                        | GFC_STRUCTURE_CAF_MODE_IN_COARRAY) : 0;
   11871         3953 :           tmp = gfc_copy_alloc_comp (ts.u.derived, rse->expr, lse->expr, 0,
   11872              :                                      caf_mode);
   11873         3953 :           tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
   11874              :                           tmp);
   11875         3953 :           gfc_add_expr_to_block (&block, tmp);
   11876              :         }
   11877              :     }
   11878       296121 :   else if (gfc_bt_struct (ts.type))
   11879              :     {
   11880        12188 :       gfc_add_block_to_block (&block, &rse->pre);
   11881        12188 :       gfc_add_block_to_block (&block, &lse->finalblock);
   11882        12188 :       gfc_add_block_to_block (&block, &lse->pre);
   11883        12188 :       tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
   11884        12188 :                              TREE_TYPE (lse->expr), rse->expr);
   11885        12188 :       gfc_add_modify (&block, lse->expr, tmp);
   11886              :     }
   11887              :   /* If possible use the rhs vptr copy with trans_scalar_class_assign....  */
   11888       283933 :   else if (ts.type == BT_CLASS)
   11889              :     {
   11890          788 :       gfc_add_block_to_block (&block, &lse->pre);
   11891          788 :       gfc_add_block_to_block (&block, &rse->pre);
   11892          788 :       gfc_add_block_to_block (&block, &lse->finalblock);
   11893              : 
   11894          788 :       if (!trans_scalar_class_assign (&block, lse, rse))
   11895              :         {
   11896              :           /* ..otherwise assignment suffices. Note the use of VIEW_CONVERT_EXPR
   11897              :           for the lhs which ensures that class data rhs cast as a string
   11898              :           assigns correctly.  */
   11899          642 :           tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
   11900          642 :                                  TREE_TYPE (rse->expr), lse->expr);
   11901          642 :           gfc_add_modify (&block, tmp, rse->expr);
   11902              : 
   11903              :           /* Copy allocatable components but guard against class pointer
   11904              :              assign, which arrives here.  */
   11905              : #define DATA_DT ts.u.derived->components->ts.u.derived
   11906          642 :           if (deep_copy
   11907          195 :               && !(GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
   11908           43 :                    && GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
   11909          152 :               && ts.u.derived->components
   11910          794 :               && DATA_DT && DATA_DT->attr.alloc_comp)
   11911              :             {
   11912            6 :               caf_mode = in_coarray ? (GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
   11913              :                                        | GFC_STRUCTURE_CAF_MODE_IN_COARRAY)
   11914              :                                     : 0;
   11915            6 :               tmp = gfc_copy_alloc_comp (DATA_DT, rse->expr, lse->expr, 0,
   11916              :                                          caf_mode);
   11917            6 :               gfc_add_expr_to_block (&block, tmp);
   11918              :             }
   11919              : #undef DATA_DT
   11920              :         }
   11921              :     }
   11922       283145 :   else if (ts.type != BT_CLASS)
   11923              :     {
   11924       283145 :       gfc_add_block_to_block (&block, &lse->pre);
   11925       283145 :       gfc_add_block_to_block (&block, &rse->pre);
   11926              : 
   11927       283145 :       if (in_coarray)
   11928              :         {
   11929          833 :           if (flag_coarray == GFC_FCOARRAY_LIB && assoc_assign)
   11930              :             {
   11931            0 :               gfc_add_modify (&block, gfc_conv_descriptor_token (lse->expr),
   11932            0 :                               TYPE_LANG_SPECIFIC (
   11933              :                                 TREE_TYPE (TREE_TYPE (rse->expr)))
   11934              :                                 ->caf_token);
   11935              :             }
   11936          833 :           if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (lse->expr)))
   11937            0 :             lse->expr = gfc_conv_array_data (lse->expr);
   11938          273 :           if (flag_coarray == GFC_FCOARRAY_SINGLE && assoc_assign
   11939          833 :               && !POINTER_TYPE_P (TREE_TYPE (rse->expr)))
   11940            0 :             rse->expr = gfc_build_addr_expr (NULL_TREE, rse->expr);
   11941              :         }
   11942       283145 :       gfc_add_modify (&block, lse->expr,
   11943       283145 :                       fold_convert (TREE_TYPE (lse->expr), rse->expr));
   11944              :     }
   11945              : 
   11946       335847 :   gfc_add_block_to_block (&block, &lse->post);
   11947       335847 :   gfc_add_block_to_block (&block, &rse->post);
   11948              : 
   11949       335847 :   return gfc_finish_block (&block);
   11950              : }
   11951              : 
   11952              : 
   11953              : /* There are quite a lot of restrictions on the optimisation in using an
   11954              :    array function assign without a temporary.  */
   11955              : 
   11956              : static bool
   11957        14423 : arrayfunc_assign_needs_temporary (gfc_expr * expr1, gfc_expr * expr2)
   11958              : {
   11959        14423 :   gfc_ref * ref;
   11960        14423 :   bool seen_array_ref;
   11961        14423 :   bool c = false;
   11962        14423 :   gfc_symbol *sym = expr1->symtree->n.sym;
   11963              : 
   11964              :   /* Play it safe with class functions assigned to a derived type.  */
   11965        14423 :   if (gfc_is_class_array_function (expr2)
   11966        14423 :       && expr1->ts.type == BT_DERIVED)
   11967              :     return true;
   11968              : 
   11969              :   /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION.  */
   11970        14399 :   if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
   11971              :     return true;
   11972              : 
   11973              :   /* Elemental functions are scalarized so that they don't need a
   11974              :      temporary in gfc_trans_assignment_1, so return a true.  Otherwise,
   11975              :      they would need special treatment in gfc_trans_arrayfunc_assign.  */
   11976         8506 :   if (expr2->value.function.esym != NULL
   11977         1577 :       && expr2->value.function.esym->attr.elemental)
   11978              :     return true;
   11979              : 
   11980              :   /* Need a temporary if rhs is not FULL or a contiguous section.  */
   11981         8147 :   if (expr1->ref && !(gfc_full_array_ref_p (expr1->ref, &c) || c))
   11982              :     return true;
   11983              : 
   11984              :   /* Need a temporary if EXPR1 can't be expressed as a descriptor.  */
   11985         7903 :   if (gfc_ref_needs_temporary_p (expr1->ref))
   11986              :     return true;
   11987              : 
   11988              :   /* Functions returning pointers or allocatables need temporaries.  */
   11989         7891 :   if (gfc_expr_attr (expr2).pointer
   11990         7891 :       || gfc_expr_attr (expr2).allocatable)
   11991          370 :     return true;
   11992              : 
   11993              :   /* Character array functions need temporaries unless the
   11994              :      character lengths are the same.  */
   11995         7521 :   if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
   11996              :     {
   11997          562 :       if (UNLIMITED_POLY (expr1))
   11998              :         return true;
   11999              : 
   12000          556 :       if (expr1->ts.u.cl->length == NULL
   12001          507 :             || expr1->ts.u.cl->length->expr_type != EXPR_CONSTANT)
   12002              :         return true;
   12003              : 
   12004          493 :       if (expr2->ts.u.cl->length == NULL
   12005          487 :             || expr2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
   12006              :         return true;
   12007              : 
   12008          475 :       if (mpz_cmp (expr1->ts.u.cl->length->value.integer,
   12009          475 :                      expr2->ts.u.cl->length->value.integer) != 0)
   12010              :         return true;
   12011              :     }
   12012              : 
   12013              :   /* Check that no LHS component references appear during an array
   12014              :      reference. This is needed because we do not have the means to
   12015              :      span any arbitrary stride with an array descriptor. This check
   12016              :      is not needed for the rhs because the function result has to be
   12017              :      a complete type.  */
   12018         7428 :   seen_array_ref = false;
   12019        14856 :   for (ref = expr1->ref; ref; ref = ref->next)
   12020              :     {
   12021         7441 :       if (ref->type == REF_ARRAY)
   12022              :         seen_array_ref= true;
   12023           13 :       else if (ref->type == REF_COMPONENT && seen_array_ref)
   12024              :         return true;
   12025              :     }
   12026              : 
   12027              :   /* Check for a dependency.  */
   12028         7415 :   if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
   12029              :                                    expr2->value.function.esym,
   12030              :                                    expr2->value.function.actual,
   12031              :                                    NOT_ELEMENTAL))
   12032              :     return true;
   12033              : 
   12034              :   /* If we have reached here with an intrinsic function, we do not
   12035              :      need a temporary except in the particular case that reallocation
   12036              :      on assignment is active and the lhs is allocatable and a target,
   12037              :      or a pointer which may be a subref pointer.  FIXME: The last
   12038              :      condition can go away when we use span in the intrinsics
   12039              :      directly.*/
   12040         6978 :   if (expr2->value.function.isym)
   12041         6100 :     return (flag_realloc_lhs && sym->attr.allocatable && sym->attr.target)
   12042        12287 :       || (sym->attr.pointer && sym->attr.subref_array_pointer);
   12043              : 
   12044              :   /* If the LHS is a dummy, we need a temporary if it is not
   12045              :      INTENT(OUT).  */
   12046          803 :   if (sym->attr.dummy && sym->attr.intent != INTENT_OUT)
   12047              :     return true;
   12048              : 
   12049              :   /* If the lhs has been host_associated, is in common, a pointer or is
   12050              :      a target and the function is not using a RESULT variable, aliasing
   12051              :      can occur and a temporary is needed.  */
   12052          797 :   if ((sym->attr.host_assoc
   12053          743 :            || sym->attr.in_common
   12054          737 :            || sym->attr.pointer
   12055          731 :            || sym->attr.cray_pointee
   12056          731 :            || sym->attr.target)
   12057           66 :         && expr2->symtree != NULL
   12058           66 :         && expr2->symtree->n.sym == expr2->symtree->n.sym->result)
   12059              :     return true;
   12060              : 
   12061              :   /* A PURE function can unconditionally be called without a temporary.  */
   12062          755 :   if (expr2->value.function.esym != NULL
   12063          730 :       && expr2->value.function.esym->attr.pure)
   12064              :     return false;
   12065              : 
   12066              :   /* Implicit_pure functions are those which could legally be declared
   12067              :      to be PURE.  */
   12068          727 :   if (expr2->value.function.esym != NULL
   12069          702 :       && expr2->value.function.esym->attr.implicit_pure)
   12070              :     return false;
   12071              : 
   12072          444 :   if (!sym->attr.use_assoc
   12073          444 :         && !sym->attr.in_common
   12074          444 :         && !sym->attr.pointer
   12075          438 :         && !sym->attr.target
   12076          438 :         && !sym->attr.cray_pointee
   12077          438 :         && expr2->value.function.esym)
   12078              :     {
   12079              :       /* A temporary is not needed if the function is not contained and
   12080              :          the variable is local or host associated and not a pointer or
   12081              :          a target.  */
   12082          413 :       if (!expr2->value.function.esym->attr.contained)
   12083              :         return false;
   12084              : 
   12085              :       /* A temporary is not needed if the lhs has never been host
   12086              :          associated and the procedure is contained.  */
   12087          164 :       else if (!sym->attr.host_assoc)
   12088              :         return false;
   12089              : 
   12090              :       /* A temporary is not needed if the variable is local and not
   12091              :          a pointer, a target or a result.  */
   12092            6 :       if (sym->ns->parent
   12093            0 :             && expr2->value.function.esym->ns == sym->ns->parent)
   12094              :         return false;
   12095              :     }
   12096              : 
   12097              :   /* Default to temporary use.  */
   12098              :   return true;
   12099              : }
   12100              : 
   12101              : 
   12102              : /* Provide the loop info so that the lhs descriptor can be built for
   12103              :    reallocatable assignments from extrinsic function calls.  */
   12104              : 
   12105              : static void
   12106          203 : realloc_lhs_loop_for_fcn_call (gfc_se *se, locus *where, gfc_ss **ss,
   12107              :                                gfc_loopinfo *loop)
   12108              : {
   12109              :   /* Signal that the function call should not be made by
   12110              :      gfc_conv_loop_setup.  */
   12111          203 :   se->ss->is_alloc_lhs = 1;
   12112          203 :   gfc_init_loopinfo (loop);
   12113          203 :   gfc_add_ss_to_loop (loop, *ss);
   12114          203 :   gfc_add_ss_to_loop (loop, se->ss);
   12115          203 :   gfc_conv_ss_startstride (loop);
   12116          203 :   gfc_conv_loop_setup (loop, where);
   12117          203 :   gfc_copy_loopinfo_to_se (se, loop);
   12118          203 :   gfc_add_block_to_block (&se->pre, &loop->pre);
   12119          203 :   gfc_add_block_to_block (&se->pre, &loop->post);
   12120          203 :   se->ss->is_alloc_lhs = 0;
   12121          203 : }
   12122              : 
   12123              : 
   12124              : /* For assignment to a reallocatable lhs from intrinsic functions,
   12125              :    replace the se.expr (ie. the result) with a temporary descriptor.
   12126              :    Null the data field so that the library allocates space for the
   12127              :    result. Free the data of the original descriptor after the function,
   12128              :    in case it appears in an argument expression and transfer the
   12129              :    result to the original descriptor.  */
   12130              : 
   12131              : static void
   12132         2126 : fcncall_realloc_result (gfc_se *se, int rank, tree dtype)
   12133              : {
   12134         2126 :   tree desc;
   12135         2126 :   tree res_desc;
   12136         2126 :   tree tmp;
   12137         2126 :   tree offset;
   12138         2126 :   tree zero_cond;
   12139         2126 :   tree not_same_shape;
   12140         2126 :   stmtblock_t shape_block;
   12141         2126 :   int n;
   12142              : 
   12143              :   /* Use the allocation done by the library.  Substitute the lhs
   12144              :      descriptor with a copy, whose data field is nulled.*/
   12145         2126 :   desc = build_fold_indirect_ref_loc (input_location, se->expr);
   12146         2126 :   if (POINTER_TYPE_P (TREE_TYPE (desc)))
   12147            9 :     desc = build_fold_indirect_ref_loc (input_location, desc);
   12148              : 
   12149              :   /* Unallocated, the descriptor does not have a dtype.  */
   12150         2126 :   tmp = gfc_conv_descriptor_dtype (desc);
   12151         2126 :   if (dtype != NULL_TREE)
   12152           13 :     gfc_add_modify (&se->pre, tmp, dtype);
   12153              :   else
   12154         2113 :     gfc_add_modify (&se->pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
   12155              : 
   12156         2126 :   res_desc = gfc_evaluate_now (desc, &se->pre);
   12157         2126 :   gfc_conv_descriptor_data_set (&se->pre, res_desc, null_pointer_node);
   12158         2126 :   se->expr = gfc_build_addr_expr (NULL_TREE, res_desc);
   12159              : 
   12160              :   /* Free the lhs after the function call and copy the result data to
   12161              :      the lhs descriptor.  */
   12162         2126 :   tmp = gfc_conv_descriptor_data_get (desc);
   12163         2126 :   zero_cond = fold_build2_loc (input_location, EQ_EXPR,
   12164              :                                logical_type_node, tmp,
   12165         2126 :                                build_int_cst (TREE_TYPE (tmp), 0));
   12166         2126 :   zero_cond = gfc_evaluate_now (zero_cond, &se->post);
   12167         2126 :   tmp = gfc_call_free (tmp);
   12168         2126 :   gfc_add_expr_to_block (&se->post, tmp);
   12169              : 
   12170         2126 :   tmp = gfc_conv_descriptor_data_get (res_desc);
   12171         2126 :   gfc_conv_descriptor_data_set (&se->post, desc, tmp);
   12172              : 
   12173              :   /* Check that the shapes are the same between lhs and expression.
   12174              :      The evaluation of the shape is done in 'shape_block' to avoid
   12175              :      unitialized warnings from the lhs bounds. */
   12176         2126 :   not_same_shape = boolean_false_node;
   12177         2126 :   gfc_start_block (&shape_block);
   12178         6844 :   for (n = 0 ; n < rank; n++)
   12179              :     {
   12180         4718 :       tree tmp1;
   12181         4718 :       tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
   12182         4718 :       tmp1 = gfc_conv_descriptor_lbound_get (res_desc, gfc_rank_cst[n]);
   12183         4718 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
   12184              :                              gfc_array_index_type, tmp, tmp1);
   12185         4718 :       tmp1 = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
   12186         4718 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
   12187              :                              gfc_array_index_type, tmp, tmp1);
   12188         4718 :       tmp1 = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
   12189         4718 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
   12190              :                              gfc_array_index_type, tmp, tmp1);
   12191         4718 :       tmp = fold_build2_loc (input_location, NE_EXPR,
   12192              :                              logical_type_node, tmp,
   12193              :                              gfc_index_zero_node);
   12194         4718 :       tmp = gfc_evaluate_now (tmp, &shape_block);
   12195         4718 :       if (n == 0)
   12196              :         not_same_shape = tmp;
   12197              :       else
   12198         2592 :         not_same_shape = fold_build2_loc (input_location, TRUTH_OR_EXPR,
   12199              :                                           logical_type_node, tmp,
   12200              :                                           not_same_shape);
   12201              :     }
   12202              : 
   12203              :   /* 'zero_cond' being true is equal to lhs not being allocated or the
   12204              :      shapes being different.  */
   12205         2126 :   tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR, logical_type_node,
   12206              :                          zero_cond, not_same_shape);
   12207         2126 :   gfc_add_modify (&shape_block, zero_cond, tmp);
   12208         2126 :   tmp = gfc_finish_block (&shape_block);
   12209         2126 :   tmp = build3_v (COND_EXPR, zero_cond,
   12210              :                   build_empty_stmt (input_location), tmp);
   12211         2126 :   gfc_add_expr_to_block (&se->post, tmp);
   12212              : 
   12213              :   /* Now reset the bounds returned from the function call to bounds based
   12214              :      on the lhs lbounds, except where the lhs is not allocated or the shapes
   12215              :      of 'variable and 'expr' are different. Set the offset accordingly.  */
   12216         2126 :   offset = gfc_index_zero_node;
   12217         6844 :   for (n = 0 ; n < rank; n++)
   12218              :     {
   12219         4718 :       tree lbound;
   12220              : 
   12221         4718 :       lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
   12222         4718 :       lbound = fold_build3_loc (input_location, COND_EXPR,
   12223              :                                 gfc_array_index_type, zero_cond,
   12224              :                                 gfc_index_one_node, lbound);
   12225         4718 :       lbound = gfc_evaluate_now (lbound, &se->post);
   12226              : 
   12227         4718 :       tmp = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
   12228         4718 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
   12229              :                              gfc_array_index_type, tmp, lbound);
   12230         4718 :       gfc_conv_descriptor_lbound_set (&se->post, desc,
   12231              :                                       gfc_rank_cst[n], lbound);
   12232         4718 :       gfc_conv_descriptor_ubound_set (&se->post, desc,
   12233              :                                       gfc_rank_cst[n], tmp);
   12234              : 
   12235              :       /* Set stride and accumulate the offset.  */
   12236         4718 :       tmp = gfc_conv_descriptor_stride_get (res_desc, gfc_rank_cst[n]);
   12237         4718 :       gfc_conv_descriptor_stride_set (&se->post, desc,
   12238              :                                       gfc_rank_cst[n], tmp);
   12239         4718 :       tmp = fold_build2_loc (input_location, MULT_EXPR,
   12240              :                              gfc_array_index_type, lbound, tmp);
   12241         4718 :       offset = fold_build2_loc (input_location, MINUS_EXPR,
   12242              :                                 gfc_array_index_type, offset, tmp);
   12243         4718 :       offset = gfc_evaluate_now (offset, &se->post);
   12244              :     }
   12245              : 
   12246         2126 :   gfc_conv_descriptor_offset_set (&se->post, desc, offset);
   12247         2126 : }
   12248              : 
   12249              : 
   12250              : 
   12251              : /* Try to translate array(:) = func (...), where func is a transformational
   12252              :    array function, without using a temporary.  Returns NULL if this isn't the
   12253              :    case.  */
   12254              : 
   12255              : static tree
   12256        14463 : gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
   12257              : {
   12258        14463 :   gfc_se se;
   12259        14463 :   gfc_ss *ss = NULL;
   12260        14463 :   gfc_component *comp = NULL;
   12261        14463 :   gfc_loopinfo loop;
   12262        14463 :   tree tmp;
   12263        14463 :   tree lhs;
   12264        14463 :   gfc_se final_se;
   12265        14463 :   gfc_symbol *sym = expr1->symtree->n.sym;
   12266        14463 :   bool finalizable =  gfc_may_be_finalized (expr1->ts);
   12267              : 
   12268              :   /* If the symbol is host associated and has not been referenced in its name
   12269              :      space, it might be lacking a backend_decl and vtable.  */
   12270        14463 :   if (sym->backend_decl == NULL_TREE)
   12271              :     return NULL_TREE;
   12272              : 
   12273        14423 :   if (arrayfunc_assign_needs_temporary (expr1, expr2))
   12274              :     return NULL_TREE;
   12275              : 
   12276              :   /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
   12277              :      functions.  */
   12278         6860 :   comp = gfc_get_proc_ptr_comp (expr2);
   12279              : 
   12280         6860 :   if (!(expr2->value.function.isym
   12281          718 :               || (comp && comp->attr.dimension)
   12282          718 :               || (!comp && gfc_return_by_reference (expr2->value.function.esym)
   12283          718 :                   && expr2->value.function.esym->result->attr.dimension)))
   12284            0 :     return NULL_TREE;
   12285              : 
   12286         6860 :   gfc_init_se (&se, NULL);
   12287         6860 :   gfc_start_block (&se.pre);
   12288         6860 :   se.want_pointer = 1;
   12289              : 
   12290              :   /* First the lhs must be finalized, if necessary. We use a copy of the symbol
   12291              :      backend decl, stash the original away for the finalization so that the
   12292              :      value used is that before the assignment. This is necessary because
   12293              :      evaluation of the rhs expression using direct by reference can change
   12294              :      the value. However, the standard mandates that the finalization must occur
   12295              :      after evaluation of the rhs.  */
   12296         6860 :   gfc_init_se (&final_se, NULL);
   12297              : 
   12298         6860 :   if (finalizable)
   12299              :     {
   12300           45 :       tmp = sym->backend_decl;
   12301           45 :       lhs = sym->backend_decl;
   12302           45 :       if (INDIRECT_REF_P (tmp))
   12303            0 :         tmp = TREE_OPERAND (tmp, 0);
   12304           45 :       sym->backend_decl = gfc_create_var (TREE_TYPE (tmp), "lhs");
   12305           45 :       gfc_add_modify (&se.pre, sym->backend_decl, tmp);
   12306           45 :       if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
   12307              :         {
   12308            0 :           tmp = gfc_copy_alloc_comp (expr1->ts.u.derived, tmp, sym->backend_decl,
   12309              :                                      expr1->rank, 0);
   12310            0 :           gfc_add_expr_to_block (&final_se.pre, tmp);
   12311              :         }
   12312              :     }
   12313              : 
   12314           45 :   if (finalizable && gfc_assignment_finalizer_call (&final_se, expr1, false))
   12315              :     {
   12316           45 :       gfc_add_block_to_block (&se.pre, &final_se.pre);
   12317           45 :       gfc_add_block_to_block (&se.post, &final_se.finalblock);
   12318              :     }
   12319              : 
   12320         6860 :   if (finalizable)
   12321           45 :     sym->backend_decl = lhs;
   12322              : 
   12323         6860 :   gfc_conv_array_parameter (&se, expr1, false, NULL, NULL, NULL);
   12324              : 
   12325         6860 :   if (expr1->ts.type == BT_DERIVED
   12326          252 :         && expr1->ts.u.derived->attr.alloc_comp)
   12327              :     {
   12328           98 :       tmp = build_fold_indirect_ref_loc (input_location, se.expr);
   12329           98 :       tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, tmp,
   12330              :                                               expr1->rank);
   12331           98 :       gfc_add_expr_to_block (&se.pre, tmp);
   12332              :     }
   12333              : 
   12334         6860 :   se.direct_byref = 1;
   12335         6860 :   se.ss = gfc_walk_expr (expr2);
   12336         6860 :   gcc_assert (se.ss != gfc_ss_terminator);
   12337              : 
   12338              :   /* Since this is a direct by reference call, references to the lhs can be
   12339              :      used for finalization of the function result just as long as the blocks
   12340              :      from final_se are added at the right time.  */
   12341         6860 :   gfc_init_se (&final_se, NULL);
   12342         6860 :   if (finalizable && expr2->value.function.esym)
   12343              :     {
   12344           32 :       final_se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
   12345           32 :       gfc_finalize_tree_expr (&final_se, expr2->ts.u.derived,
   12346           32 :                                     expr2->value.function.esym->attr,
   12347              :                                     expr2->rank);
   12348              :     }
   12349              : 
   12350              :   /* Reallocate on assignment needs the loopinfo for extrinsic functions.
   12351              :      This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
   12352              :      Clearly, this cannot be done for an allocatable function result, since
   12353              :      the shape of the result is unknown and, in any case, the function must
   12354              :      correctly take care of the reallocation internally. For intrinsic
   12355              :      calls, the array data is freed and the library takes care of allocation.
   12356              :      TODO: Add logic of trans-array.cc: gfc_alloc_allocatable_for_assignment
   12357              :      to the library.  */
   12358         6860 :   if (flag_realloc_lhs
   12359         6785 :         && gfc_is_reallocatable_lhs (expr1)
   12360         9189 :         && !gfc_expr_attr (expr1).codimension
   12361         2329 :         && !gfc_is_coindexed (expr1)
   12362         9189 :         && !(expr2->value.function.esym
   12363          203 :             && expr2->value.function.esym->result->attr.allocatable))
   12364              :     {
   12365         2329 :       realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
   12366              : 
   12367         2329 :       if (!expr2->value.function.isym)
   12368              :         {
   12369          203 :           ss = gfc_walk_expr (expr1);
   12370          203 :           gcc_assert (ss != gfc_ss_terminator);
   12371              : 
   12372          203 :           realloc_lhs_loop_for_fcn_call (&se, &expr1->where, &ss, &loop);
   12373          203 :           ss->is_alloc_lhs = 1;
   12374              :         }
   12375              :       else
   12376              :         {
   12377         2126 :           tree dtype = NULL_TREE;
   12378         2126 :           tree type = gfc_typenode_for_spec (&expr2->ts);
   12379         2126 :           if (expr1->ts.type == BT_CLASS)
   12380              :             {
   12381           13 :               tmp = gfc_class_vptr_get (sym->backend_decl);
   12382           13 :               tree tmp2 = gfc_get_symbol_decl (gfc_find_vtab (&expr2->ts));
   12383           13 :               tmp2 = gfc_build_addr_expr (TREE_TYPE (tmp), tmp2);
   12384           13 :               gfc_add_modify (&se.pre, tmp, tmp2);
   12385           13 :               dtype = gfc_get_dtype_rank_type (expr1->rank,type);
   12386              :             }
   12387         2126 :           fcncall_realloc_result (&se, expr1->rank, dtype);
   12388              :         }
   12389              :     }
   12390              : 
   12391         6860 :   gfc_conv_function_expr (&se, expr2);
   12392              : 
   12393              :   /* Fix the result.  */
   12394         6860 :   gfc_add_block_to_block (&se.pre, &se.post);
   12395         6860 :   if (finalizable)
   12396           45 :     gfc_add_block_to_block (&se.pre, &final_se.pre);
   12397              : 
   12398              :   /* Do the finalization, including final calls from function arguments.  */
   12399           45 :   if (finalizable)
   12400              :     {
   12401           45 :       gfc_add_block_to_block (&se.pre, &final_se.post);
   12402           45 :       gfc_add_block_to_block (&se.pre, &se.finalblock);
   12403           45 :       gfc_add_block_to_block (&se.pre, &final_se.finalblock);
   12404              :    }
   12405              : 
   12406         6860 :   if (ss)
   12407          203 :     gfc_cleanup_loop (&loop);
   12408              :   else
   12409         6657 :     gfc_free_ss_chain (se.ss);
   12410              : 
   12411         6860 :   return gfc_finish_block (&se.pre);
   12412              : }
   12413              : 
   12414              : 
   12415              : /* Try to efficiently translate array(:) = 0.  Return NULL if this
   12416              :    can't be done.  */
   12417              : 
   12418              : static tree
   12419         3942 : gfc_trans_zero_assign (gfc_expr * expr)
   12420              : {
   12421         3942 :   tree dest, len, type;
   12422         3942 :   tree tmp;
   12423         3942 :   gfc_symbol *sym;
   12424              : 
   12425         3942 :   sym = expr->symtree->n.sym;
   12426         3942 :   dest = gfc_get_symbol_decl (sym);
   12427              : 
   12428         3942 :   type = TREE_TYPE (dest);
   12429         3942 :   if (POINTER_TYPE_P (type))
   12430          248 :     type = TREE_TYPE (type);
   12431         3942 :   if (GFC_ARRAY_TYPE_P (type))
   12432              :     {
   12433              :       /* Determine the length of the array.  */
   12434         2765 :       len = GFC_TYPE_ARRAY_SIZE (type);
   12435         2765 :       if (!len || TREE_CODE (len) != INTEGER_CST)
   12436              :         return NULL_TREE;
   12437              :     }
   12438         1177 :   else if (GFC_DESCRIPTOR_TYPE_P (type)
   12439         1177 :           && gfc_is_simply_contiguous (expr, false, false))
   12440              :     {
   12441         1077 :       if (POINTER_TYPE_P (TREE_TYPE (dest)))
   12442            4 :         dest = build_fold_indirect_ref_loc (input_location, dest);
   12443         1077 :       len = gfc_conv_descriptor_size (dest, GFC_TYPE_ARRAY_RANK (type));
   12444         1077 :       dest = gfc_conv_descriptor_data_get (dest);
   12445              :     }
   12446              :   else
   12447          100 :     return NULL_TREE;
   12448              : 
   12449              :   /* If we are zeroing a local array avoid taking its address by emitting
   12450              :      a = {} instead.  */
   12451         3663 :   if (!POINTER_TYPE_P (TREE_TYPE (dest)))
   12452         2544 :     return build2_loc (input_location, MODIFY_EXPR, void_type_node,
   12453         2544 :                        dest, build_constructor (TREE_TYPE (dest),
   12454         2544 :                                               NULL));
   12455              : 
   12456              :   /* Multiply len by element size.  */
   12457         1119 :   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
   12458         1119 :   len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
   12459              :                          len, fold_convert (gfc_array_index_type, tmp));
   12460              : 
   12461              :   /* Convert arguments to the correct types.  */
   12462         1119 :   dest = fold_convert (pvoid_type_node, dest);
   12463         1119 :   len = fold_convert (size_type_node, len);
   12464              : 
   12465              :   /* Construct call to __builtin_memset.  */
   12466         1119 :   tmp = build_call_expr_loc (input_location,
   12467              :                              builtin_decl_explicit (BUILT_IN_MEMSET),
   12468              :                              3, dest, integer_zero_node, len);
   12469         1119 :   return fold_convert (void_type_node, tmp);
   12470              : }
   12471              : 
   12472              : 
   12473              : /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
   12474              :    that constructs the call to __builtin_memcpy.  */
   12475              : 
   12476              : tree
   12477         7848 : gfc_build_memcpy_call (tree dst, tree src, tree len)
   12478              : {
   12479         7848 :   tree tmp;
   12480              : 
   12481              :   /* Convert arguments to the correct types.  */
   12482         7848 :   if (!POINTER_TYPE_P (TREE_TYPE (dst)))
   12483         7547 :     dst = gfc_build_addr_expr (pvoid_type_node, dst);
   12484              :   else
   12485          301 :     dst = fold_convert (pvoid_type_node, dst);
   12486              : 
   12487         7848 :   if (!POINTER_TYPE_P (TREE_TYPE (src)))
   12488         7446 :     src = gfc_build_addr_expr (pvoid_type_node, src);
   12489              :   else
   12490          402 :     src = fold_convert (pvoid_type_node, src);
   12491              : 
   12492         7848 :   len = fold_convert (size_type_node, len);
   12493              : 
   12494              :   /* Construct call to __builtin_memcpy.  */
   12495         7848 :   tmp = build_call_expr_loc (input_location,
   12496              :                              builtin_decl_explicit (BUILT_IN_MEMCPY),
   12497              :                              3, dst, src, len);
   12498         7848 :   return fold_convert (void_type_node, tmp);
   12499              : }
   12500              : 
   12501              : 
   12502              : /* Try to efficiently translate dst(:) = src(:).  Return NULL if this
   12503              :    can't be done.  EXPR1 is the destination/lhs and EXPR2 is the
   12504              :    source/rhs, both are gfc_full_array_ref_p which have been checked for
   12505              :    dependencies.  */
   12506              : 
   12507              : static tree
   12508         2591 : gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
   12509              : {
   12510         2591 :   tree dst, dlen, dtype;
   12511         2591 :   tree src, slen, stype;
   12512         2591 :   tree tmp;
   12513              : 
   12514         2591 :   dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
   12515         2591 :   src = gfc_get_symbol_decl (expr2->symtree->n.sym);
   12516              : 
   12517         2591 :   dtype = TREE_TYPE (dst);
   12518         2591 :   if (POINTER_TYPE_P (dtype))
   12519          253 :     dtype = TREE_TYPE (dtype);
   12520         2591 :   stype = TREE_TYPE (src);
   12521         2591 :   if (POINTER_TYPE_P (stype))
   12522          281 :     stype = TREE_TYPE (stype);
   12523              : 
   12524         2591 :   if (!GFC_ARRAY_TYPE_P (dtype) || !GFC_ARRAY_TYPE_P (stype))
   12525              :     return NULL_TREE;
   12526              : 
   12527              :   /* Determine the lengths of the arrays.  */
   12528         1581 :   dlen = GFC_TYPE_ARRAY_SIZE (dtype);
   12529         1581 :   if (!dlen || TREE_CODE (dlen) != INTEGER_CST)
   12530              :     return NULL_TREE;
   12531         1492 :   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
   12532         1492 :   dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
   12533              :                           dlen, fold_convert (gfc_array_index_type, tmp));
   12534              : 
   12535         1492 :   slen = GFC_TYPE_ARRAY_SIZE (stype);
   12536         1492 :   if (!slen || TREE_CODE (slen) != INTEGER_CST)
   12537              :     return NULL_TREE;
   12538         1486 :   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (stype));
   12539         1486 :   slen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
   12540              :                           slen, fold_convert (gfc_array_index_type, tmp));
   12541              : 
   12542              :   /* Sanity check that they are the same.  This should always be
   12543              :      the case, as we should already have checked for conformance.  */
   12544         1486 :   if (!tree_int_cst_equal (slen, dlen))
   12545              :     return NULL_TREE;
   12546              : 
   12547         1486 :   return gfc_build_memcpy_call (dst, src, dlen);
   12548              : }
   12549              : 
   12550              : 
   12551              : /* Try to efficiently translate array(:) = (/ ... /).  Return NULL if
   12552              :    this can't be done.  EXPR1 is the destination/lhs for which
   12553              :    gfc_full_array_ref_p is true, and EXPR2 is the source/rhs.  */
   12554              : 
   12555              : static tree
   12556         7992 : gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
   12557              : {
   12558         7992 :   unsigned HOST_WIDE_INT nelem;
   12559         7992 :   tree dst, dtype;
   12560         7992 :   tree src, stype;
   12561         7992 :   tree len;
   12562         7992 :   tree tmp;
   12563              : 
   12564         7992 :   nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
   12565         7992 :   if (nelem == 0)
   12566              :     return NULL_TREE;
   12567              : 
   12568         6650 :   dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
   12569         6650 :   dtype = TREE_TYPE (dst);
   12570         6650 :   if (POINTER_TYPE_P (dtype))
   12571          258 :     dtype = TREE_TYPE (dtype);
   12572         6650 :   if (!GFC_ARRAY_TYPE_P (dtype))
   12573              :     return NULL_TREE;
   12574              : 
   12575              :   /* Determine the lengths of the array.  */
   12576         5835 :   len = GFC_TYPE_ARRAY_SIZE (dtype);
   12577         5835 :   if (!len || TREE_CODE (len) != INTEGER_CST)
   12578              :     return NULL_TREE;
   12579              : 
   12580              :   /* Confirm that the constructor is the same size.  */
   12581         5737 :   if (compare_tree_int (len, nelem) != 0)
   12582              :     return NULL_TREE;
   12583              : 
   12584         5737 :   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
   12585         5737 :   len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
   12586              :                          fold_convert (gfc_array_index_type, tmp));
   12587              : 
   12588         5737 :   stype = gfc_typenode_for_spec (&expr2->ts);
   12589         5737 :   src = gfc_build_constant_array_constructor (expr2, stype);
   12590              : 
   12591         5737 :   return gfc_build_memcpy_call (dst, src, len);
   12592              : }
   12593              : 
   12594              : 
   12595              : /* Tells whether the expression is to be treated as a variable reference.  */
   12596              : 
   12597              : bool
   12598       312187 : gfc_expr_is_variable (gfc_expr *expr)
   12599              : {
   12600       312447 :   gfc_expr *arg;
   12601       312447 :   gfc_component *comp;
   12602       312447 :   gfc_symbol *func_ifc;
   12603              : 
   12604       312447 :   if (expr->expr_type == EXPR_VARIABLE)
   12605              :     return true;
   12606              : 
   12607       277504 :   arg = gfc_get_noncopying_intrinsic_argument (expr);
   12608       277504 :   if (arg)
   12609              :     {
   12610          260 :       gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
   12611              :       return gfc_expr_is_variable (arg);
   12612              :     }
   12613              : 
   12614              :   /* A data-pointer-returning function should be considered as a variable
   12615              :      too.  */
   12616       277244 :   if (expr->expr_type == EXPR_FUNCTION
   12617        36795 :       && expr->ref == NULL)
   12618              :     {
   12619        36418 :       if (expr->value.function.isym != NULL)
   12620              :         return false;
   12621              : 
   12622         9469 :       if (expr->value.function.esym != NULL)
   12623              :         {
   12624         9460 :           func_ifc = expr->value.function.esym;
   12625         9460 :           goto found_ifc;
   12626              :         }
   12627            9 :       gcc_assert (expr->symtree);
   12628            9 :       func_ifc = expr->symtree->n.sym;
   12629            9 :       goto found_ifc;
   12630              :     }
   12631              : 
   12632       240826 :   comp = gfc_get_proc_ptr_comp (expr);
   12633       240826 :   if ((expr->expr_type == EXPR_PPC || expr->expr_type == EXPR_FUNCTION)
   12634          377 :       && comp)
   12635              :     {
   12636          275 :       func_ifc = comp->ts.interface;
   12637          275 :       goto found_ifc;
   12638              :     }
   12639              : 
   12640       240551 :   if (expr->expr_type == EXPR_COMPCALL)
   12641              :     {
   12642            0 :       gcc_assert (!expr->value.compcall.tbp->is_generic);
   12643            0 :       func_ifc = expr->value.compcall.tbp->u.specific->n.sym;
   12644            0 :       goto found_ifc;
   12645              :     }
   12646              : 
   12647              :   return false;
   12648              : 
   12649         9744 : found_ifc:
   12650         9744 :   gcc_assert (func_ifc->attr.function
   12651              :               && func_ifc->result != NULL);
   12652         9744 :   return func_ifc->result->attr.pointer;
   12653              : }
   12654              : 
   12655              : 
   12656              : /* Is the lhs OK for automatic reallocation?  */
   12657              : 
   12658              : static bool
   12659       264400 : is_scalar_reallocatable_lhs (gfc_expr *expr)
   12660              : {
   12661       264400 :   gfc_ref * ref;
   12662              : 
   12663              :   /* An allocatable variable with no reference.  */
   12664       264400 :   if (expr->symtree->n.sym->attr.allocatable
   12665         6742 :         && !expr->ref)
   12666              :     return true;
   12667              : 
   12668              :   /* All that can be left are allocatable components.  However, we do
   12669              :      not check for allocatable components here because the expression
   12670              :      could be an allocatable component of a pointer component.  */
   12671       261648 :   if (expr->symtree->n.sym->ts.type != BT_DERIVED
   12672       239360 :         && expr->symtree->n.sym->ts.type != BT_CLASS)
   12673              :     return false;
   12674              : 
   12675              :   /* Find an allocatable component ref last.  */
   12676        39564 :   for (ref = expr->ref; ref; ref = ref->next)
   12677        16327 :     if (ref->type == REF_COMPONENT
   12678        12107 :           && !ref->next
   12679         9367 :           && ref->u.c.component->attr.allocatable)
   12680              :       return true;
   12681              : 
   12682              :   return false;
   12683              : }
   12684              : 
   12685              : 
   12686              : /* Allocate or reallocate scalar lhs, as necessary.  */
   12687              : 
   12688              : static void
   12689         3586 : alloc_scalar_allocatable_for_assignment (stmtblock_t *block,
   12690              :                                          tree string_length,
   12691              :                                          gfc_expr *expr1,
   12692              :                                          gfc_expr *expr2)
   12693              : 
   12694              : {
   12695         3586 :   tree cond;
   12696         3586 :   tree tmp;
   12697         3586 :   tree size;
   12698         3586 :   tree size_in_bytes;
   12699         3586 :   tree jump_label1;
   12700         3586 :   tree jump_label2;
   12701         3586 :   gfc_se lse;
   12702         3586 :   gfc_ref *ref;
   12703              : 
   12704         3586 :   if (!expr1 || expr1->rank)
   12705            0 :     return;
   12706              : 
   12707         3586 :   if (!expr2 || expr2->rank)
   12708              :     return;
   12709              : 
   12710         5046 :   for (ref = expr1->ref; ref; ref = ref->next)
   12711         1460 :     if (ref->type == REF_SUBSTRING)
   12712              :       return;
   12713              : 
   12714         3586 :   realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
   12715              : 
   12716              :   /* Since this is a scalar lhs, we can afford to do this.  That is,
   12717              :      there is no risk of side effects being repeated.  */
   12718         3586 :   gfc_init_se (&lse, NULL);
   12719         3586 :   lse.want_pointer = 1;
   12720         3586 :   gfc_conv_expr (&lse, expr1);
   12721              : 
   12722         3586 :   jump_label1 = gfc_build_label_decl (NULL_TREE);
   12723         3586 :   jump_label2 = gfc_build_label_decl (NULL_TREE);
   12724              : 
   12725              :   /* Do the allocation if the lhs is NULL. Otherwise go to label 1.  */
   12726         3586 :   tmp = build_int_cst (TREE_TYPE (lse.expr), 0);
   12727         3586 :   cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
   12728              :                           lse.expr, tmp);
   12729         3586 :   tmp = build3_v (COND_EXPR, cond,
   12730              :                   build1_v (GOTO_EXPR, jump_label1),
   12731              :                   build_empty_stmt (input_location));
   12732         3586 :   gfc_add_expr_to_block (block, tmp);
   12733              : 
   12734         3586 :   if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   12735              :     {
   12736              :       /* Use the rhs string length and the lhs element size. Note that 'size' is
   12737              :          used below for the string-length comparison, only.  */
   12738         1492 :       size = string_length;
   12739         1492 :       tmp = TYPE_SIZE_UNIT (gfc_get_char_type (expr1->ts.kind));
   12740         2984 :       size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
   12741         1492 :                                        TREE_TYPE (tmp), tmp,
   12742         1492 :                                        fold_convert (TREE_TYPE (tmp), size));
   12743              :     }
   12744              :   else
   12745              :     {
   12746              :       /* Otherwise use the length in bytes of the rhs.  */
   12747         2094 :       size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr1->ts));
   12748         2094 :       size_in_bytes = size;
   12749              :     }
   12750              : 
   12751         3586 :   size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
   12752              :                                    size_in_bytes, size_one_node);
   12753              : 
   12754         3586 :   if (gfc_caf_attr (expr1).codimension && flag_coarray == GFC_FCOARRAY_LIB)
   12755              :     {
   12756           32 :       tree caf_decl, token;
   12757           32 :       gfc_se caf_se;
   12758           32 :       symbol_attribute attr;
   12759              : 
   12760           32 :       gfc_clear_attr (&attr);
   12761           32 :       gfc_init_se (&caf_se, NULL);
   12762              : 
   12763           32 :       caf_decl = gfc_get_tree_for_caf_expr (expr1);
   12764           32 :       gfc_get_caf_token_offset (&caf_se, &token, NULL, caf_decl, NULL_TREE,
   12765              :                                 NULL);
   12766           32 :       gfc_add_block_to_block (block, &caf_se.pre);
   12767           32 :       gfc_allocate_allocatable (block, lse.expr, size_in_bytes,
   12768              :                                 gfc_build_addr_expr (NULL_TREE, token),
   12769              :                                 NULL_TREE, NULL_TREE, NULL_TREE, jump_label1,
   12770              :                                 expr1, 1);
   12771              :     }
   12772         3554 :   else if (expr1->ts.type == BT_DERIVED && expr1->ts.u.derived->attr.alloc_comp)
   12773              :     {
   12774           55 :       tmp = build_call_expr_loc (input_location,
   12775              :                                  builtin_decl_explicit (BUILT_IN_CALLOC),
   12776              :                                  2, build_one_cst (size_type_node),
   12777              :                                  size_in_bytes);
   12778           55 :       tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
   12779           55 :       gfc_add_modify (block, lse.expr, tmp);
   12780              :     }
   12781              :   else
   12782              :     {
   12783         3499 :       tmp = build_call_expr_loc (input_location,
   12784              :                                  builtin_decl_explicit (BUILT_IN_MALLOC),
   12785              :                                  1, size_in_bytes);
   12786         3499 :       tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
   12787         3499 :       gfc_add_modify (block, lse.expr, tmp);
   12788              :     }
   12789              : 
   12790         3586 :   if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   12791              :     {
   12792              :       /* Deferred characters need checking for lhs and rhs string
   12793              :          length.  Other deferred parameter variables will have to
   12794              :          come here too.  */
   12795         1492 :       tmp = build1_v (GOTO_EXPR, jump_label2);
   12796         1492 :       gfc_add_expr_to_block (block, tmp);
   12797              :     }
   12798         3586 :   tmp = build1_v (LABEL_EXPR, jump_label1);
   12799         3586 :   gfc_add_expr_to_block (block, tmp);
   12800              : 
   12801              :   /* For a deferred length character, reallocate if lengths of lhs and
   12802              :      rhs are different.  */
   12803         3586 :   if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   12804              :     {
   12805         1492 :       cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   12806              :                               lse.string_length,
   12807         1492 :                               fold_convert (TREE_TYPE (lse.string_length),
   12808              :                                             size));
   12809              :       /* Jump past the realloc if the lengths are the same.  */
   12810         1492 :       tmp = build3_v (COND_EXPR, cond,
   12811              :                       build1_v (GOTO_EXPR, jump_label2),
   12812              :                       build_empty_stmt (input_location));
   12813         1492 :       gfc_add_expr_to_block (block, tmp);
   12814         1492 :       tmp = build_call_expr_loc (input_location,
   12815              :                                  builtin_decl_explicit (BUILT_IN_REALLOC),
   12816              :                                  2, fold_convert (pvoid_type_node, lse.expr),
   12817              :                                  size_in_bytes);
   12818         1492 :       tree omp_cond = NULL_TREE;
   12819         1492 :       if (flag_openmp_allocators)
   12820              :         {
   12821            1 :           tree omp_tmp;
   12822            1 :           omp_cond = gfc_omp_call_is_alloc (lse.expr);
   12823            1 :           omp_cond = gfc_evaluate_now (omp_cond, block);
   12824              : 
   12825            1 :           omp_tmp = builtin_decl_explicit (BUILT_IN_GOMP_REALLOC);
   12826            1 :           omp_tmp = build_call_expr_loc (input_location, omp_tmp, 4,
   12827              :                                          fold_convert (pvoid_type_node,
   12828              :                                                        lse.expr), size_in_bytes,
   12829              :                                          build_zero_cst (ptr_type_node),
   12830              :                                          build_zero_cst (ptr_type_node));
   12831            1 :           tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
   12832              :                             omp_cond, omp_tmp, tmp);
   12833              :         }
   12834         1492 :       tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
   12835         1492 :       gfc_add_modify (block, lse.expr, tmp);
   12836         1492 :       if (omp_cond)
   12837            1 :         gfc_add_expr_to_block (block,
   12838              :                                build3_loc (input_location, COND_EXPR,
   12839              :                                void_type_node, omp_cond,
   12840              :                                gfc_omp_call_add_alloc (lse.expr),
   12841              :                                build_empty_stmt (input_location)));
   12842         1492 :       tmp = build1_v (LABEL_EXPR, jump_label2);
   12843         1492 :       gfc_add_expr_to_block (block, tmp);
   12844              : 
   12845              :       /* Update the lhs character length.  */
   12846         1492 :       size = string_length;
   12847         1492 :       gfc_add_modify (block, lse.string_length,
   12848         1492 :                       fold_convert (TREE_TYPE (lse.string_length), size));
   12849              :     }
   12850              : }
   12851              : 
   12852              : /* Check for assignments of the type
   12853              : 
   12854              :    a = a + 4
   12855              : 
   12856              :    to make sure we do not check for reallocation unneccessarily.  */
   12857              : 
   12858              : 
   12859              : /* Strip parentheses from an expression to get the underlying variable.
   12860              :    This is needed for self-assignment detection since (a) creates a
   12861              :    parentheses operator node.  */
   12862              : 
   12863              : static gfc_expr *
   12864         7780 : strip_parentheses (gfc_expr *expr)
   12865              : {
   12866            0 :   while (expr->expr_type == EXPR_OP
   12867       313698 :          && expr->value.op.op == INTRINSIC_PARENTHESES)
   12868          590 :     expr = expr->value.op.op1;
   12869       312467 :   return expr;
   12870              : }
   12871              : 
   12872              : 
   12873              : static bool
   12874         7333 : is_runtime_conformable (gfc_expr *expr1, gfc_expr *expr2)
   12875              : {
   12876         7780 :   gfc_actual_arglist *a;
   12877         7780 :   gfc_expr *e1, *e2;
   12878              : 
   12879              :   /* Strip parentheses to handle cases like a = (a).  */
   12880        15611 :   expr1 = strip_parentheses (expr1);
   12881         7780 :   expr2 = strip_parentheses (expr2);
   12882              : 
   12883         7780 :   switch (expr2->expr_type)
   12884              :     {
   12885         2116 :     case EXPR_VARIABLE:
   12886         2116 :       return gfc_dep_compare_expr (expr1, expr2) == 0;
   12887              : 
   12888         2827 :     case EXPR_FUNCTION:
   12889         2827 :       if (expr2->value.function.esym
   12890          293 :           && expr2->value.function.esym->attr.elemental)
   12891              :         {
   12892           75 :           for (a = expr2->value.function.actual; a != NULL; a = a->next)
   12893              :             {
   12894           74 :               e1 = a->expr;
   12895           74 :               if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
   12896              :                 return false;
   12897              :             }
   12898              :           return true;
   12899              :         }
   12900         2765 :       else if (expr2->value.function.isym
   12901         2520 :                && expr2->value.function.isym->elemental)
   12902              :         {
   12903          332 :           for (a = expr2->value.function.actual; a != NULL; a = a->next)
   12904              :             {
   12905          322 :               e1 = a->expr;
   12906          322 :               if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
   12907              :                 return false;
   12908              :             }
   12909              :           return true;
   12910              :         }
   12911              : 
   12912              :       break;
   12913              : 
   12914          641 :     case EXPR_OP:
   12915          641 :       switch (expr2->value.op.op)
   12916              :         {
   12917           19 :         case INTRINSIC_NOT:
   12918           19 :         case INTRINSIC_UPLUS:
   12919           19 :         case INTRINSIC_UMINUS:
   12920           19 :         case INTRINSIC_PARENTHESES:
   12921           19 :           return is_runtime_conformable (expr1, expr2->value.op.op1);
   12922              : 
   12923          597 :         case INTRINSIC_PLUS:
   12924          597 :         case INTRINSIC_MINUS:
   12925          597 :         case INTRINSIC_TIMES:
   12926          597 :         case INTRINSIC_DIVIDE:
   12927          597 :         case INTRINSIC_POWER:
   12928          597 :         case INTRINSIC_AND:
   12929          597 :         case INTRINSIC_OR:
   12930          597 :         case INTRINSIC_EQV:
   12931          597 :         case INTRINSIC_NEQV:
   12932          597 :         case INTRINSIC_EQ:
   12933          597 :         case INTRINSIC_NE:
   12934          597 :         case INTRINSIC_GT:
   12935          597 :         case INTRINSIC_GE:
   12936          597 :         case INTRINSIC_LT:
   12937          597 :         case INTRINSIC_LE:
   12938          597 :         case INTRINSIC_EQ_OS:
   12939          597 :         case INTRINSIC_NE_OS:
   12940          597 :         case INTRINSIC_GT_OS:
   12941          597 :         case INTRINSIC_GE_OS:
   12942          597 :         case INTRINSIC_LT_OS:
   12943          597 :         case INTRINSIC_LE_OS:
   12944              : 
   12945          597 :           e1 = expr2->value.op.op1;
   12946          597 :           e2 = expr2->value.op.op2;
   12947              : 
   12948          597 :           if (e1->rank == 0 && e2->rank > 0)
   12949              :             return is_runtime_conformable (expr1, e2);
   12950          539 :           else if (e1->rank > 0 && e2->rank == 0)
   12951              :             return is_runtime_conformable (expr1, e1);
   12952          169 :           else if (e1->rank > 0 && e2->rank > 0)
   12953          169 :             return is_runtime_conformable (expr1, e1)
   12954          169 :               && is_runtime_conformable (expr1, e2);
   12955              :           break;
   12956              : 
   12957              :         default:
   12958              :           break;
   12959              : 
   12960              :         }
   12961              : 
   12962              :       break;
   12963              : 
   12964              :     default:
   12965              :       break;
   12966              :     }
   12967              :   return false;
   12968              : }
   12969              : 
   12970              : 
   12971              : static tree
   12972         3318 : trans_class_assignment (stmtblock_t *block, gfc_expr *lhs, gfc_expr *rhs,
   12973              :                         gfc_se *lse, gfc_se *rse, bool use_vptr_copy,
   12974              :                         bool class_realloc)
   12975              : {
   12976         3318 :   tree tmp, fcn, stdcopy, to_len, from_len, vptr, old_vptr, rhs_vptr;
   12977         3318 :   vec<tree, va_gc> *args = NULL;
   12978         3318 :   bool final_expr;
   12979              : 
   12980         3318 :   final_expr = gfc_assignment_finalizer_call (lse, lhs, false);
   12981         3318 :   if (final_expr)
   12982              :     {
   12983          485 :       if (rse->loop)
   12984          226 :         gfc_prepend_expr_to_block (&rse->loop->pre,
   12985              :                                    gfc_finish_block (&lse->finalblock));
   12986              :       else
   12987          259 :         gfc_add_block_to_block (block, &lse->finalblock);
   12988              :     }
   12989              : 
   12990              :   /* Store the old vptr so that dynamic types can be compared for
   12991              :      reallocation to occur or not.  */
   12992         3318 :   if (class_realloc)
   12993              :     {
   12994          301 :       tmp = lse->expr;
   12995          301 :       if (!GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
   12996           18 :         tmp = gfc_get_class_from_expr (tmp);
   12997              :     }
   12998              : 
   12999         3318 :   vptr = trans_class_vptr_len_assignment (block, lhs, rhs, rse, &to_len,
   13000              :                                           &from_len, &rhs_vptr);
   13001         3318 :   if (rhs_vptr == NULL_TREE)
   13002           61 :     rhs_vptr = vptr;
   13003              : 
   13004              :   /* Generate (re)allocation of the lhs.  */
   13005         3318 :   if (class_realloc)
   13006              :     {
   13007          301 :       stmtblock_t alloc, re_alloc;
   13008          301 :       tree class_han, re, size;
   13009              : 
   13010          301 :       if (tmp && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
   13011          283 :         old_vptr = gfc_evaluate_now (gfc_class_vptr_get (tmp), block);
   13012              :       else
   13013           18 :         old_vptr = build_int_cst (TREE_TYPE (vptr), 0);
   13014              : 
   13015          301 :       size = gfc_vptr_size_get (rhs_vptr);
   13016              : 
   13017              :       /* Take into account _len of unlimited polymorphic entities.
   13018              :          TODO: handle class(*) allocatable function results on rhs.  */
   13019          301 :       if (UNLIMITED_POLY (rhs))
   13020              :         {
   13021           18 :           tree len;
   13022           18 :           if (rhs->expr_type == EXPR_VARIABLE)
   13023           12 :             len = trans_get_upoly_len (block, rhs);
   13024              :           else
   13025            6 :             len = gfc_class_len_get (tmp);
   13026           18 :           len = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
   13027              :                                  fold_convert (size_type_node, len),
   13028              :                                  size_one_node);
   13029           18 :           size = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (size),
   13030           18 :                                   size, fold_convert (TREE_TYPE (size), len));
   13031           18 :         }
   13032          283 :       else if (rhs->ts.type == BT_CHARACTER && rse->string_length)
   13033           27 :         size = fold_build2_loc (input_location, MULT_EXPR,
   13034              :                                 gfc_charlen_type_node, size,
   13035              :                                 rse->string_length);
   13036              : 
   13037              : 
   13038          301 :       tmp = lse->expr;
   13039          301 :       class_han = GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
   13040          301 :           ? gfc_class_data_get (tmp) : tmp;
   13041              : 
   13042          301 :       if (!POINTER_TYPE_P (TREE_TYPE (class_han)))
   13043           18 :         class_han = gfc_build_addr_expr (NULL_TREE, class_han);
   13044              : 
   13045              :       /* Allocate block.  */
   13046          301 :       gfc_init_block (&alloc);
   13047          301 :       gfc_allocate_using_malloc (&alloc, class_han, size, NULL_TREE);
   13048              : 
   13049              :       /* Reallocate if dynamic types are different. */
   13050          301 :       gfc_init_block (&re_alloc);
   13051          301 :       if (UNLIMITED_POLY (lhs) && rhs->ts.type == BT_CHARACTER)
   13052              :         {
   13053           27 :           gfc_add_expr_to_block (&re_alloc, gfc_call_free (class_han));
   13054           27 :           gfc_allocate_using_malloc (&re_alloc, class_han, size, NULL_TREE);
   13055              :         }
   13056              :       else
   13057              :         {
   13058          274 :           tmp = fold_convert (pvoid_type_node, class_han);
   13059          274 :           re = build_call_expr_loc (input_location,
   13060              :                                     builtin_decl_explicit (BUILT_IN_REALLOC),
   13061              :                                     2, tmp, size);
   13062          274 :           re = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (tmp),
   13063              :                                 tmp, re);
   13064          274 :           tmp = fold_build2_loc (input_location, NE_EXPR,
   13065              :                                  logical_type_node, rhs_vptr, old_vptr);
   13066          274 :           re = fold_build3_loc (input_location, COND_EXPR, void_type_node,
   13067              :                                 tmp, re, build_empty_stmt (input_location));
   13068          274 :           gfc_add_expr_to_block (&re_alloc, re);
   13069              :         }
   13070          301 :       tree realloc_expr = lhs->ts.type == BT_CLASS ?
   13071          283 :                                           gfc_finish_block (&re_alloc) :
   13072           18 :                                           build_empty_stmt (input_location);
   13073              : 
   13074              :       /* Allocate if _data is NULL, reallocate otherwise.  */
   13075          301 :       tmp = fold_build2_loc (input_location, EQ_EXPR,
   13076              :                              logical_type_node, class_han,
   13077              :                              build_int_cst (prvoid_type_node, 0));
   13078          301 :       tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
   13079              :                              gfc_unlikely (tmp,
   13080              :                                            PRED_FORTRAN_FAIL_ALLOC),
   13081              :                              gfc_finish_block (&alloc),
   13082              :                              realloc_expr);
   13083          301 :       gfc_add_expr_to_block (&lse->pre, tmp);
   13084              :     }
   13085              : 
   13086         3318 :   fcn = gfc_vptr_copy_get (vptr);
   13087              : 
   13088         3318 :   tmp = GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr))
   13089         3318 :       ? gfc_class_data_get (rse->expr) : rse->expr;
   13090         3318 :   if (use_vptr_copy)
   13091              :     {
   13092         5584 :       if (!POINTER_TYPE_P (TREE_TYPE (tmp))
   13093          524 :           || INDIRECT_REF_P (tmp)
   13094          403 :           || (rhs->ts.type == BT_DERIVED
   13095            0 :               && rhs->ts.u.derived->attr.unlimited_polymorphic
   13096            0 :               && !rhs->ts.u.derived->attr.pointer
   13097            0 :               && !rhs->ts.u.derived->attr.allocatable)
   13098         3454 :           || (UNLIMITED_POLY (rhs)
   13099          134 :               && !CLASS_DATA (rhs)->attr.pointer
   13100           43 :               && !CLASS_DATA (rhs)->attr.allocatable))
   13101         2648 :         vec_safe_push (args, gfc_build_addr_expr (NULL_TREE, tmp));
   13102              :       else
   13103          403 :         vec_safe_push (args, tmp);
   13104         3051 :       tmp = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
   13105         3051 :           ? gfc_class_data_get (lse->expr) : lse->expr;
   13106         5322 :       if (!POINTER_TYPE_P (TREE_TYPE (tmp))
   13107          780 :           || INDIRECT_REF_P (tmp)
   13108          283 :           || (lhs->ts.type == BT_DERIVED
   13109            0 :               && lhs->ts.u.derived->attr.unlimited_polymorphic
   13110            0 :               && !lhs->ts.u.derived->attr.pointer
   13111            0 :               && !lhs->ts.u.derived->attr.allocatable)
   13112         3334 :           || (UNLIMITED_POLY (lhs)
   13113          119 :               && !CLASS_DATA (lhs)->attr.pointer
   13114          119 :               && !CLASS_DATA (lhs)->attr.allocatable))
   13115         2768 :         vec_safe_push (args, gfc_build_addr_expr (NULL_TREE, tmp));
   13116              :       else
   13117          283 :         vec_safe_push (args, tmp);
   13118              : 
   13119         3051 :       stdcopy = build_call_vec (TREE_TYPE (TREE_TYPE (fcn)), fcn, args);
   13120              : 
   13121         3051 :       if (to_len != NULL_TREE && !integer_zerop (from_len))
   13122              :         {
   13123          406 :           tree extcopy;
   13124          406 :           vec_safe_push (args, from_len);
   13125          406 :           vec_safe_push (args, to_len);
   13126          406 :           extcopy = build_call_vec (TREE_TYPE (TREE_TYPE (fcn)), fcn, args);
   13127              : 
   13128          406 :           tmp = fold_build2_loc (input_location, GT_EXPR,
   13129              :                                  logical_type_node, from_len,
   13130          406 :                                  build_zero_cst (TREE_TYPE (from_len)));
   13131          406 :           return fold_build3_loc (input_location, COND_EXPR,
   13132              :                                   void_type_node, tmp,
   13133          406 :                                   extcopy, stdcopy);
   13134              :         }
   13135              :       else
   13136         2645 :         return stdcopy;
   13137              :     }
   13138              :   else
   13139              :     {
   13140          267 :       tree rhst = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
   13141          267 :           ? gfc_class_data_get (lse->expr) : lse->expr;
   13142          267 :       stmtblock_t tblock;
   13143          267 :       gfc_init_block (&tblock);
   13144          267 :       if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
   13145            0 :         tmp = gfc_build_addr_expr (NULL_TREE, tmp);
   13146          267 :       if (!POINTER_TYPE_P (TREE_TYPE (rhst)))
   13147            0 :         rhst = gfc_build_addr_expr (NULL_TREE, rhst);
   13148              :       /* When coming from a ptr_copy lhs and rhs are swapped.  */
   13149          267 :       gfc_add_modify_loc (input_location, &tblock, rhst,
   13150          267 :                           fold_convert (TREE_TYPE (rhst), tmp));
   13151          267 :       return gfc_finish_block (&tblock);
   13152              :     }
   13153              : }
   13154              : 
   13155              : bool
   13156       306726 : is_assoc_assign (gfc_expr *lhs, gfc_expr *rhs)
   13157              : {
   13158       306726 :   if (lhs->expr_type != EXPR_VARIABLE || rhs->expr_type != EXPR_VARIABLE)
   13159              :     return false;
   13160              : 
   13161        31623 :   return lhs->symtree->n.sym->assoc
   13162        31623 :          && lhs->symtree->n.sym->assoc->target == rhs;
   13163              : }
   13164              : 
   13165              : /* Subroutine of gfc_trans_assignment that actually scalarizes the
   13166              :    assignment.  EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
   13167              :    init_flag indicates initialization expressions and dealloc that no
   13168              :    deallocate prior assignment is needed (if in doubt, set true).
   13169              :    When PTR_COPY is set and expr1 is a class type, then use the _vptr-copy
   13170              :    routine instead of a pointer assignment.  Alias resolution is only done,
   13171              :    when MAY_ALIAS is set (the default).  This flag is used by ALLOCATE()
   13172              :    where it is known, that newly allocated memory on the lhs can never be
   13173              :    an alias of the rhs.  */
   13174              : 
   13175              : static tree
   13176       306726 : gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
   13177              :                         bool dealloc, bool use_vptr_copy, bool may_alias)
   13178              : {
   13179       306726 :   gfc_se lse;
   13180       306726 :   gfc_se rse;
   13181       306726 :   gfc_ss *lss;
   13182       306726 :   gfc_ss *lss_section;
   13183       306726 :   gfc_ss *rss;
   13184       306726 :   gfc_loopinfo loop;
   13185       306726 :   tree tmp;
   13186       306726 :   stmtblock_t block;
   13187       306726 :   stmtblock_t body;
   13188       306726 :   bool final_expr;
   13189       306726 :   bool l_is_temp;
   13190       306726 :   bool scalar_to_array;
   13191       306726 :   tree string_length;
   13192       306726 :   int n;
   13193       306726 :   bool maybe_workshare = false, lhs_refs_comp = false, rhs_refs_comp = false;
   13194       306726 :   symbol_attribute lhs_caf_attr, rhs_caf_attr, lhs_attr, rhs_attr;
   13195       306726 :   bool is_poly_assign;
   13196       306726 :   bool realloc_flag;
   13197       306726 :   bool assoc_assign = false;
   13198       306726 :   bool dummy_class_array_copy;
   13199              : 
   13200              :   /* Assignment of the form lhs = rhs.  */
   13201       306726 :   gfc_start_block (&block);
   13202              : 
   13203       306726 :   gfc_init_se (&lse, NULL);
   13204       306726 :   gfc_init_se (&rse, NULL);
   13205              : 
   13206       306726 :   gfc_fix_class_refs (expr1);
   13207              : 
   13208       613452 :   realloc_flag = flag_realloc_lhs
   13209       300678 :                  && gfc_is_reallocatable_lhs (expr1)
   13210         8126 :                  && expr2->rank
   13211       313399 :                  && !is_runtime_conformable (expr1, expr2);
   13212              : 
   13213              :   /* Walk the lhs.  */
   13214       306726 :   lss = gfc_walk_expr (expr1);
   13215       306726 :   if (realloc_flag)
   13216              :     {
   13217         6320 :       lss->no_bounds_check = 1;
   13218         6320 :       lss->is_alloc_lhs = 1;
   13219              :     }
   13220              :   else
   13221       300406 :     lss->no_bounds_check = expr1->no_bounds_check;
   13222              : 
   13223       306726 :   rss = NULL;
   13224              : 
   13225       306726 :   if (expr2->expr_type != EXPR_VARIABLE
   13226       306726 :       && expr2->expr_type != EXPR_CONSTANT
   13227       306726 :       && (expr2->ts.type == BT_CLASS || gfc_may_be_finalized (expr2->ts)))
   13228              :     {
   13229          857 :       expr2->must_finalize = 1;
   13230              :       /* F2023 7.5.6.3: If an executable construct references a nonpointer
   13231              :          function, the result is finalized after execution of the innermost
   13232              :          executable construct containing the reference.  */
   13233          857 :       if (expr2->expr_type == EXPR_FUNCTION
   13234          857 :           && (gfc_expr_attr (expr2).pointer
   13235          292 :               || (expr2->ts.type == BT_CLASS && CLASS_DATA (expr2)->attr.class_pointer)))
   13236          146 :         expr2->must_finalize = 0;
   13237              :       /* F2008 4.5.6.3 para 5: If an executable construct references a
   13238              :          structure constructor or array constructor, the entity created by
   13239              :          the constructor is finalized after execution of the innermost
   13240              :          executable construct containing the reference.
   13241              :          These finalizations were later deleted by the Combined Techical
   13242              :          Corrigenda 1 TO 4 for fortran 2008 (f08/0011).  */
   13243          711 :       else if (gfc_notification_std (GFC_STD_F2018_DEL)
   13244          711 :           && (expr2->expr_type == EXPR_STRUCTURE
   13245          668 :               || expr2->expr_type == EXPR_ARRAY))
   13246          357 :         expr2->must_finalize = 0;
   13247              :     }
   13248              : 
   13249              : 
   13250              :   /* Checking whether a class assignment is desired is quite complicated and
   13251              :      needed at two locations, so do it once only before the information is
   13252              :      needed.  */
   13253       306726 :   lhs_attr = gfc_expr_attr (expr1);
   13254       306726 :   rhs_attr = gfc_expr_attr (expr2);
   13255       306726 :   dummy_class_array_copy
   13256       613452 :     = (expr2->expr_type == EXPR_VARIABLE
   13257        31623 :        && expr2->rank > 0
   13258         8342 :        && expr2->symtree != NULL
   13259         8342 :        && expr2->symtree->n.sym->attr.dummy
   13260         1435 :        && expr2->ts.type == BT_CLASS
   13261          127 :        && !rhs_attr.pointer
   13262          127 :        && !rhs_attr.allocatable
   13263          114 :        && !CLASS_DATA (expr2)->attr.class_pointer
   13264       306840 :        && !CLASS_DATA (expr2)->attr.allocatable);
   13265              : 
   13266       306726 :   is_poly_assign
   13267       306726 :     = (use_vptr_copy
   13268       290038 :        || ((lhs_attr.pointer || lhs_attr.allocatable) && !lhs_attr.dimension))
   13269        22533 :       && (expr1->ts.type == BT_CLASS || gfc_is_class_array_ref (expr1, NULL)
   13270        20465 :           || gfc_is_class_scalar_expr (expr1)
   13271        19166 :           || gfc_is_class_array_ref (expr2, NULL)
   13272        19166 :           || gfc_is_class_scalar_expr (expr2))
   13273       310111 :       && lhs_attr.flavor != FL_PROCEDURE;
   13274              : 
   13275       306726 :   assoc_assign = is_assoc_assign (expr1, expr2);
   13276              : 
   13277              :   /* Only analyze the expressions for coarray properties, when in coarray-lib
   13278              :      mode.  Avoid false-positive uninitialized diagnostics with initializing
   13279              :      the codimension flag unconditionally.  */
   13280       306726 :   lhs_caf_attr.codimension = false;
   13281       306726 :   rhs_caf_attr.codimension = false;
   13282       306726 :   if (flag_coarray == GFC_FCOARRAY_LIB)
   13283              :     {
   13284         6660 :       lhs_caf_attr = gfc_caf_attr (expr1, false, &lhs_refs_comp);
   13285         6660 :       rhs_caf_attr = gfc_caf_attr (expr2, false, &rhs_refs_comp);
   13286              :     }
   13287              : 
   13288       306726 :   tree reallocation = NULL_TREE;
   13289       306726 :   if (lss != gfc_ss_terminator)
   13290              :     {
   13291              :       /* The assignment needs scalarization.  */
   13292              :       lss_section = lss;
   13293              : 
   13294              :       /* Find a non-scalar SS from the lhs.  */
   13295              :       while (lss_section != gfc_ss_terminator
   13296        39604 :              && lss_section->info->type != GFC_SS_SECTION)
   13297            0 :         lss_section = lss_section->next;
   13298              : 
   13299        39604 :       gcc_assert (lss_section != gfc_ss_terminator);
   13300              : 
   13301              :       /* Initialize the scalarizer.  */
   13302        39604 :       gfc_init_loopinfo (&loop);
   13303              : 
   13304              :       /* Walk the rhs.  */
   13305        39604 :       rss = gfc_walk_expr (expr2);
   13306        39604 :       if (rss == gfc_ss_terminator)
   13307              :         {
   13308              :           /* The rhs is scalar.  Add a ss for the expression.  */
   13309        14928 :           rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
   13310        14928 :           lss->is_alloc_lhs = 0;
   13311              :         }
   13312              : 
   13313              :       /* When doing a class assign, then the handle to the rhs needs to be a
   13314              :          pointer to allow for polymorphism.  */
   13315        39604 :       if (is_poly_assign && expr2->rank == 0 && !UNLIMITED_POLY (expr2))
   13316          503 :         rss->info->type = GFC_SS_REFERENCE;
   13317              : 
   13318        39604 :       rss->no_bounds_check = expr2->no_bounds_check;
   13319              :       /* Associate the SS with the loop.  */
   13320        39604 :       gfc_add_ss_to_loop (&loop, lss);
   13321        39604 :       gfc_add_ss_to_loop (&loop, rss);
   13322              : 
   13323              :       /* Calculate the bounds of the scalarization.  */
   13324        39604 :       gfc_conv_ss_startstride (&loop);
   13325              :       /* Enable loop reversal.  */
   13326       673268 :       for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
   13327       594060 :         loop.reverse[n] = GFC_ENABLE_REVERSE;
   13328              :       /* Resolve any data dependencies in the statement.  */
   13329        39604 :       if (may_alias)
   13330        37334 :         gfc_conv_resolve_dependencies (&loop, lss, rss);
   13331              :       /* Setup the scalarizing loops.  */
   13332        39604 :       gfc_conv_loop_setup (&loop, &expr2->where);
   13333              : 
   13334              :       /* Setup the gfc_se structures.  */
   13335        39604 :       gfc_copy_loopinfo_to_se (&lse, &loop);
   13336        39604 :       gfc_copy_loopinfo_to_se (&rse, &loop);
   13337              : 
   13338        39604 :       rse.ss = rss;
   13339        39604 :       gfc_mark_ss_chain_used (rss, 1);
   13340        39604 :       if (loop.temp_ss == NULL)
   13341              :         {
   13342        38522 :           lse.ss = lss;
   13343        38522 :           gfc_mark_ss_chain_used (lss, 1);
   13344              :         }
   13345              :       else
   13346              :         {
   13347         1082 :           lse.ss = loop.temp_ss;
   13348         1082 :           gfc_mark_ss_chain_used (lss, 3);
   13349         1082 :           gfc_mark_ss_chain_used (loop.temp_ss, 3);
   13350              :         }
   13351              : 
   13352              :       /* Allow the scalarizer to workshare array assignments.  */
   13353        39604 :       if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_BODY))
   13354              :           == OMPWS_WORKSHARE_FLAG
   13355           85 :           && loop.temp_ss == NULL)
   13356              :         {
   13357           73 :           maybe_workshare = true;
   13358           73 :           ompws_flags |= OMPWS_SCALARIZER_WS | OMPWS_SCALARIZER_BODY;
   13359              :         }
   13360              : 
   13361              :       /* F2003: Allocate or reallocate lhs of allocatable array.  */
   13362        39604 :       if (realloc_flag)
   13363              :         {
   13364         6320 :           realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
   13365         6320 :           ompws_flags &= ~OMPWS_SCALARIZER_WS;
   13366         6320 :           reallocation = gfc_alloc_allocatable_for_assignment (&loop, expr1,
   13367              :                                                                expr2);
   13368              :         }
   13369              : 
   13370              :       /* Start the scalarized loop body.  */
   13371        39604 :       gfc_start_scalarized_body (&loop, &body);
   13372              :     }
   13373              :   else
   13374       267122 :     gfc_init_block (&body);
   13375              : 
   13376       306726 :   l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
   13377              : 
   13378              :   /* Translate the expression.  */
   13379       613452 :   rse.want_coarray = flag_coarray == GFC_FCOARRAY_LIB
   13380       306726 :                      && (init_flag || assoc_assign) && lhs_caf_attr.codimension;
   13381       306726 :   rse.want_pointer = rse.want_coarray && !init_flag && !lhs_caf_attr.dimension;
   13382       306726 :   gfc_conv_expr (&rse, expr2);
   13383              : 
   13384              :   /* Deal with the case of a scalar class function assigned to a derived type.
   13385              :    */
   13386       306726 :   if (gfc_is_alloc_class_scalar_function (expr2)
   13387       306726 :       && expr1->ts.type == BT_DERIVED)
   13388              :     {
   13389           60 :       rse.expr = gfc_class_data_get (rse.expr);
   13390           60 :       rse.expr = build_fold_indirect_ref_loc (input_location, rse.expr);
   13391              :     }
   13392              : 
   13393              :   /* Stabilize a string length for temporaries.  */
   13394       306726 :   if (expr2->ts.type == BT_CHARACTER && !expr1->ts.deferred
   13395        24424 :       && !(VAR_P (rse.string_length)
   13396              :            || TREE_CODE (rse.string_length) == PARM_DECL
   13397              :            || INDIRECT_REF_P (rse.string_length)))
   13398        23560 :     string_length = gfc_evaluate_now (rse.string_length, &rse.pre);
   13399       283166 :   else if (expr2->ts.type == BT_CHARACTER)
   13400              :     {
   13401         4350 :       if (expr1->ts.deferred
   13402         6745 :           && gfc_expr_attr (expr1).allocatable
   13403         6865 :           && gfc_check_dependency (expr1, expr2, true))
   13404          120 :         rse.string_length =
   13405          120 :           gfc_evaluate_now_function_scope (rse.string_length, &rse.pre);
   13406         4350 :       string_length = rse.string_length;
   13407              :     }
   13408              :   else
   13409              :     string_length = NULL_TREE;
   13410              : 
   13411       306726 :   if (l_is_temp)
   13412              :     {
   13413         1082 :       gfc_conv_tmp_array_ref (&lse);
   13414         1082 :       if (expr2->ts.type == BT_CHARACTER)
   13415          123 :         lse.string_length = string_length;
   13416              :     }
   13417              :   else
   13418              :     {
   13419       305644 :       gfc_conv_expr (&lse, expr1);
   13420              :       /* For some expression (e.g. complex numbers) fold_convert uses a
   13421              :          SAVE_EXPR, which is hazardous on the lhs, because the value is
   13422              :          not updated when assigned to.  */
   13423       305644 :       if (TREE_CODE (lse.expr) == SAVE_EXPR)
   13424            8 :         lse.expr = TREE_OPERAND (lse.expr, 0);
   13425              : 
   13426         6153 :       if (gfc_option.rtcheck & GFC_RTCHECK_MEM && !init_flag
   13427       311797 :           && gfc_expr_attr (expr1).allocatable && expr1->rank && !expr2->rank)
   13428              :         {
   13429           36 :           tree cond;
   13430           36 :           const char* msg;
   13431              : 
   13432           36 :           tmp = INDIRECT_REF_P (lse.expr)
   13433           36 :               ? gfc_build_addr_expr (NULL_TREE, lse.expr) : lse.expr;
   13434           36 :           STRIP_NOPS (tmp);
   13435              : 
   13436              :           /* We should only get array references here.  */
   13437           36 :           gcc_assert (TREE_CODE (tmp) == POINTER_PLUS_EXPR
   13438              :                       || TREE_CODE (tmp) == ARRAY_REF);
   13439              : 
   13440              :           /* 'tmp' is either the pointer to the array(POINTER_PLUS_EXPR)
   13441              :              or the array itself(ARRAY_REF).  */
   13442           36 :           tmp = TREE_OPERAND (tmp, 0);
   13443              : 
   13444              :           /* Provide the address of the array.  */
   13445           36 :           if (TREE_CODE (lse.expr) == ARRAY_REF)
   13446           18 :             tmp = gfc_build_addr_expr (NULL_TREE, tmp);
   13447              : 
   13448           36 :           cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   13449           36 :                                   tmp, build_int_cst (TREE_TYPE (tmp), 0));
   13450           36 :           msg = _("Assignment of scalar to unallocated array");
   13451           36 :           gfc_trans_runtime_check (true, false, cond, &loop.pre,
   13452              :                                    &expr1->where, msg);
   13453              :         }
   13454              : 
   13455              :       /* Deallocate the lhs parameterized components if required.  */
   13456       305644 :       if (dealloc
   13457       287414 :           && !expr1->symtree->n.sym->attr.associate_var
   13458       285527 :           && expr2->expr_type != EXPR_ARRAY
   13459       279675 :           && (IS_PDT (expr1) || IS_CLASS_PDT (expr1)))
   13460              :         {
   13461          295 :           bool pdt_dep = gfc_check_dependency (expr1, expr2, true);
   13462              : 
   13463          295 :           tmp = lse.expr;
   13464          295 :           if (pdt_dep)
   13465              :             {
   13466              :               /* Create a temporary for deallocation after assignment.  */
   13467          126 :               tmp = gfc_create_var (TREE_TYPE (lse.expr), "pdt_tmp");
   13468          126 :               gfc_add_modify (&lse.pre, tmp, lse.expr);
   13469              :             }
   13470              : 
   13471          295 :           if (expr1->ts.type == BT_DERIVED)
   13472          295 :             tmp = gfc_deallocate_pdt_comp (expr1->ts.u.derived, tmp,
   13473              :                                            expr1->rank);
   13474            0 :           else if (expr1->ts.type == BT_CLASS)
   13475              :             {
   13476            0 :               tmp = gfc_class_data_get (tmp);
   13477            0 :               tmp = gfc_deallocate_pdt_comp (CLASS_DATA (expr1)->ts.u.derived,
   13478              :                                              tmp, expr1->rank);
   13479              :             }
   13480              : 
   13481          295 :           if (tmp && pdt_dep)
   13482           68 :             gfc_add_expr_to_block (&rse.post, tmp);
   13483          227 :           else if (tmp)
   13484           43 :             gfc_add_expr_to_block (&lse.pre, tmp);
   13485              :         }
   13486              :     }
   13487              : 
   13488              :   /* Assignments of scalar derived types with allocatable components
   13489              :      to arrays must be done with a deep copy and the rhs temporary
   13490              :      must have its components deallocated afterwards.  */
   13491       613452 :   scalar_to_array = (expr2->ts.type == BT_DERIVED
   13492        19114 :                        && expr2->ts.u.derived->attr.alloc_comp
   13493         6527 :                        && !gfc_expr_is_variable (expr2)
   13494       310303 :                        && expr1->rank && !expr2->rank);
   13495       613452 :   scalar_to_array |= (expr1->ts.type == BT_DERIVED
   13496        19397 :                                     && expr1->rank
   13497         3765 :                                     && expr1->ts.u.derived->attr.alloc_comp
   13498       308099 :                                     && gfc_is_alloc_class_scalar_function (expr2));
   13499       306726 :   if (scalar_to_array && dealloc)
   13500              :     {
   13501           59 :       tmp = gfc_deallocate_alloc_comp_no_caf (expr2->ts.u.derived, rse.expr, 0);
   13502           59 :       gfc_prepend_expr_to_block (&loop.post, tmp);
   13503              :     }
   13504              : 
   13505              :   /* When assigning a character function result to a deferred-length variable,
   13506              :      the function call must happen before the (re)allocation of the lhs -
   13507              :      otherwise the character length of the result is not known.
   13508              :      NOTE 1: This relies on having the exact dependence of the length type
   13509              :      parameter available to the caller; gfortran saves it in the .mod files.
   13510              :      NOTE 2: Vector array references generate an index temporary that must
   13511              :      not go outside the loop. Otherwise, variables should not generate
   13512              :      a pre block.
   13513              :      NOTE 3: The concatenation operation generates a temporary pointer,
   13514              :      whose allocation must go to the innermost loop.
   13515              :      NOTE 4: Elemental functions may generate a temporary, too.  */
   13516       306726 :   if (flag_realloc_lhs
   13517       300678 :       && expr2->ts.type == BT_CHARACTER && expr1->ts.deferred
   13518         2958 :       && !(lss != gfc_ss_terminator
   13519          928 :            && rss != gfc_ss_terminator
   13520          928 :            && ((expr2->expr_type == EXPR_VARIABLE && expr2->rank)
   13521          741 :                || (expr2->expr_type == EXPR_FUNCTION
   13522          160 :                    && expr2->value.function.esym != NULL
   13523           26 :                    && expr2->value.function.esym->attr.elemental)
   13524          728 :                || (expr2->expr_type == EXPR_FUNCTION
   13525          147 :                    && expr2->value.function.isym != NULL
   13526          134 :                    && expr2->value.function.isym->elemental)
   13527          672 :                || (expr2->expr_type == EXPR_OP
   13528           31 :                    && expr2->value.op.op == INTRINSIC_CONCAT))))
   13529         2677 :     gfc_add_block_to_block (&block, &rse.pre);
   13530              : 
   13531              :   /* Nullify the allocatable components corresponding to those of the lhs
   13532              :      derived type, so that the finalization of the function result does not
   13533              :      affect the lhs of the assignment. Prepend is used to ensure that the
   13534              :      nullification occurs before the call to the finalizer. In the case of
   13535              :      a scalar to array assignment, this is done in gfc_trans_scalar_assign
   13536              :      as part of the deep copy.  */
   13537       305899 :   if (!scalar_to_array && expr1->ts.type == BT_DERIVED
   13538       325296 :                        && (gfc_is_class_array_function (expr2)
   13539        18546 :                            || gfc_is_alloc_class_scalar_function (expr2)))
   13540              :     {
   13541           78 :       tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, rse.expr, 0);
   13542           78 :       gfc_prepend_expr_to_block (&rse.post, tmp);
   13543           78 :       if (lss != gfc_ss_terminator && rss == gfc_ss_terminator)
   13544            0 :         gfc_add_block_to_block (&loop.post, &rse.post);
   13545              :     }
   13546              : 
   13547       306726 :   tmp = NULL_TREE;
   13548              : 
   13549       306726 :   if (is_poly_assign)
   13550              :     {
   13551         3318 :       tmp = trans_class_assignment (&body, expr1, expr2, &lse, &rse,
   13552         3318 :                                     use_vptr_copy || (lhs_attr.allocatable
   13553          301 :                                                       && !lhs_attr.dimension),
   13554         3062 :                                     !realloc_flag && flag_realloc_lhs
   13555         3886 :                                     && !lhs_attr.pointer);
   13556         3318 :       if (expr2->expr_type == EXPR_FUNCTION
   13557          231 :           && expr2->ts.type == BT_DERIVED
   13558           30 :           && expr2->ts.u.derived->attr.alloc_comp)
   13559              :         {
   13560           18 :           tree tmp2 = gfc_deallocate_alloc_comp (expr2->ts.u.derived,
   13561              :                                                  rse.expr, expr2->rank);
   13562           18 :           if (lss == gfc_ss_terminator)
   13563           18 :             gfc_add_expr_to_block (&rse.post, tmp2);
   13564              :           else
   13565            0 :             gfc_add_expr_to_block (&loop.post, tmp2);
   13566              :         }
   13567              : 
   13568         3318 :       expr1->must_finalize = 0;
   13569              :     }
   13570       303408 :   else if (!is_poly_assign
   13571       303408 :            && expr1->ts.type == BT_CLASS
   13572          442 :            && expr2->ts.type == BT_CLASS
   13573          255 :            && (expr2->must_finalize || dummy_class_array_copy))
   13574              :     {
   13575              :       /* This case comes about when the scalarizer provides array element
   13576              :          references to class temporaries or nonpointer dummy arrays. Use the
   13577              :          vptr copy function, since this does a deep copy of allocatable
   13578              :          components.  */
   13579          132 :       tmp = gfc_get_vptr_from_expr (rse.expr);
   13580          132 :       if (tmp == NULL_TREE && dummy_class_array_copy)
   13581           12 :         tmp = gfc_get_vptr_from_expr (gfc_get_class_from_gfc_expr (expr2));
   13582          132 :       if (tmp != NULL_TREE)
   13583              :         {
   13584          132 :           tree fcn = gfc_vptr_copy_get (tmp);
   13585          132 :           if (POINTER_TYPE_P (TREE_TYPE (fcn)))
   13586          132 :             fcn = build_fold_indirect_ref_loc (input_location, fcn);
   13587          132 :           tmp = build_call_expr_loc (input_location,
   13588              :                                      fcn, 2,
   13589              :                                      gfc_build_addr_expr (NULL, rse.expr),
   13590              :                                      gfc_build_addr_expr (NULL, lse.expr));
   13591              :         }
   13592              :     }
   13593              : 
   13594              :   /* Comply with F2018 (7.5.6.3). Make sure that any finalization code is added
   13595              :      after evaluation of the rhs and before reallocation.
   13596              :      Skip finalization for self-assignment to avoid use-after-free.
   13597              :      Strip parentheses from both sides to handle cases like a = (a).  */
   13598       306726 :   final_expr = gfc_assignment_finalizer_call (&lse, expr1, init_flag);
   13599       306726 :   if (final_expr
   13600          618 :       && gfc_dep_compare_expr (strip_parentheses (expr1),
   13601              :                                strip_parentheses (expr2)) != 0
   13602       307320 :       && !(strip_parentheses (expr2)->expr_type == EXPR_VARIABLE
   13603          199 :            && strip_parentheses (expr2)->symtree->n.sym->attr.artificial))
   13604              :     {
   13605          594 :       if (lss == gfc_ss_terminator)
   13606              :         {
   13607          165 :           gfc_add_block_to_block (&block, &rse.pre);
   13608          165 :           gfc_add_block_to_block (&block, &lse.finalblock);
   13609              :         }
   13610              :       else
   13611              :         {
   13612          429 :           gfc_add_block_to_block (&body, &rse.pre);
   13613          429 :           gfc_add_block_to_block (&loop.code[expr1->rank - 1],
   13614              :                                   &lse.finalblock);
   13615              :         }
   13616              :     }
   13617              :   else
   13618       306132 :     gfc_add_block_to_block (&body, &rse.pre);
   13619              : 
   13620       306726 :   if (flag_coarray != GFC_FCOARRAY_NONE && expr1->ts.type == BT_CHARACTER
   13621         2994 :       && assoc_assign)
   13622            0 :     tmp = gfc_trans_pointer_assignment (expr1, expr2);
   13623              : 
   13624              :   /* If nothing else works, do it the old fashioned way!  */
   13625       306726 :   if (tmp == NULL_TREE)
   13626              :     {
   13627              :       /* Strip parentheses to detect cases like a = (a) which need deep_copy.  */
   13628       303276 :       gfc_expr *expr2_stripped = strip_parentheses (expr2);
   13629       303276 :       tmp
   13630       303276 :         = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
   13631       303276 :                                    gfc_expr_is_variable (expr2_stripped)
   13632       273394 :                                      || scalar_to_array
   13633       575933 :                                      || expr2->expr_type == EXPR_ARRAY,
   13634       303276 :                                    !(l_is_temp || init_flag) && dealloc,
   13635       303276 :                                    expr1->symtree->n.sym->attr.codimension,
   13636              :                                    assoc_assign);
   13637              :     }
   13638              : 
   13639              :   /* Add the lse pre block to the body  */
   13640       306726 :   gfc_add_block_to_block (&body, &lse.pre);
   13641       306726 :   gfc_add_expr_to_block (&body, tmp);
   13642              : 
   13643              :   /* Add the post blocks to the body.  Scalar finalization must appear before
   13644              :      the post block in case any dellocations are done.  */
   13645       306726 :   if (rse.finalblock.head
   13646       306726 :       && (!l_is_temp || (expr2->expr_type == EXPR_FUNCTION
   13647           14 :                          && gfc_expr_attr (expr2).elemental)))
   13648              :     {
   13649          136 :       gfc_add_block_to_block (&body, &rse.finalblock);
   13650          136 :       gfc_add_block_to_block (&body, &rse.post);
   13651              :     }
   13652              :   else
   13653       306590 :     gfc_add_block_to_block (&body, &rse.post);
   13654              : 
   13655       306726 :   gfc_add_block_to_block (&body, &lse.post);
   13656              : 
   13657       306726 :   if (lss == gfc_ss_terminator)
   13658              :     {
   13659              :       /* F2003: Add the code for reallocation on assignment.  */
   13660       264400 :       if (flag_realloc_lhs && is_scalar_reallocatable_lhs (expr1)
   13661       270726 :           && !is_poly_assign)
   13662         3586 :         alloc_scalar_allocatable_for_assignment (&block, string_length,
   13663              :                                                  expr1, expr2);
   13664              : 
   13665              :       /* Use the scalar assignment as is.  */
   13666       267122 :       gfc_add_block_to_block (&block, &body);
   13667              :     }
   13668              :   else
   13669              :     {
   13670        39604 :       gcc_assert (lse.ss == gfc_ss_terminator
   13671              :                   && rse.ss == gfc_ss_terminator);
   13672              : 
   13673        39604 :       if (l_is_temp)
   13674              :         {
   13675         1082 :           gfc_trans_scalarized_loop_boundary (&loop, &body);
   13676              : 
   13677              :           /* We need to copy the temporary to the actual lhs.  */
   13678         1082 :           gfc_init_se (&lse, NULL);
   13679         1082 :           gfc_init_se (&rse, NULL);
   13680         1082 :           gfc_copy_loopinfo_to_se (&lse, &loop);
   13681         1082 :           gfc_copy_loopinfo_to_se (&rse, &loop);
   13682              : 
   13683         1082 :           rse.ss = loop.temp_ss;
   13684         1082 :           lse.ss = lss;
   13685              : 
   13686         1082 :           gfc_conv_tmp_array_ref (&rse);
   13687         1082 :           gfc_conv_expr (&lse, expr1);
   13688              : 
   13689         1082 :           gcc_assert (lse.ss == gfc_ss_terminator
   13690              :                       && rse.ss == gfc_ss_terminator);
   13691              : 
   13692         1082 :           if (expr2->ts.type == BT_CHARACTER)
   13693          123 :             rse.string_length = string_length;
   13694              : 
   13695         1082 :           tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
   13696              :                                          false, dealloc);
   13697         1082 :           gfc_add_expr_to_block (&body, tmp);
   13698              :         }
   13699              : 
   13700        39604 :       if (reallocation != NULL_TREE)
   13701         6320 :         gfc_add_expr_to_block (&loop.code[loop.dimen - 1], reallocation);
   13702              : 
   13703        39604 :       if (maybe_workshare)
   13704           73 :         ompws_flags &= ~OMPWS_SCALARIZER_BODY;
   13705              : 
   13706              :       /* Generate the copying loops.  */
   13707        39604 :       gfc_trans_scalarizing_loops (&loop, &body);
   13708              : 
   13709              :       /* Wrap the whole thing up.  */
   13710        39604 :       gfc_add_block_to_block (&block, &loop.pre);
   13711        39604 :       gfc_add_block_to_block (&block, &loop.post);
   13712              : 
   13713        39604 :       gfc_cleanup_loop (&loop);
   13714              :     }
   13715              : 
   13716              :   /* Since parameterized components cannot have default initializers,
   13717              :      the default PDT constructor leaves them unallocated. Do the
   13718              :      allocation now.  */
   13719       306726 :   if (init_flag && IS_PDT (expr1)
   13720          329 :       && !expr1->symtree->n.sym->attr.allocatable
   13721          329 :       && !expr1->symtree->n.sym->attr.dummy)
   13722              :     {
   13723           67 :       gfc_symbol *sym = expr1->symtree->n.sym;
   13724           67 :       tmp = gfc_allocate_pdt_comp (sym->ts.u.derived,
   13725              :                                    sym->backend_decl,
   13726           67 :                                    sym->as ? sym->as->rank : 0,
   13727           67 :                                              sym->param_list);
   13728           67 :       gfc_add_expr_to_block (&block, tmp);
   13729              :     }
   13730              : 
   13731       306726 :   return gfc_finish_block (&block);
   13732              : }
   13733              : 
   13734              : 
   13735              : /* Check whether EXPR is a copyable array.  */
   13736              : 
   13737              : static bool
   13738       971773 : copyable_array_p (gfc_expr * expr)
   13739              : {
   13740       971773 :   if (expr->expr_type != EXPR_VARIABLE)
   13741              :     return false;
   13742              : 
   13743              :   /* First check it's an array.  */
   13744       948367 :   if (expr->rank < 1 || !expr->ref || expr->ref->next)
   13745              :     return false;
   13746              : 
   13747       145623 :   if (!gfc_full_array_ref_p (expr->ref, NULL))
   13748              :     return false;
   13749              : 
   13750              :   /* Next check that it's of a simple enough type.  */
   13751       115078 :   switch (expr->ts.type)
   13752              :     {
   13753              :     case BT_INTEGER:
   13754              :     case BT_REAL:
   13755              :     case BT_COMPLEX:
   13756              :     case BT_LOGICAL:
   13757              :       return true;
   13758              : 
   13759              :     case BT_CHARACTER:
   13760              :       return false;
   13761              : 
   13762         6623 :     case_bt_struct:
   13763         6623 :       return (!expr->ts.u.derived->attr.alloc_comp
   13764         6623 :               && !expr->ts.u.derived->attr.pdt_type);
   13765              : 
   13766              :     default:
   13767              :       break;
   13768              :     }
   13769              : 
   13770              :   return false;
   13771              : }
   13772              : 
   13773              : /* Translate an assignment.  */
   13774              : 
   13775              : tree
   13776       324472 : gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
   13777              :                       bool dealloc, bool use_vptr_copy, bool may_alias)
   13778              : {
   13779       324472 :   tree tmp;
   13780              : 
   13781              :   /* Special case a single function returning an array.  */
   13782       324472 :   if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
   13783              :     {
   13784        14463 :       tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
   13785        14463 :       if (tmp)
   13786              :         return tmp;
   13787              :     }
   13788              : 
   13789              :   /* Special case assigning an array to zero.  */
   13790       317612 :   if (copyable_array_p (expr1)
   13791       317612 :       && is_zero_initializer_p (expr2))
   13792              :     {
   13793         3942 :       tmp = gfc_trans_zero_assign (expr1);
   13794         3942 :       if (tmp)
   13795              :         return tmp;
   13796              :     }
   13797              : 
   13798              :   /* Special case copying one array to another.  */
   13799       313949 :   if (copyable_array_p (expr1)
   13800        27749 :       && copyable_array_p (expr2)
   13801         2687 :       && gfc_compare_types (&expr1->ts, &expr2->ts)
   13802       316636 :       && !gfc_check_dependency (expr1, expr2, 0))
   13803              :     {
   13804         2591 :       tmp = gfc_trans_array_copy (expr1, expr2);
   13805         2591 :       if (tmp)
   13806              :         return tmp;
   13807              :     }
   13808              : 
   13809              :   /* Special case initializing an array from a constant array constructor.  */
   13810       312463 :   if (copyable_array_p (expr1)
   13811        26263 :       && expr2->expr_type == EXPR_ARRAY
   13812       320455 :       && gfc_compare_types (&expr1->ts, &expr2->ts))
   13813              :     {
   13814         7992 :       tmp = gfc_trans_array_constructor_copy (expr1, expr2);
   13815         7992 :       if (tmp)
   13816              :         return tmp;
   13817              :     }
   13818              : 
   13819       306726 :   if (UNLIMITED_POLY (expr1) && expr1->rank)
   13820       306726 :     use_vptr_copy = true;
   13821              : 
   13822              :   /* Fallback to the scalarizer to generate explicit loops.  */
   13823       306726 :   return gfc_trans_assignment_1 (expr1, expr2, init_flag, dealloc,
   13824       306726 :                                  use_vptr_copy, may_alias);
   13825              : }
   13826              : 
   13827              : tree
   13828        12876 : gfc_trans_init_assign (gfc_code * code)
   13829              : {
   13830        12876 :   return gfc_trans_assignment (code->expr1, code->expr2, true, false, true);
   13831              : }
   13832              : 
   13833              : tree
   13834       303313 : gfc_trans_assign (gfc_code * code)
   13835              : {
   13836       303313 :   return gfc_trans_assignment (code->expr1, code->expr2, false, true);
   13837              : }
   13838              : 
   13839              : /* Generate a simple loop for internal use of the form
   13840              :    for (var = begin; var <cond> end; var += step)
   13841              :       body;  */
   13842              : void
   13843        12159 : gfc_simple_for_loop (stmtblock_t *block, tree var, tree begin, tree end,
   13844              :                      enum tree_code cond, tree step, tree body)
   13845              : {
   13846        12159 :   tree tmp;
   13847              : 
   13848              :   /* var = begin. */
   13849        12159 :   gfc_add_modify (block, var, begin);
   13850              : 
   13851              :   /* Loop: for (var = begin; var <cond> end; var += step).  */
   13852        12159 :   tree label_loop = gfc_build_label_decl (NULL_TREE);
   13853        12159 :   tree label_cond = gfc_build_label_decl (NULL_TREE);
   13854        12159 :   TREE_USED (label_loop) = 1;
   13855        12159 :   TREE_USED (label_cond) = 1;
   13856              : 
   13857        12159 :   gfc_add_expr_to_block (block, build1_v (GOTO_EXPR, label_cond));
   13858        12159 :   gfc_add_expr_to_block (block, build1_v (LABEL_EXPR, label_loop));
   13859              : 
   13860              :   /* Loop body.  */
   13861        12159 :   gfc_add_expr_to_block (block, body);
   13862              : 
   13863              :   /* End of loop body.  */
   13864        12159 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (var), var, step);
   13865        12159 :   gfc_add_modify (block, var, tmp);
   13866        12159 :   gfc_add_expr_to_block (block, build1_v (LABEL_EXPR, label_cond));
   13867        12159 :   tmp = fold_build2_loc (input_location, cond, boolean_type_node, var, end);
   13868        12159 :   tmp = build3_v (COND_EXPR, tmp, build1_v (GOTO_EXPR, label_loop),
   13869              :                   build_empty_stmt (input_location));
   13870        12159 :   gfc_add_expr_to_block (block, tmp);
   13871        12159 : }
        

Generated by: LCOV version 2.4-beta

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