LCOV - code coverage report
Current view: top level - gcc/fortran - trans-expr.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.6 % 6853 6480
Test Date: 2025-04-19 15:48:17 Functions: 95.4 % 151 144
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Expression translation
       2                 :             :    Copyright (C) 2002-2025 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                 :       34447 : gfc_get_character_len (tree type)
      52                 :             : {
      53                 :       34447 :   tree len;
      54                 :             : 
      55                 :       34447 :   gcc_assert (type && TREE_CODE (type) == ARRAY_TYPE
      56                 :             :               && TYPE_STRING_FLAG (type));
      57                 :             : 
      58                 :       34447 :   len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
      59                 :       34447 :   len = (len) ? (len) : (integer_zero_node);
      60                 :       34447 :   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                 :       34447 : gfc_get_character_len_in_bytes (tree type)
      69                 :             : {
      70                 :       34447 :   tree tmp, len;
      71                 :             : 
      72                 :       34447 :   gcc_assert (type && TREE_CODE (type) == ARRAY_TYPE
      73                 :             :               && TYPE_STRING_FLAG (type));
      74                 :             : 
      75                 :       34447 :   tmp = TYPE_SIZE_UNIT (TREE_TYPE (type));
      76                 :       34447 :   tmp = (tmp && !integer_zerop (tmp))
      77                 :       68894 :     ? (fold_convert (gfc_charlen_type_node, tmp)) : (NULL_TREE);
      78                 :       34447 :   len = gfc_get_character_len (type);
      79                 :       34447 :   if (tmp && len && !integer_zerop (len))
      80                 :       33693 :     len = fold_build2_loc (input_location, MULT_EXPR,
      81                 :             :                            gfc_charlen_type_node, len, tmp);
      82                 :       34447 :   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                 :        5830 : get_scalar_to_descriptor_type (tree scalar, symbol_attribute attr)
      91                 :             : {
      92                 :        5830 :   enum gfc_array_kind akind;
      93                 :             : 
      94                 :        5830 :   if (attr.pointer)
      95                 :             :     akind = GFC_ARRAY_POINTER_CONT;
      96                 :        5480 :   else if (attr.allocatable)
      97                 :             :     akind = GFC_ARRAY_ALLOCATABLE;
      98                 :             :   else
      99                 :        4717 :     akind = GFC_ARRAY_ASSUMED_SHAPE_CONT;
     100                 :             : 
     101                 :        5830 :   if (POINTER_TYPE_P (TREE_TYPE (scalar)))
     102                 :        4943 :     scalar = TREE_TYPE (scalar);
     103                 :        5830 :   return gfc_get_array_type_bounds (TREE_TYPE (scalar), 0, 0, NULL, NULL, 1,
     104                 :        5830 :                                     akind, !(attr.pointer || attr.target));
     105                 :             : }
     106                 :             : 
     107                 :             : tree
     108                 :        5153 : gfc_conv_scalar_to_descriptor (gfc_se *se, tree scalar, symbol_attribute attr)
     109                 :             : {
     110                 :        5153 :   tree desc, type, etype;
     111                 :             : 
     112                 :        5153 :   type = get_scalar_to_descriptor_type (scalar, attr);
     113                 :        5153 :   etype = TREE_TYPE (scalar);
     114                 :        5153 :   desc = gfc_create_var (type, "desc");
     115                 :        5153 :   DECL_ARTIFICIAL (desc) = 1;
     116                 :             : 
     117                 :        5153 :   if (CONSTANT_CLASS_P (scalar))
     118                 :             :     {
     119                 :          54 :       tree tmp;
     120                 :          54 :       tmp = gfc_create_var (TREE_TYPE (scalar), "scalar");
     121                 :          54 :       gfc_add_modify (&se->pre, tmp, scalar);
     122                 :          54 :       scalar = tmp;
     123                 :             :     }
     124                 :        5153 :   if (!POINTER_TYPE_P (TREE_TYPE (scalar)))
     125                 :         887 :     scalar = gfc_build_addr_expr (NULL_TREE, scalar);
     126                 :        4266 :   else if (TREE_TYPE (etype) && TREE_CODE (TREE_TYPE (etype)) == ARRAY_TYPE)
     127                 :         111 :     etype = TREE_TYPE (etype);
     128                 :        5153 :   gfc_add_modify (&se->pre, gfc_conv_descriptor_dtype (desc),
     129                 :             :                   gfc_get_dtype_rank_type (0, etype));
     130                 :        5153 :   gfc_conv_descriptor_data_set (&se->pre, desc, scalar);
     131                 :        5153 :   gfc_conv_descriptor_span_set (&se->pre, desc,
     132                 :             :                                 gfc_conv_descriptor_elem_len (desc));
     133                 :             : 
     134                 :             :   /* Copy pointer address back - but only if it could have changed and
     135                 :             :      if the actual argument is a pointer and not, e.g., NULL().  */
     136                 :        5153 :   if ((attr.pointer || attr.allocatable) && attr.intent != INTENT_IN)
     137                 :         830 :     gfc_add_modify (&se->post, scalar,
     138                 :         415 :                     fold_convert (TREE_TYPE (scalar),
     139                 :             :                                   gfc_conv_descriptor_data_get (desc)));
     140                 :        5153 :   return desc;
     141                 :             : }
     142                 :             : 
     143                 :             : 
     144                 :             : /* Get the coarray token from the ultimate array or component ref.
     145                 :             :    Returns a NULL_TREE, when the ref object is not allocatable or pointer.  */
     146                 :             : 
     147                 :             : tree
     148                 :         396 : gfc_get_ultimate_alloc_ptr_comps_caf_token (gfc_se *outerse, gfc_expr *expr)
     149                 :             : {
     150                 :         396 :   gfc_symbol *sym = expr->symtree->n.sym;
     151                 :         792 :   bool is_coarray = sym->ts.type == BT_CLASS
     152                 :         396 :                       ? CLASS_DATA (sym)->attr.codimension
     153                 :         361 :                       : sym->attr.codimension;
     154                 :         396 :   gfc_expr *caf_expr = gfc_copy_expr (expr);
     155                 :         396 :   gfc_ref *ref = caf_expr->ref, *last_caf_ref = NULL;
     156                 :             : 
     157                 :        1339 :   while (ref)
     158                 :             :     {
     159                 :         943 :       if (ref->type == REF_COMPONENT
     160                 :         374 :           && (ref->u.c.component->attr.allocatable
     161                 :         374 :               || ref->u.c.component->attr.pointer)
     162                 :         373 :           && (is_coarray || ref->u.c.component->attr.codimension))
     163                 :         943 :           last_caf_ref = ref;
     164                 :         943 :       ref = ref->next;
     165                 :             :     }
     166                 :             : 
     167                 :         396 :   if (last_caf_ref == NULL)
     168                 :             :     {
     169                 :         103 :       gfc_free_expr (caf_expr);
     170                 :         103 :       return NULL_TREE;
     171                 :             :     }
     172                 :             : 
     173                 :         586 :   tree comp = last_caf_ref->u.c.component->caf_token
     174                 :         293 :                 ? gfc_comp_caf_token (last_caf_ref->u.c.component)
     175                 :             :                 : NULL_TREE,
     176                 :             :        caf;
     177                 :         293 :   gfc_se se;
     178                 :         293 :   bool comp_ref = !last_caf_ref->u.c.component->attr.dimension;
     179                 :         293 :   if (comp == NULL_TREE && comp_ref)
     180                 :             :     {
     181                 :          34 :       gfc_free_expr (caf_expr);
     182                 :          34 :       return NULL_TREE;
     183                 :             :     }
     184                 :         259 :   gfc_init_se (&se, outerse);
     185                 :         259 :   gfc_free_ref_list (last_caf_ref->next);
     186                 :         259 :   last_caf_ref->next = NULL;
     187                 :         259 :   caf_expr->rank = comp_ref ? 0 : last_caf_ref->u.c.component->as->rank;
     188                 :         518 :   caf_expr->corank = last_caf_ref->u.c.component->as
     189                 :         259 :                        ? last_caf_ref->u.c.component->as->corank
     190                 :             :                        : expr->corank;
     191                 :         259 :   se.want_pointer = comp_ref;
     192                 :         259 :   gfc_conv_expr (&se, caf_expr);
     193                 :         259 :   gfc_add_block_to_block (&outerse->pre, &se.pre);
     194                 :             : 
     195                 :         259 :   if (TREE_CODE (se.expr) == COMPONENT_REF && comp_ref)
     196                 :         133 :     se.expr = TREE_OPERAND (se.expr, 0);
     197                 :         259 :   gfc_free_expr (caf_expr);
     198                 :             : 
     199                 :         259 :   if (comp_ref)
     200                 :         133 :     caf = fold_build3_loc (input_location, COMPONENT_REF,
     201                 :         133 :                            TREE_TYPE (comp), se.expr, comp, NULL_TREE);
     202                 :             :   else
     203                 :         126 :     caf = gfc_conv_descriptor_token (se.expr);
     204                 :         259 :   return gfc_build_addr_expr (NULL_TREE, caf);
     205                 :             : }
     206                 :             : 
     207                 :             : 
     208                 :             : /* This is the seed for an eventual trans-class.c
     209                 :             : 
     210                 :             :    The following parameters should not be used directly since they might
     211                 :             :    in future implementations.  Use the corresponding APIs.  */
     212                 :             : #define CLASS_DATA_FIELD 0
     213                 :             : #define CLASS_VPTR_FIELD 1
     214                 :             : #define CLASS_LEN_FIELD 2
     215                 :             : #define VTABLE_HASH_FIELD 0
     216                 :             : #define VTABLE_SIZE_FIELD 1
     217                 :             : #define VTABLE_EXTENDS_FIELD 2
     218                 :             : #define VTABLE_DEF_INIT_FIELD 3
     219                 :             : #define VTABLE_COPY_FIELD 4
     220                 :             : #define VTABLE_FINAL_FIELD 5
     221                 :             : #define VTABLE_DEALLOCATE_FIELD 6
     222                 :             : 
     223                 :             : 
     224                 :             : tree
     225                 :          40 : gfc_class_set_static_fields (tree decl, tree vptr, tree data)
     226                 :             : {
     227                 :          40 :   tree tmp;
     228                 :          40 :   tree field;
     229                 :          40 :   vec<constructor_elt, va_gc> *init = NULL;
     230                 :             : 
     231                 :          40 :   field = TYPE_FIELDS (TREE_TYPE (decl));
     232                 :          40 :   tmp = gfc_advance_chain (field, CLASS_DATA_FIELD);
     233                 :          40 :   CONSTRUCTOR_APPEND_ELT (init, tmp, data);
     234                 :             : 
     235                 :          40 :   tmp = gfc_advance_chain (field, CLASS_VPTR_FIELD);
     236                 :          40 :   CONSTRUCTOR_APPEND_ELT (init, tmp, vptr);
     237                 :             : 
     238                 :          40 :   return build_constructor (TREE_TYPE (decl), init);
     239                 :             : }
     240                 :             : 
     241                 :             : 
     242                 :             : tree
     243                 :       30487 : gfc_class_data_get (tree decl)
     244                 :             : {
     245                 :       30487 :   tree data;
     246                 :       30487 :   if (POINTER_TYPE_P (TREE_TYPE (decl)))
     247                 :        5151 :     decl = build_fold_indirect_ref_loc (input_location, decl);
     248                 :       30487 :   data = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
     249                 :             :                             CLASS_DATA_FIELD);
     250                 :       30487 :   return fold_build3_loc (input_location, COMPONENT_REF,
     251                 :       30487 :                           TREE_TYPE (data), decl, data,
     252                 :       30487 :                           NULL_TREE);
     253                 :             : }
     254                 :             : 
     255                 :             : 
     256                 :             : tree
     257                 :       43213 : gfc_class_vptr_get (tree decl)
     258                 :             : {
     259                 :       43213 :   tree vptr;
     260                 :             :   /* For class arrays decl may be a temporary descriptor handle, the vptr is
     261                 :             :      then available through the saved descriptor.  */
     262                 :       26532 :   if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
     263                 :       44900 :       && GFC_DECL_SAVED_DESCRIPTOR (decl))
     264                 :        1220 :     decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
     265                 :       43213 :   if (POINTER_TYPE_P (TREE_TYPE (decl)))
     266                 :        2273 :     decl = build_fold_indirect_ref_loc (input_location, decl);
     267                 :       43213 :   vptr = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
     268                 :             :                             CLASS_VPTR_FIELD);
     269                 :       43213 :   return fold_build3_loc (input_location, COMPONENT_REF,
     270                 :       43213 :                           TREE_TYPE (vptr), decl, vptr,
     271                 :       43213 :                           NULL_TREE);
     272                 :             : }
     273                 :             : 
     274                 :             : 
     275                 :             : tree
     276                 :        6320 : gfc_class_len_get (tree decl)
     277                 :             : {
     278                 :        6320 :   tree len;
     279                 :             :   /* For class arrays decl may be a temporary descriptor handle, the len is
     280                 :             :      then available through the saved descriptor.  */
     281                 :        4496 :   if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
     282                 :        6569 :       && GFC_DECL_SAVED_DESCRIPTOR (decl))
     283                 :          85 :     decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
     284                 :        6320 :   if (POINTER_TYPE_P (TREE_TYPE (decl)))
     285                 :         656 :     decl = build_fold_indirect_ref_loc (input_location, decl);
     286                 :        6320 :   len = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
     287                 :             :                            CLASS_LEN_FIELD);
     288                 :        6320 :   return fold_build3_loc (input_location, COMPONENT_REF,
     289                 :        6320 :                           TREE_TYPE (len), decl, len,
     290                 :        6320 :                           NULL_TREE);
     291                 :             : }
     292                 :             : 
     293                 :             : 
     294                 :             : /* Try to get the _len component of a class.  When the class is not unlimited
     295                 :             :    poly, i.e. no _len field exists, then return a zero node.  */
     296                 :             : 
     297                 :             : static tree
     298                 :        4700 : gfc_class_len_or_zero_get (tree decl)
     299                 :             : {
     300                 :        4700 :   tree len;
     301                 :             :   /* For class arrays decl may be a temporary descriptor handle, the vptr is
     302                 :             :      then available through the saved descriptor.  */
     303                 :        2795 :   if (VAR_P (decl) && DECL_LANG_SPECIFIC (decl)
     304                 :        4742 :       && GFC_DECL_SAVED_DESCRIPTOR (decl))
     305                 :           0 :     decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
     306                 :        4700 :   if (POINTER_TYPE_P (TREE_TYPE (decl)))
     307                 :           0 :     decl = build_fold_indirect_ref_loc (input_location, decl);
     308                 :        4700 :   len = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (decl)),
     309                 :             :                            CLASS_LEN_FIELD);
     310                 :        6437 :   return len != NULL_TREE ? fold_build3_loc (input_location, COMPONENT_REF,
     311                 :        1737 :                                              TREE_TYPE (len), decl, len,
     312                 :             :                                              NULL_TREE)
     313                 :        2963 :     : build_zero_cst (gfc_charlen_type_node);
     314                 :             : }
     315                 :             : 
     316                 :             : 
     317                 :             : tree
     318                 :        4541 : gfc_resize_class_size_with_len (stmtblock_t * block, tree class_expr, tree size)
     319                 :             : {
     320                 :        4541 :   tree tmp;
     321                 :        4541 :   tree tmp2;
     322                 :        4541 :   tree type;
     323                 :             : 
     324                 :        4541 :   tmp = gfc_class_len_or_zero_get (class_expr);
     325                 :             : 
     326                 :             :   /* Include the len value in the element size if present.  */
     327                 :        4541 :   if (!integer_zerop (tmp))
     328                 :             :     {
     329                 :        1578 :       type = TREE_TYPE (size);
     330                 :        1578 :       if (block)
     331                 :             :         {
     332                 :         913 :           size = gfc_evaluate_now (size, block);
     333                 :         913 :           tmp = gfc_evaluate_now (fold_convert (type , tmp), block);
     334                 :             :         }
     335                 :             :       else
     336                 :         665 :         tmp = fold_convert (type , tmp);
     337                 :        1578 :       tmp2 = fold_build2_loc (input_location, MULT_EXPR,
     338                 :             :                               type, size, tmp);
     339                 :        1578 :       tmp = fold_build2_loc (input_location, GT_EXPR,
     340                 :             :                              logical_type_node, tmp,
     341                 :             :                              build_zero_cst (type));
     342                 :        1578 :       size = fold_build3_loc (input_location, COND_EXPR,
     343                 :             :                               type, tmp, tmp2, size);
     344                 :             :     }
     345                 :             :   else
     346                 :             :     return size;
     347                 :             : 
     348                 :        1578 :   if (block)
     349                 :         913 :     size = gfc_evaluate_now (size, block);
     350                 :             : 
     351                 :             :   return size;
     352                 :             : }
     353                 :             : 
     354                 :             : 
     355                 :             : /* Get the specified FIELD from the VPTR.  */
     356                 :             : 
     357                 :             : static tree
     358                 :       19794 : vptr_field_get (tree vptr, int fieldno)
     359                 :             : {
     360                 :       19794 :   tree field;
     361                 :       19794 :   vptr = build_fold_indirect_ref_loc (input_location, vptr);
     362                 :       19794 :   field = gfc_advance_chain (TYPE_FIELDS (TREE_TYPE (vptr)),
     363                 :             :                              fieldno);
     364                 :       19794 :   field = fold_build3_loc (input_location, COMPONENT_REF,
     365                 :       19794 :                            TREE_TYPE (field), vptr, field,
     366                 :             :                            NULL_TREE);
     367                 :       19794 :   gcc_assert (field);
     368                 :       19794 :   return field;
     369                 :             : }
     370                 :             : 
     371                 :             : 
     372                 :             : /* Get the field from the class' vptr.  */
     373                 :             : 
     374                 :             : static tree
     375                 :        9516 : class_vtab_field_get (tree decl, int fieldno)
     376                 :             : {
     377                 :        9516 :   tree vptr;
     378                 :        9516 :   vptr = gfc_class_vptr_get (decl);
     379                 :        9516 :   return vptr_field_get (vptr, fieldno);
     380                 :             : }
     381                 :             : 
     382                 :             : 
     383                 :             : /* Define a macro for creating the class_vtab_* and vptr_* accessors in
     384                 :             :    unison.  */
     385                 :             : #define VTAB_GET_FIELD_GEN(name, field) tree \
     386                 :             : gfc_class_vtab_## name ##_get (tree cl) \
     387                 :             : { \
     388                 :             :   return class_vtab_field_get (cl, field); \
     389                 :             : } \
     390                 :             :  \
     391                 :             : tree \
     392                 :             : gfc_vptr_## name ##_get (tree vptr) \
     393                 :             : { \
     394                 :             :   return vptr_field_get (vptr, field); \
     395                 :             : }
     396                 :             : 
     397                 :         171 : VTAB_GET_FIELD_GEN (hash, VTABLE_HASH_FIELD)
     398                 :           0 : VTAB_GET_FIELD_GEN (extends, VTABLE_EXTENDS_FIELD)
     399                 :           0 : VTAB_GET_FIELD_GEN (def_init, VTABLE_DEF_INIT_FIELD)
     400                 :        4172 : VTAB_GET_FIELD_GEN (copy, VTABLE_COPY_FIELD)
     401                 :        1731 : VTAB_GET_FIELD_GEN (final, VTABLE_FINAL_FIELD)
     402                 :         484 : VTAB_GET_FIELD_GEN (deallocate, VTABLE_DEALLOCATE_FIELD)
     403                 :             : #undef VTAB_GET_FIELD_GEN
     404                 :             : 
     405                 :             : /* The size field is returned as an array index type.  Therefore treat
     406                 :             :    it and only it specially.  */
     407                 :             : 
     408                 :             : tree
     409                 :        7588 : gfc_class_vtab_size_get (tree cl)
     410                 :             : {
     411                 :        7588 :   tree size;
     412                 :        7588 :   size = class_vtab_field_get (cl, VTABLE_SIZE_FIELD);
     413                 :             :   /* Always return size as an array index type.  */
     414                 :        7588 :   size = fold_convert (gfc_array_index_type, size);
     415                 :        7588 :   gcc_assert (size);
     416                 :        7588 :   return size;
     417                 :             : }
     418                 :             : 
     419                 :             : tree
     420                 :        5648 : gfc_vptr_size_get (tree vptr)
     421                 :             : {
     422                 :        5648 :   tree size;
     423                 :        5648 :   size = vptr_field_get (vptr, VTABLE_SIZE_FIELD);
     424                 :             :   /* Always return size as an array index type.  */
     425                 :        5648 :   size = fold_convert (gfc_array_index_type, size);
     426                 :        5648 :   gcc_assert (size);
     427                 :        5648 :   return size;
     428                 :             : }
     429                 :             : 
     430                 :             : 
     431                 :             : #undef CLASS_DATA_FIELD
     432                 :             : #undef CLASS_VPTR_FIELD
     433                 :             : #undef CLASS_LEN_FIELD
     434                 :             : #undef VTABLE_HASH_FIELD
     435                 :             : #undef VTABLE_SIZE_FIELD
     436                 :             : #undef VTABLE_EXTENDS_FIELD
     437                 :             : #undef VTABLE_DEF_INIT_FIELD
     438                 :             : #undef VTABLE_COPY_FIELD
     439                 :             : #undef VTABLE_FINAL_FIELD
     440                 :             : 
     441                 :             : 
     442                 :             : /* IF ts is null (default), search for the last _class ref in the chain
     443                 :             :    of references of the expression and cut the chain there.  Although
     444                 :             :    this routine is similiar to class.cc:gfc_add_component_ref (), there
     445                 :             :    is a significant difference: gfc_add_component_ref () concentrates
     446                 :             :    on an array ref that is the last ref in the chain and is oblivious
     447                 :             :    to the kind of refs following.
     448                 :             :    ELSE IF ts is non-null the cut is at the class entity or component
     449                 :             :    that is followed by an array reference, which is not an element.
     450                 :             :    These calls come from trans-array.cc:build_class_array_ref, which
     451                 :             :    handles scalarized class array references.*/
     452                 :             : 
     453                 :             : gfc_expr *
     454                 :        8818 : gfc_find_and_cut_at_last_class_ref (gfc_expr *e, bool is_mold,
     455                 :             :                                     gfc_typespec **ts)
     456                 :             : {
     457                 :        8818 :   gfc_expr *base_expr;
     458                 :        8818 :   gfc_ref *ref, *class_ref, *tail = NULL, *array_ref;
     459                 :             : 
     460                 :             :   /* Find the last class reference.  */
     461                 :        8818 :   class_ref = NULL;
     462                 :        8818 :   array_ref = NULL;
     463                 :             : 
     464                 :        8818 :   if (ts)
     465                 :             :     {
     466                 :         387 :       if (e->symtree
     467                 :         362 :           && e->symtree->n.sym->ts.type == BT_CLASS)
     468                 :         362 :         *ts = &e->symtree->n.sym->ts;
     469                 :             :       else
     470                 :          25 :         *ts = NULL;
     471                 :             :     }
     472                 :             : 
     473                 :       22070 :   for (ref = e->ref; ref; ref = ref->next)
     474                 :             :     {
     475                 :       13624 :       if (ts)
     476                 :             :         {
     477                 :         942 :           if (ref->type == REF_COMPONENT
     478                 :         442 :               && ref->u.c.component->ts.type == BT_CLASS
     479                 :           0 :               && ref->next && ref->next->type == REF_COMPONENT
     480                 :           0 :               && !strcmp (ref->next->u.c.component->name, "_data")
     481                 :           0 :               && ref->next->next
     482                 :           0 :               && ref->next->next->type == REF_ARRAY
     483                 :           0 :               && ref->next->next->u.ar.type != AR_ELEMENT)
     484                 :             :             {
     485                 :           0 :               *ts = &ref->u.c.component->ts;
     486                 :           0 :               class_ref = ref;
     487                 :           0 :               break;
     488                 :             :             }
     489                 :             : 
     490                 :         942 :           if (ref->next == NULL)
     491                 :             :             break;
     492                 :             :         }
     493                 :             :       else
     494                 :             :         {
     495                 :       12682 :           if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT)
     496                 :       12682 :             array_ref = ref;
     497                 :             : 
     498                 :       12682 :           if (ref->type == REF_COMPONENT
     499                 :        7715 :               && ref->u.c.component->ts.type == BT_CLASS)
     500                 :             :             {
     501                 :             :               /* Component to the right of a part reference with nonzero
     502                 :             :                  rank must not have the ALLOCATABLE attribute.  If attempts
     503                 :             :                  are made to reference such a component reference, an error
     504                 :             :                  results followed by an ICE.  */
     505                 :        1537 :               if (array_ref
     506                 :          10 :                   && CLASS_DATA (ref->u.c.component)->attr.allocatable)
     507                 :             :                 return NULL;
     508                 :             :               class_ref = ref;
     509                 :             :             }
     510                 :             :         }
     511                 :             :     }
     512                 :             : 
     513                 :        8808 :   if (ts && *ts == NULL)
     514                 :             :     return NULL;
     515                 :             : 
     516                 :             :   /* Remove and store all subsequent references after the
     517                 :             :      CLASS reference.  */
     518                 :        8783 :   if (class_ref)
     519                 :             :     {
     520                 :        1347 :       tail = class_ref->next;
     521                 :        1347 :       class_ref->next = NULL;
     522                 :             :     }
     523                 :        7436 :   else if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
     524                 :             :     {
     525                 :        7418 :       tail = e->ref;
     526                 :        7418 :       e->ref = NULL;
     527                 :             :     }
     528                 :             : 
     529                 :        8783 :   if (is_mold)
     530                 :          61 :     base_expr = gfc_expr_to_initialize (e);
     531                 :             :   else
     532                 :        8722 :     base_expr = gfc_copy_expr (e);
     533                 :             : 
     534                 :             :   /* Restore the original tail expression.  */
     535                 :        8783 :   if (class_ref)
     536                 :             :     {
     537                 :        1347 :       gfc_free_ref_list (class_ref->next);
     538                 :        1347 :       class_ref->next = tail;
     539                 :             :     }
     540                 :        7436 :   else if (e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
     541                 :             :     {
     542                 :        7418 :       gfc_free_ref_list (e->ref);
     543                 :        7418 :       e->ref = tail;
     544                 :             :     }
     545                 :             :   return base_expr;
     546                 :             : }
     547                 :             : 
     548                 :             : /* Reset the vptr to the declared type, e.g. after deallocation.
     549                 :             :    Use the variable in CLASS_CONTAINER if available.  Otherwise, recreate
     550                 :             :    one with e or class_type.  At least one of the two has to be set.  The
     551                 :             :    generated assignment code is added at the end of BLOCK.  */
     552                 :             : 
     553                 :             : void
     554                 :       10365 : gfc_reset_vptr (stmtblock_t *block, gfc_expr *e, tree class_container,
     555                 :             :                 gfc_symbol *class_type)
     556                 :             : {
     557                 :       10365 :   tree vptr = NULL_TREE;
     558                 :             : 
     559                 :       10365 :   if (class_container != NULL_TREE)
     560                 :        6099 :     vptr = gfc_get_vptr_from_expr (class_container);
     561                 :             : 
     562                 :        6099 :   if (vptr == NULL_TREE)
     563                 :             :     {
     564                 :        4273 :       gfc_se se;
     565                 :        4273 :       gcc_assert (e);
     566                 :             : 
     567                 :             :       /* Evaluate the expression and obtain the vptr from it.  */
     568                 :        4273 :       gfc_init_se (&se, NULL);
     569                 :        4273 :       if (e->rank)
     570                 :        2101 :         gfc_conv_expr_descriptor (&se, e);
     571                 :             :       else
     572                 :        2172 :         gfc_conv_expr (&se, e);
     573                 :        4273 :       gfc_add_block_to_block (block, &se.pre);
     574                 :             : 
     575                 :        4273 :       vptr = gfc_get_vptr_from_expr (se.expr);
     576                 :             :     }
     577                 :             : 
     578                 :             :   /* If a vptr is not found, we can do nothing more.  */
     579                 :        4273 :   if (vptr == NULL_TREE)
     580                 :             :     return;
     581                 :             : 
     582                 :       10355 :   if (UNLIMITED_POLY (e)
     583                 :        9409 :       || UNLIMITED_POLY (class_type)
     584                 :             :       /* When the class_type's source is not a symbol (e.g. a component's ts),
     585                 :             :          then look at the _data-components type.  */
     586                 :        1427 :       || (class_type != NULL && class_type->ts.type == BT_UNKNOWN
     587                 :        1427 :           && class_type->components && class_type->components->ts.u.derived
     588                 :        1421 :           && class_type->components->ts.u.derived->attr.unlimited_polymorphic))
     589                 :        1080 :     gfc_add_modify (block, vptr, build_int_cst (TREE_TYPE (vptr), 0));
     590                 :             :   else
     591                 :             :     {
     592                 :        9275 :       gfc_symbol *vtab, *type = nullptr;
     593                 :        9275 :       tree vtable;
     594                 :             : 
     595                 :        9275 :       if (e)
     596                 :        7982 :         type = e->ts.u.derived;
     597                 :        1293 :       else if (class_type)
     598                 :             :         {
     599                 :        1293 :           if (class_type->ts.type == BT_CLASS)
     600                 :           0 :             type = CLASS_DATA (class_type)->ts.u.derived;
     601                 :             :           else
     602                 :             :             type = class_type;
     603                 :             :         }
     604                 :        7982 :       gcc_assert (type);
     605                 :             :       /* Return the vptr to the address of the declared type.  */
     606                 :        9275 :       vtab = gfc_find_derived_vtab (type);
     607                 :        9275 :       vtable = vtab->backend_decl;
     608                 :        9275 :       if (vtable == NULL_TREE)
     609                 :          70 :         vtable = gfc_get_symbol_decl (vtab);
     610                 :        9275 :       vtable = gfc_build_addr_expr (NULL, vtable);
     611                 :        9275 :       vtable = fold_convert (TREE_TYPE (vptr), vtable);
     612                 :        9275 :       gfc_add_modify (block, vptr, vtable);
     613                 :             :     }
     614                 :             : }
     615                 :             : 
     616                 :             : /* Set the vptr of a class in to from the type given in from.  If from is NULL,
     617                 :             :    then reset the vptr to the default or to.  */
     618                 :             : 
     619                 :             : void
     620                 :         216 : gfc_class_set_vptr (stmtblock_t *block, tree to, tree from)
     621                 :             : {
     622                 :         216 :   tree tmp, vptr_ref;
     623                 :         216 :   gfc_symbol *type;
     624                 :             : 
     625                 :         216 :   vptr_ref = gfc_get_vptr_from_expr (to);
     626                 :         252 :   if (POINTER_TYPE_P (TREE_TYPE (from))
     627                 :         216 :       && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (from))))
     628                 :             :     {
     629                 :          44 :       gfc_add_modify (block, vptr_ref,
     630                 :          22 :                       fold_convert (TREE_TYPE (vptr_ref),
     631                 :             :                                     gfc_get_vptr_from_expr (from)));
     632                 :         238 :       return;
     633                 :             :     }
     634                 :         194 :   tmp = gfc_get_vptr_from_expr (from);
     635                 :         194 :   if (tmp)
     636                 :             :     {
     637                 :         158 :       gfc_add_modify (block, vptr_ref,
     638                 :         158 :                       fold_convert (TREE_TYPE (vptr_ref), tmp));
     639                 :         158 :       return;
     640                 :             :     }
     641                 :          36 :   if (VAR_P (from)
     642                 :          36 :       && strncmp (IDENTIFIER_POINTER (DECL_NAME (from)), "__vtab", 6) == 0)
     643                 :             :     {
     644                 :          36 :       gfc_add_modify (block, vptr_ref,
     645                 :          36 :                       gfc_build_addr_expr (TREE_TYPE (vptr_ref), from));
     646                 :          36 :       return;
     647                 :             :     }
     648                 :           0 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (from)))
     649                 :           0 :       && GFC_CLASS_TYPE_P (
     650                 :             :         TREE_TYPE (TREE_OPERAND (TREE_OPERAND (from, 0), 0))))
     651                 :             :     {
     652                 :           0 :       gfc_add_modify (block, vptr_ref,
     653                 :           0 :                       fold_convert (TREE_TYPE (vptr_ref),
     654                 :             :                                     gfc_get_vptr_from_expr (TREE_OPERAND (
     655                 :             :                                       TREE_OPERAND (from, 0), 0))));
     656                 :           0 :       return;
     657                 :             :     }
     658                 :             : 
     659                 :             :   /* If nothing of the above matches, set the vtype according to the type.  */
     660                 :           0 :   tmp = TREE_TYPE (from);
     661                 :           0 :   if (POINTER_TYPE_P (tmp))
     662                 :           0 :     tmp = TREE_TYPE (tmp);
     663                 :           0 :   gfc_find_symbol (IDENTIFIER_POINTER (TYPE_NAME (tmp)), gfc_current_ns, 1,
     664                 :             :                    &type);
     665                 :           0 :   tmp = gfc_find_derived_vtab (type)->backend_decl;
     666                 :           0 :   gcc_assert (tmp);
     667                 :           0 :   gfc_add_modify (block, vptr_ref,
     668                 :           0 :                   gfc_build_addr_expr (TREE_TYPE (vptr_ref), tmp));
     669                 :             : }
     670                 :             : 
     671                 :             : /* Reset the len for unlimited polymorphic objects.  */
     672                 :             : 
     673                 :             : void
     674                 :         563 : gfc_reset_len (stmtblock_t *block, gfc_expr *expr)
     675                 :             : {
     676                 :         563 :   gfc_expr *e;
     677                 :         563 :   gfc_se se_len;
     678                 :         563 :   e = gfc_find_and_cut_at_last_class_ref (expr);
     679                 :         563 :   if (e == NULL)
     680                 :           0 :     return;
     681                 :         563 :   gfc_add_len_component (e);
     682                 :         563 :   gfc_init_se (&se_len, NULL);
     683                 :         563 :   gfc_conv_expr (&se_len, e);
     684                 :         563 :   gfc_add_modify (block, se_len.expr,
     685                 :         563 :                   fold_convert (TREE_TYPE (se_len.expr), integer_zero_node));
     686                 :         563 :   gfc_free_expr (e);
     687                 :             : }
     688                 :             : 
     689                 :             : 
     690                 :             : /* Obtain the last class reference in a gfc_expr. Return NULL_TREE if no class
     691                 :             :    reference is found. Note that it is up to the caller to avoid using this
     692                 :             :    for expressions other than variables.  */
     693                 :             : 
     694                 :             : tree
     695                 :        1271 : gfc_get_class_from_gfc_expr (gfc_expr *e)
     696                 :             : {
     697                 :        1271 :   gfc_expr *class_expr;
     698                 :        1271 :   gfc_se cse;
     699                 :        1271 :   class_expr = gfc_find_and_cut_at_last_class_ref (e);
     700                 :        1271 :   if (class_expr == NULL)
     701                 :             :     return NULL_TREE;
     702                 :        1271 :   gfc_init_se (&cse, NULL);
     703                 :        1271 :   gfc_conv_expr (&cse, class_expr);
     704                 :        1271 :   gfc_free_expr (class_expr);
     705                 :        1271 :   return cse.expr;
     706                 :             : }
     707                 :             : 
     708                 :             : 
     709                 :             : /* Obtain the last class reference in an expression.
     710                 :             :    Return NULL_TREE if no class reference is found.  */
     711                 :             : 
     712                 :             : tree
     713                 :      100414 : gfc_get_class_from_expr (tree expr)
     714                 :             : {
     715                 :      100414 :   tree tmp;
     716                 :      100414 :   tree type;
     717                 :             : 
     718                 :      260877 :   for (tmp = expr; tmp; tmp = TREE_OPERAND (tmp, 0))
     719                 :             :     {
     720                 :      260877 :       if (CONSTANT_CLASS_P (tmp))
     721                 :             :         return NULL_TREE;
     722                 :             : 
     723                 :      260846 :       type = TREE_TYPE (tmp);
     724                 :      303302 :       while (type)
     725                 :             :         {
     726                 :      295732 :           if (GFC_CLASS_TYPE_P (type))
     727                 :             :             return tmp;
     728                 :      277073 :           if (type != TYPE_CANONICAL (type))
     729                 :       42456 :             type = TYPE_CANONICAL (type);
     730                 :             :           else
     731                 :             :             type = NULL_TREE;
     732                 :             :         }
     733                 :      242187 :       if (VAR_P (tmp) || TREE_CODE (tmp) == PARM_DECL)
     734                 :             :         break;
     735                 :             :     }
     736                 :             : 
     737                 :       81724 :   if (POINTER_TYPE_P (TREE_TYPE (tmp)))
     738                 :       53910 :     tmp = build_fold_indirect_ref_loc (input_location, tmp);
     739                 :             : 
     740                 :       81724 :   if (GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
     741                 :             :     return tmp;
     742                 :             : 
     743                 :             :   return NULL_TREE;
     744                 :             : }
     745                 :             : 
     746                 :             : 
     747                 :             : /* Obtain the vptr of the last class reference in an expression.
     748                 :             :    Return NULL_TREE if no class reference is found.  */
     749                 :             : 
     750                 :             : tree
     751                 :       10972 : gfc_get_vptr_from_expr (tree expr)
     752                 :             : {
     753                 :       10972 :   tree tmp;
     754                 :             : 
     755                 :       10972 :   tmp = gfc_get_class_from_expr (expr);
     756                 :             : 
     757                 :       10972 :   if (tmp != NULL_TREE)
     758                 :       10919 :     return gfc_class_vptr_get (tmp);
     759                 :             : 
     760                 :             :   return NULL_TREE;
     761                 :             : }
     762                 :             : 
     763                 :             : void
     764                 :        1988 : gfc_class_array_data_assign (stmtblock_t *block, tree lhs_desc, tree rhs_desc,
     765                 :             :                              bool lhs_type)
     766                 :             : {
     767                 :        1988 :   tree tmp, tmp2, type;
     768                 :             : 
     769                 :        1988 :   gfc_conv_descriptor_data_set (block, lhs_desc,
     770                 :             :                                 gfc_conv_descriptor_data_get (rhs_desc));
     771                 :        1988 :   gfc_conv_descriptor_offset_set (block, lhs_desc,
     772                 :             :                                   gfc_conv_descriptor_offset_get (rhs_desc));
     773                 :             : 
     774                 :        1988 :   gfc_add_modify (block, gfc_conv_descriptor_dtype (lhs_desc),
     775                 :             :                   gfc_conv_descriptor_dtype (rhs_desc));
     776                 :             : 
     777                 :             :   /* Assign the dimension as range-ref.  */
     778                 :        1988 :   tmp = gfc_get_descriptor_dimension (lhs_desc);
     779                 :        1988 :   tmp2 = gfc_get_descriptor_dimension (rhs_desc);
     780                 :             : 
     781                 :        1988 :   type = lhs_type ? TREE_TYPE (tmp) : TREE_TYPE (tmp2);
     782                 :        1988 :   tmp = build4_loc (input_location, ARRAY_RANGE_REF, type, tmp,
     783                 :             :                     gfc_index_zero_node, NULL_TREE, NULL_TREE);
     784                 :        1988 :   tmp2 = build4_loc (input_location, ARRAY_RANGE_REF, type, tmp2,
     785                 :             :                      gfc_index_zero_node, NULL_TREE, NULL_TREE);
     786                 :        1988 :   gfc_add_modify (block, tmp, tmp2);
     787                 :        1988 : }
     788                 :             : 
     789                 :             : /* Takes a derived type expression and returns the address of a temporary
     790                 :             :    class object of the 'declared' type.  If opt_vptr_src is not NULL, this is
     791                 :             :    used for the temporary class object.
     792                 :             :    optional_alloc_ptr is false when the dummy is neither allocatable
     793                 :             :    nor a pointer; that's only relevant for the optional handling.
     794                 :             :    The optional argument 'derived_array' is used to preserve the parmse
     795                 :             :    expression for deallocation of allocatable components. Assumed rank
     796                 :             :    formal arguments made this necessary.  */
     797                 :             : void
     798                 :        4678 : gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym,
     799                 :             :                            tree opt_vptr_src, bool optional,
     800                 :             :                            bool optional_alloc_ptr, const char *proc_name,
     801                 :             :                            tree *derived_array)
     802                 :             : {
     803                 :        4678 :   tree cond_optional = NULL_TREE;
     804                 :        4678 :   gfc_ss *ss;
     805                 :        4678 :   tree ctree;
     806                 :        4678 :   tree var;
     807                 :        4678 :   tree tmp;
     808                 :        4678 :   tree packed = NULL_TREE;
     809                 :             : 
     810                 :             :   /* The derived type needs to be converted to a temporary CLASS object.  */
     811                 :        4678 :   tmp = gfc_typenode_for_spec (&fsym->ts);
     812                 :        4678 :   var = gfc_create_var (tmp, "class");
     813                 :             : 
     814                 :             :   /* Set the vptr.  */
     815                 :        4678 :   if (opt_vptr_src)
     816                 :         116 :     gfc_class_set_vptr (&parmse->pre, var, opt_vptr_src);
     817                 :             :   else
     818                 :        4562 :     gfc_reset_vptr (&parmse->pre, e, var);
     819                 :             : 
     820                 :             :   /* Now set the data field.  */
     821                 :        4678 :   ctree = gfc_class_data_get (var);
     822                 :             : 
     823                 :        4678 :   if (flag_coarray == GFC_FCOARRAY_LIB && CLASS_DATA (fsym)->attr.codimension)
     824                 :             :     {
     825                 :           2 :       tree token;
     826                 :           2 :       tmp = gfc_get_tree_for_caf_expr (e);
     827                 :           2 :       if (POINTER_TYPE_P (TREE_TYPE (tmp)))
     828                 :           1 :         tmp = build_fold_indirect_ref (tmp);
     829                 :           2 :       gfc_get_caf_token_offset (parmse, &token, nullptr, tmp, NULL_TREE, e);
     830                 :           2 :       gfc_add_modify (&parmse->pre, gfc_conv_descriptor_token (ctree), token);
     831                 :             :     }
     832                 :             : 
     833                 :        4678 :   if (optional)
     834                 :         576 :     cond_optional = gfc_conv_expr_present (e->symtree->n.sym);
     835                 :             : 
     836                 :             :   /* Set the _len as early as possible.  */
     837                 :        4678 :   if (fsym->ts.u.derived->components->ts.type == BT_DERIVED
     838                 :        4678 :       && fsym->ts.u.derived->components->ts.u.derived->attr
     839                 :        4678 :            .unlimited_polymorphic)
     840                 :             :     {
     841                 :             :       /* Take care about initializing the _len component correctly.  */
     842                 :         374 :       tree len_tree = gfc_class_len_get (var);
     843                 :         374 :       if (UNLIMITED_POLY (e))
     844                 :             :         {
     845                 :          12 :           gfc_expr *len;
     846                 :          12 :           gfc_se se;
     847                 :             : 
     848                 :          12 :           len = gfc_find_and_cut_at_last_class_ref (e);
     849                 :          12 :           gfc_add_len_component (len);
     850                 :          12 :           gfc_init_se (&se, NULL);
     851                 :          12 :           gfc_conv_expr (&se, len);
     852                 :          12 :           if (optional)
     853                 :           0 :             tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se.expr),
     854                 :             :                               cond_optional, se.expr,
     855                 :           0 :                               fold_convert (TREE_TYPE (se.expr),
     856                 :             :                                             integer_zero_node));
     857                 :             :           else
     858                 :          12 :             tmp = se.expr;
     859                 :          12 :           gfc_free_expr (len);
     860                 :          12 :         }
     861                 :             :       else
     862                 :         362 :         tmp = integer_zero_node;
     863                 :         374 :       gfc_add_modify (&parmse->pre, len_tree,
     864                 :         374 :                       fold_convert (TREE_TYPE (len_tree), tmp));
     865                 :             :     }
     866                 :             : 
     867                 :        4678 :   if (parmse->expr && POINTER_TYPE_P (TREE_TYPE (parmse->expr)))
     868                 :             :     {
     869                 :             :       /* If there is a ready made pointer to a derived type, use it
     870                 :             :          rather than evaluating the expression again.  */
     871                 :         498 :       tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
     872                 :         498 :       gfc_add_modify (&parmse->pre, ctree, tmp);
     873                 :             :     }
     874                 :        4180 :   else if (parmse->ss && parmse->ss->info && parmse->ss->info->useflags)
     875                 :             :     {
     876                 :             :       /* For an array reference in an elemental procedure call we need
     877                 :             :          to retain the ss to provide the scalarized array reference.  */
     878                 :         252 :       gfc_conv_expr_reference (parmse, e);
     879                 :         252 :       tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
     880                 :         252 :       if (optional)
     881                 :           0 :         tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
     882                 :             :                           cond_optional, tmp,
     883                 :           0 :                           fold_convert (TREE_TYPE (tmp), null_pointer_node));
     884                 :         252 :       gfc_add_modify (&parmse->pre, ctree, tmp);
     885                 :             :     }
     886                 :             :   else
     887                 :             :     {
     888                 :        3928 :       ss = gfc_walk_expr (e);
     889                 :        3928 :       if (ss == gfc_ss_terminator)
     890                 :             :         {
     891                 :        2717 :           parmse->ss = NULL;
     892                 :        2717 :           gfc_conv_expr_reference (parmse, e);
     893                 :             : 
     894                 :             :           /* Scalar to an assumed-rank array.  */
     895                 :        2717 :           if (fsym->ts.u.derived->components->as)
     896                 :             :             {
     897                 :         321 :               tree type;
     898                 :         321 :               type = get_scalar_to_descriptor_type (parmse->expr,
     899                 :             :                                                     gfc_expr_attr (e));
     900                 :         321 :               gfc_add_modify (&parmse->pre, gfc_conv_descriptor_dtype (ctree),
     901                 :             :                               gfc_get_dtype (type));
     902                 :         321 :               if (optional)
     903                 :         192 :                 parmse->expr = build3_loc (input_location, COND_EXPR,
     904                 :          96 :                                            TREE_TYPE (parmse->expr),
     905                 :             :                                            cond_optional, parmse->expr,
     906                 :          96 :                                            fold_convert (TREE_TYPE (parmse->expr),
     907                 :             :                                                          null_pointer_node));
     908                 :         321 :               gfc_conv_descriptor_data_set (&parmse->pre, ctree, parmse->expr);
     909                 :             :             }
     910                 :             :           else
     911                 :             :             {
     912                 :        2396 :               tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
     913                 :        2396 :               if (optional)
     914                 :         132 :                 tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
     915                 :             :                                   cond_optional, tmp,
     916                 :         132 :                                   fold_convert (TREE_TYPE (tmp),
     917                 :             :                                                 null_pointer_node));
     918                 :        2396 :               gfc_add_modify (&parmse->pre, ctree, tmp);
     919                 :             :             }
     920                 :             :         }
     921                 :             :       else
     922                 :             :         {
     923                 :        1211 :           stmtblock_t block;
     924                 :        1211 :           gfc_init_block (&block);
     925                 :        1211 :           gfc_ref *ref;
     926                 :        1211 :           int dim;
     927                 :        1211 :           tree lbshift = NULL_TREE;
     928                 :             : 
     929                 :             :           /* Array refs with sections indicate, that a for a formal argument
     930                 :             :              expecting contiguous repacking needs to be done.  */
     931                 :        2271 :           for (ref = e->ref; ref; ref = ref->next)
     932                 :        1210 :             if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
     933                 :             :               break;
     934                 :        1211 :           if (IS_CLASS_ARRAY (fsym)
     935                 :        1103 :               && (CLASS_DATA (fsym)->as->type == AS_EXPLICIT
     936                 :         845 :                   || CLASS_DATA (fsym)->as->type == AS_ASSUMED_SIZE)
     937                 :         354 :               && (ref || e->rank != fsym->ts.u.derived->components->as->rank))
     938                 :         144 :             fsym->attr.contiguous = 1;
     939                 :             : 
     940                 :             :           /* Detect any array references with vector subscripts.  */
     941                 :        2415 :           for (ref = e->ref; ref; ref = ref->next)
     942                 :        1210 :             if (ref->type == REF_ARRAY && ref->u.ar.type != AR_ELEMENT
     943                 :        1168 :                 && ref->u.ar.type != AR_FULL)
     944                 :             :               {
     945                 :         336 :                 for (dim = 0; dim < ref->u.ar.dimen; dim++)
     946                 :         192 :                   if (ref->u.ar.dimen_type[dim] == DIMEN_VECTOR)
     947                 :             :                     break;
     948                 :         150 :                 if (dim < ref->u.ar.dimen)
     949                 :             :                   break;
     950                 :             :               }
     951                 :             :           /* Array references with vector subscripts and non-variable
     952                 :             :              expressions need be converted to a one-based descriptor.  */
     953                 :        1211 :           if (ref || e->expr_type != EXPR_VARIABLE)
     954                 :          49 :             lbshift = gfc_index_one_node;
     955                 :             : 
     956                 :        1211 :           parmse->expr = var;
     957                 :        1211 :           gfc_conv_array_parameter (parmse, e, false, fsym, proc_name, nullptr,
     958                 :             :                                     &lbshift, &packed);
     959                 :             : 
     960                 :        1211 :           if (derived_array && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (parmse->expr)))
     961                 :             :             {
     962                 :        1115 :               *derived_array
     963                 :        1115 :                 = gfc_create_var (TREE_TYPE (parmse->expr), "array");
     964                 :        1115 :               gfc_add_modify (&block, *derived_array, parmse->expr);
     965                 :             :             }
     966                 :             : 
     967                 :        1211 :           if (optional)
     968                 :             :             {
     969                 :         348 :               tmp = gfc_finish_block (&block);
     970                 :             : 
     971                 :         348 :               gfc_init_block (&block);
     972                 :         348 :               gfc_conv_descriptor_data_set (&block, ctree, null_pointer_node);
     973                 :         348 :               if (derived_array && *derived_array != NULL_TREE)
     974                 :         348 :                 gfc_conv_descriptor_data_set (&block, *derived_array,
     975                 :             :                                               null_pointer_node);
     976                 :             : 
     977                 :         348 :               tmp = build3_v (COND_EXPR, cond_optional, tmp,
     978                 :             :                               gfc_finish_block (&block));
     979                 :         348 :               gfc_add_expr_to_block (&parmse->pre, tmp);
     980                 :             :             }
     981                 :             :           else
     982                 :         863 :             gfc_add_block_to_block (&parmse->pre, &block);
     983                 :             :         }
     984                 :             :     }
     985                 :             : 
     986                 :             :   /* Pass the address of the class object.  */
     987                 :        4678 :   if (packed)
     988                 :          96 :     parmse->expr = packed;
     989                 :             :   else
     990                 :        4582 :     parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
     991                 :             : 
     992                 :        4678 :   if (optional && optional_alloc_ptr)
     993                 :          84 :     parmse->expr
     994                 :          84 :       = build3_loc (input_location, COND_EXPR, TREE_TYPE (parmse->expr),
     995                 :             :                     cond_optional, parmse->expr,
     996                 :          84 :                     fold_convert (TREE_TYPE (parmse->expr), null_pointer_node));
     997                 :        4678 : }
     998                 :             : 
     999                 :             : /* Create a new class container, which is required as scalar coarrays
    1000                 :             :    have an array descriptor while normal scalars haven't. Optionally,
    1001                 :             :    NULL pointer checks are added if the argument is OPTIONAL.  */
    1002                 :             : 
    1003                 :             : static void
    1004                 :          48 : class_scalar_coarray_to_class (gfc_se *parmse, gfc_expr *e,
    1005                 :             :                                gfc_typespec class_ts, bool optional)
    1006                 :             : {
    1007                 :          48 :   tree var, ctree, tmp;
    1008                 :          48 :   stmtblock_t block;
    1009                 :          48 :   gfc_ref *ref;
    1010                 :          48 :   gfc_ref *class_ref;
    1011                 :             : 
    1012                 :          48 :   gfc_init_block (&block);
    1013                 :             : 
    1014                 :          48 :   class_ref = NULL;
    1015                 :         144 :   for (ref = e->ref; ref; ref = ref->next)
    1016                 :             :     {
    1017                 :          96 :       if (ref->type == REF_COMPONENT
    1018                 :          48 :             && ref->u.c.component->ts.type == BT_CLASS)
    1019                 :          96 :         class_ref = ref;
    1020                 :             :     }
    1021                 :             : 
    1022                 :          48 :   if (class_ref == NULL
    1023                 :          48 :         && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
    1024                 :          48 :     tmp = e->symtree->n.sym->backend_decl;
    1025                 :             :   else
    1026                 :             :     {
    1027                 :             :       /* Remove everything after the last class reference, convert the
    1028                 :             :          expression and then recover its tailend once more.  */
    1029                 :           0 :       gfc_se tmpse;
    1030                 :           0 :       ref = class_ref->next;
    1031                 :           0 :       class_ref->next = NULL;
    1032                 :           0 :       gfc_init_se (&tmpse, NULL);
    1033                 :           0 :       gfc_conv_expr (&tmpse, e);
    1034                 :           0 :       class_ref->next = ref;
    1035                 :           0 :       tmp = tmpse.expr;
    1036                 :             :     }
    1037                 :             : 
    1038                 :          48 :   var = gfc_typenode_for_spec (&class_ts);
    1039                 :          48 :   var = gfc_create_var (var, "class");
    1040                 :             : 
    1041                 :          48 :   ctree = gfc_class_vptr_get (var);
    1042                 :          96 :   gfc_add_modify (&block, ctree,
    1043                 :          48 :                   fold_convert (TREE_TYPE (ctree), gfc_class_vptr_get (tmp)));
    1044                 :             : 
    1045                 :          48 :   ctree = gfc_class_data_get (var);
    1046                 :          48 :   tmp = gfc_conv_descriptor_data_get (
    1047                 :          48 :     gfc_class_data_get (GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (tmp)))
    1048                 :             :                           ? tmp
    1049                 :          24 :                           : GFC_DECL_SAVED_DESCRIPTOR (tmp)));
    1050                 :          48 :   gfc_add_modify (&block, ctree, fold_convert (TREE_TYPE (ctree), tmp));
    1051                 :             : 
    1052                 :             :   /* Pass the address of the class object.  */
    1053                 :          48 :   parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
    1054                 :             : 
    1055                 :          48 :   if (optional)
    1056                 :             :     {
    1057                 :          48 :       tree cond = gfc_conv_expr_present (e->symtree->n.sym);
    1058                 :          48 :       tree tmp2;
    1059                 :             : 
    1060                 :          48 :       tmp = gfc_finish_block (&block);
    1061                 :             : 
    1062                 :          48 :       gfc_init_block (&block);
    1063                 :          48 :       tmp2 = gfc_class_data_get (var);
    1064                 :          48 :       gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
    1065                 :             :                                                   null_pointer_node));
    1066                 :          48 :       tmp2 = gfc_finish_block (&block);
    1067                 :             : 
    1068                 :          48 :       tmp = build3_loc (input_location, COND_EXPR, void_type_node,
    1069                 :             :                         cond, tmp, tmp2);
    1070                 :          48 :       gfc_add_expr_to_block (&parmse->pre, tmp);
    1071                 :             :     }
    1072                 :             :   else
    1073                 :           0 :     gfc_add_block_to_block (&parmse->pre, &block);
    1074                 :          48 : }
    1075                 :             : 
    1076                 :             : 
    1077                 :             : /* Takes an intrinsic type expression and returns the address of a temporary
    1078                 :             :    class object of the 'declared' type.  */
    1079                 :             : void
    1080                 :         858 : gfc_conv_intrinsic_to_class (gfc_se *parmse, gfc_expr *e,
    1081                 :             :                              gfc_typespec class_ts)
    1082                 :             : {
    1083                 :         858 :   gfc_symbol *vtab;
    1084                 :         858 :   gfc_ss *ss;
    1085                 :         858 :   tree ctree;
    1086                 :         858 :   tree var;
    1087                 :         858 :   tree tmp;
    1088                 :         858 :   int dim;
    1089                 :         858 :   bool unlimited_poly;
    1090                 :             : 
    1091                 :        1716 :   unlimited_poly = class_ts.type == BT_CLASS
    1092                 :         858 :                    && class_ts.u.derived->components->ts.type == BT_DERIVED
    1093                 :         858 :                    && class_ts.u.derived->components->ts.u.derived
    1094                 :         858 :                                                 ->attr.unlimited_polymorphic;
    1095                 :             : 
    1096                 :             :   /* The intrinsic type needs to be converted to a temporary
    1097                 :             :      CLASS object.  */
    1098                 :         858 :   tmp = gfc_typenode_for_spec (&class_ts);
    1099                 :         858 :   var = gfc_create_var (tmp, "class");
    1100                 :             : 
    1101                 :             :   /* Force a temporary for component or substring references.  */
    1102                 :         858 :   if (unlimited_poly
    1103                 :         858 :       && class_ts.u.derived->components->attr.dimension
    1104                 :             :       && !class_ts.u.derived->components->attr.allocatable
    1105                 :         858 :       && !class_ts.u.derived->components->attr.class_pointer
    1106                 :        1463 :       && is_subref_array (e))
    1107                 :          17 :     parmse->force_tmp = 1;
    1108                 :             : 
    1109                 :             :   /* Set the vptr.  */
    1110                 :         858 :   ctree = gfc_class_vptr_get (var);
    1111                 :             : 
    1112                 :         858 :   vtab = gfc_find_vtab (&e->ts);
    1113                 :         858 :   gcc_assert (vtab);
    1114                 :         858 :   tmp = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtab));
    1115                 :         858 :   gfc_add_modify (&parmse->pre, ctree,
    1116                 :         858 :                   fold_convert (TREE_TYPE (ctree), tmp));
    1117                 :             : 
    1118                 :             :   /* Now set the data field.  */
    1119                 :         858 :   ctree = gfc_class_data_get (var);
    1120                 :         858 :   if (parmse->ss && parmse->ss->info->useflags)
    1121                 :             :     {
    1122                 :             :       /* For an array reference in an elemental procedure call we need
    1123                 :             :          to retain the ss to provide the scalarized array reference.  */
    1124                 :          36 :       gfc_conv_expr_reference (parmse, e);
    1125                 :          36 :       tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
    1126                 :          36 :       gfc_add_modify (&parmse->pre, ctree, tmp);
    1127                 :             :     }
    1128                 :             :   else
    1129                 :             :     {
    1130                 :         822 :       ss = gfc_walk_expr (e);
    1131                 :         822 :       if (ss == gfc_ss_terminator)
    1132                 :             :         {
    1133                 :         241 :           parmse->ss = NULL;
    1134                 :         241 :           gfc_conv_expr_reference (parmse, e);
    1135                 :         241 :           if (class_ts.u.derived->components->as
    1136                 :          24 :               && class_ts.u.derived->components->as->type == AS_ASSUMED_RANK)
    1137                 :             :             {
    1138                 :          24 :               tmp = gfc_conv_scalar_to_descriptor (parmse, parmse->expr,
    1139                 :             :                                                    gfc_expr_attr (e));
    1140                 :          24 :               tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
    1141                 :          24 :                                      TREE_TYPE (ctree), tmp);
    1142                 :             :             }
    1143                 :             :           else
    1144                 :         217 :               tmp = fold_convert (TREE_TYPE (ctree), parmse->expr);
    1145                 :         241 :           gfc_add_modify (&parmse->pre, ctree, tmp);
    1146                 :             :         }
    1147                 :             :       else
    1148                 :             :         {
    1149                 :         581 :           parmse->ss = ss;
    1150                 :         581 :           parmse->use_offset = 1;
    1151                 :         581 :           gfc_conv_expr_descriptor (parmse, e);
    1152                 :             : 
    1153                 :             :           /* Array references with vector subscripts and non-variable expressions
    1154                 :             :              need be converted to a one-based descriptor.  */
    1155                 :         581 :           if (e->expr_type != EXPR_VARIABLE)
    1156                 :             :             {
    1157                 :         368 :               for (dim = 0; dim < e->rank; ++dim)
    1158                 :         193 :                 gfc_conv_shift_descriptor_lbound (&parmse->pre, parmse->expr,
    1159                 :             :                                                   dim, gfc_index_one_node);
    1160                 :             :             }
    1161                 :             : 
    1162                 :         581 :           if (class_ts.u.derived->components->as->rank != e->rank)
    1163                 :             :             {
    1164                 :          49 :               tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
    1165                 :          49 :                                      TREE_TYPE (ctree), parmse->expr);
    1166                 :          49 :               gfc_add_modify (&parmse->pre, ctree, tmp);
    1167                 :             :             }
    1168                 :             :           else
    1169                 :         532 :             gfc_add_modify (&parmse->pre, ctree, parmse->expr);
    1170                 :             :         }
    1171                 :             :     }
    1172                 :             : 
    1173                 :         858 :   gcc_assert (class_ts.type == BT_CLASS);
    1174                 :         858 :   if (unlimited_poly)
    1175                 :             :     {
    1176                 :         858 :       ctree = gfc_class_len_get (var);
    1177                 :             :       /* When the actual arg is a char array, then set the _len component of the
    1178                 :             :          unlimited polymorphic entity to the length of the string.  */
    1179                 :         858 :       if (e->ts.type == BT_CHARACTER)
    1180                 :             :         {
    1181                 :             :           /* Start with parmse->string_length because this seems to be set to a
    1182                 :             :            correct value more often.  */
    1183                 :         175 :           if (parmse->string_length)
    1184                 :             :             tmp = parmse->string_length;
    1185                 :             :           /* When the string_length is not yet set, then try the backend_decl of
    1186                 :             :            the cl.  */
    1187                 :           0 :           else if (e->ts.u.cl->backend_decl)
    1188                 :             :             tmp = e->ts.u.cl->backend_decl;
    1189                 :             :           /* If both of the above approaches fail, then try to generate an
    1190                 :             :            expression from the input, which is only feasible currently, when the
    1191                 :             :            expression can be evaluated to a constant one.  */
    1192                 :             :           else
    1193                 :             :             {
    1194                 :             :               /* Try to simplify the expression.  */
    1195                 :           0 :               gfc_simplify_expr (e, 0);
    1196                 :           0 :               if (e->expr_type == EXPR_CONSTANT && !e->ts.u.cl->resolved)
    1197                 :             :                 {
    1198                 :             :                   /* Amazingly all data is present to compute the length of a
    1199                 :             :                    constant string, but the expression is not yet there.  */
    1200                 :           0 :                   e->ts.u.cl->length = gfc_get_constant_expr (BT_INTEGER,
    1201                 :             :                                                               gfc_charlen_int_kind,
    1202                 :             :                                                               &e->where);
    1203                 :           0 :                   mpz_set_ui (e->ts.u.cl->length->value.integer,
    1204                 :           0 :                               e->value.character.length);
    1205                 :           0 :                   gfc_conv_const_charlen (e->ts.u.cl);
    1206                 :           0 :                   e->ts.u.cl->resolved = 1;
    1207                 :           0 :                   tmp = e->ts.u.cl->backend_decl;
    1208                 :             :                 }
    1209                 :             :               else
    1210                 :             :                 {
    1211                 :           0 :                   gfc_error ("Cannot compute the length of the char array "
    1212                 :             :                              "at %L.", &e->where);
    1213                 :             :                 }
    1214                 :             :             }
    1215                 :             :         }
    1216                 :             :       else
    1217                 :         683 :         tmp = integer_zero_node;
    1218                 :             : 
    1219                 :         858 :       gfc_add_modify (&parmse->pre, ctree, fold_convert (TREE_TYPE (ctree), tmp));
    1220                 :             :     }
    1221                 :             : 
    1222                 :             :   /* Pass the address of the class object.  */
    1223                 :         858 :   parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
    1224                 :         858 : }
    1225                 :             : 
    1226                 :             : 
    1227                 :             : /* Takes a scalarized class array expression and returns the
    1228                 :             :    address of a temporary scalar class object of the 'declared'
    1229                 :             :    type.
    1230                 :             :    OOP-TODO: This could be improved by adding code that branched on
    1231                 :             :    the dynamic type being the same as the declared type. In this case
    1232                 :             :    the original class expression can be passed directly.
    1233                 :             :    optional_alloc_ptr is false when the dummy is neither allocatable
    1234                 :             :    nor a pointer; that's relevant for the optional handling.
    1235                 :             :    Set copyback to true if class container's _data and _vtab pointers
    1236                 :             :    might get modified.  */
    1237                 :             : 
    1238                 :             : void
    1239                 :        3433 : gfc_conv_class_to_class (gfc_se *parmse, gfc_expr *e, gfc_typespec class_ts,
    1240                 :             :                          bool elemental, bool copyback, bool optional,
    1241                 :             :                          bool optional_alloc_ptr)
    1242                 :             : {
    1243                 :        3433 :   tree ctree;
    1244                 :        3433 :   tree var;
    1245                 :        3433 :   tree tmp;
    1246                 :        3433 :   tree vptr;
    1247                 :        3433 :   tree cond = NULL_TREE;
    1248                 :        3433 :   tree slen = NULL_TREE;
    1249                 :        3433 :   gfc_ref *ref;
    1250                 :        3433 :   gfc_ref *class_ref;
    1251                 :        3433 :   stmtblock_t block;
    1252                 :        3433 :   bool full_array = false;
    1253                 :             : 
    1254                 :             :   /* Class transformational function results are the data field of a class
    1255                 :             :      temporary and so the class expression can be obtained directly.  */
    1256                 :        3433 :   if (e->expr_type == EXPR_FUNCTION
    1257                 :         162 :       && e->value.function.isym
    1258                 :          30 :       && e->value.function.isym->transformational
    1259                 :          30 :       && TREE_CODE (parmse->expr) == COMPONENT_REF
    1260                 :        3457 :       && !GFC_CLASS_TYPE_P (TREE_TYPE (parmse->expr)))
    1261                 :             :     {
    1262                 :          24 :       parmse->expr = TREE_OPERAND (parmse->expr, 0);
    1263                 :          24 :       if (!VAR_P (parmse->expr))
    1264                 :           0 :         parmse->expr = gfc_evaluate_now (parmse->expr, &parmse->pre);
    1265                 :          24 :       parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
    1266                 :         162 :       return;
    1267                 :             :     }
    1268                 :             : 
    1269                 :        3409 :   gfc_init_block (&block);
    1270                 :             : 
    1271                 :        3409 :   class_ref = NULL;
    1272                 :        6839 :   for (ref = e->ref; ref; ref = ref->next)
    1273                 :             :     {
    1274                 :        6469 :       if (ref->type == REF_COMPONENT
    1275                 :        3464 :             && ref->u.c.component->ts.type == BT_CLASS)
    1276                 :        6469 :         class_ref = ref;
    1277                 :             : 
    1278                 :        6469 :       if (ref->next == NULL)
    1279                 :             :         break;
    1280                 :             :     }
    1281                 :             : 
    1282                 :        3409 :   if ((ref == NULL || class_ref == ref)
    1283                 :         482 :       && !(gfc_is_class_array_function (e) && parmse->class_vptr != NULL_TREE)
    1284                 :        3879 :       && (!class_ts.u.derived->components->as
    1285                 :         379 :           || class_ts.u.derived->components->as->rank != -1))
    1286                 :             :     return;
    1287                 :             : 
    1288                 :             :   /* Test for FULL_ARRAY.  */
    1289                 :        3271 :   if (e->rank == 0
    1290                 :        3271 :       && ((gfc_expr_attr (e).codimension && gfc_expr_attr (e).dimension)
    1291                 :         493 :           || (class_ts.u.derived->components->as
    1292                 :         365 :               && class_ts.u.derived->components->as->type == AS_ASSUMED_RANK)))
    1293                 :         407 :     full_array = true;
    1294                 :             :   else
    1295                 :        2864 :     gfc_is_class_array_ref (e, &full_array);
    1296                 :             : 
    1297                 :             :   /* The derived type needs to be converted to a temporary
    1298                 :             :      CLASS object.  */
    1299                 :        3271 :   tmp = gfc_typenode_for_spec (&class_ts);
    1300                 :        3271 :   var = gfc_create_var (tmp, "class");
    1301                 :             : 
    1302                 :             :   /* Set the data.  */
    1303                 :        3271 :   ctree = gfc_class_data_get (var);
    1304                 :        3271 :   if (class_ts.u.derived->components->as
    1305                 :        3017 :       && e->rank != class_ts.u.derived->components->as->rank)
    1306                 :             :     {
    1307                 :         965 :       if (e->rank == 0)
    1308                 :             :         {
    1309                 :         356 :           tree type = get_scalar_to_descriptor_type (parmse->expr,
    1310                 :             :                                                      gfc_expr_attr (e));
    1311                 :         356 :           gfc_add_modify (&block, gfc_conv_descriptor_dtype (ctree),
    1312                 :             :                           gfc_get_dtype (type));
    1313                 :             : 
    1314                 :         356 :           tmp = gfc_class_data_get (parmse->expr);
    1315                 :         356 :           if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
    1316                 :          12 :             tmp = gfc_build_addr_expr (NULL_TREE, tmp);
    1317                 :             : 
    1318                 :         356 :           gfc_conv_descriptor_data_set (&block, ctree, tmp);
    1319                 :             :         }
    1320                 :             :       else
    1321                 :         609 :         gfc_class_array_data_assign (&block, ctree, parmse->expr, false);
    1322                 :             :     }
    1323                 :             :   else
    1324                 :             :     {
    1325                 :        2306 :       if (TREE_TYPE (parmse->expr) != TREE_TYPE (ctree))
    1326                 :        1335 :         parmse->expr = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
    1327                 :        1335 :                                         TREE_TYPE (ctree), parmse->expr);
    1328                 :        2306 :       gfc_add_modify (&block, ctree, parmse->expr);
    1329                 :             :     }
    1330                 :             : 
    1331                 :             :   /* Return the data component, except in the case of scalarized array
    1332                 :             :      references, where nullification of the cannot occur and so there
    1333                 :             :      is no need.  */
    1334                 :        3271 :   if (!elemental && full_array && copyback)
    1335                 :             :     {
    1336                 :        1128 :       if (class_ts.u.derived->components->as
    1337                 :        1128 :           && e->rank != class_ts.u.derived->components->as->rank)
    1338                 :             :         {
    1339                 :         270 :           if (e->rank == 0)
    1340                 :             :             {
    1341                 :         102 :               tmp = gfc_class_data_get (parmse->expr);
    1342                 :         204 :               gfc_add_modify (&parmse->post, tmp,
    1343                 :         102 :                               fold_convert (TREE_TYPE (tmp),
    1344                 :             :                                          gfc_conv_descriptor_data_get (ctree)));
    1345                 :             :             }
    1346                 :             :           else
    1347                 :         168 :             gfc_class_array_data_assign (&parmse->post, parmse->expr, ctree,
    1348                 :             :                                          true);
    1349                 :             :         }
    1350                 :             :       else
    1351                 :         858 :         gfc_add_modify (&parmse->post, parmse->expr, ctree);
    1352                 :             :     }
    1353                 :             : 
    1354                 :             :   /* Set the vptr.  */
    1355                 :        3271 :   ctree = gfc_class_vptr_get (var);
    1356                 :             : 
    1357                 :             :   /* The vptr is the second field of the actual argument.
    1358                 :             :      First we have to find the corresponding class reference.  */
    1359                 :             : 
    1360                 :        3271 :   tmp = NULL_TREE;
    1361                 :        3271 :   if (gfc_is_class_array_function (e)
    1362                 :        3271 :       && parmse->class_vptr != NULL_TREE)
    1363                 :             :     tmp = parmse->class_vptr;
    1364                 :        3259 :   else if (class_ref == NULL
    1365                 :        2816 :            && e->symtree && e->symtree->n.sym->ts.type == BT_CLASS)
    1366                 :             :     {
    1367                 :        2816 :       tmp = e->symtree->n.sym->backend_decl;
    1368                 :             : 
    1369                 :        2816 :       if (TREE_CODE (tmp) == FUNCTION_DECL)
    1370                 :           6 :         tmp = gfc_get_fake_result_decl (e->symtree->n.sym, 0);
    1371                 :             : 
    1372                 :        2816 :       if (DECL_LANG_SPECIFIC (tmp) && GFC_DECL_SAVED_DESCRIPTOR (tmp))
    1373                 :         372 :         tmp = GFC_DECL_SAVED_DESCRIPTOR (tmp);
    1374                 :             : 
    1375                 :        2816 :       slen = build_zero_cst (size_type_node);
    1376                 :             :     }
    1377                 :         443 :   else if (parmse->class_container != NULL_TREE)
    1378                 :             :     /* Don't redundantly evaluate the expression if the required information
    1379                 :             :        is already available.  */
    1380                 :             :     tmp = parmse->class_container;
    1381                 :             :   else
    1382                 :             :     {
    1383                 :             :       /* Remove everything after the last class reference, convert the
    1384                 :             :          expression and then recover its tailend once more.  */
    1385                 :          18 :       gfc_se tmpse;
    1386                 :          18 :       ref = class_ref->next;
    1387                 :          18 :       class_ref->next = NULL;
    1388                 :          18 :       gfc_init_se (&tmpse, NULL);
    1389                 :          18 :       gfc_conv_expr (&tmpse, e);
    1390                 :          18 :       class_ref->next = ref;
    1391                 :          18 :       tmp = tmpse.expr;
    1392                 :          18 :       slen = tmpse.string_length;
    1393                 :             :     }
    1394                 :             : 
    1395                 :        3271 :   gcc_assert (tmp != NULL_TREE);
    1396                 :             : 
    1397                 :             :   /* Dereference if needs be.  */
    1398                 :        3271 :   if (TREE_CODE (TREE_TYPE (tmp)) == REFERENCE_TYPE)
    1399                 :         320 :     tmp = build_fold_indirect_ref_loc (input_location, tmp);
    1400                 :             : 
    1401                 :        3271 :   if (!(gfc_is_class_array_function (e) && parmse->class_vptr))
    1402                 :        3259 :     vptr = gfc_class_vptr_get (tmp);
    1403                 :             :   else
    1404                 :             :     vptr = tmp;
    1405                 :             : 
    1406                 :        3271 :   gfc_add_modify (&block, ctree,
    1407                 :        3271 :                   fold_convert (TREE_TYPE (ctree), vptr));
    1408                 :             : 
    1409                 :             :   /* Return the vptr component, except in the case of scalarized array
    1410                 :             :      references, where the dynamic type cannot change.  */
    1411                 :        3271 :   if (!elemental && full_array && copyback)
    1412                 :        1128 :     gfc_add_modify (&parmse->post, vptr,
    1413                 :        1128 :                     fold_convert (TREE_TYPE (vptr), ctree));
    1414                 :             : 
    1415                 :             :   /* For unlimited polymorphic objects also set the _len component.  */
    1416                 :        3271 :   if (class_ts.type == BT_CLASS
    1417                 :        3271 :       && class_ts.u.derived->components
    1418                 :        3271 :       && class_ts.u.derived->components->ts.u
    1419                 :        3271 :                       .derived->attr.unlimited_polymorphic)
    1420                 :             :     {
    1421                 :        1013 :       ctree = gfc_class_len_get (var);
    1422                 :        1013 :       if (UNLIMITED_POLY (e))
    1423                 :         817 :         tmp = gfc_class_len_get (tmp);
    1424                 :         196 :       else if (e->ts.type == BT_CHARACTER)
    1425                 :             :         {
    1426                 :           0 :           gcc_assert (slen != NULL_TREE);
    1427                 :             :           tmp = slen;
    1428                 :             :         }
    1429                 :             :       else
    1430                 :         196 :         tmp = build_zero_cst (size_type_node);
    1431                 :        1013 :       gfc_add_modify (&parmse->pre, ctree,
    1432                 :        1013 :                       fold_convert (TREE_TYPE (ctree), tmp));
    1433                 :             : 
    1434                 :             :       /* Return the len component, except in the case of scalarized array
    1435                 :             :         references, where the dynamic type cannot change.  */
    1436                 :        1013 :       if (!elemental && full_array && copyback
    1437                 :         440 :           && (UNLIMITED_POLY (e) || VAR_P (tmp)))
    1438                 :         428 :           gfc_add_modify (&parmse->post, tmp,
    1439                 :         428 :                           fold_convert (TREE_TYPE (tmp), ctree));
    1440                 :             :     }
    1441                 :             : 
    1442                 :        3271 :   if (optional)
    1443                 :             :     {
    1444                 :         510 :       tree tmp2;
    1445                 :             : 
    1446                 :         510 :       cond = gfc_conv_expr_present (e->symtree->n.sym);
    1447                 :             :       /* parmse->pre may contain some preparatory instructions for the
    1448                 :             :          temporary array descriptor.  Those may only be executed when the
    1449                 :             :          optional argument is set, therefore add parmse->pre's instructions
    1450                 :             :          to block, which is later guarded by an if (optional_arg_given).  */
    1451                 :         510 :       gfc_add_block_to_block (&parmse->pre, &block);
    1452                 :         510 :       block.head = parmse->pre.head;
    1453                 :         510 :       parmse->pre.head = NULL_TREE;
    1454                 :         510 :       tmp = gfc_finish_block (&block);
    1455                 :             : 
    1456                 :         510 :       if (optional_alloc_ptr)
    1457                 :         102 :         tmp2 = build_empty_stmt (input_location);
    1458                 :             :       else
    1459                 :             :         {
    1460                 :         408 :           gfc_init_block (&block);
    1461                 :             : 
    1462                 :         408 :           tmp2 = gfc_conv_descriptor_data_get (gfc_class_data_get (var));
    1463                 :         408 :           gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2),
    1464                 :             :                                                       null_pointer_node));
    1465                 :         408 :           tmp2 = gfc_finish_block (&block);
    1466                 :             :         }
    1467                 :             : 
    1468                 :         510 :       tmp = build3_loc (input_location, COND_EXPR, void_type_node,
    1469                 :             :                         cond, tmp, tmp2);
    1470                 :         510 :       gfc_add_expr_to_block (&parmse->pre, tmp);
    1471                 :             : 
    1472                 :         510 :       if (!elemental && full_array && copyback)
    1473                 :             :         {
    1474                 :          30 :           tmp2 = build_empty_stmt (input_location);
    1475                 :          30 :           tmp = gfc_finish_block (&parmse->post);
    1476                 :          30 :           tmp = build3_loc (input_location, COND_EXPR, void_type_node,
    1477                 :             :                             cond, tmp, tmp2);
    1478                 :          30 :           gfc_add_expr_to_block (&parmse->post, tmp);
    1479                 :             :         }
    1480                 :             :     }
    1481                 :             :   else
    1482                 :        2761 :     gfc_add_block_to_block (&parmse->pre, &block);
    1483                 :             : 
    1484                 :             :   /* Pass the address of the class object.  */
    1485                 :        3271 :   parmse->expr = gfc_build_addr_expr (NULL_TREE, var);
    1486                 :             : 
    1487                 :        3271 :   if (optional && optional_alloc_ptr)
    1488                 :         204 :     parmse->expr = build3_loc (input_location, COND_EXPR,
    1489                 :         102 :                                TREE_TYPE (parmse->expr),
    1490                 :             :                                cond, parmse->expr,
    1491                 :         102 :                                fold_convert (TREE_TYPE (parmse->expr),
    1492                 :             :                                              null_pointer_node));
    1493                 :             : }
    1494                 :             : 
    1495                 :             : 
    1496                 :             : /* Given a class array declaration and an index, returns the address
    1497                 :             :    of the referenced element.  */
    1498                 :             : 
    1499                 :             : static tree
    1500                 :         712 : gfc_get_class_array_ref (tree index, tree class_decl, tree data_comp,
    1501                 :             :                          bool unlimited)
    1502                 :             : {
    1503                 :         712 :   tree data, size, tmp, ctmp, offset, ptr;
    1504                 :             : 
    1505                 :         712 :   data = data_comp != NULL_TREE ? data_comp :
    1506                 :           0 :                                   gfc_class_data_get (class_decl);
    1507                 :         712 :   size = gfc_class_vtab_size_get (class_decl);
    1508                 :             : 
    1509                 :         712 :   if (unlimited)
    1510                 :             :     {
    1511                 :         200 :       tmp = fold_convert (gfc_array_index_type,
    1512                 :             :                           gfc_class_len_get (class_decl));
    1513                 :         200 :       ctmp = fold_build2_loc (input_location, MULT_EXPR,
    1514                 :             :                               gfc_array_index_type, size, tmp);
    1515                 :         200 :       tmp = fold_build2_loc (input_location, GT_EXPR,
    1516                 :             :                              logical_type_node, tmp,
    1517                 :         200 :                              build_zero_cst (TREE_TYPE (tmp)));
    1518                 :         200 :       size = fold_build3_loc (input_location, COND_EXPR,
    1519                 :             :                               gfc_array_index_type, tmp, ctmp, size);
    1520                 :             :     }
    1521                 :             : 
    1522                 :         712 :   offset = fold_build2_loc (input_location, MULT_EXPR,
    1523                 :             :                             gfc_array_index_type,
    1524                 :             :                             index, size);
    1525                 :             : 
    1526                 :         712 :   data = gfc_conv_descriptor_data_get (data);
    1527                 :         712 :   ptr = fold_convert (pvoid_type_node, data);
    1528                 :         712 :   ptr = fold_build_pointer_plus_loc (input_location, ptr, offset);
    1529                 :         712 :   return fold_convert (TREE_TYPE (data), ptr);
    1530                 :             : }
    1531                 :             : 
    1532                 :             : 
    1533                 :             : /* Copies one class expression to another, assuming that if either
    1534                 :             :    'to' or 'from' are arrays they are packed.  Should 'from' be
    1535                 :             :    NULL_TREE, the initialization expression for 'to' is used, assuming
    1536                 :             :    that the _vptr is set.  */
    1537                 :             : 
    1538                 :             : tree
    1539                 :         742 : gfc_copy_class_to_class (tree from, tree to, tree nelems, bool unlimited)
    1540                 :             : {
    1541                 :         742 :   tree fcn;
    1542                 :         742 :   tree fcn_type;
    1543                 :         742 :   tree from_data;
    1544                 :         742 :   tree from_len;
    1545                 :         742 :   tree to_data;
    1546                 :         742 :   tree to_len;
    1547                 :         742 :   tree to_ref;
    1548                 :         742 :   tree from_ref;
    1549                 :         742 :   vec<tree, va_gc> *args;
    1550                 :         742 :   tree tmp;
    1551                 :         742 :   tree stdcopy;
    1552                 :         742 :   tree extcopy;
    1553                 :         742 :   tree index;
    1554                 :         742 :   bool is_from_desc = false, is_to_class = false;
    1555                 :             : 
    1556                 :         742 :   args = NULL;
    1557                 :             :   /* To prevent warnings on uninitialized variables.  */
    1558                 :         742 :   from_len = to_len = NULL_TREE;
    1559                 :             : 
    1560                 :         742 :   if (from != NULL_TREE)
    1561                 :         742 :     fcn = gfc_class_vtab_copy_get (from);
    1562                 :             :   else
    1563                 :           0 :     fcn = gfc_class_vtab_copy_get (to);
    1564                 :             : 
    1565                 :         742 :   fcn_type = TREE_TYPE (TREE_TYPE (fcn));
    1566                 :             : 
    1567                 :         742 :   if (from != NULL_TREE)
    1568                 :             :     {
    1569                 :         742 :       is_from_desc = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from));
    1570                 :         742 :       if (is_from_desc)
    1571                 :             :         {
    1572                 :           0 :           from_data = from;
    1573                 :           0 :           from = GFC_DECL_SAVED_DESCRIPTOR (from);
    1574                 :             :         }
    1575                 :             :       else
    1576                 :             :         {
    1577                 :             :           /* Check that from is a class.  When the class is part of a coarray,
    1578                 :             :              then from is a common pointer and is to be used as is.  */
    1579                 :        1484 :           tmp = POINTER_TYPE_P (TREE_TYPE (from))
    1580                 :         742 :               ? build_fold_indirect_ref (from) : from;
    1581                 :        1484 :           from_data =
    1582                 :         742 :               (GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
    1583                 :           0 :                || (DECL_P (tmp) && GFC_DECL_CLASS (tmp)))
    1584                 :         742 :               ? gfc_class_data_get (from) : from;
    1585                 :         742 :           is_from_desc = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (from_data));
    1586                 :             :         }
    1587                 :             :      }
    1588                 :             :   else
    1589                 :           0 :     from_data = gfc_class_vtab_def_init_get (to);
    1590                 :             : 
    1591                 :         742 :   if (unlimited)
    1592                 :             :     {
    1593                 :         159 :       if (from != NULL_TREE && unlimited)
    1594                 :         159 :         from_len = gfc_class_len_or_zero_get (from);
    1595                 :             :       else
    1596                 :           0 :         from_len = build_zero_cst (size_type_node);
    1597                 :             :     }
    1598                 :             : 
    1599                 :         742 :   if (GFC_CLASS_TYPE_P (TREE_TYPE (to)))
    1600                 :             :     {
    1601                 :         742 :       is_to_class = true;
    1602                 :         742 :       to_data = gfc_class_data_get (to);
    1603                 :         742 :       if (unlimited)
    1604                 :         159 :         to_len = gfc_class_len_get (to);
    1605                 :             :     }
    1606                 :             :   else
    1607                 :             :     /* When to is a BT_DERIVED and not a BT_CLASS, then to_data == to.  */
    1608                 :           0 :     to_data = to;
    1609                 :             : 
    1610                 :         742 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (to_data)))
    1611                 :             :     {
    1612                 :         356 :       stmtblock_t loopbody;
    1613                 :         356 :       stmtblock_t body;
    1614                 :         356 :       stmtblock_t ifbody;
    1615                 :         356 :       gfc_loopinfo loop;
    1616                 :             : 
    1617                 :         356 :       gfc_init_block (&body);
    1618                 :         356 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
    1619                 :             :                              gfc_array_index_type, nelems,
    1620                 :             :                              gfc_index_one_node);
    1621                 :         356 :       nelems = gfc_evaluate_now (tmp, &body);
    1622                 :         356 :       index = gfc_create_var (gfc_array_index_type, "S");
    1623                 :             : 
    1624                 :         356 :       if (is_from_desc)
    1625                 :             :         {
    1626                 :         356 :           from_ref = gfc_get_class_array_ref (index, from, from_data,
    1627                 :             :                                               unlimited);
    1628                 :         356 :           vec_safe_push (args, from_ref);
    1629                 :             :         }
    1630                 :             :       else
    1631                 :           0 :         vec_safe_push (args, from_data);
    1632                 :             : 
    1633                 :         356 :       if (is_to_class)
    1634                 :         356 :         to_ref = gfc_get_class_array_ref (index, to, to_data, unlimited);
    1635                 :             :       else
    1636                 :             :         {
    1637                 :           0 :           tmp = gfc_conv_array_data (to);
    1638                 :           0 :           tmp = build_fold_indirect_ref_loc (input_location, tmp);
    1639                 :           0 :           to_ref = gfc_build_addr_expr (NULL_TREE,
    1640                 :             :                                         gfc_build_array_ref (tmp, index, to));
    1641                 :             :         }
    1642                 :         356 :       vec_safe_push (args, to_ref);
    1643                 :             : 
    1644                 :             :       /* Add bounds check.  */
    1645                 :         356 :       if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS) > 0 && is_from_desc)
    1646                 :             :         {
    1647                 :          25 :           const char *name = "<<unknown>>";
    1648                 :          25 :           int dim, rank;
    1649                 :             : 
    1650                 :          25 :           if (DECL_P (to))
    1651                 :           0 :             name = IDENTIFIER_POINTER (DECL_NAME (to));
    1652                 :             : 
    1653                 :          25 :           rank = GFC_TYPE_ARRAY_RANK (TREE_TYPE (from_data));
    1654                 :          55 :           for (dim = 1; dim <= rank; dim++)
    1655                 :             :             {
    1656                 :          30 :               tree from_len, to_len, cond;
    1657                 :          30 :               char *msg;
    1658                 :             : 
    1659                 :          30 :               from_len = gfc_conv_descriptor_size (from_data, dim);
    1660                 :          30 :               from_len = fold_convert (long_integer_type_node, from_len);
    1661                 :          30 :               to_len = gfc_conv_descriptor_size (to_data, dim);
    1662                 :          30 :               to_len = fold_convert (long_integer_type_node, to_len);
    1663                 :          30 :               msg = xasprintf ("Array bound mismatch for dimension %d "
    1664                 :             :                                "of array '%s' (%%ld/%%ld)",
    1665                 :             :                                dim, name);
    1666                 :          30 :               cond = fold_build2_loc (input_location, NE_EXPR,
    1667                 :             :                                       logical_type_node, from_len, to_len);
    1668                 :          30 :               gfc_trans_runtime_check (true, false, cond, &body,
    1669                 :             :                                        NULL, msg, to_len, from_len);
    1670                 :          30 :               free (msg);
    1671                 :             :             }
    1672                 :             :         }
    1673                 :             : 
    1674                 :         356 :       tmp = build_call_vec (fcn_type, fcn, args);
    1675                 :             : 
    1676                 :             :       /* Build the body of the loop.  */
    1677                 :         356 :       gfc_init_block (&loopbody);
    1678                 :         356 :       gfc_add_expr_to_block (&loopbody, tmp);
    1679                 :             : 
    1680                 :             :       /* Build the loop and return.  */
    1681                 :         356 :       gfc_init_loopinfo (&loop);
    1682                 :         356 :       loop.dimen = 1;
    1683                 :         356 :       loop.from[0] = gfc_index_zero_node;
    1684                 :         356 :       loop.loopvar[0] = index;
    1685                 :         356 :       loop.to[0] = nelems;
    1686                 :         356 :       gfc_trans_scalarizing_loops (&loop, &loopbody);
    1687                 :         356 :       gfc_init_block (&ifbody);
    1688                 :         356 :       gfc_add_block_to_block (&ifbody, &loop.pre);
    1689                 :         356 :       stdcopy = gfc_finish_block (&ifbody);
    1690                 :             :       /* In initialization mode from_len is a constant zero.  */
    1691                 :         356 :       if (unlimited && !integer_zerop (from_len))
    1692                 :             :         {
    1693                 :         100 :           vec_safe_push (args, from_len);
    1694                 :         100 :           vec_safe_push (args, to_len);
    1695                 :         100 :           tmp = build_call_vec (fcn_type, fcn, args);
    1696                 :             :           /* Build the body of the loop.  */
    1697                 :         100 :           gfc_init_block (&loopbody);
    1698                 :         100 :           gfc_add_expr_to_block (&loopbody, tmp);
    1699                 :             : 
    1700                 :             :           /* Build the loop and return.  */
    1701                 :         100 :           gfc_init_loopinfo (&loop);
    1702                 :         100 :           loop.dimen = 1;
    1703                 :         100 :           loop.from[0] = gfc_index_zero_node;
    1704                 :         100 :           loop.loopvar[0] = index;
    1705                 :         100 :           loop.to[0] = nelems;
    1706                 :         100 :           gfc_trans_scalarizing_loops (&loop, &loopbody);
    1707                 :         100 :           gfc_init_block (&ifbody);
    1708                 :         100 :           gfc_add_block_to_block (&ifbody, &loop.pre);
    1709                 :         100 :           extcopy = gfc_finish_block (&ifbody);
    1710                 :             : 
    1711                 :         100 :           tmp = fold_build2_loc (input_location, GT_EXPR,
    1712                 :             :                                  logical_type_node, from_len,
    1713                 :         100 :                                  build_zero_cst (TREE_TYPE (from_len)));
    1714                 :         100 :           tmp = fold_build3_loc (input_location, COND_EXPR,
    1715                 :             :                                  void_type_node, tmp, extcopy, stdcopy);
    1716                 :         100 :           gfc_add_expr_to_block (&body, tmp);
    1717                 :         100 :           tmp = gfc_finish_block (&body);
    1718                 :             :         }
    1719                 :             :       else
    1720                 :             :         {
    1721                 :         256 :           gfc_add_expr_to_block (&body, stdcopy);
    1722                 :         256 :           tmp = gfc_finish_block (&body);
    1723                 :             :         }
    1724                 :         356 :       gfc_cleanup_loop (&loop);
    1725                 :             :     }
    1726                 :             :   else
    1727                 :             :     {
    1728                 :         386 :       gcc_assert (!is_from_desc);
    1729                 :         386 :       vec_safe_push (args, from_data);
    1730                 :         386 :       vec_safe_push (args, to_data);
    1731                 :         386 :       stdcopy = build_call_vec (fcn_type, fcn, args);
    1732                 :             : 
    1733                 :             :       /* In initialization mode from_len is a constant zero.  */
    1734                 :         386 :       if (unlimited && !integer_zerop (from_len))
    1735                 :             :         {
    1736                 :          59 :           vec_safe_push (args, from_len);
    1737                 :          59 :           vec_safe_push (args, to_len);
    1738                 :          59 :           extcopy = build_call_vec (fcn_type, unshare_expr (fcn), args);
    1739                 :          59 :           tmp = fold_build2_loc (input_location, GT_EXPR,
    1740                 :             :                                  logical_type_node, from_len,
    1741                 :          59 :                                  build_zero_cst (TREE_TYPE (from_len)));
    1742                 :          59 :           tmp = fold_build3_loc (input_location, COND_EXPR,
    1743                 :             :                                  void_type_node, tmp, extcopy, stdcopy);
    1744                 :             :         }
    1745                 :             :       else
    1746                 :             :         tmp = stdcopy;
    1747                 :             :     }
    1748                 :             : 
    1749                 :             :   /* Only copy _def_init to to_data, when it is not a NULL-pointer.  */
    1750                 :         742 :   if (from == NULL_TREE)
    1751                 :             :     {
    1752                 :           0 :       tree cond;
    1753                 :           0 :       cond = fold_build2_loc (input_location, NE_EXPR,
    1754                 :             :                               logical_type_node,
    1755                 :             :                               from_data, null_pointer_node);
    1756                 :           0 :       tmp = fold_build3_loc (input_location, COND_EXPR,
    1757                 :             :                              void_type_node, cond,
    1758                 :             :                              tmp, build_empty_stmt (input_location));
    1759                 :             :     }
    1760                 :             : 
    1761                 :         742 :   return tmp;
    1762                 :             : }
    1763                 :             : 
    1764                 :             : 
    1765                 :             : static tree
    1766                 :         106 : gfc_trans_class_array_init_assign (gfc_expr *rhs, gfc_expr *lhs, gfc_expr *obj)
    1767                 :             : {
    1768                 :         106 :   gfc_actual_arglist *actual;
    1769                 :         106 :   gfc_expr *ppc;
    1770                 :         106 :   gfc_code *ppc_code;
    1771                 :         106 :   tree res;
    1772                 :             : 
    1773                 :         106 :   actual = gfc_get_actual_arglist ();
    1774                 :         106 :   actual->expr = gfc_copy_expr (rhs);
    1775                 :         106 :   actual->next = gfc_get_actual_arglist ();
    1776                 :         106 :   actual->next->expr = gfc_copy_expr (lhs);
    1777                 :         106 :   ppc = gfc_copy_expr (obj);
    1778                 :         106 :   gfc_add_vptr_component (ppc);
    1779                 :         106 :   gfc_add_component_ref (ppc, "_copy");
    1780                 :         106 :   ppc_code = gfc_get_code (EXEC_CALL);
    1781                 :         106 :   ppc_code->resolved_sym = ppc->symtree->n.sym;
    1782                 :             :   /* Although '_copy' is set to be elemental in class.cc, it is
    1783                 :             :      not staying that way.  Find out why, sometime....  */
    1784                 :         106 :   ppc_code->resolved_sym->attr.elemental = 1;
    1785                 :         106 :   ppc_code->ext.actual = actual;
    1786                 :         106 :   ppc_code->expr1 = ppc;
    1787                 :             :   /* Since '_copy' is elemental, the scalarizer will take care
    1788                 :             :      of arrays in gfc_trans_call.  */
    1789                 :         106 :   res = gfc_trans_call (ppc_code, false, NULL, NULL, false);
    1790                 :         106 :   gfc_free_statements (ppc_code);
    1791                 :             : 
    1792                 :         106 :   if (UNLIMITED_POLY(obj))
    1793                 :             :     {
    1794                 :             :       /* Check if rhs is non-NULL. */
    1795                 :          24 :       gfc_se src;
    1796                 :          24 :       gfc_init_se (&src, NULL);
    1797                 :          24 :       gfc_conv_expr (&src, rhs);
    1798                 :          24 :       src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
    1799                 :          24 :       tree cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    1800                 :          24 :                                    src.expr, fold_convert (TREE_TYPE (src.expr),
    1801                 :             :                                                            null_pointer_node));
    1802                 :          24 :       res = build3_loc (input_location, COND_EXPR, TREE_TYPE (res), cond, res,
    1803                 :             :                         build_empty_stmt (input_location));
    1804                 :             :     }
    1805                 :             : 
    1806                 :         106 :   return res;
    1807                 :             : }
    1808                 :             : 
    1809                 :             : /* Special case for initializing a polymorphic dummy with INTENT(OUT).
    1810                 :             :    A MEMCPY is needed to copy the full data from the default initializer
    1811                 :             :    of the dynamic type.  */
    1812                 :             : 
    1813                 :             : tree
    1814                 :         441 : gfc_trans_class_init_assign (gfc_code *code)
    1815                 :             : {
    1816                 :         441 :   stmtblock_t block;
    1817                 :         441 :   tree tmp;
    1818                 :         441 :   bool cmp_flag = true;
    1819                 :         441 :   gfc_se dst,src,memsz;
    1820                 :         441 :   gfc_expr *lhs, *rhs, *sz;
    1821                 :         441 :   gfc_component *cmp;
    1822                 :         441 :   gfc_symbol *sym;
    1823                 :         441 :   gfc_ref *ref;
    1824                 :             : 
    1825                 :         441 :   gfc_start_block (&block);
    1826                 :             : 
    1827                 :         441 :   lhs = gfc_copy_expr (code->expr1);
    1828                 :             : 
    1829                 :         441 :   rhs = gfc_copy_expr (code->expr1);
    1830                 :         441 :   gfc_add_vptr_component (rhs);
    1831                 :             : 
    1832                 :             :   /* Make sure that the component backend_decls have been built, which
    1833                 :             :      will not have happened if the derived types concerned have not
    1834                 :             :      been referenced.  */
    1835                 :         441 :   gfc_get_derived_type (rhs->ts.u.derived);
    1836                 :         441 :   gfc_add_def_init_component (rhs);
    1837                 :             :   /* The _def_init is always scalar.  */
    1838                 :         441 :   rhs->rank = 0;
    1839                 :             : 
    1840                 :             :   /* Check def_init for initializers.  If this is an INTENT(OUT) dummy with all
    1841                 :             :      default initializer components NULL, use the passed value even though
    1842                 :             :      F2018(8.5.10) asserts that it should considered to be undefined. This is
    1843                 :             :      needed for consistency with other brands.  */
    1844                 :         441 :   sym = code->expr1->expr_type == EXPR_VARIABLE ? code->expr1->symtree->n.sym
    1845                 :             :                                                 : NULL;
    1846                 :         441 :   if (code->op != EXEC_ALLOCATE
    1847                 :         380 :       && sym && sym->attr.dummy
    1848                 :         380 :       && sym->attr.intent == INTENT_OUT)
    1849                 :             :     {
    1850                 :         380 :       ref = rhs->ref;
    1851                 :         760 :       while (ref && ref->next)
    1852                 :             :         ref = ref->next;
    1853                 :         380 :       cmp = ref->u.c.component->ts.u.derived->components;
    1854                 :         603 :       for (; cmp; cmp = cmp->next)
    1855                 :             :         {
    1856                 :         427 :           if (cmp->initializer)
    1857                 :             :             break;
    1858                 :         223 :           else if (!cmp->next)
    1859                 :         146 :             cmp_flag = false;
    1860                 :             :         }
    1861                 :             :     }
    1862                 :             : 
    1863                 :         441 :   if (code->expr1->ts.type == BT_CLASS
    1864                 :         418 :       && CLASS_DATA (code->expr1)->attr.dimension)
    1865                 :             :     {
    1866                 :         106 :       gfc_array_spec *tmparr = gfc_get_array_spec ();
    1867                 :         106 :       *tmparr = *CLASS_DATA (code->expr1)->as;
    1868                 :             :       /* Adding the array ref to the class expression results in correct
    1869                 :             :          indexing to the dynamic type.  */
    1870                 :         106 :       gfc_add_full_array_ref (lhs, tmparr);
    1871                 :         106 :       tmp = gfc_trans_class_array_init_assign (rhs, lhs, code->expr1);
    1872                 :         106 :     }
    1873                 :         335 :   else if (cmp_flag)
    1874                 :             :     {
    1875                 :             :       /* Scalar initialization needs the _data component.  */
    1876                 :         202 :       gfc_add_data_component (lhs);
    1877                 :         202 :       sz = gfc_copy_expr (code->expr1);
    1878                 :         202 :       gfc_add_vptr_component (sz);
    1879                 :         202 :       gfc_add_size_component (sz);
    1880                 :             : 
    1881                 :         202 :       gfc_init_se (&dst, NULL);
    1882                 :         202 :       gfc_init_se (&src, NULL);
    1883                 :         202 :       gfc_init_se (&memsz, NULL);
    1884                 :         202 :       gfc_conv_expr (&dst, lhs);
    1885                 :         202 :       gfc_conv_expr (&src, rhs);
    1886                 :         202 :       gfc_conv_expr (&memsz, sz);
    1887                 :         202 :       gfc_add_block_to_block (&block, &src.pre);
    1888                 :         202 :       src.expr = gfc_build_addr_expr (NULL_TREE, src.expr);
    1889                 :             : 
    1890                 :         202 :       tmp = gfc_build_memcpy_call (dst.expr, src.expr, memsz.expr);
    1891                 :             : 
    1892                 :         202 :       if (UNLIMITED_POLY(code->expr1))
    1893                 :             :         {
    1894                 :             :           /* Check if _def_init is non-NULL. */
    1895                 :           7 :           tree cond = fold_build2_loc (input_location, NE_EXPR,
    1896                 :             :                                        logical_type_node, src.expr,
    1897                 :           7 :                                        fold_convert (TREE_TYPE (src.expr),
    1898                 :             :                                                      null_pointer_node));
    1899                 :           7 :           tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
    1900                 :             :                             tmp, build_empty_stmt (input_location));
    1901                 :             :         }
    1902                 :             :     }
    1903                 :             :   else
    1904                 :         133 :     tmp = build_empty_stmt (input_location);
    1905                 :             : 
    1906                 :         441 :   if (code->expr1->symtree->n.sym->attr.dummy
    1907                 :         390 :       && (code->expr1->symtree->n.sym->attr.optional
    1908                 :         384 :           || code->expr1->symtree->n.sym->ns->proc_name->attr.entry_master))
    1909                 :             :     {
    1910                 :           6 :       tree present = gfc_conv_expr_present (code->expr1->symtree->n.sym);
    1911                 :           6 :       tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
    1912                 :             :                         present, tmp,
    1913                 :             :                         build_empty_stmt (input_location));
    1914                 :             :     }
    1915                 :             : 
    1916                 :         441 :   gfc_add_expr_to_block (&block, tmp);
    1917                 :         441 :   gfc_free_expr (lhs);
    1918                 :         441 :   gfc_free_expr (rhs);
    1919                 :             : 
    1920                 :         441 :   return gfc_finish_block (&block);
    1921                 :             : }
    1922                 :             : 
    1923                 :             : 
    1924                 :             : /* Class valued elemental function calls or class array elements arriving
    1925                 :             :    in gfc_trans_scalar_assign come here.  Wherever possible the vptr copy
    1926                 :             :    is used to ensure that the rhs dynamic type is assigned to the lhs.  */
    1927                 :             : 
    1928                 :             : static bool
    1929                 :         740 : trans_scalar_class_assign (stmtblock_t *block, gfc_se *lse, gfc_se *rse)
    1930                 :             : {
    1931                 :         740 :   tree fcn;
    1932                 :         740 :   tree rse_expr;
    1933                 :         740 :   tree class_data;
    1934                 :         740 :   tree tmp;
    1935                 :         740 :   tree zero;
    1936                 :         740 :   tree cond;
    1937                 :         740 :   tree final_cond;
    1938                 :         740 :   stmtblock_t inner_block;
    1939                 :         740 :   bool is_descriptor;
    1940                 :         740 :   bool not_call_expr = TREE_CODE (rse->expr) != CALL_EXPR;
    1941                 :         740 :   bool not_lhs_array_type;
    1942                 :             : 
    1943                 :             :   /* Temporaries arising from dependencies in assignment get cast as a
    1944                 :             :      character type of the dynamic size of the rhs. Use the vptr copy
    1945                 :             :      for this case.  */
    1946                 :         740 :   tmp = TREE_TYPE (lse->expr);
    1947                 :         740 :   not_lhs_array_type = !(tmp && TREE_CODE (tmp) == ARRAY_TYPE
    1948                 :           0 :                          && TYPE_MAX_VALUE (TYPE_DOMAIN (tmp)) != NULL_TREE);
    1949                 :             : 
    1950                 :             :   /* Use ordinary assignment if the rhs is not a call expression or
    1951                 :             :      the lhs is not a class entity or an array(ie. character) type.  */
    1952                 :         698 :   if ((not_call_expr && gfc_get_class_from_expr (lse->expr) == NULL_TREE)
    1953                 :        1001 :       && not_lhs_array_type)
    1954                 :             :     return false;
    1955                 :             : 
    1956                 :             :   /* Ordinary assignment can be used if both sides are class expressions
    1957                 :             :      since the dynamic type is preserved by copying the vptr.  This
    1958                 :             :      should only occur, where temporaries are involved.  */
    1959                 :         479 :   if (GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
    1960                 :         479 :       && GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
    1961                 :             :     return false;
    1962                 :             : 
    1963                 :             :   /* Fix the class expression and the class data of the rhs.  */
    1964                 :         418 :   if (!GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr))
    1965                 :         418 :       || not_call_expr)
    1966                 :             :     {
    1967                 :         418 :       tmp = gfc_get_class_from_expr (rse->expr);
    1968                 :         418 :       if (tmp == NULL_TREE)
    1969                 :             :         return false;
    1970                 :         134 :       rse_expr = gfc_evaluate_now (tmp, block);
    1971                 :             :     }
    1972                 :             :   else
    1973                 :           0 :     rse_expr = gfc_evaluate_now (rse->expr, block);
    1974                 :             : 
    1975                 :         134 :   class_data = gfc_class_data_get (rse_expr);
    1976                 :             : 
    1977                 :             :   /* Check that the rhs data is not null.  */
    1978                 :         134 :   is_descriptor = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (class_data));
    1979                 :         134 :   if (is_descriptor)
    1980                 :         134 :     class_data = gfc_conv_descriptor_data_get (class_data);
    1981                 :         134 :   class_data = gfc_evaluate_now (class_data, block);
    1982                 :             : 
    1983                 :         134 :   zero = build_int_cst (TREE_TYPE (class_data), 0);
    1984                 :         134 :   cond = fold_build2_loc (input_location, NE_EXPR,
    1985                 :             :                           logical_type_node,
    1986                 :             :                           class_data, zero);
    1987                 :             : 
    1988                 :             :   /* Copy the rhs to the lhs.  */
    1989                 :         134 :   fcn = gfc_vptr_copy_get (gfc_class_vptr_get (rse_expr));
    1990                 :         134 :   fcn = build_fold_indirect_ref_loc (input_location, fcn);
    1991                 :         134 :   tmp = gfc_evaluate_now (gfc_build_addr_expr (NULL, rse->expr), block);
    1992                 :         134 :   tmp = is_descriptor ? tmp : class_data;
    1993                 :         134 :   tmp = build_call_expr_loc (input_location, fcn, 2, tmp,
    1994                 :             :                              gfc_build_addr_expr (NULL, lse->expr));
    1995                 :         134 :   gfc_add_expr_to_block (block, tmp);
    1996                 :             : 
    1997                 :             :   /* Only elemental function results need to be finalised and freed.  */
    1998                 :         134 :   if (not_call_expr)
    1999                 :             :     return true;
    2000                 :             : 
    2001                 :             :   /* Finalize the class data if needed.  */
    2002                 :           0 :   gfc_init_block (&inner_block);
    2003                 :           0 :   fcn = gfc_vptr_final_get (gfc_class_vptr_get (rse_expr));
    2004                 :           0 :   zero = build_int_cst (TREE_TYPE (fcn), 0);
    2005                 :           0 :   final_cond = fold_build2_loc (input_location, NE_EXPR,
    2006                 :             :                                 logical_type_node, fcn, zero);
    2007                 :           0 :   fcn = build_fold_indirect_ref_loc (input_location, fcn);
    2008                 :           0 :   tmp = build_call_expr_loc (input_location, fcn, 1, class_data);
    2009                 :           0 :   tmp = build3_v (COND_EXPR, final_cond,
    2010                 :             :                   tmp, build_empty_stmt (input_location));
    2011                 :           0 :   gfc_add_expr_to_block (&inner_block, tmp);
    2012                 :             : 
    2013                 :             :   /* Free the class data.  */
    2014                 :           0 :   tmp = gfc_call_free (class_data);
    2015                 :           0 :   tmp = build3_v (COND_EXPR, cond, tmp,
    2016                 :             :                   build_empty_stmt (input_location));
    2017                 :           0 :   gfc_add_expr_to_block (&inner_block, tmp);
    2018                 :             : 
    2019                 :             :   /* Finish the inner block and subject it to the condition on the
    2020                 :             :      class data being non-zero.  */
    2021                 :           0 :   tmp = gfc_finish_block (&inner_block);
    2022                 :           0 :   tmp = build3_v (COND_EXPR, cond, tmp,
    2023                 :             :                   build_empty_stmt (input_location));
    2024                 :           0 :   gfc_add_expr_to_block (block, tmp);
    2025                 :             : 
    2026                 :           0 :   return true;
    2027                 :             : }
    2028                 :             : 
    2029                 :             : /* End of prototype trans-class.c  */
    2030                 :             : 
    2031                 :             : 
    2032                 :             : static void
    2033                 :       11503 : realloc_lhs_warning (bt type, bool array, locus *where)
    2034                 :             : {
    2035                 :       11503 :   if (array && type != BT_CLASS && type != BT_DERIVED && warn_realloc_lhs)
    2036                 :          25 :     gfc_warning (OPT_Wrealloc_lhs,
    2037                 :             :                  "Code for reallocating the allocatable array at %L will "
    2038                 :             :                  "be added", where);
    2039                 :       11478 :   else if (warn_realloc_lhs_all)
    2040                 :           4 :     gfc_warning (OPT_Wrealloc_lhs_all,
    2041                 :             :                  "Code for reallocating the allocatable variable at %L "
    2042                 :             :                  "will be added", where);
    2043                 :       11503 : }
    2044                 :             : 
    2045                 :             : 
    2046                 :             : static void gfc_apply_interface_mapping_to_expr (gfc_interface_mapping *,
    2047                 :             :                                                  gfc_expr *);
    2048                 :             : 
    2049                 :             : /* Copy the scalarization loop variables.  */
    2050                 :             : 
    2051                 :             : static void
    2052                 :     1215181 : gfc_copy_se_loopvars (gfc_se * dest, gfc_se * src)
    2053                 :             : {
    2054                 :     1215181 :   dest->ss = src->ss;
    2055                 :     1215181 :   dest->loop = src->loop;
    2056                 :     1215181 : }
    2057                 :             : 
    2058                 :             : 
    2059                 :             : /* Initialize a simple expression holder.
    2060                 :             : 
    2061                 :             :    Care must be taken when multiple se are created with the same parent.
    2062                 :             :    The child se must be kept in sync.  The easiest way is to delay creation
    2063                 :             :    of a child se until after the previous se has been translated.  */
    2064                 :             : 
    2065                 :             : void
    2066                 :     4349283 : gfc_init_se (gfc_se * se, gfc_se * parent)
    2067                 :             : {
    2068                 :     4349283 :   memset (se, 0, sizeof (gfc_se));
    2069                 :     4349283 :   gfc_init_block (&se->pre);
    2070                 :     4349283 :   gfc_init_block (&se->finalblock);
    2071                 :     4349283 :   gfc_init_block (&se->post);
    2072                 :             : 
    2073                 :     4349283 :   se->parent = parent;
    2074                 :             : 
    2075                 :     4349283 :   if (parent)
    2076                 :     1215181 :     gfc_copy_se_loopvars (se, parent);
    2077                 :     4349283 : }
    2078                 :             : 
    2079                 :             : 
    2080                 :             : /* Advances to the next SS in the chain.  Use this rather than setting
    2081                 :             :    se->ss = se->ss->next because all the parents needs to be kept in sync.
    2082                 :             :    See gfc_init_se.  */
    2083                 :             : 
    2084                 :             : void
    2085                 :      221344 : gfc_advance_se_ss_chain (gfc_se * se)
    2086                 :             : {
    2087                 :      221344 :   gfc_se *p;
    2088                 :             : 
    2089                 :      221344 :   gcc_assert (se != NULL && se->ss != NULL && se->ss != gfc_ss_terminator);
    2090                 :             : 
    2091                 :             :   p = se;
    2092                 :             :   /* Walk down the parent chain.  */
    2093                 :      576930 :   while (p != NULL)
    2094                 :             :     {
    2095                 :             :       /* Simple consistency check.  */
    2096                 :      355586 :       gcc_assert (p->parent == NULL || p->parent->ss == p->ss
    2097                 :             :                   || p->parent->ss->nested_ss == p->ss);
    2098                 :             : 
    2099                 :      355586 :       p->ss = p->ss->next;
    2100                 :             : 
    2101                 :      355586 :       p = p->parent;
    2102                 :             :     }
    2103                 :      221344 : }
    2104                 :             : 
    2105                 :             : 
    2106                 :             : /* Ensures the result of the expression as either a temporary variable
    2107                 :             :    or a constant so that it can be used repeatedly.  */
    2108                 :             : 
    2109                 :             : void
    2110                 :        7964 : gfc_make_safe_expr (gfc_se * se)
    2111                 :             : {
    2112                 :        7964 :   tree var;
    2113                 :             : 
    2114                 :        7964 :   if (CONSTANT_CLASS_P (se->expr))
    2115                 :             :     return;
    2116                 :             : 
    2117                 :             :   /* We need a temporary for this result.  */
    2118                 :         206 :   var = gfc_create_var (TREE_TYPE (se->expr), NULL);
    2119                 :         206 :   gfc_add_modify (&se->pre, var, se->expr);
    2120                 :         206 :   se->expr = var;
    2121                 :             : }
    2122                 :             : 
    2123                 :             : 
    2124                 :             : /* Return an expression which determines if a dummy parameter is present.
    2125                 :             :    Also used for arguments to procedures with multiple entry points.  */
    2126                 :             : 
    2127                 :             : tree
    2128                 :       11312 : gfc_conv_expr_present (gfc_symbol * sym, bool use_saved_desc)
    2129                 :             : {
    2130                 :       11312 :   tree decl, orig_decl, cond;
    2131                 :             : 
    2132                 :       11312 :   gcc_assert (sym->attr.dummy);
    2133                 :       11312 :   orig_decl = decl = gfc_get_symbol_decl (sym);
    2134                 :             : 
    2135                 :             :   /* Intrinsic scalars and derived types with VALUE attribute which are passed
    2136                 :             :      by value use a hidden argument to denote the presence status.  */
    2137                 :       11312 :   if (sym->attr.value && !sym->attr.dimension && sym->ts.type != BT_CLASS)
    2138                 :             :     {
    2139                 :        1040 :       char name[GFC_MAX_SYMBOL_LEN + 2];
    2140                 :        1040 :       tree tree_name;
    2141                 :             : 
    2142                 :        1040 :       gcc_assert (TREE_CODE (decl) == PARM_DECL);
    2143                 :        1040 :       name[0] = '.';
    2144                 :        1040 :       strcpy (&name[1], sym->name);
    2145                 :        1040 :       tree_name = get_identifier (name);
    2146                 :             : 
    2147                 :             :       /* Walk function argument list to find hidden arg.  */
    2148                 :        1040 :       cond = DECL_ARGUMENTS (DECL_CONTEXT (decl));
    2149                 :        5296 :       for ( ; cond != NULL_TREE; cond = TREE_CHAIN (cond))
    2150                 :        5296 :         if (DECL_NAME (cond) == tree_name
    2151                 :        5296 :             && DECL_ARTIFICIAL (cond))
    2152                 :             :           break;
    2153                 :             : 
    2154                 :        1040 :       gcc_assert (cond);
    2155                 :        1040 :       return cond;
    2156                 :             :     }
    2157                 :             : 
    2158                 :             :   /* Assumed-shape arrays use a local variable for the array data;
    2159                 :             :      the actual PARAM_DECL is in a saved decl.  As the local variable
    2160                 :             :      is NULL, it can be checked instead, unless use_saved_desc is
    2161                 :             :      requested.  */
    2162                 :             : 
    2163                 :       10272 :   if (use_saved_desc && TREE_CODE (decl) != PARM_DECL)
    2164                 :             :     {
    2165                 :         821 :       gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl))
    2166                 :             :              || GFC_ARRAY_TYPE_P (TREE_TYPE (decl)));
    2167                 :         821 :       decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
    2168                 :             :     }
    2169                 :             : 
    2170                 :       10272 :   cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node, decl,
    2171                 :       10272 :                           fold_convert (TREE_TYPE (decl), null_pointer_node));
    2172                 :             : 
    2173                 :             :   /* Fortran 2008 allows to pass null pointers and non-associated pointers
    2174                 :             :      as actual argument to denote absent dummies. For array descriptors,
    2175                 :             :      we thus also need to check the array descriptor.  For BT_CLASS, it
    2176                 :             :      can also occur for scalars and F2003 due to type->class wrapping and
    2177                 :             :      class->class wrapping.  Note further that BT_CLASS always uses an
    2178                 :             :      array descriptor for arrays, also for explicit-shape/assumed-size.
    2179                 :             :      For assumed-rank arrays, no local variable is generated, hence,
    2180                 :             :      the following also applies with !use_saved_desc.  */
    2181                 :             : 
    2182                 :       10272 :   if ((use_saved_desc || TREE_CODE (orig_decl) == PARM_DECL)
    2183                 :        7351 :       && !sym->attr.allocatable
    2184                 :        6185 :       && ((sym->ts.type != BT_CLASS && !sym->attr.pointer)
    2185                 :        2266 :           || (sym->ts.type == BT_CLASS
    2186                 :        1041 :               && !CLASS_DATA (sym)->attr.allocatable
    2187                 :        1041 :               && !CLASS_DATA (sym)->attr.class_pointer))
    2188                 :        4126 :       && ((gfc_option.allow_std & GFC_STD_F2008) != 0
    2189                 :           6 :           || sym->ts.type == BT_CLASS))
    2190                 :             :     {
    2191                 :        4120 :       tree tmp;
    2192                 :             : 
    2193                 :        4120 :       if ((sym->as && (sym->as->type == AS_ASSUMED_SHAPE
    2194                 :        1455 :                        || sym->as->type == AS_ASSUMED_RANK
    2195                 :        1373 :                        || sym->attr.codimension))
    2196                 :        3259 :           || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)->as))
    2197                 :             :         {
    2198                 :        1032 :           tmp = build_fold_indirect_ref_loc (input_location, decl);
    2199                 :        1032 :           if (sym->ts.type == BT_CLASS)
    2200                 :         171 :             tmp = gfc_class_data_get (tmp);
    2201                 :        1032 :           tmp = gfc_conv_array_data (tmp);
    2202                 :             :         }
    2203                 :        3088 :       else if (sym->ts.type == BT_CLASS)
    2204                 :          36 :         tmp = gfc_class_data_get (decl);
    2205                 :             :       else
    2206                 :             :         tmp = NULL_TREE;
    2207                 :             : 
    2208                 :        1068 :       if (tmp != NULL_TREE)
    2209                 :             :         {
    2210                 :        1068 :           tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node, tmp,
    2211                 :        1068 :                                  fold_convert (TREE_TYPE (tmp), null_pointer_node));
    2212                 :        1068 :           cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    2213                 :             :                                   logical_type_node, cond, tmp);
    2214                 :             :         }
    2215                 :             :     }
    2216                 :             : 
    2217                 :             :   return cond;
    2218                 :             : }
    2219                 :             : 
    2220                 :             : 
    2221                 :             : /* Converts a missing, dummy argument into a null or zero.  */
    2222                 :             : 
    2223                 :             : void
    2224                 :         843 : gfc_conv_missing_dummy (gfc_se * se, gfc_expr * arg, gfc_typespec ts, int kind)
    2225                 :             : {
    2226                 :         843 :   tree present;
    2227                 :         843 :   tree tmp;
    2228                 :             : 
    2229                 :         843 :   present = gfc_conv_expr_present (arg->symtree->n.sym);
    2230                 :             : 
    2231                 :         843 :   if (kind > 0)
    2232                 :             :     {
    2233                 :             :       /* Create a temporary and convert it to the correct type.  */
    2234                 :          54 :       tmp = gfc_get_int_type (kind);
    2235                 :          54 :       tmp = fold_convert (tmp, build_fold_indirect_ref_loc (input_location,
    2236                 :             :                                                         se->expr));
    2237                 :             : 
    2238                 :             :       /* Test for a NULL value.  */
    2239                 :          54 :       tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), present,
    2240                 :          54 :                         tmp, fold_convert (TREE_TYPE (tmp), integer_one_node));
    2241                 :          54 :       tmp = gfc_evaluate_now (tmp, &se->pre);
    2242                 :          54 :       se->expr = gfc_build_addr_expr (NULL_TREE, tmp);
    2243                 :             :     }
    2244                 :             :   else
    2245                 :             :     {
    2246                 :         789 :       tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (se->expr),
    2247                 :             :                         present, se->expr,
    2248                 :         789 :                         build_zero_cst (TREE_TYPE (se->expr)));
    2249                 :         789 :       tmp = gfc_evaluate_now (tmp, &se->pre);
    2250                 :         789 :       se->expr = tmp;
    2251                 :             :     }
    2252                 :             : 
    2253                 :         843 :   if (ts.type == BT_CHARACTER)
    2254                 :             :     {
    2255                 :             :       /* Handle deferred-length dummies that pass the character length by
    2256                 :             :          reference so that the value can be returned.  */
    2257                 :         244 :       if (ts.deferred && INDIRECT_REF_P (se->string_length))
    2258                 :             :         {
    2259                 :          18 :           tmp = gfc_build_addr_expr (NULL_TREE, se->string_length);
    2260                 :          18 :           tmp = fold_build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
    2261                 :             :                                  present, tmp, null_pointer_node);
    2262                 :          18 :           tmp = gfc_evaluate_now (tmp, &se->pre);
    2263                 :          18 :           tmp = build_fold_indirect_ref_loc (input_location, tmp);
    2264                 :             :         }
    2265                 :             :       else
    2266                 :             :         {
    2267                 :         226 :           tmp = build_int_cst (gfc_charlen_type_node, 0);
    2268                 :         226 :           tmp = fold_build3_loc (input_location, COND_EXPR,
    2269                 :             :                                  gfc_charlen_type_node,
    2270                 :             :                                  present, se->string_length, tmp);
    2271                 :         226 :           tmp = gfc_evaluate_now (tmp, &se->pre);
    2272                 :             :         }
    2273                 :         244 :       se->string_length = tmp;
    2274                 :             :     }
    2275                 :         843 :   return;
    2276                 :             : }
    2277                 :             : 
    2278                 :             : 
    2279                 :             : /* Get the character length of an expression, looking through gfc_refs
    2280                 :             :    if necessary.  */
    2281                 :             : 
    2282                 :             : tree
    2283                 :       19334 : gfc_get_expr_charlen (gfc_expr *e)
    2284                 :             : {
    2285                 :       19334 :   gfc_ref *r;
    2286                 :       19334 :   tree length;
    2287                 :       19334 :   tree previous = NULL_TREE;
    2288                 :       19334 :   gfc_se se;
    2289                 :             : 
    2290                 :       19334 :   gcc_assert (e->expr_type == EXPR_VARIABLE
    2291                 :             :               && e->ts.type == BT_CHARACTER);
    2292                 :             : 
    2293                 :       19334 :   length = NULL; /* To silence compiler warning.  */
    2294                 :             : 
    2295                 :       19334 :   if (is_subref_array (e) && e->ts.u.cl->length)
    2296                 :             :     {
    2297                 :         760 :       gfc_se tmpse;
    2298                 :         760 :       gfc_init_se (&tmpse, NULL);
    2299                 :         760 :       gfc_conv_expr_type (&tmpse, e->ts.u.cl->length, gfc_charlen_type_node);
    2300                 :         760 :       e->ts.u.cl->backend_decl = tmpse.expr;
    2301                 :         760 :       return tmpse.expr;
    2302                 :             :     }
    2303                 :             : 
    2304                 :             :   /* First candidate: if the variable is of type CHARACTER, the
    2305                 :             :      expression's length could be the length of the character
    2306                 :             :      variable.  */
    2307                 :       18574 :   if (e->symtree->n.sym->ts.type == BT_CHARACTER)
    2308                 :       18280 :     length = e->symtree->n.sym->ts.u.cl->backend_decl;
    2309                 :             : 
    2310                 :             :   /* Look through the reference chain for component references.  */
    2311                 :       37299 :   for (r = e->ref; r; r = r->next)
    2312                 :             :     {
    2313                 :       18725 :       previous = length;
    2314                 :       18725 :       switch (r->type)
    2315                 :             :         {
    2316                 :         294 :         case REF_COMPONENT:
    2317                 :         294 :           if (r->u.c.component->ts.type == BT_CHARACTER)
    2318                 :         294 :             length = r->u.c.component->ts.u.cl->backend_decl;
    2319                 :             :           break;
    2320                 :             : 
    2321                 :             :         case REF_ARRAY:
    2322                 :             :           /* Do nothing.  */
    2323                 :             :           break;
    2324                 :             : 
    2325                 :          26 :         case REF_SUBSTRING:
    2326                 :          26 :           gfc_init_se (&se, NULL);
    2327                 :          26 :           gfc_conv_expr_type (&se, r->u.ss.start, gfc_charlen_type_node);
    2328                 :          26 :           length = se.expr;
    2329                 :          26 :           if (r->u.ss.end)
    2330                 :           6 :             gfc_conv_expr_type (&se, r->u.ss.end, gfc_charlen_type_node);
    2331                 :             :           else
    2332                 :          20 :             se.expr = previous;
    2333                 :          26 :           length = fold_build2_loc (input_location, MINUS_EXPR,
    2334                 :             :                                     gfc_charlen_type_node,
    2335                 :             :                                     se.expr, length);
    2336                 :          26 :           length = fold_build2_loc (input_location, PLUS_EXPR,
    2337                 :             :                                     gfc_charlen_type_node, length,
    2338                 :             :                                     gfc_index_one_node);
    2339                 :          26 :           break;
    2340                 :             : 
    2341                 :           0 :         default:
    2342                 :           0 :           gcc_unreachable ();
    2343                 :       18725 :           break;
    2344                 :             :         }
    2345                 :             :     }
    2346                 :             : 
    2347                 :       18574 :   gcc_assert (length != NULL);
    2348                 :             :   return length;
    2349                 :             : }
    2350                 :             : 
    2351                 :             : 
    2352                 :             : /* Return for an expression the backend decl of the coarray.  */
    2353                 :             : 
    2354                 :             : tree
    2355                 :        1452 : gfc_get_tree_for_caf_expr (gfc_expr *expr)
    2356                 :             : {
    2357                 :        1452 :   tree caf_decl;
    2358                 :        1452 :   bool found = false;
    2359                 :        1452 :   gfc_ref *ref;
    2360                 :             : 
    2361                 :        1452 :   gcc_assert (expr && expr->expr_type == EXPR_VARIABLE);
    2362                 :             : 
    2363                 :             :   /* Not-implemented diagnostic.  */
    2364                 :        1452 :   if (expr->symtree->n.sym->ts.type == BT_CLASS
    2365                 :          23 :       && UNLIMITED_POLY (expr->symtree->n.sym)
    2366                 :           0 :       && CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
    2367                 :           0 :     gfc_error ("Sorry, coindexed access to an unlimited polymorphic object at "
    2368                 :             :                "%L is not supported", &expr->where);
    2369                 :             : 
    2370                 :        3108 :   for (ref = expr->ref; ref; ref = ref->next)
    2371                 :        1656 :     if (ref->type == REF_COMPONENT)
    2372                 :             :       {
    2373                 :         168 :         if (ref->u.c.component->ts.type == BT_CLASS
    2374                 :           0 :             && UNLIMITED_POLY (ref->u.c.component)
    2375                 :           0 :             && CLASS_DATA (ref->u.c.component)->attr.codimension)
    2376                 :           0 :           gfc_error ("Sorry, coindexed access to an unlimited polymorphic "
    2377                 :             :                      "component at %L is not supported", &expr->where);
    2378                 :             :       }
    2379                 :             : 
    2380                 :             :   /* Make sure the backend_decl is present before accessing it.  */
    2381                 :        2904 :   caf_decl = expr->symtree->n.sym->backend_decl == NULL_TREE
    2382                 :        1452 :       ? gfc_get_symbol_decl (expr->symtree->n.sym)
    2383                 :             :       : expr->symtree->n.sym->backend_decl;
    2384                 :             : 
    2385                 :        1452 :   if (expr->symtree->n.sym->ts.type == BT_CLASS)
    2386                 :             :     {
    2387                 :          23 :       if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
    2388                 :          26 :           && GFC_DECL_SAVED_DESCRIPTOR (caf_decl))
    2389                 :           3 :         caf_decl = GFC_DECL_SAVED_DESCRIPTOR (caf_decl);
    2390                 :             : 
    2391                 :          23 :       if (expr->ref && expr->ref->type == REF_ARRAY)
    2392                 :             :         {
    2393                 :          16 :           caf_decl = gfc_class_data_get (caf_decl);
    2394                 :          16 :           if (CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
    2395                 :             :             return caf_decl;
    2396                 :             :         }
    2397                 :           7 :       else if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
    2398                 :           1 :                && GFC_DECL_TOKEN (caf_decl)
    2399                 :           8 :                && CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
    2400                 :             :         return caf_decl;
    2401                 :             : 
    2402                 :          15 :       for (ref = expr->ref; ref; ref = ref->next)
    2403                 :             :         {
    2404                 :          12 :           if (ref->type == REF_COMPONENT
    2405                 :           6 :               && strcmp (ref->u.c.component->name, "_data") != 0)
    2406                 :             :             {
    2407                 :           0 :               caf_decl = gfc_class_data_get (caf_decl);
    2408                 :           0 :               if (CLASS_DATA (expr->symtree->n.sym)->attr.codimension)
    2409                 :             :                 return caf_decl;
    2410                 :             :               break;
    2411                 :             :             }
    2412                 :          12 :           else if (ref->type == REF_ARRAY && ref->u.ar.dimen)
    2413                 :             :             break;
    2414                 :             :         }
    2415                 :             :     }
    2416                 :        1435 :   if (expr->symtree->n.sym->attr.codimension)
    2417                 :             :     return caf_decl;
    2418                 :             : 
    2419                 :             :   /* The following code assumes that the coarray is a component reachable via
    2420                 :             :      only scalar components/variables; the Fortran standard guarantees this.  */
    2421                 :             : 
    2422                 :          27 :   for (ref = expr->ref; ref; ref = ref->next)
    2423                 :          27 :     if (ref->type == REF_COMPONENT)
    2424                 :             :       {
    2425                 :          27 :         gfc_component *comp = ref->u.c.component;
    2426                 :             : 
    2427                 :          27 :         if (POINTER_TYPE_P (TREE_TYPE (caf_decl)))
    2428                 :           0 :           caf_decl = build_fold_indirect_ref_loc (input_location, caf_decl);
    2429                 :          27 :         caf_decl = fold_build3_loc (input_location, COMPONENT_REF,
    2430                 :          27 :                                     TREE_TYPE (comp->backend_decl), caf_decl,
    2431                 :             :                                     comp->backend_decl, NULL_TREE);
    2432                 :          27 :         if (comp->ts.type == BT_CLASS)
    2433                 :             :           {
    2434                 :           0 :             caf_decl = gfc_class_data_get (caf_decl);
    2435                 :           0 :             if (CLASS_DATA (comp)->attr.codimension)
    2436                 :             :               {
    2437                 :             :                 found = true;
    2438                 :             :                 break;
    2439                 :             :               }
    2440                 :             :           }
    2441                 :          27 :         if (comp->attr.codimension)
    2442                 :             :           {
    2443                 :             :             found = true;
    2444                 :             :             break;
    2445                 :             :           }
    2446                 :             :       }
    2447                 :          27 :   gcc_assert (found && caf_decl);
    2448                 :             :   return caf_decl;
    2449                 :             : }
    2450                 :             : 
    2451                 :             : 
    2452                 :             : /* Obtain the Coarray token - and optionally also the offset.  */
    2453                 :             : 
    2454                 :             : void
    2455                 :        1369 : gfc_get_caf_token_offset (gfc_se *se, tree *token, tree *offset, tree caf_decl,
    2456                 :             :                           tree se_expr, gfc_expr *expr)
    2457                 :             : {
    2458                 :        1369 :   tree tmp;
    2459                 :             : 
    2460                 :        1369 :   gcc_assert (flag_coarray == GFC_FCOARRAY_LIB);
    2461                 :             : 
    2462                 :             :   /* Coarray token.  */
    2463                 :        1369 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
    2464                 :         273 :       *token = gfc_conv_descriptor_token (caf_decl);
    2465                 :        1095 :   else if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
    2466                 :        1197 :            && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
    2467                 :           4 :     *token = GFC_DECL_TOKEN (caf_decl);
    2468                 :             :   else
    2469                 :             :     {
    2470                 :        1092 :       gcc_assert (GFC_ARRAY_TYPE_P (TREE_TYPE (caf_decl))
    2471                 :             :                   && GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl)) != NULL_TREE);
    2472                 :        1092 :       *token = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (caf_decl));
    2473                 :             :     }
    2474                 :             : 
    2475                 :        1369 :   if (offset == NULL)
    2476                 :             :     return;
    2477                 :             : 
    2478                 :             :   /* Offset between the coarray base address and the address wanted.  */
    2479                 :          91 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl))
    2480                 :          91 :       && (GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_ALLOCATABLE
    2481                 :           0 :           || GFC_TYPE_ARRAY_AKIND (TREE_TYPE (caf_decl)) == GFC_ARRAY_POINTER))
    2482                 :           0 :     *offset = build_int_cst (gfc_array_index_type, 0);
    2483                 :          91 :   else if (DECL_P (caf_decl) && DECL_LANG_SPECIFIC (caf_decl)
    2484                 :          91 :            && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
    2485                 :           0 :     *offset = GFC_DECL_CAF_OFFSET (caf_decl);
    2486                 :          91 :   else if (GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl)) != NULL_TREE)
    2487                 :           0 :     *offset = GFC_TYPE_ARRAY_CAF_OFFSET (TREE_TYPE (caf_decl));
    2488                 :             :   else
    2489                 :          91 :     *offset = build_int_cst (gfc_array_index_type, 0);
    2490                 :             : 
    2491                 :          91 :   if (POINTER_TYPE_P (TREE_TYPE (se_expr))
    2492                 :          91 :       && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se_expr))))
    2493                 :             :     {
    2494                 :           0 :       tmp = build_fold_indirect_ref_loc (input_location, se_expr);
    2495                 :           0 :       tmp = gfc_conv_descriptor_data_get (tmp);
    2496                 :             :     }
    2497                 :          91 :   else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se_expr)))
    2498                 :           0 :     tmp = gfc_conv_descriptor_data_get (se_expr);
    2499                 :             :   else
    2500                 :             :     {
    2501                 :          91 :       gcc_assert (POINTER_TYPE_P (TREE_TYPE (se_expr)));
    2502                 :             :       tmp = se_expr;
    2503                 :             :     }
    2504                 :             : 
    2505                 :          91 :   *offset = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    2506                 :             :                              *offset, fold_convert (gfc_array_index_type, tmp));
    2507                 :             : 
    2508                 :          91 :   if (expr->symtree->n.sym->ts.type == BT_DERIVED
    2509                 :           0 :       && expr->symtree->n.sym->attr.codimension
    2510                 :           0 :       && expr->symtree->n.sym->ts.u.derived->attr.alloc_comp)
    2511                 :             :     {
    2512                 :           0 :       gfc_expr *base_expr = gfc_copy_expr (expr);
    2513                 :           0 :       gfc_ref *ref = base_expr->ref;
    2514                 :           0 :       gfc_se base_se;
    2515                 :             : 
    2516                 :             :       // Iterate through the refs until the last one.
    2517                 :           0 :       while (ref->next)
    2518                 :             :           ref = ref->next;
    2519                 :             : 
    2520                 :           0 :       if (ref->type == REF_ARRAY
    2521                 :           0 :           && ref->u.ar.type != AR_FULL)
    2522                 :             :         {
    2523                 :           0 :           const int ranksum = ref->u.ar.dimen + ref->u.ar.codimen;
    2524                 :           0 :           int i;
    2525                 :           0 :           for (i = 0; i < ranksum; ++i)
    2526                 :             :             {
    2527                 :           0 :               ref->u.ar.start[i] = NULL;
    2528                 :           0 :               ref->u.ar.end[i] = NULL;
    2529                 :             :             }
    2530                 :           0 :           ref->u.ar.type = AR_FULL;
    2531                 :             :         }
    2532                 :           0 :       gfc_init_se (&base_se, NULL);
    2533                 :           0 :       if (gfc_caf_attr (base_expr).dimension)
    2534                 :             :         {
    2535                 :           0 :           gfc_conv_expr_descriptor (&base_se, base_expr);
    2536                 :           0 :           tmp = gfc_conv_descriptor_data_get (base_se.expr);
    2537                 :             :         }
    2538                 :             :       else
    2539                 :             :         {
    2540                 :           0 :           gfc_conv_expr (&base_se, base_expr);
    2541                 :           0 :           tmp = base_se.expr;
    2542                 :             :         }
    2543                 :             : 
    2544                 :           0 :       gfc_free_expr (base_expr);
    2545                 :           0 :       gfc_add_block_to_block (&se->pre, &base_se.pre);
    2546                 :           0 :       gfc_add_block_to_block (&se->post, &base_se.post);
    2547                 :           0 :     }
    2548                 :          91 :   else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_decl)))
    2549                 :           0 :     tmp = gfc_conv_descriptor_data_get (caf_decl);
    2550                 :          91 :   else if (INDIRECT_REF_P (caf_decl))
    2551                 :           0 :     tmp = TREE_OPERAND (caf_decl, 0);
    2552                 :             :   else
    2553                 :             :     {
    2554                 :          91 :       gcc_assert (POINTER_TYPE_P (TREE_TYPE (caf_decl)));
    2555                 :             :       tmp = caf_decl;
    2556                 :             :     }
    2557                 :             : 
    2558                 :          91 :   *offset = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    2559                 :             :                             fold_convert (gfc_array_index_type, *offset),
    2560                 :             :                             fold_convert (gfc_array_index_type, tmp));
    2561                 :             : }
    2562                 :             : 
    2563                 :             : 
    2564                 :             : /* Convert the coindex of a coarray into an image index; the result is
    2565                 :             :    image_num =  (idx(1)-lcobound(1)+1) + (idx(2)-lcobound(2))*extent(1)
    2566                 :             :               + (idx(3)-lcobound(3))*extend(1)*extent(2) + ...  */
    2567                 :             : 
    2568                 :             : tree
    2569                 :        1174 : gfc_caf_get_image_index (stmtblock_t *block, gfc_expr *e, tree desc)
    2570                 :             : {
    2571                 :        1174 :   gfc_ref *ref;
    2572                 :        1174 :   tree lbound, ubound, extent, tmp, img_idx;
    2573                 :        1174 :   gfc_se se;
    2574                 :        1174 :   int i;
    2575                 :             : 
    2576                 :        1189 :   for (ref = e->ref; ref; ref = ref->next)
    2577                 :        1189 :     if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
    2578                 :             :       break;
    2579                 :        1174 :   gcc_assert (ref != NULL);
    2580                 :             : 
    2581                 :        1174 :   if (ref->u.ar.dimen_type[ref->u.ar.dimen] == DIMEN_THIS_IMAGE)
    2582                 :             :     {
    2583                 :           0 :       return build_call_expr_loc (input_location, gfor_fndecl_caf_this_image, 1,
    2584                 :           0 :                                   integer_zero_node);
    2585                 :             :     }
    2586                 :             : 
    2587                 :        1174 :   img_idx = build_zero_cst (gfc_array_index_type);
    2588                 :        1174 :   extent = build_one_cst (gfc_array_index_type);
    2589                 :        1174 :   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
    2590                 :         420 :     for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
    2591                 :             :       {
    2592                 :         213 :         gfc_init_se (&se, NULL);
    2593                 :         213 :         gfc_conv_expr_type (&se, ref->u.ar.start[i], gfc_array_index_type);
    2594                 :         213 :         gfc_add_block_to_block (block, &se.pre);
    2595                 :         213 :         lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[i]);
    2596                 :         213 :         tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2597                 :         213 :                                TREE_TYPE (lbound), se.expr, lbound);
    2598                 :         213 :         tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
    2599                 :             :                                extent, tmp);
    2600                 :         213 :         img_idx = fold_build2_loc (input_location, PLUS_EXPR,
    2601                 :         213 :                                    TREE_TYPE (tmp), img_idx, tmp);
    2602                 :         213 :         if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
    2603                 :             :           {
    2604                 :           6 :             ubound = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[i]);
    2605                 :           6 :             tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
    2606                 :           6 :             extent = fold_build2_loc (input_location, MULT_EXPR,
    2607                 :           6 :                                       TREE_TYPE (tmp), extent, tmp);
    2608                 :             :           }
    2609                 :             :       }
    2610                 :             :   else
    2611                 :        1942 :     for (i = ref->u.ar.dimen; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
    2612                 :             :       {
    2613                 :         975 :         gfc_init_se (&se, NULL);
    2614                 :         975 :         gfc_conv_expr_type (&se, ref->u.ar.start[i], gfc_array_index_type);
    2615                 :         975 :         gfc_add_block_to_block (block, &se.pre);
    2616                 :         975 :         lbound = GFC_TYPE_ARRAY_LBOUND (TREE_TYPE (desc), i);
    2617                 :         975 :         tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2618                 :         975 :                                TREE_TYPE (lbound), se.expr, lbound);
    2619                 :         975 :         tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (tmp),
    2620                 :             :                                extent, tmp);
    2621                 :         975 :         img_idx = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
    2622                 :             :                                    img_idx, tmp);
    2623                 :         975 :         if (i < ref->u.ar.dimen + ref->u.ar.codimen - 1)
    2624                 :             :           {
    2625                 :           8 :             ubound = GFC_TYPE_ARRAY_UBOUND (TREE_TYPE (desc), i);
    2626                 :           8 :             tmp = fold_build2_loc (input_location, MINUS_EXPR,
    2627                 :           8 :                                    TREE_TYPE (ubound), ubound, lbound);
    2628                 :           8 :             tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (tmp),
    2629                 :           8 :                                    tmp, build_one_cst (TREE_TYPE (tmp)));
    2630                 :           8 :             extent = fold_build2_loc (input_location, MULT_EXPR,
    2631                 :           8 :                                       TREE_TYPE (tmp), extent, tmp);
    2632                 :             :           }
    2633                 :             :       }
    2634                 :        1174 :   img_idx = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (img_idx),
    2635                 :        1174 :                              img_idx, build_one_cst (TREE_TYPE (img_idx)));
    2636                 :        1174 :   return fold_convert (integer_type_node, img_idx);
    2637                 :             : }
    2638                 :             : 
    2639                 :             : 
    2640                 :             : /* For each character array constructor subexpression without a ts.u.cl->length,
    2641                 :             :    replace it by its first element (if there aren't any elements, the length
    2642                 :             :    should already be set to zero).  */
    2643                 :             : 
    2644                 :             : static void
    2645                 :         105 : flatten_array_ctors_without_strlen (gfc_expr* e)
    2646                 :             : {
    2647                 :         105 :   gfc_actual_arglist* arg;
    2648                 :         105 :   gfc_constructor* c;
    2649                 :             : 
    2650                 :         105 :   if (!e)
    2651                 :             :     return;
    2652                 :             : 
    2653                 :         105 :   switch (e->expr_type)
    2654                 :             :     {
    2655                 :             : 
    2656                 :           0 :     case EXPR_OP:
    2657                 :           0 :       flatten_array_ctors_without_strlen (e->value.op.op1);
    2658                 :           0 :       flatten_array_ctors_without_strlen (e->value.op.op2);
    2659                 :           0 :       break;
    2660                 :             : 
    2661                 :           0 :     case EXPR_COMPCALL:
    2662                 :             :       /* TODO: Implement as with EXPR_FUNCTION when needed.  */
    2663                 :           0 :       gcc_unreachable ();
    2664                 :             : 
    2665                 :          12 :     case EXPR_FUNCTION:
    2666                 :          36 :       for (arg = e->value.function.actual; arg; arg = arg->next)
    2667                 :          24 :         flatten_array_ctors_without_strlen (arg->expr);
    2668                 :             :       break;
    2669                 :             : 
    2670                 :           0 :     case EXPR_ARRAY:
    2671                 :             : 
    2672                 :             :       /* We've found what we're looking for.  */
    2673                 :           0 :       if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
    2674                 :             :         {
    2675                 :           0 :           gfc_constructor *c;
    2676                 :           0 :           gfc_expr* new_expr;
    2677                 :             : 
    2678                 :           0 :           gcc_assert (e->value.constructor);
    2679                 :             : 
    2680                 :           0 :           c = gfc_constructor_first (e->value.constructor);
    2681                 :           0 :           new_expr = c->expr;
    2682                 :           0 :           c->expr = NULL;
    2683                 :             : 
    2684                 :           0 :           flatten_array_ctors_without_strlen (new_expr);
    2685                 :           0 :           gfc_replace_expr (e, new_expr);
    2686                 :           0 :           break;
    2687                 :             :         }
    2688                 :             : 
    2689                 :             :       /* Otherwise, fall through to handle constructor elements.  */
    2690                 :           0 :       gcc_fallthrough ();
    2691                 :           0 :     case EXPR_STRUCTURE:
    2692                 :           0 :       for (c = gfc_constructor_first (e->value.constructor);
    2693                 :           0 :            c; c = gfc_constructor_next (c))
    2694                 :           0 :         flatten_array_ctors_without_strlen (c->expr);
    2695                 :             :       break;
    2696                 :             : 
    2697                 :             :     default:
    2698                 :             :       break;
    2699                 :             : 
    2700                 :             :     }
    2701                 :             : }
    2702                 :             : 
    2703                 :             : 
    2704                 :             : /* Generate code to initialize a string length variable. Returns the
    2705                 :             :    value.  For array constructors, cl->length might be NULL and in this case,
    2706                 :             :    the first element of the constructor is needed.  expr is the original
    2707                 :             :    expression so we can access it but can be NULL if this is not needed.  */
    2708                 :             : 
    2709                 :             : void
    2710                 :        3657 : gfc_conv_string_length (gfc_charlen * cl, gfc_expr * expr, stmtblock_t * pblock)
    2711                 :             : {
    2712                 :        3657 :   gfc_se se;
    2713                 :             : 
    2714                 :        3657 :   gfc_init_se (&se, NULL);
    2715                 :             : 
    2716                 :        3657 :   if (!cl->length && cl->backend_decl && VAR_P (cl->backend_decl))
    2717                 :        1271 :     return;
    2718                 :             : 
    2719                 :             :   /* If cl->length is NULL, use gfc_conv_expr to obtain the string length but
    2720                 :             :      "flatten" array constructors by taking their first element; all elements
    2721                 :             :      should be the same length or a cl->length should be present.  */
    2722                 :        2466 :   if (!cl->length)
    2723                 :             :     {
    2724                 :         161 :       gfc_expr* expr_flat;
    2725                 :         161 :       if (!expr)
    2726                 :             :         return;
    2727                 :          81 :       expr_flat = gfc_copy_expr (expr);
    2728                 :          81 :       flatten_array_ctors_without_strlen (expr_flat);
    2729                 :          81 :       gfc_resolve_expr (expr_flat);
    2730                 :          81 :       if (expr_flat->rank)
    2731                 :          12 :         gfc_conv_expr_descriptor (&se, expr_flat);
    2732                 :             :       else
    2733                 :          69 :         gfc_conv_expr (&se, expr_flat);
    2734                 :          81 :       if (expr_flat->expr_type != EXPR_VARIABLE)
    2735                 :          75 :         gfc_add_block_to_block (pblock, &se.pre);
    2736                 :          81 :       se.expr = convert (gfc_charlen_type_node, se.string_length);
    2737                 :          81 :       gfc_add_block_to_block (pblock, &se.post);
    2738                 :          81 :       gfc_free_expr (expr_flat);
    2739                 :             :     }
    2740                 :             :   else
    2741                 :             :     {
    2742                 :             :       /* Convert cl->length.  */
    2743                 :        2305 :       gfc_conv_expr_type (&se, cl->length, gfc_charlen_type_node);
    2744                 :        2305 :       se.expr = fold_build2_loc (input_location, MAX_EXPR,
    2745                 :             :                                  gfc_charlen_type_node, se.expr,
    2746                 :        2305 :                                  build_zero_cst (TREE_TYPE (se.expr)));
    2747                 :        2305 :       gfc_add_block_to_block (pblock, &se.pre);
    2748                 :             :     }
    2749                 :             : 
    2750                 :        2386 :   if (cl->backend_decl && VAR_P (cl->backend_decl))
    2751                 :        1539 :     gfc_add_modify (pblock, cl->backend_decl, se.expr);
    2752                 :             :   else
    2753                 :         847 :     cl->backend_decl = gfc_evaluate_now (se.expr, pblock);
    2754                 :             : }
    2755                 :             : 
    2756                 :             : 
    2757                 :             : static void
    2758                 :        6768 : gfc_conv_substring (gfc_se * se, gfc_ref * ref, int kind,
    2759                 :             :                     const char *name, locus *where)
    2760                 :             : {
    2761                 :        6768 :   tree tmp;
    2762                 :        6768 :   tree type;
    2763                 :        6768 :   tree fault;
    2764                 :        6768 :   gfc_se start;
    2765                 :        6768 :   gfc_se end;
    2766                 :        6768 :   char *msg;
    2767                 :        6768 :   mpz_t length;
    2768                 :             : 
    2769                 :        6768 :   type = gfc_get_character_type (kind, ref->u.ss.length);
    2770                 :        6768 :   type = build_pointer_type (type);
    2771                 :             : 
    2772                 :        6768 :   gfc_init_se (&start, se);
    2773                 :        6768 :   gfc_conv_expr_type (&start, ref->u.ss.start, gfc_charlen_type_node);
    2774                 :        6768 :   gfc_add_block_to_block (&se->pre, &start.pre);
    2775                 :             : 
    2776                 :        6768 :   if (integer_onep (start.expr))
    2777                 :        2308 :     gfc_conv_string_parameter (se);
    2778                 :             :   else
    2779                 :             :     {
    2780                 :        4460 :       tmp = start.expr;
    2781                 :        4460 :       STRIP_NOPS (tmp);
    2782                 :             :       /* Avoid multiple evaluation of substring start.  */
    2783                 :        4460 :       if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
    2784                 :        1664 :         start.expr = gfc_evaluate_now (start.expr, &se->pre);
    2785                 :             : 
    2786                 :             :       /* Change the start of the string.  */
    2787                 :        4460 :       if ((TREE_CODE (TREE_TYPE (se->expr)) == ARRAY_TYPE
    2788                 :        1140 :            || TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE)
    2789                 :        4580 :           && TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
    2790                 :             :         tmp = se->expr;
    2791                 :             :       else
    2792                 :        1020 :         tmp = build_fold_indirect_ref_loc (input_location,
    2793                 :             :                                        se->expr);
    2794                 :             :       /* For BIND(C), a BT_CHARACTER is not an ARRAY_TYPE.  */
    2795                 :        4460 :       if (TREE_CODE (TREE_TYPE (tmp)) == ARRAY_TYPE)
    2796                 :             :         {
    2797                 :        4340 :           tmp = gfc_build_array_ref (tmp, start.expr, NULL_TREE, true);
    2798                 :        4340 :           se->expr = gfc_build_addr_expr (type, tmp);
    2799                 :             :         }
    2800                 :             :     }
    2801                 :             : 
    2802                 :             :   /* Length = end + 1 - start.  */
    2803                 :        6768 :   gfc_init_se (&end, se);
    2804                 :        6768 :   if (ref->u.ss.end == NULL)
    2805                 :         177 :     end.expr = se->string_length;
    2806                 :             :   else
    2807                 :             :     {
    2808                 :        6591 :       gfc_conv_expr_type (&end, ref->u.ss.end, gfc_charlen_type_node);
    2809                 :        6591 :       gfc_add_block_to_block (&se->pre, &end.pre);
    2810                 :             :     }
    2811                 :        6768 :   tmp = end.expr;
    2812                 :        6768 :   STRIP_NOPS (tmp);
    2813                 :        6768 :   if (!CONSTANT_CLASS_P (tmp) && !DECL_P (tmp))
    2814                 :        2289 :     end.expr = gfc_evaluate_now (end.expr, &se->pre);
    2815                 :             : 
    2816                 :        6768 :   if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    2817                 :         460 :       && !gfc_contains_implied_index_p (ref->u.ss.start)
    2818                 :        7209 :       && !gfc_contains_implied_index_p (ref->u.ss.end))
    2819                 :             :     {
    2820                 :         441 :       tree nonempty = fold_build2_loc (input_location, LE_EXPR,
    2821                 :             :                                        logical_type_node, start.expr,
    2822                 :             :                                        end.expr);
    2823                 :             : 
    2824                 :             :       /* Check lower bound.  */
    2825                 :         441 :       fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
    2826                 :             :                                start.expr,
    2827                 :         441 :                                build_one_cst (TREE_TYPE (start.expr)));
    2828                 :         441 :       fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    2829                 :             :                                logical_type_node, nonempty, fault);
    2830                 :         441 :       if (name)
    2831                 :         440 :         msg = xasprintf ("Substring out of bounds: lower bound (%%ld) of '%s' "
    2832                 :             :                          "is less than one", name);
    2833                 :             :       else
    2834                 :           1 :         msg = xasprintf ("Substring out of bounds: lower bound (%%ld) "
    2835                 :             :                          "is less than one");
    2836                 :         441 :       gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
    2837                 :             :                                fold_convert (long_integer_type_node,
    2838                 :             :                                              start.expr));
    2839                 :         441 :       free (msg);
    2840                 :             : 
    2841                 :             :       /* Check upper bound.  */
    2842                 :         441 :       fault = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    2843                 :             :                                end.expr, se->string_length);
    2844                 :         441 :       fault = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    2845                 :             :                                logical_type_node, nonempty, fault);
    2846                 :         441 :       if (name)
    2847                 :         440 :         msg = xasprintf ("Substring out of bounds: upper bound (%%ld) of '%s' "
    2848                 :             :                          "exceeds string length (%%ld)", name);
    2849                 :             :       else
    2850                 :           1 :         msg = xasprintf ("Substring out of bounds: upper bound (%%ld) "
    2851                 :             :                          "exceeds string length (%%ld)");
    2852                 :         441 :       gfc_trans_runtime_check (true, false, fault, &se->pre, where, msg,
    2853                 :             :                                fold_convert (long_integer_type_node, end.expr),
    2854                 :             :                                fold_convert (long_integer_type_node,
    2855                 :             :                                              se->string_length));
    2856                 :         441 :       free (msg);
    2857                 :             :     }
    2858                 :             : 
    2859                 :             :   /* Try to calculate the length from the start and end expressions.  */
    2860                 :        6768 :   if (ref->u.ss.end
    2861                 :        6768 :       && gfc_dep_difference (ref->u.ss.end, ref->u.ss.start, &length))
    2862                 :             :     {
    2863                 :        5584 :       HOST_WIDE_INT i_len;
    2864                 :             : 
    2865                 :        5584 :       i_len = gfc_mpz_get_hwi (length) + 1;
    2866                 :        5584 :       if (i_len < 0)
    2867                 :             :         i_len = 0;
    2868                 :             : 
    2869                 :        5584 :       tmp = build_int_cst (gfc_charlen_type_node, i_len);
    2870                 :        5584 :       mpz_clear (length);  /* Was initialized by gfc_dep_difference.  */
    2871                 :             :     }
    2872                 :             :   else
    2873                 :             :     {
    2874                 :        1184 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_charlen_type_node,
    2875                 :             :                              fold_convert (gfc_charlen_type_node, end.expr),
    2876                 :             :                              fold_convert (gfc_charlen_type_node, start.expr));
    2877                 :        1184 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_charlen_type_node,
    2878                 :        1184 :                              build_int_cst (gfc_charlen_type_node, 1), tmp);
    2879                 :        1184 :       tmp = fold_build2_loc (input_location, MAX_EXPR, gfc_charlen_type_node,
    2880                 :        1184 :                              tmp, build_int_cst (gfc_charlen_type_node, 0));
    2881                 :             :     }
    2882                 :             : 
    2883                 :        6768 :   se->string_length = tmp;
    2884                 :        6768 : }
    2885                 :             : 
    2886                 :             : 
    2887                 :             : /* Convert a derived type component reference.  */
    2888                 :             : 
    2889                 :             : void
    2890                 :      161633 : gfc_conv_component_ref (gfc_se * se, gfc_ref * ref)
    2891                 :             : {
    2892                 :      161633 :   gfc_component *c;
    2893                 :      161633 :   tree tmp;
    2894                 :      161633 :   tree decl;
    2895                 :      161633 :   tree field;
    2896                 :      161633 :   tree context;
    2897                 :             : 
    2898                 :      161633 :   c = ref->u.c.component;
    2899                 :             : 
    2900                 :      161633 :   if (c->backend_decl == NULL_TREE
    2901                 :           6 :       && ref->u.c.sym != NULL)
    2902                 :           6 :     gfc_get_derived_type (ref->u.c.sym);
    2903                 :             : 
    2904                 :      161633 :   field = c->backend_decl;
    2905                 :      161633 :   gcc_assert (field && TREE_CODE (field) == FIELD_DECL);
    2906                 :      161633 :   decl = se->expr;
    2907                 :      161633 :   context = DECL_FIELD_CONTEXT (field);
    2908                 :             : 
    2909                 :             :   /* Components can correspond to fields of different containing
    2910                 :             :      types, as components are created without context, whereas
    2911                 :             :      a concrete use of a component has the type of decl as context.
    2912                 :             :      So, if the type doesn't match, we search the corresponding
    2913                 :             :      FIELD_DECL in the parent type.  To not waste too much time
    2914                 :             :      we cache this result in norestrict_decl.
    2915                 :             :      On the other hand, if the context is a UNION or a MAP (a
    2916                 :             :      RECORD_TYPE within a UNION_TYPE) always use the given FIELD_DECL.  */
    2917                 :             : 
    2918                 :      161633 :   if (context != TREE_TYPE (decl)
    2919                 :      161633 :       && !(   TREE_CODE (TREE_TYPE (field)) == UNION_TYPE /* Field is union */
    2920                 :       10699 :            || TREE_CODE (context) == UNION_TYPE))         /* Field is map */
    2921                 :             :     {
    2922                 :       10699 :       tree f2 = c->norestrict_decl;
    2923                 :       18421 :       if (!f2 || DECL_FIELD_CONTEXT (f2) != TREE_TYPE (decl))
    2924                 :        6078 :         for (f2 = TYPE_FIELDS (TREE_TYPE (decl)); f2; f2 = DECL_CHAIN (f2))
    2925                 :        6078 :           if (TREE_CODE (f2) == FIELD_DECL
    2926                 :        6078 :               && DECL_NAME (f2) == DECL_NAME (field))
    2927                 :             :             break;
    2928                 :       10699 :       gcc_assert (f2);
    2929                 :       10699 :       c->norestrict_decl = f2;
    2930                 :       10699 :       field = f2;
    2931                 :             :     }
    2932                 :             : 
    2933                 :      161633 :   if (ref->u.c.sym && ref->u.c.sym->ts.type == BT_CLASS
    2934                 :           0 :       && strcmp ("_data", c->name) == 0)
    2935                 :             :     {
    2936                 :             :       /* Found a ref to the _data component.  Store the associated ref to
    2937                 :             :          the vptr in se->class_vptr.  */
    2938                 :           0 :       se->class_vptr = gfc_class_vptr_get (decl);
    2939                 :             :     }
    2940                 :             :   else
    2941                 :      161633 :     se->class_vptr = NULL_TREE;
    2942                 :             : 
    2943                 :      161633 :   tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
    2944                 :             :                          decl, field, NULL_TREE);
    2945                 :             : 
    2946                 :      161633 :   se->expr = tmp;
    2947                 :             : 
    2948                 :             :   /* Allocatable deferred char arrays are to be handled by the gfc_deferred_
    2949                 :             :      strlen () conditional below.  */
    2950                 :      161633 :   if (c->ts.type == BT_CHARACTER && !c->attr.proc_pointer
    2951                 :        8375 :       && !c->ts.deferred
    2952                 :        5440 :       && !c->attr.pdt_string)
    2953                 :             :     {
    2954                 :        5314 :       tmp = c->ts.u.cl->backend_decl;
    2955                 :             :       /* Components must always be constant length.  */
    2956                 :        5314 :       gcc_assert (tmp && INTEGER_CST_P (tmp));
    2957                 :        5314 :       se->string_length = tmp;
    2958                 :             :     }
    2959                 :             : 
    2960                 :      161633 :   if (gfc_deferred_strlen (c, &field))
    2961                 :             :     {
    2962                 :        3061 :       tmp = fold_build3_loc (input_location, COMPONENT_REF,
    2963                 :        3061 :                              TREE_TYPE (field),
    2964                 :             :                              decl, field, NULL_TREE);
    2965                 :        3061 :       se->string_length = tmp;
    2966                 :             :     }
    2967                 :             : 
    2968                 :      161633 :   if (((c->attr.pointer || c->attr.allocatable)
    2969                 :       93588 :        && (!c->attr.dimension && !c->attr.codimension)
    2970                 :       52338 :        && c->ts.type != BT_CHARACTER)
    2971                 :      111303 :       || c->attr.proc_pointer)
    2972                 :       56132 :     se->expr = build_fold_indirect_ref_loc (input_location,
    2973                 :             :                                         se->expr);
    2974                 :      161633 : }
    2975                 :             : 
    2976                 :             : 
    2977                 :             : /* This function deals with component references to components of the
    2978                 :             :    parent type for derived type extensions.  */
    2979                 :             : void
    2980                 :       61148 : conv_parent_component_references (gfc_se * se, gfc_ref * ref)
    2981                 :             : {
    2982                 :       61148 :   gfc_component *c;
    2983                 :       61148 :   gfc_component *cmp;
    2984                 :       61148 :   gfc_symbol *dt;
    2985                 :       61148 :   gfc_ref parent;
    2986                 :             : 
    2987                 :       61148 :   dt = ref->u.c.sym;
    2988                 :       61148 :   c = ref->u.c.component;
    2989                 :             : 
    2990                 :             :   /* Return if the component is in this type, i.e. not in the parent type.  */
    2991                 :      105354 :   for (cmp = dt->components; cmp; cmp = cmp->next)
    2992                 :       95435 :     if (c == cmp)
    2993                 :       51229 :       return;
    2994                 :             : 
    2995                 :             :   /* Build a gfc_ref to recursively call gfc_conv_component_ref.  */
    2996                 :        9919 :   parent.type = REF_COMPONENT;
    2997                 :        9919 :   parent.next = NULL;
    2998                 :        9919 :   parent.u.c.sym = dt;
    2999                 :        9919 :   parent.u.c.component = dt->components;
    3000                 :             : 
    3001                 :        9919 :   if (dt->backend_decl == NULL)
    3002                 :           0 :     gfc_get_derived_type (dt);
    3003                 :             : 
    3004                 :             :   /* Build the reference and call self.  */
    3005                 :        9919 :   gfc_conv_component_ref (se, &parent);
    3006                 :        9919 :   parent.u.c.sym = dt->components->ts.u.derived;
    3007                 :        9919 :   parent.u.c.component = c;
    3008                 :        9919 :   conv_parent_component_references (se, &parent);
    3009                 :             : }
    3010                 :             : 
    3011                 :             : 
    3012                 :             : static void
    3013                 :         509 : conv_inquiry (gfc_se * se, gfc_ref * ref, gfc_expr *expr, gfc_typespec *ts)
    3014                 :             : {
    3015                 :         509 :   tree res = se->expr;
    3016                 :             : 
    3017                 :         509 :   switch (ref->u.i)
    3018                 :             :     {
    3019                 :         245 :     case INQUIRY_RE:
    3020                 :         490 :       res = fold_build1_loc (input_location, REALPART_EXPR,
    3021                 :         245 :                              TREE_TYPE (TREE_TYPE (res)), res);
    3022                 :         245 :       break;
    3023                 :             : 
    3024                 :         219 :     case INQUIRY_IM:
    3025                 :         438 :       res = fold_build1_loc (input_location, IMAGPART_EXPR,
    3026                 :         219 :                              TREE_TYPE (TREE_TYPE (res)), res);
    3027                 :         219 :       break;
    3028                 :             : 
    3029                 :           7 :     case INQUIRY_KIND:
    3030                 :           7 :       res = build_int_cst (gfc_typenode_for_spec (&expr->ts),
    3031                 :           7 :                            ts->kind);
    3032                 :           7 :       se->string_length = NULL_TREE;
    3033                 :           7 :       break;
    3034                 :             : 
    3035                 :          38 :     case INQUIRY_LEN:
    3036                 :          38 :       res = fold_convert (gfc_typenode_for_spec (&expr->ts),
    3037                 :             :                           se->string_length);
    3038                 :          38 :       se->string_length = NULL_TREE;
    3039                 :          38 :       break;
    3040                 :             : 
    3041                 :           0 :     default:
    3042                 :           0 :       gcc_unreachable ();
    3043                 :             :     }
    3044                 :         509 :   se->expr = res;
    3045                 :         509 : }
    3046                 :             : 
    3047                 :             : /* Dereference VAR where needed if it is a pointer, reference, etc.
    3048                 :             :    according to Fortran semantics.  */
    3049                 :             : 
    3050                 :             : tree
    3051                 :     1375576 : gfc_maybe_dereference_var (gfc_symbol *sym, tree var, bool descriptor_only_p,
    3052                 :             :                            bool is_classarray)
    3053                 :             : {
    3054                 :     1375576 :   if (!POINTER_TYPE_P (TREE_TYPE (var)))
    3055                 :             :     return var;
    3056                 :      271637 :   if (is_CFI_desc (sym, NULL))
    3057                 :       11890 :     return build_fold_indirect_ref_loc (input_location, var);
    3058                 :             : 
    3059                 :             :   /* Characters are entirely different from other types, they are treated
    3060                 :             :      separately.  */
    3061                 :      259747 :   if (sym->ts.type == BT_CHARACTER)
    3062                 :             :     {
    3063                 :             :       /* Dereference character pointer dummy arguments
    3064                 :             :          or results.  */
    3065                 :       30078 :       if ((sym->attr.pointer || sym->attr.allocatable
    3066                 :       17169 :            || (sym->as && sym->as->type == AS_ASSUMED_RANK))
    3067                 :       13245 :           && (sym->attr.dummy
    3068                 :             :               || sym->attr.function
    3069                 :       13245 :               || sym->attr.result))
    3070                 :        4144 :         var = build_fold_indirect_ref_loc (input_location, var);
    3071                 :             :     }
    3072                 :      229669 :   else if (!sym->attr.value)
    3073                 :             :     {
    3074                 :             :       /* Dereference temporaries for class array dummy arguments.  */
    3075                 :      161131 :       if (sym->attr.dummy && is_classarray
    3076                 :      236065 :           && GFC_ARRAY_TYPE_P (TREE_TYPE (var)))
    3077                 :             :         {
    3078                 :        4908 :           if (!descriptor_only_p)
    3079                 :        2477 :             var = GFC_DECL_SAVED_DESCRIPTOR (var);
    3080                 :             : 
    3081                 :        4908 :           var = build_fold_indirect_ref_loc (input_location, var);
    3082                 :             :         }
    3083                 :             : 
    3084                 :             :       /* Dereference non-character scalar dummy arguments.  */
    3085                 :      228865 :       if (sym->attr.dummy && !sym->attr.dimension
    3086                 :       99005 :           && !(sym->attr.codimension && sym->attr.allocatable)
    3087                 :       98944 :           && (sym->ts.type != BT_CLASS
    3088                 :       18307 :               || (!CLASS_DATA (sym)->attr.dimension
    3089                 :       10648 :                   && !(CLASS_DATA (sym)->attr.codimension
    3090                 :             :                        && CLASS_DATA (sym)->attr.allocatable))))
    3091                 :       91147 :         var = build_fold_indirect_ref_loc (input_location, var);
    3092                 :             : 
    3093                 :             :       /* Dereference scalar hidden result.  */
    3094                 :      228865 :       if (flag_f2c && sym->ts.type == BT_COMPLEX
    3095                 :         286 :           && (sym->attr.function || sym->attr.result)
    3096                 :         108 :           && !sym->attr.dimension && !sym->attr.pointer
    3097                 :          60 :           && !sym->attr.always_explicit)
    3098                 :          36 :         var = build_fold_indirect_ref_loc (input_location, var);
    3099                 :             : 
    3100                 :             :       /* Dereference non-character, non-class pointer variables.
    3101                 :             :          These must be dummies, results, or scalars.  */
    3102                 :      228865 :       if (!is_classarray
    3103                 :      221241 :           && (sym->attr.pointer || sym->attr.allocatable
    3104                 :      175367 :               || gfc_is_associate_pointer (sym)
    3105                 :      170879 :               || (sym->as && sym->as->type == AS_ASSUMED_RANK))
    3106                 :      228865 :           && (sym->attr.dummy
    3107                 :             :               || sym->attr.function
    3108                 :       69017 :               || sym->attr.result
    3109                 :       31015 :               || (!sym->attr.dimension
    3110                 :       31013 :                   && (!sym->attr.codimension || !sym->attr.allocatable))))
    3111                 :       69015 :         var = build_fold_indirect_ref_loc (input_location, var);
    3112                 :             :       /* Now treat the class array pointer variables accordingly.  */
    3113                 :      159850 :       else if (sym->ts.type == BT_CLASS
    3114                 :       18731 :                && sym->attr.dummy
    3115                 :       18307 :                && (CLASS_DATA (sym)->attr.dimension
    3116                 :       18307 :                    || CLASS_DATA (sym)->attr.codimension)
    3117                 :        7929 :                && ((CLASS_DATA (sym)->as
    3118                 :        7929 :                     && CLASS_DATA (sym)->as->type == AS_ASSUMED_RANK)
    3119                 :             :                    || CLASS_DATA (sym)->attr.allocatable
    3120                 :        6932 :                    || CLASS_DATA (sym)->attr.class_pointer))
    3121                 :        2889 :         var = build_fold_indirect_ref_loc (input_location, var);
    3122                 :             :       /* And the case where a non-dummy, non-result, non-function,
    3123                 :             :          non-allocable and non-pointer classarray is present.  This case was
    3124                 :             :          previously covered by the first if, but with introducing the
    3125                 :             :          condition !is_classarray there, that case has to be covered
    3126                 :             :          explicitly.  */
    3127                 :      156961 :       else if (sym->ts.type == BT_CLASS
    3128                 :             :                && !sym->attr.dummy
    3129                 :             :                && !sym->attr.function
    3130                 :       15842 :                && !sym->attr.result
    3131                 :         424 :                && (CLASS_DATA (sym)->attr.dimension
    3132                 :         424 :                    || CLASS_DATA (sym)->attr.codimension)
    3133                 :         424 :                && (sym->assoc
    3134                 :           0 :                    || !CLASS_DATA (sym)->attr.allocatable)
    3135                 :         424 :                && !CLASS_DATA (sym)->attr.class_pointer)
    3136                 :         424 :         var = build_fold_indirect_ref_loc (input_location, var);
    3137                 :             :     }
    3138                 :             : 
    3139                 :             :   return var;
    3140                 :             : }
    3141                 :             : 
    3142                 :             : /* Return the contents of a variable. Also handles reference/pointer
    3143                 :             :    variables (all Fortran pointer references are implicit).  */
    3144                 :             : 
    3145                 :             : static void
    3146                 :     1515340 : gfc_conv_variable (gfc_se * se, gfc_expr * expr)
    3147                 :             : {
    3148                 :     1515340 :   gfc_ss *ss;
    3149                 :     1515340 :   gfc_ref *ref;
    3150                 :     1515340 :   gfc_symbol *sym;
    3151                 :     1515340 :   tree parent_decl = NULL_TREE;
    3152                 :     1515340 :   int parent_flag;
    3153                 :     1515340 :   bool return_value;
    3154                 :     1515340 :   bool alternate_entry;
    3155                 :     1515340 :   bool entry_master;
    3156                 :     1515340 :   bool is_classarray;
    3157                 :     1515340 :   bool first_time = true;
    3158                 :             : 
    3159                 :     1515340 :   sym = expr->symtree->n.sym;
    3160                 :     1515340 :   is_classarray = IS_CLASS_COARRAY_OR_ARRAY (sym);
    3161                 :     1515340 :   ss = se->ss;
    3162                 :     1515340 :   if (ss != NULL)
    3163                 :             :     {
    3164                 :      121201 :       gfc_ss_info *ss_info = ss->info;
    3165                 :             : 
    3166                 :             :       /* Check that something hasn't gone horribly wrong.  */
    3167                 :      121201 :       gcc_assert (ss != gfc_ss_terminator);
    3168                 :      121201 :       gcc_assert (ss_info->expr == expr);
    3169                 :             : 
    3170                 :             :       /* A scalarized term.  We already know the descriptor.  */
    3171                 :      121201 :       se->expr = ss_info->data.array.descriptor;
    3172                 :      121201 :       se->string_length = ss_info->string_length;
    3173                 :      121201 :       ref = ss_info->data.array.ref;
    3174                 :      121201 :       if (ref)
    3175                 :      120883 :         gcc_assert (ref->type == REF_ARRAY
    3176                 :             :                     && ref->u.ar.type != AR_ELEMENT);
    3177                 :             :       else
    3178                 :         318 :         gfc_conv_tmp_array_ref (se);
    3179                 :             :     }
    3180                 :             :   else
    3181                 :             :     {
    3182                 :     1394139 :       tree se_expr = NULL_TREE;
    3183                 :             : 
    3184                 :     1394139 :       se->expr = gfc_get_symbol_decl (sym);
    3185                 :             : 
    3186                 :             :       /* Deal with references to a parent results or entries by storing
    3187                 :             :          the current_function_decl and moving to the parent_decl.  */
    3188                 :     1394139 :       return_value = sym->attr.function && sym->result == sym;
    3189                 :       19132 :       alternate_entry = sym->attr.function && sym->attr.entry
    3190                 :     1395214 :                         && sym->result == sym;
    3191                 :     2788278 :       entry_master = sym->attr.result
    3192                 :       11247 :                      && sym->ns->proc_name->attr.entry_master
    3193                 :     1394520 :                      && !gfc_return_by_reference (sym->ns->proc_name);
    3194                 :     1394139 :       if (current_function_decl)
    3195                 :     1375657 :         parent_decl = DECL_CONTEXT (current_function_decl);
    3196                 :             : 
    3197                 :     1394139 :       if ((se->expr == parent_decl && return_value)
    3198                 :     1394028 :            || (sym->ns && sym->ns->proc_name
    3199                 :     1389210 :                && parent_decl
    3200                 :     1370728 :                && sym->ns->proc_name->backend_decl == parent_decl
    3201                 :       37081 :                && (alternate_entry || entry_master)))
    3202                 :             :         parent_flag = 1;
    3203                 :             :       else
    3204                 :     1393995 :         parent_flag = 0;
    3205                 :             : 
    3206                 :             :       /* Special case for assigning the return value of a function.
    3207                 :             :          Self recursive functions must have an explicit return value.  */
    3208                 :     1394139 :       if (return_value && (se->expr == current_function_decl || parent_flag))
    3209                 :       11609 :         se_expr = gfc_get_fake_result_decl (sym, parent_flag);
    3210                 :             : 
    3211                 :             :       /* Similarly for alternate entry points.  */
    3212                 :     1382530 :       else if (alternate_entry
    3213                 :        1042 :                && (sym->ns->proc_name->backend_decl == current_function_decl
    3214                 :           0 :                    || parent_flag))
    3215                 :             :         {
    3216                 :        1042 :           gfc_entry_list *el = NULL;
    3217                 :             : 
    3218                 :        1609 :           for (el = sym->ns->entries; el; el = el->next)
    3219                 :        1609 :             if (sym == el->sym)
    3220                 :             :               {
    3221                 :        1042 :                 se_expr = gfc_get_fake_result_decl (sym, parent_flag);
    3222                 :        1042 :                 break;
    3223                 :             :               }
    3224                 :             :         }
    3225                 :             : 
    3226                 :     1381488 :       else if (entry_master
    3227                 :         295 :                && (sym->ns->proc_name->backend_decl == current_function_decl
    3228                 :           0 :                    || parent_flag))
    3229                 :         295 :         se_expr = gfc_get_fake_result_decl (sym, parent_flag);
    3230                 :             : 
    3231                 :       12946 :       if (se_expr)
    3232                 :       12946 :         se->expr = se_expr;
    3233                 :             : 
    3234                 :             :       /* Procedure actual arguments.  Look out for temporary variables
    3235                 :             :          with the same attributes as function values.  */
    3236                 :     1381193 :       else if (!sym->attr.temporary
    3237                 :     1381125 :                && sym->attr.flavor == FL_PROCEDURE
    3238                 :       20262 :                && se->expr != current_function_decl)
    3239                 :             :         {
    3240                 :       20223 :           if (!sym->attr.dummy && !sym->attr.proc_pointer)
    3241                 :             :             {
    3242                 :       18708 :               gcc_assert (TREE_CODE (se->expr) == FUNCTION_DECL);
    3243                 :       18708 :               se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
    3244                 :             :             }
    3245                 :       20223 :           return;
    3246                 :             :         }
    3247                 :             : 
    3248                 :     1373916 :       if (sym->ts.type == BT_CLASS
    3249                 :       68375 :           && sym->attr.class_ok
    3250                 :       68133 :           && sym->ts.u.derived->attr.is_class)
    3251                 :             :         {
    3252                 :       25714 :           if (is_classarray && DECL_LANG_SPECIFIC (se->expr)
    3253                 :       75132 :               && GFC_DECL_SAVED_DESCRIPTOR (se->expr))
    3254                 :        5040 :             se->class_container = GFC_DECL_SAVED_DESCRIPTOR (se->expr);
    3255                 :             :           else
    3256                 :       63093 :             se->class_container = se->expr;
    3257                 :             :         }
    3258                 :             : 
    3259                 :             :       /* Dereference the expression, where needed.  */
    3260                 :     1373916 :       if (se->class_container && CLASS_DATA (sym)->attr.codimension
    3261                 :       68753 :           && !CLASS_DATA (sym)->attr.dimension)
    3262                 :         779 :         se->expr
    3263                 :         779 :           = gfc_maybe_dereference_var (sym, se->class_container,
    3264                 :         779 :                                        se->descriptor_only, is_classarray);
    3265                 :             :       else
    3266                 :     1373137 :         se->expr
    3267                 :     1373137 :           = gfc_maybe_dereference_var (sym, se->expr, se->descriptor_only,
    3268                 :             :                                        is_classarray);
    3269                 :             : 
    3270                 :     1373916 :       ref = expr->ref;
    3271                 :             :     }
    3272                 :             : 
    3273                 :             :   /* For character variables, also get the length.  */
    3274                 :     1495117 :   if (sym->ts.type == BT_CHARACTER)
    3275                 :             :     {
    3276                 :             :       /* If the character length of an entry isn't set, get the length from
    3277                 :             :          the master function instead.  */
    3278                 :      155997 :       if (sym->attr.entry && !sym->ts.u.cl->backend_decl)
    3279                 :           0 :         se->string_length = sym->ns->proc_name->ts.u.cl->backend_decl;
    3280                 :             :       else
    3281                 :      155997 :         se->string_length = sym->ts.u.cl->backend_decl;
    3282                 :      155997 :       gcc_assert (se->string_length);
    3283                 :             : 
    3284                 :             :       /* For coarray strings return the pointer to the data and not the
    3285                 :             :          descriptor.  */
    3286                 :        3459 :       if (sym->attr.codimension && sym->attr.associate_var
    3287                 :           6 :           && !se->descriptor_only
    3288                 :      156003 :           && TREE_CODE (TREE_TYPE (se->expr)) != ARRAY_TYPE)
    3289                 :           6 :         se->expr = gfc_conv_descriptor_data_get (se->expr);
    3290                 :             :     }
    3291                 :             : 
    3292                 :             :   /* F202Y: Runtime warning that an assumed rank object is associated
    3293                 :             :      with an assumed size object.  */
    3294                 :     1495117 :   if ((gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    3295                 :       88884 :       && (gfc_option.allow_std & GFC_STD_F202Y)
    3296                 :     1495351 :       && expr->rank == -1 && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (se->expr)))
    3297                 :             :     {
    3298                 :          60 :       tree dim, lower, upper, cond;
    3299                 :          60 :       char *msg;
    3300                 :             : 
    3301                 :          60 :       dim = fold_convert (signed_char_type_node,
    3302                 :             :                           gfc_conv_descriptor_rank (se->expr));
    3303                 :          60 :       dim = fold_build2_loc (input_location, MINUS_EXPR, signed_char_type_node,
    3304                 :          60 :                              dim, build_int_cst (signed_char_type_node, 1));
    3305                 :          60 :       lower = gfc_conv_descriptor_lbound_get (se->expr, dim);
    3306                 :          60 :       upper = gfc_conv_descriptor_ubound_get (se->expr, dim);
    3307                 :             : 
    3308                 :          60 :       msg = xasprintf ("Assumed rank object %s is associated with an "
    3309                 :             :                        "assumed size object", sym->name);
    3310                 :          60 :       cond = fold_build2_loc (input_location, LT_EXPR,
    3311                 :             :                               logical_type_node, upper, lower);
    3312                 :          60 :       gfc_trans_runtime_check (false, true, cond, &se->pre,
    3313                 :             :                                &gfc_current_locus, msg);
    3314                 :          60 :       free (msg);
    3315                 :             :     }
    3316                 :             : 
    3317                 :             :   /* Some expressions leak through that haven't been fixed up.  */
    3318                 :     1495117 :   if (IS_INFERRED_TYPE (expr) && expr->ref)
    3319                 :         380 :     gfc_fixup_inferred_type_refs (expr);
    3320                 :             : 
    3321                 :     1495117 :   gfc_typespec *ts = &sym->ts;
    3322                 :     1893255 :   while (ref)
    3323                 :             :     {
    3324                 :      723946 :       switch (ref->type)
    3325                 :             :         {
    3326                 :      564779 :         case REF_ARRAY:
    3327                 :             :           /* Return the descriptor if that's what we want and this is an array
    3328                 :             :              section reference.  */
    3329                 :      564779 :           if (se->descriptor_only && ref->u.ar.type != AR_ELEMENT)
    3330                 :             :             return;
    3331                 :             : /* TODO: Pointers to single elements of array sections, eg elemental subs.  */
    3332                 :             :           /* Return the descriptor for array pointers and allocations.  */
    3333                 :      247824 :           if (se->want_pointer
    3334                 :       22338 :               && ref->next == NULL && (se->descriptor_only))
    3335                 :             :             return;
    3336                 :             : 
    3337                 :      238971 :           gfc_conv_array_ref (se, &ref->u.ar, expr, &expr->where);
    3338                 :             :           /* Return a pointer to an element.  */
    3339                 :      238971 :           break;
    3340                 :             : 
    3341                 :      152148 :         case REF_COMPONENT:
    3342                 :      152148 :           ts = &ref->u.c.component->ts;
    3343                 :      152148 :           if (first_time && IS_CLASS_ARRAY (sym) && sym->attr.dummy
    3344                 :        5510 :               && se->descriptor_only && !CLASS_DATA (sym)->attr.allocatable
    3345                 :        3882 :               && !CLASS_DATA (sym)->attr.class_pointer && CLASS_DATA (sym)->as
    3346                 :        2898 :               && CLASS_DATA (sym)->as->type != AS_ASSUMED_RANK
    3347                 :        2431 :               && strcmp ("_data", ref->u.c.component->name) == 0)
    3348                 :             :             /* Skip the first ref of a _data component, because for class
    3349                 :             :                arrays that one is already done by introducing a temporary
    3350                 :             :                array descriptor.  */
    3351                 :             :             break;
    3352                 :             : 
    3353                 :      149717 :           if (ref->u.c.sym->attr.extension)
    3354                 :       51138 :             conv_parent_component_references (se, ref);
    3355                 :             : 
    3356                 :      149717 :           gfc_conv_component_ref (se, ref);
    3357                 :             : 
    3358                 :      149717 :           if (ref->u.c.component->ts.type == BT_CLASS
    3359                 :       11349 :               && ref->u.c.component->attr.class_ok
    3360                 :       11349 :               && ref->u.c.component->ts.u.derived->attr.is_class)
    3361                 :       11349 :             se->class_container = se->expr;
    3362                 :      138368 :           else if (!(ref->u.c.sym->attr.flavor == FL_DERIVED
    3363                 :      135874 :                      && ref->u.c.sym->attr.is_class))
    3364                 :       74348 :             se->class_container = NULL_TREE;
    3365                 :             : 
    3366                 :      149717 :           if (!ref->next && ref->u.c.sym->attr.codimension
    3367                 :           0 :               && se->want_pointer && se->descriptor_only)
    3368                 :             :             return;
    3369                 :             : 
    3370                 :             :           break;
    3371                 :             : 
    3372                 :        6510 :         case REF_SUBSTRING:
    3373                 :        6510 :           gfc_conv_substring (se, ref, expr->ts.kind,
    3374                 :        6510 :                               expr->symtree->name, &expr->where);
    3375                 :        6510 :           break;
    3376                 :             : 
    3377                 :         509 :         case REF_INQUIRY:
    3378                 :         509 :           conv_inquiry (se, ref, expr, ts);
    3379                 :         509 :           break;
    3380                 :             : 
    3381                 :           0 :         default:
    3382                 :           0 :           gcc_unreachable ();
    3383                 :      398138 :           break;
    3384                 :             :         }
    3385                 :      398138 :       first_time = false;
    3386                 :      398138 :       ref = ref->next;
    3387                 :             :     }
    3388                 :             :   /* Pointer assignment, allocation or pass by reference.  Arrays are handled
    3389                 :             :      separately.  */
    3390                 :     1169309 :   if (se->want_pointer)
    3391                 :             :     {
    3392                 :      126017 :       if (expr->ts.type == BT_CHARACTER && !gfc_is_proc_ptr_comp (expr))
    3393                 :        7555 :         gfc_conv_string_parameter (se);
    3394                 :             :       else
    3395                 :      118462 :         se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
    3396                 :             :     }
    3397                 :             : }
    3398                 :             : 
    3399                 :             : 
    3400                 :             : /* Unary ops are easy... Or they would be if ! was a valid op.  */
    3401                 :             : 
    3402                 :             : static void
    3403                 :       27988 : gfc_conv_unary_op (enum tree_code code, gfc_se * se, gfc_expr * expr)
    3404                 :             : {
    3405                 :       27988 :   gfc_se operand;
    3406                 :       27988 :   tree type;
    3407                 :             : 
    3408                 :       27988 :   gcc_assert (expr->ts.type != BT_CHARACTER);
    3409                 :             :   /* Initialize the operand.  */
    3410                 :       27988 :   gfc_init_se (&operand, se);
    3411                 :       27988 :   gfc_conv_expr_val (&operand, expr->value.op.op1);
    3412                 :       27988 :   gfc_add_block_to_block (&se->pre, &operand.pre);
    3413                 :             : 
    3414                 :       27988 :   type = gfc_typenode_for_spec (&expr->ts);
    3415                 :             : 
    3416                 :             :   /* TRUTH_NOT_EXPR is not a "true" unary operator in GCC.
    3417                 :             :      We must convert it to a compare to 0 (e.g. EQ_EXPR (op1, 0)).
    3418                 :             :      All other unary operators have an equivalent GIMPLE unary operator.  */
    3419                 :       27988 :   if (code == TRUTH_NOT_EXPR)
    3420                 :       19788 :     se->expr = fold_build2_loc (input_location, EQ_EXPR, type, operand.expr,
    3421                 :       19788 :                                 build_int_cst (type, 0));
    3422                 :             :   else
    3423                 :        8200 :     se->expr = fold_build1_loc (input_location, code, type, operand.expr);
    3424                 :             : 
    3425                 :       27988 : }
    3426                 :             : 
    3427                 :             : /* Expand power operator to optimal multiplications when a value is raised
    3428                 :             :    to a constant integer n. See section 4.6.3, "Evaluation of Powers" of
    3429                 :             :    Donald E. Knuth, "Seminumerical Algorithms", Vol. 2, "The Art of Computer
    3430                 :             :    Programming", 3rd Edition, 1998.  */
    3431                 :             : 
    3432                 :             : /* This code is mostly duplicated from expand_powi in the backend.
    3433                 :             :    We establish the "optimal power tree" lookup table with the defined size.
    3434                 :             :    The items in the table are the exponents used to calculate the index
    3435                 :             :    exponents. Any integer n less than the value can get an "addition chain",
    3436                 :             :    with the first node being one.  */
    3437                 :             : #define POWI_TABLE_SIZE 256
    3438                 :             : 
    3439                 :             : /* The table is from builtins.cc.  */
    3440                 :             : static const unsigned char powi_table[POWI_TABLE_SIZE] =
    3441                 :             :   {
    3442                 :             :       0,   1,   1,   2,   2,   3,   3,   4,  /*   0 -   7 */
    3443                 :             :       4,   6,   5,   6,   6,  10,   7,   9,  /*   8 -  15 */
    3444                 :             :       8,  16,   9,  16,  10,  12,  11,  13,  /*  16 -  23 */
    3445                 :             :      12,  17,  13,  18,  14,  24,  15,  26,  /*  24 -  31 */
    3446                 :             :      16,  17,  17,  19,  18,  33,  19,  26,  /*  32 -  39 */
    3447                 :             :      20,  25,  21,  40,  22,  27,  23,  44,  /*  40 -  47 */
    3448                 :             :      24,  32,  25,  34,  26,  29,  27,  44,  /*  48 -  55 */
    3449                 :             :      28,  31,  29,  34,  30,  60,  31,  36,  /*  56 -  63 */
    3450                 :             :      32,  64,  33,  34,  34,  46,  35,  37,  /*  64 -  71 */
    3451                 :             :      36,  65,  37,  50,  38,  48,  39,  69,  /*  72 -  79 */
    3452                 :             :      40,  49,  41,  43,  42,  51,  43,  58,  /*  80 -  87 */
    3453                 :             :      44,  64,  45,  47,  46,  59,  47,  76,  /*  88 -  95 */
    3454                 :             :      48,  65,  49,  66,  50,  67,  51,  66,  /*  96 - 103 */
    3455                 :             :      52,  70,  53,  74,  54, 104,  55,  74,  /* 104 - 111 */
    3456                 :             :      56,  64,  57,  69,  58,  78,  59,  68,  /* 112 - 119 */
    3457                 :             :      60,  61,  61,  80,  62,  75,  63,  68,  /* 120 - 127 */
    3458                 :             :      64,  65,  65, 128,  66, 129,  67,  90,  /* 128 - 135 */
    3459                 :             :      68,  73,  69, 131,  70,  94,  71,  88,  /* 136 - 143 */
    3460                 :             :      72, 128,  73,  98,  74, 132,  75, 121,  /* 144 - 151 */
    3461                 :             :      76, 102,  77, 124,  78, 132,  79, 106,  /* 152 - 159 */
    3462                 :             :      80,  97,  81, 160,  82,  99,  83, 134,  /* 160 - 167 */
    3463                 :             :      84,  86,  85,  95,  86, 160,  87, 100,  /* 168 - 175 */
    3464                 :             :      88, 113,  89,  98,  90, 107,  91, 122,  /* 176 - 183 */
    3465                 :             :      92, 111,  93, 102,  94, 126,  95, 150,  /* 184 - 191 */
    3466                 :             :      96, 128,  97, 130,  98, 133,  99, 195,  /* 192 - 199 */
    3467                 :             :     100, 128, 101, 123, 102, 164, 103, 138,  /* 200 - 207 */
    3468                 :             :     104, 145, 105, 146, 106, 109, 107, 149,  /* 208 - 215 */
    3469                 :             :     108, 200, 109, 146, 110, 170, 111, 157,  /* 216 - 223 */
    3470                 :             :     112, 128, 113, 130, 114, 182, 115, 132,  /* 224 - 231 */
    3471                 :             :     116, 200, 117, 132, 118, 158, 119, 206,  /* 232 - 239 */
    3472                 :             :     120, 240, 121, 162, 122, 147, 123, 152,  /* 240 - 247 */
    3473                 :             :     124, 166, 125, 214, 126, 138, 127, 153,  /* 248 - 255 */
    3474                 :             :   };
    3475                 :             : 
    3476                 :             : /* If n is larger than lookup table's max index, we use the "window
    3477                 :             :    method".  */
    3478                 :             : #define POWI_WINDOW_SIZE 3
    3479                 :             : 
    3480                 :             : /* Recursive function to expand the power operator. The temporary
    3481                 :             :    values are put in tmpvar. The function returns tmpvar[1] ** n.  */
    3482                 :             : static tree
    3483                 :      176241 : gfc_conv_powi (gfc_se * se, unsigned HOST_WIDE_INT n, tree * tmpvar)
    3484                 :             : {
    3485                 :      176241 :   tree op0;
    3486                 :      176241 :   tree op1;
    3487                 :      176241 :   tree tmp;
    3488                 :      176241 :   int digit;
    3489                 :             : 
    3490                 :      176241 :   if (n < POWI_TABLE_SIZE)
    3491                 :             :     {
    3492                 :      136094 :       if (tmpvar[n])
    3493                 :             :         return tmpvar[n];
    3494                 :             : 
    3495                 :       56205 :       op0 = gfc_conv_powi (se, n - powi_table[n], tmpvar);
    3496                 :       56205 :       op1 = gfc_conv_powi (se, powi_table[n], tmpvar);
    3497                 :             :     }
    3498                 :       40147 :   else if (n & 1)
    3499                 :             :     {
    3500                 :        9799 :       digit = n & ((1 << POWI_WINDOW_SIZE) - 1);
    3501                 :        9799 :       op0 = gfc_conv_powi (se, n - digit, tmpvar);
    3502                 :        9799 :       op1 = gfc_conv_powi (se, digit, tmpvar);
    3503                 :             :     }
    3504                 :             :   else
    3505                 :             :     {
    3506                 :       30348 :       op0 = gfc_conv_powi (se, n >> 1, tmpvar);
    3507                 :       30348 :       op1 = op0;
    3508                 :             :     }
    3509                 :             : 
    3510                 :       96352 :   tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (op0), op0, op1);
    3511                 :       96352 :   tmp = gfc_evaluate_now (tmp, &se->pre);
    3512                 :             : 
    3513                 :       96352 :   if (n < POWI_TABLE_SIZE)
    3514                 :       56205 :     tmpvar[n] = tmp;
    3515                 :             : 
    3516                 :             :   return tmp;
    3517                 :             : }
    3518                 :             : 
    3519                 :             : 
    3520                 :             : /* Expand lhs ** rhs. rhs is a constant integer. If it expands successfully,
    3521                 :             :    return 1. Else return 0 and a call to runtime library functions
    3522                 :             :    will have to be built.  */
    3523                 :             : static int
    3524                 :        2562 : gfc_conv_cst_int_power (gfc_se * se, tree lhs, tree rhs)
    3525                 :             : {
    3526                 :        2562 :   tree cond;
    3527                 :        2562 :   tree tmp;
    3528                 :        2562 :   tree type;
    3529                 :        2562 :   tree vartmp[POWI_TABLE_SIZE];
    3530                 :        2562 :   HOST_WIDE_INT m;
    3531                 :        2562 :   unsigned HOST_WIDE_INT n;
    3532                 :        2562 :   int sgn;
    3533                 :        2562 :   wi::tree_to_wide_ref wrhs = wi::to_wide (rhs);
    3534                 :             : 
    3535                 :             :   /* If exponent is too large, we won't expand it anyway, so don't bother
    3536                 :             :      with large integer values.  */
    3537                 :        2562 :   if (!wi::fits_shwi_p (wrhs))
    3538                 :             :     return 0;
    3539                 :             : 
    3540                 :        2202 :   m = wrhs.to_shwi ();
    3541                 :             :   /* Use the wide_int's routine to reliably get the absolute value on all
    3542                 :             :      platforms.  Then convert it to a HOST_WIDE_INT like above.  */
    3543                 :        2202 :   n = wi::abs (wrhs).to_shwi ();
    3544                 :             : 
    3545                 :        2202 :   type = TREE_TYPE (lhs);
    3546                 :        2202 :   sgn = tree_int_cst_sgn (rhs);
    3547                 :             : 
    3548                 :        2202 :   if (((FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
    3549                 :        4404 :        || optimize_size) && (m > 2 || m < -1))
    3550                 :             :     return 0;
    3551                 :             : 
    3552                 :             :   /* rhs == 0  */
    3553                 :        1244 :   if (sgn == 0)
    3554                 :             :     {
    3555                 :         167 :       se->expr = gfc_build_const (type, integer_one_node);
    3556                 :         167 :       return 1;
    3557                 :             :     }
    3558                 :             : 
    3559                 :             :   /* If rhs < 0 and lhs is an integer, the result is -1, 0 or 1.  */
    3560                 :        1077 :   if ((sgn == -1) && (TREE_CODE (type) == INTEGER_TYPE))
    3561                 :             :     {
    3562                 :         152 :       tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    3563                 :         152 :                              lhs, build_int_cst (TREE_TYPE (lhs), -1));
    3564                 :         152 :       cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    3565                 :         152 :                               lhs, build_int_cst (TREE_TYPE (lhs), 1));
    3566                 :             : 
    3567                 :             :       /* If rhs is even,
    3568                 :             :          result = (lhs == 1 || lhs == -1) ? 1 : 0.  */
    3569                 :         152 :       if ((n & 1) == 0)
    3570                 :             :         {
    3571                 :          72 :           tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR,
    3572                 :             :                                  logical_type_node, tmp, cond);
    3573                 :          72 :           se->expr = fold_build3_loc (input_location, COND_EXPR, type,
    3574                 :          72 :                                       tmp, build_int_cst (type, 1),
    3575                 :          72 :                                       build_int_cst (type, 0));
    3576                 :          72 :           return 1;
    3577                 :             :         }
    3578                 :             :       /* If rhs is odd,
    3579                 :             :          result = (lhs == 1) ? 1 : (lhs == -1) ? -1 : 0.  */
    3580                 :          80 :       tmp = fold_build3_loc (input_location, COND_EXPR, type, tmp,
    3581                 :          80 :                              build_int_cst (type, -1),
    3582                 :          80 :                              build_int_cst (type, 0));
    3583                 :          80 :       se->expr = fold_build3_loc (input_location, COND_EXPR, type,
    3584                 :          80 :                                   cond, build_int_cst (type, 1), tmp);
    3585                 :          80 :       return 1;
    3586                 :             :     }
    3587                 :             : 
    3588                 :         925 :   memset (vartmp, 0, sizeof (vartmp));
    3589                 :         925 :   vartmp[1] = lhs;
    3590                 :         925 :   if (sgn == -1)
    3591                 :             :     {
    3592                 :          91 :       tmp = gfc_build_const (type, integer_one_node);
    3593                 :          91 :       vartmp[1] = fold_build2_loc (input_location, RDIV_EXPR, type, tmp,
    3594                 :             :                                    vartmp[1]);
    3595                 :             :     }
    3596                 :             : 
    3597                 :         925 :   se->expr = gfc_conv_powi (se, n, vartmp);
    3598                 :             : 
    3599                 :         925 :   return 1;
    3600                 :             : }
    3601                 :             : 
    3602                 :             : /* Convert lhs**rhs, for constant rhs, when both are unsigned.
    3603                 :             :    Method:
    3604                 :             :    if (rhs == 0)      ! Checked here.
    3605                 :             :      return 1;
    3606                 :             :    if (lhs & 1 == 1)  ! odd_cnd
    3607                 :             :      {
    3608                 :             :        if (bit_size(rhs) < bit_size(lhs))  ! Checked here.
    3609                 :             :          return lhs ** rhs;
    3610                 :             : 
    3611                 :             :        mask = 1 << (bit_size(a) - 1) / 2;
    3612                 :             :        return lhs ** (n & rhs);
    3613                 :             :      }
    3614                 :             :    if (rhs > bit_size(lhs))  ! Checked here.
    3615                 :             :      return 0;
    3616                 :             : 
    3617                 :             :    return lhs ** rhs;
    3618                 :             : */
    3619                 :             : 
    3620                 :             : static int
    3621                 :       15120 : gfc_conv_cst_uint_power (gfc_se * se, tree lhs, tree rhs)
    3622                 :             : {
    3623                 :       15120 :   tree type = TREE_TYPE (lhs);
    3624                 :       15120 :   tree tmp, is_odd, odd_branch, even_branch;
    3625                 :       15120 :   unsigned HOST_WIDE_INT lhs_prec, rhs_prec;
    3626                 :       15120 :   wi::tree_to_wide_ref wrhs = wi::to_wide (rhs);
    3627                 :       15120 :   unsigned HOST_WIDE_INT n, n_odd;
    3628                 :       15120 :   tree vartmp_odd[POWI_TABLE_SIZE], vartmp_even[POWI_TABLE_SIZE];
    3629                 :             : 
    3630                 :             :   /* Anything ** 0 is one.  */
    3631                 :       15120 :   if (integer_zerop (rhs))
    3632                 :             :     {
    3633                 :        1800 :       se->expr = build_int_cst (type, 1);
    3634                 :        1800 :       return 1;
    3635                 :             :     }
    3636                 :             : 
    3637                 :       13320 :   if (!wi::fits_uhwi_p (wrhs))
    3638                 :             :     return 0;
    3639                 :             : 
    3640                 :       12960 :   n = wrhs.to_uhwi ();
    3641                 :             : 
    3642                 :             :   /* tmp = a & 1; . */
    3643                 :       12960 :   tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type,
    3644                 :       12960 :                          lhs, build_int_cst (type, 1));
    3645                 :       12960 :   is_odd = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    3646                 :       12960 :                             tmp, build_int_cst (type, 1));
    3647                 :             : 
    3648                 :       12960 :   lhs_prec = TYPE_PRECISION (type);
    3649                 :       12960 :   rhs_prec = TYPE_PRECISION (TREE_TYPE (rhs));
    3650                 :             : 
    3651                 :       12960 :   if (rhs_prec >= lhs_prec && lhs_prec <= HOST_BITS_PER_WIDE_INT)
    3652                 :             :     {
    3653                 :        7044 :       unsigned HOST_WIDE_INT mask = (HOST_WIDE_INT_1U << (lhs_prec - 1)) - 1;
    3654                 :        7044 :       n_odd = n & mask;
    3655                 :             :     }
    3656                 :             :   else
    3657                 :             :     n_odd = n;
    3658                 :             : 
    3659                 :       12960 :   memset (vartmp_odd, 0, sizeof (vartmp_odd));
    3660                 :       12960 :   vartmp_odd[0] = build_int_cst (type, 1);
    3661                 :       12960 :   vartmp_odd[1] = lhs;
    3662                 :       12960 :   odd_branch = gfc_conv_powi (se, n_odd, vartmp_odd);
    3663                 :       12960 :   even_branch = NULL_TREE;
    3664                 :             : 
    3665                 :       12960 :   if (n > lhs_prec)
    3666                 :        4260 :     even_branch = build_int_cst (type, 0);
    3667                 :             :   else
    3668                 :             :     {
    3669                 :        8700 :       if (n_odd != n)
    3670                 :             :         {
    3671                 :           0 :           memset (vartmp_even, 0, sizeof (vartmp_even));
    3672                 :           0 :           vartmp_even[0] = build_int_cst (type, 1);
    3673                 :           0 :           vartmp_even[1] = lhs;
    3674                 :           0 :           even_branch = gfc_conv_powi (se, n, vartmp_even);
    3675                 :             :         }
    3676                 :             :     }
    3677                 :        4260 :   if (even_branch != NULL_TREE)
    3678                 :        4260 :     se->expr = fold_build3_loc (input_location, COND_EXPR, type, is_odd,
    3679                 :             :                                 odd_branch, even_branch);
    3680                 :             :   else
    3681                 :        8700 :     se->expr = odd_branch;
    3682                 :             : 
    3683                 :             :   return 1;
    3684                 :             : }
    3685                 :             : 
    3686                 :             : /* Power op (**).  Constant integer exponent and powers of 2 have special
    3687                 :             :    handling.  */
    3688                 :             : 
    3689                 :             : static void
    3690                 :       48547 : gfc_conv_power_op (gfc_se * se, gfc_expr * expr)
    3691                 :             : {
    3692                 :       48547 :   tree gfc_int4_type_node;
    3693                 :       48547 :   int kind;
    3694                 :       48547 :   int ikind;
    3695                 :       48547 :   int res_ikind_1, res_ikind_2;
    3696                 :       48547 :   gfc_se lse;
    3697                 :       48547 :   gfc_se rse;
    3698                 :       48547 :   tree fndecl = NULL;
    3699                 :             : 
    3700                 :       48547 :   gfc_init_se (&lse, se);
    3701                 :       48547 :   gfc_conv_expr_val (&lse, expr->value.op.op1);
    3702                 :       48547 :   lse.expr = gfc_evaluate_now (lse.expr, &lse.pre);
    3703                 :       48547 :   gfc_add_block_to_block (&se->pre, &lse.pre);
    3704                 :             : 
    3705                 :       48547 :   gfc_init_se (&rse, se);
    3706                 :       48547 :   gfc_conv_expr_val (&rse, expr->value.op.op2);
    3707                 :       48547 :   gfc_add_block_to_block (&se->pre, &rse.pre);
    3708                 :             : 
    3709                 :       48547 :   if (expr->value.op.op2->expr_type == EXPR_CONSTANT)
    3710                 :             :     {
    3711                 :       16994 :       if (expr->value.op.op2->ts.type == BT_INTEGER)
    3712                 :             :         {
    3713                 :        1723 :           if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
    3714                 :       20023 :             return;
    3715                 :             :         }
    3716                 :       15271 :       else if (expr->value.op.op2->ts.type == BT_UNSIGNED)
    3717                 :             :         {
    3718                 :       15120 :           if (gfc_conv_cst_uint_power (se, lse.expr, rse.expr))
    3719                 :             :             return;
    3720                 :             :         }
    3721                 :             :     }
    3722                 :             : 
    3723                 :       32543 :   if ((expr->value.op.op2->ts.type == BT_INTEGER
    3724                 :       31461 :        || expr->value.op.op2->ts.type == BT_UNSIGNED)
    3725                 :       31682 :       && expr->value.op.op2->expr_type == EXPR_CONSTANT)
    3726                 :         839 :     if (gfc_conv_cst_int_power (se, lse.expr, rse.expr))
    3727                 :             :       return;
    3728                 :             : 
    3729                 :       32543 :   if (INTEGER_CST_P (lse.expr)
    3730                 :       15365 :       && TREE_CODE (TREE_TYPE (rse.expr)) == INTEGER_TYPE
    3731                 :       47908 :       && expr->value.op.op2->ts.type == BT_INTEGER)
    3732                 :             :     {
    3733                 :         245 :       wi::tree_to_wide_ref wlhs = wi::to_wide (lse.expr);
    3734                 :         245 :       HOST_WIDE_INT v;
    3735                 :         245 :       unsigned HOST_WIDE_INT w;
    3736                 :         245 :       int kind, ikind, bit_size;
    3737                 :             : 
    3738                 :         245 :       v = wlhs.to_shwi ();
    3739                 :         245 :       w = absu_hwi (v);
    3740                 :             : 
    3741                 :         245 :       kind = expr->value.op.op1->ts.kind;
    3742                 :         245 :       ikind = gfc_validate_kind (BT_INTEGER, kind, false);
    3743                 :         245 :       bit_size = gfc_integer_kinds[ikind].bit_size;
    3744                 :             : 
    3745                 :         245 :       if (v == 1)
    3746                 :             :         {
    3747                 :             :           /* 1**something is always 1.  */
    3748                 :          35 :           se->expr = build_int_cst (TREE_TYPE (lse.expr), 1);
    3749                 :         239 :           return;
    3750                 :             :         }
    3751                 :         210 :       else if (v == -1)
    3752                 :             :         {
    3753                 :             :           /* (-1)**n is 1 - ((n & 1) << 1) */
    3754                 :          34 :           tree type;
    3755                 :          34 :           tree tmp;
    3756                 :             : 
    3757                 :          34 :           type = TREE_TYPE (lse.expr);
    3758                 :          34 :           tmp = fold_build2_loc (input_location, BIT_AND_EXPR, type,
    3759                 :          34 :                                  rse.expr, build_int_cst (type, 1));
    3760                 :          34 :           tmp = fold_build2_loc (input_location, LSHIFT_EXPR, type,
    3761                 :          34 :                                  tmp, build_int_cst (type, 1));
    3762                 :          34 :           tmp = fold_build2_loc (input_location, MINUS_EXPR, type,
    3763                 :          34 :                                  build_int_cst (type, 1), tmp);
    3764                 :          34 :           se->expr = tmp;
    3765                 :          34 :           return;
    3766                 :             :         }
    3767                 :         176 :       else if (w > 0 && ((w & (w-1)) == 0) && ((w >> (bit_size-1)) == 0))
    3768                 :             :         {
    3769                 :             :           /* Here v is +/- 2**e.  The further simplification uses
    3770                 :             :              2**n = 1<<n, 4**n = 1<<(n+n), 8**n = 1 <<(3*n), 16**n =
    3771                 :             :              1<<(4*n), etc., but we have to make sure to return zero
    3772                 :             :              if the number of bits is too large. */
    3773                 :         170 :           tree lshift;
    3774                 :         170 :           tree type;
    3775                 :         170 :           tree shift;
    3776                 :         170 :           tree ge;
    3777                 :         170 :           tree cond;
    3778                 :         170 :           tree num_bits;
    3779                 :         170 :           tree cond2;
    3780                 :         170 :           tree tmp1;
    3781                 :             : 
    3782                 :         170 :           type = TREE_TYPE (lse.expr);
    3783                 :             : 
    3784                 :         170 :           if (w == 2)
    3785                 :         110 :             shift = rse.expr;
    3786                 :          60 :           else if (w == 4)
    3787                 :          12 :             shift = fold_build2_loc (input_location, PLUS_EXPR,
    3788                 :          12 :                                      TREE_TYPE (rse.expr),
    3789                 :             :                                        rse.expr, rse.expr);
    3790                 :             :           else
    3791                 :             :             {
    3792                 :             :               /* use popcount for fast log2(w) */
    3793                 :          48 :               int e = wi::popcount (w-1);
    3794                 :          96 :               shift = fold_build2_loc (input_location, MULT_EXPR,
    3795                 :          48 :                                        TREE_TYPE (rse.expr),
    3796                 :          48 :                                        build_int_cst (TREE_TYPE (rse.expr), e),
    3797                 :             :                                        rse.expr);
    3798                 :             :             }
    3799                 :             : 
    3800                 :         170 :           lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type,
    3801                 :         170 :                                     build_int_cst (type, 1), shift);
    3802                 :         170 :           ge = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
    3803                 :         170 :                                 rse.expr, build_int_cst (type, 0));
    3804                 :         170 :           cond = fold_build3_loc (input_location, COND_EXPR, type, ge, lshift,
    3805                 :         170 :                                  build_int_cst (type, 0));
    3806                 :         170 :           num_bits = build_int_cst (TREE_TYPE (rse.expr), TYPE_PRECISION (type));
    3807                 :         170 :           cond2 = fold_build2_loc (input_location, GE_EXPR, logical_type_node,
    3808                 :             :                                    rse.expr, num_bits);
    3809                 :         170 :           tmp1 = fold_build3_loc (input_location, COND_EXPR, type, cond2,
    3810                 :         170 :                                   build_int_cst (type, 0), cond);
    3811                 :         170 :           if (v > 0)
    3812                 :             :             {
    3813                 :         128 :               se->expr = tmp1;
    3814                 :             :             }
    3815                 :             :           else
    3816                 :             :             {
    3817                 :             :               /* for v < 0, calculate v**n = |v|**n * (-1)**n */
    3818                 :          42 :               tree tmp2;
    3819                 :          42 :               tmp2 = fold_build2_loc (input_location, BIT_AND_EXPR, type,
    3820                 :          42 :                                       rse.expr, build_int_cst (type, 1));
    3821                 :          42 :               tmp2 = fold_build2_loc (input_location, LSHIFT_EXPR, type,
    3822                 :          42 :                                       tmp2, build_int_cst (type, 1));
    3823                 :          42 :               tmp2 = fold_build2_loc (input_location, MINUS_EXPR, type,
    3824                 :          42 :                                       build_int_cst (type, 1), tmp2);
    3825                 :          42 :               se->expr = fold_build2_loc (input_location, MULT_EXPR, type,
    3826                 :             :                                           tmp1, tmp2);
    3827                 :             :             }
    3828                 :         170 :           return;
    3829                 :             :         }
    3830                 :             :     }
    3831                 :             :   /* Handle unsigned separate from signed above, things would be too
    3832                 :             :      complicated otherwise.  */
    3833                 :             : 
    3834                 :       32304 :   if (INTEGER_CST_P (lse.expr) && expr->value.op.op1->ts.type == BT_UNSIGNED)
    3835                 :             :     {
    3836                 :       15120 :       gfc_expr * op1 = expr->value.op.op1;
    3837                 :       15120 :       tree type;
    3838                 :             : 
    3839                 :       15120 :       type = TREE_TYPE (lse.expr);
    3840                 :             : 
    3841                 :       15120 :       if (mpz_cmp_ui (op1->value.integer, 1) == 0)
    3842                 :             :         {
    3843                 :             :           /* 1**something is always 1.  */
    3844                 :        1260 :           se->expr = build_int_cst (type, 1);
    3845                 :        1260 :           return;
    3846                 :             :         }
    3847                 :             : 
    3848                 :             :       /* Simplify 2u**x to a shift, with the value set to zero if it falls
    3849                 :             :        outside the range.  */
    3850                 :       26460 :       if (mpz_popcount (op1->value.integer) == 1)
    3851                 :             :         {
    3852                 :        2520 :           tree prec_m1, lim, shift, lshift, cond, tmp;
    3853                 :        2520 :           tree rtype = TREE_TYPE (rse.expr);
    3854                 :        2520 :           int e = mpz_scan1 (op1->value.integer, 0);
    3855                 :             : 
    3856                 :        2520 :           shift = fold_build2_loc (input_location, MULT_EXPR,
    3857                 :        2520 :                                    rtype, build_int_cst (rtype, e),
    3858                 :             :                                    rse.expr);
    3859                 :        2520 :           lshift = fold_build2_loc (input_location, LSHIFT_EXPR, type,
    3860                 :        2520 :                                     build_int_cst (type, 1), shift);
    3861                 :        2520 :           prec_m1 = fold_build2_loc (input_location, MINUS_EXPR, rtype,
    3862                 :        2520 :                                      build_int_cst (rtype, TYPE_PRECISION (type)),
    3863                 :        2520 :                                      build_int_cst (rtype, 1));
    3864                 :        2520 :           lim = fold_build2_loc (input_location, TRUNC_DIV_EXPR, rtype,
    3865                 :        2520 :                                  prec_m1, build_int_cst (rtype, e));
    3866                 :        2520 :           cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node,
    3867                 :             :                                   rse.expr, lim);
    3868                 :        2520 :           tmp = fold_build3_loc (input_location, COND_EXPR, type, cond,
    3869                 :        2520 :                                  build_int_cst (type, 0), lshift);
    3870                 :        2520 :           se->expr = tmp;
    3871                 :        2520 :           return;
    3872                 :             :         }
    3873                 :             :     }
    3874                 :             : 
    3875                 :       28524 :   gfc_int4_type_node = gfc_get_int_type (4);
    3876                 :             : 
    3877                 :             :   /* In case of integer operands with kinds 1 or 2, we call the integer kind 4
    3878                 :             :      library routine.  But in the end, we have to convert the result back
    3879                 :             :      if this case applies -- with res_ikind_K, we keep track whether operand K
    3880                 :             :      falls into this case.  */
    3881                 :       28524 :   res_ikind_1 = -1;
    3882                 :       28524 :   res_ikind_2 = -1;
    3883                 :             : 
    3884                 :       28524 :   kind = expr->value.op.op1->ts.kind;
    3885                 :       28524 :   switch (expr->value.op.op2->ts.type)
    3886                 :             :     {
    3887                 :         843 :     case BT_INTEGER:
    3888                 :         843 :       ikind = expr->value.op.op2->ts.kind;
    3889                 :         843 :       switch (ikind)
    3890                 :             :         {
    3891                 :         144 :         case 1:
    3892                 :         144 :         case 2:
    3893                 :         144 :           rse.expr = convert (gfc_int4_type_node, rse.expr);
    3894                 :         144 :           res_ikind_2 = ikind;
    3895                 :             :           /* Fall through.  */
    3896                 :             : 
    3897                 :             :         case 4:
    3898                 :             :           ikind = 0;
    3899                 :             :           break;
    3900                 :             : 
    3901                 :             :         case 8:
    3902                 :             :           ikind = 1;
    3903                 :             :           break;
    3904                 :             : 
    3905                 :           6 :         case 16:
    3906                 :           6 :           ikind = 2;
    3907                 :           6 :           break;
    3908                 :             : 
    3909                 :           0 :         default:
    3910                 :           0 :           gcc_unreachable ();
    3911                 :             :         }
    3912                 :         843 :       switch (kind)
    3913                 :             :         {
    3914                 :           0 :         case 1:
    3915                 :           0 :         case 2:
    3916                 :           0 :           if (expr->value.op.op1->ts.type == BT_INTEGER)
    3917                 :             :             {
    3918                 :           0 :               lse.expr = convert (gfc_int4_type_node, lse.expr);
    3919                 :           0 :               res_ikind_1 = kind;
    3920                 :             :             }
    3921                 :             :           else
    3922                 :           0 :             gcc_unreachable ();
    3923                 :             :           /* Fall through.  */
    3924                 :             : 
    3925                 :             :         case 4:
    3926                 :             :           kind = 0;
    3927                 :             :           break;
    3928                 :             : 
    3929                 :             :         case 8:
    3930                 :             :           kind = 1;
    3931                 :             :           break;
    3932                 :             : 
    3933                 :           6 :         case 10:
    3934                 :           6 :           kind = 2;
    3935                 :           6 :           break;
    3936                 :             : 
    3937                 :          18 :         case 16:
    3938                 :          18 :           kind = 3;
    3939                 :          18 :           break;
    3940                 :             : 
    3941                 :           0 :         default:
    3942                 :           0 :           gcc_unreachable ();
    3943                 :             :         }
    3944                 :             : 
    3945                 :         843 :       switch (expr->value.op.op1->ts.type)
    3946                 :             :         {
    3947                 :          99 :         case BT_INTEGER:
    3948                 :          99 :           if (kind == 3) /* Case 16 was not handled properly above.  */
    3949                 :             :             kind = 2;
    3950                 :          99 :           fndecl = gfor_fndecl_math_powi[kind][ikind].integer;
    3951                 :          99 :           break;
    3952                 :             : 
    3953                 :         557 :         case BT_REAL:
    3954                 :             :           /* Use builtins for real ** int4.  */
    3955                 :         557 :           if (ikind == 0)
    3956                 :             :             {
    3957                 :         500 :               switch (kind)
    3958                 :             :                 {
    3959                 :         327 :                 case 0:
    3960                 :         327 :                   fndecl = builtin_decl_explicit (BUILT_IN_POWIF);
    3961                 :         327 :                   break;
    3962                 :             : 
    3963                 :         155 :                 case 1:
    3964                 :         155 :                   fndecl = builtin_decl_explicit (BUILT_IN_POWI);
    3965                 :         155 :                   break;
    3966                 :             : 
    3967                 :           6 :                 case 2:
    3968                 :           6 :                   fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
    3969                 :           6 :                   break;
    3970                 :             : 
    3971                 :          12 :                 case 3:
    3972                 :             :                   /* Use the __builtin_powil() only if real(kind=16) is
    3973                 :             :                      actually the C long double type.  */
    3974                 :          12 :                   if (!gfc_real16_is_float128)
    3975                 :           0 :                     fndecl = builtin_decl_explicit (BUILT_IN_POWIL);
    3976                 :             :                   break;
    3977                 :             : 
    3978                 :             :                 default:
    3979                 :             :                   gcc_unreachable ();
    3980                 :             :                 }
    3981                 :             :             }
    3982                 :             : 
    3983                 :             :           /* If we don't have a good builtin for this, go for the
    3984                 :             :              library function.  */
    3985                 :         488 :           if (!fndecl)
    3986                 :          69 :             fndecl = gfor_fndecl_math_powi[kind][ikind].real;
    3987                 :             :           break;
    3988                 :             : 
    3989                 :         187 :         case BT_COMPLEX:
    3990                 :         187 :           fndecl = gfor_fndecl_math_powi[kind][ikind].cmplx;
    3991                 :         187 :           break;
    3992                 :             : 
    3993                 :           0 :         default:
    3994                 :           0 :           gcc_unreachable ();
    3995                 :             :         }
    3996                 :             :       break;
    3997                 :             : 
    3998                 :         132 :     case BT_REAL:
    3999                 :         132 :       fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_POW, kind);
    4000                 :         132 :       break;
    4001                 :             : 
    4002                 :         729 :     case BT_COMPLEX:
    4003                 :         729 :       fndecl = gfc_builtin_decl_for_float_kind (BUILT_IN_CPOW, kind);
    4004                 :         729 :       break;
    4005                 :             : 
    4006                 :       26820 :     case BT_UNSIGNED:
    4007                 :       26820 :       {
    4008                 :             :         /* Valid kinds for unsigned are 1, 2, 4, 8, 16.  Instead of using a
    4009                 :             :            large switch statement, let's just use __builtin_ctz.  */
    4010                 :       26820 :         int base = __builtin_ctz (expr->value.op.op1->ts.kind);
    4011                 :       26820 :         int expon = __builtin_ctz (expr->value.op.op2->ts.kind);
    4012                 :       26820 :         fndecl = gfor_fndecl_unsigned_pow_list[base][expon];
    4013                 :             :       }
    4014                 :       26820 :       break;
    4015                 :             : 
    4016                 :           0 :     default:
    4017                 :           0 :       gcc_unreachable ();
    4018                 :       28524 :       break;
    4019                 :             :     }
    4020                 :             : 
    4021                 :       28524 :   se->expr = build_call_expr_loc (input_location,
    4022                 :             :                               fndecl, 2, lse.expr, rse.expr);
    4023                 :             : 
    4024                 :             :   /* Convert the result back if it is of wrong integer kind.  */
    4025                 :       28524 :   if (res_ikind_1 != -1 && res_ikind_2 != -1)
    4026                 :             :     {
    4027                 :             :       /* We want the maximum of both operand kinds as result.  */
    4028                 :           0 :       if (res_ikind_1 < res_ikind_2)
    4029                 :           0 :         res_ikind_1 = res_ikind_2;
    4030                 :           0 :       se->expr = convert (gfc_get_int_type (res_ikind_1), se->expr);
    4031                 :             :     }
    4032                 :             : }
    4033                 :             : 
    4034                 :             : 
    4035                 :             : /* Generate code to allocate a string temporary.  */
    4036                 :             : 
    4037                 :             : tree
    4038                 :        4625 : gfc_conv_string_tmp (gfc_se * se, tree type, tree len)
    4039                 :             : {
    4040                 :        4625 :   tree var;
    4041                 :        4625 :   tree tmp;
    4042                 :             : 
    4043                 :        4625 :   if (gfc_can_put_var_on_stack (len))
    4044                 :             :     {
    4045                 :             :       /* Create a temporary variable to hold the result.  */
    4046                 :        4044 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
    4047                 :        2022 :                              TREE_TYPE (len), len,
    4048                 :        2022 :                              build_int_cst (TREE_TYPE (len), 1));
    4049                 :        2022 :       tmp = build_range_type (gfc_charlen_type_node, size_zero_node, tmp);
    4050                 :             : 
    4051                 :        2022 :       if (TREE_CODE (TREE_TYPE (type)) == ARRAY_TYPE)
    4052                 :        1992 :         tmp = build_array_type (TREE_TYPE (TREE_TYPE (type)), tmp);
    4053                 :             :       else
    4054                 :          30 :         tmp = build_array_type (TREE_TYPE (type), tmp);
    4055                 :             : 
    4056                 :        2022 :       var = gfc_create_var (tmp, "str");
    4057                 :        2022 :       var = gfc_build_addr_expr (type, var);
    4058                 :             :     }
    4059                 :             :   else
    4060                 :             :     {
    4061                 :             :       /* Allocate a temporary to hold the result.  */
    4062                 :        2603 :       var = gfc_create_var (type, "pstr");
    4063                 :        2603 :       gcc_assert (POINTER_TYPE_P (type));
    4064                 :        2603 :       tmp = TREE_TYPE (type);
    4065                 :        2603 :       if (TREE_CODE (tmp) == ARRAY_TYPE)
    4066                 :        2561 :         tmp = TREE_TYPE (tmp);
    4067                 :        2603 :       tmp = TYPE_SIZE_UNIT (tmp);
    4068                 :        2603 :       tmp = fold_build2_loc (input_location, MULT_EXPR, size_type_node,
    4069                 :             :                             fold_convert (size_type_node, len),
    4070                 :             :                             fold_convert (size_type_node, tmp));
    4071                 :        2603 :       tmp = gfc_call_malloc (&se->pre, type, tmp);
    4072                 :        2603 :       gfc_add_modify (&se->pre, var, tmp);
    4073                 :             : 
    4074                 :             :       /* Free the temporary afterwards.  */
    4075                 :        2603 :       tmp = gfc_call_free (var);
    4076                 :        2603 :       gfc_add_expr_to_block (&se->post, tmp);
    4077                 :             :     }
    4078                 :             : 
    4079                 :        4625 :   return var;
    4080                 :             : }
    4081                 :             : 
    4082                 :             : 
    4083                 :             : /* Handle a string concatenation operation.  A temporary will be allocated to
    4084                 :             :    hold the result.  */
    4085                 :             : 
    4086                 :             : static void
    4087                 :        1168 : gfc_conv_concat_op (gfc_se * se, gfc_expr * expr)
    4088                 :             : {
    4089                 :        1168 :   gfc_se lse, rse;
    4090                 :        1168 :   tree len, type, var, tmp, fndecl;
    4091                 :             : 
    4092                 :        1168 :   gcc_assert (expr->value.op.op1->ts.type == BT_CHARACTER
    4093                 :             :               && expr->value.op.op2->ts.type == BT_CHARACTER);
    4094                 :        1168 :   gcc_assert (expr->value.op.op1->ts.kind == expr->value.op.op2->ts.kind);
    4095                 :             : 
    4096                 :        1168 :   gfc_init_se (&lse, se);
    4097                 :        1168 :   gfc_conv_expr (&lse, expr->value.op.op1);
    4098                 :        1168 :   gfc_conv_string_parameter (&lse);
    4099                 :        1168 :   gfc_init_se (&rse, se);
    4100                 :        1168 :   gfc_conv_expr (&rse, expr->value.op.op2);
    4101                 :        1168 :   gfc_conv_string_parameter (&rse);
    4102                 :             : 
    4103                 :        1168 :   gfc_add_block_to_block (&se->pre, &lse.pre);
    4104                 :        1168 :   gfc_add_block_to_block (&se->pre, &rse.pre);
    4105                 :             : 
    4106                 :        1168 :   type = gfc_get_character_type (expr->ts.kind, expr->ts.u.cl);
    4107                 :        1168 :   len = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
    4108                 :        1168 :   if (len == NULL_TREE)
    4109                 :             :     {
    4110                 :        1029 :       len = fold_build2_loc (input_location, PLUS_EXPR,
    4111                 :             :                              gfc_charlen_type_node,
    4112                 :             :                              fold_convert (gfc_charlen_type_node,
    4113                 :             :                                            lse.string_length),
    4114                 :             :                              fold_convert (gfc_charlen_type_node,
    4115                 :             :                                            rse.string_length));
    4116                 :             :     }
    4117                 :             : 
    4118                 :        1168 :   type = build_pointer_type (type);
    4119                 :             : 
    4120                 :        1168 :   var = gfc_conv_string_tmp (se, type, len);
    4121                 :             : 
    4122                 :             :   /* Do the actual concatenation.  */
    4123                 :        1168 :   if (expr->ts.kind == 1)
    4124                 :        1079 :     fndecl = gfor_fndecl_concat_string;
    4125                 :          89 :   else if (expr->ts.kind == 4)
    4126                 :          89 :     fndecl = gfor_fndecl_concat_string_char4;
    4127                 :             :   else
    4128                 :           0 :     gcc_unreachable ();
    4129                 :             : 
    4130                 :        1168 :   tmp = build_call_expr_loc (input_location,
    4131                 :             :                          fndecl, 6, len, var, lse.string_length, lse.expr,
    4132                 :             :                          rse.string_length, rse.expr);
    4133                 :        1168 :   gfc_add_expr_to_block (&se->pre, tmp);
    4134                 :             : 
    4135                 :             :   /* Add the cleanup for the operands.  */
    4136                 :        1168 :   gfc_add_block_to_block (&se->pre, &rse.post);
    4137                 :        1168 :   gfc_add_block_to_block (&se->pre, &lse.post);
    4138                 :             : 
    4139                 :        1168 :   se->expr = var;
    4140                 :        1168 :   se->string_length = len;
    4141                 :        1168 : }
    4142                 :             : 
    4143                 :             : /* Translates an op expression. Common (binary) cases are handled by this
    4144                 :             :    function, others are passed on. Recursion is used in either case.
    4145                 :             :    We use the fact that (op1.ts == op2.ts) (except for the power
    4146                 :             :    operator **).
    4147                 :             :    Operators need no special handling for scalarized expressions as long as
    4148                 :             :    they call gfc_conv_simple_val to get their operands.
    4149                 :             :    Character strings get special handling.  */
    4150                 :             : 
    4151                 :             : static void
    4152                 :      484567 : gfc_conv_expr_op (gfc_se * se, gfc_expr * expr)
    4153                 :             : {
    4154                 :      484567 :   enum tree_code code;
    4155                 :      484567 :   gfc_se lse;
    4156                 :      484567 :   gfc_se rse;
    4157                 :      484567 :   tree tmp, type;
    4158                 :      484567 :   int lop;
    4159                 :      484567 :   int checkstring;
    4160                 :             : 
    4161                 :      484567 :   checkstring = 0;
    4162                 :      484567 :   lop = 0;
    4163                 :      484567 :   switch (expr->value.op.op)
    4164                 :             :     {
    4165                 :       14555 :     case INTRINSIC_PARENTHESES:
    4166                 :       14555 :       if ((expr->ts.type == BT_REAL || expr->ts.type == BT_COMPLEX)
    4167                 :        3438 :           && flag_protect_parens)
    4168                 :             :         {
    4169                 :        3305 :           gfc_conv_unary_op (PAREN_EXPR, se, expr);
    4170                 :        3305 :           gcc_assert (FLOAT_TYPE_P (TREE_TYPE (se->expr)));
    4171                 :       88959 :           return;
    4172                 :             :         }
    4173                 :             : 
    4174                 :             :       /* Fallthrough.  */
    4175                 :       11256 :     case INTRINSIC_UPLUS:
    4176                 :       11256 :       gfc_conv_expr (se, expr->value.op.op1);
    4177                 :       11256 :       return;
    4178                 :             : 
    4179                 :        4895 :     case INTRINSIC_UMINUS:
    4180                 :        4895 :       gfc_conv_unary_op (NEGATE_EXPR, se, expr);
    4181                 :        4895 :       return;
    4182                 :             : 
    4183                 :       19788 :     case INTRINSIC_NOT:
    4184                 :       19788 :       gfc_conv_unary_op (TRUTH_NOT_EXPR, se, expr);
    4185                 :       19788 :       return;
    4186                 :             : 
    4187                 :             :     case INTRINSIC_PLUS:
    4188                 :             :       code = PLUS_EXPR;
    4189                 :             :       break;
    4190                 :             : 
    4191                 :       26721 :     case INTRINSIC_MINUS:
    4192                 :       26721 :       code = MINUS_EXPR;
    4193                 :       26721 :       break;
    4194                 :             : 
    4195                 :       30258 :     case INTRINSIC_TIMES:
    4196                 :       30258 :       code = MULT_EXPR;
    4197                 :       30258 :       break;
    4198                 :             : 
    4199                 :        6309 :     case INTRINSIC_DIVIDE:
    4200                 :             :       /* If expr is a real or complex expr, use an RDIV_EXPR. If op1 is
    4201                 :             :          an integer or unsigned, we must round towards zero, so we use a
    4202                 :             :          TRUNC_DIV_EXPR.  */
    4203                 :        6309 :       if (expr->ts.type == BT_INTEGER || expr->ts.type == BT_UNSIGNED)
    4204                 :             :         code = TRUNC_DIV_EXPR;
    4205                 :             :       else
    4206                 :      395608 :         code = RDIV_EXPR;
    4207                 :             :       break;
    4208                 :             : 
    4209                 :       48547 :     case INTRINSIC_POWER:
    4210                 :       48547 :       gfc_conv_power_op (se, expr);
    4211                 :       48547 :       return;
    4212                 :             : 
    4213                 :        1168 :     case INTRINSIC_CONCAT:
    4214                 :        1168 :       gfc_conv_concat_op (se, expr);
    4215                 :        1168 :       return;
    4216                 :             : 
    4217                 :        4699 :     case INTRINSIC_AND:
    4218                 :        4699 :       code = flag_frontend_optimize ? TRUTH_ANDIF_EXPR : TRUTH_AND_EXPR;
    4219                 :             :       lop = 1;
    4220                 :             :       break;
    4221                 :             : 
    4222                 :       55426 :     case INTRINSIC_OR:
    4223                 :       55426 :       code = flag_frontend_optimize ? TRUTH_ORIF_EXPR : TRUTH_OR_EXPR;
    4224                 :             :       lop = 1;
    4225                 :             :       break;
    4226                 :             : 
    4227                 :             :       /* EQV and NEQV only work on logicals, but since we represent them
    4228                 :             :          as integers, we can use EQ_EXPR and NE_EXPR for them in GIMPLE.  */
    4229                 :       12126 :     case INTRINSIC_EQ:
    4230                 :       12126 :     case INTRINSIC_EQ_OS:
    4231                 :       12126 :     case INTRINSIC_EQV:
    4232                 :       12126 :       code = EQ_EXPR;
    4233                 :       12126 :       checkstring = 1;
    4234                 :       12126 :       lop = 1;
    4235                 :       12126 :       break;
    4236                 :             : 
    4237                 :      195866 :     case INTRINSIC_NE:
    4238                 :      195866 :     case INTRINSIC_NE_OS:
    4239                 :      195866 :     case INTRINSIC_NEQV:
    4240                 :      195866 :       code = NE_EXPR;
    4241                 :      195866 :       checkstring = 1;
    4242                 :      195866 :       lop = 1;
    4243                 :      195866 :       break;
    4244                 :             : 
    4245                 :       11532 :     case INTRINSIC_GT:
    4246                 :       11532 :     case INTRINSIC_GT_OS:
    4247                 :       11532 :       code = GT_EXPR;
    4248                 :       11532 :       checkstring = 1;
    4249                 :       11532 :       lop = 1;
    4250                 :       11532 :       break;
    4251                 :             : 
    4252                 :        1650 :     case INTRINSIC_GE:
    4253                 :        1650 :     case INTRINSIC_GE_OS:
    4254                 :        1650 :       code = GE_EXPR;
    4255                 :        1650 :       checkstring = 1;
    4256                 :        1650 :       lop = 1;
    4257                 :        1650 :       break;
    4258                 :             : 
    4259                 :        4264 :     case INTRINSIC_LT:
    4260                 :        4264 :     case INTRINSIC_LT_OS:
    4261                 :        4264 :       code = LT_EXPR;
    4262                 :        4264 :       checkstring = 1;
    4263                 :        4264 :       lop = 1;
    4264                 :        4264 :       break;
    4265                 :             : 
    4266                 :        2580 :     case INTRINSIC_LE:
    4267                 :        2580 :     case INTRINSIC_LE_OS:
    4268                 :        2580 :       code = LE_EXPR;
    4269                 :        2580 :       checkstring = 1;
    4270                 :        2580 :       lop = 1;
    4271                 :        2580 :       break;
    4272                 :             : 
    4273                 :           0 :     case INTRINSIC_USER:
    4274                 :           0 :     case INTRINSIC_ASSIGN:
    4275                 :             :       /* These should be converted into function calls by the frontend.  */
    4276                 :           0 :       gcc_unreachable ();
    4277                 :             : 
    4278                 :           0 :     default:
    4279                 :           0 :       fatal_error (input_location, "Unknown intrinsic op");
    4280                 :      395608 :       return;
    4281                 :             :     }
    4282                 :             : 
    4283                 :             :   /* The only exception to this is **, which is handled separately anyway.  */
    4284                 :      395608 :   gcc_assert (expr->value.op.op1->ts.type == expr->value.op.op2->ts.type);
    4285                 :             : 
    4286                 :      395608 :   if (checkstring && expr->value.op.op1->ts.type != BT_CHARACTER)
    4287                 :      363794 :     checkstring = 0;
    4288                 :             : 
    4289                 :             :   /* lhs */
    4290                 :      395608 :   gfc_init_se (&lse, se);
    4291                 :      395608 :   gfc_conv_expr (&lse, expr->value.op.op1);
    4292                 :      395608 :   gfc_add_block_to_block (&se->pre, &lse.pre);
    4293                 :             : 
    4294                 :             :   /* rhs */
    4295                 :      395608 :   gfc_init_se (&rse, se);
    4296                 :      395608 :   gfc_conv_expr (&rse, expr->value.op.op2);
    4297                 :      395608 :   gfc_add_block_to_block (&se->pre, &rse.pre);
    4298                 :             : 
    4299                 :      395608 :   if (checkstring)
    4300                 :             :     {
    4301                 :       31814 :       gfc_conv_string_parameter (&lse);
    4302                 :       31814 :       gfc_conv_string_parameter (&rse);
    4303                 :             : 
    4304                 :       63628 :       lse.expr = gfc_build_compare_string (lse.string_length, lse.expr,
    4305                 :             :                                            rse.string_length, rse.expr,
    4306                 :       31814 :                                            expr->value.op.op1->ts.kind,
    4307                 :             :                                            code);
    4308                 :       31814 :       rse.expr = build_int_cst (TREE_TYPE (lse.expr), 0);
    4309                 :       31814 :       gfc_add_block_to_block (&lse.post, &rse.post);
    4310                 :             :     }
    4311                 :             : 
    4312                 :      395608 :   type = gfc_typenode_for_spec (&expr->ts);
    4313                 :             : 
    4314                 :      395608 :   if (lop)
    4315                 :             :     {
    4316                 :             :       // Inhibit overeager optimization of Cray pointer comparisons (PR106692).
    4317                 :      288143 :       if (expr->value.op.op1->expr_type == EXPR_VARIABLE
    4318                 :      163999 :           && expr->value.op.op1->ts.type == BT_INTEGER
    4319                 :       70179 :           && expr->value.op.op1->symtree
    4320                 :       70179 :           && expr->value.op.op1->symtree->n.sym->attr.cray_pointer)
    4321                 :          12 :         TREE_THIS_VOLATILE (lse.expr) = 1;
    4322                 :             : 
    4323                 :      288143 :       if (expr->value.op.op2->expr_type == EXPR_VARIABLE
    4324                 :       70775 :           && expr->value.op.op2->ts.type == BT_INTEGER
    4325                 :       12152 :           && expr->value.op.op2->symtree
    4326                 :       12152 :           && expr->value.op.op2->symtree->n.sym->attr.cray_pointer)
    4327                 :          12 :         TREE_THIS_VOLATILE (rse.expr) = 1;
    4328                 :             : 
    4329                 :             :       /* The result of logical ops is always logical_type_node.  */
    4330                 :      288143 :       tmp = fold_build2_loc (input_location, code, logical_type_node,
    4331                 :             :                              lse.expr, rse.expr);
    4332                 :      288143 :       se->expr = convert (type, tmp);
    4333                 :             :     }
    4334                 :             :   else
    4335                 :      107465 :     se->expr = fold_build2_loc (input_location, code, type, lse.expr, rse.expr);
    4336                 :             : 
    4337                 :             :   /* Add the post blocks.  */
    4338                 :      395608 :   gfc_add_block_to_block (&se->post, &rse.post);
    4339                 :      395608 :   gfc_add_block_to_block (&se->post, &lse.post);
    4340                 :             : }
    4341                 :             : 
    4342                 :             : /* If a string's length is one, we convert it to a single character.  */
    4343                 :             : 
    4344                 :             : tree
    4345                 :      131056 : gfc_string_to_single_character (tree len, tree str, int kind)
    4346                 :             : {
    4347                 :             : 
    4348                 :      131056 :   if (len == NULL
    4349                 :      131056 :       || !tree_fits_uhwi_p (len)
    4350                 :      240314 :       || !POINTER_TYPE_P (TREE_TYPE (str)))
    4351                 :             :     return NULL_TREE;
    4352                 :             : 
    4353                 :      109206 :   if (TREE_INT_CST_LOW (len) == 1)
    4354                 :             :     {
    4355                 :       21683 :       str = fold_convert (gfc_get_pchar_type (kind), str);
    4356                 :       21683 :       return build_fold_indirect_ref_loc (input_location, str);
    4357                 :             :     }
    4358                 :             : 
    4359                 :       87523 :   if (kind == 1
    4360                 :       71565 :       && TREE_CODE (str) == ADDR_EXPR
    4361                 :       61443 :       && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
    4362                 :       44477 :       && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
    4363                 :       27038 :       && array_ref_low_bound (TREE_OPERAND (str, 0))
    4364                 :       27038 :          == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
    4365                 :       27038 :       && TREE_INT_CST_LOW (len) > 1
    4366                 :      112814 :       && TREE_INT_CST_LOW (len)
    4367                 :             :          == (unsigned HOST_WIDE_INT)
    4368                 :       25291 :             TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
    4369                 :             :     {
    4370                 :       25291 :       tree ret = fold_convert (gfc_get_pchar_type (kind), str);
    4371                 :       25291 :       ret = build_fold_indirect_ref_loc (input_location, ret);
    4372                 :       25291 :       if (TREE_CODE (ret) == INTEGER_CST)
    4373                 :             :         {
    4374                 :       25291 :           tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
    4375                 :       25291 :           int i, length = TREE_STRING_LENGTH (string_cst);
    4376                 :       25291 :           const char *ptr = TREE_STRING_POINTER (string_cst);
    4377                 :             : 
    4378                 :       37027 :           for (i = 1; i < length; i++)
    4379                 :       36463 :             if (ptr[i] != ' ')
    4380                 :             :               return NULL_TREE;
    4381                 :             : 
    4382                 :             :           return ret;
    4383                 :             :         }
    4384                 :             :     }
    4385                 :             : 
    4386                 :             :   return NULL_TREE;
    4387                 :             : }
    4388                 :             : 
    4389                 :             : 
    4390                 :             : static void
    4391                 :         171 : conv_scalar_char_value (gfc_symbol *sym, gfc_se *se, gfc_expr **expr)
    4392                 :             : {
    4393                 :         171 :   gcc_assert (expr);
    4394                 :             : 
    4395                 :             :   /* We used to modify the tree here. Now it is done earlier in
    4396                 :             :      the front-end, so we only check it here to avoid regressions.  */
    4397                 :         171 :   if (sym->backend_decl)
    4398                 :             :     {
    4399                 :          66 :       gcc_assert (TREE_CODE (TREE_TYPE (sym->backend_decl)) == INTEGER_TYPE);
    4400                 :          66 :       gcc_assert (TYPE_UNSIGNED (TREE_TYPE (sym->backend_decl)) == 1);
    4401                 :          66 :       gcc_assert (TYPE_PRECISION (TREE_TYPE (sym->backend_decl)) == CHAR_TYPE_SIZE);
    4402                 :          66 :       gcc_assert (DECL_BY_REFERENCE (sym->backend_decl) == 0);
    4403                 :             :     }
    4404                 :             : 
    4405                 :             :   /* If we have a constant character expression, make it into an
    4406                 :             :       integer of type C char.  */
    4407                 :         171 :   if ((*expr)->expr_type == EXPR_CONSTANT)
    4408                 :             :     {
    4409                 :         165 :       gfc_typespec ts;
    4410                 :         165 :       gfc_clear_ts (&ts);
    4411                 :             : 
    4412                 :         330 :       gfc_expr *tmp = gfc_get_int_expr (gfc_default_character_kind, NULL,
    4413                 :         165 :                                         (*expr)->value.character.string[0]);
    4414                 :         165 :       gfc_replace_expr (*expr, tmp);
    4415                 :             :     }
    4416                 :           6 :   else if (se != NULL && (*expr)->expr_type == EXPR_VARIABLE)
    4417                 :             :     {
    4418                 :           6 :       if ((*expr)->ref == NULL)
    4419                 :             :         {
    4420                 :           6 :           se->expr = gfc_string_to_single_character
    4421                 :           6 :             (integer_one_node,
    4422                 :           6 :               gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
    4423                 :             :                                   gfc_get_symbol_decl
    4424                 :           6 :                                   ((*expr)->symtree->n.sym)),
    4425                 :             :               (*expr)->ts.kind);
    4426                 :             :         }
    4427                 :             :       else
    4428                 :             :         {
    4429                 :           0 :           gfc_conv_variable (se, *expr);
    4430                 :           0 :           se->expr = gfc_string_to_single_character
    4431                 :           0 :             (integer_one_node,
    4432                 :             :               gfc_build_addr_expr (gfc_get_pchar_type ((*expr)->ts.kind),
    4433                 :             :                                   se->expr),
    4434                 :           0 :               (*expr)->ts.kind);
    4435                 :             :         }
    4436                 :             :     }
    4437                 :         171 : }
    4438                 :             : 
    4439                 :             : /* Helper function for gfc_build_compare_string.  Return LEN_TRIM value
    4440                 :             :    if STR is a string literal, otherwise return -1.  */
    4441                 :             : 
    4442                 :             : static int
    4443                 :       29462 : gfc_optimize_len_trim (tree len, tree str, int kind)
    4444                 :             : {
    4445                 :       29462 :   if (kind == 1
    4446                 :       24776 :       && TREE_CODE (str) == ADDR_EXPR
    4447                 :       21465 :       && TREE_CODE (TREE_OPERAND (str, 0)) == ARRAY_REF
    4448                 :       13933 :       && TREE_CODE (TREE_OPERAND (TREE_OPERAND (str, 0), 0)) == STRING_CST
    4449                 :        8867 :       && array_ref_low_bound (TREE_OPERAND (str, 0))
    4450                 :        8867 :          == TREE_OPERAND (TREE_OPERAND (str, 0), 1)
    4451                 :        8867 :       && tree_fits_uhwi_p (len)
    4452                 :        8867 :       && tree_to_uhwi (len) >= 1
    4453                 :       29462 :       && tree_to_uhwi (len)
    4454                 :        8823 :          == (unsigned HOST_WIDE_INT)
    4455                 :        8823 :             TREE_STRING_LENGTH (TREE_OPERAND (TREE_OPERAND (str, 0), 0)))
    4456                 :             :     {
    4457                 :        8823 :       tree folded = fold_convert (gfc_get_pchar_type (kind), str);
    4458                 :        8823 :       folded = build_fold_indirect_ref_loc (input_location, folded);
    4459                 :        8823 :       if (TREE_CODE (folded) == INTEGER_CST)
    4460                 :             :         {
    4461                 :        8823 :           tree string_cst = TREE_OPERAND (TREE_OPERAND (str, 0), 0);
    4462                 :        8823 :           int length = TREE_STRING_LENGTH (string_cst);
    4463                 :        8823 :           const char *ptr = TREE_STRING_POINTER (string_cst);
    4464                 :             : 
    4465                 :       11964 :           for (; length > 0; length--)
    4466                 :       11964 :             if (ptr[length - 1] != ' ')
    4467                 :             :               break;
    4468                 :             : 
    4469                 :             :           return length;
    4470                 :             :         }
    4471                 :             :     }
    4472                 :             :   return -1;
    4473                 :             : }
    4474                 :             : 
    4475                 :             : /* Helper to build a call to memcmp.  */
    4476                 :             : 
    4477                 :             : static tree
    4478                 :       11781 : build_memcmp_call (tree s1, tree s2, tree n)
    4479                 :             : {
    4480                 :       11781 :   tree tmp;
    4481                 :             : 
    4482                 :       11781 :   if (!POINTER_TYPE_P (TREE_TYPE (s1)))
    4483                 :           0 :     s1 = gfc_build_addr_expr (pvoid_type_node, s1);
    4484                 :             :   else
    4485                 :       11781 :     s1 = fold_convert (pvoid_type_node, s1);
    4486                 :             : 
    4487                 :       11781 :   if (!POINTER_TYPE_P (TREE_TYPE (s2)))
    4488                 :           0 :     s2 = gfc_build_addr_expr (pvoid_type_node, s2);
    4489                 :             :   else
    4490                 :       11781 :     s2 = fold_convert (pvoid_type_node, s2);
    4491                 :             : 
    4492                 :       11781 :   n = fold_convert (size_type_node, n);
    4493                 :             : 
    4494                 :       11781 :   tmp = build_call_expr_loc (input_location,
    4495                 :             :                              builtin_decl_explicit (BUILT_IN_MEMCMP),
    4496                 :             :                              3, s1, s2, n);
    4497                 :             : 
    4498                 :       11781 :   return fold_convert (integer_type_node, tmp);
    4499                 :             : }
    4500                 :             : 
    4501                 :             : /* Compare two strings. If they are all single characters, the result is the
    4502                 :             :    subtraction of them. Otherwise, we build a library call.  */
    4503                 :             : 
    4504                 :             : tree
    4505                 :       31913 : gfc_build_compare_string (tree len1, tree str1, tree len2, tree str2, int kind,
    4506                 :             :                           enum tree_code code)
    4507                 :             : {
    4508                 :       31913 :   tree sc1;
    4509                 :       31913 :   tree sc2;
    4510                 :       31913 :   tree fndecl;
    4511                 :             : 
    4512                 :       31913 :   gcc_assert (POINTER_TYPE_P (TREE_TYPE (str1)));
    4513                 :       31913 :   gcc_assert (POINTER_TYPE_P (TREE_TYPE (str2)));
    4514                 :             : 
    4515                 :       31913 :   sc1 = gfc_string_to_single_character (len1, str1, kind);
    4516                 :       31913 :   sc2 = gfc_string_to_single_character (len2, str2, kind);
    4517                 :             : 
    4518                 :       31913 :   if (sc1 != NULL_TREE && sc2 != NULL_TREE)
    4519                 :             :     {
    4520                 :             :       /* Deal with single character specially.  */
    4521                 :        4712 :       sc1 = fold_convert (integer_type_node, sc1);
    4522                 :        4712 :       sc2 = fold_convert (integer_type_node, sc2);
    4523                 :        4712 :       return fold_build2_loc (input_location, MINUS_EXPR, integer_type_node,
    4524                 :        4712 :                               sc1, sc2);
    4525                 :             :     }
    4526                 :             : 
    4527                 :       27201 :   if ((code == EQ_EXPR || code == NE_EXPR)
    4528                 :       26645 :       && optimize
    4529                 :       22390 :       && INTEGER_CST_P (len1) && INTEGER_CST_P (len2))
    4530                 :             :     {
    4531                 :             :       /* If one string is a string literal with LEN_TRIM longer
    4532                 :             :          than the length of the second string, the strings
    4533                 :             :          compare unequal.  */
    4534                 :       14731 :       int len = gfc_optimize_len_trim (len1, str1, kind);
    4535                 :       14731 :       if (len > 0 && compare_tree_int (len2, len) < 0)
    4536                 :           0 :         return integer_one_node;
    4537                 :       14731 :       len = gfc_optimize_len_trim (len2, str2, kind);
    4538                 :       14731 :       if (len > 0 && compare_tree_int (len1, len) < 0)
    4539                 :           0 :         return integer_one_node;
    4540                 :             :     }
    4541                 :             : 
    4542                 :             :   /* We can compare via memcpy if the strings are known to be equal
    4543                 :             :      in length and they are
    4544                 :             :      - kind=1
    4545                 :             :      - kind=4 and the comparison is for (in)equality.  */
    4546                 :             : 
    4547                 :       17907 :   if (INTEGER_CST_P (len1) && INTEGER_CST_P (len2)
    4548                 :       17569 :       && tree_int_cst_equal (len1, len2)
    4549                 :       39042 :       && (kind == 1 || code == EQ_EXPR || code == NE_EXPR))
    4550                 :             :     {
    4551                 :       11781 :       tree tmp;
    4552                 :       11781 :       tree chartype;
    4553                 :             : 
    4554                 :       11781 :       chartype = gfc_get_char_type (kind);
    4555                 :       11781 :       tmp = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE(len1),
    4556                 :       11781 :                              fold_convert (TREE_TYPE(len1),
    4557                 :             :                                            TYPE_SIZE_UNIT(chartype)),
    4558                 :             :                              len1);
    4559                 :       11781 :       return build_memcmp_call (str1, str2, tmp);
    4560                 :             :     }
    4561                 :             : 
    4562                 :             :   /* Build a call for the comparison.  */
    4563                 :       15420 :   if (kind == 1)
    4564                 :       12598 :     fndecl = gfor_fndecl_compare_string;
    4565                 :        2822 :   else if (kind == 4)
    4566                 :        2822 :     fndecl = gfor_fndecl_compare_string_char4;
    4567                 :             :   else
    4568                 :           0 :     gcc_unreachable ();
    4569                 :             : 
    4570                 :       15420 :   return build_call_expr_loc (input_location, fndecl, 4,
    4571                 :       15420 :                               len1, str1, len2, str2);
    4572                 :             : }
    4573                 :             : 
    4574                 :             : 
    4575                 :             : /* Return the backend_decl for a procedure pointer component.  */
    4576                 :             : 
    4577                 :             : static tree
    4578                 :        1713 : get_proc_ptr_comp (gfc_expr *e)
    4579                 :             : {
    4580                 :        1713 :   gfc_se comp_se;
    4581                 :        1713 :   gfc_expr *e2;
    4582                 :        1713 :   expr_t old_type;
    4583                 :             : 
    4584                 :        1713 :   gfc_init_se (&comp_se, NULL);
    4585                 :        1713 :   e2 = gfc_copy_expr (e);
    4586                 :             :   /* We have to restore the expr type later so that gfc_free_expr frees
    4587                 :             :      the exact same thing that was allocated.
    4588                 :             :      TODO: This is ugly.  */
    4589                 :        1713 :   old_type = e2->expr_type;
    4590                 :        1713 :   e2->expr_type = EXPR_VARIABLE;
    4591                 :        1713 :   gfc_conv_expr (&comp_se, e2);
    4592                 :        1713 :   e2->expr_type = old_type;
    4593                 :        1713 :   gfc_free_expr (e2);
    4594                 :        1713 :   return build_fold_addr_expr_loc (input_location, comp_se.expr);
    4595                 :             : }
    4596                 :             : 
    4597                 :             : 
    4598                 :             : /* Convert a typebound function reference from a class object.  */
    4599                 :             : static void
    4600                 :          80 : conv_base_obj_fcn_val (gfc_se * se, tree base_object, gfc_expr * expr)
    4601                 :             : {
    4602                 :          80 :   gfc_ref *ref;
    4603                 :          80 :   tree var;
    4604                 :             : 
    4605                 :          80 :   if (!VAR_P (base_object))
    4606                 :             :     {
    4607                 :           0 :       var = gfc_create_var (TREE_TYPE (base_object), NULL);
    4608                 :           0 :       gfc_add_modify (&se->pre, var, base_object);
    4609                 :             :     }
    4610                 :          80 :   se->expr = gfc_class_vptr_get (base_object);
    4611                 :          80 :   se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
    4612                 :          80 :   ref = expr->ref;
    4613                 :         308 :   while (ref && ref->next)
    4614                 :             :     ref = ref->next;
    4615                 :          80 :   gcc_assert (ref && ref->type == REF_COMPONENT);
    4616                 :          80 :   if (ref->u.c.sym->attr.extension)
    4617                 :           0 :     conv_parent_component_references (se, ref);
    4618                 :          80 :   gfc_conv_component_ref (se, ref);
    4619                 :          80 :   se->expr = build_fold_addr_expr_loc (input_location, se->expr);
    4620                 :          80 : }
    4621                 :             : 
    4622                 :             : static tree
    4623                 :      122673 : get_builtin_fn (gfc_symbol * sym)
    4624                 :             : {
    4625                 :      122673 :   if (!gfc_option.disable_omp_is_initial_device
    4626                 :      122669 :       && flag_openmp && sym->attr.function && sym->ts.type == BT_LOGICAL
    4627                 :         610 :       && !strcmp (sym->name, "omp_is_initial_device"))
    4628                 :          23 :     return builtin_decl_explicit (BUILT_IN_OMP_IS_INITIAL_DEVICE);
    4629                 :             : 
    4630                 :      122650 :   if (!gfc_option.disable_acc_on_device
    4631                 :      122470 :       && flag_openacc && sym->attr.function && sym->ts.type == BT_LOGICAL
    4632                 :        1163 :       && !strcmp (sym->name, "acc_on_device_h"))
    4633                 :         390 :     return builtin_decl_explicit (BUILT_IN_ACC_ON_DEVICE);
    4634                 :             : 
    4635                 :             :   return NULL_TREE;
    4636                 :             : }
    4637                 :             : 
    4638                 :             : static tree
    4639                 :         413 : update_builtin_function (tree fn_call, gfc_symbol *sym)
    4640                 :             : {
    4641                 :         413 :   tree fn = TREE_OPERAND (CALL_EXPR_FN (fn_call), 0);
    4642                 :             : 
    4643                 :         413 :   if (DECL_FUNCTION_CODE (fn) == BUILT_IN_OMP_IS_INITIAL_DEVICE)
    4644                 :             :      /* In Fortran omp_is_initial_device returns logical(4)
    4645                 :             :         but the builtin uses 'int'.  */
    4646                 :          23 :     return fold_convert (TREE_TYPE (TREE_TYPE (sym->backend_decl)), fn_call);
    4647                 :             : 
    4648                 :         390 :   else if (DECL_FUNCTION_CODE (fn) == BUILT_IN_ACC_ON_DEVICE)
    4649                 :             :     {
    4650                 :             :       /* Likewise for the return type; additionally, the argument it a
    4651                 :             :          call-by-value int, Fortran has a by-reference 'integer(4)'.  */
    4652                 :         390 :       tree arg = build_fold_indirect_ref_loc (input_location,
    4653                 :         390 :                                               CALL_EXPR_ARG (fn_call, 0));
    4654                 :         390 :       CALL_EXPR_ARG (fn_call, 0) = fold_convert (integer_type_node, arg);
    4655                 :         390 :       return fold_convert (TREE_TYPE (TREE_TYPE (sym->backend_decl)), fn_call);
    4656                 :             :     }
    4657                 :             :   return fn_call;
    4658                 :             : }
    4659                 :             : 
    4660                 :             : static void
    4661                 :      125179 : conv_function_val (gfc_se * se, bool *is_builtin, gfc_symbol * sym,
    4662                 :             :                    gfc_expr * expr, gfc_actual_arglist *actual_args)
    4663                 :             : {
    4664                 :      125179 :   tree tmp;
    4665                 :             : 
    4666                 :      125179 :   if (gfc_is_proc_ptr_comp (expr))
    4667                 :        1713 :     tmp = get_proc_ptr_comp (expr);
    4668                 :      123466 :   else if (sym->attr.dummy)
    4669                 :             :     {
    4670                 :         793 :       tmp = gfc_get_symbol_decl (sym);
    4671                 :         793 :       if (sym->attr.proc_pointer)
    4672                 :          83 :         tmp = build_fold_indirect_ref_loc (input_location,
    4673                 :             :                                        tmp);
    4674                 :         793 :       gcc_assert (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE
    4675                 :             :               && TREE_CODE (TREE_TYPE (TREE_TYPE (tmp))) == FUNCTION_TYPE);
    4676                 :             :     }
    4677                 :             :   else
    4678                 :             :     {
    4679                 :      122673 :       if (!sym->backend_decl)
    4680                 :       30940 :         sym->backend_decl = gfc_get_extern_function_decl (sym, actual_args);
    4681                 :             : 
    4682                 :      122673 :       if ((tmp = get_builtin_fn (sym)) != NULL_TREE)
    4683                 :         413 :         *is_builtin = true;
    4684                 :             :       else
    4685                 :             :         {
    4686                 :      122260 :           TREE_USED (sym->backend_decl) = 1;
    4687                 :      122260 :           tmp = sym->backend_decl;
    4688                 :             :         }
    4689                 :             : 
    4690                 :      122673 :       if (sym->attr.cray_pointee)
    4691                 :             :         {
    4692                 :             :           /* TODO - make the cray pointee a pointer to a procedure,
    4693                 :             :              assign the pointer to it and use it for the call.  This
    4694                 :             :              will do for now!  */
    4695                 :          19 :           tmp = convert (build_pointer_type (TREE_TYPE (tmp)),
    4696                 :             :                          gfc_get_symbol_decl (sym->cp_pointer));
    4697                 :          19 :           tmp = gfc_evaluate_now (tmp, &se->pre);
    4698                 :             :         }
    4699                 :             : 
    4700                 :      122673 :       if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
    4701                 :             :         {
    4702                 :      122094 :           gcc_assert (TREE_CODE (tmp) == FUNCTION_DECL);
    4703                 :      122094 :           tmp = gfc_build_addr_expr (NULL_TREE, tmp);
    4704                 :             :         }
    4705                 :             :     }
    4706                 :      125179 :   se->expr = tmp;
    4707                 :      125179 : }
    4708                 :             : 
    4709                 :             : 
    4710                 :             : /* Initialize MAPPING.  */
    4711                 :             : 
    4712                 :             : void
    4713                 :      125296 : gfc_init_interface_mapping (gfc_interface_mapping * mapping)
    4714                 :             : {
    4715                 :      125296 :   mapping->syms = NULL;
    4716                 :      125296 :   mapping->charlens = NULL;
    4717                 :      125296 : }
    4718                 :             : 
    4719                 :             : 
    4720                 :             : /* Free all memory held by MAPPING (but not MAPPING itself).  */
    4721                 :             : 
    4722                 :             : void
    4723                 :      125296 : gfc_free_interface_mapping (gfc_interface_mapping * mapping)
    4724                 :             : {
    4725                 :      125296 :   gfc_interface_sym_mapping *sym;
    4726                 :      125296 :   gfc_interface_sym_mapping *nextsym;
    4727                 :      125296 :   gfc_charlen *cl;
    4728                 :      125296 :   gfc_charlen *nextcl;
    4729                 :             : 
    4730                 :      162651 :   for (sym = mapping->syms; sym; sym = nextsym)
    4731                 :             :     {
    4732                 :       37355 :       nextsym = sym->next;
    4733                 :       37355 :       sym->new_sym->n.sym->formal = NULL;
    4734                 :       37355 :       gfc_free_symbol (sym->new_sym->n.sym);
    4735                 :       37355 :       gfc_free_expr (sym->expr);
    4736                 :       37355 :       free (sym->new_sym);
    4737                 :       37355 :       free (sym);
    4738                 :             :     }
    4739                 :      129625 :   for (cl = mapping->charlens; cl; cl = nextcl)
    4740                 :             :     {
    4741                 :        4329 :       nextcl = cl->next;
    4742                 :        4329 :       gfc_free_expr (cl->length);
    4743                 :        4329 :       free (cl);
    4744                 :             :     }
    4745                 :      125296 : }
    4746                 :             : 
    4747                 :             : 
    4748                 :             : /* Return a copy of gfc_charlen CL.  Add the returned structure to
    4749                 :             :    MAPPING so that it will be freed by gfc_free_interface_mapping.  */
    4750                 :             : 
    4751                 :             : static gfc_charlen *
    4752                 :        4329 : gfc_get_interface_mapping_charlen (gfc_interface_mapping * mapping,
    4753                 :             :                                    gfc_charlen * cl)
    4754                 :             : {
    4755                 :        4329 :   gfc_charlen *new_charlen;
    4756                 :             : 
    4757                 :        4329 :   new_charlen = gfc_get_charlen ();
    4758                 :        4329 :   new_charlen->next = mapping->charlens;
    4759                 :        4329 :   new_charlen->length = gfc_copy_expr (cl->length);
    4760                 :             : 
    4761                 :        4329 :   mapping->charlens = new_charlen;
    4762                 :        4329 :   return new_charlen;
    4763                 :             : }
    4764                 :             : 
    4765                 :             : 
    4766                 :             : /* A subroutine of gfc_add_interface_mapping.  Return a descriptorless
    4767                 :             :    array variable that can be used as the actual argument for dummy
    4768                 :             :    argument SYM.  Add any initialization code to BLOCK.  PACKED is as
    4769                 :             :    for gfc_get_nodesc_array_type and DATA points to the first element
    4770                 :             :    in the passed array.  */
    4771                 :             : 
    4772                 :             : static tree
    4773                 :        7333 : gfc_get_interface_mapping_array (stmtblock_t * block, gfc_symbol * sym,
    4774                 :             :                                  gfc_packed packed, tree data, tree len)
    4775                 :             : {
    4776                 :        7333 :   tree type;
    4777                 :        7333 :   tree var;
    4778                 :             : 
    4779                 :        7333 :   if (len != NULL_TREE && (TREE_CONSTANT (len) || VAR_P (len)))
    4780                 :          58 :     type = gfc_get_character_type_len (sym->ts.kind, len);
    4781                 :             :   else
    4782                 :        7275 :     type = gfc_typenode_for_spec (&sym->ts);
    4783                 :       14666 :   type = gfc_get_nodesc_array_type (type, sym->as, packed,
    4784                 :             :                                     !sym->attr.target && !sym->attr.pointer
    4785                 :        7333 :                                     && !sym->attr.proc_pointer);
    4786                 :             : 
    4787                 :        7333 :   var = gfc_create_var (type, "ifm");
    4788                 :        7333 :   gfc_add_modify (block, var, fold_convert (type, data));
    4789                 :             : 
    4790                 :        7333 :   return var;
    4791                 :             : }
    4792                 :             : 
    4793                 :             : 
    4794                 :             : /* A subroutine of gfc_add_interface_mapping.  Set the stride, upper bounds
    4795                 :             :    and offset of descriptorless array type TYPE given that it has the same
    4796                 :             :    size as DESC.  Add any set-up code to BLOCK.  */
    4797                 :             : 
    4798                 :             : static void
    4799                 :        7063 : gfc_set_interface_mapping_bounds (stmtblock_t * block, tree type, tree desc)
    4800                 :             : {
    4801                 :        7063 :   int n;
    4802                 :        7063 :   tree dim;
    4803                 :        7063 :   tree offset;
    4804                 :        7063 :   tree tmp;
    4805                 :             : 
    4806                 :        7063 :   offset = gfc_index_zero_node;
    4807                 :        8138 :   for (n = 0; n < GFC_TYPE_ARRAY_RANK (type); n++)
    4808                 :             :     {
    4809                 :        1075 :       dim = gfc_rank_cst[n];
    4810                 :        1075 :       GFC_TYPE_ARRAY_STRIDE (type, n) = gfc_conv_array_stride (desc, n);
    4811                 :        1075 :       if (GFC_TYPE_ARRAY_LBOUND (type, n) == NULL_TREE)
    4812                 :             :         {
    4813                 :           1 :           GFC_TYPE_ARRAY_LBOUND (type, n)
    4814                 :           1 :                 = gfc_conv_descriptor_lbound_get (desc, dim);
    4815                 :           1 :           GFC_TYPE_ARRAY_UBOUND (type, n)
    4816                 :           2 :                 = gfc_conv_descriptor_ubound_get (desc, dim);
    4817                 :             :         }
    4818                 :        1074 :       else if (GFC_TYPE_ARRAY_UBOUND (type, n) == NULL_TREE)
    4819                 :             :         {
    4820                 :        1074 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    4821                 :             :                                  gfc_array_index_type,
    4822                 :             :                                  gfc_conv_descriptor_ubound_get (desc, dim),
    4823                 :             :                                  gfc_conv_descriptor_lbound_get (desc, dim));
    4824                 :        3222 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    4825                 :             :                                  gfc_array_index_type,
    4826                 :        1074 :                                  GFC_TYPE_ARRAY_LBOUND (type, n), tmp);
    4827                 :        1074 :           tmp = gfc_evaluate_now (tmp, block);
    4828                 :        1074 :           GFC_TYPE_ARRAY_UBOUND (type, n) = tmp;
    4829                 :             :         }
    4830                 :        4300 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    4831                 :        1075 :                              GFC_TYPE_ARRAY_LBOUND (type, n),
    4832                 :        1075 :                              GFC_TYPE_ARRAY_STRIDE (type, n));
    4833                 :        1075 :       offset = fold_build2_loc (input_location, MINUS_EXPR,
    4834                 :             :                                 gfc_array_index_type, offset, tmp);
    4835                 :             :     }
    4836                 :        7063 :   offset = gfc_evaluate_now (offset, block);
    4837                 :        7063 :   GFC_TYPE_ARRAY_OFFSET (type) = offset;
    4838                 :        7063 : }
    4839                 :             : 
    4840                 :             : 
    4841                 :             : /* Extend MAPPING so that it maps dummy argument SYM to the value stored
    4842                 :             :    in SE.  The caller may still use se->expr and se->string_length after
    4843                 :             :    calling this function.  */
    4844                 :             : 
    4845                 :             : void
    4846                 :       37355 : gfc_add_interface_mapping (gfc_interface_mapping * mapping,
    4847                 :             :                            gfc_symbol * sym, gfc_se * se,
    4848                 :             :                            gfc_expr *expr)
    4849                 :             : {
    4850                 :       37355 :   gfc_interface_sym_mapping *sm;
    4851                 :       37355 :   tree desc;
    4852                 :       37355 :   tree tmp;
    4853                 :       37355 :   tree value;
    4854                 :       37355 :   gfc_symbol *new_sym;
    4855                 :       37355 :   gfc_symtree *root;
    4856                 :       37355 :   gfc_symtree *new_symtree;
    4857                 :             : 
    4858                 :             :   /* Create a new symbol to represent the actual argument.  */
    4859                 :       37355 :   new_sym = gfc_new_symbol (sym->name, NULL);
    4860                 :       37355 :   new_sym->ts = sym->ts;
    4861                 :       37355 :   new_sym->as = gfc_copy_array_spec (sym->as);
    4862                 :       37355 :   new_sym->attr.referenced = 1;
    4863                 :       37355 :   new_sym->attr.dimension = sym->attr.dimension;
    4864                 :       37355 :   new_sym->attr.contiguous = sym->attr.contiguous;
    4865                 :       37355 :   new_sym->attr.codimension = sym->attr.codimension;
    4866                 :       37355 :   new_sym->attr.pointer = sym->attr.pointer;
    4867                 :       37355 :   new_sym->attr.allocatable = sym->attr.allocatable;
    4868                 :       37355 :   new_sym->attr.flavor = sym->attr.flavor;
    4869                 :       37355 :   new_sym->attr.function = sym->attr.function;
    4870                 :             : 
    4871                 :             :   /* Ensure that the interface is available and that
    4872                 :             :      descriptors are passed for array actual arguments.  */
    4873                 :       37355 :   if (sym->attr.flavor == FL_PROCEDURE)
    4874                 :             :     {
    4875                 :          36 :       new_sym->formal = expr->symtree->n.sym->formal;
    4876                 :          36 :       new_sym->attr.always_explicit
    4877                 :          36 :             = expr->symtree->n.sym->attr.always_explicit;
    4878                 :             :     }
    4879                 :             : 
    4880                 :             :   /* Create a fake symtree for it.  */
    4881                 :       37355 :   root = NULL;
    4882                 :       37355 :   new_symtree = gfc_new_symtree (&root, sym->name);
    4883                 :       37355 :   new_symtree->n.sym = new_sym;
    4884                 :       37355 :   gcc_assert (new_symtree == root);
    4885                 :             : 
    4886                 :             :   /* Create a dummy->actual mapping.  */
    4887                 :       37355 :   sm = XCNEW (gfc_interface_sym_mapping);
    4888                 :       37355 :   sm->next = mapping->syms;
    4889                 :       37355 :   sm->old = sym;
    4890                 :       37355 :   sm->new_sym = new_symtree;
    4891                 :       37355 :   sm->expr = gfc_copy_expr (expr);
    4892                 :       37355 :   mapping->syms = sm;
    4893                 :             : 
    4894                 :             :   /* Stabilize the argument's value.  */
    4895                 :       37355 :   if (!sym->attr.function && se)
    4896                 :       37257 :     se->expr = gfc_evaluate_now (se->expr, &se->pre);
    4897                 :             : 
    4898                 :       37355 :   if (sym->ts.type == BT_CHARACTER)
    4899                 :             :     {
    4900                 :             :       /* Create a copy of the dummy argument's length.  */
    4901                 :        2545 :       new_sym->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, sym->ts.u.cl);
    4902                 :        2545 :       sm->expr->ts.u.cl = new_sym->ts.u.cl;
    4903                 :             : 
    4904                 :             :       /* If the length is specified as "*", record the length that
    4905                 :             :          the caller is passing.  We should use the callee's length
    4906                 :             :          in all other cases.  */
    4907                 :        2545 :       if (!new_sym->ts.u.cl->length && se)
    4908                 :             :         {
    4909                 :        2317 :           se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
    4910                 :        2317 :           new_sym->ts.u.cl->backend_decl = se->string_length;
    4911                 :             :         }
    4912                 :             :     }
    4913                 :             : 
    4914                 :       37341 :   if (!se)
    4915                 :          62 :     return;
    4916                 :             : 
    4917                 :             :   /* Use the passed value as-is if the argument is a function.  */
    4918                 :       37293 :   if (sym->attr.flavor == FL_PROCEDURE)
    4919                 :          36 :     value = se->expr;
    4920                 :             : 
    4921                 :             :   /* If the argument is a pass-by-value scalar, use the value as is.  */
    4922                 :       37257 :   else if (!sym->attr.dimension && sym->attr.value)
    4923                 :          63 :     value = se->expr;
    4924                 :             : 
    4925                 :             :   /* If the argument is either a string or a pointer to a string,
    4926                 :             :      convert it to a boundless character type.  */
    4927                 :       37194 :   else if (!sym->attr.dimension && sym->ts.type == BT_CHARACTER)
    4928                 :             :     {
    4929                 :        1216 :       se->string_length = gfc_evaluate_now (se->string_length, &se->pre);
    4930                 :        1216 :       tmp = gfc_get_character_type_len (sym->ts.kind, se->string_length);
    4931                 :        1216 :       tmp = build_pointer_type (tmp);
    4932                 :        1216 :       if (sym->attr.pointer)
    4933                 :         126 :         value = build_fold_indirect_ref_loc (input_location,
    4934                 :             :                                          se->expr);
    4935                 :             :       else
    4936                 :        1090 :         value = se->expr;
    4937                 :        1216 :       value = fold_convert (tmp, value);
    4938                 :             :     }
    4939                 :             : 
    4940                 :             :   /* If the argument is a scalar, a pointer to an array or an allocatable,
    4941                 :             :      dereference it.  */
    4942                 :       35978 :   else if (!sym->attr.dimension || sym->attr.pointer || sym->attr.allocatable)
    4943                 :       27388 :     value = build_fold_indirect_ref_loc (input_location,
    4944                 :             :                                      se->expr);
    4945                 :             : 
    4946                 :             :   /* For character(*), use the actual argument's descriptor.  */
    4947                 :        8590 :   else if (sym->ts.type == BT_CHARACTER && !new_sym->ts.u.cl->length)
    4948                 :        1257 :     value = build_fold_indirect_ref_loc (input_location,
    4949                 :             :                                          se->expr);
    4950                 :             : 
    4951                 :             :   /* If the argument is an array descriptor, use it to determine
    4952                 :             :      information about the actual argument's shape.  */
    4953                 :        7333 :   else if (POINTER_TYPE_P (TREE_TYPE (se->expr))
    4954                 :        7333 :            && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (TREE_TYPE (se->expr))))
    4955                 :             :     {
    4956                 :             :       /* Get the actual argument's descriptor.  */
    4957                 :        7063 :       desc = build_fold_indirect_ref_loc (input_location,
    4958                 :             :                                       se->expr);
    4959                 :             : 
    4960                 :             :       /* Create the replacement variable.  */
    4961                 :        7063 :       tmp = gfc_conv_descriptor_data_get (desc);
    4962                 :        7063 :       value = gfc_get_interface_mapping_array (&se->pre, sym,
    4963                 :             :                                                PACKED_NO, tmp,
    4964                 :             :                                                se->string_length);
    4965                 :             : 
    4966                 :             :       /* Use DESC to work out the upper bounds, strides and offset.  */
    4967                 :        7063 :       gfc_set_interface_mapping_bounds (&se->pre, TREE_TYPE (value), desc);
    4968                 :             :     }
    4969                 :             :   else
    4970                 :             :     /* Otherwise we have a packed array.  */
    4971                 :         270 :     value = gfc_get_interface_mapping_array (&se->pre, sym,
    4972                 :             :                                              PACKED_FULL, se->expr,
    4973                 :             :                                              se->string_length);
    4974                 :             : 
    4975                 :       37293 :   new_sym->backend_decl = value;
    4976                 :             : }
    4977                 :             : 
    4978                 :             : 
    4979                 :             : /* Called once all dummy argument mappings have been added to MAPPING,
    4980                 :             :    but before the mapping is used to evaluate expressions.  Pre-evaluate
    4981                 :             :    the length of each argument, adding any initialization code to PRE and
    4982                 :             :    any finalization code to POST.  */
    4983                 :             : 
    4984                 :             : static void
    4985                 :      125259 : gfc_finish_interface_mapping (gfc_interface_mapping * mapping,
    4986                 :             :                               stmtblock_t * pre, stmtblock_t * post)
    4987                 :             : {
    4988                 :      125259 :   gfc_interface_sym_mapping *sym;
    4989                 :      125259 :   gfc_expr *expr;
    4990                 :      125259 :   gfc_se se;
    4991                 :             : 
    4992                 :      162552 :   for (sym = mapping->syms; sym; sym = sym->next)
    4993                 :       37293 :     if (sym->new_sym->n.sym->ts.type == BT_CHARACTER
    4994                 :        2531 :         && !sym->new_sym->n.sym->ts.u.cl->backend_decl)
    4995                 :             :       {
    4996                 :         214 :         expr = sym->new_sym->n.sym->ts.u.cl->length;
    4997                 :         214 :         gfc_apply_interface_mapping_to_expr (mapping, expr);
    4998                 :         214 :         gfc_init_se (&se, NULL);
    4999                 :         214 :         gfc_conv_expr (&se, expr);
    5000                 :         214 :         se.expr = fold_convert (gfc_charlen_type_node, se.expr);
    5001                 :         214 :         se.expr = gfc_evaluate_now (se.expr, &se.pre);
    5002                 :         214 :         gfc_add_block_to_block (pre, &se.pre);
    5003                 :         214 :         gfc_add_block_to_block (post, &se.post);
    5004                 :             : 
    5005                 :         214 :         sym->new_sym->n.sym->ts.u.cl->backend_decl = se.expr;
    5006                 :             :       }
    5007                 :      125259 : }
    5008                 :             : 
    5009                 :             : 
    5010                 :             : /* Like gfc_apply_interface_mapping_to_expr, but applied to
    5011                 :             :    constructor C.  */
    5012                 :             : 
    5013                 :             : static void
    5014                 :          47 : gfc_apply_interface_mapping_to_cons (gfc_interface_mapping * mapping,
    5015                 :             :                                      gfc_constructor_base base)
    5016                 :             : {
    5017                 :          47 :   gfc_constructor *c;
    5018                 :         428 :   for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
    5019                 :             :     {
    5020                 :         381 :       gfc_apply_interface_mapping_to_expr (mapping, c->expr);
    5021                 :         381 :       if (c->iterator)
    5022                 :             :         {
    5023                 :           6 :           gfc_apply_interface_mapping_to_expr (mapping, c->iterator->start);
    5024                 :           6 :           gfc_apply_interface_mapping_to_expr (mapping, c->iterator->end);
    5025                 :           6 :           gfc_apply_interface_mapping_to_expr (mapping, c->iterator->step);
    5026                 :             :         }
    5027                 :             :     }
    5028                 :          47 : }
    5029                 :             : 
    5030                 :             : 
    5031                 :             : /* Like gfc_apply_interface_mapping_to_expr, but applied to
    5032                 :             :    reference REF.  */
    5033                 :             : 
    5034                 :             : static void
    5035                 :       12421 : gfc_apply_interface_mapping_to_ref (gfc_interface_mapping * mapping,
    5036                 :             :                                     gfc_ref * ref)
    5037                 :             : {
    5038                 :       12421 :   int n;
    5039                 :             : 
    5040                 :       13864 :   for (; ref; ref = ref->next)
    5041                 :        1443 :     switch (ref->type)
    5042                 :             :       {
    5043                 :             :       case REF_ARRAY:
    5044                 :        2873 :         for (n = 0; n < ref->u.ar.dimen; n++)
    5045                 :             :           {
    5046                 :        1632 :             gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.start[n]);
    5047                 :        1632 :             gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.end[n]);
    5048                 :        1632 :             gfc_apply_interface_mapping_to_expr (mapping, ref->u.ar.stride[n]);
    5049                 :             :           }
    5050                 :             :         break;
    5051                 :             : 
    5052                 :             :       case REF_COMPONENT:
    5053                 :             :       case REF_INQUIRY:
    5054                 :             :         break;
    5055                 :             : 
    5056                 :          43 :       case REF_SUBSTRING:
    5057                 :          43 :         gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.start);
    5058                 :          43 :         gfc_apply_interface_mapping_to_expr (mapping, ref->u.ss.end);
    5059                 :          43 :         break;
    5060                 :             :       }
    5061                 :       12421 : }
    5062                 :             : 
    5063                 :             : 
    5064                 :             : /* Convert intrinsic function calls into result expressions.  */
    5065                 :             : 
    5066                 :             : static bool
    5067                 :        2184 : gfc_map_intrinsic_function (gfc_expr *expr, gfc_interface_mapping *mapping)
    5068                 :             : {
    5069                 :        2184 :   gfc_symbol *sym;
    5070                 :        2184 :   gfc_expr *new_expr;
    5071                 :        2184 :   gfc_expr *arg1;
    5072                 :        2184 :   gfc_expr *arg2;
    5073                 :        2184 :   int d, dup;
    5074                 :             : 
    5075                 :        2184 :   arg1 = expr->value.function.actual->expr;
    5076                 :        2184 :   if (expr->value.function.actual->next)
    5077                 :        2063 :     arg2 = expr->value.function.actual->next->expr;
    5078                 :             :   else
    5079                 :             :     arg2 = NULL;
    5080                 :             : 
    5081                 :        2184 :   sym = arg1->symtree->n.sym;
    5082                 :             : 
    5083                 :        2184 :   if (sym->attr.dummy)
    5084                 :             :     return false;
    5085                 :             : 
    5086                 :        2160 :   new_expr = NULL;
    5087                 :             : 
    5088                 :        2160 :   switch (expr->value.function.isym->id)
    5089                 :             :     {
    5090                 :         929 :     case GFC_ISYM_LEN:
    5091                 :             :       /* TODO figure out why this condition is necessary.  */
    5092                 :         929 :       if (sym->attr.function
    5093                 :          43 :           && (arg1->ts.u.cl->length == NULL
    5094                 :          42 :               || (arg1->ts.u.cl->length->expr_type != EXPR_CONSTANT
    5095                 :          42 :                   && arg1->ts.u.cl->length->expr_type != EXPR_VARIABLE)))
    5096                 :             :         return false;
    5097                 :             : 
    5098                 :         886 :       new_expr = gfc_copy_expr (arg1->ts.u.cl->length);
    5099                 :         886 :       break;
    5100                 :             : 
    5101                 :         228 :     case GFC_ISYM_LEN_TRIM:
    5102                 :         228 :       new_expr = gfc_copy_expr (arg1);
    5103                 :         228 :       gfc_apply_interface_mapping_to_expr (mapping, new_expr);
    5104                 :             : 
    5105                 :         228 :       if (!new_expr)
    5106                 :             :         return false;
    5107                 :             : 
    5108                 :         228 :       gfc_replace_expr (arg1, new_expr);
    5109                 :         228 :       return true;
    5110                 :             : 
    5111                 :         588 :     case GFC_ISYM_SIZE:
    5112                 :         588 :       if (!sym->as || sym->as->rank == 0)
    5113                 :             :         return false;
    5114                 :             : 
    5115                 :         530 :       if (arg2 && arg2->expr_type == EXPR_CONSTANT)
    5116                 :             :         {
    5117                 :         360 :           dup = mpz_get_si (arg2->value.integer);
    5118                 :         360 :           d = dup - 1;
    5119                 :             :         }
    5120                 :             :       else
    5121                 :             :         {
    5122                 :         530 :           dup = sym->as->rank;
    5123                 :         530 :           d = 0;
    5124                 :             :         }
    5125                 :             : 
    5126                 :         542 :       for (; d < dup; d++)
    5127                 :             :         {
    5128                 :         530 :           gfc_expr *tmp;
    5129                 :             : 
    5130                 :         530 :           if (!sym->as->upper[d] || !sym->as->lower[d])
    5131                 :             :             {
    5132                 :         518 :               gfc_free_expr (new_expr);
    5133                 :         518 :               return false;
    5134                 :             :             }
    5135                 :             : 
    5136                 :          12 :           tmp = gfc_add (gfc_copy_expr (sym->as->upper[d]),
    5137                 :             :                                         gfc_get_int_expr (gfc_default_integer_kind,
    5138                 :             :                                                           NULL, 1));
    5139                 :          12 :           tmp = gfc_subtract (tmp, gfc_copy_expr (sym->as->lower[d]));
    5140                 :          12 :           if (new_expr)
    5141                 :           0 :             new_expr = gfc_multiply (new_expr, tmp);
    5142                 :             :           else
    5143                 :             :             new_expr = tmp;
    5144                 :             :         }
    5145                 :             :       break;
    5146                 :             : 
    5147                 :          44 :     case GFC_ISYM_LBOUND:
    5148                 :          44 :     case GFC_ISYM_UBOUND:
    5149                 :             :         /* TODO These implementations of lbound and ubound do not limit if
    5150                 :             :            the size < 0, according to F95's 13.14.53 and 13.14.113.  */
    5151                 :             : 
    5152                 :          44 :       if (!sym->as || sym->as->rank == 0)
    5153                 :             :         return false;
    5154                 :             : 
    5155                 :          44 :       if (arg2 && arg2->expr_type == EXPR_CONSTANT)
    5156                 :          38 :         d = mpz_get_si (arg2->value.integer) - 1;
    5157                 :             :       else
    5158                 :             :         return false;
    5159                 :             : 
    5160                 :          38 :       if (expr->value.function.isym->id == GFC_ISYM_LBOUND)
    5161                 :             :         {
    5162                 :          23 :           if (sym->as->lower[d])
    5163                 :          23 :             new_expr = gfc_copy_expr (sym->as->lower[d]);
    5164                 :             :         }
    5165                 :             :       else
    5166                 :             :         {
    5167                 :          15 :           if (sym->as->upper[d])
    5168                 :           9 :             new_expr = gfc_copy_expr (sym->as->upper[d]);
    5169                 :             :         }
    5170                 :             :       break;
    5171                 :             : 
    5172                 :             :     default:
    5173                 :             :       break;
    5174                 :             :     }
    5175                 :             : 
    5176                 :        1307 :   gfc_apply_interface_mapping_to_expr (mapping, new_expr);
    5177                 :        1307 :   if (!new_expr)
    5178                 :             :     return false;
    5179                 :             : 
    5180                 :         113 :   gfc_replace_expr (expr, new_expr);
    5181                 :         113 :   return true;
    5182                 :             : }
    5183                 :             : 
    5184                 :             : 
    5185                 :             : static void
    5186                 :          24 : gfc_map_fcn_formal_to_actual (gfc_expr *expr, gfc_expr *map_expr,
    5187                 :             :                               gfc_interface_mapping * mapping)
    5188                 :             : {
    5189                 :          24 :   gfc_formal_arglist *f;
    5190                 :          24 :   gfc_actual_arglist *actual;
    5191                 :             : 
    5192                 :          24 :   actual = expr->value.function.actual;
    5193                 :          24 :   f = gfc_sym_get_dummy_args (map_expr->symtree->n.sym);
    5194                 :             : 
    5195                 :          72 :   for (; f && actual; f = f->next, actual = actual->next)
    5196                 :             :     {
    5197                 :          24 :       if (!actual->expr)
    5198                 :           0 :         continue;
    5199                 :             : 
    5200                 :          24 :       gfc_add_interface_mapping (mapping, f->sym, NULL, actual->expr);
    5201                 :             :     }
    5202                 :             : 
    5203                 :          24 :   if (map_expr->symtree->n.sym->attr.dimension)
    5204                 :             :     {
    5205                 :           6 :       int d;
    5206                 :           6 :       gfc_array_spec *as;
    5207                 :             : 
    5208                 :           6 :       as = gfc_copy_array_spec (map_expr->symtree->n.sym->as);
    5209                 :             : 
    5210                 :          18 :       for (d = 0; d < as->rank; d++)
    5211                 :             :         {
    5212                 :           6 :           gfc_apply_interface_mapping_to_expr (mapping, as->lower[d]);
    5213                 :           6 :           gfc_apply_interface_mapping_to_expr (mapping, as->upper[d]);
    5214                 :             :         }
    5215                 :             : 
    5216                 :           6 :       expr->value.function.esym->as = as;
    5217                 :             :     }
    5218                 :             : 
    5219                 :          24 :   if (map_expr->symtree->n.sym->ts.type == BT_CHARACTER)
    5220                 :             :     {
    5221                 :           0 :       expr->value.function.esym->ts.u.cl->length
    5222                 :           0 :         = gfc_copy_expr (map_expr->symtree->n.sym->ts.u.cl->length);
    5223                 :             : 
    5224                 :           0 :       gfc_apply_interface_mapping_to_expr (mapping,
    5225                 :           0 :                         expr->value.function.esym->ts.u.cl->length);
    5226                 :             :     }
    5227                 :          24 : }
    5228                 :             : 
    5229                 :             : 
    5230                 :             : /* EXPR is a copy of an expression that appeared in the interface
    5231                 :             :    associated with MAPPING.  Walk it recursively looking for references to
    5232                 :             :    dummy arguments that MAPPING maps to actual arguments.  Replace each such
    5233                 :             :    reference with a reference to the associated actual argument.  */
    5234                 :             : 
    5235                 :             : static void
    5236                 :       20846 : gfc_apply_interface_mapping_to_expr (gfc_interface_mapping * mapping,
    5237                 :             :                                      gfc_expr * expr)
    5238                 :             : {
    5239                 :       22399 :   gfc_interface_sym_mapping *sym;
    5240                 :       22399 :   gfc_actual_arglist *actual;
    5241                 :             : 
    5242                 :       22399 :   if (!expr)
    5243                 :             :     return;
    5244                 :             : 
    5245                 :             :   /* Copying an expression does not copy its length, so do that here.  */
    5246                 :       12421 :   if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl)
    5247                 :             :     {
    5248                 :        1784 :       expr->ts.u.cl = gfc_get_interface_mapping_charlen (mapping, expr->ts.u.cl);
    5249                 :        1784 :       gfc_apply_interface_mapping_to_expr (mapping, expr->ts.u.cl->length);
    5250                 :             :     }
    5251                 :             : 
    5252                 :             :   /* Apply the mapping to any references.  */
    5253                 :       12421 :   gfc_apply_interface_mapping_to_ref (mapping, expr->ref);
    5254                 :             : 
    5255                 :             :   /* ...and to the expression's symbol, if it has one.  */
    5256                 :             :   /* TODO Find out why the condition on expr->symtree had to be moved into
    5257                 :             :      the loop rather than being outside it, as originally.  */
    5258                 :       29606 :   for (sym = mapping->syms; sym; sym = sym->next)
    5259                 :       17185 :     if (expr->symtree && !strcmp (sym->old->name, expr->symtree->n.sym->name))
    5260                 :             :       {
    5261                 :        3345 :         if (sym->new_sym->n.sym->backend_decl)
    5262                 :        3301 :           expr->symtree = sym->new_sym;
    5263                 :          44 :         else if (sym->expr)
    5264                 :          44 :           gfc_replace_expr (expr, gfc_copy_expr (sym->expr));
    5265                 :             :       }
    5266                 :             : 
    5267                 :             :       /* ...and to subexpressions in expr->value.  */
    5268                 :       12421 :   switch (expr->expr_type)
    5269                 :             :     {
    5270                 :             :     case EXPR_VARIABLE:
    5271                 :             :     case EXPR_CONSTANT:
    5272                 :             :     case EXPR_NULL:
    5273                 :             :     case EXPR_SUBSTRING:
    5274                 :             :       break;
    5275                 :             : 
    5276                 :        1553 :     case EXPR_OP:
    5277                 :        1553 :       gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op1);
    5278                 :        1553 :       gfc_apply_interface_mapping_to_expr (mapping, expr->value.op.op2);
    5279                 :        1553 :       break;
    5280                 :             : 
    5281                 :        2927 :     case EXPR_FUNCTION:
    5282                 :        9388 :       for (actual = expr->value.function.actual; actual; actual = actual->next)
    5283                 :        6461 :         gfc_apply_interface_mapping_to_expr (mapping, actual->expr);
    5284                 :             : 
    5285                 :        2927 :       if (expr->value.function.esym == NULL
    5286                 :        2614 :             && expr->value.function.isym != NULL
    5287                 :        2602 :             && expr->value.function.actual
    5288                 :        2601 :             && expr->value.function.actual->expr
    5289                 :        2601 :             && expr->value.function.actual->expr->symtree
    5290                 :        5111 :             && gfc_map_intrinsic_function (expr, mapping))
    5291                 :             :         break;
    5292                 :             : 
    5293                 :        6094 :       for (sym = mapping->syms; sym; sym = sym->next)
    5294                 :        3508 :         if (sym->old == expr->value.function.esym)
    5295                 :             :           {
    5296                 :          24 :             expr->value.function.esym = sym->new_sym->n.sym;
    5297                 :          24 :             gfc_map_fcn_formal_to_actual (expr, sym->expr, mapping);
    5298                 :          24 :             expr->value.function.esym->result = sym->new_sym->n.sym;
    5299                 :             :           }
    5300                 :             :       break;
    5301                 :             : 
    5302                 :          47 :     case EXPR_ARRAY:
    5303                 :          47 :     case EXPR_STRUCTURE:
    5304                 :          47 :       gfc_apply_interface_mapping_to_cons (mapping, expr->value.constructor);
    5305                 :          47 :       break;
    5306                 :             : 
    5307                 :           0 :     case EXPR_COMPCALL:
    5308                 :           0 :     case EXPR_PPC:
    5309                 :           0 :     case EXPR_UNKNOWN:
    5310                 :           0 :       gcc_unreachable ();
    5311                 :             :       break;
    5312                 :             :     }
    5313                 :             : 
    5314                 :             :   return;
    5315                 :             : }
    5316                 :             : 
    5317                 :             : 
    5318                 :             : /* Evaluate interface expression EXPR using MAPPING.  Store the result
    5319                 :             :    in SE.  */
    5320                 :             : 
    5321                 :             : void
    5322                 :        3906 : gfc_apply_interface_mapping (gfc_interface_mapping * mapping,
    5323                 :             :                              gfc_se * se, gfc_expr * expr)
    5324                 :             : {
    5325                 :        3906 :   expr = gfc_copy_expr (expr);
    5326                 :        3906 :   gfc_apply_interface_mapping_to_expr (mapping, expr);
    5327                 :        3906 :   gfc_conv_expr (se, expr);
    5328                 :        3906 :   se->expr = gfc_evaluate_now (se->expr, &se->pre);
    5329                 :        3906 :   gfc_free_expr (expr);
    5330                 :        3906 : }
    5331                 :             : 
    5332                 :             : 
    5333                 :             : /* Returns a reference to a temporary array into which a component of
    5334                 :             :    an actual argument derived type array is copied and then returned
    5335                 :             :    after the function call.  */
    5336                 :             : void
    5337                 :        2372 : gfc_conv_subref_array_arg (gfc_se *se, gfc_expr * expr, int g77,
    5338                 :             :                            sym_intent intent, bool formal_ptr,
    5339                 :             :                            const gfc_symbol *fsym, const char *proc_name,
    5340                 :             :                            gfc_symbol *sym, bool check_contiguous)
    5341                 :             : {
    5342                 :        2372 :   gfc_se lse;
    5343                 :        2372 :   gfc_se rse;
    5344                 :        2372 :   gfc_ss *lss;
    5345                 :        2372 :   gfc_ss *rss;
    5346                 :        2372 :   gfc_loopinfo loop;
    5347                 :        2372 :   gfc_loopinfo loop2;
    5348                 :        2372 :   gfc_array_info *info;
    5349                 :        2372 :   tree offset;
    5350                 :        2372 :   tree tmp_index;
    5351                 :        2372 :   tree tmp;
    5352                 :        2372 :   tree base_type;
    5353                 :        2372 :   tree size;
    5354                 :        2372 :   stmtblock_t body;
    5355                 :        2372 :   int n;
    5356                 :        2372 :   int dimen;
    5357                 :        2372 :   gfc_se work_se;
    5358                 :        2372 :   gfc_se *parmse;
    5359                 :        2372 :   bool pass_optional;
    5360                 :        2372 :   bool readonly;
    5361                 :             : 
    5362                 :        2372 :   pass_optional = fsym && fsym->attr.optional && sym && sym->attr.optional;
    5363                 :             : 
    5364                 :        2361 :   if (pass_optional || check_contiguous)
    5365                 :             :     {
    5366                 :        1349 :       gfc_init_se (&work_se, NULL);
    5367                 :        1349 :       parmse = &work_se;
    5368                 :             :     }
    5369                 :             :   else
    5370                 :             :     parmse = se;
    5371                 :             : 
    5372                 :        2372 :   if (gfc_option.rtcheck & GFC_RTCHECK_ARRAY_TEMPS)
    5373                 :             :     {
    5374                 :             :       /* We will create a temporary array, so let us warn.  */
    5375                 :         868 :       char * msg;
    5376                 :             : 
    5377                 :         868 :       if (fsym && proc_name)
    5378                 :         868 :         msg = xasprintf ("An array temporary was created for argument "
    5379                 :         868 :                          "'%s' of procedure '%s'", fsym->name, proc_name);
    5380                 :             :       else
    5381                 :           0 :         msg = xasprintf ("An array temporary was created");
    5382                 :             : 
    5383                 :         868 :       tmp = build_int_cst (logical_type_node, 1);
    5384                 :         868 :       gfc_trans_runtime_check (false, true, tmp, &parmse->pre,
    5385                 :             :                                &expr->where, msg);
    5386                 :         868 :       free (msg);
    5387                 :             :     }
    5388                 :             : 
    5389                 :        2372 :   gfc_init_se (&lse, NULL);
    5390                 :        2372 :   gfc_init_se (&rse, NULL);
    5391                 :             : 
    5392                 :             :   /* Walk the argument expression.  */
    5393                 :        2372 :   rss = gfc_walk_expr (expr);
    5394                 :             : 
    5395                 :        2372 :   gcc_assert (rss != gfc_ss_terminator);
    5396                 :             : 
    5397                 :             :   /* Initialize the scalarizer.  */
    5398                 :        2372 :   gfc_init_loopinfo (&loop);
    5399                 :        2372 :   gfc_add_ss_to_loop (&loop, rss);
    5400                 :             : 
    5401                 :             :   /* Calculate the bounds of the scalarization.  */
    5402                 :        2372 :   gfc_conv_ss_startstride (&loop);
    5403                 :             : 
    5404                 :             :   /* Build an ss for the temporary.  */
    5405                 :        2372 :   if (expr->ts.type == BT_CHARACTER && !expr->ts.u.cl->backend_decl)
    5406                 :         132 :     gfc_conv_string_length (expr->ts.u.cl, expr, &parmse->pre);
    5407                 :             : 
    5408                 :        2372 :   base_type = gfc_typenode_for_spec (&expr->ts);
    5409                 :        2372 :   if (GFC_ARRAY_TYPE_P (base_type)
    5410                 :        2372 :                 || GFC_DESCRIPTOR_TYPE_P (base_type))
    5411                 :           0 :     base_type = gfc_get_element_type (base_type);
    5412                 :             : 
    5413                 :        2372 :   if (expr->ts.type == BT_CLASS)
    5414                 :         121 :     base_type = gfc_typenode_for_spec (&CLASS_DATA (expr)->ts);
    5415                 :             : 
    5416                 :        3526 :   loop.temp_ss = gfc_get_temp_ss (base_type, ((expr->ts.type == BT_CHARACTER)
    5417                 :        1154 :                                               ? expr->ts.u.cl->backend_decl
    5418                 :             :                                               : NULL),
    5419                 :             :                                   loop.dimen);
    5420                 :             : 
    5421                 :        2372 :   parmse->string_length = loop.temp_ss->info->string_length;
    5422                 :             : 
    5423                 :             :   /* Associate the SS with the loop.  */
    5424                 :        2372 :   gfc_add_ss_to_loop (&loop, loop.temp_ss);
    5425                 :             : 
    5426                 :             :   /* Setup the scalarizing loops.  */
    5427                 :        2372 :   gfc_conv_loop_setup (&loop, &expr->where);
    5428                 :             : 
    5429                 :             :   /* Pass the temporary descriptor back to the caller.  */
    5430                 :        2372 :   info = &loop.temp_ss->info->data.array;
    5431                 :        2372 :   parmse->expr = info->descriptor;
    5432                 :             : 
    5433                 :             :   /* Setup the gfc_se structures.  */
    5434                 :        2372 :   gfc_copy_loopinfo_to_se (&lse, &loop);
    5435                 :        2372 :   gfc_copy_loopinfo_to_se (&rse, &loop);
    5436                 :             : 
    5437                 :        2372 :   rse.ss = rss;
    5438                 :        2372 :   lse.ss = loop.temp_ss;
    5439                 :        2372 :   gfc_mark_ss_chain_used (rss, 1);
    5440                 :        2372 :   gfc_mark_ss_chain_used (loop.temp_ss, 1);
    5441                 :             : 
    5442                 :             :   /* Start the scalarized loop body.  */
    5443                 :        2372 :   gfc_start_scalarized_body (&loop, &body);
    5444                 :             : 
    5445                 :             :   /* Translate the expression.  */
    5446                 :        2372 :   gfc_conv_expr (&rse, expr);
    5447                 :             : 
    5448                 :             :   /* Reset the offset for the function call since the loop
    5449                 :             :      is zero based on the data pointer.  Note that the temp
    5450                 :             :      comes first in the loop chain since it is added second.  */
    5451                 :        2372 :   if (gfc_is_class_array_function (expr))
    5452                 :             :     {
    5453                 :          13 :       tmp = loop.ss->loop_chain->info->data.array.descriptor;
    5454                 :          13 :       gfc_conv_descriptor_offset_set (&loop.pre, tmp,
    5455                 :             :                                       gfc_index_zero_node);
    5456                 :             :     }
    5457                 :             : 
    5458                 :        2372 :   gfc_conv_tmp_array_ref (&lse);
    5459                 :             : 
    5460                 :        2372 :   if (intent != INTENT_OUT)
    5461                 :             :     {
    5462                 :        2334 :       tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, false);
    5463                 :        2334 :       gfc_add_expr_to_block (&body, tmp);
    5464                 :        2334 :       gcc_assert (rse.ss == gfc_ss_terminator);
    5465                 :        2334 :       gfc_trans_scalarizing_loops (&loop, &body);
    5466                 :             :     }
    5467                 :             :   else
    5468                 :             :     {
    5469                 :             :       /* Make sure that the temporary declaration survives by merging
    5470                 :             :        all the loop declarations into the current context.  */
    5471                 :          85 :       for (n = 0; n < loop.dimen; n++)
    5472                 :             :         {
    5473                 :          47 :           gfc_merge_block_scope (&body);
    5474                 :          47 :           body = loop.code[loop.order[n]];
    5475                 :             :         }
    5476                 :          38 :       gfc_merge_block_scope (&body);
    5477                 :             :     }
    5478                 :             : 
    5479                 :             :   /* Add the post block after the second loop, so that any
    5480                 :             :      freeing of allocated memory is done at the right time.  */
    5481                 :        2372 :   gfc_add_block_to_block (&parmse->pre, &loop.pre);
    5482                 :             : 
    5483                 :             :   /**********Copy the temporary back again.*********/
    5484                 :             : 
    5485                 :        2372 :   gfc_init_se (&lse, NULL);
    5486                 :        2372 :   gfc_init_se (&rse, NULL);
    5487                 :             : 
    5488                 :             :   /* Walk the argument expression.  */
    5489                 :        2372 :   lss = gfc_walk_expr (expr);
    5490                 :        2372 :   rse.ss = loop.temp_ss;
    5491                 :        2372 :   lse.ss = lss;
    5492                 :             : 
    5493                 :             :   /* Initialize the scalarizer.  */
    5494                 :        2372 :   gfc_init_loopinfo (&loop2);
    5495                 :        2372 :   gfc_add_ss_to_loop (&loop2, lss);
    5496                 :             : 
    5497                 :        2372 :   dimen = rse.ss->dimen;
    5498                 :             : 
    5499                 :             :   /* Skip the write-out loop for this case.  */
    5500                 :        2372 :   if (gfc_is_class_array_function (expr))
    5501                 :          13 :     goto class_array_fcn;
    5502                 :             : 
    5503                 :             :   /* Calculate the bounds of the scalarization.  */
    5504                 :        2359 :   gfc_conv_ss_startstride (&loop2);
    5505                 :             : 
    5506                 :             :   /* Setup the scalarizing loops.  */
    5507                 :        2359 :   gfc_conv_loop_setup (&loop2, &expr->where);
    5508                 :             : 
    5509                 :        2359 :   gfc_copy_loopinfo_to_se (&lse, &loop2);
    5510                 :        2359 :   gfc_copy_loopinfo_to_se (&rse, &loop2);
    5511                 :             : 
    5512                 :        2359 :   gfc_mark_ss_chain_used (lss, 1);
    5513                 :        2359 :   gfc_mark_ss_chain_used (loop.temp_ss, 1);
    5514                 :             : 
    5515                 :             :   /* Declare the variable to hold the temporary offset and start the
    5516                 :             :      scalarized loop body.  */
    5517                 :        2359 :   offset = gfc_create_var (gfc_array_index_type, NULL);
    5518                 :        2359 :   gfc_start_scalarized_body (&loop2, &body);
    5519                 :             : 
    5520                 :             :   /* Build the offsets for the temporary from the loop variables.  The
    5521                 :             :      temporary array has lbounds of zero and strides of one in all
    5522                 :             :      dimensions, so this is very simple.  The offset is only computed
    5523                 :             :      outside the innermost loop, so the overall transfer could be
    5524                 :             :      optimized further.  */
    5525                 :        2359 :   info = &rse.ss->info->data.array;
    5526                 :             : 
    5527                 :        2359 :   tmp_index = gfc_index_zero_node;
    5528                 :        3703 :   for (n = dimen - 1; n > 0; n--)
    5529                 :             :     {
    5530                 :        1344 :       tree tmp_str;
    5531                 :        1344 :       tmp = rse.loop->loopvar[n];
    5532                 :        1344 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    5533                 :             :                              tmp, rse.loop->from[n]);
    5534                 :        1344 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    5535                 :             :                              tmp, tmp_index);
    5536                 :             : 
    5537                 :        2688 :       tmp_str = fold_build2_loc (input_location, MINUS_EXPR,
    5538                 :             :                                  gfc_array_index_type,
    5539                 :        1344 :                                  rse.loop->to[n-1], rse.loop->from[n-1]);
    5540                 :        1344 :       tmp_str = fold_build2_loc (input_location, PLUS_EXPR,
    5541                 :             :                                  gfc_array_index_type,
    5542                 :             :                                  tmp_str, gfc_index_one_node);
    5543                 :             : 
    5544                 :        1344 :       tmp_index = fold_build2_loc (input_location, MULT_EXPR,
    5545                 :             :                                    gfc_array_index_type, tmp, tmp_str);
    5546                 :             :     }
    5547                 :             : 
    5548                 :        4718 :   tmp_index = fold_build2_loc (input_location, MINUS_EXPR,
    5549                 :             :                                gfc_array_index_type,
    5550                 :        2359 :                                tmp_index, rse.loop->from[0]);
    5551                 :        2359 :   gfc_add_modify (&rse.loop->code[0], offset, tmp_index);
    5552                 :             : 
    5553                 :        4718 :   tmp_index = fold_build2_loc (input_location, PLUS_EXPR,
    5554                 :             :                                gfc_array_index_type,
    5555                 :        2359 :                                rse.loop->loopvar[0], offset);
    5556                 :             : 
    5557                 :             :   /* Now use the offset for the reference.  */
    5558                 :        2359 :   tmp = build_fold_indirect_ref_loc (input_location,
    5559                 :             :                                  info->data);
    5560                 :        2359 :   rse.expr = gfc_build_array_ref (tmp, tmp_index, NULL);
    5561                 :             : 
    5562                 :        2359 :   if (expr->ts.type == BT_CHARACTER)
    5563                 :        1154 :     rse.string_length = expr->ts.u.cl->backend_decl;
    5564                 :             : 
    5565                 :        2359 :   gfc_conv_expr (&lse, expr);
    5566                 :             : 
    5567                 :        2359 :   gcc_assert (lse.ss == gfc_ss_terminator);
    5568                 :             : 
    5569                 :        2359 :   tmp = gfc_trans_scalar_assign (&lse, &rse, expr->ts, false, true);
    5570                 :        2359 :   gfc_add_expr_to_block (&body, tmp);
    5571                 :             : 
    5572                 :             :   /* Generate the copying loops.  */
    5573                 :        2359 :   gfc_trans_scalarizing_loops (&loop2, &body);
    5574                 :             : 
    5575                 :             :   /* Wrap the whole thing up by adding the second loop to the post-block
    5576                 :             :      and following it by the post-block of the first loop.  In this way,
    5577                 :             :      if the temporary needs freeing, it is done after use!
    5578                 :             :      If input expr is read-only, e.g. a PARAMETER array, copying back
    5579                 :             :      modified values is undefined behavior.  */
    5580                 :        4718 :   readonly = (expr->expr_type == EXPR_VARIABLE
    5581                 :        2305 :               && expr->symtree
    5582                 :        4664 :               && expr->symtree->n.sym->attr.flavor == FL_PARAMETER);
    5583                 :             : 
    5584                 :        2359 :   if ((intent != INTENT_IN) && !readonly)
    5585                 :             :     {
    5586                 :        1138 :       gfc_add_block_to_block (&parmse->post, &loop2.pre);
    5587                 :        1138 :       gfc_add_block_to_block (&parmse->post, &loop2.post);
    5588                 :             :     }
    5589                 :             : 
    5590                 :        1221 : class_array_fcn:
    5591                 :             : 
    5592                 :        2372 :   gfc_add_block_to_block (&parmse->post, &loop.post);
    5593                 :             : 
    5594                 :        2372 :   gfc_cleanup_loop (&loop);
    5595                 :        2372 :   gfc_cleanup_loop (&loop2);
    5596                 :             : 
    5597                 :             :   /* Pass the string length to the argument expression.  */
    5598                 :        2372 :   if (expr->ts.type == BT_CHARACTER)
    5599                 :        1154 :     parmse->string_length = expr->ts.u.cl->backend_decl;
    5600                 :             : 
    5601                 :             :   /* Determine the offset for pointer formal arguments and set the
    5602                 :             :      lbounds to one.  */
    5603                 :        2372 :   if (formal_ptr)
    5604                 :             :     {
    5605                 :           0 :       size = gfc_index_one_node;
    5606                 :           0 :       offset = gfc_index_zero_node;
    5607                 :           0 :       for (n = 0; n < dimen; n++)
    5608                 :             :         {
    5609                 :           0 :           tmp = gfc_conv_descriptor_ubound_get (parmse->expr,
    5610                 :             :                                                 gfc_rank_cst[n]);
    5611                 :           0 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    5612                 :             :                                  gfc_array_index_type, tmp,
    5613                 :             :                                  gfc_index_one_node);
    5614                 :           0 :           gfc_conv_descriptor_ubound_set (&parmse->pre,
    5615                 :             :                                           parmse->expr,
    5616                 :             :                                           gfc_rank_cst[n],
    5617                 :             :                                           tmp);
    5618                 :           0 :           gfc_conv_descriptor_lbound_set (&parmse->pre,
    5619                 :             :                                           parmse->expr,
    5620                 :             :                                           gfc_rank_cst[n],
    5621                 :             :                                           gfc_index_one_node);
    5622                 :           0 :           size = gfc_evaluate_now (size, &parmse->pre);
    5623                 :           0 :           offset = fold_build2_loc (input_location, MINUS_EXPR,
    5624                 :             :                                     gfc_array_index_type,
    5625                 :             :                                     offset, size);
    5626                 :           0 :           offset = gfc_evaluate_now (offset, &parmse->pre);
    5627                 :           0 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    5628                 :             :                                  gfc_array_index_type,
    5629                 :           0 :                                  rse.loop->to[n], rse.loop->from[n]);
    5630                 :           0 :           tmp = fold_build2_loc (input_location, PLUS_EXPR,
    5631                 :             :                                  gfc_array_index_type,
    5632                 :             :                                  tmp, gfc_index_one_node);
    5633                 :           0 :           size = fold_build2_loc (input_location, MULT_EXPR,
    5634                 :             :                                   gfc_array_index_type, size, tmp);
    5635                 :             :         }
    5636                 :             : 
    5637                 :           0 :       gfc_conv_descriptor_offset_set (&parmse->pre, parmse->expr,
    5638                 :             :                                       offset);
    5639                 :             :     }
    5640                 :             : 
    5641                 :             :   /* We want either the address for the data or the address of the descriptor,
    5642                 :             :      depending on the mode of passing array arguments.  */
    5643                 :        2372 :   if (g77)
    5644                 :         433 :     parmse->expr = gfc_conv_descriptor_data_get (parmse->expr);
    5645                 :             :   else
    5646                 :        1939 :     parmse->expr = gfc_build_addr_expr (NULL_TREE, parmse->expr);
    5647                 :             : 
    5648                 :             :   /* Basically make this into
    5649                 :             : 
    5650                 :             :      if (present)
    5651                 :             :        {
    5652                 :             :          if (contiguous)
    5653                 :             :            {
    5654                 :             :              pointer = a;
    5655                 :             :            }
    5656                 :             :          else
    5657                 :             :            {
    5658                 :             :              parmse->pre();
    5659                 :             :              pointer = parmse->expr;
    5660                 :             :            }
    5661                 :             :        }
    5662                 :             :      else
    5663                 :             :        pointer = NULL;
    5664                 :             : 
    5665                 :             :      foo (pointer);
    5666                 :             :      if (present && !contiguous)
    5667                 :             :            se->post();
    5668                 :             : 
    5669                 :             :      */
    5670                 :             : 
    5671                 :        2372 :   if (pass_optional || check_contiguous)
    5672                 :             :     {
    5673                 :        1349 :       tree type;
    5674                 :        1349 :       stmtblock_t else_block;
    5675                 :        1349 :       tree pre_stmts, post_stmts;
    5676                 :        1349 :       tree pointer;
    5677                 :        1349 :       tree else_stmt;
    5678                 :        1349 :       tree present_var = NULL_TREE;
    5679                 :        1349 :       tree cont_var = NULL_TREE;
    5680                 :        1349 :       tree post_cond;
    5681                 :             : 
    5682                 :        1349 :       type = TREE_TYPE (parmse->expr);
    5683                 :        1349 :       if (POINTER_TYPE_P (type) && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
    5684                 :        1021 :         type = TREE_TYPE (type);
    5685                 :        1349 :       pointer = gfc_create_var (type, "arg_ptr");
    5686                 :             : 
    5687                 :        1349 :       if (check_contiguous)
    5688                 :             :         {
    5689                 :        1349 :           gfc_se cont_se, array_se;
    5690                 :        1349 :           stmtblock_t if_block, else_block;
    5691                 :        1349 :           tree if_stmt, else_stmt;
    5692                 :        1349 :           mpz_t size;
    5693                 :        1349 :           bool size_set;
    5694                 :             : 
    5695                 :        1349 :           cont_var = gfc_create_var (boolean_type_node, "contiguous");
    5696                 :             : 
    5697                 :             :           /* If the size is known to be one at compile-time, set
    5698                 :             :              cont_var to true unconditionally.  This may look
    5699                 :             :              inelegant, but we're only doing this during
    5700                 :             :              optimization, so the statements will be optimized away,
    5701                 :             :              and this saves complexity here.  */
    5702                 :             : 
    5703                 :        1349 :           size_set = gfc_array_size (expr, &size);
    5704                 :        1349 :           if (size_set && mpz_cmp_ui (size, 1) == 0)
    5705                 :             :             {
    5706                 :           6 :               gfc_add_modify (&se->pre, cont_var,
    5707                 :             :                               build_one_cst (boolean_type_node));
    5708                 :             :             }
    5709                 :             :           else
    5710                 :             :             {
    5711                 :             :               /* cont_var = is_contiguous (expr); .  */
    5712                 :        1343 :               gfc_init_se (&cont_se, parmse);
    5713                 :        1343 :               gfc_conv_is_contiguous_expr (&cont_se, expr);
    5714                 :        1343 :               gfc_add_block_to_block (&se->pre, &(&cont_se)->pre);
    5715                 :        1343 :               gfc_add_modify (&se->pre, cont_var, cont_se.expr);
    5716                 :        1343 :               gfc_add_block_to_block (&se->pre, &(&cont_se)->post);
    5717                 :             :             }
    5718                 :             : 
    5719                 :        1349 :           if (size_set)
    5720                 :        1145 :             mpz_clear (size);
    5721                 :             : 
    5722                 :             :           /* arrayse->expr = descriptor of a.  */
    5723                 :        1349 :           gfc_init_se (&array_se, se);
    5724                 :        1349 :           gfc_conv_expr_descriptor (&array_se, expr);
    5725                 :        1349 :           gfc_add_block_to_block (&se->pre, &(&array_se)->pre);
    5726                 :        1349 :           gfc_add_block_to_block (&se->pre, &(&array_se)->post);
    5727                 :             : 
    5728                 :             :           /* if_stmt = { descriptor ? pointer = a : pointer = &a[0]; } .  */
    5729                 :        1349 :           gfc_init_block (&if_block);
    5730                 :        1349 :           if (GFC_DESCRIPTOR_TYPE_P (type))
    5731                 :        1021 :             gfc_add_modify (&if_block, pointer, array_se.expr);
    5732                 :             :           else
    5733                 :             :             {
    5734                 :         328 :               tmp = gfc_conv_array_data (array_se.expr);
    5735                 :         328 :               tmp = fold_convert (type, tmp);
    5736                 :         328 :               gfc_add_modify (&if_block, pointer, tmp);
    5737                 :             :             }
    5738                 :        1349 :           if_stmt = gfc_finish_block (&if_block);
    5739                 :             : 
    5740                 :             :           /* else_stmt = { parmse->pre(); pointer = parmse->expr; } .  */
    5741                 :        1349 :           gfc_init_block (&else_block);
    5742                 :        1349 :           gfc_add_block_to_block (&else_block, &parmse->pre);
    5743                 :        1349 :           tmp = (GFC_DESCRIPTOR_TYPE_P (type)
    5744                 :        1349 :                  ? build_fold_indirect_ref_loc (input_location, parmse->expr)
    5745                 :             :                  : parmse->expr);
    5746                 :        1349 :           gfc_add_modify (&else_block, pointer, tmp);
    5747                 :        1349 :           else_stmt = gfc_finish_block (&else_block);
    5748                 :             : 
    5749                 :             :           /* And put the above into an if statement.  */
    5750                 :        1349 :           pre_stmts = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    5751                 :             :                                        gfc_likely (cont_var,
    5752                 :             :                                                    PRED_FORTRAN_CONTIGUOUS),
    5753                 :             :                                        if_stmt, else_stmt);
    5754                 :             :         }
    5755                 :             :       else
    5756                 :             :         {
    5757                 :             :           /* pointer = pramse->expr;  .  */
    5758                 :           0 :           gfc_add_modify (&parmse->pre, pointer, parmse->expr);
    5759                 :           0 :           pre_stmts = gfc_finish_block (&parmse->pre);
    5760                 :             :         }
    5761                 :             : 
    5762                 :        1349 :       if (pass_optional)
    5763                 :             :         {
    5764                 :          11 :           present_var = gfc_create_var (boolean_type_node, "present");
    5765                 :             : 
    5766                 :             :           /* present_var = present(sym); .  */
    5767                 :          11 :           tmp = gfc_conv_expr_present (sym);
    5768                 :          11 :           tmp = fold_convert (boolean_type_node, tmp);
    5769                 :          11 :           gfc_add_modify (&se->pre, present_var, tmp);
    5770                 :             : 
    5771                 :             :           /* else_stmt = { pointer = NULL; } .  */
    5772                 :          11 :           gfc_init_block (&else_block);
    5773                 :          11 :           if (GFC_DESCRIPTOR_TYPE_P (type))
    5774                 :           0 :             gfc_conv_descriptor_data_set (&else_block, pointer,
    5775                 :             :                                           null_pointer_node);
    5776                 :             :           else
    5777                 :          11 :             gfc_add_modify (&else_block, pointer, build_int_cst (type, 0));
    5778                 :          11 :           else_stmt = gfc_finish_block (&else_block);
    5779                 :             : 
    5780                 :          11 :           tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    5781                 :             :                                  gfc_likely (present_var,
    5782                 :             :                                              PRED_FORTRAN_ABSENT_DUMMY),
    5783                 :             :                                  pre_stmts, else_stmt);
    5784                 :          11 :           gfc_add_expr_to_block (&se->pre, tmp);
    5785                 :             :         }
    5786                 :             :       else
    5787                 :        1338 :         gfc_add_expr_to_block (&se->pre, pre_stmts);
    5788                 :             : 
    5789                 :        1349 :       post_stmts = gfc_finish_block (&parmse->post);
    5790                 :             : 
    5791                 :             :       /* Put together the post stuff, plus the optional
    5792                 :             :          deallocation.  */
    5793                 :        1349 :       if (check_contiguous)
    5794                 :             :         {
    5795                 :             :           /* !cont_var.  */
    5796                 :        1349 :           tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    5797                 :             :                                  cont_var,
    5798                 :             :                                  build_zero_cst (boolean_type_node));
    5799                 :        1349 :           tmp = gfc_unlikely (tmp, PRED_FORTRAN_CONTIGUOUS);
    5800                 :             : 
    5801                 :        1349 :           if (pass_optional)
    5802                 :             :             {
    5803                 :          11 :               tree present_likely = gfc_likely (present_var,
    5804                 :             :                                                 PRED_FORTRAN_ABSENT_DUMMY);
    5805                 :          11 :               post_cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    5806                 :             :                                            boolean_type_node, present_likely,
    5807                 :             :                                            tmp);
    5808                 :             :             }
    5809                 :             :           else
    5810                 :             :             post_cond = tmp;
    5811                 :             :         }
    5812                 :             :       else
    5813                 :             :         {
    5814                 :           0 :           gcc_assert (pass_optional);
    5815                 :             :           post_cond = present_var;
    5816                 :             :         }
    5817                 :             : 
    5818                 :        1349 :       tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, post_cond,
    5819                 :             :                              post_stmts, build_empty_stmt (input_location));
    5820                 :        1349 :       gfc_add_expr_to_block (&se->post, tmp);
    5821                 :        1349 :       if (GFC_DESCRIPTOR_TYPE_P (type))
    5822                 :             :         {
    5823                 :        1021 :           type = TREE_TYPE (parmse->expr);
    5824                 :        1021 :           if (POINTER_TYPE_P (type))
    5825                 :             :             {
    5826                 :        1021 :               pointer = gfc_build_addr_expr (type, pointer);
    5827                 :        1021 :               if (pass_optional)
    5828                 :             :                 {
    5829                 :           0 :                   tmp = gfc_likely (present_var, PRED_FORTRAN_ABSENT_DUMMY);
    5830                 :           0 :                   pointer = fold_build3_loc (input_location, COND_EXPR, type,
    5831                 :             :                                              tmp, pointer,
    5832                 :             :                                              fold_convert (type,
    5833                 :             :                                                            null_pointer_node));
    5834                 :             :                 }
    5835                 :             :             }
    5836                 :             :           else
    5837                 :           0 :             gcc_assert (!pass_optional);
    5838                 :             :         }
    5839                 :        1349 :       se->expr = pointer;
    5840                 :             :     }
    5841                 :             : 
    5842                 :        2372 :   return;
    5843                 :             : }
    5844                 :             : 
    5845                 :             : 
    5846                 :             : /* Generate the code for argument list functions.  */
    5847                 :             : 
    5848                 :             : static void
    5849                 :        4550 : conv_arglist_function (gfc_se *se, gfc_expr *expr, const char *name)
    5850                 :             : {
    5851                 :             :   /* Pass by value for g77 %VAL(arg), pass the address
    5852                 :             :      indirectly for %LOC, else by reference.  Thus %REF
    5853                 :             :      is a "do-nothing" and %LOC is the same as an F95
    5854                 :             :      pointer.  */
    5855                 :        4550 :   if (strcmp (name, "%VAL") == 0)
    5856                 :        4538 :     gfc_conv_expr (se, expr);
    5857                 :          12 :   else if (strcmp (name, "%LOC") == 0)
    5858                 :             :     {
    5859                 :           6 :       gfc_conv_expr_reference (se, expr);
    5860                 :           6 :       se->expr = gfc_build_addr_expr (NULL, se->expr);
    5861                 :             :     }
    5862                 :           6 :   else if (strcmp (name, "%REF") == 0)
    5863                 :           6 :     gfc_conv_expr_reference (se, expr);
    5864                 :             :   else
    5865                 :           0 :     gfc_error ("Unknown argument list function at %L", &expr->where);
    5866                 :        4550 : }
    5867                 :             : 
    5868                 :             : 
    5869                 :             : /* This function tells whether the middle-end representation of the expression
    5870                 :             :    E given as input may point to data otherwise accessible through a variable
    5871                 :             :    (sub-)reference.
    5872                 :             :    It is assumed that the only expressions that may alias are variables,
    5873                 :             :    and array constructors if ARRAY_MAY_ALIAS is true and some of its elements
    5874                 :             :    may alias.
    5875                 :             :    This function is used to decide whether freeing an expression's allocatable
    5876                 :             :    components is safe or should be avoided.
    5877                 :             : 
    5878                 :             :    If ARRAY_MAY_ALIAS is true, an array constructor may alias if some of
    5879                 :             :    its elements are copied from a variable.  This ARRAY_MAY_ALIAS trick
    5880                 :             :    is necessary because for array constructors, aliasing depends on how
    5881                 :             :    the array is used:
    5882                 :             :     - If E is an array constructor used as argument to an elemental procedure,
    5883                 :             :       the array, which is generated through shallow copy by the scalarizer,
    5884                 :             :       is used directly and can alias the expressions it was copied from.
    5885                 :             :     - If E is an array constructor used as argument to a non-elemental
    5886                 :             :       procedure,the scalarizer is used in gfc_conv_expr_descriptor to generate
    5887                 :             :       the array as in the previous case, but then that array is used
    5888                 :             :       to initialize a new descriptor through deep copy.  There is no alias
    5889                 :             :       possible in that case.
    5890                 :             :    Thus, the ARRAY_MAY_ALIAS flag is necessary to distinguish the two cases
    5891                 :             :    above.  */
    5892                 :             : 
    5893                 :             : static bool
    5894                 :        7326 : expr_may_alias_variables (gfc_expr *e, bool array_may_alias)
    5895                 :             : {
    5896                 :        7326 :   gfc_constructor *c;
    5897                 :             : 
    5898                 :        7326 :   if (e->expr_type == EXPR_VARIABLE)
    5899                 :             :     return true;
    5900                 :         398 :   else if (e->expr_type == EXPR_FUNCTION)
    5901                 :             :     {
    5902                 :         160 :       gfc_symbol *proc_ifc = gfc_get_proc_ifc_for_expr (e);
    5903                 :             : 
    5904                 :         160 :       if (proc_ifc->result != NULL
    5905                 :         160 :           && ((proc_ifc->result->ts.type == BT_CLASS
    5906                 :          25 :                && proc_ifc->result->ts.u.derived->attr.is_class
    5907                 :          25 :                && CLASS_DATA (proc_ifc->result)->attr.class_pointer)
    5908                 :         160 :               || proc_ifc->result->attr.pointer))
    5909                 :             :         return true;
    5910                 :             :       else
    5911                 :             :         return false;
    5912                 :             :     }
    5913                 :         238 :   else if (e->expr_type != EXPR_ARRAY || !array_may_alias)
    5914                 :             :     return false;
    5915                 :             : 
    5916                 :          67 :   for (c = gfc_constructor_first (e->value.constructor);
    5917                 :         101 :        c; c = gfc_constructor_next (c))
    5918                 :          69 :     if (c->expr
    5919                 :          69 :         && expr_may_alias_variables (c->expr, array_may_alias))
    5920                 :             :       return true;
    5921                 :             : 
    5922                 :             :   return false;
    5923                 :             : }
    5924                 :             : 
    5925                 :             : 
    5926                 :             : /* A helper function to set the dtype for unallocated or unassociated
    5927                 :             :    entities.  */
    5928                 :             : 
    5929                 :             : static void
    5930                 :         891 : set_dtype_for_unallocated (gfc_se *parmse, gfc_expr *e)
    5931                 :             : {
    5932                 :         891 :   tree tmp;
    5933                 :         891 :   tree desc;
    5934                 :         891 :   tree cond;
    5935                 :         891 :   tree type;
    5936                 :         891 :   stmtblock_t block;
    5937                 :             : 
    5938                 :             :   /* TODO Figure out how to handle optional dummies.  */
    5939                 :         891 :   if (e && e->expr_type == EXPR_VARIABLE
    5940                 :         807 :       && e->symtree->n.sym->attr.optional)
    5941                 :         108 :     return;
    5942                 :             : 
    5943                 :         819 :   desc = parmse->expr;
    5944                 :         819 :   if (desc == NULL_TREE)
    5945                 :             :     return;
    5946                 :             : 
    5947                 :         819 :   if (POINTER_TYPE_P (TREE_TYPE (desc)))
    5948                 :         819 :     desc = build_fold_indirect_ref_loc (input_location, desc);
    5949                 :         819 :   if (GFC_CLASS_TYPE_P (TREE_TYPE (desc)))
    5950                 :         192 :     desc = gfc_class_data_get (desc);
    5951                 :         819 :   if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
    5952                 :             :     return;
    5953                 :             : 
    5954                 :         783 :   gfc_init_block (&block);
    5955                 :         783 :   tmp = gfc_conv_descriptor_data_get (desc);
    5956                 :         783 :   cond = fold_build2_loc (input_location, EQ_EXPR,
    5957                 :             :                           logical_type_node, tmp,
    5958                 :         783 :                           build_int_cst (TREE_TYPE (tmp), 0));
    5959                 :         783 :   tmp = gfc_conv_descriptor_dtype (desc);
    5960                 :         783 :   type = gfc_get_element_type (TREE_TYPE (desc));
    5961                 :        1566 :   tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    5962                 :         783 :                          TREE_TYPE (tmp), tmp,
    5963                 :             :                          gfc_get_dtype_rank_type (e->rank, type));
    5964                 :         783 :   gfc_add_expr_to_block (&block, tmp);
    5965                 :         783 :   cond = build3_v (COND_EXPR, cond,
    5966                 :             :                    gfc_finish_block (&block),
    5967                 :             :                    build_empty_stmt (input_location));
    5968                 :         783 :   gfc_add_expr_to_block (&parmse->pre, cond);
    5969                 :             : }
    5970                 :             : 
    5971                 :             : 
    5972                 :             : 
    5973                 :             : /* Provide an interface between gfortran array descriptors and the F2018:18.4
    5974                 :             :    ISO_Fortran_binding array descriptors. */
    5975                 :             : 
    5976                 :             : static void
    5977                 :        6536 : gfc_conv_gfc_desc_to_cfi_desc (gfc_se *parmse, gfc_expr *e, gfc_symbol *fsym)
    5978                 :             : {
    5979                 :        6536 :   stmtblock_t block, block2;
    5980                 :        6536 :   tree cfi, gfc, tmp, tmp2;
    5981                 :        6536 :   tree present = NULL;
    5982                 :        6536 :   tree gfc_strlen = NULL;
    5983                 :        6536 :   tree rank;
    5984                 :        6536 :   gfc_se se;
    5985                 :             : 
    5986                 :        6536 :   if (fsym->attr.optional
    5987                 :        1094 :       && e->expr_type == EXPR_VARIABLE
    5988                 :        1094 :       && e->symtree->n.sym->attr.optional)
    5989                 :         103 :     present = gfc_conv_expr_present (e->symtree->n.sym);
    5990                 :             : 
    5991                 :        6536 :   gfc_init_block (&block);
    5992                 :             : 
    5993                 :             :   /* Convert original argument to a tree. */
    5994                 :        6536 :   gfc_init_se (&se, NULL);
    5995                 :        6536 :   if (e->rank == 0)
    5996                 :             :     {
    5997                 :         686 :       se.want_pointer = 1;
    5998                 :         686 :       gfc_conv_expr (&se, e);
    5999                 :         686 :       gfc = se.expr;
    6000                 :             :       /* gfc_conv_constant ignores se.want_poiner, e.g. for string_cst.  */
    6001                 :         686 :       if (!POINTER_TYPE_P (TREE_TYPE (gfc)))
    6002                 :          20 :         gfc = gfc_build_addr_expr (NULL, gfc);
    6003                 :             :     }
    6004                 :             :   else
    6005                 :             :     {
    6006                 :             :       /* If the actual argument can be noncontiguous, copy-in/out is required,
    6007                 :             :          if the dummy has either the CONTIGUOUS attribute or is an assumed-
    6008                 :             :          length assumed-length/assumed-size CHARACTER array.  This only
    6009                 :             :          applies if the actual argument is a "variable"; if it's some
    6010                 :             :          non-lvalue expression, we are going to evaluate it to a
    6011                 :             :          temporary below anyway.  */
    6012                 :        5850 :       se.force_no_tmp = 1;
    6013                 :        5850 :       if ((fsym->attr.contiguous
    6014                 :        4769 :            || (fsym->ts.type == BT_CHARACTER && !fsym->ts.u.cl->length
    6015                 :        1375 :                && (fsym->as->type == AS_ASSUMED_SIZE
    6016                 :         937 :                    || fsym->as->type == AS_EXPLICIT)))
    6017                 :        2023 :           && !gfc_is_simply_contiguous (e, false, true)
    6018                 :        6877 :           && gfc_expr_is_variable (e))
    6019                 :             :         {
    6020                 :        1021 :           bool optional = fsym->attr.optional;
    6021                 :        1021 :           fsym->attr.optional = 0;
    6022                 :        1021 :           gfc_conv_subref_array_arg (&se, e, false, fsym->attr.intent,
    6023                 :        1021 :                                      fsym->attr.pointer, fsym,
    6024                 :        1021 :                                      fsym->ns->proc_name->name, NULL,
    6025                 :             :                                      /* check_contiguous= */ true);
    6026                 :        1021 :           fsym->attr.optional = optional;
    6027                 :             :         }
    6028                 :             :       else
    6029                 :        4829 :         gfc_conv_expr_descriptor (&se, e);
    6030                 :        5850 :       gfc = se.expr;
    6031                 :             :       /* For dt(:)%var the elem_len*stride != sm, hence, GFC uses
    6032                 :             :          elem_len = sizeof(dt) and base_addr = dt(lb) instead.
    6033                 :             :          gfc_get_dataptr_offset fixes the base_addr; for elem_len, see below.
    6034                 :             :          While sm is fine as it uses span*stride and not elem_len.  */
    6035                 :        5850 :       if (POINTER_TYPE_P (TREE_TYPE (gfc)))
    6036                 :        1021 :         gfc = build_fold_indirect_ref_loc (input_location, gfc);
    6037                 :        4829 :       else if (is_subref_array (e) && e->ts.type != BT_CHARACTER)
    6038                 :          12 :          gfc_get_dataptr_offset (&se.pre, gfc, gfc, NULL, true, e);
    6039                 :             :     }
    6040                 :        6536 :   if (e->ts.type == BT_CHARACTER)
    6041                 :             :     {
    6042                 :        3409 :       if (se.string_length)
    6043                 :             :         gfc_strlen = se.string_length;
    6044                 :         883 :       else if (e->ts.u.cl->backend_decl)
    6045                 :             :         gfc_strlen = e->ts.u.cl->backend_decl;
    6046                 :             :       else
    6047                 :           0 :         gcc_unreachable ();
    6048                 :             :     }
    6049                 :        6536 :   gfc_add_block_to_block (&block, &se.pre);
    6050                 :             : 
    6051                 :             :   /* Create array descriptor and set version, rank, attribute, type. */
    6052                 :       12767 :   cfi = gfc_create_var (gfc_get_cfi_type (e->rank < 0
    6053                 :             :                                           ? GFC_MAX_DIMENSIONS : e->rank,
    6054                 :             :                                           false), "cfi");
    6055                 :             :   /* Convert to CFI_cdesc_t, which has dim[] to avoid TBAA issues,*/
    6056                 :        6536 :   if (fsym->attr.dimension && fsym->as->type == AS_ASSUMED_RANK)
    6057                 :             :     {
    6058                 :        2338 :       tmp = gfc_get_cfi_type (-1, !fsym->attr.pointer && !fsym->attr.target);
    6059                 :        2338 :       tmp = build_pointer_type (tmp);
    6060                 :        2338 :       parmse->expr = cfi = gfc_build_addr_expr (tmp, cfi);
    6061                 :        2338 :       cfi = build_fold_indirect_ref_loc (input_location, cfi);
    6062                 :             :     }
    6063                 :             :   else
    6064                 :        4198 :     parmse->expr = gfc_build_addr_expr (NULL, cfi);
    6065                 :             : 
    6066                 :        6536 :   tmp = gfc_get_cfi_desc_version (cfi);
    6067                 :        6536 :   gfc_add_modify (&block, tmp,
    6068                 :        6536 :                   build_int_cst (TREE_TYPE (tmp), CFI_VERSION));
    6069                 :        6536 :   if (e->rank < 0)
    6070                 :         305 :     rank = fold_convert (signed_char_type_node, gfc_conv_descriptor_rank (gfc));
    6071                 :             :   else
    6072                 :        6231 :     rank = build_int_cst (signed_char_type_node, e->rank);
    6073                 :        6536 :   tmp = gfc_get_cfi_desc_rank (cfi);
    6074                 :        6536 :   gfc_add_modify (&block, tmp, rank);
    6075                 :        6536 :   int itype = CFI_type_other;
    6076                 :        6536 :   if (e->ts.f90_type == BT_VOID)
    6077                 :          96 :     itype = (e->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
    6078                 :          96 :              ? CFI_type_cfunptr : CFI_type_cptr);
    6079                 :             :   else
    6080                 :             :     {
    6081                 :        6440 :       if (e->expr_type == EXPR_NULL && e->ts.type == BT_UNKNOWN)
    6082                 :           1 :         e->ts = fsym->ts;
    6083                 :        6440 :       switch (e->ts.type)
    6084                 :             :         {
    6085                 :        2296 :         case BT_INTEGER:
    6086                 :        2296 :         case BT_LOGICAL:
    6087                 :        2296 :         case BT_REAL:
    6088                 :        2296 :         case BT_COMPLEX:
    6089                 :        2296 :           itype = CFI_type_from_type_kind (e->ts.type, e->ts.kind);
    6090                 :        2296 :           break;
    6091                 :        3410 :         case BT_CHARACTER:
    6092                 :        3410 :           itype = CFI_type_from_type_kind (CFI_type_Character, e->ts.kind);
    6093                 :        3410 :           break;
    6094                 :             :         case BT_DERIVED:
    6095                 :        6536 :           itype = CFI_type_struct;
    6096                 :             :           break;
    6097                 :           0 :         case BT_VOID:
    6098                 :           0 :           itype = (e->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR
    6099                 :           0 :                    ? CFI_type_cfunptr : CFI_type_cptr);
    6100                 :             :           break;
    6101                 :             :         case BT_ASSUMED:
    6102                 :             :           itype = CFI_type_other;  // FIXME: Or CFI_type_cptr ?
    6103                 :             :           break;
    6104                 :           1 :         case BT_CLASS:
    6105                 :           1 :           if (fsym->ts.type == BT_ASSUMED)
    6106                 :             :             {
    6107                 :             :               // F2017: 7.3.2.2: "An entity that is declared using the TYPE(*)
    6108                 :             :               // type specifier is assumed-type and is an unlimited polymorphic
    6109                 :             :               //  entity." The actual argument _data component is passed.
    6110                 :             :               itype = CFI_type_other;  // FIXME: Or CFI_type_cptr ?
    6111                 :             :               break;
    6112                 :             :             }
    6113                 :             :           else
    6114                 :           0 :             gcc_unreachable ();
    6115                 :             : 
    6116                 :           0 :         case BT_UNSIGNED:
    6117                 :           0 :           gfc_internal_error ("Unsigned not yet implemented");
    6118                 :             : 
    6119                 :           0 :         case BT_PROCEDURE:
    6120                 :           0 :         case BT_HOLLERITH:
    6121                 :           0 :         case BT_UNION:
    6122                 :           0 :         case BT_BOZ:
    6123                 :           0 :         case BT_UNKNOWN:
    6124                 :             :           // FIXME: Really unreachable? Or reachable for type(*) ? If so, CFI_type_other?
    6125                 :           0 :           gcc_unreachable ();
    6126                 :             :         }
    6127                 :             :     }
    6128                 :             : 
    6129                 :        6536 :   tmp = gfc_get_cfi_desc_type (cfi);
    6130                 :        6536 :   gfc_add_modify (&block, tmp,
    6131                 :        6536 :                   build_int_cst (TREE_TYPE (tmp), itype));
    6132                 :             : 
    6133                 :        6536 :   int attr = CFI_attribute_other;
    6134                 :        6536 :   if (fsym->attr.pointer)
    6135                 :             :     attr = CFI_attribute_pointer;
    6136                 :        5774 :   else if (fsym->attr.allocatable)
    6137                 :         433 :     attr = CFI_attribute_allocatable;
    6138                 :        6536 :   tmp = gfc_get_cfi_desc_attribute (cfi);
    6139                 :        6536 :   gfc_add_modify (&block, tmp,
    6140                 :        6536 :                   build_int_cst (TREE_TYPE (tmp), attr));
    6141                 :             : 
    6142                 :             :   /* The cfi-base_addr assignment could be skipped for 'pointer, intent(out)'.
    6143                 :             :      That is very sensible for undefined pointers, but the C code might assume
    6144                 :             :      that the pointer retains the value, in particular, if it was NULL.  */
    6145                 :        6536 :   if (e->rank == 0)
    6146                 :             :     {
    6147                 :         686 :       tmp = gfc_get_cfi_desc_base_addr (cfi);
    6148                 :         686 :       gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), gfc));
    6149                 :             :     }
    6150                 :             :   else
    6151                 :             :     {
    6152                 :        5850 :       tmp = gfc_get_cfi_desc_base_addr (cfi);
    6153                 :        5850 :       tmp2 = gfc_conv_descriptor_data_get (gfc);
    6154                 :        5850 :       gfc_add_modify (&block, tmp, fold_convert (TREE_TYPE (tmp), tmp2));
    6155                 :             :     }
    6156                 :             : 
    6157                 :             :   /* Set elem_len if known - must be before the next if block.
    6158                 :             :      Note that allocatable implies 'len=:'.  */
    6159                 :        6536 :   if (e->ts.type != BT_ASSUMED && e->ts.type != BT_CHARACTER )
    6160                 :             :     {
    6161                 :             :       /* Length is known at compile time; use 'block' for it.  */
    6162                 :        3072 :       tmp = size_in_bytes (gfc_typenode_for_spec (&e->ts));
    6163                 :        3072 :       tmp2 = gfc_get_cfi_desc_elem_len (cfi);
    6164                 :        3072 :       gfc_add_modify (&block, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
    6165                 :             :     }
    6166                 :             : 
    6167                 :        6536 :   if (fsym->attr.pointer && fsym->attr.intent == INTENT_OUT)
    6168                 :          91 :     goto done;
    6169                 :             : 
    6170                 :             :   /* When allocatable + intent out, free the cfi descriptor.  */
    6171                 :        6445 :   if (fsym->attr.allocatable && fsym->attr.intent == INTENT_OUT)
    6172                 :             :     {
    6173                 :          90 :       tmp = gfc_get_cfi_desc_base_addr (cfi);
    6174                 :          90 :       tree call = builtin_decl_explicit (BUILT_IN_FREE);
    6175                 :          90 :       call = build_call_expr_loc (input_location, call, 1, tmp);
    6176                 :          90 :       gfc_add_expr_to_block (&block, fold_convert (void_type_node, call));
    6177                 :          90 :       gfc_add_modify (&block, tmp,
    6178                 :          90 :                       fold_convert (TREE_TYPE (tmp), null_pointer_node));
    6179                 :          90 :       goto done;
    6180                 :             :     }
    6181                 :             : 
    6182                 :             :   /* If not unallocated/unassociated. */
    6183                 :        6355 :   gfc_init_block (&block2);
    6184                 :             : 
    6185                 :             :   /* Set elem_len, which may be only known at run time. */
    6186                 :        6355 :   if (e->ts.type == BT_CHARACTER
    6187                 :        3410 :       && (e->expr_type != EXPR_NULL || gfc_strlen != NULL_TREE))
    6188                 :             :     {
    6189                 :        3408 :       gcc_assert (gfc_strlen);
    6190                 :        3409 :       tmp = gfc_strlen;
    6191                 :        3409 :       if (e->ts.kind != 1)
    6192                 :        1117 :         tmp = fold_build2_loc (input_location, MULT_EXPR,
    6193                 :             :                                gfc_charlen_type_node, tmp,
    6194                 :        1117 :                                build_int_cst (gfc_charlen_type_node,
    6195                 :        1117 :                                               e->ts.kind));
    6196                 :        3409 :       tmp2 = gfc_get_cfi_desc_elem_len (cfi);
    6197                 :        3409 :       gfc_add_modify (&block2, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
    6198                 :             :     }
    6199                 :        2946 :   else if (e->ts.type == BT_ASSUMED)
    6200                 :             :     {
    6201                 :          54 :       tmp = gfc_conv_descriptor_elem_len (gfc);
    6202                 :          54 :       tmp2 = gfc_get_cfi_desc_elem_len (cfi);
    6203                 :          54 :       gfc_add_modify (&block2, tmp2, fold_convert (TREE_TYPE (tmp2), tmp));
    6204                 :             :     }
    6205                 :             : 
    6206                 :        6355 :   if (e->ts.type == BT_ASSUMED)
    6207                 :             :     {
    6208                 :             :       /* Note: type(*) implies assumed-shape/assumed-rank if fsym requires
    6209                 :             :          an CFI descriptor.  Use the type in the descriptor as it provide
    6210                 :             :          mode information. (Quality of implementation feature.)  */
    6211                 :          54 :       tree cond;
    6212                 :          54 :       tree ctype = gfc_get_cfi_desc_type (cfi);
    6213                 :          54 :       tree type = fold_convert (TREE_TYPE (ctype),
    6214                 :             :                                 gfc_conv_descriptor_type (gfc));
    6215                 :          54 :       tree kind = fold_convert (TREE_TYPE (ctype),
    6216                 :             :                                 gfc_conv_descriptor_elem_len (gfc));
    6217                 :          54 :       kind = fold_build2_loc (input_location, LSHIFT_EXPR, TREE_TYPE (type),
    6218                 :          54 :                               kind, build_int_cst (TREE_TYPE (type),
    6219                 :          54 :                                                    CFI_type_kind_shift));
    6220                 :             : 
    6221                 :             :       /* if (BT_VOID) CFI_type_cptr else CFI_type_other  */
    6222                 :             :       /* Note: BT_VOID is could also be CFI_type_funcptr, but assume c_ptr. */
    6223                 :          54 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6224                 :          54 :                               build_int_cst (TREE_TYPE (type), BT_VOID));
    6225                 :          54 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, ctype,
    6226                 :          54 :                              build_int_cst (TREE_TYPE (type), CFI_type_cptr));
    6227                 :          54 :       tmp2 = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    6228                 :             :                               ctype,
    6229                 :          54 :                               build_int_cst (TREE_TYPE (type), CFI_type_other));
    6230                 :          54 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    6231                 :             :                               tmp, tmp2);
    6232                 :             :       /* if (BT_DERIVED) CFI_type_struct else  < tmp2 >  */
    6233                 :          54 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6234                 :          54 :                               build_int_cst (TREE_TYPE (type), BT_DERIVED));
    6235                 :          54 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node, ctype,
    6236                 :          54 :                              build_int_cst (TREE_TYPE (type), CFI_type_struct));
    6237                 :          54 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    6238                 :             :                               tmp, tmp2);
    6239                 :             :       /* if (BT_CHARACTER) CFI_type_Character + kind=1 else  < tmp2 >  */
    6240                 :             :       /* Note: could also be kind=4, with cfi->elem_len = gfc->elem_len*4.  */
    6241                 :          54 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6242                 :          54 :                               build_int_cst (TREE_TYPE (type), BT_CHARACTER));
    6243                 :          54 :       tmp = build_int_cst (TREE_TYPE (type),
    6244                 :          54 :                            CFI_type_from_type_kind (CFI_type_Character, 1));
    6245                 :          54 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    6246                 :             :                              ctype, tmp);
    6247                 :          54 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    6248                 :             :                               tmp, tmp2);
    6249                 :             :       /* if (BT_COMPLEX) CFI_type_Complex + kind/2 else  < tmp2 >  */
    6250                 :          54 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6251                 :          54 :                               build_int_cst (TREE_TYPE (type), BT_COMPLEX));
    6252                 :          54 :       tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR, TREE_TYPE (type),
    6253                 :          54 :                              kind, build_int_cst (TREE_TYPE (type), 2));
    6254                 :          54 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (type), tmp,
    6255                 :          54 :                              build_int_cst (TREE_TYPE (type),
    6256                 :          54 :                                             CFI_type_Complex));
    6257                 :          54 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    6258                 :             :                              ctype, tmp);
    6259                 :          54 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    6260                 :             :                               tmp, tmp2);
    6261                 :             :       /* if (BT_INTEGER || BT_LOGICAL || BT_REAL) type + kind else  <tmp2>  */
    6262                 :          54 :       cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6263                 :          54 :                               build_int_cst (TREE_TYPE (type), BT_INTEGER));
    6264                 :          54 :       tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6265                 :          54 :                               build_int_cst (TREE_TYPE (type), BT_LOGICAL));
    6266                 :          54 :       cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
    6267                 :             :                               cond, tmp);
    6268                 :          54 :       tmp = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node, type,
    6269                 :          54 :                               build_int_cst (TREE_TYPE (type), BT_REAL));
    6270                 :          54 :       cond = fold_build2_loc (input_location, TRUTH_OR_EXPR, boolean_type_node,
    6271                 :             :                               cond, tmp);
    6272                 :          54 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (type),
    6273                 :             :                              type, kind);
    6274                 :          54 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, void_type_node,
    6275                 :             :                              ctype, tmp);
    6276                 :          54 :       tmp2 = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    6277                 :             :                               tmp, tmp2);
    6278                 :          54 :       gfc_add_expr_to_block (&block2, tmp2);
    6279                 :             :     }
    6280                 :             : 
    6281                 :        6355 :   if (e->rank != 0)
    6282                 :             :     {
    6283                 :             :       /* Loop: for (i = 0; i < rank; ++i).  */
    6284                 :        5735 :       tree idx = gfc_create_var (TREE_TYPE (rank), "idx");
    6285                 :             :       /* Loop body.  */
    6286                 :        5735 :       stmtblock_t loop_body;
    6287                 :        5735 :       gfc_init_block (&loop_body);
    6288                 :             :       /* cfi->dim[i].lower_bound = (allocatable/pointer)
    6289                 :             :                                    ? gfc->dim[i].lbound : 0 */
    6290                 :        5735 :       if (fsym->attr.pointer || fsym->attr.allocatable)
    6291                 :         648 :         tmp = gfc_conv_descriptor_lbound_get (gfc, idx);
    6292                 :             :       else
    6293                 :        5087 :         tmp = gfc_index_zero_node;
    6294                 :        5735 :       gfc_add_modify (&loop_body, gfc_get_cfi_dim_lbound (cfi, idx), tmp);
    6295                 :             :       /* cfi->dim[i].extent = gfc->dim[i].ubound - gfc->dim[i].lbound + 1.  */
    6296                 :        5735 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    6297                 :             :                              gfc_conv_descriptor_ubound_get (gfc, idx),
    6298                 :             :                              gfc_conv_descriptor_lbound_get (gfc, idx));
    6299                 :        5735 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    6300                 :             :                              tmp, gfc_index_one_node);
    6301                 :        5735 :       gfc_add_modify (&loop_body, gfc_get_cfi_dim_extent (cfi, idx), tmp);
    6302                 :             :       /* d->dim[n].sm = gfc->dim[i].stride  * gfc->span); */
    6303                 :        5735 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    6304                 :             :                              gfc_conv_descriptor_stride_get (gfc, idx),
    6305                 :             :                              gfc_conv_descriptor_span_get (gfc));
    6306                 :        5735 :       gfc_add_modify (&loop_body, gfc_get_cfi_dim_sm (cfi, idx), tmp);
    6307                 :             : 
    6308                 :             :       /* Generate loop.  */
    6309                 :       11470 :       gfc_simple_for_loop (&block2, idx, build_int_cst (TREE_TYPE (idx), 0),
    6310                 :        5735 :                            rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
    6311                 :             :                            gfc_finish_block (&loop_body));
    6312                 :             : 
    6313                 :        5735 :       if (e->expr_type == EXPR_VARIABLE
    6314                 :        5573 :           && e->ref
    6315                 :        5573 :           && e->ref->u.ar.type == AR_FULL
    6316                 :        2732 :           && e->symtree->n.sym->attr.dummy
    6317                 :         988 :           && e->symtree->n.sym->as
    6318                 :         988 :           && e->symtree->n.sym->as->type == AS_ASSUMED_SIZE)
    6319                 :             :         {
    6320                 :         138 :           tmp = gfc_get_cfi_dim_extent (cfi, gfc_rank_cst[e->rank-1]),
    6321                 :         138 :           gfc_add_modify (&block2, tmp, build_int_cst (TREE_TYPE (tmp), -1));
    6322                 :             :         }
    6323                 :             :     }
    6324                 :             : 
    6325                 :        6355 :   if (fsym->attr.allocatable || fsym->attr.pointer)
    6326                 :             :     {
    6327                 :        1014 :       tmp = gfc_get_cfi_desc_base_addr (cfi),
    6328                 :        1014 :       tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    6329                 :             :                              tmp, null_pointer_node);
    6330                 :        1014 :       tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
    6331                 :             :                       build_empty_stmt (input_location));
    6332                 :        1014 :       gfc_add_expr_to_block (&block, tmp);
    6333                 :             :     }
    6334                 :             :   else
    6335                 :        5341 :     gfc_add_block_to_block (&block, &block2);
    6336                 :             : 
    6337                 :             : 
    6338                 :        6536 : done:
    6339                 :        6536 :   if (present)
    6340                 :             :     {
    6341                 :         103 :       parmse->expr = build3_loc (input_location, COND_EXPR,
    6342                 :         103 :                                  TREE_TYPE (parmse->expr),
    6343                 :             :                                  present, parmse->expr, null_pointer_node);
    6344                 :         103 :       tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block),
    6345                 :             :                       build_empty_stmt (input_location));
    6346                 :         103 :       gfc_add_expr_to_block (&parmse->pre, tmp);
    6347                 :             :     }
    6348                 :             :   else
    6349                 :        6433 :     gfc_add_block_to_block (&parmse->pre, &block);
    6350                 :             : 
    6351                 :        6536 :   gfc_init_block (&block);
    6352                 :             : 
    6353                 :        6536 :   if ((!fsym->attr.allocatable && !fsym->attr.pointer)
    6354                 :        1195 :       || fsym->attr.intent == INTENT_IN)
    6355                 :        5549 :     goto post_call;
    6356                 :             : 
    6357                 :         987 :   gfc_init_block (&block2);
    6358                 :         987 :   if (e->rank == 0)
    6359                 :             :     {
    6360                 :         428 :       tmp = gfc_get_cfi_desc_base_addr (cfi);
    6361                 :         428 :       gfc_add_modify (&block, gfc, fold_convert (TREE_TYPE (gfc), tmp));
    6362                 :             :     }
    6363                 :             :   else
    6364                 :             :     {
    6365                 :         559 :       tmp = gfc_get_cfi_desc_base_addr (cfi);
    6366                 :         559 :       gfc_conv_descriptor_data_set (&block, gfc, tmp);
    6367                 :             : 
    6368                 :         559 :       if (fsym->attr.allocatable)
    6369                 :             :         {
    6370                 :             :           /* gfc->span = cfi->elem_len.  */
    6371                 :         252 :           tmp = fold_convert (gfc_array_index_type,
    6372                 :             :                               gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]));
    6373                 :             :         }
    6374                 :             :       else
    6375                 :             :         {
    6376                 :             :           /* gfc->span = ((cfi->dim[0].sm % cfi->elem_len)
    6377                 :             :                           ? cfi->dim[0].sm : cfi->elem_len).  */
    6378                 :         307 :           tmp = gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]);
    6379                 :         307 :           tmp2 = fold_convert (gfc_array_index_type,
    6380                 :             :                                gfc_get_cfi_desc_elem_len (cfi));
    6381                 :         307 :           tmp = fold_build2_loc (input_location, TRUNC_MOD_EXPR,
    6382                 :             :                                  gfc_array_index_type, tmp, tmp2);
    6383                 :         307 :           tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    6384                 :             :                              tmp, gfc_index_zero_node);
    6385                 :         307 :           tmp = build3_loc (input_location, COND_EXPR, gfc_array_index_type, tmp,
    6386                 :             :                             gfc_get_cfi_dim_sm (cfi, gfc_rank_cst[0]), tmp2);
    6387                 :             :         }
    6388                 :         559 :       gfc_conv_descriptor_span_set (&block2, gfc, tmp);
    6389                 :             : 
    6390                 :             :       /* Calculate offset + set lbound, ubound and stride.  */
    6391                 :         559 :       gfc_conv_descriptor_offset_set (&block2, gfc, gfc_index_zero_node);
    6392                 :             :       /* Loop: for (i = 0; i < rank; ++i).  */
    6393                 :         559 :       tree idx = gfc_create_var (TREE_TYPE (rank), "idx");
    6394                 :             :       /* Loop body.  */
    6395                 :         559 :       stmtblock_t loop_body;
    6396                 :         559 :       gfc_init_block (&loop_body);
    6397                 :             :       /* gfc->dim[i].lbound = ... */
    6398                 :         559 :       tmp = gfc_get_cfi_dim_lbound (cfi, idx);
    6399                 :         559 :       gfc_conv_descriptor_lbound_set (&loop_body, gfc, idx, tmp);
    6400                 :             : 
    6401                 :             :       /* gfc->dim[i].ubound = gfc->dim[i].lbound + cfi->dim[i].extent - 1. */
    6402                 :         559 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    6403                 :             :                              gfc_conv_descriptor_lbound_get (gfc, idx),
    6404                 :             :                              gfc_index_one_node);
    6405                 :         559 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    6406                 :             :                              gfc_get_cfi_dim_extent (cfi, idx), tmp);
    6407                 :         559 :       gfc_conv_descriptor_ubound_set (&loop_body, gfc, idx, tmp);
    6408                 :             : 
    6409                 :             :       /* gfc->dim[i].stride = cfi->dim[i].sm / cfi>elem_len */
    6410                 :         559 :       tmp = gfc_get_cfi_dim_sm (cfi, idx);
    6411                 :         559 :       tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    6412                 :             :                              gfc_array_index_type, tmp,
    6413                 :             :                              fold_convert (gfc_array_index_type,
    6414                 :             :                                            gfc_get_cfi_desc_elem_len (cfi)));
    6415                 :         559 :       gfc_conv_descriptor_stride_set (&loop_body, gfc, idx, tmp);
    6416                 :             : 
    6417                 :             :       /* gfc->offset -= gfc->dim[i].stride * gfc->dim[i].lbound. */
    6418                 :         559 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    6419                 :             :                              gfc_conv_descriptor_stride_get (gfc, idx),
    6420                 :             :                              gfc_conv_descriptor_lbound_get (gfc, idx));
    6421                 :         559 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    6422                 :             :                              gfc_conv_descriptor_offset_get (gfc), tmp);
    6423                 :         559 :       gfc_conv_descriptor_offset_set (&loop_body, gfc, tmp);
    6424                 :             :       /* Generate loop.  */
    6425                 :        1118 :       gfc_simple_for_loop (&block2, idx, build_int_cst (TREE_TYPE (idx), 0),
    6426                 :         559 :                            rank, LT_EXPR, build_int_cst (TREE_TYPE (idx), 1),
    6427                 :             :                            gfc_finish_block (&loop_body));
    6428                 :             :     }
    6429                 :             : 
    6430                 :         987 :   if (e->ts.type == BT_CHARACTER && !e->ts.u.cl->length)
    6431                 :             :     {
    6432                 :          60 :       tmp = fold_convert (gfc_charlen_type_node,
    6433                 :             :                           gfc_get_cfi_desc_elem_len (cfi));
    6434                 :          60 :       if (e->ts.kind != 1)
    6435                 :          24 :         tmp = fold_build2_loc (input_location, TRUNC_DIV_EXPR,
    6436                 :             :                                gfc_charlen_type_node, tmp,
    6437                 :          24 :                                build_int_cst (gfc_charlen_type_node,
    6438                 :          24 :                                               e->ts.kind));
    6439                 :          60 :       gfc_add_modify (&block2, gfc_strlen, tmp);
    6440                 :             :     }
    6441                 :             : 
    6442                 :         987 :   tmp = gfc_get_cfi_desc_base_addr (cfi),
    6443                 :         987 :   tmp = fold_build2_loc (input_location, NE_EXPR, boolean_type_node,
    6444                 :             :                          tmp, null_pointer_node);
    6445                 :         987 :   tmp = build3_v (COND_EXPR, tmp, gfc_finish_block (&block2),
    6446                 :             :                   build_empty_stmt (input_location));
    6447                 :         987 :   gfc_add_expr_to_block (&block, tmp);
    6448                 :             : 
    6449                 :        6536 : post_call:
    6450                 :        6536 :   gfc_add_block_to_block (&block, &se.post);
    6451                 :        6536 :   if (present && block.head)
    6452                 :             :     {
    6453                 :           6 :       tmp = build3_v (COND_EXPR, present, gfc_finish_block (&block),
    6454                 :             :                       build_empty_stmt (input_location));
    6455                 :           6 :       gfc_add_expr_to_block (&parmse->post, tmp);
    6456                 :             :     }
    6457                 :        6530 :   else if (block.head)
    6458                 :        1558 :     gfc_add_block_to_block (&parmse->post, &block);
    6459                 :        6536 : }
    6460                 :             : 
    6461                 :             : 
    6462                 :             : /* Create "conditional temporary" to handle scalar dummy variables with the
    6463                 :             :    OPTIONAL+VALUE attribute that shall not be dereferenced.  Use null value
    6464                 :             :    as fallback.  Does not handle CLASS.  */
    6465                 :             : 
    6466                 :             : static void
    6467                 :         222 : conv_cond_temp (gfc_se * parmse, gfc_expr * e, tree cond)
    6468                 :             : {
    6469                 :         222 :   tree temp;
    6470                 :         222 :   gcc_assert (e && e->ts.type != BT_CLASS);
    6471                 :         222 :   gcc_assert (e->rank == 0);
    6472                 :         222 :   temp = gfc_create_var (TREE_TYPE (parmse->expr), "condtemp");
    6473                 :         222 :   TREE_STATIC (temp) = 1;
    6474                 :         222 :   TREE_CONSTANT (temp) = 1;
    6475                 :         222 :   TREE_READONLY (temp) = 1;
    6476                 :         222 :   DECL_INITIAL (temp) = build_zero_cst (TREE_TYPE (temp));
    6477                 :         222 :   parmse->expr = fold_build3_loc (input_location, COND_EXPR,
    6478                 :         222 :                                   TREE_TYPE (parmse->expr),
    6479                 :             :                                   cond, parmse->expr, temp);
    6480                 :         222 :   parmse->expr = gfc_evaluate_now (parmse->expr, &parmse->pre);
    6481                 :         222 : }
    6482                 :             : 
    6483                 :             : 
    6484                 :             : /* Helper function for the handling of (currently) scalar dummy variables
    6485                 :             :    with the VALUE attribute.  Argument parmse should already be set up.  */
    6486                 :             : static void
    6487                 :       22086 : conv_dummy_value (gfc_se * parmse, gfc_expr * e, gfc_symbol * fsym,
    6488                 :             :                   vec<tree, va_gc> *& optionalargs)
    6489                 :             : {
    6490                 :       22086 :   tree tmp;
    6491                 :             : 
    6492                 :       22086 :   gcc_assert (fsym && fsym->attr.value && !fsym->attr.dimension);
    6493                 :             : 
    6494                 :             :   /* Absent actual argument for optional scalar dummy.  */
    6495                 :       22086 :   if ((e == NULL || e->expr_type == EXPR_NULL) && fsym->attr.optional)
    6496                 :             :     {
    6497                 :             :       /* For scalar arguments with VALUE attribute which are passed by
    6498                 :             :          value, pass "0" and a hidden argument for the optional status.  */
    6499                 :         427 :       if (fsym->ts.type == BT_CHARACTER)
    6500                 :             :         {
    6501                 :             :           /* Pass a NULL pointer for an absent CHARACTER arg and a length of
    6502                 :             :              zero.  */
    6503                 :          90 :           parmse->expr = null_pointer_node;
    6504                 :          90 :           parmse->string_length = build_int_cst (gfc_charlen_type_node, 0);
    6505                 :             :         }
    6506                 :         337 :       else if (gfc_bt_struct (fsym->ts.type)
    6507                 :          30 :                && !(fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING))
    6508                 :             :         {
    6509                 :             :           /* Pass null struct.  Types c_ptr and c_funptr from ISO_C_BINDING
    6510                 :             :              are pointers and passed as such below.  */
    6511                 :          24 :           tree temp = gfc_create_var (gfc_sym_type (fsym), "absent");
    6512                 :          24 :           TREE_CONSTANT (temp) = 1;
    6513                 :          24 :           TREE_READONLY (temp) = 1;
    6514                 :          24 :           DECL_INITIAL (temp) = build_zero_cst (TREE_TYPE (temp));
    6515                 :          24 :           parmse->expr = temp;
    6516                 :          24 :         }
    6517                 :             :       else
    6518                 :         313 :         parmse->expr = fold_convert (gfc_sym_type (fsym),
    6519                 :             :                                      integer_zero_node);
    6520                 :         427 :       vec_safe_push (optionalargs, boolean_false_node);
    6521                 :             : 
    6522                 :         427 :       return;
    6523                 :             :     }
    6524                 :             : 
    6525                 :             :   /* gfortran argument passing conventions:
    6526                 :             :      actual arguments to CHARACTER(len=1),VALUE
    6527                 :             :      dummy arguments are actually passed by value.
    6528                 :             :      Strings are truncated to length 1.  */
    6529                 :       21659 :   if (gfc_length_one_character_type_p (&fsym->ts))
    6530                 :             :     {
    6531                 :         390 :       if (e->expr_type == EXPR_CONSTANT
    6532                 :          66 :           && e->value.character.length > 1)
    6533                 :             :         {
    6534                 :          12 :           e->value.character.length = 1;
    6535                 :          12 :           gfc_conv_expr (parmse, e);
    6536                 :             :         }
    6537                 :             : 
    6538                 :         390 :       tree slen1 = build_int_cst (gfc_charlen_type_node, 1);
    6539                 :         390 :       gfc_conv_string_parameter (parmse);
    6540                 :         390 :       parmse->expr = gfc_string_to_single_character (slen1, parmse->expr,
    6541                 :             :                                                      e->ts.kind);
    6542                 :             :       /* Truncate resulting string to length 1.  */
    6543                 :         390 :       parmse->string_length = slen1;
    6544                 :             :     }
    6545                 :             : 
    6546                 :       21659 :   if (fsym->attr.optional && fsym->ts.type != BT_CLASS)
    6547                 :             :     {
    6548                 :             :       /* F2018:15.5.2.12 Argument presence and
    6549                 :             :          restrictions on arguments not present.  */
    6550                 :         811 :       if (e->expr_type == EXPR_VARIABLE
    6551                 :         638 :           && e->rank == 0
    6552                 :        1395 :           && (gfc_expr_attr (e).allocatable
    6553                 :         482 :               || gfc_expr_attr (e).pointer))
    6554                 :             :         {
    6555                 :         186 :           gfc_se argse;
    6556                 :         186 :           tree cond;
    6557                 :         186 :           gfc_init_se (&argse, NULL);
    6558                 :         186 :           argse.want_pointer = 1;
    6559                 :         186 :           gfc_conv_expr (&argse, e);
    6560                 :         186 :           cond = fold_convert (TREE_TYPE (argse.expr), null_pointer_node);
    6561                 :         186 :           cond = fold_build2_loc (input_location, NE_EXPR,
    6562                 :             :                                   logical_type_node,
    6563                 :             :                                   argse.expr, cond);
    6564                 :         372 :           vec_safe_push (optionalargs,
    6565                 :         186 :                          fold_convert (boolean_type_node, cond));
    6566                 :             :           /* Create "conditional temporary".  */
    6567                 :         186 :           conv_cond_temp (parmse, e, cond);
    6568                 :             :         }
    6569                 :         625 :       else if (e->expr_type != EXPR_VARIABLE
    6570                 :         452 :                || !e->symtree->n.sym->attr.optional
    6571                 :         260 :                || (e->ref != NULL && e->ref->type != REF_ARRAY))
    6572                 :         365 :         vec_safe_push (optionalargs, boolean_true_node);
    6573                 :             :       else
    6574                 :             :         {
    6575                 :         260 :           tmp = gfc_conv_expr_present (e->symtree->n.sym);
    6576                 :         260 :           if (gfc_bt_struct (fsym->ts.type)
    6577                 :          36 :               && !(fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING))
    6578                 :          36 :             conv_cond_temp (parmse, e, tmp);
    6579                 :         224 :           else if (e->ts.type != BT_CHARACTER && !e->symtree->n.sym->attr.value)
    6580                 :          84 :             parmse->expr
    6581                 :         168 :               = fold_build3_loc (input_location, COND_EXPR,
    6582                 :          84 :                                  TREE_TYPE (parmse->expr),
    6583                 :             :                                  tmp, parmse->expr,
    6584                 :          84 :                                  fold_convert (TREE_TYPE (parmse->expr),
    6585                 :             :                                                integer_zero_node));
    6586                 :             : 
    6587                 :         520 :           vec_safe_push (optionalargs,
    6588                 :         260 :                          fold_convert (boolean_type_node, tmp));
    6589                 :             :         }
    6590                 :             :     }
    6591                 :             : }
    6592                 :             : 
    6593                 :             : 
    6594                 :             : /* Helper function for the handling of NULL() actual arguments associated with
    6595                 :             :    non-optional dummy variables.  Argument parmse should already be set up.  */
    6596                 :             : static void
    6597                 :         426 : conv_null_actual (gfc_se * parmse, gfc_expr * e, gfc_symbol * fsym)
    6598                 :             : {
    6599                 :         426 :   gcc_assert (fsym && e->expr_type == EXPR_NULL);
    6600                 :             : 
    6601                 :             :   /* Obtain the character length for a NULL() actual with a character
    6602                 :             :      MOLD argument.  Otherwise substitute a suitable dummy length.
    6603                 :             :      Here we handle only non-optional dummies of non-bind(c) procedures.  */
    6604                 :         426 :   if (fsym->ts.type == BT_CHARACTER)
    6605                 :             :     {
    6606                 :         216 :       if (e->ts.type == BT_CHARACTER
    6607                 :         162 :           && e->symtree->n.sym->ts.type == BT_CHARACTER)
    6608                 :             :         {
    6609                 :             :           /* MOLD is present.  Substitute a temporary character NULL pointer.
    6610                 :             :              For an assumed-rank dummy we need a descriptor that passes the
    6611                 :             :              correct rank.  */
    6612                 :         162 :           if (fsym->as && fsym->as->type == AS_ASSUMED_RANK)
    6613                 :             :             {
    6614                 :          54 :               tree rank;
    6615                 :          54 :               tree tmp = parmse->expr;
    6616                 :          54 :               tmp = gfc_conv_scalar_to_descriptor (parmse, tmp, fsym->attr);
    6617                 :          54 :               rank = gfc_conv_descriptor_rank (tmp);
    6618                 :          54 :               gfc_add_modify (&parmse->pre, rank,
    6619                 :          54 :                               build_int_cst (TREE_TYPE (rank), e->rank));
    6620                 :          54 :               parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
    6621                 :          54 :             }
    6622                 :             :           else
    6623                 :             :             {
    6624                 :         108 :               tree tmp = gfc_create_var (TREE_TYPE (parmse->expr), "null");
    6625                 :         108 :               gfc_add_modify (&parmse->pre, tmp,
    6626                 :         108 :                               build_zero_cst (TREE_TYPE (tmp)));
    6627                 :         108 :               parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
    6628                 :             :             }
    6629                 :             : 
    6630                 :             :           /* Ensure that a usable length is available.  */
    6631                 :         162 :           if (parmse->string_length == NULL_TREE)
    6632                 :             :             {
    6633                 :         162 :               gfc_typespec *ts = &e->symtree->n.sym->ts;
    6634                 :             : 
    6635                 :         162 :               if (ts->u.cl->length != NULL
    6636                 :         108 :                   && ts->u.cl->length->expr_type == EXPR_CONSTANT)
    6637                 :         108 :                 gfc_conv_const_charlen (ts->u.cl);
    6638                 :             : 
    6639                 :         162 :               if (ts->u.cl->backend_decl)
    6640                 :         162 :                 parmse->string_length = ts->u.cl->backend_decl;
    6641                 :             :             }
    6642                 :             :         }
    6643                 :          54 :       else if (e->ts.type == BT_UNKNOWN && parmse->string_length == NULL_TREE)
    6644                 :             :         {
    6645                 :             :           /* MOLD is not present.  Pass length of associated dummy character
    6646                 :             :              argument if constant, or zero.  */
    6647                 :          54 :           if (fsym->ts.u.cl->length != NULL
    6648                 :          18 :               && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
    6649                 :             :             {
    6650                 :          18 :               gfc_conv_const_charlen (fsym->ts.u.cl);
    6651                 :          18 :               parmse->string_length = fsym->ts.u.cl->backend_decl;
    6652                 :             :             }
    6653                 :             :           else
    6654                 :             :             {
    6655                 :          36 :               parmse->string_length = gfc_create_var (gfc_charlen_type_node,
    6656                 :             :                                                       "slen");
    6657                 :          36 :               gfc_add_modify (&parmse->pre, parmse->string_length,
    6658                 :             :                               build_zero_cst (gfc_charlen_type_node));
    6659                 :             :             }
    6660                 :             :         }
    6661                 :             :     }
    6662                 :         210 :   else if (fsym->ts.type == BT_DERIVED)
    6663                 :             :     {
    6664                 :         210 :       if (e->ts.type != BT_UNKNOWN)
    6665                 :             :         /* MOLD is present.  Pass a corresponding temporary NULL pointer.
    6666                 :             :            For an assumed-rank dummy we provide a descriptor that passes
    6667                 :             :            the correct rank.  */
    6668                 :             :         {
    6669                 :         138 :           tree rank;
    6670                 :         138 :           tree tmp = parmse->expr;
    6671                 :             : 
    6672                 :         138 :           tmp = gfc_conv_scalar_to_descriptor (parmse, tmp, gfc_expr_attr (e));
    6673                 :         138 :           rank = gfc_conv_descriptor_rank (tmp);
    6674                 :         138 :           gfc_add_modify (&parmse->pre, rank,
    6675                 :         138 :                           build_int_cst (TREE_TYPE (rank), e->rank));
    6676                 :         138 :           gfc_conv_descriptor_data_set (&parmse->pre, tmp, null_pointer_node);
    6677                 :         138 :           parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
    6678                 :             :         }
    6679                 :             :       else
    6680                 :             :         /* MOLD is not present.  Use attributes from dummy argument, which is
    6681                 :             :            not allowed to be assumed-rank.  */
    6682                 :             :         {
    6683                 :          72 :           int dummy_rank;
    6684                 :          72 :           tree tmp = parmse->expr;
    6685                 :             : 
    6686                 :          72 :           if ((fsym->attr.allocatable || fsym->attr.pointer)
    6687                 :          72 :               && fsym->attr.intent == INTENT_UNKNOWN)
    6688                 :          36 :             fsym->attr.intent = INTENT_IN;
    6689                 :          72 :           tmp = gfc_conv_scalar_to_descriptor (parmse, tmp, fsym->attr);
    6690                 :          72 :           dummy_rank = fsym->as ? fsym->as->rank : 0;
    6691                 :          24 :           if (dummy_rank > 0)
    6692                 :             :             {
    6693                 :          24 :               tree rank = gfc_conv_descriptor_rank (tmp);
    6694                 :          24 :               gfc_add_modify (&parmse->pre, rank,
    6695                 :          24 :                               build_int_cst (TREE_TYPE (rank), dummy_rank));
    6696                 :             :             }
    6697                 :          72 :           gfc_conv_descriptor_data_set (&parmse->pre, tmp, null_pointer_node);
    6698                 :          72 :           parmse->expr = gfc_build_addr_expr (NULL_TREE, tmp);
    6699                 :             :         }
    6700                 :             :     }
    6701                 :         426 : }
    6702                 :             : 
    6703                 :             : 
    6704                 :             : /* Generate code for a procedure call.  Note can return se->post != NULL.
    6705                 :             :    If se->direct_byref is set then se->expr contains the return parameter.
    6706                 :             :    Return nonzero, if the call has alternate specifiers.
    6707                 :             :    'expr' is only needed for procedure pointer components.  */
    6708                 :             : 
    6709                 :             : int
    6710                 :      130964 : gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
    6711                 :             :                          gfc_actual_arglist * args, gfc_expr * expr,
    6712                 :             :                          vec<tree, va_gc> *append_args)
    6713                 :             : {
    6714                 :      130964 :   gfc_interface_mapping mapping;
    6715                 :      130964 :   vec<tree, va_gc> *arglist;
    6716                 :      130964 :   vec<tree, va_gc> *retargs;
    6717                 :      130964 :   tree tmp;
    6718                 :      130964 :   tree fntype;
    6719                 :      130964 :   gfc_se parmse;
    6720                 :      130964 :   gfc_array_info *info;
    6721                 :      130964 :   int byref;
    6722                 :      130964 :   int parm_kind;
    6723                 :      130964 :   tree type;
    6724                 :      130964 :   tree var;
    6725                 :      130964 :   tree len;
    6726                 :      130964 :   tree base_object;
    6727                 :      130964 :   vec<tree, va_gc> *stringargs;
    6728                 :      130964 :   vec<tree, va_gc> *optionalargs;
    6729                 :      130964 :   tree result = NULL;
    6730                 :      130964 :   gfc_formal_arglist *formal;
    6731                 :      130964 :   gfc_actual_arglist *arg;
    6732                 :      130964 :   int has_alternate_specifier = 0;
    6733                 :      130964 :   bool need_interface_mapping;
    6734                 :      130964 :   bool is_builtin;
    6735                 :      130964 :   bool callee_alloc;
    6736                 :      130964 :   bool ulim_copy;
    6737                 :      130964 :   gfc_typespec ts;
    6738                 :      130964 :   gfc_charlen cl;
    6739                 :      130964 :   gfc_expr *e;
    6740                 :      130964 :   gfc_symbol *fsym;
    6741                 :      130964 :   enum {MISSING = 0, ELEMENTAL, SCALAR, SCALAR_POINTER, ARRAY};
    6742                 :      130964 :   gfc_component *comp = NULL;
    6743                 :      130964 :   int arglen;
    6744                 :      130964 :   unsigned int argc;
    6745                 :      130964 :   tree arg1_cntnr = NULL_TREE;
    6746                 :      130964 :   arglist = NULL;
    6747                 :      130964 :   retargs = NULL;
    6748                 :      130964 :   stringargs = NULL;
    6749                 :      130964 :   optionalargs = NULL;
    6750                 :      130964 :   var = NULL_TREE;
    6751                 :      130964 :   len = NULL_TREE;
    6752                 :      130964 :   gfc_clear_ts (&ts);
    6753                 :      130964 :   gfc_intrinsic_sym *isym = expr && expr->rank ?
    6754                 :             :                             expr->value.function.isym : NULL;
    6755                 :             : 
    6756                 :      130964 :   comp = gfc_get_proc_ptr_comp (expr);
    6757                 :             : 
    6758                 :      261928 :   bool elemental_proc = (comp
    6759                 :        1842 :                          && comp->ts.interface
    6760                 :        1789 :                          && comp->ts.interface->attr.elemental)
    6761                 :        1649 :                         || (comp && comp->attr.elemental)
    6762                 :      132613 :                         || sym->attr.elemental;
    6763                 :             : 
    6764                 :      130964 :   if (se->ss != NULL)
    6765                 :             :     {
    6766                 :       23683 :       if (!elemental_proc)
    6767                 :             :         {
    6768                 :       20395 :           gcc_assert (se->ss->info->type == GFC_SS_FUNCTION);
    6769                 :       20395 :           if (se->ss->info->useflags)
    6770                 :             :             {
    6771                 :        5705 :               gcc_assert ((!comp && gfc_return_by_reference (sym)
    6772                 :             :                            && sym->result->attr.dimension)
    6773                 :             :                           || (comp && comp->attr.dimension)
    6774                 :             :                           || gfc_is_class_array_function (expr));
    6775                 :        5705 :               gcc_assert (se->loop != NULL);
    6776                 :             :               /* Access the previously obtained result.  */
    6777                 :        5705 :               gfc_conv_tmp_array_ref (se);
    6778                 :        5705 :               return 0;
    6779                 :             :             }
    6780                 :             :         }
    6781                 :       17978 :       info = &se->ss->info->data.array;
    6782                 :             :     }
    6783                 :             :   else
    6784                 :             :     info = NULL;
    6785                 :             : 
    6786                 :      125259 :   stmtblock_t post, clobbers, dealloc_blk;
    6787                 :      125259 :   gfc_init_block (&post);
    6788                 :      125259 :   gfc_init_block (&clobbers);
    6789                 :      125259 :   gfc_init_block (&dealloc_blk);
    6790                 :      125259 :   gfc_init_interface_mapping (&mapping);
    6791                 :      125259 :   if (!comp)
    6792                 :             :     {
    6793                 :      123466 :       formal = gfc_sym_get_dummy_args (sym);
    6794                 :      123466 :       need_interface_mapping = sym->attr.dimension ||
    6795                 :      109022 :                                (sym->ts.type == BT_CHARACTER
    6796                 :        3031 :                                 && sym->ts.u.cl->length
    6797                 :        2379 :                                 && sym->ts.u.cl->length->expr_type
    6798                 :             :                                    != EXPR_CONSTANT);
    6799                 :             :     }
    6800                 :             :   else
    6801                 :             :     {
    6802                 :        1793 :       formal = comp->ts.interface ? comp->ts.interface->formal : NULL;
    6803                 :        1793 :       need_interface_mapping = comp->attr.dimension ||
    6804                 :        1724 :                                (comp->ts.type == BT_CHARACTER
    6805                 :          67 :                                 && comp->ts.u.cl->length
    6806                 :          58 :                                 && comp->ts.u.cl->length->expr_type
    6807                 :             :                                    != EXPR_CONSTANT);
    6808                 :             :     }
    6809                 :             : 
    6810                 :      125259 :   base_object = NULL_TREE;
    6811                 :             :   /* For _vprt->_copy () routines no formal symbol is present.  Nevertheless
    6812                 :             :      is the third and fourth argument to such a function call a value
    6813                 :             :      denoting the number of elements to copy (i.e., most of the time the
    6814                 :             :      length of a deferred length string).  */
    6815                 :      250518 :   ulim_copy = (formal == NULL)
    6816                 :       30893 :                && UNLIMITED_POLY (sym)
    6817                 :      125338 :                && comp && (strcmp ("_copy", comp->name) == 0);
    6818                 :             : 
    6819                 :             :   /* Scan for allocatable actual arguments passed to allocatable dummy
    6820                 :             :      arguments with INTENT(OUT).  As the corresponding actual arguments are
    6821                 :             :      deallocated before execution of the procedure, we evaluate actual
    6822                 :             :      argument expressions to avoid problems with possible dependencies.  */
    6823                 :      125259 :   bool force_eval_args = false;
    6824                 :      125259 :   gfc_formal_arglist *tmp_formal;
    6825                 :      386484 :   for (arg = args, tmp_formal = formal; arg != NULL;
    6826                 :      227915 :        arg = arg->next, tmp_formal = tmp_formal ? tmp_formal->next : NULL)
    6827                 :             :     {
    6828                 :      261712 :       e = arg->expr;
    6829                 :      261712 :       fsym = tmp_formal ? tmp_formal->sym : NULL;
    6830                 :      247909 :       if (e && fsym
    6831                 :      216026 :           && e->expr_type == EXPR_VARIABLE
    6832                 :       94090 :           && fsym->attr.intent == INTENT_OUT
    6833                 :        6191 :           && (fsym->ts.type == BT_CLASS && fsym->attr.class_ok
    6834                 :        6191 :               ? CLASS_DATA (fsym)->attr.allocatable
    6835                 :        4717 :               : fsym->attr.allocatable)
    6836                 :         487 :           && e->symtree
    6837                 :         487 :           && e->symtree->n.sym
    6838                 :      509621 :           && gfc_variable_attr (e, NULL).allocatable)
    6839                 :             :         {
    6840                 :             :           force_eval_args = true;
    6841                 :             :           break;
    6842                 :             :         }
    6843                 :             :     }
    6844                 :             : 
    6845                 :             :   /* Evaluate the arguments.  */
    6846                 :      387361 :   for (arg = args, argc = 0; arg != NULL;
    6847                 :      262102 :        arg = arg->next, formal = formal ? formal->next : NULL, ++argc)
    6848                 :             :     {
    6849                 :      262102 :       bool finalized = false;
    6850                 :      262102 :       tree derived_array = NULL_TREE;
    6851                 :      262102 :       symbol_attribute *attr;
    6852                 :             : 
    6853                 :      262102 :       e = arg->expr;
    6854                 :      262102 :       fsym = formal ? formal->sym : NULL;
    6855                 :      490894 :       parm_kind = MISSING;
    6856                 :             : 
    6857                 :      228792 :       attr = fsym ? &(fsym->ts.type == BT_CLASS ? CLASS_DATA (fsym)->attr
    6858                 :             :                                                 : fsym->attr)
    6859                 :             :                   : nullptr;
    6860                 :             :       /* If the procedure requires an explicit interface, the actual
    6861                 :             :          argument is passed according to the corresponding formal
    6862                 :             :          argument.  If the corresponding formal argument is a POINTER,
    6863                 :             :          ALLOCATABLE or assumed shape, we do not use g77's calling
    6864                 :             :          convention, and pass the address of the array descriptor
    6865                 :             :          instead.  Otherwise we use g77's calling convention, in other words
    6866                 :             :          pass the array data pointer without descriptor.  */
    6867                 :      228739 :       bool nodesc_arg = fsym != NULL
    6868                 :      228739 :                         && !(fsym->attr.pointer || fsym->attr.allocatable)
    6869                 :      219821 :                         && fsym->as
    6870                 :       37990 :                         && fsym->as->type != AS_ASSUMED_SHAPE
    6871                 :       22894 :                         && fsym->as->type != AS_ASSUMED_RANK;
    6872                 :      262102 :       if (comp)
    6873                 :        2697 :         nodesc_arg = nodesc_arg || !comp->attr.always_explicit;
    6874                 :             :       else
    6875                 :      259405 :         nodesc_arg
    6876                 :             :           = nodesc_arg
    6877                 :      259405 :             || !(sym->attr.always_explicit || (attr && attr->codimension));
    6878                 :             : 
    6879                 :             :       /* Class array expressions are sometimes coming completely unadorned
    6880                 :             :          with either arrayspec or _data component.  Correct that here.
    6881                 :             :          OOP-TODO: Move this to the frontend.  */
    6882                 :      262102 :       if (e && e->expr_type == EXPR_VARIABLE
    6883                 :      108196 :             && !e->ref
    6884                 :       49901 :             && e->ts.type == BT_CLASS
    6885                 :        2593 :             && (CLASS_DATA (e)->attr.codimension
    6886                 :        2593 :                 || CLASS_DATA (e)->attr.dimension))
    6887                 :             :         {
    6888                 :           0 :           gfc_typespec temp_ts = e->ts;
    6889                 :           0 :           gfc_add_class_array_ref (e);
    6890                 :           0 :           e->ts = temp_ts;
    6891                 :             :         }
    6892                 :             : 
    6893                 :      262102 :       if (e == NULL
    6894                 :      248293 :           || (e->expr_type == EXPR_NULL
    6895                 :         745 :               && fsym
    6896                 :             :               && fsym->attr.value
    6897                 :             :               && fsym->attr.optional
    6898                 :         745 :               && !fsym->attr.dimension
    6899                 :          72 :               && fsym->ts.type != BT_CLASS))
    6900                 :             :         {
    6901                 :       13881 :           if (se->ignore_optional)
    6902                 :             :             {
    6903                 :             :               /* Some intrinsics have already been resolved to the correct
    6904                 :             :                  parameters.  */
    6905                 :         422 :               continue;
    6906                 :             :             }
    6907                 :       13683 :           else if (arg->label)
    6908                 :             :             {
    6909                 :         224 :               has_alternate_specifier = 1;
    6910                 :         224 :               continue;
    6911                 :             :             }
    6912                 :             :           else
    6913                 :             :             {
    6914                 :       13459 :               gfc_init_se (&parmse, NULL);
    6915                 :             : 
    6916                 :             :               /* For scalar arguments with VALUE attribute which are passed by
    6917                 :             :                  value, pass "0" and a hidden argument gives the optional
    6918                 :             :                  status.  */
    6919                 :       13459 :               if (fsym && fsym->attr.optional && fsym->attr.value
    6920                 :       12401 :                   && !fsym->attr.dimension && fsym->ts.type != BT_CLASS)
    6921                 :             :                 {
    6922                 :         427 :                   conv_dummy_value (&parmse, e, fsym, optionalargs);
    6923                 :             :                 }
    6924                 :             :               else
    6925                 :             :                 {
    6926                 :             :                   /* Pass a NULL pointer for an absent arg.  */
    6927                 :       13032 :                   parmse.expr = null_pointer_node;
    6928                 :             : 
    6929                 :             :                   /* Is it an absent character dummy?  */
    6930                 :       13032 :                   bool absent_char = false;
    6931                 :       13032 :                   gfc_dummy_arg * const dummy_arg = arg->associated_dummy;
    6932                 :             : 
    6933                 :             :                   /* Fall back to inferred type only if no formal.  */
    6934                 :       13032 :                   if (fsym)
    6935                 :       11974 :                     absent_char = (fsym->ts.type == BT_CHARACTER);
    6936                 :        1058 :                   else if (dummy_arg)
    6937                 :        1058 :                     absent_char = (gfc_dummy_arg_get_typespec (*dummy_arg).type
    6938                 :             :                                    == BT_CHARACTER);
    6939                 :       13032 :                   if (absent_char)
    6940                 :        1115 :                     parmse.string_length = build_int_cst (gfc_charlen_type_node,
    6941                 :        1115 :                                                           0);
    6942                 :             :                 }
    6943                 :             :             }
    6944                 :             :         }
    6945                 :      248221 :       else if (e->expr_type == EXPR_NULL
    6946                 :         673 :                && (e->ts.type == BT_UNKNOWN || e->ts.type == BT_DERIVED)
    6947                 :         371 :                && fsym && attr && (attr->pointer || attr->allocatable)
    6948                 :         293 :                && fsym->ts.type == BT_DERIVED)
    6949                 :             :         {
    6950                 :         210 :           gfc_init_se (&parmse, NULL);
    6951                 :         210 :           gfc_conv_expr_reference (&parmse, e);
    6952                 :         210 :           conv_null_actual (&parmse, e, fsym);
    6953                 :             :         }
    6954                 :      248011 :       else if (arg->expr->expr_type == EXPR_NULL
    6955                 :         463 :                && fsym && !fsym->attr.pointer
    6956                 :         163 :                && (fsym->ts.type != BT_CLASS
    6957                 :           6 :                    || !CLASS_DATA (fsym)->attr.class_pointer))
    6958                 :             :         {
    6959                 :             :           /* Pass a NULL pointer to denote an absent arg.  */
    6960                 :         163 :           gcc_assert (fsym->attr.optional && !fsym->attr.allocatable
    6961                 :             :                       && (fsym->ts.type != BT_CLASS
    6962                 :             :                           || !CLASS_DATA (fsym)->attr.allocatable));
    6963                 :         163 :           gfc_init_se (&parmse, NULL);
    6964                 :         163 :           parmse.expr = null_pointer_node;
    6965                 :         163 :           if (fsym->ts.type == BT_CHARACTER)
    6966                 :          42 :             parmse.string_length = build_int_cst (gfc_charlen_type_node, 0);
    6967                 :             :         }
    6968                 :      247848 :       else if (fsym && fsym->ts.type == BT_CLASS
    6969                 :       10472 :                  && e->ts.type == BT_DERIVED)
    6970                 :             :         {
    6971                 :             :           /* The derived type needs to be converted to a temporary
    6972                 :             :              CLASS object.  */
    6973                 :        4180 :           gfc_init_se (&parmse, se);
    6974                 :        4180 :           gfc_conv_derived_to_class (&parmse, e, fsym, NULL_TREE,
    6975                 :        4180 :                                      fsym->attr.optional
    6976                 :        1008 :                                        && e->expr_type == EXPR_VARIABLE
    6977                 :        1008 :                                        && e->symtree->n.sym->attr.optional,
    6978                 :        4180 :                                      CLASS_DATA (fsym)->attr.class_pointer
    6979                 :        4180 :                                        || CLASS_DATA (fsym)->attr.allocatable,
    6980                 :             :                                      sym->name, &derived_array);
    6981                 :             :         }
    6982                 :      211785 :       else if (UNLIMITED_POLY (fsym) && e->ts.type != BT_CLASS
    6983                 :         870 :                && e->ts.type != BT_PROCEDURE
    6984                 :         858 :                && (gfc_expr_attr (e).flavor != FL_PROCEDURE
    6985                 :          12 :                    || gfc_expr_attr (e).proc != PROC_UNKNOWN))
    6986                 :             :         {
    6987                 :             :           /* The intrinsic type needs to be converted to a temporary
    6988                 :             :              CLASS object for the unlimited polymorphic formal.  */
    6989                 :         858 :           gfc_find_vtab (&e->ts);
    6990                 :         858 :           gfc_init_se (&parmse, se);
    6991                 :         858 :           gfc_conv_intrinsic_to_class (&parmse, e, fsym->ts);
    6992                 :             : 
    6993                 :             :         }
    6994                 :      242810 :       else if (se->ss && se->ss->info->useflags)
    6995                 :             :         {
    6996                 :        5518 :           gfc_ss *ss;
    6997                 :             : 
    6998                 :        5518 :           ss = se->ss;
    6999                 :             : 
    7000                 :             :           /* An elemental function inside a scalarized loop.  */
    7001                 :        5518 :           gfc_init_se (&parmse, se);
    7002                 :        5518 :           parm_kind = ELEMENTAL;
    7003                 :             : 
    7004                 :             :           /* When no fsym is present, ulim_copy is set and this is a third or
    7005                 :             :              fourth argument, use call-by-value instead of by reference to
    7006                 :             :              hand the length properties to the copy routine (i.e., most of the
    7007                 :             :              time this will be a call to a __copy_character_* routine where the
    7008                 :             :              third and fourth arguments are the lengths of a deferred length
    7009                 :             :              char array).  */
    7010                 :        5518 :           if ((fsym && fsym->attr.value)
    7011                 :        5284 :               || (ulim_copy && (argc == 2 || argc == 3)))
    7012                 :         234 :             gfc_conv_expr (&parmse, e);
    7013                 :        5284 :           else if (e->expr_type == EXPR_ARRAY)
    7014                 :             :             {
    7015                 :         294 :               gfc_conv_expr (&parmse, e);
    7016                 :         294 :               if (e->ts.type != BT_CHARACTER)
    7017                 :         251 :                 parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
    7018                 :             :             }
    7019                 :             :           else
    7020                 :        4990 :             gfc_conv_expr_reference (&parmse, e);
    7021                 :             : 
    7022                 :        5518 :           if (e->ts.type == BT_CHARACTER && !e->rank
    7023                 :         174 :               && e->expr_type == EXPR_FUNCTION)
    7024                 :          12 :             parmse.expr = build_fold_indirect_ref_loc (input_location,
    7025                 :             :                                                        parmse.expr);
    7026                 :             : 
    7027                 :        5468 :           if (fsym && fsym->ts.type == BT_DERIVED
    7028                 :        6858 :               && gfc_is_class_container_ref (e))
    7029                 :             :             {
    7030                 :          24 :               parmse.expr = gfc_class_data_get (parmse.expr);
    7031                 :             : 
    7032                 :          24 :               if (fsym->attr.optional && e->expr_type == EXPR_VARIABLE
    7033                 :          24 :                   && e->symtree->n.sym->attr.optional)
    7034                 :             :                 {
    7035                 :           0 :                   tree cond = gfc_conv_expr_present (e->symtree->n.sym);
    7036                 :           0 :                   parmse.expr = build3_loc (input_location, COND_EXPR,
    7037                 :           0 :                                         TREE_TYPE (parmse.expr),
    7038                 :             :                                         cond, parmse.expr,
    7039                 :           0 :                                         fold_convert (TREE_TYPE (parmse.expr),
    7040                 :             :                                                       null_pointer_node));
    7041                 :             :                 }
    7042                 :             :             }
    7043                 :             : 
    7044                 :             :           /* Scalar dummy arguments of intrinsic type or derived type with
    7045                 :             :              VALUE attribute.  */
    7046                 :        5518 :           if (fsym
    7047                 :        5468 :               && fsym->attr.value
    7048                 :         234 :               && fsym->ts.type != BT_CLASS)
    7049                 :         234 :             conv_dummy_value (&parmse, e, fsym, optionalargs);
    7050                 :             : 
    7051                 :             :           /* If we are passing an absent array as optional dummy to an
    7052                 :             :              elemental procedure, make sure that we pass NULL when the data
    7053                 :             :              pointer is NULL.  We need this extra conditional because of
    7054                 :             :              scalarization which passes arrays elements to the procedure,
    7055                 :             :              ignoring the fact that the array can be absent/unallocated/...  */
    7056                 :        5284 :           else if (ss->info->can_be_null_ref
    7057                 :         415 :                    && ss->info->type != GFC_SS_REFERENCE)
    7058                 :             :             {
    7059                 :         193 :               tree descriptor_data;
    7060                 :             : 
    7061                 :         193 :               descriptor_data = ss->info->data.array.data;
    7062                 :         193 :               tmp = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    7063                 :             :                                      descriptor_data,
    7064                 :         193 :                                      fold_convert (TREE_TYPE (descriptor_data),
    7065                 :             :                                                    null_pointer_node));
    7066                 :         193 :               parmse.expr
    7067                 :         386 :                 = fold_build3_loc (input_location, COND_EXPR,
    7068                 :         193 :                                    TREE_TYPE (parmse.expr),
    7069                 :             :                                    gfc_unlikely (tmp, PRED_FORTRAN_ABSENT_DUMMY),
    7070                 :         193 :                                    fold_convert (TREE_TYPE (parmse.expr),
    7071                 :             :                                                  null_pointer_node),
    7072                 :             :                                    parmse.expr);
    7073                 :             :             }
    7074                 :             : 
    7075                 :             :           /* The scalarizer does not repackage the reference to a class
    7076                 :             :              array - instead it returns a pointer to the data element.  */
    7077                 :        5518 :           if (fsym && fsym->ts.type == BT_CLASS && e->ts.type == BT_CLASS)
    7078                 :         156 :             gfc_conv_class_to_class (&parmse, e, fsym->ts, true,
    7079                 :         156 :                                      fsym->attr.intent != INTENT_IN
    7080                 :         156 :                                      && (CLASS_DATA (fsym)->attr.class_pointer
    7081                 :             :                                          || CLASS_DATA (fsym)->attr.allocatable),
    7082                 :         156 :                                      fsym->attr.optional
    7083                 :           0 :                                      && e->expr_type == EXPR_VARIABLE
    7084                 :           0 :                                      && e->symtree->n.sym->attr.optional,
    7085                 :         156 :                                      CLASS_DATA (fsym)->attr.class_pointer
    7086                 :         156 :                                      || CLASS_DATA (fsym)->attr.allocatable);
    7087                 :             :         }
    7088                 :             :       else
    7089                 :             :         {
    7090                 :      237292 :           bool scalar;
    7091                 :      237292 :           gfc_ss *argss;
    7092                 :             : 
    7093                 :      237292 :           gfc_init_se (&parmse, NULL);
    7094                 :             : 
    7095                 :             :           /* Check whether the expression is a scalar or not; we cannot use
    7096                 :             :              e->rank as it can be nonzero for functions arguments.  */
    7097                 :      237292 :           argss = gfc_walk_expr (e);
    7098                 :      237292 :           scalar = argss == gfc_ss_terminator;
    7099                 :      237292 :           if (!scalar)
    7100                 :       57953 :             gfc_free_ss_chain (argss);
    7101                 :             : 
    7102                 :             :           /* Special handling for passing scalar polymorphic coarrays;
    7103                 :             :              otherwise one passes "class->_data.data" instead of "&class".  */
    7104                 :      237292 :           if (e->rank == 0 && e->ts.type == BT_CLASS
    7105                 :        3518 :               && fsym && fsym->ts.type == BT_CLASS
    7106                 :        3096 :               && CLASS_DATA (fsym)->attr.codimension
    7107                 :        3096 :               && !CLASS_DATA (fsym)->attr.dimension)
    7108                 :             :             {
    7109                 :          51 :               gfc_add_class_array_ref (e);
    7110                 :          51 :               parmse.want_coarray = 1;
    7111                 :          51 :               scalar = false;
    7112                 :             :             }
    7113                 :             : 
    7114                 :             :           /* A scalar or transformational function.  */
    7115                 :      237292 :           if (scalar)
    7116                 :             :             {
    7117                 :      179288 :               if (e->expr_type == EXPR_VARIABLE
    7118                 :       53391 :                     && e->symtree->n.sym->attr.cray_pointee
    7119                 :         390 :                     && fsym && fsym->attr.flavor == FL_PROCEDURE)
    7120                 :             :                 {
    7121                 :             :                     /* The Cray pointer needs to be converted to a pointer to
    7122                 :             :                        a type given by the expression.  */
    7123                 :           6 :                     gfc_conv_expr (&parmse, e);
    7124                 :           6 :                     type = build_pointer_type (TREE_TYPE (parmse.expr));
    7125                 :           6 :                     tmp = gfc_get_symbol_decl (e->symtree->n.sym->cp_pointer);
    7126                 :           6 :                     parmse.expr = convert (type, tmp);
    7127                 :             :                 }
    7128                 :             : 
    7129                 :      179282 :               else if (sym->attr.is_bind_c && e && is_CFI_desc (fsym, NULL))
    7130                 :             :                 /* Implement F2018, 18.3.6, list item (5), bullet point 2.  */
    7131                 :         686 :                 gfc_conv_gfc_desc_to_cfi_desc (&parmse, e, fsym);
    7132                 :             : 
    7133                 :      178596 :               else if (fsym && fsym->attr.value)
    7134                 :             :                 {
    7135                 :       21596 :                   if (fsym->ts.type == BT_CHARACTER
    7136                 :         537 :                       && fsym->ts.is_c_interop
    7137                 :         180 :                       && fsym->ns->proc_name != NULL
    7138                 :         180 :                       && fsym->ns->proc_name->attr.is_bind_c)
    7139                 :             :                     {
    7140                 :         171 :                       parmse.expr = NULL;
    7141                 :         171 :                       conv_scalar_char_value (fsym, &parmse, &e);
    7142                 :         171 :                       if (parmse.expr == NULL)
    7143                 :         165 :                         gfc_conv_expr (&parmse, e);
    7144                 :             :                     }
    7145                 :             :                   else
    7146                 :             :                     {
    7147                 :       21425 :                       gfc_conv_expr (&parmse, e);
    7148                 :       21425 :                       conv_dummy_value (&parmse, e, fsym, optionalargs);
    7149                 :             :                     }
    7150                 :             :                 }
    7151                 :             : 
    7152                 :      157000 :               else if (arg->name && arg->name[0] == '%')
    7153                 :             :                 /* Argument list functions %VAL, %LOC and %REF are signalled
    7154                 :             :                    through arg->name.  */
    7155                 :        4550 :                 conv_arglist_function (&parmse, arg->expr, arg->name);
    7156                 :      152450 :               else if ((e->expr_type == EXPR_FUNCTION)
    7157                 :        8159 :                         && ((e->value.function.esym
    7158                 :        2141 :                              && e->value.function.esym->result->attr.pointer)
    7159                 :        8064 :                             || (!e->value.function.esym
    7160                 :        6018 :                                 && e->symtree->n.sym->attr.pointer))
    7161                 :          95 :                         && fsym && fsym->attr.target)
    7162                 :             :                 /* Make sure the function only gets called once.  */
    7163                 :           8 :                 gfc_conv_expr_reference (&parmse, e);
    7164                 :      152442 :               else if (e->expr_type == EXPR_FUNCTION
    7165                 :        8151 :                        && e->symtree->n.sym->result
    7166                 :        7122 :                        && e->symtree->n.sym->result != e->symtree->n.sym
    7167                 :         136 :                        && e->symtree->n.sym->result->attr.proc_pointer)
    7168                 :             :                 {
    7169                 :             :                   /* Functions returning procedure pointers.  */
    7170                 :          18 :                   gfc_conv_expr (&parmse, e);
    7171                 :          18 :                   if (fsym && fsym->attr.proc_pointer)
    7172                 :           6 :                     parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
    7173                 :             :                 }
    7174                 :             : 
    7175                 :             :               else
    7176                 :             :                 {
    7177                 :      152424 :                   bool defer_to_dealloc_blk = false;
    7178                 :      152424 :                   if (e->ts.type == BT_CLASS && fsym
    7179                 :        3455 :                       && fsym->ts.type == BT_CLASS
    7180                 :        3033 :                       && (!CLASS_DATA (fsym)->as
    7181                 :         356 :                           || CLASS_DATA (fsym)->as->type != AS_ASSUMED_RANK)
    7182                 :        2677 :                       && CLASS_DATA (e)->attr.codimension)
    7183                 :             :                     {
    7184                 :          48 :                       gcc_assert (!CLASS_DATA (fsym)->attr.codimension);
    7185                 :          48 :                       gcc_assert (!CLASS_DATA (fsym)->as);
    7186                 :          48 :                       gfc_add_class_array_ref (e);
    7187                 :          48 :                       parmse.want_coarray = 1;
    7188                 :          48 :                       gfc_conv_expr_reference (&parmse, e);
    7189                 :          48 :                       class_scalar_coarray_to_class (&parmse, e, fsym->ts,
    7190                 :          48 :                                      fsym->attr.optional
    7191                 :          48 :                                      && e->expr_type == EXPR_VARIABLE);
    7192                 :             :                     }
    7193                 :      152376 :                   else if (e->ts.type == BT_CLASS && fsym
    7194                 :        3407 :                            && fsym->ts.type == BT_CLASS
    7195                 :        2985 :                            && !CLASS_DATA (fsym)->as
    7196                 :        2629 :                            && !CLASS_DATA (e)->as
    7197                 :        2519 :                            && strcmp (fsym->ts.u.derived->name,
    7198                 :             :                                       e->ts.u.derived->name))
    7199                 :             :                     {
    7200                 :        1602 :                       type = gfc_typenode_for_spec (&fsym->ts);
    7201                 :        1602 :                       var = gfc_create_var (type, fsym->name);
    7202                 :        1602 :                       gfc_conv_expr (&parmse, e);
    7203                 :        1602 :                       if (fsym->attr.optional
    7204                 :         153 :                           && e->expr_type == EXPR_VARIABLE
    7205                 :         153 :                           && e->symtree->n.sym->attr.optional)
    7206                 :             :                         {
    7207                 :          66 :                           stmtblock_t block;
    7208                 :          66 :                           tree cond;
    7209                 :          66 :                           tmp = gfc_build_addr_expr (NULL_TREE, parmse.expr);
    7210                 :          66 :                           cond = fold_build2_loc (input_location, NE_EXPR,
    7211                 :             :                                                   logical_type_node, tmp,
    7212                 :          66 :                                                   fold_convert (TREE_TYPE (tmp),
    7213                 :             :                                                             null_pointer_node));
    7214                 :          66 :                           gfc_start_block (&block);
    7215                 :          66 :                           gfc_add_modify (&block, var,
    7216                 :             :                                           fold_build1_loc (input_location,
    7217                 :             :                                                            VIEW_CONVERT_EXPR,
    7218                 :             :                                                            type, parmse.expr));
    7219                 :          66 :                           gfc_add_expr_to_block (&parmse.pre,
    7220                 :             :                                  fold_build3_loc (input_location,
    7221                 :             :                                          COND_EXPR, void_type_node,
    7222                 :             :                                          cond, gfc_finish_block (&block),
    7223                 :             :                                          build_empty_stmt (input_location)));
    7224                 :          66 :                           parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
    7225                 :         132 :                           parmse.expr = build3_loc (input_location, COND_EXPR,
    7226                 :          66 :                                          TREE_TYPE (parmse.expr),
    7227                 :             :                                          cond, parmse.expr,
    7228                 :          66 :                                          fold_convert (TREE_TYPE (parmse.expr),
    7229                 :             :                                                        null_pointer_node));
    7230                 :          66 :                         }
    7231                 :             :                       else
    7232                 :             :                         {
    7233                 :             :                           /* Since the internal representation of unlimited
    7234                 :             :                              polymorphic expressions includes an extra field
    7235                 :             :                              that other class objects do not, a cast to the
    7236                 :             :                              formal type does not work.  */
    7237                 :        1536 :                           if (!UNLIMITED_POLY (e) && UNLIMITED_POLY (fsym))
    7238                 :             :                             {
    7239                 :          91 :                               tree efield;
    7240                 :             : 
    7241                 :             :                               /* Evaluate arguments just once, when they have
    7242                 :             :                                  side effects.  */
    7243                 :          91 :                               if (TREE_SIDE_EFFECTS (parmse.expr))
    7244                 :             :                                 {
    7245                 :          25 :                                   tree cldata, zero;
    7246                 :             : 
    7247                 :          25 :                                   parmse.expr = gfc_evaluate_now (parmse.expr,
    7248                 :             :                                                                   &parmse.pre);
    7249                 :             : 
    7250                 :             :                                   /* Prevent memory leak, when old component
    7251                 :             :                                      was allocated already.  */
    7252                 :          25 :                                   cldata = gfc_class_data_get (parmse.expr);
    7253                 :          25 :                                   zero = build_int_cst (TREE_TYPE (cldata),
    7254                 :          25 :                                                         0);
    7255                 :          25 :                                   tmp = fold_build2_loc (input_location, NE_EXPR,
    7256                 :             :                                                          logical_type_node,
    7257                 :             :                                                          cldata, zero);
    7258                 :          25 :                                   tmp = build3_v (COND_EXPR, tmp,
    7259                 :             :                                                   gfc_call_free (cldata),
    7260                 :             :                                                   build_empty_stmt (
    7261                 :             :                                                     input_location));
    7262                 :          25 :                                   gfc_add_expr_to_block (&parmse.finalblock,
    7263                 :             :                                                          tmp);
    7264                 :          25 :                                   gfc_add_modify (&parmse.finalblock,
    7265                 :             :                                                   cldata, zero);
    7266                 :             :                                 }
    7267                 :             : 
    7268                 :             :                               /* Set the _data field.  */
    7269                 :          91 :                               tmp = gfc_class_data_get (var);
    7270                 :          91 :                               efield = fold_convert (TREE_TYPE (tmp),
    7271                 :             :                                         gfc_class_data_get (parmse.expr));
    7272                 :          91 :                               gfc_add_modify (&parmse.pre, tmp, efield);
    7273                 :             : 
    7274                 :             :                               /* Set the _vptr field.  */
    7275                 :          91 :                               tmp = gfc_class_vptr_get (var);
    7276                 :          91 :                               efield = fold_convert (TREE_TYPE (tmp),
    7277                 :             :                                         gfc_class_vptr_get (parmse.expr));
    7278                 :          91 :                               gfc_add_modify (&parmse.pre, tmp, efield);
    7279                 :             : 
    7280                 :             :                               /* Set the _len field.  */
    7281                 :          91 :                               tmp = gfc_class_len_get (var);
    7282                 :          91 :                               gfc_add_modify (&parmse.pre, tmp,
    7283                 :          91 :                                               build_int_cst (TREE_TYPE (tmp), 0));
    7284                 :          91 :                             }
    7285                 :             :                           else
    7286                 :             :                             {
    7287                 :        1445 :                               tmp = fold_build1_loc (input_location,
    7288                 :             :                                                      VIEW_CONVERT_EXPR,
    7289                 :             :                                                      type, parmse.expr);
    7290                 :        1445 :                               gfc_add_modify (&parmse.pre, var, tmp);
    7291                 :        1536 :                                               ;
    7292                 :             :                             }
    7293                 :        1536 :                           parmse.expr = gfc_build_addr_expr (NULL_TREE, var);
    7294                 :             :                         }
    7295                 :             :                     }
    7296                 :             :                   else
    7297                 :             :                     {
    7298                 :      150774 :                       gfc_conv_expr_reference (&parmse, e);
    7299                 :             : 
    7300                 :      150774 :                       gfc_symbol *dsym = fsym;
    7301                 :      150774 :                       gfc_dummy_arg *dummy;
    7302                 :             : 
    7303                 :             :                       /* Use associated dummy as fallback for formal
    7304                 :             :                          argument if there is no explicit interface.  */
    7305                 :      150774 :                       if (dsym == NULL
    7306                 :       27410 :                           && (dummy = arg->associated_dummy)
    7307                 :       24916 :                           && dummy->intrinsicness == GFC_NON_INTRINSIC_DUMMY_ARG
    7308                 :      174248 :                           && dummy->u.non_intrinsic->sym)
    7309                 :             :                         dsym = dummy->u.non_intrinsic->sym;
    7310                 :             : 
    7311                 :      150774 :                       if (dsym
    7312                 :      146838 :                           && dsym->attr.intent == INTENT_OUT
    7313                 :             :                           && !dsym->attr.allocatable
    7314                 :        3214 :                           && !dsym->attr.pointer
    7315                 :        3055 :                           && e->expr_type == EXPR_VARIABLE
    7316                 :        3054 :                           && e->ref == NULL
    7317                 :        2947 :                           && e->symtree
    7318                 :        2947 :                           && e->symtree->n.sym
    7319                 :        2947 :                           && !e->symtree->n.sym->attr.dimension
    7320                 :        2947 :                           && e->ts.type != BT_CHARACTER
    7321                 :        2845 :                           && e->ts.type != BT_CLASS
    7322                 :        2615 :                           && (e->ts.type != BT_DERIVED
    7323                 :         492 :                               || (dsym->ts.type == BT_DERIVED
    7324                 :         492 :                                   && e->ts.u.derived == dsym->ts.u.derived
    7325                 :             :                                   /* Types with allocatable components are
    7326                 :             :                                      excluded from clobbering because we need
    7327                 :             :                                      the unclobbered pointers to free the
    7328                 :             :                                      allocatable components in the callee.
    7329                 :             :                                      Same goes for finalizable types or types
    7330                 :             :                                      with finalizable components, we need to
    7331                 :             :                                      pass the unclobbered values to the
    7332                 :             :                                      finalization routines.
    7333                 :             :                                      For parameterized types, it's less clear
    7334                 :             :                                      but they may not have a constant size
    7335                 :             :                                      so better exclude them in any case.  */
    7336                 :             :                                   && !e->ts.u.derived->attr.alloc_comp
    7337                 :         477 :                                   && !e->ts.u.derived->attr.pdt_type
    7338                 :         351 :                                   && !gfc_is_finalizable (e->ts.u.derived, NULL)))
    7339                 :      153206 :                           && !sym->attr.elemental)
    7340                 :             :                         {
    7341                 :        1099 :                           tree var;
    7342                 :        1099 :                           var = build_fold_indirect_ref_loc (input_location,
    7343                 :             :                                                              parmse.expr);
    7344                 :        1099 :                           tree clobber = build_clobber (TREE_TYPE (var));
    7345                 :        1099 :                           gfc_add_modify (&clobbers, var, clobber);
    7346                 :             :                         }
    7347                 :             :                     }
    7348                 :             :                   /* Catch base objects that are not variables.  */
    7349                 :      152424 :                   if (e->ts.type == BT_CLASS
    7350                 :        3455 :                         && e->expr_type != EXPR_VARIABLE
    7351                 :         306 :                         && expr && e == expr->base_expr)
    7352                 :          80 :                     base_object = build_fold_indirect_ref_loc (input_location,
    7353                 :             :                                                                parmse.expr);
    7354                 :             : 
    7355                 :             :                   /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
    7356                 :             :                      allocated on entry, it must be deallocated.  */
    7357                 :      125014 :                   if (fsym && fsym->attr.intent == INTENT_OUT
    7358                 :        3143 :                       && (fsym->attr.allocatable
    7359                 :        3002 :                           || (fsym->ts.type == BT_CLASS
    7360                 :         259 :                               && CLASS_DATA (fsym)->attr.allocatable))
    7361                 :      152714 :                       && !is_CFI_desc (fsym, NULL))
    7362                 :             :                     {
    7363                 :         290 :                       stmtblock_t block;
    7364                 :         290 :                       tree ptr;
    7365                 :             : 
    7366                 :         290 :                       defer_to_dealloc_blk = true;
    7367                 :             : 
    7368                 :         290 :                       parmse.expr = gfc_evaluate_data_ref_now (parmse.expr,
    7369                 :             :                                                                &parmse.pre);
    7370                 :             : 
    7371                 :         290 :                       if (parmse.class_container != NULL_TREE)
    7372                 :         156 :                         parmse.class_container
    7373                 :         156 :                             = gfc_evaluate_data_ref_now (parmse.class_container,
    7374                 :             :                                                          &parmse.pre);
    7375                 :             : 
    7376                 :         290 :                       gfc_init_block  (&block);
    7377                 :         290 :                       ptr = parmse.expr;
    7378                 :         290 :                       if (e->ts.type == BT_CLASS)
    7379                 :         156 :                         ptr = gfc_class_data_get (ptr);
    7380                 :             : 
    7381                 :         290 :                       tree cls = parmse.class_container;
    7382                 :         290 :                       tmp = gfc_deallocate_scalar_with_status (ptr, NULL_TREE,
    7383                 :             :                                                                NULL_TREE, true,
    7384                 :             :                                                                e, e->ts, cls);
    7385                 :         290 :                       gfc_add_expr_to_block (&block, tmp);
    7386                 :         290 :                       gfc_add_modify (&block, ptr,
    7387                 :         290 :                                       fold_convert (TREE_TYPE (ptr),
    7388                 :             :                                                     null_pointer_node));
    7389                 :             : 
    7390                 :         290 :                       if (fsym->ts.type == BT_CLASS)
    7391                 :         149 :                         gfc_reset_vptr (&block, nullptr,
    7392                 :             :                                         build_fold_indirect_ref (parmse.expr),
    7393                 :             :                                         fsym->ts.u.derived);
    7394                 :             : 
    7395                 :         290 :                       if (fsym->attr.optional
    7396                 :          42 :                           && e->expr_type == EXPR_VARIABLE
    7397                 :          42 :                           && e->symtree->n.sym->attr.optional)
    7398                 :             :                         {
    7399                 :          36 :                           tmp = fold_build3_loc (input_location, COND_EXPR,
    7400                 :             :                                      void_type_node,
    7401                 :          18 :                                      gfc_conv_expr_present (e->symtree->n.sym),
    7402                 :             :                                             gfc_finish_block (&block),
    7403                 :             :                                             build_empty_stmt (input_location));
    7404                 :             :                         }
    7405                 :             :                       else
    7406                 :         272 :                         tmp = gfc_finish_block (&block);
    7407                 :             : 
    7408                 :         290 :                       gfc_add_expr_to_block (&dealloc_blk, tmp);
    7409                 :             :                     }
    7410                 :             : 
    7411                 :             :                   /* A class array element needs converting back to be a
    7412                 :             :                      class object, if the formal argument is a class object.  */
    7413                 :      152424 :                   if (fsym && fsym->ts.type == BT_CLASS
    7414                 :        3045 :                         && e->ts.type == BT_CLASS
    7415                 :        3033 :                         && ((CLASS_DATA (fsym)->as
    7416                 :         356 :                              && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
    7417                 :        2677 :                             || CLASS_DATA (e)->attr.dimension))
    7418                 :             :                     {
    7419                 :         466 :                       gfc_se class_se = parmse;
    7420                 :         466 :                       gfc_init_block (&class_se.pre);
    7421                 :         466 :                       gfc_init_block (&class_se.post);
    7422                 :             : 
    7423                 :         466 :                       gfc_conv_class_to_class (&class_se, e, fsym->ts, false,
    7424                 :         466 :                                      fsym->attr.intent != INTENT_IN
    7425                 :         466 :                                      && (CLASS_DATA (fsym)->attr.class_pointer
    7426                 :             :                                          || CLASS_DATA (fsym)->attr.allocatable),
    7427                 :         466 :                                      fsym->attr.optional
    7428                 :         198 :                                      && e->expr_type == EXPR_VARIABLE
    7429                 :         198 :                                      && e->symtree->n.sym->attr.optional,
    7430                 :         466 :                                      CLASS_DATA (fsym)->attr.class_pointer
    7431                 :         466 :                                      || CLASS_DATA (fsym)->attr.allocatable);
    7432                 :             : 
    7433                 :         466 :                       parmse.expr = class_se.expr;
    7434                 :         932 :                       stmtblock_t *class_pre_block = defer_to_dealloc_blk
    7435                 :         466 :                                                      ? &dealloc_blk
    7436                 :             :                                                      : &parmse.pre;
    7437                 :         466 :                       gfc_add_block_to_block (class_pre_block, &class_se.pre);
    7438                 :         466 :                       gfc_add_block_to_block (&parmse.post, &class_se.post);
    7439                 :             :                     }
    7440                 :             : 
    7441                 :      125014 :                   if (fsym && (fsym->ts.type == BT_DERIVED
    7442                 :      113433 :                                || fsym->ts.type == BT_ASSUMED)
    7443                 :       12432 :                       && e->ts.type == BT_CLASS
    7444                 :         410 :                       && !CLASS_DATA (e)->attr.dimension
    7445                 :         410 :                       && !CLASS_DATA (e)->attr.codimension)
    7446                 :             :                     {
    7447                 :         374 :                       parmse.expr = gfc_class_data_get (parmse.expr);
    7448                 :             :                       /* The result is a class temporary, whose _data component
    7449                 :             :                          must be freed to avoid a memory leak.  */
    7450                 :         374 :                       if (e->expr_type == EXPR_FUNCTION
    7451                 :          23 :                           && CLASS_DATA (e)->attr.allocatable)
    7452                 :             :                         {
    7453                 :          19 :                           tree zero;
    7454                 :             : 
    7455                 :             :                           /* Finalize the expression.  */
    7456                 :          19 :                           gfc_finalize_tree_expr (&parmse, NULL,
    7457                 :             :                                                   gfc_expr_attr (e), e->rank);
    7458                 :          19 :                           gfc_add_block_to_block (&parmse.post,
    7459                 :             :                                                   &parmse.finalblock);
    7460                 :             : 
    7461                 :             :                           /* Then free the class _data.  */
    7462                 :          19 :                           zero = build_int_cst (TREE_TYPE (parmse.expr), 0);
    7463                 :          19 :                           tmp = fold_build2_loc (input_location, NE_EXPR,
    7464                 :             :                                                  logical_type_node,
    7465                 :             :                                                  parmse.expr, zero);
    7466                 :          19 :                           tmp = build3_v (COND_EXPR, tmp,
    7467                 :             :                                           gfc_call_free (parmse.expr),
    7468                 :             :                                           build_empty_stmt (input_location));
    7469                 :          19 :                           gfc_add_expr_to_block (&parmse.post, tmp);
    7470                 :          19 :                           gfc_add_modify (&parmse.post, parmse.expr, zero);
    7471                 :             :                         }
    7472                 :             :                     }
    7473                 :             : 
    7474                 :             :                   /* Wrap scalar variable in a descriptor. We need to convert
    7475                 :             :                      the address of a pointer back to the pointer itself before,
    7476                 :             :                      we can assign it to the data field.  */
    7477                 :             : 
    7478                 :      125014 :                   if (fsym && fsym->as && fsym->as->type == AS_ASSUMED_RANK
    7479                 :        1293 :                       && fsym->ts.type != BT_CLASS && e->expr_type != EXPR_NULL)
    7480                 :             :                     {
    7481                 :        1221 :                       tmp = parmse.expr;
    7482                 :        1221 :                       if (TREE_CODE (tmp) == ADDR_EXPR)
    7483                 :         715 :                         tmp = TREE_OPERAND (tmp, 0);
    7484                 :        1221 :                       parmse.expr = gfc_conv_scalar_to_descriptor (&parmse, tmp,
    7485                 :             :                                                                    fsym->attr);
    7486                 :        1221 :                       parmse.expr = gfc_build_addr_expr (NULL_TREE,
    7487                 :             :                                                          parmse.expr);
    7488                 :             :                     }
    7489                 :      123793 :                   else if (fsym && e->expr_type != EXPR_NULL
    7490                 :      123495 :                       && ((fsym->attr.pointer
    7491                 :        1734 :                            && fsym->attr.flavor != FL_PROCEDURE)
    7492                 :      121767 :                           || (fsym->attr.proc_pointer
    7493                 :         157 :                               && !(e->expr_type == EXPR_VARIABLE
    7494                 :         157 :                                    && e->symtree->n.sym->attr.dummy))
    7495                 :      121622 :                           || (fsym->attr.proc_pointer
    7496                 :          12 :                               && e->expr_type == EXPR_VARIABLE
    7497                 :          12 :                               && gfc_is_proc_ptr_comp (e))
    7498                 :      121616 :                           || (fsym->attr.allocatable
    7499                 :        1007 :                               && fsym->attr.flavor != FL_PROCEDURE)))
    7500                 :             :                     {
    7501                 :             :                       /* Scalar pointer dummy args require an extra level of
    7502                 :             :                          indirection. The null pointer already contains
    7503                 :             :                          this level of indirection.  */
    7504                 :        2880 :                       parm_kind = SCALAR_POINTER;
    7505                 :        2880 :                       parmse.expr = gfc_build_addr_expr (NULL_TREE, parmse.expr);
    7506                 :             :                     }
    7507                 :             :                 }
    7508                 :             :             }
    7509                 :       58004 :           else if (e->ts.type == BT_CLASS
    7510                 :        2567 :                     && fsym && fsym->ts.type == BT_CLASS
    7511                 :        2221 :                     && (CLASS_DATA (fsym)->attr.dimension
    7512                 :        2221 :                         || CLASS_DATA (fsym)->attr.codimension))
    7513                 :             :             {
    7514                 :             :               /* Pass a class array.  */
    7515                 :        2221 :               parmse.use_offset = 1;
    7516                 :        2221 :               gfc_conv_expr_descriptor (&parmse, e);
    7517                 :        2221 :               bool defer_to_dealloc_blk = false;
    7518                 :             : 
    7519                 :        2221 :               if (fsym->attr.optional
    7520                 :         798 :                   && e->expr_type == EXPR_VARIABLE
    7521                 :         798 :                   && e->symtree->n.sym->attr.optional)
    7522                 :             :                 {
    7523                 :         438 :                   stmtblock_t block;
    7524                 :             : 
    7525                 :         438 :                   gfc_init_block (&block);
    7526                 :         438 :                   gfc_add_block_to_block (&block, &parmse.pre);
    7527                 :             : 
    7528                 :         876 :                   tree t = fold_build3_loc (input_location, COND_EXPR,
    7529                 :             :                              void_type_node,
    7530                 :         438 :                              gfc_conv_expr_present (e->symtree->n.sym),
    7531                 :             :                                     gfc_finish_block (&block),
    7532                 :             :                                     build_empty_stmt (input_location));
    7533                 :             : 
    7534                 :         438 :                   gfc_add_expr_to_block (&parmse.pre, t);
    7535                 :             :                 }
    7536                 :             : 
    7537                 :             :               /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
    7538                 :             :                  allocated on entry, it must be deallocated.  */
    7539                 :        2221 :               if (fsym->attr.intent == INTENT_OUT
    7540                 :         141 :                   && CLASS_DATA (fsym)->attr.allocatable)
    7541                 :             :                 {
    7542                 :         110 :                   stmtblock_t block;
    7543                 :         110 :                   tree ptr;
    7544                 :             : 
    7545                 :             :                   /* In case the data reference to deallocate is dependent on
    7546                 :             :                      its own content, save the resulting pointer to a variable
    7547                 :             :                      and only use that variable from now on, before the
    7548                 :             :                      expression becomes invalid.  */
    7549                 :         110 :                   parmse.expr = gfc_evaluate_data_ref_now (parmse.expr,
    7550                 :             :                                                            &parmse.pre);
    7551                 :             : 
    7552                 :         110 :                   if (parmse.class_container != NULL_TREE)
    7553                 :         110 :                     parmse.class_container
    7554                 :         110 :                         = gfc_evaluate_data_ref_now (parmse.class_container,
    7555                 :             :                                                      &parmse.pre);
    7556                 :             : 
    7557                 :         110 :                   gfc_init_block  (&block);
    7558                 :         110 :                   ptr = parmse.expr;
    7559                 :         110 :                   ptr = gfc_class_data_get (ptr);
    7560                 :             : 
    7561                 :         110 :                   tree cls = parmse.class_container;
    7562                 :         110 :                   tmp = gfc_deallocate_with_status (ptr, NULL_TREE,
    7563                 :             :                                                     NULL_TREE, NULL_TREE,
    7564                 :             :                                                     NULL_TREE, true, e,
    7565                 :             :                                                     GFC_CAF_COARRAY_NOCOARRAY,
    7566                 :             :                                                     cls);
    7567                 :         110 :                   gfc_add_expr_to_block (&block, tmp);
    7568                 :         110 :                   tmp = fold_build2_loc (input_location, MODIFY_EXPR,
    7569                 :             :                                          void_type_node, ptr,
    7570                 :             :                                          null_pointer_node);
    7571                 :         110 :                   gfc_add_expr_to_block (&block, tmp);
    7572                 :         110 :                   gfc_reset_vptr (&block, e, parmse.class_container);
    7573                 :             : 
    7574                 :         110 :                   if (fsym->attr.optional
    7575                 :          30 :                       && e->expr_type == EXPR_VARIABLE
    7576                 :          30 :                       && (!e->ref
    7577                 :          30 :                           || (e->ref->type == REF_ARRAY
    7578                 :           0 :                               && e->ref->u.ar.type != AR_FULL))
    7579                 :           0 :                       && e->symtree->n.sym->attr.optional)
    7580                 :             :                     {
    7581                 :           0 :                       tmp = fold_build3_loc (input_location, COND_EXPR,
    7582                 :             :                                     void_type_node,
    7583                 :           0 :                                     gfc_conv_expr_present (e->symtree->n.sym),
    7584                 :             :                                     gfc_finish_block (&block),
    7585                 :             :                                     build_empty_stmt (input_location));
    7586                 :             :                     }
    7587                 :             :                   else
    7588                 :         110 :                     tmp = gfc_finish_block (&block);
    7589                 :             : 
    7590                 :         110 :                   gfc_add_expr_to_block (&dealloc_blk, tmp);
    7591                 :         110 :                   defer_to_dealloc_blk = true;
    7592                 :             :                 }
    7593                 :             : 
    7594                 :        2221 :               gfc_se class_se = parmse;
    7595                 :        2221 :               gfc_init_block (&class_se.pre);
    7596                 :        2221 :               gfc_init_block (&class_se.post);
    7597                 :             : 
    7598                 :             :               /* The conversion does not repackage the reference to a class
    7599                 :             :                  array - _data descriptor.  */
    7600                 :        2221 :               gfc_conv_class_to_class (&class_se, e, fsym->ts, false,
    7601                 :        2221 :                                      fsym->attr.intent != INTENT_IN
    7602                 :        2221 :                                      && (CLASS_DATA (fsym)->attr.class_pointer
    7603                 :             :                                          || CLASS_DATA (fsym)->attr.allocatable),
    7604                 :        2221 :                                      fsym->attr.optional
    7605                 :         798 :                                      && e->expr_type == EXPR_VARIABLE
    7606                 :         798 :                                      && e->symtree->n.sym->attr.optional,
    7607                 :        2221 :                                      CLASS_DATA (fsym)->attr.class_pointer
    7608                 :        2221 :                                      || CLASS_DATA (fsym)->attr.allocatable);
    7609                 :             : 
    7610                 :        2221 :               parmse.expr = class_se.expr;
    7611                 :        4442 :               stmtblock_t *class_pre_block = defer_to_dealloc_blk
    7612                 :        2221 :                                              ? &dealloc_blk
    7613                 :             :                                              : &parmse.pre;
    7614                 :        2221 :               gfc_add_block_to_block (class_pre_block, &class_se.pre);
    7615                 :        2221 :               gfc_add_block_to_block (&parmse.post, &class_se.post);
    7616                 :        2221 :             }
    7617                 :             :           else
    7618                 :             :             {
    7619                 :             :               /* If the argument is a function call that may not create
    7620                 :             :                  a temporary for the result, we have to check that we
    7621                 :             :                  can do it, i.e. that there is no alias between this
    7622                 :             :                  argument and another one.  */
    7623                 :       55783 :               if (gfc_get_noncopying_intrinsic_argument (e) != NULL)
    7624                 :             :                 {
    7625                 :         357 :                   gfc_expr *iarg;
    7626                 :         357 :                   sym_intent intent;
    7627                 :             : 
    7628                 :         357 :                   if (fsym != NULL)
    7629                 :         348 :                     intent = fsym->attr.intent;
    7630                 :             :                   else
    7631                 :             :                     intent = INTENT_UNKNOWN;
    7632                 :             : 
    7633                 :         357 :                   if (gfc_check_fncall_dependency (e, intent, sym, args,
    7634                 :             :                                                    NOT_ELEMENTAL))
    7635                 :          21 :                     parmse.force_tmp = 1;
    7636                 :             : 
    7637                 :         357 :                   iarg = e->value.function.actual->expr;
    7638                 :             : 
    7639                 :             :                   /* Temporary needed if aliasing due to host association.  */
    7640                 :         357 :                   if (sym->attr.contained
    7641                 :         114 :                         && !sym->attr.pure
    7642                 :         114 :                         && !sym->attr.implicit_pure
    7643                 :          36 :                         && !sym->attr.use_assoc
    7644                 :          36 :                         && iarg->expr_type == EXPR_VARIABLE
    7645                 :          36 :                         && sym->ns == iarg->symtree->n.sym->ns)
    7646                 :          36 :                     parmse.force_tmp = 1;
    7647                 :             : 
    7648                 :             :                   /* Ditto within module.  */
    7649                 :         357 :                   if (sym->attr.use_assoc
    7650                 :         357 :                         && !sym->attr.pure
    7651                 :           6 :                         && !sym->attr.implicit_pure
    7652                 :           0 :                         && iarg->expr_type == EXPR_VARIABLE
    7653                 :           0 :                         && sym->module == iarg->symtree->n.sym->module)
    7654                 :           0 :                     parmse.force_tmp = 1;
    7655                 :             :                 }
    7656                 :             : 
    7657                 :             :               /* Special case for assumed-rank arrays: when passing an
    7658                 :             :                  argument to a nonallocatable/nonpointer dummy, the bounds have
    7659                 :             :                  to be reset as otherwise a last-dim ubound of -1 is
    7660                 :             :                  indistinguishable from an assumed-size array in the callee.  */
    7661                 :       55783 :               if (!sym->attr.is_bind_c && e && fsym && fsym->as
    7662                 :       32390 :                   && fsym->as->type == AS_ASSUMED_RANK
    7663                 :       10197 :                   && e->rank != -1
    7664                 :        9908 :                   && e->expr_type == EXPR_VARIABLE
    7665                 :        9467 :                   && ((fsym->ts.type == BT_CLASS
    7666                 :           0 :                        && !CLASS_DATA (fsym)->attr.class_pointer
    7667                 :           0 :                        && !CLASS_DATA (fsym)->attr.allocatable)
    7668                 :        9467 :                       || (fsym->ts.type != BT_CLASS
    7669                 :        9467 :                           && !fsym->attr.pointer && !fsym->attr.allocatable)))
    7670                 :             :                 {
    7671                 :             :                   /* Change AR_FULL to a (:,:,:) ref to force bounds update. */
    7672                 :        8924 :                   gfc_ref *ref;
    7673                 :        9170 :                   for (ref = e->ref; ref->next; ref = ref->next)
    7674                 :             :                     {
    7675                 :         318 :                       if (ref->next->type == REF_INQUIRY)
    7676                 :             :                         break;
    7677                 :         270 :                       if (ref->type == REF_ARRAY
    7678                 :          24 :                           && ref->u.ar.type != AR_ELEMENT)
    7679                 :             :                         break;
    7680                 :        8924 :                     };
    7681                 :        8924 :                   if (ref->u.ar.type == AR_FULL
    7682                 :        8198 :                       && ref->u.ar.as->type != AS_ASSUMED_SIZE)
    7683                 :        8078 :                     ref->u.ar.type = AR_SECTION;
    7684                 :             :                 }
    7685                 :             : 
    7686                 :       55783 :               if (sym->attr.is_bind_c && e && is_CFI_desc (fsym, NULL))
    7687                 :             :                 /* Implement F2018, 18.3.6, list item (5), bullet point 2.  */
    7688                 :        5850 :                 gfc_conv_gfc_desc_to_cfi_desc (&parmse, e, fsym);
    7689                 :             : 
    7690                 :       49933 :               else if (e->expr_type == EXPR_VARIABLE
    7691                 :       38252 :                     && is_subref_array (e)
    7692                 :       50653 :                     && !(fsym && fsym->attr.pointer))
    7693                 :             :                 /* The actual argument is a component reference to an
    7694                 :             :                    array of derived types.  In this case, the argument
    7695                 :             :                    is converted to a temporary, which is passed and then
    7696                 :             :                    written back after the procedure call.  */
    7697                 :         515 :                 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
    7698                 :         473 :                                 fsym ? fsym->attr.intent : INTENT_INOUT,
    7699                 :         515 :                                 fsym && fsym->attr.pointer);
    7700                 :             : 
    7701                 :       49418 :               else if (e->ts.type == BT_CLASS && CLASS_DATA (e)->as
    7702                 :         345 :                        && CLASS_DATA (e)->as->type == AS_ASSUMED_SIZE
    7703                 :          18 :                        && nodesc_arg && fsym->ts.type == BT_DERIVED)
    7704                 :             :                 /* An assumed size class actual argument being passed to
    7705                 :             :                    a 'no descriptor' formal argument just requires the
    7706                 :             :                    data pointer to be passed. For class dummy arguments
    7707                 :             :                    this is stored in the symbol backend decl..  */
    7708                 :           6 :                 parmse.expr = e->symtree->n.sym->backend_decl;
    7709                 :             : 
    7710                 :       49412 :               else if (gfc_is_class_array_ref (e, NULL)
    7711                 :       49412 :                        && fsym && fsym->ts.type == BT_DERIVED)
    7712                 :             :                 /* The actual argument is a component reference to an
    7713                 :             :                    array of derived types.  In this case, the argument
    7714                 :             :                    is converted to a temporary, which is passed and then
    7715                 :             :                    written back after the procedure call.
    7716                 :             :                    OOP-TODO: Insert code so that if the dynamic type is
    7717                 :             :                    the same as the declared type, copy-in/copy-out does
    7718                 :             :                    not occur.  */
    7719                 :         108 :                 gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
    7720                 :         108 :                                            fsym->attr.intent,
    7721                 :         108 :                                            fsym->attr.pointer);
    7722                 :             : 
    7723                 :       49304 :               else if (gfc_is_class_array_function (e)
    7724                 :       49304 :                        && fsym && fsym->ts.type == BT_DERIVED)
    7725                 :             :                 /* See previous comment.  For function actual argument,
    7726                 :             :                    the write out is not needed so the intent is set as
    7727                 :             :                    intent in.  */
    7728                 :             :                 {
    7729                 :          13 :                   e->must_finalize = 1;
    7730                 :          13 :                   gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
    7731                 :          13 :                                              INTENT_IN, fsym->attr.pointer);
    7732                 :             :                 }
    7733                 :       45718 :               else if (fsym && fsym->attr.contiguous
    7734                 :          60 :                        && (fsym->attr.target
    7735                 :        1674 :                            ? gfc_is_not_contiguous (e)
    7736                 :        1614 :                            : !gfc_is_simply_contiguous (e, false, true))
    7737                 :       51280 :                        && gfc_expr_is_variable (e))
    7738                 :             :                 {
    7739                 :         303 :                   gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
    7740                 :         303 :                                              fsym->attr.intent,
    7741                 :         303 :                                              fsym->attr.pointer);
    7742                 :             :                 }
    7743                 :             :               else
    7744                 :             :                 /* This is where we introduce a temporary to store the
    7745                 :             :                    result of a non-lvalue array expression.  */
    7746                 :       48988 :                 gfc_conv_array_parameter (&parmse, e, nodesc_arg, fsym,
    7747                 :             :                                           sym->name, NULL);
    7748                 :             : 
    7749                 :             :               /* If an ALLOCATABLE dummy argument has INTENT(OUT) and is
    7750                 :             :                  allocated on entry, it must be deallocated.
    7751                 :             :                  CFI descriptors are handled elsewhere.  */
    7752                 :       52168 :               if (fsym && fsym->attr.allocatable
    7753                 :        1717 :                   && fsym->attr.intent == INTENT_OUT
    7754                 :       55546 :                   && !is_CFI_desc (fsym, NULL))
    7755                 :             :                 {
    7756                 :         145 :                   if (fsym->ts.type == BT_DERIVED
    7757                 :          45 :                       && fsym->ts.u.derived->attr.alloc_comp)
    7758                 :             :                   {
    7759                 :             :                     // deallocate the components first
    7760                 :           9 :                     tmp = gfc_deallocate_alloc_comp (fsym->ts.u.derived,
    7761                 :             :                                                      parmse.expr, e->rank);
    7762                 :             :                     /* But check whether dummy argument is optional.  */
    7763                 :           9 :                     if (tmp != NULL_TREE
    7764                 :           9 :                         && fsym->attr.optional
    7765                 :           6 :                         && e->expr_type == EXPR_VARIABLE
    7766                 :           6 :                         && e->symtree->n.sym->attr.optional)
    7767                 :             :                       {
    7768                 :           6 :                         tree present;
    7769                 :           6 :                         present = gfc_conv_expr_present (e->symtree->n.sym);
    7770                 :           6 :                         tmp = build3_v (COND_EXPR, present, tmp,
    7771                 :             :                                         build_empty_stmt (input_location));
    7772                 :             :                       }
    7773                 :           9 :                     if (tmp != NULL_TREE)
    7774                 :           9 :                       gfc_add_expr_to_block (&dealloc_blk, tmp);
    7775                 :             :                   }
    7776                 :             : 
    7777                 :         145 :                   tmp = parmse.expr;
    7778                 :             :                   /* With bind(C), the actual argument is replaced by a bind-C
    7779                 :             :                      descriptor; in this case, the data component arrives here,
    7780                 :             :                      which shall not be dereferenced, but still freed and
    7781                 :             :                      nullified.  */
    7782                 :         145 :                   if  (TREE_TYPE(tmp) != pvoid_type_node)
    7783                 :         145 :                     tmp = build_fold_indirect_ref_loc (input_location,
    7784                 :             :                                                        parmse.expr);
    7785                 :         145 :                   tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
    7786                 :             :                                                     NULL_TREE, NULL_TREE, true,
    7787                 :             :                                                     e,
    7788                 :             :                                                     GFC_CAF_COARRAY_NOCOARRAY);
    7789                 :         145 :                   if (fsym->attr.optional
    7790                 :          48 :                       && e->expr_type == EXPR_VARIABLE
    7791                 :          48 :                       && e->symtree->n.sym->attr.optional)
    7792                 :          48 :                     tmp = fold_build3_loc (input_location, COND_EXPR,
    7793                 :             :                                      void_type_node,
    7794                 :          24 :                                      gfc_conv_expr_present (e->symtree->n.sym),
    7795                 :             :                                        tmp, build_empty_stmt (input_location));
    7796                 :         145 :                   gfc_add_expr_to_block (&dealloc_blk, tmp);
    7797                 :             :                 }
    7798                 :             :             }
    7799                 :             :         }
    7800                 :             :       /* Special case for an assumed-rank dummy argument. */
    7801                 :      261680 :       if (!sym->attr.is_bind_c && e && fsym && e->rank > 0
    7802                 :       53885 :           && (fsym->ts.type == BT_CLASS
    7803                 :       53885 :               ? (CLASS_DATA (fsym)->as
    7804                 :        4177 :                  && CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)
    7805                 :       49708 :               : (fsym->as && fsym->as->type == AS_ASSUMED_RANK)))
    7806                 :             :         {
    7807                 :       11047 :           if (fsym->ts.type == BT_CLASS
    7808                 :       11047 :               ? (CLASS_DATA (fsym)->attr.class_pointer
    7809                 :        1055 :                  || CLASS_DATA (fsym)->attr.allocatable)
    7810                 :        9992 :               : (fsym->attr.pointer || fsym->attr.allocatable))
    7811                 :             :             {
    7812                 :             :               /* Unallocated allocatable arrays and unassociated pointer
    7813                 :             :                  arrays need their dtype setting if they are argument
    7814                 :             :                  associated with assumed rank dummies to set the rank.  */
    7815                 :         891 :               set_dtype_for_unallocated (&parmse, e);
    7816                 :             :             }
    7817                 :       10156 :           else if (e->expr_type == EXPR_VARIABLE
    7818                 :        9677 :                    && e->symtree->n.sym->attr.dummy
    7819                 :         698 :                    && (e->ts.type == BT_CLASS
    7820                 :         891 :                        ? (e->ref && e->ref->next
    7821                 :         193 :                           && e->ref->next->type == REF_ARRAY
    7822                 :         193 :                           && e->ref->next->u.ar.type == AR_FULL
    7823                 :         386 :                           && e->ref->next->u.ar.as->type == AS_ASSUMED_SIZE)
    7824                 :         505 :                        : (e->ref && e->ref->type == REF_ARRAY
    7825                 :         505 :                           && e->ref->u.ar.type == AR_FULL
    7826                 :         733 :                           && e->ref->u.ar.as->type == AS_ASSUMED_SIZE)))
    7827                 :             :             {
    7828                 :             :               /* Assumed-size actual to assumed-rank dummy requires
    7829                 :             :                  dim[rank-1].ubound = -1. */
    7830                 :         180 :               tree minus_one;
    7831                 :         180 :               tmp = build_fold_indirect_ref_loc (input_location, parmse.expr);
    7832                 :         180 :               if (fsym->ts.type == BT_CLASS)
    7833                 :          60 :                 tmp = gfc_class_data_get (tmp);
    7834                 :         180 :               minus_one = build_int_cst (gfc_array_index_type, -1);
    7835                 :         180 :               gfc_conv_descriptor_ubound_set (&parmse.pre, tmp,
    7836                 :         180 :                                               gfc_rank_cst[e->rank - 1],
    7837                 :             :                                               minus_one);
    7838                 :             :             }
    7839                 :             :         }
    7840                 :             : 
    7841                 :             :       /* The case with fsym->attr.optional is that of a user subroutine
    7842                 :             :          with an interface indicating an optional argument.  When we call
    7843                 :             :          an intrinsic subroutine, however, fsym is NULL, but we might still
    7844                 :             :          have an optional argument, so we proceed to the substitution
    7845                 :             :          just in case.  Arguments passed to bind(c) procedures via CFI
    7846                 :             :          descriptors are handled elsewhere.  */
    7847                 :      248293 :       if (e && (fsym == NULL || fsym->attr.optional)
    7848                 :      319307 :           && !(sym->attr.is_bind_c && is_CFI_desc (fsym, NULL)))
    7849                 :             :         {
    7850                 :             :           /* If an optional argument is itself an optional dummy argument,
    7851                 :             :              check its presence and substitute a null if absent.  This is
    7852                 :             :              only needed when passing an array to an elemental procedure
    7853                 :             :              as then array elements are accessed - or no NULL pointer is
    7854                 :             :              allowed and a "1" or "0" should be passed if not present.
    7855                 :             :              When passing a non-array-descriptor full array to a
    7856                 :             :              non-array-descriptor dummy, no check is needed. For
    7857                 :             :              array-descriptor actual to array-descriptor dummy, see
    7858                 :             :              PR 41911 for why a check has to be inserted.
    7859                 :             :              fsym == NULL is checked as intrinsics required the descriptor
    7860                 :             :              but do not always set fsym.
    7861                 :             :              Also, it is necessary to pass a NULL pointer to library routines
    7862                 :             :              which usually ignore optional arguments, so they can handle
    7863                 :             :              these themselves.  */
    7864                 :       56533 :           if (e->expr_type == EXPR_VARIABLE
    7865                 :       25680 :               && e->symtree->n.sym->attr.optional
    7866                 :        2414 :               && (((e->rank != 0 && elemental_proc)
    7867                 :        2239 :                    || e->representation.length || e->ts.type == BT_CHARACTER
    7868                 :        2013 :                    || (e->rank == 0 && e->symtree->n.sym->attr.value)
    7869                 :        1903 :                    || (e->rank != 0
    7870                 :        1069 :                        && (fsym == NULL
    7871                 :        1033 :                            || (fsym->as
    7872                 :         271 :                                && (fsym->as->type == AS_ASSUMED_SHAPE
    7873                 :         235 :                                    || fsym->as->type == AS_ASSUMED_RANK
    7874                 :         117 :                                    || fsym->as->type == AS_DEFERRED)))))
    7875                 :        1679 :                   || se->ignore_optional))
    7876                 :         763 :             gfc_conv_missing_dummy (&parmse, e, fsym ? fsym->ts : e->ts,
    7877                 :         763 :                                     e->representation.length);
    7878                 :             :         }
    7879                 :             : 
    7880                 :             :       /* Make the class container for the first argument available with class
    7881                 :             :          valued transformational functions.  */
    7882                 :      261680 :       if (argc == 0 && e && e->ts.type == BT_CLASS
    7883                 :        4800 :           && isym && isym->transformational
    7884                 :          84 :           && se->ss && se->ss->info)
    7885                 :             :         {
    7886                 :          84 :           arg1_cntnr = parmse.expr;
    7887                 :          84 :           if (POINTER_TYPE_P (TREE_TYPE (arg1_cntnr)))
    7888                 :          84 :             arg1_cntnr = build_fold_indirect_ref_loc (input_location, arg1_cntnr);
    7889                 :          84 :           arg1_cntnr = gfc_get_class_from_expr (arg1_cntnr);
    7890                 :          84 :           se->ss->info->class_container = arg1_cntnr;
    7891                 :             :         }
    7892                 :             : 
    7893                 :      261680 :       if (fsym && e)
    7894                 :             :         {
    7895                 :             :           /* Obtain the character length of an assumed character length
    7896                 :             :              length procedure from the typespec.  */
    7897                 :      216410 :           if (fsym->ts.type == BT_CHARACTER
    7898                 :       30864 :               && parmse.string_length == NULL_TREE
    7899                 :        3508 :               && e->ts.type == BT_PROCEDURE
    7900                 :          15 :               && e->symtree->n.sym->ts.type == BT_CHARACTER
    7901                 :          15 :               && e->symtree->n.sym->ts.u.cl->length != NULL
    7902                 :          15 :               && e->symtree->n.sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
    7903                 :             :             {
    7904                 :           8 :               gfc_conv_const_charlen (e->symtree->n.sym->ts.u.cl);
    7905                 :           8 :               parmse.string_length = e->symtree->n.sym->ts.u.cl->backend_decl;
    7906                 :             :             }
    7907                 :             : 
    7908                 :             :           /* Obtain the character length for a NULL() actual with a character
    7909                 :             :              MOLD argument.  Otherwise substitute a suitable dummy length.
    7910                 :             :              Here we handle non-optional dummies of non-bind(c) procedures.  */
    7911                 :      216410 :           if (e->expr_type == EXPR_NULL
    7912                 :         745 :               && fsym->ts.type == BT_CHARACTER
    7913                 :         296 :               && !fsym->attr.optional
    7914                 :      216628 :               && !(sym->attr.is_bind_c && is_CFI_desc (fsym, NULL)))
    7915                 :         216 :             conv_null_actual (&parmse, e, fsym);
    7916                 :             :         }
    7917                 :             : 
    7918                 :             :       /* If any actual argument of the procedure is allocatable and passed
    7919                 :             :          to an allocatable dummy with INTENT(OUT), we conservatively
    7920                 :             :          evaluate actual argument expressions before deallocations are
    7921                 :             :          performed and the procedure is executed.  May create temporaries.
    7922                 :             :          This ensures we conform to F2023:15.5.3, 15.5.4.  */
    7923                 :      248293 :       if (e && fsym && force_eval_args
    7924                 :        1078 :           && fsym->attr.intent != INTENT_OUT
    7925                 :      262077 :           && !gfc_is_constant_expr (e))
    7926                 :         256 :         parmse.expr = gfc_evaluate_now (parmse.expr, &parmse.pre);
    7927                 :             : 
    7928                 :      261680 :       if (fsym && need_interface_mapping && e)
    7929                 :       37293 :         gfc_add_interface_mapping (&mapping, fsym, &parmse, e);
    7930                 :             : 
    7931                 :      261680 :       gfc_add_block_to_block (&se->pre, &parmse.pre);
    7932                 :      261680 :       gfc_add_block_to_block (&post, &parmse.post);
    7933                 :      261680 :       gfc_add_block_to_block (&se->finalblock, &parmse.finalblock);
    7934                 :             : 
    7935                 :             :       /* Allocated allocatable components of derived types must be
    7936                 :             :          deallocated for non-variable scalars, array arguments to elemental
    7937                 :             :          procedures, and array arguments with descriptor to non-elemental
    7938                 :             :          procedures.  As bounds information for descriptorless arrays is no
    7939                 :             :          longer available here, they are dealt with in trans-array.cc
    7940                 :             :          (gfc_conv_array_parameter).  */
    7941                 :      248293 :       if (e && (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS)
    7942                 :       26905 :             && e->ts.u.derived->attr.alloc_comp
    7943                 :        7375 :             && (e->rank == 0 || elemental_proc || !nodesc_arg)
    7944                 :      268937 :             && !expr_may_alias_variables (e, elemental_proc))
    7945                 :             :         {
    7946                 :         328 :           int parm_rank;
    7947                 :             :           /* It is known the e returns a structure type with at least one
    7948                 :             :              allocatable component.  When e is a function, ensure that the
    7949                 :             :              function is called once only by using a temporary variable.  */
    7950                 :         328 :           if (!DECL_P (parmse.expr) && e->expr_type == EXPR_FUNCTION)
    7951                 :         139 :             parmse.expr = gfc_evaluate_now_loc (input_location,
    7952                 :             :                                                 parmse.expr, &se->pre);
    7953                 :             : 
    7954                 :         328 :           if ((fsym && fsym->attr.value) || e->expr_type == EXPR_ARRAY)
    7955                 :         116 :             tmp = parmse.expr;
    7956                 :             :           else
    7957                 :         212 :             tmp = build_fold_indirect_ref_loc (input_location,
    7958                 :             :                                                parmse.expr);
    7959                 :             : 
    7960                 :         328 :           parm_rank = e->rank;
    7961                 :         328 :           switch (parm_kind)
    7962                 :             :             {
    7963                 :             :             case (ELEMENTAL):
    7964                 :             :             case (SCALAR):
    7965                 :         328 :               parm_rank = 0;
    7966                 :             :               break;
    7967                 :             : 
    7968                 :           0 :             case (SCALAR_POINTER):
    7969                 :           0 :               tmp = build_fold_indirect_ref_loc (input_location,
    7970                 :             :                                              tmp);
    7971                 :           0 :               break;
    7972                 :             :             }
    7973                 :             : 
    7974                 :         328 :           if (e->ts.type == BT_DERIVED && fsym && fsym->ts.type == BT_CLASS)
    7975                 :             :             {
    7976                 :             :               /* The derived type is passed to gfc_deallocate_alloc_comp.
    7977                 :             :                  Therefore, class actuals can be handled correctly but derived
    7978                 :             :                  types passed to class formals need the _data component.  */
    7979                 :          81 :               tmp = gfc_class_data_get (tmp);
    7980                 :          81 :               if (!CLASS_DATA (fsym)->attr.dimension)
    7981                 :             :                 {
    7982                 :          55 :                   if (UNLIMITED_POLY (fsym))
    7983                 :             :                     {
    7984                 :          12 :                       tree type = gfc_typenode_for_spec (&e->ts);
    7985                 :          12 :                       type = build_pointer_type (type);
    7986                 :          12 :                       tmp = fold_convert (type, tmp);
    7987                 :             :                     }
    7988                 :          55 :                   tmp = build_fold_indirect_ref_loc (input_location, tmp);
    7989                 :             :                 }
    7990                 :             :             }
    7991                 :             : 
    7992                 :         328 :           if (e->expr_type == EXPR_OP
    7993                 :          24 :                 && e->value.op.op == INTRINSIC_PARENTHESES
    7994                 :          24 :                 && e->value.op.op1->expr_type == EXPR_VARIABLE)
    7995                 :             :             {
    7996                 :          24 :               tree local_tmp;
    7997                 :          24 :               local_tmp = gfc_evaluate_now (tmp, &se->pre);
    7998                 :          24 :               local_tmp = gfc_copy_alloc_comp (e->ts.u.derived, local_tmp, tmp,
    7999                 :             :                                                parm_rank, 0);
    8000                 :          24 :               gfc_add_expr_to_block (&se->post, local_tmp);
    8001                 :             :             }
    8002                 :             : 
    8003                 :             :           /* Items of array expressions passed to a polymorphic formal arguments
    8004                 :             :              create their own clean up, so prevent double free.  */
    8005                 :         328 :           if (!finalized && !e->must_finalize
    8006                 :         327 :               && !(e->expr_type == EXPR_ARRAY && fsym
    8007                 :          50 :                    && fsym->ts.type == BT_CLASS))
    8008                 :             :             {
    8009                 :         307 :               bool scalar_res_outside_loop;
    8010                 :         909 :               scalar_res_outside_loop = e->expr_type == EXPR_FUNCTION
    8011                 :         150 :                                         && parm_rank == 0
    8012                 :         445 :                                         && parmse.loop;
    8013                 :             : 
    8014                 :             :               /* Scalars passed to an assumed rank argument are converted to
    8015                 :             :                  a descriptor. Obtain the data field before deallocating any
    8016                 :             :                  allocatable components.  */
    8017                 :         278 :               if (parm_rank == 0 && e->expr_type != EXPR_ARRAY
    8018                 :         560 :                   && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
    8019                 :          19 :                 tmp = gfc_conv_descriptor_data_get (tmp);
    8020                 :             : 
    8021                 :         307 :               if (scalar_res_outside_loop)
    8022                 :             :                 {
    8023                 :             :                   /* Go through the ss chain to find the argument and use
    8024                 :             :                      the stored value.  */
    8025                 :          30 :                   gfc_ss *tmp_ss = parmse.loop->ss;
    8026                 :          72 :                   for (; tmp_ss; tmp_ss = tmp_ss->next)
    8027                 :          60 :                     if (tmp_ss->info
    8028                 :          48 :                         && tmp_ss->info->expr == e
    8029                 :          18 :                         && tmp_ss->info->data.scalar.value != NULL_TREE)
    8030                 :             :                       {
    8031                 :          18 :                         tmp = tmp_ss->info->data.scalar.value;
    8032                 :          18 :                         break;
    8033                 :             :                       }
    8034                 :             :                 }
    8035                 :             : 
    8036                 :         307 :               STRIP_NOPS (tmp);
    8037                 :             : 
    8038                 :         307 :               if (derived_array != NULL_TREE)
    8039                 :           0 :                 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived,
    8040                 :             :                                                  derived_array,
    8041                 :             :                                                  parm_rank);
    8042                 :         307 :               else if ((e->ts.type == BT_CLASS
    8043                 :          24 :                         && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
    8044                 :         307 :                        || e->ts.type == BT_DERIVED)
    8045                 :         307 :                 tmp = gfc_deallocate_alloc_comp (e->ts.u.derived, tmp,
    8046                 :             :                                                  parm_rank);
    8047                 :           0 :               else if (e->ts.type == BT_CLASS)
    8048                 :           0 :                 tmp = gfc_deallocate_alloc_comp (CLASS_DATA (e)->ts.u.derived,
    8049                 :             :                                                  tmp, parm_rank);
    8050                 :             : 
    8051                 :         307 :               if (scalar_res_outside_loop)
    8052                 :          30 :                 gfc_add_expr_to_block (&parmse.loop->post, tmp);
    8053                 :             :               else
    8054                 :         277 :                 gfc_prepend_expr_to_block (&post, tmp);
    8055                 :             :             }
    8056                 :             :         }
    8057                 :             : 
    8058                 :             :       /* Add argument checking of passing an unallocated/NULL actual to
    8059                 :             :          a nonallocatable/nonpointer dummy.  */
    8060                 :             : 
    8061                 :      261680 :       if (gfc_option.rtcheck & GFC_RTCHECK_POINTER && e != NULL)
    8062                 :             :         {
    8063                 :        6504 :           symbol_attribute attr;
    8064                 :        6504 :           char *msg;
    8065                 :        6504 :           tree cond;
    8066                 :        6504 :           tree tmp;
    8067                 :        6504 :           symbol_attribute fsym_attr;
    8068                 :             : 
    8069                 :        6504 :           if (fsym)
    8070                 :             :             {
    8071                 :        6343 :               if (fsym->ts.type == BT_CLASS)
    8072                 :             :                 {
    8073                 :         303 :                   fsym_attr = CLASS_DATA (fsym)->attr;
    8074                 :         303 :                   fsym_attr.pointer = fsym_attr.class_pointer;
    8075                 :             :                 }
    8076                 :             :               else
    8077                 :        6040 :                 fsym_attr = fsym->attr;
    8078                 :             :             }
    8079                 :             : 
    8080                 :        6504 :           if (e->expr_type == EXPR_VARIABLE || e->expr_type == EXPR_FUNCTION)
    8081                 :        4055 :             attr = gfc_expr_attr (e);
    8082                 :             :           else
    8083                 :        6046 :             goto end_pointer_check;
    8084                 :             : 
    8085                 :             :           /*  In Fortran 2008 it's allowed to pass a NULL pointer/nonallocated
    8086                 :             :               allocatable to an optional dummy, cf. 12.5.2.12.  */
    8087                 :        4055 :           if (fsym != NULL && fsym->attr.optional && !attr.proc_pointer
    8088                 :        1038 :               && (gfc_option.allow_std & GFC_STD_F2008) != 0)
    8089                 :        1032 :             goto end_pointer_check;
    8090                 :             : 
    8091                 :        3023 :           if (attr.optional)
    8092                 :             :             {
    8093                 :             :               /* If the actual argument is an optional pointer/allocatable and
    8094                 :             :                  the formal argument takes an nonpointer optional value,
    8095                 :             :                  it is invalid to pass a non-present argument on, even
    8096                 :             :                  though there is no technical reason for this in gfortran.
    8097                 :             :                  See Fortran 2003, Section 12.4.1.6 item (7)+(8).  */
    8098                 :          60 :               tree present, null_ptr, type;
    8099                 :             : 
    8100                 :          60 :               if (attr.allocatable
    8101                 :           0 :                   && (fsym == NULL || !fsym_attr.allocatable))
    8102                 :           0 :                 msg = xasprintf ("Allocatable actual argument '%s' is not "
    8103                 :             :                                  "allocated or not present",
    8104                 :           0 :                                  e->symtree->n.sym->name);
    8105                 :          60 :               else if (attr.pointer
    8106                 :          12 :                        && (fsym == NULL || !fsym_attr.pointer))
    8107                 :          12 :                 msg = xasprintf ("Pointer actual argument '%s' is not "
    8108                 :             :                                  "associated or not present",
    8109                 :          12 :                                  e->symtree->n.sym->name);
    8110                 :          48 :               else if (attr.proc_pointer && !e->value.function.actual
    8111                 :           0 :                        && (fsym == NULL || !fsym_attr.proc_pointer))
    8112                 :           0 :                 msg = xasprintf ("Proc-pointer actual argument '%s' is not "
    8113                 :             :                                  "associated or not present",
    8114                 :           0 :                                  e->symtree->n.sym->name);
    8115                 :             :               else
    8116                 :          48 :                 goto end_pointer_check;
    8117                 :             : 
    8118                 :          12 :               present = gfc_conv_expr_present (e->symtree->n.sym);
    8119                 :          12 :               type = TREE_TYPE (present);
    8120                 :          12 :               present = fold_build2_loc (input_location, EQ_EXPR,
    8121                 :             :                                          logical_type_node, present,
    8122                 :             :                                          fold_convert (type,
    8123                 :             :                                                        null_pointer_node));
    8124                 :          12 :               type = TREE_TYPE (parmse.expr);
    8125                 :          12 :               null_ptr = fold_build2_loc (input_location, EQ_EXPR,
    8126                 :             :                                           logical_type_node, parmse.expr,
    8127                 :             :                                           fold_convert (type,
    8128                 :             :                                                         null_pointer_node));
    8129                 :          12 :               cond = fold_build2_loc (input_location, TRUTH_ORIF_EXPR,
    8130                 :             :                                       logical_type_node, present, null_ptr);
    8131                 :             :             }
    8132                 :             :           else
    8133                 :             :             {
    8134                 :        2963 :               if (attr.allocatable
    8135                 :         256 :                   && (fsym == NULL || !fsym_attr.allocatable))
    8136                 :         190 :                 msg = xasprintf ("Allocatable actual argument '%s' is not "
    8137                 :         190 :                                  "allocated", e->symtree->n.sym->name);
    8138                 :        2773 :               else if (attr.pointer
    8139                 :         253 :                        && (fsym == NULL || !fsym_attr.pointer))
    8140                 :         184 :                 msg = xasprintf ("Pointer actual argument '%s' is not "
    8141                 :         184 :                                  "associated", e->symtree->n.sym->name);
    8142                 :        2589 :               else if (attr.proc_pointer && !e->value.function.actual
    8143                 :          72 :                        && (fsym == NULL || !fsym_attr.proc_pointer))
    8144                 :          72 :                 msg = xasprintf ("Proc-pointer actual argument '%s' is not "
    8145                 :          72 :                                  "associated", e->symtree->n.sym->name);
    8146                 :             :               else
    8147                 :        2517 :                 goto end_pointer_check;
    8148                 :             : 
    8149                 :         446 :               tmp = parmse.expr;
    8150                 :         446 :               if (fsym && fsym->ts.type == BT_CLASS)
    8151                 :             :                 {
    8152                 :          76 :                   if (POINTER_TYPE_P (TREE_TYPE (tmp)))
    8153                 :          70 :                     tmp = build_fold_indirect_ref_loc (input_location, tmp);
    8154                 :          76 :                   tmp = gfc_class_data_get (tmp);
    8155                 :          76 :                   if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
    8156                 :           3 :                     tmp = gfc_conv_descriptor_data_get (tmp);
    8157                 :             :                 }
    8158                 :             : 
    8159                 :             :               /* If the argument is passed by value, we need to strip the
    8160                 :             :                  INDIRECT_REF.  */
    8161                 :         446 :               if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
    8162                 :          12 :                 tmp = gfc_build_addr_expr (NULL_TREE, tmp);
    8163                 :             : 
    8164                 :         446 :               cond = fold_build2_loc (input_location, EQ_EXPR,
    8165                 :             :                                       logical_type_node, tmp,
    8166                 :         446 :                                       fold_convert (TREE_TYPE (tmp),
    8167                 :             :                                                     null_pointer_node));
    8168                 :             :             }
    8169                 :             : 
    8170                 :         458 :           gfc_trans_runtime_check (true, false, cond, &se->pre, &e->where,
    8171                 :             :                                    msg);
    8172                 :         458 :           free (msg);
    8173                 :             :         }
    8174                 :      255176 :       end_pointer_check:
    8175                 :             : 
    8176                 :             :       /* Deferred length dummies pass the character length by reference
    8177                 :             :          so that the value can be returned.  */
    8178                 :      261680 :       if (parmse.string_length && fsym && fsym->ts.deferred)
    8179                 :             :         {
    8180                 :         781 :           if (INDIRECT_REF_P (parmse.string_length))
    8181                 :             :             {
    8182                 :             :               /* In chains of functions/procedure calls the string_length already
    8183                 :             :                  is a pointer to the variable holding the length.  Therefore
    8184                 :             :                  remove the deref on call.  */
    8185                 :          90 :               tmp = parmse.string_length;
    8186                 :          90 :               parmse.string_length = TREE_OPERAND (parmse.string_length, 0);
    8187                 :             :             }
    8188                 :             :           else
    8189                 :             :             {
    8190                 :         691 :               tmp = parmse.string_length;
    8191                 :         691 :               if (!VAR_P (tmp) && TREE_CODE (tmp) != COMPONENT_REF)
    8192                 :          61 :                 tmp = gfc_evaluate_now (parmse.string_length, &se->pre);
    8193                 :         691 :               parmse.string_length = gfc_build_addr_expr (NULL_TREE, tmp);
    8194                 :             :             }
    8195                 :             : 
    8196                 :         781 :           if (e && e->expr_type == EXPR_VARIABLE
    8197                 :         624 :               && fsym->attr.allocatable
    8198                 :         360 :               && e->ts.u.cl->backend_decl
    8199                 :         360 :               && VAR_P (e->ts.u.cl->backend_decl))
    8200                 :             :             {
    8201                 :         276 :               if (INDIRECT_REF_P (tmp))
    8202                 :           0 :                 tmp = TREE_OPERAND (tmp, 0);
    8203                 :         276 :               gfc_add_modify (&se->post, e->ts.u.cl->backend_decl,
    8204                 :             :                               fold_convert (gfc_charlen_type_node, tmp));
    8205                 :             :             }
    8206                 :             :         }
    8207                 :             : 
    8208                 :             :       /* Character strings are passed as two parameters, a length and a
    8209                 :             :          pointer - except for Bind(c) and c_ptrs which only passe the pointer.
    8210                 :             :          An unlimited polymorphic formal argument likewise does not
    8211                 :             :          need the length.  */
    8212                 :      261680 :       if (parmse.string_length != NULL_TREE
    8213                 :       36060 :           && !sym->attr.is_bind_c
    8214                 :       35364 :           && !(fsym && fsym->ts.type == BT_DERIVED && fsym->ts.u.derived
    8215                 :             :                && fsym->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
    8216                 :             :                && fsym->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING )
    8217                 :       29475 :           && !(fsym && fsym->ts.type == BT_ASSUMED)
    8218                 :       29366 :           && !(fsym && UNLIMITED_POLY (fsym)))
    8219                 :       35074 :         vec_safe_push (stringargs, parmse.string_length);
    8220                 :             : 
    8221                 :             :       /* When calling __copy for character expressions to unlimited
    8222                 :             :          polymorphic entities, the dst argument needs a string length.  */
    8223                 :       48290 :       if (sym->name[0] == '_' && e && e->ts.type == BT_CHARACTER
    8224                 :        4807 :           && startswith (sym->name, "__vtab_CHARACTER")
    8225                 :           0 :           && arg->next && arg->next->expr
    8226                 :           0 :           && (arg->next->expr->ts.type == BT_DERIVED
    8227                 :           0 :               || arg->next->expr->ts.type == BT_CLASS)
    8228                 :      261680 :           && arg->next->expr->ts.u.derived->attr.unlimited_polymorphic)
    8229                 :           0 :         vec_safe_push (stringargs, parmse.string_length);
    8230                 :             : 
    8231                 :             :       /* For descriptorless coarrays and assumed-shape coarray dummies, we
    8232                 :             :          pass the token and the offset as additional arguments.  */
    8233                 :      261680 :       if (fsym && e == NULL && flag_coarray == GFC_FCOARRAY_LIB
    8234                 :          71 :           && attr->codimension && !attr->allocatable)
    8235                 :             :         {
    8236                 :             :           /* Token and offset.  */
    8237                 :           5 :           vec_safe_push (stringargs, null_pointer_node);
    8238                 :           5 :           vec_safe_push (stringargs, build_int_cst (gfc_array_index_type, 0));
    8239                 :           5 :           gcc_assert (fsym->attr.optional);
    8240                 :             :         }
    8241                 :      228734 :       else if (fsym && flag_coarray == GFC_FCOARRAY_LIB && attr->codimension
    8242                 :         342 :                && !attr->allocatable)
    8243                 :             :         {
    8244                 :          78 :           tree caf_decl, caf_type, caf_desc = NULL_TREE;
    8245                 :          78 :           tree offset, tmp2;
    8246                 :             : 
    8247                 :          78 :           caf_decl = gfc_get_tree_for_caf_expr (e);
    8248                 :          78 :           caf_type = TREE_TYPE (caf_decl);
    8249                 :          78 :           if (POINTER_TYPE_P (caf_type)
    8250                 :          78 :               && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (caf_type)))
    8251                 :           2 :             caf_desc = TREE_TYPE (caf_type);
    8252                 :          76 :           else if (GFC_DESCRIPTOR_TYPE_P (caf_type))
    8253                 :             :             caf_desc = caf_type;
    8254                 :             : 
    8255                 :          32 :           if (caf_desc
    8256                 :          32 :               && (GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_ALLOCATABLE
    8257                 :           0 :                   || GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_POINTER))
    8258                 :             :             {
    8259                 :          64 :               tmp = POINTER_TYPE_P (TREE_TYPE (caf_decl))
    8260                 :          34 :                       ? build_fold_indirect_ref (caf_decl)
    8261                 :             :                       : caf_decl;
    8262                 :          32 :               tmp = gfc_conv_descriptor_token (tmp);
    8263                 :             :             }
    8264                 :          46 :           else if (DECL_LANG_SPECIFIC (caf_decl)
    8265                 :          46 :                    && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
    8266                 :           9 :             tmp = GFC_DECL_TOKEN (caf_decl);
    8267                 :             :           else
    8268                 :             :             {
    8269                 :          37 :               gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
    8270                 :             :                           && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type) != NULL_TREE);
    8271                 :          37 :               tmp = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
    8272                 :             :             }
    8273                 :             : 
    8274                 :          78 :           vec_safe_push (stringargs, tmp);
    8275                 :             : 
    8276                 :          78 :           if (caf_desc
    8277                 :          78 :               && GFC_TYPE_ARRAY_AKIND (caf_desc) == GFC_ARRAY_ALLOCATABLE)
    8278                 :          32 :             offset = build_int_cst (gfc_array_index_type, 0);
    8279                 :          46 :           else if (DECL_LANG_SPECIFIC (caf_decl)
    8280                 :          46 :                    && GFC_DECL_CAF_OFFSET (caf_decl) != NULL_TREE)
    8281                 :           9 :             offset = GFC_DECL_CAF_OFFSET (caf_decl);
    8282                 :          37 :           else if (GFC_TYPE_ARRAY_CAF_OFFSET (caf_type) != NULL_TREE)
    8283                 :           0 :             offset = GFC_TYPE_ARRAY_CAF_OFFSET (caf_type);
    8284                 :             :           else
    8285                 :          37 :             offset = build_int_cst (gfc_array_index_type, 0);
    8286                 :             : 
    8287                 :          78 :           if (caf_desc)
    8288                 :             :             {
    8289                 :          64 :               tmp = POINTER_TYPE_P (TREE_TYPE (caf_decl))
    8290                 :          34 :                       ? build_fold_indirect_ref (caf_decl)
    8291                 :             :                       : caf_decl;
    8292                 :          32 :               tmp = gfc_conv_descriptor_data_get (tmp);
    8293                 :             :             }
    8294                 :             :           else
    8295                 :             :             {
    8296                 :          46 :               gcc_assert (POINTER_TYPE_P (caf_type));
    8297                 :          46 :               tmp = caf_decl;
    8298                 :             :             }
    8299                 :             : 
    8300                 :         156 :           tmp2 = fsym->ts.type == BT_CLASS
    8301                 :          78 :                  ? gfc_class_data_get (parmse.expr) : parmse.expr;
    8302                 :          78 :           if ((fsym->ts.type != BT_CLASS
    8303                 :          69 :                && (fsym->as->type == AS_ASSUMED_SHAPE
    8304                 :          40 :                    || fsym->as->type == AS_ASSUMED_RANK))
    8305                 :          49 :               || (fsym->ts.type == BT_CLASS
    8306                 :           9 :                   && (CLASS_DATA (fsym)->as->type == AS_ASSUMED_SHAPE
    8307                 :           6 :                       || CLASS_DATA (fsym)->as->type == AS_ASSUMED_RANK)))
    8308                 :             :             {
    8309                 :          32 :               if (fsym->ts.type == BT_CLASS)
    8310                 :           3 :                 gcc_assert (!POINTER_TYPE_P (TREE_TYPE (tmp2)));
    8311                 :             :               else
    8312                 :             :                 {
    8313                 :          29 :                   gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
    8314                 :          29 :                   tmp2 = build_fold_indirect_ref_loc (input_location, tmp2);
    8315                 :             :                 }
    8316                 :          32 :               gcc_assert (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)));
    8317                 :          32 :               tmp2 = gfc_conv_descriptor_data_get (tmp2);
    8318                 :             :             }
    8319                 :          46 :           else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp2)))
    8320                 :           6 :             tmp2 = gfc_conv_descriptor_data_get (tmp2);
    8321                 :             :           else
    8322                 :             :             {
    8323                 :          40 :               gcc_assert (POINTER_TYPE_P (TREE_TYPE (tmp2)));
    8324                 :             :             }
    8325                 :             : 
    8326                 :          78 :           tmp = fold_build2_loc (input_location, MINUS_EXPR,
    8327                 :             :                                  gfc_array_index_type,
    8328                 :             :                                  fold_convert (gfc_array_index_type, tmp2),
    8329                 :             :                                  fold_convert (gfc_array_index_type, tmp));
    8330                 :          78 :           offset = fold_build2_loc (input_location, PLUS_EXPR,
    8331                 :             :                                     gfc_array_index_type, offset, tmp);
    8332                 :             : 
    8333                 :          78 :           vec_safe_push (stringargs, offset);
    8334                 :             :         }
    8335                 :             : 
    8336                 :      261680 :       vec_safe_push (arglist, parmse.expr);
    8337                 :             :     }
    8338                 :             : 
    8339                 :      125259 :   gfc_add_block_to_block (&se->pre, &dealloc_blk);
    8340                 :      125259 :   gfc_add_block_to_block (&se->pre, &clobbers);
    8341                 :      125259 :   gfc_finish_interface_mapping (&mapping, &se->pre, &se->post);
    8342                 :             : 
    8343                 :      125259 :   if (comp)
    8344                 :        1793 :     ts = comp->ts;
    8345                 :      123466 :   else if (sym->ts.type == BT_CLASS)
    8346                 :         830 :     ts = CLASS_DATA (sym)->ts;
    8347                 :             :   else
    8348                 :      122636 :     ts = sym->ts;
    8349                 :             : 
    8350                 :      125259 :   if (ts.type == BT_CHARACTER && sym->attr.is_bind_c)
    8351                 :         186 :     se->string_length = build_int_cst (gfc_charlen_type_node, 1);
    8352                 :      125073 :   else if (ts.type == BT_CHARACTER)
    8353                 :             :     {
    8354                 :        4733 :       if (ts.u.cl->length == NULL)
    8355                 :             :         {
    8356                 :             :           /* Assumed character length results are not allowed by C418 of the 2003
    8357                 :             :              standard and are trapped in resolve.cc; except in the case of SPREAD
    8358                 :             :              (and other intrinsics?) and dummy functions.  In the case of SPREAD,
    8359                 :             :              we take the character length of the first argument for the result.
    8360                 :             :              For dummies, we have to look through the formal argument list for
    8361                 :             :              this function and use the character length found there.
    8362                 :             :              Likewise, we handle the case of deferred-length character dummy
    8363                 :             :              arguments to intrinsics that determine the characteristics of
    8364                 :             :              the result, which cannot be deferred-length.  */
    8365                 :        2213 :           if (expr->value.function.isym)
    8366                 :        1701 :             ts.deferred = false;
    8367                 :        2213 :           if (ts.deferred)
    8368                 :         507 :             cl.backend_decl = gfc_create_var (gfc_charlen_type_node, "slen");
    8369                 :        1706 :           else if (!sym->attr.dummy)
    8370                 :        1701 :             cl.backend_decl = (*stringargs)[0];
    8371                 :             :           else
    8372                 :             :             {
    8373                 :           5 :               formal = gfc_sym_get_dummy_args (sym->ns->proc_name);
    8374                 :          18 :               for (; formal; formal = formal->next)
    8375                 :           8 :                 if (strcmp (formal->sym->name, sym->name) == 0)
    8376                 :           5 :                   cl.backend_decl = formal->sym->ts.u.cl->backend_decl;
    8377                 :             :             }
    8378                 :        2213 :           len = cl.backend_decl;
    8379                 :             :         }
    8380                 :             :       else
    8381                 :             :         {
    8382                 :        2520 :           tree tmp;
    8383                 :             : 
    8384                 :             :           /* Calculate the length of the returned string.  */
    8385                 :        2520 :           gfc_init_se (&parmse, NULL);
    8386                 :        2520 :           if (need_interface_mapping)
    8387                 :        1867 :             gfc_apply_interface_mapping (&mapping, &parmse, ts.u.cl->length);
    8388                 :             :           else
    8389                 :         653 :             gfc_conv_expr (&parmse, ts.u.cl->length);
    8390                 :        2520 :           gfc_add_block_to_block (&se->pre, &parmse.pre);
    8391                 :        2520 :           gfc_add_block_to_block (&se->post, &parmse.post);
    8392                 :        2520 :           tmp = parmse.expr;
    8393                 :             :           /* TODO: It would be better to have the charlens as
    8394                 :             :              gfc_charlen_type_node already when the interface is
    8395                 :             :              created instead of converting it here (see PR 84615).  */
    8396                 :        2520 :           tmp = fold_build2_loc (input_location, MAX_EXPR,
    8397                 :             :                                  gfc_charlen_type_node,
    8398                 :             :                                  fold_convert (gfc_charlen_type_node, tmp),
    8399                 :             :                                  build_zero_cst (gfc_charlen_type_node));
    8400                 :        2520 :           cl.backend_decl = tmp;
    8401                 :             :         }
    8402                 :             : 
    8403                 :             :       /* Set up a charlen structure for it.  */
    8404                 :        4733 :       cl.next = NULL;
    8405                 :        4733 :       cl.length = NULL;
    8406                 :        4733 :       ts.u.cl = &cl;
    8407                 :             : 
    8408                 :        4733 :       len = cl.backend_decl;
    8409                 :             :     }
    8410                 :             : 
    8411                 :        1793 :   byref = (comp && (comp->attr.dimension
    8412                 :        1724 :            || (comp->ts.type == BT_CHARACTER && !sym->attr.is_bind_c)))
    8413                 :      125259 :            || (!comp && gfc_return_by_reference (sym));
    8414                 :             : 
    8415                 :       17486 :   if (byref)
    8416                 :             :     {
    8417                 :       17486 :       if (se->direct_byref)
    8418                 :             :         {
    8419                 :             :           /* Sometimes, too much indirection can be applied; e.g. for
    8420                 :             :              function_result = array_valued_recursive_function.  */
    8421                 :        7145 :           if (TREE_TYPE (TREE_TYPE (se->expr))
    8422                 :        7145 :                 && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))
    8423                 :        7163 :                 && GFC_DESCRIPTOR_TYPE_P
    8424                 :             :                         (TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr)))))
    8425                 :          18 :             se->expr = build_fold_indirect_ref_loc (input_location,
    8426                 :             :                                                     se->expr);
    8427                 :             : 
    8428                 :             :           /* If the lhs of an assignment x = f(..) is allocatable and
    8429                 :             :              f2003 is allowed, we must do the automatic reallocation.
    8430                 :             :              TODO - deal with intrinsics, without using a temporary.  */
    8431                 :        7145 :           if (flag_realloc_lhs
    8432                 :        7070 :                 && se->ss && se->ss->loop_chain
    8433                 :         160 :                 && se->ss->loop_chain->is_alloc_lhs
    8434                 :         160 :                 && !expr->value.function.isym
    8435                 :         160 :                 && sym->result->as != NULL)
    8436                 :             :             {
    8437                 :             :               /* Evaluate the bounds of the result, if known.  */
    8438                 :         160 :               gfc_set_loop_bounds_from_array_spec (&mapping, se,
    8439                 :             :                                                    sym->result->as);
    8440                 :             : 
    8441                 :             :               /* Perform the automatic reallocation.  */
    8442                 :         160 :               tmp = gfc_alloc_allocatable_for_assignment (se->loop,
    8443                 :             :                                                           expr, NULL);
    8444                 :         160 :               gfc_add_expr_to_block (&se->pre, tmp);
    8445                 :             : 
    8446                 :             :               /* Pass the temporary as the first argument.  */
    8447                 :         160 :               result = info->descriptor;
    8448                 :             :             }
    8449                 :             :           else
    8450                 :        6985 :             result = build_fold_indirect_ref_loc (input_location,
    8451                 :             :                                                   se->expr);
    8452                 :        7145 :           vec_safe_push (retargs, se->expr);
    8453                 :             :         }
    8454                 :       10341 :       else if (comp && comp->attr.dimension)
    8455                 :             :         {
    8456                 :          66 :           gcc_assert (se->loop && info);
    8457                 :             : 
    8458                 :             :           /* Set the type of the array. vtable charlens are not always reliable.
    8459                 :             :              Use the interface, if possible.  */
    8460                 :          66 :           if (comp->ts.type == BT_CHARACTER
    8461                 :           1 :               && expr->symtree->n.sym->ts.type == BT_CLASS
    8462                 :           1 :               && comp->ts.interface && comp->ts.interface->result)
    8463                 :           1 :             tmp = gfc_typenode_for_spec (&comp->ts.interface->result->ts);
    8464                 :             :           else
    8465                 :          65 :             tmp = gfc_typenode_for_spec (&comp->ts);
    8466                 :          66 :           gcc_assert (se->ss->dimen == se->loop->dimen);
    8467                 :             : 
    8468                 :             :           /* Evaluate the bounds of the result, if known.  */
    8469                 :          66 :           gfc_set_loop_bounds_from_array_spec (&mapping, se, comp->as);
    8470                 :             : 
    8471                 :             :           /* If the lhs of an assignment x = f(..) is allocatable and
    8472                 :             :              f2003 is allowed, we must not generate the function call
    8473                 :             :              here but should just send back the results of the mapping.
    8474                 :             :              This is signalled by the function ss being flagged.  */
    8475                 :          66 :           if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
    8476                 :             :             {
    8477                 :           0 :               gfc_free_interface_mapping (&mapping);
    8478                 :           0 :               return has_alternate_specifier;
    8479                 :             :             }
    8480                 :             : 
    8481                 :             :           /* Create a temporary to store the result.  In case the function
    8482                 :             :              returns a pointer, the temporary will be a shallow copy and
    8483                 :             :              mustn't be deallocated.  */
    8484                 :          66 :           callee_alloc = comp->attr.allocatable || comp->attr.pointer;
    8485                 :          66 :           gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
    8486                 :             :                                        tmp, NULL_TREE, false,
    8487                 :             :                                        !comp->attr.pointer, callee_alloc,
    8488                 :          66 :                                        &se->ss->info->expr->where);
    8489                 :             : 
    8490                 :             :           /* Pass the temporary as the first argument.  */
    8491                 :          66 :           result = info->descriptor;
    8492                 :          66 :           tmp = gfc_build_addr_expr (NULL_TREE, result);
    8493                 :          66 :           vec_safe_push (retargs, tmp);
    8494                 :             :         }
    8495                 :       10208 :       else if (!comp && sym->result->attr.dimension)
    8496                 :             :         {
    8497                 :        7302 :           gcc_assert (se->loop && info);
    8498                 :             : 
    8499                 :             :           /* Set the type of the array.  */
    8500                 :        7302 :           tmp = gfc_typenode_for_spec (&ts);
    8501                 :        7302 :           tmp = arg1_cntnr ? TREE_TYPE (arg1_cntnr) : tmp;
    8502                 :        7302 :           gcc_assert (se->ss->dimen == se->loop->dimen);
    8503                 :             : 
    8504                 :             :           /* Evaluate the bounds of the result, if known.  */
    8505                 :        7302 :           gfc_set_loop_bounds_from_array_spec (&mapping, se, sym->result->as);
    8506                 :             : 
    8507                 :             :           /* If the lhs of an assignment x = f(..) is allocatable and
    8508                 :             :              f2003 is allowed, we must not generate the function call
    8509                 :             :              here but should just send back the results of the mapping.
    8510                 :             :              This is signalled by the function ss being flagged.  */
    8511                 :        7302 :           if (flag_realloc_lhs && se->ss && se->ss->is_alloc_lhs)
    8512                 :             :             {
    8513                 :           0 :               gfc_free_interface_mapping (&mapping);
    8514                 :           0 :               return has_alternate_specifier;
    8515                 :             :             }
    8516                 :             : 
    8517                 :             :           /* Create a temporary to store the result.  In case the function
    8518                 :             :              returns a pointer, the temporary will be a shallow copy and
    8519                 :             :              mustn't be deallocated.  */
    8520                 :        7302 :           callee_alloc = sym->attr.allocatable || sym->attr.pointer;
    8521                 :        7302 :           gfc_trans_create_temp_array (&se->pre, &se->post, se->ss,
    8522                 :             :                                        tmp, NULL_TREE, false,
    8523                 :             :                                        !sym->attr.pointer, callee_alloc,
    8524                 :        7302 :                                        &se->ss->info->expr->where);
    8525                 :             : 
    8526                 :             :           /* Pass the temporary as the first argument.  */
    8527                 :        7302 :           result = info->descriptor;
    8528                 :        7302 :           tmp = gfc_build_addr_expr (NULL_TREE, result);
    8529                 :        7302 :           vec_safe_push (retargs, tmp);
    8530                 :             :         }
    8531                 :        2973 :       else if (ts.type == BT_CHARACTER)
    8532                 :             :         {
    8533                 :             :           /* Pass the string length.  */
    8534                 :        2912 :           type = gfc_get_character_type (ts.kind, ts.u.cl);
    8535                 :        2912 :           type = build_pointer_type (type);
    8536                 :             : 
    8537                 :             :           /* Emit a DECL_EXPR for the VLA type.  */
    8538                 :        2912 :           tmp = TREE_TYPE (type);
    8539                 :        2912 :           if (TYPE_SIZE (tmp)
    8540                 :        2912 :               && TREE_CODE (TYPE_SIZE (tmp)) != INTEGER_CST)
    8541                 :             :             {
    8542                 :        1835 :               tmp = build_decl (input_location, TYPE_DECL, NULL_TREE, tmp);
    8543                 :        1835 :               DECL_ARTIFICIAL (tmp) = 1;
    8544                 :        1835 :               DECL_IGNORED_P (tmp) = 1;
    8545                 :        1835 :               tmp = fold_build1_loc (input_location, DECL_EXPR,
    8546                 :        1835 :                                      TREE_TYPE (tmp), tmp);
    8547                 :        1835 :               gfc_add_expr_to_block (&se->pre, tmp);
    8548                 :             :             }
    8549                 :             : 
    8550                 :             :           /* Return an address to a char[0:len-1]* temporary for
    8551                 :             :              character pointers.  */
    8552                 :        2912 :           if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
    8553                 :          67 :                || (comp && (comp->attr.pointer || comp->attr.allocatable)))
    8554                 :             :             {
    8555                 :         550 :               var = gfc_create_var (type, "pstr");
    8556                 :             : 
    8557                 :         550 :               if ((!comp && sym->attr.allocatable)
    8558                 :          21 :                   || (comp && comp->attr.allocatable))
    8559                 :             :                 {
    8560                 :         265 :                   gfc_add_modify (&se->pre, var,
    8561                 :         265 :                                   fold_convert (TREE_TYPE (var),
    8562                 :             :                                                 null_pointer_node));
    8563                 :         265 :                   tmp = gfc_call_free (var);
    8564                 :         265 :                   gfc_add_expr_to_block (&se->post, tmp);
    8565                 :             :                 }
    8566                 :             : 
    8567                 :             :               /* Provide an address expression for the function arguments.  */
    8568                 :         550 :               var = gfc_build_addr_expr (NULL_TREE, var);
    8569                 :             :             }
    8570                 :             :           else
    8571                 :        2362 :             var = gfc_conv_string_tmp (se, type, len);
    8572                 :             : 
    8573                 :        2912 :           vec_safe_push (retargs, var);
    8574                 :             :         }
    8575                 :             :       else
    8576                 :             :         {
    8577                 :          61 :           gcc_assert (flag_f2c && ts.type == BT_COMPLEX);
    8578                 :             : 
    8579                 :          61 :           type = gfc_get_complex_type (ts.kind);
    8580                 :          61 :           var = gfc_build_addr_expr (NULL_TREE, gfc_create_var (type, "cmplx"));
    8581                 :          61 :           vec_safe_push (retargs, var);
    8582                 :             :         }
    8583                 :             : 
    8584                 :             :       /* Add the string length to the argument list.  */
    8585                 :       17486 :       if (ts.type == BT_CHARACTER && ts.deferred)
    8586                 :             :         {
    8587                 :         507 :           tmp = len;
    8588                 :         507 :           if (!VAR_P (tmp))
    8589                 :           0 :             tmp = gfc_evaluate_now (len, &se->pre);
    8590                 :         507 :           TREE_STATIC (tmp) = 1;
    8591                 :         507 :           gfc_add_modify (&se->pre, tmp,
    8592                 :         507 :                           build_int_cst (TREE_TYPE (tmp), 0));
    8593                 :         507 :           tmp = gfc_build_addr_expr (NULL_TREE, tmp);
    8594                 :         507 :           vec_safe_push (retargs, tmp);
    8595                 :             :         }
    8596                 :       16979 :       else if (ts.type == BT_CHARACTER)
    8597                 :        4226 :         vec_safe_push (retargs, len);
    8598                 :             :     }
    8599                 :             : 
    8600                 :      125259 :   gfc_free_interface_mapping (&mapping);
    8601                 :             : 
    8602                 :             :   /* We need to glom RETARGS + ARGLIST + STRINGARGS + APPEND_ARGS.  */
    8603                 :      233461 :   arglen = (vec_safe_length (arglist) + vec_safe_length (optionalargs)
    8604                 :      149793 :             + vec_safe_length (stringargs) + vec_safe_length (append_args));
    8605                 :      125259 :   vec_safe_reserve (retargs, arglen);
    8606                 :             : 
    8607                 :             :   /* Add the return arguments.  */
    8608                 :      125259 :   vec_safe_splice (retargs, arglist);
    8609                 :             : 
    8610                 :             :   /* Add the hidden present status for optional+value to the arguments.  */
    8611                 :      125259 :   vec_safe_splice (retargs, optionalargs);
    8612                 :             : 
    8613                 :             :   /* Add the hidden string length parameters to the arguments.  */
    8614                 :      125259 :   vec_safe_splice (retargs, stringargs);
    8615                 :             : 
    8616                 :             :   /* We may want to append extra arguments here.  This is used e.g. for
    8617                 :             :      calls to libgfortran_matmul_??, which need extra information.  */
    8618                 :      125259 :   vec_safe_splice (retargs, append_args);
    8619                 :             : 
    8620                 :      125259 :   arglist = retargs;
    8621                 :             : 
    8622                 :             :   /* Generate the actual call.  */
    8623                 :      125259 :   is_builtin = false;
    8624                 :      125259 :   if (base_object == NULL_TREE)
    8625                 :      125179 :     conv_function_val (se, &is_builtin, sym, expr, args);
    8626                 :             :   else
    8627                 :          80 :     conv_base_obj_fcn_val (se, base_object, expr);
    8628                 :             : 
    8629                 :             :   /* If there are alternate return labels, function type should be
    8630                 :             :      integer.  Can't modify the type in place though, since it can be shared
    8631                 :             :      with other functions.  For dummy arguments, the typing is done to
    8632                 :             :      this result, even if it has to be repeated for each call.  */
    8633                 :      125259 :   if (has_alternate_specifier
    8634                 :      125259 :       && TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) != integer_type_node)
    8635                 :             :     {
    8636                 :           7 :       if (!sym->attr.dummy)
    8637                 :             :         {
    8638                 :           0 :           TREE_TYPE (sym->backend_decl)
    8639                 :           0 :                 = build_function_type (integer_type_node,
    8640                 :           0 :                       TYPE_ARG_TYPES (TREE_TYPE (sym->backend_decl)));
    8641                 :           0 :           se->expr = gfc_build_addr_expr (NULL_TREE, sym->backend_decl);
    8642                 :             :         }
    8643                 :             :       else
    8644                 :           7 :         TREE_TYPE (TREE_TYPE (TREE_TYPE (se->expr))) = integer_type_node;
    8645                 :             :     }
    8646                 :             : 
    8647                 :      125259 :   fntype = TREE_TYPE (TREE_TYPE (se->expr));
    8648                 :      125259 :   se->expr = build_call_vec (TREE_TYPE (fntype), se->expr, arglist);
    8649                 :             : 
    8650                 :      125259 :   if (is_builtin)
    8651                 :         413 :     se->expr = update_builtin_function (se->expr, sym);
    8652                 :             : 
    8653                 :             :   /* Allocatable scalar function results must be freed and nullified
    8654                 :             :      after use. This necessitates the creation of a temporary to
    8655                 :             :      hold the result to prevent duplicate calls.  */
    8656                 :      125259 :   symbol_attribute attr =  comp ? comp->attr : sym->attr;
    8657                 :      125259 :   bool allocatable = attr.allocatable && !attr.dimension;
    8658                 :      125259 :   gfc_symbol *der = comp ?
    8659                 :        1793 :                     comp->ts.type == BT_DERIVED ? comp->ts.u.derived : NULL
    8660                 :             :                          :
    8661                 :      123466 :                     sym->ts.type == BT_DERIVED ? sym->ts.u.derived : NULL;
    8662                 :        2868 :   bool finalizable = der != NULL && der->ns->proc_name
    8663                 :        5733 :                             && gfc_is_finalizable (der, NULL);
    8664                 :             : 
    8665                 :      125259 :   if (!byref && finalizable)
    8666                 :         149 :     gfc_finalize_tree_expr (se, der, attr, expr->rank);
    8667                 :             : 
    8668                 :      125259 :   if (!byref && sym->ts.type != BT_CHARACTER
    8669                 :      107587 :       && allocatable && !finalizable)
    8670                 :             :     {
    8671                 :         224 :       tmp = gfc_create_var (TREE_TYPE (se->expr), NULL);
    8672                 :         224 :       gfc_add_modify (&se->pre, tmp, se->expr);
    8673                 :         224 :       se->expr = tmp;
    8674                 :         224 :       tmp = gfc_call_free (tmp);
    8675                 :         224 :       gfc_add_expr_to_block (&post, tmp);
    8676                 :         224 :       gfc_add_modify (&post, se->expr, build_int_cst (TREE_TYPE (se->expr), 0));
    8677                 :             :     }
    8678                 :             : 
    8679                 :             :   /* If we have a pointer function, but we don't want a pointer, e.g.
    8680                 :             :      something like
    8681                 :             :         x = f()
    8682                 :             :      where f is pointer valued, we have to dereference the result.  */
    8683                 :      125259 :   if (!se->want_pointer && !byref
    8684                 :      107198 :       && ((!comp && (sym->attr.pointer || sym->attr.allocatable))
    8685                 :        1613 :           || (comp && (comp->attr.pointer || comp->attr.allocatable))))
    8686                 :         450 :     se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
    8687                 :             : 
    8688                 :             :   /* f2c calling conventions require a scalar default real function to
    8689                 :             :      return a double precision result.  Convert this back to default
    8690                 :             :      real.  We only care about the cases that can happen in Fortran 77.
    8691                 :             :   */
    8692                 :      125259 :   if (flag_f2c && sym->ts.type == BT_REAL
    8693                 :          97 :       && sym->ts.kind == gfc_default_real_kind
    8694                 :             :       && !sym->attr.pointer
    8695                 :          73 :       && !sym->attr.allocatable
    8696                 :          42 :       && !sym->attr.always_explicit)
    8697                 :          42 :     se->expr = fold_convert (gfc_get_real_type (sym->ts.kind), se->expr);
    8698                 :             : 
    8699                 :             :   /* A pure function may still have side-effects - it may modify its
    8700                 :             :      parameters.  */
    8701                 :      125259 :   TREE_SIDE_EFFECTS (se->expr) = 1;
    8702                 :             : #if 0
    8703                 :             :   if (!sym->attr.pure)
    8704                 :             :     TREE_SIDE_EFFECTS (se->expr) = 1;
    8705                 :             : #endif
    8706                 :             : 
    8707                 :      125259 :   if (byref)
    8708                 :             :     {
    8709                 :             :       /* Add the function call to the pre chain.  There is no expression.  */
    8710                 :       17486 :       gfc_add_expr_to_block (&se->pre, se->expr);
    8711                 :       17486 :       se->expr = NULL_TREE;
    8712                 :             : 
    8713                 :       17486 :       if (!se->direct_byref)
    8714                 :             :         {
    8715                 :       10341 :           if ((sym->attr.dimension && !comp) || (comp && comp->attr.dimension))
    8716                 :             :             {
    8717                 :        7368 :               if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
    8718                 :             :                 {
    8719                 :             :                   /* Check the data pointer hasn't been modified.  This would
    8720                 :             :                      happen in a function returning a pointer.  */
    8721                 :         251 :                   tmp = gfc_conv_descriptor_data_get (info->descriptor);
    8722                 :         251 :                   tmp = fold_build2_loc (input_location, NE_EXPR,
    8723                 :             :                                          logical_type_node,
    8724                 :             :                                          tmp, info->data);
    8725                 :         251 :                   gfc_trans_runtime_check (true, false, tmp, &se->pre, NULL,
    8726                 :             :                                            gfc_msg_fault);
    8727                 :             :                 }
    8728                 :        7368 :               se->expr = info->descriptor;
    8729                 :             :               /* Bundle in the string length.  */
    8730                 :        7368 :               se->string_length = len;
    8731                 :             : 
    8732                 :        7368 :               if (finalizable)
    8733                 :           6 :                 gfc_finalize_tree_expr (se, der, attr, expr->rank);
    8734                 :             :             }
    8735                 :        2973 :           else if (ts.type == BT_CHARACTER)
    8736                 :             :             {
    8737                 :             :               /* Dereference for character pointer results.  */
    8738                 :        2912 :               if ((!comp && (sym->attr.pointer || sym->attr.allocatable))
    8739                 :          67 :                   || (comp && (comp->attr.pointer || comp->attr.allocatable)))
    8740                 :         550 :                 se->expr = build_fold_indirect_ref_loc (input_location, var);
    8741                 :             :               else
    8742                 :        2362 :                 se->expr = var;
    8743                 :             : 
    8744                 :        2912 :               se->string_length = len;
    8745                 :             :             }
    8746                 :             :           else
    8747                 :             :             {
    8748                 :          61 :               gcc_assert (ts.type == BT_COMPLEX && flag_f2c);
    8749                 :          61 :               se->expr = build_fold_indirect_ref_loc (input_location, var);
    8750                 :             :             }
    8751                 :             :         }
    8752                 :             :     }
    8753                 :             : 
    8754                 :             :   /* Associate the rhs class object's meta-data with the result, when the
    8755                 :             :      result is a temporary.  */
    8756                 :      108207 :   if (args && args->expr && args->expr->ts.type == BT_CLASS
    8757                 :        4800 :       && sym->ts.type == BT_CLASS && result != NULL_TREE && DECL_P (result)
    8758                 :      125291 :       && !GFC_CLASS_TYPE_P (TREE_TYPE (result)))
    8759                 :             :     {
    8760                 :          32 :       gfc_se parmse;
    8761                 :          32 :       gfc_expr *class_expr = gfc_find_and_cut_at_last_class_ref (args->expr);
    8762                 :             : 
    8763                 :          32 :       gfc_init_se (&parmse, NULL);
    8764                 :          32 :       parmse.data_not_needed = 1;
    8765                 :          32 :       gfc_conv_expr (&parmse, class_expr);
    8766                 :          32 :       if (!DECL_LANG_SPECIFIC (result))
    8767                 :          32 :         gfc_allocate_lang_decl (result);
    8768                 :          32 :       GFC_DECL_SAVED_DESCRIPTOR (result) = parmse.expr;
    8769                 :          32 :       gfc_free_expr (class_expr);
    8770                 :             :       /* -fcheck= can add diagnostic code, which has to be placed before
    8771                 :             :          the call. */
    8772                 :          32 :       if (parmse.pre.head != NULL)
    8773                 :          12 :           gfc_add_expr_to_block (&se->pre, parmse.pre.head);
    8774                 :          32 :       gcc_assert (parmse.post.head == NULL_TREE);
    8775                 :             :     }
    8776                 :             : 
    8777                 :             :   /* Follow the function call with the argument post block.  */
    8778                 :      125259 :   if (byref)
    8779                 :             :     {
    8780                 :       17486 :       gfc_add_block_to_block (&se->pre, &post);
    8781                 :             : 
    8782                 :             :       /* Transformational functions of derived types with allocatable
    8783                 :             :          components must have the result allocatable components copied when the
    8784                 :             :          argument is actually given.  This is unnecessry for REDUCE because the
    8785                 :             :          wrapper for the OPERATION function takes care of this.  */
    8786                 :       17486 :       arg = expr->value.function.actual;
    8787                 :       17486 :       if (result && arg && expr->rank
    8788                 :       13723 :           && isym && isym->transformational
    8789                 :       12179 :           && isym->id != GFC_ISYM_REDUCE
    8790                 :       12053 :           && arg->expr
    8791                 :       12011 :           && arg->expr->ts.type == BT_DERIVED
    8792                 :         221 :           && arg->expr->ts.u.derived->attr.alloc_comp)
    8793                 :             :         {
    8794                 :          36 :           tree tmp2;
    8795                 :             :           /* Copy the allocatable components.  We have to use a
    8796                 :             :              temporary here to prevent source allocatable components
    8797                 :             :              from being corrupted.  */
    8798                 :          36 :           tmp2 = gfc_evaluate_now (result, &se->pre);
    8799                 :          36 :           tmp = gfc_copy_alloc_comp (arg->expr->ts.u.derived,
    8800                 :             :                                      result, tmp2, expr->rank, 0);
    8801                 :          36 :           gfc_add_expr_to_block (&se->pre, tmp);
    8802                 :          36 :           tmp = gfc_copy_allocatable_data (result, tmp2, TREE_TYPE(tmp2),
    8803                 :             :                                            expr->rank);
    8804                 :          36 :           gfc_add_expr_to_block (&se->pre, tmp);
    8805                 :             : 
    8806                 :             :           /* Finally free the temporary's data field.  */
    8807                 :          36 :           tmp = gfc_conv_descriptor_data_get (tmp2);
    8808                 :          36 :           tmp = gfc_deallocate_with_status (tmp, NULL_TREE, NULL_TREE,
    8809                 :             :                                             NULL_TREE, NULL_TREE, true,
    8810                 :             :                                             NULL, GFC_CAF_COARRAY_NOCOARRAY);
    8811                 :          36 :           gfc_add_expr_to_block (&se->pre, tmp);
    8812                 :             :         }
    8813                 :             :     }
    8814                 :             :   else
    8815                 :             :     {
    8816                 :             :       /* For a function with a class array result, save the result as
    8817                 :             :          a temporary, set the info fields needed by the scalarizer and
    8818                 :             :          call the finalization function of the temporary. Note that the
    8819                 :             :          nullification of allocatable components needed by the result
    8820                 :             :          is done in gfc_trans_assignment_1.  */
    8821                 :       33266 :       if (expr && (gfc_is_class_array_function (expr)
    8822                 :       32957 :                    || gfc_is_alloc_class_scalar_function (expr))
    8823                 :         828 :           && se->expr && GFC_CLASS_TYPE_P (TREE_TYPE (se->expr))
    8824                 :      108589 :           && expr->must_finalize)
    8825                 :             :         {
    8826                 :         315 :           int n;
    8827                 :         315 :           if (se->ss && se->ss->loop)
    8828                 :             :             {
    8829                 :         177 :               gfc_add_block_to_block (&se->ss->loop->pre, &se->pre);
    8830                 :         177 :               se->expr = gfc_evaluate_now (se->expr, &se->ss->loop->pre);
    8831                 :         177 :               tmp = gfc_class_data_get (se->expr);
    8832                 :         177 :               info->descriptor = tmp;
    8833                 :         177 :               info->data = gfc_conv_descriptor_data_get (tmp);
    8834                 :         177 :               info->offset = gfc_conv_descriptor_offset_get (tmp);
    8835                 :         366 :               for (n = 0; n < se->ss->loop->dimen; n++)
    8836                 :             :                 {
    8837                 :         189 :                   tree dim = gfc_rank_cst[n];
    8838                 :         189 :                   se->ss->loop->to[n] = gfc_conv_descriptor_ubound_get (tmp, dim);
    8839                 :         189 :                   se->ss->loop->from[n] = gfc_conv_descriptor_lbound_get (tmp, dim);
    8840                 :             :                 }
    8841                 :             :             }
    8842                 :             :           else
    8843                 :             :             {
    8844                 :             :               /* TODO Eliminate the doubling of temporaries. This
    8845                 :             :                  one is necessary to ensure no memory leakage.  */
    8846                 :         138 :               se->expr = gfc_evaluate_now (se->expr, &se->pre);
    8847                 :             :             }
    8848                 :             : 
    8849                 :             :           /* Finalize the result, if necessary.  */
    8850                 :         630 :           attr = expr->value.function.esym
    8851                 :         315 :                  ? CLASS_DATA (expr->value.function.esym->result)->attr
    8852                 :          14 :                  : CLASS_DATA (expr)->attr;
    8853                 :         315 :           if (!((gfc_is_class_array_function (expr)
    8854                 :         108 :                  || gfc_is_alloc_class_scalar_function (expr))
    8855                 :         315 :                 && attr.pointer))
    8856                 :         270 :             gfc_finalize_tree_expr (se, NULL, attr, expr->rank);
    8857                 :             :         }
    8858                 :      107773 :       gfc_add_block_to_block (&se->post, &post);
    8859                 :             :     }
    8860                 :             : 
    8861                 :             :   return has_alternate_specifier;
    8862                 :             : }
    8863                 :             : 
    8864                 :             : 
    8865                 :             : /* Fill a character string with spaces.  */
    8866                 :             : 
    8867                 :             : static tree
    8868                 :       28492 : fill_with_spaces (tree start, tree type, tree size)
    8869                 :             : {
    8870                 :       28492 :   stmtblock_t block, loop;
    8871                 :       28492 :   tree i, el, exit_label, cond, tmp;
    8872                 :             : 
    8873                 :             :   /* For a simple char type, we can call memset().  */
    8874                 :       28492 :   if (compare_tree_int (TYPE_SIZE_UNIT (type), 1) == 0)
    8875                 :       47530 :     return build_call_expr_loc (input_location,
    8876                 :             :                             builtin_decl_explicit (BUILT_IN_MEMSET),
    8877                 :             :                             3, start,
    8878                 :       23765 :                             build_int_cst (gfc_get_int_type (gfc_c_int_kind),
    8879                 :       23765 :                                            lang_hooks.to_target_charset (' ')),
    8880                 :             :                                 fold_convert (size_type_node, size));
    8881                 :             : 
    8882                 :             :   /* Otherwise, we use a loop:
    8883                 :             :         for (el = start, i = size; i > 0; el--, i+= TYPE_SIZE_UNIT (type))
    8884                 :             :           *el = (type) ' ';
    8885                 :             :    */
    8886                 :             : 
    8887                 :             :   /* Initialize variables.  */
    8888                 :        4727 :   gfc_init_block (&block);
    8889                 :        4727 :   i = gfc_create_var (sizetype, "i");
    8890                 :        4727 :   gfc_add_modify (&block, i, fold_convert (sizetype, size));
    8891                 :        4727 :   el = gfc_create_var (build_pointer_type (type), "el");
    8892                 :        4727 :   gfc_add_modify (&block, el, fold_convert (TREE_TYPE (el), start));
    8893                 :        4727 :   exit_label = gfc_build_label_decl (NULL_TREE);
    8894                 :        4727 :   TREE_USED (exit_label) = 1;
    8895                 :             : 
    8896                 :             : 
    8897                 :             :   /* Loop body.  */
    8898                 :        4727 :   gfc_init_block (&loop);
    8899                 :             : 
    8900                 :             :   /* Exit condition.  */
    8901                 :        4727 :   cond = fold_build2_loc (input_location, LE_EXPR, logical_type_node, i,
    8902                 :             :                           build_zero_cst (sizetype));
    8903                 :        4727 :   tmp = build1_v (GOTO_EXPR, exit_label);
    8904                 :        4727 :   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
    8905                 :             :                          build_empty_stmt (input_location));
    8906                 :        4727 :   gfc_add_expr_to_block (&loop, tmp);
    8907                 :             : 
    8908                 :             :   /* Assignment.  */
    8909                 :        4727 :   gfc_add_modify (&loop,
    8910                 :             :                   fold_build1_loc (input_location, INDIRECT_REF, type, el),
    8911                 :        4727 :                   build_int_cst (type, lang_hooks.to_target_charset (' ')));
    8912                 :             : 
    8913                 :             :   /* Increment loop variables.  */
    8914                 :        4727 :   gfc_add_modify (&loop, i,
    8915                 :             :                   fold_build2_loc (input_location, MINUS_EXPR, sizetype, i,
    8916                 :        4727 :                                    TYPE_SIZE_UNIT (type)));
    8917                 :        4727 :   gfc_add_modify (&loop, el,
    8918                 :             :                   fold_build_pointer_plus_loc (input_location,
    8919                 :        4727 :                                                el, TYPE_SIZE_UNIT (type)));
    8920                 :             : 
    8921                 :             :   /* Making the loop... actually loop!  */
    8922                 :        4727 :   tmp = gfc_finish_block (&loop);
    8923                 :        4727 :   tmp = build1_v (LOOP_EXPR, tmp);
    8924                 :        4727 :   gfc_add_expr_to_block (&block, tmp);
    8925                 :             : 
    8926                 :             :   /* The exit label.  */
    8927                 :        4727 :   tmp = build1_v (LABEL_EXPR, exit_label);
    8928                 :        4727 :   gfc_add_expr_to_block (&block, tmp);
    8929                 :             : 
    8930                 :             : 
    8931                 :        4727 :   return gfc_finish_block (&block);
    8932                 :             : }
    8933                 :             : 
    8934                 :             : 
    8935                 :             : /* Generate code to copy a string.  */
    8936                 :             : 
    8937                 :             : void
    8938                 :       33409 : gfc_trans_string_copy (stmtblock_t * block, tree dlength, tree dest,
    8939                 :             :                        int dkind, tree slength, tree src, int skind)
    8940                 :             : {
    8941                 :       33409 :   tree tmp, dlen, slen;
    8942                 :       33409 :   tree dsc;
    8943                 :       33409 :   tree ssc;
    8944                 :       33409 :   tree cond;
    8945                 :       33409 :   tree cond2;
    8946                 :       33409 :   tree tmp2;
    8947                 :       33409 :   tree tmp3;
    8948                 :       33409 :   tree tmp4;
    8949                 :       33409 :   tree chartype;
    8950                 :       33409 :   stmtblock_t tempblock;
    8951                 :             : 
    8952                 :       33409 :   gcc_assert (dkind == skind);
    8953                 :             : 
    8954                 :       33409 :   if (slength != NULL_TREE)
    8955                 :             :     {
    8956                 :       33409 :       slen = gfc_evaluate_now (fold_convert (gfc_charlen_type_node, slength), block);
    8957                 :       33409 :       ssc = gfc_string_to_single_character (slen, src, skind);
    8958                 :             :     }
    8959                 :             :   else
    8960                 :             :     {
    8961                 :           0 :       slen = build_one_cst (gfc_charlen_type_node);
    8962                 :           0 :       ssc =  src;
    8963                 :             :     }
    8964                 :             : 
    8965                 :       33409 :   if (dlength != NULL_TREE)
    8966                 :             :     {
    8967                 :       33409 :       dlen = gfc_evaluate_now (fold_convert (gfc_charlen_type_node, dlength), block);
    8968                 :       33409 :       dsc = gfc_string_to_single_character (dlen, dest, dkind);
    8969                 :             :     }
    8970                 :             :   else
    8971                 :             :     {
    8972                 :           0 :       dlen = build_one_cst (gfc_charlen_type_node);
    8973                 :           0 :       dsc =  dest;
    8974                 :             :     }
    8975                 :             : 
    8976                 :             :   /* Assign directly if the types are compatible.  */
    8977                 :       33409 :   if (dsc != NULL_TREE && ssc != NULL_TREE
    8978                 :       33409 :       && TREE_TYPE (dsc) == TREE_TYPE (ssc))
    8979                 :             :     {
    8980                 :        4917 :       gfc_add_modify (block, dsc, ssc);
    8981                 :        4917 :       return;
    8982                 :             :     }
    8983                 :             : 
    8984                 :             :   /* The string copy algorithm below generates code like
    8985                 :             : 
    8986                 :             :      if (destlen > 0)
    8987                 :             :        {
    8988                 :             :          if (srclen < destlen)
    8989                 :             :            {
    8990                 :             :              memmove (dest, src, srclen);
    8991                 :             :              // Pad with spaces.
    8992                 :             :              memset (&dest[srclen], ' ', destlen - srclen);
    8993                 :             :            }
    8994                 :             :          else
    8995                 :             :            {
    8996                 :             :              // Truncate if too long.
    8997                 :             :              memmove (dest, src, destlen);
    8998                 :             :            }
    8999                 :             :        }
    9000                 :             :   */
    9001                 :             : 
    9002                 :             :   /* Do nothing if the destination length is zero.  */
    9003                 :       28492 :   cond = fold_build2_loc (input_location, GT_EXPR, logical_type_node, dlen,
    9004                 :       28492 :                           build_zero_cst (TREE_TYPE (dlen)));
    9005                 :             : 
    9006                 :             :   /* For non-default character kinds, we have to multiply the string
    9007                 :             :      length by the base type size.  */
    9008                 :       28492 :   chartype = gfc_get_char_type (dkind);
    9009                 :       28492 :   slen = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (slen),
    9010                 :             :                           slen,
    9011                 :       28492 :                           fold_convert (TREE_TYPE (slen),
    9012                 :             :                                         TYPE_SIZE_UNIT (chartype)));
    9013                 :       28492 :   dlen = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (dlen),
    9014                 :             :                           dlen,
    9015                 :       28492 :                           fold_convert (TREE_TYPE (dlen),
    9016                 :             :                                         TYPE_SIZE_UNIT (chartype)));
    9017                 :             : 
    9018                 :       28492 :   if (dlength && POINTER_TYPE_P (TREE_TYPE (dest)))
    9019                 :       28444 :     dest = fold_convert (pvoid_type_node, dest);
    9020                 :             :   else
    9021                 :          48 :     dest = gfc_build_addr_expr (pvoid_type_node, dest);
    9022                 :             : 
    9023                 :       28492 :   if (slength && POINTER_TYPE_P (TREE_TYPE (src)))
    9024                 :       28488 :     src = fold_convert (pvoid_type_node, src);
    9025                 :             :   else
    9026                 :           4 :     src = gfc_build_addr_expr (pvoid_type_node, src);
    9027                 :             : 
    9028                 :             :   /* Truncate string if source is too long.  */
    9029                 :       28492 :   cond2 = fold_build2_loc (input_location, LT_EXPR, logical_type_node, slen,
    9030                 :             :                            dlen);
    9031                 :             : 
    9032                 :             :   /* Pre-evaluate pointers unless one of the IF arms will be optimized away.  */
    9033                 :       28492 :   if (!CONSTANT_CLASS_P (cond2))
    9034                 :             :     {
    9035                 :        8821 :       dest = gfc_evaluate_now (dest, block);
    9036                 :        8821 :       src = gfc_evaluate_now (src, block);
    9037                 :             :     }
    9038                 :             : 
    9039                 :             :   /* Copy and pad with spaces.  */
    9040                 :       28492 :   tmp3 = build_call_expr_loc (input_location,
    9041                 :             :                               builtin_decl_explicit (BUILT_IN_MEMMOVE),
    9042                 :             :                               3, dest, src,
    9043                 :             :                               fold_convert (size_type_node, slen));
    9044                 :             : 
    9045                 :             :   /* Wstringop-overflow appears at -O3 even though this warning is not
    9046                 :             :      explicitly available in fortran nor can it be switched off. If the
    9047                 :             :      source length is a constant, its negative appears as a very large
    9048                 :             :      positive number and triggers the warning in BUILTIN_MEMSET. Fixing
    9049                 :             :      the result of the MINUS_EXPR suppresses this spurious warning.  */
    9050                 :       28492 :   tmp = fold_build2_loc (input_location, MINUS_EXPR,
    9051                 :       28492 :                          TREE_TYPE(dlen), dlen, slen);
    9052                 :       28492 :   if (slength && TREE_CONSTANT (slength))
    9053                 :       25132 :     tmp = gfc_evaluate_now (tmp, block);
    9054                 :             : 
    9055                 :       28492 :   tmp4 = fold_build_pointer_plus_loc (input_location, dest, slen);
    9056                 :       28492 :   tmp4 = fill_with_spaces (tmp4, chartype, tmp);
    9057                 :             : 
    9058                 :       28492 :   gfc_init_block (&tempblock);
    9059                 :       28492 :   gfc_add_expr_to_block (&tempblock, tmp3);
    9060                 :       28492 :   gfc_add_expr_to_block (&tempblock, tmp4);
    9061                 :       28492 :   tmp3 = gfc_finish_block (&tempblock);
    9062                 :             : 
    9063                 :             :   /* The truncated memmove if the slen >= dlen.  */
    9064                 :       28492 :   tmp2 = build_call_expr_loc (input_location,
    9065                 :             :                               builtin_decl_explicit (BUILT_IN_MEMMOVE),
    9066                 :             :                               3, dest, src,
    9067                 :             :                               fold_convert (size_type_node, dlen));
    9068                 :             : 
    9069                 :             :   /* The whole copy_string function is there.  */
    9070                 :       28492 :   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond2,
    9071                 :             :                          tmp3, tmp2);
    9072                 :       28492 :   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
    9073                 :             :                          build_empty_stmt (input_location));
    9074                 :       28492 :   gfc_add_expr_to_block (block, tmp);
    9075                 :             : }
    9076                 :             : 
    9077                 :             : 
    9078                 :             : /* Translate a statement function.
    9079                 :             :    The value of a statement function reference is obtained by evaluating the
    9080                 :             :    expression using the values of the actual arguments for the values of the
    9081                 :             :    corresponding dummy arguments.  */
    9082                 :             : 
    9083                 :             : static void
    9084                 :         269 : gfc_conv_statement_function (gfc_se * se, gfc_expr * expr)
    9085                 :             : {
    9086                 :         269 :   gfc_symbol *sym;
    9087                 :         269 :   gfc_symbol *fsym;
    9088                 :         269 :   gfc_formal_arglist *fargs;
    9089                 :         269 :   gfc_actual_arglist *args;
    9090                 :         269 :   gfc_se lse;
    9091                 :         269 :   gfc_se rse;
    9092                 :         269 :   gfc_saved_var *saved_vars;
    9093                 :         269 :   tree *temp_vars;
    9094                 :         269 :   tree type;
    9095                 :         269 :   tree tmp;
    9096                 :         269 :   int n;
    9097                 :             : 
    9098                 :         269 :   sym = expr->symtree->n.sym;
    9099                 :         269 :   args = expr->value.function.actual;
    9100                 :         269 :   gfc_init_se (&lse, NULL);
    9101                 :         269 :   gfc_init_se (&rse, NULL);
    9102                 :             : 
    9103                 :         269 :   n = 0;
    9104                 :         727 :   for (fargs = gfc_sym_get_dummy_args (sym); fargs; fargs = fargs->next)
    9105                 :         458 :     n++;
    9106                 :         269 :   saved_vars = XCNEWVEC (gfc_saved_var, n);
    9107                 :         269 :   temp_vars = XCNEWVEC (tree, n);
    9108                 :             : 
    9109                 :         727 :   for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
    9110                 :         458 :        fargs = fargs->next, n++)
    9111                 :             :     {
    9112                 :             :       /* Each dummy shall be specified, explicitly or implicitly, to be
    9113                 :             :          scalar.  */
    9114                 :         458 :       gcc_assert (fargs->sym->attr.dimension == 0);
    9115                 :         458 :       fsym = fargs->sym;
    9116                 :             : 
    9117                 :         458 :       if (fsym->ts.type == BT_CHARACTER)
    9118                 :             :         {
    9119                 :             :           /* Copy string arguments.  */
    9120                 :          48 :           tree arglen;
    9121                 :             : 
    9122                 :          48 :           gcc_assert (fsym->ts.u.cl && fsym->ts.u.cl->length
    9123                 :             :                       && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT);
    9124                 :             : 
    9125                 :             :           /* Create a temporary to hold the value.  */
    9126                 :          48 :           if (fsym->ts.u.cl->backend_decl == NULL_TREE)
    9127                 :           1 :              fsym->ts.u.cl->backend_decl
    9128                 :           1 :                 = gfc_conv_constant_to_tree (fsym->ts.u.cl->length);
    9129                 :             : 
    9130                 :          48 :           type = gfc_get_character_type (fsym->ts.kind, fsym->ts.u.cl);
    9131                 :          48 :           temp_vars[n] = gfc_create_var (type, fsym->name);
    9132                 :             : 
    9133                 :          48 :           arglen = TYPE_MAX_VALUE (TYPE_DOMAIN (type));
    9134                 :             : 
    9135                 :          48 :           gfc_conv_expr (&rse, args->expr);
    9136                 :          48 :           gfc_conv_string_parameter (&rse);
    9137                 :          48 :           gfc_add_block_to_block (&se->pre, &lse.pre);
    9138                 :          48 :           gfc_add_block_to_block (&se->pre, &rse.pre);
    9139                 :             : 
    9140                 :          48 :           gfc_trans_string_copy (&se->pre, arglen, temp_vars[n], fsym->ts.kind,
    9141                 :             :                                  rse.string_length, rse.expr, fsym->ts.kind);
    9142                 :          48 :           gfc_add_block_to_block (&se->pre, &lse.post);
    9143                 :          48 :           gfc_add_block_to_block (&se->pre, &rse.post);
    9144                 :             :         }
    9145                 :             :       else
    9146                 :             :         {
    9147                 :             :           /* For everything else, just evaluate the expression.  */
    9148                 :             : 
    9149                 :             :           /* Create a temporary to hold the value.  */
    9150                 :         410 :           type = gfc_typenode_for_spec (&fsym->ts);
    9151                 :         410 :           temp_vars[n] = gfc_create_var (type, fsym->name);
    9152                 :             : 
    9153                 :         410 :           gfc_conv_expr (&lse, args->expr);
    9154                 :             : 
    9155                 :         410 :           gfc_add_block_to_block (&se->pre, &lse.pre);
    9156                 :         410 :           gfc_add_modify (&se->pre, temp_vars[n], lse.expr);
    9157                 :         410 :           gfc_add_block_to_block (&se->pre, &lse.post);
    9158                 :             :         }
    9159                 :             : 
    9160                 :         458 :       args = args->next;
    9161                 :             :     }
    9162                 :             : 
    9163                 :             :   /* Use the temporary variables in place of the real ones.  */
    9164                 :         727 :   for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
    9165                 :         458 :        fargs = fargs->next, n++)
    9166                 :         458 :     gfc_shadow_sym (fargs->sym, temp_vars[n], &saved_vars[n]);
    9167                 :             : 
    9168                 :         269 :   gfc_conv_expr (se, sym->value);
    9169                 :             : 
    9170                 :         269 :   if (sym->ts.type == BT_CHARACTER)
    9171                 :             :     {
    9172                 :          55 :       gfc_conv_const_charlen (sym->ts.u.cl);
    9173                 :             : 
    9174                 :             :       /* Force the expression to the correct length.  */
    9175                 :          55 :       if (!INTEGER_CST_P (se->string_length)
    9176                 :         101 :           || tree_int_cst_lt (se->string_length,
    9177                 :          46 :                               sym->ts.u.cl->backend_decl))
    9178                 :             :         {
    9179                 :          31 :           type = gfc_get_character_type (sym->ts.kind, sym->ts.u.cl);
    9180                 :          31 :           tmp = gfc_create_var (type, sym->name);
    9181                 :          31 :           tmp = gfc_build_addr_expr (build_pointer_type (type), tmp);
    9182                 :          31 :           gfc_trans_string_copy (&se->pre, sym->ts.u.cl->backend_decl, tmp,
    9183                 :             :                                  sym->ts.kind, se->string_length, se->expr,
    9184                 :             :                                  sym->ts.kind);
    9185                 :          31 :           se->expr = tmp;
    9186                 :             :         }
    9187                 :          55 :       se->string_length = sym->ts.u.cl->backend_decl;
    9188                 :             :     }
    9189                 :             : 
    9190                 :             :   /* Restore the original variables.  */
    9191                 :         727 :   for (fargs = gfc_sym_get_dummy_args (sym), n = 0; fargs;
    9192                 :         458 :        fargs = fargs->next, n++)
    9193                 :         458 :     gfc_restore_sym (fargs->sym, &saved_vars[n]);
    9194                 :         269 :   free (temp_vars);
    9195                 :         269 :   free (saved_vars);
    9196                 :         269 : }
    9197                 :             : 
    9198                 :             : 
    9199                 :             : /* Translate a function expression.  */
    9200                 :             : 
    9201                 :             : static void
    9202                 :      286528 : gfc_conv_function_expr (gfc_se * se, gfc_expr * expr)
    9203                 :             : {
    9204                 :      286528 :   gfc_symbol *sym;
    9205                 :             : 
    9206                 :      286528 :   if (expr->value.function.isym)
    9207                 :             :     {
    9208                 :      237480 :       gfc_conv_intrinsic_function (se, expr);
    9209                 :      237480 :       return;
    9210                 :             :     }
    9211                 :             : 
    9212                 :             :   /* expr.value.function.esym is the resolved (specific) function symbol for
    9213                 :             :      most functions.  However this isn't set for dummy procedures.  */
    9214                 :       49048 :   sym = expr->value.function.esym;
    9215                 :       49048 :   if (!sym)
    9216                 :        1437 :     sym = expr->symtree->n.sym;
    9217                 :             : 
    9218                 :             :   /* The IEEE_ARITHMETIC functions are caught here. */
    9219                 :       49048 :   if (sym->from_intmod == INTMOD_IEEE_ARITHMETIC)
    9220                 :       13939 :     if (gfc_conv_ieee_arithmetic_function (se, expr))
    9221                 :             :       return;
    9222                 :             : 
    9223                 :             :   /* We distinguish statement functions from general functions to improve
    9224                 :             :      runtime performance.  */
    9225                 :       36591 :   if (sym->attr.proc == PROC_ST_FUNCTION)
    9226                 :             :     {
    9227                 :         269 :       gfc_conv_statement_function (se, expr);
    9228                 :         269 :       return;
    9229                 :             :     }
    9230                 :             : 
    9231                 :       36322 :   gfc_conv_procedure_call (se, sym, expr->value.function.actual, expr,
    9232                 :             :                            NULL);
    9233                 :             : }
    9234                 :             : 
    9235                 :             : 
    9236                 :             : /* Determine whether the given EXPR_CONSTANT is a zero initializer.  */
    9237                 :             : 
    9238                 :             : static bool
    9239                 :       37527 : is_zero_initializer_p (gfc_expr * expr)
    9240                 :             : {
    9241                 :       37527 :   if (expr->expr_type != EXPR_CONSTANT)
    9242                 :             :     return false;
    9243                 :             : 
    9244                 :             :   /* We ignore constants with prescribed memory representations for now.  */
    9245                 :       10885 :   if (expr->representation.string)
    9246                 :             :     return false;
    9247                 :             : 
    9248                 :       10867 :   switch (expr->ts.type)
    9249                 :             :     {
    9250                 :        4920 :     case BT_INTEGER:
    9251                 :        4920 :       return mpz_cmp_si (expr->value.integer, 0) == 0;
    9252                 :             : 
    9253                 :        4762 :     case BT_REAL:
    9254                 :        4762 :       return mpfr_zero_p (expr->value.real)
    9255                 :        4762 :              && MPFR_SIGN (expr->value.real) >= 0;
    9256                 :             : 
    9257                 :         811 :     case BT_LOGICAL:
    9258                 :         811 :       return expr->value.logical == 0;
    9259                 :             : 
    9260                 :         240 :     case BT_COMPLEX:
    9261                 :         240 :       return mpfr_zero_p (mpc_realref (expr->value.complex))
    9262                 :         154 :              && MPFR_SIGN (mpc_realref (expr->value.complex)) >= 0
    9263                 :         154 :              && mpfr_zero_p (mpc_imagref (expr->value.complex))
    9264                 :         382 :              && MPFR_SIGN (mpc_imagref (expr->value.complex)) >= 0;
    9265                 :             : 
    9266                 :             :     default:
    9267                 :             :       break;
    9268                 :             :     }
    9269                 :             :   return false;
    9270                 :             : }
    9271                 :             : 
    9272                 :             : 
    9273                 :             : static void
    9274                 :       34167 : gfc_conv_array_constructor_expr (gfc_se * se, gfc_expr * expr)
    9275                 :             : {
    9276                 :       34167 :   gfc_ss *ss;
    9277                 :             : 
    9278                 :       34167 :   ss = se->ss;
    9279                 :       34167 :   gcc_assert (ss != NULL && ss != gfc_ss_terminator);
    9280                 :       34167 :   gcc_assert (ss->info->expr == expr && ss->info->type == GFC_SS_CONSTRUCTOR);
    9281                 :             : 
    9282                 :       34167 :   gfc_conv_tmp_array_ref (se);
    9283                 :       34167 : }
    9284                 :             : 
    9285                 :             : 
    9286                 :             : /* Build a static initializer.  EXPR is the expression for the initial value.
    9287                 :             :    The other parameters describe the variable of the component being
    9288                 :             :    initialized. EXPR may be null.  */
    9289                 :             : 
    9290                 :             : tree
    9291                 :      131580 : gfc_conv_initializer (gfc_expr * expr, gfc_typespec * ts, tree type,
    9292                 :             :                       bool array, bool pointer, bool procptr)
    9293                 :             : {
    9294                 :      131580 :   gfc_se se;
    9295                 :             : 
    9296                 :      131580 :   if (flag_coarray != GFC_FCOARRAY_LIB && ts->type == BT_DERIVED
    9297                 :       41962 :       && ts->u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    9298                 :       41962 :       && ts->u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
    9299                 :          57 :     return build_constructor (type, NULL);
    9300                 :             : 
    9301                 :      131523 :   if (!(expr || pointer || procptr))
    9302                 :             :     return NULL_TREE;
    9303                 :             : 
    9304                 :             :   /* Check if we have ISOCBINDING_NULL_PTR or ISOCBINDING_NULL_FUNPTR
    9305                 :             :      (these are the only two iso_c_binding derived types that can be
    9306                 :             :      used as initialization expressions).  If so, we need to modify
    9307                 :             :      the 'expr' to be that for a (void *).  */
    9308                 :      123415 :   if (expr != NULL && expr->ts.type == BT_DERIVED
    9309                 :       37794 :       && expr->ts.is_iso_c && expr->ts.u.derived)
    9310                 :             :     {
    9311                 :         186 :       if (TREE_CODE (type) == ARRAY_TYPE)
    9312                 :           4 :         return build_constructor (type, NULL);
    9313                 :         182 :       else if (POINTER_TYPE_P (type))
    9314                 :         182 :         return build_int_cst (type, 0);
    9315                 :             :       else
    9316                 :           0 :         gcc_unreachable ();
    9317                 :             :     }
    9318                 :             : 
    9319                 :      123229 :   if (array && !procptr)
    9320                 :             :     {
    9321                 :        8213 :       tree ctor;
    9322                 :             :       /* Arrays need special handling.  */
    9323                 :        8213 :       if (pointer)
    9324                 :         697 :         ctor = gfc_build_null_descriptor (type);
    9325                 :             :       /* Special case assigning an array to zero.  */
    9326                 :        7516 :       else if (is_zero_initializer_p (expr))
    9327                 :         215 :         ctor = build_constructor (type, NULL);
    9328                 :             :       else
    9329                 :        7301 :         ctor = gfc_conv_array_initializer (type, expr);
    9330                 :        8213 :       TREE_STATIC (ctor) = 1;
    9331                 :        8213 :       return ctor;
    9332                 :             :     }
    9333                 :      115016 :   else if (pointer || procptr)
    9334                 :             :     {
    9335                 :       55571 :       if (ts->type == BT_CLASS && !procptr)
    9336                 :             :         {
    9337                 :        1694 :           gfc_init_se (&se, NULL);
    9338                 :        1694 :           gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
    9339                 :        1694 :           gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
    9340                 :        1694 :           TREE_STATIC (se.expr) = 1;
    9341                 :        1694 :           return se.expr;
    9342                 :             :         }
    9343                 :       53877 :       else if (!expr || expr->expr_type == EXPR_NULL)
    9344                 :       29256 :         return fold_convert (type, null_pointer_node);
    9345                 :             :       else
    9346                 :             :         {
    9347                 :       24621 :           gfc_init_se (&se, NULL);
    9348                 :       24621 :           se.want_pointer = 1;
    9349                 :       24621 :           gfc_conv_expr (&se, expr);
    9350                 :       24621 :           gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
    9351                 :             :           return se.expr;
    9352                 :             :         }
    9353                 :             :     }
    9354                 :             :   else
    9355                 :             :     {
    9356                 :       59445 :       switch (ts->type)
    9357                 :             :         {
    9358                 :       17609 :         case_bt_struct:
    9359                 :       17609 :         case BT_CLASS:
    9360                 :       17609 :           gfc_init_se (&se, NULL);
    9361                 :       17609 :           if (ts->type == BT_CLASS && expr->expr_type == EXPR_NULL)
    9362                 :         739 :             gfc_conv_structure (&se, gfc_class_initializer (ts, expr), 1);
    9363                 :             :           else
    9364                 :       16870 :             gfc_conv_structure (&se, expr, 1);
    9365                 :       17609 :           gcc_assert (TREE_CODE (se.expr) == CONSTRUCTOR);
    9366                 :       17609 :           TREE_STATIC (se.expr) = 1;
    9367                 :       17609 :           return se.expr;
    9368                 :             : 
    9369                 :        2537 :         case BT_CHARACTER:
    9370                 :        2537 :           if (expr->expr_type == EXPR_CONSTANT)
    9371                 :             :             {
    9372                 :        2536 :               tree ctor = gfc_conv_string_init (ts->u.cl->backend_decl, expr);
    9373                 :        2536 :               TREE_STATIC (ctor) = 1;
    9374                 :        2536 :               return ctor;
    9375                 :             :             }
    9376                 :             : 
    9377                 :             :           /* Fallthrough.  */
    9378                 :       39300 :         default:
    9379                 :       39300 :           gfc_init_se (&se, NULL);
    9380                 :       39300 :           gfc_conv_constant (&se, expr);
    9381                 :       39300 :           gcc_assert (TREE_CODE (se.expr) != CONSTRUCTOR);
    9382                 :             :           return se.expr;
    9383                 :             :         }
    9384                 :             :     }
    9385                 :             : }
    9386                 :             : 
    9387                 :             : static tree
    9388                 :         921 : gfc_trans_subarray_assign (tree dest, gfc_component * cm, gfc_expr * expr)
    9389                 :             : {
    9390                 :         921 :   gfc_se rse;
    9391                 :         921 :   gfc_se lse;
    9392                 :         921 :   gfc_ss *rss;
    9393                 :         921 :   gfc_ss *lss;
    9394                 :         921 :   gfc_array_info *lss_array;
    9395                 :         921 :   stmtblock_t body;
    9396                 :         921 :   stmtblock_t block;
    9397                 :         921 :   gfc_loopinfo loop;
    9398                 :         921 :   int n;
    9399                 :         921 :   tree tmp;
    9400                 :             : 
    9401                 :         921 :   gfc_start_block (&block);
    9402                 :             : 
    9403                 :             :   /* Initialize the scalarizer.  */
    9404                 :         921 :   gfc_init_loopinfo (&loop);
    9405                 :             : 
    9406                 :         921 :   gfc_init_se (&lse, NULL);
    9407                 :         921 :   gfc_init_se (&rse, NULL);
    9408                 :             : 
    9409                 :             :   /* Walk the rhs.  */
    9410                 :         921 :   rss = gfc_walk_expr (expr);
    9411                 :         921 :   if (rss == gfc_ss_terminator)
    9412                 :             :     /* The rhs is scalar.  Add a ss for the expression.  */
    9413                 :         191 :     rss = gfc_get_scalar_ss (gfc_ss_terminator, expr);
    9414                 :             : 
    9415                 :             :   /* Create a SS for the destination.  */
    9416                 :         921 :   lss = gfc_get_array_ss (gfc_ss_terminator, NULL, cm->as->rank,
    9417                 :             :                           GFC_SS_COMPONENT);
    9418                 :         921 :   lss_array = &lss->info->data.array;
    9419                 :         921 :   lss_array->shape = gfc_get_shape (cm->as->rank);
    9420                 :         921 :   lss_array->descriptor = dest;
    9421                 :         921 :   lss_array->data = gfc_conv_array_data (dest);
    9422                 :         921 :   lss_array->offset = gfc_conv_array_offset (dest);
    9423                 :        1899 :   for (n = 0; n < cm->as->rank; n++)
    9424                 :             :     {
    9425                 :         978 :       lss_array->start[n] = gfc_conv_array_lbound (dest, n);
    9426                 :         978 :       lss_array->stride[n] = gfc_index_one_node;
    9427                 :             : 
    9428                 :         978 :       mpz_init (lss_array->shape[n]);
    9429                 :         978 :       mpz_sub (lss_array->shape[n], cm->as->upper[n]->value.integer,
    9430                 :         978 :                cm->as->lower[n]->value.integer);
    9431                 :         978 :       mpz_add_ui (lss_array->shape[n], lss_array->shape[n], 1);
    9432                 :             :     }
    9433                 :             : 
    9434                 :             :   /* Associate the SS with the loop.  */
    9435                 :         921 :   gfc_add_ss_to_loop (&loop, lss);
    9436                 :         921 :   gfc_add_ss_to_loop (&loop, rss);
    9437                 :             : 
    9438                 :             :   /* Calculate the bounds of the scalarization.  */
    9439                 :         921 :   gfc_conv_ss_startstride (&loop);
    9440                 :             : 
    9441                 :             :   /* Setup the scalarizing loops.  */
    9442                 :         921 :   gfc_conv_loop_setup (&loop, &expr->where);
    9443                 :             : 
    9444                 :             :   /* Setup the gfc_se structures.  */
    9445                 :         921 :   gfc_copy_loopinfo_to_se (&lse, &loop);
    9446                 :         921 :   gfc_copy_loopinfo_to_se (&rse, &loop);
    9447                 :             : 
    9448                 :         921 :   rse.ss = rss;
    9449                 :         921 :   gfc_mark_ss_chain_used (rss, 1);
    9450                 :         921 :   lse.ss = lss;
    9451                 :         921 :   gfc_mark_ss_chain_used (lss, 1);
    9452                 :             : 
    9453                 :             :   /* Start the scalarized loop body.  */
    9454                 :         921 :   gfc_start_scalarized_body (&loop, &body);
    9455                 :             : 
    9456                 :         921 :   gfc_conv_tmp_array_ref (&lse);
    9457                 :         921 :   if (cm->ts.type == BT_CHARACTER)
    9458                 :         176 :     lse.string_length = cm->ts.u.cl->backend_decl;
    9459                 :             : 
    9460                 :         921 :   gfc_conv_expr (&rse, expr);
    9461                 :             : 
    9462                 :         921 :   tmp = gfc_trans_scalar_assign (&lse, &rse, cm->ts, true, false);
    9463                 :         921 :   gfc_add_expr_to_block (&body, tmp);
    9464                 :             : 
    9465                 :         921 :   gcc_assert (rse.ss == gfc_ss_terminator);
    9466                 :             : 
    9467                 :             :   /* Generate the copying loops.  */
    9468                 :         921 :   gfc_trans_scalarizing_loops (&loop, &body);
    9469                 :             : 
    9470                 :             :   /* Wrap the whole thing up.  */
    9471                 :         921 :   gfc_add_block_to_block (&block, &loop.pre);
    9472                 :         921 :   gfc_add_block_to_block (&block, &loop.post);
    9473                 :             : 
    9474                 :         921 :   gcc_assert (lss_array->shape != NULL);
    9475                 :         921 :   gfc_free_shape (&lss_array->shape, cm->as->rank);
    9476                 :         921 :   gfc_cleanup_loop (&loop);
    9477                 :             : 
    9478                 :         921 :   return gfc_finish_block (&block);
    9479                 :             : }
    9480                 :             : 
    9481                 :             : 
    9482                 :             : static tree
    9483                 :         967 : gfc_trans_alloc_subarray_assign (tree dest, gfc_component * cm,
    9484                 :             :                                  gfc_expr * expr)
    9485                 :             : {
    9486                 :         967 :   gfc_se se;
    9487                 :         967 :   stmtblock_t block;
    9488                 :         967 :   tree offset;
    9489                 :         967 :   int n;
    9490                 :         967 :   tree tmp;
    9491                 :         967 :   tree tmp2;
    9492                 :         967 :   gfc_array_spec *as;
    9493                 :         967 :   gfc_expr *arg = NULL;
    9494                 :             : 
    9495                 :         967 :   gfc_start_block (&block);
    9496                 :         967 :   gfc_init_se (&se, NULL);
    9497                 :             : 
    9498                 :             :   /* Get the descriptor for the expressions.  */
    9499                 :         967 :   se.want_pointer = 0;
    9500                 :         967 :   gfc_conv_expr_descriptor (&se, expr);
    9501                 :         967 :   gfc_add_block_to_block (&block, &se.pre);
    9502                 :         967 :   gfc_add_modify (&block, dest, se.expr);
    9503                 :         967 :   if (cm->ts.type == BT_CHARACTER
    9504                 :         967 :       && gfc_deferred_strlen (cm, &tmp))
    9505                 :             :     {
    9506                 :          30 :       tmp = fold_build3_loc (input_location, COMPONENT_REF,
    9507                 :          30 :                              TREE_TYPE (tmp),
    9508                 :          30 :                              TREE_OPERAND (dest, 0),
    9509                 :             :                              tmp, NULL_TREE);
    9510                 :          30 :       gfc_add_modify (&block, tmp,
    9511                 :          30 :                               fold_convert (TREE_TYPE (tmp),
    9512                 :             :                               se.string_length));
    9513                 :          30 :       cm->ts.u.cl->backend_decl = gfc_create_var (gfc_charlen_type_node,
    9514                 :             :                                                   "slen");
    9515                 :          30 :       gfc_add_modify (&block, cm->ts.u.cl->backend_decl, se.string_length);
    9516                 :             :     }
    9517                 :             : 
    9518                 :             :   /* Deal with arrays of derived types with allocatable components.  */
    9519                 :         967 :   if (gfc_bt_struct (cm->ts.type)
    9520                 :         150 :         && cm->ts.u.derived->attr.alloc_comp)
    9521                 :             :     // TODO: Fix caf_mode
    9522                 :         106 :     tmp = gfc_copy_alloc_comp (cm->ts.u.derived,
    9523                 :             :                                se.expr, dest,
    9524                 :         106 :                                cm->as->rank, 0);
    9525                 :         861 :   else if (cm->ts.type == BT_CLASS && expr->ts.type == BT_DERIVED
    9526                 :          36 :            && CLASS_DATA(cm)->attr.allocatable)
    9527                 :             :     {
    9528                 :          36 :       if (cm->ts.u.derived->attr.alloc_comp)
    9529                 :             :         // TODO: Fix caf_mode
    9530                 :           0 :         tmp = gfc_copy_alloc_comp (expr->ts.u.derived,
    9531                 :             :                                    se.expr, dest,
    9532                 :             :                                    expr->rank, 0);
    9533                 :             :       else
    9534                 :             :         {
    9535                 :          36 :           tmp = TREE_TYPE (dest);
    9536                 :          36 :           tmp = gfc_duplicate_allocatable (dest, se.expr,
    9537                 :             :                                            tmp, expr->rank, NULL_TREE);
    9538                 :             :         }
    9539                 :             :     }
    9540                 :         825 :   else if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
    9541                 :          30 :     tmp = gfc_duplicate_allocatable (dest, se.expr,
    9542                 :             :                                      gfc_typenode_for_spec (&cm->ts),
    9543                 :          30 :                                      cm->as->rank, NULL_TREE);
    9544                 :             :   else
    9545                 :         795 :     tmp = gfc_duplicate_allocatable (dest, se.expr,
    9546                 :         795 :                                      TREE_TYPE(cm->backend_decl),
    9547                 :         795 :                                      cm->as->rank, NULL_TREE);
    9548                 :             : 
    9549                 :             : 
    9550                 :         967 :   gfc_add_expr_to_block (&block, tmp);
    9551                 :         967 :   gfc_add_block_to_block (&block, &se.post);
    9552                 :             : 
    9553                 :         967 :   if (expr->expr_type != EXPR_VARIABLE)
    9554                 :         856 :     gfc_conv_descriptor_data_set (&block, se.expr,
    9555                 :             :                                   null_pointer_node);
    9556                 :             : 
    9557                 :             :   /* We need to know if the argument of a conversion function is a
    9558                 :             :      variable, so that the correct lower bound can be used.  */
    9559                 :         967 :   if (expr->expr_type == EXPR_FUNCTION
    9560                 :          50 :         && expr->value.function.isym
    9561                 :          38 :         && expr->value.function.isym->conversion
    9562                 :          38 :         && expr->value.function.actual->expr
    9563                 :          38 :         && expr->value.function.actual->expr->expr_type == EXPR_VARIABLE)
    9564                 :          38 :     arg = expr->value.function.actual->expr;
    9565                 :             : 
    9566                 :             :   /* Obtain the array spec of full array references.  */
    9567                 :          38 :   if (arg)
    9568                 :          38 :     as = gfc_get_full_arrayspec_from_expr (arg);
    9569                 :             :   else
    9570                 :         929 :     as = gfc_get_full_arrayspec_from_expr (expr);
    9571                 :             : 
    9572                 :             :   /* Shift the lbound and ubound of temporaries to being unity,
    9573                 :             :      rather than zero, based. Always calculate the offset.  */
    9574                 :         967 :   offset = gfc_conv_descriptor_offset_get (dest);
    9575                 :         967 :   gfc_add_modify (&block, offset, gfc_index_zero_node);
    9576                 :         967 :   tmp2 =gfc_create_var (gfc_array_index_type, NULL);
    9577                 :             : 
    9578                 :        1990 :   for (n = 0; n < expr->rank; n++)
    9579                 :             :     {
    9580                 :        1023 :       tree span;
    9581                 :        1023 :       tree lbound;
    9582                 :             : 
    9583                 :             :       /* Obtain the correct lbound - ISO/IEC TR 15581:2001 page 9.
    9584                 :             :          TODO It looks as if gfc_conv_expr_descriptor should return
    9585                 :             :          the correct bounds and that the following should not be
    9586                 :             :          necessary.  This would simplify gfc_conv_intrinsic_bound
    9587                 :             :          as well.  */
    9588                 :        1023 :       if (as && as->lower[n])
    9589                 :             :         {
    9590                 :          54 :           gfc_se lbse;
    9591                 :          54 :           gfc_init_se (&lbse, NULL);
    9592                 :          54 :           gfc_conv_expr (&lbse, as->lower[n]);
    9593                 :          54 :           gfc_add_block_to_block (&block, &lbse.pre);
    9594                 :          54 :           lbound = gfc_evaluate_now (lbse.expr, &block);
    9595                 :          54 :         }
    9596                 :         969 :       else if (as && arg)
    9597                 :             :         {
    9598                 :          46 :           tmp = gfc_get_symbol_decl (arg->symtree->n.sym);
    9599                 :          46 :           lbound = gfc_conv_descriptor_lbound_get (tmp,
    9600                 :             :                                         gfc_rank_cst[n]);
    9601                 :             :         }
    9602                 :         923 :       else if (as)
    9603                 :          63 :         lbound = gfc_conv_descriptor_lbound_get (dest,
    9604                 :             :                                                 gfc_rank_cst[n]);
    9605                 :             :       else
    9606                 :         860 :         lbound = gfc_index_one_node;
    9607                 :             : 
    9608                 :        1023 :       lbound = fold_convert (gfc_array_index_type, lbound);
    9609                 :             : 
    9610                 :             :       /* Shift the bounds and set the offset accordingly.  */
    9611                 :        1023 :       tmp = gfc_conv_descriptor_ubound_get (dest, gfc_rank_cst[n]);
    9612                 :        1023 :       span = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    9613                 :             :                 tmp, gfc_conv_descriptor_lbound_get (dest, gfc_rank_cst[n]));
    9614                 :        1023 :       tmp = fold_build2_loc (input_location, PLUS_EXPR, gfc_array_index_type,
    9615                 :             :                              span, lbound);
    9616                 :        1023 :       gfc_conv_descriptor_ubound_set (&block, dest,
    9617                 :             :                                       gfc_rank_cst[n], tmp);
    9618                 :        1023 :       gfc_conv_descriptor_lbound_set (&block, dest,
    9619                 :             :                                       gfc_rank_cst[n], lbound);
    9620                 :             : 
    9621                 :        1023 :       tmp = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
    9622                 :             :                          gfc_conv_descriptor_lbound_get (dest,
    9623                 :             :                                                          gfc_rank_cst[n]),
    9624                 :             :                          gfc_conv_descriptor_stride_get (dest,
    9625                 :             :                                                          gfc_rank_cst[n]));
    9626                 :        1023 :       gfc_add_modify (&block, tmp2, tmp);
    9627                 :        1023 :       tmp = fold_build2_loc (input_location, MINUS_EXPR, gfc_array_index_type,
    9628                 :             :                              offset, tmp2);
    9629                 :        1023 :       gfc_conv_descriptor_offset_set (&block, dest, tmp);
    9630                 :             :     }
    9631                 :             : 
    9632                 :         967 :   if (arg)
    9633                 :             :     {
    9634                 :             :       /* If a conversion expression has a null data pointer
    9635                 :             :          argument, nullify the allocatable component.  */
    9636                 :          38 :       tree non_null_expr;
    9637                 :          38 :       tree null_expr;
    9638                 :             : 
    9639                 :          38 :       if (arg->symtree->n.sym->attr.allocatable
    9640                 :          38 :             || arg->symtree->n.sym->attr.pointer)
    9641                 :             :         {
    9642                 :          38 :           non_null_expr = gfc_finish_block (&block);
    9643                 :          38 :           gfc_start_block (&block);
    9644                 :          38 :           gfc_conv_descriptor_data_set (&block, dest,
    9645                 :             :                                         null_pointer_node);
    9646                 :          38 :           null_expr = gfc_finish_block (&block);
    9647                 :          38 :           tmp = gfc_conv_descriptor_data_get (arg->symtree->n.sym->backend_decl);
    9648                 :          38 :           tmp = build2_loc (input_location, EQ_EXPR, logical_type_node, tmp,
    9649                 :          38 :                             fold_convert (TREE_TYPE (tmp), null_pointer_node));
    9650                 :          38 :           return build3_v (COND_EXPR, tmp,
    9651                 :             :                            null_expr, non_null_expr);
    9652                 :             :         }
    9653                 :             :     }
    9654                 :             : 
    9655                 :         929 :   return gfc_finish_block (&block);
    9656                 :             : }
    9657                 :             : 
    9658                 :             : 
    9659                 :             : /* Allocate or reallocate scalar component, as necessary.  */
    9660                 :             : 
    9661                 :             : static void
    9662                 :         351 : alloc_scalar_allocatable_subcomponent (stmtblock_t *block, tree comp,
    9663                 :             :                                        gfc_component *cm, gfc_expr *expr2,
    9664                 :             :                                        tree slen)
    9665                 :             : {
    9666                 :         351 :   tree tmp;
    9667                 :         351 :   tree ptr;
    9668                 :         351 :   tree size;
    9669                 :         351 :   tree size_in_bytes;
    9670                 :         351 :   tree lhs_cl_size = NULL_TREE;
    9671                 :         351 :   gfc_se se;
    9672                 :             : 
    9673                 :         351 :   if (!comp)
    9674                 :           0 :     return;
    9675                 :             : 
    9676                 :         351 :   if (!expr2 || expr2->rank)
    9677                 :             :     return;
    9678                 :             : 
    9679                 :         351 :   realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
    9680                 :             : 
    9681                 :         351 :   if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
    9682                 :             :     {
    9683                 :         127 :       gcc_assert (expr2->ts.type == BT_CHARACTER);
    9684                 :         127 :       size = expr2->ts.u.cl->backend_decl;
    9685                 :         127 :       if (!size || !VAR_P (size))
    9686                 :         127 :         size = gfc_create_var (TREE_TYPE (slen), "slen");
    9687                 :         127 :       gfc_add_modify (block, size, slen);
    9688                 :             : 
    9689                 :         127 :       gfc_deferred_strlen (cm, &tmp);
    9690                 :         127 :       lhs_cl_size = fold_build3_loc (input_location, COMPONENT_REF,
    9691                 :             :                                      gfc_charlen_type_node,
    9692                 :         127 :                                      TREE_OPERAND (comp, 0),
    9693                 :             :                                      tmp, NULL_TREE);
    9694                 :             : 
    9695                 :         127 :       tmp = TREE_TYPE (gfc_typenode_for_spec (&cm->ts));
    9696                 :         127 :       tmp = TYPE_SIZE_UNIT (tmp);
    9697                 :         254 :       size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
    9698                 :         127 :                                        TREE_TYPE (tmp), tmp,
    9699                 :         127 :                                        fold_convert (TREE_TYPE (tmp), size));
    9700                 :             :     }
    9701                 :         224 :   else if (cm->ts.type == BT_CLASS)
    9702                 :             :     {
    9703                 :          78 :       if (expr2->ts.type != BT_CLASS)
    9704                 :             :         {
    9705                 :          78 :           if (expr2->ts.type == BT_CHARACTER)
    9706                 :             :             {
    9707                 :          18 :               gfc_init_se (&se, NULL);
    9708                 :          18 :               gfc_conv_expr (&se, expr2);
    9709                 :          18 :               size = build_int_cst (gfc_charlen_type_node, expr2->ts.kind);
    9710                 :          18 :               size = fold_build2_loc (input_location, MULT_EXPR,
    9711                 :             :                                       gfc_charlen_type_node,
    9712                 :             :                                       se.string_length, size);
    9713                 :          18 :               size = fold_convert (size_type_node, size);
    9714                 :             :             }
    9715                 :             :           else
    9716                 :             :             {
    9717                 :          60 :               if (expr2->ts.type == BT_DERIVED)
    9718                 :          48 :                 tmp = gfc_get_symbol_decl (expr2->ts.u.derived);
    9719                 :             :               else
    9720                 :          12 :                 tmp = gfc_typenode_for_spec (&expr2->ts);
    9721                 :          60 :               size = TYPE_SIZE_UNIT (tmp);
    9722                 :             :             }
    9723                 :             :         }
    9724                 :             :       else
    9725                 :             :         {
    9726                 :           0 :           gfc_expr *e2vtab;
    9727                 :           0 :           e2vtab = gfc_find_and_cut_at_last_class_ref (expr2);
    9728                 :           0 :           gfc_add_vptr_component (e2vtab);
    9729                 :           0 :           gfc_add_size_component (e2vtab);
    9730                 :           0 :           gfc_init_se (&se, NULL);
    9731                 :           0 :           gfc_conv_expr (&se, e2vtab);
    9732                 :           0 :           gfc_add_block_to_block (block, &se.pre);
    9733                 :           0 :           size = fold_convert (size_type_node, se.expr);
    9734                 :           0 :           gfc_free_expr (e2vtab);
    9735                 :             :         }
    9736                 :             :       size_in_bytes = size;
    9737                 :             :     }
    9738                 :             :   else
    9739                 :             :     {
    9740                 :             :       /* Otherwise use the length in bytes of the rhs.  */
    9741                 :         146 :       size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&cm->ts));
    9742                 :         146 :       size_in_bytes = size;
    9743                 :             :     }
    9744                 :             : 
    9745                 :         351 :   size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
    9746                 :             :                                    size_in_bytes, size_one_node);
    9747                 :             : 
    9748                 :         351 :   if (cm->ts.type == BT_DERIVED && cm->ts.u.derived->attr.alloc_comp)
    9749                 :             :     {
    9750                 :           0 :       tmp = build_call_expr_loc (input_location,
    9751                 :             :                                  builtin_decl_explicit (BUILT_IN_CALLOC),
    9752                 :             :                                  2, build_one_cst (size_type_node),
    9753                 :             :                                  size_in_bytes);
    9754                 :           0 :       tmp = fold_convert (TREE_TYPE (comp), tmp);
    9755                 :           0 :       gfc_add_modify (block, comp, tmp);
    9756                 :             :     }
    9757                 :             :   else
    9758                 :             :     {
    9759                 :         351 :       tmp = build_call_expr_loc (input_location,
    9760                 :             :                                  builtin_decl_explicit (BUILT_IN_MALLOC),
    9761                 :             :                                  1, size_in_bytes);
    9762                 :         351 :       if (GFC_CLASS_TYPE_P (TREE_TYPE (comp)))
    9763                 :          78 :         ptr = gfc_class_data_get (comp);
    9764                 :             :       else
    9765                 :             :         ptr = comp;
    9766                 :         351 :       tmp = fold_convert (TREE_TYPE (ptr), tmp);
    9767                 :         351 :       gfc_add_modify (block, ptr, tmp);
    9768                 :             :     }
    9769                 :             : 
    9770                 :         351 :   if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
    9771                 :             :     /* Update the lhs character length.  */
    9772                 :         127 :     gfc_add_modify (block, lhs_cl_size,
    9773                 :         127 :                     fold_convert (TREE_TYPE (lhs_cl_size), size));
    9774                 :             : }
    9775                 :             : 
    9776                 :             : 
    9777                 :             : /* Assign a single component of a derived type constructor.  */
    9778                 :             : 
    9779                 :             : static tree
    9780                 :       26215 : gfc_trans_subcomponent_assign (tree dest, gfc_component * cm,
    9781                 :             :                                gfc_expr * expr, bool init)
    9782                 :             : {
    9783                 :       26215 :   gfc_se se;
    9784                 :       26215 :   gfc_se lse;
    9785                 :       26215 :   stmtblock_t block;
    9786                 :       26215 :   tree tmp;
    9787                 :       26215 :   tree vtab;
    9788                 :             : 
    9789                 :       26215 :   gfc_start_block (&block);
    9790                 :             : 
    9791                 :       26215 :   if (cm->attr.pointer || cm->attr.proc_pointer)
    9792                 :             :     {
    9793                 :             :       /* Only care about pointers here, not about allocatables.  */
    9794                 :        2436 :       gfc_init_se (&se, NULL);
    9795                 :             :       /* Pointer component.  */
    9796                 :        2436 :       if ((cm->attr.dimension || cm->attr.codimension)
    9797                 :         632 :           && !cm->attr.proc_pointer)
    9798                 :             :         {
    9799                 :             :           /* Array pointer.  */
    9800                 :         617 :           if (expr->expr_type == EXPR_NULL)
    9801                 :         611 :             gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
    9802                 :             :           else
    9803                 :             :             {
    9804                 :           6 :               se.direct_byref = 1;
    9805                 :           6 :               se.expr = dest;
    9806                 :           6 :               gfc_conv_expr_descriptor (&se, expr);
    9807                 :           6 :               gfc_add_block_to_block (&block, &se.pre);
    9808                 :           6 :               gfc_add_block_to_block (&block, &se.post);
    9809                 :             :             }
    9810                 :             :         }
    9811                 :             :       else
    9812                 :             :         {
    9813                 :             :           /* Scalar pointers.  */
    9814                 :        1819 :           se.want_pointer = 1;
    9815                 :        1819 :           gfc_conv_expr (&se, expr);
    9816                 :        1819 :           gfc_add_block_to_block (&block, &se.pre);
    9817                 :             : 
    9818                 :        1819 :           if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
    9819                 :         942 :               && expr->symtree->n.sym->attr.dummy)
    9820                 :          12 :             se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
    9821                 :             : 
    9822                 :        1819 :           gfc_add_modify (&block, dest,
    9823                 :        1819 :                                fold_convert (TREE_TYPE (dest), se.expr));
    9824                 :        1819 :           gfc_add_block_to_block (&block, &se.post);
    9825                 :             :         }
    9826                 :             :     }
    9827                 :       23779 :   else if (cm->ts.type == BT_CLASS && expr->expr_type == EXPR_NULL)
    9828                 :             :     {
    9829                 :             :       /* NULL initialization for CLASS components.  */
    9830                 :         855 :       tmp = gfc_trans_structure_assign (dest,
    9831                 :             :                                         gfc_class_initializer (&cm->ts, expr),
    9832                 :             :                                         false);
    9833                 :         855 :       gfc_add_expr_to_block (&block, tmp);
    9834                 :             :     }
    9835                 :       22924 :   else if ((cm->attr.dimension || cm->attr.codimension)
    9836                 :        3855 :            && !cm->attr.proc_pointer)
    9837                 :             :     {
    9838                 :        3855 :       if (cm->attr.allocatable && expr->expr_type == EXPR_NULL)
    9839                 :        2003 :         gfc_conv_descriptor_data_set (&block, dest, null_pointer_node);
    9840                 :        1852 :       else if (cm->attr.allocatable || cm->attr.pdt_array)
    9841                 :             :         {
    9842                 :         931 :           tmp = gfc_trans_alloc_subarray_assign (dest, cm, expr);
    9843                 :         931 :           gfc_add_expr_to_block (&block, tmp);
    9844                 :             :         }
    9845                 :             :       else
    9846                 :             :         {
    9847                 :         921 :           tmp = gfc_trans_subarray_assign (dest, cm, expr);
    9848                 :         921 :           gfc_add_expr_to_block (&block, tmp);
    9849                 :             :         }
    9850                 :             :     }
    9851                 :       19069 :   else if (cm->ts.type == BT_CLASS
    9852                 :         120 :            && CLASS_DATA (cm)->attr.dimension
    9853                 :         120 :            && CLASS_DATA (cm)->attr.allocatable
    9854                 :          36 :            && expr->ts.type == BT_DERIVED)
    9855                 :             :     {
    9856                 :          36 :       vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
    9857                 :          36 :       vtab = gfc_build_addr_expr (NULL_TREE, vtab);
    9858                 :          36 :       tmp = gfc_class_vptr_get (dest);
    9859                 :          36 :       gfc_add_modify (&block, tmp,
    9860                 :          36 :                       fold_convert (TREE_TYPE (tmp), vtab));
    9861                 :          36 :       tmp = gfc_class_data_get (dest);
    9862                 :          36 :       tmp = gfc_trans_alloc_subarray_assign (tmp, cm, expr);
    9863                 :          36 :       gfc_add_expr_to_block (&block, tmp);
    9864                 :             :     }
    9865                 :       19033 :   else if (cm->attr.allocatable && expr->expr_type == EXPR_NULL
    9866                 :        1477 :            && (init
    9867                 :        1351 :                || (cm->ts.type == BT_CHARACTER
    9868                 :          92 :                    && !(cm->ts.deferred || cm->attr.pdt_string))))
    9869                 :             :     {
    9870                 :             :       /* NULL initialization for allocatable components.
    9871                 :             :          Deferred-length character is dealt with later.  */
    9872                 :         138 :       gfc_add_modify (&block, dest, fold_convert (TREE_TYPE (dest),
    9873                 :             :                                                   null_pointer_node));
    9874                 :             :     }
    9875                 :       18895 :   else if (init && (cm->attr.allocatable
    9876                 :       12770 :            || (cm->ts.type == BT_CLASS && CLASS_DATA (cm)->attr.allocatable
    9877                 :          84 :                && expr->ts.type != BT_CLASS)))
    9878                 :             :     {
    9879                 :         351 :       tree size;
    9880                 :             : 
    9881                 :         351 :       gfc_init_se (&se, NULL);
    9882                 :         351 :       gfc_conv_expr (&se, expr);
    9883                 :             : 
    9884                 :             :       /* The remainder of these instructions follow the if (cm->attr.pointer)
    9885                 :             :          if (!cm->attr.dimension) part above.  */
    9886                 :         351 :       gfc_add_block_to_block (&block, &se.pre);
    9887                 :             :       /* Take care about non-array allocatable components here.  The alloc_*
    9888                 :             :          routine below is motivated by the alloc_scalar_allocatable_for_
    9889                 :             :          assignment() routine, but with the realloc portions removed and
    9890                 :             :          different input.  */
    9891                 :         351 :       alloc_scalar_allocatable_subcomponent (&block, dest, cm, expr,
    9892                 :             :                                              se.string_length);
    9893                 :             : 
    9894                 :         351 :       if (expr->symtree && expr->symtree->n.sym->attr.proc_pointer
    9895                 :         108 :           && expr->symtree->n.sym->attr.dummy)
    9896                 :           0 :         se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
    9897                 :             : 
    9898                 :         351 :       if (cm->ts.type == BT_CLASS)
    9899                 :             :         {
    9900                 :          78 :           tmp = gfc_class_data_get (dest);
    9901                 :          78 :           tmp = build_fold_indirect_ref_loc (input_location, tmp);
    9902                 :          78 :           vtab = gfc_get_symbol_decl (gfc_find_vtab (&expr->ts));
    9903                 :          78 :           vtab = gfc_build_addr_expr (NULL_TREE, vtab);
    9904                 :          78 :           gfc_add_modify (&block, gfc_class_vptr_get (dest),
    9905                 :          78 :                  fold_convert (TREE_TYPE (gfc_class_vptr_get (dest)), vtab));
    9906                 :             :         }
    9907                 :             :       else
    9908                 :         273 :         tmp = build_fold_indirect_ref_loc (input_location, dest);
    9909                 :             : 
    9910                 :             :       /* For deferred strings insert a memcpy.  */
    9911                 :         351 :       if (cm->ts.type == BT_CHARACTER && cm->ts.deferred)
    9912                 :             :         {
    9913                 :         127 :           gcc_assert (se.string_length || expr->ts.u.cl->backend_decl);
    9914                 :         127 :           size = size_of_string_in_bytes (cm->ts.kind, se.string_length
    9915                 :             :                                                 ? se.string_length
    9916                 :           0 :                                                 : expr->ts.u.cl->backend_decl);
    9917                 :         127 :           tmp = gfc_build_memcpy_call (tmp, se.expr, size);
    9918                 :         127 :           gfc_add_expr_to_block (&block, tmp);
    9919                 :             :         }
    9920                 :         224 :       else if (cm->ts.type == BT_CLASS)
    9921                 :             :         {
    9922                 :             :           /* Fix the expression for memcpy.  */
    9923                 :          78 :           if (expr->expr_type != EXPR_VARIABLE)
    9924                 :          48 :             se.expr = gfc_evaluate_now (se.expr, &block);
    9925                 :             : 
    9926                 :          78 :           if (expr->ts.type == BT_CHARACTER)
    9927                 :             :             {
    9928                 :          18 :               size = build_int_cst (gfc_charlen_type_node, expr->ts.kind);
    9929                 :          18 :               size = fold_build2_loc (input_location, MULT_EXPR,
    9930                 :             :                                       gfc_charlen_type_node,
    9931                 :             :                                       se.string_length, size);
    9932                 :          18 :               size = fold_convert (size_type_node, size);
    9933                 :             :             }
    9934                 :             :           else
    9935                 :          60 :             size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr->ts));
    9936                 :             : 
    9937                 :             :           /* Now copy the expression to the constructor component _data.  */
    9938                 :          78 :           gfc_add_expr_to_block (&block,
    9939                 :             :                                  gfc_build_memcpy_call (tmp, se.expr, size));
    9940                 :             : 
    9941                 :             :           /* Fill the unlimited polymorphic _len field.  */
    9942                 :          78 :           if (UNLIMITED_POLY (cm) && expr->ts.type == BT_CHARACTER)
    9943                 :             :             {
    9944                 :          18 :               tmp = gfc_class_len_get (gfc_get_class_from_expr (tmp));
    9945                 :          18 :               gfc_add_modify (&block, tmp,
    9946                 :          18 :                               fold_convert (TREE_TYPE (tmp),
    9947                 :             :                               se.string_length));
    9948                 :             :             }
    9949                 :             :         }
    9950                 :             :       else
    9951                 :         146 :         gfc_add_modify (&block, tmp,
    9952                 :         146 :                         fold_convert (TREE_TYPE (tmp), se.expr));
    9953                 :         351 :       gfc_add_block_to_block (&block, &se.post);
    9954                 :         351 :     }
    9955                 :       18544 :   else if (expr->ts.type == BT_UNION)
    9956                 :             :     {
    9957                 :          13 :       tree tmp;
    9958                 :          13 :       gfc_constructor *c = gfc_constructor_first (expr->value.constructor);
    9959                 :             :       /* We mark that the entire union should be initialized with a contrived
    9960                 :             :          EXPR_NULL expression at the beginning.  */
    9961                 :          13 :       if (c != NULL && c->n.component == NULL
    9962                 :           7 :           && c->expr != NULL && c->expr->expr_type == EXPR_NULL)
    9963                 :             :         {
    9964                 :           6 :           tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
    9965                 :           6 :                             dest, build_constructor (TREE_TYPE (dest), NULL));
    9966                 :           6 :           gfc_add_expr_to_block (&block, tmp);
    9967                 :           6 :           c = gfc_constructor_next (c);
    9968                 :             :         }
    9969                 :             :       /* The following constructor expression, if any, represents a specific
    9970                 :             :          map intializer, as given by the user.  */
    9971                 :          13 :       if (c != NULL && c->expr != NULL)
    9972                 :             :         {
    9973                 :           6 :           gcc_assert (expr->expr_type == EXPR_STRUCTURE);
    9974                 :           6 :           tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
    9975                 :           6 :           gfc_add_expr_to_block (&block, tmp);
    9976                 :             :         }
    9977                 :             :     }
    9978                 :       18531 :   else if (expr->ts.type == BT_DERIVED && expr->ts.f90_type != BT_VOID)
    9979                 :             :     {
    9980                 :        2807 :       if (expr->expr_type != EXPR_STRUCTURE)
    9981                 :             :         {
    9982                 :         348 :           tree dealloc = NULL_TREE;
    9983                 :         348 :           gfc_init_se (&se, NULL);
    9984                 :         348 :           gfc_conv_expr (&se, expr);
    9985                 :         348 :           gfc_add_block_to_block (&block, &se.pre);
    9986                 :             :           /* Prevent repeat evaluations in gfc_copy_alloc_comp by fixing the
    9987                 :             :              expression in  a temporary variable and deallocate the allocatable
    9988                 :             :              components. Then we can the copy the expression to the result.  */
    9989                 :         348 :           if (cm->ts.u.derived->attr.alloc_comp
    9990                 :         250 :               && expr->expr_type != EXPR_VARIABLE)
    9991                 :             :             {
    9992                 :         220 :               se.expr = gfc_evaluate_now (se.expr, &block);
    9993                 :         220 :               dealloc = gfc_deallocate_alloc_comp (cm->ts.u.derived, se.expr,
    9994                 :             :                                                    expr->rank);
    9995                 :             :             }
    9996                 :         348 :           gfc_add_modify (&block, dest,
    9997                 :         348 :                           fold_convert (TREE_TYPE (dest), se.expr));
    9998                 :         348 :           if (cm->ts.u.derived->attr.alloc_comp
    9999                 :         250 :               && expr->expr_type != EXPR_NULL)
   10000                 :             :             {
   10001                 :             :               // TODO: Fix caf_mode
   10002                 :          48 :               tmp = gfc_copy_alloc_comp (cm->ts.u.derived, se.expr,
   10003                 :             :                                          dest, expr->rank, 0);
   10004                 :          48 :               gfc_add_expr_to_block (&block, tmp);
   10005                 :          48 :               if (dealloc != NULL_TREE)
   10006                 :          18 :                 gfc_add_expr_to_block (&block, dealloc);
   10007                 :             :             }
   10008                 :         348 :           gfc_add_block_to_block (&block, &se.post);
   10009                 :             :         }
   10010                 :             :       else
   10011                 :             :         {
   10012                 :             :           /* Nested constructors.  */
   10013                 :        2459 :           tmp = gfc_trans_structure_assign (dest, expr, expr->symtree != NULL);
   10014                 :        2459 :           gfc_add_expr_to_block (&block, tmp);
   10015                 :             :         }
   10016                 :             :     }
   10017                 :       15724 :   else if (gfc_deferred_strlen (cm, &tmp))
   10018                 :             :     {
   10019                 :          92 :       tree strlen;
   10020                 :          92 :       strlen = tmp;
   10021                 :          92 :       gcc_assert (strlen);
   10022                 :          92 :       strlen = fold_build3_loc (input_location, COMPONENT_REF,
   10023                 :          92 :                                 TREE_TYPE (strlen),
   10024                 :          92 :                                 TREE_OPERAND (dest, 0),
   10025                 :             :                                 strlen, NULL_TREE);
   10026                 :             : 
   10027                 :          92 :       if (expr->expr_type == EXPR_NULL)
   10028                 :             :         {
   10029                 :          80 :           tmp = build_int_cst (TREE_TYPE (cm->backend_decl), 0);
   10030                 :          80 :           gfc_add_modify (&block, dest, tmp);
   10031                 :          80 :           tmp = build_int_cst (TREE_TYPE (strlen), 0);
   10032                 :          80 :           gfc_add_modify (&block, strlen, tmp);
   10033                 :             :         }
   10034                 :             :       else
   10035                 :             :         {
   10036                 :          12 :           tree size;
   10037                 :          12 :           gfc_init_se (&se, NULL);
   10038                 :          12 :           gfc_conv_expr (&se, expr);
   10039                 :          12 :           size = size_of_string_in_bytes (cm->ts.kind, se.string_length);
   10040                 :          12 :           size = fold_convert (size_type_node, size);
   10041                 :          12 :           tmp = build_call_expr_loc (input_location,
   10042                 :             :                                      builtin_decl_explicit (BUILT_IN_MALLOC),
   10043                 :             :                                      1, size);
   10044                 :          12 :           gfc_add_modify (&block, dest,
   10045                 :          12 :                           fold_convert (TREE_TYPE (dest), tmp));
   10046                 :          12 :           gfc_add_modify (&block, strlen,
   10047                 :          12 :                           fold_convert (TREE_TYPE (strlen), se.string_length));
   10048                 :          12 :           tmp = gfc_build_memcpy_call (dest, se.expr, size);
   10049                 :          12 :           gfc_add_expr_to_block (&block, tmp);
   10050                 :             :         }
   10051                 :             :     }
   10052                 :       15632 :   else if (!cm->attr.artificial)
   10053                 :             :     {
   10054                 :             :       /* Scalar component (excluding deferred parameters).  */
   10055                 :       15538 :       gfc_init_se (&se, NULL);
   10056                 :       15538 :       gfc_init_se (&lse, NULL);
   10057                 :             : 
   10058                 :       15538 :       gfc_conv_expr (&se, expr);
   10059                 :       15538 :       if (cm->ts.type == BT_CHARACTER)
   10060                 :        1047 :         lse.string_length = cm->ts.u.cl->backend_decl;
   10061                 :       15538 :       lse.expr = dest;
   10062                 :       15538 :       tmp = gfc_trans_scalar_assign (&lse, &se, cm->ts, false, false);
   10063                 :       15538 :       gfc_add_expr_to_block (&block, tmp);
   10064                 :             :     }
   10065                 :       26215 :   return gfc_finish_block (&block);
   10066                 :             : }
   10067                 :             : 
   10068                 :             : /* Assign a derived type constructor to a variable.  */
   10069                 :             : 
   10070                 :             : tree
   10071                 :       18489 : gfc_trans_structure_assign (tree dest, gfc_expr * expr, bool init, bool coarray)
   10072                 :             : {
   10073                 :       18489 :   gfc_constructor *c;
   10074                 :       18489 :   gfc_component *cm;
   10075                 :       18489 :   stmtblock_t block;
   10076                 :       18489 :   tree field;
   10077                 :       18489 :   tree tmp;
   10078                 :       18489 :   gfc_se se;
   10079                 :             : 
   10080                 :       18489 :   gfc_start_block (&block);
   10081                 :             : 
   10082                 :       18489 :   if (expr->ts.u.derived->from_intmod == INTMOD_ISO_C_BINDING
   10083                 :         172 :       && (expr->ts.u.derived->intmod_sym_id == ISOCBINDING_PTR
   10084                 :           9 :           || expr->ts.u.derived->intmod_sym_id == ISOCBINDING_FUNPTR))
   10085                 :             :     {
   10086                 :         172 :       gfc_se lse;
   10087                 :             : 
   10088                 :         172 :       gfc_init_se (&se, NULL);
   10089                 :         172 :       gfc_init_se (&lse, NULL);
   10090                 :         172 :       gfc_conv_expr (&se, gfc_constructor_first (expr->value.constructor)->expr);
   10091                 :         172 :       lse.expr = dest;
   10092                 :         172 :       gfc_add_modify (&block, lse.expr,
   10093                 :         172 :                       fold_convert (TREE_TYPE (lse.expr), se.expr));
   10094                 :             : 
   10095                 :         172 :       return gfc_finish_block (&block);
   10096                 :             :     }
   10097                 :             : 
   10098                 :             :   /* Make sure that the derived type has been completely built.  */
   10099                 :       18317 :   if (!expr->ts.u.derived->backend_decl
   10100                 :       18317 :       || !TYPE_FIELDS (expr->ts.u.derived->backend_decl))
   10101                 :             :     {
   10102                 :         223 :       tmp = gfc_typenode_for_spec (&expr->ts);
   10103                 :         223 :       gcc_assert (tmp);
   10104                 :             :     }
   10105                 :             : 
   10106                 :       18317 :   cm = expr->ts.u.derived->components;
   10107                 :             : 
   10108                 :             : 
   10109                 :       18317 :   if (coarray)
   10110                 :         168 :     gfc_init_se (&se, NULL);
   10111                 :             : 
   10112                 :       18317 :   for (c = gfc_constructor_first (expr->value.constructor);
   10113                 :       47438 :        c; c = gfc_constructor_next (c), cm = cm->next)
   10114                 :             :     {
   10115                 :             :       /* Skip absent members in default initializers.  */
   10116                 :       29121 :       if (!c->expr && !cm->attr.allocatable)
   10117                 :        2906 :         continue;
   10118                 :             : 
   10119                 :             :       /* Register the component with the caf-lib before it is initialized.
   10120                 :             :          Register only allocatable components, that are not coarray'ed
   10121                 :             :          components (%comp[*]).  Only register when the constructor is the
   10122                 :             :          null-expression.  */
   10123                 :       26215 :       if (coarray && !cm->attr.codimension
   10124                 :         380 :           && (cm->attr.allocatable || cm->attr.pointer)
   10125                 :         169 :           && (!c->expr || c->expr->expr_type == EXPR_NULL))
   10126                 :             :         {
   10127                 :         168 :           tree token, desc, size;
   10128                 :         336 :           bool is_array = cm->ts.type == BT_CLASS
   10129                 :         168 :               ? CLASS_DATA (cm)->attr.dimension : cm->attr.dimension;
   10130                 :             : 
   10131                 :         168 :           field = cm->backend_decl;
   10132                 :         168 :           field = fold_build3_loc (input_location, COMPONENT_REF,
   10133                 :         168 :                                    TREE_TYPE (field), dest, field, NULL_TREE);
   10134                 :         168 :           if (cm->ts.type == BT_CLASS)
   10135                 :           0 :             field = gfc_class_data_get (field);
   10136                 :             : 
   10137                 :         168 :           token
   10138                 :         168 :             = is_array
   10139                 :         168 :                 ? gfc_conv_descriptor_token (field)
   10140                 :          49 :                 : fold_build3_loc (input_location, COMPONENT_REF,
   10141                 :          49 :                                    TREE_TYPE (gfc_comp_caf_token (cm)), dest,
   10142                 :          49 :                                    gfc_comp_caf_token (cm), NULL_TREE);
   10143                 :             : 
   10144                 :         168 :           if (is_array)
   10145                 :             :             {
   10146                 :             :               /* The _caf_register routine looks at the rank of the array
   10147                 :             :                  descriptor to decide whether the data registered is an array
   10148                 :             :                  or not.  */
   10149                 :         119 :               int rank = cm->ts.type == BT_CLASS ? CLASS_DATA (cm)->as->rank
   10150                 :         119 :                                                  : cm->as->rank;
   10151                 :             :               /* When the rank is not known just set a positive rank, which
   10152                 :             :                  suffices to recognize the data as array.  */
   10153                 :         119 :               if (rank < 0)
   10154                 :           0 :                 rank = 1;
   10155                 :         119 :               size = build_zero_cst (size_type_node);
   10156                 :         119 :               desc = field;
   10157                 :         119 :               gfc_add_modify (&block, gfc_conv_descriptor_rank (desc),
   10158                 :         119 :                               build_int_cst (signed_char_type_node, rank));
   10159                 :             :             }
   10160                 :             :           else
   10161                 :             :             {
   10162                 :          49 :               desc = gfc_conv_scalar_to_descriptor (&se, field,
   10163                 :          49 :                                                     cm->ts.type == BT_CLASS
   10164                 :          49 :                                                     ? CLASS_DATA (cm)->attr
   10165                 :             :                                                     : cm->attr);
   10166                 :          49 :               size = TYPE_SIZE_UNIT (TREE_TYPE (field));
   10167                 :             :             }
   10168                 :         168 :           gfc_add_block_to_block (&block, &se.pre);
   10169                 :         336 :           tmp =  build_call_expr_loc (input_location, gfor_fndecl_caf_register,
   10170                 :         168 :                                       7, size, build_int_cst (
   10171                 :             :                                         integer_type_node,
   10172                 :         168 :                                         GFC_CAF_COARRAY_ALLOC_REGISTER_ONLY),
   10173                 :             :                                       gfc_build_addr_expr (pvoid_type_node,
   10174                 :             :                                                            token),
   10175                 :             :                                       gfc_build_addr_expr (NULL_TREE, desc),
   10176                 :             :                                       null_pointer_node, null_pointer_node,
   10177                 :             :                                       integer_zero_node);
   10178                 :         168 :           gfc_add_expr_to_block (&block, tmp);
   10179                 :             :         }
   10180                 :       26215 :       field = cm->backend_decl;
   10181                 :       26215 :       gcc_assert(field);
   10182                 :       26215 :       tmp = fold_build3_loc (input_location, COMPONENT_REF, TREE_TYPE (field),
   10183                 :             :                              dest, field, NULL_TREE);
   10184                 :       26215 :       if (!c->expr)
   10185                 :             :         {
   10186                 :           0 :           gfc_expr *e = gfc_get_null_expr (NULL);
   10187                 :           0 :           tmp = gfc_trans_subcomponent_assign (tmp, cm, e, init);
   10188                 :           0 :           gfc_free_expr (e);
   10189                 :             :         }
   10190                 :             :       else
   10191                 :       26215 :         tmp = gfc_trans_subcomponent_assign (tmp, cm, c->expr, init);
   10192                 :       26215 :       gfc_add_expr_to_block (&block, tmp);
   10193                 :             :     }
   10194                 :       18317 :   return gfc_finish_block (&block);
   10195                 :             : }
   10196                 :             : 
   10197                 :             : static void
   10198                 :          21 : gfc_conv_union_initializer (vec<constructor_elt, va_gc> *&v,
   10199                 :             :                             gfc_component *un, gfc_expr *init)
   10200                 :             : {
   10201                 :          21 :   gfc_constructor *ctor;
   10202                 :             : 
   10203                 :          21 :   if (un->ts.type != BT_UNION || un == NULL || init == NULL)
   10204                 :             :     return;
   10205                 :             : 
   10206                 :          21 :   ctor = gfc_constructor_first (init->value.constructor);
   10207                 :             : 
   10208                 :          21 :   if (ctor == NULL || ctor->expr == NULL)
   10209                 :             :     return;
   10210                 :             : 
   10211                 :          21 :   gcc_assert (init->expr_type == EXPR_STRUCTURE);
   10212                 :             : 
   10213                 :             :   /* If we have an 'initialize all' constructor, do it first.  */
   10214                 :          21 :   if (ctor->expr->expr_type == EXPR_NULL)
   10215                 :             :     {
   10216                 :           9 :       tree union_type = TREE_TYPE (un->backend_decl);
   10217                 :           9 :       tree val = build_constructor (union_type, NULL);
   10218                 :           9 :       CONSTRUCTOR_APPEND_ELT (v, un->backend_decl, val);
   10219                 :           9 :       ctor = gfc_constructor_next (ctor);
   10220                 :             :     }
   10221                 :             : 
   10222                 :             :   /* Add the map initializer on top.  */
   10223                 :          21 :   if (ctor != NULL && ctor->expr != NULL)
   10224                 :             :     {
   10225                 :          12 :       gcc_assert (ctor->expr->expr_type == EXPR_STRUCTURE);
   10226                 :          12 :       tree val = gfc_conv_initializer (ctor->expr, &un->ts,
   10227                 :          12 :                                        TREE_TYPE (un->backend_decl),
   10228                 :          12 :                                        un->attr.dimension, un->attr.pointer,
   10229                 :          12 :                                        un->attr.proc_pointer);
   10230                 :          12 :       CONSTRUCTOR_APPEND_ELT (v, un->backend_decl, val);
   10231                 :             :     }
   10232                 :             : }
   10233                 :             : 
   10234                 :             : /* Build an expression for a constructor. If init is nonzero then
   10235                 :             :    this is part of a static variable initializer.  */
   10236                 :             : 
   10237                 :             : void
   10238                 :       35845 : gfc_conv_structure (gfc_se * se, gfc_expr * expr, int init)
   10239                 :             : {
   10240                 :       35845 :   gfc_constructor *c;
   10241                 :       35845 :   gfc_component *cm;
   10242                 :       35845 :   tree val;
   10243                 :       35845 :   tree type;
   10244                 :       35845 :   tree tmp;
   10245                 :       35845 :   vec<constructor_elt, va_gc> *v = NULL;
   10246                 :             : 
   10247                 :       35845 :   gcc_assert (se->ss == NULL);
   10248                 :       35845 :   gcc_assert (expr->expr_type == EXPR_STRUCTURE);
   10249                 :       35845 :   type = gfc_typenode_for_spec (&expr->ts);
   10250                 :             : 
   10251                 :       35845 :   if (!init)
   10252                 :             :     {
   10253                 :             :       /* Create a temporary variable and fill it in.  */
   10254                 :       14605 :       se->expr = gfc_create_var (type, expr->ts.u.derived->name);
   10255                 :             :       /* The symtree in expr is NULL, if the code to generate is for
   10256                 :             :          initializing the static members only.  */
   10257                 :       29210 :       tmp = gfc_trans_structure_assign (se->expr, expr, expr->symtree != NULL,
   10258                 :       14605 :                                         se->want_coarray);
   10259                 :       14605 :       gfc_add_expr_to_block (&se->pre, tmp);
   10260                 :       14605 :       return;
   10261                 :             :     }
   10262                 :             : 
   10263                 :       21240 :   cm = expr->ts.u.derived->components;
   10264                 :             : 
   10265                 :       21240 :   for (c = gfc_constructor_first (expr->value.constructor);
   10266                 :      112207 :        c && cm; c = gfc_constructor_next (c), cm = cm->next)
   10267                 :             :     {
   10268                 :             :       /* Skip absent members in default initializers and allocatable
   10269                 :             :          components.  Although the latter have a default initializer
   10270                 :             :          of EXPR_NULL,... by default, the static nullify is not needed
   10271                 :             :          since this is done every time we come into scope.  */
   10272                 :       90967 :       if (!c->expr || (cm->attr.allocatable && cm->attr.flavor != FL_PROCEDURE))
   10273                 :        7696 :         continue;
   10274                 :             : 
   10275                 :       83271 :       if (cm->initializer && cm->initializer->expr_type != EXPR_NULL
   10276                 :       48074 :           && strcmp (cm->name, "_extends") == 0
   10277                 :        1254 :           && cm->initializer->symtree)
   10278                 :             :         {
   10279                 :        1254 :           tree vtab;
   10280                 :        1254 :           gfc_symbol *vtabs;
   10281                 :        1254 :           vtabs = cm->initializer->symtree->n.sym;
   10282                 :        1254 :           vtab = gfc_build_addr_expr (NULL_TREE, gfc_get_symbol_decl (vtabs));
   10283                 :        1254 :           vtab = unshare_expr_without_location (vtab);
   10284                 :        1254 :           CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, vtab);
   10285                 :        1254 :         }
   10286                 :       82017 :       else if (cm->ts.u.derived && strcmp (cm->name, "_size") == 0)
   10287                 :             :         {
   10288                 :        9091 :           val = TYPE_SIZE_UNIT (gfc_get_derived_type (cm->ts.u.derived));
   10289                 :        9091 :           CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
   10290                 :             :                                   fold_convert (TREE_TYPE (cm->backend_decl),
   10291                 :             :                                                 val));
   10292                 :        9091 :         }
   10293                 :       72926 :       else if (cm->ts.type == BT_INTEGER && strcmp (cm->name, "_len") == 0)
   10294                 :         387 :         CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl,
   10295                 :             :                                 fold_convert (TREE_TYPE (cm->backend_decl),
   10296                 :         387 :                                               integer_zero_node));
   10297                 :       72539 :       else if (cm->ts.type == BT_UNION)
   10298                 :          21 :         gfc_conv_union_initializer (v, cm, c->expr);
   10299                 :             :       else
   10300                 :             :         {
   10301                 :       72518 :           val = gfc_conv_initializer (c->expr, &cm->ts,
   10302                 :       72518 :                                       TREE_TYPE (cm->backend_decl),
   10303                 :             :                                       cm->attr.dimension, cm->attr.pointer,
   10304                 :       72518 :                                       cm->attr.proc_pointer);
   10305                 :       72518 :           val = unshare_expr_without_location (val);
   10306                 :             : 
   10307                 :             :           /* Append it to the constructor list.  */
   10308                 :      163485 :           CONSTRUCTOR_APPEND_ELT (v, cm->backend_decl, val);
   10309                 :             :         }
   10310                 :             :     }
   10311                 :             : 
   10312                 :       21240 :   se->expr = build_constructor (type, v);
   10313                 :       21240 :   if (init)
   10314                 :       21240 :     TREE_CONSTANT (se->expr) = 1;
   10315                 :             : }
   10316                 :             : 
   10317                 :             : 
   10318                 :             : /* Translate a substring expression.  */
   10319                 :             : 
   10320                 :             : static void
   10321                 :         258 : gfc_conv_substring_expr (gfc_se * se, gfc_expr * expr)
   10322                 :             : {
   10323                 :         258 :   gfc_ref *ref;
   10324                 :             : 
   10325                 :         258 :   ref = expr->ref;
   10326                 :             : 
   10327                 :         258 :   gcc_assert (ref == NULL || ref->type == REF_SUBSTRING);
   10328                 :             : 
   10329                 :         516 :   se->expr = gfc_build_wide_string_const (expr->ts.kind,
   10330                 :         258 :                                           expr->value.character.length,
   10331                 :         258 :                                           expr->value.character.string);
   10332                 :             : 
   10333                 :         258 :   se->string_length = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (se->expr)));
   10334                 :         258 :   TYPE_STRING_FLAG (TREE_TYPE (se->expr)) = 1;
   10335                 :             : 
   10336                 :         258 :   if (ref)
   10337                 :         258 :     gfc_conv_substring (se, ref, expr->ts.kind, NULL, &expr->where);
   10338                 :         258 : }
   10339                 :             : 
   10340                 :             : 
   10341                 :             : /* Entry point for expression translation.  Evaluates a scalar quantity.
   10342                 :             :    EXPR is the expression to be translated, and SE is the state structure if
   10343                 :             :    called from within the scalarized.  */
   10344                 :             : 
   10345                 :             : void
   10346                 :     3445308 : gfc_conv_expr (gfc_se * se, gfc_expr * expr)
   10347                 :             : {
   10348                 :     3445308 :   gfc_ss *ss;
   10349                 :             : 
   10350                 :     3445308 :   ss = se->ss;
   10351                 :     3445308 :   if (ss && ss->info->expr == expr
   10352                 :      216200 :       && (ss->info->type == GFC_SS_SCALAR
   10353                 :             :           || ss->info->type == GFC_SS_REFERENCE))
   10354                 :             :     {
   10355                 :       34785 :       gfc_ss_info *ss_info;
   10356                 :             : 
   10357                 :       34785 :       ss_info = ss->info;
   10358                 :             :       /* Substitute a scalar expression evaluated outside the scalarization
   10359                 :             :          loop.  */
   10360                 :       34785 :       se->expr = ss_info->data.scalar.value;
   10361                 :       34785 :       if (gfc_scalar_elemental_arg_saved_as_reference (ss_info))
   10362                 :         824 :         se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
   10363                 :             : 
   10364                 :       34785 :       se->string_length = ss_info->string_length;
   10365                 :       34785 :       gfc_advance_se_ss_chain (se);
   10366                 :       34785 :       return;
   10367                 :             :     }
   10368                 :             : 
   10369                 :             :   /* We need to convert the expressions for the iso_c_binding derived types.
   10370                 :             :      C_NULL_PTR and C_NULL_FUNPTR will be made EXPR_NULL, which evaluates to
   10371                 :             :      null_pointer_node.  C_PTR and C_FUNPTR are converted to match the
   10372                 :             :      typespec for the C_PTR and C_FUNPTR symbols, which has already been
   10373                 :             :      updated to be an integer with a kind equal to the size of a (void *).  */
   10374                 :     3410523 :   if (expr->ts.type == BT_DERIVED && expr->ts.u.derived->ts.f90_type == BT_VOID
   10375                 :       15559 :       && expr->ts.u.derived->attr.is_bind_c)
   10376                 :             :     {
   10377                 :       14742 :       if (expr->expr_type == EXPR_VARIABLE
   10378                 :       10552 :           && (expr->symtree->n.sym->intmod_sym_id == ISOCBINDING_NULL_PTR
   10379                 :       10552 :               || expr->symtree->n.sym->intmod_sym_id
   10380                 :             :                  == ISOCBINDING_NULL_FUNPTR))
   10381                 :             :         {
   10382                 :             :           /* Set expr_type to EXPR_NULL, which will result in
   10383                 :             :              null_pointer_node being used below.  */
   10384                 :           0 :           expr->expr_type = EXPR_NULL;
   10385                 :             :         }
   10386                 :             :       else
   10387                 :             :         {
   10388                 :             :           /* Update the type/kind of the expression to be what the new
   10389                 :             :              type/kind are for the updated symbols of C_PTR/C_FUNPTR.  */
   10390                 :       14742 :           expr->ts.type = BT_INTEGER;
   10391                 :       14742 :           expr->ts.f90_type = BT_VOID;
   10392                 :       14742 :           expr->ts.kind = gfc_index_integer_kind;
   10393                 :             :         }
   10394                 :             :     }
   10395                 :             : 
   10396                 :     3410523 :   gfc_fix_class_refs (expr);
   10397                 :             : 
   10398                 :     3410523 :   switch (expr->expr_type)
   10399                 :             :     {
   10400                 :      484567 :     case EXPR_OP:
   10401                 :      484567 :       gfc_conv_expr_op (se, expr);
   10402                 :      484567 :       break;
   10403                 :             : 
   10404                 :      279472 :     case EXPR_FUNCTION:
   10405                 :      279472 :       gfc_conv_function_expr (se, expr);
   10406                 :      279472 :       break;
   10407                 :             : 
   10408                 :     1078269 :     case EXPR_CONSTANT:
   10409                 :     1078269 :       gfc_conv_constant (se, expr);
   10410                 :     1078269 :       break;
   10411                 :             : 
   10412                 :     1515340 :     case EXPR_VARIABLE:
   10413                 :     1515340 :       gfc_conv_variable (se, expr);
   10414                 :     1515340 :       break;
   10415                 :             : 
   10416                 :        3845 :     case EXPR_NULL:
   10417                 :        3845 :       se->expr = null_pointer_node;
   10418                 :        3845 :       break;
   10419                 :             : 
   10420                 :         258 :     case EXPR_SUBSTRING:
   10421                 :         258 :       gfc_conv_substring_expr (se, expr);
   10422                 :         258 :       break;
   10423                 :             : 
   10424                 :       14605 :     case EXPR_STRUCTURE:
   10425                 :       14605 :       gfc_conv_structure (se, expr, 0);
   10426                 :             :       /* F2008 4.5.6.3 para 5: If an executable construct references a
   10427                 :             :          structure constructor or array constructor, the entity created by
   10428                 :             :          the constructor is finalized after execution of the innermost
   10429                 :             :          executable construct containing the reference. This, in fact,
   10430                 :             :          was later deleted by the Combined Techical Corrigenda 1 TO 4 for
   10431                 :             :          fortran 2008 (f08/0011).  */
   10432                 :       14605 :       if ((gfc_option.allow_std & (GFC_STD_F2008 | GFC_STD_F2003))
   10433                 :       14605 :           && !(gfc_option.allow_std & GFC_STD_GNU)
   10434                 :         139 :           && expr->must_finalize
   10435                 :       14617 :           && gfc_may_be_finalized (expr->ts))
   10436                 :             :         {
   10437                 :          12 :           locus loc;
   10438                 :          12 :           gfc_locus_from_location (&loc, input_location);
   10439                 :          12 :           gfc_warning (0, "The structure constructor at %L has been"
   10440                 :             :                          " finalized. This feature was removed by f08/0011."
   10441                 :             :                          " Use -std=f2018 or -std=gnu to eliminate the"
   10442                 :             :                          " finalization.", &loc);
   10443                 :          12 :           symbol_attribute attr;
   10444                 :          12 :           attr.allocatable = attr.pointer = 0;
   10445                 :          12 :           gfc_finalize_tree_expr (se, expr->ts.u.derived, attr, 0);
   10446                 :          12 :           gfc_add_block_to_block (&se->post, &se->finalblock);
   10447                 :             :         }
   10448                 :             :       break;
   10449                 :             : 
   10450                 :       34167 :     case EXPR_ARRAY:
   10451                 :       34167 :       gfc_conv_array_constructor_expr (se, expr);
   10452                 :       34167 :       gfc_add_block_to_block (&se->post, &se->finalblock);
   10453                 :       34167 :       break;
   10454                 :             : 
   10455                 :           0 :     default:
   10456                 :           0 :       gcc_unreachable ();
   10457                 :     3445308 :       break;
   10458                 :             :     }
   10459                 :             : }
   10460                 :             : 
   10461                 :             : /* Like gfc_conv_expr_val, but the value is also suitable for use in the lhs
   10462                 :             :    of an assignment.  */
   10463                 :             : void
   10464                 :      348421 : gfc_conv_expr_lhs (gfc_se * se, gfc_expr * expr)
   10465                 :             : {
   10466                 :      348421 :   gfc_conv_expr (se, expr);
   10467                 :             :   /* All numeric lvalues should have empty post chains.  If not we need to
   10468                 :             :      figure out a way of rewriting an lvalue so that it has no post chain.  */
   10469                 :      348421 :   gcc_assert (expr->ts.type == BT_CHARACTER || !se->post.head);
   10470                 :      348421 : }
   10471                 :             : 
   10472                 :             : /* Like gfc_conv_expr, but the POST block is guaranteed to be empty for
   10473                 :             :    numeric expressions.  Used for scalar values where inserting cleanup code
   10474                 :             :    is inconvenient.  */
   10475                 :             : void
   10476                 :      974543 : gfc_conv_expr_val (gfc_se * se, gfc_expr * expr)
   10477                 :             : {
   10478                 :      974543 :   tree val;
   10479                 :             : 
   10480                 :      974543 :   gcc_assert (expr->ts.type != BT_CHARACTER);
   10481                 :      974543 :   gfc_conv_expr (se, expr);
   10482                 :      974543 :   if (se->post.head)
   10483                 :             :     {
   10484                 :        2416 :       val = gfc_create_var (TREE_TYPE (se->expr), NULL);
   10485                 :        2416 :       gfc_add_modify (&se->pre, val, se->expr);
   10486                 :        2416 :       se->expr = val;
   10487                 :        2416 :       gfc_add_block_to_block (&se->pre, &se->post);
   10488                 :             :     }
   10489                 :      974543 : }
   10490                 :             : 
   10491                 :             : /* Helper to translate an expression and convert it to a particular type.  */
   10492                 :             : void
   10493                 :      273862 : gfc_conv_expr_type (gfc_se * se, gfc_expr * expr, tree type)
   10494                 :             : {
   10495                 :      273862 :   gfc_conv_expr_val (se, expr);
   10496                 :      273862 :   se->expr = convert (type, se->expr);
   10497                 :      273862 : }
   10498                 :             : 
   10499                 :             : 
   10500                 :             : /* Converts an expression so that it can be passed by reference.  Scalar
   10501                 :             :    values only.  */
   10502                 :             : 
   10503                 :             : void
   10504                 :      218531 : gfc_conv_expr_reference (gfc_se * se, gfc_expr * expr)
   10505                 :             : {
   10506                 :      218531 :   gfc_ss *ss;
   10507                 :      218531 :   tree var;
   10508                 :             : 
   10509                 :      218531 :   ss = se->ss;
   10510                 :      218531 :   if (ss && ss->info->expr == expr
   10511                 :        7154 :       && ss->info->type == GFC_SS_REFERENCE)
   10512                 :             :     {
   10513                 :             :       /* Returns a reference to the scalar evaluated outside the loop
   10514                 :             :          for this case.  */
   10515                 :         906 :       gfc_conv_expr (se, expr);
   10516                 :             : 
   10517                 :         906 :       if (expr->ts.type == BT_CHARACTER
   10518                 :         114 :           && expr->expr_type != EXPR_FUNCTION)
   10519                 :         102 :         gfc_conv_string_parameter (se);
   10520                 :             :      else
   10521                 :         804 :         se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
   10522                 :             : 
   10523                 :         906 :       return;
   10524                 :             :     }
   10525                 :             : 
   10526                 :      217625 :   if (expr->ts.type == BT_CHARACTER)
   10527                 :             :     {
   10528                 :       48415 :       gfc_conv_expr (se, expr);
   10529                 :       48415 :       gfc_conv_string_parameter (se);
   10530                 :       48415 :       return;
   10531                 :             :     }
   10532                 :             : 
   10533                 :      169210 :   if (expr->expr_type == EXPR_VARIABLE)
   10534                 :             :     {
   10535                 :       66859 :       se->want_pointer = 1;
   10536                 :       66859 :       gfc_conv_expr (se, expr);
   10537                 :       66859 :       if (se->post.head)
   10538                 :             :         {
   10539                 :           0 :           var = gfc_create_var (TREE_TYPE (se->expr), NULL);
   10540                 :           0 :           gfc_add_modify (&se->pre, var, se->expr);
   10541                 :           0 :           gfc_add_block_to_block (&se->pre, &se->post);
   10542                 :           0 :           se->expr = var;
   10543                 :             :         }
   10544                 :       66859 :       return;
   10545                 :             :     }
   10546                 :             : 
   10547                 :      102351 :   if (expr->expr_type == EXPR_FUNCTION
   10548                 :       12968 :       && ((expr->value.function.esym
   10549                 :        2026 :            && expr->value.function.esym->result
   10550                 :             :            && expr->value.function.esym->result->attr.pointer
   10551                 :        2025 :            && !expr->value.function.esym->result->attr.dimension)
   10552                 :       12903 :           || (!expr->value.function.esym && !expr->ref
   10553                 :       10836 :               && expr->symtree->n.sym->attr.pointer
   10554                 :       10836 :               && !expr->symtree->n.sym->attr.dimension)))
   10555                 :             :     {
   10556                 :          65 :       se->want_pointer = 1;
   10557                 :          65 :       gfc_conv_expr (se, expr);
   10558                 :          65 :       var = gfc_create_var (TREE_TYPE (se->expr), NULL);
   10559                 :          65 :       gfc_add_modify (&se->pre, var, se->expr);
   10560                 :          65 :       se->expr = var;
   10561                 :          65 :       return;
   10562                 :             :     }
   10563                 :             : 
   10564                 :      102286 :   gfc_conv_expr (se, expr);
   10565                 :             : 
   10566                 :             :   /* Create a temporary var to hold the value.  */
   10567                 :      102286 :   if (TREE_CONSTANT (se->expr))
   10568                 :             :     {
   10569                 :             :       tree tmp = se->expr;
   10570                 :       81496 :       STRIP_TYPE_NOPS (tmp);
   10571                 :       81496 :       var = build_decl (input_location,
   10572                 :       81496 :                         CONST_DECL, NULL, TREE_TYPE (tmp));
   10573                 :       81496 :       DECL_INITIAL (var) = tmp;
   10574                 :       81496 :       TREE_STATIC (var) = 1;
   10575                 :       81496 :       pushdecl (var);
   10576                 :             :     }
   10577                 :             :   else
   10578                 :             :     {
   10579                 :       20790 :       var = gfc_create_var (TREE_TYPE (se->expr), NULL);
   10580                 :       20790 :       gfc_add_modify (&se->pre, var, se->expr);
   10581                 :             :     }
   10582                 :             : 
   10583                 :      102286 :   if (!expr->must_finalize)
   10584                 :      102196 :     gfc_add_block_to_block (&se->pre, &se->post);
   10585                 :             : 
   10586                 :             :   /* Take the address of that value.  */
   10587                 :      102286 :   se->expr = gfc_build_addr_expr (NULL_TREE, var);
   10588                 :             : }
   10589                 :             : 
   10590                 :             : 
   10591                 :             : /* Get the _len component for an unlimited polymorphic expression.  */
   10592                 :             : 
   10593                 :             : static tree
   10594                 :        1641 : trans_get_upoly_len (stmtblock_t *block, gfc_expr *expr)
   10595                 :             : {
   10596                 :        1641 :   gfc_se se;
   10597                 :        1641 :   gfc_ref *ref = expr->ref;
   10598                 :             : 
   10599                 :        1641 :   gfc_init_se (&se, NULL);
   10600                 :        3372 :   while (ref && ref->next)
   10601                 :             :     ref = ref->next;
   10602                 :        1641 :   gfc_add_len_component (expr);
   10603                 :        1641 :   gfc_conv_expr (&se, expr);
   10604                 :        1641 :   gfc_add_block_to_block (block, &se.pre);
   10605                 :        1641 :   gcc_assert (se.post.head == NULL_TREE);
   10606                 :        1641 :   if (ref)
   10607                 :             :     {
   10608                 :         238 :       gfc_free_ref_list (ref->next);
   10609                 :         238 :       ref->next = NULL;
   10610                 :             :     }
   10611                 :             :   else
   10612                 :             :     {
   10613                 :        1403 :       gfc_free_ref_list (expr->ref);
   10614                 :        1403 :       expr->ref = NULL;
   10615                 :             :     }
   10616                 :        1641 :   return se.expr;
   10617                 :             : }
   10618                 :             : 
   10619                 :             : 
   10620                 :             : /* Assign _vptr and _len components as appropriate.  BLOCK should be a
   10621                 :             :    statement-list outside of the scalarizer-loop.  When code is generated, that
   10622                 :             :    depends on the scalarized expression, it is added to RSE.PRE.
   10623                 :             :    Returns le's _vptr tree and when set the len expressions in to_lenp and
   10624                 :             :    from_lenp to form a le%_vptr%_copy (re, le, [from_lenp, to_lenp])
   10625                 :             :    expression.  */
   10626                 :             : 
   10627                 :             : static tree
   10628                 :        4307 : trans_class_vptr_len_assignment (stmtblock_t *block, gfc_expr * le,
   10629                 :             :                                  gfc_expr * re, gfc_se *rse,
   10630                 :             :                                  tree * to_lenp, tree * from_lenp,
   10631                 :             :                                  tree * from_vptrp)
   10632                 :             : {
   10633                 :        4307 :   gfc_se se;
   10634                 :        4307 :   gfc_expr * vptr_expr;
   10635                 :        4307 :   tree tmp, to_len = NULL_TREE, from_len = NULL_TREE, lhs_vptr;
   10636                 :        4307 :   bool set_vptr = false, temp_rhs = false;
   10637                 :        4307 :   stmtblock_t *pre = block;
   10638                 :        4307 :   tree class_expr = NULL_TREE;
   10639                 :        4307 :   tree from_vptr = NULL_TREE;
   10640                 :             : 
   10641                 :             :   /* Create a temporary for complicated expressions.  */
   10642                 :        4307 :   if (re->expr_type != EXPR_VARIABLE && re->expr_type != EXPR_NULL
   10643                 :        1165 :       && rse->expr != NULL_TREE)
   10644                 :             :     {
   10645                 :        1165 :       if (!DECL_P (rse->expr))
   10646                 :             :         {
   10647                 :         324 :           if (re->ts.type == BT_CLASS && !GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
   10648                 :          37 :             class_expr = gfc_get_class_from_expr (rse->expr);
   10649                 :             : 
   10650                 :         324 :           if (rse->loop)
   10651                 :         111 :             pre = &rse->loop->pre;
   10652                 :             :           else
   10653                 :         213 :             pre = &rse->pre;
   10654                 :             : 
   10655                 :         324 :           if (class_expr != NULL_TREE && UNLIMITED_POLY (re))
   10656                 :          37 :               tmp = gfc_evaluate_now (TREE_OPERAND (rse->expr, 0), &rse->pre);
   10657                 :             :           else
   10658                 :         287 :               tmp = gfc_evaluate_now (rse->expr, &rse->pre);
   10659                 :             : 
   10660                 :         324 :           rse->expr = tmp;
   10661                 :             :         }
   10662                 :             :       else
   10663                 :         841 :         pre = &rse->pre;
   10664                 :             : 
   10665                 :             :       temp_rhs = true;
   10666                 :             :     }
   10667                 :             : 
   10668                 :             :   /* Get the _vptr for the left-hand side expression.  */
   10669                 :        4307 :   gfc_init_se (&se, NULL);
   10670                 :        4307 :   vptr_expr = gfc_find_and_cut_at_last_class_ref (le);
   10671                 :        4307 :   if (vptr_expr != NULL && gfc_expr_attr (vptr_expr).class_ok)
   10672                 :             :     {
   10673                 :             :       /* Care about _len for unlimited polymorphic entities.  */
   10674                 :        4289 :       if (UNLIMITED_POLY (vptr_expr)
   10675                 :        3416 :           || (vptr_expr->ts.type == BT_DERIVED
   10676                 :        2427 :               && vptr_expr->ts.u.derived->attr.unlimited_polymorphic))
   10677                 :        1357 :         to_len = trans_get_upoly_len (block, vptr_expr);
   10678                 :        4289 :       gfc_add_vptr_component (vptr_expr);
   10679                 :        4289 :       set_vptr = true;
   10680                 :             :     }
   10681                 :             :   else
   10682                 :          18 :     vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&le->ts));
   10683                 :        4307 :   se.want_pointer = 1;
   10684                 :        4307 :   gfc_conv_expr (&se, vptr_expr);
   10685                 :        4307 :   gfc_free_expr (vptr_expr);
   10686                 :        4307 :   gfc_add_block_to_block (block, &se.pre);
   10687                 :        4307 :   gcc_assert (se.post.head == NULL_TREE);
   10688                 :        4307 :   lhs_vptr = se.expr;
   10689                 :        4307 :   STRIP_NOPS (lhs_vptr);
   10690                 :             : 
   10691                 :             :   /* Set the _vptr only when the left-hand side of the assignment is a
   10692                 :             :      class-object.  */
   10693                 :        4307 :   if (set_vptr)
   10694                 :             :     {
   10695                 :             :       /* Get the vptr from the rhs expression only, when it is variable.
   10696                 :             :          Functions are expected to be assigned to a temporary beforehand.  */
   10697                 :        3017 :       vptr_expr = (re->expr_type == EXPR_VARIABLE && re->ts.type == BT_CLASS)
   10698                 :        5038 :           ? gfc_find_and_cut_at_last_class_ref (re)
   10699                 :             :           : NULL;
   10700                 :         749 :       if (vptr_expr != NULL && vptr_expr->ts.type == BT_CLASS)
   10701                 :             :         {
   10702                 :         749 :           if (to_len != NULL_TREE)
   10703                 :             :             {
   10704                 :             :               /* Get the _len information from the rhs.  */
   10705                 :         299 :               if (UNLIMITED_POLY (vptr_expr)
   10706                 :             :                   || (vptr_expr->ts.type == BT_DERIVED
   10707                 :             :                       && vptr_expr->ts.u.derived->attr.unlimited_polymorphic))
   10708                 :         272 :                 from_len = trans_get_upoly_len (block, vptr_expr);
   10709                 :             :             }
   10710                 :         749 :           gfc_add_vptr_component (vptr_expr);
   10711                 :             :         }
   10712                 :             :       else
   10713                 :             :         {
   10714                 :        3540 :           if (re->expr_type == EXPR_VARIABLE
   10715                 :        2268 :               && DECL_P (re->symtree->n.sym->backend_decl)
   10716                 :        2268 :               && DECL_LANG_SPECIFIC (re->symtree->n.sym->backend_decl)
   10717                 :         813 :               && GFC_DECL_SAVED_DESCRIPTOR (re->symtree->n.sym->backend_decl)
   10718                 :        3607 :               && GFC_CLASS_TYPE_P (TREE_TYPE (GFC_DECL_SAVED_DESCRIPTOR (
   10719                 :             :                                            re->symtree->n.sym->backend_decl))))
   10720                 :             :             {
   10721                 :          43 :               vptr_expr = NULL;
   10722                 :          43 :               se.expr = gfc_class_vptr_get (GFC_DECL_SAVED_DESCRIPTOR (
   10723                 :             :                                              re->symtree->n.sym->backend_decl));
   10724                 :          43 :               if (to_len && UNLIMITED_POLY (re))
   10725                 :           0 :                 from_len = gfc_class_len_get (GFC_DECL_SAVED_DESCRIPTOR (
   10726                 :             :                                              re->symtree->n.sym->backend_decl));
   10727                 :             :             }
   10728                 :        3497 :           else if (temp_rhs && re->ts.type == BT_CLASS)
   10729                 :             :             {
   10730                 :         207 :               vptr_expr = NULL;
   10731                 :         207 :               if (class_expr)
   10732                 :             :                 tmp = class_expr;
   10733                 :         170 :               else if (!GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr)))
   10734                 :           0 :                 tmp = gfc_get_class_from_expr (rse->expr);
   10735                 :             :               else
   10736                 :             :                 tmp = rse->expr;
   10737                 :             : 
   10738                 :         207 :               se.expr = gfc_class_vptr_get (tmp);
   10739                 :         207 :               from_vptr = se.expr;
   10740                 :         207 :               if (UNLIMITED_POLY (re))
   10741                 :          67 :                 from_len = gfc_class_len_get (tmp);
   10742                 :             : 
   10743                 :             :             }
   10744                 :        3290 :           else if (re->expr_type != EXPR_NULL)
   10745                 :             :             /* Only when rhs is non-NULL use its declared type for vptr
   10746                 :             :                initialisation.  */
   10747                 :        3171 :             vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&re->ts));
   10748                 :             :           else
   10749                 :             :             /* When the rhs is NULL use the vtab of lhs' declared type.  */
   10750                 :         119 :             vptr_expr = gfc_lval_expr_from_sym (gfc_find_vtab (&le->ts));
   10751                 :             :         }
   10752                 :             : 
   10753                 :        4106 :       if (vptr_expr)
   10754                 :             :         {
   10755                 :        4039 :           gfc_init_se (&se, NULL);
   10756                 :        4039 :           se.want_pointer = 1;
   10757                 :        4039 :           gfc_conv_expr (&se, vptr_expr);
   10758                 :        4039 :           gfc_free_expr (vptr_expr);
   10759                 :        4039 :           gfc_add_block_to_block (block, &se.pre);
   10760                 :        4039 :           gcc_assert (se.post.head == NULL_TREE);
   10761                 :        4039 :           from_vptr = se.expr;
   10762                 :             :         }
   10763                 :        4289 :       gfc_add_modify (pre, lhs_vptr, fold_convert (TREE_TYPE (lhs_vptr),
   10764                 :             :                                                 se.expr));
   10765                 :             : 
   10766                 :        4289 :       if (to_len != NULL_TREE)
   10767                 :             :         {
   10768                 :             :           /* The _len component needs to be set.  Figure how to get the
   10769                 :             :              value of the right-hand side.  */
   10770                 :        1357 :           if (from_len == NULL_TREE)
   10771                 :             :             {
   10772                 :        1018 :               if (rse->string_length != NULL_TREE)
   10773                 :             :                 from_len = rse->string_length;
   10774                 :         578 :               else if (re->ts.type == BT_CHARACTER && re->ts.u.cl->length)
   10775                 :             :                 {
   10776                 :           0 :                   gfc_init_se (&se, NULL);
   10777                 :           0 :                   gfc_conv_expr (&se, re->ts.u.cl->length);
   10778                 :           0 :                   gfc_add_block_to_block (block, &se.pre);
   10779                 :           0 :                   gcc_assert (se.post.head == NULL_TREE);
   10780                 :           0 :                   from_len = gfc_evaluate_now (se.expr, block);
   10781                 :             :                 }
   10782                 :             :               else
   10783                 :         578 :                 from_len = build_zero_cst (gfc_charlen_type_node);
   10784                 :             :             }
   10785                 :        1357 :           gfc_add_modify (pre, to_len, fold_convert (TREE_TYPE (to_len),
   10786                 :             :                                                      from_len));
   10787                 :             :         }
   10788                 :             :     }
   10789                 :             : 
   10790                 :             :   /* Return the _len and _vptr trees only, when requested.  */
   10791                 :        4307 :   if (to_lenp)
   10792                 :        3176 :     *to_lenp = to_len;
   10793                 :        4307 :   if (from_lenp)
   10794                 :        3176 :     *from_lenp = from_len;
   10795                 :        4307 :   if (from_vptrp)
   10796                 :        3176 :     *from_vptrp = from_vptr;
   10797                 :        4307 :   return lhs_vptr;
   10798                 :             : }
   10799                 :             : 
   10800                 :             : 
   10801                 :             : /* Assign tokens for pointer components.  */
   10802                 :             : 
   10803                 :             : static void
   10804                 :           6 : trans_caf_token_assign (gfc_se *lse, gfc_se *rse, gfc_expr *expr1,
   10805                 :             :                         gfc_expr *expr2)
   10806                 :             : {
   10807                 :           6 :   symbol_attribute lhs_attr, rhs_attr;
   10808                 :           6 :   tree tmp, lhs_tok, rhs_tok;
   10809                 :             :   /* Flag to indicated component refs on the rhs.  */
   10810                 :           6 :   bool rhs_cr;
   10811                 :             : 
   10812                 :           6 :   lhs_attr = gfc_caf_attr (expr1);
   10813                 :           6 :   if (expr2->expr_type != EXPR_NULL)
   10814                 :             :     {
   10815                 :           4 :       rhs_attr = gfc_caf_attr (expr2, false, &rhs_cr);
   10816                 :           4 :       if (lhs_attr.codimension && rhs_attr.codimension)
   10817                 :             :         {
   10818                 :           2 :           lhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (lse, expr1);
   10819                 :           2 :           lhs_tok = build_fold_indirect_ref (lhs_tok);
   10820                 :             : 
   10821                 :           2 :           if (rhs_cr)
   10822                 :           0 :             rhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (rse, expr2);
   10823                 :             :           else
   10824                 :             :             {
   10825                 :           2 :               tree caf_decl;
   10826                 :           2 :               caf_decl = gfc_get_tree_for_caf_expr (expr2);
   10827                 :           2 :               gfc_get_caf_token_offset (rse, &rhs_tok, NULL, caf_decl,
   10828                 :             :                                         NULL_TREE, NULL);
   10829                 :             :             }
   10830                 :           2 :           tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
   10831                 :             :                             lhs_tok,
   10832                 :           2 :                             fold_convert (TREE_TYPE (lhs_tok), rhs_tok));
   10833                 :           2 :           gfc_prepend_expr_to_block (&lse->post, tmp);
   10834                 :             :         }
   10835                 :             :     }
   10836                 :           2 :   else if (lhs_attr.codimension)
   10837                 :             :     {
   10838                 :           2 :       lhs_tok = gfc_get_ultimate_alloc_ptr_comps_caf_token (lse, expr1);
   10839                 :           2 :       if (!lhs_tok)
   10840                 :             :         {
   10841                 :           1 :           lhs_tok = gfc_get_tree_for_caf_expr (expr1);
   10842                 :           1 :           lhs_tok = GFC_TYPE_ARRAY_CAF_TOKEN (TREE_TYPE (lhs_tok));
   10843                 :             :         }
   10844                 :             :       else
   10845                 :           1 :         lhs_tok = build_fold_indirect_ref (lhs_tok);
   10846                 :           2 :       tmp = build2_loc (input_location, MODIFY_EXPR, void_type_node,
   10847                 :             :                         lhs_tok, null_pointer_node);
   10848                 :           2 :       gfc_prepend_expr_to_block (&lse->post, tmp);
   10849                 :             :     }
   10850                 :           6 : }
   10851                 :             : 
   10852                 :             : 
   10853                 :             : /* Do everything that is needed for a CLASS function expr2.  */
   10854                 :             : 
   10855                 :             : static tree
   10856                 :          18 : trans_class_pointer_fcn (stmtblock_t *block, gfc_se *lse, gfc_se *rse,
   10857                 :             :                          gfc_expr *expr1, gfc_expr *expr2)
   10858                 :             : {
   10859                 :          18 :   tree expr1_vptr = NULL_TREE;
   10860                 :          18 :   tree tmp;
   10861                 :             : 
   10862                 :          18 :   gfc_conv_function_expr (rse, expr2);
   10863                 :          18 :   rse->expr = gfc_evaluate_now (rse->expr, &rse->pre);
   10864                 :             : 
   10865                 :          18 :   if (expr1->ts.type != BT_CLASS)
   10866                 :          12 :       rse->expr = gfc_class_data_get (rse->expr);
   10867                 :             :   else
   10868                 :             :     {
   10869                 :           6 :       expr1_vptr = trans_class_vptr_len_assignment (block, expr1,
   10870                 :             :                                                     expr2, rse,
   10871                 :             :                                                     NULL, NULL, NULL);
   10872                 :           6 :       gfc_add_block_to_block (block, &rse->pre);
   10873                 :           6 :       tmp = gfc_create_var (TREE_TYPE (rse->expr), "ptrtemp");
   10874                 :           6 :       gfc_add_modify (&lse->pre, tmp, rse->expr);
   10875                 :             : 
   10876                 :          12 :       gfc_add_modify (&lse->pre, expr1_vptr,
   10877                 :           6 :                       fold_convert (TREE_TYPE (expr1_vptr),
   10878                 :             :                       gfc_class_vptr_get (tmp)));
   10879                 :           6 :       rse->expr = gfc_class_data_get (tmp);
   10880                 :             :     }
   10881                 :             : 
   10882                 :          18 :   return expr1_vptr;
   10883                 :             : }
   10884                 :             : 
   10885                 :             : 
   10886                 :             : tree
   10887                 :        9754 : gfc_trans_pointer_assign (gfc_code * code)
   10888                 :             : {
   10889                 :        9754 :   return gfc_trans_pointer_assignment (code->expr1, code->expr2);
   10890                 :             : }
   10891                 :             : 
   10892                 :             : 
   10893                 :             : /* Generate code for a pointer assignment.  */
   10894                 :             : 
   10895                 :             : tree
   10896                 :        9809 : gfc_trans_pointer_assignment (gfc_expr * expr1, gfc_expr * expr2)
   10897                 :             : {
   10898                 :        9809 :   gfc_se lse;
   10899                 :        9809 :   gfc_se rse;
   10900                 :        9809 :   stmtblock_t block;
   10901                 :        9809 :   tree desc;
   10902                 :        9809 :   tree tmp;
   10903                 :        9809 :   tree expr1_vptr = NULL_TREE;
   10904                 :        9809 :   bool scalar, non_proc_ptr_assign;
   10905                 :        9809 :   gfc_ss *ss;
   10906                 :             : 
   10907                 :        9809 :   gfc_start_block (&block);
   10908                 :             : 
   10909                 :        9809 :   gfc_init_se (&lse, NULL);
   10910                 :             : 
   10911                 :             :   /* Usually testing whether this is not a proc pointer assignment.  */
   10912                 :        9809 :   non_proc_ptr_assign = !(gfc_expr_attr (expr1).proc_pointer
   10913                 :        1157 :                         && expr2->expr_type == EXPR_VARIABLE
   10914                 :         928 :                         && expr2->symtree->n.sym->attr.flavor == FL_PROCEDURE);
   10915                 :             : 
   10916                 :             :   /* Check whether the expression is a scalar or not; we cannot use
   10917                 :             :      expr1->rank as it can be nonzero for proc pointers.  */
   10918                 :        9809 :   ss = gfc_walk_expr (expr1);
   10919                 :        9809 :   scalar = ss == gfc_ss_terminator;
   10920                 :        9809 :   if (!scalar)
   10921                 :        4209 :     gfc_free_ss_chain (ss);
   10922                 :             : 
   10923                 :        9809 :   if (expr1->ts.type == BT_DERIVED && expr2->ts.type == BT_CLASS
   10924                 :          84 :       && expr2->expr_type != EXPR_FUNCTION && non_proc_ptr_assign)
   10925                 :             :     {
   10926                 :          66 :       gfc_add_data_component (expr2);
   10927                 :             :       /* The following is required as gfc_add_data_component doesn't
   10928                 :             :          update ts.type if there is a trailing REF_ARRAY.  */
   10929                 :          66 :       expr2->ts.type = BT_DERIVED;
   10930                 :             :     }
   10931                 :             : 
   10932                 :        9809 :   if (scalar)
   10933                 :             :     {
   10934                 :             :       /* Scalar pointers.  */
   10935                 :        5600 :       lse.want_pointer = 1;
   10936                 :        5600 :       gfc_conv_expr (&lse, expr1);
   10937                 :        5600 :       gfc_init_se (&rse, NULL);
   10938                 :        5600 :       rse.want_pointer = 1;
   10939                 :        5600 :       if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
   10940                 :           6 :         trans_class_pointer_fcn (&block, &lse, &rse, expr1, expr2);
   10941                 :             :       else
   10942                 :        5594 :         gfc_conv_expr (&rse, expr2);
   10943                 :             : 
   10944                 :        5600 :       if (non_proc_ptr_assign && expr1->ts.type == BT_CLASS)
   10945                 :             :         {
   10946                 :         750 :           trans_class_vptr_len_assignment (&block, expr1, expr2, &rse, NULL,
   10947                 :             :                                            NULL, NULL);
   10948                 :         750 :           lse.expr = gfc_class_data_get (lse.expr);
   10949                 :             :         }
   10950                 :             : 
   10951                 :        5600 :       if (expr1->symtree->n.sym->attr.proc_pointer
   10952                 :        5600 :           && expr1->symtree->n.sym->attr.dummy)
   10953                 :          49 :         lse.expr = build_fold_indirect_ref_loc (input_location,
   10954                 :             :                                                 lse.expr);
   10955                 :             : 
   10956                 :        5600 :       if (expr2->symtree && expr2->symtree->n.sym->attr.proc_pointer
   10957                 :        4422 :           && expr2->symtree->n.sym->attr.dummy)
   10958                 :          20 :         rse.expr = build_fold_indirect_ref_loc (input_location,
   10959                 :             :                                                 rse.expr);
   10960                 :             : 
   10961                 :        5600 :       gfc_add_block_to_block (&block, &lse.pre);
   10962                 :        5600 :       gfc_add_block_to_block (&block, &rse.pre);
   10963                 :             : 
   10964                 :             :       /* Check character lengths if character expression.  The test is only
   10965                 :             :          really added if -fbounds-check is enabled.  Exclude deferred
   10966                 :             :          character length lefthand sides.  */
   10967                 :         921 :       if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL
   10968                 :         747 :           && !expr1->ts.deferred
   10969                 :         333 :           && !expr1->symtree->n.sym->attr.proc_pointer
   10970                 :        5926 :           && !gfc_is_proc_ptr_comp (expr1))
   10971                 :             :         {
   10972                 :         307 :           gcc_assert (expr2->ts.type == BT_CHARACTER);
   10973                 :         307 :           gcc_assert (lse.string_length && rse.string_length);
   10974                 :         307 :           gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
   10975                 :             :                                        lse.string_length, rse.string_length,
   10976                 :             :                                        &block);
   10977                 :             :         }
   10978                 :             : 
   10979                 :             :       /* The assignment to an deferred character length sets the string
   10980                 :             :          length to that of the rhs.  */
   10981                 :        5600 :       if (expr1->ts.deferred)
   10982                 :             :         {
   10983                 :         529 :           if (expr2->expr_type != EXPR_NULL && lse.string_length != NULL)
   10984                 :         412 :             gfc_add_modify (&block, lse.string_length,
   10985                 :         412 :                             fold_convert (TREE_TYPE (lse.string_length),
   10986                 :             :                                           rse.string_length));
   10987                 :         117 :           else if (lse.string_length != NULL)
   10988                 :         115 :             gfc_add_modify (&block, lse.string_length,
   10989                 :         115 :                             build_zero_cst (TREE_TYPE (lse.string_length)));
   10990                 :             :         }
   10991                 :             : 
   10992                 :        5600 :       gfc_add_modify (&block, lse.expr,
   10993                 :        5600 :                       fold_convert (TREE_TYPE (lse.expr), rse.expr));
   10994                 :             : 
   10995                 :        5600 :       if (flag_coarray == GFC_FCOARRAY_LIB)
   10996                 :             :         {
   10997                 :         237 :           if (expr1->ref)
   10998                 :             :             /* Also set the tokens for pointer components in derived typed
   10999                 :             :                coarrays.  */
   11000                 :           6 :             trans_caf_token_assign (&lse, &rse, expr1, expr2);
   11001                 :         231 :           else if (gfc_caf_attr (expr1).codimension)
   11002                 :             :             {
   11003                 :           0 :               tree lhs_caf_decl, rhs_caf_decl, lhs_tok, rhs_tok;
   11004                 :             : 
   11005                 :           0 :               lhs_caf_decl = gfc_get_tree_for_caf_expr (expr1);
   11006                 :           0 :               rhs_caf_decl = gfc_get_tree_for_caf_expr (expr2);
   11007                 :           0 :               gfc_get_caf_token_offset (&lse, &lhs_tok, nullptr, lhs_caf_decl,
   11008                 :             :                                         NULL_TREE, expr1);
   11009                 :           0 :               gfc_get_caf_token_offset (&rse, &rhs_tok, nullptr, rhs_caf_decl,
   11010                 :             :                                         NULL_TREE, expr2);
   11011                 :           0 :               gfc_add_modify (&block, lhs_tok, rhs_tok);
   11012                 :             :             }
   11013                 :             :         }
   11014                 :             : 
   11015                 :        5600 :       gfc_add_block_to_block (&block, &rse.post);
   11016                 :        5600 :       gfc_add_block_to_block (&block, &lse.post);
   11017                 :             :     }
   11018                 :             :   else
   11019                 :             :     {
   11020                 :        4209 :       gfc_ref* remap;
   11021                 :        4209 :       bool rank_remap;
   11022                 :        4209 :       tree strlen_lhs;
   11023                 :        4209 :       tree strlen_rhs = NULL_TREE;
   11024                 :             : 
   11025                 :             :       /* Array pointer.  Find the last reference on the LHS and if it is an
   11026                 :             :          array section ref, we're dealing with bounds remapping.  In this case,
   11027                 :             :          set it to AR_FULL so that gfc_conv_expr_descriptor does
   11028                 :             :          not see it and process the bounds remapping afterwards explicitly.  */
   11029                 :       13572 :       for (remap = expr1->ref; remap; remap = remap->next)
   11030                 :        5485 :         if (!remap->next && remap->type == REF_ARRAY
   11031                 :        4209 :             && remap->u.ar.type == AR_SECTION)
   11032                 :             :           break;
   11033                 :        4209 :       rank_remap = (remap && remap->u.ar.end[0]);
   11034                 :             : 
   11035                 :         331 :       if (remap && expr2->expr_type == EXPR_NULL)
   11036                 :             :         {
   11037                 :           2 :           gfc_error ("If bounds remapping is specified at %L, "
   11038                 :             :                      "the pointer target shall not be NULL", &expr1->where);
   11039                 :           2 :           return NULL_TREE;
   11040                 :             :         }
   11041                 :             : 
   11042                 :        4207 :       gfc_init_se (&lse, NULL);
   11043                 :        4207 :       if (remap)
   11044                 :         329 :         lse.descriptor_only = 1;
   11045                 :        4207 :       gfc_conv_expr_descriptor (&lse, expr1);
   11046                 :        4207 :       strlen_lhs = lse.string_length;
   11047                 :        4207 :       desc = lse.expr;
   11048                 :             : 
   11049                 :        4207 :       if (expr2->expr_type == EXPR_NULL)
   11050                 :             :         {
   11051                 :             :           /* Just set the data pointer to null.  */
   11052                 :         679 :           gfc_conv_descriptor_data_set (&lse.pre, lse.expr, null_pointer_node);
   11053                 :             :         }
   11054                 :        3528 :       else if (rank_remap)
   11055                 :             :         {
   11056                 :             :           /* If we are rank-remapping, just get the RHS's descriptor and
   11057                 :             :              process this later on.  */
   11058                 :         206 :           gfc_init_se (&rse, NULL);
   11059                 :         206 :           rse.direct_byref = 1;
   11060                 :         206 :           rse.byref_noassign = 1;
   11061                 :             : 
   11062                 :         206 :           if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
   11063                 :          12 :             expr1_vptr = trans_class_pointer_fcn (&block, &lse, &rse,
   11064                 :             :                                                   expr1, expr2);
   11065                 :         194 :           else if (expr2->expr_type == EXPR_FUNCTION)
   11066                 :             :             {
   11067                 :             :               tree bound[GFC_MAX_DIMENSIONS];
   11068                 :             :               int i;
   11069                 :             : 
   11070                 :          26 :               for (i = 0; i < expr2->rank; i++)
   11071                 :          13 :                 bound[i] = NULL_TREE;
   11072                 :          13 :               tmp = gfc_typenode_for_spec (&expr2->ts);
   11073                 :          13 :               tmp = gfc_get_array_type_bounds (tmp, expr2->rank, 0,
   11074                 :             :                                                bound, bound, 0,
   11075                 :             :                                                GFC_ARRAY_POINTER_CONT, false);
   11076                 :          13 :               tmp = gfc_create_var (tmp, "ptrtemp");
   11077                 :          13 :               rse.descriptor_only = 0;
   11078                 :          13 :               rse.expr = tmp;
   11079                 :          13 :               rse.direct_byref = 1;
   11080                 :          13 :               gfc_conv_expr_descriptor (&rse, expr2);
   11081                 :          13 :               strlen_rhs = rse.string_length;
   11082                 :          13 :               rse.expr = tmp;
   11083                 :             :             }
   11084                 :             :           else
   11085                 :             :             {
   11086                 :         181 :               gfc_conv_expr_descriptor (&rse, expr2);
   11087                 :         181 :               strlen_rhs = rse.string_length;
   11088                 :         181 :               if (expr1->ts.type == BT_CLASS)
   11089                 :          36 :                 expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
   11090                 :             :                                                               expr2, &rse,
   11091                 :             :                                                               NULL, NULL,
   11092                 :             :                                                               NULL);
   11093                 :             :             }
   11094                 :             :         }
   11095                 :        3322 :       else if (expr2->expr_type == EXPR_VARIABLE)
   11096                 :             :         {
   11097                 :             :           /* Assign directly to the LHS's descriptor.  */
   11098                 :        3196 :           lse.descriptor_only = 0;
   11099                 :        3196 :           lse.direct_byref = 1;
   11100                 :        3196 :           gfc_conv_expr_descriptor (&lse, expr2);
   11101                 :        3196 :           strlen_rhs = lse.string_length;
   11102                 :        3196 :           gfc_init_se (&rse, NULL);
   11103                 :             : 
   11104                 :        3196 :           if (expr1->ts.type == BT_CLASS)
   11105                 :             :             {
   11106                 :         326 :               rse.expr = NULL_TREE;
   11107                 :         326 :               rse.string_length = strlen_rhs;
   11108                 :         326 :               trans_class_vptr_len_assignment (&block, expr1, expr2, &rse,
   11109                 :             :                                                NULL, NULL, NULL);
   11110                 :             :             }
   11111                 :             : 
   11112                 :        3196 :           if (remap == NULL)
   11113                 :             :             {
   11114                 :             :               /* If the target is not a whole array, use the target array
   11115                 :             :                  reference for remap.  */
   11116                 :        6601 :               for (remap = expr2->ref; remap; remap = remap->next)
   11117                 :        3629 :                 if (remap->type == REF_ARRAY
   11118                 :        3133 :                     && remap->u.ar.type == AR_FULL
   11119                 :        2465 :                     && remap->next)
   11120                 :             :                   break;
   11121                 :             :             }
   11122                 :             :         }
   11123                 :         126 :       else if (expr2->expr_type == EXPR_FUNCTION && expr2->ts.type == BT_CLASS)
   11124                 :             :         {
   11125                 :          19 :           gfc_init_se (&rse, NULL);
   11126                 :          19 :           rse.want_pointer = 1;
   11127                 :          19 :           gfc_conv_function_expr (&rse, expr2);
   11128                 :          19 :           if (expr1->ts.type != BT_CLASS)
   11129                 :             :             {
   11130                 :           6 :               rse.expr = gfc_class_data_get (rse.expr);
   11131                 :           6 :               gfc_add_modify (&lse.pre, desc, rse.expr);
   11132                 :             :               /* Set the lhs span.  */
   11133                 :           6 :               tmp = TREE_TYPE (rse.expr);
   11134                 :           6 :               tmp = TYPE_SIZE_UNIT (gfc_get_element_type (tmp));
   11135                 :           6 :               tmp = fold_convert (gfc_array_index_type, tmp);
   11136                 :           6 :               gfc_conv_descriptor_span_set (&lse.pre, desc, tmp);
   11137                 :             :             }
   11138                 :             :           else
   11139                 :             :             {
   11140                 :          13 :               expr1_vptr = trans_class_vptr_len_assignment (&block, expr1,
   11141                 :             :                                                             expr2, &rse, NULL,
   11142                 :             :                                                             NULL, NULL);
   11143                 :          13 :               gfc_add_block_to_block (&block, &rse.pre);
   11144                 :          13 :               tmp = gfc_create_var (TREE_TYPE (rse.expr), "ptrtemp");
   11145                 :          13 :               gfc_add_modify (&lse.pre, tmp, rse.expr);
   11146                 :             : 
   11147                 :          26 :               gfc_add_modify (&lse.pre, expr1_vptr,
   11148                 :          13 :                               fold_convert (TREE_TYPE (expr1_vptr),
   11149                 :             :                                         gfc_class_vptr_get (tmp)));
   11150                 :          13 :               rse.expr = gfc_class_data_get (tmp);
   11151                 :          13 :               gfc_add_modify (&lse.pre, desc, rse.expr);
   11152                 :             :             }
   11153                 :             :         }
   11154                 :             :       else
   11155                 :             :         {
   11156                 :             :           /* Assign to a temporary descriptor and then copy that
   11157                 :             :              temporary to the pointer.  */
   11158                 :         107 :           tmp = gfc_create_var (TREE_TYPE (desc), "ptrtemp");
   11159                 :         107 :           lse.descriptor_only = 0;
   11160                 :         107 :           lse.expr = tmp;
   11161                 :         107 :           lse.direct_byref = 1;
   11162                 :         107 :           gfc_conv_expr_descriptor (&lse, expr2);
   11163                 :         107 :           strlen_rhs = lse.string_length;
   11164                 :         107 :           gfc_add_modify (&lse.pre, desc, tmp);
   11165                 :             :         }
   11166                 :             : 
   11167                 :        4207 :       if (expr1->ts.type == BT_CHARACTER
   11168                 :         572 :           && expr1->ts.deferred)
   11169                 :             :         {
   11170                 :         326 :           gfc_symbol *psym = expr1->symtree->n.sym;
   11171                 :         326 :           tmp = NULL_TREE;
   11172                 :         326 :           if (psym->ts.type == BT_CHARACTER
   11173                 :         325 :               && psym->ts.u.cl->backend_decl)
   11174                 :         325 :             tmp = psym->ts.u.cl->backend_decl;
   11175                 :           1 :           else if (expr1->ts.u.cl->backend_decl
   11176                 :           1 :                    && VAR_P (expr1->ts.u.cl->backend_decl))
   11177                 :           0 :             tmp = expr1->ts.u.cl->backend_decl;
   11178                 :           1 :           else if (TREE_CODE (lse.expr) == COMPONENT_REF)
   11179                 :             :             {
   11180                 :           1 :               gfc_ref *ref = expr1->ref;
   11181                 :           3 :               for (;ref; ref = ref->next)
   11182                 :             :                 {
   11183                 :           2 :                   if (ref->type == REF_COMPONENT
   11184                 :           1 :                       && ref->u.c.component->ts.type == BT_CHARACTER
   11185                 :           3 :                       && gfc_deferred_strlen (ref->u.c.component, &tmp))
   11186                 :           1 :                     tmp = fold_build3_loc (input_location, COMPONENT_REF,
   11187                 :           1 :                                            TREE_TYPE (tmp),
   11188                 :           1 :                                            TREE_OPERAND (lse.expr, 0),
   11189                 :             :                                            tmp, NULL_TREE);
   11190                 :             :                 }
   11191                 :             :             }
   11192                 :             : 
   11193                 :         326 :           gcc_assert (tmp);
   11194                 :             : 
   11195                 :         326 :           if (expr2->expr_type != EXPR_NULL)
   11196                 :         314 :             gfc_add_modify (&block, tmp,
   11197                 :         314 :                             fold_convert (TREE_TYPE (tmp), strlen_rhs));
   11198                 :             :           else
   11199                 :          12 :             gfc_add_modify (&block, tmp, build_zero_cst (TREE_TYPE (tmp)));
   11200                 :             :         }
   11201                 :             : 
   11202                 :        4207 :       gfc_add_block_to_block (&block, &lse.pre);
   11203                 :        4207 :       if (rank_remap)
   11204                 :         206 :         gfc_add_block_to_block (&block, &rse.pre);
   11205                 :             : 
   11206                 :             :       /* If we do bounds remapping, update LHS descriptor accordingly.  */
   11207                 :        4207 :       if (remap)
   11208                 :             :         {
   11209                 :         430 :           int dim;
   11210                 :         430 :           gcc_assert (remap->u.ar.dimen == expr1->rank);
   11211                 :             : 
   11212                 :         430 :           if (rank_remap)
   11213                 :             :             {
   11214                 :             :               /* Do rank remapping.  We already have the RHS's descriptor
   11215                 :             :                  converted in rse and now have to build the correct LHS
   11216                 :             :                  descriptor for it.  */
   11217                 :             : 
   11218                 :         206 :               tree dtype, data, span;
   11219                 :         206 :               tree offs, stride;
   11220                 :         206 :               tree lbound, ubound;
   11221                 :             : 
   11222                 :             :               /* Set dtype.  */
   11223                 :         206 :               dtype = gfc_conv_descriptor_dtype (desc);
   11224                 :         206 :               tmp = gfc_get_dtype (TREE_TYPE (desc));
   11225                 :         206 :               gfc_add_modify (&block, dtype, tmp);
   11226                 :             : 
   11227                 :             :               /* Copy data pointer.  */
   11228                 :         206 :               data = gfc_conv_descriptor_data_get (rse.expr);
   11229                 :         206 :               gfc_conv_descriptor_data_set (&block, desc, data);
   11230                 :             : 
   11231                 :             :               /* Copy the span.  */
   11232                 :         206 :               if (VAR_P (rse.expr)
   11233                 :         206 :                   && GFC_DECL_PTR_ARRAY_P (rse.expr))
   11234                 :          12 :                 span = gfc_conv_descriptor_span_get (rse.expr);
   11235                 :             :               else
   11236                 :             :                 {
   11237                 :         194 :                   tmp = TREE_TYPE (rse.expr);
   11238                 :         194 :                   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (tmp));
   11239                 :         194 :                   span = fold_convert (gfc_array_index_type, tmp);
   11240                 :             :                 }
   11241                 :         206 :               gfc_conv_descriptor_span_set (&block, desc, span);
   11242                 :             : 
   11243                 :             :               /* Copy offset but adjust it such that it would correspond
   11244                 :             :                  to a lbound of zero.  */
   11245                 :         206 :               if (expr2->rank == -1)
   11246                 :          42 :                 gfc_conv_descriptor_offset_set (&block, desc,
   11247                 :             :                                                 gfc_index_zero_node);
   11248                 :             :               else
   11249                 :             :                 {
   11250                 :         164 :                   offs = gfc_conv_descriptor_offset_get (rse.expr);
   11251                 :         510 :                   for (dim = 0; dim < expr2->rank; ++dim)
   11252                 :             :                     {
   11253                 :         182 :                       stride = gfc_conv_descriptor_stride_get (rse.expr,
   11254                 :             :                                                         gfc_rank_cst[dim]);
   11255                 :         182 :                       lbound = gfc_conv_descriptor_lbound_get (rse.expr,
   11256                 :             :                                                         gfc_rank_cst[dim]);
   11257                 :         182 :                       tmp = fold_build2_loc (input_location, MULT_EXPR,
   11258                 :             :                                              gfc_array_index_type, stride,
   11259                 :             :                                              lbound);
   11260                 :         182 :                       offs = fold_build2_loc (input_location, PLUS_EXPR,
   11261                 :             :                                               gfc_array_index_type, offs, tmp);
   11262                 :             :                     }
   11263                 :         164 :                   gfc_conv_descriptor_offset_set (&block, desc, offs);
   11264                 :             :                 }
   11265                 :             :               /* Set the bounds as declared for the LHS and calculate strides as
   11266                 :             :                  well as another offset update accordingly.  */
   11267                 :         206 :               stride = gfc_conv_descriptor_stride_get (rse.expr,
   11268                 :             :                                                        gfc_rank_cst[0]);
   11269                 :         545 :               for (dim = 0; dim < expr1->rank; ++dim)
   11270                 :             :                 {
   11271                 :         339 :                   gfc_se lower_se;
   11272                 :         339 :                   gfc_se upper_se;
   11273                 :             : 
   11274                 :         339 :                   gcc_assert (remap->u.ar.start[dim] && remap->u.ar.end[dim]);
   11275                 :             : 
   11276                 :         339 :                   if (remap->u.ar.start[dim]->expr_type != EXPR_CONSTANT
   11277                 :             :                       || remap->u.ar.start[dim]->expr_type != EXPR_VARIABLE)
   11278                 :         339 :                     gfc_resolve_expr (remap->u.ar.start[dim]);
   11279                 :         339 :                   if (remap->u.ar.end[dim]->expr_type != EXPR_CONSTANT
   11280                 :             :                       || remap->u.ar.end[dim]->expr_type != EXPR_VARIABLE)
   11281                 :         339 :                     gfc_resolve_expr (remap->u.ar.end[dim]);
   11282                 :             : 
   11283                 :             :                   /* Convert declared bounds.  */
   11284                 :         339 :                   gfc_init_se (&lower_se, NULL);
   11285                 :         339 :                   gfc_init_se (&upper_se, NULL);
   11286                 :         339 :                   gfc_conv_expr (&lower_se, remap->u.ar.start[dim]);
   11287                 :         339 :                   gfc_conv_expr (&upper_se, remap->u.ar.end[dim]);
   11288                 :             : 
   11289                 :         339 :                   gfc_add_block_to_block (&block, &lower_se.pre);
   11290                 :         339 :                   gfc_add_block_to_block (&block, &upper_se.pre);
   11291                 :             : 
   11292                 :         339 :                   lbound = fold_convert (gfc_array_index_type, lower_se.expr);
   11293                 :         339 :                   ubound = fold_convert (gfc_array_index_type, upper_se.expr);
   11294                 :             : 
   11295                 :         339 :                   lbound = gfc_evaluate_now (lbound, &block);
   11296                 :         339 :                   ubound = gfc_evaluate_now (ubound, &block);
   11297                 :             : 
   11298                 :         339 :                   gfc_add_block_to_block (&block, &lower_se.post);
   11299                 :         339 :                   gfc_add_block_to_block (&block, &upper_se.post);
   11300                 :             : 
   11301                 :             :                   /* Set bounds in descriptor.  */
   11302                 :         339 :                   gfc_conv_descriptor_lbound_set (&block, desc,
   11303                 :             :                                                   gfc_rank_cst[dim], lbound);
   11304                 :         339 :                   gfc_conv_descriptor_ubound_set (&block, desc,
   11305                 :             :                                                   gfc_rank_cst[dim], ubound);
   11306                 :             : 
   11307                 :             :                   /* Set stride.  */
   11308                 :         339 :                   stride = gfc_evaluate_now (stride, &block);
   11309                 :         339 :                   gfc_conv_descriptor_stride_set (&block, desc,
   11310                 :             :                                                   gfc_rank_cst[dim], stride);
   11311                 :             : 
   11312                 :             :                   /* Update offset.  */
   11313                 :         339 :                   offs = gfc_conv_descriptor_offset_get (desc);
   11314                 :         339 :                   tmp = fold_build2_loc (input_location, MULT_EXPR,
   11315                 :             :                                          gfc_array_index_type, lbound, stride);
   11316                 :         339 :                   offs = fold_build2_loc (input_location, MINUS_EXPR,
   11317                 :             :                                           gfc_array_index_type, offs, tmp);
   11318                 :         339 :                   offs = gfc_evaluate_now (offs, &block);
   11319                 :         339 :                   gfc_conv_descriptor_offset_set (&block, desc, offs);
   11320                 :             : 
   11321                 :             :                   /* Update stride.  */
   11322                 :         339 :                   tmp = gfc_conv_array_extent_dim (lbound, ubound, NULL);
   11323                 :         339 :                   stride = fold_build2_loc (input_location, MULT_EXPR,
   11324                 :             :                                             gfc_array_index_type, stride, tmp);
   11325                 :             :                 }
   11326                 :             :             }
   11327                 :             :           else
   11328                 :             :             {
   11329                 :             :               /* Bounds remapping.  Just shift the lower bounds.  */
   11330                 :             : 
   11331                 :         224 :               gcc_assert (expr1->rank == expr2->rank);
   11332                 :             : 
   11333                 :         556 :               for (dim = 0; dim < remap->u.ar.dimen; ++dim)
   11334                 :             :                 {
   11335                 :         332 :                   gfc_se lbound_se;
   11336                 :             : 
   11337                 :         332 :                   gcc_assert (!remap->u.ar.end[dim]);
   11338                 :         332 :                   gfc_init_se (&lbound_se, NULL);
   11339                 :         332 :                   if (remap->u.ar.start[dim])
   11340                 :             :                     {
   11341                 :         225 :                       gfc_conv_expr (&lbound_se, remap->u.ar.start[dim]);
   11342                 :         225 :                       gfc_add_block_to_block (&block, &lbound_se.pre);
   11343                 :             :                     }
   11344                 :             :                   else
   11345                 :             :                     /* This remap arises from a target that is not a whole
   11346                 :             :                        array. The start expressions will be NULL but we need
   11347                 :             :                        the lbounds to be one.  */
   11348                 :         107 :                     lbound_se.expr = gfc_index_one_node;
   11349                 :         332 :                   gfc_conv_shift_descriptor_lbound (&block, desc,
   11350                 :             :                                                     dim, lbound_se.expr);
   11351                 :         332 :                   gfc_add_block_to_block (&block, &lbound_se.post);
   11352                 :             :                 }
   11353                 :             :             }
   11354                 :             :         }
   11355                 :             : 
   11356                 :             :       /* If rank remapping was done, check with -fcheck=bounds that
   11357                 :             :          the target is at least as large as the pointer.  */
   11358                 :        4207 :       if (rank_remap && (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
   11359                 :          72 :           && expr2->rank != -1)
   11360                 :             :         {
   11361                 :          54 :           tree lsize, rsize;
   11362                 :          54 :           tree fault;
   11363                 :          54 :           const char* msg;
   11364                 :             : 
   11365                 :          54 :           lsize = gfc_conv_descriptor_size (lse.expr, expr1->rank);
   11366                 :          54 :           rsize = gfc_conv_descriptor_size (rse.expr, expr2->rank);
   11367                 :             : 
   11368                 :          54 :           lsize = gfc_evaluate_now (lsize, &block);
   11369                 :          54 :           rsize = gfc_evaluate_now (rsize, &block);
   11370                 :          54 :           fault = fold_build2_loc (input_location, LT_EXPR, logical_type_node,
   11371                 :             :                                    rsize, lsize);
   11372                 :             : 
   11373                 :          54 :           msg = _("Target of rank remapping is too small (%ld < %ld)");
   11374                 :          54 :           gfc_trans_runtime_check (true, false, fault, &block, &expr2->where,
   11375                 :             :                                    msg, rsize, lsize);
   11376                 :             :         }
   11377                 :             : 
   11378                 :             :       /* Check string lengths if applicable.  The check is only really added
   11379                 :             :          to the output code if -fbounds-check is enabled.  */
   11380                 :        4207 :       if (expr1->ts.type == BT_CHARACTER && expr2->expr_type != EXPR_NULL)
   11381                 :             :         {
   11382                 :         506 :           gcc_assert (expr2->ts.type == BT_CHARACTER);
   11383                 :         506 :           gcc_assert (strlen_lhs && strlen_rhs);
   11384                 :         506 :           gfc_trans_same_strlen_check ("pointer assignment", &expr1->where,
   11385                 :             :                                        strlen_lhs, strlen_rhs, &block);
   11386                 :             :         }
   11387                 :             : 
   11388                 :        4207 :       gfc_add_block_to_block (&block, &lse.post);
   11389                 :        4207 :       if (rank_remap)
   11390                 :         206 :         gfc_add_block_to_block (&block, &rse.post);
   11391                 :             :     }
   11392                 :             : 
   11393                 :        9807 :   return gfc_finish_block (&block);
   11394                 :             : }
   11395                 :             : 
   11396                 :             : 
   11397                 :             : /* Makes sure se is suitable for passing as a function string parameter.  */
   11398                 :             : /* TODO: Need to check all callers of this function.  It may be abused.  */
   11399                 :             : 
   11400                 :             : void
   11401                 :      230805 : gfc_conv_string_parameter (gfc_se * se)
   11402                 :             : {
   11403                 :      230805 :   tree type;
   11404                 :             : 
   11405                 :      230805 :   if (TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE
   11406                 :      230805 :       && integer_onep (se->string_length))
   11407                 :             :     {
   11408                 :         667 :       se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
   11409                 :         667 :       return;
   11410                 :             :     }
   11411                 :             : 
   11412                 :      230138 :   if (TREE_CODE (se->expr) == STRING_CST)
   11413                 :             :     {
   11414                 :       96944 :       type = TREE_TYPE (TREE_TYPE (se->expr));
   11415                 :       96944 :       se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
   11416                 :       96944 :       return;
   11417                 :             :     }
   11418                 :             : 
   11419                 :      133194 :   if ((TREE_CODE (TREE_TYPE (se->expr)) == ARRAY_TYPE
   11420                 :       51715 :        || TREE_CODE (TREE_TYPE (se->expr)) == INTEGER_TYPE)
   11421                 :      133290 :       && TYPE_STRING_FLAG (TREE_TYPE (se->expr)))
   11422                 :             :     {
   11423                 :       81575 :       type = TREE_TYPE (se->expr);
   11424                 :       81575 :       if (TREE_CODE (se->expr) != INDIRECT_REF)
   11425                 :       76723 :         se->expr = gfc_build_addr_expr (build_pointer_type (type), se->expr);
   11426                 :             :       else
   11427                 :             :         {
   11428                 :        4852 :           if (TREE_CODE (type) == ARRAY_TYPE)
   11429                 :        4852 :             type = TREE_TYPE (type);
   11430                 :        4852 :           type = gfc_get_character_type_len_for_eltype (type,
   11431                 :             :                                                         se->string_length);
   11432                 :        4852 :           type = build_pointer_type (type);
   11433                 :        4852 :           se->expr = gfc_build_addr_expr (type, se->expr);
   11434                 :             :         }
   11435                 :             :     }
   11436                 :             : 
   11437                 :      133194 :   gcc_assert (POINTER_TYPE_P (TREE_TYPE (se->expr)));
   11438                 :             : }
   11439                 :             : 
   11440                 :             : 
   11441                 :             : /* Generate code for assignment of scalar variables.  Includes character
   11442                 :             :    strings and derived types with allocatable components.
   11443                 :             :    If you know that the LHS has no allocations, set dealloc to false.
   11444                 :             : 
   11445                 :             :    DEEP_COPY has no effect if the typespec TS is not a derived type with
   11446                 :             :    allocatable components.  Otherwise, if it is set, an explicit copy of each
   11447                 :             :    allocatable component is made.  This is necessary as a simple copy of the
   11448                 :             :    whole object would copy array descriptors as is, so that the lhs's
   11449                 :             :    allocatable components would point to the rhs's after the assignment.
   11450                 :             :    Typically, setting DEEP_COPY is necessary if the rhs is a variable, and not
   11451                 :             :    necessary if the rhs is a non-pointer function, as the allocatable components
   11452                 :             :    are not accessible by other means than the function's result after the
   11453                 :             :    function has returned.  It is even more subtle when temporaries are involved,
   11454                 :             :    as the two following examples show:
   11455                 :             :     1.  When we evaluate an array constructor, a temporary is created.  Thus
   11456                 :             :       there is theoretically no alias possible.  However, no deep copy is
   11457                 :             :       made for this temporary, so that if the constructor is made of one or
   11458                 :             :       more variable with allocatable components, those components still point
   11459                 :             :       to the variable's: DEEP_COPY should be set for the assignment from the
   11460                 :             :       temporary to the lhs in that case.
   11461                 :             :     2.  When assigning a scalar to an array, we evaluate the scalar value out
   11462                 :             :       of the loop, store it into a temporary variable, and assign from that.
   11463                 :             :       In that case, deep copying when assigning to the temporary would be a
   11464                 :             :       waste of resources; however deep copies should happen when assigning from
   11465                 :             :       the temporary to each array element: again DEEP_COPY should be set for
   11466                 :             :       the assignment from the temporary to the lhs.  */
   11467                 :             : 
   11468                 :             : tree
   11469                 :      321519 : gfc_trans_scalar_assign (gfc_se *lse, gfc_se *rse, gfc_typespec ts,
   11470                 :             :                          bool deep_copy, bool dealloc, bool in_coarray,
   11471                 :             :                          bool assoc_assign)
   11472                 :             : {
   11473                 :      321519 :   stmtblock_t block;
   11474                 :      321519 :   tree tmp;
   11475                 :      321519 :   tree cond;
   11476                 :             : 
   11477                 :      321519 :   gfc_init_block (&block);
   11478                 :             : 
   11479                 :      321519 :   if (ts.type == BT_CHARACTER)
   11480                 :             :     {
   11481                 :       31156 :       tree rlen = NULL;
   11482                 :       31156 :       tree llen = NULL;
   11483                 :             : 
   11484                 :       31156 :       if (lse->string_length != NULL_TREE)
   11485                 :             :         {
   11486                 :       31156 :           gfc_conv_string_parameter (lse);
   11487                 :       31156 :           gfc_add_block_to_block (&block, &lse->pre);
   11488                 :       31156 :           llen = lse->string_length;
   11489                 :             :         }
   11490                 :             : 
   11491                 :       31156 :       if (rse->string_length != NULL_TREE)
   11492                 :             :         {
   11493                 :       31156 :           gfc_conv_string_parameter (rse);
   11494                 :       31156 :           gfc_add_block_to_block (&block, &rse->pre);
   11495                 :       31156 :           rlen = rse->string_length;
   11496                 :             :         }
   11497                 :             : 
   11498                 :       31156 :       gfc_trans_string_copy (&block, llen, lse->expr, ts.kind, rlen,
   11499                 :             :                              rse->expr, ts.kind);
   11500                 :             :     }
   11501                 :      290363 :   else if (gfc_bt_struct (ts.type)
   11502                 :       16552 :            && (ts.u.derived->attr.alloc_comp
   11503                 :       11467 :                 || (deep_copy && ts.u.derived->attr.pdt_type)))
   11504                 :             :     {
   11505                 :        5156 :       tree tmp_var = NULL_TREE;
   11506                 :        5156 :       cond = NULL_TREE;
   11507                 :             : 
   11508                 :             :       /* Are the rhs and the lhs the same?  */
   11509                 :        5156 :       if (deep_copy)
   11510                 :             :         {
   11511                 :        3159 :           cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   11512                 :             :                                   gfc_build_addr_expr (NULL_TREE, lse->expr),
   11513                 :             :                                   gfc_build_addr_expr (NULL_TREE, rse->expr));
   11514                 :        3159 :           cond = gfc_evaluate_now (cond, &lse->pre);
   11515                 :             :         }
   11516                 :             : 
   11517                 :             :       /* Deallocate the lhs allocated components as long as it is not
   11518                 :             :          the same as the rhs.  This must be done following the assignment
   11519                 :             :          to prevent deallocating data that could be used in the rhs
   11520                 :             :          expression.  */
   11521                 :        5156 :       if (dealloc)
   11522                 :             :         {
   11523                 :        1533 :           tmp_var = gfc_evaluate_now (lse->expr, &lse->pre);
   11524                 :        1533 :           tmp = gfc_deallocate_alloc_comp_no_caf (ts.u.derived, tmp_var,
   11525                 :        1533 :                                                   0, gfc_may_be_finalized (ts));
   11526                 :        1533 :           if (deep_copy)
   11527                 :         631 :             tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
   11528                 :             :                             tmp);
   11529                 :        1533 :           gfc_add_expr_to_block (&lse->post, tmp);
   11530                 :             :         }
   11531                 :             : 
   11532                 :        5156 :       gfc_add_block_to_block (&block, &rse->pre);
   11533                 :        5156 :       gfc_add_block_to_block (&block, &lse->finalblock);
   11534                 :        5156 :       gfc_add_block_to_block (&block, &lse->pre);
   11535                 :             : 
   11536                 :        5156 :       gfc_add_modify (&block, lse->expr,
   11537                 :        5156 :                            fold_convert (TREE_TYPE (lse->expr), rse->expr));
   11538                 :             : 
   11539                 :             :       /* Restore pointer address of coarray components.  */
   11540                 :        5156 :       if (ts.u.derived->attr.coarray_comp && deep_copy && tmp_var != NULL_TREE)
   11541                 :             :         {
   11542                 :           4 :           tmp = gfc_reassign_alloc_comp_caf (ts.u.derived, tmp_var, lse->expr);
   11543                 :           4 :           tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
   11544                 :             :                           tmp);
   11545                 :           4 :           gfc_add_expr_to_block (&block, tmp);
   11546                 :             :         }
   11547                 :             : 
   11548                 :             :       /* Do a deep copy if the rhs is a variable, if it is not the
   11549                 :             :          same as the lhs.  */
   11550                 :        5156 :       if (deep_copy)
   11551                 :             :         {
   11552                 :        3159 :           int caf_mode = in_coarray ? (GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
   11553                 :             :                                        | GFC_STRUCTURE_CAF_MODE_IN_COARRAY) : 0;
   11554                 :        3159 :           tmp = gfc_copy_alloc_comp (ts.u.derived, rse->expr, lse->expr, 0,
   11555                 :             :                                      caf_mode);
   11556                 :        3159 :           tmp = build3_v (COND_EXPR, cond, build_empty_stmt (input_location),
   11557                 :             :                           tmp);
   11558                 :        3159 :           gfc_add_expr_to_block (&block, tmp);
   11559                 :             :         }
   11560                 :             :     }
   11561                 :      285207 :   else if (gfc_bt_struct (ts.type))
   11562                 :             :     {
   11563                 :       11396 :       gfc_add_block_to_block (&block, &rse->pre);
   11564                 :       11396 :       gfc_add_block_to_block (&block, &lse->finalblock);
   11565                 :       11396 :       gfc_add_block_to_block (&block, &lse->pre);
   11566                 :       11396 :       tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
   11567                 :       11396 :                              TREE_TYPE (lse->expr), rse->expr);
   11568                 :       11396 :       gfc_add_modify (&block, lse->expr, tmp);
   11569                 :             :     }
   11570                 :             :   /* If possible use the rhs vptr copy with trans_scalar_class_assign....  */
   11571                 :      273811 :   else if (ts.type == BT_CLASS)
   11572                 :             :     {
   11573                 :         740 :       gfc_add_block_to_block (&block, &lse->pre);
   11574                 :         740 :       gfc_add_block_to_block (&block, &rse->pre);
   11575                 :         740 :       gfc_add_block_to_block (&block, &lse->finalblock);
   11576                 :             : 
   11577                 :         740 :       if (!trans_scalar_class_assign (&block, lse, rse))
   11578                 :             :         {
   11579                 :             :           /* ...otherwise assignment suffices. Note the use of VIEW_CONVERT_EXPR
   11580                 :             :           for the lhs which ensures that class data rhs cast as a string assigns
   11581                 :             :           correctly.  */
   11582                 :         606 :           tmp = fold_build1_loc (input_location, VIEW_CONVERT_EXPR,
   11583                 :         606 :                                  TREE_TYPE (rse->expr), lse->expr);
   11584                 :         606 :           gfc_add_modify (&block, tmp, rse->expr);
   11585                 :             :         }
   11586                 :             :     }
   11587                 :      273071 :   else if (ts.type != BT_CLASS)
   11588                 :             :     {
   11589                 :      273071 :       gfc_add_block_to_block (&block, &lse->pre);
   11590                 :      273071 :       gfc_add_block_to_block (&block, &rse->pre);
   11591                 :             : 
   11592                 :      273071 :       if (in_coarray)
   11593                 :             :         {
   11594                 :         675 :           if (flag_coarray == GFC_FCOARRAY_LIB && assoc_assign)
   11595                 :             :             {
   11596                 :           0 :               gfc_add_modify (&block, gfc_conv_descriptor_token (lse->expr),
   11597                 :           0 :                               TYPE_LANG_SPECIFIC (
   11598                 :             :                                 TREE_TYPE (TREE_TYPE (rse->expr)))
   11599                 :             :                                 ->caf_token);
   11600                 :             :             }
   11601                 :         675 :           if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (lse->expr)))
   11602                 :           0 :             lse->expr = gfc_conv_array_data (lse->expr);
   11603                 :         266 :           if (flag_coarray == GFC_FCOARRAY_SINGLE && assoc_assign
   11604                 :         675 :               && !POINTER_TYPE_P (TREE_TYPE (rse->expr)))
   11605                 :           0 :             rse->expr = gfc_build_addr_expr (NULL_TREE, rse->expr);
   11606                 :             :         }
   11607                 :      273071 :       gfc_add_modify (&block, lse->expr,
   11608                 :      273071 :                       fold_convert (TREE_TYPE (lse->expr), rse->expr));
   11609                 :             :     }
   11610                 :             : 
   11611                 :      321519 :   gfc_add_block_to_block (&block, &lse->post);
   11612                 :      321519 :   gfc_add_block_to_block (&block, &rse->post);
   11613                 :             : 
   11614                 :      321519 :   return gfc_finish_block (&block);
   11615                 :             : }
   11616                 :             : 
   11617                 :             : 
   11618                 :             : /* There are quite a lot of restrictions on the optimisation in using an
   11619                 :             :    array function assign without a temporary.  */
   11620                 :             : 
   11621                 :             : static bool
   11622                 :       14426 : arrayfunc_assign_needs_temporary (gfc_expr * expr1, gfc_expr * expr2)
   11623                 :             : {
   11624                 :       14426 :   gfc_ref * ref;
   11625                 :       14426 :   bool seen_array_ref;
   11626                 :       14426 :   bool c = false;
   11627                 :       14426 :   gfc_symbol *sym = expr1->symtree->n.sym;
   11628                 :             : 
   11629                 :             :   /* Play it safe with class functions assigned to a derived type.  */
   11630                 :       14426 :   if (gfc_is_class_array_function (expr2)
   11631                 :       14426 :       && expr1->ts.type == BT_DERIVED)
   11632                 :             :     return true;
   11633                 :             : 
   11634                 :             :   /* The caller has already checked rank>0 and expr_type == EXPR_FUNCTION.  */
   11635                 :       14402 :   if (expr2->value.function.isym && !gfc_is_intrinsic_libcall (expr2))
   11636                 :             :     return true;
   11637                 :             : 
   11638                 :             :   /* Elemental functions are scalarized so that they don't need a
   11639                 :             :      temporary in gfc_trans_assignment_1, so return a true.  Otherwise,
   11640                 :             :      they would need special treatment in gfc_trans_arrayfunc_assign.  */
   11641                 :        8650 :   if (expr2->value.function.esym != NULL
   11642                 :        1502 :       && expr2->value.function.esym->attr.elemental)
   11643                 :             :     return true;
   11644                 :             : 
   11645                 :             :   /* Need a temporary if rhs is not FULL or a contiguous section.  */
   11646                 :        8310 :   if (expr1->ref && !(gfc_full_array_ref_p (expr1->ref, &c) || c))
   11647                 :             :     return true;
   11648                 :             : 
   11649                 :             :   /* Need a temporary if EXPR1 can't be expressed as a descriptor.  */
   11650                 :        8073 :   if (gfc_ref_needs_temporary_p (expr1->ref))
   11651                 :             :     return true;
   11652                 :             : 
   11653                 :             :   /* Functions returning pointers or allocatables need temporaries.  */
   11654                 :        8061 :   if (gfc_expr_attr (expr2).pointer
   11655                 :        8061 :       || gfc_expr_attr (expr2).allocatable)
   11656                 :         381 :     return true;
   11657                 :             : 
   11658                 :             :   /* Character array functions need temporaries unless the
   11659                 :             :      character lengths are the same.  */
   11660                 :        7680 :   if (expr2->ts.type == BT_CHARACTER && expr2->rank > 0)
   11661                 :             :     {
   11662                 :         562 :       if (UNLIMITED_POLY (expr1))
   11663                 :             :         return true;
   11664                 :             : 
   11665                 :         556 :       if (expr1->ts.u.cl->length == NULL
   11666                 :         507 :             || expr1->ts.u.cl->length->expr_type != EXPR_CONSTANT)
   11667                 :             :         return true;
   11668                 :             : 
   11669                 :         493 :       if (expr2->ts.u.cl->length == NULL
   11670                 :         487 :             || expr2->ts.u.cl->length->expr_type != EXPR_CONSTANT)
   11671                 :             :         return true;
   11672                 :             : 
   11673                 :         475 :       if (mpz_cmp (expr1->ts.u.cl->length->value.integer,
   11674                 :         475 :                      expr2->ts.u.cl->length->value.integer) != 0)
   11675                 :             :         return true;
   11676                 :             :     }
   11677                 :             : 
   11678                 :             :   /* Check that no LHS component references appear during an array
   11679                 :             :      reference. This is needed because we do not have the means to
   11680                 :             :      span any arbitrary stride with an array descriptor. This check
   11681                 :             :      is not needed for the rhs because the function result has to be
   11682                 :             :      a complete type.  */
   11683                 :        7587 :   seen_array_ref = false;
   11684                 :       15174 :   for (ref = expr1->ref; ref; ref = ref->next)
   11685                 :             :     {
   11686                 :        7600 :       if (ref->type == REF_ARRAY)
   11687                 :             :         seen_array_ref= true;
   11688                 :          13 :       else if (ref->type == REF_COMPONENT && seen_array_ref)
   11689                 :             :         return true;
   11690                 :             :     }
   11691                 :             : 
   11692                 :             :   /* Check for a dependency.  */
   11693                 :        7574 :   if (gfc_check_fncall_dependency (expr1, INTENT_OUT,
   11694                 :             :                                    expr2->value.function.esym,
   11695                 :             :                                    expr2->value.function.actual,
   11696                 :             :                                    NOT_ELEMENTAL))
   11697                 :             :     return true;
   11698                 :             : 
   11699                 :             :   /* If we have reached here with an intrinsic function, we do not
   11700                 :             :      need a temporary except in the particular case that reallocation
   11701                 :             :      on assignment is active and the lhs is allocatable and a target,
   11702                 :             :      or a pointer which may be a subref pointer.  FIXME: The last
   11703                 :             :      condition can go away when we use span in the intrinsics
   11704                 :             :      directly.*/
   11705                 :        7137 :   if (expr2->value.function.isym)
   11706                 :        6314 :     return (flag_realloc_lhs && sym->attr.allocatable && sym->attr.target)
   11707                 :       12715 :       || (sym->attr.pointer && sym->attr.subref_array_pointer);
   11708                 :             : 
   11709                 :             :   /* If the LHS is a dummy, we need a temporary if it is not
   11710                 :             :      INTENT(OUT).  */
   11711                 :         748 :   if (sym->attr.dummy && sym->attr.intent != INTENT_OUT)
   11712                 :             :     return true;
   11713                 :             : 
   11714                 :             :   /* If the lhs has been host_associated, is in common, a pointer or is
   11715                 :             :      a target and the function is not using a RESULT variable, aliasing
   11716                 :             :      can occur and a temporary is needed.  */
   11717                 :         742 :   if ((sym->attr.host_assoc
   11718                 :             :            || sym->attr.in_common
   11719                 :         742 :            || sym->attr.pointer
   11720                 :         676 :            || sym->attr.cray_pointee
   11721                 :         676 :            || sym->attr.target)
   11722                 :          66 :         && expr2->symtree != NULL
   11723                 :          66 :         && expr2->symtree->n.sym == expr2->symtree->n.sym->result)
   11724                 :             :     return true;
   11725                 :             : 
   11726                 :             :   /* A PURE function can unconditionally be called without a temporary.  */
   11727                 :         700 :   if (expr2->value.function.esym != NULL
   11728                 :         675 :       && expr2->value.function.esym->attr.pure)
   11729                 :             :     return false;
   11730                 :             : 
   11731                 :             :   /* Implicit_pure functions are those which could legally be declared
   11732                 :             :      to be PURE.  */
   11733                 :         672 :   if (expr2->value.function.esym != NULL
   11734                 :         647 :       && expr2->value.function.esym->attr.implicit_pure)
   11735                 :             :     return false;
   11736                 :             : 
   11737                 :         408 :   if (!sym->attr.use_assoc
   11738                 :             :         && !sym->attr.in_common
   11739                 :             :         && !sym->attr.pointer
   11740                 :         408 :         && !sym->attr.target
   11741                 :         402 :         && !sym->attr.cray_pointee
   11742                 :         402 :         && expr2->value.function.esym)
   11743                 :             :     {
   11744                 :             :       /* A temporary is not needed if the function is not contained and
   11745                 :             :          the variable is local or host associated and not a pointer or
   11746                 :             :          a target.  */
   11747                 :         377 :       if (!expr2->value.function.esym->attr.contained)
   11748                 :             :         return false;
   11749                 :             : 
   11750                 :             :       /* A temporary is not needed if the lhs has never been host
   11751                 :             :          associated and the procedure is contained.  */
   11752                 :         146 :       else if (!sym->attr.host_assoc)
   11753                 :             :         return false;
   11754                 :             : 
   11755                 :             :       /* A temporary is not needed if the variable is local and not
   11756                 :             :          a pointer, a target or a result.  */
   11757                 :           6 :       if (sym->ns->parent
   11758                 :           0 :             && expr2->value.function.esym->ns == sym->ns->parent)
   11759                 :             :         return false;
   11760                 :             :     }
   11761                 :             : 
   11762                 :             :   /* Default to temporary use.  */
   11763                 :             :   return true;
   11764                 :             : }
   11765                 :             : 
   11766                 :             : 
   11767                 :             : /* Provide the loop info so that the lhs descriptor can be built for
   11768                 :             :    reallocatable assignments from extrinsic function calls.  */
   11769                 :             : 
   11770                 :             : static void
   11771                 :         160 : realloc_lhs_loop_for_fcn_call (gfc_se *se, locus *where, gfc_ss **ss,
   11772                 :             :                                gfc_loopinfo *loop)
   11773                 :             : {
   11774                 :             :   /* Signal that the function call should not be made by
   11775                 :             :      gfc_conv_loop_setup.  */
   11776                 :         160 :   se->ss->is_alloc_lhs = 1;
   11777                 :         160 :   gfc_init_loopinfo (loop);
   11778                 :         160 :   gfc_add_ss_to_loop (loop, *ss);
   11779                 :         160 :   gfc_add_ss_to_loop (loop, se->ss);
   11780                 :         160 :   gfc_conv_ss_startstride (loop);
   11781                 :         160 :   gfc_conv_loop_setup (loop, where);
   11782                 :         160 :   gfc_copy_loopinfo_to_se (se, loop);
   11783                 :         160 :   gfc_add_block_to_block (&se->pre, &loop->pre);
   11784                 :         160 :   gfc_add_block_to_block (&se->pre, &loop->post);
   11785                 :         160 :   se->ss->is_alloc_lhs = 0;
   11786                 :         160 : }
   11787                 :             : 
   11788                 :             : 
   11789                 :             : /* For assignment to a reallocatable lhs from intrinsic functions,
   11790                 :             :    replace the se.expr (ie. the result) with a temporary descriptor.
   11791                 :             :    Null the data field so that the library allocates space for the
   11792                 :             :    result. Free the data of the original descriptor after the function,
   11793                 :             :    in case it appears in an argument expression and transfer the
   11794                 :             :    result to the original descriptor.  */
   11795                 :             : 
   11796                 :             : static void
   11797                 :        2109 : fcncall_realloc_result (gfc_se *se, int rank, tree dtype)
   11798                 :             : {
   11799                 :        2109 :   tree desc;
   11800                 :        2109 :   tree res_desc;
   11801                 :        2109 :   tree tmp;
   11802                 :        2109 :   tree offset;
   11803                 :        2109 :   tree zero_cond;
   11804                 :        2109 :   tree not_same_shape;
   11805                 :        2109 :   stmtblock_t shape_block;
   11806                 :        2109 :   int n;
   11807                 :             : 
   11808                 :             :   /* Use the allocation done by the library.  Substitute the lhs
   11809                 :             :      descriptor with a copy, whose data field is nulled.*/
   11810                 :        2109 :   desc = build_fold_indirect_ref_loc (input_location, se->expr);
   11811                 :        2109 :   if (POINTER_TYPE_P (TREE_TYPE (desc)))
   11812                 :           9 :     desc = build_fold_indirect_ref_loc (input_location, desc);
   11813                 :             : 
   11814                 :             :   /* Unallocated, the descriptor does not have a dtype.  */
   11815                 :        2109 :   tmp = gfc_conv_descriptor_dtype (desc);
   11816                 :        2109 :   if (dtype != NULL_TREE)
   11817                 :          13 :     gfc_add_modify (&se->pre, tmp, dtype);
   11818                 :             :   else
   11819                 :        2096 :     gfc_add_modify (&se->pre, tmp, gfc_get_dtype (TREE_TYPE (desc)));
   11820                 :             : 
   11821                 :        2109 :   res_desc = gfc_evaluate_now (desc, &se->pre);
   11822                 :        2109 :   gfc_conv_descriptor_data_set (&se->pre, res_desc, null_pointer_node);
   11823                 :        2109 :   se->expr = gfc_build_addr_expr (NULL_TREE, res_desc);
   11824                 :             : 
   11825                 :             :   /* Free the lhs after the function call and copy the result data to
   11826                 :             :      the lhs descriptor.  */
   11827                 :        2109 :   tmp = gfc_conv_descriptor_data_get (desc);
   11828                 :        2109 :   zero_cond = fold_build2_loc (input_location, EQ_EXPR,
   11829                 :             :                                logical_type_node, tmp,
   11830                 :        2109 :                                build_int_cst (TREE_TYPE (tmp), 0));
   11831                 :        2109 :   zero_cond = gfc_evaluate_now (zero_cond, &se->post);
   11832                 :        2109 :   tmp = gfc_call_free (tmp);
   11833                 :        2109 :   gfc_add_expr_to_block (&se->post, tmp);
   11834                 :             : 
   11835                 :        2109 :   tmp = gfc_conv_descriptor_data_get (res_desc);
   11836                 :        2109 :   gfc_conv_descriptor_data_set (&se->post, desc, tmp);
   11837                 :             : 
   11838                 :             :   /* Check that the shapes are the same between lhs and expression.
   11839                 :             :      The evaluation of the shape is done in 'shape_block' to avoid
   11840                 :             :      unitialized warnings from the lhs bounds. */
   11841                 :        2109 :   not_same_shape = boolean_false_node;
   11842                 :        2109 :   gfc_start_block (&shape_block);
   11843                 :        6801 :   for (n = 0 ; n < rank; n++)
   11844                 :             :     {
   11845                 :        4692 :       tree tmp1;
   11846                 :        4692 :       tmp = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
   11847                 :        4692 :       tmp1 = gfc_conv_descriptor_lbound_get (res_desc, gfc_rank_cst[n]);
   11848                 :        4692 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
   11849                 :             :                              gfc_array_index_type, tmp, tmp1);
   11850                 :        4692 :       tmp1 = gfc_conv_descriptor_ubound_get (desc, gfc_rank_cst[n]);
   11851                 :        4692 :       tmp = fold_build2_loc (input_location, MINUS_EXPR,
   11852                 :             :                              gfc_array_index_type, tmp, tmp1);
   11853                 :        4692 :       tmp1 = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
   11854                 :        4692 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
   11855                 :             :                              gfc_array_index_type, tmp, tmp1);
   11856                 :        4692 :       tmp = fold_build2_loc (input_location, NE_EXPR,
   11857                 :             :                              logical_type_node, tmp,
   11858                 :             :                              gfc_index_zero_node);
   11859                 :        4692 :       tmp = gfc_evaluate_now (tmp, &shape_block);
   11860                 :        4692 :       if (n == 0)
   11861                 :             :         not_same_shape = tmp;
   11862                 :             :       else
   11863                 :        2583 :         not_same_shape = fold_build2_loc (input_location, TRUTH_OR_EXPR,
   11864                 :             :                                           logical_type_node, tmp,
   11865                 :             :                                           not_same_shape);
   11866                 :             :     }
   11867                 :             : 
   11868                 :             :   /* 'zero_cond' being true is equal to lhs not being allocated or the
   11869                 :             :      shapes being different.  */
   11870                 :        2109 :   tmp = fold_build2_loc (input_location, TRUTH_OR_EXPR, logical_type_node,
   11871                 :             :                          zero_cond, not_same_shape);
   11872                 :        2109 :   gfc_add_modify (&shape_block, zero_cond, tmp);
   11873                 :        2109 :   tmp = gfc_finish_block (&shape_block);
   11874                 :        2109 :   tmp = build3_v (COND_EXPR, zero_cond,
   11875                 :             :                   build_empty_stmt (input_location), tmp);
   11876                 :        2109 :   gfc_add_expr_to_block (&se->post, tmp);
   11877                 :             : 
   11878                 :             :   /* Now reset the bounds returned from the function call to bounds based
   11879                 :             :      on the lhs lbounds, except where the lhs is not allocated or the shapes
   11880                 :             :      of 'variable and 'expr' are different. Set the offset accordingly.  */
   11881                 :        2109 :   offset = gfc_index_zero_node;
   11882                 :        6801 :   for (n = 0 ; n < rank; n++)
   11883                 :             :     {
   11884                 :        4692 :       tree lbound;
   11885                 :             : 
   11886                 :        4692 :       lbound = gfc_conv_descriptor_lbound_get (desc, gfc_rank_cst[n]);
   11887                 :        4692 :       lbound = fold_build3_loc (input_location, COND_EXPR,
   11888                 :             :                                 gfc_array_index_type, zero_cond,
   11889                 :             :                                 gfc_index_one_node, lbound);
   11890                 :        4692 :       lbound = gfc_evaluate_now (lbound, &se->post);
   11891                 :             : 
   11892                 :        4692 :       tmp = gfc_conv_descriptor_ubound_get (res_desc, gfc_rank_cst[n]);
   11893                 :        4692 :       tmp = fold_build2_loc (input_location, PLUS_EXPR,
   11894                 :             :                              gfc_array_index_type, tmp, lbound);
   11895                 :        4692 :       gfc_conv_descriptor_lbound_set (&se->post, desc,
   11896                 :             :                                       gfc_rank_cst[n], lbound);
   11897                 :        4692 :       gfc_conv_descriptor_ubound_set (&se->post, desc,
   11898                 :             :                                       gfc_rank_cst[n], tmp);
   11899                 :             : 
   11900                 :             :       /* Set stride and accumulate the offset.  */
   11901                 :        4692 :       tmp = gfc_conv_descriptor_stride_get (res_desc, gfc_rank_cst[n]);
   11902                 :        4692 :       gfc_conv_descriptor_stride_set (&se->post, desc,
   11903                 :             :                                       gfc_rank_cst[n], tmp);
   11904                 :        4692 :       tmp = fold_build2_loc (input_location, MULT_EXPR,
   11905                 :             :                              gfc_array_index_type, lbound, tmp);
   11906                 :        4692 :       offset = fold_build2_loc (input_location, MINUS_EXPR,
   11907                 :             :                                 gfc_array_index_type, offset, tmp);
   11908                 :        4692 :       offset = gfc_evaluate_now (offset, &se->post);
   11909                 :             :     }
   11910                 :             : 
   11911                 :        2109 :   gfc_conv_descriptor_offset_set (&se->post, desc, offset);
   11912                 :        2109 : }
   11913                 :             : 
   11914                 :             : 
   11915                 :             : 
   11916                 :             : /* Try to translate array(:) = func (...), where func is a transformational
   11917                 :             :    array function, without using a temporary.  Returns NULL if this isn't the
   11918                 :             :    case.  */
   11919                 :             : 
   11920                 :             : static tree
   11921                 :       14426 : gfc_trans_arrayfunc_assign (gfc_expr * expr1, gfc_expr * expr2)
   11922                 :             : {
   11923                 :       14426 :   gfc_se se;
   11924                 :       14426 :   gfc_ss *ss = NULL;
   11925                 :       14426 :   gfc_component *comp = NULL;
   11926                 :       14426 :   gfc_loopinfo loop;
   11927                 :       14426 :   tree tmp;
   11928                 :       14426 :   tree lhs;
   11929                 :       14426 :   gfc_se final_se;
   11930                 :       14426 :   gfc_symbol *sym = expr1->symtree->n.sym;
   11931                 :       14426 :   bool finalizable =  gfc_may_be_finalized (expr1->ts);
   11932                 :             : 
   11933                 :       14426 :   if (arrayfunc_assign_needs_temporary (expr1, expr2))
   11934                 :             :     return NULL;
   11935                 :             : 
   11936                 :             :   /* The frontend doesn't seem to bother filling in expr->symtree for intrinsic
   11937                 :             :      functions.  */
   11938                 :        7019 :   comp = gfc_get_proc_ptr_comp (expr2);
   11939                 :             : 
   11940                 :        7019 :   if (!(expr2->value.function.isym
   11941                 :         663 :               || (comp && comp->attr.dimension)
   11942                 :         663 :               || (!comp && gfc_return_by_reference (expr2->value.function.esym)
   11943                 :         663 :                   && expr2->value.function.esym->result->attr.dimension)))
   11944                 :           0 :     return NULL;
   11945                 :             : 
   11946                 :        7019 :   gfc_init_se (&se, NULL);
   11947                 :        7019 :   gfc_start_block (&se.pre);
   11948                 :        7019 :   se.want_pointer = 1;
   11949                 :             : 
   11950                 :             :   /* First the lhs must be finalized, if necessary. We use a copy of the symbol
   11951                 :             :      backend decl, stash the original away for the finalization so that the
   11952                 :             :      value used is that before the assignment. This is necessary because
   11953                 :             :      evaluation of the rhs expression using direct by reference can change
   11954                 :             :      the value. However, the standard mandates that the finalization must occur
   11955                 :             :      after evaluation of the rhs.  */
   11956                 :        7019 :   gfc_init_se (&final_se, NULL);
   11957                 :             : 
   11958                 :        7019 :   if (finalizable)
   11959                 :             :     {
   11960                 :          33 :       tmp = sym->backend_decl;
   11961                 :          33 :       lhs = sym->backend_decl;
   11962                 :          33 :       if (INDIRECT_REF_P (tmp))
   11963                 :           0 :         tmp = TREE_OPERAND (tmp, 0);
   11964                 :          33 :       sym->backend_decl = gfc_create_var (TREE_TYPE (tmp), "lhs");
   11965                 :          33 :       gfc_add_modify (&se.pre, sym->backend_decl, tmp);
   11966                 :          33 :       if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
   11967                 :             :         {
   11968                 :           0 :           tmp = gfc_copy_alloc_comp (expr1->ts.u.derived, tmp, sym->backend_decl,
   11969                 :             :                                      expr1->rank, 0);
   11970                 :           0 :           gfc_add_expr_to_block (&final_se.pre, tmp);
   11971                 :             :         }
   11972                 :             :     }
   11973                 :             : 
   11974                 :          33 :   if (finalizable && gfc_assignment_finalizer_call (&final_se, expr1, false))
   11975                 :             :     {
   11976                 :          33 :       gfc_add_block_to_block (&se.pre, &final_se.pre);
   11977                 :          33 :       gfc_add_block_to_block (&se.post, &final_se.finalblock);
   11978                 :             :     }
   11979                 :             : 
   11980                 :        7019 :   if (finalizable)
   11981                 :          33 :     sym->backend_decl = lhs;
   11982                 :             : 
   11983                 :        7019 :   gfc_conv_array_parameter (&se, expr1, false, NULL, NULL, NULL);
   11984                 :             : 
   11985                 :        7019 :   if (expr1->ts.type == BT_DERIVED
   11986                 :         216 :         && expr1->ts.u.derived->attr.alloc_comp)
   11987                 :             :     {
   11988                 :          74 :       tmp = build_fold_indirect_ref_loc (input_location, se.expr);
   11989                 :          74 :       tmp = gfc_deallocate_alloc_comp_no_caf (expr1->ts.u.derived, tmp,
   11990                 :             :                                               expr1->rank);
   11991                 :          74 :       gfc_add_expr_to_block (&se.pre, tmp);
   11992                 :             :     }
   11993                 :             : 
   11994                 :        7019 :   se.direct_byref = 1;
   11995                 :        7019 :   se.ss = gfc_walk_expr (expr2);
   11996                 :        7019 :   gcc_assert (se.ss != gfc_ss_terminator);
   11997                 :             : 
   11998                 :             :   /* Since this is a direct by reference call, references to the lhs can be
   11999                 :             :      used for finalization of the function result just as long as the blocks
   12000                 :             :      from final_se are added at the right time.  */
   12001                 :        7019 :   gfc_init_se (&final_se, NULL);
   12002                 :        7019 :   if (finalizable && expr2->value.function.esym)
   12003                 :             :     {
   12004                 :          20 :       final_se.expr = build_fold_indirect_ref_loc (input_location, se.expr);
   12005                 :          20 :       gfc_finalize_tree_expr (&final_se, expr2->ts.u.derived,
   12006                 :          20 :                                     expr2->value.function.esym->attr,
   12007                 :             :                                     expr2->rank);
   12008                 :             :     }
   12009                 :             : 
   12010                 :             :   /* Reallocate on assignment needs the loopinfo for extrinsic functions.
   12011                 :             :      This is signalled to gfc_conv_procedure_call by setting is_alloc_lhs.
   12012                 :             :      Clearly, this cannot be done for an allocatable function result, since
   12013                 :             :      the shape of the result is unknown and, in any case, the function must
   12014                 :             :      correctly take care of the reallocation internally. For intrinsic
   12015                 :             :      calls, the array data is freed and the library takes care of allocation.
   12016                 :             :      TODO: Add logic of trans-array.cc: gfc_alloc_allocatable_for_assignment
   12017                 :             :      to the library.  */
   12018                 :        7019 :   if (flag_realloc_lhs
   12019                 :        6944 :         && gfc_is_reallocatable_lhs (expr1)
   12020                 :        9288 :         && !gfc_expr_attr (expr1).codimension
   12021                 :        2269 :         && !gfc_is_coindexed (expr1)
   12022                 :        9288 :         && !(expr2->value.function.esym
   12023                 :         160 :             && expr2->value.function.esym->result->attr.allocatable))
   12024                 :             :     {
   12025                 :        2269 :       realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
   12026                 :             : 
   12027                 :        2269 :       if (!expr2->value.function.isym)
   12028                 :             :         {
   12029                 :         160 :           ss = gfc_walk_expr (expr1);
   12030                 :         160 :           gcc_assert (ss != gfc_ss_terminator);
   12031                 :             : 
   12032                 :         160 :           realloc_lhs_loop_for_fcn_call (&se, &expr1->where, &ss, &loop);
   12033                 :         160 :           ss->is_alloc_lhs = 1;
   12034                 :             :         }
   12035                 :             :       else
   12036                 :             :         {
   12037                 :        2109 :           tree dtype = NULL_TREE;
   12038                 :        2109 :           tree type = gfc_typenode_for_spec (&expr2->ts);
   12039                 :        2109 :           if (expr1->ts.type == BT_CLASS)
   12040                 :             :             {
   12041                 :          13 :               tmp = gfc_class_vptr_get (sym->backend_decl);
   12042                 :          13 :               tree tmp2 = gfc_get_symbol_decl (gfc_find_vtab (&expr2->ts));
   12043                 :          13 :               tmp2 = gfc_build_addr_expr (TREE_TYPE (tmp), tmp2);
   12044                 :          13 :               gfc_add_modify (&se.pre, tmp, tmp2);
   12045                 :          13 :               dtype = gfc_get_dtype_rank_type (expr1->rank,type);
   12046                 :             :             }
   12047                 :        2109 :           fcncall_realloc_result (&se, expr1->rank, dtype);
   12048                 :             :         }
   12049                 :             :     }
   12050                 :             : 
   12051                 :        7019 :   gfc_conv_function_expr (&se, expr2);
   12052                 :             : 
   12053                 :             :   /* Fix the result.  */
   12054                 :        7019 :   gfc_add_block_to_block (&se.pre, &se.post);
   12055                 :        7019 :   if (finalizable)
   12056                 :          33 :     gfc_add_block_to_block (&se.pre, &final_se.pre);
   12057                 :             : 
   12058                 :             :   /* Do the finalization, including final calls from function arguments.  */
   12059                 :          33 :   if (finalizable)
   12060                 :             :     {
   12061                 :          33 :       gfc_add_block_to_block (&se.pre, &final_se.post);
   12062                 :          33 :       gfc_add_block_to_block (&se.pre, &se.finalblock);
   12063                 :          33 :       gfc_add_block_to_block (&se.pre, &final_se.finalblock);
   12064                 :             :    }
   12065                 :             : 
   12066                 :        7019 :   if (ss)
   12067                 :         160 :     gfc_cleanup_loop (&loop);
   12068                 :             :   else
   12069                 :        6859 :     gfc_free_ss_chain (se.ss);
   12070                 :             : 
   12071                 :        7019 :   return gfc_finish_block (&se.pre);
   12072                 :             : }
   12073                 :             : 
   12074                 :             : 
   12075                 :             : /* Try to efficiently translate array(:) = 0.  Return NULL if this
   12076                 :             :    can't be done.  */
   12077                 :             : 
   12078                 :             : static tree
   12079                 :        3651 : gfc_trans_zero_assign (gfc_expr * expr)
   12080                 :             : {
   12081                 :        3651 :   tree dest, len, type;
   12082                 :        3651 :   tree tmp;
   12083                 :        3651 :   gfc_symbol *sym;
   12084                 :             : 
   12085                 :        3651 :   sym = expr->symtree->n.sym;
   12086                 :        3651 :   dest = gfc_get_symbol_decl (sym);
   12087                 :             : 
   12088                 :        3651 :   type = TREE_TYPE (dest);
   12089                 :        3651 :   if (POINTER_TYPE_P (type))
   12090                 :         244 :     type = TREE_TYPE (type);
   12091                 :        3651 :   if (GFC_ARRAY_TYPE_P (type))
   12092                 :             :     {
   12093                 :             :       /* Determine the length of the array.  */
   12094                 :        2476 :       len = GFC_TYPE_ARRAY_SIZE (type);
   12095                 :        2476 :       if (!len || TREE_CODE (len) != INTEGER_CST)
   12096                 :             :         return NULL_TREE;
   12097                 :             :     }
   12098                 :        1175 :   else if (GFC_DESCRIPTOR_TYPE_P (type)
   12099                 :        1175 :           && gfc_is_simply_contiguous (expr, false, false))
   12100                 :             :     {
   12101                 :        1075 :       if (POINTER_TYPE_P (TREE_TYPE (dest)))
   12102                 :           4 :         dest = build_fold_indirect_ref_loc (input_location, dest);
   12103                 :        1075 :       len = gfc_conv_descriptor_size (dest, GFC_TYPE_ARRAY_RANK (type));
   12104                 :        1075 :       dest = gfc_conv_descriptor_data_get (dest);
   12105                 :             :     }
   12106                 :             :   else
   12107                 :         100 :     return NULL_TREE;
   12108                 :             : 
   12109                 :             :   /* If we are zeroing a local array avoid taking its address by emitting
   12110                 :             :      a = {} instead.  */
   12111                 :        3376 :   if (!POINTER_TYPE_P (TREE_TYPE (dest)))
   12112                 :        2259 :     return build2_loc (input_location, MODIFY_EXPR, void_type_node,
   12113                 :        2259 :                        dest, build_constructor (TREE_TYPE (dest),
   12114                 :        2259 :                                               NULL));
   12115                 :             : 
   12116                 :             :   /* Multiply len by element size.  */
   12117                 :        1117 :   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (type));
   12118                 :        1117 :   len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
   12119                 :             :                          len, fold_convert (gfc_array_index_type, tmp));
   12120                 :             : 
   12121                 :             :   /* Convert arguments to the correct types.  */
   12122                 :        1117 :   dest = fold_convert (pvoid_type_node, dest);
   12123                 :        1117 :   len = fold_convert (size_type_node, len);
   12124                 :             : 
   12125                 :             :   /* Construct call to __builtin_memset.  */
   12126                 :        1117 :   tmp = build_call_expr_loc (input_location,
   12127                 :             :                              builtin_decl_explicit (BUILT_IN_MEMSET),
   12128                 :             :                              3, dest, integer_zero_node, len);
   12129                 :        1117 :   return fold_convert (void_type_node, tmp);
   12130                 :             : }
   12131                 :             : 
   12132                 :             : 
   12133                 :             : /* Helper for gfc_trans_array_copy and gfc_trans_array_constructor_copy
   12134                 :             :    that constructs the call to __builtin_memcpy.  */
   12135                 :             : 
   12136                 :             : tree
   12137                 :        7005 : gfc_build_memcpy_call (tree dst, tree src, tree len)
   12138                 :             : {
   12139                 :        7005 :   tree tmp;
   12140                 :             : 
   12141                 :             :   /* Convert arguments to the correct types.  */
   12142                 :        7005 :   if (!POINTER_TYPE_P (TREE_TYPE (dst)))
   12143                 :        6773 :     dst = gfc_build_addr_expr (pvoid_type_node, dst);
   12144                 :             :   else
   12145                 :         232 :     dst = fold_convert (pvoid_type_node, dst);
   12146                 :             : 
   12147                 :        7005 :   if (!POINTER_TYPE_P (TREE_TYPE (src)))
   12148                 :        6671 :     src = gfc_build_addr_expr (pvoid_type_node, src);
   12149                 :             :   else
   12150                 :         334 :     src = fold_convert (pvoid_type_node, src);
   12151                 :             : 
   12152                 :        7005 :   len = fold_convert (size_type_node, len);
   12153                 :             : 
   12154                 :             :   /* Construct call to __builtin_memcpy.  */
   12155                 :        7005 :   tmp = build_call_expr_loc (input_location,
   12156                 :             :                              builtin_decl_explicit (BUILT_IN_MEMCPY),
   12157                 :             :                              3, dst, src, len);
   12158                 :        7005 :   return fold_convert (void_type_node, tmp);
   12159                 :             : }
   12160                 :             : 
   12161                 :             : 
   12162                 :             : /* Try to efficiently translate dst(:) = src(:).  Return NULL if this
   12163                 :             :    can't be done.  EXPR1 is the destination/lhs and EXPR2 is the
   12164                 :             :    source/rhs, both are gfc_full_array_ref_p which have been checked for
   12165                 :             :    dependencies.  */
   12166                 :             : 
   12167                 :             : static tree
   12168                 :        2502 : gfc_trans_array_copy (gfc_expr * expr1, gfc_expr * expr2)
   12169                 :             : {
   12170                 :        2502 :   tree dst, dlen, dtype;
   12171                 :        2502 :   tree src, slen, stype;
   12172                 :        2502 :   tree tmp;
   12173                 :             : 
   12174                 :        2502 :   dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
   12175                 :        2502 :   src = gfc_get_symbol_decl (expr2->symtree->n.sym);
   12176                 :             : 
   12177                 :        2502 :   dtype = TREE_TYPE (dst);
   12178                 :        2502 :   if (POINTER_TYPE_P (dtype))
   12179                 :         223 :     dtype = TREE_TYPE (dtype);
   12180                 :        2502 :   stype = TREE_TYPE (src);
   12181                 :        2502 :   if (POINTER_TYPE_P (stype))
   12182                 :         265 :     stype = TREE_TYPE (stype);
   12183                 :             : 
   12184                 :        2502 :   if (!GFC_ARRAY_TYPE_P (dtype) || !GFC_ARRAY_TYPE_P (stype))
   12185                 :             :     return NULL_TREE;
   12186                 :             : 
   12187                 :             :   /* Determine the lengths of the arrays.  */
   12188                 :        1533 :   dlen = GFC_TYPE_ARRAY_SIZE (dtype);
   12189                 :        1533 :   if (!dlen || TREE_CODE (dlen) != INTEGER_CST)
   12190                 :             :     return NULL_TREE;
   12191                 :        1444 :   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
   12192                 :        1444 :   dlen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
   12193                 :             :                           dlen, fold_convert (gfc_array_index_type, tmp));
   12194                 :             : 
   12195                 :        1444 :   slen = GFC_TYPE_ARRAY_SIZE (stype);
   12196                 :        1444 :   if (!slen || TREE_CODE (slen) != INTEGER_CST)
   12197                 :             :     return NULL_TREE;
   12198                 :        1438 :   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (stype));
   12199                 :        1438 :   slen = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type,
   12200                 :             :                           slen, fold_convert (gfc_array_index_type, tmp));
   12201                 :             : 
   12202                 :             :   /* Sanity check that they are the same.  This should always be
   12203                 :             :      the case, as we should already have checked for conformance.  */
   12204                 :        1438 :   if (!tree_int_cst_equal (slen, dlen))
   12205                 :             :     return NULL_TREE;
   12206                 :             : 
   12207                 :        1438 :   return gfc_build_memcpy_call (dst, src, dlen);
   12208                 :             : }
   12209                 :             : 
   12210                 :             : 
   12211                 :             : /* Try to efficiently translate array(:) = (/ ... /).  Return NULL if
   12212                 :             :    this can't be done.  EXPR1 is the destination/lhs for which
   12213                 :             :    gfc_full_array_ref_p is true, and EXPR2 is the source/rhs.  */
   12214                 :             : 
   12215                 :             : static tree
   12216                 :        7426 : gfc_trans_array_constructor_copy (gfc_expr * expr1, gfc_expr * expr2)
   12217                 :             : {
   12218                 :        7426 :   unsigned HOST_WIDE_INT nelem;
   12219                 :        7426 :   tree dst, dtype;
   12220                 :        7426 :   tree src, stype;
   12221                 :        7426 :   tree len;
   12222                 :        7426 :   tree tmp;
   12223                 :             : 
   12224                 :        7426 :   nelem = gfc_constant_array_constructor_p (expr2->value.constructor);
   12225                 :        7426 :   if (nelem == 0)
   12226                 :             :     return NULL_TREE;
   12227                 :             : 
   12228                 :        5801 :   dst = gfc_get_symbol_decl (expr1->symtree->n.sym);
   12229                 :        5801 :   dtype = TREE_TYPE (dst);
   12230                 :        5801 :   if (POINTER_TYPE_P (dtype))
   12231                 :         251 :     dtype = TREE_TYPE (dtype);
   12232                 :        5801 :   if (!GFC_ARRAY_TYPE_P (dtype))
   12233                 :             :     return NULL_TREE;
   12234                 :             : 
   12235                 :             :   /* Determine the lengths of the array.  */
   12236                 :        5145 :   len = GFC_TYPE_ARRAY_SIZE (dtype);
   12237                 :        5145 :   if (!len || TREE_CODE (len) != INTEGER_CST)
   12238                 :             :     return NULL_TREE;
   12239                 :             : 
   12240                 :             :   /* Confirm that the constructor is the same size.  */
   12241                 :        5047 :   if (compare_tree_int (len, nelem) != 0)
   12242                 :             :     return NULL_TREE;
   12243                 :             : 
   12244                 :        5047 :   tmp = TYPE_SIZE_UNIT (gfc_get_element_type (dtype));
   12245                 :        5047 :   len = fold_build2_loc (input_location, MULT_EXPR, gfc_array_index_type, len,
   12246                 :             :                          fold_convert (gfc_array_index_type, tmp));
   12247                 :             : 
   12248                 :        5047 :   stype = gfc_typenode_for_spec (&expr2->ts);
   12249                 :        5047 :   src = gfc_build_constant_array_constructor (expr2, stype);
   12250                 :             : 
   12251                 :        5047 :   return gfc_build_memcpy_call (dst, src, len);
   12252                 :             : }
   12253                 :             : 
   12254                 :             : 
   12255                 :             : /* Tells whether the expression is to be treated as a variable reference.  */
   12256                 :             : 
   12257                 :             : bool
   12258                 :      298118 : gfc_expr_is_variable (gfc_expr *expr)
   12259                 :             : {
   12260                 :      298378 :   gfc_expr *arg;
   12261                 :      298378 :   gfc_component *comp;
   12262                 :      298378 :   gfc_symbol *func_ifc;
   12263                 :             : 
   12264                 :      298378 :   if (expr->expr_type == EXPR_VARIABLE)
   12265                 :             :     return true;
   12266                 :             : 
   12267                 :      265550 :   arg = gfc_get_noncopying_intrinsic_argument (expr);
   12268                 :      265550 :   if (arg)
   12269                 :             :     {
   12270                 :         260 :       gcc_assert (expr->value.function.isym->id == GFC_ISYM_TRANSPOSE);
   12271                 :             :       return gfc_expr_is_variable (arg);
   12272                 :             :     }
   12273                 :             : 
   12274                 :             :   /* A data-pointer-returning function should be considered as a variable
   12275                 :             :      too.  */
   12276                 :      265290 :   if (expr->expr_type == EXPR_FUNCTION
   12277                 :       34936 :       && expr->ref == NULL)
   12278                 :             :     {
   12279                 :       34569 :       if (expr->value.function.isym != NULL)
   12280                 :             :         return false;
   12281                 :             : 
   12282                 :        8898 :       if (expr->value.function.esym != NULL)
   12283                 :             :         {
   12284                 :        8889 :           func_ifc = expr->value.function.esym;
   12285                 :        8889 :           goto found_ifc;
   12286                 :             :         }
   12287                 :           9 :       gcc_assert (expr->symtree);
   12288                 :           9 :       func_ifc = expr->symtree->n.sym;
   12289                 :           9 :       goto found_ifc;
   12290                 :             :     }
   12291                 :             : 
   12292                 :      230721 :   comp = gfc_get_proc_ptr_comp (expr);
   12293                 :      230721 :   if ((expr->expr_type == EXPR_PPC || expr->expr_type == EXPR_FUNCTION)
   12294                 :         367 :       && comp)
   12295                 :             :     {
   12296                 :         265 :       func_ifc = comp->ts.interface;
   12297                 :         265 :       goto found_ifc;
   12298                 :             :     }
   12299                 :             : 
   12300                 :      230456 :   if (expr->expr_type == EXPR_COMPCALL)
   12301                 :             :     {
   12302                 :           0 :       gcc_assert (!expr->value.compcall.tbp->is_generic);
   12303                 :           0 :       func_ifc = expr->value.compcall.tbp->u.specific->n.sym;
   12304                 :           0 :       goto found_ifc;
   12305                 :             :     }
   12306                 :             : 
   12307                 :             :   return false;
   12308                 :             : 
   12309                 :        9163 : found_ifc:
   12310                 :        9163 :   gcc_assert (func_ifc->attr.function
   12311                 :             :               && func_ifc->result != NULL);
   12312                 :        9163 :   return func_ifc->result->attr.pointer;
   12313                 :             : }
   12314                 :             : 
   12315                 :             : 
   12316                 :             : /* Is the lhs OK for automatic reallocation?  */
   12317                 :             : 
   12318                 :             : static bool
   12319                 :      254037 : is_scalar_reallocatable_lhs (gfc_expr *expr)
   12320                 :             : {
   12321                 :      254037 :   gfc_ref * ref;
   12322                 :             : 
   12323                 :             :   /* An allocatable variable with no reference.  */
   12324                 :      254037 :   if (expr->symtree->n.sym->attr.allocatable
   12325                 :        6514 :         && !expr->ref)
   12326                 :             :     return true;
   12327                 :             : 
   12328                 :             :   /* All that can be left are allocatable components.  However, we do
   12329                 :             :      not check for allocatable components here because the expression
   12330                 :             :      could be an allocatable component of a pointer component.  */
   12331                 :      251432 :   if (expr->symtree->n.sym->ts.type != BT_DERIVED
   12332                 :      231200 :         && expr->symtree->n.sym->ts.type != BT_CLASS)
   12333                 :             :     return false;
   12334                 :             : 
   12335                 :             :   /* Find an allocatable component ref last.  */
   12336                 :       36387 :   for (ref = expr->ref; ref; ref = ref->next)
   12337                 :       15173 :     if (ref->type == REF_COMPONENT
   12338                 :       11296 :           && !ref->next
   12339                 :        8794 :           && ref->u.c.component->attr.allocatable)
   12340                 :             :       return true;
   12341                 :             : 
   12342                 :             :   return false;
   12343                 :             : }
   12344                 :             : 
   12345                 :             : 
   12346                 :             : /* Allocate or reallocate scalar lhs, as necessary.  */
   12347                 :             : 
   12348                 :             : static void
   12349                 :        3312 : alloc_scalar_allocatable_for_assignment (stmtblock_t *block,
   12350                 :             :                                          tree string_length,
   12351                 :             :                                          gfc_expr *expr1,
   12352                 :             :                                          gfc_expr *expr2)
   12353                 :             : 
   12354                 :             : {
   12355                 :        3312 :   tree cond;
   12356                 :        3312 :   tree tmp;
   12357                 :        3312 :   tree size;
   12358                 :        3312 :   tree size_in_bytes;
   12359                 :        3312 :   tree jump_label1;
   12360                 :        3312 :   tree jump_label2;
   12361                 :        3312 :   gfc_se lse;
   12362                 :        3312 :   gfc_ref *ref;
   12363                 :             : 
   12364                 :        3312 :   if (!expr1 || expr1->rank)
   12365                 :           0 :     return;
   12366                 :             : 
   12367                 :        3312 :   if (!expr2 || expr2->rank)
   12368                 :             :     return;
   12369                 :             : 
   12370                 :        4506 :   for (ref = expr1->ref; ref; ref = ref->next)
   12371                 :        1194 :     if (ref->type == REF_SUBSTRING)
   12372                 :             :       return;
   12373                 :             : 
   12374                 :        3312 :   realloc_lhs_warning (expr2->ts.type, false, &expr2->where);
   12375                 :             : 
   12376                 :             :   /* Since this is a scalar lhs, we can afford to do this.  That is,
   12377                 :             :      there is no risk of side effects being repeated.  */
   12378                 :        3312 :   gfc_init_se (&lse, NULL);
   12379                 :        3312 :   lse.want_pointer = 1;
   12380                 :        3312 :   gfc_conv_expr (&lse, expr1);
   12381                 :             : 
   12382                 :        3312 :   jump_label1 = gfc_build_label_decl (NULL_TREE);
   12383                 :        3312 :   jump_label2 = gfc_build_label_decl (NULL_TREE);
   12384                 :             : 
   12385                 :             :   /* Do the allocation if the lhs is NULL. Otherwise go to label 1.  */
   12386                 :        3312 :   tmp = build_int_cst (TREE_TYPE (lse.expr), 0);
   12387                 :        3312 :   cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
   12388                 :             :                           lse.expr, tmp);
   12389                 :        3312 :   tmp = build3_v (COND_EXPR, cond,
   12390                 :             :                   build1_v (GOTO_EXPR, jump_label1),
   12391                 :             :                   build_empty_stmt (input_location));
   12392                 :        3312 :   gfc_add_expr_to_block (block, tmp);
   12393                 :             : 
   12394                 :        3312 :   if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   12395                 :             :     {
   12396                 :             :       /* Use the rhs string length and the lhs element size. Note that 'size' is
   12397                 :             :          used below for the string-length comparison, only.  */
   12398                 :        1345 :       size = string_length;
   12399                 :        1345 :       tmp = TYPE_SIZE_UNIT (gfc_get_char_type (expr1->ts.kind));
   12400                 :        2690 :       size_in_bytes = fold_build2_loc (input_location, MULT_EXPR,
   12401                 :        1345 :                                        TREE_TYPE (tmp), tmp,
   12402                 :        1345 :                                        fold_convert (TREE_TYPE (tmp), size));
   12403                 :             :     }
   12404                 :             :   else
   12405                 :             :     {
   12406                 :             :       /* Otherwise use the length in bytes of the rhs.  */
   12407                 :        1967 :       size = TYPE_SIZE_UNIT (gfc_typenode_for_spec (&expr1->ts));
   12408                 :        1967 :       size_in_bytes = size;
   12409                 :             :     }
   12410                 :             : 
   12411                 :        3312 :   size_in_bytes = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
   12412                 :             :                                    size_in_bytes, size_one_node);
   12413                 :             : 
   12414                 :        3312 :   if (gfc_caf_attr (expr1).codimension && flag_coarray == GFC_FCOARRAY_LIB)
   12415                 :             :     {
   12416                 :          31 :       tree caf_decl, token;
   12417                 :          31 :       gfc_se caf_se;
   12418                 :          31 :       symbol_attribute attr;
   12419                 :             : 
   12420                 :          31 :       gfc_clear_attr (&attr);
   12421                 :          31 :       gfc_init_se (&caf_se, NULL);
   12422                 :             : 
   12423                 :          31 :       caf_decl = gfc_get_tree_for_caf_expr (expr1);
   12424                 :          31 :       gfc_get_caf_token_offset (&caf_se, &token, NULL, caf_decl, NULL_TREE,
   12425                 :             :                                 NULL);
   12426                 :          31 :       gfc_add_block_to_block (block, &caf_se.pre);
   12427                 :          31 :       gfc_allocate_allocatable (block, lse.expr, size_in_bytes,
   12428                 :             :                                 gfc_build_addr_expr (NULL_TREE, token),
   12429                 :             :                                 NULL_TREE, NULL_TREE, NULL_TREE, jump_label1,
   12430                 :             :                                 expr1, 1);
   12431                 :             :     }
   12432                 :        3281 :   else if (expr1->ts.type == BT_DERIVED && expr1->ts.u.derived->attr.alloc_comp)
   12433                 :             :     {
   12434                 :          33 :       tmp = build_call_expr_loc (input_location,
   12435                 :             :                                  builtin_decl_explicit (BUILT_IN_CALLOC),
   12436                 :             :                                  2, build_one_cst (size_type_node),
   12437                 :             :                                  size_in_bytes);
   12438                 :          33 :       tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
   12439                 :          33 :       gfc_add_modify (block, lse.expr, tmp);
   12440                 :             :     }
   12441                 :             :   else
   12442                 :             :     {
   12443                 :        3248 :       tmp = build_call_expr_loc (input_location,
   12444                 :             :                                  builtin_decl_explicit (BUILT_IN_MALLOC),
   12445                 :             :                                  1, size_in_bytes);
   12446                 :        3248 :       tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
   12447                 :        3248 :       gfc_add_modify (block, lse.expr, tmp);
   12448                 :             :     }
   12449                 :             : 
   12450                 :        3312 :   if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   12451                 :             :     {
   12452                 :             :       /* Deferred characters need checking for lhs and rhs string
   12453                 :             :          length.  Other deferred parameter variables will have to
   12454                 :             :          come here too.  */
   12455                 :        1345 :       tmp = build1_v (GOTO_EXPR, jump_label2);
   12456                 :        1345 :       gfc_add_expr_to_block (block, tmp);
   12457                 :             :     }
   12458                 :        3312 :   tmp = build1_v (LABEL_EXPR, jump_label1);
   12459                 :        3312 :   gfc_add_expr_to_block (block, tmp);
   12460                 :             : 
   12461                 :             :   /* For a deferred length character, reallocate if lengths of lhs and
   12462                 :             :      rhs are different.  */
   12463                 :        3312 :   if (expr1->ts.type == BT_CHARACTER && expr1->ts.deferred)
   12464                 :             :     {
   12465                 :        1345 :       cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   12466                 :             :                               lse.string_length,
   12467                 :        1345 :                               fold_convert (TREE_TYPE (lse.string_length),
   12468                 :             :                                             size));
   12469                 :             :       /* Jump past the realloc if the lengths are the same.  */
   12470                 :        1345 :       tmp = build3_v (COND_EXPR, cond,
   12471                 :             :                       build1_v (GOTO_EXPR, jump_label2),
   12472                 :             :                       build_empty_stmt (input_location));
   12473                 :        1345 :       gfc_add_expr_to_block (block, tmp);
   12474                 :        1345 :       tmp = build_call_expr_loc (input_location,
   12475                 :             :                                  builtin_decl_explicit (BUILT_IN_REALLOC),
   12476                 :             :                                  2, fold_convert (pvoid_type_node, lse.expr),
   12477                 :             :                                  size_in_bytes);
   12478                 :        1345 :       tree omp_cond = NULL_TREE;
   12479                 :        1345 :       if (flag_openmp_allocators)
   12480                 :             :         {
   12481                 :           1 :           tree omp_tmp;
   12482                 :           1 :           omp_cond = gfc_omp_call_is_alloc (lse.expr);
   12483                 :           1 :           omp_cond = gfc_evaluate_now (omp_cond, block);
   12484                 :             : 
   12485                 :           1 :           omp_tmp = builtin_decl_explicit (BUILT_IN_GOMP_REALLOC);
   12486                 :           1 :           omp_tmp = build_call_expr_loc (input_location, omp_tmp, 4,
   12487                 :             :                                          fold_convert (pvoid_type_node,
   12488                 :             :                                                        lse.expr), size_in_bytes,
   12489                 :             :                                          build_zero_cst (ptr_type_node),
   12490                 :             :                                          build_zero_cst (ptr_type_node));
   12491                 :           1 :           tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp),
   12492                 :             :                             omp_cond, omp_tmp, tmp);
   12493                 :             :         }
   12494                 :        1345 :       tmp = fold_convert (TREE_TYPE (lse.expr), tmp);
   12495                 :        1345 :       gfc_add_modify (block, lse.expr, tmp);
   12496                 :        1345 :       if (omp_cond)
   12497                 :           1 :         gfc_add_expr_to_block (block,
   12498                 :             :                                build3_loc (input_location, COND_EXPR,
   12499                 :             :                                void_type_node, omp_cond,
   12500                 :             :                                gfc_omp_call_add_alloc (lse.expr),
   12501                 :             :                                build_empty_stmt (input_location)));
   12502                 :        1345 :       tmp = build1_v (LABEL_EXPR, jump_label2);
   12503                 :        1345 :       gfc_add_expr_to_block (block, tmp);
   12504                 :             : 
   12505                 :             :       /* Update the lhs character length.  */
   12506                 :        1345 :       size = string_length;
   12507                 :        1345 :       gfc_add_modify (block, lse.string_length,
   12508                 :        1345 :                       fold_convert (TREE_TYPE (lse.string_length), size));
   12509                 :             :     }
   12510                 :             : }
   12511                 :             : 
   12512                 :             : /* Check for assignments of the type
   12513                 :             : 
   12514                 :             :    a = a + 4
   12515                 :             : 
   12516                 :             :    to make sure we do not check for reallocation unneccessarily.  */
   12517                 :             : 
   12518                 :             : 
   12519                 :             : static bool
   12520                 :        6388 : is_runtime_conformable (gfc_expr *expr1, gfc_expr *expr2)
   12521                 :             : {
   12522                 :        6683 :   gfc_actual_arglist *a;
   12523                 :        6683 :   gfc_expr *e1, *e2;
   12524                 :             : 
   12525                 :        6683 :   switch (expr2->expr_type)
   12526                 :             :     {
   12527                 :        1765 :     case EXPR_VARIABLE:
   12528                 :        1765 :       return gfc_dep_compare_expr (expr1, expr2) == 0;
   12529                 :             : 
   12530                 :        2758 :     case EXPR_FUNCTION:
   12531                 :        2758 :       if (expr2->value.function.esym
   12532                 :         267 :           && expr2->value.function.esym->attr.elemental)
   12533                 :             :         {
   12534                 :          51 :           for (a = expr2->value.function.actual; a != NULL; a = a->next)
   12535                 :             :             {
   12536                 :          50 :               e1 = a->expr;
   12537                 :          50 :               if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
   12538                 :             :                 return false;
   12539                 :             :             }
   12540                 :             :           return true;
   12541                 :             :         }
   12542                 :        2720 :       else if (expr2->value.function.isym
   12543                 :        2477 :                && expr2->value.function.isym->elemental)
   12544                 :             :         {
   12545                 :         324 :           for (a = expr2->value.function.actual; a != NULL; a = a->next)
   12546                 :             :             {
   12547                 :         314 :               e1 = a->expr;
   12548                 :         314 :               if (e1 && e1->rank > 0 && !is_runtime_conformable (expr1, e1))
   12549                 :             :                 return false;
   12550                 :             :             }
   12551                 :             :           return true;
   12552                 :             :         }
   12553                 :             : 
   12554                 :             :       break;
   12555                 :             : 
   12556                 :         489 :     case EXPR_OP:
   12557                 :         489 :       switch (expr2->value.op.op)
   12558                 :             :         {
   12559                 :          32 :         case INTRINSIC_NOT:
   12560                 :          32 :         case INTRINSIC_UPLUS:
   12561                 :          32 :         case INTRINSIC_UMINUS:
   12562                 :          32 :         case INTRINSIC_PARENTHESES:
   12563                 :          32 :           return is_runtime_conformable (expr1, expr2->value.op.op1);
   12564                 :             : 
   12565                 :         432 :         case INTRINSIC_PLUS:
   12566                 :         432 :         case INTRINSIC_MINUS:
   12567                 :         432 :         case INTRINSIC_TIMES:
   12568                 :         432 :         case INTRINSIC_DIVIDE:
   12569                 :         432 :         case INTRINSIC_POWER:
   12570                 :         432 :         case INTRINSIC_AND:
   12571                 :         432 :         case INTRINSIC_OR:
   12572                 :         432 :         case INTRINSIC_EQV:
   12573                 :         432 :         case INTRINSIC_NEQV:
   12574                 :         432 :         case INTRINSIC_EQ:
   12575                 :         432 :         case INTRINSIC_NE:
   12576                 :         432 :         case INTRINSIC_GT:
   12577                 :         432 :         case INTRINSIC_GE:
   12578                 :         432 :         case INTRINSIC_LT:
   12579                 :         432 :         case INTRINSIC_LE:
   12580                 :         432 :         case INTRINSIC_EQ_OS:
   12581                 :         432 :         case INTRINSIC_NE_OS:
   12582                 :         432 :         case INTRINSIC_GT_OS:
   12583                 :         432 :         case INTRINSIC_GE_OS:
   12584                 :         432 :         case INTRINSIC_LT_OS:
   12585                 :         432 :         case INTRINSIC_LE_OS:
   12586                 :             : 
   12587                 :         432 :           e1 = expr2->value.op.op1;
   12588                 :         432 :           e2 = expr2->value.op.op2;
   12589                 :             : 
   12590                 :         432 :           if (e1->rank == 0 && e2->rank > 0)
   12591                 :             :             return is_runtime_conformable (expr1, e2);
   12592                 :         380 :           else if (e1->rank > 0 && e2->rank == 0)
   12593                 :             :             return is_runtime_conformable (expr1, e1);
   12594                 :         169 :           else if (e1->rank > 0 && e2->rank > 0)
   12595                 :         169 :             return is_runtime_conformable (expr1, e1)
   12596                 :         169 :               && is_runtime_conformable (expr1, e2);
   12597                 :             :           break;
   12598                 :             : 
   12599                 :             :         default:
   12600                 :             :           break;
   12601                 :             : 
   12602                 :             :         }
   12603                 :             : 
   12604                 :             :       break;
   12605                 :             : 
   12606                 :             :     default:
   12607                 :             :       break;
   12608                 :             :     }
   12609                 :             :   return false;
   12610                 :             : }
   12611                 :             : 
   12612                 :             : 
   12613                 :             : static tree
   12614                 :        3176 : trans_class_assignment (stmtblock_t *block, gfc_expr *lhs, gfc_expr *rhs,
   12615                 :             :                         gfc_se *lse, gfc_se *rse, bool use_vptr_copy,
   12616                 :             :                         bool class_realloc)
   12617                 :             : {
   12618                 :        3176 :   tree tmp, fcn, stdcopy, to_len, from_len, vptr, old_vptr, rhs_vptr;
   12619                 :        3176 :   vec<tree, va_gc> *args = NULL;
   12620                 :        3176 :   bool final_expr;
   12621                 :             : 
   12622                 :        3176 :   final_expr = gfc_assignment_finalizer_call (lse, lhs, false);
   12623                 :        3176 :   if (final_expr)
   12624                 :             :     {
   12625                 :         431 :       if (rse->loop)
   12626                 :         172 :         gfc_prepend_expr_to_block (&rse->loop->pre,
   12627                 :             :                                    gfc_finish_block (&lse->finalblock));
   12628                 :             :       else
   12629                 :         259 :         gfc_add_block_to_block (block, &lse->finalblock);
   12630                 :             :     }
   12631                 :             : 
   12632                 :             :   /* Store the old vptr so that dynamic types can be compared for
   12633                 :             :      reallocation to occur or not.  */
   12634                 :        3176 :   if (class_realloc)
   12635                 :             :     {
   12636                 :         271 :       tmp = lse->expr;
   12637                 :         271 :       if (!GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
   12638                 :          18 :         tmp = gfc_get_class_from_expr (tmp);
   12639                 :             :     }
   12640                 :             : 
   12641                 :        3176 :   vptr = trans_class_vptr_len_assignment (block, lhs, rhs, rse, &to_len,
   12642                 :             :                                           &from_len, &rhs_vptr);
   12643                 :        3176 :   if (rhs_vptr == NULL_TREE)
   12644                 :          61 :     rhs_vptr = vptr;
   12645                 :             : 
   12646                 :             :   /* Generate (re)allocation of the lhs.  */
   12647                 :        3176 :   if (class_realloc)
   12648                 :             :     {
   12649                 :         271 :       stmtblock_t alloc, re_alloc;
   12650                 :         271 :       tree class_han, re, size;
   12651                 :             : 
   12652                 :         271 :       if (tmp && GFC_CLASS_TYPE_P (TREE_TYPE (tmp)))
   12653                 :         253 :         old_vptr = gfc_evaluate_now (gfc_class_vptr_get (tmp), block);
   12654                 :             :       else
   12655                 :          18 :         old_vptr = build_int_cst (TREE_TYPE (vptr), 0);
   12656                 :             : 
   12657                 :         271 :       size = gfc_vptr_size_get (rhs_vptr);
   12658                 :             : 
   12659                 :             :       /* Take into account _len of unlimited polymorphic entities.
   12660                 :             :          TODO: handle class(*) allocatable function results on rhs.  */
   12661                 :         271 :       if (UNLIMITED_POLY (rhs))
   12662                 :             :         {
   12663                 :          18 :           tree len;
   12664                 :          18 :           if (rhs->expr_type == EXPR_VARIABLE)
   12665                 :          12 :             len = trans_get_upoly_len (block, rhs);
   12666                 :             :           else
   12667                 :           6 :             len = gfc_class_len_get (tmp);
   12668                 :          18 :           len = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
   12669                 :             :                                  fold_convert (size_type_node, len),
   12670                 :             :                                  size_one_node);
   12671                 :          18 :           size = fold_build2_loc (input_location, MULT_EXPR, TREE_TYPE (size),
   12672                 :          18 :                                   size, fold_convert (TREE_TYPE (size), len));
   12673                 :          18 :         }
   12674                 :         253 :       else if (rhs->ts.type == BT_CHARACTER && rse->string_length)
   12675                 :          21 :         size = fold_build2_loc (input_location, MULT_EXPR,
   12676                 :             :                                 gfc_charlen_type_node, size,
   12677                 :             :                                 rse->string_length);
   12678                 :             : 
   12679                 :             : 
   12680                 :         271 :       tmp = lse->expr;
   12681                 :         271 :       class_han = GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
   12682                 :         271 :           ? gfc_class_data_get (tmp) : tmp;
   12683                 :             : 
   12684                 :         271 :       if (!POINTER_TYPE_P (TREE_TYPE (class_han)))
   12685                 :          18 :         class_han = gfc_build_addr_expr (NULL_TREE, class_han);
   12686                 :             : 
   12687                 :             :       /* Allocate block.  */
   12688                 :         271 :       gfc_init_block (&alloc);
   12689                 :         271 :       gfc_allocate_using_malloc (&alloc, class_han, size, NULL_TREE);
   12690                 :             : 
   12691                 :             :       /* Reallocate if dynamic types are different. */
   12692                 :         271 :       gfc_init_block (&re_alloc);
   12693                 :         271 :       if (UNLIMITED_POLY (lhs) && rhs->ts.type == BT_CHARACTER)
   12694                 :             :         {
   12695                 :          21 :           gfc_add_expr_to_block (&re_alloc, gfc_call_free (class_han));
   12696                 :          21 :           gfc_allocate_using_malloc (&re_alloc, class_han, size, NULL_TREE);
   12697                 :             :         }
   12698                 :             :       else
   12699                 :             :         {
   12700                 :         250 :           tmp = fold_convert (pvoid_type_node, class_han);
   12701                 :         250 :           re = build_call_expr_loc (input_location,
   12702                 :             :                                     builtin_decl_explicit (BUILT_IN_REALLOC),
   12703                 :             :                                     2, tmp, size);
   12704                 :         250 :           re = fold_build2_loc (input_location, MODIFY_EXPR, TREE_TYPE (tmp),
   12705                 :             :                                 tmp, re);
   12706                 :         250 :           tmp = fold_build2_loc (input_location, NE_EXPR,
   12707                 :             :                                  logical_type_node, rhs_vptr, old_vptr);
   12708                 :         250 :           re = fold_build3_loc (input_location, COND_EXPR, void_type_node,
   12709                 :             :                                 tmp, re, build_empty_stmt (input_location));
   12710                 :         250 :           gfc_add_expr_to_block (&re_alloc, re);
   12711                 :             :         }
   12712                 :         271 :       tree realloc_expr = lhs->ts.type == BT_CLASS ?
   12713                 :         253 :                                           gfc_finish_block (&re_alloc) :
   12714                 :          18 :                                           build_empty_stmt (input_location);
   12715                 :             : 
   12716                 :             :       /* Allocate if _data is NULL, reallocate otherwise.  */
   12717                 :         271 :       tmp = fold_build2_loc (input_location, EQ_EXPR,
   12718                 :             :                              logical_type_node, class_han,
   12719                 :         271 :                              build_int_cst (prvoid_type_node, 0));
   12720                 :         271 :       tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
   12721                 :             :                              gfc_unlikely (tmp,
   12722                 :             :                                            PRED_FORTRAN_FAIL_ALLOC),
   12723                 :             :                              gfc_finish_block (&alloc),
   12724                 :             :                              realloc_expr);
   12725                 :         271 :       gfc_add_expr_to_block (&lse->pre, tmp);
   12726                 :             :     }
   12727                 :             : 
   12728                 :        3176 :   fcn = gfc_vptr_copy_get (vptr);
   12729                 :             : 
   12730                 :        3176 :   tmp = GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr))
   12731                 :        3176 :       ? gfc_class_data_get (rse->expr) : rse->expr;
   12732                 :        3176 :   if (use_vptr_copy)
   12733                 :             :     {
   12734                 :        5348 :       if (!POINTER_TYPE_P (TREE_TYPE (tmp))
   12735                 :         518 :           || INDIRECT_REF_P (tmp)
   12736                 :         397 :           || (rhs->ts.type == BT_DERIVED
   12737                 :           0 :               && rhs->ts.u.derived->attr.unlimited_polymorphic
   12738                 :             :               && !rhs->ts.u.derived->attr.pointer
   12739                 :           0 :               && !rhs->ts.u.derived->attr.allocatable)
   12740                 :        3327 :           || (UNLIMITED_POLY (rhs)
   12741                 :             :               && !CLASS_DATA (rhs)->attr.pointer
   12742                 :         134 :               && !CLASS_DATA (rhs)->attr.allocatable))
   12743                 :        2533 :         vec_safe_push (args, gfc_build_addr_expr (NULL_TREE, tmp));
   12744                 :             :       else
   12745                 :         397 :         vec_safe_push (args, tmp);
   12746                 :        2930 :       tmp = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
   12747                 :        2930 :           ? gfc_class_data_get (lse->expr) : lse->expr;
   12748                 :        5164 :       if (!POINTER_TYPE_P (TREE_TYPE (tmp))
   12749                 :         696 :           || INDIRECT_REF_P (tmp)
   12750                 :         253 :           || (lhs->ts.type == BT_DERIVED
   12751                 :           0 :               && lhs->ts.u.derived->attr.unlimited_polymorphic
   12752                 :             :               && !lhs->ts.u.derived->attr.pointer
   12753                 :           0 :               && !lhs->ts.u.derived->attr.allocatable)
   12754                 :        3183 :           || (UNLIMITED_POLY (lhs)
   12755                 :             :               && !CLASS_DATA (lhs)->attr.pointer
   12756                 :          95 :               && !CLASS_DATA (lhs)->attr.allocatable))
   12757                 :        2677 :         vec_safe_push (args, gfc_build_addr_expr (NULL_TREE, tmp));
   12758                 :             :       else
   12759                 :         253 :         vec_safe_push (args, tmp);
   12760                 :             : 
   12761                 :        2930 :       stdcopy = build_call_vec (TREE_TYPE (TREE_TYPE (fcn)), fcn, args);
   12762                 :             : 
   12763                 :        2930 :       if (to_len != NULL_TREE && !integer_zerop (from_len))
   12764                 :             :         {
   12765                 :         400 :           tree extcopy;
   12766                 :         400 :           vec_safe_push (args, from_len);
   12767                 :         400 :           vec_safe_push (args, to_len);
   12768                 :         400 :           extcopy = build_call_vec (TREE_TYPE (TREE_TYPE (fcn)), fcn, args);
   12769                 :             : 
   12770                 :         400 :           tmp = fold_build2_loc (input_location, GT_EXPR,
   12771                 :             :                                  logical_type_node, from_len,
   12772                 :         400 :                                  build_zero_cst (TREE_TYPE (from_len)));
   12773                 :         400 :           return fold_build3_loc (input_location, COND_EXPR,
   12774                 :             :                                   void_type_node, tmp,
   12775                 :         400 :                                   extcopy, stdcopy);
   12776                 :             :         }
   12777                 :             :       else
   12778                 :        2530 :         return stdcopy;
   12779                 :             :     }
   12780                 :             :   else
   12781                 :             :     {
   12782                 :         246 :       tree rhst = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
   12783                 :         246 :           ? gfc_class_data_get (lse->expr) : lse->expr;
   12784                 :         246 :       stmtblock_t tblock;
   12785                 :         246 :       gfc_init_block (&tblock);
   12786                 :         246 :       if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
   12787                 :           0 :         tmp = gfc_build_addr_expr (NULL_TREE, tmp);
   12788                 :         246 :       if (!POINTER_TYPE_P (TREE_TYPE (rhst)))
   12789                 :           0 :         rhst = gfc_build_addr_expr (NULL_TREE, rhst);
   12790                 :             :       /* When coming from a ptr_copy lhs and rhs are swapped.  */
   12791                 :         246 :       gfc_add_modify_loc (input_location, &tblock, rhst,
   12792                 :         246 :                           fold_convert (TREE_TYPE (rhst), tmp));
   12793                 :         246 :       return gfc_finish_block (&tblock);
   12794                 :             :     }
   12795                 :             : }
   12796                 :             : 
   12797                 :             : bool
   12798                 :      293868 : is_assoc_assign (gfc_expr *lhs, gfc_expr *rhs)
   12799                 :             : {
   12800                 :      293868 :   if (lhs->expr_type != EXPR_VARIABLE || rhs->expr_type != EXPR_VARIABLE)
   12801                 :             :     return false;
   12802                 :             : 
   12803                 :       30006 :   return lhs->symtree->n.sym->assoc
   12804                 :       30006 :          && lhs->symtree->n.sym->assoc->target == rhs;
   12805                 :             : }
   12806                 :             : 
   12807                 :             : /* Subroutine of gfc_trans_assignment that actually scalarizes the
   12808                 :             :    assignment.  EXPR1 is the destination/LHS and EXPR2 is the source/RHS.
   12809                 :             :    init_flag indicates initialization expressions and dealloc that no
   12810                 :             :    deallocate prior assignment is needed (if in doubt, set true).
   12811                 :             :    When PTR_COPY is set and expr1 is a class type, then use the _vptr-copy
   12812                 :             :    routine instead of a pointer assignment.  Alias resolution is only done,
   12813                 :             :    when MAY_ALIAS is set (the default).  This flag is used by ALLOCATE()
   12814                 :             :    where it is known, that newly allocated memory on the lhs can never be
   12815                 :             :    an alias of the rhs.  */
   12816                 :             : 
   12817                 :             : static tree
   12818                 :      293868 : gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
   12819                 :             :                         bool dealloc, bool use_vptr_copy, bool may_alias)
   12820                 :             : {
   12821                 :      293868 :   gfc_se lse;
   12822                 :      293868 :   gfc_se rse;
   12823                 :      293868 :   gfc_ss *lss;
   12824                 :      293868 :   gfc_ss *lss_section;
   12825                 :      293868 :   gfc_ss *rss;
   12826                 :      293868 :   gfc_loopinfo loop;
   12827                 :      293868 :   tree tmp;
   12828                 :      293868 :   stmtblock_t block;
   12829                 :      293868 :   stmtblock_t body;
   12830                 :      293868 :   bool final_expr;
   12831                 :      293868 :   bool l_is_temp;
   12832                 :      293868 :   bool scalar_to_array;
   12833                 :      293868 :   tree string_length;
   12834                 :      293868 :   int n;
   12835                 :      293868 :   bool maybe_workshare = false, lhs_refs_comp = false, rhs_refs_comp = false;
   12836                 :      293868 :   symbol_attribute lhs_caf_attr, rhs_caf_attr, lhs_attr;
   12837                 :      293868 :   bool is_poly_assign;
   12838                 :      293868 :   bool realloc_flag;
   12839                 :      293868 :   bool assoc_assign = false;
   12840                 :             : 
   12841                 :             :   /* Assignment of the form lhs = rhs.  */
   12842                 :      293868 :   gfc_start_block (&block);
   12843                 :             : 
   12844                 :      293868 :   gfc_init_se (&lse, NULL);
   12845                 :      293868 :   gfc_init_se (&rse, NULL);
   12846                 :             : 
   12847                 :             :   /* Walk the lhs.  */
   12848                 :      293868 :   lss = gfc_walk_expr (expr1);
   12849                 :      293868 :   if (gfc_is_reallocatable_lhs (expr1))
   12850                 :             :     {
   12851                 :       10323 :       lss->no_bounds_check = 1;
   12852                 :       10323 :       if (!(expr2->expr_type == EXPR_FUNCTION
   12853                 :        2575 :             && expr2->value.function.isym != NULL
   12854                 :        2288 :             && !(expr2->value.function.isym->elemental
   12855                 :             :                  || expr2->value.function.isym->conversion)))
   12856                 :        8320 :         lss->is_alloc_lhs = 1;
   12857                 :             :     }
   12858                 :             :   else
   12859                 :      283545 :     lss->no_bounds_check = expr1->no_bounds_check;
   12860                 :             : 
   12861                 :      293868 :   rss = NULL;
   12862                 :             : 
   12863                 :      293868 :   if (expr2->expr_type != EXPR_VARIABLE
   12864                 :      293868 :       && expr2->expr_type != EXPR_CONSTANT
   12865                 :      293868 :       && (expr2->ts.type == BT_CLASS || gfc_may_be_finalized (expr2->ts)))
   12866                 :             :     {
   12867                 :         676 :       expr2->must_finalize = 1;
   12868                 :             :       /* F2023 7.5.6.3: If an executable construct references a nonpointer
   12869                 :             :          function, the result is finalized after execution of the innermost
   12870                 :             :          executable construct containing the reference.  */
   12871                 :         676 :       if (expr2->expr_type == EXPR_FUNCTION
   12872                 :         676 :           && (gfc_expr_attr (expr2).pointer
   12873                 :         291 :               || (expr2->ts.type == BT_CLASS && CLASS_DATA (expr2)->attr.class_pointer)))
   12874                 :         133 :         expr2->must_finalize = 0;
   12875                 :             :       /* F2008 4.5.6.3 para 5: If an executable construct references a
   12876                 :             :          structure constructor or array constructor, the entity created by
   12877                 :             :          the constructor is finalized after execution of the innermost
   12878                 :             :          executable construct containing the reference.
   12879                 :             :          These finalizations were later deleted by the Combined Techical
   12880                 :             :          Corrigenda 1 TO 4 for fortran 2008 (f08/0011).  */
   12881                 :         543 :       else if (gfc_notification_std (GFC_STD_F2018_DEL)
   12882                 :         543 :           && (expr2->expr_type == EXPR_STRUCTURE
   12883                 :         500 :               || expr2->expr_type == EXPR_ARRAY))
   12884                 :         220 :         expr2->must_finalize = 0;
   12885                 :             :     }
   12886                 :             : 
   12887                 :             : 
   12888                 :             :   /* Checking whether a class assignment is desired is quite complicated and
   12889                 :             :      needed at two locations, so do it once only before the information is
   12890                 :             :      needed.  */
   12891                 :      293868 :   lhs_attr = gfc_expr_attr (expr1);
   12892                 :             : 
   12893                 :      293868 :   is_poly_assign
   12894                 :      293868 :     = (use_vptr_copy
   12895                 :      278865 :        || ((lhs_attr.pointer || lhs_attr.allocatable) && !lhs_attr.dimension))
   12896                 :       20344 :       && (expr1->ts.type == BT_CLASS || gfc_is_class_array_ref (expr1, NULL)
   12897                 :       18415 :           || gfc_is_class_scalar_expr (expr1)
   12898                 :       17125 :           || gfc_is_class_array_ref (expr2, NULL)
   12899                 :       17125 :           || gfc_is_class_scalar_expr (expr2))
   12900                 :      297105 :       && lhs_attr.flavor != FL_PROCEDURE;
   12901                 :             : 
   12902                 :      293868 :   assoc_assign = is_assoc_assign (expr1, expr2);
   12903                 :             : 
   12904                 :      587736 :   realloc_flag = flag_realloc_lhs
   12905                 :      288166 :                  && gfc_is_reallocatable_lhs (expr1)
   12906                 :        7167 :                  && expr2->rank
   12907                 :      299628 :                  && !is_runtime_conformable (expr1, expr2);
   12908                 :             : 
   12909                 :             :   /* Only analyze the expressions for coarray properties, when in coarray-lib
   12910                 :             :      mode.  Avoid false-positive uninitialized diagnostics with initializing
   12911                 :             :      the codimension flag unconditionally.  */
   12912                 :      293868 :   lhs_caf_attr.codimension = false;
   12913                 :      293868 :   rhs_caf_attr.codimension = false;
   12914                 :      293868 :   if (flag_coarray == GFC_FCOARRAY_LIB)
   12915                 :             :     {
   12916                 :        4283 :       lhs_caf_attr = gfc_caf_attr (expr1, false, &lhs_refs_comp);
   12917                 :        4283 :       rhs_caf_attr = gfc_caf_attr (expr2, false, &rhs_refs_comp);
   12918                 :             :     }
   12919                 :             : 
   12920                 :      293868 :   if (lss != gfc_ss_terminator)
   12921                 :             :     {
   12922                 :             :       /* The assignment needs scalarization.  */
   12923                 :             :       lss_section = lss;
   12924                 :             : 
   12925                 :             :       /* Find a non-scalar SS from the lhs.  */
   12926                 :             :       while (lss_section != gfc_ss_terminator
   12927                 :       37206 :              && lss_section->info->type != GFC_SS_SECTION)
   12928                 :           0 :         lss_section = lss_section->next;
   12929                 :             : 
   12930                 :       37206 :       gcc_assert (lss_section != gfc_ss_terminator);
   12931                 :             : 
   12932                 :             :       /* Initialize the scalarizer.  */
   12933                 :       37206 :       gfc_init_loopinfo (&loop);
   12934                 :             : 
   12935                 :             :       /* Walk the rhs.  */
   12936                 :       37206 :       rss = gfc_walk_expr (expr2);
   12937                 :       37206 :       if (rss == gfc_ss_terminator)
   12938                 :             :         /* The rhs is scalar.  Add a ss for the expression.  */
   12939                 :       13871 :         rss = gfc_get_scalar_ss (gfc_ss_terminator, expr2);
   12940                 :             :       /* When doing a class assign, then the handle to the rhs needs to be a
   12941                 :             :          pointer to allow for polymorphism.  */
   12942                 :       37206 :       if (is_poly_assign && expr2->rank == 0 && !UNLIMITED_POLY (expr2))
   12943                 :         484 :         rss->info->type = GFC_SS_REFERENCE;
   12944                 :             : 
   12945                 :       37206 :       rss->no_bounds_check = expr2->no_bounds_check;
   12946                 :             :       /* Associate the SS with the loop.  */
   12947                 :       37206 :       gfc_add_ss_to_loop (&loop, lss);
   12948                 :       37206 :       gfc_add_ss_to_loop (&loop, rss);
   12949                 :             : 
   12950                 :             :       /* Calculate the bounds of the scalarization.  */
   12951                 :       37206 :       gfc_conv_ss_startstride (&loop);
   12952                 :             :       /* Enable loop reversal.  */
   12953                 :      632502 :       for (n = 0; n < GFC_MAX_DIMENSIONS; n++)
   12954                 :      558090 :         loop.reverse[n] = GFC_ENABLE_REVERSE;
   12955                 :             :       /* Resolve any data dependencies in the statement.  */
   12956                 :       37206 :       if (may_alias)
   12957                 :       35038 :         gfc_conv_resolve_dependencies (&loop, lss, rss);
   12958                 :             :       /* Setup the scalarizing loops.  */
   12959                 :       37206 :       gfc_conv_loop_setup (&loop, &expr2->where);
   12960                 :             : 
   12961                 :             :       /* Setup the gfc_se structures.  */
   12962                 :       37206 :       gfc_copy_loopinfo_to_se (&lse, &loop);
   12963                 :       37206 :       gfc_copy_loopinfo_to_se (&rse, &loop);
   12964                 :             : 
   12965                 :       37206 :       rse.ss = rss;
   12966                 :       37206 :       gfc_mark_ss_chain_used (rss, 1);
   12967                 :       37206 :       if (loop.temp_ss == NULL)
   12968                 :             :         {
   12969                 :       36223 :           lse.ss = lss;
   12970                 :       36223 :           gfc_mark_ss_chain_used (lss, 1);
   12971                 :             :         }
   12972                 :             :       else
   12973                 :             :         {
   12974                 :         983 :           lse.ss = loop.temp_ss;
   12975                 :         983 :           gfc_mark_ss_chain_used (lss, 3);
   12976                 :         983 :           gfc_mark_ss_chain_used (loop.temp_ss, 3);
   12977                 :             :         }
   12978                 :             : 
   12979                 :             :       /* Allow the scalarizer to workshare array assignments.  */
   12980                 :       37206 :       if ((ompws_flags & (OMPWS_WORKSHARE_FLAG | OMPWS_SCALARIZER_BODY))
   12981                 :             :           == OMPWS_WORKSHARE_FLAG
   12982                 :          85 :           && loop.temp_ss == NULL)
   12983                 :             :         {
   12984                 :          73 :           maybe_workshare = true;
   12985                 :          73 :           ompws_flags |= OMPWS_SCALARIZER_WS | OMPWS_SCALARIZER_BODY;
   12986                 :             :         }
   12987                 :             : 
   12988                 :             :       /* Start the scalarized loop body.  */
   12989                 :       37206 :       gfc_start_scalarized_body (&loop, &body);
   12990                 :             :     }
   12991                 :             :   else
   12992                 :      256662 :     gfc_init_block (&body);
   12993                 :             : 
   12994                 :      293868 :   l_is_temp = (lss != gfc_ss_terminator && loop.temp_ss != NULL);
   12995                 :             : 
   12996                 :             :   /* Translate the expression.  */
   12997                 :      587736 :   rse.want_coarray = flag_coarray == GFC_FCOARRAY_LIB
   12998                 :      293868 :                      && (init_flag || assoc_assign) && lhs_caf_attr.codimension;
   12999                 :      293868 :   rse.want_pointer = rse.want_coarray && !init_flag && !lhs_caf_attr.dimension;
   13000                 :      293868 :   gfc_conv_expr (&rse, expr2);
   13001                 :             : 
   13002                 :             :   /* Deal with the case of a scalar class function assigned to a derived type.
   13003                 :             :    */
   13004                 :      293868 :   if (gfc_is_alloc_class_scalar_function (expr2)
   13005                 :      293868 :       && expr1->ts.type == BT_DERIVED)
   13006                 :             :     {
   13007                 :          60 :       rse.expr = gfc_class_data_get (rse.expr);
   13008                 :          60 :       rse.expr = build_fold_indirect_ref_loc (input_location, rse.expr);
   13009                 :             :     }
   13010                 :             : 
   13011                 :             :   /* Stabilize a string length for temporaries.  */
   13012                 :      293868 :   if (expr2->ts.type == BT_CHARACTER && !expr1->ts.deferred
   13013                 :       22962 :       && !(VAR_P (rse.string_length)
   13014                 :             :            || TREE_CODE (rse.string_length) == PARM_DECL
   13015                 :             :            || INDIRECT_REF_P (rse.string_length)))
   13016                 :       22193 :     string_length = gfc_evaluate_now (rse.string_length, &rse.pre);
   13017                 :      271675 :   else if (expr2->ts.type == BT_CHARACTER)
   13018                 :             :     {
   13019                 :        3843 :       if (expr1->ts.deferred
   13020                 :        6001 :           && gfc_expr_attr (expr1).allocatable
   13021                 :        6115 :           && gfc_check_dependency (expr1, expr2, true))
   13022                 :         114 :         rse.string_length =
   13023                 :         114 :           gfc_evaluate_now_function_scope (rse.string_length, &rse.pre);
   13024                 :        3843 :       string_length = rse.string_length;
   13025                 :             :     }
   13026                 :             :   else
   13027                 :             :     string_length = NULL_TREE;
   13028                 :             : 
   13029                 :      293868 :   if (l_is_temp)
   13030                 :             :     {
   13031                 :         983 :       gfc_conv_tmp_array_ref (&lse);
   13032                 :         983 :       if (expr2->ts.type == BT_CHARACTER)
   13033                 :         123 :         lse.string_length = string_length;
   13034                 :             :     }
   13035                 :             :   else
   13036                 :             :     {
   13037                 :      292885 :       gfc_conv_expr (&lse, expr1);
   13038                 :             :       /* For some expression (e.g. complex numbers) fold_convert uses a
   13039                 :             :          SAVE_EXPR, which is hazardous on the lhs, because the value is
   13040                 :             :          not updated when assigned to.  */
   13041                 :      292885 :       if (TREE_CODE (lse.expr) == SAVE_EXPR)
   13042                 :           5 :         lse.expr = TREE_OPERAND (lse.expr, 0);
   13043                 :             : 
   13044                 :        6138 :       if (gfc_option.rtcheck & GFC_RTCHECK_MEM && !init_flag
   13045                 :      299023 :           && gfc_expr_attr (expr1).allocatable && expr1->rank && !expr2->rank)
   13046                 :             :         {
   13047                 :          36 :           tree cond;
   13048                 :          36 :           const char* msg;
   13049                 :             : 
   13050                 :          72 :           tmp = INDIRECT_REF_P (lse.expr)
   13051                 :          36 :               ? gfc_build_addr_expr (NULL_TREE, lse.expr) : lse.expr;
   13052                 :          36 :           STRIP_NOPS (tmp);
   13053                 :             : 
   13054                 :             :           /* We should only get array references here.  */
   13055                 :          36 :           gcc_assert (TREE_CODE (tmp) == POINTER_PLUS_EXPR
   13056                 :             :                       || TREE_CODE (tmp) == ARRAY_REF);
   13057                 :             : 
   13058                 :             :           /* 'tmp' is either the pointer to the array(POINTER_PLUS_EXPR)
   13059                 :             :              or the array itself(ARRAY_REF).  */
   13060                 :          36 :           tmp = TREE_OPERAND (tmp, 0);
   13061                 :             : 
   13062                 :             :           /* Provide the address of the array.  */
   13063                 :          36 :           if (TREE_CODE (lse.expr) == ARRAY_REF)
   13064                 :          18 :             tmp = gfc_build_addr_expr (NULL_TREE, tmp);
   13065                 :             : 
   13066                 :          36 :           cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
   13067                 :          36 :                                   tmp, build_int_cst (TREE_TYPE (tmp), 0));
   13068                 :          36 :           msg = _("Assignment of scalar to unallocated array");
   13069                 :          36 :           gfc_trans_runtime_check (true, false, cond, &loop.pre,
   13070                 :             :                                    &expr1->where, msg);
   13071                 :             :         }
   13072                 :             : 
   13073                 :             :       /* Deallocate the lhs parameterized components if required.  */
   13074                 :      292885 :       if (dealloc && expr2->expr_type == EXPR_FUNCTION
   13075                 :       34610 :           && !expr1->symtree->n.sym->attr.associate_var)
   13076                 :             :         {
   13077                 :       34527 :           if (expr1->ts.type == BT_DERIVED
   13078                 :        1918 :               && expr1->ts.u.derived
   13079                 :        1918 :               && expr1->ts.u.derived->attr.pdt_type)
   13080                 :             :             {
   13081                 :          12 :               tmp = gfc_deallocate_pdt_comp (expr1->ts.u.derived, lse.expr,
   13082                 :             :                                              expr1->rank);
   13083                 :          12 :               gfc_add_expr_to_block (&lse.pre, tmp);
   13084                 :             :             }
   13085                 :       34515 :           else if (expr1->ts.type == BT_CLASS
   13086                 :         337 :                    && CLASS_DATA (expr1)->ts.u.derived
   13087                 :         337 :                    && CLASS_DATA (expr1)->ts.u.derived->attr.pdt_type)
   13088                 :             :             {
   13089                 :           0 :               tmp = gfc_class_data_get (lse.expr);
   13090                 :           0 :               tmp = gfc_deallocate_pdt_comp (CLASS_DATA (expr1)->ts.u.derived,
   13091                 :             :                                              tmp, expr1->rank);
   13092                 :           0 :               gfc_add_expr_to_block (&lse.pre, tmp);
   13093                 :             :             }
   13094                 :             :         }
   13095                 :             :     }
   13096                 :             : 
   13097                 :             :   /* Assignments of scalar derived types with allocatable components
   13098                 :             :      to arrays must be done with a deep copy and the rhs temporary
   13099                 :             :      must have its components deallocated afterwards.  */
   13100                 :      587736 :   scalar_to_array = (expr2->ts.type == BT_DERIVED
   13101                 :       17006 :                        && expr2->ts.u.derived->attr.alloc_comp
   13102                 :        5208 :                        && !gfc_expr_is_variable (expr2)
   13103                 :      296578 :                        && expr1->rank && !expr2->rank);
   13104                 :      587736 :   scalar_to_array |= (expr1->ts.type == BT_DERIVED
   13105                 :       17292 :                                     && expr1->rank
   13106                 :        3295 :                                     && expr1->ts.u.derived->attr.alloc_comp
   13107                 :      294969 :                                     && gfc_is_alloc_class_scalar_function (expr2));
   13108                 :      293868 :   if (scalar_to_array && dealloc)
   13109                 :             :     {
   13110                 :          50 :       tmp = gfc_deallocate_alloc_comp_no_caf (expr2->ts.u.derived, rse.expr, 0);
   13111                 :          50 :       gfc_prepend_expr_to_block (&loop.post, tmp);
   13112                 :             :     }
   13113                 :             : 
   13114                 :             :   /* When assigning a character function result to a deferred-length variable,
   13115                 :             :      the function call must happen before the (re)allocation of the lhs -
   13116                 :             :      otherwise the character length of the result is not known.
   13117                 :             :      NOTE 1: This relies on having the exact dependence of the length type
   13118                 :             :      parameter available to the caller; gfortran saves it in the .mod files.
   13119                 :             :      NOTE 2: Vector array references generate an index temporary that must
   13120                 :             :      not go outside the loop. Otherwise, variables should not generate
   13121                 :             :      a pre block.
   13122                 :             :      NOTE 3: The concatenation operation generates a temporary pointer,
   13123                 :             :      whose allocation must go to the innermost loop.
   13124                 :             :      NOTE 4: Elemental functions may generate a temporary, too.  */
   13125                 :      293868 :   if (flag_realloc_lhs
   13126                 :      288166 :       && expr2->ts.type == BT_CHARACTER && expr1->ts.deferred
   13127                 :        2580 :       && !(lss != gfc_ss_terminator
   13128                 :         743 :            && rss != gfc_ss_terminator
   13129                 :         743 :            && ((expr2->expr_type == EXPR_VARIABLE && expr2->rank)
   13130                 :         577 :                || (expr2->expr_type == EXPR_FUNCTION
   13131                 :         126 :                    && expr2->value.function.esym != NULL
   13132                 :          26 :                    && expr2->value.function.esym->attr.elemental)
   13133                 :         564 :                || (expr2->expr_type == EXPR_FUNCTION
   13134                 :         113 :                    && expr2->value.function.isym != NULL
   13135                 :         100 :                    && expr2->value.function.isym->elemental)
   13136                 :         542 :                || (expr2->expr_type == EXPR_OP
   13137                 :          31 :                    && expr2->value.op.op == INTRINSIC_CONCAT))))
   13138                 :        2354 :     gfc_add_block_to_block (&block, &rse.pre);
   13139                 :             : 
   13140                 :             :   /* Nullify the allocatable components corresponding to those of the lhs
   13141                 :             :      derived type, so that the finalization of the function result does not
   13142                 :             :      affect the lhs of the assignment. Prepend is used to ensure that the
   13143                 :             :      nullification occurs before the call to the finalizer. In the case of
   13144                 :             :      a scalar to array assignment, this is done in gfc_trans_scalar_assign
   13145                 :             :      as part of the deep copy.  */
   13146                 :      293247 :   if (!scalar_to_array && expr1->ts.type == BT_DERIVED
   13147                 :      310539 :                        && (gfc_is_class_array_function (expr2)
   13148                 :       16647 :                            || gfc_is_alloc_class_scalar_function (expr2)))
   13149                 :             :     {
   13150                 :          78 :       tmp = gfc_nullify_alloc_comp (expr1->ts.u.derived, rse.expr, 0);
   13151                 :          78 :       gfc_prepend_expr_to_block (&rse.post, tmp);
   13152                 :          78 :       if (lss != gfc_ss_terminator && rss == gfc_ss_terminator)
   13153                 :           0 :         gfc_add_block_to_block (&loop.post, &rse.post);
   13154                 :             :     }
   13155                 :             : 
   13156                 :      293868 :   tmp = NULL_TREE;
   13157                 :             : 
   13158                 :      293868 :   if (is_poly_assign)
   13159                 :             :     {
   13160                 :        3176 :       tmp = trans_class_assignment (&body, expr1, expr2, &lse, &rse,
   13161                 :        3176 :                                     use_vptr_copy || (lhs_attr.allocatable
   13162                 :         517 :                                                       && !lhs_attr.dimension),
   13163                 :        2974 :                                     !realloc_flag && flag_realloc_lhs
   13164                 :        3693 :                                     && !lhs_attr.pointer);
   13165                 :        3176 :       if (expr2->expr_type == EXPR_FUNCTION
   13166                 :         224 :           && expr2->ts.type == BT_DERIVED
   13167                 :          30 :           && expr2->ts.u.derived->attr.alloc_comp)
   13168                 :             :         {
   13169                 :          18 :           tree tmp2 = gfc_deallocate_alloc_comp (expr2->ts.u.derived,
   13170                 :             :                                                  rse.expr, expr2->rank);
   13171                 :          18 :           if (lss == gfc_ss_terminator)
   13172                 :          18 :             gfc_add_expr_to_block (&rse.post, tmp2);
   13173                 :             :           else
   13174                 :           0 :             gfc_add_expr_to_block (&loop.post, tmp2);
   13175                 :             :         }
   13176                 :             : 
   13177                 :        3176 :       expr1->must_finalize = 0;
   13178                 :             :     }
   13179                 :      290692 :   else if (!is_poly_assign && expr2->must_finalize
   13180                 :         348 :            && expr1->ts.type == BT_CLASS
   13181                 :         126 :            && expr2->ts.type == BT_CLASS)
   13182                 :             :     {
   13183                 :             :       /* This case comes about when the scalarizer provides array element
   13184                 :             :          references. Use the vptr copy function, since this does a deep
   13185                 :             :          copy of allocatable components, without which the finalizer call
   13186                 :             :          will deallocate the components.  */
   13187                 :         120 :       tmp = gfc_get_vptr_from_expr (rse.expr);
   13188                 :         120 :       if (tmp != NULL_TREE)
   13189                 :             :         {
   13190                 :         120 :           tree fcn = gfc_vptr_copy_get (tmp);
   13191                 :         120 :           if (POINTER_TYPE_P (TREE_TYPE (fcn)))
   13192                 :         120 :             fcn = build_fold_indirect_ref_loc (input_location, fcn);
   13193                 :         120 :           tmp = build_call_expr_loc (input_location,
   13194                 :             :                                      fcn, 2,
   13195                 :             :                                      gfc_build_addr_expr (NULL, rse.expr),
   13196                 :             :                                      gfc_build_addr_expr (NULL, lse.expr));
   13197                 :             :         }
   13198                 :             :     }
   13199                 :             : 
   13200                 :             :   /* Comply with F2018 (7.5.6.3). Make sure that any finalization code is added
   13201                 :             :      after evaluation of the rhs and before reallocation.  */
   13202                 :      293868 :   final_expr = gfc_assignment_finalizer_call (&lse, expr1, init_flag);
   13203                 :      293868 :   if (final_expr && !(expr2->expr_type == EXPR_VARIABLE
   13204                 :         163 :                       && expr2->symtree->n.sym->attr.artificial))
   13205                 :             :     {
   13206                 :         545 :       if (lss == gfc_ss_terminator)
   13207                 :             :         {
   13208                 :         158 :           gfc_add_block_to_block (&block, &rse.pre);
   13209                 :         158 :           gfc_add_block_to_block (&block, &lse.finalblock);
   13210                 :             :         }
   13211                 :             :       else
   13212                 :             :         {
   13213                 :         387 :           gfc_add_block_to_block (&body, &rse.pre);
   13214                 :         387 :           gfc_add_block_to_block (&loop.code[expr1->rank - 1],
   13215                 :             :                                   &lse.finalblock);
   13216                 :             :         }
   13217                 :             :     }
   13218                 :             :   else
   13219                 :      293323 :     gfc_add_block_to_block (&body, &rse.pre);
   13220                 :             : 
   13221                 :      293868 :   if (flag_coarray != GFC_FCOARRAY_NONE && expr1->ts.type == BT_CHARACTER
   13222                 :        2001 :       && assoc_assign)
   13223                 :           0 :     tmp = gfc_trans_pointer_assignment (expr1, expr2);
   13224                 :             : 
   13225                 :             :   /* If nothing else works, do it the old fashioned way!  */
   13226                 :      293868 :   if (tmp == NULL_TREE)
   13227                 :      290572 :     tmp
   13228                 :      290572 :       = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
   13229                 :      552891 :                                  gfc_expr_is_variable (expr2) || scalar_to_array
   13230                 :      552354 :                                    || expr2->expr_type == EXPR_ARRAY,
   13231                 :      290572 :                                  !(l_is_temp || init_flag) && dealloc,
   13232                 :      290572 :                                  expr1->symtree->n.sym->attr.codimension,
   13233                 :             :                                  assoc_assign);
   13234                 :             : 
   13235                 :             :   /* Add the lse pre block to the body  */
   13236                 :      293868 :   gfc_add_block_to_block (&body, &lse.pre);
   13237                 :      293868 :   gfc_add_expr_to_block (&body, tmp);
   13238                 :             : 
   13239                 :             :   /* Add the post blocks to the body.  Scalar finalization must appear before
   13240                 :             :      the post block in case any dellocations are done.  */
   13241                 :      293868 :   if (rse.finalblock.head
   13242                 :      293868 :       && (!l_is_temp || (expr2->expr_type == EXPR_FUNCTION
   13243                 :          14 :                          && gfc_expr_attr (expr2).elemental)))
   13244                 :             :     {
   13245                 :         135 :       gfc_add_block_to_block (&body, &rse.finalblock);
   13246                 :         135 :       gfc_add_block_to_block (&body, &rse.post);
   13247                 :             :     }
   13248                 :             :   else
   13249                 :      293733 :     gfc_add_block_to_block (&body, &rse.post);
   13250                 :             : 
   13251                 :      293868 :   gfc_add_block_to_block (&body, &lse.post);
   13252                 :             : 
   13253                 :      293868 :   if (lss == gfc_ss_terminator)
   13254                 :             :     {
   13255                 :             :       /* F2003: Add the code for reallocation on assignment.  */
   13256                 :      254037 :       if (flag_realloc_lhs && is_scalar_reallocatable_lhs (expr1)
   13257                 :      259992 :           && !is_poly_assign)
   13258                 :        3312 :         alloc_scalar_allocatable_for_assignment (&block, string_length,
   13259                 :             :                                                  expr1, expr2);
   13260                 :             : 
   13261                 :             :       /* Use the scalar assignment as is.  */
   13262                 :      256662 :       gfc_add_block_to_block (&block, &body);
   13263                 :             :     }
   13264                 :             :   else
   13265                 :             :     {
   13266                 :       37206 :       gcc_assert (lse.ss == gfc_ss_terminator
   13267                 :             :                   && rse.ss == gfc_ss_terminator);
   13268                 :             : 
   13269                 :       37206 :       if (l_is_temp)
   13270                 :             :         {
   13271                 :         983 :           gfc_trans_scalarized_loop_boundary (&loop, &body);
   13272                 :             : 
   13273                 :             :           /* We need to copy the temporary to the actual lhs.  */
   13274                 :         983 :           gfc_init_se (&lse, NULL);
   13275                 :         983 :           gfc_init_se (&rse, NULL);
   13276                 :         983 :           gfc_copy_loopinfo_to_se (&lse, &loop);
   13277                 :         983 :           gfc_copy_loopinfo_to_se (&rse, &loop);
   13278                 :             : 
   13279                 :         983 :           rse.ss = loop.temp_ss;
   13280                 :         983 :           lse.ss = lss;
   13281                 :             : 
   13282                 :         983 :           gfc_conv_tmp_array_ref (&rse);
   13283                 :         983 :           gfc_conv_expr (&lse, expr1);
   13284                 :             : 
   13285                 :         983 :           gcc_assert (lse.ss == gfc_ss_terminator
   13286                 :             :                       && rse.ss == gfc_ss_terminator);
   13287                 :             : 
   13288                 :         983 :           if (expr2->ts.type == BT_CHARACTER)
   13289                 :         123 :             rse.string_length = string_length;
   13290                 :             : 
   13291                 :         983 :           tmp = gfc_trans_scalar_assign (&lse, &rse, expr1->ts,
   13292                 :             :                                          false, dealloc);
   13293                 :         983 :           gfc_add_expr_to_block (&body, tmp);
   13294                 :             :         }
   13295                 :             : 
   13296                 :             :       /* F2003: Allocate or reallocate lhs of allocatable array.  */
   13297                 :       37206 :       if (realloc_flag)
   13298                 :             :         {
   13299                 :        5571 :           realloc_lhs_warning (expr1->ts.type, true, &expr1->where);
   13300                 :        5571 :           ompws_flags &= ~OMPWS_SCALARIZER_WS;
   13301                 :        5571 :           tmp = gfc_alloc_allocatable_for_assignment (&loop, expr1, expr2);
   13302                 :        5571 :           if (tmp != NULL_TREE)
   13303                 :        5571 :             gfc_add_expr_to_block (&loop.code[expr1->rank - 1], tmp);
   13304                 :             :         }
   13305                 :             : 
   13306                 :       37206 :       if (maybe_workshare)
   13307                 :          73 :         ompws_flags &= ~OMPWS_SCALARIZER_BODY;
   13308                 :             : 
   13309                 :             :       /* Generate the copying loops.  */
   13310                 :       37206 :       gfc_trans_scalarizing_loops (&loop, &body);
   13311                 :             : 
   13312                 :             :       /* Wrap the whole thing up.  */
   13313                 :       37206 :       gfc_add_block_to_block (&block, &loop.pre);
   13314                 :       37206 :       gfc_add_block_to_block (&block, &loop.post);
   13315                 :             : 
   13316                 :       37206 :       gfc_cleanup_loop (&loop);
   13317                 :             :     }
   13318                 :             : 
   13319                 :      293868 :   return gfc_finish_block (&block);
   13320                 :             : }
   13321                 :             : 
   13322                 :             : 
   13323                 :             : /* Check whether EXPR is a copyable array.  */
   13324                 :             : 
   13325                 :             : static bool
   13326                 :      929632 : copyable_array_p (gfc_expr * expr)
   13327                 :             : {
   13328                 :      929632 :   if (expr->expr_type != EXPR_VARIABLE)
   13329                 :             :     return false;
   13330                 :             : 
   13331                 :             :   /* First check it's an array.  */
   13332                 :      907205 :   if (expr->rank < 1 || !expr->ref || expr->ref->next)
   13333                 :             :     return false;
   13334                 :             : 
   13335                 :      135299 :   if (!gfc_full_array_ref_p (expr->ref, NULL))
   13336                 :             :     return false;
   13337                 :             : 
   13338                 :             :   /* Next check that it's of a simple enough type.  */
   13339                 :      108395 :   switch (expr->ts.type)
   13340                 :             :     {
   13341                 :             :     case BT_INTEGER:
   13342                 :             :     case BT_REAL:
   13343                 :             :     case BT_COMPLEX:
   13344                 :             :     case BT_LOGICAL:
   13345                 :             :       return true;
   13346                 :             : 
   13347                 :             :     case BT_CHARACTER:
   13348                 :             :       return false;
   13349                 :             : 
   13350                 :        5653 :     case_bt_struct:
   13351                 :        5653 :       return !expr->ts.u.derived->attr.alloc_comp;
   13352                 :             : 
   13353                 :             :     default:
   13354                 :             :       break;
   13355                 :             :     }
   13356                 :             : 
   13357                 :             :   return false;
   13358                 :             : }
   13359                 :             : 
   13360                 :             : /* Translate an assignment.  */
   13361                 :             : 
   13362                 :             : tree
   13363                 :      310748 : gfc_trans_assignment (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
   13364                 :             :                       bool dealloc, bool use_vptr_copy, bool may_alias)
   13365                 :             : {
   13366                 :      310748 :   tree tmp;
   13367                 :             : 
   13368                 :             :   /* Special case a single function returning an array.  */
   13369                 :      310748 :   if (expr2->expr_type == EXPR_FUNCTION && expr2->rank > 0)
   13370                 :             :     {
   13371                 :       14426 :       tmp = gfc_trans_arrayfunc_assign (expr1, expr2);
   13372                 :       14426 :       if (tmp)
   13373                 :             :         return tmp;
   13374                 :             :     }
   13375                 :             : 
   13376                 :             :   /* Special case assigning an array to zero.  */
   13377                 :      303729 :   if (copyable_array_p (expr1)
   13378                 :      303729 :       && is_zero_initializer_p (expr2))
   13379                 :             :     {
   13380                 :        3651 :       tmp = gfc_trans_zero_assign (expr1);
   13381                 :        3651 :       if (tmp)
   13382                 :             :         return tmp;
   13383                 :             :     }
   13384                 :             : 
   13385                 :             :   /* Special case copying one array to another.  */
   13386                 :      300353 :   if (copyable_array_p (expr1)
   13387                 :       26635 :       && copyable_array_p (expr2)
   13388                 :        2597 :       && gfc_compare_types (&expr1->ts, &expr2->ts)
   13389                 :      302950 :       && !gfc_check_dependency (expr1, expr2, 0))
   13390                 :             :     {
   13391                 :        2502 :       tmp = gfc_trans_array_copy (expr1, expr2);
   13392                 :        2502 :       if (tmp)
   13393                 :             :         return tmp;
   13394                 :             :     }
   13395                 :             : 
   13396                 :             :   /* Special case initializing an array from a constant array constructor.  */
   13397                 :      298915 :   if (copyable_array_p (expr1)
   13398                 :       25197 :       && expr2->expr_type == EXPR_ARRAY
   13399                 :      306341 :       && gfc_compare_types (&expr1->ts, &expr2->ts))
   13400                 :             :     {
   13401                 :        7426 :       tmp = gfc_trans_array_constructor_copy (expr1, expr2);
   13402                 :        7426 :       if (tmp)
   13403                 :             :         return tmp;
   13404                 :             :     }
   13405                 :             : 
   13406                 :      293868 :   if (UNLIMITED_POLY (expr1) && expr1->rank)
   13407                 :      293868 :     use_vptr_copy = true;
   13408                 :             : 
   13409                 :             :   /* Fallback to the scalarizer to generate explicit loops.  */
   13410                 :      293868 :   return gfc_trans_assignment_1 (expr1, expr2, init_flag, dealloc,
   13411                 :      293868 :                                  use_vptr_copy, may_alias);
   13412                 :             : }
   13413                 :             : 
   13414                 :             : tree
   13415                 :       11402 : gfc_trans_init_assign (gfc_code * code)
   13416                 :             : {
   13417                 :       11402 :   return gfc_trans_assignment (code->expr1, code->expr2, true, false, true);
   13418                 :             : }
   13419                 :             : 
   13420                 :             : tree
   13421                 :      291460 : gfc_trans_assign (gfc_code * code)
   13422                 :             : {
   13423                 :      291460 :   return gfc_trans_assignment (code->expr1, code->expr2, false, true);
   13424                 :             : }
   13425                 :             : 
   13426                 :             : /* Generate a simple loop for internal use of the form
   13427                 :             :    for (var = begin; var <cond> end; var += step)
   13428                 :             :       body;  */
   13429                 :             : void
   13430                 :       12144 : gfc_simple_for_loop (stmtblock_t *block, tree var, tree begin, tree end,
   13431                 :             :                      enum tree_code cond, tree step, tree body)
   13432                 :             : {
   13433                 :       12144 :   tree tmp;
   13434                 :             : 
   13435                 :             :   /* var = begin. */
   13436                 :       12144 :   gfc_add_modify (block, var, begin);
   13437                 :             : 
   13438                 :             :   /* Loop: for (var = begin; var <cond> end; var += step).  */
   13439                 :       12144 :   tree label_loop = gfc_build_label_decl (NULL_TREE);
   13440                 :       12144 :   tree label_cond = gfc_build_label_decl (NULL_TREE);
   13441                 :       12144 :   TREE_USED (label_loop) = 1;
   13442                 :       12144 :   TREE_USED (label_cond) = 1;
   13443                 :             : 
   13444                 :       12144 :   gfc_add_expr_to_block (block, build1_v (GOTO_EXPR, label_cond));
   13445                 :       12144 :   gfc_add_expr_to_block (block, build1_v (LABEL_EXPR, label_loop));
   13446                 :             : 
   13447                 :             :   /* Loop body.  */
   13448                 :       12144 :   gfc_add_expr_to_block (block, body);
   13449                 :             : 
   13450                 :             :   /* End of loop body.  */
   13451                 :       12144 :   tmp = fold_build2_loc (input_location, PLUS_EXPR, TREE_TYPE (var), var, step);
   13452                 :       12144 :   gfc_add_modify (block, var, tmp);
   13453                 :       12144 :   gfc_add_expr_to_block (block, build1_v (LABEL_EXPR, label_cond));
   13454                 :       12144 :   tmp = fold_build2_loc (input_location, cond, boolean_type_node, var, end);
   13455                 :       12144 :   tmp = build3_v (COND_EXPR, tmp, build1_v (GOTO_EXPR, label_loop),
   13456                 :             :                   build_empty_stmt (input_location));
   13457                 :       12144 :   gfc_add_expr_to_block (block, tmp);
   13458                 :       12144 : }
        

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