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

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.