LCOV - code coverage report
Current view: top level - gcc/fortran - expr.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 92.2 % 3372 3110
Test Date: 2026-03-28 14:25:54 Functions: 99.2 % 124 123
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Routines for manipulation of expression nodes.
       2              :    Copyright (C) 2000-2026 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     88558081 : gfc_get_expr (void)
      46              : {
      47     88558081 :   gfc_expr *e;
      48              : 
      49     88558081 :   e = XCNEW (gfc_expr);
      50     88558081 :   gfc_clear_ts (&e->ts);
      51     88558081 :   e->shape = NULL;
      52     88558081 :   e->ref = NULL;
      53     88558081 :   e->symtree = NULL;
      54     88558081 :   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       169966 : gfc_get_array_expr (bt type, int kind, locus *where)
      63              : {
      64       169966 :   gfc_expr *e;
      65              : 
      66       169966 :   e = gfc_get_expr ();
      67       169966 :   e->expr_type = EXPR_ARRAY;
      68       169966 :   e->value.constructor = NULL;
      69       169966 :   e->rank = 1;
      70       169966 :   e->shape = NULL;
      71              : 
      72       169966 :   e->ts.type = type;
      73       169966 :   e->ts.kind = kind;
      74       169966 :   if (where)
      75       168710 :     e->where = *where;
      76              : 
      77       169966 :   return e;
      78              : }
      79              : 
      80              : 
      81              : /* Get a new expression node that is the NULL expression.  */
      82              : 
      83              : gfc_expr *
      84        50041 : gfc_get_null_expr (locus *where)
      85              : {
      86        50041 :   gfc_expr *e;
      87              : 
      88        50041 :   e = gfc_get_expr ();
      89        50041 :   e->expr_type = EXPR_NULL;
      90        50041 :   e->ts.type = BT_UNKNOWN;
      91              : 
      92        50041 :   if (where)
      93        14412 :     e->where = *where;
      94              : 
      95        50041 :   return e;
      96              : }
      97              : 
      98              : 
      99              : /* Get a new expression node that is an operator expression node.  */
     100              : 
     101              : gfc_expr *
     102      1582639 : gfc_get_operator_expr (locus *where, gfc_intrinsic_op op,
     103              :                       gfc_expr *op1, gfc_expr *op2)
     104              : {
     105      1582639 :   gfc_expr *e;
     106              : 
     107      1582639 :   e = gfc_get_expr ();
     108      1582639 :   e->expr_type = EXPR_OP;
     109      1582639 :   e->value.op.op = op;
     110      1582639 :   e->value.op.op1 = op1;
     111      1582639 :   e->value.op.op2 = op2;
     112              : 
     113      1582639 :   if (where)
     114      1582639 :     e->where = *where;
     115              : 
     116      1582639 :   return e;
     117              : }
     118              : 
     119              : /* Get a new expression node that is an conditional expression node.  */
     120              : 
     121              : gfc_expr *
     122          222 : gfc_get_conditional_expr (locus *where, gfc_expr *condition,
     123              :                           gfc_expr *true_expr, gfc_expr *false_expr)
     124              : {
     125          222 :   gfc_expr *e;
     126              : 
     127          222 :   e = gfc_get_expr ();
     128          222 :   e->expr_type = EXPR_CONDITIONAL;
     129          222 :   e->value.conditional.condition = condition;
     130          222 :   e->value.conditional.true_expr = true_expr;
     131          222 :   e->value.conditional.false_expr = false_expr;
     132              : 
     133          222 :   if (where)
     134          222 :     e->where = *where;
     135              : 
     136          222 :   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        33168 : gfc_get_structure_constructor_expr (bt type, int kind, locus *where)
     144              : {
     145        33168 :   gfc_expr *e;
     146              : 
     147        33168 :   e = gfc_get_expr ();
     148        33168 :   e->expr_type = EXPR_STRUCTURE;
     149        33168 :   e->value.constructor = NULL;
     150              : 
     151        33168 :   e->ts.type = type;
     152        33168 :   e->ts.kind = kind;
     153        33168 :   if (where)
     154        33168 :     e->where = *where;
     155              : 
     156        33168 :   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     31425657 : gfc_get_constant_expr (bt type, int kind, locus *where)
     164              : {
     165     31425657 :   gfc_expr *e;
     166              : 
     167     31425657 :   if (!where)
     168            0 :     gfc_internal_error ("gfc_get_constant_expr(): locus %<where%> cannot be "
     169              :                         "NULL");
     170              : 
     171     31425657 :   e = gfc_get_expr ();
     172              : 
     173     31425657 :   e->expr_type = EXPR_CONSTANT;
     174     31425657 :   e->ts.type = type;
     175     31425657 :   e->ts.kind = kind;
     176     31425657 :   e->where = *where;
     177              : 
     178     31425657 :   switch (type)
     179              :     {
     180     30484881 :     case BT_INTEGER:
     181     30484881 :     case BT_UNSIGNED:
     182     30484881 :       mpz_init (e->value.integer);
     183     30484881 :       break;
     184              : 
     185       407447 :     case BT_REAL:
     186       407447 :       gfc_set_model_kind (kind);
     187       407447 :       mpfr_init (e->value.real);
     188       407447 :       break;
     189              : 
     190        19442 :     case BT_COMPLEX:
     191        19442 :       gfc_set_model_kind (kind);
     192        19442 :       mpc_init2 (e->value.complex, mpfr_get_default_prec());
     193        19442 :       break;
     194              : 
     195              :     default:
     196              :       break;
     197              :     }
     198              : 
     199     31425657 :   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       346055 : gfc_get_character_expr (int kind, locus *where, const char *src, gfc_charlen_t len)
     209              : {
     210       346055 :   gfc_expr *e;
     211       346055 :   gfc_char_t *dest;
     212              : 
     213       346055 :   if (!src)
     214              :     {
     215       344350 :       dest = gfc_get_wide_string (len + 1);
     216       344350 :       gfc_wide_memset (dest, ' ', len);
     217       344350 :       dest[len] = '\0';
     218              :     }
     219              :   else
     220         1705 :     dest = gfc_char_to_widechar (src);
     221              : 
     222       347806 :   e = gfc_get_constant_expr (BT_CHARACTER, kind,
     223              :                             where ? where : &gfc_current_locus);
     224       346055 :   e->value.character.string = dest;
     225       346055 :   e->value.character.length = len;
     226              : 
     227       346055 :   return e;
     228              : }
     229              : 
     230              : 
     231              : /* Get a new expression node that is an integer constant.  */
     232              : 
     233              : gfc_expr *
     234     14394842 : gfc_get_int_expr (int kind, locus *where, HOST_WIDE_INT value)
     235              : {
     236     14394842 :   gfc_expr *p;
     237     28748312 :   p = gfc_get_constant_expr (BT_INTEGER, kind,
     238              :                              where ? where : &gfc_current_locus);
     239              : 
     240     14394842 :   const wide_int w = wi::shwi (value, kind * BITS_PER_UNIT);
     241     14394842 :   wi::to_mpz (w, p->value.integer, SIGNED);
     242              : 
     243     14394842 :   return p;
     244     14394842 : }
     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        75410 : gfc_get_logical_expr (int kind, locus *where, bool value)
     264              : {
     265        75410 :   gfc_expr *p;
     266        86778 :   p = gfc_get_constant_expr (BT_LOGICAL, kind,
     267              :                              where ? where : &gfc_current_locus);
     268              : 
     269        75410 :   p->value.logical = value;
     270              : 
     271        75410 :   return p;
     272              : }
     273              : 
     274              : 
     275              : gfc_expr *
     276        32503 : gfc_get_iokind_expr (locus *where, io_kind k)
     277              : {
     278        32503 :   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        32503 :   e = gfc_get_expr ();
     285        32503 :   e->expr_type = EXPR_CONSTANT;
     286        32503 :   e->ts.type = BT_LOGICAL;
     287        32503 :   e->value.iokind = k;
     288        32503 :   e->where = *where;
     289              : 
     290        32503 :   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     56187459 : gfc_copy_expr (gfc_expr *p)
     299              : {
     300     56187459 :   gfc_expr *q;
     301     56187459 :   gfc_char_t *s;
     302     56187459 :   char *c;
     303              : 
     304     56187459 :   if (p == NULL)
     305              :     return NULL;
     306              : 
     307     48182446 :   q = gfc_get_expr ();
     308     48182446 :   *q = *p;
     309              : 
     310     48182446 :   switch (q->expr_type)
     311              :     {
     312          980 :     case EXPR_SUBSTRING:
     313          980 :       s = gfc_get_wide_string (p->value.character.length + 1);
     314          980 :       q->value.character.string = s;
     315          980 :       memcpy (s, p->value.character.string,
     316          980 :               (p->value.character.length + 1) * sizeof (gfc_char_t));
     317          980 :       break;
     318              : 
     319     16893554 :     case EXPR_CONSTANT:
     320              :       /* Copy target representation, if it exists.  */
     321     16893554 :       if (p->representation.string)
     322              :         {
     323         3478 :           c = XCNEWVEC (char, p->representation.length + 1);
     324         3478 :           q->representation.string = c;
     325         3478 :           memcpy (c, p->representation.string, (p->representation.length + 1));
     326              :         }
     327              : 
     328              :       /* Copy the values of any pointer components of p->value.  */
     329     16893554 :       switch (q->ts.type)
     330              :         {
     331     15123820 :         case BT_INTEGER:
     332     15123820 :         case BT_UNSIGNED:
     333     15123820 :           mpz_init_set (q->value.integer, p->value.integer);
     334     15123820 :           break;
     335              : 
     336       345996 :         case BT_REAL:
     337       345996 :           gfc_set_model_kind (q->ts.kind);
     338       345996 :           mpfr_init (q->value.real);
     339       345996 :           mpfr_set (q->value.real, p->value.real, GFC_RND_MODE);
     340       345996 :           break;
     341              : 
     342        27527 :         case BT_COMPLEX:
     343        27527 :           gfc_set_model_kind (q->ts.kind);
     344        27527 :           mpc_init2 (q->value.complex, mpfr_get_default_prec());
     345        27527 :           mpc_set (q->value.complex, p->value.complex, GFC_MPC_RND_MODE);
     346        27527 :           break;
     347              : 
     348       294455 :         case BT_CHARACTER:
     349       294455 :           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       293677 :               s = gfc_get_wide_string (p->value.character.length + 1);
     356       293677 :               q->value.character.string = s;
     357              : 
     358              :               /* This is the case for the C_NULL_CHAR named constant.  */
     359       293677 :               if (p->value.character.length == 0
     360         2395 :                   && (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       293677 :                 memcpy (s, p->value.character.string,
     369       293677 :                         (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            3 :         case BT_BOZ:
     381            3 :           q->boz.len = p->boz.len;
     382            3 :           q->boz.rdx = p->boz.rdx;
     383            3 :           q->boz.str = XCNEWVEC (char, q->boz.len + 1);
     384            3 :           strncpy (q->boz.str, p->boz.str, p->boz.len);
     385            3 :           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     16420247 :     case EXPR_OP:
     398     16420247 :       switch (q->value.op.op)
     399              :         {
     400      5269671 :         case INTRINSIC_NOT:
     401      5269671 :         case INTRINSIC_PARENTHESES:
     402      5269671 :         case INTRINSIC_UPLUS:
     403      5269671 :         case INTRINSIC_UMINUS:
     404      5269671 :           q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
     405      5269671 :           break;
     406              : 
     407     11150576 :         default:                /* Binary operators.  */
     408     11150576 :           q->value.op.op1 = gfc_copy_expr (p->value.op.op1);
     409     11150576 :           q->value.op.op2 = gfc_copy_expr (p->value.op.op2);
     410     11150576 :           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       396032 :     case EXPR_FUNCTION:
     425       792064 :       q->value.function.actual =
     426       396032 :         gfc_copy_actual_arglist (p->value.function.actual);
     427       396032 :       break;
     428              : 
     429           90 :     case EXPR_COMPCALL:
     430           90 :     case EXPR_PPC:
     431          180 :       q->value.compcall.actual =
     432           90 :         gfc_copy_actual_arglist (p->value.compcall.actual);
     433           90 :       q->value.compcall.tbp = p->value.compcall.tbp;
     434           90 :       break;
     435              : 
     436       111919 :     case EXPR_STRUCTURE:
     437       111919 :     case EXPR_ARRAY:
     438       111919 :       q->value.constructor = gfc_constructor_copy (p->value.constructor);
     439       111919 :       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     48182446 :   q->shape = gfc_copy_shape (p->shape, p->rank);
     450              : 
     451     48182446 :   q->ref = gfc_copy_ref (p->ref);
     452              : 
     453     48182446 :   if (p->param_list)
     454         1563 :     q->param_list = gfc_copy_actual_arglist (p->param_list);
     455              : 
     456              :   return q;
     457              : }
     458              : 
     459              : 
     460              : void
     461       442644 : gfc_clear_shape (mpz_t *shape, int rank)
     462              : {
     463       442644 :   int i;
     464              : 
     465      1015656 :   for (i = 0; i < rank; i++)
     466       573012 :     mpz_clear (shape[i]);
     467       442644 : }
     468              : 
     469              : 
     470              : void
     471     88340075 : gfc_free_shape (mpz_t **shape, int rank)
     472              : {
     473     88340075 :   if (*shape == NULL)
     474              :     return;
     475              : 
     476       428739 :   gfc_clear_shape (*shape, rank);
     477       428739 :   free (*shape);
     478       428739 :   *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     88318780 : free_expr0 (gfc_expr *e)
     489              : {
     490     88318780 :   switch (e->expr_type)
     491              :     {
     492     48569751 :     case EXPR_CONSTANT:
     493              :       /* Free any parts of the value that need freeing.  */
     494     48569751 :       switch (e->ts.type)
     495              :         {
     496     45823891 :         case BT_INTEGER:
     497     45823891 :         case BT_UNSIGNED:
     498     45823891 :           mpz_clear (e->value.integer);
     499     45823891 :           break;
     500              : 
     501       753707 :         case BT_REAL:
     502       753707 :           mpfr_clear (e->value.real);
     503       753707 :           break;
     504              : 
     505       657683 :         case BT_CHARACTER:
     506       657683 :           free (e->value.character.string);
     507       657683 :           break;
     508              : 
     509        46911 :         case BT_COMPLEX:
     510        46911 :           mpc_clear (e->value.complex);
     511        46911 :           break;
     512              : 
     513         1677 :         case BT_BOZ:
     514         1677 :           free (e->boz.str);
     515         1677 :           break;
     516              : 
     517              :         default:
     518              :           break;
     519              :         }
     520              : 
     521              :       /* Free the representation.  */
     522     48569751 :       free (e->representation.string);
     523              : 
     524     48569751 :       break;
     525              : 
     526     18032828 :     case EXPR_OP:
     527     18032828 :       if (e->value.op.op1 != NULL)
     528      1640254 :         gfc_free_expr (e->value.op.op1);
     529     18032828 :       if (e->value.op.op2 != NULL)
     530      1485817 :         gfc_free_expr (e->value.op.op2);
     531              :       break;
     532              : 
     533          224 :     case EXPR_CONDITIONAL:
     534          224 :       gfc_free_expr (e->value.conditional.condition);
     535          224 :       gfc_free_expr (e->value.conditional.true_expr);
     536          224 :       gfc_free_expr (e->value.conditional.false_expr);
     537          224 :       break;
     538              : 
     539      1889102 :     case EXPR_FUNCTION:
     540      1889102 :       gfc_free_actual_arglist (e->value.function.actual);
     541      1889102 :       break;
     542              : 
     543         3545 :     case EXPR_COMPCALL:
     544         3545 :     case EXPR_PPC:
     545         3545 :       gfc_free_actual_arglist (e->value.compcall.actual);
     546         3545 :       break;
     547              : 
     548              :     case EXPR_VARIABLE:
     549              :       break;
     550              : 
     551       341085 :     case EXPR_ARRAY:
     552       341085 :     case EXPR_STRUCTURE:
     553       341085 :       gfc_constructor_free (e->value.constructor);
     554       341085 :       break;
     555              : 
     556         1199 :     case EXPR_SUBSTRING:
     557         1199 :       free (e->value.character.string);
     558         1199 :       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     88318780 :   gfc_free_shape (&e->shape, e->rank);
     569              : 
     570     88318780 :   gfc_free_ref_list (e->ref);
     571              : 
     572     88318780 :   gfc_free_actual_arglist (e->param_list);
     573              : 
     574     88318780 :   memset (e, '\0', sizeof (gfc_expr));
     575     88318780 : }
     576              : 
     577              : 
     578              : /* Free an expression node and everything beneath it.  */
     579              : 
     580              : void
     581    120787973 : gfc_free_expr (gfc_expr *e)
     582              : {
     583    120787973 :   if (e == NULL)
     584              :     return;
     585     57463907 :   free_expr0 (e);
     586     57463907 :   free (e);
     587              : }
     588              : 
     589              : 
     590              : /* Free an argument list and everything below it.  */
     591              : 
     592              : void
     593     90351731 : gfc_free_actual_arglist (gfc_actual_arglist *a1)
     594              : {
     595     90351731 :   gfc_actual_arglist *a2;
     596              : 
     597     93544324 :   while (a1)
     598              :     {
     599      3192593 :       a2 = a1->next;
     600      3192593 :       if (a1->expr)
     601      2905756 :         gfc_free_expr (a1->expr);
     602      3192593 :       free (a1->associated_dummy);
     603      3192593 :       free (a1);
     604      3192593 :       a1 = a2;
     605              :     }
     606     90351731 : }
     607              : 
     608              : 
     609              : /* Copy an arglist structure and all of the arguments.  */
     610              : 
     611              : gfc_actual_arglist *
     612       401611 : gfc_copy_actual_arglist (gfc_actual_arglist *p)
     613              : {
     614       401611 :   gfc_actual_arglist *head, *tail, *new_arg;
     615              : 
     616       401611 :   head = tail = NULL;
     617              : 
     618      1170864 :   for (; p; p = p->next)
     619              :     {
     620       769253 :       new_arg = gfc_get_actual_arglist ();
     621       769253 :       *new_arg = *p;
     622              : 
     623       769253 :       if (p->associated_dummy != NULL)
     624              :         {
     625       690333 :           new_arg->associated_dummy = gfc_get_dummy_arg ();
     626       690333 :           *new_arg->associated_dummy = *p->associated_dummy;
     627              :         }
     628              : 
     629       769253 :       new_arg->expr = gfc_copy_expr (p->expr);
     630       769253 :       new_arg->next = NULL;
     631              : 
     632       769253 :       if (head == NULL)
     633              :         head = new_arg;
     634              :       else
     635       369723 :         tail->next = new_arg;
     636              : 
     637       769253 :       tail = new_arg;
     638              :     }
     639              : 
     640       401611 :   return head;
     641              : }
     642              : 
     643              : 
     644              : /* Free a list of reference structures.  */
     645              : 
     646              : void
     647     88420598 : gfc_free_ref_list (gfc_ref *p)
     648              : {
     649     88420598 :   gfc_ref *q;
     650     88420598 :   int i;
     651              : 
     652     89724434 :   for (; p; p = q)
     653              :     {
     654      1303836 :       q = p->next;
     655              : 
     656      1303836 :       switch (p->type)
     657              :         {
     658              :         case REF_ARRAY:
     659     15717008 :           for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
     660              :             {
     661     14734695 :               gfc_free_expr (p->u.ar.start[i]);
     662     14734695 :               gfc_free_expr (p->u.ar.end[i]);
     663     14734695 :               gfc_free_expr (p->u.ar.stride[i]);
     664              :             }
     665              : 
     666       982313 :           gfc_free_expr (p->u.ar.stat);
     667       982313 :           gfc_free_expr (p->u.ar.team);
     668       982313 :           break;
     669              : 
     670        20814 :         case REF_SUBSTRING:
     671        20814 :           gfc_free_expr (p->u.ss.start);
     672        20814 :           gfc_free_expr (p->u.ss.end);
     673        20814 :           break;
     674              : 
     675              :         case REF_COMPONENT:
     676              :         case REF_INQUIRY:
     677              :           break;
     678              :         }
     679              : 
     680      1303836 :       free (p);
     681              :     }
     682     88420598 : }
     683              : 
     684              : 
     685              : /* Graft the *src expression onto the *dest subexpression.  */
     686              : 
     687              : void
     688     30854445 : gfc_replace_expr (gfc_expr *dest, gfc_expr *src)
     689              : {
     690     30854445 :   free_expr0 (dest);
     691     30854445 :   *dest = *src;
     692     30854445 :   free (src);
     693     30854445 : }
     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       465211 : gfc_extract_int (gfc_expr *expr, int *result, int report_error)
     703              : {
     704       465211 :   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       465211 :   if (gfc_expr_attr(expr).pdt_kind)
     710              :     {
     711           16 :       for (ref = expr->ref; ref; ref = ref->next)
     712              :         {
     713            8 :            if (ref->u.c.component->attr.pdt_kind)
     714            8 :              expr = ref->u.c.component->initializer;
     715              :         }
     716              :     }
     717              : 
     718       465211 :   if (expr->expr_type != EXPR_CONSTANT)
     719              :     {
     720          923 :       if (report_error > 0)
     721          908 :         gfc_error ("Constant expression required at %C");
     722           15 :       else if (report_error < 0)
     723            4 :         gfc_error_now ("Constant expression required at %C");
     724          923 :       return true;
     725              :     }
     726              : 
     727       464288 :   if (expr->ts.type != BT_INTEGER)
     728              :     {
     729          454 :       if (report_error > 0)
     730          454 :         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          454 :       return true;
     734              :     }
     735              : 
     736       463834 :   if ((mpz_cmp_si (expr->value.integer, INT_MAX) > 0)
     737       463834 :       || (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       463834 :   *result = (int) mpz_get_si (expr->value.integer);
     747              : 
     748       463834 :   return false;
     749              : }
     750              : 
     751              : /* Same as gfc_extract_int, but use a HWI.  */
     752              : 
     753              : bool
     754        10372 : gfc_extract_hwi (gfc_expr *expr, HOST_WIDE_INT *result, int report_error)
     755              : {
     756        10372 :   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        10372 :   if (gfc_expr_attr(expr).pdt_kind)
     762              :     {
     763            3 :       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        10372 :   if (expr->expr_type != EXPR_CONSTANT)
     771              :     {
     772          148 :       if (report_error > 0)
     773            0 :         gfc_error ("Constant expression required at %C");
     774          148 :       else if (report_error < 0)
     775            0 :         gfc_error_now ("Constant expression required at %C");
     776          148 :       return true;
     777              :     }
     778              : 
     779        10224 :   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        10224 :   const wide_int val = wi::from_mpz (long_long_integer_type_node,
     790        10224 :                                      expr->value.integer, false);
     791              : 
     792        10224 :   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        10224 :   *result = val.to_shwi ();
     802              : 
     803        10224 :   return false;
     804        10224 : }
     805              : 
     806              : 
     807              : /* Recursively copy a list of reference structures.  */
     808              : 
     809              : gfc_ref *
     810     48444380 : gfc_copy_ref (gfc_ref *src)
     811              : {
     812     48444380 :   gfc_array_ref *ar;
     813     48444380 :   gfc_ref *dest;
     814              : 
     815     48444380 :   if (src == NULL)
     816              :     return NULL;
     817              : 
     818       235036 :   dest = gfc_get_ref ();
     819       235036 :   dest->type = src->type;
     820              : 
     821       235036 :   switch (src->type)
     822              :     {
     823       170227 :     case REF_ARRAY:
     824       170227 :       ar = gfc_copy_array_ref (&src->u.ar);
     825       170227 :       dest->u.ar = *ar;
     826       170227 :       free (ar);
     827       170227 :       break;
     828              : 
     829        56720 :     case REF_COMPONENT:
     830        56720 :       dest->u.c = src->u.c;
     831        56720 :       break;
     832              : 
     833         2221 :     case REF_INQUIRY:
     834         2221 :       dest->u.i = src->u.i;
     835         2221 :       break;
     836              : 
     837         5868 :     case REF_SUBSTRING:
     838         5868 :       dest->u.ss = src->u.ss;
     839         5868 :       dest->u.ss.start = gfc_copy_expr (src->u.ss.start);
     840         5868 :       dest->u.ss.end = gfc_copy_expr (src->u.ss.end);
     841         5868 :       break;
     842              :     }
     843              : 
     844       235036 :   dest->next = gfc_copy_ref (src->next);
     845              : 
     846       235036 :   return dest;
     847              : }
     848              : 
     849              : 
     850              : /* Detect whether an expression has any vector index array references.  */
     851              : 
     852              : bool
     853        36122 : gfc_has_vector_index (gfc_expr *e)
     854              : {
     855        36122 :   gfc_ref *ref;
     856        36122 :   int i;
     857        43445 :   for (ref = e->ref; ref; ref = ref->next)
     858         7333 :     if (ref->type == REF_ARRAY)
     859        12361 :       for (i = 0; i < ref->u.ar.dimen; i++)
     860         6671 :         if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
     861              :           return 1;
     862              :   return 0;
     863              : }
     864              : 
     865              : 
     866              : bool
     867         2300 : gfc_is_ptr_fcn (gfc_expr *e)
     868              : {
     869         2300 :   return e != NULL && e->expr_type == EXPR_FUNCTION
     870         2790 :               && gfc_expr_attr (e).pointer;
     871              : }
     872              : 
     873              : 
     874              : /* Copy a shape array.  */
     875              : 
     876              : mpz_t *
     877     48446180 : gfc_copy_shape (mpz_t *shape, int rank)
     878              : {
     879     48446180 :   mpz_t *new_shape;
     880     48446180 :   int n;
     881              : 
     882     48446180 :   if (shape == NULL)
     883              :     return NULL;
     884              : 
     885       151635 :   new_shape = gfc_get_shape (rank);
     886              : 
     887       510630 :   for (n = 0; n < rank; n++)
     888       207360 :     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        95609 : gfc_kind_max (gfc_expr *e1, gfc_expr *e2)
     943              : {
     944        95609 :   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     25521279 : 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     25516684 : gfc_numeric_ts (gfc_typespec *ts)
     962              : {
     963     25516684 :   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       132728 : gfc_build_conversion (gfc_expr *e)
     973              : {
     974       132728 :   gfc_expr *p;
     975              : 
     976       132728 :   p = gfc_get_expr ();
     977       132728 :   p->expr_type = EXPR_FUNCTION;
     978       132728 :   p->symtree = NULL;
     979       132728 :   p->value.function.actual = gfc_get_actual_arglist ();
     980       132728 :   p->value.function.actual->expr = e;
     981              : 
     982       132728 :   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     12197108 : gfc_type_convert_binary (gfc_expr *e, int wconversion)
     998              : {
     999     12197108 :   gfc_expr *op1, *op2;
    1000              : 
    1001     12197108 :   op1 = e->value.op.op1;
    1002     12197108 :   op2 = e->value.op.op2;
    1003              : 
    1004     12197108 :   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     12197108 :   if (op1->ts.type == op2->ts.type)
    1012              :     {
    1013     12175406 :       if (op1->ts.kind == op2->ts.kind)
    1014              :         {
    1015              :           /* No type conversions.  */
    1016     12062386 :           e->ts = op1->ts;
    1017     12062386 :           goto done;
    1018              :         }
    1019              : 
    1020              :       /* Unsigned exponentiation is special, we need the type of the first
    1021              :          argument here because of modulo arithmetic.  */
    1022       113020 :       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        28642 :       if (op1->ts.kind > op2->ts.kind)
    1029        21733 :         gfc_convert_type_warn (op2, &op1->ts, 2, wconversion);
    1030              :       else
    1031         6909 :         gfc_convert_type_warn (op1, &op2->ts, 2, wconversion);
    1032              : 
    1033        28642 :       e->ts = op1->ts;
    1034        28642 :       goto done;
    1035              :     }
    1036              : 
    1037              :   /* Integer combined with real or complex.  */
    1038        21702 :   if (op2->ts.type == BT_INTEGER)
    1039              :     {
    1040        16629 :       e->ts = op1->ts;
    1041              : 
    1042              :       /* Special case for ** operator.  */
    1043        16629 :       if (e->value.op.op == INTRINSIC_POWER)
    1044         4695 :         goto done;
    1045              : 
    1046        11934 :       gfc_convert_type_warn (e->value.op.op2, &e->ts, 2, wconversion);
    1047        11934 :       goto done;
    1048              :     }
    1049              : 
    1050         5073 :   if (op1->ts.type == BT_INTEGER)
    1051              :     {
    1052         4475 :       e->ts = op2->ts;
    1053         4475 :       gfc_convert_type_warn (e->value.op.op1, &e->ts, 2, wconversion);
    1054         4475 :       goto done;
    1055              :     }
    1056              : 
    1057              :   /* Real combined with complex.  */
    1058          598 :   e->ts.type = BT_COMPLEX;
    1059          598 :   if (op1->ts.kind > op2->ts.kind)
    1060           25 :     e->ts.kind = op1->ts.kind;
    1061              :   else
    1062          573 :     e->ts.kind = op2->ts.kind;
    1063          598 :   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          598 :   if (op2->ts.type != BT_COMPLEX || op2->ts.kind != e->ts.kind)
    1066          494 :     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       283367 : is_non_constant_intrinsic (gfc_expr *e)
    1079              : {
    1080       283367 :   if (e->expr_type == EXPR_FUNCTION
    1081       283367 :       && e->value.function.isym)
    1082              :     {
    1083       283367 :       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       279886 :         default:
    1094       279886 :           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     45297965 : gfc_is_constant_expr (gfc_expr *e)
    1106              : {
    1107     45297965 :   gfc_constructor *c;
    1108     45297965 :   gfc_actual_arglist *arg;
    1109              : 
    1110     45297965 :   if (e == NULL)
    1111              :     return true;
    1112              : 
    1113     45278565 :   switch (e->expr_type)
    1114              :     {
    1115      1114448 :     case EXPR_OP:
    1116      1114448 :       return (gfc_is_constant_expr (e->value.op.op1)
    1117      1114448 :               && (e->value.op.op2 == NULL
    1118       102562 :                   || 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      1478309 :     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      1478309 :       if (e->symtree->n.sym->attr.pdt_len
    1129      1476350 :           || e->symtree->n.sym->attr.pdt_kind)
    1130              :         return true;
    1131              :       return false;
    1132              : 
    1133       346915 :     case EXPR_FUNCTION:
    1134       346915 :     case EXPR_PPC:
    1135       346915 :     case EXPR_COMPCALL:
    1136       346915 :       gcc_assert (e->symtree || e->value.function.esym
    1137              :                   || e->value.function.isym);
    1138              : 
    1139              :       /* Check for intrinsics excluded in constant expressions.  */
    1140       346915 :       if (e->value.function.isym && is_non_constant_intrinsic (e))
    1141              :         return false;
    1142              : 
    1143              :       /* Call to intrinsic with at least one argument.  */
    1144       343434 :       if (e->value.function.isym && e->value.function.actual)
    1145              :         {
    1146       287536 :           for (arg = e->value.function.actual; arg; arg = arg->next)
    1147       284244 :             if (!gfc_is_constant_expr (arg->expr))
    1148              :               return false;
    1149              :         }
    1150              : 
    1151        67000 :       if (e->value.function.isym
    1152         3452 :           && (e->value.function.isym->elemental
    1153         3379 :               || 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         2022 :     case EXPR_SUBSTRING:
    1165         2022 :       return e->ref == NULL || (gfc_is_constant_expr (e->ref->u.ss.start)
    1166          817 :                                 && gfc_is_constant_expr (e->ref->u.ss.end));
    1167              : 
    1168       156493 :     case EXPR_ARRAY:
    1169       156493 :     case EXPR_STRUCTURE:
    1170       156493 :       c = gfc_constructor_first (e->value.constructor);
    1171       156493 :       if ((e->expr_type == EXPR_ARRAY) && c && c->iterator)
    1172         5604 :         return gfc_constant_ac (e);
    1173              : 
    1174      1942292 :       for (; c; c = gfc_constructor_next (c))
    1175      1802301 :         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       715018 : is_CFI_desc (gfc_symbol *sym, gfc_expr *e)
    1191              : {
    1192       715018 :   if (sym == NULL
    1193       715018 :       && e && e->expr_type == EXPR_VARIABLE)
    1194       178284 :     sym = e->symtree->n.sym;
    1195              : 
    1196       715018 :   if (sym && sym->attr.dummy
    1197       301184 :       && sym->ns->proc_name->attr.is_bind_c
    1198        77516 :       && (sym->attr.pointer
    1199        73130 :           || sym->attr.allocatable
    1200        69867 :           || (sym->attr.dimension
    1201        42474 :               && (sym->as->type == AS_ASSUMED_SHAPE
    1202        26194 :                   || sym->as->type == AS_ASSUMED_RANK))
    1203        42722 :           || (sym->ts.type == BT_CHARACTER
    1204        14661 :               && (!sym->ts.u.cl || !sym->ts.u.cl->length))))
    1205        47241 :     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       231852 : is_subref_array (gfc_expr * e)
    1215              : {
    1216       231852 :   gfc_ref * ref;
    1217       231852 :   bool seen_array;
    1218       231852 :   gfc_symbol *sym;
    1219              : 
    1220       231852 :   if (e->expr_type != EXPR_VARIABLE)
    1221              :     return false;
    1222              : 
    1223       230772 :   sym = e->symtree->n.sym;
    1224              : 
    1225       230772 :   if (sym->attr.subref_array_pointer)
    1226              :     return true;
    1227              : 
    1228       227145 :   seen_array = false;
    1229              : 
    1230       477849 :   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       253271 :       if (!seen_array && ref->type == REF_COMPONENT
    1236        29591 :           && ref->next == NULL
    1237         4492 :           && ref->u.c.component->ts.type != BT_CHARACTER
    1238         4465 :           && ref->u.c.component->ts.type != BT_CLASS
    1239         4091 :           && !gfc_bt_struct (ref->u.c.component->ts.type))
    1240              :         return false;
    1241              : 
    1242       253126 :       if (ref->type == REF_ARRAY
    1243       221058 :             && ref->u.ar.type != AR_ELEMENT)
    1244              :         seen_array = true;
    1245              : 
    1246        35534 :       if (seen_array
    1247       220014 :             && ref->type != REF_ARRAY)
    1248              :         return seen_array;
    1249              :     }
    1250              : 
    1251       224578 :   if (sym->ts.type == BT_CLASS
    1252        20758 :       && sym->attr.dummy
    1253         6388 :       && CLASS_DATA (sym)->attr.dimension
    1254         3987 :       && CLASS_DATA (sym)->attr.class_pointer)
    1255          628 :     return true;
    1256              : 
    1257              :   return false;
    1258              : }
    1259              : 
    1260              : 
    1261              : /* Try to collapse intrinsic expressions.  */
    1262              : 
    1263              : static bool
    1264     17291253 : simplify_intrinsic_op (gfc_expr *p, int type)
    1265              : {
    1266     17291253 :   gfc_intrinsic_op op;
    1267     17291253 :   gfc_expr *op1, *op2, *result;
    1268              : 
    1269     17291253 :   if (p->value.op.op == INTRINSIC_USER)
    1270              :     return true;
    1271              : 
    1272     17291250 :   op1 = p->value.op.op1;
    1273     17291250 :   op2 = p->value.op.op2;
    1274     17291250 :   op  = p->value.op.op;
    1275              : 
    1276     17291250 :   if (!gfc_simplify_expr (op1, type))
    1277              :     return false;
    1278     17291012 :   if (!gfc_simplify_expr (op2, type))
    1279              :     return false;
    1280              : 
    1281     17290964 :   if (!gfc_is_constant_expr (op1)
    1282     17290964 :       || (op2 != NULL && !gfc_is_constant_expr (op2)))
    1283       898368 :     return true;
    1284              : 
    1285              :   /* Rip p apart.  */
    1286     16392596 :   p->value.op.op1 = NULL;
    1287     16392596 :   p->value.op.op2 = NULL;
    1288              : 
    1289     16392596 :   switch (op)
    1290              :     {
    1291      5258498 :     case INTRINSIC_PARENTHESES:
    1292      5258498 :       result = gfc_parentheses (op1);
    1293      5258498 :       break;
    1294              : 
    1295           31 :     case INTRINSIC_UPLUS:
    1296           31 :       result = gfc_uplus (op1);
    1297           31 :       break;
    1298              : 
    1299        13146 :     case INTRINSIC_UMINUS:
    1300        13146 :       result = gfc_uminus (op1);
    1301        13146 :       break;
    1302              : 
    1303     10280092 :     case INTRINSIC_PLUS:
    1304     10280092 :       result = gfc_add (op1, op2);
    1305     10280092 :       break;
    1306              : 
    1307       503131 :     case INTRINSIC_MINUS:
    1308       503131 :       result = gfc_subtract (op1, op2);
    1309       503131 :       break;
    1310              : 
    1311       297285 :     case INTRINSIC_TIMES:
    1312       297285 :       result = gfc_multiply (op1, op2);
    1313       297285 :       break;
    1314              : 
    1315         5764 :     case INTRINSIC_DIVIDE:
    1316         5764 :       result = gfc_divide (op1, op2);
    1317         5764 :       break;
    1318              : 
    1319         5998 :     case INTRINSIC_POWER:
    1320         5998 :       result = gfc_power (op1, op2);
    1321         5998 :       break;
    1322              : 
    1323         2427 :     case INTRINSIC_CONCAT:
    1324         2427 :       result = gfc_concat (op1, op2);
    1325         2427 :       break;
    1326              : 
    1327         1213 :     case INTRINSIC_EQ:
    1328         1213 :     case INTRINSIC_EQ_OS:
    1329         1213 :       result = gfc_eq (op1, op2, op);
    1330         1213 :       break;
    1331              : 
    1332        20555 :     case INTRINSIC_NE:
    1333        20555 :     case INTRINSIC_NE_OS:
    1334        20555 :       result = gfc_ne (op1, op2, op);
    1335        20555 :       break;
    1336              : 
    1337          601 :     case INTRINSIC_GT:
    1338          601 :     case INTRINSIC_GT_OS:
    1339          601 :       result = gfc_gt (op1, op2, op);
    1340          601 :       break;
    1341              : 
    1342           71 :     case INTRINSIC_GE:
    1343           71 :     case INTRINSIC_GE_OS:
    1344           71 :       result = gfc_ge (op1, op2, op);
    1345           71 :       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          490 :     case INTRINSIC_NOT:
    1358          490 :       result = gfc_not (op1);
    1359          490 :       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     16392596 :   if (result == NULL)
    1382              :     {
    1383           55 :       gfc_free_expr (op1);
    1384           55 :       gfc_free_expr (op2);
    1385           55 :       return false;
    1386              :     }
    1387              : 
    1388     16392541 :   result->rank = p->rank;
    1389     16392541 :   result->corank = p->corank;
    1390     16392541 :   result->where = p->where;
    1391     16392541 :   gfc_replace_expr (p, result);
    1392              : 
    1393     16392541 :   return true;
    1394              : }
    1395              : 
    1396              : /* Try to collapse conditional expressions.  */
    1397              : 
    1398              : static bool
    1399           27 : simplify_conditional (gfc_expr *p, int type)
    1400              : {
    1401           27 :   gfc_expr *condition, *true_expr, *false_expr;
    1402              : 
    1403           27 :   condition = p->value.conditional.condition;
    1404           27 :   true_expr = p->value.conditional.true_expr;
    1405           27 :   false_expr = p->value.conditional.false_expr;
    1406              : 
    1407           27 :   if (!gfc_simplify_expr (condition, type)
    1408           27 :       || !gfc_simplify_expr (true_expr, type)
    1409           54 :       || !gfc_simplify_expr (false_expr, type))
    1410            0 :     return false;
    1411              : 
    1412           27 :   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       131422 : simplify_constructor (gfc_constructor_base base, int type)
    1439              : {
    1440       131422 :   gfc_constructor *c;
    1441       131422 :   gfc_expr *p;
    1442              : 
    1443       810897 :   for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c))
    1444              :     {
    1445       679475 :       if (c->iterator
    1446       679475 :           && (!gfc_simplify_expr(c->iterator->start, type)
    1447          765 :               || !gfc_simplify_expr (c->iterator->end, type)
    1448          765 :               || !gfc_simplify_expr (c->iterator->step, type)))
    1449            0 :         return false;
    1450              : 
    1451       679475 :       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        39566 :           p = gfc_copy_expr (c->expr);
    1457              : 
    1458        39566 :           if (!gfc_simplify_expr (p, type))
    1459              :             {
    1460           10 :               gfc_free_expr (p);
    1461           10 :               continue;
    1462              :             }
    1463              : 
    1464        39556 :           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         4754 : find_array_element (gfc_constructor_base base, gfc_array_ref *ar,
    1476              :                     gfc_constructor **rval)
    1477              : {
    1478         4754 :   unsigned long nelemen;
    1479         4754 :   int i;
    1480         4754 :   mpz_t delta;
    1481         4754 :   mpz_t offset;
    1482         4754 :   mpz_t span;
    1483         4754 :   mpz_t tmp;
    1484         4754 :   gfc_constructor *cons;
    1485         4754 :   gfc_expr *e;
    1486         4754 :   bool t;
    1487              : 
    1488         4754 :   t = true;
    1489         4754 :   e = NULL;
    1490              : 
    1491         4754 :   mpz_init_set_ui (offset, 0);
    1492         4754 :   mpz_init (delta);
    1493         4754 :   mpz_init (tmp);
    1494         4754 :   mpz_init_set_ui (span, 1);
    1495        12320 :   for (i = 0; i < ar->dimen; i++)
    1496              :     {
    1497         4821 :       if (!gfc_reduce_init_expr (ar->as->lower[i])
    1498         4816 :           || !gfc_reduce_init_expr (ar->as->upper[i])
    1499         4816 :           || ar->as->upper[i]->expr_type != EXPR_CONSTANT
    1500         9633 :           || ar->as->lower[i]->expr_type != EXPR_CONSTANT)
    1501              :         {
    1502            9 :           t = false;
    1503            9 :           cons = NULL;
    1504            9 :           goto depart;
    1505              :         }
    1506              : 
    1507         4812 :       e = ar->start[i];
    1508         4812 :       if (e->expr_type != EXPR_CONSTANT)
    1509              :         {
    1510         1991 :           cons = NULL;
    1511         1991 :           goto depart;
    1512              :         }
    1513              : 
    1514              :       /* Check the bounds.  */
    1515         2821 :       if ((ar->as->upper[i]
    1516         2821 :            && mpz_cmp (e->value.integer,
    1517         2821 :                        ar->as->upper[i]->value.integer) > 0)
    1518         2812 :           || (mpz_cmp (e->value.integer,
    1519         2812 :                        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         2812 :       mpz_sub (delta, e->value.integer, ar->as->lower[i]->value.integer);
    1529         2812 :       mpz_mul (delta, delta, span);
    1530         2812 :       mpz_add (offset, offset, delta);
    1531              : 
    1532         2812 :       mpz_set_ui (tmp, 1);
    1533         2812 :       mpz_add (tmp, tmp, ar->as->upper[i]->value.integer);
    1534         2812 :       mpz_sub (tmp, tmp, ar->as->lower[i]->value.integer);
    1535         2812 :       mpz_mul (span, span, tmp);
    1536              :     }
    1537              : 
    1538         2745 :   for (cons = gfc_constructor_first (base), nelemen = mpz_get_ui (offset);
    1539        11928 :        cons && nelemen > 0; cons = gfc_constructor_next (cons), nelemen--)
    1540              :     {
    1541         9183 :       if (cons->iterator)
    1542              :         {
    1543            0 :           cons = NULL;
    1544            0 :           goto depart;
    1545              :         }
    1546              :     }
    1547              : 
    1548         2745 : depart:
    1549         4754 :   mpz_clear (delta);
    1550         4754 :   mpz_clear (offset);
    1551         4754 :   mpz_clear (span);
    1552         4754 :   mpz_clear (tmp);
    1553         4754 :   *rval = cons;
    1554         4754 :   return t;
    1555              : }
    1556              : 
    1557              : 
    1558              : /* Find a component of a structure constructor.  */
    1559              : 
    1560              : static gfc_constructor *
    1561         1766 : find_component_ref (gfc_constructor_base base, gfc_ref *ref)
    1562              : {
    1563         1766 :   gfc_component *pick = ref->u.c.component;
    1564         1766 :   gfc_constructor *c = gfc_constructor_first (base);
    1565              : 
    1566         1766 :   gfc_symbol *dt = ref->u.c.sym;
    1567         1766 :   int ext = dt->attr.extension;
    1568              : 
    1569              :   /* For extended types, check if the desired component is in one of the
    1570              :    * parent types.  */
    1571         1856 :   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         1766 :   gfc_component *comp = dt->components;
    1580         1904 :   while (comp != pick)
    1581              :     {
    1582          138 :       comp = comp->next;
    1583          138 :       c = gfc_constructor_next (c);
    1584              :     }
    1585              : 
    1586         1766 :   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         4529 : remove_subobject_ref (gfc_expr *p, gfc_constructor *cons)
    1595              : {
    1596         4529 :   gfc_expr *e;
    1597              : 
    1598         4529 :   if (cons)
    1599              :     {
    1600         4511 :       e = cons->expr;
    1601         4511 :       cons->expr = NULL;
    1602              :     }
    1603              :   else
    1604           18 :     e = gfc_copy_expr (p);
    1605         4529 :   e->ref = p->ref->next;
    1606         4529 :   p->ref->next =  NULL;
    1607         4529 :   gfc_replace_expr (p, e);
    1608         4529 : }
    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         2037 : find_inquiry_ref (gfc_expr *p, gfc_expr **newp)
    1970              : {
    1971         2037 :   gfc_ref *ref;
    1972         2037 :   gfc_ref *inquiry = NULL;
    1973         2037 :   gfc_ref *inquiry_head;
    1974         2037 :   gfc_ref *ref_ss = NULL;
    1975         2037 :   gfc_expr *tmp;
    1976         2037 :   bool nofail = false;
    1977              : 
    1978         2037 :   tmp = gfc_copy_expr (p);
    1979              : 
    1980         2037 :   if (tmp->ref && tmp->ref->type == REF_INQUIRY)
    1981              :     {
    1982          560 :       inquiry = tmp->ref;
    1983          560 :       tmp->ref = NULL;
    1984              :     }
    1985              :   else
    1986              :     {
    1987         1636 :       for (ref = tmp->ref; ref; ref = ref->next)
    1988         1636 :         if (ref->next && ref->next->type == REF_INQUIRY)
    1989              :           {
    1990         1477 :             inquiry = ref->next;
    1991         1477 :             ref->next = NULL;
    1992         1477 :             if (ref->type == REF_SUBSTRING)
    1993           14 :               ref_ss = ref;
    1994              :             break;
    1995              :           }
    1996              :     }
    1997              : 
    1998         2037 :   if (!inquiry)
    1999              :     {
    2000            0 :       gfc_free_expr (tmp);
    2001            0 :       return false;
    2002              :     }
    2003              : 
    2004         2037 :   inquiry_head = inquiry;
    2005         2037 :   gfc_resolve_expr (tmp);
    2006              : 
    2007              :   /* Leave these to the backend since the type and kind is not confirmed until
    2008              :      resolution.  */
    2009         2037 :   if (IS_INFERRED_TYPE (tmp))
    2010          282 :     goto cleanup;
    2011              : 
    2012              :   /* In principle there can be more than one inquiry reference.  */
    2013         2174 :   for (; inquiry; inquiry = inquiry->next)
    2014              :     {
    2015         1755 :       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          822 :         case INQUIRY_RE:
    2076          822 :           if (tmp->ts.type != BT_COMPLEX)
    2077           77 :             goto cleanup;
    2078              : 
    2079          745 :           if (!gfc_notify_std (GFC_STD_F2008, "RE part_ref at %C"))
    2080            0 :             goto cleanup;
    2081              : 
    2082          745 :           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          715 :           if (tmp->expr_type != EXPR_CONSTANT)
    2093          661 :             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         1618 : cleanup:
    2143         1618 :   gfc_free_expr (tmp);
    2144         1618 :   gfc_free_ref_list (inquiry_head);
    2145         1618 :   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       134036 : simplify_const_ref (gfc_expr *p)
    2155              : {
    2156       134036 :   gfc_constructor *cons, *c;
    2157       134036 :   gfc_expr *newp = NULL;
    2158       134036 :   gfc_ref *last_ref;
    2159              : 
    2160       282828 :   while (p->ref)
    2161              :     {
    2162        17209 :       switch (p->ref->type)
    2163              :         {
    2164        14185 :         case REF_ARRAY:
    2165        14185 :           switch (p->ref->u.ar.type)
    2166              :             {
    2167         4772 :             case AR_ELEMENT:
    2168              :               /* <type/kind spec>, parameter :: x(<int>) = scalar_expr
    2169              :                  will generate this.  */
    2170         4772 :               if (p->expr_type != EXPR_ARRAY)
    2171              :                 {
    2172           18 :                   remove_subobject_ref (p, NULL);
    2173           18 :                   break;
    2174              :                 }
    2175         4754 :               if (!find_array_element (p->value.constructor, &p->ref->u.ar, &cons))
    2176              :                 return false;
    2177              : 
    2178         4736 :               if (!cons)
    2179              :                 return true;
    2180              : 
    2181         2745 :               remove_subobject_ref (p, cons);
    2182         2745 :               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         8969 :             case AR_FULL:
    2192         8969 :               if (p->ref->next != NULL
    2193          336 :                   && (p->ts.type == BT_CHARACTER || gfc_bt_struct (p->ts.type)))
    2194              :                 {
    2195          336 :                   for (c = gfc_constructor_first (p->value.constructor);
    2196         2950 :                        c; c = gfc_constructor_next (c))
    2197              :                     {
    2198         2614 :                       c->expr->ref = gfc_copy_ref (p->ref->next);
    2199         2614 :                       if (!simplify_const_ref (c->expr))
    2200              :                         return false;
    2201              :                     }
    2202              : 
    2203           75 :                   if (gfc_bt_struct (p->ts.type)
    2204          261 :                         && p->ref->next
    2205          597 :                         && (c = gfc_constructor_first (p->value.constructor)))
    2206              :                     {
    2207              :                       /* There may have been component references.  */
    2208          261 :                       p->ts = c->expr->ts;
    2209              :                     }
    2210              : 
    2211          336 :                   last_ref = p->ref;
    2212          690 :                   for (; last_ref->next; last_ref = last_ref->next) {};
    2213              : 
    2214          336 :                   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         8969 :               gfc_free_ref_list (p->ref);
    2250         8969 :               p->ref = NULL;
    2251         8969 :               break;
    2252              : 
    2253              :             default:
    2254              :               return true;
    2255              :             }
    2256              : 
    2257              :           break;
    2258              : 
    2259         1766 :         case REF_COMPONENT:
    2260         1766 :           cons = find_component_ref (p->value.constructor, p->ref);
    2261         1766 :           remove_subobject_ref (p, cons);
    2262         1766 :           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     15110635 : simplify_ref_chain (gfc_ref *ref, int type, gfc_expr **p)
    2292              : {
    2293     15110635 :   int n;
    2294     15110635 :   gfc_expr *newp = NULL;
    2295              : 
    2296     15441313 :   for (; ref; ref = ref->next)
    2297              :     {
    2298       332716 :       switch (ref->type)
    2299              :         {
    2300              :         case REF_ARRAY:
    2301       583165 :           for (n = 0; n < ref->u.ar.dimen; n++)
    2302              :             {
    2303       325047 :               if (!gfc_simplify_expr (ref->u.ar.start[n], type))
    2304              :                 return false;
    2305       325047 :               if (!gfc_simplify_expr (ref->u.ar.end[n], type))
    2306              :                 return false;
    2307       325047 :               if (!gfc_simplify_expr (ref->u.ar.stride[n], type))
    2308              :                 return false;
    2309              :             }
    2310              :           break;
    2311              : 
    2312         9724 :         case REF_SUBSTRING:
    2313         9724 :           if (!gfc_simplify_expr (ref->u.ss.start, type))
    2314              :             return false;
    2315         9724 :           if (!gfc_simplify_expr (ref->u.ss.end, type))
    2316              :             return false;
    2317              :           break;
    2318              : 
    2319         2037 :         case REF_INQUIRY:
    2320         2037 :           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        14951 : simplify_parameter_variable (gfc_expr *p, int type)
    2340              : {
    2341        14951 :   gfc_expr *e;
    2342        14951 :   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        14951 :   if (!gfc_resolve_ref (p))
    2347              :     {
    2348            1 :       gfc_error_check ();
    2349            1 :       return false;
    2350              :     }
    2351        14950 :   gfc_expression_rank (p);
    2352              : 
    2353              :   /* Is this an inquiry?  */
    2354        14950 :   bool inquiry = false;
    2355        14950 :   gfc_ref* ref = p->ref;
    2356        30711 :   while (ref)
    2357              :     {
    2358        15889 :       if (ref->type == REF_INQUIRY)
    2359              :         break;
    2360        15761 :       ref = ref->next;
    2361              :     }
    2362        14950 :   if (ref && ref->type == REF_INQUIRY)
    2363          128 :     inquiry = ref->u.i == INQUIRY_LEN || ref->u.i == INQUIRY_KIND;
    2364              : 
    2365        14950 :   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        14260 :       e = gfc_copy_expr (p->symtree->n.sym->value);
    2389        14260 :       if (e == NULL)
    2390              :         return false;
    2391              : 
    2392        14160 :       gfc_free_shape (&e->shape, e->rank);
    2393        14160 :       e->shape = gfc_copy_shape (p->shape, p->rank);
    2394        14160 :       e->rank = p->rank;
    2395        14160 :       e->corank = p->corank;
    2396              : 
    2397        14160 :       if (e->ts.type == BT_CHARACTER && p->ts.u.cl)
    2398         3483 :         e->ts = p->ts;
    2399              :     }
    2400              : 
    2401        14230 :   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        14230 :   if (e->expr_type != EXPR_CONSTANT && p->ref != NULL)
    2406        14225 :     e->ref = gfc_copy_ref (p->ref);
    2407        14230 :   t = gfc_simplify_expr (e, type);
    2408        14230 :   e->where = p->where;
    2409              : 
    2410              :   /* Only use the simplification if it eliminated all subobject references.  */
    2411        14230 :   if (t && !e->ref)
    2412        11774 :     gfc_replace_expr (p, e);
    2413              :   else
    2414         2456 :     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     56305418 : gfc_simplify_expr (gfc_expr *p, int type)
    2444              : {
    2445     56305418 :   gfc_actual_arglist *ap;
    2446     56305418 :   gfc_intrinsic_sym* isym = NULL;
    2447              : 
    2448              : 
    2449     56305418 :   if (p == NULL)
    2450              :     return true;
    2451              : 
    2452     49974684 :   switch (p->expr_type)
    2453              :     {
    2454     16983355 :     case EXPR_CONSTANT:
    2455     16983355 :       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       572752 :     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       572752 :       ap = p->value.function.actual;
    2468       572752 :       if (p->value.function.isym &&
    2469       537103 :           (p->value.function.isym->id == GFC_ISYM_LBOUND
    2470       524068 :            || p->value.function.isym->id == GFC_ISYM_UBOUND
    2471       516249 :            || p->value.function.isym->id == GFC_ISYM_LCOBOUND
    2472       516003 :            || p->value.function.isym->id == GFC_ISYM_UCOBOUND
    2473       515749 :            || p->value.function.isym->id == GFC_ISYM_SHAPE))
    2474        26049 :         ap = ap->next;
    2475              : 
    2476      1661157 :       for ( ; ap; ap = ap->next)
    2477      1088559 :         if (!gfc_simplify_expr (ap->expr, type))
    2478              :           return false;
    2479              : 
    2480       572598 :       if (p->value.function.isym != NULL
    2481       572598 :           && gfc_intrinsic_func_interface (p, 1) == MATCH_ERROR)
    2482              :         return false;
    2483              : 
    2484       572539 :       if (p->symtree && (p->value.function.isym || p->ts.type == BT_UNKNOWN))
    2485              :         {
    2486       218634 :           isym = gfc_find_function (p->symtree->n.sym->name);
    2487       218634 :           if (isym && isym->elemental)
    2488       110012 :             scalarize_intrinsic_call (p, false);
    2489              :         }
    2490              : 
    2491              :       break;
    2492              : 
    2493         1444 :     case EXPR_SUBSTRING:
    2494         1444 :       if (!simplify_ref_chain (p->ref, type, &p))
    2495              :         return false;
    2496              : 
    2497         1444 :       if (gfc_is_constant_expr (p))
    2498              :         {
    2499          772 :           gfc_char_t *s;
    2500          772 :           HOST_WIDE_INT start, end;
    2501              : 
    2502          772 :           start = 0;
    2503          772 :           if (p->ref && p->ref->u.ss.start)
    2504              :             {
    2505          747 :               gfc_extract_hwi (p->ref->u.ss.start, &start);
    2506          747 :               start--;  /* Convert from one-based to zero-based.  */
    2507              :             }
    2508              : 
    2509          772 :           if (start < 0)
    2510            3 :             return false;
    2511              : 
    2512          769 :           end = p->value.character.length;
    2513          769 :           if (p->ref && p->ref->u.ss.end)
    2514          744 :             gfc_extract_hwi (p->ref->u.ss.end, &end);
    2515              : 
    2516          769 :           if (end < start)
    2517            7 :             end = start;
    2518              : 
    2519          769 :           s = gfc_get_wide_string (end - start + 2);
    2520          769 :           memcpy (s, p->value.character.string + start,
    2521          769 :                   (end - start) * sizeof (gfc_char_t));
    2522          769 :           s[end - start + 1] = '\0';  /* TODO: C-style string.  */
    2523          769 :           free (p->value.character.string);
    2524          769 :           p->value.character.string = s;
    2525          769 :           p->value.character.length = end - start;
    2526          769 :           p->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
    2527         1538 :           p->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind,
    2528              :                                                  NULL,
    2529          769 :                                                  p->value.character.length);
    2530          769 :           gfc_free_ref_list (p->ref);
    2531          769 :           p->ref = NULL;
    2532          769 :           p->expr_type = EXPR_CONSTANT;
    2533              :         }
    2534              :       break;
    2535              : 
    2536     17291253 :     case EXPR_OP:
    2537     17291253 :       if (!simplify_intrinsic_op (p, type))
    2538              :         return false;
    2539              :       break;
    2540              : 
    2541           27 :     case EXPR_CONDITIONAL:
    2542           27 :       if (!simplify_conditional (p, type))
    2543              :         return false;
    2544              :       break;
    2545              : 
    2546     14991223 :     case EXPR_VARIABLE:
    2547              :       /* Only substitute array parameter variables if we are in an
    2548              :          initialization expression, or we want a subsection.  */
    2549     14991223 :       if (p->symtree->n.sym->attr.flavor == FL_PARAMETER
    2550        14573 :           && (gfc_init_expr_flag || p->ref
    2551            1 :               || (p->symtree->n.sym->value
    2552            0 :                   && p->symtree->n.sym->value->expr_type != EXPR_ARRAY)))
    2553              :         {
    2554        14572 :           if (!simplify_parameter_variable (p, type))
    2555              :             return false;
    2556        14019 :           if (!iter_stack)
    2557              :             break;
    2558              :         }
    2559              : 
    2560     14977545 :       if (type == 1)
    2561              :         {
    2562     13975828 :           gfc_simplify_iterator_var (p);
    2563              :         }
    2564              : 
    2565              :       /* Simplify subcomponent references.  */
    2566     14977545 :       if (!simplify_ref_chain (p->ref, type, &p))
    2567              :         return false;
    2568              : 
    2569              :       break;
    2570              : 
    2571       131606 :     case EXPR_STRUCTURE:
    2572       131606 :     case EXPR_ARRAY:
    2573       131606 :       if (!simplify_ref_chain (p->ref, type, &p))
    2574              :         return false;
    2575              : 
    2576              :       /* If the following conditions hold, we found something like kind type
    2577              :          inquiry of the form a(2)%kind while simplify the ref chain.  */
    2578       131605 :       if (p->expr_type == EXPR_CONSTANT && !p->ref && !p->rank && !p->shape)
    2579              :         return true;
    2580              : 
    2581       131422 :       if (!simplify_constructor (p->value.constructor, type))
    2582              :         return false;
    2583              : 
    2584       131422 :       if (p->expr_type == EXPR_ARRAY && p->ref && p->ref->type == REF_ARRAY
    2585        14126 :           && p->ref->u.ar.type == AR_FULL)
    2586         8082 :           gfc_expand_constructor (p, false);
    2587              : 
    2588       131422 :       if (!simplify_const_ref (p))
    2589              :         return false;
    2590              : 
    2591              :       break;
    2592              : 
    2593              :     case EXPR_COMPCALL:
    2594              :     case EXPR_PPC:
    2595              :       break;
    2596              : 
    2597            0 :     case EXPR_UNKNOWN:
    2598            0 :       gcc_unreachable ();
    2599              :     }
    2600              : 
    2601              :   return true;
    2602              : }
    2603              : 
    2604              : 
    2605              : /* Try simplification of an expression via gfc_simplify_expr.
    2606              :    When an error occurs (arithmetic or otherwise), roll back.  */
    2607              : 
    2608              : bool
    2609            0 : gfc_try_simplify_expr (gfc_expr *e, int type)
    2610              : {
    2611            0 :   gfc_expr *n;
    2612            0 :   bool t, saved_div0;
    2613              : 
    2614            0 :   if (e == NULL || e->expr_type == EXPR_CONSTANT)
    2615              :     return true;
    2616              : 
    2617            0 :   saved_div0 = gfc_seen_div0;
    2618            0 :   gfc_seen_div0 = false;
    2619            0 :   n = gfc_copy_expr (e);
    2620            0 :   t = gfc_simplify_expr (n, type) && !gfc_seen_div0;
    2621            0 :   if (t)
    2622            0 :     gfc_replace_expr (e, n);
    2623              :   else
    2624            0 :     gfc_free_expr (n);
    2625            0 :   gfc_seen_div0 = saved_div0;
    2626            0 :   return t;
    2627              : }
    2628              : 
    2629              : 
    2630              : /* Returns the type of an expression with the exception that iterator
    2631              :    variables are automatically integers no matter what else they may
    2632              :    be declared as.  */
    2633              : 
    2634              : static bt
    2635         4812 : et0 (gfc_expr *e)
    2636              : {
    2637         4812 :   if (e->expr_type == EXPR_VARIABLE && gfc_check_iter_variable (e))
    2638              :     return BT_INTEGER;
    2639              : 
    2640         4812 :   return e->ts.type;
    2641              : }
    2642              : 
    2643              : 
    2644              : /* Scalarize an expression for an elemental intrinsic call.  */
    2645              : 
    2646              : static bool
    2647       110252 : scalarize_intrinsic_call (gfc_expr *e, bool init_flag)
    2648              : {
    2649       110252 :   gfc_actual_arglist *a, *b;
    2650       110252 :   gfc_constructor_base ctor;
    2651       110252 :   gfc_constructor *args[5] = {};  /* Avoid uninitialized warnings.  */
    2652       110252 :   gfc_constructor *ci, *new_ctor;
    2653       110252 :   gfc_expr *expr, *old, *p;
    2654       110252 :   int n, i, rank[5], array_arg;
    2655              : 
    2656       110252 :   if (e == NULL)
    2657              :     return false;
    2658              : 
    2659       110252 :   a = e->value.function.actual;
    2660       118120 :   for (; a; a = a->next)
    2661       117384 :     if (a->expr && !gfc_is_constant_expr (a->expr))
    2662              :       return false;
    2663              : 
    2664              :   /* Find which, if any, arguments are arrays.  Assume that the old
    2665              :      expression carries the type information and that the first arg
    2666              :      that is an array expression carries all the shape information.*/
    2667          736 :   n = array_arg = 0;
    2668          736 :   a = e->value.function.actual;
    2669         1466 :   for (; a; a = a->next)
    2670              :     {
    2671         1158 :       n++;
    2672         1158 :       if (!a->expr || a->expr->expr_type != EXPR_ARRAY)
    2673          730 :         continue;
    2674          428 :       array_arg = n;
    2675          428 :       expr = gfc_copy_expr (a->expr);
    2676          428 :       break;
    2677              :     }
    2678              : 
    2679          736 :   if (!array_arg)
    2680              :     return false;
    2681              : 
    2682          428 :   old = gfc_copy_expr (e);
    2683              : 
    2684          428 :   gfc_constructor_free (expr->value.constructor);
    2685          428 :   expr->value.constructor = NULL;
    2686          428 :   expr->ts = old->ts;
    2687          428 :   expr->where = old->where;
    2688          428 :   expr->expr_type = EXPR_ARRAY;
    2689              : 
    2690              :   /* Copy the array argument constructors into an array, with nulls
    2691              :      for the scalars.  */
    2692          428 :   n = 0;
    2693          428 :   a = old->value.function.actual;
    2694         1342 :   for (; a; a = a->next)
    2695              :     {
    2696              :       /* Check that this is OK for an initialization expression.  */
    2697          914 :       if (a->expr && init_flag && !gfc_check_init_expr (a->expr))
    2698            0 :         goto cleanup;
    2699              : 
    2700          914 :       rank[n] = 0;
    2701          914 :       if (a->expr && a->expr->rank && a->expr->expr_type == EXPR_VARIABLE)
    2702              :         {
    2703            0 :           rank[n] = a->expr->rank;
    2704            0 :           ctor = a->expr->symtree->n.sym->value->value.constructor;
    2705            0 :           args[n] = gfc_constructor_first (ctor);
    2706              :         }
    2707          914 :       else if (a->expr && a->expr->expr_type == EXPR_ARRAY)
    2708              :         {
    2709          469 :           if (a->expr->rank)
    2710          469 :             rank[n] = a->expr->rank;
    2711              :           else
    2712            0 :             rank[n] = 1;
    2713          469 :           ctor = a->expr->value.constructor;
    2714          469 :           args[n] = gfc_constructor_first (ctor);
    2715              :         }
    2716              :       else
    2717          445 :         args[n] = NULL;
    2718              : 
    2719          914 :       n++;
    2720              :     }
    2721              : 
    2722              :   /* Using the array argument as the master, step through the array
    2723              :      calling the function for each element and advancing the array
    2724              :      constructors together.  */
    2725         3460 :   for (ci = args[array_arg - 1]; ci; ci = gfc_constructor_next (ci))
    2726              :     {
    2727         3032 :       new_ctor = gfc_constructor_append_expr (&expr->value.constructor,
    2728              :                                               gfc_copy_expr (old), NULL);
    2729              : 
    2730         3032 :       gfc_free_actual_arglist (new_ctor->expr->value.function.actual);
    2731         3032 :       a = NULL;
    2732         3032 :       b = old->value.function.actual;
    2733         9169 :       for (i = 0; i < n; i++)
    2734              :         {
    2735         6137 :           if (a == NULL)
    2736         6064 :             new_ctor->expr->value.function.actual
    2737         3032 :                         = a = gfc_get_actual_arglist ();
    2738              :           else
    2739              :             {
    2740         3105 :               a->next = gfc_get_actual_arglist ();
    2741         3105 :               a = a->next;
    2742              :             }
    2743              : 
    2744         6137 :           if (args[i])
    2745         4033 :             a->expr = gfc_copy_expr (args[i]->expr);
    2746              :           else
    2747         2104 :             a->expr = gfc_copy_expr (b->expr);
    2748              : 
    2749         6137 :           b = b->next;
    2750              :         }
    2751              : 
    2752              :       /* Simplify the function calls.  If the simplification fails, the
    2753              :          error will be flagged up down-stream or the library will deal
    2754              :          with it.  */
    2755         3032 :       p = gfc_copy_expr (new_ctor->expr);
    2756              : 
    2757         3032 :       if (!gfc_simplify_expr (p, init_flag))
    2758           13 :         gfc_free_expr (p);
    2759              :       else
    2760         3019 :         gfc_replace_expr (new_ctor->expr, p);
    2761              : 
    2762         9169 :       for (i = 0; i < n; i++)
    2763         6137 :         if (args[i])
    2764         4033 :           args[i] = gfc_constructor_next (args[i]);
    2765              : 
    2766         6137 :       for (i = 1; i < n; i++)
    2767         3105 :         if (rank[i] && ((args[i] != NULL && args[array_arg - 1] == NULL)
    2768         1133 :                         || (args[i] == NULL && args[array_arg - 1] != NULL)))
    2769            0 :           goto compliance;
    2770              :     }
    2771              : 
    2772          428 :   free_expr0 (e);
    2773          428 :   *e = *expr;
    2774              :   /* Free "expr" but not the pointers it contains.  */
    2775          428 :   free (expr);
    2776          428 :   gfc_free_expr (old);
    2777          428 :   return true;
    2778              : 
    2779            0 : compliance:
    2780            0 :   gfc_error_now ("elemental function arguments at %C are not compliant");
    2781              : 
    2782            0 : cleanup:
    2783            0 :   gfc_free_expr (expr);
    2784            0 :   gfc_free_expr (old);
    2785            0 :   return false;
    2786              : }
    2787              : 
    2788              : 
    2789              : static bool
    2790         4220 : check_intrinsic_op (gfc_expr *e, bool (*check_function) (gfc_expr *))
    2791              : {
    2792         4220 :   gfc_expr *op1 = e->value.op.op1;
    2793         4220 :   gfc_expr *op2 = e->value.op.op2;
    2794              : 
    2795         4220 :   if (!(*check_function)(op1))
    2796              :     return false;
    2797              : 
    2798         2954 :   switch (e->value.op.op)
    2799              :     {
    2800          523 :     case INTRINSIC_UPLUS:
    2801          523 :     case INTRINSIC_UMINUS:
    2802          523 :       if (!numeric_type (et0 (op1)))
    2803            0 :         goto not_numeric;
    2804              :       break;
    2805              : 
    2806          145 :     case INTRINSIC_EQ:
    2807          145 :     case INTRINSIC_EQ_OS:
    2808          145 :     case INTRINSIC_NE:
    2809          145 :     case INTRINSIC_NE_OS:
    2810          145 :     case INTRINSIC_GT:
    2811          145 :     case INTRINSIC_GT_OS:
    2812          145 :     case INTRINSIC_GE:
    2813          145 :     case INTRINSIC_GE_OS:
    2814          145 :     case INTRINSIC_LT:
    2815          145 :     case INTRINSIC_LT_OS:
    2816          145 :     case INTRINSIC_LE:
    2817          145 :     case INTRINSIC_LE_OS:
    2818          145 :       if (!(*check_function)(op2))
    2819              :         return false;
    2820              : 
    2821          217 :       if (!(et0 (op1) == BT_CHARACTER && et0 (op2) == BT_CHARACTER)
    2822          145 :           && !(numeric_type (et0 (op1)) && numeric_type (et0 (op2))))
    2823              :         {
    2824            0 :           gfc_error ("Numeric or CHARACTER operands are required in "
    2825              :                      "expression at %L", &e->where);
    2826            0 :          return false;
    2827              :         }
    2828              :       break;
    2829              : 
    2830         2237 :     case INTRINSIC_PLUS:
    2831         2237 :     case INTRINSIC_MINUS:
    2832         2237 :     case INTRINSIC_TIMES:
    2833         2237 :     case INTRINSIC_DIVIDE:
    2834         2237 :     case INTRINSIC_POWER:
    2835         2237 :       if (!(*check_function)(op2))
    2836              :         return false;
    2837              : 
    2838         1963 :       if (!numeric_type (et0 (op1)) || !numeric_type (et0 (op2)))
    2839            0 :         goto not_numeric;
    2840              : 
    2841              :       break;
    2842              : 
    2843            1 :     case INTRINSIC_CONCAT:
    2844            1 :       if (!(*check_function)(op2))
    2845              :         return false;
    2846              : 
    2847            0 :       if (et0 (op1) != BT_CHARACTER || et0 (op2) != BT_CHARACTER)
    2848              :         {
    2849            0 :           gfc_error ("Concatenation operator in expression at %L "
    2850              :                      "must have two CHARACTER operands", &op1->where);
    2851            0 :           return false;
    2852              :         }
    2853              : 
    2854            0 :       if (op1->ts.kind != op2->ts.kind)
    2855              :         {
    2856            0 :           gfc_error ("Concat operator at %L must concatenate strings of the "
    2857              :                      "same kind", &e->where);
    2858            0 :           return false;
    2859              :         }
    2860              : 
    2861              :       break;
    2862              : 
    2863            0 :     case INTRINSIC_NOT:
    2864            0 :       if (et0 (op1) != BT_LOGICAL)
    2865              :         {
    2866            0 :           gfc_error (".NOT. operator in expression at %L must have a LOGICAL "
    2867              :                      "operand", &op1->where);
    2868            0 :           return false;
    2869              :         }
    2870              : 
    2871              :       break;
    2872              : 
    2873            0 :     case INTRINSIC_AND:
    2874            0 :     case INTRINSIC_OR:
    2875            0 :     case INTRINSIC_EQV:
    2876            0 :     case INTRINSIC_NEQV:
    2877            0 :       if (!(*check_function)(op2))
    2878              :         return false;
    2879              : 
    2880            0 :       if (et0 (op1) != BT_LOGICAL || et0 (op2) != BT_LOGICAL)
    2881              :         {
    2882            0 :           gfc_error ("LOGICAL operands are required in expression at %L",
    2883              :                      &e->where);
    2884            0 :           return false;
    2885              :         }
    2886              : 
    2887              :       break;
    2888              : 
    2889              :     case INTRINSIC_PARENTHESES:
    2890              :       break;
    2891              : 
    2892            0 :     default:
    2893            0 :       gfc_error ("Only intrinsic operators can be used in expression at %L",
    2894              :                  &e->where);
    2895            0 :       return false;
    2896              :     }
    2897              : 
    2898              :   return true;
    2899              : 
    2900            0 : not_numeric:
    2901            0 :   gfc_error ("Numeric operands are required in expression at %L", &e->where);
    2902              : 
    2903            0 :   return false;
    2904              : }
    2905              : 
    2906              : /* F2003, 7.1.7 (3): In init expression, allocatable components
    2907              :    must not be data-initialized.  */
    2908              : static bool
    2909         2002 : check_alloc_comp_init (gfc_expr *e)
    2910              : {
    2911         2002 :   gfc_component *comp;
    2912         2002 :   gfc_constructor *ctor;
    2913              : 
    2914         2002 :   gcc_assert (e->expr_type == EXPR_STRUCTURE);
    2915         2002 :   gcc_assert (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS);
    2916              : 
    2917         2002 :   for (comp = e->ts.u.derived->components,
    2918         2002 :        ctor = gfc_constructor_first (e->value.constructor);
    2919         4603 :        comp; comp = comp->next, ctor = gfc_constructor_next (ctor))
    2920              :     {
    2921         2602 :       if (comp->attr.allocatable && ctor->expr
    2922           31 :           && ctor->expr->expr_type != EXPR_NULL)
    2923              :         {
    2924            1 :           gfc_error ("Invalid initialization expression for ALLOCATABLE "
    2925              :                      "component %qs in structure constructor at %L",
    2926              :                      comp->name, &ctor->expr->where);
    2927            1 :           return false;
    2928              :         }
    2929              :     }
    2930              : 
    2931              :   return true;
    2932              : }
    2933              : 
    2934              : static match
    2935          586 : check_init_expr_arguments (gfc_expr *e)
    2936              : {
    2937          586 :   gfc_actual_arglist *ap;
    2938              : 
    2939         1528 :   for (ap = e->value.function.actual; ap; ap = ap->next)
    2940         1255 :     if (!gfc_check_init_expr (ap->expr))
    2941              :       return MATCH_ERROR;
    2942              : 
    2943              :   return MATCH_YES;
    2944              : }
    2945              : 
    2946              : static bool check_restricted (gfc_expr *);
    2947              : 
    2948              : /* F95, 7.1.6.1, Initialization expressions, (7)
    2949              :    F2003, 7.1.7 Initialization expression, (8)
    2950              :    F2008, 7.1.12 Constant expression, (4)  */
    2951              : 
    2952              : static match
    2953         4156 : check_inquiry (gfc_expr *e, int not_restricted)
    2954              : {
    2955         4156 :   const char *name;
    2956         4156 :   const char *const *functions;
    2957              : 
    2958         4156 :   static const char *const inquiry_func_f95[] = {
    2959              :     "lbound", "shape", "size", "ubound",
    2960              :     "bit_size", "len", "kind",
    2961              :     "digits", "epsilon", "huge", "maxexponent", "minexponent",
    2962              :     "precision", "radix", "range", "tiny",
    2963              :     NULL
    2964              :   };
    2965              : 
    2966         4156 :   static const char *const inquiry_func_f2003[] = {
    2967              :     "lbound", "shape", "size", "ubound",
    2968              :     "bit_size", "len", "kind",
    2969              :     "digits", "epsilon", "huge", "maxexponent", "minexponent",
    2970              :     "precision", "radix", "range", "tiny",
    2971              :     "new_line", NULL
    2972              :   };
    2973              : 
    2974              :   /* std=f2008+ or -std=gnu */
    2975         4156 :   static const char *const inquiry_func_gnu[] = {
    2976              :     "lbound", "shape", "size", "ubound",
    2977              :     "bit_size", "len", "kind",
    2978              :     "digits", "epsilon", "huge", "maxexponent", "minexponent",
    2979              :     "precision", "radix", "range", "tiny",
    2980              :     "new_line", "storage_size", NULL
    2981              :   };
    2982              : 
    2983         4156 :   int i = 0;
    2984         4156 :   gfc_actual_arglist *ap;
    2985         4156 :   gfc_symbol *sym;
    2986         4156 :   gfc_symbol *asym;
    2987              : 
    2988         4156 :   if (!e->value.function.isym
    2989         4050 :       || !e->value.function.isym->inquiry)
    2990              :     return MATCH_NO;
    2991              : 
    2992              :   /* An undeclared parameter will get us here (PR25018).  */
    2993         2816 :   if (e->symtree == NULL)
    2994              :     return MATCH_NO;
    2995              : 
    2996         2814 :   sym = e->symtree->n.sym;
    2997              : 
    2998         2814 :   if (sym->from_intmod)
    2999              :     {
    3000            2 :       if (sym->from_intmod == INTMOD_ISO_FORTRAN_ENV
    3001            0 :           && sym->intmod_sym_id != ISOFORTRAN_COMPILER_OPTIONS
    3002            0 :           && sym->intmod_sym_id != ISOFORTRAN_COMPILER_VERSION)
    3003              :         return MATCH_NO;
    3004              : 
    3005            2 :       if (sym->from_intmod == INTMOD_ISO_C_BINDING
    3006            2 :           && sym->intmod_sym_id != ISOCBINDING_C_SIZEOF)
    3007              :         return MATCH_NO;
    3008              :     }
    3009              :   else
    3010              :     {
    3011         2812 :       name = sym->name;
    3012              : 
    3013         2812 :       functions = inquiry_func_gnu;
    3014         2812 :       if (gfc_option.warn_std & GFC_STD_F2003)
    3015            0 :         functions = inquiry_func_f2003;
    3016         2812 :       if (gfc_option.warn_std & GFC_STD_F95)
    3017            0 :         functions = inquiry_func_f95;
    3018              : 
    3019        11781 :       for (i = 0; functions[i]; i++)
    3020        11775 :         if (strcmp (functions[i], name) == 0)
    3021              :           break;
    3022              : 
    3023         2812 :       if (functions[i] == NULL)
    3024              :         return MATCH_ERROR;
    3025              :     }
    3026              : 
    3027              :   /* At this point we have an inquiry function with a variable argument.  The
    3028              :      type of the variable might be undefined, but we need it now, because the
    3029              :      arguments of these functions are not allowed to be undefined.  */
    3030              : 
    3031         9021 :   for (ap = e->value.function.actual; ap; ap = ap->next)
    3032              :     {
    3033         6714 :       if (!ap->expr)
    3034         3285 :         continue;
    3035              : 
    3036         3429 :       asym = ap->expr->symtree ? ap->expr->symtree->n.sym : NULL;
    3037              : 
    3038         3429 :       if (ap->expr->ts.type == BT_UNKNOWN)
    3039              :         {
    3040            0 :           if (asym && asym->ts.type == BT_UNKNOWN
    3041            0 :               && !gfc_set_default_type (asym, 0, gfc_current_ns))
    3042              :             return MATCH_NO;
    3043              : 
    3044            0 :           ap->expr->ts = asym->ts;
    3045              :         }
    3046              : 
    3047         3429 :       if (asym && asym->assoc && asym->assoc->target
    3048           12 :           && asym->assoc->target->expr_type == EXPR_CONSTANT)
    3049              :         {
    3050           12 :           gfc_free_expr (ap->expr);
    3051           12 :           ap->expr = gfc_copy_expr (asym->assoc->target);
    3052              :         }
    3053              : 
    3054              :       /* Assumed character length will not reduce to a constant expression
    3055              :          with LEN, as required by the standard.  */
    3056         3429 :       if (i == 5 && not_restricted && asym
    3057          403 :           && asym->ts.type == BT_CHARACTER
    3058          403 :           && ((asym->ts.u.cl && asym->ts.u.cl->length == NULL)
    3059           49 :               || asym->ts.deferred))
    3060              :         {
    3061          354 :           gfc_error ("Assumed or deferred character length variable %qs "
    3062              :                      "in constant expression at %L",
    3063          354 :                       asym->name, &ap->expr->where);
    3064          354 :           return MATCH_ERROR;
    3065              :         }
    3066         3075 :       else if (not_restricted && !gfc_check_init_expr (ap->expr))
    3067              :         return MATCH_ERROR;
    3068              : 
    3069         2933 :       if (not_restricted == 0
    3070         2913 :           && ap->expr->expr_type != EXPR_VARIABLE
    3071         3612 :           && !check_restricted (ap->expr))
    3072              :         return MATCH_ERROR;
    3073              : 
    3074         2931 :       if (not_restricted == 0
    3075         2911 :           && ap->expr->expr_type == EXPR_VARIABLE
    3076         2234 :           && asym->attr.dummy && asym->attr.optional)
    3077              :         return MATCH_NO;
    3078              :     }
    3079              : 
    3080              :   return MATCH_YES;
    3081              : }
    3082              : 
    3083              : 
    3084              : /* F95, 7.1.6.1, Initialization expressions, (5)
    3085              :    F2003, 7.1.7 Initialization expression, (5)  */
    3086              : 
    3087              : static match
    3088          587 : check_transformational (gfc_expr *e)
    3089              : {
    3090          587 :   static const char * const trans_func_f95[] = {
    3091              :     "repeat", "reshape", "selected_int_kind",
    3092              :     "selected_real_kind", "transfer", "trim", NULL
    3093              :   };
    3094              : 
    3095          587 :   static const char * const trans_func_f2003[] =  {
    3096              :     "all", "any", "count", "dot_product", "matmul", "null", "pack",
    3097              :     "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
    3098              :     "selected_real_kind", "spread", "sum", "transfer", "transpose",
    3099              :     "trim", "unpack", NULL
    3100              :   };
    3101              : 
    3102          587 :   static const char * const trans_func_f2008[] =  {
    3103              :     "all", "any", "count", "dot_product", "matmul", "null", "pack",
    3104              :     "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
    3105              :     "selected_real_kind", "spread", "sum", "transfer", "transpose",
    3106              :     "trim", "unpack", "findloc", NULL
    3107              :   };
    3108              : 
    3109          587 :   static const char * const trans_func_f2023[] =  {
    3110              :     "all", "any", "count", "dot_product", "matmul", "null", "pack",
    3111              :     "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
    3112              :     "selected_logical_kind", "selected_real_kind", "spread", "sum", "transfer",
    3113              :     "transpose", "trim", "unpack", "findloc", NULL
    3114              :   };
    3115              : 
    3116          587 :   int i;
    3117          587 :   const char *name;
    3118          587 :   const char *const *functions;
    3119              : 
    3120          587 :   if (!e->value.function.isym
    3121          587 :       || !e->value.function.isym->transformational)
    3122              :     return MATCH_NO;
    3123              : 
    3124          102 :   name = e->symtree->n.sym->name;
    3125              : 
    3126          102 :   if (gfc_option.allow_std & GFC_STD_F2023)
    3127              :     functions = trans_func_f2023;
    3128            0 :   else if (gfc_option.allow_std & GFC_STD_F2008)
    3129              :     functions = trans_func_f2008;
    3130            0 :   else if (gfc_option.allow_std & GFC_STD_F2003)
    3131              :     functions = trans_func_f2003;
    3132              :   else
    3133            0 :     functions = trans_func_f95;
    3134              : 
    3135              :   /* NULL() is dealt with below.  */
    3136          102 :   if (strcmp ("null", name) == 0)
    3137              :     return MATCH_NO;
    3138              : 
    3139         1621 :   for (i = 0; functions[i]; i++)
    3140         1620 :     if (strcmp (functions[i], name) == 0)
    3141              :        break;
    3142              : 
    3143          102 :   if (functions[i] == NULL)
    3144              :     {
    3145            1 :       gfc_error ("transformational intrinsic %qs at %L is not permitted "
    3146              :                  "in an initialization expression", name, &e->where);
    3147            1 :       return MATCH_ERROR;
    3148              :     }
    3149              : 
    3150          101 :   return check_init_expr_arguments (e);
    3151              : }
    3152              : 
    3153              : 
    3154              : /* F95, 7.1.6.1, Initialization expressions, (6)
    3155              :    F2003, 7.1.7 Initialization expression, (6)  */
    3156              : 
    3157              : static match
    3158          587 : check_null (gfc_expr *e)
    3159              : {
    3160          587 :   if (strcmp ("null", e->symtree->n.sym->name) != 0)
    3161              :     return MATCH_NO;
    3162              : 
    3163            0 :   return check_init_expr_arguments (e);
    3164              : }
    3165              : 
    3166              : 
    3167              : static match
    3168          485 : check_elemental (gfc_expr *e)
    3169              : {
    3170          485 :   if (!e->value.function.isym
    3171          485 :       || !e->value.function.isym->elemental)
    3172              :     return MATCH_NO;
    3173              : 
    3174          482 :   if (e->ts.type != BT_INTEGER
    3175            2 :       && e->ts.type != BT_CHARACTER
    3176          484 :       && !gfc_notify_std (GFC_STD_F2003, "Evaluation of nonstandard "
    3177              :                           "initialization expression at %L", &e->where))
    3178              :     return MATCH_ERROR;
    3179              : 
    3180          482 :   return check_init_expr_arguments (e);
    3181              : }
    3182              : 
    3183              : 
    3184              : static match
    3185         1104 : check_conversion (gfc_expr *e)
    3186              : {
    3187         1104 :   if (!e->value.function.isym
    3188         1104 :       || !e->value.function.isym->conversion)
    3189              :     return MATCH_NO;
    3190              : 
    3191            3 :   return check_init_expr_arguments (e);
    3192              : }
    3193              : 
    3194              : 
    3195              : /* Verify that an expression is an initialization expression.  A side
    3196              :    effect is that the expression tree is reduced to a single constant
    3197              :    node if all goes well.  This would normally happen when the
    3198              :    expression is constructed but function references are assumed to be
    3199              :    intrinsics in the context of initialization expressions.  If
    3200              :    false is returned an error message has been generated.  */
    3201              : 
    3202              : bool
    3203       661434 : gfc_check_init_expr (gfc_expr *e)
    3204              : {
    3205       661434 :   match m;
    3206       661434 :   bool t;
    3207              : 
    3208       661434 :   if (e == NULL)
    3209              :     return true;
    3210              : 
    3211       661393 :   switch (e->expr_type)
    3212              :     {
    3213         1553 :     case EXPR_OP:
    3214         1553 :       t = check_intrinsic_op (e, gfc_check_init_expr);
    3215         1553 :       if (t)
    3216           14 :         t = gfc_simplify_expr (e, 0);
    3217              : 
    3218              :       break;
    3219              : 
    3220            1 :     case EXPR_CONDITIONAL:
    3221            1 :       t = gfc_check_init_expr (e->value.conditional.condition);
    3222            1 :       if (!t)
    3223              :         break;
    3224            0 :       t = gfc_check_init_expr (e->value.conditional.true_expr);
    3225            0 :       if (!t)
    3226              :         break;
    3227            0 :       t = gfc_check_init_expr (e->value.conditional.false_expr);
    3228            0 :       if (t)
    3229            0 :         t = gfc_simplify_expr (e, 0);
    3230              :       else
    3231              :         t = false;
    3232              :       break;
    3233              : 
    3234         1662 :     case EXPR_FUNCTION:
    3235         1662 :       t = false;
    3236              : 
    3237         1662 :       {
    3238         1662 :         bool conversion;
    3239         1662 :         gfc_intrinsic_sym* isym = NULL;
    3240         1662 :         gfc_symbol* sym = e->symtree->n.sym;
    3241              : 
    3242              :         /* Simplify here the intrinsics from the IEEE_ARITHMETIC and
    3243              :            IEEE_EXCEPTIONS modules.  */
    3244         1662 :         int mod = sym->from_intmod;
    3245         1662 :         if (mod == INTMOD_NONE && sym->generic)
    3246          192 :           mod = sym->generic->sym->from_intmod;
    3247         1662 :         if (mod == INTMOD_IEEE_ARITHMETIC || mod == INTMOD_IEEE_EXCEPTIONS)
    3248              :           {
    3249          453 :             gfc_expr *new_expr = gfc_simplify_ieee_functions (e);
    3250          453 :             if (new_expr)
    3251              :               {
    3252          327 :                 gfc_replace_expr (e, new_expr);
    3253          327 :                 t = true;
    3254          327 :                 break;
    3255              :               }
    3256              :           }
    3257              : 
    3258              :         /* If a conversion function, e.g., __convert_i8_i4, was inserted
    3259              :            into an array constructor, we need to skip the error check here.
    3260              :            Conversion errors are  caught below in scalarize_intrinsic_call.  */
    3261         3771 :         conversion = e->value.function.isym
    3262         1335 :                    && (e->value.function.isym->conversion == 1);
    3263              : 
    3264         1332 :         if (!conversion && (!gfc_is_intrinsic (sym, 0, e->where)
    3265         1117 :             || (m = gfc_intrinsic_func_interface (e, 0)) == MATCH_NO))
    3266              :           {
    3267          231 :             gfc_error ("Function %qs in initialization expression at %L "
    3268              :                        "must be an intrinsic function",
    3269          231 :                        e->symtree->n.sym->name, &e->where);
    3270          231 :             break;
    3271              :           }
    3272              : 
    3273         1104 :         if ((m = check_conversion (e)) == MATCH_NO
    3274         1101 :             && (m = check_inquiry (e, 1)) == MATCH_NO
    3275          587 :             && (m = check_null (e)) == MATCH_NO
    3276          587 :             && (m = check_transformational (e)) == MATCH_NO
    3277         1589 :             && (m = check_elemental (e)) == MATCH_NO)
    3278              :           {
    3279            3 :             gfc_error ("Intrinsic function %qs at %L is not permitted "
    3280              :                        "in an initialization expression",
    3281            3 :                        e->symtree->n.sym->name, &e->where);
    3282            3 :             m = MATCH_ERROR;
    3283              :           }
    3284              : 
    3285         1104 :         if (m == MATCH_ERROR)
    3286          815 :           return false;
    3287              : 
    3288              :         /* Try to scalarize an elemental intrinsic function that has an
    3289              :            array argument.  */
    3290          289 :         isym = gfc_find_function (e->symtree->n.sym->name);
    3291          289 :         if (isym && isym->elemental
    3292          529 :             && (t = scalarize_intrinsic_call (e, true)))
    3293              :           break;
    3294              :       }
    3295              : 
    3296          289 :       if (m == MATCH_YES)
    3297          289 :         t = gfc_simplify_expr (e, 0);
    3298              : 
    3299              :       break;
    3300              : 
    3301         5248 :     case EXPR_VARIABLE:
    3302         5248 :       t = true;
    3303              : 
    3304              :       /* This occurs when parsing pdt templates.  */
    3305         5248 :       if (gfc_expr_attr (e).pdt_kind)
    3306              :         break;
    3307              : 
    3308         5236 :       if (gfc_check_iter_variable (e))
    3309              :         break;
    3310              : 
    3311         5220 :       if (e->symtree->n.sym->attr.flavor == FL_PARAMETER)
    3312              :         {
    3313              :           /* A PARAMETER shall not be used to define itself, i.e.
    3314              :                 REAL, PARAMETER :: x = transfer(0, x)
    3315              :              is invalid.  */
    3316          388 :           if (!e->symtree->n.sym->value)
    3317              :             {
    3318            9 :               gfc_error ("PARAMETER %qs is used at %L before its definition "
    3319              :                          "is complete", e->symtree->n.sym->name, &e->where);
    3320            9 :               t = false;
    3321              :             }
    3322              :           else
    3323          379 :             t = simplify_parameter_variable (e, 0);
    3324              : 
    3325              :           break;
    3326              :         }
    3327              : 
    3328         4832 :       if (gfc_in_match_data ())
    3329              :         break;
    3330              : 
    3331         4815 :       t = false;
    3332              : 
    3333         4815 :       if (e->symtree->n.sym->as)
    3334              :         {
    3335          155 :           switch (e->symtree->n.sym->as->type)
    3336              :             {
    3337            1 :               case AS_ASSUMED_SIZE:
    3338            1 :                 gfc_error ("Assumed size array %qs at %L is not permitted "
    3339              :                            "in an initialization expression",
    3340              :                            e->symtree->n.sym->name, &e->where);
    3341            1 :                 break;
    3342              : 
    3343           18 :               case AS_ASSUMED_SHAPE:
    3344           18 :                 gfc_error ("Assumed shape array %qs at %L is not permitted "
    3345              :                            "in an initialization expression",
    3346              :                            e->symtree->n.sym->name, &e->where);
    3347           18 :                 break;
    3348              : 
    3349          110 :               case AS_DEFERRED:
    3350          110 :                 if (!e->symtree->n.sym->attr.allocatable
    3351           89 :                     && !e->symtree->n.sym->attr.pointer
    3352           65 :                     && e->symtree->n.sym->attr.dummy)
    3353           65 :                   gfc_error ("Assumed-shape array %qs at %L is not permitted "
    3354              :                              "in an initialization expression",
    3355              :                              e->symtree->n.sym->name, &e->where);
    3356              :                 else
    3357           45 :                   gfc_error ("Deferred array %qs at %L is not permitted "
    3358              :                              "in an initialization expression",
    3359              :                              e->symtree->n.sym->name, &e->where);
    3360              :                 break;
    3361              : 
    3362           20 :               case AS_EXPLICIT:
    3363           20 :                 gfc_error ("Array %qs at %L is a variable, which does "
    3364              :                            "not reduce to a constant expression",
    3365              :                            e->symtree->n.sym->name, &e->where);
    3366           20 :                 break;
    3367              : 
    3368            6 :               case AS_ASSUMED_RANK:
    3369            6 :                 gfc_error ("Assumed-rank array %qs at %L is not permitted "
    3370              :                            "in an initialization expression",
    3371              :                            e->symtree->n.sym->name, &e->where);
    3372            6 :                 break;
    3373              : 
    3374            0 :               default:
    3375            0 :                 gcc_unreachable();
    3376              :           }
    3377              :         }
    3378              :       else
    3379         4660 :         gfc_error ("Parameter %qs at %L has not been declared or is "
    3380              :                    "a variable, which does not reduce to a constant "
    3381              :                    "expression", e->symtree->name, &e->where);
    3382              : 
    3383              :       break;
    3384              : 
    3385              :     case EXPR_CONSTANT:
    3386              :     case EXPR_NULL:
    3387              :       t = true;
    3388              :       break;
    3389              : 
    3390           11 :     case EXPR_SUBSTRING:
    3391           11 :       if (e->ref)
    3392              :         {
    3393            7 :           t = gfc_check_init_expr (e->ref->u.ss.start);
    3394            7 :           if (!t)
    3395              :             break;
    3396              : 
    3397            7 :           t = gfc_check_init_expr (e->ref->u.ss.end);
    3398            7 :           if (t)
    3399            7 :             t = gfc_simplify_expr (e, 0);
    3400              :         }
    3401              :       else
    3402              :         t = false;
    3403              :       break;
    3404              : 
    3405         2116 :     case EXPR_STRUCTURE:
    3406         2116 :       t = e->ts.is_iso_c ? true : false;
    3407         2116 :       if (t)
    3408              :         break;
    3409              : 
    3410         2002 :       t = check_alloc_comp_init (e);
    3411         2002 :       if (!t)
    3412              :         break;
    3413              : 
    3414         2001 :       t = gfc_check_constructor (e, gfc_check_init_expr);
    3415         2001 :       if (!t)
    3416              :         break;
    3417              : 
    3418         2001 :       break;
    3419              : 
    3420         4902 :     case EXPR_ARRAY:
    3421         4902 :       t = gfc_check_constructor (e, gfc_check_init_expr);
    3422         4902 :       if (!t)
    3423              :         break;
    3424              : 
    3425         4883 :       t = gfc_expand_constructor (e, true);
    3426         4883 :       if (!t)
    3427              :         break;
    3428              : 
    3429         4862 :       t = gfc_check_constructor_type (e);
    3430         4862 :       break;
    3431              : 
    3432            0 :     default:
    3433            0 :       gfc_internal_error ("check_init_expr(): Unknown expression type");
    3434              :     }
    3435              : 
    3436              :   return t;
    3437              : }
    3438              : 
    3439              : /* Reduces a general expression to an initialization expression (a constant).
    3440              :    This used to be part of gfc_match_init_expr.
    3441              :    Note that this function doesn't free the given expression on false.  */
    3442              : 
    3443              : bool
    3444       301410 : gfc_reduce_init_expr (gfc_expr *expr)
    3445              : {
    3446       301410 :   bool t;
    3447              : 
    3448              :   /* It is far too early to resolve a class compcall. Punt to resolution.  */
    3449       301410 :   if (expr && expr->expr_type == EXPR_COMPCALL
    3450           25 :       && expr->symtree->n.sym->ts.type == BT_CLASS)
    3451              :     return false;
    3452              : 
    3453       301385 :   gfc_init_expr_flag = true;
    3454       301385 :   t = gfc_resolve_expr (expr);
    3455       301385 :   if (t)
    3456       301244 :     t = gfc_check_init_expr (expr);
    3457       301385 :   gfc_init_expr_flag = false;
    3458              : 
    3459       301385 :   if (!t || !expr)
    3460              :     return false;
    3461              : 
    3462       295785 :   if (expr->expr_type == EXPR_ARRAY)
    3463              :     {
    3464         5108 :       if (!gfc_check_constructor_type (expr))
    3465              :         return false;
    3466         5108 :       if (!gfc_expand_constructor (expr, true))
    3467              :         return false;
    3468              :     }
    3469              : 
    3470              :   return true;
    3471              : }
    3472              : 
    3473              : 
    3474              : /* Match an initialization expression.  We work by first matching an
    3475              :    expression, then reducing it to a constant.  */
    3476              : 
    3477              : match
    3478        91303 : gfc_match_init_expr (gfc_expr **result)
    3479              : {
    3480        91303 :   gfc_expr *expr;
    3481        91303 :   match m;
    3482        91303 :   bool t;
    3483              : 
    3484        91303 :   expr = NULL;
    3485              : 
    3486        91303 :   gfc_init_expr_flag = true;
    3487              : 
    3488        91303 :   m = gfc_match_expr (&expr);
    3489        91303 :   if (m != MATCH_YES)
    3490              :     {
    3491          115 :       gfc_init_expr_flag = false;
    3492          115 :       return m;
    3493              :     }
    3494              : 
    3495        91188 :   if (expr->expr_type != EXPR_FUNCTION && gfc_derived_parameter_expr (expr))
    3496              :     {
    3497          174 :       *result = expr;
    3498          174 :       gfc_init_expr_flag = false;
    3499          174 :       return m;
    3500              :     }
    3501              : 
    3502        91014 :   t = gfc_reduce_init_expr (expr);
    3503        91014 :   if (!t)
    3504              :     {
    3505          494 :       gfc_free_expr (expr);
    3506          494 :       gfc_init_expr_flag = false;
    3507          494 :       return MATCH_ERROR;
    3508              :     }
    3509              : 
    3510        90520 :   *result = expr;
    3511        90520 :   gfc_init_expr_flag = false;
    3512              : 
    3513        90520 :   return MATCH_YES;
    3514              : }
    3515              : 
    3516              : 
    3517              : /* Given an actual argument list, test to see that each argument is a
    3518              :    restricted expression and optionally if the expression type is
    3519              :    integer or character.  */
    3520              : 
    3521              : static bool
    3522         1341 : restricted_args (gfc_actual_arglist *a)
    3523              : {
    3524         3417 :   for (; a; a = a->next)
    3525              :     {
    3526         2077 :       if (!check_restricted (a->expr))
    3527              :         return false;
    3528              :     }
    3529              : 
    3530              :   return true;
    3531              : }
    3532              : 
    3533              : 
    3534              : /************* Restricted/specification expressions *************/
    3535              : 
    3536              : 
    3537              : /* Make sure a non-intrinsic function is a specification function,
    3538              :  * see F08:7.1.11.5.  */
    3539              : 
    3540              : static bool
    3541          579 : external_spec_function (gfc_expr *e)
    3542              : {
    3543          579 :   gfc_symbol *f;
    3544              : 
    3545          579 :   f = e->value.function.esym;
    3546              : 
    3547              :   /* IEEE functions allowed are "a reference to a transformational function
    3548              :      from the intrinsic module IEEE_ARITHMETIC or IEEE_EXCEPTIONS", and
    3549              :      "inquiry function from the intrinsic modules IEEE_ARITHMETIC and
    3550              :      IEEE_EXCEPTIONS".  */
    3551          579 :   if (f->from_intmod == INTMOD_IEEE_ARITHMETIC
    3552          579 :       || f->from_intmod == INTMOD_IEEE_EXCEPTIONS)
    3553              :     {
    3554          234 :       if (!strcmp (f->name, "ieee_selected_real_kind")
    3555          216 :           || !strcmp (f->name, "ieee_support_rounding")
    3556          216 :           || !strcmp (f->name, "ieee_support_flag")
    3557          216 :           || !strcmp (f->name, "ieee_support_halting")
    3558          216 :           || !strcmp (f->name, "ieee_support_datatype")
    3559          216 :           || !strcmp (f->name, "ieee_support_denormal")
    3560          216 :           || !strcmp (f->name, "ieee_support_subnormal")
    3561          216 :           || !strcmp (f->name, "ieee_support_divide")
    3562          216 :           || !strcmp (f->name, "ieee_support_inf")
    3563          216 :           || !strcmp (f->name, "ieee_support_io")
    3564          216 :           || !strcmp (f->name, "ieee_support_nan")
    3565          216 :           || !strcmp (f->name, "ieee_support_sqrt")
    3566          216 :           || !strcmp (f->name, "ieee_support_standard")
    3567          216 :           || !strcmp (f->name, "ieee_support_underflow_control"))
    3568           18 :         goto function_allowed;
    3569              :     }
    3570              : 
    3571          561 :   if (f->attr.proc == PROC_ST_FUNCTION)
    3572              :     {
    3573            0 :       gfc_error ("Specification function %qs at %L cannot be a statement "
    3574              :                  "function", f->name, &e->where);
    3575            0 :       return false;
    3576              :     }
    3577              : 
    3578          561 :   if (f->attr.proc == PROC_INTERNAL)
    3579              :     {
    3580            0 :       gfc_error ("Specification function %qs at %L cannot be an internal "
    3581              :                  "function", f->name, &e->where);
    3582            0 :       return false;
    3583              :     }
    3584              : 
    3585          561 :   if (!f->attr.pure && !f->attr.elemental)
    3586              :     {
    3587            2 :       gfc_error ("Specification function %qs at %L must be PURE", f->name,
    3588              :                  &e->where);
    3589            2 :       return false;
    3590              :     }
    3591              : 
    3592              :   /* F08:7.1.11.6. */
    3593          559 :   if (f->attr.recursive
    3594          559 :       && !gfc_notify_std (GFC_STD_F2003,
    3595              :                           "Specification function %qs "
    3596              :                           "at %L cannot be RECURSIVE",  f->name, &e->where))
    3597              :       return false;
    3598              : 
    3599          577 : function_allowed:
    3600          577 :   return restricted_args (e->value.function.actual);
    3601              : }
    3602              : 
    3603              : 
    3604              : /* Check to see that a function reference to an intrinsic is a
    3605              :    restricted expression.  */
    3606              : 
    3607              : static bool
    3608         3055 : restricted_intrinsic (gfc_expr *e)
    3609              : {
    3610              :   /* TODO: Check constraints on inquiry functions.  7.1.6.2 (7).  */
    3611         3055 :   if (check_inquiry (e, 0) == MATCH_YES)
    3612              :     return true;
    3613              : 
    3614          764 :   return restricted_args (e->value.function.actual);
    3615              : }
    3616              : 
    3617              : 
    3618              : /* Check the expressions of an actual arglist.  Used by check_restricted.  */
    3619              : 
    3620              : static bool
    3621         1342 : check_arglist (gfc_actual_arglist* arg, bool (*checker) (gfc_expr*))
    3622              : {
    3623         3401 :   for (; arg; arg = arg->next)
    3624         2067 :     if (!checker (arg->expr))
    3625              :       return false;
    3626              : 
    3627              :   return true;
    3628              : }
    3629              : 
    3630              : 
    3631              : /* Check the subscription expressions of a reference chain with a checking
    3632              :    function; used by check_restricted.  */
    3633              : 
    3634              : static bool
    3635        15202 : check_references (gfc_ref* ref, bool (*checker) (gfc_expr*))
    3636              : {
    3637        16064 :   int dim;
    3638              : 
    3639        16064 :   if (!ref)
    3640              :     return true;
    3641              : 
    3642          865 :   switch (ref->type)
    3643              :     {
    3644              :     case REF_ARRAY:
    3645         1384 :       for (dim = 0; dim < ref->u.ar.dimen; ++dim)
    3646              :         {
    3647          699 :           if (!checker (ref->u.ar.start[dim]))
    3648              :             return false;
    3649          697 :           if (!checker (ref->u.ar.end[dim]))
    3650              :             return false;
    3651          697 :           if (!checker (ref->u.ar.stride[dim]))
    3652              :             return false;
    3653              :         }
    3654              :       break;
    3655              : 
    3656              :     case REF_COMPONENT:
    3657              :       /* Nothing needed, just proceed to next reference.  */
    3658              :       break;
    3659              : 
    3660           13 :     case REF_SUBSTRING:
    3661           13 :       if (!checker (ref->u.ss.start))
    3662              :         return false;
    3663           12 :       if (!checker (ref->u.ss.end))
    3664              :         return false;
    3665              :       break;
    3666              : 
    3667            0 :     default:
    3668            0 :       gcc_unreachable ();
    3669          862 :       break;
    3670              :     }
    3671              : 
    3672          862 :   return check_references (ref->next, checker);
    3673              : }
    3674              : 
    3675              : /*  Return true if ns is a parent of the current ns.  */
    3676              : 
    3677              : static bool
    3678          548 : is_parent_of_current_ns (gfc_namespace *ns)
    3679              : {
    3680          548 :   gfc_namespace *p;
    3681          576 :   for (p = gfc_current_ns->parent; p; p = p->parent)
    3682          561 :     if (ns == p)
    3683              :       return true;
    3684              : 
    3685              :   return false;
    3686              : }
    3687              : 
    3688              : /* Verify that an expression is a restricted expression.  Like its
    3689              :    cousin check_init_expr(), an error message is generated if we
    3690              :    return false.  */
    3691              : 
    3692              : static bool
    3693       440768 : check_restricted (gfc_expr *e)
    3694              : {
    3695       440768 :   gfc_symbol* sym;
    3696       440768 :   bool t;
    3697              : 
    3698       440768 :   if (e == NULL)
    3699              :     return true;
    3700              : 
    3701       438231 :   switch (e->expr_type)
    3702              :     {
    3703         2667 :     case EXPR_OP:
    3704         2667 :       t = check_intrinsic_op (e, check_restricted);
    3705         2667 :       if (t)
    3706         2665 :         t = gfc_simplify_expr (e, 0);
    3707              : 
    3708              :       break;
    3709              : 
    3710            1 :     case EXPR_CONDITIONAL:
    3711            1 :       t = check_restricted (e->value.conditional.condition);
    3712            1 :       if (!t)
    3713              :         break;
    3714            1 :       t = check_restricted (e->value.conditional.true_expr);
    3715            1 :       if (!t)
    3716              :         break;
    3717            1 :       t = check_restricted (e->value.conditional.false_expr);
    3718            1 :       if (t)
    3719            1 :         t = gfc_simplify_expr (e, 0);
    3720              :       else
    3721              :         t = false;
    3722              :       break;
    3723              : 
    3724         3642 :     case EXPR_FUNCTION:
    3725         3642 :       if (e->value.function.esym)
    3726              :         {
    3727          579 :           t = check_arglist (e->value.function.actual, &check_restricted);
    3728          579 :           if (t)
    3729          579 :             t = external_spec_function (e);
    3730              :         }
    3731              :       else
    3732              :         {
    3733         3063 :           if (e->value.function.isym && e->value.function.isym->inquiry)
    3734              :             t = true;
    3735              :           else
    3736          763 :             t = check_arglist (e->value.function.actual, &check_restricted);
    3737              : 
    3738          763 :           if (t)
    3739         3055 :             t = restricted_intrinsic (e);
    3740              :         }
    3741              :       break;
    3742              : 
    3743        15208 :     case EXPR_VARIABLE:
    3744        15208 :       sym = e->symtree->n.sym;
    3745        15208 :       t = false;
    3746              : 
    3747              :       /* If a dummy argument appears in a context that is valid for a
    3748              :          restricted expression in an elemental procedure, it will have
    3749              :          already been simplified away once we get here.  Therefore we
    3750              :          don't need to jump through hoops to distinguish valid from
    3751              :          invalid cases.  Allowed in F2008 and F2018.  */
    3752        15208 :       if (gfc_notification_std (GFC_STD_F2008)
    3753           40 :           && sym->attr.dummy && sym->ns == gfc_current_ns
    3754        15248 :           && sym->ns->proc_name && sym->ns->proc_name->attr.elemental)
    3755              :         {
    3756            4 :           gfc_error_now ("Dummy argument %qs not "
    3757              :                          "allowed in expression at %L",
    3758              :                          sym->name, &e->where);
    3759            4 :           break;
    3760              :         }
    3761              : 
    3762        15204 :       if (sym->attr.optional)
    3763              :         {
    3764            2 :           gfc_error ("Dummy argument %qs at %L cannot be OPTIONAL",
    3765              :                      sym->name, &e->where);
    3766            2 :           break;
    3767              :         }
    3768              : 
    3769        15202 :       if (sym->attr.intent == INTENT_OUT)
    3770              :         {
    3771            0 :           gfc_error ("Dummy argument %qs at %L cannot be INTENT(OUT)",
    3772              :                      sym->name, &e->where);
    3773            0 :           break;
    3774              :         }
    3775              : 
    3776              :       /* Check reference chain if any.  */
    3777        15202 :       if (!check_references (e->ref, &check_restricted))
    3778              :         break;
    3779              : 
    3780        15199 :       if (e->error
    3781        15179 :             || sym->attr.in_common
    3782        14984 :             || sym->attr.use_assoc
    3783        11710 :             || sym->attr.used_in_submodule
    3784        11709 :             || sym->attr.dummy
    3785          606 :             || sym->attr.implied_index
    3786          606 :             || sym->attr.flavor == FL_PARAMETER
    3787        16295 :             || is_parent_of_current_ns (gfc_get_spec_ns (sym)))
    3788              :         {
    3789              :           t = true;
    3790              :           break;
    3791              :         }
    3792              : 
    3793           15 :       gfc_error ("Variable %qs cannot appear in the expression at %L",
    3794              :                  sym->name, &e->where);
    3795              :       /* Prevent a repetition of the error.  */
    3796           15 :       e->error = 1;
    3797           15 :       break;
    3798              : 
    3799              :     case EXPR_NULL:
    3800              :     case EXPR_CONSTANT:
    3801              :       t = true;
    3802              :       break;
    3803              : 
    3804            7 :     case EXPR_SUBSTRING:
    3805            7 :       t = gfc_specification_expr (e->ref->u.ss.start);
    3806            7 :       if (!t)
    3807              :         break;
    3808              : 
    3809            6 :       t = gfc_specification_expr (e->ref->u.ss.end);
    3810            6 :       if (t)
    3811            6 :         t = gfc_simplify_expr (e, 0);
    3812              : 
    3813              :       break;
    3814              : 
    3815            6 :     case EXPR_STRUCTURE:
    3816            6 :       t = gfc_check_constructor (e, check_restricted);
    3817            6 :       break;
    3818              : 
    3819           58 :     case EXPR_ARRAY:
    3820           58 :       t = gfc_check_constructor (e, check_restricted);
    3821           58 :       break;
    3822              : 
    3823            0 :     default:
    3824            0 :       gfc_internal_error ("check_restricted(): Unknown expression type");
    3825              :     }
    3826              : 
    3827              :   return t;
    3828              : }
    3829              : 
    3830              : 
    3831              : /* Check to see that an expression is a specification expression.  If
    3832              :    we return false, an error has been generated.  */
    3833              : 
    3834              : bool
    3835       463311 : gfc_specification_expr (gfc_expr *e)
    3836              : {
    3837       463311 :   gfc_component *comp;
    3838              : 
    3839       463311 :   if (e == NULL)
    3840              :     return true;
    3841              : 
    3842       428749 :   if (e->ts.type != BT_INTEGER)
    3843              :     {
    3844           26 :       gfc_error ("Expression at %L must be of INTEGER type, found %s",
    3845              :                  &e->where, gfc_basic_typename (e->ts.type));
    3846           26 :       return false;
    3847              :     }
    3848              : 
    3849       428723 :   comp = gfc_get_proc_ptr_comp (e);
    3850       428723 :   if (e->expr_type == EXPR_FUNCTION
    3851         2388 :       && !e->value.function.isym
    3852          392 :       && !e->value.function.esym
    3853          109 :       && !gfc_pure (e->symtree->n.sym)
    3854       428825 :       && (!comp || !comp->attr.pure))
    3855              :     {
    3856            3 :       gfc_error ("Function %qs at %L must be PURE",
    3857            3 :                  e->symtree->n.sym->name, &e->where);
    3858              :       /* Prevent repeat error messages.  */
    3859            3 :       e->symtree->n.sym->attr.pure = 1;
    3860            3 :       return false;
    3861              :     }
    3862              : 
    3863       428720 :   if (e->rank != 0)
    3864              :     {
    3865            3 :       gfc_error ("Expression at %L must be scalar", &e->where);
    3866            3 :       return false;
    3867              :     }
    3868              : 
    3869       428717 :   if (!gfc_simplify_expr (e, 0))
    3870              :     return false;
    3871              : 
    3872       428711 :   return check_restricted (e);
    3873              : }
    3874              : 
    3875              : 
    3876              : /************** Expression conformance checks.  *************/
    3877              : 
    3878              : /* Given two expressions, make sure that the arrays are conformable.  */
    3879              : 
    3880              : bool
    3881       194001 : gfc_check_conformance (gfc_expr *op1, gfc_expr *op2, const char *optype_msgid, ...)
    3882              : {
    3883       194001 :   int op1_flag, op2_flag, d;
    3884       194001 :   mpz_t op1_size, op2_size;
    3885       194001 :   bool t;
    3886              : 
    3887       194001 :   va_list argp;
    3888       194001 :   char buffer[240];
    3889              : 
    3890       194001 :   if (op1->rank == 0 || op2->rank == 0)
    3891              :     return true;
    3892              : 
    3893        69760 :   va_start (argp, optype_msgid);
    3894        69760 :   d = vsnprintf (buffer, sizeof (buffer), optype_msgid, argp);
    3895        69760 :   va_end (argp);
    3896        69760 :   if (d < 1 || d >= (int) sizeof (buffer)) /* Reject truncation.  */
    3897            0 :     gfc_internal_error ("optype_msgid overflow: %d", d);
    3898              : 
    3899        69760 :   if (op1->rank != op2->rank)
    3900              :     {
    3901           34 :       gfc_error ("Incompatible ranks in %s (%d and %d) at %L", _(buffer),
    3902              :                  op1->rank, op2->rank, &op1->where);
    3903           34 :       return false;
    3904              :     }
    3905              : 
    3906              :   t = true;
    3907              : 
    3908       168606 :   for (d = 0; d < op1->rank; d++)
    3909              :     {
    3910        98948 :       op1_flag = gfc_array_dimen_size(op1, d, &op1_size);
    3911        98948 :       op2_flag = gfc_array_dimen_size(op2, d, &op2_size);
    3912              : 
    3913        98948 :       if (op1_flag && op2_flag && mpz_cmp (op1_size, op2_size) != 0)
    3914              :         {
    3915           68 :           gfc_error ("Different shape for %s at %L on dimension %d "
    3916              :                      "(%d and %d)", _(buffer), &op1->where, d + 1,
    3917           68 :                      (int) mpz_get_si (op1_size),
    3918           68 :                      (int) mpz_get_si (op2_size));
    3919              : 
    3920           68 :           t = false;
    3921              :         }
    3922              : 
    3923        98948 :       if (op1_flag)
    3924        65255 :         mpz_clear (op1_size);
    3925        98948 :       if (op2_flag)
    3926        74832 :         mpz_clear (op2_size);
    3927              : 
    3928        98948 :       if (!t)
    3929              :         return false;
    3930              :     }
    3931              : 
    3932              :   return true;
    3933              : }
    3934              : 
    3935              : 
    3936              : /* Functions to check constant valued type specification parameters.  */
    3937              : 
    3938              : static gfc_actual_arglist *
    3939         2682 : get_parm_list_from_expr (gfc_expr *expr)
    3940              : {
    3941         2682 :   gfc_actual_arglist *a = NULL;
    3942         2682 :   gfc_constructor *c;
    3943              : 
    3944         2682 :   if (expr->expr_type == EXPR_STRUCTURE)
    3945         1214 :     a = expr->param_list;
    3946         1468 :   else if (expr->expr_type == EXPR_ARRAY)
    3947              :     {
    3948              :       /* Take the first constant expression, if there is one.  */
    3949           28 :       c = gfc_constructor_first (expr->value.constructor);
    3950           58 :       for (; c; c = gfc_constructor_next (c))
    3951           29 :         if (!c->iterator && c->expr && c->expr->param_list)
    3952              :           {
    3953              :             a = c->expr->param_list;
    3954              :             break;
    3955              :           }
    3956              :     }
    3957         1440 :   else if (expr->expr_type == EXPR_VARIABLE)
    3958         1309 :     a = expr->symtree->n.sym->param_list;
    3959              : 
    3960         2682 :   return a;
    3961              : }
    3962              : 
    3963              : bool
    3964         1341 : gfc_check_type_spec_parms (gfc_expr *expr1, gfc_expr *expr2,
    3965              :                            const char *context)
    3966              : {
    3967         1341 :   bool t = true;
    3968         1341 :   gfc_actual_arglist *a1, *a2;
    3969              : 
    3970         1341 :   gcc_assert (expr1->ts.type == BT_DERIVED
    3971              :               && expr1->ts.u.derived->attr.pdt_type);
    3972              : 
    3973         1341 :   a1 = get_parm_list_from_expr (expr1);
    3974         1341 :   a2 = get_parm_list_from_expr (expr2);
    3975              : 
    3976         2981 :   for (; a1 && a2; a1 = a1->next, a2 = a2->next)
    3977              :     {
    3978          299 :       if (a1->expr && a1->expr->expr_type == EXPR_CONSTANT
    3979          287 :           && a2->expr && a2->expr->expr_type == EXPR_CONSTANT
    3980          274 :           && !strcmp (a1->name, a2->name)
    3981          250 :           && mpz_cmp (a1->expr->value.integer, a2->expr->value.integer))
    3982              :         {
    3983           30 :           gfc_error ("Mismatched type parameters %qs(%d/%d) %s at %L/%L",
    3984              :                      a2->name,
    3985           20 :                      (int)mpz_get_ui (a1->expr->value.integer),
    3986           12 :                      (int)mpz_get_ui (a2->expr->value.integer),
    3987              :                      context,
    3988              :                      &expr1->where, &expr2->where);
    3989           10 :           t = false;
    3990              :         }
    3991              :     }
    3992              : 
    3993         1341 :   return t;
    3994              : }
    3995              : 
    3996              : 
    3997              : /* Given an assignable expression and an arbitrary expression, make
    3998              :    sure that the assignment can take place.  Only add a call to the intrinsic
    3999              :    conversion routines, when allow_convert is set.  When this assign is a
    4000              :    coarray call, then the convert is done by the coarray routine implicitly and
    4001              :    adding the intrinsic conversion would do harm in most cases.  */
    4002              : 
    4003              : bool
    4004       771418 : gfc_check_assign (gfc_expr *lvalue, gfc_expr *rvalue, int conform,
    4005              :                   bool allow_convert)
    4006              : {
    4007       771418 :   gfc_symbol *sym;
    4008       771418 :   gfc_ref *ref;
    4009       771418 :   int has_pointer;
    4010              : 
    4011       771418 :   sym = lvalue->symtree->n.sym;
    4012              : 
    4013              :   /* See if this is the component or subcomponent of a pointer and guard
    4014              :      against assignment to LEN or KIND part-refs.  */
    4015       771418 :   has_pointer = sym->attr.pointer;
    4016       903973 :   for (ref = lvalue->ref; ref; ref = ref->next)
    4017              :     {
    4018       132555 :       if (!has_pointer && ref->type == REF_COMPONENT
    4019        40535 :           && ref->u.c.component->attr.pointer)
    4020              :         has_pointer = 1;
    4021       131594 :       else if (ref->type == REF_INQUIRY
    4022           92 :                && (ref->u.i == INQUIRY_LEN || ref->u.i == INQUIRY_KIND))
    4023              :         {
    4024            0 :           gfc_error ("Assignment to a LEN or KIND part_ref at %L is not "
    4025              :                      "allowed", &lvalue->where);
    4026            0 :           return false;
    4027              :         }
    4028              :     }
    4029              : 
    4030              :   /* 12.5.2.2, Note 12.26: The result variable is very similar to any other
    4031              :      variable local to a function subprogram.  Its existence begins when
    4032              :      execution of the function is initiated and ends when execution of the
    4033              :      function is terminated...
    4034              :      Therefore, the left hand side is no longer a variable, when it is:  */
    4035       771418 :   if (sym->attr.flavor == FL_PROCEDURE && sym->attr.proc != PROC_ST_FUNCTION
    4036         8378 :       && !sym->attr.external)
    4037              :     {
    4038         8368 :       bool bad_proc;
    4039         8368 :       bad_proc = false;
    4040              : 
    4041              :       /* (i) Use associated;  */
    4042         8368 :       if (sym->attr.use_assoc)
    4043            0 :         bad_proc = true;
    4044              : 
    4045              :       /* (ii) The assignment is in the main program; or  */
    4046         8368 :       if (gfc_current_ns->proc_name
    4047         8367 :           && gfc_current_ns->proc_name->attr.is_main_program)
    4048         8368 :         bad_proc = true;
    4049              : 
    4050              :       /* (iii) A module or internal procedure...  */
    4051         8368 :       if (gfc_current_ns->proc_name
    4052         8367 :           && (gfc_current_ns->proc_name->attr.proc == PROC_INTERNAL
    4053         4744 :               || gfc_current_ns->proc_name->attr.proc == PROC_MODULE)
    4054         5943 :           && gfc_current_ns->parent
    4055         5460 :           && (!(gfc_current_ns->parent->proc_name->attr.function
    4056         5307 :                 || gfc_current_ns->parent->proc_name->attr.subroutine)
    4057         2897 :               || gfc_current_ns->parent->proc_name->attr.is_main_program))
    4058              :         {
    4059              :           /* ... that is not a function...  */
    4060         4990 :           if (gfc_current_ns->proc_name
    4061         4990 :               && !gfc_current_ns->proc_name->attr.function)
    4062            0 :             bad_proc = true;
    4063              : 
    4064              :           /* ... or is not an entry and has a different name.  */
    4065         4990 :           if (!sym->attr.entry && sym->name != gfc_current_ns->proc_name->name)
    4066         8368 :             bad_proc = true;
    4067              :         }
    4068              : 
    4069              :       /* (iv) Host associated and not the function symbol or the
    4070              :               parent result.  This picks up sibling references, which
    4071              :               cannot be entries.  */
    4072         8368 :       if (!sym->attr.entry
    4073         7626 :             && sym->ns == gfc_current_ns->parent
    4074         5215 :             && sym != gfc_current_ns->proc_name
    4075           72 :             && sym != gfc_current_ns->parent->proc_name->result)
    4076              :         bad_proc = true;
    4077              : 
    4078         8367 :       if (bad_proc)
    4079              :         {
    4080            1 :           gfc_error ("%qs at %L is not a VALUE", sym->name, &lvalue->where);
    4081            1 :           return false;
    4082              :         }
    4083              :     }
    4084              :   else
    4085              :     {
    4086              :       /* Reject assigning to an external symbol.  For initializers, this
    4087              :          was already done before, in resolve_fl_procedure.  */
    4088       763050 :       if (sym->attr.flavor == FL_PROCEDURE && sym->attr.external
    4089           10 :           && sym->attr.proc != PROC_MODULE && !rvalue->error)
    4090              :         {
    4091            2 :           gfc_error ("Illegal assignment to external procedure at %L",
    4092              :                      &lvalue->where);
    4093            2 :           return false;
    4094              :         }
    4095              :     }
    4096              : 
    4097       771415 :   if (rvalue->rank != 0 && lvalue->rank != rvalue->rank)
    4098              :     {
    4099           25 :       gfc_error ("Incompatible ranks %d and %d in assignment at %L",
    4100              :                  lvalue->rank, rvalue->rank, &lvalue->where);
    4101           25 :       return false;
    4102              :     }
    4103              : 
    4104       771390 :   if (lvalue->ts.type == BT_UNKNOWN)
    4105              :     {
    4106            0 :       gfc_error ("Variable type is UNKNOWN in assignment at %L",
    4107              :                  &lvalue->where);
    4108            0 :       return false;
    4109              :     }
    4110              : 
    4111       771390 :   if (rvalue->expr_type == EXPR_NULL)
    4112              :     {
    4113           19 :       if (has_pointer && (ref == NULL || ref->next == NULL)
    4114            8 :           && lvalue->symtree->n.sym->attr.data)
    4115              :         return true;
    4116              :       /* Prevent the following error message for caf-single mode, because there
    4117              :          are no teams in single mode and the simplify returns a null then.  */
    4118           12 :       else if (!(flag_coarray == GFC_FCOARRAY_SINGLE
    4119            9 :                  && rvalue->ts.type == BT_DERIVED
    4120            9 :                  && rvalue->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    4121            9 :                  && rvalue->ts.u.derived->intmod_sym_id
    4122              :                       == ISOFORTRAN_TEAM_TYPE))
    4123              :         {
    4124            3 :           gfc_error ("NULL appears on right-hand side in assignment at %L",
    4125              :                      &rvalue->where);
    4126            3 :           return false;
    4127              :         }
    4128              :     }
    4129              : 
    4130              :   /* This is possibly a typo: x = f() instead of x => f().  */
    4131       771380 :   if (warn_surprising
    4132       771380 :       && rvalue->expr_type == EXPR_FUNCTION && gfc_expr_attr (rvalue).pointer)
    4133            6 :     gfc_warning (OPT_Wsurprising,
    4134              :                  "POINTER-valued function appears on right-hand side of "
    4135              :                  "assignment at %L", &rvalue->where);
    4136              : 
    4137              :   /* Check size of array assignments.  */
    4138        77238 :   if (lvalue->rank != 0 && rvalue->rank != 0
    4139       822039 :       && !gfc_check_conformance (lvalue, rvalue, _("array assignment")))
    4140              :     return false;
    4141              : 
    4142              :   /* Handle the case of a BOZ literal on the RHS.  */
    4143       771348 :   if (rvalue->ts.type == BT_BOZ)
    4144              :     {
    4145          241 :       if (lvalue->symtree->n.sym->attr.data)
    4146              :         {
    4147           93 :           if (lvalue->ts.type == BT_INTEGER
    4148           93 :               && gfc_boz2int (rvalue, lvalue->ts.kind))
    4149              :             return true;
    4150              : 
    4151            2 :           if (lvalue->ts.type == BT_REAL
    4152            2 :               && gfc_boz2real (rvalue, lvalue->ts.kind))
    4153              :             {
    4154            2 :               if (gfc_invalid_boz ("BOZ literal constant near %L cannot "
    4155              :                                    "be assigned to a REAL variable",
    4156              :                                    &rvalue->where))
    4157              :                 return false;
    4158              :               return true;
    4159              :             }
    4160              :         }
    4161              : 
    4162          148 :       if (!lvalue->symtree->n.sym->attr.data
    4163          148 :           && gfc_invalid_boz ("BOZ literal constant at %L is neither a "
    4164              :                               "data-stmt-constant nor an actual argument to "
    4165              :                               "INT, REAL, DBLE, or CMPLX intrinsic function",
    4166              :                               &rvalue->where))
    4167              :         return false;
    4168              : 
    4169          148 :       if (lvalue->ts.type == BT_INTEGER
    4170          148 :           && gfc_boz2int (rvalue, lvalue->ts.kind))
    4171              :         return true;
    4172              : 
    4173            1 :       if (lvalue->ts.type == BT_REAL
    4174            1 :           && gfc_boz2real (rvalue, lvalue->ts.kind))
    4175              :         return true;
    4176              : 
    4177            0 :       gfc_error ("BOZ literal constant near %L cannot be assigned to a "
    4178              :                  "%qs variable", &rvalue->where, gfc_typename (lvalue));
    4179            0 :       return false;
    4180              :     }
    4181              : 
    4182       771107 :   if (gfc_expr_attr (lvalue).pdt_kind || gfc_expr_attr (lvalue).pdt_len)
    4183              :     {
    4184            3 :       gfc_error ("The assignment to a KIND or LEN component of a "
    4185              :                  "parameterized type at %L is not allowed",
    4186              :                  &lvalue->where);
    4187            3 :       return false;
    4188              :     }
    4189              : 
    4190              : 
    4191              :   /* Check that the type spec. parameters are the same on both sides.  */
    4192        54180 :   if (lvalue->ts.type == BT_DERIVED && lvalue->ts.u.derived->attr.pdt_type
    4193       772330 :       && !gfc_check_type_spec_parms (lvalue, rvalue, "in assignment"))
    4194              :     return false;
    4195              : 
    4196       771101 :   if (gfc_compare_types (&lvalue->ts, &rvalue->ts))
    4197              :     return true;
    4198              : 
    4199              :   /* Only DATA Statements come here.  */
    4200        19208 :   if (!conform)
    4201              :     {
    4202         1524 :       locus *where;
    4203              : 
    4204              :       /* Numeric can be converted to any other numeric. And Hollerith can be
    4205              :          converted to any other type.  */
    4206         2817 :       if ((gfc_numeric_ts (&lvalue->ts) && gfc_numeric_ts (&rvalue->ts))
    4207         2127 :           || rvalue->ts.type == BT_HOLLERITH)
    4208         1145 :         return true;
    4209              : 
    4210          364 :       if (flag_dec_char_conversions && (gfc_numeric_ts (&lvalue->ts)
    4211           91 :           || lvalue->ts.type == BT_LOGICAL)
    4212          364 :           && rvalue->ts.type == BT_CHARACTER
    4213          743 :           && rvalue->ts.kind == gfc_default_character_kind)
    4214              :         return true;
    4215              : 
    4216           19 :       if (lvalue->ts.type == BT_LOGICAL && rvalue->ts.type == BT_LOGICAL)
    4217              :         return true;
    4218              : 
    4219           18 :       where = (GFC_LOCUS_IS_SET (lvalue->where)
    4220           18 :                ? &lvalue->where : &rvalue->where);
    4221           18 :       gfc_error ("Incompatible types in DATA statement at %L; attempted "
    4222              :                  "conversion of %s to %s", where,
    4223              :                  gfc_typename (rvalue), gfc_typename (lvalue));
    4224              : 
    4225           18 :       return false;
    4226              :     }
    4227              : 
    4228              :   /* Assignment is the only case where character variables of different
    4229              :      kind values can be converted into one another.  */
    4230        17684 :   if (lvalue->ts.type == BT_CHARACTER && rvalue->ts.type == BT_CHARACTER)
    4231              :     {
    4232          382 :       if (lvalue->ts.kind != rvalue->ts.kind && allow_convert)
    4233          382 :         return gfc_convert_chartype (rvalue, &lvalue->ts);
    4234              :       else
    4235              :         return true;
    4236              :     }
    4237              : 
    4238        17302 :   if (!allow_convert)
    4239              :     return true;
    4240              : 
    4241        17302 :   return gfc_convert_type (rvalue, &lvalue->ts, 1);
    4242              : }
    4243              : 
    4244              : 
    4245              : /* Check that a pointer assignment is OK.  We first check lvalue, and
    4246              :    we only check rvalue if it's not an assignment to NULL() or a
    4247              :    NULLIFY statement.  */
    4248              : 
    4249              : bool
    4250        16081 : gfc_check_pointer_assign (gfc_expr *lvalue, gfc_expr *rvalue,
    4251              :                           bool suppress_type_test, bool is_init_expr)
    4252              : {
    4253        16081 :   symbol_attribute attr, lhs_attr;
    4254        16081 :   gfc_ref *ref;
    4255        16081 :   bool is_pure, is_implicit_pure, rank_remap;
    4256        16081 :   int proc_pointer;
    4257        16081 :   bool same_rank;
    4258              : 
    4259        16081 :   if (!lvalue->symtree)
    4260              :     return false;
    4261              : 
    4262        16080 :   lhs_attr = gfc_expr_attr (lvalue);
    4263        16080 :   if (lvalue->ts.type == BT_UNKNOWN && !lhs_attr.proc_pointer)
    4264              :     {
    4265            0 :       gfc_error ("Pointer assignment target is not a POINTER at %L",
    4266              :                  &lvalue->where);
    4267            0 :       return false;
    4268              :     }
    4269              : 
    4270        16080 :   if (lhs_attr.flavor == FL_PROCEDURE && lhs_attr.use_assoc
    4271           36 :       && !lhs_attr.proc_pointer)
    4272              :     {
    4273            0 :       gfc_error ("%qs in the pointer assignment at %L cannot be an "
    4274              :                  "l-value since it is a procedure",
    4275            0 :                  lvalue->symtree->n.sym->name, &lvalue->where);
    4276            0 :       return false;
    4277              :     }
    4278              : 
    4279        16080 :   proc_pointer = lvalue->symtree->n.sym->attr.proc_pointer;
    4280              : 
    4281        16080 :   rank_remap = false;
    4282        16080 :   same_rank = lvalue->rank == rvalue->rank;
    4283        23175 :   for (ref = lvalue->ref; ref; ref = ref->next)
    4284              :     {
    4285        11138 :       if (ref->type == REF_COMPONENT)
    4286         6271 :         proc_pointer = ref->u.c.component->attr.proc_pointer;
    4287              : 
    4288        11138 :       if (ref->type == REF_ARRAY && ref->next == NULL)
    4289              :         {
    4290         4409 :           int dim;
    4291              : 
    4292         4409 :           if (ref->u.ar.type == AR_FULL)
    4293              :             break;
    4294              : 
    4295          377 :           if (ref->u.ar.type != AR_SECTION)
    4296              :             {
    4297            2 :               gfc_error ("Expected bounds specification for %qs at %L",
    4298            2 :                          lvalue->symtree->n.sym->name, &lvalue->where);
    4299            2 :               return false;
    4300              :             }
    4301              : 
    4302          375 :           if (!gfc_notify_std (GFC_STD_F2003, "Bounds specification "
    4303              :                                "for %qs in pointer assignment at %L",
    4304          375 :                                lvalue->symtree->n.sym->name, &lvalue->where))
    4305              :             return false;
    4306              : 
    4307              :           /* Fortran standard (e.g. F2018, 10.2.2 Pointer assignment):
    4308              :            *
    4309              :            * (C1017) If bounds-spec-list is specified, the number of
    4310              :            * bounds-specs shall equal the rank of data-pointer-object.
    4311              :            *
    4312              :            * If bounds-spec-list appears, it specifies the lower bounds.
    4313              :            *
    4314              :            * (C1018) If bounds-remapping-list is specified, the number of
    4315              :            * bounds-remappings shall equal the rank of data-pointer-object.
    4316              :            *
    4317              :            * If bounds-remapping-list appears, it specifies the upper and
    4318              :            * lower bounds of each dimension of the pointer; the pointer target
    4319              :            * shall be simply contiguous or of rank one.
    4320              :            *
    4321              :            * (C1019) If bounds-remapping-list is not specified, the ranks of
    4322              :            * data-pointer-object and data-target shall be the same.
    4323              :            *
    4324              :            * Thus when bounds are given, all lbounds are necessary and either
    4325              :            * all or none of the upper bounds; no strides are allowed.  If the
    4326              :            * upper bounds are present, we may do rank remapping.  */
    4327          966 :           for (dim = 0; dim < ref->u.ar.dimen; ++dim)
    4328              :             {
    4329          600 :               if (ref->u.ar.stride[dim])
    4330              :                 {
    4331            1 :                   gfc_error ("Stride must not be present at %L",
    4332              :                              &lvalue->where);
    4333            1 :                   return false;
    4334              :                 }
    4335          599 :               if (!same_rank && (!ref->u.ar.start[dim] ||!ref->u.ar.end[dim]))
    4336              :                 {
    4337            3 :                   gfc_error ("Rank remapping requires a "
    4338              :                              "list of %<lower-bound : upper-bound%> "
    4339              :                              "specifications at %L", &lvalue->where);
    4340            3 :                   return false;
    4341              :                 }
    4342          596 :               if (!ref->u.ar.start[dim]
    4343          595 :                   || ref->u.ar.dimen_type[dim] != DIMEN_RANGE)
    4344              :                 {
    4345            2 :                   gfc_error ("Expected list of %<lower-bound :%> or "
    4346              :                              "list of %<lower-bound : upper-bound%> "
    4347              :                              "specifications at %L", &lvalue->where);
    4348            2 :                   return false;
    4349              :                 }
    4350              : 
    4351          594 :               if (dim == 0)
    4352          367 :                 rank_remap = (ref->u.ar.end[dim] != NULL);
    4353              :               else
    4354              :                 {
    4355          227 :                   if ((rank_remap && !ref->u.ar.end[dim]))
    4356              :                     {
    4357            0 :                       gfc_error ("Rank remapping requires a "
    4358              :                                  "list of %<lower-bound : upper-bound%> "
    4359              :                                  "specifications at %L", &lvalue->where);
    4360            0 :                       return false;
    4361              :                     }
    4362          102 :                   if (!rank_remap && ref->u.ar.end[dim])
    4363              :                     {
    4364            0 :                       gfc_error ("Expected list of %<lower-bound :%> or "
    4365              :                                  "list of %<lower-bound : upper-bound%> "
    4366              :                                  "specifications at %L", &lvalue->where);
    4367            0 :                       return false;
    4368              :                     }
    4369              :                 }
    4370              :             }
    4371              :         }
    4372              :     }
    4373              : 
    4374        16069 :   is_pure = gfc_pure (NULL);
    4375        16069 :   is_implicit_pure = gfc_implicit_pure (NULL);
    4376              : 
    4377              :   /* If rvalue is a NULL() or NULLIFY, we're done. Otherwise the type,
    4378              :      kind, etc for lvalue and rvalue must match, and rvalue must be a
    4379              :      pure variable if we're in a pure function.  */
    4380        16069 :   if (rvalue->expr_type == EXPR_NULL && rvalue->ts.type == BT_UNKNOWN)
    4381              :     return true;
    4382              : 
    4383              :   /* F2008, C723 (pointer) and C726 (proc-pointer); for PURE also C1283.  */
    4384         8883 :   if (lvalue->expr_type == EXPR_VARIABLE
    4385         8883 :       && gfc_is_coindexed (lvalue))
    4386              :     {
    4387            5 :       gfc_ref *ref;
    4388            6 :       for (ref = lvalue->ref; ref; ref = ref->next)
    4389            6 :         if (ref->type == REF_ARRAY && ref->u.ar.codimen)
    4390              :           {
    4391            5 :             gfc_error ("Pointer object at %L shall not have a coindex",
    4392              :                        &lvalue->where);
    4393            5 :             return false;
    4394              :           }
    4395              :     }
    4396              : 
    4397              :   /* Checks on rvalue for procedure pointer assignments.  */
    4398         8878 :   if (proc_pointer)
    4399              :     {
    4400         1249 :       char err[200];
    4401         1249 :       gfc_symbol *s1,*s2;
    4402         1249 :       gfc_component *comp1, *comp2;
    4403         1249 :       const char *name;
    4404              : 
    4405         1249 :       attr = gfc_expr_attr (rvalue);
    4406         2257 :       if (!((rvalue->expr_type == EXPR_NULL)
    4407         1243 :             || (rvalue->expr_type == EXPR_FUNCTION && attr.proc_pointer)
    4408         1122 :             || (rvalue->expr_type == EXPR_VARIABLE && attr.proc_pointer)
    4409              :             || (rvalue->expr_type == EXPR_VARIABLE
    4410         1006 :                 && attr.flavor == FL_PROCEDURE)))
    4411              :         {
    4412            6 :           gfc_error ("Invalid procedure pointer assignment at %L",
    4413              :                      &rvalue->where);
    4414            6 :           return false;
    4415              :         }
    4416              : 
    4417         1243 :       if (rvalue->expr_type == EXPR_VARIABLE && !attr.proc_pointer)
    4418              :         {
    4419              :           /* Check for intrinsics.  */
    4420         1002 :           gfc_symbol *sym = rvalue->symtree->n.sym;
    4421         1002 :           if (!sym->attr.intrinsic
    4422         1002 :               && (gfc_is_intrinsic (sym, 0, sym->declared_at)
    4423          875 :                   || gfc_is_intrinsic (sym, 1, sym->declared_at)))
    4424              :             {
    4425           37 :               sym->attr.intrinsic = 1;
    4426           37 :               gfc_resolve_intrinsic (sym, &rvalue->where);
    4427           37 :               attr = gfc_expr_attr (rvalue);
    4428              :             }
    4429              :           /* Check for result of embracing function.  */
    4430         1002 :           if (sym->attr.function && sym->result == sym)
    4431              :             {
    4432          373 :               gfc_namespace *ns;
    4433              : 
    4434          819 :               for (ns = gfc_current_ns; ns; ns = ns->parent)
    4435          450 :                 if (sym == ns->proc_name)
    4436              :                   {
    4437            4 :                     gfc_error ("Function result %qs is invalid as proc-target "
    4438              :                                "in procedure pointer assignment at %L",
    4439              :                                sym->name, &rvalue->where);
    4440            4 :                     return false;
    4441              :                   }
    4442              :             }
    4443              :         }
    4444         1239 :       if (attr.abstract)
    4445              :         {
    4446            1 :           gfc_error ("Abstract interface %qs is invalid "
    4447              :                      "in procedure pointer assignment at %L",
    4448            1 :                      rvalue->symtree->name, &rvalue->where);
    4449            1 :           return false;
    4450              :         }
    4451              :       /* Check for F08:C729.  */
    4452         1238 :       if (attr.flavor == FL_PROCEDURE)
    4453              :         {
    4454         1232 :           if (attr.proc == PROC_ST_FUNCTION)
    4455              :             {
    4456            1 :               gfc_error ("Statement function %qs is invalid "
    4457              :                          "in procedure pointer assignment at %L",
    4458            1 :                          rvalue->symtree->name, &rvalue->where);
    4459            1 :               return false;
    4460              :             }
    4461         1563 :           if (attr.proc == PROC_INTERNAL &&
    4462          332 :               !gfc_notify_std(GFC_STD_F2008, "Internal procedure %qs "
    4463              :                               "is invalid in procedure pointer assignment "
    4464          332 :                               "at %L", rvalue->symtree->name, &rvalue->where))
    4465              :             return false;
    4466         1357 :           if (attr.intrinsic && gfc_intrinsic_actual_ok (rvalue->symtree->name,
    4467          127 :                                                          attr.subroutine) == 0)
    4468              :             {
    4469            1 :               gfc_error ("Intrinsic %qs at %L is invalid in procedure pointer "
    4470            1 :                          "assignment", rvalue->symtree->name, &rvalue->where);
    4471            1 :               return false;
    4472              :             }
    4473              :         }
    4474              :       /* Check for F08:C730.  */
    4475         1235 :       if (attr.elemental && !attr.intrinsic)
    4476              :         {
    4477            1 :           gfc_error ("Nonintrinsic elemental procedure %qs is invalid "
    4478              :                      "in procedure pointer assignment at %L",
    4479            1 :                      rvalue->symtree->name, &rvalue->where);
    4480            1 :           return false;
    4481              :         }
    4482              : 
    4483              :       /* Ensure that the calling convention is the same. As other attributes
    4484              :          such as DLLEXPORT may differ, one explicitly only tests for the
    4485              :          calling conventions.  */
    4486         1234 :       if (rvalue->expr_type == EXPR_VARIABLE
    4487         1107 :           && lvalue->symtree->n.sym->attr.ext_attr
    4488         1107 :                != rvalue->symtree->n.sym->attr.ext_attr)
    4489              :         {
    4490           10 :           symbol_attribute calls;
    4491              : 
    4492           10 :           calls.ext_attr = 0;
    4493           10 :           gfc_add_ext_attribute (&calls, EXT_ATTR_CDECL, NULL);
    4494           10 :           gfc_add_ext_attribute (&calls, EXT_ATTR_STDCALL, NULL);
    4495           10 :           gfc_add_ext_attribute (&calls, EXT_ATTR_FASTCALL, NULL);
    4496              : 
    4497           10 :           if ((calls.ext_attr & lvalue->symtree->n.sym->attr.ext_attr)
    4498           10 :               != (calls.ext_attr & rvalue->symtree->n.sym->attr.ext_attr))
    4499              :             {
    4500           10 :               gfc_error ("Mismatch in the procedure pointer assignment "
    4501              :                          "at %L: mismatch in the calling convention",
    4502              :                          &rvalue->where);
    4503           10 :           return false;
    4504              :             }
    4505              :         }
    4506              : 
    4507         1224 :       comp1 = gfc_get_proc_ptr_comp (lvalue);
    4508         1224 :       if (comp1)
    4509          383 :         s1 = comp1->ts.interface;
    4510              :       else
    4511              :         {
    4512          841 :           s1 = lvalue->symtree->n.sym;
    4513          841 :           if (s1->ts.interface)
    4514          636 :             s1 = s1->ts.interface;
    4515              :         }
    4516              : 
    4517         1224 :       comp2 = gfc_get_proc_ptr_comp (rvalue);
    4518         1224 :       if (comp2)
    4519              :         {
    4520           67 :           if (rvalue->expr_type == EXPR_FUNCTION)
    4521              :             {
    4522            6 :               s2 = comp2->ts.interface->result;
    4523            6 :               name = s2->name;
    4524              :             }
    4525              :           else
    4526              :             {
    4527           61 :               s2 = comp2->ts.interface;
    4528           61 :               name = comp2->name;
    4529              :             }
    4530              :         }
    4531         1157 :       else if (rvalue->expr_type == EXPR_FUNCTION)
    4532              :         {
    4533          115 :           if (rvalue->value.function.esym)
    4534          115 :             s2 = rvalue->value.function.esym->result;
    4535              :           else
    4536            0 :             s2 = rvalue->symtree->n.sym->result;
    4537              : 
    4538          115 :           name = s2->name;
    4539              :         }
    4540              :       else
    4541              :         {
    4542         1042 :           s2 = rvalue->symtree->n.sym;
    4543         1042 :           name = s2->name;
    4544              :         }
    4545              : 
    4546         1224 :       if (s2 && s2->attr.proc_pointer && s2->ts.interface)
    4547         1224 :         s2 = s2->ts.interface;
    4548              : 
    4549              :       /* Special check for the case of absent interface on the lvalue.
    4550              :        * All other interface checks are done below. */
    4551         1224 :       if (!s1 && comp1 && comp1->attr.subroutine && s2 && s2->attr.function)
    4552              :         {
    4553            1 :           gfc_error ("Interface mismatch in procedure pointer assignment "
    4554              :                      "at %L: %qs is not a subroutine", &rvalue->where, name);
    4555            1 :           return false;
    4556              :         }
    4557              : 
    4558              :       /* F08:7.2.2.4 (4)  */
    4559         1221 :       if (s2 && gfc_explicit_interface_required (s2, err, sizeof(err)))
    4560              :         {
    4561          251 :           if (comp1 && !s1)
    4562              :             {
    4563            2 :               gfc_error ("Explicit interface required for component %qs at %L: %s",
    4564              :                          comp1->name, &lvalue->where, err);
    4565            2 :               return false;
    4566              :             }
    4567          249 :           else if (s1->attr.if_source == IFSRC_UNKNOWN)
    4568              :             {
    4569            2 :               gfc_error ("Explicit interface required for %qs at %L: %s",
    4570              :                          s1->name, &lvalue->where, err);
    4571            2 :               return false;
    4572              :             }
    4573              :         }
    4574         1219 :       if (s1 && gfc_explicit_interface_required (s1, err, sizeof(err)))
    4575              :         {
    4576          263 :           if (comp2 && !s2)
    4577              :             {
    4578            2 :               gfc_error ("Explicit interface required for component %qs at %L: %s",
    4579              :                          comp2->name, &rvalue->where, err);
    4580            2 :               return false;
    4581              :             }
    4582          261 :           else if (s2->attr.if_source == IFSRC_UNKNOWN)
    4583              :             {
    4584            2 :               gfc_error ("Explicit interface required for %qs at %L: %s",
    4585              :                          s2->name, &rvalue->where, err);
    4586            2 :               return false;
    4587              :             }
    4588              :         }
    4589              : 
    4590         1215 :       if (s1 == s2 || !s1 || !s2)
    4591              :         return true;
    4592              : 
    4593          717 :       if (!gfc_compare_interfaces (s1, s2, name, 0, 1,
    4594              :                                    err, sizeof(err), NULL, NULL))
    4595              :         {
    4596           23 :           gfc_error ("Interface mismatch in procedure pointer assignment "
    4597              :                      "at %L: %s", &rvalue->where, err);
    4598           23 :           return false;
    4599              :         }
    4600              : 
    4601              :       /* Check F2008Cor2, C729.  */
    4602          694 :       if (!s2->attr.intrinsic && s2->attr.if_source == IFSRC_UNKNOWN
    4603          102 :           && !s2->attr.external && !s2->attr.subroutine && !s2->attr.function)
    4604              :         {
    4605            1 :           gfc_error ("Procedure pointer target %qs at %L must be either an "
    4606              :                      "intrinsic, host or use associated, referenced or have "
    4607              :                      "the EXTERNAL attribute", s2->name, &rvalue->where);
    4608            1 :           return false;
    4609              :         }
    4610              : 
    4611              :       return true;
    4612              :     }
    4613              :   else
    4614              :     {
    4615              :       /* A non-proc pointer cannot point to a constant.  */
    4616         7629 :       if (rvalue->expr_type == EXPR_CONSTANT)
    4617              :         {
    4618            2 :           gfc_error_now ("Pointer assignment target cannot be a constant at %L",
    4619              :                          &rvalue->where);
    4620            2 :           return false;
    4621              :         }
    4622              :     }
    4623              : 
    4624         7627 :   if (!gfc_compare_types (&lvalue->ts, &rvalue->ts))
    4625              :     {
    4626              :       /* Check for F03:C717.  */
    4627           11 :       if (UNLIMITED_POLY (rvalue)
    4628            1 :           && !(UNLIMITED_POLY (lvalue)
    4629            1 :                || (lvalue->ts.type == BT_DERIVED
    4630            0 :                    && (lvalue->ts.u.derived->attr.is_bind_c
    4631            0 :                        || lvalue->ts.u.derived->attr.sequence))))
    4632            1 :         gfc_error ("Data-pointer-object at %L must be unlimited "
    4633              :                    "polymorphic, or of a type with the BIND or SEQUENCE "
    4634              :                    "attribute, to be compatible with an unlimited "
    4635              :                    "polymorphic target", &lvalue->where);
    4636           10 :       else if (!suppress_type_test)
    4637            8 :         gfc_error ("Different types in pointer assignment at %L; "
    4638              :                    "attempted assignment of %s to %s", &lvalue->where,
    4639              :                    gfc_typename (rvalue), gfc_typename (lvalue));
    4640           11 :       return false;
    4641              :     }
    4642              : 
    4643         7616 :   if (lvalue->ts.type != BT_CLASS && lvalue->ts.kind != rvalue->ts.kind)
    4644              :     {
    4645            0 :       gfc_error ("Different kind type parameters in pointer "
    4646              :                  "assignment at %L", &lvalue->where);
    4647            0 :       return false;
    4648              :     }
    4649              : 
    4650         7616 :   if (lvalue->rank != rvalue->rank && !rank_remap
    4651           64 :       && !(rvalue->expr_type == EXPR_NULL && is_init_expr))
    4652              :     {
    4653            4 :       gfc_error ("Different ranks in pointer assignment at %L", &lvalue->where);
    4654            4 :       return false;
    4655              :     }
    4656              : 
    4657              :   /* Make sure the vtab is present.  */
    4658         7612 :   if (lvalue->ts.type == BT_CLASS && !UNLIMITED_POLY (rvalue))
    4659         1332 :     gfc_find_vtab (&rvalue->ts);
    4660              : 
    4661              :   /* Check rank remapping.  */
    4662         7612 :   if (rank_remap)
    4663              :     {
    4664          240 :       mpz_t lsize, rsize;
    4665              : 
    4666              :       /* If this can be determined, check that the target must be at least as
    4667              :          large as the pointer assigned to it is.  */
    4668          240 :       bool got_lsize = gfc_array_size (lvalue, &lsize);
    4669          240 :       bool got_rsize = got_lsize && gfc_array_size (rvalue, &rsize);
    4670           87 :       bool too_small = got_rsize && mpz_cmp (rsize, lsize) < 0;
    4671              : 
    4672          240 :       if (too_small)
    4673              :         {
    4674            4 :           gfc_error ("Rank remapping target is smaller than size of the"
    4675              :                      " pointer (%ld < %ld) at %L",
    4676              :                      mpz_get_si (rsize), mpz_get_si (lsize),
    4677              :                      &lvalue->where);
    4678            4 :           mpz_clear (lsize);
    4679            4 :           mpz_clear (rsize);
    4680            8 :           return false;
    4681              :         }
    4682          236 :       if (got_lsize)
    4683          151 :         mpz_clear (lsize);
    4684          236 :       if (got_rsize)
    4685           83 :         mpz_clear (rsize);
    4686              : 
    4687              :       /* An assumed rank target is an experimental F202y feature.  */
    4688          236 :       if (rvalue->rank == -1 && !(gfc_option.allow_std & GFC_STD_F202Y))
    4689              :         {
    4690            1 :           gfc_error ("The assumed rank target at %L is an experimental F202y "
    4691              :                      "feature. Use option -std=f202y to enable",
    4692              :                      &rvalue->where);
    4693            1 :           return false;
    4694              :         }
    4695              : 
    4696              :       /* The target must be either rank one or it must be simply contiguous
    4697              :          and F2008 must be allowed.  */
    4698          235 :       if (rvalue->rank != 1 && rvalue->rank != -1)
    4699              :         {
    4700           21 :           if (!gfc_is_simply_contiguous (rvalue, true, false))
    4701              :             {
    4702            2 :               gfc_error ("Rank remapping target must be rank 1 or"
    4703              :                          " simply contiguous at %L", &rvalue->where);
    4704            2 :               return false;
    4705              :             }
    4706           19 :           if (!gfc_notify_std (GFC_STD_F2008, "Rank remapping target is not "
    4707              :                                "rank 1 at %L", &rvalue->where))
    4708              :             return false;
    4709              :         }
    4710              :     }
    4711         7372 :   else if (rvalue->rank == -1)
    4712              :     {
    4713            0 :       gfc_error ("The data-target at %L is an assumed rank object and so the "
    4714              :                  "data-pointer-object %s must have a bounds remapping list "
    4715              :                  "(list of lbound:ubound for each dimension)",
    4716            0 :                   &rvalue->where, lvalue->symtree->name);
    4717            0 :       return false;
    4718              :     }
    4719              : 
    4720         7604 :   if (rvalue->rank == -1 && !gfc_is_simply_contiguous (rvalue, true, false))
    4721              :     {
    4722            0 :       gfc_error ("The assumed rank data-target at %L must be contiguous",
    4723              :                  &rvalue->where);
    4724            0 :       return false;
    4725              :     }
    4726              : 
    4727              :   /* Now punt if we are dealing with a NULLIFY(X) or X = NULL(X).  */
    4728         7604 :   if (rvalue->expr_type == EXPR_NULL)
    4729              :     return true;
    4730              : 
    4731         7517 :   if (rvalue->expr_type == EXPR_VARIABLE && is_subref_array (rvalue))
    4732          549 :     lvalue->symtree->n.sym->attr.subref_array_pointer = 1;
    4733              : 
    4734         7517 :   attr = gfc_expr_attr (rvalue);
    4735              : 
    4736         7517 :   if (rvalue->expr_type == EXPR_FUNCTION && !attr.pointer)
    4737              :     {
    4738              :       /* F2008, C725.  For PURE also C1283.  Sometimes rvalue is a function call
    4739              :          to caf_get.  Map this to the same error message as below when it is
    4740              :          still a variable expression.  */
    4741            1 :       if (rvalue->value.function.isym
    4742            0 :           && rvalue->value.function.isym->id == GFC_ISYM_CAF_GET)
    4743              :         /* The test above might need to be extend when F08, Note 5.4 has to be
    4744              :            interpreted in the way that target and pointer with the same coindex
    4745              :            are allowed.  */
    4746            0 :         gfc_error ("Data target at %L shall not have a coindex",
    4747              :                    &rvalue->where);
    4748              :       else
    4749            1 :         gfc_error ("Target expression in pointer assignment "
    4750              :                    "at %L must deliver a pointer result",
    4751              :                    &rvalue->where);
    4752            1 :       return false;
    4753              :     }
    4754              : 
    4755         7516 :   if (is_init_expr)
    4756              :     {
    4757          245 :       gfc_symbol *sym;
    4758          245 :       bool target;
    4759          245 :       gfc_ref *ref;
    4760              : 
    4761          245 :       if (gfc_is_size_zero_array (rvalue))
    4762              :         {
    4763            1 :           gfc_error ("Zero-sized array detected at %L where an entity with "
    4764              :                      "the TARGET attribute is expected", &rvalue->where);
    4765            1 :           return false;
    4766              :         }
    4767          244 :       else if (!rvalue->symtree)
    4768              :         {
    4769            1 :           gfc_error ("Pointer assignment target in initialization expression "
    4770              :                      "does not have the TARGET attribute at %L",
    4771              :                      &rvalue->where);
    4772            1 :           return false;
    4773              :         }
    4774              : 
    4775          243 :       sym = rvalue->symtree->n.sym;
    4776              : 
    4777          243 :       if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
    4778            0 :         target = CLASS_DATA (sym)->attr.target;
    4779              :       else
    4780          243 :         target = sym->attr.target;
    4781              : 
    4782          243 :       if (!target && !proc_pointer)
    4783              :         {
    4784            4 :           gfc_error ("Pointer assignment target in initialization expression "
    4785              :                      "does not have the TARGET attribute at %L",
    4786              :                      &rvalue->where);
    4787            4 :           return false;
    4788              :         }
    4789              : 
    4790          312 :       for (ref = rvalue->ref; ref; ref = ref->next)
    4791              :         {
    4792           78 :           switch (ref->type)
    4793              :             {
    4794              :             case REF_ARRAY:
    4795           47 :               for (int n = 0; n < ref->u.ar.dimen; n++)
    4796           25 :                 if (!gfc_is_constant_expr (ref->u.ar.start[n])
    4797           23 :                     || !gfc_is_constant_expr (ref->u.ar.end[n])
    4798           47 :                     || !gfc_is_constant_expr (ref->u.ar.stride[n]))
    4799              :                   {
    4800            3 :                     gfc_error ("Every subscript of target specification "
    4801              :                                "at %L must be a constant expression",
    4802              :                                &ref->u.ar.where);
    4803            3 :                     return false;
    4804              :                   }
    4805              :               break;
    4806              : 
    4807            5 :             case REF_SUBSTRING:
    4808            5 :               if (!gfc_is_constant_expr (ref->u.ss.start)
    4809            5 :                   || !gfc_is_constant_expr (ref->u.ss.end))
    4810              :                 {
    4811            2 :                   gfc_error ("Substring starting and ending points of target "
    4812              :                              "specification at %L must be constant expressions",
    4813            2 :                              &ref->u.ss.start->where);
    4814            2 :                   return false;
    4815              :                 }
    4816              :               break;
    4817              : 
    4818              :             default:
    4819              :               break;
    4820              :             }
    4821              :         }
    4822              :     }
    4823              :   else
    4824              :     {
    4825         7271 :       if (!attr.target && !attr.pointer)
    4826              :         {
    4827            9 :           gfc_error ("Pointer assignment target is neither TARGET "
    4828              :                      "nor POINTER at %L", &rvalue->where);
    4829            9 :           return false;
    4830              :         }
    4831              :     }
    4832              : 
    4833         7496 :   if (lvalue->ts.type == BT_CHARACTER)
    4834              :     {
    4835         1253 :       bool t = gfc_check_same_strlen (lvalue, rvalue, "pointer assignment");
    4836         1253 :       if (!t)
    4837              :         return false;
    4838              :     }
    4839              : 
    4840         7494 :   if (is_pure && gfc_impure_variable (rvalue->symtree->n.sym))
    4841              :     {
    4842            3 :       gfc_error ("Bad target in pointer assignment in PURE "
    4843              :                  "procedure at %L", &rvalue->where);
    4844              :     }
    4845              : 
    4846         7494 :   if (is_implicit_pure && gfc_impure_variable (rvalue->symtree->n.sym))
    4847          303 :     gfc_unset_implicit_pure (gfc_current_ns->proc_name);
    4848              : 
    4849         7494 :   if (gfc_has_vector_index (rvalue))
    4850              :     {
    4851            2 :       gfc_error ("Pointer assignment with vector subscript "
    4852              :                  "on rhs at %L", &rvalue->where);
    4853            2 :       return false;
    4854              :     }
    4855              : 
    4856         7492 :   if (attr.is_protected && attr.use_assoc
    4857            4 :       && !(attr.pointer || attr.proc_pointer))
    4858              :     {
    4859            3 :       gfc_error ("Pointer assignment target has PROTECTED "
    4860              :                  "attribute at %L", &rvalue->where);
    4861            3 :       return false;
    4862              :     }
    4863              : 
    4864              :   /* F2008, C725. For PURE also C1283.  */
    4865         7489 :   if (rvalue->expr_type == EXPR_VARIABLE
    4866         7489 :       && gfc_is_coindexed (rvalue))
    4867              :     {
    4868            4 :       gfc_ref *ref;
    4869            5 :       for (ref = rvalue->ref; ref; ref = ref->next)
    4870            5 :         if (ref->type == REF_ARRAY && ref->u.ar.codimen)
    4871              :           {
    4872            4 :             gfc_error ("Data target at %L shall not have a coindex",
    4873              :                        &rvalue->where);
    4874            4 :             return false;
    4875              :           }
    4876              :     }
    4877              : 
    4878              :   /* Warn for assignments of contiguous pointers to targets which is not
    4879              :      contiguous.  Be lenient in the definition of what counts as
    4880              :      contiguous.  */
    4881              : 
    4882         7485 :   if (lhs_attr.contiguous
    4883           74 :       && lhs_attr.dimension > 0)
    4884              :     {
    4885           70 :       if (gfc_is_not_contiguous (rvalue))
    4886              :         {
    4887            6 :           gfc_error ("Assignment to contiguous pointer from "
    4888              :                      "non-contiguous target at %L", &rvalue->where);
    4889            6 :           return false;
    4890              :         }
    4891           64 :       if (!gfc_is_simply_contiguous (rvalue, false, true))
    4892           14 :         gfc_warning (OPT_Wextra, "Assignment to contiguous pointer from "
    4893              :                                  "non-contiguous target at %L", &rvalue->where);
    4894              :     }
    4895              : 
    4896              :   /* Warn if it is the LHS pointer may lives longer than the RHS target.  */
    4897         7479 :   if (warn_target_lifetime
    4898           15 :       && rvalue->expr_type == EXPR_VARIABLE
    4899           15 :       && !rvalue->symtree->n.sym->attr.save
    4900           15 :       && !rvalue->symtree->n.sym->attr.pointer && !attr.pointer
    4901           13 :       && !rvalue->symtree->n.sym->attr.host_assoc
    4902           11 :       && !rvalue->symtree->n.sym->attr.in_common
    4903           11 :       && !rvalue->symtree->n.sym->attr.use_assoc
    4904           11 :       && !rvalue->symtree->n.sym->attr.dummy)
    4905              :     {
    4906            9 :       bool warn;
    4907            9 :       gfc_namespace *ns;
    4908              : 
    4909           18 :       warn = lvalue->symtree->n.sym->attr.dummy
    4910            9 :              || lvalue->symtree->n.sym->attr.result
    4911            8 :              || lvalue->symtree->n.sym->attr.function
    4912            7 :              || (lvalue->symtree->n.sym->attr.host_assoc
    4913            4 :                  && lvalue->symtree->n.sym->ns
    4914            4 :                     != rvalue->symtree->n.sym->ns)
    4915            4 :              || lvalue->symtree->n.sym->attr.use_assoc
    4916           13 :              || lvalue->symtree->n.sym->attr.in_common;
    4917              : 
    4918            9 :       if (rvalue->symtree->n.sym->ns->proc_name
    4919            9 :           && rvalue->symtree->n.sym->ns->proc_name->attr.flavor != FL_PROCEDURE
    4920            3 :           && rvalue->symtree->n.sym->ns->proc_name->attr.flavor != FL_PROGRAM)
    4921              :        for (ns = rvalue->symtree->n.sym->ns;
    4922            5 :             ns && ns->proc_name && ns->proc_name->attr.flavor != FL_PROCEDURE;
    4923              :             ns = ns->parent)
    4924            3 :         if (ns->parent == lvalue->symtree->n.sym->ns)
    4925              :           {
    4926              :             warn = true;
    4927              :             break;
    4928              :           }
    4929              : 
    4930            9 :       if (warn)
    4931            5 :         gfc_warning (OPT_Wtarget_lifetime,
    4932              :                      "Pointer at %L in pointer assignment might outlive the "
    4933              :                      "pointer target", &lvalue->where);
    4934              :     }
    4935              : 
    4936              :   return true;
    4937              : }
    4938              : 
    4939              : 
    4940              : /* Relative of gfc_check_assign() except that the lvalue is a single
    4941              :    symbol.  Used for initialization assignments.  */
    4942              : 
    4943              : bool
    4944       485013 : gfc_check_assign_symbol (gfc_symbol *sym, gfc_component *comp, gfc_expr *rvalue)
    4945              : {
    4946       485013 :   gfc_expr lvalue;
    4947       485013 :   bool r;
    4948       485013 :   bool pointer, proc_pointer;
    4949              : 
    4950       485013 :   memset (&lvalue, '\0', sizeof (gfc_expr));
    4951              : 
    4952       485013 :   if (sym && sym->attr.pdt_template && comp && comp->initializer)
    4953              :     {
    4954          275 :       int i, flag;
    4955          275 :       gfc_expr *param_expr;
    4956          275 :       flag = 0;
    4957              : 
    4958          275 :       if (comp->as && comp->as->type == AS_EXPLICIT
    4959            7 :           && !(comp->ts.type == BT_DERIVED
    4960            6 :                && comp->ts.u.derived->attr.pdt_template))
    4961              :         {
    4962              :           /* Are the bounds of the array parameterized?  */
    4963            2 :           for (i = 0; i < comp->as->rank; i++)
    4964              :             {
    4965            1 :               param_expr = gfc_copy_expr (comp->as->lower[i]);
    4966            1 :               if (gfc_simplify_expr (param_expr, 1)
    4967            1 :                   && param_expr->expr_type != EXPR_CONSTANT)
    4968            0 :                 flag++;
    4969            1 :               gfc_free_expr (param_expr);
    4970            1 :               param_expr = gfc_copy_expr (comp->as->upper[i]);
    4971            1 :               if (gfc_simplify_expr (param_expr, 1)
    4972            1 :                   && param_expr->expr_type != EXPR_CONSTANT)
    4973            1 :                 flag++;
    4974            1 :               gfc_free_expr (param_expr);
    4975              :             }
    4976              :         }
    4977              : 
    4978              :       /* Is the character length parameterized?  */
    4979          275 :       if (comp->ts.type == BT_CHARACTER && comp->ts.u.cl->length)
    4980              :         {
    4981            3 :           param_expr = gfc_copy_expr (comp->ts.u.cl->length);
    4982            3 :           if (gfc_simplify_expr (param_expr, 1)
    4983            3 :               && param_expr->expr_type != EXPR_CONSTANT)
    4984            1 :             flag++;
    4985            3 :           gfc_free_expr (param_expr);
    4986              :         }
    4987              : 
    4988          275 :       if (flag)
    4989              :         {
    4990            2 :           gfc_error ("The component %qs at %L of derived type %qs has "
    4991              :                      "paramterized type or array length parameters, which is "
    4992              :                      "not compatible with a default initializer",
    4993            2 :                       comp->name, &comp->initializer->where, sym->name);
    4994            2 :           return false;
    4995              :         }
    4996              :     }
    4997              : 
    4998       485011 :   lvalue.expr_type = EXPR_VARIABLE;
    4999       485011 :   lvalue.ts = sym->ts;
    5000       485011 :   if (sym->as)
    5001              :     {
    5002        16632 :       lvalue.rank = sym->as->rank;
    5003        16632 :       lvalue.corank = sym->as->corank;
    5004              :     }
    5005       485011 :   lvalue.symtree = XCNEW (gfc_symtree);
    5006       485011 :   lvalue.symtree->n.sym = sym;
    5007       485011 :   lvalue.where = sym->declared_at;
    5008              : 
    5009       485011 :   if (comp)
    5010              :     {
    5011        28763 :       lvalue.ref = gfc_get_ref ();
    5012        28763 :       lvalue.ref->type = REF_COMPONENT;
    5013        28763 :       lvalue.ref->u.c.component = comp;
    5014        28763 :       lvalue.ref->u.c.sym = sym;
    5015        28763 :       lvalue.ts = comp->ts;
    5016        28763 :       lvalue.rank = comp->as ? comp->as->rank : 0;
    5017        28763 :       lvalue.corank = comp->as ? comp->as->corank : 0;
    5018        28763 :       lvalue.where = comp->loc;
    5019         1022 :       pointer = comp->ts.type == BT_CLASS &&  CLASS_DATA (comp)
    5020        29785 :                 ? CLASS_DATA (comp)->attr.class_pointer : comp->attr.pointer;
    5021        28763 :       proc_pointer = comp->attr.proc_pointer;
    5022              :     }
    5023              :   else
    5024              :     {
    5025         2750 :       pointer = sym->ts.type == BT_CLASS &&  CLASS_DATA (sym)
    5026       458998 :                 ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer;
    5027       456248 :       proc_pointer = sym->attr.proc_pointer;
    5028              :     }
    5029              : 
    5030       485011 :   if (pointer || proc_pointer)
    5031         5650 :     r = gfc_check_pointer_assign (&lvalue, rvalue, false, true);
    5032              :   else
    5033              :     {
    5034              :       /* If a conversion function, e.g., __convert_i8_i4, was inserted
    5035              :          into an array constructor, we should check if it can be reduced
    5036              :          as an initialization expression.  */
    5037       479361 :       if (rvalue->expr_type == EXPR_FUNCTION
    5038           61 :           && rvalue->value.function.isym
    5039           30 :           && (rvalue->value.function.isym->conversion == 1))
    5040            0 :         gfc_check_init_expr (rvalue);
    5041              : 
    5042       479361 :       r = gfc_check_assign (&lvalue, rvalue, 1);
    5043              :     }
    5044              : 
    5045       485011 :   free (lvalue.symtree);
    5046       485011 :   free (lvalue.ref);
    5047              : 
    5048       485011 :   if (!r)
    5049              :     return r;
    5050              : 
    5051       484960 :   if (pointer && rvalue->expr_type != EXPR_NULL && !proc_pointer)
    5052              :     {
    5053              :       /* F08:C461. Additional checks for pointer initialization.  */
    5054          227 :       symbol_attribute attr;
    5055          227 :       attr = gfc_expr_attr (rvalue);
    5056          227 :       if (attr.allocatable)
    5057              :         {
    5058            2 :           gfc_error ("Pointer initialization target at %L "
    5059              :                      "must not be ALLOCATABLE", &rvalue->where);
    5060           13 :           return false;
    5061              :         }
    5062          225 :       if (!attr.target || attr.pointer)
    5063              :         {
    5064            1 :           gfc_error ("Pointer initialization target at %L "
    5065              :                      "must have the TARGET attribute", &rvalue->where);
    5066            1 :           return false;
    5067              :         }
    5068              : 
    5069          224 :       if (!attr.save && rvalue->expr_type == EXPR_VARIABLE
    5070           14 :           && rvalue->symtree->n.sym->ns->proc_name
    5071           14 :           && rvalue->symtree->n.sym->ns->proc_name->attr.is_main_program)
    5072              :         {
    5073            4 :           rvalue->symtree->n.sym->ns->proc_name->attr.save = SAVE_IMPLICIT;
    5074            4 :           attr.save = SAVE_IMPLICIT;
    5075              :         }
    5076              : 
    5077          224 :       if (!attr.save)
    5078              :         {
    5079           10 :           gfc_error ("Pointer initialization target at %L "
    5080              :                      "must have the SAVE attribute", &rvalue->where);
    5081           10 :           return false;
    5082              :         }
    5083              :     }
    5084              : 
    5085       484947 :   if (proc_pointer && rvalue->expr_type != EXPR_NULL)
    5086              :     {
    5087              :       /* F08:C1220. Additional checks for procedure pointer initialization.  */
    5088           59 :       symbol_attribute attr = gfc_expr_attr (rvalue);
    5089           59 :       if (attr.proc_pointer)
    5090              :         {
    5091            1 :           gfc_error ("Procedure pointer initialization target at %L "
    5092              :                      "may not be a procedure pointer", &rvalue->where);
    5093            3 :           return false;
    5094              :         }
    5095           58 :       if (attr.proc == PROC_INTERNAL)
    5096              :         {
    5097            1 :           gfc_error ("Internal procedure %qs is invalid in "
    5098              :                      "procedure pointer initialization at %L",
    5099            1 :                      rvalue->symtree->name, &rvalue->where);
    5100            1 :           return false;
    5101              :         }
    5102           57 :       if (attr.dummy)
    5103              :         {
    5104            1 :           gfc_error ("Dummy procedure %qs is invalid in "
    5105              :                      "procedure pointer initialization at %L",
    5106            1 :                      rvalue->symtree->name, &rvalue->where);
    5107            1 :           return false;
    5108              :         }
    5109              :     }
    5110              : 
    5111              :   return true;
    5112              : }
    5113              : 
    5114              : /* Build an initializer for a local integer, real, complex, logical, or
    5115              :    character variable, based on the command line flags finit-local-zero,
    5116              :    finit-integer=, finit-real=, finit-logical=, and finit-character=.
    5117              :    With force, an initializer is ALWAYS generated.  */
    5118              : 
    5119              : static gfc_expr *
    5120       101361 : gfc_build_init_expr (gfc_typespec *ts, locus *where, bool force)
    5121              : {
    5122       101361 :   gfc_expr *init_expr;
    5123              : 
    5124              :   /* Try to build an initializer expression.  */
    5125       101361 :   init_expr = gfc_get_constant_expr (ts->type, ts->kind, where);
    5126              : 
    5127              :   /* If we want to force generation, make sure we default to zero.  */
    5128       101361 :   gfc_init_local_real init_real = flag_init_real;
    5129       101361 :   int init_logical = gfc_option.flag_init_logical;
    5130       101361 :   if (force)
    5131              :     {
    5132          210 :       if (init_real == GFC_INIT_REAL_OFF)
    5133              :         init_real = GFC_INIT_REAL_ZERO;
    5134          210 :       if (init_logical == GFC_INIT_LOGICAL_OFF)
    5135           40 :         init_logical = GFC_INIT_LOGICAL_FALSE;
    5136              :     }
    5137              : 
    5138              :   /* We will only initialize integers, reals, complex, logicals, and
    5139              :      characters, and only if the corresponding command-line flags
    5140              :      were set.  Otherwise, we free init_expr and return null.  */
    5141       101361 :   switch (ts->type)
    5142              :     {
    5143        53423 :     case BT_INTEGER:
    5144        53423 :       if (force || gfc_option.flag_init_integer != GFC_INIT_INTEGER_OFF)
    5145          285 :         mpz_set_si (init_expr->value.integer,
    5146              :                          gfc_option.flag_init_integer_value);
    5147              :       else
    5148              :         {
    5149        53138 :           gfc_free_expr (init_expr);
    5150        53138 :           init_expr = NULL;
    5151              :         }
    5152              :       break;
    5153              : 
    5154        15855 :     case BT_REAL:
    5155        15855 :       switch (init_real)
    5156              :         {
    5157            0 :         case GFC_INIT_REAL_SNAN:
    5158            0 :           init_expr->is_snan = 1;
    5159              :           /* Fall through.  */
    5160           48 :         case GFC_INIT_REAL_NAN:
    5161           48 :           mpfr_set_nan (init_expr->value.real);
    5162           48 :           break;
    5163              : 
    5164           26 :         case GFC_INIT_REAL_INF:
    5165           26 :           mpfr_set_inf (init_expr->value.real, 1);
    5166           26 :           break;
    5167              : 
    5168           24 :         case GFC_INIT_REAL_NEG_INF:
    5169           24 :           mpfr_set_inf (init_expr->value.real, -1);
    5170           24 :           break;
    5171              : 
    5172           63 :         case GFC_INIT_REAL_ZERO:
    5173           63 :           mpfr_set_ui (init_expr->value.real, 0.0, GFC_RND_MODE);
    5174           63 :           break;
    5175              : 
    5176        15694 :         default:
    5177        15694 :           gfc_free_expr (init_expr);
    5178        15694 :           init_expr = NULL;
    5179        15694 :           break;
    5180              :         }
    5181              :       break;
    5182              : 
    5183         1681 :     case BT_COMPLEX:
    5184         1681 :       switch (init_real)
    5185              :         {
    5186            0 :         case GFC_INIT_REAL_SNAN:
    5187            0 :           init_expr->is_snan = 1;
    5188              :           /* Fall through.  */
    5189           12 :         case GFC_INIT_REAL_NAN:
    5190           12 :           mpfr_set_nan (mpc_realref (init_expr->value.complex));
    5191           12 :           mpfr_set_nan (mpc_imagref (init_expr->value.complex));
    5192           12 :           break;
    5193              : 
    5194            0 :         case GFC_INIT_REAL_INF:
    5195            0 :           mpfr_set_inf (mpc_realref (init_expr->value.complex), 1);
    5196            0 :           mpfr_set_inf (mpc_imagref (init_expr->value.complex), 1);
    5197            0 :           break;
    5198              : 
    5199            0 :         case GFC_INIT_REAL_NEG_INF:
    5200            0 :           mpfr_set_inf (mpc_realref (init_expr->value.complex), -1);
    5201            0 :           mpfr_set_inf (mpc_imagref (init_expr->value.complex), -1);
    5202            0 :           break;
    5203              : 
    5204           24 :         case GFC_INIT_REAL_ZERO:
    5205           24 :           mpc_set_ui (init_expr->value.complex, 0, GFC_MPC_RND_MODE);
    5206           24 :           break;
    5207              : 
    5208         1645 :         default:
    5209         1645 :           gfc_free_expr (init_expr);
    5210         1645 :           init_expr = NULL;
    5211         1645 :           break;
    5212              :         }
    5213              :       break;
    5214              : 
    5215         4948 :     case BT_LOGICAL:
    5216         4948 :       if (init_logical == GFC_INIT_LOGICAL_FALSE)
    5217           39 :         init_expr->value.logical = 0;
    5218         4909 :       else if (init_logical == GFC_INIT_LOGICAL_TRUE)
    5219           26 :         init_expr->value.logical = 1;
    5220              :       else
    5221              :         {
    5222         4883 :           gfc_free_expr (init_expr);
    5223         4883 :           init_expr = NULL;
    5224              :         }
    5225              :       break;
    5226              : 
    5227         9628 :     case BT_CHARACTER:
    5228              :       /* For characters, the length must be constant in order to
    5229              :          create a default initializer.  */
    5230         9628 :       if ((force || gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON)
    5231           83 :           && ts->u.cl->length
    5232           83 :           && ts->u.cl->length->expr_type == EXPR_CONSTANT)
    5233              :         {
    5234           76 :           HOST_WIDE_INT char_len = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
    5235           76 :           init_expr->value.character.length = char_len;
    5236           76 :           init_expr->value.character.string = gfc_get_wide_string (char_len+1);
    5237          320 :           for (size_t i = 0; i < (size_t) char_len; i++)
    5238          244 :             init_expr->value.character.string[i]
    5239          244 :               = (unsigned char) gfc_option.flag_init_character_value;
    5240              :         }
    5241              :       else
    5242              :         {
    5243         9552 :           gfc_free_expr (init_expr);
    5244         9552 :           init_expr = NULL;
    5245              :         }
    5246         9552 :       if (!init_expr
    5247         9552 :           && (force || gfc_option.flag_init_character == GFC_INIT_CHARACTER_ON)
    5248            7 :           && ts->u.cl->length && flag_max_stack_var_size != 0)
    5249              :         {
    5250            6 :           gfc_actual_arglist *arg;
    5251            6 :           init_expr = gfc_get_expr ();
    5252            6 :           init_expr->where = *where;
    5253            6 :           init_expr->ts = *ts;
    5254            6 :           init_expr->expr_type = EXPR_FUNCTION;
    5255           12 :           init_expr->value.function.isym =
    5256            6 :                 gfc_intrinsic_function_by_id (GFC_ISYM_REPEAT);
    5257            6 :           init_expr->value.function.name = "repeat";
    5258            6 :           arg = gfc_get_actual_arglist ();
    5259            6 :           arg->expr = gfc_get_character_expr (ts->kind, where, NULL, 1);
    5260            6 :           arg->expr->value.character.string[0] =
    5261            6 :             gfc_option.flag_init_character_value;
    5262            6 :           arg->next = gfc_get_actual_arglist ();
    5263            6 :           arg->next->expr = gfc_copy_expr (ts->u.cl->length);
    5264            6 :           init_expr->value.function.actual = arg;
    5265              :         }
    5266              :       break;
    5267              : 
    5268        15826 :     default:
    5269        15826 :      gfc_free_expr (init_expr);
    5270        15826 :      init_expr = NULL;
    5271              :     }
    5272              : 
    5273       101361 :   return init_expr;
    5274              : }
    5275              : 
    5276              : /* Invoke gfc_build_init_expr to create an initializer expression, but do not
    5277              :  * require that an expression be built.  */
    5278              : 
    5279              : gfc_expr *
    5280       101151 : gfc_build_default_init_expr (gfc_typespec *ts, locus *where)
    5281              : {
    5282       101151 :   return gfc_build_init_expr (ts, where, false);
    5283              : }
    5284              : 
    5285              : /* Apply an initialization expression to a typespec. Can be used for symbols or
    5286              :    components. Similar to add_init_expr_to_sym in decl.cc; could probably be
    5287              :    combined with some effort.  */
    5288              : 
    5289              : void
    5290        17984 : gfc_apply_init (gfc_typespec *ts, symbol_attribute *attr, gfc_expr *init)
    5291              : {
    5292        17984 :   if (ts->type == BT_CHARACTER && !attr->pointer && init
    5293          357 :       && ts->u.cl
    5294          357 :       && ts->u.cl->length
    5295          357 :       && ts->u.cl->length->expr_type == EXPR_CONSTANT
    5296          353 :       && ts->u.cl->length->ts.type == BT_INTEGER)
    5297              :     {
    5298          353 :       HOST_WIDE_INT len = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
    5299              : 
    5300          353 :       if (init->expr_type == EXPR_CONSTANT)
    5301          246 :         gfc_set_constant_character_len (len, init, -1);
    5302          107 :       else if (init
    5303          107 :                && init->ts.type == BT_CHARACTER
    5304          102 :                && init->ts.u.cl && init->ts.u.cl->length
    5305          102 :                && mpz_cmp (ts->u.cl->length->value.integer,
    5306          102 :                            init->ts.u.cl->length->value.integer))
    5307              :         {
    5308            0 :           gfc_constructor *ctor;
    5309            0 :           ctor = gfc_constructor_first (init->value.constructor);
    5310              : 
    5311            0 :           if (ctor)
    5312              :             {
    5313            0 :               bool has_ts = (init->ts.u.cl
    5314            0 :                              && init->ts.u.cl->length_from_typespec);
    5315              : 
    5316              :               /* Remember the length of the first element for checking
    5317              :                  that all elements *in the constructor* have the same
    5318              :                  length.  This need not be the length of the LHS!  */
    5319            0 :               gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
    5320            0 :               gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
    5321            0 :               gfc_charlen_t first_len = ctor->expr->value.character.length;
    5322              : 
    5323            0 :               for ( ; ctor; ctor = gfc_constructor_next (ctor))
    5324            0 :                 if (ctor->expr->expr_type == EXPR_CONSTANT)
    5325              :                 {
    5326            0 :                   gfc_set_constant_character_len (len, ctor->expr,
    5327              :                                                   has_ts ? -1 : first_len);
    5328            0 :                   if (!ctor->expr->ts.u.cl)
    5329            0 :                     ctor->expr->ts.u.cl
    5330            0 :                       = gfc_new_charlen (gfc_current_ns, ts->u.cl);
    5331              :                   else
    5332            0 :                     ctor->expr->ts.u.cl->length
    5333            0 :                       = gfc_copy_expr (ts->u.cl->length);
    5334              :                 }
    5335              :             }
    5336              :         }
    5337              :     }
    5338        17984 : }
    5339              : 
    5340              : 
    5341              : /* Check whether an expression is a structure constructor and whether it has
    5342              :    other values than NULL.  */
    5343              : 
    5344              : static bool
    5345          843 : is_non_empty_structure_constructor (gfc_expr * e)
    5346              : {
    5347          843 :   if (e->expr_type != EXPR_STRUCTURE)
    5348              :     return false;
    5349              : 
    5350          843 :   gfc_constructor *cons = gfc_constructor_first (e->value.constructor);
    5351         2242 :   while (cons)
    5352              :     {
    5353          961 :       if (!cons->expr || cons->expr->expr_type != EXPR_NULL)
    5354              :         return true;
    5355          556 :       cons = gfc_constructor_next (cons);
    5356              :     }
    5357              :   return false;
    5358              : }
    5359              : 
    5360              : 
    5361              : /* Check for default initializer; sym->value is not enough
    5362              :    as it is also set for EXPR_NULL of allocatables.  */
    5363              : 
    5364              : bool
    5365         7142 : gfc_has_default_initializer (gfc_symbol *der)
    5366              : {
    5367         7142 :   static hash_set<gfc_symbol *> seen_derived_types;
    5368         7142 :   gfc_component *c;
    5369              :   /* The rewrite to a result variable and breaks is only needed, because
    5370              :      there is no scope_guard in C++ yet.  */
    5371         7142 :   bool result = false;
    5372              : 
    5373         7142 :   gcc_assert (gfc_fl_struct (der->attr.flavor));
    5374         7142 :   seen_derived_types.add (der);
    5375        14655 :   for (c = der->components; c; c = c->next)
    5376         7373 :     if (gfc_bt_struct (c->ts.type)
    5377         9068 :         && !seen_derived_types.contains (c->ts.u.derived))
    5378              :       {
    5379         1484 :         if (!c->attr.pointer && !c->attr.proc_pointer
    5380         1484 :             && !(c->attr.allocatable && der == c->ts.u.derived)
    5381         3098 :             && ((c->initializer
    5382          843 :                  && is_non_empty_structure_constructor (c->initializer))
    5383         1079 :                 || gfc_has_default_initializer (c->ts.u.derived)))
    5384              :           {
    5385              :             result = true;
    5386              :             break;
    5387              :           }
    5388         1151 :         if (c->attr.pointer && c->initializer)
    5389              :           {
    5390              :             result = true;
    5391              :             break;
    5392              :           }
    5393              :       }
    5394              :     else
    5395              :       {
    5396         7450 :         if (c->initializer)
    5397              :           {
    5398              :             result = true;
    5399              :             break;
    5400              :           }
    5401              :       }
    5402              : 
    5403         7142 :   seen_derived_types.remove (der);
    5404         7142 :   return result;
    5405              : }
    5406              : 
    5407              : 
    5408              : /*
    5409              :    Generate an initializer expression which initializes the entirety of a union.
    5410              :    A normal structure constructor is insufficient without undue effort, because
    5411              :    components of maps may be oddly aligned/overlapped. (For example if a
    5412              :    character is initialized from one map overtop a real from the other, only one
    5413              :    byte of the real is actually initialized.)  Unfortunately we don't know the
    5414              :    size of the union right now, so we can't generate a proper initializer, but
    5415              :    we use a NULL expr as a placeholder and do the right thing later in
    5416              :    gfc_trans_subcomponent_assign.
    5417              :  */
    5418              : static gfc_expr *
    5419           15 : generate_union_initializer (gfc_component *un)
    5420              : {
    5421           15 :   if (un == NULL || un->ts.type != BT_UNION)
    5422              :     return NULL;
    5423              : 
    5424           15 :   gfc_expr *placeholder = gfc_get_null_expr (&un->loc);
    5425           15 :   placeholder->ts = un->ts;
    5426           15 :   return placeholder;
    5427              : }
    5428              : 
    5429              : 
    5430              : /* Get the user-specified initializer for a union, if any. This means the user
    5431              :    has said to initialize component(s) of a map.  For simplicity's sake we
    5432              :    only allow the user to initialize the first map.  We don't have to worry
    5433              :    about overlapping initializers as they are released early in resolution (see
    5434              :    resolve_fl_struct).   */
    5435              : 
    5436              : static gfc_expr *
    5437           15 : get_union_initializer (gfc_symbol *union_type, gfc_component **map_p)
    5438              : {
    5439           15 :   gfc_component *map;
    5440           15 :   gfc_expr *init=NULL;
    5441              : 
    5442           15 :   if (!union_type || union_type->attr.flavor != FL_UNION)
    5443              :     return NULL;
    5444              : 
    5445           48 :   for (map = union_type->components; map; map = map->next)
    5446              :     {
    5447           33 :       if (gfc_has_default_initializer (map->ts.u.derived))
    5448              :         {
    5449            0 :           init = gfc_default_initializer (&map->ts);
    5450            0 :           if (map_p)
    5451            0 :             *map_p = map;
    5452              :           break;
    5453              :         }
    5454              :     }
    5455              : 
    5456           15 :   if (map_p && !init)
    5457           15 :     *map_p = NULL;
    5458              : 
    5459              :   return init;
    5460              : }
    5461              : 
    5462              : static bool
    5463       151102 : class_allocatable (gfc_component *comp)
    5464              : {
    5465         2954 :   return comp->ts.type == BT_CLASS && comp->attr.class_ok && CLASS_DATA (comp)
    5466       154055 :     && CLASS_DATA (comp)->attr.allocatable;
    5467              : }
    5468              : 
    5469              : static bool
    5470          268 : class_pointer (gfc_component *comp)
    5471              : {
    5472            1 :   return comp->ts.type == BT_CLASS && comp->attr.class_ok && CLASS_DATA (comp)
    5473          269 :     && CLASS_DATA (comp)->attr.pointer;
    5474              : }
    5475              : 
    5476              : static bool
    5477       169181 : comp_allocatable (gfc_component *comp)
    5478              : {
    5479       169181 :   return comp->attr.allocatable || class_allocatable (comp);
    5480              : }
    5481              : 
    5482              : static bool
    5483          271 : comp_pointer (gfc_component *comp)
    5484              : {
    5485          271 :   return comp->attr.pointer
    5486          268 :     || comp->attr.proc_pointer
    5487          268 :     || comp->attr.class_pointer
    5488          539 :     || class_pointer (comp);
    5489              : }
    5490              : 
    5491              : /* Fetch or generate an initializer for the given component.
    5492              :    Only generate an initializer if generate is true.  */
    5493              : 
    5494              : static gfc_expr *
    5495       115962 : component_initializer (gfc_component *c, bool generate)
    5496              : {
    5497       115962 :   gfc_expr *init = NULL;
    5498              : 
    5499              :   /* Allocatable components always get EXPR_NULL.
    5500              :      Pointer components are only initialized when generating, and only if they
    5501              :      do not already have an initializer.  */
    5502       115962 :   if (comp_allocatable (c) || (generate && comp_pointer (c) && !c->initializer))
    5503              :     {
    5504        12063 :       init = gfc_get_null_expr (&c->loc);
    5505        12063 :       init->ts = c->ts;
    5506        12063 :       return init;
    5507              :     }
    5508              : 
    5509              :   /* See if we can find the initializer immediately.  */
    5510       103899 :   if (c->initializer || !generate)
    5511              :     return c->initializer;
    5512              : 
    5513              :   /* Recursively handle derived type components.  */
    5514          243 :   else if (c->ts.type == BT_DERIVED || c->ts.type == BT_CLASS)
    5515           18 :     init = gfc_generate_initializer (&c->ts, true);
    5516              : 
    5517          225 :   else if (c->ts.type == BT_UNION && c->ts.u.derived->components)
    5518              :     {
    5519           15 :       gfc_component *map = NULL;
    5520           15 :       gfc_constructor *ctor;
    5521           15 :       gfc_expr *user_init;
    5522              : 
    5523              :       /* If we don't have a user initializer and we aren't generating one, this
    5524              :          union has no initializer.  */
    5525           15 :       user_init = get_union_initializer (c->ts.u.derived, &map);
    5526           15 :       if (!user_init && !generate)
    5527              :         return NULL;
    5528              : 
    5529              :       /* Otherwise use a structure constructor.  */
    5530           15 :       init = gfc_get_structure_constructor_expr (c->ts.type, c->ts.kind,
    5531              :                                                  &c->loc);
    5532           15 :       init->ts = c->ts;
    5533              : 
    5534              :       /* If we are to generate an initializer for the union, add a constructor
    5535              :          which initializes the whole union first.  */
    5536           15 :       if (generate)
    5537              :         {
    5538           15 :           ctor = gfc_constructor_get ();
    5539           15 :           ctor->expr = generate_union_initializer (c);
    5540           15 :           gfc_constructor_append (&init->value.constructor, ctor);
    5541              :         }
    5542              : 
    5543              :       /* If we found an initializer in one of our maps, apply it.  Note this
    5544              :          is applied _after_ the entire-union initializer above if any.  */
    5545           15 :       if (user_init)
    5546              :         {
    5547            0 :           ctor = gfc_constructor_get ();
    5548            0 :           ctor->expr = user_init;
    5549            0 :           ctor->n.component = map;
    5550            0 :           gfc_constructor_append (&init->value.constructor, ctor);
    5551              :         }
    5552           15 :     }
    5553              : 
    5554              :   /* Treat simple components like locals.  */
    5555              :   else
    5556              :     {
    5557              :       /* We MUST give an initializer, so force generation.  */
    5558          210 :       init = gfc_build_init_expr (&c->ts, &c->loc, true);
    5559          210 :       gfc_apply_init (&c->ts, &c->attr, init);
    5560              :     }
    5561              : 
    5562              :   return init;
    5563              : }
    5564              : 
    5565              : 
    5566              : /* Get an expression for a default initializer of a derived type.  */
    5567              : 
    5568              : gfc_expr *
    5569        27054 : gfc_default_initializer (gfc_typespec *ts)
    5570              : {
    5571        27054 :   return gfc_generate_initializer (ts, false);
    5572              : }
    5573              : 
    5574              : /* Generate an initializer expression for an iso_c_binding type
    5575              :    such as c_[fun]ptr. The appropriate initializer is c_null_[fun]ptr.  */
    5576              : 
    5577              : static gfc_expr *
    5578            3 : generate_isocbinding_initializer (gfc_symbol *derived)
    5579              : {
    5580              :   /* The initializers have already been built into the c_null_[fun]ptr symbols
    5581              :      from gen_special_c_interop_ptr.  */
    5582            3 :   gfc_symtree *npsym = NULL;
    5583            3 :   if (0 == strcmp (derived->name, "c_ptr"))
    5584            2 :     gfc_find_sym_tree ("c_null_ptr", gfc_current_ns, true, &npsym);
    5585            1 :   else if (0 == strcmp (derived->name, "c_funptr"))
    5586            1 :     gfc_find_sym_tree ("c_null_funptr", gfc_current_ns, true, &npsym);
    5587              :   else
    5588            0 :     gfc_internal_error ("generate_isocbinding_initializer(): bad iso_c_binding"
    5589              :                         " type, expected %<c_ptr%> or %<c_funptr%>");
    5590            3 :   if (npsym)
    5591              :     {
    5592            3 :       gfc_expr *init = gfc_copy_expr (npsym->n.sym->value);
    5593            3 :       init->symtree = npsym;
    5594            3 :       init->ts.is_iso_c = true;
    5595            3 :       return init;
    5596              :     }
    5597              : 
    5598              :   return NULL;
    5599              : }
    5600              : 
    5601              : /* Get or generate an expression for a default initializer of a derived type.
    5602              :    If -finit-derived is specified, generate default initialization expressions
    5603              :    for components that lack them when generate is set.  */
    5604              : 
    5605              : gfc_expr *
    5606        58120 : gfc_generate_initializer (gfc_typespec *ts, bool generate)
    5607              : {
    5608        58120 :   gfc_expr *init, *tmp;
    5609        58120 :   gfc_component *comp;
    5610              : 
    5611        58120 :   generate = flag_init_derived && generate;
    5612              : 
    5613        58120 :   if (ts->u.derived->ts.is_iso_c && generate)
    5614            3 :     return generate_isocbinding_initializer (ts->u.derived);
    5615              : 
    5616              :   /* See if we have a default initializer in this, but not in nested
    5617              :      types (otherwise we could use gfc_has_default_initializer()).
    5618              :      We don't need to check if we are going to generate them.  */
    5619        58117 :   comp = ts->u.derived->components;
    5620        58117 :   if (!generate)
    5621              :     {
    5622       102845 :       for (; comp; comp = comp->next)
    5623        73620 :         if (comp->initializer || comp_allocatable (comp))
    5624              :           break;
    5625              :     }
    5626              : 
    5627        58117 :   if (!comp)
    5628              :     return NULL;
    5629              : 
    5630        28892 :   init = gfc_get_structure_constructor_expr (ts->type, ts->kind,
    5631              :                                              &ts->u.derived->declared_at);
    5632        28892 :   init->ts = *ts;
    5633              : 
    5634       144853 :   for (comp = ts->u.derived->components; comp; comp = comp->next)
    5635              :     {
    5636       115962 :       gfc_constructor *ctor = gfc_constructor_get();
    5637              : 
    5638              :       /* Fetch or generate an initializer for the component.  */
    5639       115962 :       tmp = component_initializer (comp, generate);
    5640       115962 :       if (tmp)
    5641              :         {
    5642              :           /* Save the component ref for STRUCTUREs and UNIONs.  */
    5643       105453 :           if (ts->u.derived->attr.flavor == FL_STRUCT
    5644       105133 :               || ts->u.derived->attr.flavor == FL_UNION)
    5645          343 :             ctor->n.component = comp;
    5646              : 
    5647              :           /* If the initializer was not generated, we need a copy.  */
    5648       105453 :           ctor->expr = comp->initializer ? gfc_copy_expr (tmp) : tmp;
    5649       105453 :           if ((comp->ts.type != tmp->ts.type || comp->ts.kind != tmp->ts.kind)
    5650        18040 :               && !comp->attr.pointer && !comp->attr.proc_pointer)
    5651              :             {
    5652          273 :               bool val;
    5653          273 :               val = gfc_convert_type_warn (ctor->expr, &comp->ts, 1, false);
    5654          273 :               if (val == false)
    5655              :                 return NULL;
    5656              :             }
    5657              :         }
    5658              : 
    5659       115961 :       gfc_constructor_append (&init->value.constructor, ctor);
    5660              :     }
    5661              : 
    5662              :   return init;
    5663              : }
    5664              : 
    5665              : 
    5666              : /* Given a symbol, create an expression node with that symbol as a
    5667              :    variable. If the symbol is array valued, setup a reference of the
    5668              :    whole array.  */
    5669              : 
    5670              : gfc_expr *
    5671        13645 : gfc_get_variable_expr (gfc_symtree *var)
    5672              : {
    5673        13645 :   gfc_expr *e;
    5674              : 
    5675        13645 :   e = gfc_get_expr ();
    5676        13645 :   e->expr_type = EXPR_VARIABLE;
    5677        13645 :   e->symtree = var;
    5678        13645 :   e->ts = var->n.sym->ts;
    5679              : 
    5680        13645 :   if (var->n.sym->attr.flavor != FL_PROCEDURE
    5681         9609 :       && ((var->n.sym->as != NULL && var->n.sym->ts.type != BT_CLASS)
    5682         7552 :            || (var->n.sym->ts.type == BT_CLASS && var->n.sym->ts.u.derived
    5683         4253 :                && CLASS_DATA (var->n.sym)
    5684         4253 :                && CLASS_DATA (var->n.sym)->as)))
    5685              :     {
    5686         5589 :       gfc_array_spec *as = var->n.sym->ts.type == BT_CLASS
    5687         3823 :                              ? CLASS_DATA (var->n.sym)->as
    5688              :                              : var->n.sym->as;
    5689         3823 :       e->rank = as->rank;
    5690         3823 :       e->corank = as->corank;
    5691         3823 :       e->ref = gfc_get_ref ();
    5692         3823 :       e->ref->type = REF_ARRAY;
    5693         3823 :       e->ref->u.ar.type = AR_FULL;
    5694         3823 :       e->ref->u.ar.as = gfc_copy_array_spec (as);
    5695              :     }
    5696              : 
    5697        13645 :   return e;
    5698              : }
    5699              : 
    5700              : 
    5701              : /* Adds a full array reference to an expression, as needed.  */
    5702              : 
    5703              : void
    5704        40319 : gfc_add_full_array_ref (gfc_expr *e, gfc_array_spec *as)
    5705              : {
    5706        40319 :   gfc_ref *ref;
    5707        40333 :   for (ref = e->ref; ref; ref = ref->next)
    5708          193 :     if (!ref->next)
    5709              :       break;
    5710        40319 :   if (ref)
    5711              :     {
    5712          179 :       ref->next = gfc_get_ref ();
    5713          179 :       ref = ref->next;
    5714              :     }
    5715              :   else
    5716              :     {
    5717        40140 :       e->ref = gfc_get_ref ();
    5718        40140 :       ref = e->ref;
    5719              :     }
    5720        40319 :   ref->type = REF_ARRAY;
    5721        40319 :   ref->u.ar.type = AR_FULL;
    5722        40319 :   ref->u.ar.dimen = e->rank;
    5723              :   /* Do not set the corank here, or resolve will not be able to set correct
    5724              :      dimen-types for the coarray.  */
    5725        40319 :   ref->u.ar.where = e->where;
    5726        40319 :   ref->u.ar.as = as;
    5727        40319 : }
    5728              : 
    5729              : 
    5730              : gfc_expr *
    5731       173305 : gfc_lval_expr_from_sym (gfc_symbol *sym)
    5732              : {
    5733       173305 :   gfc_expr *lval;
    5734       173305 :   gfc_array_spec *as;
    5735       173305 :   lval = gfc_get_expr ();
    5736       173305 :   lval->expr_type = EXPR_VARIABLE;
    5737       173305 :   lval->where = sym->declared_at;
    5738       173305 :   lval->ts = sym->ts;
    5739       173305 :   lval->symtree = gfc_find_symtree (sym->ns->sym_root, sym->name);
    5740              : 
    5741              :   /* It will always be a full array.  */
    5742       173305 :   as = IS_CLASS_ARRAY (sym) ? CLASS_DATA (sym)->as : sym->as;
    5743       173305 :   lval->rank = as ? as->rank : 0;
    5744       173305 :   lval->corank = as ? as->corank : 0;
    5745       173305 :   if (lval->rank || lval->corank)
    5746        38818 :     gfc_add_full_array_ref (lval, as);
    5747       173305 :   return lval;
    5748              : }
    5749              : 
    5750              : 
    5751              : /* Returns the array_spec of a full array expression.  A NULL is
    5752              :    returned otherwise.  */
    5753              : gfc_array_spec *
    5754        25834 : gfc_get_full_arrayspec_from_expr (gfc_expr *expr)
    5755              : {
    5756        25834 :   gfc_array_spec *as;
    5757        25834 :   gfc_ref *ref;
    5758              : 
    5759        25834 :   if (expr->rank == 0)
    5760              :     return NULL;
    5761              : 
    5762              :   /* Follow any component references.  */
    5763        25834 :   if (expr->expr_type == EXPR_VARIABLE
    5764        25834 :       || expr->expr_type == EXPR_CONSTANT)
    5765              :     {
    5766        19348 :       if (expr->symtree)
    5767        19348 :         as = expr->symtree->n.sym->as;
    5768              :       else
    5769              :         as = NULL;
    5770              : 
    5771        40583 :       for (ref = expr->ref; ref; ref = ref->next)
    5772              :         {
    5773        21235 :           switch (ref->type)
    5774              :             {
    5775         1718 :             case REF_COMPONENT:
    5776         1718 :               as = ref->u.c.component->as;
    5777         1718 :               continue;
    5778              : 
    5779           24 :             case REF_SUBSTRING:
    5780           24 :             case REF_INQUIRY:
    5781           24 :               continue;
    5782              : 
    5783        19493 :             case REF_ARRAY:
    5784        19493 :               {
    5785        19493 :                 switch (ref->u.ar.type)
    5786              :                   {
    5787         2179 :                   case AR_ELEMENT:
    5788         2179 :                   case AR_SECTION:
    5789         2179 :                   case AR_UNKNOWN:
    5790         2179 :                     as = NULL;
    5791         2179 :                     continue;
    5792              : 
    5793              :                   case AR_FULL:
    5794              :                     break;
    5795              :                   }
    5796              :                 break;
    5797              :               }
    5798              :             }
    5799              :         }
    5800              :     }
    5801              :   else
    5802              :     as = NULL;
    5803              : 
    5804              :   return as;
    5805              : }
    5806              : 
    5807              : 
    5808              : /* General expression traversal function.  */
    5809              : 
    5810              : bool
    5811       969897 : gfc_traverse_expr (gfc_expr *expr, gfc_symbol *sym,
    5812              :                    bool (*func)(gfc_expr *, gfc_symbol *, int*),
    5813              :                    int f)
    5814              : {
    5815       969897 :   gfc_array_ref ar;
    5816       969897 :   gfc_ref *ref;
    5817       969897 :   gfc_actual_arglist *args;
    5818       969897 :   gfc_constructor *c;
    5819       969897 :   int i;
    5820              : 
    5821       969897 :   if (!expr)
    5822              :     return false;
    5823              : 
    5824       472085 :   if ((*func) (expr, sym, &f))
    5825              :     return true;
    5826              : 
    5827              :   /* Descend into length type parameter of character expressions only for
    5828              :      non-negative f.  */
    5829       465160 :   if (f >= 0
    5830       442658 :       && expr->ts.type == BT_CHARACTER
    5831        11792 :       && expr->ts.u.cl
    5832         4262 :       && expr->ts.u.cl->length
    5833         2250 :       && expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
    5834       466089 :       && gfc_traverse_expr (expr->ts.u.cl->length, sym, func, f))
    5835              :     return true;
    5836              : 
    5837       465159 :   switch (expr->expr_type)
    5838              :     {
    5839        18362 :     case EXPR_PPC:
    5840        18362 :     case EXPR_COMPCALL:
    5841        18362 :     case EXPR_FUNCTION:
    5842        42881 :       for (args = expr->value.function.actual; args; args = args->next)
    5843              :         {
    5844        24627 :           if (gfc_traverse_expr (args->expr, sym, func, f))
    5845              :             return true;
    5846              :         }
    5847              :       break;
    5848              : 
    5849              :     case EXPR_VARIABLE:
    5850              :     case EXPR_CONSTANT:
    5851              :     case EXPR_NULL:
    5852              :     case EXPR_SUBSTRING:
    5853              :       break;
    5854              : 
    5855         4698 :     case EXPR_STRUCTURE:
    5856         4698 :     case EXPR_ARRAY:
    5857         4698 :       for (c = gfc_constructor_first (expr->value.constructor);
    5858        28881 :            c; c = gfc_constructor_next (c))
    5859              :         {
    5860        24183 :           if (gfc_traverse_expr (c->expr, sym, func, f))
    5861              :             return true;
    5862        24183 :           if (c->iterator)
    5863              :             {
    5864          493 :               if (gfc_traverse_expr (c->iterator->var, sym, func, f))
    5865              :                 return true;
    5866          493 :               if (gfc_traverse_expr (c->iterator->start, sym, func, f))
    5867              :                 return true;
    5868          493 :               if (gfc_traverse_expr (c->iterator->end, sym, func, f))
    5869              :                 return true;
    5870          493 :               if (gfc_traverse_expr (c->iterator->step, sym, func, f))
    5871              :                 return true;
    5872              :             }
    5873              :         }
    5874              :       break;
    5875              : 
    5876         9766 :     case EXPR_OP:
    5877         9766 :       if (gfc_traverse_expr (expr->value.op.op1, sym, func, f))
    5878              :         return true;
    5879         8052 :       if (gfc_traverse_expr (expr->value.op.op2, sym, func, f))
    5880              :         return true;
    5881              :       break;
    5882              : 
    5883            6 :     case EXPR_CONDITIONAL:
    5884            6 :       if (gfc_traverse_expr (expr->value.conditional.condition, sym, func, f))
    5885              :         return true;
    5886            6 :       if (gfc_traverse_expr (expr->value.conditional.true_expr, sym, func, f))
    5887              :         return true;
    5888            6 :       if (gfc_traverse_expr (expr->value.conditional.false_expr, sym, func, f))
    5889              :         return true;
    5890              :       break;
    5891              : 
    5892            0 :     default:
    5893            0 :       gcc_unreachable ();
    5894       462957 :       break;
    5895              :     }
    5896              : 
    5897       462957 :   ref = expr->ref;
    5898       474427 :   while (ref != NULL)
    5899              :     {
    5900        15499 :       switch (ref->type)
    5901              :         {
    5902        13700 :         case  REF_ARRAY:
    5903        13700 :           ar = ref->u.ar;
    5904       164114 :           for (i = 0; i < GFC_MAX_DIMENSIONS; i++)
    5905              :             {
    5906       154265 :               if (gfc_traverse_expr (ar.start[i], sym, func, f))
    5907              :                 return true;
    5908       150415 :               if (gfc_traverse_expr (ar.end[i], sym, func, f))
    5909              :                 return true;
    5910       150414 :               if (gfc_traverse_expr (ar.stride[i], sym, func, f))
    5911              :                 return true;
    5912              :             }
    5913              :           break;
    5914              : 
    5915          805 :         case REF_SUBSTRING:
    5916          805 :           if (gfc_traverse_expr (ref->u.ss.start, sym, func, f))
    5917              :             return true;
    5918          632 :           if (gfc_traverse_expr (ref->u.ss.end, sym, func, f))
    5919              :             return true;
    5920              :           break;
    5921              : 
    5922          990 :         case REF_COMPONENT:
    5923          990 :           if (f >= 0
    5924          975 :               && ref->u.c.component->ts.type == BT_CHARACTER
    5925           91 :               && ref->u.c.component->ts.u.cl
    5926           91 :               && ref->u.c.component->ts.u.cl->length
    5927           91 :               && ref->u.c.component->ts.u.cl->length->expr_type
    5928              :               != EXPR_CONSTANT
    5929          990 :               && gfc_traverse_expr (ref->u.c.component->ts.u.cl->length,
    5930              :                                     sym, func, f))
    5931              :             return true;
    5932              : 
    5933          990 :           if (ref->u.c.component->as)
    5934          432 :             for (i = 0; i < ref->u.c.component->as->rank
    5935          824 :                             + ref->u.c.component->as->corank; i++)
    5936              :               {
    5937          432 :                 if (gfc_traverse_expr (ref->u.c.component->as->lower[i],
    5938              :                                        sym, func, f))
    5939              :                   return true;
    5940          432 :                 if (gfc_traverse_expr (ref->u.c.component->as->upper[i],
    5941              :                                        sym, func, f))
    5942              :                   return true;
    5943              :               }
    5944              :           break;
    5945              : 
    5946              :         case REF_INQUIRY:
    5947              :           return false;
    5948              : 
    5949            0 :         default:
    5950            0 :           gcc_unreachable ();
    5951              :         }
    5952        11470 :       ref = ref->next;
    5953              :     }
    5954              :   return false;
    5955              : }
    5956              : 
    5957              : /* Traverse expr, marking all EXPR_VARIABLE symbols referenced.  */
    5958              : 
    5959              : static bool
    5960         3927 : expr_set_symbols_referenced (gfc_expr *expr,
    5961              :                              gfc_symbol *sym ATTRIBUTE_UNUSED,
    5962              :                              int *f ATTRIBUTE_UNUSED)
    5963              : {
    5964         3927 :   if (expr->expr_type != EXPR_VARIABLE)
    5965              :     return false;
    5966          933 :   gfc_set_sym_referenced (expr->symtree->n.sym);
    5967          933 :   return false;
    5968              : }
    5969              : 
    5970              : void
    5971         1238 : gfc_expr_set_symbols_referenced (gfc_expr *expr)
    5972              : {
    5973         1238 :   gfc_traverse_expr (expr, NULL, expr_set_symbols_referenced, 0);
    5974         1238 : }
    5975              : 
    5976              : 
    5977              : /* Determine if an expression is a procedure pointer component and return
    5978              :    the component in that case.  Otherwise return NULL.  */
    5979              : 
    5980              : gfc_component *
    5981      3268853 : gfc_get_proc_ptr_comp (gfc_expr *expr)
    5982              : {
    5983      3268853 :   gfc_ref *ref;
    5984              : 
    5985      3268853 :   if (!expr || !expr->ref)
    5986              :     return NULL;
    5987              : 
    5988              :   ref = expr->ref;
    5989       251017 :   while (ref->next)
    5990              :     ref = ref->next;
    5991              : 
    5992       226257 :   if (ref->type == REF_COMPONENT
    5993        20205 :       && ref->u.c.component->attr.proc_pointer)
    5994         9022 :     return ref->u.c.component;
    5995              : 
    5996              :   return NULL;
    5997              : }
    5998              : 
    5999              : 
    6000              : /* Determine if an expression is a procedure pointer component.  */
    6001              : 
    6002              : bool
    6003      1121092 : gfc_is_proc_ptr_comp (gfc_expr *expr)
    6004              : {
    6005      1121092 :   return (gfc_get_proc_ptr_comp (expr) != NULL);
    6006              : }
    6007              : 
    6008              : 
    6009              : /* Determine if an expression is a function with an allocatable class scalar
    6010              :    result.  */
    6011              : bool
    6012       398718 : gfc_is_alloc_class_scalar_function (gfc_expr *expr)
    6013              : {
    6014       398718 :   if (expr->expr_type == EXPR_FUNCTION
    6015        73529 :       && ((expr->value.function.esym
    6016        40882 :            && expr->value.function.esym->result
    6017        40881 :            && expr->value.function.esym->result->ts.type == BT_CLASS
    6018         1028 :            && !CLASS_DATA (expr->value.function.esym->result)->attr.dimension
    6019          895 :            && CLASS_DATA (expr->value.function.esym->result)->attr.allocatable)
    6020        72908 :           || (expr->ts.type == BT_CLASS
    6021          762 :               && CLASS_DATA (expr)->attr.allocatable
    6022          397 :               && !CLASS_DATA (expr)->attr.dimension)))
    6023          861 :     return true;
    6024              : 
    6025              :   return false;
    6026              : }
    6027              : 
    6028              : 
    6029              : /* Determine if an expression is a function with an allocatable class array
    6030              :    result.  */
    6031              : bool
    6032       169086 : gfc_is_class_array_function (gfc_expr *expr)
    6033              : {
    6034       169086 :   if (expr->expr_type == EXPR_FUNCTION
    6035        81451 :       && expr->value.function.esym
    6036        44530 :       && expr->value.function.esym->result
    6037        44529 :       && expr->value.function.esym->result->ts.type == BT_CLASS
    6038         2431 :       && CLASS_DATA (expr->value.function.esym->result)->attr.dimension
    6039         1560 :       && (CLASS_DATA (expr->value.function.esym->result)->attr.allocatable
    6040          312 :           || CLASS_DATA (expr->value.function.esym->result)->attr.pointer))
    6041         1560 :     return true;
    6042              : 
    6043              :   return false;
    6044              : }
    6045              : 
    6046              : 
    6047              : /* Walk an expression tree and check each variable encountered for being typed.
    6048              :    If strict is not set, a top-level variable is tolerated untyped in -std=gnu
    6049              :    mode as is a basic arithmetic expression using those; this is for things in
    6050              :    legacy-code like:
    6051              : 
    6052              :      INTEGER :: arr(n), n
    6053              :      INTEGER :: arr(n + 1), n
    6054              : 
    6055              :    The namespace is needed for IMPLICIT typing.  */
    6056              : 
    6057              : static gfc_namespace* check_typed_ns;
    6058              : 
    6059              : static bool
    6060        82130 : expr_check_typed_help (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
    6061              :                        int* f ATTRIBUTE_UNUSED)
    6062              : {
    6063        82130 :   bool t;
    6064              : 
    6065        82130 :   if (e->expr_type != EXPR_VARIABLE)
    6066              :     return false;
    6067              : 
    6068         2451 :   gcc_assert (e->symtree);
    6069         2451 :   t = gfc_check_symbol_typed (e->symtree->n.sym, check_typed_ns,
    6070              :                               true, e->where);
    6071              : 
    6072         2451 :   return (!t);
    6073              : }
    6074              : 
    6075              : bool
    6076        89295 : gfc_expr_check_typed (gfc_expr* e, gfc_namespace* ns, bool strict)
    6077              : {
    6078        89295 :   bool error_found;
    6079              : 
    6080              :   /* If this is a top-level variable or EXPR_OP, do the check with strict given
    6081              :      to us.  */
    6082        89295 :   if (!strict)
    6083              :     {
    6084        88970 :       if (e->expr_type == EXPR_VARIABLE && !e->ref)
    6085         9136 :         return gfc_check_symbol_typed (e->symtree->n.sym, ns, strict, e->where);
    6086              : 
    6087        79834 :       if (e->expr_type == EXPR_OP)
    6088              :         {
    6089         2267 :           bool t = true;
    6090              : 
    6091         2267 :           gcc_assert (e->value.op.op1);
    6092         2267 :           t = gfc_expr_check_typed (e->value.op.op1, ns, strict);
    6093              : 
    6094         2267 :           if (t && e->value.op.op2)
    6095         1762 :             t = gfc_expr_check_typed (e->value.op.op2, ns, strict);
    6096              : 
    6097         2267 :           return t;
    6098              :         }
    6099              :     }
    6100              : 
    6101              :   /* Otherwise, walk the expression and do it strictly.  */
    6102        77892 :   check_typed_ns = ns;
    6103        77892 :   error_found = gfc_traverse_expr (e, NULL, &expr_check_typed_help, 0);
    6104              : 
    6105        77892 :   return error_found ? false : true;
    6106              : }
    6107              : 
    6108              : 
    6109              : /* This function returns true if it contains any references to PDT KIND
    6110              :    or LEN parameters.  */
    6111              : 
    6112              : static bool
    6113       169901 : derived_parameter_expr (gfc_expr* e, gfc_symbol* sym ATTRIBUTE_UNUSED,
    6114              :                         int* f ATTRIBUTE_UNUSED)
    6115              : {
    6116       169901 :   if (e->expr_type != EXPR_VARIABLE)
    6117              :     return false;
    6118              : 
    6119         2948 :   gcc_assert (e->symtree);
    6120         2948 :   if (e->symtree->n.sym->attr.pdt_kind
    6121         2597 :       || e->symtree->n.sym->attr.pdt_len)
    6122          720 :     return true;
    6123              : 
    6124              :   return false;
    6125              : }
    6126              : 
    6127              : 
    6128              : bool
    6129       139112 : gfc_derived_parameter_expr (gfc_expr *e)
    6130              : {
    6131       139112 :   return gfc_traverse_expr (e, NULL, &derived_parameter_expr, 0);
    6132              : }
    6133              : 
    6134              : 
    6135              : /* This function returns the overall type of a type parameter spec list.
    6136              :    If all the specs are explicit, SPEC_EXPLICIT is returned. If any of the
    6137              :    parameters are assumed/deferred then SPEC_ASSUMED/DEFERRED is returned
    6138              :    unless derived is not NULL.  In this latter case, all the LEN parameters
    6139              :    must be either assumed or deferred for the return argument to be set to
    6140              :    anything other than SPEC_EXPLICIT.  */
    6141              : 
    6142              : gfc_param_spec_type
    6143          128 : gfc_spec_list_type (gfc_actual_arglist *param_list, gfc_symbol *derived)
    6144              : {
    6145          128 :   gfc_param_spec_type res = SPEC_EXPLICIT;
    6146          128 :   gfc_component *c;
    6147          128 :   bool seen_assumed = false;
    6148          128 :   bool seen_deferred = false;
    6149          128 :   bool seen_len = false;
    6150              : 
    6151          128 :   if (derived == NULL)
    6152              :     {
    6153            0 :       for (; param_list; param_list = param_list->next)
    6154            0 :         if (param_list->spec_type == SPEC_ASSUMED
    6155            0 :             || param_list->spec_type == SPEC_DEFERRED)
    6156              :           return param_list->spec_type;
    6157              :     }
    6158              :   else
    6159              :     {
    6160          338 :       for (; param_list; param_list = param_list->next)
    6161              :         {
    6162          214 :           c = gfc_find_component (derived, param_list->name,
    6163              :                                   true, true, NULL);
    6164          214 :           gcc_assert (c != NULL);
    6165          214 :           if (c->attr.pdt_kind)
    6166          114 :             continue;
    6167          100 :           else if (param_list->spec_type == SPEC_EXPLICIT)
    6168              :             return SPEC_EXPLICIT;
    6169           96 :           seen_assumed = param_list->spec_type == SPEC_ASSUMED;
    6170           96 :           seen_deferred = param_list->spec_type == SPEC_DEFERRED;
    6171           96 :           if (c->attr.pdt_len)
    6172           96 :             seen_len = true;
    6173              :           if (seen_assumed && seen_deferred)
    6174              :             return SPEC_EXPLICIT;
    6175              :         }
    6176          124 :       res = (seen_assumed || !seen_len) ? SPEC_ASSUMED : SPEC_DEFERRED;
    6177              :     }
    6178              :   return res;
    6179              : }
    6180              : 
    6181              : 
    6182              : bool
    6183        27550 : gfc_ref_this_image (gfc_ref *ref)
    6184              : {
    6185        27550 :   int n;
    6186              : 
    6187        27550 :   gcc_assert (ref->type == REF_ARRAY && ref->u.ar.codimen > 0);
    6188              : 
    6189        60265 :   for (n = ref->u.ar.dimen; n < ref->u.ar.dimen + ref->u.ar.codimen; n++)
    6190        36510 :     if (ref->u.ar.dimen_type[n] != DIMEN_THIS_IMAGE)
    6191              :       return false;
    6192              : 
    6193              :   return true;
    6194              : }
    6195              : 
    6196              : gfc_expr *
    6197         2522 : gfc_find_team_co (gfc_expr *e, enum gfc_array_ref_team_type req_team_type)
    6198              : {
    6199         2522 :   gfc_ref *ref;
    6200              : 
    6201         3768 :   for (ref = e->ref; ref; ref = ref->next)
    6202         1280 :     if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0
    6203         1280 :         && ref->u.ar.team_type == req_team_type)
    6204           34 :       return ref->u.ar.team;
    6205              : 
    6206         2488 :   if (e->expr_type == EXPR_FUNCTION && e->value.function.actual->expr)
    6207         2498 :     for (ref = e->value.function.actual->expr->ref; ref;
    6208         1256 :          ref = ref->next)
    6209         1270 :       if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0
    6210         1242 :           && ref->u.ar.team_type == req_team_type)
    6211           14 :         return ref->u.ar.team;
    6212              : 
    6213              :   return NULL;
    6214              : }
    6215              : 
    6216              : gfc_expr *
    6217         1261 : gfc_find_stat_co (gfc_expr *e)
    6218              : {
    6219         1261 :   gfc_ref *ref;
    6220              : 
    6221         1261 :   for (ref = e->ref; ref; ref = ref->next)
    6222          640 :     if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
    6223          640 :       return ref->u.ar.stat;
    6224              : 
    6225          621 :   if (e->value.function.actual->expr)
    6226          635 :     for (ref = e->value.function.actual->expr->ref; ref;
    6227           14 :          ref = ref->next)
    6228          635 :       if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
    6229          621 :         return ref->u.ar.stat;
    6230              : 
    6231              :   return NULL;
    6232              : }
    6233              : 
    6234              : bool
    6235       854831 : gfc_is_coindexed (gfc_expr *e)
    6236              : {
    6237       854831 :   gfc_ref *ref;
    6238              : 
    6239       854831 :   if (e->expr_type == EXPR_FUNCTION && e->value.function.isym
    6240          532 :       && e->value.function.isym->id == GFC_ISYM_CAF_GET)
    6241            0 :     e = e->value.function.actual->expr;
    6242              : 
    6243      1270622 :   for (ref = e->ref; ref; ref = ref->next)
    6244       437462 :     if (ref->type == REF_ARRAY && ref->u.ar.codimen > 0)
    6245        21671 :       return !gfc_ref_this_image (ref);
    6246              : 
    6247              :   return false;
    6248              : }
    6249              : 
    6250              : 
    6251              : /* Coarrays are variables with a corank but not being coindexed. However, also
    6252              :    the following is a coarray: A subobject of a coarray is a coarray if it does
    6253              :    not have any cosubscripts, vector subscripts, allocatable component
    6254              :    selection, or pointer component selection. (F2008, 2.4.7)  */
    6255              : 
    6256              : bool
    6257       171604 : gfc_is_coarray (gfc_expr *e)
    6258              : {
    6259       171604 :   gfc_ref *ref;
    6260       171604 :   gfc_symbol *sym;
    6261       171604 :   gfc_component *comp;
    6262       171604 :   bool coindexed;
    6263       171604 :   bool coarray;
    6264       171604 :   int i;
    6265              : 
    6266       171604 :   if (e->expr_type != EXPR_VARIABLE)
    6267              :     return false;
    6268              : 
    6269       169000 :   coindexed = false;
    6270       169000 :   sym = e->symtree->n.sym;
    6271              : 
    6272       169000 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
    6273        17390 :     coarray = CLASS_DATA (sym)->attr.codimension;
    6274              :   else
    6275       151610 :     coarray = sym->attr.codimension;
    6276              : 
    6277       358474 :   for (ref = e->ref; ref; ref = ref->next)
    6278       189474 :     switch (ref->type)
    6279              :     {
    6280        26179 :       case REF_COMPONENT:
    6281        26179 :         comp = ref->u.c.component;
    6282        26179 :         if (comp->ts.type == BT_CLASS && comp->attr.class_ok
    6283         2429 :             && (CLASS_DATA (comp)->attr.class_pointer
    6284         2124 :                 || CLASS_DATA (comp)->attr.allocatable))
    6285              :           {
    6286         2429 :             coindexed = false;
    6287         2429 :             coarray = CLASS_DATA (comp)->attr.codimension;
    6288              :           }
    6289        23750 :         else if (comp->attr.pointer || comp->attr.allocatable)
    6290              :           {
    6291        22258 :             coindexed = false;
    6292        22258 :             coarray = comp->attr.codimension;
    6293              :           }
    6294              :         break;
    6295              : 
    6296       162857 :      case REF_ARRAY:
    6297       162857 :         if (!coarray)
    6298              :           break;
    6299              : 
    6300         5919 :         if (ref->u.ar.codimen > 0 && !gfc_ref_this_image (ref))
    6301              :           {
    6302              :             coindexed = true;
    6303              :             break;
    6304              :           }
    6305              : 
    6306         9438 :         for (i = 0; i < ref->u.ar.dimen; i++)
    6307         4145 :           if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
    6308              :             {
    6309              :               coarray = false;
    6310              :               break;
    6311              :             }
    6312              :         break;
    6313              : 
    6314              :      case REF_SUBSTRING:
    6315              :      case REF_INQUIRY:
    6316              :         break;
    6317              :     }
    6318              : 
    6319       169000 :   return coarray && !coindexed;
    6320              : }
    6321              : 
    6322              : 
    6323              : /* Check whether the expression has an ultimate allocatable component.
    6324              :    Being itself allocatable does not count.  */
    6325              : bool
    6326          344 : gfc_has_ultimate_allocatable (gfc_expr *e)
    6327              : {
    6328          344 :   gfc_ref *ref, *last = NULL;
    6329              : 
    6330          344 :   if (e->expr_type != EXPR_VARIABLE)
    6331              :     return false;
    6332              : 
    6333          589 :   for (ref = e->ref; ref; ref = ref->next)
    6334          245 :     if (ref->type == REF_COMPONENT)
    6335           10 :       last = ref;
    6336              : 
    6337          344 :   if (last && last->u.c.component->ts.type == BT_CLASS)
    6338            0 :     return CLASS_DATA (last->u.c.component)->attr.alloc_comp;
    6339            9 :   else if (last && last->u.c.component->ts.type == BT_DERIVED)
    6340            1 :     return last->u.c.component->ts.u.derived->attr.alloc_comp;
    6341          335 :   else if (last)
    6342              :     return false;
    6343              : 
    6344          335 :   if (e->ts.type == BT_CLASS)
    6345            4 :     return CLASS_DATA (e)->attr.alloc_comp;
    6346          331 :   else if (e->ts.type == BT_DERIVED)
    6347          147 :     return e->ts.u.derived->attr.alloc_comp;
    6348              :   else
    6349              :     return false;
    6350              : }
    6351              : 
    6352              : 
    6353              : /* Check whether the expression has an pointer component.
    6354              :    Being itself a pointer does not count.  */
    6355              : bool
    6356          445 : gfc_has_ultimate_pointer (gfc_expr *e)
    6357              : {
    6358          445 :   gfc_ref *ref, *last = NULL;
    6359              : 
    6360          445 :   if (e->expr_type != EXPR_VARIABLE)
    6361              :     return false;
    6362              : 
    6363         1138 :   for (ref = e->ref; ref; ref = ref->next)
    6364          693 :     if (ref->type == REF_COMPONENT)
    6365          156 :       last = ref;
    6366              : 
    6367          445 :   if (last && last->u.c.component->ts.type == BT_CLASS)
    6368            0 :     return CLASS_DATA (last->u.c.component)->attr.pointer_comp;
    6369          141 :   else if (last && last->u.c.component->ts.type == BT_DERIVED)
    6370            4 :     return last->u.c.component->ts.u.derived->attr.pointer_comp;
    6371          304 :   else if (last)
    6372              :     return false;
    6373              : 
    6374          304 :   if (e->ts.type == BT_CLASS)
    6375            2 :     return CLASS_DATA (e)->attr.pointer_comp;
    6376          302 :   else if (e->ts.type == BT_DERIVED)
    6377            6 :     return e->ts.u.derived->attr.pointer_comp;
    6378              :   else
    6379              :     return false;
    6380              : }
    6381              : 
    6382              : 
    6383              : /* Check whether an expression is "simply contiguous", cf. F2008, 6.5.4.
    6384              :    Note: A scalar is not regarded as "simply contiguous" by the standard.
    6385              :    if bool is not strict, some further checks are done - for instance,
    6386              :    a "(::1)" is accepted.  */
    6387              : 
    6388              : bool
    6389        22352 : gfc_is_simply_contiguous (gfc_expr *expr, bool strict, bool permit_element)
    6390              : {
    6391        22352 :   bool colon;
    6392        22352 :   int i;
    6393        22352 :   gfc_array_ref *ar = NULL;
    6394        22352 :   gfc_ref *ref, *part_ref = NULL;
    6395        22352 :   gfc_symbol *sym;
    6396              : 
    6397        22352 :   if (expr->expr_type == EXPR_ARRAY)
    6398              :     return true;
    6399              : 
    6400        22080 :   if (expr->expr_type == EXPR_NULL)
    6401              :     {
    6402              :       /* F2018:16.9.144  NULL ([MOLD]):
    6403              :          "If MOLD is present, the characteristics are the same as MOLD."
    6404              :          "If MOLD is absent, the characteristics of the result are
    6405              :          determined by the entity with which the reference is associated."
    6406              :          F2018:15.3.2.2 characteristics attributes include CONTIGUOUS.  */
    6407            7 :       if (expr->ts.type == BT_UNKNOWN)
    6408              :         return true;
    6409              :       else
    6410            6 :         return (gfc_variable_attr (expr, NULL).contiguous
    6411           12 :                 || gfc_variable_attr (expr, NULL).allocatable);
    6412              :     }
    6413              : 
    6414        22073 :   if (expr->expr_type == EXPR_FUNCTION)
    6415              :     {
    6416          360 :       if (expr->value.function.isym)
    6417              :         /* TRANSPOSE is the only intrinsic that may return a
    6418              :            non-contiguous array.  It's treated as a special case in
    6419              :            gfc_conv_expr_descriptor too.  */
    6420          298 :         return (expr->value.function.isym->id != GFC_ISYM_TRANSPOSE);
    6421           62 :       else if (expr->value.function.esym)
    6422              :         /* Only a pointer to an array without the contiguous attribute
    6423              :            can be non-contiguous as a result value.  */
    6424           60 :         return (expr->value.function.esym->result->attr.contiguous
    6425           96 :                 || !expr->value.function.esym->result->attr.pointer);
    6426              :       else
    6427              :         {
    6428              :           /* Type-bound procedures.  */
    6429            2 :           gfc_symbol *s = expr->symtree->n.sym;
    6430            2 :           if (s->ts.type != BT_CLASS && s->ts.type != BT_DERIVED)
    6431              :             return false;
    6432              : 
    6433            2 :           gfc_ref *rc = NULL;
    6434            7 :           for (gfc_ref *r = expr->ref; r; r = r->next)
    6435            5 :             if (r->type == REF_COMPONENT)
    6436            5 :               rc = r;
    6437              : 
    6438            2 :           if (rc == NULL || rc->u.c.component == NULL
    6439            2 :               || rc->u.c.component->ts.interface == NULL)
    6440              :             return false;
    6441              : 
    6442            2 :           return rc->u.c.component->ts.interface->attr.contiguous;
    6443              :         }
    6444              :     }
    6445        21713 :   else if (expr->expr_type != EXPR_VARIABLE)
    6446              :     return false;
    6447              : 
    6448        21660 :   if (!permit_element && expr->rank == 0)
    6449              :     return false;
    6450              : 
    6451        46865 :   for (ref = expr->ref; ref; ref = ref->next)
    6452              :     {
    6453        25297 :       if (ar)
    6454              :         return false; /* Array shall be last part-ref.  */
    6455              : 
    6456        25221 :       if (ref->type == REF_COMPONENT)
    6457              :         part_ref  = ref;
    6458        21936 :       else if (ref->type == REF_SUBSTRING)
    6459              :         return false;
    6460        21929 :       else if (ref->type == REF_INQUIRY)
    6461              :         return false;
    6462        21921 :       else if (ref->u.ar.type != AR_ELEMENT)
    6463        21003 :         ar = &ref->u.ar;
    6464              :     }
    6465              : 
    6466        21568 :   sym = expr->symtree->n.sym;
    6467        21568 :   if ((part_ref
    6468         2638 :        && part_ref->u.c.component
    6469         2638 :        && !part_ref->u.c.component->attr.contiguous
    6470         2629 :        && IS_POINTER (part_ref->u.c.component))
    6471              :       || (!part_ref
    6472        18930 :           && expr->ts.type != BT_CLASS
    6473        18840 :           && !sym->attr.contiguous
    6474        13468 :           && (sym->attr.pointer
    6475        11605 :               || (sym->as && sym->as->type == AS_ASSUMED_RANK)
    6476        11207 :               || (sym->as && sym->as->type == AS_ASSUMED_SHAPE))))
    6477              :     return false;
    6478              : 
    6479              :   /* An associate variable may point to a non-contiguous target.  */
    6480        17101 :   if (ar && ar->type == AR_FULL
    6481        10093 :       && sym->attr.associate_var && !sym->attr.contiguous
    6482          162 :       && sym->assoc
    6483          162 :       && sym->assoc->target)
    6484          162 :     return gfc_is_simply_contiguous (sym->assoc->target, strict,
    6485          162 :                                      permit_element);
    6486              : 
    6487        16574 :   if (!ar || ar->type == AR_FULL)
    6488              :     return true;
    6489              : 
    6490         6643 :   gcc_assert (ar->type == AR_SECTION);
    6491              : 
    6492              :   /* Check for simply contiguous array */
    6493              :   colon = true;
    6494        12775 :   for (i = 0; i < ar->dimen; i++)
    6495              :     {
    6496         7403 :       if (ar->dimen_type[i] == DIMEN_VECTOR)
    6497              :         return false;
    6498              : 
    6499         7403 :       if (ar->dimen_type[i] == DIMEN_ELEMENT)
    6500              :         {
    6501           25 :           colon = false;
    6502           25 :           continue;
    6503              :         }
    6504              : 
    6505         7378 :       gcc_assert (ar->dimen_type[i] == DIMEN_RANGE);
    6506              : 
    6507              : 
    6508              :       /* If the previous section was not contiguous, that's an error,
    6509              :          unless we have effective only one element and checking is not
    6510              :          strict.  */
    6511         7378 :       if (!colon && (strict || !ar->start[i] || !ar->end[i]
    6512           95 :                      || ar->start[i]->expr_type != EXPR_CONSTANT
    6513           93 :                      || ar->end[i]->expr_type != EXPR_CONSTANT
    6514           51 :                      || mpz_cmp (ar->start[i]->value.integer,
    6515           51 :                                  ar->end[i]->value.integer) != 0))
    6516              :         return false;
    6517              : 
    6518              :       /* Following the standard, "(::1)" or - if known at compile time -
    6519              :          "(lbound:ubound)" are not simply contiguous; if strict
    6520              :          is false, they are regarded as simply contiguous.  */
    6521         7178 :       if (ar->stride[i] && (strict || ar->stride[i]->expr_type != EXPR_CONSTANT
    6522         1069 :                             || ar->stride[i]->ts.type != BT_INTEGER
    6523         1069 :                             || mpz_cmp_si (ar->stride[i]->value.integer, 1) != 0))
    6524              :         return false;
    6525              : 
    6526         6107 :       if (ar->start[i]
    6527         3905 :           && (strict || ar->start[i]->expr_type != EXPR_CONSTANT
    6528         3859 :               || !ar->as->lower[i]
    6529         2130 :               || ar->as->lower[i]->expr_type != EXPR_CONSTANT
    6530         2130 :               || mpz_cmp (ar->start[i]->value.integer,
    6531         2130 :                           ar->as->lower[i]->value.integer) != 0))
    6532         6107 :         colon = false;
    6533              : 
    6534         6107 :       if (ar->end[i]
    6535         3936 :           && (strict || ar->end[i]->expr_type != EXPR_CONSTANT
    6536         3427 :               || !ar->as->upper[i]
    6537         1988 :               || ar->as->upper[i]->expr_type != EXPR_CONSTANT
    6538         1988 :               || mpz_cmp (ar->end[i]->value.integer,
    6539         1988 :                           ar->as->upper[i]->value.integer) != 0))
    6540         6132 :         colon = false;
    6541              :     }
    6542              : 
    6543              :   return true;
    6544              : }
    6545              : 
    6546              : /* Return true if the expression is guaranteed to be non-contiguous,
    6547              :    false if we cannot prove anything.  It is probably best to call
    6548              :    this after gfc_is_simply_contiguous.  If neither of them returns
    6549              :    true, we cannot say (at compile-time).  */
    6550              : 
    6551              : bool
    6552         2658 : gfc_is_not_contiguous (gfc_expr *array)
    6553              : {
    6554         2658 :   int i;
    6555         2658 :   gfc_array_ref *ar = NULL;
    6556         2658 :   gfc_ref *ref;
    6557         2658 :   bool previous_incomplete;
    6558              : 
    6559         6612 :   for (ref = array->ref; ref; ref = ref->next)
    6560              :     {
    6561              :       /* Array-ref shall be last ref.  */
    6562              : 
    6563         4014 :       if (ar && ar->type != AR_ELEMENT)
    6564              :         return true;
    6565              : 
    6566         3954 :       if (ref->type == REF_ARRAY)
    6567         2656 :         ar = &ref->u.ar;
    6568              :     }
    6569              : 
    6570         2598 :   if (ar == NULL || ar->type != AR_SECTION)
    6571              :     return false;
    6572              : 
    6573              :   previous_incomplete = false;
    6574              : 
    6575              :   /* Check if we can prove that the array is not contiguous.  */
    6576              : 
    6577         1525 :   for (i = 0; i < ar->dimen; i++)
    6578              :     {
    6579          862 :       mpz_t arr_size, ref_size;
    6580              : 
    6581          862 :       if (gfc_ref_dimen_size (ar, i, &ref_size, NULL))
    6582              :         {
    6583          419 :           if (gfc_dep_difference (ar->as->upper[i], ar->as->lower[i], &arr_size))
    6584              :             {
    6585              :               /* a(2:4,2:) is known to be non-contiguous, but
    6586              :                  a(2:4,i:i) can be contiguous.  */
    6587           61 :               mpz_add_ui (arr_size, arr_size, 1L);
    6588           61 :               if (previous_incomplete && mpz_cmp_si (ref_size, 1) != 0)
    6589              :                 {
    6590            6 :                   mpz_clear (arr_size);
    6591            6 :                   mpz_clear (ref_size);
    6592           13 :                   return true;
    6593              :                 }
    6594           55 :               else if (mpz_cmp (arr_size, ref_size) != 0)
    6595           28 :                 previous_incomplete = true;
    6596              : 
    6597           55 :               mpz_clear (arr_size);
    6598              :             }
    6599              : 
    6600              :           /* Check for a(::2), i.e. where the stride is not unity.
    6601              :              This is only done if there is more than one element in
    6602              :              the reference along this dimension.  */
    6603              : 
    6604          413 :           if (mpz_cmp_ui (ref_size, 1) > 0 && ar->type == AR_SECTION
    6605          407 :               && ar->dimen_type[i] == DIMEN_RANGE
    6606          407 :               && ar->stride[i] && ar->stride[i]->expr_type == EXPR_CONSTANT
    6607           15 :               && mpz_cmp_si (ar->stride[i]->value.integer, 1) != 0)
    6608              :             {
    6609            7 :               mpz_clear (ref_size);
    6610            7 :               return true;
    6611              :             }
    6612              : 
    6613          406 :           mpz_clear (ref_size);
    6614              :         }
    6615              :     }
    6616              :   /* We didn't find anything definitive.  */
    6617              :   return false;
    6618              : }
    6619              : 
    6620              : /* Build call to an intrinsic procedure.  The number of arguments has to be
    6621              :    passed (rather than ending the list with a NULL value) because we may
    6622              :    want to add arguments but with a NULL-expression.  */
    6623              : 
    6624              : gfc_expr*
    6625        21754 : gfc_build_intrinsic_call (gfc_namespace *ns, gfc_isym_id id, const char* name,
    6626              :                           locus where, unsigned numarg, ...)
    6627              : {
    6628        21754 :   gfc_expr* result;
    6629        21754 :   gfc_actual_arglist* atail;
    6630        21754 :   gfc_intrinsic_sym* isym;
    6631        21754 :   va_list ap;
    6632        21754 :   unsigned i;
    6633        21754 :   const char *mangled_name = gfc_get_string (GFC_PREFIX ("%s"), name);
    6634              : 
    6635        21754 :   isym = gfc_intrinsic_function_by_id (id);
    6636        21754 :   gcc_assert (isym);
    6637              : 
    6638        21754 :   result = gfc_get_expr ();
    6639        21754 :   result->expr_type = EXPR_FUNCTION;
    6640        21754 :   result->ts = isym->ts;
    6641        21754 :   result->where = where;
    6642        21754 :   result->value.function.name = mangled_name;
    6643        21754 :   result->value.function.isym = isym;
    6644              : 
    6645        21754 :   gfc_get_sym_tree (mangled_name, ns, &result->symtree, false);
    6646        21754 :   gfc_commit_symbol (result->symtree->n.sym);
    6647        21754 :   gcc_assert (result->symtree
    6648              :               && (result->symtree->n.sym->attr.flavor == FL_PROCEDURE
    6649              :                   || result->symtree->n.sym->attr.flavor == FL_UNKNOWN));
    6650        21754 :   result->symtree->n.sym->intmod_sym_id = id;
    6651        21754 :   result->symtree->n.sym->attr.flavor = FL_PROCEDURE;
    6652        21754 :   result->symtree->n.sym->attr.intrinsic = 1;
    6653        21754 :   result->symtree->n.sym->attr.artificial = 1;
    6654              : 
    6655        21754 :   va_start (ap, numarg);
    6656        21754 :   atail = NULL;
    6657        74802 :   for (i = 0; i < numarg; ++i)
    6658              :     {
    6659        53048 :       if (atail)
    6660              :         {
    6661        31294 :           atail->next = gfc_get_actual_arglist ();
    6662        31294 :           atail = atail->next;
    6663              :         }
    6664              :       else
    6665        21754 :         atail = result->value.function.actual = gfc_get_actual_arglist ();
    6666              : 
    6667        53048 :       atail->expr = va_arg (ap, gfc_expr*);
    6668              :     }
    6669        21754 :   va_end (ap);
    6670              : 
    6671        21754 :   return result;
    6672              : }
    6673              : 
    6674              : 
    6675              : /* Check if a symbol referenced in a submodule is declared in the ancestor
    6676              :    module and not accessed by use-association, and that the submodule is a
    6677              :    descendant.  */
    6678              : 
    6679              : static bool
    6680            4 : sym_is_from_ancestor (gfc_symbol *sym)
    6681              : {
    6682            4 :   const char dot[2] = ".";
    6683              :   /* Symbols take the form module.submodule_ or module.name_. */
    6684            4 :   char ancestor_module[2 * GFC_MAX_SYMBOL_LEN + 2];
    6685            4 :   char *ancestor;
    6686              : 
    6687            4 :   if (sym == NULL
    6688            4 :       || sym->attr.use_assoc
    6689            4 :       || !sym->attr.used_in_submodule
    6690            4 :       || !sym->module
    6691            4 :       || !sym->ns->proc_name
    6692            4 :       || !sym->ns->proc_name->name)
    6693              :     return false;
    6694              : 
    6695            4 :   memset (ancestor_module, '\0', sizeof (ancestor_module));
    6696            4 :   strcpy (ancestor_module, sym->ns->proc_name->name);
    6697            4 :   ancestor = strtok (ancestor_module, dot);
    6698            4 :   return strcmp (ancestor, sym->module) == 0;
    6699              : }
    6700              : 
    6701              : 
    6702              : /* Check if an expression may appear in a variable definition context
    6703              :    (F2008, 16.6.7) or pointer association context (F2008, 16.6.8).
    6704              :    This is called from the various places when resolving
    6705              :    the pieces that make up such a context.
    6706              :    If own_scope is true (applies to, e.g., ac-implied-do/data-implied-do
    6707              :    variables), some checks are not performed.
    6708              : 
    6709              :    Optionally, a possible error message can be suppressed if context is NULL
    6710              :    and just the return status (true / false) be requested.  */
    6711              : 
    6712              : bool
    6713       410048 : gfc_check_vardef_context (gfc_expr* e, bool pointer, bool alloc_obj,
    6714              :                           bool own_scope, const char* context)
    6715              : {
    6716       410048 :   gfc_symbol* sym = NULL;
    6717       410048 :   bool is_pointer;
    6718       410048 :   bool check_intentin;
    6719       410048 :   bool ptr_component;
    6720       410048 :   symbol_attribute attr;
    6721       410048 :   gfc_ref* ref;
    6722       410048 :   int i;
    6723              : 
    6724       410048 :   if (e->expr_type == EXPR_VARIABLE)
    6725              :     {
    6726       409974 :       gcc_assert (e->symtree);
    6727       409974 :       sym = e->symtree->n.sym;
    6728              :     }
    6729           74 :   else if (e->expr_type == EXPR_FUNCTION)
    6730              :     {
    6731           18 :       gcc_assert (e->symtree);
    6732           18 :       sym = e->value.function.esym ? e->value.function.esym : e->symtree->n.sym;
    6733              :     }
    6734              : 
    6735       410048 :   attr = gfc_expr_attr (e);
    6736       410048 :   if (!pointer && e->expr_type == EXPR_FUNCTION && attr.pointer)
    6737              :     {
    6738           16 :       if (!(gfc_option.allow_std & GFC_STD_F2008))
    6739              :         {
    6740            1 :           if (context)
    6741            1 :             gfc_error ("Fortran 2008: Pointer functions in variable definition"
    6742              :                        " context (%s) at %L", context, &e->where);
    6743            1 :           return false;
    6744              :         }
    6745              :     }
    6746       410032 :   else if (e->expr_type != EXPR_VARIABLE)
    6747              :     {
    6748           58 :       if (context)
    6749           55 :         gfc_error ("Non-variable expression in variable definition context (%s)"
    6750              :                    " at %L", context, &e->where);
    6751           58 :       return false;
    6752              :     }
    6753              : 
    6754       409989 :   if (!pointer && sym->attr.flavor == FL_PARAMETER)
    6755              :     {
    6756            5 :       if (context)
    6757            5 :         gfc_error ("Named constant %qs in variable definition context (%s)"
    6758              :                    " at %L", sym->name, context, &e->where);
    6759            5 :       return false;
    6760              :     }
    6761       393147 :   if (!pointer && sym->attr.flavor != FL_VARIABLE
    6762        10566 :       && !(sym->attr.flavor == FL_PROCEDURE && sym == sym->result)
    6763          562 :       && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.proc_pointer)
    6764            3 :       && !(sym->attr.flavor == FL_PROCEDURE
    6765            3 :            && sym->attr.function && attr.pointer))
    6766              :     {
    6767            0 :       if (context)
    6768            0 :         gfc_error ("%qs in variable definition context (%s) at %L is not"
    6769              :                    " a variable", sym->name, context, &e->where);
    6770            0 :       return false;
    6771              :     }
    6772              : 
    6773              :   /* Find out whether the expr is a pointer; this also means following
    6774              :      component references to the last one.  */
    6775       409984 :   is_pointer = (attr.pointer || attr.proc_pointer);
    6776       409984 :   if (pointer && !is_pointer)
    6777              :     {
    6778           10 :       if (context)
    6779            5 :         gfc_error ("Non-POINTER in pointer association context (%s)"
    6780              :                    " at %L", context, &e->where);
    6781           10 :       return false;
    6782              :     }
    6783              : 
    6784       409974 :   if (e->ts.type == BT_DERIVED
    6785        20770 :       && e->ts.u.derived == NULL)
    6786              :     {
    6787            1 :       if (context)
    6788            1 :         gfc_error ("Type inaccessible in variable definition context (%s) "
    6789              :                    "at %L", context, &e->where);
    6790            1 :       return false;
    6791              :     }
    6792              : 
    6793              :   /* F2008, C1303.  */
    6794       409973 :   if (!alloc_obj
    6795       378256 :       && (attr.lock_comp
    6796       378256 :           || (e->ts.type == BT_DERIVED
    6797        15933 :               && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    6798           32 :               && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_LOCK_TYPE)))
    6799              :     {
    6800            3 :       if (context)
    6801            3 :         gfc_error ("LOCK_TYPE in variable definition context (%s) at %L",
    6802              :                    context, &e->where);
    6803            3 :       return false;
    6804              :     }
    6805              : 
    6806              :   /* TS18508, C702/C203.  */
    6807       378253 :   if (!alloc_obj
    6808              :       && (attr.lock_comp
    6809       378253 :           || (e->ts.type == BT_DERIVED
    6810        15930 :               && e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
    6811           29 :               && e->ts.u.derived->intmod_sym_id == ISOFORTRAN_EVENT_TYPE)))
    6812              :     {
    6813            0 :       if (context)
    6814            0 :         gfc_error ("LOCK_EVENT in variable definition context (%s) at %L",
    6815              :                    context, &e->where);
    6816            0 :       return false;
    6817              :     }
    6818              : 
    6819              :   /* INTENT(IN) dummy argument.  Check this, unless the object itself is the
    6820              :      component of sub-component of a pointer; we need to distinguish
    6821              :      assignment to a pointer component from pointer-assignment to a pointer
    6822              :      component.  Note that (normal) assignment to procedure pointers is not
    6823              :      possible.  */
    6824       409970 :   check_intentin = !own_scope;
    6825        14098 :   ptr_component = (sym->ts.type == BT_CLASS && sym->ts.u.derived
    6826        14098 :                    && CLASS_DATA (sym))
    6827       424068 :                   ? CLASS_DATA (sym)->attr.class_pointer : sym->attr.pointer;
    6828       540639 :   for (ref = e->ref; ref && check_intentin; ref = ref->next)
    6829              :     {
    6830              :       /* Associate-targets need special handling.  Subobjects of an object with
    6831              :          the PROTECTED attribute inherit this attribute.  */
    6832       130677 :       if (ptr_component && ref->type == REF_COMPONENT
    6833         2349 :           && !sym->assoc && !sym->attr.is_protected)
    6834       130677 :         check_intentin = false;
    6835       130677 :       if (ref->type == REF_COMPONENT)
    6836              :         {
    6837        30712 :           gfc_component *comp = ref->u.c.component;
    6838         2407 :           ptr_component = (comp->ts.type == BT_CLASS && comp->attr.class_ok)
    6839        33119 :                         ? CLASS_DATA (comp)->attr.class_pointer
    6840        28305 :                         : comp->attr.pointer;
    6841        30712 :           if (ptr_component && !pointer)
    6842         4284 :             check_intentin = false;
    6843              :         }
    6844       130677 :       if (ref->type == REF_INQUIRY
    6845           90 :           && (ref->u.i == INQUIRY_KIND || ref->u.i == INQUIRY_LEN))
    6846              :         {
    6847            8 :           if (context)
    6848           16 :             gfc_error ("%qs parameter inquiry for %qs in "
    6849              :                        "variable definition context (%s) at %L",
    6850              :                        ref->u.i == INQUIRY_KIND ? "KIND" : "LEN",
    6851              :                        sym->name, context, &e->where);
    6852            8 :           return false;
    6853              :         }
    6854              :     }
    6855              : 
    6856              :   /* See if the INTENT(IN) check should apply to an ASSOCIATE target.  */
    6857       409962 :   if (check_intentin && sym->assoc && sym->assoc->target)
    6858              :     {
    6859              :       gfc_expr *target;
    6860              :       gfc_symbol *tsym;
    6861              : 
    6862         1953 :       check_intentin = false;
    6863              : 
    6864              :       /* Walk through associate target chain to find a dummy argument.  */
    6865         1953 :       for (target = sym->assoc->target; target; target = tsym->assoc->target)
    6866              :         {
    6867         1953 :           tsym = target->symtree ? target->symtree->n.sym : NULL;
    6868              : 
    6869         1951 :           if (tsym == NULL)
    6870              :             break;
    6871              : 
    6872         1951 :           if (tsym->attr.dummy)
    6873              :             {
    6874          925 :               check_intentin = (tsym->attr.intent == INTENT_IN);
    6875          925 :               break;
    6876              :             }
    6877              : 
    6878         1026 :           if (tsym->assoc == NULL)
    6879              :             break;
    6880              :         }
    6881              :     }
    6882              : 
    6883       399065 :   if (check_intentin
    6884       397284 :       && (sym->attr.intent == INTENT_IN
    6885       397187 :           || (sym->attr.select_type_temporary && sym->assoc
    6886            7 :               && sym->assoc->target && sym->assoc->target->symtree
    6887            7 :               && sym->assoc->target->symtree->n.sym->attr.intent == INTENT_IN)))
    6888              :     {
    6889           97 :       const char *name = (sym->attr.select_type_temporary
    6890          100 :                           ? sym->assoc->target->symtree->name : sym->name);
    6891          100 :       if (pointer && is_pointer)
    6892              :         {
    6893           18 :           if (context)
    6894           18 :             gfc_error ("Dummy argument %qs with INTENT(IN) in pointer"
    6895              :                        " association context (%s) at %L",
    6896              :                        name, context, &e->where);
    6897           18 :           return false;
    6898              :         }
    6899           82 :       if (!pointer && !is_pointer && !sym->attr.pointer)
    6900              :         {
    6901           30 :           if (context)
    6902           17 :             gfc_error ("Dummy argument %qs with INTENT(IN) in variable"
    6903              :                        " definition context (%s) at %L",
    6904              :                        name, context, &e->where);
    6905           30 :           return false;
    6906              :         }
    6907              :     }
    6908              : 
    6909              :   /* PROTECTED and use-associated.  */
    6910       409914 :   if (sym->attr.is_protected
    6911          263 :       && (sym->attr.use_assoc
    6912          201 :           || (sym->attr.used_in_submodule && !sym_is_from_ancestor (sym)))
    6913           63 :       && !own_scope
    6914       409975 :       && (check_intentin || !pointer))
    6915              :     {
    6916           61 :       if (pointer && is_pointer)
    6917              :         {
    6918           16 :           if (context)
    6919           16 :             gfc_error ("Variable %qs is PROTECTED and cannot appear in a "
    6920              :                        "pointer association context (%s) at %L",
    6921              :                        sym->name, context, &e->where);
    6922           16 :           return false;
    6923              :         }
    6924           45 :       if (!pointer && !is_pointer)
    6925              :         {
    6926           25 :           if (context)
    6927           24 :             gfc_error ("Variable %qs is PROTECTED and cannot appear in a "
    6928              :                        "variable definition context (%s) at %L",
    6929              :                        sym->name, context, &e->where);
    6930           25 :           return false;
    6931              :         }
    6932              :     }
    6933              : 
    6934              :   /* Variable not assignable from a PURE procedure but appears in
    6935              :      variable definition context.  */
    6936      1216345 :   own_scope = own_scope
    6937       409873 :               || (sym->attr.result && sym->ns->proc_name
    6938         8622 :                   && sym == sym->ns->proc_name->result);
    6939       396607 :   if (!pointer && !own_scope && gfc_pure (NULL) && gfc_impure_variable (sym))
    6940              :     {
    6941            8 :       if (context)
    6942            8 :         gfc_error ("Variable %qs cannot appear in a variable definition"
    6943              :                    " context (%s) at %L in PURE procedure",
    6944              :                    sym->name, context, &e->where);
    6945            8 :       return false;
    6946              :     }
    6947              : 
    6948       387819 :   if (!pointer && context && gfc_implicit_pure (NULL)
    6949       422217 :       && gfc_impure_variable (sym))
    6950              :     {
    6951         1098 :       gfc_namespace *ns;
    6952         1098 :       gfc_symbol *sym;
    6953              : 
    6954         1172 :       for (ns = gfc_current_ns; ns; ns = ns->parent)
    6955              :         {
    6956         1172 :           sym = ns->proc_name;
    6957         1172 :           if (sym == NULL)
    6958              :             break;
    6959         1172 :           if (sym->attr.flavor == FL_PROCEDURE)
    6960              :             {
    6961         1098 :               sym->attr.implicit_pure = 0;
    6962         1098 :               break;
    6963              :             }
    6964              :         }
    6965              :     }
    6966              :   /* Check variable definition context for associate-names.  */
    6967       409865 :   if ((!pointer || check_intentin)
    6968       409355 :       && sym->assoc && !sym->attr.select_rank_temporary)
    6969              :     {
    6970         1302 :       const char* name;
    6971         1302 :       gfc_association_list* assoc;
    6972              : 
    6973         1302 :       gcc_assert (sym->assoc->target);
    6974              : 
    6975              :       /* If this is a SELECT TYPE temporary (the association is used internally
    6976              :          for SELECT TYPE), silently go over to the target.  */
    6977         1302 :       if (sym->attr.select_type_temporary)
    6978              :         {
    6979          933 :           gfc_expr* t = sym->assoc->target;
    6980              : 
    6981          933 :           gcc_assert (t->expr_type == EXPR_VARIABLE);
    6982          933 :           name = t->symtree->name;
    6983              : 
    6984          933 :           if (t->symtree->n.sym->assoc)
    6985              :             assoc = t->symtree->n.sym->assoc;
    6986              :           else
    6987          851 :             assoc = sym->assoc;
    6988              :         }
    6989              :       else
    6990              :         {
    6991          369 :           name = sym->name;
    6992          369 :           assoc = sym->assoc;
    6993              :         }
    6994         1302 :       gcc_assert (name && assoc);
    6995              : 
    6996              :       /* Is association to a valid variable?  */
    6997         1302 :       if (!assoc->variable)
    6998              :         {
    6999            9 :           if (context)
    7000              :             {
    7001            9 :               if (assoc->target->expr_type == EXPR_VARIABLE
    7002            9 :                   && gfc_has_vector_index (assoc->target))
    7003            4 :                 gfc_error ("%qs at %L associated to vector-indexed target"
    7004              :                            " cannot be used in a variable definition"
    7005              :                            " context (%s)",
    7006              :                            name, &e->where, context);
    7007              :               else
    7008            5 :                 gfc_error ("%qs at %L associated to expression"
    7009              :                            " cannot be used in a variable definition"
    7010              :                            " context (%s)",
    7011              :                            name, &e->where, context);
    7012              :             }
    7013            9 :           return false;
    7014              :         }
    7015         1293 :       else if (context && gfc_is_ptr_fcn (assoc->target))
    7016              :         {
    7017            5 :           if (!gfc_notify_std (GFC_STD_F2018, "%qs at %L associated to "
    7018              :                                "pointer function target being used in a "
    7019              :                                "variable definition context (%s)", name,
    7020              :                                &e->where, context))
    7021              :             return false;
    7022            1 :           else if (gfc_has_vector_index (e))
    7023              :             {
    7024            0 :               gfc_error ("%qs at %L associated to vector-indexed target"
    7025              :                          " cannot be used in a variable definition"
    7026              :                          " context (%s)",
    7027              :                          name, &e->where, context);
    7028            0 :               return false;
    7029              :             }
    7030              :         }
    7031              : 
    7032              :       /* Target must be allowed to appear in a variable definition context.
    7033              :          Check valid assignment to pointers and invalid reassociations.  */
    7034         1289 :       if (!gfc_check_vardef_context (assoc->target, pointer, false, false, NULL)
    7035         1289 :           && (!ptr_component || pointer))
    7036              :         {
    7037            9 :           if (context)
    7038            6 :             gfc_error ("Associate-name %qs cannot appear in a variable"
    7039              :                        " definition context (%s) at %L because its target"
    7040              :                        " at %L cannot, either",
    7041              :                        name, context, &e->where,
    7042            6 :                        &assoc->target->where);
    7043            9 :           return false;
    7044              :         }
    7045              :     }
    7046              : 
    7047              :   /* Check for same value in vector expression subscript.  */
    7048              : 
    7049       409843 :   if (e->rank > 0)
    7050       155744 :     for (ref = e->ref; ref != NULL; ref = ref->next)
    7051        78156 :       if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
    7052        20091 :         for (i = 0; i < GFC_MAX_DIMENSIONS
    7053        31201 :                && ref->u.ar.dimen_type[i] != 0; i++)
    7054        20098 :           if (ref->u.ar.dimen_type[i] == DIMEN_VECTOR)
    7055              :             {
    7056          248 :               gfc_expr *arr = ref->u.ar.start[i];
    7057          248 :               if (arr->expr_type == EXPR_ARRAY)
    7058              :                 {
    7059           61 :                   gfc_constructor *c, *n;
    7060           61 :                   gfc_expr *ec, *en;
    7061              : 
    7062           61 :                   for (c = gfc_constructor_first (arr->value.constructor);
    7063          208 :                        c != NULL; c = gfc_constructor_next (c))
    7064              :                     {
    7065          154 :                       if (c == NULL || c->iterator != NULL)
    7066           12 :                         continue;
    7067              : 
    7068          142 :                       ec = c->expr;
    7069              : 
    7070          297 :                       for (n = gfc_constructor_next (c); n != NULL;
    7071          155 :                            n = gfc_constructor_next (n))
    7072              :                         {
    7073          162 :                           if (n->iterator != NULL)
    7074           12 :                             continue;
    7075              : 
    7076          150 :                           en = n->expr;
    7077          150 :                           if (gfc_dep_compare_expr (ec, en) == 0)
    7078              :                             {
    7079            7 :                               if (context)
    7080            7 :                                 gfc_error_now ("Elements with the same value "
    7081              :                                                "at %L and %L in vector "
    7082              :                                                "subscript in a variable "
    7083              :                                                "definition context (%s)",
    7084              :                                                &(ec->where), &(en->where),
    7085              :                                                context);
    7086            7 :                               return false;
    7087              :                             }
    7088              :                         }
    7089              :                     }
    7090              :                 }
    7091              :             }
    7092              : 
    7093              :   return true;
    7094              : }
    7095              : 
    7096              : gfc_expr*
    7097           12 : gfc_pdt_find_component_copy_initializer (gfc_symbol *sym, const char *name)
    7098              : {
    7099              :   /* The actual length of a pdt is in its components.  In the
    7100              :      initializer of the current ref is only the default value.
    7101              :      Therefore traverse the chain of components and pick the correct
    7102              :      one's initializer expressions.  */
    7103           12 :   for (gfc_component *comp = sym->ts.u.derived->components; comp != NULL;
    7104            0 :        comp = comp->next)
    7105              :     {
    7106           12 :       if (!strcmp (comp->name, name))
    7107           12 :         return gfc_copy_expr (comp->initializer);
    7108              :     }
    7109              :   return NULL;
    7110              : }
    7111              : 
    7112              : 
    7113              : /* Test for parameterized array or string components.  */
    7114              : 
    7115         9067 : bool has_parameterized_comps (gfc_symbol * der_type)
    7116              : {
    7117         9067 :   bool parameterized_comps = false;
    7118        22829 :   for (gfc_component *c = der_type->components; c; c = c->next)
    7119        13762 :     if (c->attr.pdt_array || c->attr.pdt_string)
    7120              :       parameterized_comps = true;
    7121        13020 :     else if (IS_PDT (c) && strcmp (der_type->name, c->ts.u.derived->name))
    7122          378 :       parameterized_comps = has_parameterized_comps (c->ts.u.derived);
    7123         9067 :   return parameterized_comps;
    7124              : }
        

Generated by: LCOV version 2.4-beta

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