LCOV - code coverage report
Current view: top level - gcc/fortran - expr.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 91.9 % 3313 3045
Test Date: 2025-09-20 13:40:47 Functions: 97.5 % 121 118
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Routines for manipulation of expression nodes.
       2                 :             :    Copyright (C) 2000-2025 Free Software Foundation, Inc.
       3                 :             :    Contributed by Andy Vaught
       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 "gfortran.h"
      26                 :             : #include "arith.h"
      27                 :             : #include "match.h"
      28                 :             : #include "target-memory.h" /* for gfc_convert_boz */
      29                 :             : #include "constructor.h"
      30                 :             : #include "tree.h"
      31                 :             : 
      32                 :             : 
      33                 :             : /* The following set of functions provide access to gfc_expr* of
      34                 :             :    various types - actual all but EXPR_FUNCTION and EXPR_VARIABLE.
      35                 :             : 
      36                 :             :    There are two functions available elsewhere that provide
      37                 :             :    slightly different flavours of variables.  Namely:
      38                 :             :      expr.cc (gfc_get_variable_expr)
      39                 :             :      symbol.cc (gfc_lval_expr_from_sym)
      40                 :             :    TODO: Merge these functions, if possible.  */
      41                 :             : 
      42                 :             : /* Get a new expression node.  */
      43                 :             : 
      44                 :             : gfc_expr *
      45                 :    88093588 : gfc_get_expr (void)
      46                 :             : {
      47                 :    88093588 :   gfc_expr *e;
      48                 :             : 
      49                 :    88093588 :   e = XCNEW (gfc_expr);
      50                 :    88093588 :   gfc_clear_ts (&e->ts);
      51                 :    88093588 :   e->shape = NULL;
      52                 :    88093588 :   e->ref = NULL;
      53                 :    88093588 :   e->symtree = NULL;
      54                 :    88093588 :   return e;
      55                 :             : }
      56                 :             : 
      57                 :             : 
      58                 :             : /* Get a new expression node that is an array constructor
      59                 :             :    of given type and kind.  */
      60                 :             : 
      61                 :             : gfc_expr *
      62                 :      163722 : gfc_get_array_expr (bt type, int kind, locus *where)
      63                 :             : {
      64                 :      163722 :   gfc_expr *e;
      65                 :             : 
      66                 :      163722 :   e = gfc_get_expr ();
      67                 :      163722 :   e->expr_type = EXPR_ARRAY;
      68                 :      163722 :   e->value.constructor = NULL;
      69                 :      163722 :   e->rank = 1;
      70                 :      163722 :   e->shape = NULL;
      71                 :             : 
      72                 :      163722 :   e->ts.type = type;
      73                 :      163722 :   e->ts.kind = kind;
      74                 :      163722 :   if (where)
      75                 :      162530 :     e->where = *where;
      76                 :             : 
      77                 :      163722 :   return e;
      78                 :             : }
      79                 :             : 
      80                 :             : 
      81                 :             : /* Get a new expression node that is the NULL expression.  */
      82                 :             : 
      83                 :             : gfc_expr *
      84                 :       47354 : gfc_get_null_expr (locus *where)
      85                 :             : {
      86                 :       47354 :   gfc_expr *e;
      87                 :             : 
      88                 :       47354 :   e = gfc_get_expr ();
      89                 :       47354 :   e->expr_type = EXPR_NULL;
      90                 :       47354 :   e->ts.type = BT_UNKNOWN;
      91                 :             : 
      92                 :       47354 :   if (where)
      93                 :       13020 :     e->where = *where;
      94                 :             : 
      95                 :       47354 :   return e;
      96                 :             : }
      97                 :             : 
      98                 :             : 
      99                 :             : /* Get a new expression node that is an operator expression node.  */
     100                 :             : 
     101                 :             : gfc_expr *
     102                 :     1557456 : gfc_get_operator_expr (locus *where, gfc_intrinsic_op op,
     103                 :             :                       gfc_expr *op1, gfc_expr *op2)
     104                 :             : {
     105                 :     1557456 :   gfc_expr *e;
     106                 :             : 
     107                 :     1557456 :   e = gfc_get_expr ();
     108                 :     1557456 :   e->expr_type = EXPR_OP;
     109                 :     1557456 :   e->value.op.op = op;
     110                 :     1557456 :   e->value.op.op1 = op1;
     111                 :     1557456 :   e->value.op.op2 = op2;
     112                 :             : 
     113                 :     1557456 :   if (where)
     114                 :     1557456 :     e->where = *where;
     115                 :             : 
     116                 :     1557456 :   return e;
     117                 :             : }
     118                 :             : 
     119                 :             : /* Get a new expression node that is an conditional expression node.  */
     120                 :             : 
     121                 :             : gfc_expr *
     122                 :          90 : gfc_get_conditional_expr (locus *where, gfc_expr *condition,
     123                 :             :                           gfc_expr *true_expr, gfc_expr *false_expr)
     124                 :             : {
     125                 :          90 :   gfc_expr *e;
     126                 :             : 
     127                 :          90 :   e = gfc_get_expr ();
     128                 :          90 :   e->expr_type = EXPR_CONDITIONAL;
     129                 :          90 :   e->value.conditional.condition = condition;
     130                 :          90 :   e->value.conditional.true_expr = true_expr;
     131                 :          90 :   e->value.conditional.false_expr = false_expr;
     132                 :             : 
     133                 :          90 :   if (where)
     134                 :          90 :     e->where = *where;
     135                 :             : 
     136                 :          90 :   return e;
     137                 :             : }
     138                 :             : 
     139                 :             : /* Get a new expression node that is an structure constructor
     140                 :             :    of given type and kind.  */
     141                 :             : 
     142                 :             : gfc_expr *
     143                 :       31096 : gfc_get_structure_constructor_expr (bt type, int kind, locus *where)
     144                 :             : {
     145                 :       31096 :   gfc_expr *e;
     146                 :             : 
     147                 :       31096 :   e = gfc_get_expr ();
     148                 :       31096 :   e->expr_type = EXPR_STRUCTURE;
     149                 :       31096 :   e->value.constructor = NULL;
     150                 :             : 
     151                 :       31096 :   e->ts.type = type;
     152                 :       31096 :   e->ts.kind = kind;
     153                 :       31096 :   if (where)
     154                 :       31096 :     e->where = *where;
     155                 :             : 
     156                 :       31096 :   return e;
     157                 :             : }
     158                 :             : 
     159                 :             : 
     160                 :             : /* Get a new expression node that is an constant of given type and kind.  */
     161                 :             : 
     162                 :             : gfc_expr *
     163                 :    31317692 : gfc_get_constant_expr (bt type, int kind, locus *where)
     164                 :             : {
     165                 :    31317692 :   gfc_expr *e;
     166                 :             : 
     167                 :    31317692 :   if (!where)
     168                 :           0 :     gfc_internal_error ("gfc_get_constant_expr(): locus %<where%> cannot be "
     169                 :             :                         "NULL");
     170                 :             : 
     171                 :    31317692 :   e = gfc_get_expr ();
     172                 :             : 
     173                 :    31317692 :   e->expr_type = EXPR_CONSTANT;
     174                 :    31317692 :   e->ts.type = type;
     175                 :    31317692 :   e->ts.kind = kind;
     176                 :    31317692 :   e->where = *where;
     177                 :             : 
     178                 :    31317692 :   switch (type)
     179                 :             :     {
     180                 :    30376130 :     case BT_INTEGER:
     181                 :    30376130 :     case BT_UNSIGNED:
     182                 :    30376130 :       mpz_init (e->value.integer);
     183                 :    30376130 :       break;
     184                 :             : 
     185                 :      418828 :     case BT_REAL:
     186                 :      418828 :       gfc_set_model_kind (kind);
     187                 :      418828 :       mpfr_init (e->value.real);
     188                 :      418828 :       break;
     189                 :             : 
     190                 :       18886 :     case BT_COMPLEX:
     191                 :       18886 :       gfc_set_model_kind (kind);
     192                 :       18886 :       mpc_init2 (e->value.complex, mpfr_get_default_prec());
     193                 :       18886 :       break;
     194                 :             : 
     195                 :             :     default:
     196                 :             :       break;
     197                 :             :     }
     198                 :             : 
     199                 :    31317692 :   return e;
     200                 :             : }
     201                 :             : 
     202                 :             : 
     203                 :             : /* Get a new expression node that is an string constant.
     204                 :             :    If no string is passed, a string of len is allocated,
     205                 :             :    blanked and null-terminated.  */
     206                 :             : 
     207                 :             : gfc_expr *
     208                 :      338690 : gfc_get_character_expr (int kind, locus *where, const char *src, gfc_charlen_t len)
     209                 :             : {
     210                 :      338690 :   gfc_expr *e;
     211                 :      338690 :   gfc_char_t *dest;
     212                 :             : 
     213                 :      338690 :   if (!src)
     214                 :             :     {
     215                 :      337015 :       dest = gfc_get_wide_string (len + 1);
     216                 :      337015 :       gfc_wide_memset (dest, ' ', len);
     217                 :      337015 :       dest[len] = '\0';
     218                 :             :     }
     219                 :             :   else
     220                 :        1675 :     dest = gfc_char_to_widechar (src);
     221                 :             : 
     222                 :      340411 :   e = gfc_get_constant_expr (BT_CHARACTER, kind,
     223                 :             :                             where ? where : &gfc_current_locus);
     224                 :      338690 :   e->value.character.string = dest;
     225                 :      338690 :   e->value.character.length = len;
     226                 :             : 
     227                 :      338690 :   return e;
     228                 :             : }
     229                 :             : 
     230                 :             : 
     231                 :             : /* Get a new expression node that is an integer constant.  */
     232                 :             : 
     233                 :             : gfc_expr *
     234                 :    14372576 : gfc_get_int_expr (int kind, locus *where, HOST_WIDE_INT value)
     235                 :             : {
     236                 :    14372576 :   gfc_expr *p;
     237                 :    28703802 :   p = gfc_get_constant_expr (BT_INTEGER, kind,
     238                 :             :                              where ? where : &gfc_current_locus);
     239                 :             : 
     240                 :    14372576 :   const wide_int w = wi::shwi (value, kind * BITS_PER_UNIT);
     241                 :    14372576 :   wi::to_mpz (w, p->value.integer, SIGNED);
     242                 :             : 
     243                 :    14372576 :   return p;
     244                 :    14372576 : }
     245                 :             : 
     246                 :             : /* Get a new expression node that is an unsigned constant.  */
     247                 :             : 
     248                 :             : gfc_expr *
     249                 :          66 : gfc_get_unsigned_expr (int kind, locus *where, HOST_WIDE_INT value)
     250                 :             : {
     251                 :          66 :   gfc_expr *p;
     252                 :         132 :   p = gfc_get_constant_expr (BT_UNSIGNED, kind,
     253                 :             :                              where ? where : &gfc_current_locus);
     254                 :          66 :   const wide_int w = wi::shwi (value, kind * BITS_PER_UNIT);
     255                 :          66 :   wi::to_mpz (w, p->value.integer, UNSIGNED);
     256                 :             : 
     257                 :          66 :   return p;
     258                 :          66 : }
     259                 :             : 
     260                 :             : /* Get a new expression node that is a logical constant.  */
     261                 :             : 
     262                 :             : gfc_expr *
     263                 :       74293 : gfc_get_logical_expr (int kind, locus *where, bool value)
     264                 :             : {
     265                 :       74293 :   gfc_expr *p;
     266                 :       85661 :   p = gfc_get_constant_expr (BT_LOGICAL, kind,
     267                 :             :                              where ? where : &gfc_current_locus);
     268                 :             : 
     269                 :       74293 :   p->value.logical = value;
     270                 :             : 
     271                 :       74293 :   return p;
     272                 :             : }
     273                 :             : 
     274                 :             : 
     275                 :             : gfc_expr *
     276                 :       31777 : gfc_get_iokind_expr (locus *where, io_kind k)
     277                 :             : {
     278                 :       31777 :   gfc_expr *e;
     279                 :             : 
     280                 :             :   /* Set the types to something compatible with iokind. This is needed to
     281                 :             :      get through gfc_free_expr later since iokind really has no Basic Type,
     282                 :             :      BT, of its own.  */
     283                 :             : 
     284                 :       31777 :   e = gfc_get_expr ();
     285                 :       31777 :   e->expr_type = EXPR_CONSTANT;
     286                 :       31777 :   e->ts.type = BT_LOGICAL;
     287                 :       31777 :   e->value.iokind = k;
     288                 :       31777 :   e->where = *where;
     289                 :             : 
     290                 :       31777 :   return e;
     291                 :             : }
     292                 :             : 
     293                 :             : 
     294                 :             : /* Given an expression pointer, return a copy of the expression.  This
     295                 :             :    subroutine is recursive.  */
     296                 :             : 
     297                 :             : gfc_expr *
     298                 :    55806870 : gfc_copy_expr (gfc_expr *p)
     299                 :             : {
     300                 :    55806870 :   gfc_expr *q;
     301                 :    55806870 :   gfc_char_t *s;
     302                 :    55806870 :   char *c;
     303                 :             : 
     304                 :    55806870 :   if (p == NULL)
     305                 :             :     return NULL;
     306                 :             : 
     307                 :    48016324 :   q = gfc_get_expr ();
     308                 :    48016324 :   *q = *p;
     309                 :             : 
     310                 :    48016324 :   switch (q->expr_type)
     311                 :             :     {
     312                 :         977 :     case EXPR_SUBSTRING:
     313                 :         977 :       s = gfc_get_wide_string (p->value.character.length + 1);
     314                 :         977 :       q->value.character.string = s;
     315                 :         977 :       memcpy (s, p->value.character.string,
     316                 :         977 :               (p->value.character.length + 1) * sizeof (gfc_char_t));
     317                 :         977 :       break;
     318                 :             : 
     319                 :    16775247 :     case EXPR_CONSTANT:
     320                 :             :       /* Copy target representation, if it exists.  */
     321                 :    16775247 :       if (p->representation.string)
     322                 :             :         {
     323                 :        3466 :           c = XCNEWVEC (char, p->representation.length + 1);
     324                 :        3466 :           q->representation.string = c;
     325                 :        3466 :           memcpy (c, p->representation.string, (p->representation.length + 1));
     326                 :             :         }
     327                 :             : 
     328                 :             :       /* Copy the values of any pointer components of p->value.  */
     329                 :    16775247 :       switch (q->ts.type)
     330                 :             :         {
     331                 :    15133335 :         case BT_INTEGER:
     332                 :    15133335 :         case BT_UNSIGNED:
     333                 :    15133335 :           mpz_init_set (q->value.integer, p->value.integer);
     334                 :    15133335 :           break;
     335                 :             : 
     336                 :      231877 :         case BT_REAL:
     337                 :      231877 :           gfc_set_model_kind (q->ts.kind);
     338                 :      231877 :           mpfr_init (q->value.real);
     339                 :      231877 :           mpfr_set (q->value.real, p->value.real, GFC_RND_MODE);
     340                 :      231877 :           break;
     341                 :             : 
     342                 :       26776 :         case BT_COMPLEX:
     343                 :       26776 :           gfc_set_model_kind (q->ts.kind);
     344                 :       26776 :           mpc_init2 (q->value.complex, mpfr_get_default_prec());
     345                 :       26776 :           mpc_set (q->value.complex, p->value.complex, GFC_MPC_RND_MODE);
     346                 :       26776 :           break;
     347                 :             : 
     348                 :      281934 :         case BT_CHARACTER:
     349                 :      281934 :           if (p->representation.string
     350                 :         784 :               && p->ts.kind == gfc_default_character_kind)
     351                 :         778 :             q->value.character.string
     352                 :         778 :               = gfc_char_to_widechar (q->representation.string);
     353                 :             :           else
     354                 :             :             {
     355                 :      281156 :               s = gfc_get_wide_string (p->value.character.length + 1);
     356                 :      281156 :               q->value.character.string = s;
     357                 :             : 
     358                 :             :               /* This is the case for the C_NULL_CHAR named constant.  */
     359                 :      281156 :               if (p->value.character.length == 0
     360                 :        2359 :                   && (p->ts.is_c_interop || p->ts.is_iso_c))
     361                 :             :                 {
     362                 :           0 :                   *s = '\0';
     363                 :             :                   /* Need to set the length to 1 to make sure the NUL
     364                 :             :                      terminator is copied.  */
     365                 :           0 :                   q->value.character.length = 1;
     366                 :             :                 }
     367                 :             :               else
     368                 :      281156 :                 memcpy (s, p->value.character.string,
     369                 :      281156 :                         (p->value.character.length + 1) * sizeof (gfc_char_t));
     370                 :             :             }
     371                 :             :           break;
     372                 :             : 
     373                 :             :         case BT_HOLLERITH:
     374                 :             :         case BT_LOGICAL:
     375                 :             :         case_bt_struct:
     376                 :             :         case BT_CLASS:
     377                 :             :         case BT_ASSUMED:
     378                 :             :           break;                /* Already done.  */
     379                 :             : 
     380                 :           0 :         case BT_BOZ:
     381                 :           0 :           q->boz.len = p->boz.len;
     382                 :           0 :           q->boz.rdx = p->boz.rdx;
     383                 :           0 :           q->boz.str = XCNEWVEC (char, q->boz.len + 1);
     384                 :           0 :           strncpy (q->boz.str, p->boz.str, p->boz.len);
     385                 :           0 :           break;
     386                 :             : 
     387                 :           0 :         case BT_PROCEDURE:
     388                 :           0 :         case BT_VOID:
     389                 :             :            /* Should never be reached.  */
     390                 :           0 :         case BT_UNKNOWN:
     391                 :           0 :           gfc_internal_error ("gfc_copy_expr(): Bad expr node");
     392                 :             :           /* Not reached.  */
     393                 :             :         }
     394                 :             : 
     395                 :             :       break;
     396                 :             : 
     397                 :    16414255 :     case EXPR_OP:
     398                 :    16414255 :       switch (q->value.op.op)
     399                 :             :         {
     400                 :     5269204 :         case INTRINSIC_NOT:
     401                 :     5269204 :         case INTRINSIC_PARENTHESES:
     402                 :     5269204 :         case INTRINSIC_UPLUS:
     403                 :     5269204 :         case INTRINSIC_UMINUS:
     404                 :     5269204 :           q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
     405                 :     5269204 :           break;
     406                 :             : 
     407                 :    11145051 :         default:                /* Binary operators.  */
     408                 :    11145051 :           q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
     409                 :    11145051 :           q->value.op.op2 = gfc_copy_expr (p->value.op.op2);
     410                 :    11145051 :           break;
     411                 :             :         }
     412                 :             : 
     413                 :             :       break;
     414                 :             : 
     415                 :           2 :     case EXPR_CONDITIONAL:
     416                 :           2 :       q->value.conditional.condition
     417                 :           2 :         = gfc_copy_expr (p->value.conditional.condition);
     418                 :           2 :       q->value.conditional.true_expr
     419                 :           2 :         = gfc_copy_expr (p->value.conditional.true_expr);
     420                 :           2 :       q->value.conditional.false_expr
     421                 :           2 :         = gfc_copy_expr (p->value.conditional.false_expr);
     422                 :           2 :       break;
     423                 :             : 
     424                 :      388615 :     case EXPR_FUNCTION:
     425                 :      777230 :       q->value.function.actual =
     426                 :      388615 :         gfc_copy_actual_arglist (p->value.function.actual);
     427                 :      388615 :       break;
     428                 :             : 
     429                 :          87 :     case EXPR_COMPCALL:
     430                 :          87 :     case EXPR_PPC:
     431                 :         174 :       q->value.compcall.actual =
     432                 :          87 :         gfc_copy_actual_arglist (p->value.compcall.actual);
     433                 :          87 :       q->value.compcall.tbp = p->value.compcall.tbp;
     434                 :          87 :       break;
     435                 :             : 
     436                 :      105659 :     case EXPR_STRUCTURE:
     437                 :      105659 :     case EXPR_ARRAY:
     438                 :      105659 :       q->value.constructor = gfc_constructor_copy (p->value.constructor);
     439                 :      105659 :       break;
     440                 :             : 
     441                 :             :     case EXPR_VARIABLE:
     442                 :             :     case EXPR_NULL:
     443                 :             :       break;
     444                 :             : 
     445                 :           0 :     case EXPR_UNKNOWN:
     446                 :           0 :       gcc_unreachable ();
     447                 :             :     }
     448                 :             : 
     449                 :    48016324 :   q->shape = gfc_copy_shape (p->shape, p->rank);
     450                 :             : 
     451                 :    48016324 :   q->ref = gfc_copy_ref (p->ref);
     452                 :             : 
     453                 :    48016324 :   if (p->param_list)
     454                 :         175 :     q->param_list = gfc_copy_actual_arglist (p->param_list);
     455                 :             : 
     456                 :             :   return q;
     457                 :             : }
     458                 :             : 
     459                 :             : 
     460                 :             : void
     461                 :      437050 : gfc_clear_shape (mpz_t *shape, int rank)
     462                 :             : {
     463                 :      437050 :   int i;
     464                 :             : 
     465                 :     1002880 :   for (i = 0; i < rank; i++)
     466                 :      565830 :     mpz_clear (shape[i]);
     467                 :      437050 : }
     468                 :             : 
     469                 :             : 
     470                 :             : void
     471                 :    87872899 : gfc_free_shape (mpz_t **shape, int rank)
     472                 :             : {
     473                 :    87872899 :   if (*shape == NULL)
     474                 :             :     return;
     475                 :             : 
     476                 :      423182 :   gfc_clear_shape (*shape, rank);
     477                 :      423182 :   free (*shape);
     478                 :      423182 :   *shape = NULL;
     479                 :             : }
     480                 :             : 
     481                 :             : 
     482                 :             : /* Workhorse function for gfc_free_expr() that frees everything
     483                 :             :    beneath an expression node, but not the node itself.  This is
     484                 :             :    useful when we want to simplify a node and replace it with
     485                 :             :    something else or the expression node belongs to another structure.  */
     486                 :             : 
     487                 :             : static void
     488                 :    87852509 : free_expr0 (gfc_expr *e)
     489                 :             : {
     490                 :    87852509 :   switch (e->expr_type)
     491                 :             :     {
     492                 :    48332133 :     case EXPR_CONSTANT:
     493                 :             :       /* Free any parts of the value that need freeing.  */
     494                 :    48332133 :       switch (e->ts.type)
     495                 :             :         {
     496                 :    45715898 :         case BT_INTEGER:
     497                 :    45715898 :         case BT_UNSIGNED:
     498                 :    45715898 :           mpz_clear (e->value.integer);
     499                 :    45715898 :           break;
     500                 :             : 
     501                 :      650022 :         case BT_REAL:
     502                 :      650022 :           mpfr_clear (e->value.real);
     503                 :      650022 :           break;
     504                 :             : 
     505                 :      636456 :         case BT_CHARACTER:
     506                 :      636456 :           free (e->value.character.string);
     507                 :      636456 :           break;
     508                 :             : 
     509                 :       45565 :         case BT_COMPLEX:
     510                 :       45565 :           mpc_clear (e->value.complex);
     511                 :       45565 :           break;
     512                 :             : 
     513                 :        1668 :         case BT_BOZ:
     514                 :        1668 :           free (e->boz.str);
     515                 :        1668 :           break;
     516                 :             : 
     517                 :             :         default:
     518                 :             :           break;
     519                 :             :         }
     520                 :             : 
     521                 :             :       /* Free the representation.  */
     522                 :    48332133 :       free (e->representation.string);
     523                 :             : 
     524                 :    48332133 :       break;
     525                 :             : 
     526                 :    17998147 :     case EXPR_OP:
     527                 :    17998147 :       if (e->value.op.op1 != NULL)
     528                 :     1610642 :         gfc_free_expr (e->value.op.op1);
     529                 :    17998147 :       if (e->value.op.op2 != NULL)
     530                 :     1459000 :         gfc_free_expr (e->value.op.op2);
     531                 :             :       break;
     532                 :             : 
     533                 :          92 :     case EXPR_CONDITIONAL:
     534                 :          92 :       gfc_free_expr (e->value.conditional.condition);
     535                 :          92 :       gfc_free_expr (e->value.conditional.true_expr);
     536                 :          92 :       gfc_free_expr (e->value.conditional.false_expr);
     537                 :          92 :       break;
     538                 :             : 
     539                 :     1853226 :     case EXPR_FUNCTION:
     540                 :     1853226 :       gfc_free_actual_arglist (e->value.function.actual);
     541                 :     1853226 :       break;
     542                 :             : 
     543                 :        3482 :     case EXPR_COMPCALL:
     544                 :        3482 :     case EXPR_PPC:
     545                 :        3482 :       gfc_free_actual_arglist (e->value.compcall.actual);
     546                 :        3482 :       break;
     547                 :             : 
     548                 :             :     case EXPR_VARIABLE:
     549                 :             :       break;
     550                 :             : 
     551                 :      322524 :     case EXPR_ARRAY:
     552                 :      322524 :     case EXPR_STRUCTURE:
     553                 :      322524 :       gfc_constructor_free (e->value.constructor);
     554                 :      322524 :       break;
     555                 :             : 
     556                 :        1190 :     case EXPR_SUBSTRING:
     557                 :        1190 :       free (e->value.character.string);
     558                 :        1190 :       break;
     559                 :             : 
     560                 :             :     case EXPR_NULL:
     561                 :             :       break;
     562                 :             : 
     563                 :           0 :     default:
     564                 :           0 :       gfc_internal_error ("free_expr0(): Bad expr type");
     565                 :             :     }
     566                 :             : 
     567                 :             :   /* Free a shape array.  */
     568                 :    87852509 :   gfc_free_shape (&e->shape, e->rank);
     569                 :             : 
     570                 :    87852509 :   gfc_free_ref_list (e->ref);
     571                 :             : 
     572                 :    87852509 :   gfc_free_actual_arglist (e->param_list);
     573                 :             : 
     574                 :    87852509 :   memset (e, '\0', sizeof (gfc_expr));
     575                 :    87852509 : }
     576                 :             : 
     577                 :             : 
     578                 :             : /* Free an expression node and everything beneath it.  */
     579                 :             : 
     580                 :             : void
     581                 :   118519549 : gfc_free_expr (gfc_expr *e)
     582                 :             : {
     583                 :   118519549 :   if (e == NULL)
     584                 :             :     return;
     585                 :    57019640 :   free_expr0 (e);
     586                 :    57019614 :   free (e);
     587                 :             : }
     588                 :             : 
     589                 :             : 
     590                 :             : /* Free an argument list and everything below it.  */
     591                 :             : 
     592                 :             : void
     593                 :    89844894 : gfc_free_actual_arglist (gfc_actual_arglist *a1)
     594                 :             : {
     595                 :    89844894 :   gfc_actual_arglist *a2;
     596                 :             : 
     597                 :    92979582 :   while (a1)
     598                 :             :     {
     599                 :     3134714 :       a2 = a1->next;
     600                 :     3134714 :       if (a1->expr)
     601                 :     2855273 :         gfc_free_expr (a1->expr);
     602                 :     3134688 :       free (a1->associated_dummy);
     603                 :     3134688 :       free (a1);
     604                 :     3134688 :       a1 = a2;
     605                 :             :     }
     606                 :    89844868 : }
     607                 :             : 
     608                 :             : 
     609                 :             : /* Copy an arglist structure and all of the arguments.  */
     610                 :             : 
     611                 :             : gfc_actual_arglist *
     612                 :      391735 : gfc_copy_actual_arglist (gfc_actual_arglist *p)
     613                 :             : {
     614                 :      391735 :   gfc_actual_arglist *head, *tail, *new_arg;
     615                 :             : 
     616                 :      391735 :   head = tail = NULL;
     617                 :             : 
     618                 :     1147163 :   for (; p; p = p->next)
     619                 :             :     {
     620                 :      755428 :       new_arg = gfc_get_actual_arglist ();
     621                 :      755428 :       *new_arg = *p;
     622                 :             : 
     623                 :      755428 :       if (p->associated_dummy != NULL)
     624                 :             :         {
     625                 :      682756 :           new_arg->associated_dummy = gfc_get_dummy_arg ();
     626                 :      682756 :           *new_arg->associated_dummy = *p->associated_dummy;
     627                 :             :         }
     628                 :             : 
     629                 :      755428 :       new_arg->expr = gfc_copy_expr (p->expr);
     630                 :      755428 :       new_arg->next = NULL;
     631                 :             : 
     632                 :      755428 :       if (head == NULL)
     633                 :             :         head = new_arg;
     634                 :             :       else
     635                 :      365569 :         tail->next = new_arg;
     636                 :             : 
     637                 :      755428 :       tail = new_arg;
     638                 :             :     }
     639                 :             : 
     640                 :      391735 :   return head;
     641                 :             : }
     642                 :             : 
     643                 :             : 
     644                 :             : /* Free a list of reference structures.  */
     645                 :             : 
     646                 :             : void
     647                 :    87951400 : gfc_free_ref_list (gfc_ref *p)
     648                 :             : {
     649                 :    87951400 :   gfc_ref *q;
     650                 :    87951400 :   int i;
     651                 :             : 
     652                 :    89208391 :   for (; p; p = q)
     653                 :             :     {
     654                 :     1256991 :       q = p->next;
     655                 :             : 
     656                 :     1256991 :       switch (p->type)
     657                 :             :         {
     658                 :             :         case REF_ARRAY:
     659                 :    15218592 :           for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
     660                 :             :             {
     661                 :    14267430 :               gfc_free_expr (p->u.ar.start[i]);
     662                 :    14267430 :               gfc_free_expr (p->u.ar.end[i]);
     663                 :    14267430 :               gfc_free_expr (p->u.ar.stride[i]);
     664                 :             :             }
     665                 :             : 
     666                 :      951162 :           gfc_free_expr (p->u.ar.stat);
     667                 :      951162 :           gfc_free_expr (p->u.ar.team);
     668                 :      951162 :           break;
     669                 :             : 
     670                 :       20680 :         case REF_SUBSTRING:
     671                 :       20680 :           gfc_free_expr (p->u.ss.start);
     672                 :       20680 :           gfc_free_expr (p->u.ss.end);
     673                 :       20680 :           break;
     674                 :             : 
     675                 :             :         case REF_COMPONENT:
     676                 :             :         case REF_INQUIRY:
     677                 :             :           break;
     678                 :             :         }
     679                 :             : 
     680                 :     1256991 :       free (p);
     681                 :             :     }
     682                 :    87951400 : }
     683                 :             : 
     684                 :             : 
     685                 :             : /* Graft the *src expression onto the *dest subexpression.  */
     686                 :             : 
     687                 :             : void
     688                 :    30832467 : gfc_replace_expr (gfc_expr *dest, gfc_expr *src)
     689                 :             : {
     690                 :    30832467 :   free_expr0 (dest);
     691                 :    30832467 :   *dest = *src;
     692                 :    30832467 :   free (src);
     693                 :    30832467 : }
     694                 :             : 
     695                 :             : 
     696                 :             : /* Try to extract an integer constant from the passed expression node.
     697                 :             :    Return true if some error occurred, false on success.  If REPORT_ERROR
     698                 :             :    is non-zero, emit error, for positive REPORT_ERROR using gfc_error,
     699                 :             :    for negative using gfc_error_now.  */
     700                 :             : 
     701                 :             : bool
     702                 :      456669 : gfc_extract_int (gfc_expr *expr, int *result, int report_error)
     703                 :             : {
     704                 :      456669 :   gfc_ref *ref;
     705                 :             : 
     706                 :             :   /* A KIND component is a parameter too. The expression for it
     707                 :             :      is stored in the initializer and should be consistent with
     708                 :             :      the tests below.  */
     709                 :      456669 :   if (gfc_expr_attr(expr).pdt_kind)
     710                 :             :     {
     711                 :          64 :       for (ref = expr->ref; ref; ref = ref->next)
     712                 :             :         {
     713                 :          20 :            if (ref->u.c.component->attr.pdt_kind)
     714                 :          20 :              expr = ref->u.c.component->initializer;
     715                 :             :         }
     716                 :             :     }
     717                 :             : 
     718                 :      456669 :   if (expr->expr_type != EXPR_CONSTANT)
     719                 :             :     {
     720                 :         894 :       if (report_error > 0)
     721                 :         857 :         gfc_error ("Constant expression required at %C");
     722                 :          37 :       else if (report_error < 0)
     723                 :           4 :         gfc_error_now ("Constant expression required at %C");
     724                 :         894 :       return true;
     725                 :             :     }
     726                 :             : 
     727                 :      455775 :   if (expr->ts.type != BT_INTEGER)
     728                 :             :     {
     729                 :         418 :       if (report_error > 0)
     730                 :         418 :         gfc_error ("Integer expression required at %C");
     731                 :           0 :       else if (report_error < 0)
     732                 :           0 :         gfc_error_now ("Integer expression required at %C");
     733                 :         418 :       return true;
     734                 :             :     }
     735                 :             : 
     736                 :      455357 :   if ((mpz_cmp_si (expr->value.integer, INT_MAX) > 0)
     737                 :      455357 :       || (mpz_cmp_si (expr->value.integer, INT_MIN) < 0))
     738                 :             :     {
     739                 :           0 :       if (report_error > 0)
     740                 :           0 :         gfc_error ("Integer value too large in expression at %C");
     741                 :           0 :       else if (report_error < 0)
     742                 :           0 :         gfc_error_now ("Integer value too large in expression at %C");
     743                 :           0 :       return true;
     744                 :             :     }
     745                 :             : 
     746                 :      455357 :   *result = (int) mpz_get_si (expr->value.integer);
     747                 :             : 
     748                 :      455357 :   return false;
     749                 :             : }
     750                 :             : 
     751                 :             : /* Same as gfc_extract_int, but use a HWI.  */
     752                 :             : 
     753                 :             : bool
     754                 :        9369 : gfc_extract_hwi (gfc_expr *expr, HOST_WIDE_INT *result, int report_error)
     755                 :             : {
     756                 :        9369 :   gfc_ref *ref;
     757                 :             : 
     758                 :             :   /* A KIND component is a parameter too. The expression for it is
     759                 :             :      stored in the initializer and should be consistent with the tests
     760                 :             :      below.  */
     761                 :        9369 :   if (gfc_expr_attr(expr).pdt_kind)
     762                 :             :     {
     763                 :           0 :       for (ref = expr->ref; ref; ref = ref->next)
     764                 :             :         {
     765                 :           0 :           if (ref->u.c.component->attr.pdt_kind)
     766                 :           0 :             expr = ref->u.c.component->initializer;
     767                 :             :         }
     768                 :             :     }
     769                 :             : 
     770                 :        9369 :   if (expr->expr_type != EXPR_CONSTANT)
     771                 :             :     {
     772                 :          67 :       if (report_error > 0)
     773                 :           0 :         gfc_error ("Constant expression required at %C");
     774                 :          67 :       else if (report_error < 0)
     775                 :           0 :         gfc_error_now ("Constant expression required at %C");
     776                 :          67 :       return true;
     777                 :             :     }
     778                 :             : 
     779                 :        9302 :   if (expr->ts.type != BT_INTEGER)
     780                 :             :     {
     781                 :           0 :       if (report_error > 0)
     782                 :           0 :         gfc_error ("Integer expression required at %C");
     783                 :           0 :       else if (report_error < 0)
     784                 :           0 :         gfc_error_now ("Integer expression required at %C");
     785                 :           0 :       return true;
     786                 :             :     }
     787                 :             : 
     788                 :             :   /* Use long_long_integer_type_node to determine when to saturate.  */
     789                 :        9302 :   const wide_int val = wi::from_mpz (long_long_integer_type_node,
     790                 :        9302 :                                      expr->value.integer, false);
     791                 :             : 
     792                 :        9302 :   if (!wi::fits_shwi_p (val))
     793                 :             :     {
     794                 :           0 :       if (report_error > 0)
     795                 :           0 :         gfc_error ("Integer value too large in expression at %C");
     796                 :           0 :       else if (report_error < 0)
     797                 :           0 :         gfc_error_now ("Integer value too large in expression at %C");
     798                 :           0 :       return true;
     799                 :             :     }
     800                 :             : 
     801                 :        9302 :   *result = val.to_shwi ();
     802                 :             : 
     803                 :        9302 :   return false;
     804                 :        9302 : }
     805                 :             : 
     806                 :             : 
     807                 :             : /* Recursively copy a list of reference structures.  */
     808                 :             : 
     809                 :             : gfc_ref *
     810                 :    48270290 : gfc_copy_ref (gfc_ref *src)
     811                 :             : {
     812                 :    48270290 :   gfc_array_ref *ar;
     813                 :    48270290 :   gfc_ref *dest;
     814                 :             : 
     815                 :    48270290 :   if (src == NULL)
     816                 :             :     return NULL;
     817                 :             : 
     818                 :      227789 :   dest = gfc_get_ref ();
     819                 :      227789 :   dest->type = src->type;
     820                 :             : 
     821                 :      227789 :   switch (src->type)
     822                 :             :     {
     823                 :      165646 :     case REF_ARRAY:
     824                 :      165646 :       ar = gfc_copy_array_ref (&src->u.ar);
     825                 :      165646 :       dest->u.ar = *ar;
     826                 :      165646 :       free (ar);
     827                 :      165646 :       break;
     828                 :             : 
     829                 :       54211 :     case REF_COMPONENT:
     830                 :       54211 :       dest->u.c = src->u.c;
     831                 :       54211 :       break;
     832                 :             : 
     833                 :        2077 :     case REF_INQUIRY:
     834                 :        2077 :       dest->u.i = src->u.i;
     835                 :        2077 :       break;
     836                 :             : 
     837                 :        5855 :     case REF_SUBSTRING:
     838                 :        5855 :       dest->u.ss = src->u.ss;
     839                 :        5855 :       dest->u.ss.start = gfc_copy_expr (src->u.ss.start);
     840                 :        5855 :       dest->u.ss.end = gfc_copy_expr (src->u.ss.end);
     841                 :        5855 :       break;
     842                 :             :     }
     843                 :             : 
     844                 :      227789 :   dest->next = gfc_copy_ref (src->next);
     845                 :             : 
     846                 :      227789 :   return dest;
     847                 :             : }
     848                 :             : 
     849                 :             : 
     850                 :             : /* Detect whether an expression has any vector index array references.  */
     851                 :             : 
     852                 :             : bool
     853                 :       34489 : gfc_has_vector_index (gfc_expr *e)
     854                 :             : {
     855                 :       34489 :   gfc_ref *ref;
     856                 :       34489 :   int i;
     857                 :       41448 :   for (ref = e->ref; ref; ref = ref->next)
     858                 :        6969 :     if (ref->type == REF_ARRAY)
     859                 :       12021 :       for (i = 0; i < ref->u.ar.dimen; i++)
     860                 :        6490 :         if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
     861                 :             :           return 1;
     862                 :             :   return 0;
     863                 :             : }
     864                 :             : 
     865                 :             : 
     866                 :             : bool
     867                 :        2122 : gfc_is_ptr_fcn (gfc_expr *e)
     868                 :             : {
     869                 :        2122 :   return e != NULL && e->expr_type == EXPR_FUNCTION
     870                 :        2567 :               && gfc_expr_attr (e).pointer;
     871                 :             : }
     872                 :             : 
     873                 :             : 
     874                 :             : /* Copy a shape array.  */
     875                 :             : 
     876                 :             : mpz_t *
     877                 :    48273365 : gfc_copy_shape (mpz_t *shape, int rank)
     878                 :             : {
     879                 :    48273365 :   mpz_t *new_shape;
     880                 :    48273365 :   int n;
     881                 :             : 
     882                 :    48273365 :   if (shape == NULL)
     883                 :             :     return NULL;
     884                 :             : 
     885                 :      153157 :   new_shape = gfc_get_shape (rank);
     886                 :             : 
     887                 :      514713 :   for (n = 0; n < rank; n++)
     888                 :      208399 :     mpz_init_set (new_shape[n], shape[n]);
     889                 :             : 
     890                 :             :   return new_shape;
     891                 :             : }
     892                 :             : 
     893                 :             : 
     894                 :             : /* Copy a shape array excluding dimension N, where N is an integer
     895                 :             :    constant expression.  Dimensions are numbered in Fortran style --
     896                 :             :    starting with ONE.
     897                 :             : 
     898                 :             :    So, if the original shape array contains R elements
     899                 :             :       { s1 ... sN-1  sN  sN+1 ... sR-1 sR}
     900                 :             :    the result contains R-1 elements:
     901                 :             :       { s1 ... sN-1  sN+1    ...  sR-1}
     902                 :             : 
     903                 :             :    If anything goes wrong -- N is not a constant, its value is out
     904                 :             :    of range -- or anything else, just returns NULL.  */
     905                 :             : 
     906                 :             : mpz_t *
     907                 :        2990 : gfc_copy_shape_excluding (mpz_t *shape, int rank, gfc_expr *dim)
     908                 :             : {
     909                 :        2990 :   mpz_t *new_shape, *s;
     910                 :        2990 :   int i, n;
     911                 :             : 
     912                 :        2990 :   if (shape == NULL
     913                 :        2990 :       || rank <= 1
     914                 :        2424 :       || dim == NULL
     915                 :        2424 :       || dim->expr_type != EXPR_CONSTANT
     916                 :        2151 :       || dim->ts.type != BT_INTEGER)
     917                 :             :     return NULL;
     918                 :             : 
     919                 :        2151 :   n = mpz_get_si (dim->value.integer);
     920                 :        2151 :   n--; /* Convert to zero based index.  */
     921                 :        2151 :   if (n < 0 || n >= rank)
     922                 :             :     return NULL;
     923                 :             : 
     924                 :        2151 :   s = new_shape = gfc_get_shape (rank - 1);
     925                 :             : 
     926                 :        9177 :   for (i = 0; i < rank; i++)
     927                 :             :     {
     928                 :        4875 :       if (i == n)
     929                 :        2151 :         continue;
     930                 :        2724 :       mpz_init_set (*s, shape[i]);
     931                 :        2724 :       s++;
     932                 :             :     }
     933                 :             : 
     934                 :             :   return new_shape;
     935                 :             : }
     936                 :             : 
     937                 :             : 
     938                 :             : /* Return the maximum kind of two expressions.  In general, higher
     939                 :             :    kind numbers mean more precision for numeric types.  */
     940                 :             : 
     941                 :             : int
     942                 :       94235 : gfc_kind_max (gfc_expr *e1, gfc_expr *e2)
     943                 :             : {
     944                 :       94235 :   return (e1->ts.kind > e2->ts.kind) ? e1->ts.kind : e2->ts.kind;
     945                 :             : }
     946                 :             : 
     947                 :             : 
     948                 :             : /* Returns nonzero if the type is numeric, zero otherwise.  */
     949                 :             : 
     950                 :             : static bool
     951                 :    25468295 : numeric_type (bt type)
     952                 :             : {
     953                 :           0 :   return type == BT_COMPLEX || type == BT_REAL || type == BT_INTEGER
     954                 :           0 :     || type == BT_UNSIGNED;
     955                 :             : }
     956                 :             : 
     957                 :             : 
     958                 :             : /* Returns nonzero if the typespec is a numeric type, zero otherwise.  */
     959                 :             : 
     960                 :             : bool
     961                 :    25463854 : gfc_numeric_ts (gfc_typespec *ts)
     962                 :             : {
     963                 :    25463854 :   return numeric_type (ts->type);
     964                 :             : }
     965                 :             : 
     966                 :             : 
     967                 :             : /* Return an expression node with an optional argument list attached.
     968                 :             :    A variable number of gfc_expr pointers are strung together in an
     969                 :             :    argument list with a NULL pointer terminating the list.  */
     970                 :             : 
     971                 :             : gfc_expr *
     972                 :      130087 : gfc_build_conversion (gfc_expr *e)
     973                 :             : {
     974                 :      130087 :   gfc_expr *p;
     975                 :             : 
     976                 :      130087 :   p = gfc_get_expr ();
     977                 :      130087 :   p->expr_type = EXPR_FUNCTION;
     978                 :      130087 :   p->symtree = NULL;
     979                 :      130087 :   p->value.function.actual = gfc_get_actual_arglist ();
     980                 :      130087 :   p->value.function.actual->expr = e;
     981                 :             : 
     982                 :      130087 :   return p;
     983                 :             : }
     984                 :             : 
     985                 :             : 
     986                 :             : /* Given an expression node with some sort of numeric binary
     987                 :             :    expression, insert type conversions required to make the operands
     988                 :             :    have the same type. Conversion warnings are disabled if wconversion
     989                 :             :    is set to 0.
     990                 :             : 
     991                 :             :    The exception is that the operands of an exponential don't have to
     992                 :             :    have the same type.  If possible, the base is promoted to the type
     993                 :             :    of the exponent.  For example, 1**2.3 becomes 1.0**2.3, but
     994                 :             :    1.0**2 stays as it is.  */
     995                 :             : 
     996                 :             : void
     997                 :    12180699 : gfc_type_convert_binary (gfc_expr *e, int wconversion)
     998                 :             : {
     999                 :    12180699 :   gfc_expr *op1, *op2;
    1000                 :             : 
    1001                 :    12180699 :   op1 = e->value.op.op1;
    1002                 :    12180699 :   op2 = e->value.op.op2;
    1003                 :             : 
    1004                 :    12180699 :   if (op1->ts.type == BT_UNKNOWN || op2->ts.type == BT_UNKNOWN)
    1005                 :             :     {
    1006                 :           0 :       gfc_clear_ts (&e->ts);
    1007                 :           0 :       return;
    1008                 :             :     }
    1009                 :             : 
    1010                 :             :   /* Kind conversions of same type.  */
    1011                 :    12180699 :   if (op1->ts.type == op2->ts.type)
    1012                 :             :     {
    1013                 :    12160753 :       if (op1->ts.kind == op2->ts.kind)
    1014                 :             :         {
    1015                 :             :           /* No type conversions.  */
    1016                 :    12048133 :           e->ts = op1->ts;
    1017                 :    12048133 :           goto done;
    1018                 :             :         }
    1019                 :             : 
    1020                 :             :       /* Unsigned exponentiation is special, we need the type of the first
    1021                 :             :          argument here because of modulo arithmetic.  */
    1022                 :      112620 :       if (op1->ts.type == BT_UNSIGNED && e->value.op.op == INTRINSIC_POWER)
    1023                 :             :         {
    1024                 :       84378 :           e->ts = op1->ts;
    1025                 :       84378 :           goto done;
    1026                 :             :         }
    1027                 :             : 
    1028                 :       28242 :       if (op1->ts.kind > op2->ts.kind)
    1029                 :       21342 :         gfc_convert_type_warn (op2, &op1->ts, 2, wconversion);
    1030                 :             :       else
    1031                 :        6900 :         gfc_convert_type_warn (op1, &op2->ts, 2, wconversion);
    1032                 :             : 
    1033                 :       28242 :       e->ts = op1->ts;
    1034                 :       28242 :       goto done;
    1035                 :             :     }
    1036                 :             : 
    1037                 :             :   /* Integer combined with real or complex.  */
    1038                 :       19946 :   if (op2->ts.type == BT_INTEGER)
    1039                 :             :     {
    1040                 :       14929 :       e->ts = op1->ts;
    1041                 :             : 
    1042                 :             :       /* Special case for ** operator.  */
    1043                 :       14929 :       if (e->value.op.op == INTRINSIC_POWER)
    1044                 :        3174 :         goto done;
    1045                 :             : 
    1046                 :       11755 :       gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion);
    1047                 :       11755 :       goto done;
    1048                 :             :     }
    1049                 :             : 
    1050                 :        5017 :   if (op1->ts.type == BT_INTEGER)
    1051                 :             :     {
    1052                 :        4423 :       e->ts = op2->ts;
    1053                 :        4423 :       gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion);
    1054                 :        4423 :       goto done;
    1055                 :             :     }
    1056                 :             : 
    1057                 :             :   /* Real combined with complex.  */
    1058                 :         594 :   e->ts.type = BT_COMPLEX;
    1059                 :         594 :   if (op1->ts.kind > op2->ts.kind)
    1060                 :          25 :     e->ts.kind = op1->ts.kind;
    1061                 :             :   else
    1062                 :         569 :     e->ts.kind = op2->ts.kind;
    1063                 :         594 :   if (op1->ts.type != BT_COMPLEX || op1->ts.kind != e->ts.kind)
    1064                 :         116 :     gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion);
    1065                 :         594 :   if (op2->ts.type != BT_COMPLEX || op2->ts.kind != e->ts.kind)
    1066                 :         490 :     gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion);
    1067                 :             : 
    1068                 :         104 : done:
    1069                 :             :   return;
    1070                 :             : }
    1071                 :             : 
    1072                 :             : 
    1073                 :             : /* Standard intrinsics listed under F2018:10.1.12 (6), which are excluded in
    1074                 :             :    constant expressions, except TRANSFER (c.f. item (8)), which would need
    1075                 :             :    separate treatment.  */
    1076                 :             : 
    1077                 :             : static bool
    1078                 :      279774 : is_non_constant_intrinsic (gfc_expr *e)
    1079                 :             : {
    1080                 :      279774 :   if (e->expr_type == EXPR_FUNCTION
    1081                 :      279774 :       && e->value.function.isym)
    1082                 :             :     {
    1083                 :      279774 :       switch (e->value.function.isym->id)
    1084                 :             :         {
    1085                 :             :           case GFC_ISYM_COMMAND_ARGUMENT_COUNT:
    1086                 :             :           case GFC_ISYM_GET_TEAM:
    1087                 :             :           case GFC_ISYM_NULL:
    1088                 :             :           case GFC_ISYM_NUM_IMAGES:
    1089                 :             :           case GFC_ISYM_TEAM_NUMBER:
    1090                 :             :           case GFC_ISYM_THIS_IMAGE:
    1091                 :             :             return true;
    1092                 :             : 
    1093                 :      277675 :         default:
    1094                 :      277675 :           return false;
    1095                 :             :         }
    1096                 :             :     }
    1097                 :             :   return false;
    1098                 :             : }
    1099                 :             : 
    1100                 :             : 
    1101                 :             : /* Determine if an expression is constant in the sense of F08:7.1.12.
    1102                 :             :  * This function expects that the expression has already been simplified.  */
    1103                 :             : 
    1104                 :             : bool
    1105                 :    44934227 : gfc_is_constant_expr (gfc_expr *e)
    1106                 :             : {
    1107                 :    44934227 :   gfc_constructor *c;
    1108                 :    44934227 :   gfc_actual_arglist *arg;
    1109                 :             : 
    1110                 :    44934227 :   if (e == NULL)
    1111                 :             :     return true;
    1112                 :             : 
    1113                 :    44915310 :   switch (e->expr_type)
    1114                 :             :     {
    1115                 :     1096003 :     case EXPR_OP:
    1116                 :     1096003 :       return (gfc_is_constant_expr (e->value.op.op1)
    1117                 :     1096003 :               && (e->value.op.op2 == NULL
    1118                 :      100709 :                   || gfc_is_constant_expr (e->value.op.op2)));
    1119                 :             : 
    1120                 :           3 :     case EXPR_CONDITIONAL:
    1121                 :           3 :       return gfc_is_constant_expr (e->value.conditional.condition)
    1122                 :           0 :              && gfc_is_constant_expr (e->value.conditional.true_expr)
    1123                 :           3 :              && gfc_is_constant_expr (e->value.conditional.false_expr);
    1124                 :             : 
    1125                 :     1453824 :     case EXPR_VARIABLE:
    1126                 :             :       /* The only context in which this can occur is in a parameterized
    1127                 :             :          derived type declaration, so returning true is OK.  */
    1128                 :     1453824 :       if (e->symtree->n.sym->attr.pdt_len
    1129                 :     1452252 :           || e->symtree->n.sym->attr.pdt_kind)
    1130                 :             :         return true;
    1131                 :             :       return false;
    1132                 :             : 
    1133                 :      342649 :     case EXPR_FUNCTION:
    1134                 :      342649 :     case EXPR_PPC:
    1135                 :      342649 :     case EXPR_COMPCALL:
    1136                 :      342649 :       gcc_assert (e->symtree || e->value.function.esym
    1137                 :             :                   || e->value.function.isym);
    1138                 :             : 
    1139                 :             :       /* Check for intrinsics excluded in constant expressions.  */
    1140                 :      342649 :       if (e->value.function.isym && is_non_constant_intrinsic (e))
    1141                 :             :         return false;
    1142                 :             : 
    1143                 :             :       /* Call to intrinsic with at least one argument.  */
    1144                 :      340550 :       if (e->value.function.isym && e->value.function.actual)
    1145                 :             :         {
    1146                 :      285148 :           for (arg = e->value.function.actual; arg; arg = arg->next)
    1147                 :      281898 :             if (!gfc_is_constant_expr (arg->expr))
    1148                 :             :               return false;
    1149                 :             :         }
    1150                 :             : 
    1151                 :       66285 :       if (e->value.function.isym
    1152                 :        3410 :           && (e->value.function.isym->elemental
    1153                 :        3326 :               || e->value.function.isym->pure
    1154                 :        3170 :               || e->value.function.isym->inquiry
    1155                 :        3170 :               || e->value.function.isym->transformational))
    1156                 :             :         return true;
    1157                 :             : 
    1158                 :             :       return false;
    1159                 :             : 
    1160                 :             :     case EXPR_CONSTANT:
    1161                 :             :     case EXPR_NULL:
    1162                 :             :       return true;
    1163                 :             : 
    1164                 :        1956 :     case EXPR_SUBSTRING:
    1165                 :        1956 :       return e->ref == NULL || (gfc_is_constant_expr (e->ref->u.ss.start)
    1166                 :         813 :                                 && gfc_is_constant_expr (e->ref->u.ss.end));
    1167                 :             : 
    1168                 :      139540 :     case EXPR_ARRAY:
    1169                 :      139540 :     case EXPR_STRUCTURE:
    1170                 :      139540 :       c = gfc_constructor_first (e->value.constructor);
    1171                 :      139540 :       if ((e->expr_type == EXPR_ARRAY) && c && c->iterator)
    1172                 :        5495 :         return gfc_constant_ac (e);
    1173                 :             : 
    1174                 :     1643218 :       for (; c; c = gfc_constructor_next (c))
    1175                 :     1520923 :         if (!gfc_is_constant_expr (c->expr))
    1176                 :             :           return false;
    1177                 :             : 
    1178                 :             :       return true;
    1179                 :             : 
    1180                 :             : 
    1181                 :           0 :     default:
    1182                 :           0 :       gfc_internal_error ("gfc_is_constant_expr(): Unknown expression type");
    1183                 :             :       return false;
    1184                 :             :     }
    1185                 :             : }
    1186                 :             : 
    1187                 :             : 
    1188                 :             : /* Is true if the expression or symbol is a passed CFI descriptor.  */
    1189                 :             : bool
    1190                 :      691243 : is_CFI_desc (gfc_symbol *sym, gfc_expr *e)
    1191                 :             : {
    1192                 :      691243 :   if (sym == NULL
    1193                 :      691243 :       && e && e->expr_type == EXPR_VARIABLE)
    1194                 :      175923 :     sym = e->symtree->n.sym;
    1195                 :             : 
    1196                 :      691243 :   if (sym && sym->attr.dummy
    1197                 :      294138 :       && sym->ns->proc_name->attr.is_bind_c
    1198                 :       77296 :       && (sym->attr.pointer
    1199                 :       72929 :           || sym->attr.allocatable
    1200                 :       69675 :           || (sym->attr.dimension
    1201                 :       42445 :               && (sym->as->type == AS_ASSUMED_SHAPE
    1202                 :       26176 :                   || sym->as->type == AS_ASSUMED_RANK))
    1203                 :       42544 :           || (sym->ts.type == BT_CHARACTER
    1204                 :       14661 :               && (!sym->ts.u.cl || !sym->ts.u.cl->length))))
    1205                 :       47199 :     return true;
    1206                 :             : 
    1207                 :             : return false;
    1208                 :             : }
    1209                 :             : 
    1210                 :             : 
    1211                 :             : /* Is true if an array reference is followed by a component or substring
    1212                 :             :    reference.  */
    1213                 :             : bool
    1214                 :      220546 : is_subref_array (gfc_expr * e)
    1215                 :             : {
    1216                 :      220546 :   gfc_ref * ref;
    1217                 :      220546 :   bool seen_array;
    1218                 :      220546 :   gfc_symbol *sym;
    1219                 :             : 
    1220                 :      220546 :   if (e->expr_type != EXPR_VARIABLE)
    1221                 :             :     return false;
    1222                 :             : 
    1223                 :      220233 :   sym = e->symtree->n.sym;
    1224                 :             : 
    1225                 :      220233 :   if (sym->attr.subref_array_pointer)
    1226                 :             :     return true;
    1227                 :             : 
    1228                 :      217189 :   seen_array = false;
    1229                 :             : 
    1230                 :      455358 :   for (ref = e->ref; ref; ref = ref->next)
    1231                 :             :     {
    1232                 :             :       /* If we haven't seen the array reference and this is an intrinsic,
    1233                 :             :          what follows cannot be a subreference array, unless there is a
    1234                 :             :          substring reference.  */
    1235                 :      240333 :       if (!seen_array && ref->type == REF_COMPONENT
    1236                 :       24462 :           && ref->next == NULL
    1237                 :        2353 :           && ref->u.c.component->ts.type != BT_CHARACTER
    1238                 :        2333 :           && ref->u.c.component->ts.type != BT_CLASS
    1239                 :        2115 :           && !gfc_bt_struct (ref->u.c.component->ts.type))
    1240                 :             :         return false;
    1241                 :             : 
    1242                 :      240224 :       if (ref->type == REF_ARRAY
    1243                 :      213640 :             && ref->u.ar.type != AR_ELEMENT)
    1244                 :             :         seen_array = true;
    1245                 :             : 
    1246                 :       29674 :       if (seen_array
    1247                 :      212605 :             && ref->type != REF_ARRAY)
    1248                 :             :         return seen_array;
    1249                 :             :     }
    1250                 :             : 
    1251                 :      215025 :   if (sym->ts.type == BT_CLASS
    1252                 :       16292 :       && sym->attr.dummy
    1253                 :        5054 :       && CLASS_DATA (sym)->attr.dimension
    1254                 :        3473 :       && CLASS_DATA (sym)->attr.class_pointer)
    1255                 :         544 :     return true;
    1256                 :             : 
    1257                 :             :   return false;
    1258                 :             : }
    1259                 :             : 
    1260                 :             : 
    1261                 :             : /* Try to collapse intrinsic expressions.  */
    1262                 :             : 
    1263                 :             : static bool
    1264                 :    17271335 : simplify_intrinsic_op (gfc_expr *p, int type)
    1265                 :             : {
    1266                 :    17271335 :   gfc_intrinsic_op op;
    1267                 :    17271335 :   gfc_expr *op1, *op2, *result;
    1268                 :             : 
    1269                 :    17271335 :   if (p->value.op.op == INTRINSIC_USER)
    1270                 :             :     return true;
    1271                 :             : 
    1272                 :    17271332 :   op1 = p->value.op.op1;
    1273                 :    17271332 :   op2 = p->value.op.op2;
    1274                 :    17271332 :   op  = p->value.op.op;
    1275                 :             : 
    1276                 :    17271332 :   if (!gfc_simplify_expr (op1, type))
    1277                 :             :     return false;
    1278                 :    17271094 :   if (!gfc_simplify_expr (op2, type))
    1279                 :             :     return false;
    1280                 :             : 
    1281                 :    17271046 :   if (!gfc_is_constant_expr (op1)
    1282                 :    17271046 :       || (op2 != NULL && !gfc_is_constant_expr (op2)))
    1283                 :      883531 :     return true;
    1284                 :             : 
    1285                 :             :   /* Rip p apart.  */
    1286                 :    16387515 :   p->value.op.op1 = NULL;
    1287                 :    16387515 :   p->value.op.op2 = NULL;
    1288                 :             : 
    1289                 :    16387515 :   switch (op)
    1290                 :             :     {
    1291                 :     5256627 :     case INTRINSIC_PARENTHESES:
    1292                 :     5256627 :       result = gfc_parentheses (op1);
    1293                 :     5256627 :       break;
    1294                 :             : 
    1295                 :          22 :     case INTRINSIC_UPLUS:
    1296                 :          22 :       result = gfc_uplus (op1);
    1297                 :          22 :       break;
    1298                 :             : 
    1299                 :       13061 :     case INTRINSIC_UMINUS:
    1300                 :       13061 :       result = gfc_uminus (op1);
    1301                 :       13061 :       break;
    1302                 :             : 
    1303                 :    10279923 :     case INTRINSIC_PLUS:
    1304                 :    10279923 :       result = gfc_add (op1, op2);
    1305                 :    10279923 :       break;
    1306                 :             : 
    1307                 :      502089 :     case INTRINSIC_MINUS:
    1308                 :      502089 :       result = gfc_subtract (op1, op2);
    1309                 :      502089 :       break;
    1310                 :             : 
    1311                 :      296324 :     case INTRINSIC_TIMES:
    1312                 :      296324 :       result = gfc_multiply (op1, op2);
    1313                 :      296324 :       break;
    1314                 :             : 
    1315                 :        5577 :     case INTRINSIC_DIVIDE:
    1316                 :        5577 :       result = gfc_divide (op1, op2);
    1317                 :        5577 :       break;
    1318                 :             : 
    1319                 :        5440 :     case INTRINSIC_POWER:
    1320                 :        5440 :       result = gfc_power (op1, op2);
    1321                 :        5440 :       break;
    1322                 :             : 
    1323                 :        2415 :     case INTRINSIC_CONCAT:
    1324                 :        2415 :       result = gfc_concat (op1, op2);
    1325                 :        2415 :       break;
    1326                 :             : 
    1327                 :        1189 :     case INTRINSIC_EQ:
    1328                 :        1189 :     case INTRINSIC_EQ_OS:
    1329                 :        1189 :       result = gfc_eq (op1, op2, op);
    1330                 :        1189 :       break;
    1331                 :             : 
    1332                 :       20406 :     case INTRINSIC_NE:
    1333                 :       20406 :     case INTRINSIC_NE_OS:
    1334                 :       20406 :       result = gfc_ne (op1, op2, op);
    1335                 :       20406 :       break;
    1336                 :             : 
    1337                 :         597 :     case INTRINSIC_GT:
    1338                 :         597 :     case INTRINSIC_GT_OS:
    1339                 :         597 :       result = gfc_gt (op1, op2, op);
    1340                 :         597 :       break;
    1341                 :             : 
    1342                 :          70 :     case INTRINSIC_GE:
    1343                 :          70 :     case INTRINSIC_GE_OS:
    1344                 :          70 :       result = gfc_ge (op1, op2, op);
    1345                 :          70 :       break;
    1346                 :             : 
    1347                 :          90 :     case INTRINSIC_LT:
    1348                 :          90 :     case INTRINSIC_LT_OS:
    1349                 :          90 :       result = gfc_lt (op1, op2, op);
    1350                 :          90 :       break;
    1351                 :             : 
    1352                 :         412 :     case INTRINSIC_LE:
    1353                 :         412 :     case INTRINSIC_LE_OS:
    1354                 :         412 :       result = gfc_le (op1, op2, op);
    1355                 :         412 :       break;
    1356                 :             : 
    1357                 :         481 :     case INTRINSIC_NOT:
    1358                 :         481 :       result = gfc_not (op1);
    1359                 :         481 :       break;
    1360                 :             : 
    1361                 :        1010 :     case INTRINSIC_AND:
    1362                 :        1010 :       result = gfc_and (op1, op2);
    1363                 :        1010 :       break;
    1364                 :             : 
    1365                 :         437 :     case INTRINSIC_OR:
    1366                 :         437 :       result = gfc_or (op1, op2);
    1367                 :         437 :       break;
    1368                 :             : 
    1369                 :          12 :     case INTRINSIC_EQV:
    1370                 :          12 :       result = gfc_eqv (op1, op2);
    1371                 :          12 :       break;
    1372                 :             : 
    1373                 :        1333 :     case INTRINSIC_NEQV:
    1374                 :        1333 :       result = gfc_neqv (op1, op2);
    1375                 :        1333 :       break;
    1376                 :             : 
    1377                 :           0 :     default:
    1378                 :           0 :       gfc_internal_error ("simplify_intrinsic_op(): Bad operator");
    1379                 :             :     }
    1380                 :             : 
    1381                 :    16387515 :   if (result == NULL)
    1382                 :             :     {
    1383                 :          43 :       gfc_free_expr (op1);
    1384                 :          43 :       gfc_free_expr (op2);
    1385                 :          43 :       return false;
    1386                 :             :     }
    1387                 :             : 
    1388                 :    16387472 :   result->rank = p->rank;
    1389                 :    16387472 :   result->corank = p->corank;
    1390                 :    16387472 :   result->where = p->where;
    1391                 :    16387472 :   gfc_replace_expr (p, result);
    1392                 :             : 
    1393                 :    16387472 :   return true;
    1394                 :             : }
    1395                 :             : 
    1396                 :             : /* Try to collapse conditional expressions.  */
    1397                 :             : 
    1398                 :             : static bool
    1399                 :           3 : simplify_conditional (gfc_expr *p, int type)
    1400                 :             : {
    1401                 :           3 :   gfc_expr *condition, *true_expr, *false_expr;
    1402                 :             : 
    1403                 :           3 :   condition = p->value.conditional.condition;
    1404                 :           3 :   true_expr = p->value.conditional.true_expr;
    1405                 :           3 :   false_expr = p->value.conditional.false_expr;
    1406                 :             : 
    1407                 :           3 :   if (!gfc_simplify_expr (condition, type)
    1408                 :           3 :       || !gfc_simplify_expr (true_expr, type)
    1409                 :           6 :       || !gfc_simplify_expr (false_expr, type))
    1410                 :           0 :     return false;
    1411                 :             : 
    1412                 :           3 :   if (!gfc_is_constant_expr (condition))
    1413                 :             :     return true;
    1414                 :             : 
    1415                 :           0 :   p->value.conditional.condition = NULL;
    1416                 :           0 :   p->value.conditional.true_expr = NULL;
    1417                 :           0 :   p->value.conditional.false_expr = NULL;
    1418                 :             : 
    1419                 :           0 :   if (condition->value.logical)
    1420                 :             :     {
    1421                 :           0 :       gfc_replace_expr (p, true_expr);
    1422                 :           0 :       gfc_free_expr (false_expr);
    1423                 :             :     }
    1424                 :             :   else
    1425                 :             :     {
    1426                 :           0 :       gfc_replace_expr (p, false_expr);
    1427                 :           0 :       gfc_free_expr (true_expr);
    1428                 :             :     }
    1429                 :           0 :   gfc_free_expr (condition);
    1430                 :             : 
    1431                 :           0 :   return true;
    1432                 :             : }
    1433                 :             : 
    1434                 :             : /* Subroutine to simplify constructor expressions.  Mutually recursive
    1435                 :             :    with gfc_simplify_expr().  */
    1436                 :             : 
    1437                 :             : static bool
    1438                 :      117079 : simplify_constructor (gfc_constructor_base base, int type)
    1439                 :             : {
    1440                 :      117079 :   gfc_constructor *c;
    1441                 :      117079 :   gfc_expr *p;
    1442                 :             : 
    1443                 :      789771 :   for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
    1444                 :             :     {
    1445                 :      672692 :       if (c->iterator
    1446                 :      672692 :           && (!gfc_simplify_expr(c->iterator->start, type)
    1447                 :         762 :               || !gfc_simplify_expr (c->iterator->end, type)
    1448                 :         762 :               || !gfc_simplify_expr (c->iterator->step, type)))
    1449                 :           0 :         return false;
    1450                 :             : 
    1451                 :      672692 :       if (c->expr && c->expr->expr_type != EXPR_CONSTANT)
    1452                 :             :         {
    1453                 :             :           /* Try and simplify a copy.  Replace the original if successful
    1454                 :             :              but keep going through the constructor at all costs.  Not
    1455                 :             :              doing so can make a dog's dinner of complicated things.  */
    1456                 :       35151 :           p = gfc_copy_expr (c->expr);
    1457                 :             : 
    1458                 :       35151 :           if (!gfc_simplify_expr (p, type))
    1459                 :             :             {
    1460                 :          10 :               gfc_free_expr (p);
    1461                 :          10 :               continue;
    1462                 :             :             }
    1463                 :             : 
    1464                 :       35141 :           gfc_replace_expr (c->expr, p);
    1465                 :             :         }
    1466                 :             :     }
    1467                 :             : 
    1468                 :             :   return true;
    1469                 :             : }
    1470                 :             : 
    1471                 :             : 
    1472                 :             : /* Pull a single array element out of an array constructor.  */
    1473                 :             : 
    1474                 :             : static bool
    1475                 :        3914 : find_array_element (gfc_constructor_base base, gfc_array_ref *ar,
    1476                 :             :                     gfc_constructor **rval)
    1477                 :             : {
    1478                 :        3914 :   unsigned long nelemen;
    1479                 :        3914 :   int i;
    1480                 :        3914 :   mpz_t delta;
    1481                 :        3914 :   mpz_t offset;
    1482                 :        3914 :   mpz_t span;
    1483                 :        3914 :   mpz_t tmp;
    1484                 :        3914 :   gfc_constructor *cons;
    1485                 :        3914 :   gfc_expr *e;
    1486                 :        3914 :   bool t;
    1487                 :             : 
    1488                 :        3914 :   t = true;
    1489                 :        3914 :   e = NULL;
    1490                 :             : 
    1491                 :        3914 :   mpz_init_set_ui (offset, 0);
    1492                 :        3914 :   mpz_init (delta);
    1493                 :        3914 :   mpz_init (tmp);
    1494                 :        3914 :   mpz_init_set_ui (span, 1);
    1495                 :       10237 :   for (i = 0; i < ar->dimen; i++)
    1496                 :             :     {
    1497                 :        3981 :       if (!gfc_reduce_init_expr (ar->as->lower[i])
    1498                 :        3976 :           || !gfc_reduce_init_expr (ar->as->upper[i])
    1499                 :        3976 :           || ar->as->upper[i]->expr_type != EXPR_CONSTANT
    1500                 :        7953 :           || ar->as->lower[i]->expr_type != EXPR_CONSTANT)
    1501                 :             :         {
    1502                 :           9 :           t = false;
    1503                 :           9 :           cons = NULL;
    1504                 :           9 :           goto depart;
    1505                 :             :         }
    1506                 :             : 
    1507                 :        3972 :       e = ar->start[i];
    1508                 :        3972 :       if (e->expr_type != EXPR_CONSTANT)
    1509                 :             :         {
    1510                 :        1554 :           cons = NULL;
    1511                 :        1554 :           goto depart;
    1512                 :             :         }
    1513                 :             : 
    1514                 :             :       /* Check the bounds.  */
    1515                 :        2418 :       if ((ar->as->upper[i]
    1516                 :        2418 :            && mpz_cmp (e->value.integer,
    1517                 :        2418 :                        ar->as->upper[i]->value.integer) > 0)
    1518                 :        2409 :           || (mpz_cmp (e->value.integer,
    1519                 :        2409 :                        ar->as->lower[i]->value.integer) < 0))
    1520                 :             :         {
    1521                 :           9 :           gfc_error ("Index in dimension %d is out of bounds "
    1522                 :             :                      "at %L", i + 1, &ar->c_where[i]);
    1523                 :           9 :           cons = NULL;
    1524                 :           9 :           t = false;
    1525                 :           9 :           goto depart;
    1526                 :             :         }
    1527                 :             : 
    1528                 :        2409 :       mpz_sub (delta, e->value.integer, ar->as->lower[i]->value.integer);
    1529                 :        2409 :       mpz_mul (delta, delta, span);
    1530                 :        2409 :       mpz_add (offset, offset, delta);
    1531                 :             : 
    1532                 :        2409 :       mpz_set_ui (tmp, 1);
    1533                 :        2409 :       mpz_add (tmp, tmp, ar->as->upper[i]->value.integer);
    1534                 :        2409 :       mpz_sub (tmp, tmp, ar->as->lower[i]->value.integer);
    1535                 :        2409 :       mpz_mul (span, span, tmp);
    1536                 :             :     }
    1537                 :             : 
    1538                 :        2342 :   for (cons = gfc_constructor_first (base), nelemen = mpz_get_ui (offset);
    1539                 :       10942 :        cons && nelemen > 0; cons = gfc_constructor_next (cons), nelemen--)
    1540                 :             :     {
    1541                 :        8600 :       if (cons->iterator)
    1542                 :             :         {
    1543                 :           0 :           cons = NULL;
    1544                 :           0 :           goto depart;
    1545                 :             :         }
    1546                 :             :     }
    1547                 :             : 
    1548                 :        2342 : depart:
    1549                 :        3914 :   mpz_clear (delta);
    1550                 :        3914 :   mpz_clear (offset);
    1551                 :        3914 :   mpz_clear (span);
    1552                 :        3914 :   mpz_clear (tmp);
    1553                 :        3914 :   *rval = cons;
    1554                 :        3914 :   return t;
    1555                 :             : }
    1556                 :             : 
    1557                 :             : 
    1558                 :             : /* Find a component of a structure constructor.  */
    1559                 :             : 
    1560                 :             : static gfc_constructor *
    1561                 :        1748 : find_component_ref (gfc_constructor_base base, gfc_ref *ref)
    1562                 :             : {
    1563                 :        1748 :   gfc_component *pick = ref->u.c.component;
    1564                 :        1748 :   gfc_constructor *c = gfc_constructor_first (base);
    1565                 :             : 
    1566                 :        1748 :   gfc_symbol *dt = ref->u.c.sym;
    1567                 :        1748 :   int ext = dt->attr.extension;
    1568                 :             : 
    1569                 :             :   /* For extended types, check if the desired component is in one of the
    1570                 :             :    * parent types.  */
    1571                 :        1838 :   while (ext > 0 && gfc_find_component (dt->components->ts.u.derived,
    1572                 :             :                                         pick->name, true, true, NULL))
    1573                 :             :     {
    1574                 :          90 :       dt = dt->components->ts.u.derived;
    1575                 :          90 :       c = gfc_constructor_first (c->expr->value.constructor);
    1576                 :          90 :       ext--;
    1577                 :             :     }
    1578                 :             : 
    1579                 :        1748 :   gfc_component *comp = dt->components;
    1580                 :        1886 :   while (comp != pick)
    1581                 :             :     {
    1582                 :         138 :       comp = comp->next;
    1583                 :         138 :       c = gfc_constructor_next (c);
    1584                 :             :     }
    1585                 :             : 
    1586                 :        1748 :   return c;
    1587                 :             : }
    1588                 :             : 
    1589                 :             : 
    1590                 :             : /* Replace an expression with the contents of a constructor, removing
    1591                 :             :    the subobject reference in the process.  */
    1592                 :             : 
    1593                 :             : static void
    1594                 :        4108 : remove_subobject_ref (gfc_expr *p, gfc_constructor *cons)
    1595                 :             : {
    1596                 :        4108 :   gfc_expr *e;
    1597                 :             : 
    1598                 :        4108 :   if (cons)
    1599                 :             :     {
    1600                 :        4090 :       e = cons->expr;
    1601                 :        4090 :       cons->expr = NULL;
    1602                 :             :     }
    1603                 :             :   else
    1604                 :          18 :     e = gfc_copy_expr (p);
    1605                 :        4108 :   e->ref = p->ref->next;
    1606                 :        4108 :   p->ref->next =  NULL;
    1607                 :        4108 :   gfc_replace_expr (p, e);
    1608                 :        4108 : }
    1609                 :             : 
    1610                 :             : 
    1611                 :             : /* Pull an array section out of an array constructor.  */
    1612                 :             : 
    1613                 :             : static bool
    1614                 :        1315 : find_array_section (gfc_expr *expr, gfc_ref *ref)
    1615                 :             : {
    1616                 :        1315 :   int idx;
    1617                 :        1315 :   int rank;
    1618                 :        1315 :   int d;
    1619                 :        1315 :   int shape_i;
    1620                 :        1315 :   int limit;
    1621                 :        1315 :   long unsigned one = 1;
    1622                 :        1315 :   bool incr_ctr;
    1623                 :        1315 :   mpz_t start[GFC_MAX_DIMENSIONS];
    1624                 :        1315 :   mpz_t end[GFC_MAX_DIMENSIONS];
    1625                 :        1315 :   mpz_t stride[GFC_MAX_DIMENSIONS];
    1626                 :        1315 :   mpz_t delta[GFC_MAX_DIMENSIONS];
    1627                 :        1315 :   mpz_t ctr[GFC_MAX_DIMENSIONS];
    1628                 :        1315 :   mpz_t delta_mpz;
    1629                 :        1315 :   mpz_t tmp_mpz;
    1630                 :        1315 :   mpz_t nelts;
    1631                 :        1315 :   mpz_t ptr;
    1632                 :        1315 :   gfc_constructor_base base;
    1633                 :        1315 :   gfc_constructor *cons, *vecsub[GFC_MAX_DIMENSIONS];
    1634                 :        1315 :   gfc_expr *begin;
    1635                 :        1315 :   gfc_expr *finish;
    1636                 :        1315 :   gfc_expr *step;
    1637                 :        1315 :   gfc_expr *upper;
    1638                 :        1315 :   gfc_expr *lower;
    1639                 :        1315 :   bool t;
    1640                 :             : 
    1641                 :        1315 :   t = true;
    1642                 :             : 
    1643                 :        1315 :   base = expr->value.constructor;
    1644                 :        1315 :   expr->value.constructor = NULL;
    1645                 :             : 
    1646                 :        1315 :   rank = ref->u.ar.as->rank;
    1647                 :             : 
    1648                 :        1315 :   if (expr->shape == NULL)
    1649                 :         243 :     expr->shape = gfc_get_shape (rank);
    1650                 :             : 
    1651                 :        1315 :   mpz_init_set_ui (delta_mpz, one);
    1652                 :        1315 :   mpz_init_set_ui (nelts, one);
    1653                 :        1315 :   mpz_init (tmp_mpz);
    1654                 :        1315 :   mpz_init (ptr);
    1655                 :             : 
    1656                 :             :   /* Do the initialization now, so that we can cleanup without
    1657                 :             :      keeping track of where we were.  */
    1658                 :        4472 :   for (d = 0; d < rank; d++)
    1659                 :             :     {
    1660                 :        1842 :       mpz_init (delta[d]);
    1661                 :        1842 :       mpz_init (start[d]);
    1662                 :        1842 :       mpz_init (end[d]);
    1663                 :        1842 :       mpz_init (ctr[d]);
    1664                 :        1842 :       mpz_init (stride[d]);
    1665                 :        1842 :       vecsub[d] = NULL;
    1666                 :             :     }
    1667                 :             : 
    1668                 :             :   /* Build the counters to clock through the array reference.  */
    1669                 :             :   shape_i = 0;
    1670                 :        2467 :   for (d = 0; d < rank; d++)
    1671                 :             :     {
    1672                 :             :       /* Make this stretch of code easier on the eye!  */
    1673                 :        1595 :       begin = ref->u.ar.start[d];
    1674                 :        1595 :       finish = ref->u.ar.end[d];
    1675                 :        1595 :       step = ref->u.ar.stride[d];
    1676                 :        1595 :       lower = ref->u.ar.as->lower[d];
    1677                 :        1595 :       upper = ref->u.ar.as->upper[d];
    1678                 :             : 
    1679                 :        1595 :       if (!lower || !upper
    1680                 :        1585 :           || lower->expr_type != EXPR_CONSTANT
    1681                 :        1585 :           || upper->expr_type != EXPR_CONSTANT
    1682                 :        1585 :           || lower->ts.type != BT_INTEGER
    1683                 :        1585 :           || upper->ts.type != BT_INTEGER)
    1684                 :             :         {
    1685                 :          11 :           t = false;
    1686                 :          11 :           goto cleanup;
    1687                 :             :         }
    1688                 :             : 
    1689                 :        1584 :       if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR)  /* Vector subscript.  */
    1690                 :             :         {
    1691                 :          70 :           gfc_constructor *ci;
    1692                 :          70 :           gcc_assert (begin);
    1693                 :             : 
    1694                 :          70 :           if (begin->expr_type != EXPR_ARRAY || !gfc_is_constant_expr (begin))
    1695                 :             :             {
    1696                 :           6 :               t = false;
    1697                 :           6 :               goto cleanup;
    1698                 :             :             }
    1699                 :             : 
    1700                 :          64 :           gcc_assert (begin->rank == 1);
    1701                 :             :           /* Zero-sized arrays have no shape and no elements, stop early.  */
    1702                 :          64 :           if (!begin->shape)
    1703                 :             :             {
    1704                 :           0 :               mpz_set_ui (nelts, 0);
    1705                 :           0 :               break;
    1706                 :             :             }
    1707                 :             : 
    1708                 :          64 :           vecsub[d] = gfc_constructor_first (begin->value.constructor);
    1709                 :          64 :           mpz_set (ctr[d], vecsub[d]->expr->value.integer);
    1710                 :          64 :           mpz_mul (nelts, nelts, begin->shape[0]);
    1711                 :          64 :           mpz_set (expr->shape[shape_i++], begin->shape[0]);
    1712                 :             : 
    1713                 :             :           /* Check bounds.  */
    1714                 :         296 :           for (ci = vecsub[d]; ci; ci = gfc_constructor_next (ci))
    1715                 :             :             {
    1716                 :         170 :               if (mpz_cmp (ci->expr->value.integer, upper->value.integer) > 0
    1717                 :         168 :                   || mpz_cmp (ci->expr->value.integer,
    1718                 :         168 :                               lower->value.integer) < 0)
    1719                 :             :                 {
    1720                 :           2 :                   gfc_error ("index in dimension %d is out of bounds "
    1721                 :             :                              "at %L", d + 1, &ref->u.ar.c_where[d]);
    1722                 :           2 :                   t = false;
    1723                 :           2 :                   goto cleanup;
    1724                 :             :                 }
    1725                 :             :             }
    1726                 :             :         }
    1727                 :             :       else
    1728                 :             :         {
    1729                 :        1514 :           if ((begin && begin->expr_type != EXPR_CONSTANT)
    1730                 :        1154 :               || (finish && finish->expr_type != EXPR_CONSTANT)
    1731                 :        1124 :               || (step && step->expr_type != EXPR_CONSTANT))
    1732                 :             :             {
    1733                 :         390 :               t = false;
    1734                 :         390 :               goto cleanup;
    1735                 :             :             }
    1736                 :             : 
    1737                 :             :           /* Obtain the stride.  */
    1738                 :        1124 :           if (step)
    1739                 :         118 :             mpz_set (stride[d], step->value.integer);
    1740                 :             :           else
    1741                 :        1006 :             mpz_set_ui (stride[d], one);
    1742                 :             : 
    1743                 :        1124 :           if (mpz_cmp_ui (stride[d], 0) == 0)
    1744                 :           0 :             mpz_set_ui (stride[d], one);
    1745                 :             : 
    1746                 :             :           /* Obtain the start value for the index.  */
    1747                 :        1124 :           if (begin)
    1748                 :         854 :             mpz_set (start[d], begin->value.integer);
    1749                 :             :           else
    1750                 :         270 :             mpz_set (start[d], lower->value.integer);
    1751                 :             : 
    1752                 :        1124 :           mpz_set (ctr[d], start[d]);
    1753                 :             : 
    1754                 :             :           /* Obtain the end value for the index.  */
    1755                 :        1124 :           if (finish)
    1756                 :         625 :             mpz_set (end[d], finish->value.integer);
    1757                 :             :           else
    1758                 :         499 :             mpz_set (end[d], upper->value.integer);
    1759                 :             : 
    1760                 :             :           /* Separate 'if' because elements sometimes arrive with
    1761                 :             :              non-null end.  */
    1762                 :        1124 :           if (ref->u.ar.dimen_type[d] == DIMEN_ELEMENT)
    1763                 :         248 :             mpz_set (end [d], begin->value.integer);
    1764                 :             : 
    1765                 :             :           /* Check the bounds.  */
    1766                 :        1124 :           if (mpz_cmp (ctr[d], upper->value.integer) > 0
    1767                 :        1105 :               || mpz_cmp (end[d], upper->value.integer) > 0
    1768                 :        1105 :               || mpz_cmp (ctr[d], lower->value.integer) < 0
    1769                 :        1090 :               || mpz_cmp (end[d], lower->value.integer) < 0)
    1770                 :             :             {
    1771                 :          34 :               gfc_error ("index in dimension %d is out of bounds "
    1772                 :             :                          "at %L", d + 1, &ref->u.ar.c_where[d]);
    1773                 :          34 :               t = false;
    1774                 :          34 :               goto cleanup;
    1775                 :             :             }
    1776                 :             : 
    1777                 :             :           /* Calculate the number of elements and the shape.  */
    1778                 :        1090 :           mpz_set (tmp_mpz, stride[d]);
    1779                 :        1090 :           mpz_add (tmp_mpz, end[d], tmp_mpz);
    1780                 :        1090 :           mpz_sub (tmp_mpz, tmp_mpz, ctr[d]);
    1781                 :        1090 :           mpz_div (tmp_mpz, tmp_mpz, stride[d]);
    1782                 :        1090 :           mpz_mul (nelts, nelts, tmp_mpz);
    1783                 :             : 
    1784                 :             :           /* An element reference reduces the rank of the expression; don't
    1785                 :             :              add anything to the shape array.  */
    1786                 :        1090 :           if (ref->u.ar.dimen_type[d] != DIMEN_ELEMENT)
    1787                 :         842 :             mpz_set (expr->shape[shape_i++], tmp_mpz);
    1788                 :             :         }
    1789                 :             : 
    1790                 :             :       /* Calculate the 'stride' (=delta) for conversion of the
    1791                 :             :          counter values into the index along the constructor.  */
    1792                 :        1152 :       mpz_set (delta[d], delta_mpz);
    1793                 :        1152 :       mpz_sub (tmp_mpz, upper->value.integer, lower->value.integer);
    1794                 :        1152 :       mpz_add_ui (tmp_mpz, tmp_mpz, one);
    1795                 :        1152 :       mpz_mul (delta_mpz, delta_mpz, tmp_mpz);
    1796                 :             :     }
    1797                 :             : 
    1798                 :         872 :   cons = gfc_constructor_first (base);
    1799                 :             : 
    1800                 :             :   /* Now clock through the array reference, calculating the index in
    1801                 :             :      the source constructor and transferring the elements to the new
    1802                 :             :      constructor.  */
    1803                 :       14220 :   for (idx = 0; idx < (int) mpz_get_si (nelts); idx++)
    1804                 :             :     {
    1805                 :       12477 :       mpz_set_ui (ptr, 0);
    1806                 :             : 
    1807                 :       12477 :       incr_ctr = true;
    1808                 :       38376 :       for (d = 0; d < rank; d++)
    1809                 :             :         {
    1810                 :       13422 :           mpz_set (tmp_mpz, ctr[d]);
    1811                 :       13422 :           mpz_sub (tmp_mpz, tmp_mpz, ref->u.ar.as->lower[d]->value.integer);
    1812                 :       13422 :           mpz_mul (tmp_mpz, tmp_mpz, delta[d]);
    1813                 :       13422 :           mpz_add (ptr, ptr, tmp_mpz);
    1814                 :             : 
    1815                 :       13422 :           if (!incr_ctr) continue;
    1816                 :             : 
    1817                 :       13057 :           if (ref->u.ar.dimen_type[d] == DIMEN_VECTOR) /* Vector subscript.  */
    1818                 :             :             {
    1819                 :         203 :               gcc_assert(vecsub[d]);
    1820                 :             : 
    1821                 :         203 :               if (!gfc_constructor_next (vecsub[d]))
    1822                 :          74 :                 vecsub[d] = gfc_constructor_first (ref->u.ar.start[d]->value.constructor);
    1823                 :             :               else
    1824                 :             :                 {
    1825                 :         129 :                   vecsub[d] = gfc_constructor_next (vecsub[d]);
    1826                 :         129 :                   incr_ctr = false;
    1827                 :             :                 }
    1828                 :         203 :               mpz_set (ctr[d], vecsub[d]->expr->value.integer);
    1829                 :             :             }
    1830                 :             :           else
    1831                 :             :             {
    1832                 :       12854 :               mpz_add (ctr[d], ctr[d], stride[d]);
    1833                 :             : 
    1834                 :       25708 :               if (mpz_cmp_ui (stride[d], 0) > 0
    1835                 :       12503 :                   ? mpz_cmp (ctr[d], end[d]) > 0
    1836                 :         351 :                   : mpz_cmp (ctr[d], end[d]) < 0)
    1837                 :        1377 :                 mpz_set (ctr[d], start[d]);
    1838                 :             :               else
    1839                 :             :                 incr_ctr = false;
    1840                 :             :             }
    1841                 :             :         }
    1842                 :             : 
    1843                 :       12477 :       limit = mpz_get_ui (ptr);
    1844                 :       12477 :       if (limit >= flag_max_array_constructor)
    1845                 :             :         {
    1846                 :           0 :           gfc_error ("The number of elements in the array constructor "
    1847                 :             :                      "at %L requires an increase of the allowed %d "
    1848                 :             :                      "upper limit.  See %<-fmax-array-constructor%> "
    1849                 :             :                      "option", &expr->where, flag_max_array_constructor);
    1850                 :           0 :           t = false;
    1851                 :           0 :           goto cleanup;
    1852                 :             :         }
    1853                 :             : 
    1854                 :       12477 :       cons = gfc_constructor_lookup (base, limit);
    1855                 :       12477 :       if (cons == NULL)
    1856                 :             :         {
    1857                 :           1 :           gfc_error ("Error in array constructor referenced at %L",
    1858                 :             :                      &ref->u.ar.where);
    1859                 :           1 :           t = false;
    1860                 :           1 :           goto cleanup;
    1861                 :             :         }
    1862                 :       12476 :       gfc_constructor_append_expr (&expr->value.constructor,
    1863                 :             :                                    gfc_copy_expr (cons->expr), NULL);
    1864                 :             :     }
    1865                 :             : 
    1866                 :         871 : cleanup:
    1867                 :             : 
    1868                 :        1315 :   mpz_clear (delta_mpz);
    1869                 :        1315 :   mpz_clear (tmp_mpz);
    1870                 :        1315 :   mpz_clear (nelts);
    1871                 :        4472 :   for (d = 0; d < rank; d++)
    1872                 :             :     {
    1873                 :        1842 :       mpz_clear (delta[d]);
    1874                 :        1842 :       mpz_clear (start[d]);
    1875                 :        1842 :       mpz_clear (end[d]);
    1876                 :        1842 :       mpz_clear (ctr[d]);
    1877                 :        1842 :       mpz_clear (stride[d]);
    1878                 :             :     }
    1879                 :        1315 :   mpz_clear (ptr);
    1880                 :        1315 :   gfc_constructor_free (base);
    1881                 :        1315 :   return t;
    1882                 :             : }
    1883                 :             : 
    1884                 :             : /* Pull a substring out of an expression.  */
    1885                 :             : 
    1886                 :             : static bool
    1887                 :        1258 : find_substring_ref (gfc_expr *p, gfc_expr **newp)
    1888                 :             : {
    1889                 :        1258 :   gfc_charlen_t end;
    1890                 :        1258 :   gfc_charlen_t start;
    1891                 :        1258 :   gfc_charlen_t length;
    1892                 :        1258 :   gfc_char_t *chr;
    1893                 :             : 
    1894                 :        1258 :   if (p->ref->u.ss.start->expr_type != EXPR_CONSTANT
    1895                 :        1258 :       || p->ref->u.ss.end->expr_type != EXPR_CONSTANT)
    1896                 :             :     return false;
    1897                 :             : 
    1898                 :        1258 :   *newp = gfc_copy_expr (p);
    1899                 :        1258 :   free ((*newp)->value.character.string);
    1900                 :             : 
    1901                 :        1258 :   end = (gfc_charlen_t) mpz_get_si (p->ref->u.ss.end->value.integer);
    1902                 :        1258 :   start = (gfc_charlen_t) mpz_get_si (p->ref->u.ss.start->value.integer);
    1903                 :        1258 :   if (end >= start)
    1904                 :        1237 :     length = end - start + 1;
    1905                 :             :   else
    1906                 :             :     length = 0;
    1907                 :             : 
    1908                 :        1258 :   chr = (*newp)->value.character.string = gfc_get_wide_string (length + 1);
    1909                 :        1258 :   (*newp)->value.character.length = length;
    1910                 :        1258 :   memcpy (chr, &p->value.character.string[start - 1],
    1911                 :        1258 :           length * sizeof (gfc_char_t));
    1912                 :        1258 :   chr[length] = '\0';
    1913                 :        1258 :   return true;
    1914                 :             : }
    1915                 :             : 
    1916                 :             : 
    1917                 :             : /* Simplify inquiry references (%re/%im) of constant complex arrays.
    1918                 :             :    Used by find_inquiry_ref.  */
    1919                 :             : 
    1920                 :             : static gfc_expr *
    1921                 :          60 : simplify_complex_array_inquiry_ref (gfc_expr *p, inquiry_type inquiry)
    1922                 :             : {
    1923                 :          60 :   gfc_expr *e, *r, *result;
    1924                 :          60 :   gfc_constructor_base base;
    1925                 :          60 :   gfc_constructor *c;
    1926                 :             : 
    1927                 :          60 :   if ((inquiry != INQUIRY_RE && inquiry != INQUIRY_IM)
    1928                 :          60 :       || p->expr_type != EXPR_ARRAY
    1929                 :          60 :       || p->ts.type != BT_COMPLEX
    1930                 :          60 :       || p->rank <= 0
    1931                 :          60 :       || p->value.constructor == NULL
    1932                 :         120 :       || !gfc_is_constant_array_expr (p))
    1933                 :           0 :     return NULL;
    1934                 :             : 
    1935                 :             :   /* Simplify array sections.  */
    1936                 :          60 :   gfc_simplify_expr (p, 0);
    1937                 :             : 
    1938                 :          60 :   result = gfc_get_array_expr (BT_REAL, p->ts.kind, &p->where);
    1939                 :          60 :   result->rank = p->rank;
    1940                 :          60 :   result->shape = gfc_copy_shape (p->shape, p->rank);
    1941                 :             : 
    1942                 :          60 :   base = p->value.constructor;
    1943                 :         312 :   for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
    1944                 :             :     {
    1945                 :         252 :       e = c->expr;
    1946                 :         252 :       if (e->expr_type != EXPR_CONSTANT)
    1947                 :           0 :         goto fail;
    1948                 :             : 
    1949                 :         252 :       r = gfc_get_constant_expr (BT_REAL, e->ts.kind, &e->where);
    1950                 :         252 :       if (inquiry == INQUIRY_RE)
    1951                 :         126 :         mpfr_set (r->value.real, mpc_realref (e->value.complex), GFC_RND_MODE);
    1952                 :             :       else
    1953                 :         126 :         mpfr_set (r->value.real, mpc_imagref (e->value.complex), GFC_RND_MODE);
    1954                 :             : 
    1955                 :         252 :       gfc_constructor_append_expr (&result->value.constructor, r, &e->where);
    1956                 :             :     }
    1957                 :             : 
    1958                 :             :   return result;
    1959                 :             : 
    1960                 :           0 : fail:
    1961                 :           0 :   gfc_free_expr (result);
    1962                 :           0 :   return NULL;
    1963                 :             : }
    1964                 :             : 
    1965                 :             : 
    1966                 :             : /* Pull an inquiry result out of an expression.  */
    1967                 :             : 
    1968                 :             : static bool
    1969                 :        1893 : find_inquiry_ref (gfc_expr *p, gfc_expr **newp)
    1970                 :             : {
    1971                 :        1893 :   gfc_ref *ref;
    1972                 :        1893 :   gfc_ref *inquiry = NULL;
    1973                 :        1893 :   gfc_ref *inquiry_head;
    1974                 :        1893 :   gfc_ref *ref_ss = NULL;
    1975                 :        1893 :   gfc_expr *tmp;
    1976                 :        1893 :   bool nofail = false;
    1977                 :             : 
    1978                 :        1893 :   tmp = gfc_copy_expr (p);
    1979                 :             : 
    1980                 :        1893 :   if (tmp->ref && tmp->ref->type == REF_INQUIRY)
    1981                 :             :     {
    1982                 :         560 :       inquiry = tmp->ref;
    1983                 :         560 :       tmp->ref = NULL;
    1984                 :             :     }
    1985                 :             :   else
    1986                 :             :     {
    1987                 :        1492 :       for (ref = tmp->ref; ref; ref = ref->next)
    1988                 :        1492 :         if (ref->next && ref->next->type == REF_INQUIRY)
    1989                 :             :           {
    1990                 :        1333 :             inquiry = ref->next;
    1991                 :        1333 :             ref->next = NULL;
    1992                 :        1333 :             if (ref->type == REF_SUBSTRING)
    1993                 :          14 :               ref_ss = ref;
    1994                 :             :             break;
    1995                 :             :           }
    1996                 :             :     }
    1997                 :             : 
    1998                 :        1893 :   if (!inquiry)
    1999                 :             :     {
    2000                 :           0 :       gfc_free_expr (tmp);
    2001                 :           0 :       return false;
    2002                 :             :     }
    2003                 :             : 
    2004                 :        1893 :   inquiry_head = inquiry;
    2005                 :        1893 :   gfc_resolve_expr (tmp);
    2006                 :             : 
    2007                 :             :   /* Leave these to the backend since the type and kind is not confirmed until
    2008                 :             :      resolution.  */
    2009                 :        1893 :   if (IS_INFERRED_TYPE (tmp))
    2010                 :         282 :     goto cleanup;
    2011                 :             : 
    2012                 :             :   /* In principle there can be more than one inquiry reference.  */
    2013                 :        2030 :   for (; inquiry; inquiry = inquiry->next)
    2014                 :             :     {
    2015                 :        1611 :       switch (inquiry->u.i)
    2016                 :             :         {
    2017                 :         194 :         case INQUIRY_LEN:
    2018                 :         194 :           if (tmp->ts.type != BT_CHARACTER)
    2019                 :          12 :             goto cleanup;
    2020                 :             : 
    2021                 :         182 :           if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
    2022                 :           0 :             goto cleanup;
    2023                 :             : 
    2024                 :             :           /* Inquire length of substring?  */
    2025                 :         182 :           if (ref_ss)
    2026                 :             :             {
    2027                 :           8 :               if (ref_ss->u.ss.start->expr_type == EXPR_CONSTANT
    2028                 :           8 :                   && ref_ss->u.ss.end->expr_type == EXPR_CONSTANT)
    2029                 :             :                 {
    2030                 :           8 :                   HOST_WIDE_INT istart, iend, length;
    2031                 :           8 :                   istart = gfc_mpz_get_hwi (ref_ss->u.ss.start->value.integer);
    2032                 :           8 :                   iend = gfc_mpz_get_hwi (ref_ss->u.ss.end->value.integer);
    2033                 :             : 
    2034                 :           8 :                   if (istart <= iend)
    2035                 :           8 :                     length = iend - istart + 1;
    2036                 :             :                   else
    2037                 :             :                     length = 0;
    2038                 :           8 :                   *newp = gfc_get_int_expr (gfc_default_integer_kind,
    2039                 :             :                                             NULL, length);
    2040                 :           8 :                   break;
    2041                 :             :                 }
    2042                 :             :               else
    2043                 :           0 :                 goto cleanup;
    2044                 :             :             }
    2045                 :             : 
    2046                 :         174 :           if (tmp->ts.u.cl->length
    2047                 :          99 :               && tmp->ts.u.cl->length->expr_type == EXPR_CONSTANT)
    2048                 :          63 :             *newp = gfc_copy_expr (tmp->ts.u.cl->length);
    2049                 :         111 :           else if (tmp->expr_type == EXPR_CONSTANT)
    2050                 :          12 :             *newp = gfc_get_int_expr (gfc_default_integer_kind,
    2051                 :          12 :                                       NULL, tmp->value.character.length);
    2052                 :          99 :           else if (gfc_init_expr_flag
    2053                 :           6 :                    && tmp->ts.u.cl->length->symtree->n.sym->attr.pdt_len)
    2054                 :           6 :             *newp = gfc_pdt_find_component_copy_initializer (tmp->symtree->n
    2055                 :             :                                                              .sym,
    2056                 :             :                                                              tmp->ts.u.cl
    2057                 :             :                                                              ->length->symtree
    2058                 :             :                                                              ->n.sym->name);
    2059                 :             :           else
    2060                 :          93 :             goto cleanup;
    2061                 :             : 
    2062                 :             :           break;
    2063                 :             : 
    2064                 :         186 :         case INQUIRY_KIND:
    2065                 :         186 :           if (tmp->ts.type == BT_DERIVED || tmp->ts.type == BT_CLASS)
    2066                 :           0 :             goto cleanup;
    2067                 :             : 
    2068                 :         186 :           if (!gfc_notify_std (GFC_STD_F2003, "KIND part_ref at %C"))
    2069                 :           0 :             goto cleanup;
    2070                 :             : 
    2071                 :         372 :           *newp = gfc_get_int_expr (gfc_default_integer_kind,
    2072                 :         186 :                                     NULL, tmp->ts.kind);
    2073                 :         186 :           break;
    2074                 :             : 
    2075                 :         678 :         case INQUIRY_RE:
    2076                 :         678 :           if (tmp->ts.type != BT_COMPLEX)
    2077                 :          77 :             goto cleanup;
    2078                 :             : 
    2079                 :         601 :           if (!gfc_notify_std (GFC_STD_F2008, "RE part_ref at %C"))
    2080                 :           0 :             goto cleanup;
    2081                 :             : 
    2082                 :         601 :           if (tmp->expr_type == EXPR_ARRAY)
    2083                 :             :             {
    2084                 :          30 :               *newp = simplify_complex_array_inquiry_ref (tmp, INQUIRY_RE);
    2085                 :          30 :               if (*newp != NULL)
    2086                 :             :                 {
    2087                 :             :                   nofail = true;
    2088                 :             :                   break;
    2089                 :             :                 }
    2090                 :             :             }
    2091                 :             : 
    2092                 :         571 :           if (tmp->expr_type != EXPR_CONSTANT)
    2093                 :         517 :             goto cleanup;
    2094                 :             : 
    2095                 :          54 :           *newp = gfc_get_constant_expr (BT_REAL, tmp->ts.kind, &tmp->where);
    2096                 :          54 :           mpfr_set ((*newp)->value.real,
    2097                 :             :                     mpc_realref (tmp->value.complex), GFC_RND_MODE);
    2098                 :          54 :           break;
    2099                 :             : 
    2100                 :         553 :         case INQUIRY_IM:
    2101                 :         553 :           if (tmp->ts.type != BT_COMPLEX)
    2102                 :          74 :             goto cleanup;
    2103                 :             : 
    2104                 :         479 :           if (!gfc_notify_std (GFC_STD_F2008, "IM part_ref at %C"))
    2105                 :           0 :             goto cleanup;
    2106                 :             : 
    2107                 :         479 :           if (tmp->expr_type == EXPR_ARRAY)
    2108                 :             :             {
    2109                 :          30 :               *newp = simplify_complex_array_inquiry_ref (tmp, INQUIRY_IM);
    2110                 :          30 :               if (*newp != NULL)
    2111                 :             :                 {
    2112                 :             :                   nofail = true;
    2113                 :             :                   break;
    2114                 :             :                 }
    2115                 :             :             }
    2116                 :             : 
    2117                 :         449 :           if (tmp->expr_type != EXPR_CONSTANT)
    2118                 :         419 :             goto cleanup;
    2119                 :             : 
    2120                 :          30 :           *newp = gfc_get_constant_expr (BT_REAL, tmp->ts.kind, &tmp->where);
    2121                 :          30 :           mpfr_set ((*newp)->value.real,
    2122                 :             :                     mpc_imagref (tmp->value.complex), GFC_RND_MODE);
    2123                 :          30 :           break;
    2124                 :             :         }
    2125                 :             : 
    2126                 :         419 :       if (inquiry->next)
    2127                 :           0 :         gfc_replace_expr (tmp, *newp);
    2128                 :             :     }
    2129                 :             : 
    2130                 :         419 :   if (!(*newp))
    2131                 :           0 :     goto cleanup;
    2132                 :         419 :   else if ((*newp)->expr_type != EXPR_CONSTANT && !nofail)
    2133                 :             :     {
    2134                 :           0 :       gfc_free_expr (*newp);
    2135                 :           0 :       goto cleanup;
    2136                 :             :     }
    2137                 :             : 
    2138                 :         419 :   gfc_free_expr (tmp);
    2139                 :         419 :   gfc_free_ref_list (inquiry_head);
    2140                 :         419 :   return true;
    2141                 :             : 
    2142                 :        1474 : cleanup:
    2143                 :        1474 :   gfc_free_expr (tmp);
    2144                 :        1474 :   gfc_free_ref_list (inquiry_head);
    2145                 :        1474 :   return false;
    2146                 :             : }
    2147                 :             : 
    2148                 :             : 
    2149                 :             : 
    2150                 :             : /* Simplify a subobject reference of a constructor.  This occurs when
    2151                 :             :    parameter variable values are substituted.  */
    2152                 :             : 
    2153                 :             : static bool
    2154                 :      119675 : simplify_const_ref (gfc_expr *p)
    2155                 :             : {
    2156                 :      119675 :   gfc_constructor *cons, *c;
    2157                 :      119675 :   gfc_expr *newp = NULL;
    2158                 :      119675 :   gfc_ref *last_ref;
    2159                 :             : 
    2160                 :      254020 :   while (p->ref)
    2161                 :             :     {
    2162                 :       16686 :       switch (p->ref->type)
    2163                 :             :         {
    2164                 :       13680 :         case REF_ARRAY:
    2165                 :       13680 :           switch (p->ref->u.ar.type)
    2166                 :             :             {
    2167                 :        3932 :             case AR_ELEMENT:
    2168                 :             :               /* <type/kind spec>, parameter :: x(<int>) = scalar_expr
    2169                 :             :                  will generate this.  */
    2170                 :        3932 :               if (p->expr_type != EXPR_ARRAY)
    2171                 :             :                 {
    2172                 :          18 :                   remove_subobject_ref (p, NULL);
    2173                 :          18 :                   break;
    2174                 :             :                 }
    2175                 :        3914 :               if (!find_array_element (p->value.constructor, &p->ref->u.ar, &cons))
    2176                 :             :                 return false;
    2177                 :             : 
    2178                 :        3896 :               if (!cons)
    2179                 :             :                 return true;
    2180                 :             : 
    2181                 :        2342 :               remove_subobject_ref (p, cons);
    2182                 :        2342 :               break;
    2183                 :             : 
    2184                 :        1315 :             case AR_SECTION:
    2185                 :        1315 :               if (!find_array_section (p, p->ref))
    2186                 :             :                 return false;
    2187                 :         871 :               p->ref->u.ar.type = AR_FULL;
    2188                 :             : 
    2189                 :             :             /* Fall through.  */
    2190                 :             : 
    2191                 :        9304 :             case AR_FULL:
    2192                 :        9304 :               if (p->ref->next != NULL
    2193                 :         330 :                   && (p->ts.type == BT_CHARACTER || gfc_bt_struct (p->ts.type)))
    2194                 :             :                 {
    2195                 :         330 :                   for (c = gfc_constructor_first (p->value.constructor);
    2196                 :        2926 :                        c; c = gfc_constructor_next (c))
    2197                 :             :                     {
    2198                 :        2596 :                       c->expr->ref = gfc_copy_ref (p->ref->next);
    2199                 :        2596 :                       if (!simplify_const_ref (c->expr))
    2200                 :             :                         return false;
    2201                 :             :                     }
    2202                 :             : 
    2203                 :          75 :                   if (gfc_bt_struct (p->ts.type)
    2204                 :         255 :                         && p->ref->next
    2205                 :         585 :                         && (c = gfc_constructor_first (p->value.constructor)))
    2206                 :             :                     {
    2207                 :             :                       /* There may have been component references.  */
    2208                 :         255 :                       p->ts = c->expr->ts;
    2209                 :             :                     }
    2210                 :             : 
    2211                 :         330 :                   last_ref = p->ref;
    2212                 :         678 :                   for (; last_ref->next; last_ref = last_ref->next) {};
    2213                 :             : 
    2214                 :         330 :                   if (p->ts.type == BT_CHARACTER
    2215                 :          97 :                         && last_ref->type == REF_SUBSTRING)
    2216                 :             :                     {
    2217                 :             :                       /* If this is a CHARACTER array and we possibly took
    2218                 :             :                          a substring out of it, update the type-spec's
    2219                 :             :                          character length according to the first element
    2220                 :             :                          (as all should have the same length).  */
    2221                 :          75 :                       gfc_charlen_t string_len;
    2222                 :          75 :                       if ((c = gfc_constructor_first (p->value.constructor)))
    2223                 :             :                         {
    2224                 :          75 :                           const gfc_expr* first = c->expr;
    2225                 :          75 :                           gcc_assert (first->expr_type == EXPR_CONSTANT);
    2226                 :          75 :                           gcc_assert (first->ts.type == BT_CHARACTER);
    2227                 :          75 :                           string_len = first->value.character.length;
    2228                 :             :                         }
    2229                 :             :                       else
    2230                 :             :                         string_len = 0;
    2231                 :             : 
    2232                 :          75 :                       if (!p->ts.u.cl)
    2233                 :             :                         {
    2234                 :           0 :                           if (p->symtree)
    2235                 :           0 :                             p->ts.u.cl = gfc_new_charlen (p->symtree->n.sym->ns,
    2236                 :             :                                                           NULL);
    2237                 :             :                           else
    2238                 :           0 :                             p->ts.u.cl = gfc_new_charlen (gfc_current_ns,
    2239                 :             :                                                           NULL);
    2240                 :             :                         }
    2241                 :             :                       else
    2242                 :          75 :                         gfc_free_expr (p->ts.u.cl->length);
    2243                 :             : 
    2244                 :          75 :                       p->ts.u.cl->length
    2245                 :          75 :                         = gfc_get_int_expr (gfc_charlen_int_kind,
    2246                 :             :                                             NULL, string_len);
    2247                 :             :                     }
    2248                 :             :                 }
    2249                 :        9304 :               gfc_free_ref_list (p->ref);
    2250                 :        9304 :               p->ref = NULL;
    2251                 :        9304 :               break;
    2252                 :             : 
    2253                 :             :             default:
    2254                 :             :               return true;
    2255                 :             :             }
    2256                 :             : 
    2257                 :             :           break;
    2258                 :             : 
    2259                 :        1748 :         case REF_COMPONENT:
    2260                 :        1748 :           cons = find_component_ref (p->value.constructor, p->ref);
    2261                 :        1748 :           remove_subobject_ref (p, cons);
    2262                 :        1748 :           break;
    2263                 :             : 
    2264                 :           0 :         case REF_INQUIRY:
    2265                 :           0 :           if (!find_inquiry_ref (p, &newp))
    2266                 :             :             return false;
    2267                 :             : 
    2268                 :           0 :           gfc_replace_expr (p, newp);
    2269                 :           0 :           gfc_free_ref_list (p->ref);
    2270                 :           0 :           p->ref = NULL;
    2271                 :           0 :           break;
    2272                 :             : 
    2273                 :        1258 :         case REF_SUBSTRING:
    2274                 :        1258 :           if (!find_substring_ref (p, &newp))
    2275                 :             :             return false;
    2276                 :             : 
    2277                 :        1258 :           gfc_replace_expr (p, newp);
    2278                 :        1258 :           gfc_free_ref_list (p->ref);
    2279                 :        1258 :           p->ref = NULL;
    2280                 :        1258 :           break;
    2281                 :             :         }
    2282                 :             :     }
    2283                 :             : 
    2284                 :             :   return true;
    2285                 :             : }
    2286                 :             : 
    2287                 :             : 
    2288                 :             : /* Simplify a chain of references.  */
    2289                 :             : 
    2290                 :             : static bool
    2291                 :    15070553 : simplify_ref_chain (gfc_ref *ref, int type, gfc_expr **p)
    2292                 :             : {
    2293                 :    15070553 :   int n;
    2294                 :    15070553 :   gfc_expr *newp = NULL;
    2295                 :             : 
    2296                 :    15392507 :   for (; ref; ref = ref->next)
    2297                 :             :     {
    2298                 :      323848 :       switch (ref->type)
    2299                 :             :         {
    2300                 :             :         case REF_ARRAY:
    2301                 :      570462 :           for (n = 0; n < ref->u.ar.dimen; n++)
    2302                 :             :             {
    2303                 :      318581 :               if (!gfc_simplify_expr (ref->u.ar.start[n], type))
    2304                 :             :                 return false;
    2305                 :      318581 :               if (!gfc_simplify_expr (ref->u.ar.end[n], type))
    2306                 :             :                 return false;
    2307                 :      318581 :               if (!gfc_simplify_expr (ref->u.ar.stride[n], type))
    2308                 :             :                 return false;
    2309                 :             :             }
    2310                 :             :           break;
    2311                 :             : 
    2312                 :        9479 :         case REF_SUBSTRING:
    2313                 :        9479 :           if (!gfc_simplify_expr (ref->u.ss.start, type))
    2314                 :             :             return false;
    2315                 :        9479 :           if (!gfc_simplify_expr (ref->u.ss.end, type))
    2316                 :             :             return false;
    2317                 :             :           break;
    2318                 :             : 
    2319                 :        1893 :         case REF_INQUIRY:
    2320                 :        1893 :           if (!find_inquiry_ref (*p, &newp))
    2321                 :             :             return false;
    2322                 :             : 
    2323                 :         419 :           gfc_replace_expr (*p, newp);
    2324                 :         419 :           gfc_free_ref_list ((*p)->ref);
    2325                 :         419 :           (*p)->ref = NULL;
    2326                 :         419 :           return true;
    2327                 :             : 
    2328                 :             :         default:
    2329                 :             :           break;
    2330                 :             :         }
    2331                 :             :     }
    2332                 :             :   return true;
    2333                 :             : }
    2334                 :             : 
    2335                 :             : 
    2336                 :             : /* Try to substitute the value of a parameter variable.  */
    2337                 :             : 
    2338                 :             : static bool
    2339                 :       14410 : simplify_parameter_variable (gfc_expr *p, int type)
    2340                 :             : {
    2341                 :       14410 :   gfc_expr *e;
    2342                 :       14410 :   bool t;
    2343                 :             : 
    2344                 :             :   /* Set rank and check array ref; as resolve_variable calls
    2345                 :             :      gfc_simplify_expr, call gfc_resolve_ref + gfc_expression_rank instead.  */
    2346                 :       14410 :   if (!gfc_resolve_ref (p))
    2347                 :             :     {
    2348                 :           1 :       gfc_error_check ();
    2349                 :           1 :       return false;
    2350                 :             :     }
    2351                 :       14409 :   gfc_expression_rank (p);
    2352                 :             : 
    2353                 :             :   /* Is this an inquiry?  */
    2354                 :       14409 :   bool inquiry = false;
    2355                 :       14409 :   gfc_ref* ref = p->ref;
    2356                 :       29613 :   while (ref)
    2357                 :             :     {
    2358                 :       15332 :       if (ref->type == REF_INQUIRY)
    2359                 :             :         break;
    2360                 :       15204 :       ref = ref->next;
    2361                 :             :     }
    2362                 :       14409 :   if (ref && ref->type == REF_INQUIRY)
    2363                 :         128 :     inquiry = ref->u.i == INQUIRY_LEN || ref->u.i == INQUIRY_KIND;
    2364                 :             : 
    2365                 :       14409 :   if (gfc_is_size_zero_array (p))
    2366                 :             :     {
    2367                 :         690 :       if (p->expr_type == EXPR_ARRAY)
    2368                 :             :         return true;
    2369                 :             : 
    2370                 :         690 :       e = gfc_get_expr ();
    2371                 :         690 :       e->expr_type = EXPR_ARRAY;
    2372                 :         690 :       e->ts = p->ts;
    2373                 :         690 :       e->rank = p->rank;
    2374                 :         690 :       e->corank = p->corank;
    2375                 :         690 :       e->value.constructor = NULL;
    2376                 :         690 :       e->shape = gfc_copy_shape (p->shape, p->rank);
    2377                 :         690 :       e->where = p->where;
    2378                 :             :       /* If %kind and %len are not used then we're done, otherwise
    2379                 :             :          drop through for simplification.  */
    2380                 :         690 :       if (!inquiry)
    2381                 :             :         {
    2382                 :         620 :           gfc_replace_expr (p, e);
    2383                 :         620 :           return true;
    2384                 :             :         }
    2385                 :             :     }
    2386                 :             :   else
    2387                 :             :     {
    2388                 :       13719 :       e = gfc_copy_expr (p->symtree->n.sym->value);
    2389                 :       13719 :       if (e == NULL)
    2390                 :             :         return false;
    2391                 :             : 
    2392                 :       13653 :       gfc_free_shape (&e->shape, e->rank);
    2393                 :       13653 :       e->shape = gfc_copy_shape (p->shape, p->rank);
    2394                 :       13653 :       e->rank = p->rank;
    2395                 :       13653 :       e->corank = p->corank;
    2396                 :             : 
    2397                 :       13653 :       if (e->ts.type == BT_CHARACTER && p->ts.u.cl)
    2398                 :        2675 :         e->ts = p->ts;
    2399                 :             :     }
    2400                 :             : 
    2401                 :       13723 :   if (e->ts.type == BT_CHARACTER && e->ts.u.cl == NULL)
    2402                 :           0 :     e->ts.u.cl = gfc_new_charlen (gfc_current_ns, p->ts.u.cl);
    2403                 :             : 
    2404                 :             :   /* Do not copy subobject refs for constant.  */
    2405                 :       13723 :   if (e->expr_type != EXPR_CONSTANT && p->ref != NULL)
    2406                 :       13718 :     e->ref = gfc_copy_ref (p->ref);
    2407                 :       13723 :   t = gfc_simplify_expr (e, type);
    2408                 :       13723 :   e->where = p->where;
    2409                 :             : 
    2410                 :             :   /* Only use the simplification if it eliminated all subobject references.  */
    2411                 :       13723 :   if (t && !e->ref)
    2412                 :       11706 :     gfc_replace_expr (p, e);
    2413                 :             :   else
    2414                 :        2017 :     gfc_free_expr (e);
    2415                 :             : 
    2416                 :             :   return t;
    2417                 :             : }
    2418                 :             : 
    2419                 :             : 
    2420                 :             : static bool
    2421                 :             : scalarize_intrinsic_call (gfc_expr *, bool init_flag);
    2422                 :             : 
    2423                 :             : /* Given an expression, simplify it by collapsing constant
    2424                 :             :    expressions.  Most simplification takes place when the expression
    2425                 :             :    tree is being constructed.  If an intrinsic function is simplified
    2426                 :             :    at some point, we get called again to collapse the result against
    2427                 :             :    other constants.
    2428                 :             : 
    2429                 :             :    We work by recursively simplifying expression nodes, simplifying
    2430                 :             :    intrinsic functions where possible, which can lead to further
    2431                 :             :    constant collapsing.  If an operator has constant operand(s), we
    2432                 :             :    rip the expression apart, and rebuild it, hoping that it becomes
    2433                 :             :    something simpler.
    2434                 :             : 
    2435                 :             :    The expression type is defined for:
    2436                 :             :      0   Basic expression parsing
    2437                 :             :      1   Simplifying array constructors -- will substitute
    2438                 :             :          iterator values.
    2439                 :             :    Returns false on error, true otherwise.
    2440                 :             :    NOTE: Will return true even if the expression cannot be simplified.  */
    2441                 :             : 
    2442                 :             : bool
    2443                 :    56110559 : gfc_simplify_expr (gfc_expr *p, int type)
    2444                 :             : {
    2445                 :    56110559 :   gfc_actual_arglist *ap;
    2446                 :    56110559 :   gfc_intrinsic_sym* isym = NULL;
    2447                 :             : 
    2448                 :             : 
    2449                 :    56110559 :   if (p == NULL)
    2450                 :             :     return true;
    2451                 :             : 
    2452                 :    49802524 :   switch (p->expr_type)
    2453                 :             :     {
    2454                 :    16876778 :     case EXPR_CONSTANT:
    2455                 :    16876778 :       if (p->ref && p->ref->type == REF_INQUIRY)
    2456                 :          40 :         simplify_ref_chain (p->ref, type, &p);
    2457                 :             :       break;
    2458                 :             :     case EXPR_NULL:
    2459                 :             :       break;
    2460                 :             : 
    2461                 :      567569 :     case EXPR_FUNCTION:
    2462                 :             :       // For array-bound functions, we don't need to optimize
    2463                 :             :       // the 'array' argument. In particular, if the argument
    2464                 :             :       // is a PARAMETER, simplifying might convert an EXPR_VARIABLE
    2465                 :             :       // into an EXPR_ARRAY; the latter has lbound = 1, the former
    2466                 :             :       // can have any lbound.
    2467                 :      567569 :       ap = p->value.function.actual;
    2468                 :      567569 :       if (p->value.function.isym &&
    2469                 :      532498 :           (p->value.function.isym->id == GFC_ISYM_LBOUND
    2470                 :      519478 :            || p->value.function.isym->id == GFC_ISYM_UBOUND
    2471                 :      511672 :            || p->value.function.isym->id == GFC_ISYM_LCOBOUND
    2472                 :      511470 :            || p->value.function.isym->id == GFC_ISYM_UCOBOUND
    2473                 :      511277 :            || p->value.function.isym->id == GFC_ISYM_SHAPE))
    2474                 :       25901 :         ap = ap->next;
    2475                 :             : 
    2476                 :     1647329 :       for ( ; ap; ap = ap->next)
    2477                 :     1079911 :         if (!gfc_simplify_expr (ap->expr, type))
    2478                 :             :           return false;
    2479                 :             : 
    2480                 :      567418 :       if (p->value.function.isym != NULL
    2481                 :      567418 :           && gfc_intrinsic_func_interface (p, 1) == MATCH_ERROR)
    2482                 :             :         return false;
    2483                 :             : 
    2484                 :      567359 :       if (p->symtree && (p->value.function.isym || p->ts.type == BT_UNKNOWN))
    2485                 :             :         {
    2486                 :      214713 :           isym = gfc_find_function (p->symtree->n.sym->name);
    2487                 :      214713 :           if (isym && isym->elemental)
    2488                 :      108079 :             scalarize_intrinsic_call (p, false);
    2489                 :             :         }
    2490                 :             : 
    2491                 :             :       break;
    2492                 :             : 
    2493                 :        1378 :     case EXPR_SUBSTRING:
    2494                 :        1378 :       if (!simplify_ref_chain (p->ref, type, &p))
    2495                 :             :         return false;
    2496                 :             : 
    2497                 :        1378 :       if (gfc_is_constant_expr (p))
    2498                 :             :         {
    2499                 :         768 :           gfc_char_t *s;
    2500                 :         768 :           HOST_WIDE_INT start, end;
    2501                 :             : 
    2502                 :         768 :           start = 0;
    2503                 :         768 :           if (p->ref && p->ref->u.ss.start)
    2504                 :             :             {
    2505                 :         743 :               gfc_extract_hwi (p->ref->u.ss.start, &start);
    2506                 :         743 :               start--;  /* Convert from one-based to zero-based.  */
    2507                 :             :             }
    2508                 :             : 
    2509                 :         768 :           end = p->value.character.length;
    2510                 :         768 :           if (p->ref && p->ref->u.ss.end)
    2511                 :         743 :             gfc_extract_hwi (p->ref->u.ss.end, &end);
    2512                 :             : 
    2513                 :         768 :           if (end < start)
    2514                 :           7 :             end = start;
    2515                 :             : 
    2516                 :         768 :           s = gfc_get_wide_string (end - start + 2);
    2517                 :         768 :           memcpy (s, p->value.character.string + start,
    2518                 :         768 :                   (end - start) * sizeof (gfc_char_t));
    2519                 :         768 :           s[end - start + 1] = '\0';  /* TODO: C-style string.  */
    2520                 :         768 :           free (p->value.character.string);
    2521                 :         768 :           p->value.character.string = s;
    2522                 :         768 :           p->value.character.length = end - start;
    2523                 :         768 :           p->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    2524                 :        1536 :           p->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
    2525                 :             :                                                  NULL,
    2526                 :         768 :                                                  p->value.character.length);
    2527                 :         768 :           gfc_free_ref_list (p->ref);
    2528                 :         768 :           p->ref = NULL;
    2529                 :         768 :           p->expr_type = EXPR_CONSTANT;
    2530                 :             :         }
    2531                 :             :       break;
    2532                 :             : 
    2533                 :    17271335 :     case EXPR_OP:
    2534                 :    17271335 :       if (!simplify_intrinsic_op (p, type))
    2535                 :             :         return false;
    2536                 :             :       break;
    2537                 :             : 
    2538                 :           3 :     case EXPR_CONDITIONAL:
    2539                 :           3 :       if (!simplify_conditional (p, type))
    2540                 :             :         return false;
    2541                 :             :       break;
    2542                 :             : 
    2543                 :    14965426 :     case EXPR_VARIABLE:
    2544                 :             :       /* Only substitute array parameter variables if we are in an
    2545                 :             :          initialization expression, or we want a subsection.  */
    2546                 :    14965426 :       if (p->symtree->n.sym->attr.flavor == FL_PARAMETER
    2547                 :       14055 :           && (gfc_init_expr_flag || p->ref
    2548                 :           1 :               || (p->symtree->n.sym->value
    2549                 :           0 :                   && p->symtree->n.sym->value->expr_type != EXPR_ARRAY)))
    2550                 :             :         {
    2551                 :       14054 :           if (!simplify_parameter_variable (p, type))
    2552                 :             :             return false;
    2553                 :       13535 :           if (!iter_stack)
    2554                 :             :             break;
    2555                 :             :         }
    2556                 :             : 
    2557                 :    14951872 :       if (type == 1)
    2558                 :             :         {
    2559                 :    13967182 :           gfc_simplify_iterator_var (p);
    2560                 :             :         }
    2561                 :             : 
    2562                 :             :       /* Simplify subcomponent references.  */
    2563                 :    14951872 :       if (!simplify_ref_chain (p->ref, type, &p))
    2564                 :             :         return false;
    2565                 :             : 
    2566                 :             :       break;
    2567                 :             : 
    2568                 :      117263 :     case EXPR_STRUCTURE:
    2569                 :      117263 :     case EXPR_ARRAY:
    2570                 :      117263 :       if (!simplify_ref_chain (p->ref, type, &p))
    2571                 :             :         return false;
    2572                 :             : 
    2573                 :             :       /* If the following conditions hold, we found something like kind type
    2574                 :             :          inquiry of the form a(2)%kind while simplify the ref chain.  */
    2575                 :      117262 :       if (p->expr_type == EXPR_CONSTANT && !p->ref && !p->rank && !p->shape)
    2576                 :             :         return true;
    2577                 :             : 
    2578                 :      117079 :       if (!simplify_constructor (p->value.constructor, type))
    2579                 :             :         return false;
    2580                 :             : 
    2581                 :      117079 :       if (p->expr_type == EXPR_ARRAY && p->ref && p->ref->type == REF_ARRAY
    2582                 :       13621 :           && p->ref->u.ar.type == AR_FULL)
    2583                 :        8417 :           gfc_expand_constructor (p, false);
    2584                 :             : 
    2585                 :      117079 :       if (!simplify_const_ref (p))
    2586                 :             :         return false;
    2587                 :             : 
    2588                 :             :       break;
    2589                 :             : 
    2590                 :             :     case EXPR_COMPCALL:
    2591                 :             :     case EXPR_PPC:
    2592                 :             :       break;
    2593                 :             : 
    2594                 :           0 :     case EXPR_UNKNOWN:
    2595                 :           0 :       gcc_unreachable ();
    2596                 :             :     }
    2597                 :             : 
    2598                 :             :   return true;
    2599                 :             : }
    2600                 :             : 
    2601                 :             : 
    2602                 :             : /* Try simplification of an expression via gfc_simplify_expr.
    2603                 :             :    When an error occurs (arithmetic or otherwise), roll back.  */
    2604                 :             : 
    2605                 :             : bool
    2606                 :           0 : gfc_try_simplify_expr (gfc_expr *e, int type)
    2607                 :             : {
    2608                 :           0 :   gfc_expr *n;
    2609                 :           0 :   bool t, saved_div0;
    2610                 :             : 
    2611                 :           0 :   if (e == NULL || e->expr_type == EXPR_CONSTANT)
    2612                 :             :     return true;
    2613                 :             : 
    2614                 :           0 :   saved_div0 = gfc_seen_div0;
    2615                 :           0 :   gfc_seen_div0 = false;
    2616                 :           0 :   n = gfc_copy_expr (e);
    2617                 :           0 :   t = gfc_simplify_expr (n, type) && !gfc_seen_div0;
    2618                 :           0 :   if (t)
    2619                 :           0 :     gfc_replace_expr (e, n);
    2620                 :             :   else
    2621                 :           0 :     gfc_free_expr (n);
    2622                 :           0 :   gfc_seen_div0 = saved_div0;
    2623                 :           0 :   return t;
    2624                 :             : }
    2625                 :             : 
    2626                 :             : 
    2627                 :             : /* Returns the type of an expression with the exception that iterator
    2628                 :             :    variables are automatically integers no matter what else they may
    2629                 :             :    be declared as.  */
    2630                 :             : 
    2631                 :             : static bt
    2632                 :        4658 : et0 (gfc_expr *e)
    2633                 :             : {
    2634                 :        4658 :   if (e->expr_type == EXPR_VARIABLE && gfc_check_iter_variable (e))
    2635                 :             :     return BT_INTEGER;
    2636                 :             : 
    2637                 :        4658 :   return e->ts.type;
    2638                 :             : }
    2639                 :             : 
    2640                 :             : 
    2641                 :             : /* Scalarize an expression for an elemental intrinsic call.  */
    2642                 :             : 
    2643                 :             : static bool
    2644                 :      108319 : scalarize_intrinsic_call (gfc_expr *e, bool init_flag)
    2645                 :             : {
    2646                 :      108319 :   gfc_actual_arglist *a, *b;
    2647                 :      108319 :   gfc_constructor_base ctor;
    2648                 :      108319 :   gfc_constructor *args[5] = {};  /* Avoid uninitialized warnings.  */
    2649                 :      108319 :   gfc_constructor *ci, *new_ctor;
    2650                 :      108319 :   gfc_expr *expr, *old, *p;
    2651                 :      108319 :   int n, i, rank[5], array_arg;
    2652                 :             : 
    2653                 :      108319 :   if (e == NULL)
    2654                 :             :     return false;
    2655                 :             : 
    2656                 :      108319 :   a = e->value.function.actual;
    2657                 :      116036 :   for (; a; a = a->next)
    2658                 :      115310 :     if (a->expr && !gfc_is_constant_expr (a->expr))
    2659                 :             :       return false;
    2660                 :             : 
    2661                 :             :   /* Find which, if any, arguments are arrays.  Assume that the old
    2662                 :             :      expression carries the type information and that the first arg
    2663                 :             :      that is an array expression carries all the shape information.*/
    2664                 :         726 :   n = array_arg = 0;
    2665                 :         726 :   a = e->value.function.actual;
    2666                 :        1439 :   for (; a; a = a->next)
    2667                 :             :     {
    2668                 :        1141 :       n++;
    2669                 :        1141 :       if (!a->expr || a->expr->expr_type != EXPR_ARRAY)
    2670                 :         713 :         continue;
    2671                 :         428 :       array_arg = n;
    2672                 :         428 :       expr = gfc_copy_expr (a->expr);
    2673                 :         428 :       break;
    2674                 :             :     }
    2675                 :             : 
    2676                 :         726 :   if (!array_arg)
    2677                 :             :     return false;
    2678                 :             : 
    2679                 :         428 :   old = gfc_copy_expr (e);
    2680                 :             : 
    2681                 :         428 :   gfc_constructor_free (expr->value.constructor);
    2682                 :         428 :   expr->value.constructor = NULL;
    2683                 :         428 :   expr->ts = old->ts;
    2684                 :         428 :   expr->where = old->where;
    2685                 :         428 :   expr->expr_type = EXPR_ARRAY;
    2686                 :             : 
    2687                 :             :   /* Copy the array argument constructors into an array, with nulls
    2688                 :             :      for the scalars.  */
    2689                 :         428 :   n = 0;
    2690                 :         428 :   a = old->value.function.actual;
    2691                 :        1342 :   for (; a; a = a->next)
    2692                 :             :     {
    2693                 :             :       /* Check that this is OK for an initialization expression.  */
    2694                 :         914 :       if (a->expr && init_flag && !gfc_check_init_expr (a->expr))
    2695                 :           0 :         goto cleanup;
    2696                 :             : 
    2697                 :         914 :       rank[n] = 0;
    2698                 :         914 :       if (a->expr && a->expr->rank && a->expr->expr_type == EXPR_VARIABLE)
    2699                 :             :         {
    2700                 :           0 :           rank[n] = a->expr->rank;
    2701                 :           0 :           ctor = a->expr->symtree->n.sym->value->value.constructor;
    2702                 :           0 :           args[n] = gfc_constructor_first (ctor);
    2703                 :             :         }
    2704                 :         914 :       else if (a->expr && a->expr->expr_type == EXPR_ARRAY)
    2705                 :             :         {
    2706                 :         469 :           if (a->expr->rank)
    2707                 :         469 :             rank[n] = a->expr->rank;
    2708                 :             :           else
    2709                 :           0 :             rank[n] = 1;
    2710                 :         469 :           ctor = a->expr->value.constructor;
    2711                 :         469 :           args[n] = gfc_constructor_first (ctor);
    2712                 :             :         }
    2713                 :             :       else
    2714                 :         445 :         args[n] = NULL;
    2715                 :             : 
    2716                 :         914 :       n++;
    2717                 :             :     }
    2718                 :             : 
    2719                 :             :   /* Using the array argument as the master, step through the array
    2720                 :             :      calling the function for each element and advancing the array
    2721                 :             :      constructors together.  */
    2722                 :        3460 :   for (ci = args[array_arg - 1]; ci; ci = gfc_constructor_next (ci))
    2723                 :             :     {
    2724                 :        3032 :       new_ctor = gfc_constructor_append_expr (&expr->value.constructor,
    2725                 :             :                                               gfc_copy_expr (old), NULL);
    2726                 :             : 
    2727                 :        3032 :       gfc_free_actual_arglist (new_ctor->expr->value.function.actual);
    2728                 :        3032 :       a = NULL;
    2729                 :        3032 :       b = old->value.function.actual;
    2730                 :        9169 :       for (i = 0; i < n; i++)
    2731                 :             :         {
    2732                 :        6137 :           if (a == NULL)
    2733                 :        6064 :             new_ctor->expr->value.function.actual
    2734                 :        3032 :                         = a = gfc_get_actual_arglist ();
    2735                 :             :           else
    2736                 :             :             {
    2737                 :        3105 :               a->next = gfc_get_actual_arglist ();
    2738                 :        3105 :               a = a->next;
    2739                 :             :             }
    2740                 :             : 
    2741                 :        6137 :           if (args[i])
    2742                 :        4033 :             a->expr = gfc_copy_expr (args[i]->expr);
    2743                 :             :           else
    2744                 :        2104 :             a->expr = gfc_copy_expr (b->expr);
    2745                 :             : 
    2746                 :        6137 :           b = b->next;
    2747                 :             :         }
    2748                 :             : 
    2749                 :             :       /* Simplify the function calls.  If the simplification fails, the
    2750                 :             :          error will be flagged up down-stream or the library will deal
    2751                 :             :          with it.  */
    2752                 :        3032 :       p = gfc_copy_expr (new_ctor->expr);
    2753                 :             : 
    2754                 :        3032 :       if (!gfc_simplify_expr (p, init_flag))
    2755                 :          13 :         gfc_free_expr (p);
    2756                 :             :       else
    2757                 :        3019 :         gfc_replace_expr (new_ctor->expr, p);
    2758                 :             : 
    2759                 :        9169 :       for (i = 0; i < n; i++)
    2760                 :        6137 :         if (args[i])
    2761                 :        4033 :           args[i] = gfc_constructor_next (args[i]);
    2762                 :             : 
    2763                 :        6137 :       for (i = 1; i < n; i++)
    2764                 :        3105 :         if (rank[i] && ((args[i] != NULL && args[array_arg - 1] == NULL)
    2765                 :        1133 :                         || (args[i] == NULL && args[array_arg - 1] != NULL)))
    2766                 :           0 :           goto compliance;
    2767                 :             :     }
    2768                 :             : 
    2769                 :         428 :   free_expr0 (e);
    2770                 :         428 :   *e = *expr;
    2771                 :             :   /* Free "expr" but not the pointers it contains.  */
    2772                 :         428 :   free (expr);
    2773                 :         428 :   gfc_free_expr (old);
    2774                 :         428 :   return true;
    2775                 :             : 
    2776                 :           0 : compliance:
    2777                 :           0 :   gfc_error_now ("elemental function arguments at %C are not compliant");
    2778                 :             : 
    2779                 :           0 : cleanup:
    2780                 :           0 :   gfc_free_expr (expr);
    2781                 :           0 :   gfc_free_expr (old);
    2782                 :           0 :   return false;
    2783                 :             : }
    2784                 :             : 
    2785                 :             : 
    2786                 :             : static bool
    2787                 :        3662 : check_intrinsic_op (gfc_expr *e, bool (*check_function) (gfc_expr *))
    2788                 :             : {
    2789                 :        3662 :   gfc_expr *op1 = e->value.op.op1;
    2790                 :        3662 :   gfc_expr *op2 = e->value.op.op2;
    2791                 :             : 
    2792                 :        3662 :   if (!(*check_function)(op1))
    2793                 :             :     return false;
    2794                 :             : 
    2795                 :        2812 :   switch (e->value.op.op)
    2796                 :             :     {
    2797                 :         513 :     case INTRINSIC_UPLUS:
    2798                 :         513 :     case INTRINSIC_UMINUS:
    2799                 :         513 :       if (!numeric_type (et0 (op1)))
    2800                 :           0 :         goto not_numeric;
    2801                 :             :       break;
    2802                 :             : 
    2803                 :         145 :     case INTRINSIC_EQ:
    2804                 :         145 :     case INTRINSIC_EQ_OS:
    2805                 :         145 :     case INTRINSIC_NE:
    2806                 :         145 :     case INTRINSIC_NE_OS:
    2807                 :         145 :     case INTRINSIC_GT:
    2808                 :         145 :     case INTRINSIC_GT_OS:
    2809                 :         145 :     case INTRINSIC_GE:
    2810                 :         145 :     case INTRINSIC_GE_OS:
    2811                 :         145 :     case INTRINSIC_LT:
    2812                 :         145 :     case INTRINSIC_LT_OS:
    2813                 :         145 :     case INTRINSIC_LE:
    2814                 :         145 :     case INTRINSIC_LE_OS:
    2815                 :         145 :       if (!(*check_function)(op2))
    2816                 :             :         return false;
    2817                 :             : 
    2818                 :         217 :       if (!(et0 (op1) == BT_CHARACTER && et0 (op2) == BT_CHARACTER)
    2819                 :         145 :           && !(numeric_type (et0 (op1)) && numeric_type (et0 (op2))))
    2820                 :             :         {
    2821                 :           0 :           gfc_error ("Numeric or CHARACTER operands are required in "
    2822                 :             :                      "expression at %L", &e->where);
    2823                 :           0 :          return false;
    2824                 :             :         }
    2825                 :             :       break;
    2826                 :             : 
    2827                 :        2105 :     case INTRINSIC_PLUS:
    2828                 :        2105 :     case INTRINSIC_MINUS:
    2829                 :        2105 :     case INTRINSIC_TIMES:
    2830                 :        2105 :     case INTRINSIC_DIVIDE:
    2831                 :        2105 :     case INTRINSIC_POWER:
    2832                 :        2105 :       if (!(*check_function)(op2))
    2833                 :             :         return false;
    2834                 :             : 
    2835                 :        1891 :       if (!numeric_type (et0 (op1)) || !numeric_type (et0 (op2)))
    2836                 :           0 :         goto not_numeric;
    2837                 :             : 
    2838                 :             :       break;
    2839                 :             : 
    2840                 :           1 :     case INTRINSIC_CONCAT:
    2841                 :           1 :       if (!(*check_function)(op2))
    2842                 :             :         return false;
    2843                 :             : 
    2844                 :           0 :       if (et0 (op1) != BT_CHARACTER || et0 (op2) != BT_CHARACTER)
    2845                 :             :         {
    2846                 :           0 :           gfc_error ("Concatenation operator in expression at %L "
    2847                 :             :                      "must have two CHARACTER operands", &op1->where);
    2848                 :           0 :           return false;
    2849                 :             :         }
    2850                 :             : 
    2851                 :           0 :       if (op1->ts.kind != op2->ts.kind)
    2852                 :             :         {
    2853                 :           0 :           gfc_error ("Concat operator at %L must concatenate strings of the "
    2854                 :             :                      "same kind", &e->where);
    2855                 :           0 :           return false;
    2856                 :             :         }
    2857                 :             : 
    2858                 :             :       break;
    2859                 :             : 
    2860                 :           0 :     case INTRINSIC_NOT:
    2861                 :           0 :       if (et0 (op1) != BT_LOGICAL)
    2862                 :             :         {
    2863                 :           0 :           gfc_error (".NOT. operator in expression at %L must have a LOGICAL "
    2864                 :             :                      "operand", &op1->where);
    2865                 :           0 :           return false;
    2866                 :             :         }
    2867                 :             : 
    2868                 :             :       break;
    2869                 :             : 
    2870                 :           0 :     case INTRINSIC_AND:
    2871                 :           0 :     case INTRINSIC_OR:
    2872                 :           0 :     case INTRINSIC_EQV:
    2873                 :           0 :     case INTRINSIC_NEQV:
    2874                 :           0 :       if (!(*check_function)(op2))
    2875                 :             :         return false;
    2876                 :             : 
    2877                 :           0 :       if (et0 (op1) != BT_LOGICAL || et0 (op2) != BT_LOGICAL)
    2878                 :             :         {
    2879                 :           0 :           gfc_error ("LOGICAL operands are required in expression at %L",
    2880                 :             :                      &e->where);
    2881                 :           0 :           return false;
    2882                 :             :         }
    2883                 :             : 
    2884                 :             :       break;
    2885                 :             : 
    2886                 :             :     case INTRINSIC_PARENTHESES:
    2887                 :             :       break;
    2888                 :             : 
    2889                 :           0 :     default:
    2890                 :           0 :       gfc_error ("Only intrinsic operators can be used in expression at %L",
    2891                 :             :                  &e->where);
    2892                 :           0 :       return false;
    2893                 :             :     }
    2894                 :             : 
    2895                 :             :   return true;
    2896                 :             : 
    2897                 :           0 : not_numeric:
    2898                 :           0 :   gfc_error ("Numeric operands are required in expression at %L", &e->where);
    2899                 :             : 
    2900                 :           0 :   return false;
    2901                 :             : }
    2902                 :             : 
    2903                 :             : /* F2003, 7.1.7 (3): In init expression, allocatable components
    2904                 :             :    must not be data-initialized.  */
    2905                 :             : static bool
    2906                 :        1840 : check_alloc_comp_init (gfc_expr *e)
    2907                 :             : {
    2908                 :        1840 :   gfc_component *comp;
    2909                 :        1840 :   gfc_constructor *ctor;
    2910                 :             : 
    2911                 :        1840 :   gcc_assert (e->expr_type == EXPR_STRUCTURE);
    2912                 :        1840 :   gcc_assert (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS);
    2913                 :             : 
    2914                 :        1840 :   for (comp = e->ts.u.derived->components,
    2915                 :        1840 :        ctor = gfc_constructor_first (e->value.constructor);
    2916                 :        4213 :        comp; comp = comp->next, ctor = gfc_constructor_next (ctor))
    2917                 :             :     {
    2918                 :        2374 :       if (comp->attr.allocatable && ctor->expr
    2919                 :          31 :           && ctor->expr->expr_type != EXPR_NULL)
    2920                 :             :         {
    2921                 :           1 :           gfc_error ("Invalid initialization expression for ALLOCATABLE "
    2922                 :             :                      "component %qs in structure constructor at %L",
    2923                 :             :                      comp->name, &ctor->expr->where);
    2924                 :           1 :           return false;
    2925                 :             :         }
    2926                 :             :     }
    2927                 :             : 
    2928                 :             :   return true;
    2929                 :             : }
    2930                 :             : 
    2931                 :             : static match
    2932                 :         586 : check_init_expr_arguments (gfc_expr *e)
    2933                 :             : {
    2934                 :         586 :   gfc_actual_arglist *ap;
    2935                 :             : 
    2936                 :        1528 :   for (ap = e->value.function.actual; ap; ap = ap->next)
    2937                 :        1255 :     if (!gfc_check_init_expr (ap->expr))
    2938                 :             :       return MATCH_ERROR;
    2939                 :             : 
    2940                 :             :   return MATCH_YES;
    2941                 :             : }
    2942                 :             : 
    2943                 :             : static bool check_restricted (gfc_expr *);
    2944                 :             : 
    2945                 :             : /* F95, 7.1.6.1, Initialization expressions, (7)
    2946                 :             :    F2003, 7.1.7 Initialization expression, (8)
    2947                 :             :    F2008, 7.1.12 Constant expression, (4)  */
    2948                 :             : 
    2949                 :             : static match
    2950                 :        4132 : check_inquiry (gfc_expr *e, int not_restricted)
    2951                 :             : {
    2952                 :        4132 :   const char *name;
    2953                 :        4132 :   const char *const *functions;
    2954                 :             : 
    2955                 :        4132 :   static const char *const inquiry_func_f95[] = {
    2956                 :             :     "lbound", "shape", "size", "ubound",
    2957                 :             :     "bit_size", "len", "kind",
    2958                 :             :     "digits", "epsilon", "huge", "maxexponent", "minexponent",
    2959                 :             :     "precision", "radix", "range", "tiny",
    2960                 :             :     NULL
    2961                 :             :   };
    2962                 :             : 
    2963                 :        4132 :   static const char *const inquiry_func_f2003[] = {
    2964                 :             :     "lbound", "shape", "size", "ubound",
    2965                 :             :     "bit_size", "len", "kind",
    2966                 :             :     "digits", "epsilon", "huge", "maxexponent", "minexponent",
    2967                 :             :     "precision", "radix", "range", "tiny",
    2968                 :             :     "new_line", NULL
    2969                 :             :   };
    2970                 :             : 
    2971                 :             :   /* std=f2008+ or -std=gnu */
    2972                 :        4132 :   static const char *const inquiry_func_gnu[] = {
    2973                 :             :     "lbound", "shape", "size", "ubound",
    2974                 :             :     "bit_size", "len", "kind",
    2975                 :             :     "digits", "epsilon", "huge", "maxexponent", "minexponent",
    2976                 :             :     "precision", "radix", "range", "tiny",
    2977                 :             :     "new_line", "storage_size", NULL
    2978                 :             :   };
    2979                 :             : 
    2980                 :        4132 :   int i = 0;
    2981                 :        4132 :   gfc_actual_arglist *ap;
    2982                 :        4132 :   gfc_symbol *sym;
    2983                 :        4132 :   gfc_symbol *asym;
    2984                 :             : 
    2985                 :        4132 :   if (!e->value.function.isym
    2986                 :        4032 :       || !e->value.function.isym->inquiry)
    2987                 :             :     return MATCH_NO;
    2988                 :             : 
    2989                 :             :   /* An undeclared parameter will get us here (PR25018).  */
    2990                 :        2804 :   if (e->symtree == NULL)
    2991                 :             :     return MATCH_NO;
    2992                 :             : 
    2993                 :        2802 :   sym = e->symtree->n.sym;
    2994                 :             : 
    2995                 :        2802 :   if (sym->from_intmod)
    2996                 :             :     {
    2997                 :           2 :       if (sym->from_intmod == INTMOD_ISO_FORTRAN_ENV
    2998                 :           0 :           && sym->intmod_sym_id != ISOFORTRAN_COMPILER_OPTIONS
    2999                 :           0 :           && sym->intmod_sym_id != ISOFORTRAN_COMPILER_VERSION)
    3000                 :             :         return MATCH_NO;
    3001                 :             : 
    3002                 :           2 :       if (sym->from_intmod == INTMOD_ISO_C_BINDING
    3003                 :           2 :           && sym->intmod_sym_id != ISOCBINDING_C_SIZEOF)
    3004                 :             :         return MATCH_NO;
    3005                 :             :     }
    3006                 :             :   else
    3007                 :             :     {
    3008                 :        2800 :       name = sym->name;
    3009                 :             : 
    3010                 :        2800 :       functions = inquiry_func_gnu;
    3011                 :        2800 :       if (gfc_option.warn_std & GFC_STD_F2003)
    3012                 :           0 :         functions = inquiry_func_f2003;
    3013                 :        2800 :       if (gfc_option.warn_std & GFC_STD_F95)
    3014                 :           0 :         functions = inquiry_func_f95;
    3015                 :             : 
    3016                 :       11757 :       for (i = 0; functions[i]; i++)
    3017                 :       11751 :         if (strcmp (functions[i], name) == 0)
    3018                 :             :           break;
    3019                 :             : 
    3020                 :        2800 :       if (functions[i] == NULL)
    3021                 :             :         return MATCH_ERROR;
    3022                 :             :     }
    3023                 :             : 
    3024                 :             :   /* At this point we have an inquiry function with a variable argument.  The
    3025                 :             :      type of the variable might be undefined, but we need it now, because the
    3026                 :             :      arguments of these functions are not allowed to be undefined.  */
    3027                 :             : 
    3028                 :        8985 :   for (ap = e->value.function.actual; ap; ap = ap->next)
    3029                 :             :     {
    3030                 :        6690 :       if (!ap->expr)
    3031                 :        3273 :         continue;
    3032                 :             : 
    3033                 :        3417 :       asym = ap->expr->symtree ? ap->expr->symtree->n.sym : NULL;
    3034                 :             : 
    3035                 :        3417 :       if (ap->expr->ts.type == BT_UNKNOWN)
    3036                 :             :         {
    3037                 :           0 :           if (asym && asym->ts.type == BT_UNKNOWN
    3038                 :           0 :               && !gfc_set_default_type (asym, 0, gfc_current_ns))
    3039                 :             :             return MATCH_NO;
    3040                 :             : 
    3041                 :           0 :           ap->expr->ts = asym->ts;
    3042                 :             :         }
    3043                 :             : 
    3044                 :        3417 :       if (asym && asym->assoc && asym->assoc->target
    3045                 :          12 :           && asym->assoc->target->expr_type == EXPR_CONSTANT)
    3046                 :             :         {
    3047                 :          12 :           gfc_free_expr (ap->expr);
    3048                 :          12 :           ap->expr = gfc_copy_expr (asym->assoc->target);
    3049                 :             :         }
    3050                 :             : 
    3051                 :             :       /* Assumed character length will not reduce to a constant expression
    3052                 :             :          with LEN, as required by the standard.  */
    3053                 :        3417 :       if (i == 5 && not_restricted && asym
    3054                 :         403 :           && asym->ts.type == BT_CHARACTER
    3055                 :         403 :           && ((asym->ts.u.cl && asym->ts.u.cl->length == NULL)
    3056                 :          49 :               || asym->ts.deferred))
    3057                 :             :         {
    3058                 :         354 :           gfc_error ("Assumed or deferred character length variable %qs "
    3059                 :             :                      "in constant expression at %L",
    3060                 :         354 :                       asym->name, &ap->expr->where);
    3061                 :         354 :           return MATCH_ERROR;
    3062                 :             :         }
    3063                 :        3063 :       else if (not_restricted && !gfc_check_init_expr (ap->expr))
    3064                 :             :         return MATCH_ERROR;
    3065                 :             : 
    3066                 :        2921 :       if (not_restricted == 0
    3067                 :        2901 :           && ap->expr->expr_type != EXPR_VARIABLE
    3068                 :        3600 :           && !check_restricted (ap->expr))
    3069                 :             :         return MATCH_ERROR;
    3070                 :             : 
    3071                 :        2919 :       if (not_restricted == 0
    3072                 :        2899 :           && ap->expr->expr_type == EXPR_VARIABLE
    3073                 :        2222 :           && asym->attr.dummy && asym->attr.optional)
    3074                 :             :         return MATCH_NO;
    3075                 :             :     }
    3076                 :             : 
    3077                 :             :   return MATCH_YES;
    3078                 :             : }
    3079                 :             : 
    3080                 :             : 
    3081                 :             : /* F95, 7.1.6.1, Initialization expressions, (5)
    3082                 :             :    F2003, 7.1.7 Initialization expression, (5)  */
    3083                 :             : 
    3084                 :             : static match
    3085                 :         587 : check_transformational (gfc_expr *e)
    3086                 :             : {
    3087                 :         587 :   static const char * const trans_func_f95[] = {
    3088                 :             :     "repeat", "reshape", "selected_int_kind",
    3089                 :             :     "selected_real_kind", "transfer", "trim", NULL
    3090                 :             :   };
    3091                 :             : 
    3092                 :         587 :   static const char * const trans_func_f2003[] =  {
    3093                 :             :     "all", "any", "count", "dot_product", "matmul", "null", "pack",
    3094                 :             :     "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
    3095                 :             :     "selected_real_kind", "spread", "sum", "transfer", "transpose",
    3096                 :             :     "trim", "unpack", NULL
    3097                 :             :   };
    3098                 :             : 
    3099                 :         587 :   static const char * const trans_func_f2008[] =  {
    3100                 :             :     "all", "any", "count", "dot_product", "matmul", "null", "pack",
    3101                 :             :     "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
    3102                 :             :     "selected_real_kind", "spread", "sum", "transfer", "transpose",
    3103                 :             :     "trim", "unpack", "findloc", NULL
    3104                 :             :   };
    3105                 :             : 
    3106                 :         587 :   static const char * const trans_func_f2023[] =  {
    3107                 :             :     "all", "any", "count", "dot_product", "matmul", "null", "pack",
    3108                 :             :     "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
    3109                 :             :     "selected_logical_kind", "selected_real_kind", "spread", "sum", "transfer",
    3110                 :             :     "transpose", "trim", "unpack", "findloc", NULL
    3111                 :             :   };
    3112                 :             : 
    3113                 :         587 :   int i;
    3114                 :         587 :   const char *name;
    3115                 :         587 :   const char *const *functions;
    3116                 :             : 
    3117                 :         587 :   if (!e->value.function.isym
    3118                 :         587 :       || !e->value.function.isym->transformational)
    3119                 :             :     return MATCH_NO;
    3120                 :             : 
    3121                 :         102 :   name = e->symtree->n.sym->name;
    3122                 :             : 
    3123                 :         102 :   if (gfc_option.allow_std & GFC_STD_F2023)
    3124                 :             :     functions = trans_func_f2023;
    3125                 :           0 :   else if (gfc_option.allow_std & GFC_STD_F2008)
    3126                 :             :     functions = trans_func_f2008;
    3127                 :           0 :   else if (gfc_option.allow_std & GFC_STD_F2003)
    3128                 :             :     functions = trans_func_f2003;
    3129                 :             :   else
    3130                 :           0 :     functions = trans_func_f95;
    3131                 :             : 
    3132                 :             :   /* NULL() is dealt with below.  */
    3133                 :         102 :   if (strcmp ("null", name) == 0)
    3134                 :             :     return MATCH_NO;
    3135                 :             : 
    3136                 :        1621 :   for (i = 0; functions[i]; i++)
    3137                 :        1620 :     if (strcmp (functions[i], name) == 0)
    3138                 :             :        break;
    3139                 :             : 
    3140                 :         102 :   if (functions[i] == NULL)
    3141                 :             :     {
    3142                 :           1 :       gfc_error ("transformational intrinsic %qs at %L is not permitted "
    3143                 :             :                  "in an initialization expression", name, &e->where);
    3144                 :           1 :       return MATCH_ERROR;
    3145                 :             :     }
    3146                 :             : 
    3147                 :         101 :   return check_init_expr_arguments (e);
    3148                 :             : }
    3149                 :             : 
    3150                 :             : 
    3151                 :             : /* F95, 7.1.6.1, Initialization expressions, (6)
    3152                 :             :    F2003, 7.1.7 Initialization expression, (6)  */
    3153                 :             : 
    3154                 :             : static match
    3155                 :         587 : check_null (gfc_expr *e)
    3156                 :             : {
    3157                 :         587 :   if (strcmp ("null", e->symtree->n.sym->name) != 0)
    3158                 :             :     return MATCH_NO;
    3159                 :             : 
    3160                 :           0 :   return check_init_expr_arguments (e);
    3161                 :             : }
    3162                 :             : 
    3163                 :             : 
    3164                 :             : static match
    3165                 :         485 : check_elemental (gfc_expr *e)
    3166                 :             : {
    3167                 :         485 :   if (!e->value.function.isym
    3168                 :         485 :       || !e->value.function.isym->elemental)
    3169                 :             :     return MATCH_NO;
    3170                 :             : 
    3171                 :         482 :   if (e->ts.type != BT_INTEGER
    3172                 :           2 :       && e->ts.type != BT_CHARACTER
    3173                 :         484 :       && !gfc_notify_std (GFC_STD_F2003, "Evaluation of nonstandard "
    3174                 :             :                           "initialization expression at %L", &e->where))
    3175                 :             :     return MATCH_ERROR;
    3176                 :             : 
    3177                 :         482 :   return check_init_expr_arguments (e);
    3178                 :             : }
    3179                 :             : 
    3180                 :             : 
    3181                 :             : static match
    3182                 :        1104 : check_conversion (gfc_expr *e)
    3183                 :             : {
    3184                 :        1104 :   if (!e->value.function.isym
    3185                 :        1104 :       || !e->value.function.isym->conversion)
    3186                 :             :     return MATCH_NO;
    3187                 :             : 
    3188                 :           3 :   return check_init_expr_arguments (e);
    3189                 :             : }
    3190                 :             : 
    3191                 :             : 
    3192                 :             : /* Verify that an expression is an initialization expression.  A side
    3193                 :             :    effect is that the expression tree is reduced to a single constant
    3194                 :             :    node if all goes well.  This would normally happen when the
    3195                 :             :    expression is constructed but function references are assumed to be
    3196                 :             :    intrinsics in the context of initialization expressions.  If
    3197                 :             :    false is returned an error message has been generated.  */
    3198                 :             : 
    3199                 :             : bool
    3200                 :      653870 : gfc_check_init_expr (gfc_expr *e)
    3201                 :             : {
    3202                 :      653870 :   match m;
    3203                 :      653870 :   bool t;
    3204                 :             : 
    3205                 :      653870 :   if (e == NULL)
    3206                 :             :     return true;
    3207                 :             : 
    3208                 :      653829 :   switch (e->expr_type)
    3209                 :             :     {
    3210                 :        1077 :     case EXPR_OP:
    3211                 :        1077 :       t = check_intrinsic_op (e, gfc_check_init_expr);
    3212                 :        1077 :       if (t)
    3213                 :          14 :         t = gfc_simplify_expr (e, 0);
    3214                 :             : 
    3215                 :             :       break;
    3216                 :             : 
    3217                 :           1 :     case EXPR_CONDITIONAL:
    3218                 :           1 :       t = gfc_check_init_expr (e->value.conditional.condition);
    3219                 :           1 :       if (!t)
    3220                 :             :         break;
    3221                 :           0 :       t = gfc_check_init_expr (e->value.conditional.true_expr);
    3222                 :           0 :       if (!t)
    3223                 :             :         break;
    3224                 :           0 :       t = gfc_check_init_expr (e->value.conditional.false_expr);
    3225                 :           0 :       if (t)
    3226                 :           0 :         t = gfc_simplify_expr (e, 0);
    3227                 :             :       else
    3228                 :             :         t = false;
    3229                 :             :       break;
    3230                 :             : 
    3231                 :        1656 :     case EXPR_FUNCTION:
    3232                 :        1656 :       t = false;
    3233                 :             : 
    3234                 :        1656 :       {
    3235                 :        1656 :         bool conversion;
    3236                 :        1656 :         gfc_intrinsic_sym* isym = NULL;
    3237                 :        1656 :         gfc_symbol* sym = e->symtree->n.sym;
    3238                 :             : 
    3239                 :             :         /* Simplify here the intrinsics from the IEEE_ARITHMETIC and
    3240                 :             :            IEEE_EXCEPTIONS modules.  */
    3241                 :        1656 :         int mod = sym->from_intmod;
    3242                 :        1656 :         if (mod == INTMOD_NONE && sym->generic)
    3243                 :         192 :           mod = sym->generic->sym->from_intmod;
    3244                 :        1656 :         if (mod == INTMOD_IEEE_ARITHMETIC || mod == INTMOD_IEEE_EXCEPTIONS)
    3245                 :             :           {
    3246                 :         453 :             gfc_expr *new_expr = gfc_simplify_ieee_functions (e);
    3247                 :         453 :             if (new_expr)
    3248                 :             :               {
    3249                 :         327 :                 gfc_replace_expr (e, new_expr);
    3250                 :         327 :                 t = true;
    3251                 :         327 :                 break;
    3252                 :             :               }
    3253                 :             :           }
    3254                 :             : 
    3255                 :             :         /* If a conversion function, e.g., __convert_i8_i4, was inserted
    3256                 :             :            into an array constructor, we need to skip the error check here.
    3257                 :             :            Conversion errors are  caught below in scalarize_intrinsic_call.  */
    3258                 :        3759 :         conversion = e->value.function.isym
    3259                 :        1329 :                    && (e->value.function.isym->conversion == 1);
    3260                 :             : 
    3261                 :        1326 :         if (!conversion && (!gfc_is_intrinsic (sym, 0, e->where)
    3262                 :        1117 :             || (m = gfc_intrinsic_func_interface (e, 0)) == MATCH_NO))
    3263                 :             :           {
    3264                 :         225 :             gfc_error ("Function %qs in initialization expression at %L "
    3265                 :             :                        "must be an intrinsic function",
    3266                 :         225 :                        e->symtree->n.sym->name, &e->where);
    3267                 :         225 :             break;
    3268                 :             :           }
    3269                 :             : 
    3270                 :        1104 :         if ((m = check_conversion (e)) == MATCH_NO
    3271                 :        1101 :             && (m = check_inquiry (e, 1)) == MATCH_NO
    3272                 :         587 :             && (m = check_null (e)) == MATCH_NO
    3273                 :         587 :             && (m = check_transformational (e)) == MATCH_NO
    3274                 :        1589 :             && (m = check_elemental (e)) == MATCH_NO)
    3275                 :             :           {
    3276                 :           3 :             gfc_error ("Intrinsic function %qs at %L is not permitted "
    3277                 :             :                        "in an initialization expression",
    3278                 :           3 :                        e->symtree->n.sym->name, &e->where);
    3279                 :           3 :             m = MATCH_ERROR;
    3280                 :             :           }
    3281                 :             : 
    3282                 :        1104 :         if (m == MATCH_ERROR)
    3283                 :         815 :           return false;
    3284                 :             : 
    3285                 :             :         /* Try to scalarize an elemental intrinsic function that has an
    3286                 :             :            array argument.  */
    3287                 :         289 :         isym = gfc_find_function (e->symtree->n.sym->name);
    3288                 :         289 :         if (isym && isym->elemental
    3289                 :         529 :             && (t = scalarize_intrinsic_call (e, true)))
    3290                 :             :           break;
    3291                 :             :       }
    3292                 :             : 
    3293                 :         289 :       if (m == MATCH_YES)
    3294                 :         289 :         t = gfc_simplify_expr (e, 0);
    3295                 :             : 
    3296                 :             :       break;
    3297                 :             : 
    3298                 :        4317 :     case EXPR_VARIABLE:
    3299                 :        4317 :       t = true;
    3300                 :             : 
    3301                 :             :       /* This occurs when parsing pdt templates.  */
    3302                 :        4317 :       if (gfc_expr_attr (e).pdt_kind)
    3303                 :             :         break;
    3304                 :             : 
    3305                 :        4297 :       if (gfc_check_iter_variable (e))
    3306                 :             :         break;
    3307                 :             : 
    3308                 :        4281 :       if (e->symtree->n.sym->attr.flavor == FL_PARAMETER)
    3309                 :             :         {
    3310                 :             :           /* A PARAMETER shall not be used to define itself, i.e.
    3311                 :             :                 REAL, PARAMETER :: x = transfer(0, x)
    3312                 :             :              is invalid.  */
    3313                 :         365 :           if (!e->symtree->n.sym->value)
    3314                 :             :             {
    3315                 :           9 :               gfc_error ("PARAMETER %qs is used at %L before its definition "
    3316                 :             :                          "is complete", e->symtree->n.sym->name, &e->where);
    3317                 :           9 :               t = false;
    3318                 :             :             }
    3319                 :             :           else
    3320                 :         356 :             t = simplify_parameter_variable (e, 0);
    3321                 :             : 
    3322                 :             :           break;
    3323                 :             :         }
    3324                 :             : 
    3325                 :        3916 :       if (gfc_in_match_data ())
    3326                 :             :         break;
    3327                 :             : 
    3328                 :        3898 :       t = false;
    3329                 :             : 
    3330                 :        3898 :       if (e->symtree->n.sym->as)
    3331                 :             :         {
    3332                 :         155 :           switch (e->symtree->n.sym->as->type)
    3333                 :             :             {
    3334                 :           1 :               case AS_ASSUMED_SIZE:
    3335                 :           1 :                 gfc_error ("Assumed size array %qs at %L is not permitted "
    3336                 :             :                            "in an initialization expression",
    3337                 :             :                            e->symtree->n.sym->name, &e->where);
    3338                 :           1 :                 break;
    3339                 :             : 
    3340                 :          18 :               case AS_ASSUMED_SHAPE:
    3341                 :          18 :                 gfc_error ("Assumed shape array %qs at %L is not permitted "
    3342                 :             :                            "in an initialization expression",
    3343                 :             :                            e->symtree->n.sym->name, &e->where);
    3344                 :          18 :                 break;
    3345                 :             : 
    3346                 :         110 :               case AS_DEFERRED:
    3347                 :         110 :                 if (!e->symtree->n.sym->attr.allocatable
    3348                 :          89 :                     && !e->symtree->n.sym->attr.pointer
    3349                 :          65 :                     && e->symtree->n.sym->attr.dummy)
    3350                 :          65 :                   gfc_error ("Assumed-shape array %qs at %L is not permitted "
    3351                 :             :                              "in an initialization expression",
    3352                 :             :                              e->symtree->n.sym->name, &e->where);
    3353                 :             :                 else
    3354                 :          45 :                   gfc_error ("Deferred array %qs at %L is not permitted "
    3355                 :             :                              "in an initialization expression",
    3356                 :             :                              e->symtree->n.sym->name, &e->where);
    3357                 :             :                 break;
    3358                 :             : 
    3359                 :          20 :               case AS_EXPLICIT:
    3360                 :          20 :                 gfc_error ("Array %qs at %L is a variable, which does "
    3361                 :             :                            "not reduce to a constant expression",
    3362                 :             :                            e->symtree->n.sym->name, &e->where);
    3363                 :          20 :                 break;
    3364                 :             : 
    3365                 :           6 :               case AS_ASSUMED_RANK:
    3366                 :           6 :                 gfc_error ("Assumed-rank array %qs at %L is not permitted "
    3367                 :             :                            "in an initialization expression",
    3368                 :             :                            e->symtree->n.sym->name, &e->where);
    3369                 :           6 :                 break;
    3370                 :             : 
    3371                 :           0 :               default:
    3372                 :           0 :                 gcc_unreachable();
    3373                 :             :           }
    3374                 :             :         }
    3375                 :             :       else
    3376                 :        3743 :         gfc_error ("Parameter %qs at %L has not been declared or is "
    3377                 :             :                    "a variable, which does not reduce to a constant "
    3378                 :             :                    "expression", e->symtree->name, &e->where);
    3379                 :             : 
    3380                 :             :       break;
    3381                 :             : 
    3382                 :             :     case EXPR_CONSTANT:
    3383                 :             :     case EXPR_NULL:
    3384                 :             :       t = true;
    3385                 :             :       break;
    3386                 :             : 
    3387                 :          11 :     case EXPR_SUBSTRING:
    3388                 :          11 :       if (e->ref)
    3389                 :             :         {
    3390                 :           7 :           t = gfc_check_init_expr (e->ref->u.ss.start);
    3391                 :           7 :           if (!t)
    3392                 :             :             break;
    3393                 :             : 
    3394                 :           7 :           t = gfc_check_init_expr (e->ref->u.ss.end);
    3395                 :           7 :           if (t)
    3396                 :           7 :             t = gfc_simplify_expr (e, 0);
    3397                 :             :         }
    3398                 :             :       else
    3399                 :             :         t = false;
    3400                 :             :       break;
    3401                 :             : 
    3402                 :        1954 :     case EXPR_STRUCTURE:
    3403                 :        1954 :       t = e->ts.is_iso_c ? true : false;
    3404                 :        1954 :       if (t)
    3405                 :             :         break;
    3406                 :             : 
    3407                 :        1840 :       t = check_alloc_comp_init (e);
    3408                 :        1840 :       if (!t)
    3409                 :             :         break;
    3410                 :             : 
    3411                 :        1839 :       t = gfc_check_constructor (e, gfc_check_init_expr);
    3412                 :        1839 :       if (!t)
    3413                 :             :         break;
    3414                 :             : 
    3415                 :        1839 :       break;
    3416                 :             : 
    3417                 :        4886 :     case EXPR_ARRAY:
    3418                 :        4886 :       t = gfc_check_constructor (e, gfc_check_init_expr);
    3419                 :        4886 :       if (!t)
    3420                 :             :         break;
    3421                 :             : 
    3422                 :        4867 :       t = gfc_expand_constructor (e, true);
    3423                 :        4867 :       if (!t)
    3424                 :             :         break;
    3425                 :             : 
    3426                 :        4846 :       t = gfc_check_constructor_type (e);
    3427                 :        4846 :       break;
    3428                 :             : 
    3429                 :           0 :     default:
    3430                 :           0 :       gfc_internal_error ("check_init_expr(): Unknown expression type");
    3431                 :             :     }
    3432                 :             : 
    3433                 :             :   return t;
    3434                 :             : }
    3435                 :             : 
    3436                 :             : /* Reduces a general expression to an initialization expression (a constant).
    3437                 :             :    This used to be part of gfc_match_init_expr.
    3438                 :             :    Note that this function doesn't free the given expression on false.  */
    3439                 :             : 
    3440                 :             : bool
    3441                 :      293975 : gfc_reduce_init_expr (gfc_expr *expr)
    3442                 :             : {
    3443                 :      293975 :   bool t;
    3444                 :             : 
    3445                 :             :   /* It is far too early to resolve a class compcall. Punt to resolution.  */
    3446                 :      293975 :   if (expr && expr->expr_type == EXPR_COMPCALL
    3447                 :          25 :       && expr->symtree->n.sym->ts.type == BT_CLASS)
    3448                 :             :     return false;
    3449                 :             : 
    3450                 :      293950 :   gfc_init_expr_flag = true;
    3451                 :      293950 :   t = gfc_resolve_expr (expr);
    3452                 :      293950 :   if (t)
    3453                 :      293813 :     t = gfc_check_init_expr (expr);
    3454                 :      293950 :   gfc_init_expr_flag = false;
    3455                 :             : 
    3456                 :      293950 :   if (!t || !expr)
    3457                 :             :     return false;
    3458                 :             : 
    3459                 :      289277 :   if (expr->expr_type == EXPR_ARRAY)
    3460                 :             :     {
    3461                 :        5074 :       if (!gfc_check_constructor_type (expr))
    3462                 :             :         return false;
    3463                 :        5074 :       if (!gfc_expand_constructor (expr, true))
    3464                 :             :         return false;
    3465                 :             :     }
    3466                 :             : 
    3467                 :             :   return true;
    3468                 :             : }
    3469                 :             : 
    3470                 :             : 
    3471                 :             : /* Match an initialization expression.  We work by first matching an
    3472                 :             :    expression, then reducing it to a constant.  */
    3473                 :             : 
    3474                 :             : match
    3475                 :       90026 : gfc_match_init_expr (gfc_expr **result)
    3476                 :             : {
    3477                 :       90026 :   gfc_expr *expr;
    3478                 :       90026 :   match m;
    3479                 :       90026 :   bool t;
    3480                 :             : 
    3481                 :       90026 :   expr = NULL;
    3482                 :             : 
    3483                 :       90026 :   gfc_init_expr_flag = true;
    3484                 :             : 
    3485                 :       90026 :   m = gfc_match_expr (&expr);
    3486                 :       90026 :   if (m != MATCH_YES)
    3487                 :             :     {
    3488                 :         112 :       gfc_init_expr_flag = false;
    3489                 :         112 :       return m;
    3490                 :             :     }
    3491                 :             : 
    3492                 :       89914 :   if (expr->expr_type != EXPR_FUNCTION && gfc_derived_parameter_expr (expr))
    3493                 :             :     {
    3494                 :         110 :       *result = expr;
    3495                 :         110 :       gfc_init_expr_flag = false;
    3496                 :         110 :       return m;
    3497                 :             :     }
    3498                 :             : 
    3499                 :       89804 :   t = gfc_reduce_init_expr (expr);
    3500                 :       89804 :   if (!t)
    3501                 :             :     {
    3502                 :         491 :       gfc_free_expr (expr);
    3503                 :         491 :       gfc_init_expr_flag = false;
    3504                 :         491 :       return MATCH_ERROR;
    3505                 :             :     }
    3506                 :             : 
    3507                 :       89313 :   *result = expr;
    3508                 :       89313 :   gfc_init_expr_flag = false;
    3509                 :             : 
    3510                 :       89313 :   return MATCH_YES;
    3511                 :             : }
    3512                 :             : 
    3513                 :             : 
    3514                 :             : /* Given an actual argument list, test to see that each argument is a
    3515                 :             :    restricted expression and optionally if the expression type is
    3516                 :             :    integer or character.  */
    3517                 :             : 
    3518                 :             : static bool
    3519                 :        1327 : restricted_args (gfc_actual_arglist *a)
    3520                 :             : {
    3521                 :        3383 :   for (; a; a = a->next)
    3522                 :             :     {
    3523                 :        2057 :       if (!check_restricted (a->expr))
    3524                 :             :         return false;
    3525                 :             :     }
    3526                 :             : 
    3527                 :             :   return true;
    3528                 :             : }
    3529                 :             : 
    3530                 :             : 
    3531                 :             : /************* Restricted/specification expressions *************/
    3532                 :             : 
    3533                 :             : 
    3534                 :             : /* Make sure a non-intrinsic function is a specification function,
    3535                 :             :  * see F08:7.1.11.5.  */
    3536                 :             : 
    3537                 :             : static bool
    3538                 :         577 : external_spec_function (gfc_expr *e)
    3539                 :             : {
    3540                 :         577 :   gfc_symbol *f;
    3541                 :             : 
    3542                 :         577 :   f = e->value.function.esym;
    3543                 :             : 
    3544                 :             :   /* IEEE functions allowed are "a reference to a transformational function
    3545                 :             :      from the intrinsic module IEEE_ARITHMETIC or IEEE_EXCEPTIONS", and
    3546                 :             :      "inquiry function from the intrinsic modules IEEE_ARITHMETIC and
    3547                 :             :      IEEE_EXCEPTIONS".  */
    3548                 :         577 :   if (f->from_intmod == INTMOD_IEEE_ARITHMETIC
    3549                 :         577 :       || f->from_intmod == INTMOD_IEEE_EXCEPTIONS)
    3550                 :             :     {
    3551                 :         234 :       if (!strcmp (f->name, "ieee_selected_real_kind")
    3552                 :         216 :           || !strcmp (f->name, "ieee_support_rounding")
    3553                 :         216 :           || !strcmp (f->name, "ieee_support_flag")
    3554                 :         216 :           || !strcmp (f->name, "ieee_support_halting")
    3555                 :         216 :           || !strcmp (f->name, "ieee_support_datatype")
    3556                 :         216 :           || !strcmp (f->name, "ieee_support_denormal")
    3557                 :         216 :           || !strcmp (f->name, "ieee_support_subnormal")
    3558                 :         216 :           || !strcmp (f->name, "ieee_support_divide")
    3559                 :         216 :           || !strcmp (f->name, "ieee_support_inf")
    3560                 :         216 :           || !strcmp (f->name, "ieee_support_io")
    3561                 :         216 :           || !strcmp (f->name, "ieee_support_nan")
    3562                 :         216 :           || !strcmp (f->name, "ieee_support_sqrt")
    3563                 :         216 :           || !strcmp (f->name, "ieee_support_standard")
    3564                 :         216 :           || !strcmp (f->name, "ieee_support_underflow_control"))
    3565                 :          18 :         goto function_allowed;
    3566                 :             :     }
    3567                 :             : 
    3568                 :         559 :   if (f->attr.proc == PROC_ST_FUNCTION)
    3569                 :             :     {
    3570                 :           0 :       gfc_error ("Specification function %qs at %L cannot be a statement "
    3571                 :             :                  "function", f->name, &e->where);
    3572                 :           0 :       return false;
    3573                 :             :     }
    3574                 :             : 
    3575                 :         559 :   if (f->attr.proc == PROC_INTERNAL)
    3576                 :             :     {
    3577                 :           0 :       gfc_error ("Specification function %qs at %L cannot be an internal "
    3578                 :             :                  "function", f->name, &e->where);
    3579                 :           0 :       return false;
    3580                 :             :     }
    3581                 :             : 
    3582                 :         559 :   if (!f->attr.pure && !f->attr.elemental)
    3583                 :             :     {
    3584                 :           2 :       gfc_error ("Specification function %qs at %L must be PURE", f->name,
    3585                 :             :                  &e->where);
    3586                 :           2 :       return false;
    3587                 :             :     }
    3588                 :             : 
    3589                 :             :   /* F08:7.1.11.6. */
    3590                 :         557 :   if (f->attr.recursive
    3591                 :         557 :       && !gfc_notify_std (GFC_STD_F2003,
    3592                 :             :                           "Specification function %qs "
    3593                 :             :                           "at %L cannot be RECURSIVE",  f->name, &e->where))
    3594                 :             :       return false;
    3595                 :             : 
    3596                 :         575 : function_allowed:
    3597                 :         575 :   return restricted_args (e->value.function.actual);
    3598                 :             : }
    3599                 :             : 
    3600                 :             : 
    3601                 :             : /* Check to see that a function reference to an intrinsic is a
    3602                 :             :    restricted expression.  */
    3603                 :             : 
    3604                 :             : static bool
    3605                 :        3031 : restricted_intrinsic (gfc_expr *e)
    3606                 :             : {
    3607                 :             :   /* TODO: Check constraints on inquiry functions.  7.1.6.2 (7).  */
    3608                 :        3031 :   if (check_inquiry (e, 0) == MATCH_YES)
    3609                 :             :     return true;
    3610                 :             : 
    3611                 :         752 :   return restricted_args (e->value.function.actual);
    3612                 :             : }
    3613                 :             : 
    3614                 :             : 
    3615                 :             : /* Check the expressions of an actual arglist.  Used by check_restricted.  */
    3616                 :             : 
    3617                 :             : static bool
    3618                 :        1328 : check_arglist (gfc_actual_arglist* arg, bool (*checker) (gfc_expr*))
    3619                 :             : {
    3620                 :        3367 :   for (; arg; arg = arg->next)
    3621                 :        2047 :     if (!checker (arg->expr))
    3622                 :             :       return false;
    3623                 :             : 
    3624                 :             :   return true;
    3625                 :             : }
    3626                 :             : 
    3627                 :             : 
    3628                 :             : /* Check the subscription expressions of a reference chain with a checking
    3629                 :             :    function; used by check_restricted.  */
    3630                 :             : 
    3631                 :             : static bool
    3632                 :       15021 : check_references (gfc_ref* ref, bool (*checker) (gfc_expr*))
    3633                 :             : {
    3634                 :       15883 :   int dim;
    3635                 :             : 
    3636                 :       15883 :   if (!ref)
    3637                 :             :     return true;
    3638                 :             : 
    3639                 :         865 :   switch (ref->type)
    3640                 :             :     {
    3641                 :             :     case REF_ARRAY:
    3642                 :        1384 :       for (dim = 0; dim < ref->u.ar.dimen; ++dim)
    3643                 :             :         {
    3644                 :         699 :           if (!checker (ref->u.ar.start[dim]))
    3645                 :             :             return false;
    3646                 :         697 :           if (!checker (ref->u.ar.end[dim]))
    3647                 :             :             return false;
    3648                 :         697 :           if (!checker (ref->u.ar.stride[dim]))
    3649                 :             :             return false;
    3650                 :             :         }
    3651                 :             :       break;
    3652                 :             : 
    3653                 :             :     case REF_COMPONENT:
    3654                 :             :       /* Nothing needed, just proceed to next reference.  */
    3655                 :             :       break;
    3656                 :             : 
    3657                 :          13 :     case REF_SUBSTRING:
    3658                 :          13 :       if (!checker (ref->u.ss.start))
    3659                 :             :         return false;
    3660                 :          12 :       if (!checker (ref->u.ss.end))
    3661                 :             :         return false;
    3662                 :             :       break;
    3663                 :             : 
    3664                 :           0 :     default:
    3665                 :           0 :       gcc_unreachable ();
    3666                 :         862 :       break;
    3667                 :             :     }
    3668                 :             : 
    3669                 :         862 :   return check_references (ref->next, checker);
    3670                 :             : }
    3671                 :             : 
    3672                 :             : /*  Return true if ns is a parent of the current ns.  */
    3673                 :             : 
    3674                 :             : static bool
    3675                 :         520 : is_parent_of_current_ns (gfc_namespace *ns)
    3676                 :             : {
    3677                 :         520 :   gfc_namespace *p;
    3678                 :         548 :   for (p = gfc_current_ns->parent; p; p = p->parent)
    3679                 :         533 :     if (ns == p)
    3680                 :             :       return true;
    3681                 :             : 
    3682                 :             :   return false;
    3683                 :             : }
    3684                 :             : 
    3685                 :             : /* Verify that an expression is a restricted expression.  Like its
    3686                 :             :    cousin check_init_expr(), an error message is generated if we
    3687                 :             :    return false.  */
    3688                 :             : 
    3689                 :             : static bool
    3690                 :      435129 : check_restricted (gfc_expr *e)
    3691                 :             : {
    3692                 :      435129 :   gfc_symbol* sym;
    3693                 :      435129 :   bool t;
    3694                 :             : 
    3695                 :      435129 :   if (e == NULL)
    3696                 :             :     return true;
    3697                 :             : 
    3698                 :      432616 :   switch (e->expr_type)
    3699                 :             :     {
    3700                 :        2585 :     case EXPR_OP:
    3701                 :        2585 :       t = check_intrinsic_op (e, check_restricted);
    3702                 :        2585 :       if (t)
    3703                 :        2583 :         t = gfc_simplify_expr (e, 0);
    3704                 :             : 
    3705                 :             :       break;
    3706                 :             : 
    3707                 :           1 :     case EXPR_CONDITIONAL:
    3708                 :           1 :       t = check_restricted (e->value.conditional.condition);
    3709                 :           1 :       if (!t)
    3710                 :             :         break;
    3711                 :           1 :       t = check_restricted (e->value.conditional.true_expr);
    3712                 :           1 :       if (!t)
    3713                 :             :         break;
    3714                 :           1 :       t = check_restricted (e->value.conditional.false_expr);
    3715                 :           1 :       if (t)
    3716                 :           1 :         t = gfc_simplify_expr (e, 0);
    3717                 :             :       else
    3718                 :             :         t = false;
    3719                 :             :       break;
    3720                 :             : 
    3721                 :        3616 :     case EXPR_FUNCTION:
    3722                 :        3616 :       if (e->value.function.esym)
    3723                 :             :         {
    3724                 :         577 :           t = check_arglist (e->value.function.actual, &check_restricted);
    3725                 :         577 :           if (t)
    3726                 :         577 :             t = external_spec_function (e);
    3727                 :             :         }
    3728                 :             :       else
    3729                 :             :         {
    3730                 :        3039 :           if (e->value.function.isym && e->value.function.isym->inquiry)
    3731                 :             :             t = true;
    3732                 :             :           else
    3733                 :         751 :             t = check_arglist (e->value.function.actual, &check_restricted);
    3734                 :             : 
    3735                 :         751 :           if (t)
    3736                 :        3031 :             t = restricted_intrinsic (e);
    3737                 :             :         }
    3738                 :             :       break;
    3739                 :             : 
    3740                 :       15027 :     case EXPR_VARIABLE:
    3741                 :       15027 :       sym = e->symtree->n.sym;
    3742                 :       15027 :       t = false;
    3743                 :             : 
    3744                 :             :       /* If a dummy argument appears in a context that is valid for a
    3745                 :             :          restricted expression in an elemental procedure, it will have
    3746                 :             :          already been simplified away once we get here.  Therefore we
    3747                 :             :          don't need to jump through hoops to distinguish valid from
    3748                 :             :          invalid cases.  Allowed in F2008 and F2018.  */
    3749                 :       15027 :       if (gfc_notification_std (GFC_STD_F2008)
    3750                 :          40 :           && sym->attr.dummy && sym->ns == gfc_current_ns
    3751                 :       15067 :           && sym->ns->proc_name && sym->ns->proc_name->attr.elemental)
    3752                 :             :         {
    3753                 :           4 :           gfc_error_now ("Dummy argument %qs not "
    3754                 :             :                          "allowed in expression at %L",
    3755                 :             :                          sym->name, &e->where);
    3756                 :           4 :           break;
    3757                 :             :         }
    3758                 :             : 
    3759                 :       15023 :       if (sym->attr.optional)
    3760                 :             :         {
    3761                 :           2 :           gfc_error ("Dummy argument %qs at %L cannot be OPTIONAL",
    3762                 :             :                      sym->name, &e->where);
    3763                 :           2 :           break;
    3764                 :             :         }
    3765                 :             : 
    3766                 :       15021 :       if (sym->attr.intent == INTENT_OUT)
    3767                 :             :         {
    3768                 :           0 :           gfc_error ("Dummy argument %qs at %L cannot be INTENT(OUT)",
    3769                 :             :                      sym->name, &e->where);
    3770                 :           0 :           break;
    3771                 :             :         }
    3772                 :             : 
    3773                 :             :       /* Check reference chain if any.  */
    3774                 :       15021 :       if (!check_references (e->ref, &check_restricted))
    3775                 :             :         break;
    3776                 :             : 
    3777                 :       15018 :       if (e->error
    3778                 :       14998 :             || sym->attr.in_common
    3779                 :       14803 :             || sym->attr.use_assoc
    3780                 :       11557 :             || sym->attr.used_in_submodule
    3781                 :       11556 :             || sym->attr.dummy
    3782                 :         578 :             || sym->attr.implied_index
    3783                 :         578 :             || sym->attr.flavor == FL_PARAMETER
    3784                 :       16058 :             || is_parent_of_current_ns (gfc_get_spec_ns (sym)))
    3785                 :             :         {
    3786                 :             :           t = true;
    3787                 :             :           break;
    3788                 :             :         }
    3789                 :             : 
    3790                 :          15 :       gfc_error ("Variable %qs cannot appear in the expression at %L",
    3791                 :             :                  sym->name, &e->where);
    3792                 :             :       /* Prevent a repetition of the error.  */
    3793                 :          15 :       e->error = 1;
    3794                 :          15 :       break;
    3795                 :             : 
    3796                 :             :     case EXPR_NULL:
    3797                 :             :     case EXPR_CONSTANT:
    3798                 :             :       t = true;
    3799                 :             :       break;
    3800                 :             : 
    3801                 :           7 :     case EXPR_SUBSTRING:
    3802                 :           7 :       t = gfc_specification_expr (e->ref->u.ss.start);
    3803                 :           7 :       if (!t)
    3804                 :             :         break;
    3805                 :             : 
    3806                 :           6 :       t = gfc_specification_expr (e->ref->u.ss.end);
    3807                 :           6 :       if (t)
    3808                 :           6 :         t = gfc_simplify_expr (e, 0);
    3809                 :             : 
    3810                 :             :       break;
    3811                 :             : 
    3812                 :           6 :     case EXPR_STRUCTURE:
    3813                 :           6 :       t = gfc_check_constructor (e, check_restricted);
    3814                 :           6 :       break;
    3815                 :             : 
    3816                 :          58 :     case EXPR_ARRAY:
    3817                 :          58 :       t = gfc_check_constructor (e, check_restricted);
    3818                 :          58 :       break;
    3819                 :             : 
    3820                 :           0 :     default:
    3821                 :           0 :       gfc_internal_error ("check_restricted(): Unknown expression type");
    3822                 :             :     }
    3823                 :             : 
    3824                 :             :   return t;
    3825                 :             : }
    3826                 :             : 
    3827                 :             : 
    3828                 :             : /* Check to see that an expression is a specification expression.  If
    3829                 :             :    we return false, an error has been generated.  */
    3830                 :             : 
    3831                 :             : bool
    3832                 :      457068 : gfc_specification_expr (gfc_expr *e)
    3833                 :             : {
    3834                 :      457068 :   gfc_component *comp;
    3835                 :             : 
    3836                 :      457068 :   if (e == NULL)
    3837                 :             :     return true;
    3838                 :             : 
    3839                 :      423303 :   if (e->ts.type != BT_INTEGER)
    3840                 :             :     {
    3841                 :          26 :       gfc_error ("Expression at %L must be of INTEGER type, found %s",
    3842                 :             :                  &e->where, gfc_basic_typename (e->ts.type));
    3843                 :          26 :       return false;
    3844                 :             :     }
    3845                 :             : 
    3846                 :      423277 :   comp = gfc_get_proc_ptr_comp (e);
    3847                 :      423277 :   if (e->expr_type == EXPR_FUNCTION
    3848                 :        2373 :       && !e->value.function.isym
    3849                 :         384 :       && !e->value.function.esym
    3850                 :         103 :       && !gfc_pure (e->symtree->n.sym)
    3851                 :      423379 :       && (!comp || !comp->attr.pure))
    3852                 :             :     {
    3853                 :           3 :       gfc_error ("Function %qs at %L must be PURE",
    3854                 :           3 :                  e->symtree->n.sym->name, &e->where);
    3855                 :             :       /* Prevent repeat error messages.  */
    3856                 :           3 :       e->symtree->n.sym->attr.pure = 1;
    3857                 :           3 :       return false;
    3858                 :             :     }
    3859                 :             : 
    3860                 :      423274 :   if (e->rank != 0)
    3861                 :             :     {
    3862                 :           3 :       gfc_error ("Expression at %L must be scalar", &e->where);
    3863                 :           3 :       return false;
    3864                 :             :     }
    3865                 :             : 
    3866                 :      423271 :   if (!gfc_simplify_expr (e, 0))
    3867                 :             :     return false;
    3868                 :             : 
    3869                 :      423266 :   return check_restricted (e);
    3870                 :             : }
    3871                 :             : 
    3872                 :             : 
    3873                 :             : /************** Expression conformance checks.  *************/
    3874                 :             : 
    3875                 :             : /* Given two expressions, make sure that the arrays are conformable.  */
    3876                 :             : 
    3877                 :             : bool
    3878                 :      191669 : gfc_check_conformance (gfc_expr *op1, gfc_expr *op2, const char *optype_msgid, ...)
    3879                 :             : {
    3880                 :      191669 :   int op1_flag, op2_flag, d;
    3881                 :      191669 :   mpz_t op1_size, op2_size;
    3882                 :      191669 :   bool t;
    3883                 :             : 
    3884                 :      191669 :   va_list argp;
    3885                 :      191669 :   char buffer[240];
    3886                 :             : 
    3887                 :      191669 :   if (op1->rank == 0 || op2->rank == 0)
    3888                 :             :     return true;
    3889                 :             : 
    3890                 :       68343 :   va_start (argp, optype_msgid);
    3891                 :       68343 :   d = vsnprintf (buffer, sizeof (buffer), optype_msgid, argp);
    3892                 :       68343 :   va_end (argp);
    3893                 :       68343 :   if (d < 1 || d >= (int) sizeof (buffer)) /* Reject truncation.  */
    3894                 :           0 :     gfc_internal_error ("optype_msgid overflow: %d", d);
    3895                 :             : 
    3896                 :       68343 :   if (op1->rank != op2->rank)
    3897                 :             :     {
    3898                 :          34 :       gfc_error ("Incompatible ranks in %s (%d and %d) at %L", _(buffer),
    3899                 :             :                  op1->rank, op2->rank, &op1->where);
    3900                 :          34 :       return false;
    3901                 :             :     }
    3902                 :             : 
    3903                 :             :   t = true;
    3904                 :             : 
    3905                 :      165415 :   for (d = 0; d < op1->rank; d++)
    3906                 :             :     {
    3907                 :       97174 :       op1_flag = gfc_array_dimen_size(op1, d, &op1_size);
    3908                 :       97174 :       op2_flag = gfc_array_dimen_size(op2, d, &op2_size);
    3909                 :             : 
    3910                 :       97174 :       if (op1_flag && op2_flag && mpz_cmp (op1_size, op2_size) != 0)
    3911                 :             :         {
    3912                 :          68 :           gfc_error ("Different shape for %s at %L on dimension %d "
    3913                 :             :                      "(%d and %d)", _(buffer), &op1->where, d + 1,
    3914                 :          68 :                      (int) mpz_get_si (op1_size),
    3915                 :          68 :                      (int) mpz_get_si (op2_size));
    3916                 :             : 
    3917                 :          68 :           t = false;
    3918                 :             :         }
    3919                 :             : 
    3920                 :       97174 :       if (op1_flag)
    3921                 :       64692 :         mpz_clear (op1_size);
    3922                 :       97174 :       if (op2_flag)
    3923                 :       73572 :         mpz_clear (op2_size);
    3924                 :             : 
    3925                 :       97174 :       if (!t)
    3926                 :             :         return false;
    3927                 :             :     }
    3928                 :             : 
    3929                 :             :   return true;
    3930                 :             : }
    3931                 :             : 
    3932                 :             : 
    3933                 :             : /* Given an assignable expression and an arbitrary expression, make
    3934                 :             :    sure that the assignment can take place.  Only add a call to the intrinsic
    3935                 :             :    conversion routines, when allow_convert is set.  When this assign is a
    3936                 :             :    coarray call, then the convert is done by the coarray routine implicitly and
    3937                 :             :    adding the intrinsic conversion would do harm in most cases.  */
    3938                 :             : 
    3939                 :             : bool
    3940                 :      753221 : gfc_check_assign (gfc_expr *lvalue, gfc_expr *rvalue, int conform,
    3941                 :             :                   bool allow_convert)
    3942                 :             : {
    3943                 :      753221 :   gfc_symbol *sym;
    3944                 :      753221 :   gfc_ref *ref;
    3945                 :      753221 :   int has_pointer;
    3946                 :             : 
    3947                 :      753221 :   sym = lvalue->symtree->n.sym;
    3948                 :             : 
    3949                 :             :   /* See if this is the component or subcomponent of a pointer and guard
    3950                 :             :      against assignment to LEN or KIND part-refs.  */
    3951                 :      753221 :   has_pointer = sym->attr.pointer;
    3952                 :      880512 :   for (ref = lvalue->ref; ref; ref = ref->next)
    3953                 :             :     {
    3954                 :      127291 :       if (!has_pointer && ref->type == REF_COMPONENT
    3955                 :       38084 :           && ref->u.c.component->attr.pointer)
    3956                 :             :         has_pointer = 1;
    3957                 :      126360 :       else if (ref->type == REF_INQUIRY
    3958                 :          92 :                && (ref->u.i == INQUIRY_LEN || ref->u.i == INQUIRY_KIND))
    3959                 :             :         {
    3960                 :           0 :           gfc_error ("Assignment to a LEN or KIND part_ref at %L is not "
    3961                 :             :                      "allowed", &lvalue->where);
    3962                 :           0 :           return false;
    3963                 :             :         }
    3964                 :             :     }
    3965                 :             : 
    3966                 :             :   /* 12.5.2.2, Note 12.26: The result variable is very similar to any other
    3967                 :             :      variable local to a function subprogram.  Its existence begins when
    3968                 :             :      execution of the function is initiated and ends when execution of the
    3969                 :             :      function is terminated...
    3970                 :             :      Therefore, the left hand side is no longer a variable, when it is:  */
    3971                 :      753221 :   if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_ST_FUNCTION
    3972                 :        8220 :       && !sym->attr.external)
    3973                 :             :     {
    3974                 :        8210 :       bool bad_proc;
    3975                 :        8210 :       bad_proc = false;
    3976                 :             : 
    3977                 :             :       /* (i) Use associated;  */
    3978                 :        8210 :       if (sym->attr.use_assoc)
    3979                 :           0 :         bad_proc = true;
    3980                 :             : 
    3981                 :             :       /* (ii) The assignment is in the main program; or  */
    3982                 :        8210 :       if (gfc_current_ns->proc_name
    3983                 :        8209 :           && gfc_current_ns->proc_name->attr.is_main_program)
    3984                 :        8210 :         bad_proc = true;
    3985                 :             : 
    3986                 :             :       /* (iii) A module or internal procedure...  */
    3987                 :        8210 :       if (gfc_current_ns->proc_name
    3988                 :        8209 :           && (gfc_current_ns->proc_name->attr.proc == PROC_INTERNAL
    3989                 :        4635 :               || gfc_current_ns->proc_name->attr.proc == PROC_MODULE)
    3990                 :        5813 :           && gfc_current_ns->parent
    3991                 :        5332 :           && (!(gfc_current_ns->parent->proc_name->attr.function
    3992                 :        5179 :                 || gfc_current_ns->parent->proc_name->attr.subroutine)
    3993                 :        2852 :               || gfc_current_ns->parent->proc_name->attr.is_main_program))
    3994                 :             :         {
    3995                 :             :           /* ... that is not a function...  */
    3996                 :        4868 :           if (gfc_current_ns->proc_name
    3997                 :        4868 :               && !gfc_current_ns->proc_name->attr.function)
    3998                 :           0 :             bad_proc = true;
    3999                 :             : 
    4000                 :             :           /* ... or is not an entry and has a different name.  */
    4001                 :        4868 :           if (!sym->attr.entry && sym->name != gfc_current_ns->proc_name->name)
    4002                 :        8210 :             bad_proc = true;
    4003                 :             :         }
    4004                 :             : 
    4005                 :             :       /* (iv) Host associated and not the function symbol or the
    4006                 :             :               parent result.  This picks up sibling references, which
    4007                 :             :               cannot be entries.  */
    4008                 :        8210 :       if (!sym->attr.entry
    4009                 :        7472 :             && sym->ns == gfc_current_ns->parent
    4010                 :        5089 :             && sym != gfc_current_ns->proc_name
    4011                 :          72 :             && sym != gfc_current_ns->parent->proc_name->result)
    4012                 :             :         bad_proc = true;
    4013                 :             : 
    4014                 :        8209 :       if (bad_proc)
    4015                 :             :         {
    4016                 :           1 :           gfc_error ("%qs at %L is not a VALUE", sym->name, &lvalue->where);
    4017                 :           1 :           return false;
    4018                 :             :         }
    4019                 :             :     }
    4020                 :             :   else
    4021                 :             :     {
    4022                 :             :       /* Reject assigning to an external symbol.  For initializers, this
    4023                 :             :          was already done before, in resolve_fl_procedure.  */
    4024                 :      745011 :       if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
    4025                 :          10 :           && sym->attr.proc != PROC_MODULE && !rvalue->error)
    4026                 :             :         {
    4027                 :           2 :           gfc_error ("Illegal assignment to external procedure at %L",
    4028                 :             :                      &lvalue->where);
    4029                 :           2 :           return false;
    4030                 :             :         }
    4031                 :             :     }
    4032                 :             : 
    4033                 :      753218 :   if (rvalue->rank != 0 && lvalue->rank != rvalue->rank)
    4034                 :             :     {
    4035                 :          25 :       gfc_error ("Incompatible ranks %d and %d in assignment at %L",
    4036                 :             :                  lvalue->rank, rvalue->rank, &lvalue->where);
    4037                 :          25 :       return false;
    4038                 :             :     }
    4039                 :             : 
    4040                 :      753193 :   if (lvalue->ts.type == BT_UNKNOWN)
    4041                 :             :     {
    4042                 :           0 :       gfc_error ("Variable type is UNKNOWN in assignment at %L",
    4043                 :             :                  &lvalue->where);
    4044                 :           0 :       return false;
    4045                 :             :     }
    4046                 :             : 
    4047                 :      753193 :   if (rvalue->expr_type == EXPR_NULL)
    4048                 :             :     {
    4049                 :          19 :       if (has_pointer && (ref == NULL || ref->next == NULL)
    4050                 :           8 :           && lvalue->symtree->n.sym->attr.data)
    4051                 :             :         return true;
    4052                 :             :       /* Prevent the following error message for caf-single mode, because there
    4053                 :             :          are no teams in single mode and the simplify returns a null then.  */
    4054                 :          12 :       else if (!(flag_coarray == GFC_FCOARRAY_SINGLE
    4055                 :           9 :                  && rvalue->ts.type == BT_DERIVED
    4056                 :           9 :                  && rvalue->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    4057                 :           9 :                  && rvalue->ts.u.derived->intmod_sym_id
    4058                 :             :                       == ISOFORTRAN_TEAM_TYPE))
    4059                 :             :         {
    4060                 :           3 :           gfc_error ("NULL appears on right-hand side in assignment at %L",
    4061                 :             :                      &rvalue->where);
    4062                 :           3 :           return false;
    4063                 :             :         }
    4064                 :             :     }
    4065                 :             : 
    4066                 :             :   /* This is possibly a typo: x = f() instead of x => f().  */
    4067                 :      753183 :   if (warn_surprising
    4068                 :      753183 :       && rvalue->expr_type == EXPR_FUNCTION && gfc_expr_attr (rvalue).pointer)
    4069                 :           6 :     gfc_warning (OPT_Wsurprising,
    4070                 :             :                  "POINTER-valued function appears on right-hand side of "
    4071                 :             :                  "assignment at %L", &rvalue->where);
    4072                 :             : 
    4073                 :             :   /* Check size of array assignments.  */
    4074                 :       74917 :   if (lvalue->rank != 0 && rvalue->rank != 0
    4075                 :      802539 :       && !gfc_check_conformance (lvalue, rvalue, _("array assignment")))
    4076                 :             :     return false;
    4077                 :             : 
    4078                 :             :   /* Handle the case of a BOZ literal on the RHS.  */
    4079                 :      753151 :   if (rvalue->ts.type == BT_BOZ)
    4080                 :             :     {
    4081                 :         241 :       if (lvalue->symtree->n.sym->attr.data)
    4082                 :             :         {
    4083                 :          93 :           if (lvalue->ts.type == BT_INTEGER
    4084                 :          93 :               && gfc_boz2int (rvalue, lvalue->ts.kind))
    4085                 :             :             return true;
    4086                 :             : 
    4087                 :           2 :           if (lvalue->ts.type == BT_REAL
    4088                 :           2 :               && gfc_boz2real (rvalue, lvalue->ts.kind))
    4089                 :             :             {
    4090                 :           2 :               if (gfc_invalid_boz ("BOZ literal constant near %L cannot "
    4091                 :             :                                    "be assigned to a REAL variable",
    4092                 :             :                                    &rvalue->where))
    4093                 :             :                 return false;
    4094                 :             :               return true;
    4095                 :             :             }
    4096                 :             :         }
    4097                 :             : 
    4098                 :         148 :       if (!lvalue->symtree->n.sym->attr.data
    4099                 :         148 :           && gfc_invalid_boz ("BOZ literal constant at %L is neither a "
    4100                 :             :                               "data-stmt-constant nor an actual argument to "
    4101                 :             :                               "INT, REAL, DBLE, or CMPLX intrinsic function",
    4102                 :             :                               &rvalue->where))
    4103                 :             :         return false;
    4104                 :             : 
    4105                 :         148 :       if (lvalue->ts.type == BT_INTEGER
    4106                 :         148 :           && gfc_boz2int (rvalue, lvalue->ts.kind))
    4107                 :             :         return true;
    4108                 :             : 
    4109                 :           1 :       if (lvalue->ts.type == BT_REAL
    4110                 :           1 :           && gfc_boz2real (rvalue, lvalue->ts.kind))
    4111                 :             :         return true;
    4112                 :             : 
    4113                 :           0 :       gfc_error ("BOZ literal constant near %L cannot be assigned to a "
    4114                 :             :                  "%qs variable", &rvalue->where, gfc_typename (lvalue));
    4115                 :           0 :       return false;
    4116                 :             :     }
    4117                 :             : 
    4118                 :      752910 :   if (gfc_expr_attr (lvalue).pdt_kind || gfc_expr_attr (lvalue).pdt_len)
    4119                 :             :     {
    4120                 :           0 :       gfc_error ("The assignment to a KIND or LEN component of a "
    4121                 :             :                  "parameterized type at %L is not allowed",
    4122                 :             :                  &lvalue->where);
    4123                 :           0 :       return false;
    4124                 :             :     }
    4125                 :             : 
    4126                 :      752910 :   if (gfc_compare_types (&lvalue->ts, &rvalue->ts))
    4127                 :             :     return true;
    4128                 :             : 
    4129                 :             :   /* Only DATA Statements come here.  */
    4130                 :       18922 :   if (!conform)
    4131                 :             :     {
    4132                 :        1524 :       locus *where;
    4133                 :             : 
    4134                 :             :       /* Numeric can be converted to any other numeric. And Hollerith can be
    4135                 :             :          converted to any other type.  */
    4136                 :        2817 :       if ((gfc_numeric_ts (&lvalue->ts) && gfc_numeric_ts (&rvalue->ts))
    4137                 :        2127 :           || rvalue->ts.type == BT_HOLLERITH)
    4138                 :        1145 :         return true;
    4139                 :             : 
    4140                 :         364 :       if (flag_dec_char_conversions && (gfc_numeric_ts (&lvalue->ts)
    4141                 :          91 :           || lvalue->ts.type == BT_LOGICAL)
    4142                 :         364 :           && rvalue->ts.type == BT_CHARACTER
    4143                 :         743 :           && rvalue->ts.kind == gfc_default_character_kind)
    4144                 :             :         return true;
    4145                 :             : 
    4146                 :          19 :       if (lvalue->ts.type == BT_LOGICAL && rvalue->ts.type == BT_LOGICAL)
    4147                 :             :         return true;
    4148                 :             : 
    4149                 :          18 :       where = (GFC_LOCUS_IS_SET (lvalue->where)
    4150                 :          18 :                ? &lvalue->where : &rvalue->where);
    4151                 :          18 :       gfc_error ("Incompatible types in DATA statement at %L; attempted "
    4152                 :             :                  "conversion of %s to %s", where,
    4153                 :             :                  gfc_typename (rvalue), gfc_typename (lvalue));
    4154                 :             : 
    4155                 :          18 :       return false;
    4156                 :             :     }
    4157                 :             : 
    4158                 :             :   /* Assignment is the only case where character variables of different
    4159                 :             :      kind values can be converted into one another.  */
    4160                 :       17398 :   if (lvalue->ts.type == BT_CHARACTER && rvalue->ts.type == BT_CHARACTER)
    4161                 :             :     {
    4162                 :         366 :       if (lvalue->ts.kind != rvalue->ts.kind && allow_convert)
    4163                 :         366 :         return gfc_convert_chartype (rvalue, &lvalue->ts);
    4164                 :             :       else
    4165                 :             :         return true;
    4166                 :             :     }
    4167                 :             : 
    4168                 :       17032 :   if (!allow_convert)
    4169                 :             :     return true;
    4170                 :             : 
    4171                 :       17032 :   return gfc_convert_type (rvalue, &lvalue->ts, 1);
    4172                 :             : }
    4173                 :             : 
    4174                 :             : 
    4175                 :             : /* Check that a pointer assignment is OK.  We first check lvalue, and
    4176                 :             :    we only check rvalue if it's not an assignment to NULL() or a
    4177                 :             :    NULLIFY statement.  */
    4178                 :             : 
    4179                 :             : bool
    4180                 :       15113 : gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue,
    4181                 :             :                           bool suppress_type_test, bool is_init_expr)
    4182                 :             : {
    4183                 :       15113 :   symbol_attribute attr, lhs_attr;
    4184                 :       15113 :   gfc_ref *ref;
    4185                 :       15113 :   bool is_pure, is_implicit_pure, rank_remap;
    4186                 :       15113 :   int proc_pointer;
    4187                 :       15113 :   bool same_rank;
    4188                 :             : 
    4189                 :       15113 :   if (!lvalue->symtree)
    4190                 :             :     return false;
    4191                 :             : 
    4192                 :       15112 :   lhs_attr = gfc_expr_attr (lvalue);
    4193                 :       15112 :   if (lvalue->ts.type == BT_UNKNOWN && !lhs_attr.proc_pointer)
    4194                 :             :     {
    4195                 :           0 :       gfc_error ("Pointer assignment target is not a POINTER at %L",
    4196                 :             :                  &lvalue->where);
    4197                 :           0 :       return false;
    4198                 :             :     }
    4199                 :             : 
    4200                 :       15112 :   if (lhs_attr.flavor == FL_PROCEDURE && lhs_attr.use_assoc
    4201                 :          36 :       && !lhs_attr.proc_pointer)
    4202                 :             :     {
    4203                 :           0 :       gfc_error ("%qs in the pointer assignment at %L cannot be an "
    4204                 :             :                  "l-value since it is a procedure",
    4205                 :           0 :                  lvalue->symtree->n.sym->name, &lvalue->where);
    4206                 :           0 :       return false;
    4207                 :             :     }
    4208                 :             : 
    4209                 :       15112 :   proc_pointer = lvalue->symtree->n.sym->attr.proc_pointer;
    4210                 :             : 
    4211                 :       15112 :   rank_remap = false;
    4212                 :       15112 :   same_rank = lvalue->rank == rvalue->rank;
    4213                 :       21590 :   for (ref = lvalue->ref; ref; ref = ref->next)
    4214                 :             :     {
    4215                 :       10442 :       if (ref->type == REF_COMPONENT)
    4216                 :        5712 :         proc_pointer = ref->u.c.component->attr.proc_pointer;
    4217                 :             : 
    4218                 :       10442 :       if (ref->type == REF_ARRAY && ref->next == NULL)
    4219                 :             :         {
    4220                 :        4282 :           int dim;
    4221                 :             : 
    4222                 :        4282 :           if (ref->u.ar.type == AR_FULL)
    4223                 :             :             break;
    4224                 :             : 
    4225                 :         329 :           if (ref->u.ar.type != AR_SECTION)
    4226                 :             :             {
    4227                 :           2 :               gfc_error ("Expected bounds specification for %qs at %L",
    4228                 :           2 :                          lvalue->symtree->n.sym->name, &lvalue->where);
    4229                 :           2 :               return false;
    4230                 :             :             }
    4231                 :             : 
    4232                 :         327 :           if (!gfc_notify_std (GFC_STD_F2003, "Bounds specification "
    4233                 :             :                                "for %qs in pointer assignment at %L",
    4234                 :         327 :                                lvalue->symtree->n.sym->name, &lvalue->where))
    4235                 :             :             return false;
    4236                 :             : 
    4237                 :             :           /* Fortran standard (e.g. F2018, 10.2.2 Pointer assignment):
    4238                 :             :            *
    4239                 :             :            * (C1017) If bounds-spec-list is specified, the number of
    4240                 :             :            * bounds-specs shall equal the rank of data-pointer-object.
    4241                 :             :            *
    4242                 :             :            * If bounds-spec-list appears, it specifies the lower bounds.
    4243                 :             :            *
    4244                 :             :            * (C1018) If bounds-remapping-list is specified, the number of
    4245                 :             :            * bounds-remappings shall equal the rank of data-pointer-object.
    4246                 :             :            *
    4247                 :             :            * If bounds-remapping-list appears, it specifies the upper and
    4248                 :             :            * lower bounds of each dimension of the pointer; the pointer target
    4249                 :             :            * shall be simply contiguous or of rank one.
    4250                 :             :            *
    4251                 :             :            * (C1019) If bounds-remapping-list is not specified, the ranks of
    4252                 :             :            * data-pointer-object and data-target shall be the same.
    4253                 :             :            *
    4254                 :             :            * Thus when bounds are given, all lbounds are necessary and either
    4255                 :             :            * all or none of the upper bounds; no strides are allowed.  If the
    4256                 :             :            * upper bounds are present, we may do rank remapping.  */
    4257                 :         870 :           for (dim = 0; dim < ref->u.ar.dimen; ++dim)
    4258                 :             :             {
    4259                 :         552 :               if (ref->u.ar.stride[dim])
    4260                 :             :                 {
    4261                 :           1 :                   gfc_error ("Stride must not be present at %L",
    4262                 :             :                              &lvalue->where);
    4263                 :           1 :                   return false;
    4264                 :             :                 }
    4265                 :         551 :               if (!same_rank && (!ref->u.ar.start[dim] ||!ref->u.ar.end[dim]))
    4266                 :             :                 {
    4267                 :           3 :                   gfc_error ("Rank remapping requires a "
    4268                 :             :                              "list of %<lower-bound : upper-bound%> "
    4269                 :             :                              "specifications at %L", &lvalue->where);
    4270                 :           3 :                   return false;
    4271                 :             :                 }
    4272                 :         548 :               if (!ref->u.ar.start[dim]
    4273                 :         547 :                   || ref->u.ar.dimen_type[dim] != DIMEN_RANGE)
    4274                 :             :                 {
    4275                 :           2 :                   gfc_error ("Expected list of %<lower-bound :%> or "
    4276                 :             :                              "list of %<lower-bound : upper-bound%> "
    4277                 :             :                              "specifications at %L", &lvalue->where);
    4278                 :           2 :                   return false;
    4279                 :             :                 }
    4280                 :             : 
    4281                 :         546 :               if (dim == 0)
    4282                 :         319 :                 rank_remap = (ref->u.ar.end[dim] != NULL);
    4283                 :             :               else
    4284                 :             :                 {
    4285                 :         227 :                   if ((rank_remap && !ref->u.ar.end[dim]))
    4286                 :             :                     {
    4287                 :           0 :                       gfc_error ("Rank remapping requires a "
    4288                 :             :                                  "list of %<lower-bound : upper-bound%> "
    4289                 :             :                                  "specifications at %L", &lvalue->where);
    4290                 :           0 :                       return false;
    4291                 :             :                     }
    4292                 :         102 :                   if (!rank_remap && ref->u.ar.end[dim])
    4293                 :             :                     {
    4294                 :           0 :                       gfc_error ("Expected list of %<lower-bound :%> or "
    4295                 :             :                                  "list of %<lower-bound : upper-bound%> "
    4296                 :             :                                  "specifications at %L", &lvalue->where);
    4297                 :           0 :                       return false;
    4298                 :             :                     }
    4299                 :             :                 }
    4300                 :             :             }
    4301                 :             :         }
    4302                 :             :     }
    4303                 :             : 
    4304                 :       15101 :   is_pure = gfc_pure (NULL);
    4305                 :       15101 :   is_implicit_pure = gfc_implicit_pure (NULL);
    4306                 :             : 
    4307                 :             :   /* If rvalue is a NULL() or NULLIFY, we're done. Otherwise the type,
    4308                 :             :      kind, etc for lvalue and rvalue must match, and rvalue must be a
    4309                 :             :      pure variable if we're in a pure function.  */
    4310                 :       15101 :   if (rvalue->expr_type == EXPR_NULL && rvalue->ts.type == BT_UNKNOWN)
    4311                 :             :     return true;
    4312                 :             : 
    4313                 :             :   /* F2008, C723 (pointer) and C726 (proc-pointer); for PURE also C1283.  */
    4314                 :        8523 :   if (lvalue->expr_type == EXPR_VARIABLE
    4315                 :        8523 :       && gfc_is_coindexed (lvalue))
    4316                 :             :     {
    4317                 :           5 :       gfc_ref *ref;
    4318                 :           6 :       for (ref = lvalue->ref; ref; ref = ref->next)
    4319                 :           6 :         if (ref->type == REF_ARRAY && ref->u.ar.codimen)
    4320                 :             :           {
    4321                 :           5 :             gfc_error ("Pointer object at %L shall not have a coindex",
    4322                 :             :                        &lvalue->where);
    4323                 :           5 :             return false;
    4324                 :             :           }
    4325                 :             :     }
    4326                 :             : 
    4327                 :             :   /* Checks on rvalue for procedure pointer assignments.  */
    4328                 :        8518 :   if (proc_pointer)
    4329                 :             :     {
    4330                 :        1246 :       char err[200];
    4331                 :        1246 :       gfc_symbol *s1,*s2;
    4332                 :        1246 :       gfc_component *comp1, *comp2;
    4333                 :        1246 :       const char *name;
    4334                 :             : 
    4335                 :        1246 :       attr = gfc_expr_attr (rvalue);
    4336                 :        2251 :       if (!((rvalue->expr_type == EXPR_NULL)
    4337                 :        1240 :             || (rvalue->expr_type == EXPR_FUNCTION && attr.proc_pointer)
    4338                 :        1119 :             || (rvalue->expr_type == EXPR_VARIABLE && attr.proc_pointer)
    4339                 :             :             || (rvalue->expr_type == EXPR_VARIABLE
    4340                 :        1003 :                 && attr.flavor == FL_PROCEDURE)))
    4341                 :             :         {
    4342                 :           6 :           gfc_error ("Invalid procedure pointer assignment at %L",
    4343                 :             :                      &rvalue->where);
    4344                 :           6 :           return false;
    4345                 :             :         }
    4346                 :             : 
    4347                 :        1240 :       if (rvalue->expr_type == EXPR_VARIABLE && !attr.proc_pointer)
    4348                 :             :         {
    4349                 :             :           /* Check for intrinsics.  */
    4350                 :         999 :           gfc_symbol *sym = rvalue->symtree->n.sym;
    4351                 :         999 :           if (!sym->attr.intrinsic
    4352                 :         999 :               && (gfc_is_intrinsic (sym, 0, sym->declared_at)
    4353                 :         872 :                   || gfc_is_intrinsic (sym, 1, sym->declared_at)))
    4354                 :             :             {
    4355                 :          37 :               sym->attr.intrinsic = 1;
    4356                 :          37 :               gfc_resolve_intrinsic (sym, &rvalue->where);
    4357                 :          37 :               attr = gfc_expr_attr (rvalue);
    4358                 :             :             }
    4359                 :             :           /* Check for result of embracing function.  */
    4360                 :         999 :           if (sym->attr.function && sym->result == sym)
    4361                 :             :             {
    4362                 :         373 :               gfc_namespace *ns;
    4363                 :             : 
    4364                 :         819 :               for (ns = gfc_current_ns; ns; ns = ns->parent)
    4365                 :         450 :                 if (sym == ns->proc_name)
    4366                 :             :                   {
    4367                 :           4 :                     gfc_error ("Function result %qs is invalid as proc-target "
    4368                 :             :                                "in procedure pointer assignment at %L",
    4369                 :             :                                sym->name, &rvalue->where);
    4370                 :           4 :                     return false;
    4371                 :             :                   }
    4372                 :             :             }
    4373                 :             :         }
    4374                 :        1236 :       if (attr.abstract)
    4375                 :             :         {
    4376                 :           1 :           gfc_error ("Abstract interface %qs is invalid "
    4377                 :             :                      "in procedure pointer assignment at %L",
    4378                 :           1 :                      rvalue->symtree->name, &rvalue->where);
    4379                 :           1 :           return false;
    4380                 :             :         }
    4381                 :             :       /* Check for F08:C729.  */
    4382                 :        1235 :       if (attr.flavor == FL_PROCEDURE)
    4383                 :             :         {
    4384                 :        1229 :           if (attr.proc == PROC_ST_FUNCTION)
    4385                 :             :             {
    4386                 :           1 :               gfc_error ("Statement function %qs is invalid "
    4387                 :             :                          "in procedure pointer assignment at %L",
    4388                 :           1 :                          rvalue->symtree->name, &rvalue->where);
    4389                 :           1 :               return false;
    4390                 :             :             }
    4391                 :        1559 :           if (attr.proc == PROC_INTERNAL &&
    4392                 :         331 :               !gfc_notify_std(GFC_STD_F2008, "Internal procedure %qs "
    4393                 :             :                               "is invalid in procedure pointer assignment "
    4394                 :         331 :                               "at %L", rvalue->symtree->name, &rvalue->where))
    4395                 :             :             return false;
    4396                 :        1354 :           if (attr.intrinsic && gfc_intrinsic_actual_ok (rvalue->symtree->name,
    4397                 :         127 :                                                          attr.subroutine) == 0)
    4398                 :             :             {
    4399                 :           1 :               gfc_error ("Intrinsic %qs at %L is invalid in procedure pointer "
    4400                 :           1 :                          "assignment", rvalue->symtree->name, &rvalue->where);
    4401                 :           1 :               return false;
    4402                 :             :             }
    4403                 :             :         }
    4404                 :             :       /* Check for F08:C730.  */
    4405                 :        1232 :       if (attr.elemental && !attr.intrinsic)
    4406                 :             :         {
    4407                 :           1 :           gfc_error ("Nonintrinsic elemental procedure %qs is invalid "
    4408                 :             :                      "in procedure pointer assignment at %L",
    4409                 :           1 :                      rvalue->symtree->name, &rvalue->where);
    4410                 :           1 :           return false;
    4411                 :             :         }
    4412                 :             : 
    4413                 :             :       /* Ensure that the calling convention is the same. As other attributes
    4414                 :             :          such as DLLEXPORT may differ, one explicitly only tests for the
    4415                 :             :          calling conventions.  */
    4416                 :        1231 :       if (rvalue->expr_type == EXPR_VARIABLE
    4417                 :        1104 :           && lvalue->symtree->n.sym->attr.ext_attr
    4418                 :        1104 :                != rvalue->symtree->n.sym->attr.ext_attr)
    4419                 :             :         {
    4420                 :          10 :           symbol_attribute calls;
    4421                 :             : 
    4422                 :          10 :           calls.ext_attr = 0;
    4423                 :          10 :           gfc_add_ext_attribute (&calls, EXT_ATTR_CDECL, NULL);
    4424                 :          10 :           gfc_add_ext_attribute (&calls, EXT_ATTR_STDCALL, NULL);
    4425                 :          10 :           gfc_add_ext_attribute (&calls, EXT_ATTR_FASTCALL, NULL);
    4426                 :             : 
    4427                 :          10 :           if ((calls.ext_attr & lvalue->symtree->n.sym->attr.ext_attr)
    4428                 :          10 :               != (calls.ext_attr & rvalue->symtree->n.sym->attr.ext_attr))
    4429                 :             :             {
    4430                 :          10 :               gfc_error ("Mismatch in the procedure pointer assignment "
    4431                 :             :                          "at %L: mismatch in the calling convention",
    4432                 :             :                          &rvalue->where);
    4433                 :          10 :           return false;
    4434                 :             :             }
    4435                 :             :         }
    4436                 :             : 
    4437                 :        1221 :       comp1 = gfc_get_proc_ptr_comp (lvalue);
    4438                 :        1221 :       if (comp1)
    4439                 :         381 :         s1 = comp1->ts.interface;
    4440                 :             :       else
    4441                 :             :         {
    4442                 :         840 :           s1 = lvalue->symtree->n.sym;
    4443                 :         840 :           if (s1->ts.interface)
    4444                 :         635 :             s1 = s1->ts.interface;
    4445                 :             :         }
    4446                 :             : 
    4447                 :        1221 :       comp2 = gfc_get_proc_ptr_comp (rvalue);
    4448                 :        1221 :       if (comp2)
    4449                 :             :         {
    4450                 :          67 :           if (rvalue->expr_type == EXPR_FUNCTION)
    4451                 :             :             {
    4452                 :           6 :               s2 = comp2->ts.interface->result;
    4453                 :           6 :               name = s2->name;
    4454                 :             :             }
    4455                 :             :           else
    4456                 :             :             {
    4457                 :          61 :               s2 = comp2->ts.interface;
    4458                 :          61 :               name = comp2->name;
    4459                 :             :             }
    4460                 :             :         }
    4461                 :        1154 :       else if (rvalue->expr_type == EXPR_FUNCTION)
    4462                 :             :         {
    4463                 :         115 :           if (rvalue->value.function.esym)
    4464                 :         115 :             s2 = rvalue->value.function.esym->result;
    4465                 :             :           else
    4466                 :           0 :             s2 = rvalue->symtree->n.sym->result;
    4467                 :             : 
    4468                 :         115 :           name = s2->name;
    4469                 :             :         }
    4470                 :             :       else
    4471                 :             :         {
    4472                 :        1039 :           s2 = rvalue->symtree->n.sym;
    4473                 :        1039 :           name = s2->name;
    4474                 :             :         }
    4475                 :             : 
    4476                 :        1221 :       if (s2 && s2->attr.proc_pointer && s2->ts.interface)
    4477                 :        1221 :         s2 = s2->ts.interface;
    4478                 :             : 
    4479                 :             :       /* Special check for the case of absent interface on the lvalue.
    4480                 :             :        * All other interface checks are done below. */
    4481                 :        1221 :       if (!s1 && comp1 && comp1->attr.subroutine && s2 && s2->attr.function)
    4482                 :             :         {
    4483                 :           1 :           gfc_error ("Interface mismatch in procedure pointer assignment "
    4484                 :             :                      "at %L: %qs is not a subroutine", &rvalue->where, name);
    4485                 :           1 :           return false;
    4486                 :             :         }
    4487                 :             : 
    4488                 :             :       /* F08:7.2.2.4 (4)  */
    4489                 :        1218 :       if (s2 && gfc_explicit_interface_required (s2, err, sizeof(err)))
    4490                 :             :         {
    4491                 :         250 :           if (comp1 && !s1)
    4492                 :             :             {
    4493                 :           2 :               gfc_error ("Explicit interface required for component %qs at %L: %s",
    4494                 :             :                          comp1->name, &lvalue->where, err);
    4495                 :           2 :               return false;
    4496                 :             :             }
    4497                 :         248 :           else if (s1->attr.if_source == IFSRC_UNKNOWN)
    4498                 :             :             {
    4499                 :           2 :               gfc_error ("Explicit interface required for %qs at %L: %s",
    4500                 :             :                          s1->name, &lvalue->where, err);
    4501                 :           2 :               return false;
    4502                 :             :             }
    4503                 :             :         }
    4504                 :        1216 :       if (s1 && gfc_explicit_interface_required (s1, err, sizeof(err)))
    4505                 :             :         {
    4506                 :         262 :           if (comp2 && !s2)
    4507                 :             :             {
    4508                 :           2 :               gfc_error ("Explicit interface required for component %qs at %L: %s",
    4509                 :             :                          comp2->name, &rvalue->where, err);
    4510                 :           2 :               return false;
    4511                 :             :             }
    4512                 :         260 :           else if (s2->attr.if_source == IFSRC_UNKNOWN)
    4513                 :             :             {
    4514                 :           2 :               gfc_error ("Explicit interface required for %qs at %L: %s",
    4515                 :             :                          s2->name, &rvalue->where, err);
    4516                 :           2 :               return false;
    4517                 :             :             }
    4518                 :             :         }
    4519                 :             : 
    4520                 :        1212 :       if (s1 == s2 || !s1 || !s2)
    4521                 :             :         return true;
    4522                 :             : 
    4523                 :         716 :       if (!gfc_compare_interfaces (s1, s2, name, 0, 1,
    4524                 :             :                                    err, sizeof(err), NULL, NULL))
    4525                 :             :         {
    4526                 :          23 :           gfc_error ("Interface mismatch in procedure pointer assignment "
    4527                 :             :                      "at %L: %s", &rvalue->where, err);
    4528                 :          23 :           return false;
    4529                 :             :         }
    4530                 :             : 
    4531                 :             :       /* Check F2008Cor2, C729.  */
    4532                 :         693 :       if (!s2->attr.intrinsic && s2->attr.if_source == IFSRC_UNKNOWN
    4533                 :         102 :           && !s2->attr.external && !s2->attr.subroutine && !s2->attr.function)
    4534                 :             :         {
    4535                 :           1 :           gfc_error ("Procedure pointer target %qs at %L must be either an "
    4536                 :             :                      "intrinsic, host or use associated, referenced or have "
    4537                 :             :                      "the EXTERNAL attribute", s2->name, &rvalue->where);
    4538                 :           1 :           return false;
    4539                 :             :         }
    4540                 :             : 
    4541                 :             :       return true;
    4542                 :             :     }
    4543                 :             :   else
    4544                 :             :     {
    4545                 :             :       /* A non-proc pointer cannot point to a constant.  */
    4546                 :        7272 :       if (rvalue->expr_type == EXPR_CONSTANT)
    4547                 :             :         {
    4548                 :           2 :           gfc_error_now ("Pointer assignment target cannot be a constant at %L",
    4549                 :             :                          &rvalue->where);
    4550                 :           2 :           return false;
    4551                 :             :         }
    4552                 :             :     }
    4553                 :             : 
    4554                 :        7270 :   if (!gfc_compare_types (&lvalue->ts, &rvalue->ts))
    4555                 :             :     {
    4556                 :             :       /* Check for F03:C717.  */
    4557                 :          11 :       if (UNLIMITED_POLY (rvalue)
    4558                 :           1 :           && !(UNLIMITED_POLY (lvalue)
    4559                 :           1 :                || (lvalue->ts.type == BT_DERIVED
    4560                 :           0 :                    && (lvalue->ts.u.derived->attr.is_bind_c
    4561                 :           0 :                        || lvalue->ts.u.derived->attr.sequence))))
    4562                 :           1 :         gfc_error ("Data-pointer-object at %L must be unlimited "
    4563                 :             :                    "polymorphic, or of a type with the BIND or SEQUENCE "
    4564                 :             :                    "attribute, to be compatible with an unlimited "
    4565                 :             :                    "polymorphic target", &lvalue->where);
    4566                 :          10 :       else if (!suppress_type_test)
    4567                 :           8 :         gfc_error ("Different types in pointer assignment at %L; "
    4568                 :             :                    "attempted assignment of %s to %s", &lvalue->where,
    4569                 :             :                    gfc_typename (rvalue), gfc_typename (lvalue));
    4570                 :          11 :       return false;
    4571                 :             :     }
    4572                 :             : 
    4573                 :        7259 :   if (lvalue->ts.type != BT_CLASS && lvalue->ts.kind != rvalue->ts.kind)
    4574                 :             :     {
    4575                 :           0 :       gfc_error ("Different kind type parameters in pointer "
    4576                 :             :                  "assignment at %L", &lvalue->where);
    4577                 :           0 :       return false;
    4578                 :             :     }
    4579                 :             : 
    4580                 :        7259 :   if (lvalue->rank != rvalue->rank && !rank_remap)
    4581                 :             :     {
    4582                 :           4 :       gfc_error ("Different ranks in pointer assignment at %L", &lvalue->where);
    4583                 :           4 :       return false;
    4584                 :             :     }
    4585                 :             : 
    4586                 :             :   /* Make sure the vtab is present.  */
    4587                 :        7255 :   if (lvalue->ts.type == BT_CLASS && !UNLIMITED_POLY (rvalue))
    4588                 :        1268 :     gfc_find_vtab (&rvalue->ts);
    4589                 :             : 
    4590                 :             :   /* Check rank remapping.  */
    4591                 :        7255 :   if (rank_remap)
    4592                 :             :     {
    4593                 :         192 :       mpz_t lsize, rsize;
    4594                 :             : 
    4595                 :             :       /* If this can be determined, check that the target must be at least as
    4596                 :             :          large as the pointer assigned to it is.  */
    4597                 :         192 :       bool got_lsize = gfc_array_size (lvalue, &lsize);
    4598                 :         192 :       bool got_rsize = got_lsize && gfc_array_size (rvalue, &rsize);
    4599                 :          75 :       bool too_small = got_rsize && mpz_cmp (rsize, lsize) < 0;
    4600                 :             : 
    4601                 :         192 :       if (too_small)
    4602                 :             :         {
    4603                 :           4 :           gfc_error ("Rank remapping target is smaller than size of the"
    4604                 :             :                      " pointer (%ld < %ld) at %L",
    4605                 :             :                      mpz_get_si (rsize), mpz_get_si (lsize),
    4606                 :             :                      &lvalue->where);
    4607                 :           4 :           mpz_clear (lsize);
    4608                 :           4 :           mpz_clear (rsize);
    4609                 :           8 :           return false;
    4610                 :             :         }
    4611                 :         188 :       if (got_lsize)
    4612                 :         127 :         mpz_clear (lsize);
    4613                 :         188 :       if (got_rsize)
    4614                 :          71 :         mpz_clear (rsize);
    4615                 :             : 
    4616                 :             :       /* An assumed rank target is an experimental F202y feature.  */
    4617                 :         188 :       if (rvalue->rank == -1 && !(gfc_option.allow_std & GFC_STD_F202Y))
    4618                 :             :         {
    4619                 :           1 :           gfc_error ("The assumed rank target at %L is an experimental F202y "
    4620                 :             :                      "feature. Use option -std=f202y to enable",
    4621                 :             :                      &rvalue->where);
    4622                 :           1 :           return false;
    4623                 :             :         }
    4624                 :             : 
    4625                 :             :       /* The target must be either rank one or it must be simply contiguous
    4626                 :             :          and F2008 must be allowed.  */
    4627                 :         187 :       if (rvalue->rank != 1 && rvalue->rank != -1)
    4628                 :             :         {
    4629                 :          21 :           if (!gfc_is_simply_contiguous (rvalue, true, false))
    4630                 :             :             {
    4631                 :           2 :               gfc_error ("Rank remapping target must be rank 1 or"
    4632                 :             :                          " simply contiguous at %L", &rvalue->where);
    4633                 :           2 :               return false;
    4634                 :             :             }
    4635                 :          19 :           if (!gfc_notify_std (GFC_STD_F2008, "Rank remapping target is not "
    4636                 :             :                                "rank 1 at %L", &rvalue->where))
    4637                 :             :             return false;
    4638                 :             :         }
    4639                 :             :     }
    4640                 :        7063 :   else if (rvalue->rank == -1)
    4641                 :             :     {
    4642                 :           0 :       gfc_error ("The data-target at %L is an assumed rank object and so the "
    4643                 :             :                  "data-pointer-object %s must have a bounds remapping list "
    4644                 :             :                  "(list of lbound:ubound for each dimension)",
    4645                 :           0 :                   &rvalue->where, lvalue->symtree->name);
    4646                 :           0 :       return false;
    4647                 :             :     }
    4648                 :             : 
    4649                 :        7247 :   if (rvalue->rank == -1 && !gfc_is_simply_contiguous (rvalue, true, false))
    4650                 :             :     {
    4651                 :           0 :       gfc_error ("The assumed rank data-target at %L must be contiguous",
    4652                 :             :                  &rvalue->where);
    4653                 :           0 :       return false;
    4654                 :             :     }
    4655                 :             : 
    4656                 :             :   /* Now punt if we are dealing with a NULLIFY(X) or X = NULL(X).  */
    4657                 :        7247 :   if (rvalue->expr_type == EXPR_NULL)
    4658                 :             :     return true;
    4659                 :             : 
    4660                 :        7220 :   if (rvalue->expr_type == EXPR_VARIABLE && is_subref_array (rvalue))
    4661                 :         452 :     lvalue->symtree->n.sym->attr.subref_array_pointer = 1;
    4662                 :             : 
    4663                 :        7220 :   attr = gfc_expr_attr (rvalue);
    4664                 :             : 
    4665                 :        7220 :   if (rvalue->expr_type == EXPR_FUNCTION && !attr.pointer)
    4666                 :             :     {
    4667                 :             :       /* F2008, C725.  For PURE also C1283.  Sometimes rvalue is a function call
    4668                 :             :          to caf_get.  Map this to the same error message as below when it is
    4669                 :             :          still a variable expression.  */
    4670                 :           1 :       if (rvalue->value.function.isym
    4671                 :           0 :           && rvalue->value.function.isym->id == GFC_ISYM_CAF_GET)
    4672                 :             :         /* The test above might need to be extend when F08, Note 5.4 has to be
    4673                 :             :            interpreted in the way that target and pointer with the same coindex
    4674                 :             :            are allowed.  */
    4675                 :           0 :         gfc_error ("Data target at %L shall not have a coindex",
    4676                 :             :                    &rvalue->where);
    4677                 :             :       else
    4678                 :           1 :         gfc_error ("Target expression in pointer assignment "
    4679                 :             :                    "at %L must deliver a pointer result",
    4680                 :             :                    &rvalue->where);
    4681                 :           1 :       return false;
    4682                 :             :     }
    4683                 :             : 
    4684                 :        7219 :   if (is_init_expr)
    4685                 :             :     {
    4686                 :         245 :       gfc_symbol *sym;
    4687                 :         245 :       bool target;
    4688                 :         245 :       gfc_ref *ref;
    4689                 :             : 
    4690                 :         245 :       if (gfc_is_size_zero_array (rvalue))
    4691                 :             :         {
    4692                 :           1 :           gfc_error ("Zero-sized array detected at %L where an entity with "
    4693                 :             :                      "the TARGET attribute is expected", &rvalue->where);
    4694                 :           1 :           return false;
    4695                 :             :         }
    4696                 :         244 :       else if (!rvalue->symtree)
    4697                 :             :         {
    4698                 :           1 :           gfc_error ("Pointer assignment target in initialization expression "
    4699                 :             :                      "does not have the TARGET attribute at %L",
    4700                 :             :                      &rvalue->where);
    4701                 :           1 :           return false;
    4702                 :             :         }
    4703                 :             : 
    4704                 :         243 :       sym = rvalue->symtree->n.sym;
    4705                 :             : 
    4706                 :         243 :       if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
    4707                 :           0 :         target = CLASS_DATA (sym)->attr.target;
    4708                 :             :       else
    4709                 :         243 :         target = sym->attr.target;
    4710                 :             : 
    4711                 :         243 :       if (!target && !proc_pointer)
    4712                 :             :         {
    4713                 :           4 :           gfc_error ("Pointer assignment target in initialization expression "
    4714                 :             :                      "does not have the TARGET attribute at %L",
    4715                 :             :                      &rvalue->where);
    4716                 :           4 :           return false;
    4717                 :             :         }
    4718                 :             : 
    4719                 :         312 :       for (ref = rvalue->ref; ref; ref = ref->next)
    4720                 :             :         {
    4721                 :          78 :           switch (ref->type)
    4722                 :             :             {
    4723                 :             :             case REF_ARRAY:
    4724                 :          47 :               for (int n = 0; n < ref->u.ar.dimen; n++)
    4725                 :          25 :                 if (!gfc_is_constant_expr (ref->u.ar.start[n])
    4726                 :          23 :                     || !gfc_is_constant_expr (ref->u.ar.end[n])
    4727                 :          47 :                     || !gfc_is_constant_expr (ref->u.ar.stride[n]))
    4728                 :             :                   {
    4729                 :           3 :                     gfc_error ("Every subscript of target specification "
    4730                 :             :                                "at %L must be a constant expression",
    4731                 :             :                                &ref->u.ar.where);
    4732                 :           3 :                     return false;
    4733                 :             :                   }
    4734                 :             :               break;
    4735                 :             : 
    4736                 :           5 :             case REF_SUBSTRING:
    4737                 :           5 :               if (!gfc_is_constant_expr (ref->u.ss.start)
    4738                 :           5 :                   || !gfc_is_constant_expr (ref->u.ss.end))
    4739                 :             :                 {
    4740                 :           2 :                   gfc_error ("Substring starting and ending points of target "
    4741                 :             :                              "specification at %L must be constant expressions",
    4742                 :           2 :                              &ref->u.ss.start->where);
    4743                 :           2 :                   return false;
    4744                 :             :                 }
    4745                 :             :               break;
    4746                 :             : 
    4747                 :             :             default:
    4748                 :             :               break;
    4749                 :             :             }
    4750                 :             :         }
    4751                 :             :     }
    4752                 :             :   else
    4753                 :             :     {
    4754                 :        6974 :       if (!attr.target && !attr.pointer)
    4755                 :             :         {
    4756                 :           9 :           gfc_error ("Pointer assignment target is neither TARGET "
    4757                 :             :                      "nor POINTER at %L", &rvalue->where);
    4758                 :           9 :           return false;
    4759                 :             :         }
    4760                 :             :     }
    4761                 :             : 
    4762                 :        7199 :   if (lvalue->ts.type == BT_CHARACTER)
    4763                 :             :     {
    4764                 :        1228 :       bool t = gfc_check_same_strlen (lvalue, rvalue, "pointer assignment");
    4765                 :        1228 :       if (!t)
    4766                 :             :         return false;
    4767                 :             :     }
    4768                 :             : 
    4769                 :        7197 :   if (is_pure && gfc_impure_variable (rvalue->symtree->n.sym))
    4770                 :             :     {
    4771                 :           3 :       gfc_error ("Bad target in pointer assignment in PURE "
    4772                 :             :                  "procedure at %L", &rvalue->where);
    4773                 :             :     }
    4774                 :             : 
    4775                 :        7197 :   if (is_implicit_pure && gfc_impure_variable (rvalue->symtree->n.sym))
    4776                 :         290 :     gfc_unset_implicit_pure (gfc_current_ns->proc_name);
    4777                 :             : 
    4778                 :        7197 :   if (gfc_has_vector_index (rvalue))
    4779                 :             :     {
    4780                 :           2 :       gfc_error ("Pointer assignment with vector subscript "
    4781                 :             :                  "on rhs at %L", &rvalue->where);
    4782                 :           2 :       return false;
    4783                 :             :     }
    4784                 :             : 
    4785                 :        7195 :   if (attr.is_protected && attr.use_assoc
    4786                 :           4 :       && !(attr.pointer || attr.proc_pointer))
    4787                 :             :     {
    4788                 :           3 :       gfc_error ("Pointer assignment target has PROTECTED "
    4789                 :             :                  "attribute at %L", &rvalue->where);
    4790                 :           3 :       return false;
    4791                 :             :     }
    4792                 :             : 
    4793                 :             :   /* F2008, C725. For PURE also C1283.  */
    4794                 :        7192 :   if (rvalue->expr_type == EXPR_VARIABLE
    4795                 :        7192 :       && gfc_is_coindexed (rvalue))
    4796                 :             :     {
    4797                 :           4 :       gfc_ref *ref;
    4798                 :           5 :       for (ref = rvalue->ref; ref; ref = ref->next)
    4799                 :           5 :         if (ref->type == REF_ARRAY && ref->u.ar.codimen)
    4800                 :             :           {
    4801                 :           4 :             gfc_error ("Data target at %L shall not have a coindex",
    4802                 :             :                        &rvalue->where);
    4803                 :           4 :             return false;
    4804                 :             :           }
    4805                 :             :     }
    4806                 :             : 
    4807                 :             :   /* Warn for assignments of contiguous pointers to targets which is not
    4808                 :             :      contiguous.  Be lenient in the definition of what counts as
    4809                 :             :      contiguous.  */
    4810                 :             : 
    4811                 :        7188 :   if (lhs_attr.contiguous
    4812                 :          73 :       && lhs_attr.dimension > 0)
    4813                 :             :     {
    4814                 :          69 :       if (gfc_is_not_contiguous (rvalue))
    4815                 :             :         {
    4816                 :           6 :           gfc_error ("Assignment to contiguous pointer from "
    4817                 :             :                      "non-contiguous target at %L", &rvalue->where);
    4818                 :           6 :           return false;
    4819                 :             :         }
    4820                 :          63 :       if (!gfc_is_simply_contiguous (rvalue, false, true))
    4821                 :           8 :         gfc_warning (OPT_Wextra, "Assignment to contiguous pointer from "
    4822                 :             :                                  "non-contiguous target at %L", &rvalue->where);
    4823                 :             :     }
    4824                 :             : 
    4825                 :             :   /* Warn if it is the LHS pointer may lives longer than the RHS target.  */
    4826                 :        7182 :   if (warn_target_lifetime
    4827                 :          15 :       && rvalue->expr_type == EXPR_VARIABLE
    4828                 :          15 :       && !rvalue->symtree->n.sym->attr.save
    4829                 :          15 :       && !rvalue->symtree->n.sym->attr.pointer && !attr.pointer
    4830                 :          13 :       && !rvalue->symtree->n.sym->attr.host_assoc
    4831                 :          11 :       && !rvalue->symtree->n.sym->attr.in_common
    4832                 :          11 :       && !rvalue->symtree->n.sym->attr.use_assoc
    4833                 :          11 :       && !rvalue->symtree->n.sym->attr.dummy)
    4834                 :             :     {
    4835                 :           9 :       bool warn;
    4836                 :           9 :       gfc_namespace *ns;
    4837                 :             : 
    4838                 :          18 :       warn = lvalue->symtree->n.sym->attr.dummy
    4839                 :           9 :              || lvalue->symtree->n.sym->attr.result
    4840                 :           8 :              || lvalue->symtree->n.sym->attr.function
    4841                 :           7 :              || (lvalue->symtree->n.sym->attr.host_assoc
    4842                 :           4 :                  && lvalue->symtree->n.sym->ns
    4843                 :           4 :                     != rvalue->symtree->n.sym->ns)
    4844                 :           4 :              || lvalue->symtree->n.sym->attr.use_assoc
    4845                 :          13 :              || lvalue->symtree->n.sym->attr.in_common;
    4846                 :             : 
    4847                 :           9 :       if (rvalue->symtree->n.sym->ns->proc_name
    4848                 :           9 :           && rvalue->symtree->n.sym->ns->proc_name->attr.flavor != FL_PROCEDURE
    4849                 :           3 :           && rvalue->symtree->n.sym->ns->proc_name->attr.flavor != FL_PROGRAM)
    4850                 :             :        for (ns = rvalue->symtree->n.sym->ns;
    4851                 :           5 :             ns && ns->proc_name && ns->proc_name->attr.flavor != FL_PROCEDURE;
    4852                 :             :             ns = ns->parent)
    4853                 :           3 :         if (ns->parent == lvalue->symtree->n.sym->ns)
    4854                 :             :           {
    4855                 :             :             warn = true;
    4856                 :             :             break;
    4857                 :             :           }
    4858                 :             : 
    4859                 :           9 :       if (warn)
    4860                 :           5 :         gfc_warning (OPT_Wtarget_lifetime,
    4861                 :             :                      "Pointer at %L in pointer assignment might outlive the "
    4862                 :             :                      "pointer target", &lvalue->where);
    4863                 :             :     }
    4864                 :             : 
    4865                 :             :   return true;
    4866                 :             : }
    4867                 :             : 
    4868                 :             : 
    4869                 :             : /* Relative of gfc_check_assign() except that the lvalue is a single
    4870                 :             :    symbol.  Used for initialization assignments.  */
    4871                 :             : 
    4872                 :             : bool
    4873                 :      470646 : gfc_check_assign_symbol (gfc_symbol *sym, gfc_component *comp, gfc_expr *rvalue)
    4874                 :             : {
    4875                 :      470646 :   gfc_expr lvalue;
    4876                 :      470646 :   bool r;
    4877                 :      470646 :   bool pointer, proc_pointer;
    4878                 :             : 
    4879                 :      470646 :   memset (&lvalue, '\0', sizeof (gfc_expr));
    4880                 :             : 
    4881                 :      470646 :   if (sym && sym->attr.pdt_template && comp && comp->initializer)
    4882                 :             :     {
    4883                 :          89 :       int i, flag;
    4884                 :          89 :       gfc_expr *param_expr;
    4885                 :          89 :       flag = 0;
    4886                 :             : 
    4887                 :          89 :       if (comp->as && comp->as->type == AS_EXPLICIT
    4888                 :           7 :           && !(comp->ts.type == BT_DERIVED
    4889                 :           6 :                && comp->ts.u.derived->attr.pdt_template))
    4890                 :             :         {
    4891                 :             :           /* Are the bounds of the array parameterized?  */
    4892                 :           2 :           for (i = 0; i < comp->as->rank; i++)
    4893                 :             :             {
    4894                 :           1 :               param_expr = gfc_copy_expr (comp->as->lower[i]);
    4895                 :           1 :               if (gfc_simplify_expr (param_expr, 1)
    4896                 :           1 :                   && param_expr->expr_type != EXPR_CONSTANT)
    4897                 :           0 :                 flag++;
    4898                 :           1 :               gfc_free_expr (param_expr);
    4899                 :           1 :               param_expr = gfc_copy_expr (comp->as->upper[i]);
    4900                 :           1 :               if (gfc_simplify_expr (param_expr, 1)
    4901                 :           1 :                   && param_expr->expr_type != EXPR_CONSTANT)
    4902                 :           1 :                 flag++;
    4903                 :           1 :               gfc_free_expr (param_expr);
    4904                 :             :             }
    4905                 :             :         }
    4906                 :             : 
    4907                 :             :       /* Is the character length parameterized?  */
    4908                 :          89 :       if (comp->ts.type == BT_CHARACTER && comp->ts.u.cl->length)
    4909                 :             :         {
    4910                 :           1 :           param_expr = gfc_copy_expr (comp->ts.u.cl->length);
    4911                 :           1 :           if (gfc_simplify_expr (param_expr, 1)
    4912                 :           1 :               && param_expr->expr_type != EXPR_CONSTANT)
    4913                 :           1 :             flag++;
    4914                 :           1 :           gfc_free_expr (param_expr);
    4915                 :             :         }
    4916                 :             : 
    4917                 :          89 :       if (flag)
    4918                 :             :         {
    4919                 :           2 :           gfc_error ("The component %qs at %L of derived type %qs has "
    4920                 :             :                      "paramterized type or array length parameters, which is "
    4921                 :             :                      "not compatible with a default initializer",
    4922                 :           2 :                       comp->name, &comp->initializer->where, sym->name);
    4923                 :           2 :           return false;
    4924                 :             :         }
    4925                 :             :     }
    4926                 :             : 
    4927                 :      470644 :   lvalue.expr_type = EXPR_VARIABLE;
    4928                 :      470644 :   lvalue.ts = sym->ts;
    4929                 :      470644 :   if (sym->as)
    4930                 :             :     {
    4931                 :       16148 :       lvalue.rank = sym->as->rank;
    4932                 :       16148 :       lvalue.corank = sym->as->corank;
    4933                 :             :     }
    4934                 :      470644 :   lvalue.symtree = XCNEW (gfc_symtree);
    4935                 :      470644 :   lvalue.symtree->n.sym = sym;
    4936                 :      470644 :   lvalue.where = sym->declared_at;
    4937                 :             : 
    4938                 :      470644 :   if (comp)
    4939                 :             :     {
    4940                 :       26963 :       lvalue.ref = gfc_get_ref ();
    4941                 :       26963 :       lvalue.ref->type = REF_COMPONENT;
    4942                 :       26963 :       lvalue.ref->u.c.component = comp;
    4943                 :       26963 :       lvalue.ref->u.c.sym = sym;
    4944                 :       26963 :       lvalue.ts = comp->ts;
    4945                 :       26963 :       lvalue.rank = comp->as ? comp->as->rank : 0;
    4946                 :       26963 :       lvalue.corank = comp->as ? comp->as->corank : 0;
    4947                 :       26963 :       lvalue.where = comp->loc;
    4948                 :        1008 :       pointer = comp->ts.type == BT_CLASS &&  CLASS_DATA (comp)
    4949                 :       27971 :                 ? CLASS_DATA (comp)->attr.class_pointer : comp->attr.pointer;
    4950                 :       26963 :       proc_pointer = comp->attr.proc_pointer;
    4951                 :             :     }
    4952                 :             :   else
    4953                 :             :     {
    4954                 :        2699 :       pointer = sym->ts.type == BT_CLASS &&  CLASS_DATA (sym)
    4955                 :      446380 :                 ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer;
    4956                 :      443681 :       proc_pointer = sym->attr.proc_pointer;
    4957                 :             :     }
    4958                 :             : 
    4959                 :      470644 :   if (pointer || proc_pointer)
    4960                 :        5020 :     r = gfc_check_pointer_assign (&lvalue, rvalue, false, true);
    4961                 :             :   else
    4962                 :             :     {
    4963                 :             :       /* If a conversion function, e.g., __convert_i8_i4, was inserted
    4964                 :             :          into an array constructor, we should check if it can be reduced
    4965                 :             :          as an initialization expression.  */
    4966                 :      465624 :       if (rvalue->expr_type == EXPR_FUNCTION
    4967                 :          36 :           && rvalue->value.function.isym
    4968                 :          30 :           && (rvalue->value.function.isym->conversion == 1))
    4969                 :           0 :         gfc_check_init_expr (rvalue);
    4970                 :             : 
    4971                 :      465624 :       r = gfc_check_assign (&lvalue, rvalue, 1);
    4972                 :             :     }
    4973                 :             : 
    4974                 :      470644 :   free (lvalue.symtree);
    4975                 :      470644 :   free (lvalue.ref);
    4976                 :             : 
    4977                 :      470644 :   if (!r)
    4978                 :             :     return r;
    4979                 :             : 
    4980                 :      470593 :   if (pointer && rvalue->expr_type != EXPR_NULL && !proc_pointer)
    4981                 :             :     {
    4982                 :             :       /* F08:C461. Additional checks for pointer initialization.  */
    4983                 :         227 :       symbol_attribute attr;
    4984                 :         227 :       attr = gfc_expr_attr (rvalue);
    4985                 :         227 :       if (attr.allocatable)
    4986                 :             :         {
    4987                 :           2 :           gfc_error ("Pointer initialization target at %L "
    4988                 :             :                      "must not be ALLOCATABLE", &rvalue->where);
    4989                 :          13 :           return false;
    4990                 :             :         }
    4991                 :         225 :       if (!attr.target || attr.pointer)
    4992                 :             :         {
    4993                 :           1 :           gfc_error ("Pointer initialization target at %L "
    4994                 :             :                      "must have the TARGET attribute", &rvalue->where);
    4995                 :           1 :           return false;
    4996                 :             :         }
    4997                 :             : 
    4998                 :         224 :       if (!attr.save && rvalue->expr_type == EXPR_VARIABLE
    4999                 :          14 :           && rvalue->symtree->n.sym->ns->proc_name
    5000                 :          14 :           && rvalue->symtree->n.sym->ns->proc_name->attr.is_main_program)
    5001                 :             :         {
    5002                 :           4 :           rvalue->symtree->n.sym->ns->proc_name->attr.save = SAVE_IMPLICIT;
    5003                 :           4 :           attr.save = SAVE_IMPLICIT;
    5004                 :             :         }
    5005                 :             : 
    5006                 :         224 :       if (!attr.save)
    5007                 :             :         {
    5008                 :          10 :           gfc_error ("Pointer initialization target at %L "
    5009                 :             :                      "must have the SAVE attribute", &rvalue->where);
    5010                 :          10 :           return false;
    5011                 :             :         }
    5012                 :             :     }
    5013                 :             : 
    5014                 :      470580 :   if (proc_pointer && rvalue->expr_type != EXPR_NULL)
    5015                 :             :     {
    5016                 :             :       /* F08:C1220. Additional checks for procedure pointer initialization.  */
    5017                 :          59 :       symbol_attribute attr = gfc_expr_attr (rvalue);
    5018                 :          59 :       if (attr.proc_pointer)
    5019                 :             :         {
    5020                 :           1 :           gfc_error ("Procedure pointer initialization target at %L "
    5021                 :             :                      "may not be a procedure pointer", &rvalue->where);
    5022                 :           3 :           return false;
    5023                 :             :         }
    5024                 :          58 :       if (attr.proc == PROC_INTERNAL)
    5025                 :             :         {
    5026                 :           1 :           gfc_error ("Internal procedure %qs is invalid in "
    5027                 :             :                      "procedure pointer initialization at %L",
    5028                 :           1 :                      rvalue->symtree->name, &rvalue->where);
    5029                 :           1 :           return false;
    5030                 :             :         }
    5031                 :          57 :       if (attr.dummy)
    5032                 :             :         {
    5033                 :           1 :           gfc_error ("Dummy procedure %qs is invalid in "
    5034                 :             :                      "procedure pointer initialization at %L",
    5035                 :           1 :                      rvalue->symtree->name, &rvalue->where);
    5036                 :           1 :           return false;
    5037                 :             :         }
    5038                 :             :     }
    5039                 :             : 
    5040                 :             :   return true;
    5041                 :             : }
    5042                 :             : 
    5043                 :             : /* Build an initializer for a local integer, real, complex, logical, or
    5044                 :             :    character variable, based on the command line flags finit-local-zero,
    5045                 :             :    finit-integer=, finit-real=, finit-logical=, and finit-character=.
    5046                 :             :    With force, an initializer is ALWAYS generated.  */
    5047                 :             : 
    5048                 :             : static gfc_expr *
    5049                 :       99708 : gfc_build_init_expr (gfc_typespec *ts, locus *where, bool force)
    5050                 :             : {
    5051                 :       99708 :   gfc_expr *init_expr;
    5052                 :             : 
    5053                 :             :   /* Try to build an initializer expression.  */
    5054                 :       99708 :   init_expr = gfc_get_constant_expr (ts->type, ts->kind, where);
    5055                 :             : 
    5056                 :             :   /* If we want to force generation, make sure we default to zero.  */
    5057                 :       99708 :   gfc_init_local_real init_real = flag_init_real;
    5058                 :       99708 :   int init_logical = gfc_option.flag_init_logical;
    5059                 :       99708 :   if (force)
    5060                 :             :     {
    5061                 :         210 :       if (init_real == GFC_INIT_REAL_OFF)
    5062                 :             :         init_real = GFC_INIT_REAL_ZERO;
    5063                 :         210 :       if (init_logical == GFC_INIT_LOGICAL_OFF)
    5064                 :          40 :         init_logical = GFC_INIT_LOGICAL_FALSE;
    5065                 :             :     }
    5066                 :             : 
    5067                 :             :   /* We will only initialize integers, reals, complex, logicals, and
    5068                 :             :      characters, and only if the corresponding command-line flags
    5069                 :             :      were set.  Otherwise, we free init_expr and return null.  */
    5070                 :       99708 :   switch (ts->type)
    5071                 :             :     {
    5072                 :       52610 :     case BT_INTEGER:
    5073                 :       52610 :       if (force || gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
    5074                 :         285 :         mpz_set_si (init_expr->value.integer,
    5075                 :             :                          gfc_option.flag_init_integer_value);
    5076                 :             :       else
    5077                 :             :         {
    5078                 :       52325 :           gfc_free_expr (init_expr);
    5079                 :       52325 :           init_expr = NULL;
    5080                 :             :         }
    5081                 :             :       break;
    5082                 :             : 
    5083                 :       15759 :     case BT_REAL:
    5084                 :       15759 :       switch (init_real)
    5085                 :             :         {
    5086                 :           0 :         case GFC_INIT_REAL_SNAN:
    5087                 :           0 :           init_expr->is_snan = 1;
    5088                 :             :           /* Fall through.  */
    5089                 :          48 :         case GFC_INIT_REAL_NAN:
    5090                 :          48 :           mpfr_set_nan (init_expr->value.real);
    5091                 :          48 :           break;
    5092                 :             : 
    5093                 :          26 :         case GFC_INIT_REAL_INF:
    5094                 :          26 :           mpfr_set_inf (init_expr->value.real, 1);
    5095                 :          26 :           break;
    5096                 :             : 
    5097                 :          24 :         case GFC_INIT_REAL_NEG_INF:
    5098                 :          24 :           mpfr_set_inf (init_expr->value.real, -1);
    5099                 :          24 :           break;
    5100                 :             : 
    5101                 :          63 :         case GFC_INIT_REAL_ZERO:
    5102                 :          63 :           mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
    5103                 :          63 :           break;
    5104                 :             : 
    5105                 :       15598 :         default:
    5106                 :       15598 :           gfc_free_expr (init_expr);
    5107                 :       15598 :           init_expr = NULL;
    5108                 :       15598 :           break;
    5109                 :             :         }
    5110                 :             :       break;
    5111                 :             : 
    5112                 :        1669 :     case BT_COMPLEX:
    5113                 :        1669 :       switch (init_real)
    5114                 :             :         {
    5115                 :           0 :         case GFC_INIT_REAL_SNAN:
    5116                 :           0 :           init_expr->is_snan = 1;
    5117                 :             :           /* Fall through.  */
    5118                 :          12 :         case GFC_INIT_REAL_NAN:
    5119                 :          12 :           mpfr_set_nan (mpc_realref (init_expr->value.complex));
    5120                 :          12 :           mpfr_set_nan (mpc_imagref (init_expr->value.complex));
    5121                 :          12 :           break;
    5122                 :             : 
    5123                 :           0 :         case GFC_INIT_REAL_INF:
    5124                 :           0 :           mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
    5125                 :           0 :           mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
    5126                 :           0 :           break;
    5127                 :             : 
    5128                 :           0 :         case GFC_INIT_REAL_NEG_INF:
    5129                 :           0 :           mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
    5130                 :           0 :           mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
    5131                 :           0 :           break;
    5132                 :             : 
    5133                 :          24 :         case GFC_INIT_REAL_ZERO:
    5134                 :          24 :           mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
    5135                 :          24 :           break;
    5136                 :             : 
    5137                 :        1633 :         default:
    5138                 :        1633 :           gfc_free_expr (init_expr);
    5139                 :        1633 :           init_expr = NULL;
    5140                 :        1633 :           break;
    5141                 :             :         }
    5142                 :             :       break;
    5143                 :             : 
    5144                 :        4893 :     case BT_LOGICAL:
    5145                 :        4893 :       if (init_logical == GFC_INIT_LOGICAL_FALSE)
    5146                 :          39 :         init_expr->value.logical = 0;
    5147                 :        4854 :       else if (init_logical == GFC_INIT_LOGICAL_TRUE)
    5148                 :          26 :         init_expr->value.logical = 1;
    5149                 :             :       else
    5150                 :             :         {
    5151                 :        4828 :           gfc_free_expr (init_expr);
    5152                 :        4828 :           init_expr = NULL;
    5153                 :             :         }
    5154                 :             :       break;
    5155                 :             : 
    5156                 :        9496 :     case BT_CHARACTER:
    5157                 :             :       /* For characters, the length must be constant in order to
    5158                 :             :          create a default initializer.  */
    5159                 :        9496 :       if ((force || gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON)
    5160                 :          83 :           && ts->u.cl->length
    5161                 :          83 :           && ts->u.cl->length->expr_type == EXPR_CONSTANT)
    5162                 :             :         {
    5163                 :          76 :           HOST_WIDE_INT char_len = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
    5164                 :          76 :           init_expr->value.character.length = char_len;
    5165                 :          76 :           init_expr->value.character.string = gfc_get_wide_string (char_len+1);
    5166                 :         320 :           for (size_t i = 0; i < (size_t) char_len; i++)
    5167                 :         244 :             init_expr->value.character.string[i]
    5168                 :         244 :               = (unsigned char) gfc_option.flag_init_character_value;
    5169                 :             :         }
    5170                 :             :       else
    5171                 :             :         {
    5172                 :        9420 :           gfc_free_expr (init_expr);
    5173                 :        9420 :           init_expr = NULL;
    5174                 :             :         }
    5175                 :        9420 :       if (!init_expr
    5176                 :        9420 :           && (force || gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON)
    5177                 :           7 :           && ts->u.cl->length && flag_max_stack_var_size != 0)
    5178                 :             :         {
    5179                 :           6 :           gfc_actual_arglist *arg;
    5180                 :           6 :           init_expr = gfc_get_expr ();
    5181                 :           6 :           init_expr->where = *where;
    5182                 :           6 :           init_expr->ts = *ts;
    5183                 :           6 :           init_expr->expr_type = EXPR_FUNCTION;
    5184                 :          12 :           init_expr->value.function.isym =
    5185                 :           6 :                 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
    5186                 :           6 :           init_expr->value.function.name = "repeat";
    5187                 :           6 :           arg = gfc_get_actual_arglist ();
    5188                 :           6 :           arg->expr = gfc_get_character_expr (ts->kind, where, NULL, 1);
    5189                 :           6 :           arg->expr->value.character.string[0] =
    5190                 :           6 :             gfc_option.flag_init_character_value;
    5191                 :           6 :           arg->next = gfc_get_actual_arglist ();
    5192                 :           6 :           arg->next->expr = gfc_copy_expr (ts->u.cl->length);
    5193                 :           6 :           init_expr->value.function.actual = arg;
    5194                 :             :         }
    5195                 :             :       break;
    5196                 :             : 
    5197                 :       15281 :     default:
    5198                 :       15281 :      gfc_free_expr (init_expr);
    5199                 :       15281 :      init_expr = NULL;
    5200                 :             :     }
    5201                 :             : 
    5202                 :       99708 :   return init_expr;
    5203                 :             : }
    5204                 :             : 
    5205                 :             : /* Invoke gfc_build_init_expr to create an initializer expression, but do not
    5206                 :             :  * require that an expression be built.  */
    5207                 :             : 
    5208                 :             : gfc_expr *
    5209                 :       99498 : gfc_build_default_init_expr (gfc_typespec *ts, locus *where)
    5210                 :             : {
    5211                 :       99498 :   return gfc_build_init_expr (ts, where, false);
    5212                 :             : }
    5213                 :             : 
    5214                 :             : /* Apply an initialization expression to a typespec. Can be used for symbols or
    5215                 :             :    components. Similar to add_init_expr_to_sym in decl.cc; could probably be
    5216                 :             :    combined with some effort.  */
    5217                 :             : 
    5218                 :             : void
    5219                 :       16970 : gfc_apply_init (gfc_typespec *ts, symbol_attribute *attr, gfc_expr *init)
    5220                 :             : {
    5221                 :       16970 :   if (ts->type == BT_CHARACTER && !attr->pointer && init
    5222                 :         355 :       && ts->u.cl
    5223                 :         355 :       && ts->u.cl->length
    5224                 :         355 :       && ts->u.cl->length->expr_type == EXPR_CONSTANT
    5225                 :         353 :       && ts->u.cl->length->ts.type == BT_INTEGER)
    5226                 :             :     {
    5227                 :         353 :       HOST_WIDE_INT len = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
    5228                 :             : 
    5229                 :         353 :       if (init->expr_type == EXPR_CONSTANT)
    5230                 :         246 :         gfc_set_constant_character_len (len, init, -1);
    5231                 :         107 :       else if (init
    5232                 :         107 :                && init->ts.type == BT_CHARACTER
    5233                 :         102 :                && init->ts.u.cl && init->ts.u.cl->length
    5234                 :         102 :                && mpz_cmp (ts->u.cl->length->value.integer,
    5235                 :         102 :                            init->ts.u.cl->length->value.integer))
    5236                 :             :         {
    5237                 :           0 :           gfc_constructor *ctor;
    5238                 :           0 :           ctor = gfc_constructor_first (init->value.constructor);
    5239                 :             : 
    5240                 :           0 :           if (ctor)
    5241                 :             :             {
    5242                 :           0 :               bool has_ts = (init->ts.u.cl
    5243                 :           0 :                              && init->ts.u.cl->length_from_typespec);
    5244                 :             : 
    5245                 :             :               /* Remember the length of the first element for checking
    5246                 :             :                  that all elements *in the constructor* have the same
    5247                 :             :                  length.  This need not be the length of the LHS!  */
    5248                 :           0 :               gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
    5249                 :           0 :               gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
    5250                 :           0 :               gfc_charlen_t first_len = ctor->expr->value.character.length;
    5251                 :             : 
    5252                 :           0 :               for ( ; ctor; ctor = gfc_constructor_next (ctor))
    5253                 :           0 :                 if (ctor->expr->expr_type == EXPR_CONSTANT)
    5254                 :             :                 {
    5255                 :           0 :                   gfc_set_constant_character_len (len, ctor->expr,
    5256                 :             :                                                   has_ts ? -1 : first_len);
    5257                 :           0 :                   if (!ctor->expr->ts.u.cl)
    5258                 :           0 :                     ctor->expr->ts.u.cl
    5259                 :           0 :                       = gfc_new_charlen (gfc_current_ns, ts->u.cl);
    5260                 :             :                   else
    5261                 :           0 :                     ctor->expr->ts.u.cl->length
    5262                 :           0 :                       = gfc_copy_expr (ts->u.cl->length);
    5263                 :             :                 }
    5264                 :             :             }
    5265                 :             :         }
    5266                 :             :     }
    5267                 :       16970 : }
    5268                 :             : 
    5269                 :             : 
    5270                 :             : /* Check whether an expression is a structure constructor and whether it has
    5271                 :             :    other values than NULL.  */
    5272                 :             : 
    5273                 :             : static bool
    5274                 :         796 : is_non_empty_structure_constructor (gfc_expr * e)
    5275                 :             : {
    5276                 :         796 :   if (e->expr_type != EXPR_STRUCTURE)
    5277                 :             :     return false;
    5278                 :             : 
    5279                 :         796 :   gfc_constructor *cons = gfc_constructor_first (e->value.constructor);
    5280                 :        2103 :   while (cons)
    5281                 :             :     {
    5282                 :         911 :       if (!cons->expr || cons->expr->expr_type != EXPR_NULL)
    5283                 :             :         return true;
    5284                 :         511 :       cons = gfc_constructor_next (cons);
    5285                 :             :     }
    5286                 :             :   return false;
    5287                 :             : }
    5288                 :             : 
    5289                 :             : 
    5290                 :             : /* Check for default initializer; sym->value is not enough
    5291                 :             :    as it is also set for EXPR_NULL of allocatables.  */
    5292                 :             : 
    5293                 :             : bool
    5294                 :        6804 : gfc_has_default_initializer (gfc_symbol *der)
    5295                 :             : {
    5296                 :        6804 :   static hash_set<gfc_symbol *> seen_derived_types;
    5297                 :        6804 :   gfc_component *c;
    5298                 :             :   /* The rewrite to a result variable and breaks is only needed, because
    5299                 :             :      there is no scope_guard in C++ yet.  */
    5300                 :        6804 :   bool result = false;
    5301                 :             : 
    5302                 :        6804 :   gcc_assert (gfc_fl_struct (der->attr.flavor));
    5303                 :        6804 :   seen_derived_types.add (der);
    5304                 :       14053 :   for (c = der->components; c; c = c->next)
    5305                 :        7053 :     if (gfc_bt_struct (c->ts.type)
    5306                 :        8643 :         && !seen_derived_types.contains (c->ts.u.derived))
    5307                 :             :       {
    5308                 :        1402 :         if (!c->attr.pointer && !c->attr.proc_pointer
    5309                 :        1402 :             && !(c->attr.allocatable && der == c->ts.u.derived)
    5310                 :        2934 :             && ((c->initializer
    5311                 :         796 :                  && is_non_empty_structure_constructor (c->initializer))
    5312                 :        1002 :                 || gfc_has_default_initializer (c->ts.u.derived)))
    5313                 :             :           {
    5314                 :             :             result = true;
    5315                 :             :             break;
    5316                 :             :           }
    5317                 :        1077 :         if (c->attr.pointer && c->initializer)
    5318                 :             :           {
    5319                 :             :             result = true;
    5320                 :             :             break;
    5321                 :             :           }
    5322                 :             :       }
    5323                 :             :     else
    5324                 :             :       {
    5325                 :        7107 :         if (c->initializer)
    5326                 :             :           {
    5327                 :             :             result = true;
    5328                 :             :             break;
    5329                 :             :           }
    5330                 :             :       }
    5331                 :             : 
    5332                 :        6804 :   seen_derived_types.remove (der);
    5333                 :        6804 :   return result;
    5334                 :             : }
    5335                 :             : 
    5336                 :             : 
    5337                 :             : /*
    5338                 :             :    Generate an initializer expression which initializes the entirety of a union.
    5339                 :             :    A normal structure constructor is insufficient without undue effort, because
    5340                 :             :    components of maps may be oddly aligned/overlapped. (For example if a
    5341                 :             :    character is initialized from one map overtop a real from the other, only one
    5342                 :             :    byte of the real is actually initialized.)  Unfortunately we don't know the
    5343                 :             :    size of the union right now, so we can't generate a proper initializer, but
    5344                 :             :    we use a NULL expr as a placeholder and do the right thing later in
    5345                 :             :    gfc_trans_subcomponent_assign.
    5346                 :             :  */
    5347                 :             : static gfc_expr *
    5348                 :          15 : generate_union_initializer (gfc_component *un)
    5349                 :             : {
    5350                 :          15 :   if (un == NULL || un->ts.type != BT_UNION)
    5351                 :             :     return NULL;
    5352                 :             : 
    5353                 :          15 :   gfc_expr *placeholder = gfc_get_null_expr (&un->loc);
    5354                 :          15 :   placeholder->ts = un->ts;
    5355                 :          15 :   return placeholder;
    5356                 :             : }
    5357                 :             : 
    5358                 :             : 
    5359                 :             : /* Get the user-specified initializer for a union, if any. This means the user
    5360                 :             :    has said to initialize component(s) of a map.  For simplicity's sake we
    5361                 :             :    only allow the user to initialize the first map.  We don't have to worry
    5362                 :             :    about overlapping initializers as they are released early in resolution (see
    5363                 :             :    resolve_fl_struct).   */
    5364                 :             : 
    5365                 :             : static gfc_expr *
    5366                 :          15 : get_union_initializer (gfc_symbol *union_type, gfc_component **map_p)
    5367                 :             : {
    5368                 :          15 :   gfc_component *map;
    5369                 :          15 :   gfc_expr *init=NULL;
    5370                 :             : 
    5371                 :          15 :   if (!union_type || union_type->attr.flavor != FL_UNION)
    5372                 :             :     return NULL;
    5373                 :             : 
    5374                 :          48 :   for (map = union_type->components; map; map = map->next)
    5375                 :             :     {
    5376                 :          33 :       if (gfc_has_default_initializer (map->ts.u.derived))
    5377                 :             :         {
    5378                 :           0 :           init = gfc_default_initializer (&map->ts);
    5379                 :           0 :           if (map_p)
    5380                 :           0 :             *map_p = map;
    5381                 :             :           break;
    5382                 :             :         }
    5383                 :             :     }
    5384                 :             : 
    5385                 :          15 :   if (map_p && !init)
    5386                 :          15 :     *map_p = NULL;
    5387                 :             : 
    5388                 :             :   return init;
    5389                 :             : }
    5390                 :             : 
    5391                 :             : static bool
    5392                 :      144690 : class_allocatable (gfc_component *comp)
    5393                 :             : {
    5394                 :        2905 :   return comp->ts.type == BT_CLASS && comp->attr.class_ok && CLASS_DATA (comp)
    5395                 :      147594 :     && CLASS_DATA (comp)->attr.allocatable;
    5396                 :             : }
    5397                 :             : 
    5398                 :             : static bool
    5399                 :         268 : class_pointer (gfc_component *comp)
    5400                 :             : {
    5401                 :           1 :   return comp->ts.type == BT_CLASS && comp->attr.class_ok && CLASS_DATA (comp)
    5402                 :         269 :     && CLASS_DATA (comp)->attr.pointer;
    5403                 :             : }
    5404                 :             : 
    5405                 :             : static bool
    5406                 :      161014 : comp_allocatable (gfc_component *comp)
    5407                 :             : {
    5408                 :      161014 :   return comp->attr.allocatable || class_allocatable (comp);
    5409                 :             : }
    5410                 :             : 
    5411                 :             : static bool
    5412                 :         271 : comp_pointer (gfc_component *comp)
    5413                 :             : {
    5414                 :         271 :   return comp->attr.pointer
    5415                 :         268 :     || comp->attr.proc_pointer
    5416                 :         268 :     || comp->attr.class_pointer
    5417                 :         539 :     || class_pointer (comp);
    5418                 :             : }
    5419                 :             : 
    5420                 :             : /* Fetch or generate an initializer for the given component.
    5421                 :             :    Only generate an initializer if generate is true.  */
    5422                 :             : 
    5423                 :             : static gfc_expr *
    5424                 :      109217 : component_initializer (gfc_component *c, bool generate)
    5425                 :             : {
    5426                 :      109217 :   gfc_expr *init = NULL;
    5427                 :             : 
    5428                 :             :   /* Allocatable components always get EXPR_NULL.
    5429                 :             :      Pointer components are only initialized when generating, and only if they
    5430                 :             :      do not already have an initializer.  */
    5431                 :      109217 :   if (comp_allocatable (c) || (generate && comp_pointer (c) && !c->initializer))
    5432                 :             :     {
    5433                 :       10789 :       init = gfc_get_null_expr (&c->loc);
    5434                 :       10789 :       init->ts = c->ts;
    5435                 :       10789 :       return init;
    5436                 :             :     }
    5437                 :             : 
    5438                 :             :   /* See if we can find the initializer immediately.  */
    5439                 :       98428 :   if (c->initializer || !generate)
    5440                 :             :     return c->initializer;
    5441                 :             : 
    5442                 :             :   /* Recursively handle derived type components.  */
    5443                 :         243 :   else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
    5444                 :          18 :     init = gfc_generate_initializer (&c->ts, true);
    5445                 :             : 
    5446                 :         225 :   else if (c->ts.type == BT_UNION && c->ts.u.derived->components)
    5447                 :             :     {
    5448                 :          15 :       gfc_component *map = NULL;
    5449                 :          15 :       gfc_constructor *ctor;
    5450                 :          15 :       gfc_expr *user_init;
    5451                 :             : 
    5452                 :             :       /* If we don't have a user initializer and we aren't generating one, this
    5453                 :             :          union has no initializer.  */
    5454                 :          15 :       user_init = get_union_initializer (c->ts.u.derived, &map);
    5455                 :          15 :       if (!user_init && !generate)
    5456                 :             :         return NULL;
    5457                 :             : 
    5458                 :             :       /* Otherwise use a structure constructor.  */
    5459                 :          15 :       init = gfc_get_structure_constructor_expr (c->ts.type, c->ts.kind,
    5460                 :             :                                                  &c->loc);
    5461                 :          15 :       init->ts = c->ts;
    5462                 :             : 
    5463                 :             :       /* If we are to generate an initializer for the union, add a constructor
    5464                 :             :          which initializes the whole union first.  */
    5465                 :          15 :       if (generate)
    5466                 :             :         {
    5467                 :          15 :           ctor = gfc_constructor_get ();
    5468                 :          15 :           ctor->expr = generate_union_initializer (c);
    5469                 :          15 :           gfc_constructor_append (&init->value.constructor, ctor);
    5470                 :             :         }
    5471                 :             : 
    5472                 :             :       /* If we found an initializer in one of our maps, apply it.  Note this
    5473                 :             :          is applied _after_ the entire-union initializer above if any.  */
    5474                 :          15 :       if (user_init)
    5475                 :             :         {
    5476                 :           0 :           ctor = gfc_constructor_get ();
    5477                 :           0 :           ctor->expr = user_init;
    5478                 :           0 :           ctor->n.component = map;
    5479                 :           0 :           gfc_constructor_append (&init->value.constructor, ctor);
    5480                 :             :         }
    5481                 :          15 :     }
    5482                 :             : 
    5483                 :             :   /* Treat simple components like locals.  */
    5484                 :             :   else
    5485                 :             :     {
    5486                 :             :       /* We MUST give an initializer, so force generation.  */
    5487                 :         210 :       init = gfc_build_init_expr (&c->ts, &c->loc, true);
    5488                 :         210 :       gfc_apply_init (&c->ts, &c->attr, init);
    5489                 :             :     }
    5490                 :             : 
    5491                 :             :   return init;
    5492                 :             : }
    5493                 :             : 
    5494                 :             : 
    5495                 :             : /* Get an expression for a default initializer of a derived type.  */
    5496                 :             : 
    5497                 :             : gfc_expr *
    5498                 :       25524 : gfc_default_initializer (gfc_typespec *ts)
    5499                 :             : {
    5500                 :       25524 :   return gfc_generate_initializer (ts, false);
    5501                 :             : }
    5502                 :             : 
    5503                 :             : /* Generate an initializer expression for an iso_c_binding type
    5504                 :             :    such as c_[fun]ptr. The appropriate initializer is c_null_[fun]ptr.  */
    5505                 :             : 
    5506                 :             : static gfc_expr *
    5507                 :           3 : generate_isocbinding_initializer (gfc_symbol *derived)
    5508                 :             : {
    5509                 :             :   /* The initializers have already been built into the c_null_[fun]ptr symbols
    5510                 :             :      from gen_special_c_interop_ptr.  */
    5511                 :           3 :   gfc_symtree *npsym = NULL;
    5512                 :           3 :   if (0 == strcmp (derived->name, "c_ptr"))
    5513                 :           2 :     gfc_find_sym_tree ("c_null_ptr", gfc_current_ns, true, &npsym);
    5514                 :           1 :   else if (0 == strcmp (derived->name, "c_funptr"))
    5515                 :           1 :     gfc_find_sym_tree ("c_null_funptr", gfc_current_ns, true, &npsym);
    5516                 :             :   else
    5517                 :           0 :     gfc_internal_error ("generate_isocbinding_initializer(): bad iso_c_binding"
    5518                 :             :                         " type, expected %<c_ptr%> or %<c_funptr%>");
    5519                 :           3 :   if (npsym)
    5520                 :             :     {
    5521                 :           3 :       gfc_expr *init = gfc_copy_expr (npsym->n.sym->value);
    5522                 :           3 :       init->symtree = npsym;
    5523                 :           3 :       init->ts.is_iso_c = true;
    5524                 :           3 :       return init;
    5525                 :             :     }
    5526                 :             : 
    5527                 :             :   return NULL;
    5528                 :             : }
    5529                 :             : 
    5530                 :             : /* Get or generate an expression for a default initializer of a derived type.
    5531                 :             :    If -finit-derived is specified, generate default initialization expressions
    5532                 :             :    for components that lack them when generate is set.  */
    5533                 :             : 
    5534                 :             : gfc_expr *
    5535                 :       55557 : gfc_generate_initializer (gfc_typespec *ts, bool generate)
    5536                 :             : {
    5537                 :       55557 :   gfc_expr *init, *tmp;
    5538                 :       55557 :   gfc_component *comp;
    5539                 :             : 
    5540                 :       55557 :   generate = flag_init_derived && generate;
    5541                 :             : 
    5542                 :       55557 :   if (ts->u.derived->ts.is_iso_c && generate)
    5543                 :           3 :     return generate_isocbinding_initializer (ts->u.derived);
    5544                 :             : 
    5545                 :             :   /* See if we have a default initializer in this, but not in nested
    5546                 :             :      types (otherwise we could use gfc_has_default_initializer()).
    5547                 :             :      We don't need to check if we are going to generate them.  */
    5548                 :       55554 :   comp = ts->u.derived->components;
    5549                 :       55554 :   if (!generate)
    5550                 :             :     {
    5551                 :       99374 :       for (; comp; comp = comp->next)
    5552                 :       70694 :         if (comp->initializer || comp_allocatable (comp))
    5553                 :             :           break;
    5554                 :             :     }
    5555                 :             : 
    5556                 :       55554 :   if (!comp)
    5557                 :             :     return NULL;
    5558                 :             : 
    5559                 :       26874 :   init = gfc_get_structure_constructor_expr (ts->type, ts->kind,
    5560                 :             :                                              &ts->u.derived->declared_at);
    5561                 :       26874 :   init->ts = *ts;
    5562                 :             : 
    5563                 :      136090 :   for (comp = ts->u.derived->components; comp; comp = comp->next)
    5564                 :             :     {
    5565                 :      109217 :       gfc_constructor *ctor = gfc_constructor_get();
    5566                 :             : 
    5567                 :             :       /* Fetch or generate an initializer for the component.  */
    5568                 :      109217 :       tmp = component_initializer (comp, generate);
    5569                 :      109217 :       if (tmp)
    5570                 :             :         {
    5571                 :             :           /* Save the component ref for STRUCTUREs and UNIONs.  */
    5572                 :       99254 :           if (ts->u.derived->attr.flavor == FL_STRUCT
    5573                 :       98934 :               || ts->u.derived->attr.flavor == FL_UNION)
    5574                 :         343 :             ctor->n.component = comp;
    5575                 :             : 
    5576                 :             :           /* If the initializer was not generated, we need a copy.  */
    5577                 :       99254 :           ctor->expr = comp->initializer ? gfc_copy_expr (tmp) : tmp;
    5578                 :       99254 :           if ((comp->ts.type != tmp->ts.type || comp->ts.kind != tmp->ts.kind)
    5579                 :       17029 :               && !comp->attr.pointer && !comp->attr.proc_pointer)
    5580                 :             :             {
    5581                 :         262 :               bool val;
    5582                 :         262 :               val = gfc_convert_type_warn (ctor->expr, &comp->ts, 1, false);
    5583                 :         262 :               if (val == false)
    5584                 :             :                 return NULL;
    5585                 :             :             }
    5586                 :             :         }
    5587                 :             : 
    5588                 :      109216 :       gfc_constructor_append (&init->value.constructor, ctor);
    5589                 :             :     }
    5590                 :             : 
    5591                 :             :   return init;
    5592                 :             : }
    5593                 :             : 
    5594                 :             : 
    5595                 :             : /* Given a symbol, create an expression node with that symbol as a
    5596                 :             :    variable. If the symbol is array valued, setup a reference of the
    5597                 :             :    whole array.  */
    5598                 :             : 
    5599                 :             : gfc_expr *
    5600                 :       12817 : gfc_get_variable_expr (gfc_symtree *var)
    5601                 :             : {
    5602                 :       12817 :   gfc_expr *e;
    5603                 :             : 
    5604                 :       12817 :   e = gfc_get_expr ();
    5605                 :       12817 :   e->expr_type = EXPR_VARIABLE;
    5606                 :       12817 :   e->symtree = var;
    5607                 :       12817 :   e->ts = var->n.sym->ts;
    5608                 :             : 
    5609                 :       12817 :   if (var->n.sym->attr.flavor != FL_PROCEDURE
    5610                 :        8867 :       && ((var->n.sym->as != NULL && var->n.sym->ts.type != BT_CLASS)
    5611                 :        6813 :            || (var->n.sym->ts.type == BT_CLASS && var->n.sym->ts.u.derived
    5612                 :        4124 :                && CLASS_DATA (var->n.sym)
    5613                 :        4124 :                && CLASS_DATA (var->n.sym)->as)))
    5614                 :             :     {
    5615                 :        5432 :       gfc_array_spec *as = var->n.sym->ts.type == BT_CLASS
    5616                 :        3743 :                              ? CLASS_DATA (var->n.sym)->as
    5617                 :             :                              : var->n.sym->as;
    5618                 :        3743 :       e->rank = as->rank;
    5619                 :        3743 :       e->corank = as->corank;
    5620                 :        3743 :       e->ref = gfc_get_ref ();
    5621                 :        3743 :       e->ref->type = REF_ARRAY;
    5622                 :        3743 :       e->ref->u.ar.type = AR_FULL;
    5623                 :        3743 :       e->ref->u.ar.as = gfc_copy_array_spec (as);
    5624                 :             :     }
    5625                 :             : 
    5626                 :       12817 :   return e;
    5627                 :             : }
    5628                 :             : 
    5629                 :             : 
    5630                 :             : /* Adds a full array reference to an expression, as needed.  */
    5631                 :             : 
    5632                 :             : void
    5633                 :       35354 : gfc_add_full_array_ref (gfc_expr *e, gfc_array_spec *as)
    5634                 :             : {
    5635                 :       35354 :   gfc_ref *ref;
    5636                 :       35367 :   for (ref = e->ref; ref; ref = ref->next)
    5637                 :         191 :     if (!ref->next)
    5638                 :             :       break;
    5639                 :       35354 :   if (ref)
    5640                 :             :     {
    5641                 :         178 :       ref->next = gfc_get_ref ();
    5642                 :         178 :       ref = ref->next;
    5643                 :             :     }
    5644                 :             :   else
    5645                 :             :     {
    5646                 :       35176 :       e->ref = gfc_get_ref ();
    5647                 :       35176 :       ref = e->ref;
    5648                 :             :     }
    5649                 :       35354 :   ref->type = REF_ARRAY;
    5650                 :       35354 :   ref->u.ar.type = AR_FULL;
    5651                 :       35354 :   ref->u.ar.dimen = e->rank;
    5652                 :             :   /* Do not set the corank here, or resolve will not be able to set correct
    5653                 :             :      dimen-types for the coarray.  */
    5654                 :       35354 :   ref->u.ar.where = e->where;
    5655                 :       35354 :   ref->u.ar.as = as;
    5656                 :       35354 : }
    5657                 :             : 
    5658                 :             : 
    5659                 :             : gfc_expr *
    5660                 :      155472 : gfc_lval_expr_from_sym (gfc_symbol *sym)
    5661                 :             : {
    5662                 :      155472 :   gfc_expr *lval;
    5663                 :      155472 :   gfc_array_spec *as;
    5664                 :      155472 :   lval = gfc_get_expr ();
    5665                 :      155472 :   lval->expr_type = EXPR_VARIABLE;
    5666                 :      155472 :   lval->where = sym->declared_at;
    5667                 :      155472 :   lval->ts = sym->ts;
    5668                 :      155472 :   lval->symtree = gfc_find_symtree (sym->ns->sym_root, sym->name);
    5669                 :             : 
    5670                 :             :   /* It will always be a full array.  */
    5671                 :      155472 :   as = IS_CLASS_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
    5672                 :      155472 :   lval->rank = as ? as->rank : 0;
    5673                 :      155472 :   lval->corank = as ? as->corank : 0;
    5674                 :      155472 :   if (lval->rank || lval->corank)
    5675                 :       33914 :     gfc_add_full_array_ref (lval, as);
    5676                 :      155472 :   return lval;
    5677                 :             : }
    5678                 :             : 
    5679                 :             : 
    5680                 :             : /* Returns the array_spec of a full array expression.  A NULL is
    5681                 :             :    returned otherwise.  */
    5682                 :             : gfc_array_spec *
    5683                 :       24755 : gfc_get_full_arrayspec_from_expr (gfc_expr *expr)
    5684                 :             : {
    5685                 :       24755 :   gfc_array_spec *as;
    5686                 :       24755 :   gfc_ref *ref;
    5687                 :             : 
    5688                 :       24755 :   if (expr->rank == 0)
    5689                 :             :     return NULL;
    5690                 :             : 
    5691                 :             :   /* Follow any component references.  */
    5692                 :       24755 :   if (expr->expr_type == EXPR_VARIABLE
    5693                 :       24755 :       || expr->expr_type == EXPR_CONSTANT)
    5694                 :             :     {
    5695                 :       19058 :       if (expr->symtree)
    5696                 :       19058 :         as = expr->symtree->n.sym->as;
    5697                 :             :       else
    5698                 :             :         as = NULL;
    5699                 :             : 
    5700                 :       39934 :       for (ref = expr->ref; ref; ref = ref->next)
    5701                 :             :         {
    5702                 :       20876 :           switch (ref->type)
    5703                 :             :             {
    5704                 :        1649 :             case REF_COMPONENT:
    5705                 :        1649 :               as = ref->u.c.component->as;
    5706                 :        1649 :               continue;
    5707                 :             : 
    5708                 :          24 :             case REF_SUBSTRING:
    5709                 :          24 :             case REF_INQUIRY:
    5710                 :          24 :               continue;
    5711                 :             : 
    5712                 :       19203 :             case REF_ARRAY:
    5713                 :       19203 :               {
    5714                 :       19203 :                 switch (ref->u.ar.type)
    5715                 :             :                   {
    5716                 :        2125 :                   case AR_ELEMENT:
    5717                 :        2125 :                   case AR_SECTION:
    5718                 :        2125 :                   case AR_UNKNOWN:
    5719                 :        2125 :                     as = NULL;
    5720                 :        2125 :                     continue;
    5721                 :             : 
    5722                 :             :                   case AR_FULL:
    5723                 :             :                     break;
    5724                 :             :                   }
    5725                 :             :                 break;
    5726                 :             :               }
    5727                 :             :             }
    5728                 :             :         }
    5729                 :             :     }
    5730                 :             :   else
    5731                 :             :     as = NULL;
    5732                 :             : 
    5733                 :             :   return as;
    5734                 :             : }
    5735                 :             : 
    5736                 :             : 
    5737                 :             : /* General expression traversal function.  */
    5738                 :             : 
    5739                 :             : bool
    5740                 :     1093522 : gfc_traverse_expr (gfc_expr *expr, gfc_symbol *sym,
    5741                 :             :                    bool (*func)(gfc_expr *, gfc_symbol *, int*),
    5742                 :             :                    int f)
    5743                 :             : {
    5744                 :     1093522 :   gfc_array_ref ar;
    5745                 :     1093522 :   gfc_ref *ref;
    5746                 :     1093522 :   gfc_actual_arglist *args;
    5747                 :     1093522 :   gfc_constructor *c;
    5748                 :     1093522 :   int i;
    5749                 :             : 
    5750                 :     1093522 :   if (!expr)
    5751                 :             :     return false;
    5752                 :             : 
    5753                 :      471870 :   if ((*func) (expr, sym, &f))
    5754                 :             :     return true;
    5755                 :             : 
    5756                 :             :   /* Descend into length type parameter of character expressions only for
    5757                 :             :      non-negative f.  */
    5758                 :      465210 :   if (f >= 0
    5759                 :      443410 :       && expr->ts.type == BT_CHARACTER
    5760                 :       11639 :       && expr->ts.u.cl
    5761                 :        4250 :       && expr->ts.u.cl->length
    5762                 :        2238 :       && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
    5763                 :      466139 :       && gfc_traverse_expr (expr->ts.u.cl->length, sym, func, f))
    5764                 :             :     return true;
    5765                 :             : 
    5766                 :      465209 :   switch (expr->expr_type)
    5767                 :             :     {
    5768                 :       24434 :     case EXPR_PPC:
    5769                 :       24434 :     case EXPR_COMPCALL:
    5770                 :       24434 :     case EXPR_FUNCTION:
    5771                 :       54948 :       for (args = expr->value.function.actual; args; args = args->next)
    5772                 :             :         {
    5773                 :       30619 :           if (gfc_traverse_expr (args->expr, sym, func, f))
    5774                 :             :             return true;
    5775                 :             :         }
    5776                 :             :       break;
    5777                 :             : 
    5778                 :             :     case EXPR_VARIABLE:
    5779                 :             :     case EXPR_CONSTANT:
    5780                 :             :     case EXPR_NULL:
    5781                 :             :     case EXPR_SUBSTRING:
    5782                 :             :       break;
    5783                 :             : 
    5784                 :        4588 :     case EXPR_STRUCTURE:
    5785                 :        4588 :     case EXPR_ARRAY:
    5786                 :        4588 :       for (c = gfc_constructor_first (expr->value.constructor);
    5787                 :       28353 :            c; c = gfc_constructor_next (c))
    5788                 :             :         {
    5789                 :       23765 :           if (gfc_traverse_expr (c->expr, sym, func, f))
    5790                 :             :             return true;
    5791                 :       23765 :           if (c->iterator)
    5792                 :             :             {
    5793                 :         489 :               if (gfc_traverse_expr (c->iterator->var, sym, func, f))
    5794                 :             :                 return true;
    5795                 :         489 :               if (gfc_traverse_expr (c->iterator->start, sym, func, f))
    5796                 :             :                 return true;
    5797                 :         489 :               if (gfc_traverse_expr (c->iterator->end, sym, func, f))
    5798                 :             :                 return true;
    5799                 :         489 :               if (gfc_traverse_expr (c->iterator->step, sym, func, f))
    5800                 :             :                 return true;
    5801                 :             :             }
    5802                 :             :         }
    5803                 :             :       break;
    5804                 :             : 
    5805                 :        9384 :     case EXPR_OP:
    5806                 :        9384 :       if (gfc_traverse_expr (expr->value.op.op1, sym, func, f))
    5807                 :             :         return true;
    5808                 :        7709 :       if (gfc_traverse_expr (expr->value.op.op2, sym, func, f))
    5809                 :             :         return true;
    5810                 :             :       break;
    5811                 :             : 
    5812                 :           6 :     case EXPR_CONDITIONAL:
    5813                 :           6 :       if (gfc_traverse_expr (expr->value.conditional.condition, sym, func, f))
    5814                 :             :         return true;
    5815                 :           6 :       if (gfc_traverse_expr (expr->value.conditional.true_expr, sym, func, f))
    5816                 :             :         return true;
    5817                 :           6 :       if (gfc_traverse_expr (expr->value.conditional.false_expr, sym, func, f))
    5818                 :             :         return true;
    5819                 :             :       break;
    5820                 :             : 
    5821                 :           0 :     default:
    5822                 :           0 :       gcc_unreachable ();
    5823                 :      463050 :       break;
    5824                 :             :     }
    5825                 :             : 
    5826                 :      463050 :   ref = expr->ref;
    5827                 :      477023 :   while (ref != NULL)
    5828                 :             :     {
    5829                 :       17932 :       switch (ref->type)
    5830                 :             :         {
    5831                 :       16407 :         case  REF_ARRAY:
    5832                 :       16407 :           ar = ref->u.ar;
    5833                 :      208434 :           for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
    5834                 :             :             {
    5835                 :      195809 :               if (gfc_traverse_expr (ar.start[i], sym, func, f))
    5836                 :             :                 return true;
    5837                 :      192028 :               if (gfc_traverse_expr (ar.end[i], sym, func, f))
    5838                 :             :                 return true;
    5839                 :      192027 :               if (gfc_traverse_expr (ar.stride[i], sym, func, f))
    5840                 :             :                 return true;
    5841                 :             :             }
    5842                 :             :           break;
    5843                 :             : 
    5844                 :         801 :         case REF_SUBSTRING:
    5845                 :         801 :           if (gfc_traverse_expr (ref->u.ss.start, sym, func, f))
    5846                 :             :             return true;
    5847                 :         629 :           if (gfc_traverse_expr (ref->u.ss.end, sym, func, f))
    5848                 :             :             return true;
    5849                 :             :           break;
    5850                 :             : 
    5851                 :         720 :         case REF_COMPONENT:
    5852                 :         720 :           if (f >= 0
    5853                 :         705 :               && ref->u.c.component->ts.type == BT_CHARACTER
    5854                 :          91 :               && ref->u.c.component->ts.u.cl
    5855                 :          91 :               && ref->u.c.component->ts.u.cl->length
    5856                 :          91 :               && ref->u.c.component->ts.u.cl->length->expr_type
    5857                 :             :               != EXPR_CONSTANT
    5858                 :         720 :               && gfc_traverse_expr (ref->u.c.component->ts.u.cl->length,
    5859                 :             :                                     sym, func, f))
    5860                 :             :             return true;
    5861                 :             : 
    5862                 :         720 :           if (ref->u.c.component->as)
    5863                 :         306 :             for (i = 0; i < ref->u.c.component->as->rank
    5864                 :         572 :                             + ref->u.c.component->as->corank; i++)
    5865                 :             :               {
    5866                 :         306 :                 if (gfc_traverse_expr (ref->u.c.component->as->lower[i],
    5867                 :             :                                        sym, func, f))
    5868                 :             :                   return true;
    5869                 :         306 :                 if (gfc_traverse_expr (ref->u.c.component->as->upper[i],
    5870                 :             :                                        sym, func, f))
    5871                 :             :                   return true;
    5872                 :             :               }
    5873                 :             :           break;
    5874                 :             : 
    5875                 :             :         case REF_INQUIRY:
    5876                 :             :           return false;
    5877                 :             : 
    5878                 :           0 :         default:
    5879                 :           0 :           gcc_unreachable ();
    5880                 :             :         }
    5881                 :       13973 :       ref = ref->next;
    5882                 :             :     }
    5883                 :             :   return false;
    5884                 :             : }
    5885                 :             : 
    5886                 :             : /* Traverse expr, marking all EXPR_VARIABLE symbols referenced.  */
    5887                 :             : 
    5888                 :             : static bool
    5889                 :        3927 : expr_set_symbols_referenced (gfc_expr *expr,
    5890                 :             :                              gfc_symbol *sym ATTRIBUTE_UNUSED,
    5891                 :             :                              int *f ATTRIBUTE_UNUSED)
    5892                 :             : {
    5893                 :        3927 :   if (expr->expr_type != EXPR_VARIABLE)
    5894                 :             :     return false;
    5895                 :         933 :   gfc_set_sym_referenced (expr->symtree->n.sym);
    5896                 :         933 :   return false;
    5897                 :             : }
    5898                 :             : 
    5899                 :             : void
    5900                 :        1236 : gfc_expr_set_symbols_referenced (gfc_expr *expr)
    5901                 :             : {
    5902                 :        1236 :   gfc_traverse_expr (expr, NULL, expr_set_symbols_referenced, 0);
    5903                 :        1236 : }
    5904                 :             : 
    5905                 :             : 
    5906                 :             : /* Determine if an expression is a procedure pointer component and return
    5907                 :             :    the component in that case.  Otherwise return NULL.  */
    5908                 :             : 
    5909                 :             : gfc_component *
    5910                 :     3215874 : gfc_get_proc_ptr_comp (gfc_expr *expr)
    5911                 :             : {
    5912                 :     3215874 :   gfc_ref *ref;
    5913                 :             : 
    5914                 :     3215874 :   if (!expr || !expr->ref)
    5915                 :             :     return NULL;
    5916                 :             : 
    5917                 :             :   ref = expr->ref;
    5918                 :      244707 :   while (ref->next)
    5919                 :             :     ref = ref->next;
    5920                 :             : 
    5921                 :      221765 :   if (ref->type == REF_COMPONENT
    5922                 :       18711 :       && ref->u.c.component->attr.proc_pointer)
    5923                 :        8014 :     return ref->u.c.component;
    5924                 :             : 
    5925                 :             :   return NULL;
    5926                 :             : }
    5927                 :             : 
    5928                 :             : 
    5929                 :             : /* Determine if an expression is a procedure pointer component.  */
    5930                 :             : 
    5931                 :             : bool
    5932                 :     1103809 : gfc_is_proc_ptr_comp (gfc_expr *expr)
    5933                 :             : {
    5934                 :     1103809 :   return (gfc_get_proc_ptr_comp (expr) != NULL);
    5935                 :             : }
    5936                 :             : 
    5937                 :             : 
    5938                 :             : /* Determine if an expression is a function with an allocatable class scalar
    5939                 :             :    result.  */
    5940                 :             : bool
    5941                 :      387150 : gfc_is_alloc_class_scalar_function (gfc_expr *expr)
    5942                 :             : {
    5943                 :      387150 :   if (expr->expr_type == EXPR_FUNCTION
    5944                 :       71789 :       && ((expr->value.function.esym
    5945                 :       40024 :            && expr->value.function.esym->result
    5946                 :       40023 :            && expr->value.function.esym->result->ts.type == BT_CLASS
    5947                 :        1026 :            && !CLASS_DATA (expr->value.function.esym->result)->attr.dimension
    5948                 :         893 :            && CLASS_DATA (expr->value.function.esym->result)->attr.allocatable)
    5949                 :       71168 :           || (expr->ts.type == BT_CLASS
    5950                 :         760 :               && CLASS_DATA (expr)->attr.allocatable
    5951                 :         397 :               && !CLASS_DATA (expr)->attr.dimension)))
    5952                 :         861 :     return true;
    5953                 :             : 
    5954                 :             :   return false;
    5955                 :             : }
    5956                 :             : 
    5957                 :             : 
    5958                 :             : /* Determine if an expression is a function with an allocatable class array
    5959                 :             :    result.  */
    5960                 :             : bool
    5961                 :      166813 : gfc_is_class_array_function (gfc_expr *expr)
    5962                 :             : {
    5963                 :      166813 :   if (expr->expr_type == EXPR_FUNCTION
    5964                 :       80690 :       && expr->value.function.esym
    5965                 :       43750 :       && expr->value.function.esym->result
    5966                 :       43749 :       && expr->value.function.esym->result->ts.type == BT_CLASS
    5967                 :        2427 :       && CLASS_DATA (expr->value.function.esym->result)->attr.dimension
    5968                 :        1557 :       && (CLASS_DATA (expr->value.function.esym->result)->attr.allocatable
    5969                 :         312 :           || CLASS_DATA (expr->value.function.esym->result)->attr.pointer))
    5970                 :        1557 :     return true;
    5971                 :             : 
    5972                 :             :   return false;
    5973                 :             : }
    5974                 :             : 
    5975                 :             : 
    5976                 :             : /* Walk an expression tree and check each variable encountered for being typed.
    5977                 :             :    If strict is not set, a top-level variable is tolerated untyped in -std=gnu
    5978                 :             :    mode as is a basic arithmetic expression using those; this is for things in
    5979                 :             :    legacy-code like:
    5980                 :             : 
    5981                 :             :      INTEGER :: arr(n), n
    5982                 :             :      INTEGER :: arr(n + 1), n
    5983                 :             : 
    5984                 :             :    The namespace is needed for IMPLICIT typing.  */
    5985                 :             : 
    5986                 :             : static gfc_namespace* check_typed_ns;
    5987                 :             : 
    5988                 :             : static bool
    5989                 :       80360 : expr_check_typed_help (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
    5990                 :             :                        int* f ATTRIBUTE_UNUSED)
    5991                 :             : {
    5992                 :       80360 :   bool t;
    5993                 :             : 
    5994                 :       80360 :   if (e->expr_type != EXPR_VARIABLE)
    5995                 :             :     return false;
    5996                 :             : 
    5997                 :        2420 :   gcc_assert (e->symtree);
    5998                 :        2420 :   t = gfc_check_symbol_typed (e->symtree->n.sym, check_typed_ns,
    5999                 :             :                               true, e->where);
    6000                 :             : 
    6001                 :        2420 :   return (!t);
    6002                 :             : }
    6003                 :             : 
    6004                 :             : bool
    6005                 :       86236 : gfc_expr_check_typed (gfc_expr* e, gfc_namespace* ns, bool strict)
    6006                 :             : {
    6007                 :       86236 :   bool error_found;
    6008                 :             : 
    6009                 :             :   /* If this is a top-level variable or EXPR_OP, do the check with strict given
    6010                 :             :      to us.  */
    6011                 :       86236 :   if (!strict)
    6012                 :             :     {
    6013                 :       85935 :       if (e->expr_type == EXPR_VARIABLE && !e->ref)
    6014                 :        8334 :         return gfc_check_symbol_typed (e->symtree->n.sym, ns, strict, e->where);
    6015                 :             : 
    6016                 :       77601 :       if (e->expr_type == EXPR_OP)
    6017                 :             :         {
    6018                 :        1734 :           bool t = true;
    6019                 :             : 
    6020                 :        1734 :           gcc_assert (e->value.op.op1);
    6021                 :        1734 :           t = gfc_expr_check_typed (e->value.op.op1, ns, strict);
    6022                 :             : 
    6023                 :        1734 :           if (t && e->value.op.op2)
    6024                 :        1365 :             t = gfc_expr_check_typed (e->value.op.op2, ns, strict);
    6025                 :             : 
    6026                 :        1734 :           return t;
    6027                 :             :         }
    6028                 :             :     }
    6029                 :             : 
    6030                 :             :   /* Otherwise, walk the expression and do it strictly.  */
    6031                 :       76168 :   check_typed_ns = ns;
    6032                 :       76168 :   error_found = gfc_traverse_expr (e, NULL, &expr_check_typed_help, 0);
    6033                 :             : 
    6034                 :       76168 :   return error_found ? false : true;
    6035                 :             : }
    6036                 :             : 
    6037                 :             : 
    6038                 :             : /* This function returns true if it contains any references to PDT KIND
    6039                 :             :    or LEN parameters.  */
    6040                 :             : 
    6041                 :             : static bool
    6042                 :      167392 : derived_parameter_expr (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
    6043                 :             :                         int* f ATTRIBUTE_UNUSED)
    6044                 :             : {
    6045                 :      167392 :   if (e->expr_type != EXPR_VARIABLE)
    6046                 :             :     return false;
    6047                 :             : 
    6048                 :        2640 :   gcc_assert (e->symtree);
    6049                 :        2640 :   if (e->symtree->n.sym->attr.pdt_kind
    6050                 :        2420 :       || e->symtree->n.sym->attr.pdt_len)
    6051                 :         527 :     return true;
    6052                 :             : 
    6053                 :             :   return false;
    6054                 :             : }
    6055                 :             : 
    6056                 :             : 
    6057                 :             : bool
    6058                 :      137129 : gfc_derived_parameter_expr (gfc_expr *e)
    6059                 :             : {
    6060                 :      137129 :   return gfc_traverse_expr (e, NULL, &derived_parameter_expr, 0);
    6061                 :             : }
    6062                 :             : 
    6063                 :             : 
    6064                 :             : /* This function returns the overall type of a type parameter spec list.
    6065                 :             :    If all the specs are explicit, SPEC_EXPLICIT is returned. If any of the
    6066                 :             :    parameters are assumed/deferred then SPEC_ASSUMED/DEFERRED is returned
    6067                 :             :    unless derived is not NULL.  In this latter case, all the LEN parameters
    6068                 :             :    must be either assumed or deferred for the return argument to be set to
    6069                 :             :    anything other than SPEC_EXPLICIT.  */
    6070                 :             : 
    6071                 :             : gfc_param_spec_type
    6072                 :         109 : gfc_spec_list_type (gfc_actual_arglist *param_list, gfc_symbol *derived)
    6073                 :             : {
    6074                 :         109 :   gfc_param_spec_type res = SPEC_EXPLICIT;
    6075                 :         109 :   gfc_component *c;
    6076                 :         109 :   bool seen_assumed = false;
    6077                 :         109 :   bool seen_deferred = false;
    6078                 :         109 :   bool seen_len = false;
    6079                 :             : 
    6080                 :         109 :   if (derived == NULL)
    6081                 :             :     {
    6082                 :         185 :       for (; param_list; param_list = param_list->next)
    6083                 :         127 :         if (param_list->spec_type == SPEC_ASSUMED
    6084                 :         127 :             || param_list->spec_type == SPEC_DEFERRED)
    6085                 :             :           return param_list->spec_type;
    6086                 :             :     }
    6087                 :             :   else
    6088                 :             :     {
    6089                 :         142 :       for (; param_list; param_list = param_list->next)
    6090                 :             :         {
    6091                 :          94 :           c = gfc_find_component (derived, param_list->name,
    6092                 :             :                                   true, true, NULL);
    6093                 :          94 :           gcc_assert (c != NULL);
    6094                 :          94 :           if (c->attr.pdt_kind)
    6095                 :          53 :             continue;
    6096                 :          41 :           else if (param_list->spec_type == SPEC_EXPLICIT)
    6097                 :             :             return SPEC_EXPLICIT;
    6098                 :          38 :           seen_assumed = param_list->spec_type == SPEC_ASSUMED;
    6099                 :          38 :           seen_deferred = param_list->spec_type == SPEC_DEFERRED;
    6100                 :          38 :           if (c->attr.pdt_len)
    6101                 :          38 :             seen_len = true;
    6102                 :             :           if (seen_assumed && seen_deferred)
    6103                 :             :             return SPEC_EXPLICIT;
    6104                 :             :         }
    6105                 :          48 :       res = (seen_assumed || !seen_len) ? SPEC_ASSUMED : SPEC_DEFERRED;
    6106                 :             :     }
    6107                 :             :   return res;
    6108                 :             : }
    6109                 :             : 
    6110                 :             : 
    6111                 :             : bool
    6112                 :       20591 : gfc_ref_this_image (gfc_ref *ref)
    6113                 :             : {
    6114                 :       20591 :   int n;
    6115                 :             : 
    6116                 :       20591 :   gcc_assert (ref->type == REF_ARRAY && ref->u.ar.codimen > 0);
    6117                 :             : 
    6118                 :       44916 :   for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
    6119                 :       27196 :     if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
    6120                 :             :       return false;
    6121                 :             : 
    6122                 :             :   return true;
    6123                 :             : }
    6124                 :             : 
    6125                 :             : gfc_expr *
    6126                 :        2060 : gfc_find_team_co (gfc_expr *e, enum gfc_array_ref_team_type req_team_type)
    6127                 :             : {
    6128                 :        2060 :   gfc_ref *ref;
    6129                 :             : 
    6130                 :        3017 :   for (ref = e->ref; ref; ref = ref->next)
    6131                 :         974 :     if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0
    6132                 :         974 :         && ref->u.ar.team_type == req_team_type)
    6133                 :          17 :       return ref->u.ar.team;
    6134                 :             : 
    6135                 :        2043 :   if (e->expr_type == EXPR_FUNCTION && e->value.function.actual->expr)
    6136                 :        2191 :     for (ref = e->value.function.actual->expr->ref; ref;
    6137                 :        1105 :          ref = ref->next)
    6138                 :        1112 :       if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0
    6139                 :        1086 :           && ref->u.ar.team_type == req_team_type)
    6140                 :           7 :         return ref->u.ar.team;
    6141                 :             : 
    6142                 :             :   return NULL;
    6143                 :             : }
    6144                 :             : 
    6145                 :             : gfc_expr *
    6146                 :        1030 : gfc_find_stat_co (gfc_expr *e)
    6147                 :             : {
    6148                 :        1030 :   gfc_ref *ref;
    6149                 :             : 
    6150                 :        1030 :   for (ref = e->ref; ref; ref = ref->next)
    6151                 :         487 :     if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
    6152                 :         487 :       return ref->u.ar.stat;
    6153                 :             : 
    6154                 :         543 :   if (e->value.function.actual->expr)
    6155                 :         556 :     for (ref = e->value.function.actual->expr->ref; ref;
    6156                 :          13 :          ref = ref->next)
    6157                 :         556 :       if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
    6158                 :         543 :         return ref->u.ar.stat;
    6159                 :             : 
    6160                 :             :   return NULL;
    6161                 :             : }
    6162                 :             : 
    6163                 :             : bool
    6164                 :      831458 : gfc_is_coindexed (gfc_expr *e)
    6165                 :             : {
    6166                 :      831458 :   gfc_ref *ref;
    6167                 :             : 
    6168                 :      831458 :   if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
    6169                 :         532 :       && e->value.function.isym->id == GFC_ISYM_CAF_GET)
    6170                 :           0 :     e = e->value.function.actual->expr;
    6171                 :             : 
    6172                 :     1234772 :   for (ref = e->ref; ref; ref = ref->next)
    6173                 :      419097 :     if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
    6174                 :       15783 :       return !gfc_ref_this_image (ref);
    6175                 :             : 
    6176                 :             :   return false;
    6177                 :             : }
    6178                 :             : 
    6179                 :             : 
    6180                 :             : /* Coarrays are variables with a corank but not being coindexed. However, also
    6181                 :             :    the following is a coarray: A subobject of a coarray is a coarray if it does
    6182                 :             :    not have any cosubscripts, vector subscripts, allocatable component
    6183                 :             :    selection, or pointer component selection. (F2008, 2.4.7)  */
    6184                 :             : 
    6185                 :             : bool
    6186                 :      165612 : gfc_is_coarray (gfc_expr *e)
    6187                 :             : {
    6188                 :      165612 :   gfc_ref *ref;
    6189                 :      165612 :   gfc_symbol *sym;
    6190                 :      165612 :   gfc_component *comp;
    6191                 :      165612 :   bool coindexed;
    6192                 :      165612 :   bool coarray;
    6193                 :      165612 :   int i;
    6194                 :             : 
    6195                 :      165612 :   if (e->expr_type != EXPR_VARIABLE)
    6196                 :             :     return false;
    6197                 :             : 
    6198                 :      163099 :   coindexed = false;
    6199                 :      163099 :   sym = e->symtree->n.sym;
    6200                 :             : 
    6201                 :      163099 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
    6202                 :       16628 :     coarray = CLASS_DATA (sym)->attr.codimension;
    6203                 :             :   else
    6204                 :      146471 :     coarray = sym->attr.codimension;
    6205                 :             : 
    6206                 :      345566 :   for (ref = e->ref; ref; ref = ref->next)
    6207                 :      182467 :     switch (ref->type)
    6208                 :             :     {
    6209                 :       24991 :       case REF_COMPONENT:
    6210                 :       24991 :         comp = ref->u.c.component;
    6211                 :       24991 :         if (comp->ts.type == BT_CLASS && comp->attr.class_ok
    6212                 :        2398 :             && (CLASS_DATA (comp)->attr.class_pointer
    6213                 :        2098 :                 || CLASS_DATA (comp)->attr.allocatable))
    6214                 :             :           {
    6215                 :        2398 :             coindexed = false;
    6216                 :        2398 :             coarray = CLASS_DATA (comp)->attr.codimension;
    6217                 :             :           }
    6218                 :       22593 :         else if (comp->attr.pointer || comp->attr.allocatable)
    6219                 :             :           {
    6220                 :       21221 :             coindexed = false;
    6221                 :       21221 :             coarray = comp->attr.codimension;
    6222                 :             :           }
    6223                 :             :         break;
    6224                 :             : 
    6225                 :      157086 :      case REF_ARRAY:
    6226                 :      157086 :         if (!coarray)
    6227                 :             :           break;
    6228                 :             : 
    6229                 :        4807 :         if (ref->u.ar.codimen > 0 && !gfc_ref_this_image (ref))
    6230                 :             :           {
    6231                 :             :             coindexed = true;
    6232                 :             :             break;
    6233                 :             :           }
    6234                 :             : 
    6235                 :        7791 :         for (i = 0; i < ref->u.ar.dimen; i++)
    6236                 :        3409 :           if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
    6237                 :             :             {
    6238                 :             :               coarray = false;
    6239                 :             :               break;
    6240                 :             :             }
    6241                 :             :         break;
    6242                 :             : 
    6243                 :             :      case REF_SUBSTRING:
    6244                 :             :      case REF_INQUIRY:
    6245                 :             :         break;
    6246                 :             :     }
    6247                 :             : 
    6248                 :      163099 :   return coarray && !coindexed;
    6249                 :             : }
    6250                 :             : 
    6251                 :             : 
    6252                 :             : /* Check whether the expression has an ultimate allocatable component.
    6253                 :             :    Being itself allocatable does not count.  */
    6254                 :             : bool
    6255                 :         341 : gfc_has_ultimate_allocatable (gfc_expr *e)
    6256                 :             : {
    6257                 :         341 :   gfc_ref *ref, *last = NULL;
    6258                 :             : 
    6259                 :         341 :   if (e->expr_type != EXPR_VARIABLE)
    6260                 :             :     return false;
    6261                 :             : 
    6262                 :         584 :   for (ref = e->ref; ref; ref = ref->next)
    6263                 :         243 :     if (ref->type == REF_COMPONENT)
    6264                 :          10 :       last = ref;
    6265                 :             : 
    6266                 :         341 :   if (last && last->u.c.component->ts.type == BT_CLASS)
    6267                 :           0 :     return CLASS_DATA (last->u.c.component)->attr.alloc_comp;
    6268                 :           9 :   else if (last && last->u.c.component->ts.type == BT_DERIVED)
    6269                 :           1 :     return last->u.c.component->ts.u.derived->attr.alloc_comp;
    6270                 :         332 :   else if (last)
    6271                 :             :     return false;
    6272                 :             : 
    6273                 :         332 :   if (e->ts.type == BT_CLASS)
    6274                 :           4 :     return CLASS_DATA (e)->attr.alloc_comp;
    6275                 :         328 :   else if (e->ts.type == BT_DERIVED)
    6276                 :         147 :     return e->ts.u.derived->attr.alloc_comp;
    6277                 :             :   else
    6278                 :             :     return false;
    6279                 :             : }
    6280                 :             : 
    6281                 :             : 
    6282                 :             : /* Check whether the expression has an pointer component.
    6283                 :             :    Being itself a pointer does not count.  */
    6284                 :             : bool
    6285                 :         306 : gfc_has_ultimate_pointer (gfc_expr *e)
    6286                 :             : {
    6287                 :         306 :   gfc_ref *ref, *last = NULL;
    6288                 :             : 
    6289                 :         306 :   if (e->expr_type != EXPR_VARIABLE)
    6290                 :             :     return false;
    6291                 :             : 
    6292                 :         749 :   for (ref = e->ref; ref; ref = ref->next)
    6293                 :         443 :     if (ref->type == REF_COMPONENT)
    6294                 :          99 :       last = ref;
    6295                 :             : 
    6296                 :         306 :   if (last && last->u.c.component->ts.type == BT_CLASS)
    6297                 :           0 :     return CLASS_DATA (last->u.c.component)->attr.pointer_comp;
    6298                 :          96 :   else if (last && last->u.c.component->ts.type == BT_DERIVED)
    6299                 :           4 :     return last->u.c.component->ts.u.derived->attr.pointer_comp;
    6300                 :         210 :   else if (last)
    6301                 :             :     return false;
    6302                 :             : 
    6303                 :         210 :   if (e->ts.type == BT_CLASS)
    6304                 :           2 :     return CLASS_DATA (e)->attr.pointer_comp;
    6305                 :         208 :   else if (e->ts.type == BT_DERIVED)
    6306                 :           6 :     return e->ts.u.derived->attr.pointer_comp;
    6307                 :             :   else
    6308                 :             :     return false;
    6309                 :             : }
    6310                 :             : 
    6311                 :             : 
    6312                 :             : /* Check whether an expression is "simply contiguous", cf. F2008, 6.5.4.
    6313                 :             :    Note: A scalar is not regarded as "simply contiguous" by the standard.
    6314                 :             :    if bool is not strict, some further checks are done - for instance,
    6315                 :             :    a "(::1)" is accepted.  */
    6316                 :             : 
    6317                 :             : bool
    6318                 :       20842 : gfc_is_simply_contiguous (gfc_expr *expr, bool strict, bool permit_element)
    6319                 :             : {
    6320                 :       20842 :   bool colon;
    6321                 :       20842 :   int i;
    6322                 :       20842 :   gfc_array_ref *ar = NULL;
    6323                 :       20842 :   gfc_ref *ref, *part_ref = NULL;
    6324                 :       20842 :   gfc_symbol *sym;
    6325                 :             : 
    6326                 :       20842 :   if (expr->expr_type == EXPR_ARRAY)
    6327                 :             :     return true;
    6328                 :             : 
    6329                 :       20530 :   if (expr->expr_type == EXPR_NULL)
    6330                 :             :     {
    6331                 :             :       /* F2018:16.9.144  NULL ([MOLD]):
    6332                 :             :          "If MOLD is present, the characteristics are the same as MOLD."
    6333                 :             :          "If MOLD is absent, the characteristics of the result are
    6334                 :             :          determined by the entity with which the reference is associated."
    6335                 :             :          F2018:15.3.2.2 characteristics attributes include CONTIGUOUS.  */
    6336                 :           7 :       if (expr->ts.type == BT_UNKNOWN)
    6337                 :             :         return true;
    6338                 :             :       else
    6339                 :           6 :         return (gfc_variable_attr (expr, NULL).contiguous
    6340                 :          12 :                 || gfc_variable_attr (expr, NULL).allocatable);
    6341                 :             :     }
    6342                 :             : 
    6343                 :       20523 :   if (expr->expr_type == EXPR_FUNCTION)
    6344                 :             :     {
    6345                 :         360 :       if (expr->value.function.isym)
    6346                 :             :         /* TRANSPOSE is the only intrinsic that may return a
    6347                 :             :            non-contiguous array.  It's treated as a special case in
    6348                 :             :            gfc_conv_expr_descriptor too.  */
    6349                 :         298 :         return (expr->value.function.isym->id != GFC_ISYM_TRANSPOSE);
    6350                 :          62 :       else if (expr->value.function.esym)
    6351                 :             :         /* Only a pointer to an array without the contiguous attribute
    6352                 :             :            can be non-contiguous as a result value.  */
    6353                 :          60 :         return (expr->value.function.esym->result->attr.contiguous
    6354                 :          96 :                 || !expr->value.function.esym->result->attr.pointer);
    6355                 :             :       else
    6356                 :             :         {
    6357                 :             :           /* Type-bound procedures.  */
    6358                 :           2 :           gfc_symbol *s = expr->symtree->n.sym;
    6359                 :           2 :           if (s->ts.type != BT_CLASS && s->ts.type != BT_DERIVED)
    6360                 :             :             return false;
    6361                 :             : 
    6362                 :           2 :           gfc_ref *rc = NULL;
    6363                 :           7 :           for (gfc_ref *r = expr->ref; r; r = r->next)
    6364                 :           5 :             if (r->type == REF_COMPONENT)
    6365                 :           5 :               rc = r;
    6366                 :             : 
    6367                 :           2 :           if (rc == NULL || rc->u.c.component == NULL
    6368                 :           2 :               || rc->u.c.component->ts.interface == NULL)
    6369                 :             :             return false;
    6370                 :             : 
    6371                 :           2 :           return rc->u.c.component->ts.interface->attr.contiguous;
    6372                 :             :         }
    6373                 :             :     }
    6374                 :       20163 :   else if (expr->expr_type != EXPR_VARIABLE)
    6375                 :             :     return false;
    6376                 :             : 
    6377                 :       20114 :   if (!permit_element && expr->rank == 0)
    6378                 :             :     return false;
    6379                 :             : 
    6380                 :       42615 :   for (ref = expr->ref; ref; ref = ref->next)
    6381                 :             :     {
    6382                 :       22537 :       if (ar)
    6383                 :             :         return false; /* Array shall be last part-ref.  */
    6384                 :             : 
    6385                 :       22517 :       if (ref->type == REF_COMPONENT)
    6386                 :             :         part_ref  = ref;
    6387                 :       20045 :       else if (ref->type == REF_SUBSTRING)
    6388                 :             :         return false;
    6389                 :       20038 :       else if (ref->type == REF_INQUIRY)
    6390                 :             :         return false;
    6391                 :       20030 :       else if (ref->u.ar.type != AR_ELEMENT)
    6392                 :       19475 :         ar = &ref->u.ar;
    6393                 :             :     }
    6394                 :             : 
    6395                 :       20078 :   sym = expr->symtree->n.sym;
    6396                 :       20078 :   if ((part_ref
    6397                 :        2245 :        && part_ref->u.c.component
    6398                 :        2245 :        && !part_ref->u.c.component->attr.contiguous
    6399                 :        2238 :        && IS_POINTER (part_ref->u.c.component))
    6400                 :             :       || (!part_ref
    6401                 :       17833 :           && expr->ts.type != BT_CLASS
    6402                 :       17791 :           && !sym->attr.contiguous
    6403                 :       13196 :           && (sym->attr.pointer
    6404                 :       11387 :               || (sym->as && sym->as->type == AS_ASSUMED_RANK)
    6405                 :       11001 :               || (sym->as && sym->as->type == AS_ASSUMED_SHAPE))))
    6406                 :             :     return false;
    6407                 :             : 
    6408                 :       15725 :   if (!ar || ar->type == AR_FULL)
    6409                 :             :     return true;
    6410                 :             : 
    6411                 :        6635 :   gcc_assert (ar->type == AR_SECTION);
    6412                 :             : 
    6413                 :             :   /* Check for simply contiguous array */
    6414                 :             :   colon = true;
    6415                 :       12763 :   for (i = 0; i < ar->dimen; i++)
    6416                 :             :     {
    6417                 :        7393 :       if (ar->dimen_type[i] == DIMEN_VECTOR)
    6418                 :             :         return false;
    6419                 :             : 
    6420                 :        7393 :       if (ar->dimen_type[i] == DIMEN_ELEMENT)
    6421                 :             :         {
    6422                 :          25 :           colon = false;
    6423                 :          25 :           continue;
    6424                 :             :         }
    6425                 :             : 
    6426                 :        7368 :       gcc_assert (ar->dimen_type[i] == DIMEN_RANGE);
    6427                 :             : 
    6428                 :             : 
    6429                 :             :       /* If the previous section was not contiguous, that's an error,
    6430                 :             :          unless we have effective only one element and checking is not
    6431                 :             :          strict.  */
    6432                 :        7368 :       if (!colon && (strict || !ar->start[i] || !ar->end[i]
    6433                 :          95 :                      || ar->start[i]->expr_type != EXPR_CONSTANT
    6434                 :          93 :                      || ar->end[i]->expr_type != EXPR_CONSTANT
    6435                 :          51 :                      || mpz_cmp (ar->start[i]->value.integer,
    6436                 :          51 :                                  ar->end[i]->value.integer) != 0))
    6437                 :             :         return false;
    6438                 :             : 
    6439                 :             :       /* Following the standard, "(::1)" or - if known at compile time -
    6440                 :             :          "(lbound:ubound)" are not simply contiguous; if strict
    6441                 :             :          is false, they are regarded as simply contiguous.  */
    6442                 :        7168 :       if (ar->stride[i] && (strict || ar->stride[i]->expr_type != EXPR_CONSTANT
    6443                 :        1063 :                             || ar->stride[i]->ts.type != BT_INTEGER
    6444                 :        1063 :                             || mpz_cmp_si (ar->stride[i]->value.integer, 1) != 0))
    6445                 :             :         return false;
    6446                 :             : 
    6447                 :        6103 :       if (ar->start[i]
    6448                 :        3905 :           && (strict || ar->start[i]->expr_type != EXPR_CONSTANT
    6449                 :        3859 :               || !ar->as->lower[i]
    6450                 :        2130 :               || ar->as->lower[i]->expr_type != EXPR_CONSTANT
    6451                 :        2130 :               || mpz_cmp (ar->start[i]->value.integer,
    6452                 :        2130 :                           ar->as->lower[i]->value.integer) != 0))
    6453                 :        6103 :         colon = false;
    6454                 :             : 
    6455                 :        6103 :       if (ar->end[i]
    6456                 :        3936 :           && (strict || ar->end[i]->expr_type != EXPR_CONSTANT
    6457                 :        3427 :               || !ar->as->upper[i]
    6458                 :        1988 :               || ar->as->upper[i]->expr_type != EXPR_CONSTANT
    6459                 :        1988 :               || mpz_cmp (ar->end[i]->value.integer,
    6460                 :        1988 :                           ar->as->upper[i]->value.integer) != 0))
    6461                 :        6128 :         colon = false;
    6462                 :             :     }
    6463                 :             : 
    6464                 :             :   return true;
    6465                 :             : }
    6466                 :             : 
    6467                 :             : /* Return true if the expression is guaranteed to be non-contiguous,
    6468                 :             :    false if we cannot prove anything.  It is probably best to call
    6469                 :             :    this after gfc_is_simply_contiguous.  If neither of them returns
    6470                 :             :    true, we cannot say (at compile-time).  */
    6471                 :             : 
    6472                 :             : bool
    6473                 :        2495 : gfc_is_not_contiguous (gfc_expr *array)
    6474                 :             : {
    6475                 :        2495 :   int i;
    6476                 :        2495 :   gfc_array_ref *ar = NULL;
    6477                 :        2495 :   gfc_ref *ref;
    6478                 :        2495 :   bool previous_incomplete;
    6479                 :             : 
    6480                 :        6232 :   for (ref = array->ref; ref; ref = ref->next)
    6481                 :             :     {
    6482                 :             :       /* Array-ref shall be last ref.  */
    6483                 :             : 
    6484                 :        3749 :       if (ar && ar->type != AR_ELEMENT)
    6485                 :             :         return true;
    6486                 :             : 
    6487                 :        3737 :       if (ref->type == REF_ARRAY)
    6488                 :        2493 :         ar = &ref->u.ar;
    6489                 :             :     }
    6490                 :             : 
    6491                 :        2483 :   if (ar == NULL || ar->type != AR_SECTION)
    6492                 :             :     return false;
    6493                 :             : 
    6494                 :             :   previous_incomplete = false;
    6495                 :             : 
    6496                 :             :   /* Check if we can prove that the array is not contiguous.  */
    6497                 :             : 
    6498                 :        1525 :   for (i = 0; i < ar->dimen; i++)
    6499                 :             :     {
    6500                 :         862 :       mpz_t arr_size, ref_size;
    6501                 :             : 
    6502                 :         862 :       if (gfc_ref_dimen_size (ar, i, &ref_size, NULL))
    6503                 :             :         {
    6504                 :         419 :           if (gfc_dep_difference (ar->as->upper[i], ar->as->lower[i], &arr_size))
    6505                 :             :             {
    6506                 :             :               /* a(2:4,2:) is known to be non-contiguous, but
    6507                 :             :                  a(2:4,i:i) can be contiguous.  */
    6508                 :          61 :               mpz_add_ui (arr_size, arr_size, 1L);
    6509                 :          61 :               if (previous_incomplete && mpz_cmp_si (ref_size, 1) != 0)
    6510                 :             :                 {
    6511                 :           6 :                   mpz_clear (arr_size);
    6512                 :           6 :                   mpz_clear (ref_size);
    6513                 :          13 :                   return true;
    6514                 :             :                 }
    6515                 :          55 :               else if (mpz_cmp (arr_size, ref_size) != 0)
    6516                 :          28 :                 previous_incomplete = true;
    6517                 :             : 
    6518                 :          55 :               mpz_clear (arr_size);
    6519                 :             :             }
    6520                 :             : 
    6521                 :             :           /* Check for a(::2), i.e. where the stride is not unity.
    6522                 :             :              This is only done if there is more than one element in
    6523                 :             :              the reference along this dimension.  */
    6524                 :             : 
    6525                 :         413 :           if (mpz_cmp_ui (ref_size, 1) > 0 && ar->type == AR_SECTION
    6526                 :         407 :               && ar->dimen_type[i] == DIMEN_RANGE
    6527                 :         407 :               && ar->stride[i] && ar->stride[i]->expr_type == EXPR_CONSTANT
    6528                 :          15 :               && mpz_cmp_si (ar->stride[i]->value.integer, 1) != 0)
    6529                 :             :             {
    6530                 :           7 :               mpz_clear (ref_size);
    6531                 :           7 :               return true;
    6532                 :             :             }
    6533                 :             : 
    6534                 :         406 :           mpz_clear (ref_size);
    6535                 :             :         }
    6536                 :             :     }
    6537                 :             :   /* We didn't find anything definitive.  */
    6538                 :             :   return false;
    6539                 :             : }
    6540                 :             : 
    6541                 :             : /* Build call to an intrinsic procedure.  The number of arguments has to be
    6542                 :             :    passed (rather than ending the list with a NULL value) because we may
    6543                 :             :    want to add arguments but with a NULL-expression.  */
    6544                 :             : 
    6545                 :             : gfc_expr*
    6546                 :       19892 : gfc_build_intrinsic_call (gfc_namespace *ns, gfc_isym_id id, const char* name,
    6547                 :             :                           locus where, unsigned numarg, ...)
    6548                 :             : {
    6549                 :       19892 :   gfc_expr* result;
    6550                 :       19892 :   gfc_actual_arglist* atail;
    6551                 :       19892 :   gfc_intrinsic_sym* isym;
    6552                 :       19892 :   va_list ap;
    6553                 :       19892 :   unsigned i;
    6554                 :       19892 :   const char *mangled_name = gfc_get_string (GFC_PREFIX ("%s"), name);
    6555                 :             : 
    6556                 :       19892 :   isym = gfc_intrinsic_function_by_id (id);
    6557                 :       19892 :   gcc_assert (isym);
    6558                 :             : 
    6559                 :       19892 :   result = gfc_get_expr ();
    6560                 :       19892 :   result->expr_type = EXPR_FUNCTION;
    6561                 :       19892 :   result->ts = isym->ts;
    6562                 :       19892 :   result->where = where;
    6563                 :       19892 :   result->value.function.name = mangled_name;
    6564                 :       19892 :   result->value.function.isym = isym;
    6565                 :             : 
    6566                 :       19892 :   gfc_get_sym_tree (mangled_name, ns, &result->symtree, false);
    6567                 :       19892 :   gfc_commit_symbol (result->symtree->n.sym);
    6568                 :       19892 :   gcc_assert (result->symtree
    6569                 :             :               && (result->symtree->n.sym->attr.flavor == FL_PROCEDURE
    6570                 :             :                   || result->symtree->n.sym->attr.flavor == FL_UNKNOWN));
    6571                 :       19892 :   result->symtree->n.sym->intmod_sym_id = id;
    6572                 :       19892 :   result->symtree->n.sym->attr.flavor = FL_PROCEDURE;
    6573                 :       19892 :   result->symtree->n.sym->attr.intrinsic = 1;
    6574                 :       19892 :   result->symtree->n.sym->attr.artificial = 1;
    6575                 :             : 
    6576                 :       19892 :   va_start (ap, numarg);
    6577                 :       19892 :   atail = NULL;
    6578                 :       68911 :   for (i = 0; i < numarg; ++i)
    6579                 :             :     {
    6580                 :       49019 :       if (atail)
    6581                 :             :         {
    6582                 :       29127 :           atail->next = gfc_get_actual_arglist ();
    6583                 :       29127 :           atail = atail->next;
    6584                 :             :         }
    6585                 :             :       else
    6586                 :       19892 :         atail = result->value.function.actual = gfc_get_actual_arglist ();
    6587                 :             : 
    6588                 :       49019 :       atail->expr = va_arg (ap, gfc_expr*);
    6589                 :             :     }
    6590                 :       19892 :   va_end (ap);
    6591                 :             : 
    6592                 :       19892 :   return result;
    6593                 :             : }
    6594                 :             : 
    6595                 :             : 
    6596                 :             : /* Check if a symbol referenced in a submodule is declared in the ancestor
    6597                 :             :    module and not accessed by use-association, and that the submodule is a
    6598                 :             :    descendant.  */
    6599                 :             : 
    6600                 :             : static bool
    6601                 :           4 : sym_is_from_ancestor (gfc_symbol *sym)
    6602                 :             : {
    6603                 :           4 :   const char dot[2] = ".";
    6604                 :             :   /* Symbols take the form module.submodule_ or module.name_. */
    6605                 :           4 :   char ancestor_module[2 * GFC_MAX_SYMBOL_LEN + 2];
    6606                 :           4 :   char *ancestor;
    6607                 :             : 
    6608                 :           4 :   if (sym == NULL
    6609                 :           4 :       || sym->attr.use_assoc
    6610                 :           4 :       || !sym->attr.used_in_submodule
    6611                 :           4 :       || !sym->module
    6612                 :           4 :       || !sym->ns->proc_name
    6613                 :           4 :       || !sym->ns->proc_name->name)
    6614                 :             :     return false;
    6615                 :             : 
    6616                 :           4 :   memset (ancestor_module, '\0', sizeof (ancestor_module));
    6617                 :           4 :   strcpy (ancestor_module, sym->ns->proc_name->name);
    6618                 :           4 :   ancestor = strtok (ancestor_module, dot);
    6619                 :           4 :   return strcmp (ancestor, sym->module) == 0;
    6620                 :             : }
    6621                 :             : 
    6622                 :             : 
    6623                 :             : /* Check if an expression may appear in a variable definition context
    6624                 :             :    (F2008, 16.6.7) or pointer association context (F2008, 16.6.8).
    6625                 :             :    This is called from the various places when resolving
    6626                 :             :    the pieces that make up such a context.
    6627                 :             :    If own_scope is true (applies to, e.g., ac-implied-do/data-implied-do
    6628                 :             :    variables), some checks are not performed.
    6629                 :             : 
    6630                 :             :    Optionally, a possible error message can be suppressed if context is NULL
    6631                 :             :    and just the return status (true / false) be requested.  */
    6632                 :             : 
    6633                 :             : bool
    6634                 :      402319 : gfc_check_vardef_context (gfc_expr* e, bool pointer, bool alloc_obj,
    6635                 :             :                           bool own_scope, const char* context)
    6636                 :             : {
    6637                 :      402319 :   gfc_symbol* sym = NULL;
    6638                 :      402319 :   bool is_pointer;
    6639                 :      402319 :   bool check_intentin;
    6640                 :      402319 :   bool ptr_component;
    6641                 :      402319 :   symbol_attribute attr;
    6642                 :      402319 :   gfc_ref* ref;
    6643                 :      402319 :   int i;
    6644                 :             : 
    6645                 :      402319 :   if (e->expr_type == EXPR_VARIABLE)
    6646                 :             :     {
    6647                 :      402245 :       gcc_assert (e->symtree);
    6648                 :      402245 :       sym = e->symtree->n.sym;
    6649                 :             :     }
    6650                 :          74 :   else if (e->expr_type == EXPR_FUNCTION)
    6651                 :             :     {
    6652                 :          18 :       gcc_assert (e->symtree);
    6653                 :          18 :       sym = e->value.function.esym ? e->value.function.esym : e->symtree->n.sym;
    6654                 :             :     }
    6655                 :             : 
    6656                 :      402319 :   attr = gfc_expr_attr (e);
    6657                 :      402319 :   if (!pointer && e->expr_type == EXPR_FUNCTION && attr.pointer)
    6658                 :             :     {
    6659                 :          16 :       if (!(gfc_option.allow_std & GFC_STD_F2008))
    6660                 :             :         {
    6661                 :           1 :           if (context)
    6662                 :           1 :             gfc_error ("Fortran 2008: Pointer functions in variable definition"
    6663                 :             :                        " context (%s) at %L", context, &e->where);
    6664                 :           1 :           return false;
    6665                 :             :         }
    6666                 :             :     }
    6667                 :      402303 :   else if (e->expr_type != EXPR_VARIABLE)
    6668                 :             :     {
    6669                 :          58 :       if (context)
    6670                 :          55 :         gfc_error ("Non-variable expression in variable definition context (%s)"
    6671                 :             :                    " at %L", context, &e->where);
    6672                 :          58 :       return false;
    6673                 :             :     }
    6674                 :             : 
    6675                 :      402260 :   if (!pointer && sym->attr.flavor == FL_PARAMETER)
    6676                 :             :     {
    6677                 :           5 :       if (context)
    6678                 :           5 :         gfc_error ("Named constant %qs in variable definition context (%s)"
    6679                 :             :                    " at %L", sym->name, context, &e->where);
    6680                 :           5 :       return false;
    6681                 :             :     }
    6682                 :      385989 :   if (!pointer && sym->attr.flavor != FL_VARIABLE
    6683                 :       10394 :       && !(sym->attr.flavor == FL_PROCEDURE && sym == sym->result)
    6684                 :         562 :       && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer)
    6685                 :           3 :       && !(sym->attr.flavor == FL_PROCEDURE
    6686                 :           3 :            && sym->attr.function && attr.pointer))
    6687                 :             :     {
    6688                 :           0 :       if (context)
    6689                 :           0 :         gfc_error ("%qs in variable definition context (%s) at %L is not"
    6690                 :             :                    " a variable", sym->name, context, &e->where);
    6691                 :           0 :       return false;
    6692                 :             :     }
    6693                 :             : 
    6694                 :             :   /* Find out whether the expr is a pointer; this also means following
    6695                 :             :      component references to the last one.  */
    6696                 :      402255 :   is_pointer = (attr.pointer || attr.proc_pointer);
    6697                 :      402255 :   if (pointer && !is_pointer)
    6698                 :             :     {
    6699                 :           5 :       if (context)
    6700                 :           5 :         gfc_error ("Non-POINTER in pointer association context (%s)"
    6701                 :             :                    " at %L", context, &e->where);
    6702                 :           5 :       return false;
    6703                 :             :     }
    6704                 :             : 
    6705                 :      402250 :   if (e->ts.type == BT_DERIVED
    6706                 :       19483 :       && e->ts.u.derived == NULL)
    6707                 :             :     {
    6708                 :           1 :       if (context)
    6709                 :           1 :         gfc_error ("Type inaccessible in variable definition context (%s) "
    6710                 :             :                    "at %L", context, &e->where);
    6711                 :           1 :       return false;
    6712                 :             :     }
    6713                 :             : 
    6714                 :             :   /* F2008, C1303.  */
    6715                 :      402249 :   if (!alloc_obj
    6716                 :      371684 :       && (attr.lock_comp
    6717                 :      371684 :           || (e->ts.type == BT_DERIVED
    6718                 :       15076 :               && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    6719                 :          24 :               && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)))
    6720                 :             :     {
    6721                 :           3 :       if (context)
    6722                 :           3 :         gfc_error ("LOCK_TYPE in variable definition context (%s) at %L",
    6723                 :             :                    context, &e->where);
    6724                 :           3 :       return false;
    6725                 :             :     }
    6726                 :             : 
    6727                 :             :   /* TS18508, C702/C203.  */
    6728                 :      371681 :   if (!alloc_obj
    6729                 :             :       && (attr.lock_comp
    6730                 :      371681 :           || (e->ts.type == BT_DERIVED
    6731                 :       15073 :               && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    6732                 :          21 :               && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)))
    6733                 :             :     {
    6734                 :           0 :       if (context)
    6735                 :           0 :         gfc_error ("LOCK_EVENT in variable definition context (%s) at %L",
    6736                 :             :                    context, &e->where);
    6737                 :           0 :       return false;
    6738                 :             :     }
    6739                 :             : 
    6740                 :             :   /* INTENT(IN) dummy argument.  Check this, unless the object itself is the
    6741                 :             :      component of sub-component of a pointer; we need to distinguish
    6742                 :             :      assignment to a pointer component from pointer-assignment to a pointer
    6743                 :             :      component.  Note that (normal) assignment to procedure pointers is not
    6744                 :             :      possible.  */
    6745                 :      402246 :   check_intentin = !own_scope;
    6746                 :       13669 :   ptr_component = (sym->ts.type == BT_CLASS && sym->ts.u.derived
    6747                 :       13669 :                    && CLASS_DATA (sym))
    6748                 :      415915 :                   ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer;
    6749                 :      527586 :   for (ref = e->ref; ref && check_intentin; ref = ref->next)
    6750                 :             :     {
    6751                 :      125348 :       if (ptr_component && ref->type == REF_COMPONENT)
    6752                 :      125348 :         check_intentin = false;
    6753                 :      125348 :       if (ref->type == REF_COMPONENT)
    6754                 :             :         {
    6755                 :       28818 :           gfc_component *comp = ref->u.c.component;
    6756                 :        2439 :           ptr_component = (comp->ts.type == BT_CLASS && comp->attr.class_ok)
    6757                 :       31257 :                         ? CLASS_DATA (comp)->attr.class_pointer
    6758                 :       26379 :                         : comp->attr.pointer;
    6759                 :       28818 :           if (ptr_component && !pointer)
    6760                 :        4129 :             check_intentin = false;
    6761                 :             :         }
    6762                 :      125348 :       if (ref->type == REF_INQUIRY
    6763                 :          90 :           && (ref->u.i == INQUIRY_KIND || ref->u.i == INQUIRY_LEN))
    6764                 :             :         {
    6765                 :           8 :           if (context)
    6766                 :          16 :             gfc_error ("%qs parameter inquiry for %qs in "
    6767                 :             :                        "variable definition context (%s) at %L",
    6768                 :             :                        ref->u.i == INQUIRY_KIND ? "KIND" : "LEN",
    6769                 :             :                        sym->name, context, &e->where);
    6770                 :           8 :           return false;
    6771                 :             :         }
    6772                 :             :     }
    6773                 :             : 
    6774                 :      402238 :   if (check_intentin
    6775                 :      391552 :       && (sym->attr.intent == INTENT_IN
    6776                 :      391462 :           || (sym->attr.select_type_temporary && sym->assoc
    6777                 :        1315 :               && sym->assoc->target && sym->assoc->target->symtree
    6778                 :        1315 :               && sym->assoc->target->symtree->n.sym->attr.intent == INTENT_IN)))
    6779                 :             :     {
    6780                 :          92 :       if (pointer && is_pointer)
    6781                 :             :         {
    6782                 :          16 :           if (context)
    6783                 :          16 :             gfc_error ("Dummy argument %qs with INTENT(IN) in pointer"
    6784                 :             :                        " association context (%s) at %L",
    6785                 :             :                        sym->name, context, &e->where);
    6786                 :          16 :           return false;
    6787                 :             :         }
    6788                 :          76 :       if (!pointer && !is_pointer && !sym->attr.pointer)
    6789                 :             :         {
    6790                 :          22 :           const char *name = sym->attr.select_type_temporary
    6791                 :          24 :                            ? sym->assoc->target->symtree->name : sym->name;
    6792                 :          24 :           if (context)
    6793                 :          17 :             gfc_error ("Dummy argument %qs with INTENT(IN) in variable"
    6794                 :             :                        " definition context (%s) at %L",
    6795                 :             :                        name, context, &e->where);
    6796                 :          24 :           return false;
    6797                 :             :         }
    6798                 :             :     }
    6799                 :             : 
    6800                 :             :   /* PROTECTED and use-associated.  */
    6801                 :      402198 :   if (sym->attr.is_protected
    6802                 :         264 :       && (sym->attr.use_assoc
    6803                 :         201 :           || (sym->attr.used_in_submodule && !sym_is_from_ancestor (sym)))
    6804                 :      402262 :       && check_intentin)
    6805                 :             :     {
    6806                 :          57 :       if (pointer && is_pointer)
    6807                 :             :         {
    6808                 :          15 :           if (context)
    6809                 :          15 :             gfc_error ("Variable %qs is PROTECTED and cannot appear in a "
    6810                 :             :                        "pointer association context (%s) at %L",
    6811                 :             :                        sym->name, context, &e->where);
    6812                 :          15 :           return false;
    6813                 :             :         }
    6814                 :          42 :       if (!pointer && !is_pointer)
    6815                 :             :         {
    6816                 :          24 :           if (context)
    6817                 :          23 :             gfc_error ("Variable %qs is PROTECTED and cannot appear in a "
    6818                 :             :                        "variable definition context (%s) at %L",
    6819                 :             :                        sym->name, context, &e->where);
    6820                 :          24 :           return false;
    6821                 :             :         }
    6822                 :             :     }
    6823                 :             : 
    6824                 :             :   /* Variable not assignable from a PURE procedure but appears in
    6825                 :             :      variable definition context.  */
    6826                 :     1193685 :   own_scope = own_scope
    6827                 :      402159 :               || (sym->attr.result && sym->ns->proc_name
    6828                 :        8245 :                   && sym == sym->ns->proc_name->result);
    6829                 :      389375 :   if (!pointer && !own_scope && gfc_pure (NULL) && gfc_impure_variable (sym))
    6830                 :             :     {
    6831                 :           8 :       if (context)
    6832                 :           8 :         gfc_error ("Variable %qs cannot appear in a variable definition"
    6833                 :             :                    " context (%s) at %L in PURE procedure",
    6834                 :             :                    sym->name, context, &e->where);
    6835                 :           8 :       return false;
    6836                 :             :     }
    6837                 :             : 
    6838                 :      380944 :   if (!pointer && context && gfc_implicit_pure (NULL)
    6839                 :      414221 :       && gfc_impure_variable (sym))
    6840                 :             :     {
    6841                 :        1070 :       gfc_namespace *ns;
    6842                 :        1070 :       gfc_symbol *sym;
    6843                 :             : 
    6844                 :        1144 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
    6845                 :             :         {
    6846                 :        1144 :           sym = ns->proc_name;
    6847                 :        1144 :           if (sym == NULL)
    6848                 :             :             break;
    6849                 :        1144 :           if (sym->attr.flavor == FL_PROCEDURE)
    6850                 :             :             {
    6851                 :        1070 :               sym->attr.implicit_pure = 0;
    6852                 :        1070 :               break;
    6853                 :             :             }
    6854                 :             :         }
    6855                 :             :     }
    6856                 :             :   /* Check variable definition context for associate-names.  */
    6857                 :      402151 :   if (!pointer && sym->assoc && !sym->attr.select_rank_temporary)
    6858                 :             :     {
    6859                 :        1241 :       const char* name;
    6860                 :        1241 :       gfc_association_list* assoc;
    6861                 :             : 
    6862                 :        1241 :       gcc_assert (sym->assoc->target);
    6863                 :             : 
    6864                 :             :       /* If this is a SELECT TYPE temporary (the association is used internally
    6865                 :             :          for SELECT TYPE), silently go over to the target.  */
    6866                 :        1241 :       if (sym->attr.select_type_temporary)
    6867                 :             :         {
    6868                 :         898 :           gfc_expr* t = sym->assoc->target;
    6869                 :             : 
    6870                 :         898 :           gcc_assert (t->expr_type == EXPR_VARIABLE);
    6871                 :         898 :           name = t->symtree->name;
    6872                 :             : 
    6873                 :         898 :           if (t->symtree->n.sym->assoc)
    6874                 :             :             assoc = t->symtree->n.sym->assoc;
    6875                 :             :           else
    6876                 :         826 :             assoc = sym->assoc;
    6877                 :             :         }
    6878                 :             :       else
    6879                 :             :         {
    6880                 :         343 :           name = sym->name;
    6881                 :         343 :           assoc = sym->assoc;
    6882                 :             :         }
    6883                 :        1241 :       gcc_assert (name && assoc);
    6884                 :             : 
    6885                 :             :       /* Is association to a valid variable?  */
    6886                 :        1241 :       if (!assoc->variable)
    6887                 :             :         {
    6888                 :           9 :           if (context)
    6889                 :             :             {
    6890                 :           9 :               if (assoc->target->expr_type == EXPR_VARIABLE
    6891                 :           9 :                   && gfc_has_vector_index (assoc->target))
    6892                 :           4 :                 gfc_error ("%qs at %L associated to vector-indexed target"
    6893                 :             :                            " cannot be used in a variable definition"
    6894                 :             :                            " context (%s)",
    6895                 :             :                            name, &e->where, context);
    6896                 :             :               else
    6897                 :           5 :                 gfc_error ("%qs at %L associated to expression"
    6898                 :             :                            " cannot be used in a variable definition"
    6899                 :             :                            " context (%s)",
    6900                 :             :                            name, &e->where, context);
    6901                 :             :             }
    6902                 :           9 :           return false;
    6903                 :             :         }
    6904                 :        1232 :       else if (context && gfc_is_ptr_fcn (assoc->target))
    6905                 :             :         {
    6906                 :           5 :           if (!gfc_notify_std (GFC_STD_F2018, "%qs at %L associated to "
    6907                 :             :                                "pointer function target being used in a "
    6908                 :             :                                "variable definition context (%s)", name,
    6909                 :             :                                &e->where, context))
    6910                 :             :             return false;
    6911                 :           1 :           else if (gfc_has_vector_index (e))
    6912                 :             :             {
    6913                 :           0 :               gfc_error ("%qs at %L associated to vector-indexed target"
    6914                 :             :                          " cannot be used in a variable definition"
    6915                 :             :                          " context (%s)",
    6916                 :             :                          name, &e->where, context);
    6917                 :           0 :               return false;
    6918                 :             :             }
    6919                 :             :         }
    6920                 :             : 
    6921                 :             :       /* Target must be allowed to appear in a variable definition context.  */
    6922                 :        1228 :       if (!gfc_check_vardef_context (assoc->target, pointer, false, false, NULL))
    6923                 :             :         {
    6924                 :           1 :           if (context)
    6925                 :           1 :             gfc_error ("Associate-name %qs cannot appear in a variable"
    6926                 :             :                        " definition context (%s) at %L because its target"
    6927                 :             :                        " at %L cannot, either",
    6928                 :             :                        name, context, &e->where,
    6929                 :           1 :                        &assoc->target->where);
    6930                 :           1 :           return false;
    6931                 :             :         }
    6932                 :             :     }
    6933                 :             : 
    6934                 :             :   /* Check for same value in vector expression subscript.  */
    6935                 :             : 
    6936                 :      402137 :   if (e->rank > 0)
    6937                 :      150522 :     for (ref = e->ref; ref != NULL; ref = ref->next)
    6938                 :       75218 :       if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
    6939                 :       19570 :         for (i = 0; i < GFC_MAX_DIMENSIONS
    6940                 :       30568 :                && ref->u.ar.dimen_type[i] != 0; i++)
    6941                 :       19577 :           if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
    6942                 :             :             {
    6943                 :         243 :               gfc_expr *arr = ref->u.ar.start[i];
    6944                 :         243 :               if (arr->expr_type == EXPR_ARRAY)
    6945                 :             :                 {
    6946                 :          56 :                   gfc_constructor *c, *n;
    6947                 :          56 :                   gfc_expr *ec, *en;
    6948                 :             : 
    6949                 :          56 :                   for (c = gfc_constructor_first (arr->value.constructor);
    6950                 :         191 :                        c != NULL; c = gfc_constructor_next (c))
    6951                 :             :                     {
    6952                 :         142 :                       if (c == NULL || c->iterator != NULL)
    6953                 :          12 :                         continue;
    6954                 :             : 
    6955                 :         130 :                       ec = c->expr;
    6956                 :             : 
    6957                 :         275 :                       for (n = gfc_constructor_next (c); n != NULL;
    6958                 :         145 :                            n = gfc_constructor_next (n))
    6959                 :             :                         {
    6960                 :         152 :                           if (n->iterator != NULL)
    6961                 :          12 :                             continue;
    6962                 :             : 
    6963                 :         140 :                           en = n->expr;
    6964                 :         140 :                           if (gfc_dep_compare_expr (ec, en) == 0)
    6965                 :             :                             {
    6966                 :           7 :                               if (context)
    6967                 :           7 :                                 gfc_error_now ("Elements with the same value "
    6968                 :             :                                                "at %L and %L in vector "
    6969                 :             :                                                "subscript in a variable "
    6970                 :             :                                                "definition context (%s)",
    6971                 :             :                                                &(ec->where), &(en->where),
    6972                 :             :                                                context);
    6973                 :           7 :                               return false;
    6974                 :             :                             }
    6975                 :             :                         }
    6976                 :             :                     }
    6977                 :             :                 }
    6978                 :             :             }
    6979                 :             : 
    6980                 :             :   return true;
    6981                 :             : }
    6982                 :             : 
    6983                 :             : gfc_expr*
    6984                 :          12 : gfc_pdt_find_component_copy_initializer (gfc_symbol *sym, const char *name)
    6985                 :             : {
    6986                 :             :   /* The actual length of a pdt is in its components.  In the
    6987                 :             :      initializer of the current ref is only the default value.
    6988                 :             :      Therefore traverse the chain of components and pick the correct
    6989                 :             :      one's initializer expressions.  */
    6990                 :          12 :   for (gfc_component *comp = sym->ts.u.derived->components; comp != NULL;
    6991                 :           0 :        comp = comp->next)
    6992                 :             :     {
    6993                 :          12 :       if (!strcmp (comp->name, name))
    6994                 :          12 :         return gfc_copy_expr (comp->initializer);
    6995                 :             :     }
    6996                 :             :   return NULL;
    6997                 :             : }
        

Generated by: LCOV version 2.1-beta

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