LCOV - code coverage report
Current view: top level - gcc/fortran - trans.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 95.3 % 1397 1332
Test Date: 2026-12-05 16:10:02 Functions: 98.2 % 56 55
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Code translation -- generate GCC trees from gfc_code.
       2              :    Copyright (C) 2002-2026 Free Software Foundation, Inc.
       3              :    Contributed by Paul Brook
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it under
       8              : the terms of the GNU General Public License as published by the Free
       9              : Software Foundation; either version 3, or (at your option) any later
      10              : version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15              : for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #include "config.h"
      22              : #include "system.h"
      23              : #include "coretypes.h"
      24              : #include "options.h"
      25              : #include "tree.h"
      26              : #include "gfortran.h"
      27              : #include "gimple-expr.h"      /* For create_tmp_var_raw.  */
      28              : #include "trans.h"
      29              : #include "stringpool.h"
      30              : #include "fold-const.h"
      31              : #include "tree-iterator.h"
      32              : #include "trans-stmt.h"
      33              : #include "trans-array.h"
      34              : #include "trans-types.h"
      35              : #include "trans-const.h"
      36              : #include "trans-descriptor.h"
      37              : 
      38              : /* Naming convention for backend interface code:
      39              : 
      40              :    gfc_trans_*  translate gfc_code into STMT trees.
      41              : 
      42              :    gfc_conv_*   expression conversion
      43              : 
      44              :    gfc_get_*    get a backend tree representation of a decl or type  */
      45              : 
      46              : const char gfc_msg_fault[] = N_("Array reference out of bounds");
      47              : 
      48              : /* Nonzero if we're translating a defined assignment call. */
      49              : int is_assign_call = 0;
      50              : 
      51              : /* Incremented by gfc_conv_procedure_call while it evaluates actual
      52              :    arguments.  */
      53              : int actual_arg_level = 0;
      54              : 
      55              : /* Advance along TREE_CHAIN n times.  */
      56              : 
      57              : tree
      58      6560955 : gfc_advance_chain (tree t, int n)
      59              : {
      60     18929804 :   for (; n > 0; n--)
      61              :     {
      62     12368849 :       gcc_assert (t != NULL_TREE);
      63     12368849 :       t = DECL_CHAIN (t);
      64              :     }
      65      6560955 :   return t;
      66              : }
      67              : 
      68              : void
      69       106263 : gfc_locus_from_location (locus *where, location_t loc)
      70              : {
      71       106263 :   where->nextc = (gfc_char_t *) -1;
      72       106263 :   where->u.location = loc;
      73       106263 : }
      74              : 
      75              : 
      76              : static int num_var;
      77              : 
      78              : #define MAX_PREFIX_LEN 20
      79              : 
      80              : static tree
      81            0 : create_var_debug_raw (tree type, const char *prefix)
      82              : {
      83              :   /* Space for prefix + "_" + 10-digit-number + \0.  */
      84            0 :   char name_buf[MAX_PREFIX_LEN + 1 + 10 + 1];
      85            0 :   tree t;
      86            0 :   int i;
      87              : 
      88            0 :   if (prefix == NULL)
      89              :     prefix = "gfc";
      90              :   else
      91            0 :     gcc_assert (strlen (prefix) <= MAX_PREFIX_LEN);
      92              : 
      93            0 :   for (i = 0; prefix[i] != 0; i++)
      94            0 :     name_buf[i] = gfc_wide_toupper (prefix[i]);
      95              : 
      96            0 :   snprintf (name_buf + i, sizeof (name_buf) - i, "_%d", num_var++);
      97              : 
      98            0 :   t = build_decl (input_location, VAR_DECL, get_identifier (name_buf), type);
      99              : 
     100              :   /* Not setting this causes some regressions.  */
     101            0 :   DECL_ARTIFICIAL (t) = 1;
     102              : 
     103              :   /* We want debug info for it.  */
     104            0 :   DECL_IGNORED_P (t) = 0;
     105              :   /* It should not be nameless.  */
     106            0 :   DECL_NAMELESS (t) = 0;
     107              : 
     108              :   /* Make the variable writable.  */
     109            0 :   TREE_READONLY (t) = 0;
     110              : 
     111            0 :   DECL_EXTERNAL (t) = 0;
     112            0 :   TREE_STATIC (t) = 0;
     113            0 :   TREE_USED (t) = 1;
     114              : 
     115            0 :   return t;
     116              : }
     117              : 
     118              : /* Creates a variable declaration with a given TYPE.  */
     119              : 
     120              : tree
     121      1746003 : gfc_create_var_np (tree type, const char *prefix)
     122              : {
     123      1746003 :   tree t;
     124              : 
     125      1746003 :   if (flag_debug_aux_vars)
     126            0 :     return create_var_debug_raw (type, prefix);
     127              : 
     128      1746003 :   t = create_tmp_var_raw (type, prefix);
     129              : 
     130              :   /* No warnings for anonymous variables.  */
     131      1746003 :   if (prefix == NULL)
     132      1070509 :     suppress_warning (t);
     133              : 
     134              :   return t;
     135              : }
     136              : 
     137              : 
     138              : /* Like above, but also adds it to the current scope.  */
     139              : 
     140              : tree
     141      1607619 : gfc_create_var (tree type, const char *prefix)
     142              : {
     143      1607619 :   tree tmp;
     144              : 
     145      1607619 :   tmp = gfc_create_var_np (type, prefix);
     146              : 
     147      1607619 :   pushdecl (tmp);
     148              : 
     149      1607619 :   return tmp;
     150              : }
     151              : 
     152              : 
     153              : /* If the expression is not constant, evaluate it now.  We assign the
     154              :    result of the expression to an artificially created variable VAR, and
     155              :    return a pointer to the VAR_DECL node for this variable.  */
     156              : 
     157              : tree
     158      2292959 : gfc_evaluate_now_loc (location_t loc, tree expr, stmtblock_t * pblock)
     159              : {
     160      2292959 :   tree var;
     161              : 
     162      2292959 :   if (CONSTANT_CLASS_P (expr))
     163              :     return expr;
     164              : 
     165       908725 :   var = gfc_create_var (TREE_TYPE (expr), NULL);
     166       908725 :   gfc_add_modify_loc (loc, pblock, var, expr);
     167              : 
     168       908725 :   return var;
     169              : }
     170              : 
     171              : 
     172              : tree
     173      2253670 : gfc_evaluate_now (tree expr, stmtblock_t * pblock)
     174              : {
     175      2253670 :   return gfc_evaluate_now_loc (input_location, expr, pblock);
     176              : }
     177              : 
     178              : 
     179              : /* Returns a fresh pointer variable pointing to the same data as EXPR, adding
     180              :    in BLOCK the initialization code that makes it point to EXPR.  */
     181              : 
     182              : tree
     183          758 : gfc_evaluate_data_ref_now (tree expr, stmtblock_t *block)
     184              : {
     185          758 :   tree t = expr;
     186              : 
     187          758 :   STRIP_NOPS (t);
     188              : 
     189              :   /* If EXPR can be used as lhs of an assignment, we have to take the address
     190              :      of EXPR.  Otherwise, reassigning the pointer would retarget it to some
     191              :      other data without EXPR being retargetted as well.  */
     192          758 :   bool lvalue_p = DECL_P (t) || REFERENCE_CLASS_P (t) || INDIRECT_REF_P (t);
     193              : 
     194          161 :   tree value;
     195          161 :   if (lvalue_p)
     196              :     {
     197          597 :       value = gfc_build_addr_expr (NULL_TREE, expr);
     198          597 :       value = gfc_evaluate_now (value, block);
     199          597 :       return build_fold_indirect_ref_loc (input_location, value);
     200              :     }
     201              :   else
     202          161 :     return gfc_evaluate_now (expr, block);
     203              : }
     204              : 
     205              : 
     206              : /* Like gfc_evaluate_now, but add the created variable to the
     207              :    function scope.  */
     208              : 
     209              : tree
     210          120 : gfc_evaluate_now_function_scope (tree expr, stmtblock_t * pblock)
     211              : {
     212          120 :   tree var;
     213          120 :   var = gfc_create_var_np (TREE_TYPE (expr), NULL);
     214          120 :   gfc_add_decl_to_function (var);
     215          120 :   gfc_add_modify (pblock, var, expr);
     216              : 
     217          120 :   return var;
     218              : }
     219              : 
     220              : /* Build a MODIFY_EXPR node and add it to a given statement block PBLOCK.
     221              :    A MODIFY_EXPR is an assignment:
     222              :    LHS <- RHS.  */
     223              : 
     224              : void
     225      3993392 : gfc_add_modify_loc (location_t loc, stmtblock_t * pblock, tree lhs, tree rhs)
     226              : {
     227      3993392 :   tree tmp;
     228              : 
     229      3993392 :   tree t1, t2;
     230      3993392 :   t1 = TREE_TYPE (rhs);
     231      3993392 :   t2 = TREE_TYPE (lhs);
     232              :   /* Make sure that the types of the rhs and the lhs are compatible
     233              :      for scalar assignments.  We should probably have something
     234              :      similar for aggregates, but right now removing that check just
     235              :      breaks everything.  */
     236      3993392 :   gcc_checking_assert (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2)
     237              :                        || AGGREGATE_TYPE_P (TREE_TYPE (lhs)));
     238              : 
     239      3993392 :   tmp = fold_build2_loc (loc, MODIFY_EXPR, void_type_node, lhs,
     240              :                          rhs);
     241      3993392 :   gfc_add_expr_to_block (pblock, tmp);
     242      3993392 : }
     243              : 
     244              : 
     245              : void
     246      2864131 : gfc_add_modify (stmtblock_t * pblock, tree lhs, tree rhs)
     247              : {
     248      2864131 :   gfc_add_modify_loc (input_location, pblock, lhs, rhs);
     249      2864131 : }
     250              : 
     251              : tree
     252         1285 : gfc_trans_force_lval (stmtblock_t *pblock, tree e)
     253              : {
     254         1285 :   if (VAR_P (e))
     255              :     return e;
     256              : 
     257         1102 :   tree v = gfc_create_var (TREE_TYPE (e), NULL);
     258         1102 :   gfc_add_modify (pblock, v, e);
     259         1102 :   return v;
     260              : }
     261              : 
     262              : /* Create a new scope/binding level and initialize a block.  Care must be
     263              :    taken when translating expressions as any temporaries will be placed in
     264              :    the innermost scope.  */
     265              : 
     266              : void
     267      2386300 : gfc_start_block (stmtblock_t * block)
     268              : {
     269              :   /* Start a new binding level.  */
     270      2386300 :   pushlevel ();
     271      2386300 :   block->has_scope = 1;
     272              : 
     273              :   /* The block is empty.  */
     274      2386300 :   block->head = NULL_TREE;
     275      2386300 : }
     276              : 
     277              : 
     278              : /* Initialize a block without creating a new scope.  */
     279              : 
     280              : void
     281     18421111 : gfc_init_block (stmtblock_t * block)
     282              : {
     283     18421111 :   block->head = NULL_TREE;
     284     18421111 :   block->has_scope = 0;
     285     18421111 : }
     286              : 
     287              : 
     288              : /* Sometimes we create a scope but it turns out that we don't actually
     289              :    need it.  This function merges the scope of BLOCK with its parent.
     290              :    Only variable decls will be merged, you still need to add the code.  */
     291              : 
     292              : void
     293           97 : gfc_merge_block_scope (stmtblock_t * block)
     294              : {
     295           97 :   tree decl;
     296           97 :   tree next;
     297              : 
     298           97 :   gcc_assert (block->has_scope);
     299           97 :   block->has_scope = 0;
     300              : 
     301              :   /* Remember the decls in this scope.  */
     302           97 :   decl = getdecls ();
     303           97 :   poplevel (0, 0);
     304              : 
     305              :   /* Add them to the parent scope.  */
     306          313 :   while (decl != NULL_TREE)
     307              :     {
     308          119 :       next = DECL_CHAIN (decl);
     309          119 :       DECL_CHAIN (decl) = NULL_TREE;
     310              : 
     311          119 :       pushdecl (decl);
     312          119 :       decl = next;
     313              :     }
     314           97 : }
     315              : 
     316              : 
     317              : /* Finish a scope containing a block of statements.  */
     318              : 
     319              : tree
     320      4242421 : gfc_finish_block (stmtblock_t * stmtblock)
     321              : {
     322      4242421 :   tree decl;
     323      4242421 :   tree expr;
     324      4242421 :   tree block;
     325              : 
     326      4242421 :   expr = stmtblock->head;
     327      4242421 :   if (!expr)
     328       537397 :     expr = build_empty_stmt (input_location);
     329              : 
     330      4242421 :   stmtblock->head = NULL_TREE;
     331              : 
     332      4242421 :   if (stmtblock->has_scope)
     333              :     {
     334      2386196 :       decl = getdecls ();
     335              : 
     336      2386196 :       if (decl)
     337              :         {
     338       602686 :           block = poplevel (1, 0);
     339       602686 :           expr = build3_v (BIND_EXPR, decl, expr, block);
     340              :         }
     341              :       else
     342      1783510 :         poplevel (0, 0);
     343              :     }
     344              : 
     345      4242421 :   return expr;
     346              : }
     347              : 
     348              : 
     349              : /* Build an ADDR_EXPR and cast the result to TYPE.  If TYPE is NULL, the
     350              :    natural type is used.  */
     351              : 
     352              : tree
     353      1671230 : gfc_build_addr_expr (tree type, tree t)
     354              : {
     355      1671230 :   tree base_type = TREE_TYPE (t);
     356      1671230 :   tree natural_type;
     357              : 
     358       719832 :   if (type && POINTER_TYPE_P (type)
     359       719832 :       && TREE_CODE (base_type) == ARRAY_TYPE
     360      2312259 :       && TYPE_MAIN_VARIANT (TREE_TYPE (type))
     361       641029 :          == TYPE_MAIN_VARIANT (TREE_TYPE (base_type)))
     362              :     {
     363       431766 :       tree min_val = size_zero_node;
     364       431766 :       tree type_domain = TYPE_DOMAIN (base_type);
     365       431766 :       if (type_domain && TYPE_MIN_VALUE (type_domain))
     366       431766 :         min_val = TYPE_MIN_VALUE (type_domain);
     367       431766 :       t = fold (build4_loc (input_location, ARRAY_REF, TREE_TYPE (type),
     368              :                             t, min_val, NULL_TREE, NULL_TREE));
     369       431766 :       natural_type = type;
     370              :     }
     371              :   else
     372      1239464 :     natural_type = build_pointer_type (base_type);
     373              : 
     374      1671230 :   if (INDIRECT_REF_P (t))
     375              :     {
     376       179881 :       if (!type)
     377        82052 :         type = natural_type;
     378       179881 :       t = TREE_OPERAND (t, 0);
     379       179881 :       natural_type = TREE_TYPE (t);
     380              :     }
     381              :   else
     382              :     {
     383      1491349 :       tree base = get_base_address (t);
     384      1491349 :       if (base && DECL_P (base))
     385      1039791 :         TREE_ADDRESSABLE (base) = 1;
     386      1491349 :       t = fold_build1_loc (input_location, ADDR_EXPR, natural_type, t);
     387              :     }
     388              : 
     389      1671230 :   if (type && natural_type != type)
     390       223757 :     t = convert (type, t);
     391              : 
     392      1671230 :   return t;
     393              : }
     394              : 
     395              : 
     396              : /* Return the descriptor that carries the span of DECL, which is marked as a
     397              :    pointer array.  Such a decl usually is a descriptor.  The local decl of a
     398              :    descriptorless dummy array is not, so its span comes from the descriptor it
     399              :    was built from, which is the saved one.  */
     400              : 
     401              : tree
     402        10197 : gfc_get_span_descriptor (tree decl)
     403              : {
     404        10197 :   if (DECL_P (decl)
     405         9898 :       && GFC_ARRAY_TYPE_P (TREE_TYPE (decl))
     406           91 :       && DECL_LANG_SPECIFIC (decl)
     407        10288 :       && GFC_DECL_SAVED_DESCRIPTOR (decl))
     408           91 :     decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
     409              : 
     410        10197 :   if (POINTER_TYPE_P (TREE_TYPE (decl)))
     411         2386 :     decl = build_fold_indirect_ref_loc (input_location, decl);
     412              : 
     413        10197 :   return decl;
     414              : }
     415              : 
     416              : 
     417              : static tree
     418        23236 : get_array_span (tree type, tree decl)
     419              : {
     420        23236 :   tree span;
     421              : 
     422              :   /* Component references are guaranteed to have a reliable value for
     423              :      'span'. Likewise indirect references since they emerge from the
     424              :      conversion of a CFI descriptor or the hidden dummy descriptor.  */
     425        23236 :   if (TREE_CODE (decl) == COMPONENT_REF
     426        23236 :       && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
     427         4159 :     return gfc_conv_descriptor_span_get (decl);
     428        19077 :   else if (INDIRECT_REF_P (decl)
     429        19077 :            && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
     430         2435 :     return gfc_conv_descriptor_span_get (decl);
     431              : 
     432              :   /* Return the span for deferred character length array references.  */
     433        16642 :   if (type
     434        16642 :       && (TREE_CODE (type) == ARRAY_TYPE || TREE_CODE (type) == INTEGER_TYPE)
     435        27680 :       && TYPE_STRING_FLAG (type))
     436              :     {
     437         7872 :       if (DECL_P (decl) && GFC_DECL_PTR_ARRAY_P (decl))
     438         1384 :         decl = gfc_get_span_descriptor (decl);
     439         6488 :       else if (TREE_CODE (decl) == PARM_DECL)
     440          140 :         decl = build_fold_indirect_ref_loc (input_location, decl);
     441         7872 :       if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (decl)))
     442         6162 :         span = gfc_conv_descriptor_span_get (decl);
     443              :       else
     444         1710 :         span = gfc_get_character_len_in_bytes (type);
     445        15744 :       span = (span && !integer_zerop (span))
     446        15744 :         ? (fold_convert (gfc_array_index_type, span)) : (NULL_TREE);
     447              :     }
     448              :   /* Likewise for class array or pointer array references.  */
     449         8770 :   else if (TREE_CODE (decl) == FIELD_DECL
     450              :            || VAR_OR_FUNCTION_DECL_P (decl)
     451              :            || TREE_CODE (decl) == PARM_DECL)
     452              :     {
     453         8770 :       if (GFC_DECL_CLASS (decl))
     454              :         {
     455              :           /* When a temporary is in place for the class array, then the
     456              :              original class' declaration is stored in the saved
     457              :              descriptor.  */
     458            0 :           if (DECL_LANG_SPECIFIC (decl) && GFC_DECL_SAVED_DESCRIPTOR (decl))
     459            0 :             decl = GFC_DECL_SAVED_DESCRIPTOR (decl);
     460              :           else
     461              :             {
     462              :               /* Allow for dummy arguments and other good things.  */
     463            0 :               if (POINTER_TYPE_P (TREE_TYPE (decl)))
     464            0 :                 decl = build_fold_indirect_ref_loc (input_location, decl);
     465              : 
     466              :               /* Check if '_data' is an array descriptor.  If it is not,
     467              :                  the array must be one of the components of the class
     468              :                  object, so return a null span.  */
     469            0 :               if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (
     470              :                                           gfc_class_data_get (decl))))
     471              :                 return NULL_TREE;
     472              :             }
     473            0 :           span = gfc_class_vtab_size_get (decl);
     474              :           /* For unlimited polymorphic entities then _len component needs
     475              :              to be multiplied with the size.  */
     476            0 :           span = gfc_resize_class_size_with_len (NULL, decl, span);
     477              :         }
     478         8770 :       else if (tree cached = GFC_DECL_GET_SPAN (decl))
     479              :         /* A span addressed dummy loaded its span on entry.  */
     480              :         span = cached;
     481         8542 :       else if (GFC_DECL_PTR_ARRAY_P (decl))
     482         8175 :         span = gfc_conv_descriptor_span_get (gfc_get_span_descriptor (decl));
     483              :       else
     484              :         span = NULL_TREE;
     485              :     }
     486              :   else
     487              :     span = NULL_TREE;
     488              : 
     489              :   return span;
     490              : }
     491              : 
     492              : 
     493              : tree
     494        31089 : gfc_build_spanned_array_ref (tree base, tree offset, tree span)
     495              : {
     496        31089 :   tree type;
     497        31089 :   tree tmp;
     498        31089 :   type = TREE_TYPE (TREE_TYPE (base));
     499        31089 :   offset = fold_build2_loc (input_location, MULT_EXPR,
     500              :                             gfc_array_index_type,
     501              :                             offset, span);
     502        31089 :   tmp = gfc_build_addr_expr (pvoid_type_node, base);
     503        31089 :   tmp = fold_build_pointer_plus_loc (input_location, tmp, offset);
     504        31089 :   tmp = fold_convert (build_pointer_type (type), tmp);
     505        31089 :   tmp = build_fold_indirect_ref_loc (input_location, tmp);
     506        31089 :   return tmp;
     507              : }
     508              : 
     509              : 
     510              : /* Build an ARRAY_REF with its natural type.
     511              :    NON_NEGATIVE_OFFSET indicates if it’s true that OFFSET can’t be negative,
     512              :    and thus that an ARRAY_REF can safely be generated.  If it’s false, we
     513              :    have to play it safe and use pointer arithmetic.  */
     514              : 
     515              : tree
     516      1567811 : gfc_build_array_ref (tree base, tree offset, tree decl,
     517              :                      bool non_negative_offset, tree vptr)
     518              : {
     519      1567811 :   tree type = TREE_TYPE (base);
     520      1567811 :   tree span = NULL_TREE;
     521              : 
     522      1567811 :   if (GFC_ARRAY_TYPE_P (type) && GFC_TYPE_ARRAY_RANK (type) == 0)
     523              :     {
     524          203 :       gcc_assert (GFC_TYPE_ARRAY_CORANK (type) > 0);
     525              : 
     526          203 :       return fold_convert (TYPE_MAIN_VARIANT (type), base);
     527              :     }
     528              : 
     529              :   /* Scalar coarray, there is nothing to do.  */
     530      1567608 :   if (TREE_CODE (type) != ARRAY_TYPE)
     531              :     {
     532           19 :       gcc_assert (decl == NULL_TREE);
     533           19 :       gcc_assert (integer_zerop (offset));
     534              :       return base;
     535              :     }
     536              : 
     537      1567589 :   type = TREE_TYPE (type);
     538              : 
     539      1567589 :   if (DECL_P (base))
     540       215814 :     TREE_ADDRESSABLE (base) = 1;
     541              : 
     542              :   /* Strip NON_LVALUE_EXPR nodes.  */
     543      1589790 :   STRIP_TYPE_NOPS (offset);
     544              : 
     545              :   /* If decl or vptr are non-null, pointer arithmetic for the array reference
     546              :      is likely. Generate the 'span' for the array reference.  */
     547      1567589 :   if (vptr)
     548              :     {
     549         4015 :       span = gfc_vptr_size_get (vptr);
     550              : 
     551              :       /* Check if this is an unlimited polymorphic object carrying a character
     552              :          payload. In this case, the 'len' field is non-zero.  */
     553         4015 :       if (decl && GFC_CLASS_TYPE_P (TREE_TYPE (decl)))
     554         3830 :         span = gfc_resize_class_size_with_len (NULL, decl, span);
     555              :     }
     556      1563574 :   else if (decl)
     557        23236 :     span = get_array_span (type, decl);
     558              : 
     559              :   /* If a non-null span has been generated reference the element with
     560              :      pointer arithmetic.  */
     561        27251 :   if (span != NULL_TREE)
     562        26884 :     return gfc_build_spanned_array_ref (base, offset, span);
     563              :   /* Else use a straightforward array reference if possible.  */
     564      1540705 :   else if (non_negative_offset)
     565      1490610 :     return build4_loc (input_location, ARRAY_REF, type, base, offset,
     566      1490610 :                        NULL_TREE, NULL_TREE);
     567              :   /* Otherwise use pointer arithmetic.  */
     568              :   else
     569              :     {
     570        50095 :       gcc_assert (TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE);
     571        50095 :       tree min = NULL_TREE;
     572        50095 :       if (TYPE_DOMAIN (TREE_TYPE (base))
     573        50095 :           && !integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (base)))))
     574          332 :         min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (base)));
     575              : 
     576          332 :       tree zero_based_index
     577          332 :            = min ? fold_build2_loc (input_location, MINUS_EXPR,
     578              :                                     gfc_array_index_type,
     579              :                                     fold_convert (gfc_array_index_type, offset),
     580              :                                     fold_convert (gfc_array_index_type, min))
     581        49763 :                  : fold_convert (gfc_array_index_type, offset);
     582              : 
     583        50095 :       tree elt_size = fold_convert (gfc_array_index_type,
     584              :                                     TYPE_SIZE_UNIT (type));
     585              : 
     586        50095 :       tree offset_bytes = fold_build2_loc (input_location, MULT_EXPR,
     587              :                                            gfc_array_index_type,
     588              :                                            zero_based_index, elt_size);
     589              : 
     590        50095 :       tree base_addr = gfc_build_addr_expr (pvoid_type_node, base);
     591              : 
     592        50095 :       tree ptr = fold_build_pointer_plus_loc (input_location, base_addr,
     593              :                                               offset_bytes);
     594        50095 :       return build1_loc (input_location, INDIRECT_REF, type,
     595        50095 :                          fold_convert (build_pointer_type (type), ptr));
     596              :     }
     597              : }
     598              : 
     599              : 
     600              : /* Generate a call to print a runtime error possibly including multiple
     601              :    arguments and a locus.  */
     602              : 
     603              : static tree
     604        86136 : trans_runtime_error_vararg (tree errorfunc, locus* where, const char* msgid,
     605              :                             va_list ap)
     606              : {
     607        86136 :   stmtblock_t block;
     608        86136 :   tree tmp;
     609        86136 :   tree arg, arg2;
     610        86136 :   tree *argarray;
     611        86136 :   tree fntype;
     612        86136 :   char *message;
     613        86136 :   const char *p;
     614        86136 :   int nargs, i;
     615        86136 :   location_t loc;
     616              : 
     617              :   /* Compute the number of extra arguments from the format string.  */
     618      4529816 :   for (p = msgid, nargs = 0; *p; p++)
     619      4443680 :     if (*p == '%')
     620              :       {
     621       124572 :         p++;
     622       124572 :         if (*p != '%')
     623       123639 :           nargs++;
     624              :       }
     625              : 
     626              :   /* The code to generate the error.  */
     627        86136 :   gfc_start_block (&block);
     628              : 
     629        86136 :   if (where)
     630              :     {
     631        65681 :       location_t loc = gfc_get_location (where);
     632        65681 :       message = xasprintf ("At line %d of file %s",  LOCATION_LINE (loc),
     633       131362 :                            LOCATION_FILE (loc));
     634              :     }
     635              :   else
     636        20455 :     message = xasprintf ("In file '%s', around line %d",
     637        40910 :                          gfc_source_file, LOCATION_LINE (input_location));
     638              : 
     639        86136 :   arg = gfc_build_addr_expr (pchar_type_node,
     640              :                              gfc_build_localized_cstring_const (message));
     641        86136 :   free (message);
     642              : 
     643        86136 :   message = xasprintf ("%s", _(msgid));
     644        86136 :   arg2 = gfc_build_addr_expr (pchar_type_node,
     645              :                               gfc_build_localized_cstring_const (message));
     646        86136 :   free (message);
     647              : 
     648              :   /* Build the argument array.  */
     649        86136 :   argarray = XALLOCAVEC (tree, nargs + 2);
     650        86136 :   argarray[0] = arg;
     651        86136 :   argarray[1] = arg2;
     652       209775 :   for (i = 0; i < nargs; i++)
     653       123639 :     argarray[2 + i] = va_arg (ap, tree);
     654              : 
     655              :   /* Build the function call to runtime_(warning,error)_at; because of the
     656              :      variable number of arguments, we can't use build_call_expr_loc dinput_location,
     657              :      irectly.  */
     658        86136 :   fntype = TREE_TYPE (errorfunc);
     659              : 
     660        86136 :   loc = where ? gfc_get_location (where) : input_location;
     661        86136 :   tmp = fold_build_call_array_loc (loc, TREE_TYPE (fntype),
     662              :                                    fold_build1_loc (loc, ADDR_EXPR,
     663              :                                              build_pointer_type (fntype),
     664              :                                              errorfunc),
     665              :                                    nargs + 2, argarray);
     666        86136 :   gfc_add_expr_to_block (&block, tmp);
     667              : 
     668        86136 :   return gfc_finish_block (&block);
     669              : }
     670              : 
     671              : 
     672              : tree
     673        26680 : gfc_trans_runtime_error (bool error, locus* where, const char* msgid, ...)
     674              : {
     675        26680 :   va_list ap;
     676        26680 :   tree result;
     677              : 
     678        26680 :   va_start (ap, msgid);
     679        26680 :   result = trans_runtime_error_vararg (error
     680              :                                        ? gfor_fndecl_runtime_error_at
     681              :                                        : gfor_fndecl_runtime_warning_at,
     682              :                                        where, msgid, ap);
     683        26680 :   va_end (ap);
     684        26680 :   return result;
     685              : }
     686              : 
     687              : 
     688              : /* Generate a runtime error if COND is true.  */
     689              : 
     690              : void
     691       167693 : gfc_trans_runtime_check (bool error, bool once, tree cond, stmtblock_t * pblock,
     692              :                          locus * where, const char * msgid, ...)
     693              : {
     694       167693 :   va_list ap;
     695       167693 :   stmtblock_t block;
     696       167693 :   tree body;
     697       167693 :   tree tmp;
     698       167693 :   tree tmpvar = NULL;
     699              : 
     700       167693 :   if (integer_zerop (cond))
     701       128572 :     return;
     702              : 
     703        39121 :   if (once)
     704              :     {
     705          954 :        tmpvar = gfc_create_var (boolean_type_node, "print_warning");
     706          954 :        TREE_STATIC (tmpvar) = 1;
     707          954 :        DECL_INITIAL (tmpvar) = boolean_true_node;
     708          954 :        gfc_add_expr_to_block (pblock, tmpvar);
     709              :     }
     710              : 
     711        39121 :   gfc_start_block (&block);
     712              : 
     713              :   /* For error, runtime_error_at already implies PRED_NORETURN.  */
     714        39121 :   if (!error && once)
     715          954 :     gfc_add_expr_to_block (&block, build_predict_expr (PRED_FORTRAN_WARN_ONCE,
     716              :                                                        NOT_TAKEN));
     717              : 
     718              :   /* The code to generate the error.  */
     719        39121 :   va_start (ap, msgid);
     720        39121 :   gfc_add_expr_to_block (&block,
     721              :                          trans_runtime_error_vararg
     722              :                          (error ? gfor_fndecl_runtime_error_at
     723              :                           : gfor_fndecl_runtime_warning_at,
     724              :                           where, msgid, ap));
     725        39121 :   va_end (ap);
     726              : 
     727        39121 :   if (once)
     728          954 :     gfc_add_modify (&block, tmpvar, boolean_false_node);
     729              : 
     730        39121 :   body = gfc_finish_block (&block);
     731              : 
     732        39121 :   if (integer_onep (cond))
     733              :     {
     734          904 :       gfc_add_expr_to_block (pblock, body);
     735              :     }
     736              :   else
     737              :     {
     738        38217 :       location_t loc = where ? gfc_get_location (where) : input_location;
     739        38217 :       if (once)
     740           86 :         cond = fold_build2_loc (loc, TRUTH_AND_EXPR, boolean_type_node, tmpvar,
     741              :                                 fold_convert (boolean_type_node, cond));
     742              : 
     743        38217 :       tmp = fold_build3_loc (loc, COND_EXPR, void_type_node, cond, body,
     744              :                              build_empty_stmt (loc));
     745        38217 :       gfc_add_expr_to_block (pblock, tmp);
     746              :     }
     747              : }
     748              : 
     749              : 
     750              : static tree
     751        20335 : trans_os_error_at (locus* where, const char* msgid, ...)
     752              : {
     753        20335 :   va_list ap;
     754        20335 :   tree result;
     755              : 
     756        20335 :   va_start (ap, msgid);
     757        20335 :   result = trans_runtime_error_vararg (gfor_fndecl_os_error_at,
     758              :                                        where, msgid, ap);
     759        20335 :   va_end (ap);
     760        20335 :   return result;
     761              : }
     762              : 
     763              : 
     764              : 
     765              : /* Call malloc to allocate size bytes of memory, with special conditions:
     766              :       + if size == 0, return a malloced area of size 1,
     767              :       + if malloc returns NULL, issue a runtime error.  */
     768              : tree
     769        28034 : gfc_call_malloc (stmtblock_t * block, tree type, tree size)
     770              : {
     771        28034 :   tree tmp, malloc_result, null_result, res, malloc_tree;
     772        28034 :   stmtblock_t block2;
     773              : 
     774              :   /* Create a variable to hold the result.  */
     775        28034 :   res = gfc_create_var (prvoid_type_node, NULL);
     776              : 
     777              :   /* Call malloc.  */
     778        28034 :   gfc_start_block (&block2);
     779              : 
     780        28034 :   if (size == NULL_TREE)
     781            1 :     size = build_int_cst (size_type_node, 1);
     782              : 
     783        28034 :   size = fold_convert (size_type_node, size);
     784        28034 :   size = fold_build2_loc (input_location, MAX_EXPR, size_type_node, size,
     785              :                           build_int_cst (size_type_node, 1));
     786              : 
     787        28034 :   malloc_tree = builtin_decl_explicit (BUILT_IN_MALLOC);
     788        28034 :   gfc_add_modify (&block2, res,
     789              :                   fold_convert (prvoid_type_node,
     790              :                                 build_call_expr_loc (input_location,
     791              :                                                      malloc_tree, 1, size)));
     792              : 
     793              :   /* Optionally check whether malloc was successful.  */
     794        28034 :   if (gfc_option.rtcheck & GFC_RTCHECK_MEM)
     795              :     {
     796          137 :       null_result = fold_build2_loc (input_location, EQ_EXPR,
     797              :                                      logical_type_node, res,
     798              :                                      build_int_cst (pvoid_type_node, 0));
     799          137 :       tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
     800              :                              null_result,
     801              :                              trans_os_error_at (NULL,
     802              :                                                 "Error allocating %lu bytes",
     803              :                                                 fold_convert
     804              :                                                 (long_unsigned_type_node,
     805              :                                                  size)),
     806              :                              build_empty_stmt (input_location));
     807          137 :       gfc_add_expr_to_block (&block2, tmp);
     808              :     }
     809              : 
     810        28034 :   malloc_result = gfc_finish_block (&block2);
     811        28034 :   gfc_add_expr_to_block (block, malloc_result);
     812              : 
     813        28034 :   if (type != NULL)
     814        21570 :     res = fold_convert (type, res);
     815        28034 :   return res;
     816              : }
     817              : 
     818              : 
     819              : /* Allocate memory, using an optional status argument.
     820              : 
     821              :    This function follows the following pseudo-code:
     822              : 
     823              :     void *
     824              :     allocate (size_t size, integer_type stat)
     825              :     {
     826              :       void *newmem;
     827              : 
     828              :       if (stat requested)
     829              :         stat = 0;
     830              : 
     831              :       // if cond == NULL_NULL:
     832              :       newmem = malloc (MAX (size, 1));
     833              :       // otherwise:
     834              :       newmem = <cond> ? <alt_alloc> : malloc (MAX (size, 1))
     835              :       if (newmem == NULL)
     836              :       {
     837              :         if (stat)
     838              :           *stat = LIBERROR_NO_MEMORY;
     839              :         else
     840              :           runtime_error ("Allocation would exceed memory limit");
     841              :       }
     842              :       return newmem;
     843              :     }  */
     844              : void
     845        19277 : gfc_allocate_using_malloc (stmtblock_t * block, tree pointer,
     846              :                            tree size, tree status, tree cond, tree alt_alloc,
     847              :                            tree extra_success_expr)
     848              : {
     849        19277 :   tree tmp, error_cond;
     850        19277 :   stmtblock_t on_error;
     851        19277 :   tree status_type = status ? TREE_TYPE (status) : NULL_TREE;
     852        19277 :   bool cond_is_true = cond == boolean_true_node;
     853              : 
     854              :   /* If successful and stat= is given, set status to 0.  */
     855        18955 :   if (status != NULL_TREE)
     856          322 :       gfc_add_expr_to_block (block,
     857              :              fold_build2_loc (input_location, MODIFY_EXPR, status_type,
     858              :                               status, build_int_cst (status_type, 0)));
     859              : 
     860              :   /* The allocation itself.  */
     861        19277 :   size = fold_convert (size_type_node, size);
     862        19277 :   tmp = fold_build2_loc (input_location, MAX_EXPR, size_type_node,
     863              :                          size, build_int_cst (size_type_node, 1));
     864              : 
     865        19277 :   if (!cond_is_true)
     866        19216 :     tmp = build_call_expr_loc (input_location,
     867              :                                builtin_decl_explicit (BUILT_IN_MALLOC), 1, tmp);
     868              :   else
     869              :     tmp = alt_alloc;
     870              : 
     871        19277 :   if (!cond_is_true && cond)
     872            0 :     tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
     873              :                       alt_alloc, tmp);
     874              : 
     875        19277 :   gfc_add_modify (block, pointer, fold_convert (TREE_TYPE (pointer), tmp));
     876              : 
     877              :   /* What to do in case of error.  */
     878        19277 :   gfc_start_block (&on_error);
     879        19277 :   if (status != NULL_TREE)
     880              :     {
     881          322 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type, status,
     882              :                              build_int_cst (status_type, LIBERROR_NO_MEMORY));
     883          322 :       gfc_add_expr_to_block (&on_error, tmp);
     884              :     }
     885              :   else
     886              :     {
     887              :       /* Here, os_error_at already implies PRED_NORETURN.  */
     888        18955 :       tree lusize = fold_convert (long_unsigned_type_node, size);
     889        18955 :       tmp = trans_os_error_at (NULL, "Error allocating %lu bytes", lusize);
     890        18955 :       gfc_add_expr_to_block (&on_error, tmp);
     891              :     }
     892              : 
     893        19277 :   error_cond = fold_build2_loc (input_location, EQ_EXPR,
     894              :                                 logical_type_node, pointer,
     895              :                                 build_int_cst (prvoid_type_node, 0));
     896        38493 :   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
     897              :                          gfc_unlikely (error_cond, PRED_FORTRAN_FAIL_ALLOC),
     898              :                          gfc_finish_block (&on_error),
     899              :                          extra_success_expr
     900              :                          ? extra_success_expr
     901        19216 :                          : build_empty_stmt (input_location));
     902              : 
     903        19277 :   gfc_add_expr_to_block (block, tmp);
     904        19277 : }
     905              : 
     906              : 
     907              : /* Allocate memory, using an optional status argument.
     908              : 
     909              :    This function follows the following pseudo-code:
     910              : 
     911              :     void *
     912              :     allocate (size_t size, void** token, int *stat, char* errmsg, int errlen)
     913              :     {
     914              :       void *newmem;
     915              : 
     916              :       newmem = _caf_register (size, regtype, token, &stat, errmsg, errlen);
     917              :       return newmem;
     918              :     }  */
     919              : void
     920          825 : gfc_allocate_using_caf_lib (stmtblock_t * block, tree pointer, tree size,
     921              :                             tree token, tree status, tree errmsg, tree errlen,
     922              :                             gfc_coarray_regtype alloc_type)
     923              : {
     924          825 :   tree tmp, pstat;
     925              : 
     926          825 :   gcc_assert (token != NULL_TREE);
     927              : 
     928              :   /* The allocation itself.  */
     929          825 :   if (status == NULL_TREE)
     930          793 :     pstat  = null_pointer_node;
     931              :   else
     932           32 :     pstat  = gfc_build_addr_expr (NULL_TREE, status);
     933              : 
     934          825 :   if (errmsg == NULL_TREE)
     935              :     {
     936          793 :       gcc_assert(errlen == NULL_TREE);
     937          793 :       errmsg = null_pointer_node;
     938          793 :       errlen = integer_zero_node;
     939              :     }
     940              : 
     941          825 :   size = fold_convert (size_type_node, size);
     942          825 :   tmp = build_call_expr_loc (input_location,
     943              :              gfor_fndecl_caf_register, 7,
     944              :              fold_build2_loc (input_location,
     945              :                               MAX_EXPR, size_type_node, size, size_one_node),
     946          825 :              build_int_cst (integer_type_node, alloc_type),
     947              :              token, gfc_build_addr_expr (pvoid_type_node, pointer),
     948              :              pstat, errmsg, errlen);
     949              : 
     950          825 :   gfc_add_expr_to_block (block, tmp);
     951              : 
     952              :   /* It guarantees memory consistency within the same segment */
     953          825 :   tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
     954          825 :   tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
     955              :                     gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
     956              :                     tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
     957          825 :   ASM_VOLATILE_P (tmp) = 1;
     958          825 :   gfc_add_expr_to_block (block, tmp);
     959          825 : }
     960              : 
     961              : 
     962              : /* Generate code for an ALLOCATE statement when the argument is an
     963              :    allocatable variable.  If the variable is currently allocated, it is an
     964              :    error to allocate it again.
     965              : 
     966              :    This function follows the following pseudo-code:
     967              : 
     968              :     void *
     969              :     allocate_allocatable (void *mem, size_t size, integer_type stat)
     970              :     {
     971              :       if (mem == NULL)
     972              :         return allocate (size, stat);
     973              :       else
     974              :       {
     975              :         if (stat)
     976              :           stat = LIBERROR_ALLOCATION;
     977              :         else
     978              :           runtime_error ("Attempting to allocate already allocated variable");
     979              :       }
     980              :     }
     981              : 
     982              :     expr must be set to the original expression being allocated for its locus
     983              :     and variable name in case a runtime error has to be printed.  */
     984              : void
     985        14490 : gfc_allocate_allocatable (stmtblock_t * block, tree mem, tree size,
     986              :                           tree token, tree status, tree errmsg, tree errlen,
     987              :                           tree label_finish, gfc_expr* expr, int corank,
     988              :                           tree cond, tree alt_alloc, tree extra_success_expr,
     989              :                           tree var_overflow)
     990              : {
     991        14490 :   stmtblock_t alloc_block;
     992        14490 :   tree tmp, null_mem, alloc, error;
     993        14490 :   tree type = TREE_TYPE (mem);
     994        14490 :   symbol_attribute caf_attr;
     995        14490 :   bool need_assign = false, refs_comp = false;
     996        14490 :   gfc_coarray_regtype caf_alloc_type = GFC_CAF_COARRAY_ALLOC;
     997              : 
     998        14490 :   size = fold_convert (size_type_node, size);
     999        14490 :   null_mem = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
    1000              :                                             logical_type_node, mem,
    1001              :                                             build_int_cst (type, 0)),
    1002              :                            PRED_FORTRAN_REALLOC);
    1003              : 
    1004              :   /* If mem is NULL, we call gfc_allocate_using_malloc or
    1005              :      gfc_allocate_using_lib.  */
    1006        14490 :   gfc_start_block (&alloc_block);
    1007              : 
    1008        14490 :   if (flag_coarray == GFC_FCOARRAY_LIB)
    1009          554 :     caf_attr = gfc_caf_attr (expr, true, &refs_comp);
    1010              : 
    1011        14490 :   if (flag_coarray == GFC_FCOARRAY_LIB
    1012          554 :       && (corank > 0 || caf_attr.codimension))
    1013              :     {
    1014          493 :       tree cond2, sub_caf_tree;
    1015          493 :       gfc_se se;
    1016          493 :       bool compute_special_caf_types_size = false;
    1017              : 
    1018          493 :       if (expr->ts.type == BT_DERIVED
    1019          108 :           && expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    1020           10 :           && expr->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)
    1021              :         {
    1022              :           compute_special_caf_types_size = true;
    1023              :           caf_alloc_type = GFC_CAF_LOCK_ALLOC;
    1024              :         }
    1025          487 :       else if (expr->ts.type == BT_DERIVED
    1026          102 :                && expr->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    1027            4 :                && expr->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)
    1028              :         {
    1029              :           compute_special_caf_types_size = true;
    1030              :           caf_alloc_type = GFC_CAF_EVENT_ALLOC;
    1031              :         }
    1032          483 :       else if (!caf_attr.coarray_comp && refs_comp)
    1033              :         /* Only allocatable components in a derived type coarray can be
    1034              :            allocate only.  */
    1035          493 :         caf_alloc_type = GFC_CAF_COARRAY_ALLOC_ALLOCATE_ONLY;
    1036              : 
    1037          493 :       gfc_init_se (&se, NULL);
    1038          493 :       sub_caf_tree = gfc_get_ultimate_alloc_ptr_comps_caf_token (&se, expr);
    1039          493 :       if (sub_caf_tree == NULL_TREE)
    1040          277 :         sub_caf_tree = token;
    1041              : 
    1042              :       /* When mem is an array ref, then strip the .data-ref.  */
    1043          493 :       if (TREE_CODE (mem) == COMPONENT_REF
    1044          493 :           && !(GFC_ARRAY_TYPE_P (TREE_TYPE (mem))))
    1045          493 :         tmp = TREE_OPERAND (mem, 0);
    1046              :       else
    1047              :         tmp = mem;
    1048              : 
    1049          493 :       if (!(GFC_ARRAY_TYPE_P (TREE_TYPE (tmp))
    1050           48 :             && TYPE_LANG_SPECIFIC (TREE_TYPE (tmp))->corank == 0)
    1051          541 :           && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (tmp)))
    1052              :         {
    1053          100 :           symbol_attribute attr;
    1054              : 
    1055          100 :           gfc_clear_attr (&attr);
    1056          100 :           tmp = gfc_conv_scalar_to_descriptor (&se, mem, attr);
    1057          100 :           need_assign = true;
    1058              :         }
    1059          493 :       gfc_add_block_to_block (&alloc_block, &se.pre);
    1060              : 
    1061              :       /* In the front end, we represent the lock variable as pointer. However,
    1062              :          the FE only passes the pointer around and leaves the actual
    1063              :          representation to the library. Hence, we have to convert back to the
    1064              :          number of elements.  */
    1065          493 :       if (compute_special_caf_types_size)
    1066           10 :         size = fold_build2_loc (input_location, TRUNC_DIV_EXPR, size_type_node,
    1067           10 :                                 size, TYPE_SIZE_UNIT (ptr_type_node));
    1068              : 
    1069          493 :       gfc_allocate_using_caf_lib (&alloc_block, tmp, size, sub_caf_tree,
    1070              :                                   status, errmsg, errlen, caf_alloc_type);
    1071          493 :       if (need_assign)
    1072          100 :         gfc_add_modify (&alloc_block, mem, fold_convert (TREE_TYPE (mem),
    1073              :                                            gfc_conv_descriptor_data_get (tmp)));
    1074          493 :       if (status != NULL_TREE)
    1075              :         {
    1076           32 :           TREE_USED (label_finish) = 1;
    1077           32 :           tmp = build1_v (GOTO_EXPR, label_finish);
    1078           32 :           cond2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    1079           32 :                                    status, build_zero_cst (TREE_TYPE (status)));
    1080           32 :           tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    1081              :                                  gfc_unlikely (cond2, PRED_FORTRAN_FAIL_ALLOC),
    1082              :                                  tmp, build_empty_stmt (input_location));
    1083           32 :           gfc_add_expr_to_block (&alloc_block, tmp);
    1084              :         }
    1085          493 :     }
    1086              :   else
    1087        13997 :     gfc_allocate_using_malloc (&alloc_block, mem, size, status,
    1088              :                                cond, alt_alloc, extra_success_expr);
    1089              : 
    1090        14490 :   alloc = gfc_finish_block (&alloc_block);
    1091              : 
    1092              :   /* If mem is not NULL, we issue a runtime error or set the
    1093              :      status variable.  */
    1094        14490 :   if (expr)
    1095              :     {
    1096        14490 :       tree varname;
    1097              : 
    1098        14490 :       gcc_assert (expr->expr_type == EXPR_VARIABLE && expr->symtree);
    1099        14490 :       varname = gfc_build_cstring_const (expr->symtree->name);
    1100        14490 :       varname = gfc_build_addr_expr (pchar_type_node, varname);
    1101              : 
    1102        14490 :       error = gfc_trans_runtime_error (true, &expr->where,
    1103              :                                        "Attempting to allocate already"
    1104              :                                        " allocated variable '%s'",
    1105              :                                        varname);
    1106              :     }
    1107              :   else
    1108            0 :     error = gfc_trans_runtime_error (true, NULL,
    1109              :                                      "Attempting to allocate already allocated"
    1110              :                                      " variable");
    1111              : 
    1112        14490 :   if (status != NULL_TREE)
    1113              :     {
    1114          330 :       tree status_type = TREE_TYPE (status);
    1115              : 
    1116          330 :       error = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
    1117              :               status, build_int_cst (status_type, LIBERROR_ALLOCATION));
    1118              :     }
    1119              : 
    1120              :   /* When STATUS and a size overflow check are present, we give precedence to
    1121              :      "already allocated variable" error over overflow error.  */
    1122        14490 :   if (status != NULL_TREE && var_overflow != NULL_TREE)
    1123              :     {
    1124          223 :       tree status_type = TREE_TYPE (status);
    1125          223 :       tree cond = gfc_unlikely (fold_build2_loc (input_location, NE_EXPR,
    1126              :                                                  logical_type_node,
    1127              :                                                  var_overflow,
    1128              :                                                  integer_zero_node),
    1129              :                                 PRED_FORTRAN_OVERFLOW);
    1130          223 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
    1131              :                              status, build_int_cst (status_type,
    1132              :                                                     LIBERROR_NO_MEMORY));
    1133          223 :       alloc = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    1134              :                                cond, tmp, alloc);
    1135              :     }
    1136              : 
    1137        14490 :   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, null_mem,
    1138              :                          error, alloc);
    1139        14490 :   gfc_add_expr_to_block (block, tmp);
    1140        14490 : }
    1141              : 
    1142              : 
    1143              : /* Free a given variable.  */
    1144              : 
    1145              : tree
    1146        27022 : gfc_call_free (tree var)
    1147              : {
    1148        27022 :   return build_call_expr_loc (input_location,
    1149              :                               builtin_decl_explicit (BUILT_IN_FREE),
    1150        27022 :                               1, fold_convert (pvoid_type_node, var));
    1151              : }
    1152              : 
    1153              : 
    1154              : /* Generate the data reference to the finalization procedure pointer associated
    1155              :    with the expression passed as argument in EXPR.  */
    1156              : 
    1157              : static void
    1158         5699 : get_final_proc_ref (gfc_se *se, gfc_expr *expr, tree class_container)
    1159              : {
    1160         5699 :   gfc_expr *final_wrapper = NULL;
    1161              : 
    1162         5699 :   gcc_assert (expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS);
    1163              : 
    1164         5699 :   bool using_class_container = false;
    1165         5699 :   if (expr->ts.type == BT_DERIVED)
    1166          973 :     gfc_is_finalizable (expr->ts.u.derived, &final_wrapper);
    1167         4726 :   else if (class_container)
    1168              :     {
    1169          308 :       using_class_container = true;
    1170          308 :       se->expr = gfc_class_vtab_final_get (class_container);
    1171              :     }
    1172              :   else
    1173              :     {
    1174         4418 :       final_wrapper = gfc_copy_expr (expr);
    1175         4418 :       gfc_add_vptr_component (final_wrapper);
    1176         4418 :       gfc_add_final_component (final_wrapper);
    1177              :     }
    1178              : 
    1179         5699 :   if (!using_class_container)
    1180              :     {
    1181         5391 :       gcc_assert (final_wrapper->expr_type == EXPR_VARIABLE);
    1182              : 
    1183         5391 :       gfc_conv_expr (se, final_wrapper);
    1184              :     }
    1185              : 
    1186         5699 :   if (POINTER_TYPE_P (TREE_TYPE (se->expr)))
    1187         1268 :     se->expr = build_fold_indirect_ref_loc (input_location, se->expr);
    1188              : 
    1189         5699 :   if (expr->ts.type != BT_DERIVED && !using_class_container)
    1190         4418 :     gfc_free_expr (final_wrapper);
    1191         5699 : }
    1192              : 
    1193              : 
    1194              : /* Generate the code to obtain the value of the element size of the expression
    1195              :    passed as argument in EXPR.  */
    1196              : 
    1197              : static void
    1198         5699 : get_elem_size (gfc_se *se, gfc_expr *expr, tree class_container)
    1199              : {
    1200         5699 :   gcc_assert (expr->ts.type == BT_DERIVED || expr->ts.type == BT_CLASS);
    1201              : 
    1202         5699 :   if (expr->ts.type == BT_DERIVED)
    1203              :     {
    1204          973 :       se->expr = gfc_typenode_for_spec (&expr->ts);
    1205          973 :       se->expr = TYPE_SIZE_UNIT (se->expr);
    1206          973 :       se->expr = fold_convert (gfc_array_index_type, se->expr);
    1207              :     }
    1208         4726 :   else if (class_container)
    1209          308 :     se->expr = gfc_class_vtab_size_get (class_container);
    1210              :   else
    1211              :     {
    1212         4418 :       gfc_expr *class_size = gfc_copy_expr (expr);
    1213         4418 :       gfc_add_vptr_component (class_size);
    1214         4418 :       gfc_add_size_component (class_size);
    1215              : 
    1216         4418 :       gfc_conv_expr (se, class_size);
    1217         4418 :       gcc_assert (se->post.head == NULL_TREE);
    1218         4418 :       gfc_free_expr (class_size);
    1219              :     }
    1220         5699 : }
    1221              : 
    1222              : 
    1223              : /* Generate the data reference (array) descriptor corresponding to the
    1224              :    expression passed as argument in VAR.  */
    1225              : 
    1226              : static void
    1227         5699 : get_var_descr (gfc_se *se, gfc_expr *var, tree class_container)
    1228              : {
    1229         5699 :   gfc_se tmp_se;
    1230              : 
    1231         5699 :   gcc_assert (var);
    1232              : 
    1233         5699 :   gfc_init_se (&tmp_se, NULL);
    1234              : 
    1235         5699 :   if (var->ts.type == BT_DERIVED)
    1236              :     {
    1237          973 :       tmp_se.want_pointer = 1;
    1238          973 :       if (var->rank)
    1239              :         {
    1240          278 :           tmp_se.descriptor_only = 1;
    1241          278 :           gfc_conv_expr_descriptor (&tmp_se, var);
    1242              :         }
    1243              :       else
    1244          695 :         gfc_conv_expr (&tmp_se, var);
    1245              :     }
    1246         4726 :   else if (class_container)
    1247          308 :     tmp_se.expr = gfc_class_data_get (class_container);
    1248              :   else
    1249              :     {
    1250         4418 :       gfc_expr *array_expr;
    1251              : 
    1252         4418 :       array_expr = gfc_copy_expr (var);
    1253              : 
    1254         4418 :       tmp_se.want_pointer = 1;
    1255         4418 :       if (array_expr->rank)
    1256              :         {
    1257         2314 :           gfc_add_class_array_ref (array_expr);
    1258         2314 :           tmp_se.descriptor_only = 1;
    1259         2314 :           gfc_conv_expr_descriptor (&tmp_se, array_expr);
    1260              :         }
    1261              :       else
    1262              :         {
    1263         2104 :           gfc_add_data_component (array_expr);
    1264         2104 :           gfc_conv_expr (&tmp_se, array_expr);
    1265         2104 :           gcc_assert (tmp_se.post.head == NULL_TREE);
    1266              :         }
    1267         4418 :       gfc_free_expr (array_expr);
    1268              :     }
    1269              : 
    1270         5699 :   if (var->rank == 0)
    1271              :     {
    1272         2973 :       if (var->ts.type == BT_DERIVED
    1273         2973 :           || !gfc_is_coarray (var))
    1274              :         {
    1275              :           /* No copy back needed, hence set attr's allocatable/pointer
    1276              :              to zero.  */
    1277         2931 :           symbol_attribute attr;
    1278         2931 :           gfc_clear_attr (&attr);
    1279         2931 :           tmp_se.expr = gfc_conv_scalar_to_descriptor (&tmp_se, tmp_se.expr,
    1280              :                                                        attr);
    1281              :         }
    1282         2973 :       gcc_assert (tmp_se.post.head == NULL_TREE);
    1283              :     }
    1284              : 
    1285         5699 :   if (!POINTER_TYPE_P (TREE_TYPE (tmp_se.expr)))
    1286         3065 :     tmp_se.expr = gfc_build_addr_expr (NULL, tmp_se.expr);
    1287              : 
    1288         5699 :   gfc_add_block_to_block (&se->pre, &tmp_se.pre);
    1289         5699 :   gfc_add_block_to_block (&se->post, &tmp_se.post);
    1290         5699 :   se->expr = tmp_se.expr;
    1291         5699 : }
    1292              : 
    1293              : 
    1294              : static void
    1295         1270 : get_vptr (gfc_se *se, gfc_expr *expr, tree class_container)
    1296              : {
    1297         1270 :   if (class_container)
    1298           54 :     se->expr = gfc_class_vptr_get (class_container);
    1299              :   else
    1300              :     {
    1301         1216 :       gfc_expr *vptr_expr = gfc_copy_expr (expr);
    1302         1216 :       gfc_add_vptr_component (vptr_expr);
    1303              : 
    1304         1216 :       gfc_se tmp_se;
    1305         1216 :       gfc_init_se (&tmp_se, NULL);
    1306         1216 :       tmp_se.want_pointer = 1;
    1307         1216 :       gfc_conv_expr (&tmp_se, vptr_expr);
    1308         1216 :       gfc_free_expr (vptr_expr);
    1309              : 
    1310         1216 :       gfc_add_block_to_block (&se->pre, &tmp_se.pre);
    1311         1216 :       gfc_add_block_to_block (&se->post, &tmp_se.post);
    1312         1216 :       se->expr = tmp_se.expr;
    1313              :     }
    1314         1270 : }
    1315              : 
    1316              : 
    1317              : bool
    1318         4892 : gfc_add_comp_finalizer_call (stmtblock_t *block, tree decl, gfc_component *comp,
    1319              :                              bool fini_coarray)
    1320              : {
    1321         4892 :   gfc_se se;
    1322         4892 :   stmtblock_t block2;
    1323         4892 :   tree final_fndecl, size, array, tmp, cond;
    1324         4892 :   symbol_attribute attr;
    1325         4892 :   gfc_expr *final_expr = NULL;
    1326              : 
    1327         4892 :   if (comp->ts.type != BT_DERIVED && comp->ts.type != BT_CLASS)
    1328              :     return false;
    1329              : 
    1330         4892 :   gfc_init_block (&block2);
    1331              : 
    1332         4892 :   if (comp->ts.type == BT_DERIVED)
    1333              :     {
    1334         3546 :       if (comp->attr.pointer)
    1335              :         return false;
    1336              : 
    1337         3546 :       gfc_is_finalizable (comp->ts.u.derived, &final_expr);
    1338         3546 :       if (!final_expr)
    1339              :         return false;
    1340              : 
    1341           81 :       gfc_init_se (&se, NULL);
    1342           81 :       gfc_conv_expr (&se, final_expr);
    1343           81 :       final_fndecl = se.expr;
    1344           81 :       size = gfc_typenode_for_spec (&comp->ts);
    1345           81 :       size = TYPE_SIZE_UNIT (size);
    1346           81 :       size = fold_convert (gfc_array_index_type, size);
    1347              : 
    1348           81 :       array = decl;
    1349              :     }
    1350              :   else /* comp->ts.type == BT_CLASS.  */
    1351              :     {
    1352         1346 :       if (CLASS_DATA (comp)->attr.class_pointer)
    1353              :         return false;
    1354              : 
    1355         1346 :       gfc_is_finalizable (CLASS_DATA (comp)->ts.u.derived, &final_expr);
    1356         1346 :       final_fndecl = gfc_class_vtab_final_get (decl);
    1357         1346 :       size = gfc_class_vtab_size_get (decl);
    1358         1346 :       array = gfc_class_data_get (decl);
    1359              :     }
    1360              : 
    1361         1427 :   if (comp->attr.allocatable
    1362         1346 :       || (comp->ts.type == BT_CLASS && CLASS_DATA (comp)->attr.allocatable))
    1363              :     {
    1364         1427 :       tmp = GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array))
    1365         1427 :             ?  gfc_conv_descriptor_data_get (array) : array;
    1366         1427 :       cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    1367         1427 :                             tmp, fold_convert (TREE_TYPE (tmp),
    1368              :                                                  null_pointer_node));
    1369              :       /* The component is deallocated after it is finalized.  */
    1370         1427 :       fini_coarray = true;
    1371              :     }
    1372              :   else
    1373            0 :     cond = logical_true_node;
    1374              : 
    1375         1427 :   if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (array)))
    1376              :     {
    1377          811 :       gfc_clear_attr (&attr);
    1378          811 :       gfc_init_se (&se, NULL);
    1379          811 :       array = gfc_conv_scalar_to_descriptor (&se, array, attr);
    1380          811 :       gfc_add_block_to_block (&block2, &se.pre);
    1381          811 :       gcc_assert (se.post.head == NULL_TREE);
    1382              :     }
    1383              : 
    1384         1427 :   if (!POINTER_TYPE_P (TREE_TYPE (array)))
    1385         1427 :     array = gfc_build_addr_expr (NULL, array);
    1386              : 
    1387         1427 :   if (!final_expr)
    1388              :     {
    1389         1344 :       tmp = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    1390              :                              final_fndecl,
    1391         1344 :                              fold_convert (TREE_TYPE (final_fndecl),
    1392              :                                            null_pointer_node));
    1393         1344 :       cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    1394              :                               logical_type_node, cond, tmp);
    1395              :     }
    1396              : 
    1397         1427 :   if (POINTER_TYPE_P (TREE_TYPE (final_fndecl)))
    1398         1427 :     final_fndecl = build_fold_indirect_ref_loc (input_location, final_fndecl);
    1399              : 
    1400         1427 :   tmp = build_call_expr_loc (input_location,
    1401              :                              final_fndecl, 3, array,
    1402              :                              size, fini_coarray ? boolean_true_node
    1403              :                                                 : boolean_false_node);
    1404         1427 :   gfc_add_expr_to_block (&block2, tmp);
    1405         1427 :   tmp = gfc_finish_block (&block2);
    1406              : 
    1407         1427 :   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node, cond, tmp,
    1408              :                          build_empty_stmt (input_location));
    1409         1427 :   gfc_add_expr_to_block (block, tmp);
    1410              : 
    1411         1427 :   return true;
    1412              : }
    1413              : 
    1414              : 
    1415              : /* Add a call to the finalizer, using the passed *expr. Returns
    1416              :    true when a finalizer call has been inserted.  FINI_COARRAY is true
    1417              :    when EXPR is about to be deallocated.  */
    1418              : 
    1419              : bool
    1420        33218 : gfc_add_finalizer_call (stmtblock_t *block, gfc_expr *expr2,
    1421              :                         tree class_container, bool fini_coarray)
    1422              : {
    1423        33218 :   tree tmp;
    1424        33218 :   gfc_ref *ref;
    1425        33218 :   gfc_expr *expr;
    1426              : 
    1427        33218 :   if (!expr2 || (expr2->ts.type != BT_DERIVED && expr2->ts.type != BT_CLASS))
    1428              :     return false;
    1429              : 
    1430              :   /* Finalization of these temporaries is made by explicit calls in
    1431              :      resolve.cc(generate_component_assignments).  */
    1432         8147 :   if (expr2->expr_type == EXPR_VARIABLE
    1433         8147 :       && expr2->symtree->n.sym->name[0] == '_'
    1434          109 :       && expr2->ts.type == BT_DERIVED
    1435           55 :       && expr2->ts.u.derived->attr.defined_assign_comp)
    1436              :     return false;
    1437              : 
    1438         8098 :   if (expr2->ts.type == BT_DERIVED
    1439         8098 :       && !gfc_is_finalizable (expr2->ts.u.derived, NULL))
    1440              :     return false;
    1441              : 
    1442              :   /* If we have a class array, we need go back to the class
    1443              :      container.  */
    1444         5699 :   expr = gfc_copy_expr (expr2);
    1445              : 
    1446         5699 :   if (expr->ref && expr->ref->next && !expr->ref->next->next
    1447         1229 :       && expr->ref->next->type == REF_ARRAY
    1448         1144 :       && expr->ref->type == REF_COMPONENT
    1449         1144 :       && strcmp (expr->ref->u.c.component->name, "_data") == 0)
    1450              :     {
    1451         1101 :       gfc_free_ref_list (expr->ref);
    1452         1101 :       expr->ref = NULL;
    1453              :     }
    1454              :   else
    1455         7006 :     for (ref = expr->ref; ref; ref = ref->next)
    1456         2408 :       if (ref->next && ref->next->next && !ref->next->next->next
    1457          406 :          && ref->next->next->type == REF_ARRAY
    1458          387 :          && ref->next->type == REF_COMPONENT
    1459          387 :          && strcmp (ref->next->u.c.component->name, "_data") == 0)
    1460              :        {
    1461          387 :          gfc_free_ref_list (ref->next);
    1462          387 :          ref->next = NULL;
    1463              :        }
    1464              : 
    1465         5699 :   if (expr->ts.type == BT_CLASS && (!expr2->rank || !expr2->corank)
    1466         4679 :       && !expr2->ref && CLASS_DATA (expr2->symtree->n.sym)->as)
    1467              :     {
    1468            3 :       expr->rank = CLASS_DATA (expr2->symtree->n.sym)->as->rank;
    1469            3 :       expr->corank = CLASS_DATA (expr2->symtree->n.sym)->as->corank;
    1470              :     }
    1471              : 
    1472         5699 :   stmtblock_t tmp_block;
    1473         5699 :   gfc_start_block (&tmp_block);
    1474              : 
    1475         5699 :   gfc_se final_se;
    1476         5699 :   gfc_init_se (&final_se, NULL);
    1477         5699 :   get_final_proc_ref (&final_se, expr, class_container);
    1478         5699 :   gfc_add_block_to_block (block, &final_se.pre);
    1479              : 
    1480         5699 :   gfc_se size_se;
    1481         5699 :   gfc_init_se (&size_se, NULL);
    1482         5699 :   get_elem_size (&size_se, expr, class_container);
    1483         5699 :   gfc_add_block_to_block (&tmp_block, &size_se.pre);
    1484              : 
    1485         5699 :   gfc_se desc_se;
    1486         5699 :   gfc_init_se (&desc_se, NULL);
    1487         5699 :   get_var_descr (&desc_se, expr, class_container);
    1488         5699 :   gfc_add_block_to_block (&tmp_block, &desc_se.pre);
    1489              : 
    1490         5699 :   tmp = build_call_expr_loc (input_location, final_se.expr, 3,
    1491              :                              desc_se.expr, size_se.expr,
    1492              :                              fini_coarray ? boolean_true_node
    1493              :                                           : boolean_false_node);
    1494              : 
    1495         5699 :   gfc_add_expr_to_block (&tmp_block, tmp);
    1496              : 
    1497         5699 :   gfc_add_block_to_block (&tmp_block, &desc_se.post);
    1498         5699 :   gfc_add_block_to_block (&tmp_block, &size_se.post);
    1499              : 
    1500         5699 :   tmp = gfc_finish_block (&tmp_block);
    1501              : 
    1502         5699 :   if (expr->ts.type == BT_CLASS
    1503         5699 :       && !gfc_is_finalizable (expr->ts.u.derived, NULL))
    1504              :     {
    1505         4726 :       tree cond;
    1506              : 
    1507         4726 :       tree ptr = gfc_build_addr_expr (NULL_TREE, final_se.expr);
    1508              : 
    1509         4726 :       cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    1510         4726 :                               ptr, build_int_cst (TREE_TYPE (ptr), 0));
    1511              : 
    1512              :       /* For CLASS(*) not only sym->_vtab->_final can be NULL
    1513              :          but already sym->_vtab itself.  */
    1514         4726 :       if (UNLIMITED_POLY (expr))
    1515              :         {
    1516         1270 :           tree cond2;
    1517         1270 :           gfc_se vptr_se;
    1518              : 
    1519         1270 :           gfc_init_se (&vptr_se, NULL);
    1520         1270 :           get_vptr (&vptr_se, expr, class_container);
    1521              : 
    1522         1270 :           cond2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    1523              :                                    vptr_se.expr,
    1524         1270 :                                    build_int_cst (TREE_TYPE (vptr_se.expr), 0));
    1525         1270 :           cond = fold_build2_loc (input_location, TRUTH_ANDIF_EXPR,
    1526              :                                   logical_type_node, cond2, cond);
    1527              :         }
    1528              : 
    1529         4726 :       tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    1530              :                              cond, tmp, build_empty_stmt (input_location));
    1531              :     }
    1532              : 
    1533         5699 :   gfc_add_expr_to_block (block, tmp);
    1534         5699 :   gfc_add_block_to_block (block, &final_se.post);
    1535         5699 :   gfc_free_expr (expr);
    1536              : 
    1537         5699 :   return true;
    1538              : }
    1539              : 
    1540              : 
    1541              :   /* F2018 (7.5.6.3): "When an intrinsic assignment statement is executed
    1542              :      (10.2.1.3), if the variable is not an unallocated allocatable variable,
    1543              :      it is finalized after evaluation of expr and before the definition of
    1544              :      the variable. If the variable is an allocated allocatable variable, or
    1545              :      has an allocated allocatable subobject, that would be deallocated by
    1546              :      intrinsic assignment, the finalization occurs before the deallocation */
    1547              : 
    1548              : bool
    1549       325387 : gfc_assignment_finalizer_call (gfc_se *lse, gfc_expr *expr1, bool init_flag)
    1550              : {
    1551       325387 :   symbol_attribute lhs_attr;
    1552       325387 :   tree final_expr;
    1553       325387 :   tree ptr;
    1554       325387 :   tree cond;
    1555       325387 :   gfc_se se;
    1556       325387 :   gfc_symbol *sym = expr1->symtree->n.sym;
    1557       325387 :   gfc_ref *ref = expr1->ref;
    1558       325387 :   stmtblock_t final_block;
    1559       325387 :   gfc_init_block (&final_block);
    1560       325387 :   gfc_expr *finalize_expr;
    1561       325387 :   bool class_array_ref;
    1562              : 
    1563              :   /* We have to exclude vtable procedures (_copy and _final especially), uses
    1564              :      of gfc_trans_assignment_1 in initialization and allocation before trying
    1565              :      to build a final call.  */
    1566       325387 :   if (!expr1->must_finalize
    1567         1450 :       || sym->attr.artificial
    1568         1450 :       || sym->ns->proc_name->attr.artificial
    1569         1450 :       || init_flag)
    1570              :     return false;
    1571              : 
    1572          924 :   class_array_ref = ref && ref->type == REF_COMPONENT
    1573          769 :                     && !strcmp (ref->u.c.component->name, "_data")
    1574          593 :                     && ref->next && ref->next->type == REF_ARRAY
    1575         2043 :                     && !ref->next->next;
    1576              : 
    1577         1450 :   if (class_array_ref)
    1578              :     {
    1579          581 :       finalize_expr = gfc_lval_expr_from_sym (sym);
    1580          581 :       finalize_expr->must_finalize = 1;
    1581          581 :       ref = NULL;
    1582              :     }
    1583              :   else
    1584          869 :     finalize_expr = gfc_copy_expr (expr1);
    1585              : 
    1586              :   /* F2018 7.5.6.2: Only finalizable entities are finalized.  */
    1587          365 :   if (!(expr1->ts.type == BT_DERIVED
    1588          365 :         && gfc_is_finalizable (expr1->ts.u.derived, NULL))
    1589         1450 :       && expr1->ts.type != BT_CLASS)
    1590              :       return false;
    1591              : 
    1592         1432 :   if (!gfc_may_be_finalized (sym->ts))
    1593              :     return false;
    1594              : 
    1595         1346 :   gfc_init_block (&final_block);
    1596         1346 :   bool finalizable = gfc_add_finalizer_call (&final_block, finalize_expr);
    1597         1346 :   gfc_free_expr (finalize_expr);
    1598              : 
    1599         1346 :   if (!finalizable)
    1600              :     return false;
    1601              : 
    1602         1346 :   lhs_attr = gfc_expr_attr (expr1);
    1603              : 
    1604              :   /* Check allocatable/pointer is allocated/associated.  */
    1605         1346 :   if (lhs_attr.allocatable || lhs_attr.pointer)
    1606              :     {
    1607         1095 :       if (expr1->ts.type == BT_CLASS)
    1608              :         {
    1609          987 :           ptr = gfc_get_class_from_gfc_expr (expr1);
    1610          987 :           gcc_assert (ptr != NULL_TREE);
    1611          987 :           ptr = gfc_class_data_get (ptr);
    1612          987 :           if (lhs_attr.dimension)
    1613          662 :             ptr = gfc_conv_descriptor_data_get (ptr);
    1614              :         }
    1615              :       else
    1616              :         {
    1617          108 :           gfc_init_se (&se, NULL);
    1618          108 :           if (expr1->rank)
    1619              :             {
    1620              :               /* Avoid calling trans-array.cc(set_factored_descriptor_value) by
    1621              :                  not using gfc_conv_expr_descriptor.  */
    1622           54 :               se.descriptor_only = 1;
    1623           54 :               gfc_conv_expr (&se, expr1);
    1624           54 :               ptr = gfc_conv_descriptor_data_get (se.expr);
    1625              :             }
    1626              :           else
    1627              :             {
    1628           54 :               gfc_conv_expr (&se, expr1);
    1629           54 :               ptr = gfc_build_addr_expr (NULL_TREE, se.expr);
    1630              :             }
    1631              :         }
    1632              : 
    1633         1095 :       cond = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    1634         1095 :                               ptr, build_zero_cst (TREE_TYPE (ptr)));
    1635         1095 :       final_expr = build3_loc (input_location, COND_EXPR, void_type_node,
    1636              :                                cond, gfc_finish_block (&final_block),
    1637              :                                build_empty_stmt (input_location));
    1638              :     }
    1639              :   else
    1640          251 :     final_expr = gfc_finish_block (&final_block);
    1641              : 
    1642              :   /* Check optional present.  */
    1643         1346 :   if (sym->attr.optional)
    1644              :     {
    1645            0 :       cond = gfc_conv_expr_present (sym);
    1646            0 :       final_expr = build3_loc (input_location, COND_EXPR, void_type_node,
    1647              :                                cond, final_expr,
    1648              :                                build_empty_stmt (input_location));
    1649              :     }
    1650              : 
    1651         1346 :   gfc_add_expr_to_block (&lse->finalblock, final_expr);
    1652              : 
    1653         1346 :   return true;
    1654              : }
    1655              : 
    1656              : 
    1657              : /* Finalize a TREE expression using the finalizer wrapper. The result is
    1658              :    fixed in order to prevent repeated calls.  */
    1659              : 
    1660              : void
    1661          867 : gfc_finalize_tree_expr (gfc_se *se, gfc_symbol *derived,
    1662              :                         const symbol_attribute &attr, int rank)
    1663              : {
    1664          867 :   tree vptr, final_fndecl, desc, tmp, size, is_final;
    1665          867 :   tree data_ptr, data_null, cond;
    1666          867 :   gfc_symbol *vtab;
    1667          867 :   gfc_se post_se;
    1668          867 :   bool is_class = GFC_CLASS_TYPE_P (TREE_TYPE (se->expr));
    1669              : 
    1670          867 :   if (attr.pointer)
    1671           64 :     return;
    1672              : 
    1673              :   /* Derived type function results with components that have defined
    1674              :      assignments are handled in resolve.cc(generate_component_assignments),
    1675              :      unless the assignment was replaced by a subroutine call to the
    1676              :      subroutine associated with the assignment operator.  An actual
    1677              :      argument is never the right-hand side of such an assignment.  */
    1678          858 :   if ( ! is_assign_call && ! actual_arg_level
    1679          563 :        && derived && (derived->attr.is_c_interop
    1680          161 :        || derived->attr.is_iso_c
    1681          161 :        || derived->attr.is_bind_c
    1682          161 :        || (derived->attr.extension && derived->f2k_derived
    1683           24 :            && derived->f2k_derived->tb_op[INTRINSIC_ASSIGN])
    1684          161 :        || (!derived->attr.extension
    1685          137 :            && derived->attr.defined_assign_comp)))
    1686              :     return;
    1687              : 
    1688          846 :   if (is_class)
    1689              :     {
    1690          505 :       if (!VAR_P (se->expr))
    1691              :         {
    1692            0 :           desc = gfc_evaluate_now (se->expr, &se->pre);
    1693            0 :           se->expr = desc;
    1694              :         }
    1695          505 :       desc = gfc_class_data_get (se->expr);
    1696          505 :       vptr = gfc_class_vptr_get (se->expr);
    1697              :     }
    1698          341 :   else if (derived && gfc_is_finalizable (derived, NULL))
    1699              :     {
    1700          302 :       tree type = TREE_TYPE (se->expr);
    1701          302 :       if (type && TYPE_SIZE_UNIT (type)
    1702          302 :           && integer_zerop (TYPE_SIZE_UNIT (type))
    1703          307 :           && (!rank || attr.elemental))
    1704              :         {
    1705              :           /* Any attempt to assign zero length entities, causes the gimplifier
    1706              :              all manner of problems. Instead, a variable is created to act as
    1707              :              the argument for the final call.  */
    1708            5 :           desc = gfc_create_var (type, "zero");
    1709              :         }
    1710          297 :       else if (se->direct_byref)
    1711              :         {
    1712            0 :           desc = gfc_evaluate_now (se->expr, &se->finalblock);
    1713            0 :           if (derived->attr.alloc_comp)
    1714              :             {
    1715              :               /* Need to copy allocated components and not finalize.  */
    1716            0 :               tmp = gfc_copy_alloc_comp_no_fini (derived, se->expr, desc, rank, 0);
    1717            0 :               gfc_add_expr_to_block (&se->finalblock, tmp);
    1718              :             }
    1719              :         }
    1720              :       else
    1721              :         {
    1722          297 :           desc = gfc_evaluate_now (se->expr, &se->pre);
    1723          297 :           se->expr = gfc_evaluate_now (desc, &se->pre);
    1724          297 :           if (derived->attr.alloc_comp)
    1725              :             {
    1726              :               /* Need to copy allocated components and not finalize.  */
    1727           38 :               tmp = gfc_copy_alloc_comp_no_fini (derived, se->expr, desc, rank, 0);
    1728           38 :               gfc_add_expr_to_block (&se->pre, tmp);
    1729              :             }
    1730              :         }
    1731              : 
    1732          302 :       vtab = gfc_find_derived_vtab (derived);
    1733          302 :       if (vtab->backend_decl == NULL_TREE)
    1734            6 :         vptr = gfc_get_symbol_decl (vtab);
    1735              :       else
    1736              :         vptr = vtab->backend_decl;
    1737          302 :       vptr = gfc_build_addr_expr (NULL, vptr);
    1738              :     }
    1739              :   else
    1740              :     return;
    1741              : 
    1742          807 :   size = gfc_vptr_size_get (vptr);
    1743          807 :   final_fndecl = gfc_vptr_final_get (vptr);
    1744          807 :   is_final = fold_build2_loc (input_location, NE_EXPR,
    1745              :                               logical_type_node,
    1746              :                               final_fndecl,
    1747          807 :                               fold_convert (TREE_TYPE (final_fndecl),
    1748              :                                             null_pointer_node));
    1749              : 
    1750          807 :   final_fndecl = build_fold_indirect_ref_loc (input_location,
    1751              :                                               final_fndecl);
    1752          807 :   if (!GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (desc)))
    1753              :     {
    1754          543 :       if (is_class || attr.elemental)
    1755          317 :         desc = gfc_conv_scalar_to_descriptor (se, desc, attr);
    1756              :       else
    1757              :         {
    1758          226 :           gfc_init_se (&post_se, NULL);
    1759          226 :           desc = gfc_conv_scalar_to_descriptor (&post_se, desc, attr);
    1760          226 :           gfc_add_expr_to_block (&se->pre, gfc_finish_block (&post_se.pre));
    1761              :         }
    1762              :     }
    1763              : 
    1764          807 :   if (derived && !derived->components)
    1765              :     {
    1766              :       /* All the conditions below break down for zero length derived types.  */
    1767            4 :       tmp = build_call_expr_loc (input_location, final_fndecl, 3,
    1768              :                                  gfc_build_addr_expr (NULL, desc),
    1769              :                                  size, boolean_false_node);
    1770            4 :       gfc_add_expr_to_block (&se->finalblock, tmp);
    1771            4 :       return;
    1772              :     }
    1773              : 
    1774          803 :   if (!VAR_P (desc))
    1775              :     {
    1776          228 :       tmp = gfc_create_var (TREE_TYPE (desc), "res");
    1777          228 :       if (se->direct_byref)
    1778            0 :         gfc_add_modify (&se->finalblock, tmp, desc);
    1779              :       else
    1780          228 :         gfc_add_modify (&se->pre, tmp, desc);
    1781              :       desc = tmp;
    1782              :     }
    1783              : 
    1784          803 :   data_ptr = gfc_conv_descriptor_data_get (desc);
    1785          803 :   data_null = fold_convert (TREE_TYPE (data_ptr), null_pointer_node);
    1786          803 :   cond = fold_build2_loc (input_location, NE_EXPR,
    1787              :                           logical_type_node, data_ptr, data_null);
    1788          803 :   is_final = fold_build2_loc (input_location, TRUTH_AND_EXPR,
    1789              :                               logical_type_node, is_final, cond);
    1790          803 :   tmp = build_call_expr_loc (input_location, final_fndecl, 3,
    1791              :                              gfc_build_addr_expr (NULL, desc),
    1792              :                              size, boolean_false_node);
    1793          803 :   tmp = fold_build3_loc (input_location, COND_EXPR,
    1794              :                          void_type_node, is_final, tmp,
    1795              :                          build_empty_stmt (input_location));
    1796              : 
    1797          803 :   if (is_class && se->ss && se->ss->loop)
    1798              :     {
    1799          146 :       gfc_add_expr_to_block (&se->loop->post, tmp);
    1800          146 :       tmp = fold_build3_loc (input_location, COND_EXPR,
    1801              :                              void_type_node, cond,
    1802              :                              gfc_call_free (data_ptr),
    1803              :                              build_empty_stmt (input_location));
    1804          146 :       gfc_add_expr_to_block (&se->loop->post, tmp);
    1805          146 :       gfc_conv_descriptor_data_set (&se->loop->post, desc, data_null);
    1806              :     }
    1807              :   else
    1808              :     {
    1809          657 :       gfc_add_expr_to_block (&se->finalblock, tmp);
    1810              : 
    1811              :       /* Let the scalarizer take care of freeing of temporary arrays.  */
    1812          657 :       if (attr.allocatable && !(se->loop && se->loop->temp_dim))
    1813              :         {
    1814          359 :           tmp = fold_build3_loc (input_location, COND_EXPR,
    1815              :                                  void_type_node, cond,
    1816              :                                  gfc_call_free (data_ptr),
    1817              :                                  build_empty_stmt (input_location));
    1818          359 :           gfc_add_expr_to_block (&se->finalblock, tmp);
    1819          359 :           gfc_conv_descriptor_data_set (&se->finalblock, desc, data_null);
    1820              :         }
    1821              :     }
    1822              : }
    1823              : 
    1824              : 
    1825              : /* User-deallocate; we emit the code directly from the front-end, and the
    1826              :    logic is the same as the previous library function:
    1827              : 
    1828              :     void
    1829              :     deallocate (void *pointer, GFC_INTEGER_4 * stat)
    1830              :     {
    1831              :       if (!pointer)
    1832              :         {
    1833              :           if (stat)
    1834              :             *stat = 1;
    1835              :           else
    1836              :             runtime_error ("Attempt to DEALLOCATE unallocated memory.");
    1837              :         }
    1838              :       else
    1839              :         {
    1840              :           free (pointer);
    1841              :           if (stat)
    1842              :             *stat = 0;
    1843              :         }
    1844              :     }
    1845              : 
    1846              :    In this front-end version, status doesn't have to be GFC_INTEGER_4.
    1847              :    Moreover, if CAN_FAIL is true, then we will not emit a runtime error,
    1848              :    even when no status variable is passed to us (this is used for
    1849              :    unconditional deallocation generated by the front-end at end of
    1850              :    each procedure).
    1851              : 
    1852              :    If a runtime-message is possible, `expr' must point to the original
    1853              :    expression being deallocated for its locus and variable name.
    1854              : 
    1855              :    For coarrays, "pointer" must be the array descriptor and not its
    1856              :    "data" component.
    1857              : 
    1858              :    COARRAY_DEALLOC_MODE gives the mode unregister coarrays.  Available modes are
    1859              :    the ones of GFC_CAF_DEREGTYPE, -1 when the mode for deregistration is to be
    1860              :    analyzed and set by this routine, and -2 to indicate that a non-coarray is to
    1861              :    be deallocated.  */
    1862              : tree
    1863        25356 : gfc_deallocate_with_status (tree pointer, tree status, tree errmsg, tree errlen,
    1864              :                             tree label_finish, bool can_fail, gfc_expr *expr,
    1865              :                             int coarray_dealloc_mode, tree class_container,
    1866              :                             tree add_when_allocated, tree caf_token,
    1867              :                             bool unalloc_ok)
    1868              : {
    1869        25356 :   stmtblock_t null, non_null;
    1870        25356 :   tree cond, tmp, error;
    1871        25356 :   tree status_type = NULL_TREE;
    1872        25356 :   tree token = NULL_TREE;
    1873        25356 :   tree descr = NULL_TREE;
    1874        25356 :   gfc_coarray_deregtype caf_dereg_type = GFC_CAF_COARRAY_DEREGISTER;
    1875              : 
    1876        25356 :   if (coarray_dealloc_mode >= GFC_CAF_COARRAY_ANALYZE)
    1877              :     {
    1878          499 :       if (flag_coarray == GFC_FCOARRAY_LIB)
    1879              :         {
    1880          329 :           if (caf_token)
    1881              :             {
    1882           63 :               token = caf_token;
    1883           63 :               if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (pointer)))
    1884           44 :                 pointer = gfc_conv_descriptor_data_get (pointer);
    1885              :             }
    1886              :           else
    1887              :             {
    1888          266 :               tree caf_type, caf_decl = pointer;
    1889          266 :               pointer = gfc_conv_descriptor_data_get (caf_decl);
    1890          266 :               caf_type = TREE_TYPE (caf_decl);
    1891          266 :               STRIP_NOPS (pointer);
    1892          266 :               if (GFC_DESCRIPTOR_TYPE_P (caf_type))
    1893          266 :                 token = gfc_conv_descriptor_token (caf_decl);
    1894            0 :               else if (DECL_LANG_SPECIFIC (caf_decl)
    1895            0 :                        && GFC_DECL_TOKEN (caf_decl) != NULL_TREE)
    1896            0 :                 token = GFC_DECL_TOKEN (caf_decl);
    1897              :               else
    1898              :                 {
    1899            0 :                   gcc_assert (GFC_ARRAY_TYPE_P (caf_type)
    1900              :                               && GFC_TYPE_ARRAY_CAF_TOKEN (caf_type)
    1901              :                                  != NULL_TREE);
    1902            0 :                   token = GFC_TYPE_ARRAY_CAF_TOKEN (caf_type);
    1903              :                 }
    1904              :             }
    1905              : 
    1906          329 :           if (coarray_dealloc_mode == GFC_CAF_COARRAY_ANALYZE)
    1907              :             {
    1908            4 :               bool comp_ref;
    1909            4 :               if (expr && !gfc_caf_attr (expr, false, &comp_ref).coarray_comp
    1910            4 :                   && comp_ref)
    1911            0 :                 caf_dereg_type = GFC_CAF_COARRAY_DEALLOCATE_ONLY;
    1912              :               // else do a deregister as set by default.
    1913              :             }
    1914              :           else
    1915              :             caf_dereg_type = (enum gfc_coarray_deregtype) coarray_dealloc_mode;
    1916              :         }
    1917          170 :       else if (flag_coarray == GFC_FCOARRAY_SINGLE
    1918          170 :                && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (pointer)))
    1919          170 :         pointer = gfc_conv_descriptor_data_get (pointer);
    1920              :     }
    1921        24857 :   else if (GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (pointer)))
    1922              :     {
    1923        19883 :       descr = pointer;
    1924        19883 :       pointer = gfc_conv_descriptor_data_get (pointer);
    1925              :     }
    1926              : 
    1927        25356 :   cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, pointer,
    1928        25356 :                           build_int_cst (TREE_TYPE (pointer), 0));
    1929              : 
    1930              :   /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
    1931              :      we emit a runtime error.  */
    1932        25356 :   gfc_start_block (&null);
    1933        25356 :   if (!can_fail)
    1934              :     {
    1935         8408 :       tree varname;
    1936              : 
    1937         8408 :       gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
    1938              : 
    1939         8408 :       varname = gfc_build_cstring_const (expr->symtree->name);
    1940         8408 :       varname = gfc_build_addr_expr (pchar_type_node, varname);
    1941              : 
    1942         8408 :       error = gfc_trans_runtime_error (true, &expr->where,
    1943              :                                        "Attempt to DEALLOCATE unallocated '%s'",
    1944              :                                        varname);
    1945              :     }
    1946              :   else
    1947        16948 :     error = build_empty_stmt (input_location);
    1948              : 
    1949        25356 :   if (status != NULL_TREE && !integer_zerop (status))
    1950              :     {
    1951         2385 :       tree cond2;
    1952              : 
    1953         2385 :       status_type = TREE_TYPE (TREE_TYPE (status));
    1954         2385 :       cond2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    1955         2385 :                                status, build_int_cst (TREE_TYPE (status), 0));
    1956         2385 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
    1957              :                              fold_build1_loc (input_location, INDIRECT_REF,
    1958              :                                               status_type, status),
    1959         4764 :                              build_int_cst (status_type, unalloc_ok ? 0 : 1));
    1960         2385 :       error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    1961              :                                cond2, tmp, error);
    1962              :     }
    1963              : 
    1964        25356 :   gfc_add_expr_to_block (&null, error);
    1965              : 
    1966              :   /* When POINTER is not NULL, we free it.  */
    1967        25356 :   gfc_start_block (&non_null);
    1968        25356 :   if (add_when_allocated)
    1969         5798 :     gfc_add_expr_to_block (&non_null, add_when_allocated);
    1970        25356 :   gfc_add_finalizer_call (&non_null, expr, class_container, true);
    1971        25356 :   if (coarray_dealloc_mode == GFC_CAF_COARRAY_NOCOARRAY
    1972          499 :       || flag_coarray != GFC_FCOARRAY_LIB)
    1973              :     {
    1974        25027 :       tmp = build_call_expr_loc (input_location,
    1975              :                                  builtin_decl_explicit (BUILT_IN_FREE), 1,
    1976              :                                  fold_convert (pvoid_type_node, pointer));
    1977        25027 :       if (flag_openmp_allocators && coarray_dealloc_mode < GFC_CAF_COARRAY_ANALYZE)
    1978              :         {
    1979           65 :           tree cond, omp_tmp;
    1980           65 :           if (descr)
    1981           48 :             cond = fold_build2_loc (input_location, EQ_EXPR, boolean_type_node,
    1982              :                                     gfc_conv_descriptor_version_get (descr),
    1983              :                                     integer_one_node);
    1984              :           else
    1985           17 :             cond = gfc_omp_call_is_alloc (pointer);
    1986           65 :           omp_tmp = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
    1987           65 :           omp_tmp = build_call_expr_loc (input_location, omp_tmp, 2, pointer,
    1988              :                                          build_zero_cst (ptr_type_node));
    1989           65 :           tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
    1990              :                             omp_tmp, tmp);
    1991              :         }
    1992        25027 :       gfc_add_expr_to_block (&non_null, tmp);
    1993        25027 :       gfc_add_modify (&non_null, pointer, build_int_cst (TREE_TYPE (pointer),
    1994              :                                                          0));
    1995        25027 :       if (flag_openmp_allocators && descr)
    1996           48 :         gfc_conv_descriptor_version_set (&non_null, descr, integer_zero_node);
    1997              : 
    1998        25027 :       if (status != NULL_TREE && !integer_zerop (status))
    1999              :         {
    2000              :           /* We set STATUS to zero if it is present.  */
    2001         2365 :           tree status_type = TREE_TYPE (TREE_TYPE (status));
    2002         2365 :           tree cond2;
    2003              : 
    2004         2365 :           cond2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    2005              :                                    status,
    2006         2365 :                                    build_int_cst (TREE_TYPE (status), 0));
    2007         2365 :           tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
    2008              :                                  fold_build1_loc (input_location, INDIRECT_REF,
    2009              :                                                   status_type, status),
    2010              :                                  build_int_cst (status_type, 0));
    2011         2365 :           tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    2012              :                                  gfc_unlikely (cond2, PRED_FORTRAN_FAIL_ALLOC),
    2013              :                                  tmp, build_empty_stmt (input_location));
    2014         2365 :           gfc_add_expr_to_block (&non_null, tmp);
    2015              :         }
    2016              :     }
    2017              :   else
    2018              :     {
    2019          329 :       tree cond2, pstat = null_pointer_node;
    2020              : 
    2021          329 :       if (errmsg == NULL_TREE)
    2022              :         {
    2023          317 :           gcc_assert (errlen == NULL_TREE);
    2024          317 :           errmsg = null_pointer_node;
    2025          317 :           errlen = integer_zero_node;
    2026              :         }
    2027              :       else
    2028              :         {
    2029           12 :           gcc_assert (errlen != NULL_TREE);
    2030           12 :           if (!POINTER_TYPE_P (TREE_TYPE (errmsg)))
    2031            0 :             errmsg = gfc_build_addr_expr (NULL_TREE, errmsg);
    2032              :         }
    2033              : 
    2034          329 :       if (status != NULL_TREE && !integer_zerop (status))
    2035              :         {
    2036           20 :           gcc_assert (status_type == integer_type_node);
    2037              :           pstat = status;
    2038              :         }
    2039              : 
    2040          329 :       token = gfc_build_addr_expr  (NULL_TREE, token);
    2041          329 :       gcc_assert (caf_dereg_type > GFC_CAF_COARRAY_ANALYZE);
    2042          329 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_deregister, 5,
    2043              :                                  token,
    2044              :                                  build_int_cst (integer_type_node,
    2045          329 :                                                 caf_dereg_type),
    2046              :                                  pstat, errmsg, errlen);
    2047          329 :       gfc_add_expr_to_block (&non_null, tmp);
    2048              : 
    2049              :       /* It guarantees memory consistency within the same segment */
    2050          329 :       tmp = gfc_build_string_const (strlen ("memory")+1, "memory"),
    2051          329 :       tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
    2052              :                         gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
    2053              :                         tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
    2054          329 :       ASM_VOLATILE_P (tmp) = 1;
    2055          329 :       gfc_add_expr_to_block (&non_null, tmp);
    2056              : 
    2057          329 :       if (status != NULL_TREE && !integer_zerop (status))
    2058              :         {
    2059           20 :           tree stat = build_fold_indirect_ref_loc (input_location, status);
    2060           20 :           tree nullify = fold_build2_loc (input_location, MODIFY_EXPR,
    2061              :                                           void_type_node, pointer,
    2062           20 :                                           build_int_cst (TREE_TYPE (pointer),
    2063              :                                                          0));
    2064              : 
    2065           20 :           TREE_USED (label_finish) = 1;
    2066           20 :           tmp = build1_v (GOTO_EXPR, label_finish);
    2067           20 :           cond2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    2068           20 :                                    stat, build_zero_cst (TREE_TYPE (stat)));
    2069           20 :           tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    2070              :                                  gfc_unlikely (cond2, PRED_FORTRAN_REALLOC),
    2071              :                                  tmp, nullify);
    2072           20 :           gfc_add_expr_to_block (&non_null, tmp);
    2073              :         }
    2074              :       else
    2075          309 :         gfc_add_modify (&non_null, pointer, build_int_cst (TREE_TYPE (pointer),
    2076              :                                                            0));
    2077              :     }
    2078              : 
    2079        25356 :   return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    2080              :                           gfc_finish_block (&null),
    2081        25356 :                           gfc_finish_block (&non_null));
    2082              : }
    2083              : 
    2084              : 
    2085              : /* Generate code for deallocation of allocatable scalars (variables or
    2086              :    components). Before the object itself is freed, any allocatable
    2087              :    subcomponents are being deallocated.  */
    2088              : 
    2089              : tree
    2090         5712 : gfc_deallocate_scalar_with_status (tree pointer, tree status, tree label_finish,
    2091              :                                    bool can_fail, gfc_expr *expr,
    2092              :                                    gfc_typespec ts, tree class_container,
    2093              :                                    bool coarray, bool unalloc_ok, tree errmsg,
    2094              :                                    tree errmsg_len)
    2095              : {
    2096         5712 :   stmtblock_t null, non_null;
    2097         5712 :   tree cond, tmp, error;
    2098         5712 :   bool finalizable, comp_ref;
    2099         5712 :   gfc_coarray_deregtype caf_dereg_type = GFC_CAF_COARRAY_DEREGISTER;
    2100              : 
    2101         5712 :   if (coarray && expr && !gfc_caf_attr (expr, false, &comp_ref).coarray_comp
    2102         5755 :       && comp_ref)
    2103           43 :     caf_dereg_type = GFC_CAF_COARRAY_DEALLOCATE_ONLY;
    2104              : 
    2105         5712 :   cond = fold_build2_loc (input_location, EQ_EXPR, logical_type_node, pointer,
    2106         5712 :                           build_int_cst (TREE_TYPE (pointer), 0));
    2107              : 
    2108              :   /* When POINTER is NULL, we set STATUS to 1 if it's present, otherwise
    2109              :      we emit a runtime error.  */
    2110         5712 :   gfc_start_block (&null);
    2111         5712 :   if (!can_fail)
    2112              :     {
    2113         3692 :       tree varname;
    2114              : 
    2115         3692 :       gcc_assert (expr && expr->expr_type == EXPR_VARIABLE && expr->symtree);
    2116              : 
    2117         3692 :       varname = gfc_build_cstring_const (expr->symtree->name);
    2118         3692 :       varname = gfc_build_addr_expr (pchar_type_node, varname);
    2119              : 
    2120         3692 :       error = gfc_trans_runtime_error (true, &expr->where,
    2121              :                                        "Attempt to DEALLOCATE unallocated '%s'",
    2122              :                                        varname);
    2123              :     }
    2124              :   else
    2125         2020 :     error = build_empty_stmt (input_location);
    2126              : 
    2127         5712 :   if (status != NULL_TREE && !integer_zerop (status))
    2128              :     {
    2129          889 :       tree status_type = TREE_TYPE (TREE_TYPE (status));
    2130          889 :       tree cond2;
    2131              : 
    2132          889 :       cond2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    2133          889 :                                status, build_int_cst (TREE_TYPE (status), 0));
    2134          889 :       tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
    2135              :                              fold_build1_loc (input_location, INDIRECT_REF,
    2136              :                                               status_type, status),
    2137         1778 :                              build_int_cst (status_type, unalloc_ok ? 0 : 1));
    2138          889 :       error = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    2139              :                                cond2, tmp, error);
    2140              :     }
    2141         5712 :   gfc_add_expr_to_block (&null, error);
    2142              : 
    2143              :   /* When POINTER is not NULL, we free it.  */
    2144         5712 :   gfc_start_block (&non_null);
    2145              : 
    2146              :   /* Free allocatable components.  */
    2147         5712 :   finalizable = gfc_add_finalizer_call (&non_null, expr, class_container,
    2148              :                                         true);
    2149         5712 :   if (!finalizable && ts.type == BT_DERIVED && ts.u.derived->attr.alloc_comp)
    2150              :     {
    2151            0 :       int caf_mode = coarray
    2152          590 :           ? ((caf_dereg_type == GFC_CAF_COARRAY_DEALLOCATE_ONLY
    2153              :               ? GFC_STRUCTURE_CAF_MODE_DEALLOC_ONLY : 0)
    2154              :              | GFC_STRUCTURE_CAF_MODE_ENABLE_COARRAY
    2155            4 :              | GFC_STRUCTURE_CAF_MODE_IN_COARRAY)
    2156              :           : 0;
    2157            4 :       if (coarray && GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (pointer)))
    2158            0 :         tmp = gfc_conv_descriptor_data_get (pointer);
    2159              :       else
    2160          590 :         tmp = build_fold_indirect_ref_loc (input_location, pointer);
    2161          590 :       tmp = gfc_deallocate_alloc_comp (ts.u.derived, tmp, 0, caf_mode);
    2162          590 :       gfc_add_expr_to_block (&non_null, tmp);
    2163              :     }
    2164              : 
    2165         5712 :   if (!coarray || flag_coarray == GFC_FCOARRAY_SINGLE)
    2166              :     {
    2167         5672 :       tmp = build_call_expr_loc (input_location,
    2168              :                                  builtin_decl_explicit (BUILT_IN_FREE), 1,
    2169              :                                  fold_convert (pvoid_type_node, pointer));
    2170         5672 :       if (flag_openmp_allocators)
    2171              :         {
    2172           31 :           tree cond, omp_tmp;
    2173           31 :           cond = gfc_omp_call_is_alloc (pointer);
    2174           31 :           omp_tmp = builtin_decl_explicit (BUILT_IN_GOMP_FREE);
    2175           31 :           omp_tmp = build_call_expr_loc (input_location, omp_tmp, 2, pointer,
    2176              :                                          build_zero_cst (ptr_type_node));
    2177           31 :           tmp = build3_loc (input_location, COND_EXPR, TREE_TYPE (tmp), cond,
    2178              :                             omp_tmp, tmp);
    2179              :         }
    2180         5672 :       gfc_add_expr_to_block (&non_null, tmp);
    2181              : 
    2182         5672 :       if (status != NULL_TREE && !integer_zerop (status))
    2183              :         {
    2184              :           /* We set STATUS to zero if it is present.  */
    2185          889 :           tree status_type = TREE_TYPE (TREE_TYPE (status));
    2186          889 :           tree cond2;
    2187              : 
    2188          889 :           cond2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    2189              :                                    status,
    2190          889 :                                    build_int_cst (TREE_TYPE (status), 0));
    2191          889 :           tmp = fold_build2_loc (input_location, MODIFY_EXPR, status_type,
    2192              :                                  fold_build1_loc (input_location, INDIRECT_REF,
    2193              :                                                   status_type, status),
    2194              :                                  build_int_cst (status_type, 0));
    2195          889 :           tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    2196              :                                  cond2, tmp, build_empty_stmt (input_location));
    2197          889 :           gfc_add_expr_to_block (&non_null, tmp);
    2198              :         }
    2199              :     }
    2200              :   else
    2201              :     {
    2202           40 :       tree token;
    2203           40 :       tree pstat = null_pointer_node, perrmsg = null_pointer_node,
    2204           40 :            perrlen = size_zero_node;
    2205           40 :       gfc_se se;
    2206              : 
    2207           40 :       gfc_init_se (&se, NULL);
    2208           40 :       token = gfc_get_ultimate_alloc_ptr_comps_caf_token (&se, expr);
    2209           40 :       gcc_assert (token != NULL_TREE);
    2210              : 
    2211           40 :       if (status != NULL_TREE && !integer_zerop (status))
    2212              :         {
    2213            0 :           gcc_assert (TREE_TYPE (TREE_TYPE (status)) == integer_type_node);
    2214              :           pstat = status;
    2215              :         }
    2216              : 
    2217           40 :       if (errmsg != NULL_TREE)
    2218              :         {
    2219            0 :           perrmsg = errmsg;
    2220            0 :           perrlen = errmsg_len;
    2221              :         }
    2222              : 
    2223           40 :       tmp = build_call_expr_loc (input_location, gfor_fndecl_caf_deregister, 5,
    2224              :                                  token,
    2225              :                                  build_int_cst (integer_type_node,
    2226           40 :                                                 caf_dereg_type),
    2227              :                                  pstat, perrmsg, perrlen);
    2228           40 :       gfc_add_expr_to_block (&non_null, tmp);
    2229              : 
    2230              :       /* It guarantees memory consistency within the same segment.  */
    2231           40 :       tmp = gfc_build_string_const (strlen ("memory")+1, "memory");
    2232           40 :       tmp = build5_loc (input_location, ASM_EXPR, void_type_node,
    2233              :                         gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE,
    2234              :                         tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE);
    2235           40 :       ASM_VOLATILE_P (tmp) = 1;
    2236           40 :       gfc_add_expr_to_block (&non_null, tmp);
    2237              : 
    2238           40 :       if (status != NULL_TREE)
    2239              :         {
    2240            0 :           tree stat = build_fold_indirect_ref_loc (input_location, status);
    2241            0 :           tree cond2;
    2242              : 
    2243            0 :           TREE_USED (label_finish) = 1;
    2244            0 :           tmp = build1_v (GOTO_EXPR, label_finish);
    2245            0 :           cond2 = fold_build2_loc (input_location, NE_EXPR, logical_type_node,
    2246            0 :                                    stat, build_zero_cst (TREE_TYPE (stat)));
    2247            0 :           tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    2248              :                                  gfc_unlikely (cond2, PRED_FORTRAN_REALLOC),
    2249              :                                  tmp, build_empty_stmt (input_location));
    2250            0 :           gfc_add_expr_to_block (&non_null, tmp);
    2251              :         }
    2252              :     }
    2253              : 
    2254         5712 :   return fold_build3_loc (input_location, COND_EXPR, void_type_node, cond,
    2255              :                           gfc_finish_block (&null),
    2256         5712 :                           gfc_finish_block (&non_null));
    2257              : }
    2258              : 
    2259              : /* Reallocate MEM so it has SIZE bytes of data.  This behaves like the
    2260              :    following pseudo-code:
    2261              : 
    2262              : void *
    2263              : internal_realloc (void *mem, size_t size)
    2264              : {
    2265              :   res = realloc (mem, size);
    2266              :   if (!res && size != 0)
    2267              :     _gfortran_os_error ("Allocation would exceed memory limit");
    2268              : 
    2269              :   return res;
    2270              : }  */
    2271              : tree
    2272         1243 : gfc_call_realloc (stmtblock_t * block, tree mem, tree size)
    2273              : {
    2274         1243 :   tree res, nonzero, null_result, tmp;
    2275         1243 :   tree type = TREE_TYPE (mem);
    2276              : 
    2277              :   /* Only evaluate the size once.  */
    2278         1243 :   size = save_expr (fold_convert (size_type_node, size));
    2279              : 
    2280              :   /* Create a variable to hold the result.  */
    2281         1243 :   res = gfc_create_var (type, NULL);
    2282              : 
    2283              :   /* Call realloc and check the result.  */
    2284         1243 :   tmp = build_call_expr_loc (input_location,
    2285              :                          builtin_decl_explicit (BUILT_IN_REALLOC), 2,
    2286              :                          fold_convert (pvoid_type_node, mem), size);
    2287         1243 :   gfc_add_modify (block, res, fold_convert (type, tmp));
    2288         1243 :   null_result = fold_build2_loc (input_location, EQ_EXPR, logical_type_node,
    2289              :                                  res, build_int_cst (pvoid_type_node, 0));
    2290         1243 :   nonzero = fold_build2_loc (input_location, NE_EXPR, logical_type_node, size,
    2291              :                              build_int_cst (size_type_node, 0));
    2292         1243 :   null_result = fold_build2_loc (input_location, TRUTH_AND_EXPR, logical_type_node,
    2293              :                                  null_result, nonzero);
    2294         1243 :   tmp = fold_build3_loc (input_location, COND_EXPR, void_type_node,
    2295              :                          null_result,
    2296              :                          trans_os_error_at (NULL,
    2297              :                                             "Error reallocating to %lu bytes",
    2298              :                                             fold_convert
    2299              :                                             (long_unsigned_type_node, size)),
    2300              :                          build_empty_stmt (input_location));
    2301         1243 :   gfc_add_expr_to_block (block, tmp);
    2302              : 
    2303         1243 :   return res;
    2304              : }
    2305              : 
    2306              : 
    2307              : /* Add an expression to another one, either at the front or the back.  */
    2308              : 
    2309              : static void
    2310     20664079 : add_expr_to_chain (tree* chain, tree expr, bool front)
    2311              : {
    2312     20664079 :   if (expr == NULL_TREE || IS_EMPTY_STMT (expr))
    2313              :     return;
    2314              : 
    2315     10559839 :   if (*chain)
    2316              :     {
    2317      5663054 :       if (TREE_CODE (*chain) != STATEMENT_LIST)
    2318              :         {
    2319      1614258 :           tree tmp;
    2320              : 
    2321      1614258 :           tmp = *chain;
    2322      1614258 :           *chain = NULL_TREE;
    2323      1614258 :           append_to_statement_list (tmp, chain);
    2324              :         }
    2325              : 
    2326      5663054 :       if (front)
    2327              :         {
    2328        30514 :           tree_stmt_iterator i;
    2329              : 
    2330        30514 :           i = tsi_start (*chain);
    2331        30514 :           tsi_link_before (&i, expr, TSI_CONTINUE_LINKING);
    2332              :         }
    2333              :       else
    2334      5632540 :         append_to_statement_list (expr, chain);
    2335              :     }
    2336              :   else
    2337      4896785 :     *chain = expr;
    2338              : }
    2339              : 
    2340              : 
    2341              : /* Add a statement at the end of a block.  */
    2342              : 
    2343              : void
    2344     19760575 : gfc_add_expr_to_block (stmtblock_t * block, tree expr)
    2345              : {
    2346     19760575 :   gcc_assert (block);
    2347     19760575 :   add_expr_to_chain (&block->head, expr, false);
    2348     19760575 : }
    2349              : 
    2350              : 
    2351              : /* Add a statement at the beginning of a block.  */
    2352              : 
    2353              : void
    2354        12122 : gfc_prepend_expr_to_block (stmtblock_t * block, tree expr)
    2355              : {
    2356        12122 :   gcc_assert (block);
    2357        12122 :   add_expr_to_chain (&block->head, expr, true);
    2358        12122 : }
    2359              : 
    2360              : 
    2361              : /* Add a block the end of a block.  */
    2362              : 
    2363              : void
    2364      9985531 : gfc_add_block_to_block (stmtblock_t * block, stmtblock_t * append)
    2365              : {
    2366      9985531 :   gcc_assert (append);
    2367      9985531 :   gcc_assert (!append->has_scope);
    2368              : 
    2369      9985531 :   gfc_add_expr_to_block (block, append->head);
    2370      9985531 :   append->head = NULL_TREE;
    2371      9985531 : }
    2372              : 
    2373              : 
    2374              : /* Translate an executable statement. The tree cond is used by gfc_trans_do.
    2375              :    This static function is wrapped by gfc_trans_code_cond and
    2376              :    gfc_trans_code.  */
    2377              : 
    2378              : static tree
    2379       458140 : trans_code (gfc_code * code, tree cond)
    2380              : {
    2381       458140 :   stmtblock_t block;
    2382       458140 :   tree res;
    2383              : 
    2384       458140 :   if (!code)
    2385         2140 :     return build_empty_stmt (input_location);
    2386              : 
    2387       456000 :   gfc_start_block (&block);
    2388              : 
    2389              :   /* Translate statements one by one into GENERIC trees until we reach
    2390              :      the end of this gfc_code branch.  */
    2391      2124706 :   for (; code; code = code->next)
    2392              :     {
    2393      1212706 :       if (code->here != 0)
    2394              :         {
    2395         3577 :           res = gfc_trans_label_here (code);
    2396         3577 :           gfc_add_expr_to_block (&block, res);
    2397              :         }
    2398              : 
    2399      1212706 :       input_location = gfc_get_location (&code->loc);
    2400              : 
    2401      1212706 :       switch (code->op)
    2402              :         {
    2403              :         case EXEC_NOP:
    2404              :         case EXEC_END_BLOCK:
    2405              :         case EXEC_END_NESTED_BLOCK:
    2406              :         case EXEC_END_PROCEDURE:
    2407              :           res = NULL_TREE;
    2408              :           break;
    2409              : 
    2410       317209 :         case EXEC_ASSIGN:
    2411       317209 :           res = gfc_trans_assign (code);
    2412       317209 :           break;
    2413              : 
    2414          116 :         case EXEC_LABEL_ASSIGN:
    2415          116 :           res = gfc_trans_label_assign (code);
    2416          116 :           break;
    2417              : 
    2418        10526 :         case EXEC_POINTER_ASSIGN:
    2419        10526 :           res = gfc_trans_pointer_assign (code);
    2420        10526 :           break;
    2421              : 
    2422        12709 :         case EXEC_INIT_ASSIGN:
    2423        12709 :           if (code->expr1->ts.type == BT_CLASS)
    2424          448 :             res = gfc_trans_class_init_assign (code);
    2425              :           else
    2426        12261 :             res = gfc_trans_init_assign (code);
    2427              :           break;
    2428              : 
    2429              :         case EXEC_CONTINUE:
    2430              :           res = NULL_TREE;
    2431              :           break;
    2432              : 
    2433           37 :         case EXEC_CRITICAL:
    2434           37 :           res = gfc_trans_critical (code);
    2435           37 :           break;
    2436              : 
    2437          125 :         case EXEC_CYCLE:
    2438          125 :           res = gfc_trans_cycle (code);
    2439          125 :           break;
    2440              : 
    2441          734 :         case EXEC_EXIT:
    2442          734 :           res = gfc_trans_exit (code);
    2443          734 :           break;
    2444              : 
    2445         1244 :         case EXEC_GOTO:
    2446         1244 :           res = gfc_trans_goto (code);
    2447         1244 :           break;
    2448              : 
    2449         1424 :         case EXEC_ENTRY:
    2450         1424 :           res = gfc_trans_entry (code);
    2451         1424 :           break;
    2452              : 
    2453           30 :         case EXEC_PAUSE:
    2454           30 :           res = gfc_trans_pause (code);
    2455           30 :           break;
    2456              : 
    2457       224534 :         case EXEC_STOP:
    2458       224534 :         case EXEC_ERROR_STOP:
    2459       224534 :           res = gfc_trans_stop (code, code->op == EXEC_ERROR_STOP);
    2460       224534 :           break;
    2461              : 
    2462        86448 :         case EXEC_CALL:
    2463              :           /* For MVBITS we've got the special exception that we need a
    2464              :              dependency check, too.  */
    2465        86448 :           {
    2466        86448 :             bool is_mvbits = false;
    2467              : 
    2468        86448 :             if (code->resolved_isym)
    2469              :               {
    2470         7481 :                 res = gfc_conv_intrinsic_subroutine (code);
    2471         7481 :                 if (res != NULL_TREE)
    2472              :                   break;
    2473              :               }
    2474              : 
    2475        80820 :             if (code->resolved_isym
    2476         1853 :                 && code->resolved_isym->id == GFC_ISYM_MVBITS)
    2477        80820 :               is_mvbits = true;
    2478              : 
    2479        80820 :             res = gfc_trans_call (code, is_mvbits, NULL_TREE,
    2480              :                                   NULL_TREE, false);
    2481              :           }
    2482        80820 :           break;
    2483              : 
    2484          115 :         case EXEC_CALL_PPC:
    2485          115 :           res = gfc_trans_call (code, false, NULL_TREE,
    2486              :                                 NULL_TREE, false);
    2487          115 :           break;
    2488              : 
    2489          881 :         case EXEC_ASSIGN_CALL:
    2490              :           /* Record that an assignment call is being processed, to
    2491              :              ensure finalization occurs in gfc_finalize_tree_expr */
    2492          881 :           is_assign_call = 1;
    2493          881 :           res = gfc_trans_call (code, true, NULL_TREE,
    2494              :                                 NULL_TREE, false);
    2495          881 :           is_assign_call = 0;
    2496          881 :           break;
    2497              : 
    2498         3223 :         case EXEC_RETURN:
    2499         3223 :           res = gfc_trans_return (code);
    2500         3223 :           break;
    2501              : 
    2502       247949 :         case EXEC_IF:
    2503       247949 :           res = gfc_trans_if (code);
    2504       247949 :           break;
    2505              : 
    2506           64 :         case EXEC_ARITHMETIC_IF:
    2507           64 :           res = gfc_trans_arithmetic_if (code);
    2508           64 :           break;
    2509              : 
    2510        15151 :         case EXEC_BLOCK:
    2511        15151 :           res = gfc_trans_block_construct (code);
    2512        15151 :           break;
    2513              : 
    2514        30315 :         case EXEC_DO:
    2515        30315 :           res = gfc_trans_do (code, cond);
    2516        30315 :           break;
    2517              : 
    2518          306 :         case EXEC_DO_CONCURRENT:
    2519          306 :           res = gfc_trans_do_concurrent (code);
    2520          306 :           break;
    2521              : 
    2522          538 :         case EXEC_DO_WHILE:
    2523          538 :           res = gfc_trans_do_while (code);
    2524          538 :           break;
    2525              : 
    2526         1163 :         case EXEC_SELECT:
    2527         1163 :           res = gfc_trans_select (code);
    2528         1163 :           break;
    2529              : 
    2530         3250 :         case EXEC_SELECT_TYPE:
    2531         3250 :           res = gfc_trans_select_type (code);
    2532         3250 :           break;
    2533              : 
    2534         1043 :         case EXEC_SELECT_RANK:
    2535         1043 :           res = gfc_trans_select_rank (code);
    2536         1043 :           break;
    2537              : 
    2538           78 :         case EXEC_FLUSH:
    2539           78 :           res = gfc_trans_flush (code);
    2540           78 :           break;
    2541              : 
    2542         1420 :         case EXEC_SYNC_ALL:
    2543         1420 :         case EXEC_SYNC_IMAGES:
    2544         1420 :         case EXEC_SYNC_MEMORY:
    2545         1420 :           res = gfc_trans_sync (code, code->op);
    2546         1420 :           break;
    2547              : 
    2548          126 :         case EXEC_LOCK:
    2549          126 :         case EXEC_UNLOCK:
    2550          126 :           res = gfc_trans_lock_unlock (code, code->op);
    2551          126 :           break;
    2552              : 
    2553           58 :         case EXEC_EVENT_POST:
    2554           58 :         case EXEC_EVENT_WAIT:
    2555           58 :           res = gfc_trans_event_post_wait (code, code->op);
    2556           58 :           break;
    2557              : 
    2558           21 :         case EXEC_FAIL_IMAGE:
    2559           21 :           res = gfc_trans_fail_image (code);
    2560           21 :           break;
    2561              : 
    2562         1866 :         case EXEC_FORALL:
    2563         1866 :           res = gfc_trans_forall (code);
    2564         1866 :           break;
    2565              : 
    2566          168 :         case EXEC_FORM_TEAM:
    2567          168 :           res = gfc_trans_form_team (code);
    2568          168 :           break;
    2569              : 
    2570          112 :         case EXEC_CHANGE_TEAM:
    2571          112 :           res = gfc_trans_change_team (code);
    2572          112 :           break;
    2573              : 
    2574           83 :         case EXEC_END_TEAM:
    2575           83 :           res = gfc_trans_end_team (code);
    2576           83 :           break;
    2577              : 
    2578           36 :         case EXEC_SYNC_TEAM:
    2579           36 :           res = gfc_trans_sync_team (code);
    2580           36 :           break;
    2581              : 
    2582          330 :         case EXEC_WHERE:
    2583          330 :           res = gfc_trans_where (code);
    2584          330 :           break;
    2585              : 
    2586        15213 :         case EXEC_ALLOCATE:
    2587        15213 :           res = gfc_trans_allocate (code, NULL);
    2588        15213 :           break;
    2589              : 
    2590         9871 :         case EXEC_DEALLOCATE:
    2591         9871 :           res = gfc_trans_deallocate (code);
    2592         9871 :           break;
    2593              : 
    2594         3678 :         case EXEC_OPEN:
    2595         3678 :           res = gfc_trans_open (code);
    2596         3678 :           break;
    2597              : 
    2598         3158 :         case EXEC_CLOSE:
    2599         3158 :           res = gfc_trans_close (code);
    2600         3158 :           break;
    2601              : 
    2602         6558 :         case EXEC_READ:
    2603         6558 :           res = gfc_trans_read (code);
    2604         6558 :           break;
    2605              : 
    2606        25767 :         case EXEC_WRITE:
    2607        25767 :           res = gfc_trans_write (code);
    2608        25767 :           break;
    2609              : 
    2610           84 :         case EXEC_IOLENGTH:
    2611           84 :           res = gfc_trans_iolength (code);
    2612           84 :           break;
    2613              : 
    2614          389 :         case EXEC_BACKSPACE:
    2615          389 :           res = gfc_trans_backspace (code);
    2616          389 :           break;
    2617              : 
    2618           56 :         case EXEC_ENDFILE:
    2619           56 :           res = gfc_trans_endfile (code);
    2620           56 :           break;
    2621              : 
    2622          816 :         case EXEC_INQUIRE:
    2623          816 :           res = gfc_trans_inquire (code);
    2624          816 :           break;
    2625              : 
    2626           74 :         case EXEC_WAIT:
    2627           74 :           res = gfc_trans_wait (code);
    2628           74 :           break;
    2629              : 
    2630         2342 :         case EXEC_REWIND:
    2631         2342 :           res = gfc_trans_rewind (code);
    2632         2342 :           break;
    2633              : 
    2634        46527 :         case EXEC_TRANSFER:
    2635        46527 :           res = gfc_trans_transfer (code);
    2636        46527 :           break;
    2637              : 
    2638        32409 :         case EXEC_DT_END:
    2639        32409 :           res = gfc_trans_dt_end (code);
    2640        32409 :           break;
    2641              : 
    2642        19792 :         case EXEC_OMP_ALLOCATE:
    2643        19792 :         case EXEC_OMP_ALLOCATORS:
    2644        19792 :         case EXEC_OMP_ASSUME:
    2645        19792 :         case EXEC_OMP_ATOMIC:
    2646        19792 :         case EXEC_OMP_BARRIER:
    2647        19792 :         case EXEC_OMP_CANCEL:
    2648        19792 :         case EXEC_OMP_CANCELLATION_POINT:
    2649        19792 :         case EXEC_OMP_CRITICAL:
    2650        19792 :         case EXEC_OMP_DEPOBJ:
    2651        19792 :         case EXEC_OMP_DISPATCH:
    2652        19792 :         case EXEC_OMP_DISTRIBUTE:
    2653        19792 :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
    2654        19792 :         case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
    2655        19792 :         case EXEC_OMP_DISTRIBUTE_SIMD:
    2656        19792 :         case EXEC_OMP_DO:
    2657        19792 :         case EXEC_OMP_DO_SIMD:
    2658        19792 :         case EXEC_OMP_ERROR:
    2659        19792 :         case EXEC_OMP_FLUSH:
    2660        19792 :         case EXEC_OMP_INTEROP:
    2661        19792 :         case EXEC_OMP_LOOP:
    2662        19792 :         case EXEC_OMP_MASKED:
    2663        19792 :         case EXEC_OMP_MASKED_TASKLOOP:
    2664        19792 :         case EXEC_OMP_MASKED_TASKLOOP_SIMD:
    2665        19792 :         case EXEC_OMP_MASTER:
    2666        19792 :         case EXEC_OMP_MASTER_TASKLOOP:
    2667        19792 :         case EXEC_OMP_MASTER_TASKLOOP_SIMD:
    2668        19792 :         case EXEC_OMP_METADIRECTIVE:
    2669        19792 :         case EXEC_OMP_ORDERED:
    2670        19792 :         case EXEC_OMP_PARALLEL:
    2671        19792 :         case EXEC_OMP_PARALLEL_DO:
    2672        19792 :         case EXEC_OMP_PARALLEL_DO_SIMD:
    2673        19792 :         case EXEC_OMP_PARALLEL_LOOP:
    2674        19792 :         case EXEC_OMP_PARALLEL_MASKED:
    2675        19792 :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
    2676        19792 :         case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
    2677        19792 :         case EXEC_OMP_PARALLEL_MASTER:
    2678        19792 :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
    2679        19792 :         case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
    2680        19792 :         case EXEC_OMP_PARALLEL_SECTIONS:
    2681        19792 :         case EXEC_OMP_PARALLEL_WORKSHARE:
    2682        19792 :         case EXEC_OMP_SCOPE:
    2683        19792 :         case EXEC_OMP_SECTIONS:
    2684        19792 :         case EXEC_OMP_SIMD:
    2685        19792 :         case EXEC_OMP_SINGLE:
    2686        19792 :         case EXEC_OMP_TARGET:
    2687        19792 :         case EXEC_OMP_TARGET_DATA:
    2688        19792 :         case EXEC_OMP_TARGET_ENTER_DATA:
    2689        19792 :         case EXEC_OMP_TARGET_EXIT_DATA:
    2690        19792 :         case EXEC_OMP_TARGET_PARALLEL:
    2691        19792 :         case EXEC_OMP_TARGET_PARALLEL_DO:
    2692        19792 :         case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
    2693        19792 :         case EXEC_OMP_TARGET_PARALLEL_LOOP:
    2694        19792 :         case EXEC_OMP_TARGET_SIMD:
    2695        19792 :         case EXEC_OMP_TARGET_TEAMS:
    2696        19792 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
    2697        19792 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
    2698        19792 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
    2699        19792 :         case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
    2700        19792 :         case EXEC_OMP_TARGET_TEAMS_LOOP:
    2701        19792 :         case EXEC_OMP_TARGET_UPDATE:
    2702        19792 :         case EXEC_OMP_TASK:
    2703        19792 :         case EXEC_OMP_TASKGROUP:
    2704        19792 :         case EXEC_OMP_TASKLOOP:
    2705        19792 :         case EXEC_OMP_TASKLOOP_SIMD:
    2706        19792 :         case EXEC_OMP_TASKWAIT:
    2707        19792 :         case EXEC_OMP_TASKYIELD:
    2708        19792 :         case EXEC_OMP_TEAMS:
    2709        19792 :         case EXEC_OMP_TEAMS_DISTRIBUTE:
    2710        19792 :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
    2711        19792 :         case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
    2712        19792 :         case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
    2713        19792 :         case EXEC_OMP_TEAMS_LOOP:
    2714        19792 :         case EXEC_OMP_TILE:
    2715        19792 :         case EXEC_OMP_UNROLL:
    2716        19792 :         case EXEC_OMP_WORKSHARE:
    2717        19792 :           res = gfc_trans_omp_directive (code);
    2718        19792 :           break;
    2719              : 
    2720        12412 :         case EXEC_OACC_CACHE:
    2721        12412 :         case EXEC_OACC_WAIT:
    2722        12412 :         case EXEC_OACC_UPDATE:
    2723        12412 :         case EXEC_OACC_LOOP:
    2724        12412 :         case EXEC_OACC_HOST_DATA:
    2725        12412 :         case EXEC_OACC_DATA:
    2726        12412 :         case EXEC_OACC_KERNELS:
    2727        12412 :         case EXEC_OACC_KERNELS_LOOP:
    2728        12412 :         case EXEC_OACC_PARALLEL:
    2729        12412 :         case EXEC_OACC_PARALLEL_LOOP:
    2730        12412 :         case EXEC_OACC_SERIAL:
    2731        12412 :         case EXEC_OACC_SERIAL_LOOP:
    2732        12412 :         case EXEC_OACC_ENTER_DATA:
    2733        12412 :         case EXEC_OACC_EXIT_DATA:
    2734        12412 :         case EXEC_OACC_ATOMIC:
    2735        12412 :         case EXEC_OACC_DECLARE:
    2736        12412 :         case EXEC_OACC_INIT:
    2737        12412 :         case EXEC_OACC_SHUTDOWN:
    2738        12412 :         case EXEC_OACC_SET:
    2739        12412 :           res = gfc_trans_oacc_directive (code);
    2740        12412 :           break;
    2741              : 
    2742            0 :         default:
    2743            0 :           gfc_internal_error ("gfc_trans_code(): Bad statement code");
    2744              :         }
    2745              : 
    2746      1212706 :       input_location = gfc_get_location (&code->loc);
    2747              : 
    2748      1212706 :       if (res != NULL_TREE && ! IS_EMPTY_STMT (res))
    2749              :         {
    2750              :           /* Don't try to set location for trees that don't have one,
    2751              :              e.g. STATEMENT_LIST or error_mark_node.  */
    2752      1141864 :           if (EXPR_P (res))
    2753       846081 :             SET_EXPR_LOCATION (res, input_location);
    2754              : 
    2755              :           /* Add the new statement to the block.  */
    2756      1141864 :           gfc_add_expr_to_block (&block, res);
    2757              :         }
    2758              :     }
    2759              : 
    2760              :   /* Return the finished block.  */
    2761       456000 :   return gfc_finish_block (&block);
    2762              : }
    2763              : 
    2764              : 
    2765              : /* Translate an executable statement with condition, cond.  The condition is
    2766              :    used by gfc_trans_do to test for IO result conditions inside implied
    2767              :    DO loops of READ and WRITE statements.  See build_dt in trans-io.cc.  */
    2768              : 
    2769              : tree
    2770        62724 : gfc_trans_code_cond (gfc_code * code, tree cond)
    2771              : {
    2772        62724 :   return trans_code (code, cond);
    2773              : }
    2774              : 
    2775              : /* Translate an executable statement without condition.  */
    2776              : 
    2777              : tree
    2778       395416 : gfc_trans_code (gfc_code * code)
    2779              : {
    2780       395416 :   return trans_code (code, NULL_TREE);
    2781              : }
    2782              : 
    2783              : 
    2784              : /* This function is called after a complete program unit has been parsed
    2785              :    and resolved.  */
    2786              : 
    2787              : void
    2788        37750 : gfc_generate_code (gfc_namespace * ns)
    2789              : {
    2790        37750 :   ompws_flags = 0;
    2791        37750 :   if (ns->is_block_data)
    2792              :     {
    2793           72 :       gfc_generate_block_data (ns);
    2794           72 :       return;
    2795              :     }
    2796              : 
    2797        37678 :   gfc_generate_function_code (ns);
    2798              : }
    2799              : 
    2800              : 
    2801              : /* This function is called after a complete module has been parsed
    2802              :    and resolved.  */
    2803              : 
    2804              : void
    2805         9840 : gfc_generate_module_code (gfc_namespace * ns)
    2806              : {
    2807         9840 :   gfc_namespace *n;
    2808         9840 :   struct module_htab_entry *entry;
    2809              : 
    2810         9840 :   gcc_assert (ns->proc_name->backend_decl == NULL);
    2811        19680 :   ns->proc_name->backend_decl
    2812         9840 :     = build_decl (gfc_get_location (&ns->proc_name->declared_at),
    2813              :                   NAMESPACE_DECL, get_identifier (ns->proc_name->name),
    2814              :                   void_type_node);
    2815         9840 :   entry = gfc_find_module (ns->proc_name->name);
    2816         9840 :   if (entry->namespace_decl)
    2817              :     /* Buggy sourcecode, using a module before defining it?  */
    2818            0 :     entry->decls->empty ();
    2819         9840 :   entry->namespace_decl = ns->proc_name->backend_decl;
    2820              : 
    2821         9840 :   gfc_generate_module_vars (ns);
    2822              : 
    2823              :   /* We need to generate all module function prototypes first, to allow
    2824              :      sibling calls.  */
    2825        38500 :   for (n = ns->contained; n; n = n->sibling)
    2826              :     {
    2827        28660 :       gfc_entry_list *el;
    2828              : 
    2829        28660 :       if (!n->proc_name)
    2830            0 :         continue;
    2831              : 
    2832        28660 :       gfc_create_function_decl (n, false);
    2833        28660 :       DECL_CONTEXT (n->proc_name->backend_decl) = ns->proc_name->backend_decl;
    2834        28660 :       gfc_module_add_decl (entry, n->proc_name->backend_decl);
    2835        28660 :       for (el = ns->entries; el; el = el->next)
    2836              :         {
    2837            0 :           DECL_CONTEXT (el->sym->backend_decl) = ns->proc_name->backend_decl;
    2838            0 :           gfc_module_add_decl (entry, el->sym->backend_decl);
    2839              :         }
    2840              :     }
    2841              : 
    2842        38500 :   for (n = ns->contained; n; n = n->sibling)
    2843              :     {
    2844        28660 :       if (!n->proc_name)
    2845            0 :         continue;
    2846              : 
    2847        28660 :       gfc_generate_function_code (n);
    2848              :     }
    2849         9840 : }
    2850              : 
    2851              : 
    2852              : /* Initialize an init/cleanup block with existing code.  */
    2853              : 
    2854              : void
    2855       106174 : gfc_start_wrapped_block (gfc_wrapped_block* block, tree code)
    2856              : {
    2857       106174 :   gcc_assert (block);
    2858              : 
    2859       106174 :   block->init = NULL_TREE;
    2860       106174 :   block->code = code;
    2861       106174 :   block->cleanup = NULL_TREE;
    2862       106174 : }
    2863              : 
    2864              : 
    2865              : /* Add a new pair of initializers/clean-up code.  */
    2866              : 
    2867              : void
    2868       392604 : gfc_add_init_cleanup (gfc_wrapped_block* block, tree init, tree cleanup,
    2869              :                       bool back)
    2870              : {
    2871       392604 :   gcc_assert (block);
    2872              : 
    2873              :   /* The new pair of init/cleanup should be "wrapped around" the existing
    2874              :      block of code, thus the initialization is added to the front and the
    2875              :      cleanup to the back.  */
    2876       392604 :   add_expr_to_chain (&block->init, init, !back);
    2877       392604 :   add_expr_to_chain (&block->cleanup, cleanup, false);
    2878       392604 : }
    2879              : 
    2880              : 
    2881              : /* Finish up a wrapped block by building a corresponding try-finally expr.  */
    2882              : 
    2883              : tree
    2884       106174 : gfc_finish_wrapped_block (gfc_wrapped_block* block)
    2885              : {
    2886       106174 :   tree result;
    2887              : 
    2888       106174 :   gcc_assert (block);
    2889              : 
    2890              :   /* Build the final expression.  For this, just add init and body together,
    2891              :      and put clean-up with that into a TRY_FINALLY_EXPR.  */
    2892       106174 :   result = block->init;
    2893       106174 :   add_expr_to_chain (&result, block->code, false);
    2894       106174 :   if (block->cleanup)
    2895        11332 :     result = build2_loc (input_location, TRY_FINALLY_EXPR, void_type_node,
    2896              :                          result, block->cleanup);
    2897              : 
    2898              :   /* Clear the block.  */
    2899       106174 :   block->init = NULL_TREE;
    2900       106174 :   block->code = NULL_TREE;
    2901       106174 :   block->cleanup = NULL_TREE;
    2902              : 
    2903       106174 :   return result;
    2904              : }
    2905              : 
    2906              : 
    2907              : /* Helper function for marking a boolean expression tree as unlikely.  */
    2908              : 
    2909              : tree
    2910       132292 : gfc_unlikely (tree cond, enum br_predictor predictor)
    2911              : {
    2912       132292 :   tree tmp;
    2913              : 
    2914       132292 :   if (optimize)
    2915              :     {
    2916       113530 :       cond = fold_convert (long_integer_type_node, cond);
    2917       113530 :       tmp = build_zero_cst (long_integer_type_node);
    2918       113530 :       cond = build_call_expr_loc (input_location,
    2919              :                                   builtin_decl_explicit (BUILT_IN_EXPECT),
    2920              :                                   3, cond, tmp,
    2921              :                                   build_int_cst (integer_type_node,
    2922       113530 :                                                  predictor));
    2923              :     }
    2924       132292 :   return cond;
    2925              : }
    2926              : 
    2927              : 
    2928              : /* Helper function for marking a boolean expression tree as likely.  */
    2929              : 
    2930              : tree
    2931         3133 : gfc_likely (tree cond, enum br_predictor predictor)
    2932              : {
    2933         3133 :   tree tmp;
    2934              : 
    2935         3133 :   if (optimize)
    2936              :     {
    2937         2775 :       cond = fold_convert (long_integer_type_node, cond);
    2938         2775 :       tmp = build_one_cst (long_integer_type_node);
    2939         2775 :       cond = build_call_expr_loc (input_location,
    2940              :                                   builtin_decl_explicit (BUILT_IN_EXPECT),
    2941              :                                   3, cond, tmp,
    2942              :                                   build_int_cst (integer_type_node,
    2943         2775 :                                                  predictor));
    2944              :     }
    2945         3133 :   return cond;
    2946              : }
    2947              : 
    2948              : 
    2949              : /* Get the string length for a deferred character length component.  */
    2950              : 
    2951              : bool
    2952       230820 : gfc_deferred_strlen (gfc_component *c, tree *decl)
    2953              : {
    2954       230820 :   char name[GFC_MAX_SYMBOL_LEN+9];
    2955       230820 :   gfc_component *strlen;
    2956       230820 :   if (!(c->ts.type == BT_CHARACTER
    2957        13107 :         && (c->ts.deferred || c->attr.pdt_string)))
    2958              :     return false;
    2959         5286 :   sprintf (name, "_%s_length", c->name);
    2960        16089 :   for (strlen = c; strlen; strlen = strlen->next)
    2961        16078 :     if (strcmp (strlen->name, name) == 0)
    2962              :       break;
    2963         5286 :   *decl = strlen ? strlen->backend_decl : NULL_TREE;
    2964         5286 :   return strlen != NULL;
    2965              : }
        

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