LCOV - code coverage report
Current view: top level - gcc/fortran - primary.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.2 % 2381 2243
Test Date: 2026-12-05 16:10:02 Functions: 100.0 % 48 48
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Primary expression subroutines
       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 "parse.h"
      29              : #include "constructor.h"
      30              : 
      31              : int matching_actual_arglist = 0;
      32              : 
      33              : /* Matches a kind-parameter expression, which is either a named
      34              :    symbolic constant or a nonnegative integer constant.  If
      35              :    successful, sets the kind value to the correct integer.
      36              :    The argument 'is_iso_c' signals whether the kind is an ISO_C_BINDING
      37              :    symbol like e.g. 'c_int'.  */
      38              : 
      39              : static match
      40       477096 : match_kind_param (int *kind, int *is_iso_c)
      41              : {
      42       477096 :   char name[GFC_MAX_SYMBOL_LEN + 1];
      43       477096 :   gfc_symbol *sym;
      44       477096 :   match m;
      45              : 
      46       477096 :   *is_iso_c = 0;
      47              : 
      48       477096 :   m = gfc_match_small_literal_int (kind, NULL, false);
      49       477096 :   if (m != MATCH_NO)
      50              :     return m;
      51              : 
      52        95124 :   m = gfc_match_name (name, false);
      53        95124 :   if (m != MATCH_YES)
      54              :     return m;
      55              : 
      56        93392 :   if (gfc_find_symbol (name, NULL, 1, &sym))
      57              :     return MATCH_ERROR;
      58              : 
      59        93392 :   if (sym == NULL)
      60              :     return MATCH_NO;
      61              : 
      62        93391 :   *is_iso_c = sym->attr.is_iso_c;
      63              : 
      64        93391 :   if (sym->attr.flavor != FL_PARAMETER)
      65              :     return MATCH_NO;
      66              : 
      67        93391 :   if (sym->value == NULL)
      68              :     return MATCH_NO;
      69              : 
      70        93390 :   if (gfc_extract_int (sym->value, kind))
      71              :     return MATCH_NO;
      72              : 
      73        93390 :   gfc_set_sym_referenced (sym);
      74              : 
      75        93390 :   if (*kind < 0)
      76            0 :     return MATCH_NO;
      77              : 
      78              :   return MATCH_YES;
      79              : }
      80              : 
      81              : 
      82              : /* Get a trailing kind-specification for non-character variables.
      83              :    Returns:
      84              :      * the integer kind value or
      85              :      * -1 if an error was generated,
      86              :      * -2 if no kind was found.
      87              :    The argument 'is_iso_c' signals whether the kind is an ISO_C_BINDING
      88              :    symbol like e.g. 'c_int'.  */
      89              : 
      90              : static int
      91      4748580 : get_kind (int *is_iso_c)
      92              : {
      93      4748580 :   int kind;
      94      4748580 :   match m;
      95              : 
      96      4748580 :   *is_iso_c = 0;
      97              : 
      98      4748580 :   if (gfc_match_char ('_', false) != MATCH_YES)
      99              :     return -2;
     100              : 
     101       477096 :   m = match_kind_param (&kind, is_iso_c);
     102       477096 :   if (m == MATCH_NO)
     103         1734 :     gfc_error ("Missing kind-parameter at %C");
     104              : 
     105       477096 :   return (m == MATCH_YES) ? kind : -1;
     106              : }
     107              : 
     108              : 
     109              : /* Given a character and a radix, see if the character is a valid
     110              :    digit in that radix.  */
     111              : 
     112              : bool
     113     31945346 : gfc_check_digit (char c, int radix)
     114              : {
     115     31945346 :   bool r;
     116              : 
     117     31945346 :   switch (radix)
     118              :     {
     119        15638 :     case 2:
     120        15638 :       r = ('0' <= c && c <= '1');
     121        15638 :       break;
     122              : 
     123        19182 :     case 8:
     124        19182 :       r = ('0' <= c && c <= '7');
     125        19182 :       break;
     126              : 
     127     31845653 :     case 10:
     128     31845653 :       r = ('0' <= c && c <= '9');
     129     31845653 :       break;
     130              : 
     131        64873 :     case 16:
     132        64873 :       r = ISXDIGIT (c);
     133        64873 :       break;
     134              : 
     135            0 :     default:
     136            0 :       gfc_internal_error ("gfc_check_digit(): bad radix");
     137              :     }
     138              : 
     139     31945346 :   return r;
     140              : }
     141              : 
     142              : 
     143              : /* Match the digit string part of an integer if signflag is not set,
     144              :    the signed digit string part if signflag is set.  If the buffer
     145              :    is NULL, we just count characters for the resolution pass.  Returns
     146              :    the number of characters matched, -1 for no match.  */
     147              : 
     148              : static int
     149     18680416 : match_digits (int signflag, int radix, char *buffer)
     150              : {
     151     18680416 :   locus old_loc;
     152     18680416 :   int length;
     153     18680416 :   char c;
     154              : 
     155     18680416 :   length = 0;
     156     18680416 :   c = gfc_next_ascii_char ();
     157              : 
     158     18680416 :   if (signflag && (c == '+' || c == '-'))
     159              :     {
     160         5716 :       if (buffer != NULL)
     161         2285 :         *buffer++ = c;
     162         5716 :       gfc_gobble_whitespace ();
     163         5716 :       c = gfc_next_ascii_char ();
     164         5716 :       length++;
     165              :     }
     166              : 
     167     18680416 :   if (!gfc_check_digit (c, radix))
     168              :     return -1;
     169              : 
     170      8959543 :   length++;
     171      8959543 :   if (buffer != NULL)
     172      4471042 :     *buffer++ = c;
     173              : 
     174     17519853 :   for (;;)
     175              :     {
     176     13239698 :       old_loc = gfc_current_locus;
     177     13239698 :       c = gfc_next_ascii_char ();
     178              : 
     179     13239698 :       if (!gfc_check_digit (c, radix))
     180              :         break;
     181              : 
     182      4280155 :       if (buffer != NULL)
     183      2137242 :         *buffer++ = c;
     184      4280155 :       length++;
     185              :     }
     186              : 
     187      8959543 :   gfc_current_locus = old_loc;
     188              : 
     189      8959543 :   return length;
     190              : }
     191              : 
     192              : /* Convert an integer string to an expression node.  */
     193              : 
     194              : static gfc_expr *
     195      4362852 : convert_integer (const char *buffer, int kind, int radix, locus *where)
     196              : {
     197      4362852 :   gfc_expr *e;
     198      4362852 :   const char *t;
     199              : 
     200      4362852 :   e = gfc_get_constant_expr (BT_INTEGER, kind, where);
     201              :   /* A leading plus is allowed, but not by mpz_set_str.  */
     202      4362852 :   if (buffer[0] == '+')
     203           21 :     t = buffer + 1;
     204              :   else
     205              :     t = buffer;
     206      4362852 :   mpz_set_str (e->value.integer, t, radix);
     207              : 
     208      4362852 :   return e;
     209              : }
     210              : 
     211              : 
     212              : /* Convert an unsigned string to an expression node.  XXX:
     213              :    This needs a calculation modulo 2^n.  TODO: Implement restriction
     214              :    that no unary minus is permitted.  */
     215              : static gfc_expr *
     216       102290 : convert_unsigned (const char *buffer, int kind, int radix, locus *where)
     217              : {
     218       102290 :   gfc_expr *e;
     219       102290 :   const char *t;
     220       102290 :   int k;
     221       102290 :   arith rc;
     222              : 
     223       102290 :   e = gfc_get_constant_expr (BT_UNSIGNED, kind, where);
     224              :   /* A leading plus is allowed, but not by mpz_set_str.  */
     225       102290 :   if (buffer[0] == '+')
     226            0 :     t = buffer + 1;
     227              :   else
     228              :     t = buffer;
     229              : 
     230       102290 :   mpz_set_str (e->value.integer, t, radix);
     231              : 
     232       102290 :   k = gfc_validate_kind (BT_UNSIGNED, kind, false);
     233              : 
     234              :   /* TODO Maybe move this somewhere else.  */
     235       102290 :   rc = gfc_range_check (e);
     236       102290 :   if (rc != ARITH_OK)
     237              :     {
     238            2 :     if (pedantic)
     239            1 :       gfc_error_now (gfc_arith_error (rc), &e->where);
     240              :     else
     241            1 :       gfc_warning (0, gfc_arith_error (rc), &e->where);
     242              :     }
     243              : 
     244       102290 :   gfc_convert_mpz_to_unsigned (e->value.integer, gfc_unsigned_kinds[k].bit_size,
     245              :                                false);
     246              : 
     247       102290 :   return e;
     248              : }
     249              : 
     250              : /* Convert a real string to an expression node.  */
     251              : 
     252              : static gfc_expr *
     253       224142 : convert_real (const char *buffer, int kind, locus *where)
     254              : {
     255       224142 :   gfc_expr *e;
     256              : 
     257       224142 :   e = gfc_get_constant_expr (BT_REAL, kind, where);
     258       224142 :   mpfr_set_str (e->value.real, buffer, 10, GFC_RND_MODE);
     259              : 
     260       224142 :   return e;
     261              : }
     262              : 
     263              : 
     264              : /* Convert a pair of real, constant expression nodes to a single
     265              :    complex expression node.  */
     266              : 
     267              : static gfc_expr *
     268         7109 : convert_complex (gfc_expr *real, gfc_expr *imag, int kind)
     269              : {
     270         7109 :   gfc_expr *e;
     271              : 
     272         7109 :   e = gfc_get_constant_expr (BT_COMPLEX, kind, &real->where);
     273         7109 :   mpc_set_fr_fr (e->value.complex, real->value.real, imag->value.real,
     274              :                  GFC_MPC_RND_MODE);
     275              : 
     276         7109 :   return e;
     277              : }
     278              : 
     279              : 
     280              : /* Match an integer (digit string and optional kind).
     281              :    A sign will be accepted if signflag is set.  */
     282              : 
     283              : static match
     284     13607517 : match_integer_constant (gfc_expr **result, int signflag)
     285              : {
     286     13607517 :   int length, kind, is_iso_c;
     287     13607517 :   locus old_loc;
     288     13607517 :   char *buffer;
     289     13607517 :   gfc_expr *e;
     290              : 
     291     13607517 :   old_loc = gfc_current_locus;
     292     13607517 :   gfc_gobble_whitespace ();
     293              : 
     294     13607517 :   length = match_digits (signflag, 10, NULL);
     295     13607517 :   gfc_current_locus = old_loc;
     296     13607517 :   if (length == -1)
     297              :     return MATCH_NO;
     298              : 
     299      4364586 :   buffer = (char *) alloca (length + 1);
     300      4364586 :   memset (buffer, '\0', length + 1);
     301              : 
     302      4364586 :   gfc_gobble_whitespace ();
     303              : 
     304      4364586 :   match_digits (signflag, 10, buffer);
     305              : 
     306      4364586 :   kind = get_kind (&is_iso_c);
     307      4364586 :   if (kind == -2)
     308      4055540 :     kind = gfc_default_integer_kind;
     309      4364586 :   if (kind == -1)
     310              :     return MATCH_ERROR;
     311              : 
     312      4362856 :   if (kind == 4 && flag_integer4_kind == 8)
     313            0 :     kind = 8;
     314              : 
     315      4362856 :   if (gfc_validate_kind (BT_INTEGER, kind, true) < 0)
     316              :     {
     317            4 :       gfc_error ("Integer kind %d at %C not available", kind);
     318            4 :       return MATCH_ERROR;
     319              :     }
     320              : 
     321      4362852 :   e = convert_integer (buffer, kind, 10, &gfc_current_locus);
     322      4362852 :   e->ts.is_c_interop = is_iso_c;
     323              : 
     324      4362852 :   if (gfc_range_check (e) != ARITH_OK)
     325              :     {
     326         9892 :       gfc_error ("Integer too big for its kind at %C. This check can be "
     327              :                  "disabled with the option %<-fno-range-check%>");
     328              : 
     329         9892 :       gfc_free_expr (e);
     330         9892 :       return MATCH_ERROR;
     331              :     }
     332              : 
     333      4352960 :   *result = e;
     334      4352960 :   return MATCH_YES;
     335              : }
     336              : 
     337              : /* Match an unsigned constant (an integer with suffix u).  No sign
     338              :    is currently accepted, in accordance with 24-116.txt, but that
     339              :    could be changed later.  This is very much like the integer
     340              :    constant matching above, but with enough differences to put it into
     341              :    its own function.  */
     342              : 
     343              : static match
     344       597690 : match_unsigned_constant (gfc_expr **result)
     345              : {
     346       597690 :   int length, kind, is_iso_c;
     347       597690 :   locus old_loc;
     348       597690 :   char *buffer;
     349       597690 :   gfc_expr *e;
     350       597690 :   match m;
     351              : 
     352       597690 :   old_loc = gfc_current_locus;
     353       597690 :   gfc_gobble_whitespace ();
     354              : 
     355       597690 :   length = match_digits (/* signflag = */ false, 10, NULL);
     356              : 
     357       597690 :   if (length == -1)
     358       477942 :     goto fail;
     359              : 
     360       119748 :   m = gfc_match_char ('u');
     361       119748 :   if (m == MATCH_NO)
     362        17458 :     goto fail;
     363              : 
     364       102290 :   gfc_current_locus = old_loc;
     365              : 
     366       102290 :   buffer = (char *) alloca (length + 1);
     367       102290 :   memset (buffer, '\0', length + 1);
     368              : 
     369       102290 :   gfc_gobble_whitespace ();
     370              : 
     371       102290 :   match_digits (false, 10, buffer);
     372              : 
     373       102290 :   m = gfc_match_char ('u');
     374       102290 :   if (m == MATCH_NO)
     375            0 :     goto fail;
     376              : 
     377       102290 :   kind = get_kind (&is_iso_c);
     378       102290 :   if (kind == -2)
     379         8357 :     kind = gfc_default_unsigned_kind;
     380       102290 :   if (kind == -1)
     381              :     return MATCH_ERROR;
     382              : 
     383       102290 :   if (kind == 4 && flag_integer4_kind == 8)
     384            0 :     kind = 8;
     385              : 
     386       102290 :   if (gfc_validate_kind (BT_UNSIGNED, kind, true) < 0)
     387              :     {
     388            0 :       gfc_error ("Unsigned kind %d at %C not available", kind);
     389            0 :       return MATCH_ERROR;
     390              :     }
     391              : 
     392       102290 :   e = convert_unsigned (buffer, kind, 10, &gfc_current_locus);
     393       102290 :   e->ts.is_c_interop = is_iso_c;
     394              : 
     395       102290 :   *result = e;
     396       102290 :   return MATCH_YES;
     397              : 
     398       495400 :  fail:
     399       495400 :   gfc_current_locus = old_loc;
     400       495400 :   return MATCH_NO;
     401              : }
     402              : 
     403              : /* Match a Hollerith constant.  */
     404              : 
     405              : static match
     406      6809395 : match_hollerith_constant (gfc_expr **result)
     407              : {
     408      6809395 :   locus old_loc;
     409      6809395 :   gfc_expr *e = NULL;
     410      6809395 :   int num, pad;
     411      6809395 :   int i;
     412              : 
     413      6809395 :   old_loc = gfc_current_locus;
     414      6809395 :   gfc_gobble_whitespace ();
     415              : 
     416      6809395 :   if (match_integer_constant (&e, 0) == MATCH_YES
     417      6809395 :       && gfc_match_char ('h') == MATCH_YES)
     418              :     {
     419         2636 :       if (!gfc_notify_std (GFC_STD_LEGACY, "Hollerith constant at %C"))
     420           14 :         goto cleanup;
     421              : 
     422         2622 :       if (gfc_extract_int (e, &num, 1))
     423            0 :         goto cleanup;
     424         2622 :       if (num == 0)
     425              :         {
     426            1 :           gfc_error ("Invalid Hollerith constant: %L must contain at least "
     427              :                      "one character", &old_loc);
     428            1 :           goto cleanup;
     429              :         }
     430         2621 :       if (e->ts.kind != gfc_default_integer_kind)
     431              :         {
     432            1 :           gfc_error ("Invalid Hollerith constant: Integer kind at %L "
     433              :                      "should be default", &old_loc);
     434            1 :           goto cleanup;
     435              :         }
     436              :       else
     437              :         {
     438         2620 :           gfc_free_expr (e);
     439         2620 :           e = gfc_get_constant_expr (BT_HOLLERITH, gfc_default_character_kind,
     440              :                                      &gfc_current_locus);
     441              : 
     442              :           /* Calculate padding needed to fit default integer memory.  */
     443         2620 :           pad = gfc_default_integer_kind - (num % gfc_default_integer_kind);
     444              : 
     445         2620 :           e->representation.string = XCNEWVEC (char, num + pad + 1);
     446              : 
     447        14886 :           for (i = 0; i < num; i++)
     448              :             {
     449        12266 :               gfc_char_t c = gfc_next_char_literal (INSTRING_WARN);
     450        12266 :               if (! gfc_wide_fits_in_byte (c))
     451              :                 {
     452            0 :                   gfc_error ("Invalid Hollerith constant at %L contains a "
     453              :                              "wide character", &old_loc);
     454            0 :                   goto cleanup;
     455              :                 }
     456              : 
     457        12266 :               e->representation.string[i] = (unsigned char) c;
     458              :             }
     459              : 
     460              :           /* Now pad with blanks and end with a null char.  */
     461        11762 :           for (i = 0; i < pad; i++)
     462         9142 :             e->representation.string[num + i] = ' ';
     463              : 
     464         2620 :           e->representation.string[num + i] = '\0';
     465         2620 :           e->representation.length = num + pad;
     466         2620 :           e->ts.u.pad = pad;
     467              : 
     468         2620 :           *result = e;
     469         2620 :           return MATCH_YES;
     470              :         }
     471              :     }
     472              : 
     473      6806759 :   gfc_free_expr (e);
     474      6806759 :   gfc_current_locus = old_loc;
     475      6806759 :   return MATCH_NO;
     476              : 
     477           16 : cleanup:
     478           16 :   gfc_free_expr (e);
     479           16 :   return MATCH_ERROR;
     480              : }
     481              : 
     482              : 
     483              : /* Match a binary, octal or hexadecimal constant that can be found in
     484              :    a DATA statement.  The standard permits b'010...', o'73...', and
     485              :    z'a1...' where b, o, and z can be capital letters.  This function
     486              :    also accepts postfixed forms of the constants: '01...'b, '73...'o,
     487              :    and 'a1...'z.  An additional extension is the use of x for z.  */
     488              : 
     489              : static match
     490      7023107 : match_boz_constant (gfc_expr **result)
     491              : {
     492      7023107 :   int radix, length, x_hex;
     493      7023107 :   locus old_loc, start_loc;
     494      7023107 :   char *buffer, post, delim;
     495      7023107 :   gfc_expr *e;
     496              : 
     497      7023107 :   start_loc = old_loc = gfc_current_locus;
     498      7023107 :   gfc_gobble_whitespace ();
     499              : 
     500      7023107 :   x_hex = 0;
     501      7023107 :   switch (post = gfc_next_ascii_char ())
     502              :     {
     503              :     case 'b':
     504              :       radix = 2;
     505              :       post = 0;
     506              :       break;
     507        60320 :     case 'o':
     508        60320 :       radix = 8;
     509        60320 :       post = 0;
     510        60320 :       break;
     511        96064 :     case 'x':
     512        96064 :       x_hex = 1;
     513              :       /* Fall through.  */
     514              :     case 'z':
     515              :       radix = 16;
     516              :       post = 0;
     517              :       break;
     518              :     case '\'':
     519              :       /* Fall through.  */
     520              :     case '\"':
     521              :       delim = post;
     522              :       post = 1;
     523              :       radix = 16;  /* Set to accept any valid digit string.  */
     524              :       break;
     525      6723901 :     default:
     526      6723901 :       goto backup;
     527              :     }
     528              : 
     529              :   /* No whitespace allowed here.  */
     530              : 
     531        60320 :   if (post == 0)
     532       299181 :     delim = gfc_next_ascii_char ();
     533              : 
     534       299206 :   if (delim != '\'' && delim != '\"')
     535       295037 :     goto backup;
     536              : 
     537         4169 :   if (x_hex
     538         4169 :       && gfc_invalid_boz (G_("Hexadecimal constant at %L uses "
     539              :                           "nonstandard X instead of Z"), &gfc_current_locus))
     540              :     return MATCH_ERROR;
     541              : 
     542         4167 :   old_loc = gfc_current_locus;
     543              : 
     544         4167 :   length = match_digits (0, radix, NULL);
     545         4167 :   if (length == -1)
     546              :     {
     547            0 :       gfc_error ("Empty set of digits in BOZ constant at %C");
     548            0 :       return MATCH_ERROR;
     549              :     }
     550              : 
     551         4167 :   if (gfc_next_ascii_char () != delim)
     552              :     {
     553            0 :       gfc_error ("Illegal character in BOZ constant at %C");
     554            0 :       return MATCH_ERROR;
     555              :     }
     556              : 
     557         4167 :   if (post == 1)
     558              :     {
     559           25 :       switch (gfc_next_ascii_char ())
     560              :         {
     561              :         case 'b':
     562              :           radix = 2;
     563              :           break;
     564            6 :         case 'o':
     565            6 :           radix = 8;
     566            6 :           break;
     567           13 :         case 'x':
     568              :           /* Fall through.  */
     569           13 :         case 'z':
     570           13 :           radix = 16;
     571           13 :           break;
     572            0 :         default:
     573            0 :           goto backup;
     574              :         }
     575              : 
     576           25 :       if (gfc_invalid_boz (G_("BOZ constant at %C uses nonstandard postfix "
     577              :                            "syntax"), &gfc_current_locus))
     578              :         return MATCH_ERROR;
     579              :     }
     580              : 
     581         4166 :   gfc_current_locus = old_loc;
     582              : 
     583         4166 :   buffer = (char *) alloca (length + 1);
     584         4166 :   memset (buffer, '\0', length + 1);
     585              : 
     586         4166 :   match_digits (0, radix, buffer);
     587         4166 :   gfc_next_ascii_char ();    /* Eat delimiter.  */
     588         4166 :   if (post == 1)
     589           24 :     gfc_next_ascii_char ();  /* Eat postfixed b, o, z, or x.  */
     590              : 
     591         4166 :   e = gfc_get_expr ();
     592         4166 :   e->expr_type = EXPR_CONSTANT;
     593         4166 :   e->ts.type = BT_BOZ;
     594         4166 :   e->where = gfc_current_locus;
     595         4166 :   e->boz.rdx = radix;
     596         4166 :   e->boz.len = length;
     597         4166 :   e->boz.str = XCNEWVEC (char, length + 1);
     598         4166 :   strncpy (e->boz.str, buffer, length);
     599              : 
     600         4166 :   if (!gfc_in_match_data ()
     601         4166 :       && (!gfc_notify_std(GFC_STD_F2003, "BOZ used outside a DATA "
     602              :                           "statement at %L", &e->where)))
     603              :     return MATCH_ERROR;
     604              : 
     605         4161 :   *result = e;
     606         4161 :   return MATCH_YES;
     607              : 
     608      7018938 : backup:
     609      7018938 :   gfc_current_locus = start_loc;
     610      7018938 :   return MATCH_NO;
     611              : }
     612              : 
     613              : 
     614              : /* Match a real constant of some sort.  Allow a signed constant if signflag
     615              :    is nonzero.  */
     616              : 
     617              : static match
     618      7127204 : match_real_constant (gfc_expr **result, int signflag)
     619              : {
     620      7127204 :   int kind, count, seen_dp, seen_digits, is_iso_c, default_exponent;
     621      7127204 :   locus old_loc, temp_loc;
     622      7127204 :   char *p, *buffer, c, exp_char;
     623      7127204 :   gfc_expr *e;
     624      7127204 :   bool negate;
     625              : 
     626      7127204 :   old_loc = gfc_current_locus;
     627      7127204 :   gfc_gobble_whitespace ();
     628              : 
     629      7127204 :   e = NULL;
     630              : 
     631      7127204 :   default_exponent = 0;
     632      7127204 :   count = 0;
     633      7127204 :   seen_dp = 0;
     634      7127204 :   seen_digits = 0;
     635      7127204 :   exp_char = ' ';
     636      7127204 :   negate = false;
     637              : 
     638      7127204 :   c = gfc_next_ascii_char ();
     639      7127204 :   if (signflag && (c == '+' || c == '-'))
     640              :     {
     641         6516 :       if (c == '-')
     642         6380 :         negate = true;
     643              : 
     644         6516 :       gfc_gobble_whitespace ();
     645         6516 :       c = gfc_next_ascii_char ();
     646              :     }
     647              : 
     648              :   /* Scan significand.  */
     649      4050203 :   for (;; c = gfc_next_ascii_char (), count++)
     650              :     {
     651     11177407 :       if (c == '.')
     652              :         {
     653       284983 :           if (seen_dp)
     654          204 :             goto done;
     655              : 
     656              :           /* Check to see if "." goes with a following operator like
     657              :              ".eq.".  */
     658       284779 :           temp_loc = gfc_current_locus;
     659       284779 :           c = gfc_next_ascii_char ();
     660              : 
     661       284779 :           if (c == 'e' || c == 'd' || c == 'q')
     662              :             {
     663        17768 :               c = gfc_next_ascii_char ();
     664        17768 :               if (c == '.')
     665            0 :                 goto done;      /* Operator named .e. or .d.  */
     666              :             }
     667              : 
     668       284779 :           if (ISALPHA (c))
     669        67920 :             goto done;          /* Distinguish 1.e9 from 1.eq.2 */
     670              : 
     671       216859 :           gfc_current_locus = temp_loc;
     672       216859 :           seen_dp = 1;
     673       216859 :           continue;
     674              :         }
     675              : 
     676     10892424 :       if (ISDIGIT (c))
     677              :         {
     678      3833344 :           seen_digits = 1;
     679      3833344 :           continue;
     680              :         }
     681              : 
     682      7059080 :       break;
     683              :     }
     684              : 
     685      7059080 :   if (!seen_digits || (c != 'e' && c != 'd' && c != 'q'))
     686      2422062 :     goto done;
     687        38956 :   exp_char = c;
     688              : 
     689              : 
     690        38956 :   if (c == 'q')
     691              :     {
     692            0 :       if (!gfc_notify_std (GFC_STD_GNU, "exponent-letter %<q%> in "
     693              :                            "real-literal-constant at %C"))
     694              :         return MATCH_ERROR;
     695            0 :       else if (warn_real_q_constant)
     696            0 :         gfc_warning (OPT_Wreal_q_constant,
     697              :                      "Extension: exponent-letter %<q%> in real-literal-constant "
     698              :                      "at %C");
     699              :     }
     700              : 
     701              :   /* Scan exponent.  */
     702        38956 :   c = gfc_next_ascii_char ();
     703        38956 :   count++;
     704              : 
     705        38956 :   if (c == '+' || c == '-')
     706              :     {                           /* optional sign */
     707         7807 :       c = gfc_next_ascii_char ();
     708         7807 :       count++;
     709              :     }
     710              : 
     711        38956 :   if (!ISDIGIT (c))
     712              :     {
     713              :       /* With -fdec, default exponent to 0 instead of complaining.  */
     714           40 :       if (flag_dec)
     715        38946 :         default_exponent = 1;
     716              :       else
     717              :         {
     718           10 :           gfc_error ("Missing exponent in real number at %C");
     719           10 :           return MATCH_ERROR;
     720              :         }
     721              :     }
     722              : 
     723        80963 :   while (ISDIGIT (c))
     724              :     {
     725        42017 :       c = gfc_next_ascii_char ();
     726        42017 :       count++;
     727              :     }
     728              : 
     729      7127194 : done:
     730              :   /* Check that we have a numeric constant.  */
     731      7127194 :   if (!seen_digits || (!seen_dp && exp_char == ' '))
     732              :     {
     733      6903048 :       gfc_current_locus = old_loc;
     734      6903048 :       return MATCH_NO;
     735              :     }
     736              : 
     737              :   /* Convert the number.  */
     738       224146 :   gfc_current_locus = old_loc;
     739       224146 :   gfc_gobble_whitespace ();
     740              : 
     741       224146 :   buffer = (char *) alloca (count + default_exponent + 1);
     742       224146 :   memset (buffer, '\0', count + default_exponent + 1);
     743              : 
     744       224146 :   p = buffer;
     745       224146 :   c = gfc_next_ascii_char ();
     746       224146 :   if (c == '+' || c == '-')
     747              :     {
     748         3085 :       gfc_gobble_whitespace ();
     749         3085 :       c = gfc_next_ascii_char ();
     750              :     }
     751              : 
     752              :   /* Hack for mpfr_set_str().  */
     753      1454458 :   for (;;)
     754              :     {
     755       839302 :       if (c == 'd' || c == 'q')
     756              :         *p = 'e';
     757              :       else
     758       808753 :         *p = c;
     759       839302 :       p++;
     760       839302 :       if (--count == 0)
     761              :         break;
     762              : 
     763       615156 :       c = gfc_next_ascii_char ();
     764              :     }
     765       224146 :   if (default_exponent)
     766           30 :     *p++ = '0';
     767              : 
     768       224146 :   kind = get_kind (&is_iso_c);
     769       224146 :   if (kind == -1)
     770            4 :     goto cleanup;
     771              : 
     772       224142 :   if (kind == 4)
     773              :     {
     774        20952 :       if (flag_real4_kind == 8)
     775          192 :         kind = 8;
     776        20952 :       if (flag_real4_kind == 10)
     777          192 :         kind = 10;
     778        20952 :       if (flag_real4_kind == 16)
     779          384 :         kind = 16;
     780              :     }
     781       203190 :   else if (kind == 8)
     782              :     {
     783        28174 :       if (flag_real8_kind == 4)
     784          192 :         kind = 4;
     785        28174 :       if (flag_real8_kind == 10)
     786          192 :         kind = 10;
     787        28174 :       if (flag_real8_kind == 16)
     788          384 :         kind = 16;
     789              :     }
     790              : 
     791       224142 :   switch (exp_char)
     792              :     {
     793        30549 :     case 'd':
     794        30549 :       if (kind != -2)
     795              :         {
     796            0 :           gfc_error ("Real number at %C has a %<d%> exponent and an explicit "
     797              :                      "kind");
     798            0 :           goto cleanup;
     799              :         }
     800        30549 :       kind = gfc_default_double_kind;
     801        30549 :       break;
     802              : 
     803            0 :     case 'q':
     804            0 :       if (kind != -2)
     805              :         {
     806            0 :           gfc_error ("Real number at %C has a %<q%> exponent and an explicit "
     807              :                      "kind");
     808            0 :           goto cleanup;
     809              :         }
     810              : 
     811              :       /* The maximum possible real kind type parameter is 16.  First, try
     812              :          that for the kind, then fallback to trying kind=10 (Intel 80 bit)
     813              :          extended precision.  If neither value works, just given up.  */
     814            0 :       kind = 16;
     815            0 :       if (gfc_validate_kind (BT_REAL, kind, true) < 0)
     816              :         {
     817            0 :           kind = 10;
     818            0 :           if (gfc_validate_kind (BT_REAL, kind, true) < 0)
     819              :             {
     820            0 :               gfc_error ("Invalid exponent-letter %<q%> in "
     821              :                          "real-literal-constant at %C");
     822            0 :               goto cleanup;
     823              :             }
     824              :         }
     825              :       break;
     826              : 
     827       193593 :     default:
     828       193593 :       if (kind == -2)
     829       119975 :         kind = gfc_default_real_kind;
     830              : 
     831       193593 :       if (gfc_validate_kind (BT_REAL, kind, true) < 0)
     832              :         {
     833            0 :           gfc_error ("Invalid real kind %d at %C", kind);
     834            0 :           goto cleanup;
     835              :         }
     836              :     }
     837              : 
     838       224142 :   e = convert_real (buffer, kind, &gfc_current_locus);
     839       224142 :   if (negate)
     840         2980 :     mpfr_neg (e->value.real, e->value.real, GFC_RND_MODE);
     841       224142 :   e->ts.is_c_interop = is_iso_c;
     842              : 
     843       224142 :   switch (gfc_range_check (e))
     844              :     {
     845              :     case ARITH_OK:
     846              :       break;
     847            1 :     case ARITH_OVERFLOW:
     848            1 :       gfc_error ("Real constant overflows its kind at %C");
     849            1 :       goto cleanup;
     850              : 
     851            0 :     case ARITH_UNDERFLOW:
     852            0 :       if (warn_underflow)
     853            0 :         gfc_warning (OPT_Wunderflow, "Real constant underflows its kind at %C");
     854            0 :       mpfr_set_ui (e->value.real, 0, GFC_RND_MODE);
     855            0 :       break;
     856              : 
     857            0 :     default:
     858            0 :       gfc_internal_error ("gfc_range_check() returned bad value");
     859              :     }
     860              : 
     861              :   /* Warn about trailing digits which suggest the user added too many
     862              :      trailing digits, which may cause the appearance of higher precision
     863              :      than the kind can support.
     864              : 
     865              :      This is done by replacing the rightmost non-zero digit with zero
     866              :      and comparing with the original value.  If these are equal, we
     867              :      assume the user supplied more digits than intended (or forgot to
     868              :      convert to the correct kind).
     869              :   */
     870              : 
     871       224141 :   if (warn_conversion_extra)
     872              :     {
     873           21 :       mpfr_t r;
     874           21 :       char *c1;
     875           21 :       bool did_break;
     876              : 
     877           21 :       c1 = strchr (buffer, 'e');
     878           21 :       if (c1 == NULL)
     879           18 :         c1 = buffer + strlen(buffer);
     880              : 
     881           21 :       did_break = false;
     882           30 :       for (p = c1; p > buffer;)
     883              :         {
     884           30 :           p--;
     885           30 :           if (*p == '.')
     886            7 :             continue;
     887              : 
     888           23 :           if (*p != '0')
     889              :             {
     890           21 :               *p = '0';
     891           21 :               did_break = true;
     892           21 :               break;
     893              :             }
     894              :         }
     895              : 
     896           21 :       if (did_break)
     897              :         {
     898           21 :           mpfr_init (r);
     899           21 :           mpfr_set_str (r, buffer, 10, GFC_RND_MODE);
     900           21 :           if (negate)
     901            0 :             mpfr_neg (r, r, GFC_RND_MODE);
     902              : 
     903           21 :           mpfr_sub (r, r, e->value.real, GFC_RND_MODE);
     904              : 
     905           21 :           if (mpfr_cmp_ui (r, 0) == 0)
     906            1 :             gfc_warning (OPT_Wconversion_extra, "Non-significant digits "
     907              :                          "in %qs number at %C, maybe incorrect KIND",
     908              :                          gfc_typename (&e->ts));
     909              : 
     910           21 :           mpfr_clear (r);
     911              :         }
     912              :     }
     913              : 
     914       224141 :   *result = e;
     915       224141 :   return MATCH_YES;
     916              : 
     917            5 : cleanup:
     918            5 :   gfc_free_expr (e);
     919            5 :   return MATCH_ERROR;
     920              : }
     921              : 
     922              : 
     923              : /* Match a substring reference.  */
     924              : 
     925              : static match
     926       630320 : match_substring (gfc_charlen *cl, int init, gfc_ref **result, bool deferred)
     927              : {
     928       630320 :   gfc_expr *start, *end;
     929       630320 :   locus old_loc;
     930       630320 :   gfc_ref *ref;
     931       630320 :   match m;
     932              : 
     933       630320 :   start = NULL;
     934       630320 :   end = NULL;
     935              : 
     936       630320 :   old_loc = gfc_current_locus;
     937              : 
     938       630320 :   m = gfc_match_char ('(');
     939       630320 :   if (m != MATCH_YES)
     940              :     return MATCH_NO;
     941              : 
     942        17438 :   if (gfc_match_char (':') != MATCH_YES)
     943              :     {
     944        16560 :       if (init)
     945            0 :         m = gfc_match_init_expr (&start);
     946              :       else
     947        16560 :         m = gfc_match_expr (&start);
     948              : 
     949        16560 :       if (m != MATCH_YES)
     950              :         {
     951          154 :           m = MATCH_NO;
     952          154 :           goto cleanup;
     953              :         }
     954              : 
     955        16406 :       m = gfc_match_char (':');
     956        16406 :       if (m != MATCH_YES)
     957          460 :         goto cleanup;
     958              :     }
     959              : 
     960        16824 :   if (gfc_match_char (')') != MATCH_YES)
     961              :     {
     962        15895 :       if (init)
     963            0 :         m = gfc_match_init_expr (&end);
     964              :       else
     965        15895 :         m = gfc_match_expr (&end);
     966              : 
     967        15895 :       if (m == MATCH_NO)
     968            2 :         goto syntax;
     969        15893 :       if (m == MATCH_ERROR)
     970            0 :         goto cleanup;
     971              : 
     972        15893 :       m = gfc_match_char (')');
     973        15893 :       if (m == MATCH_NO)
     974            3 :         goto syntax;
     975              :     }
     976              : 
     977              :   /* Optimize away the (:) reference.  */
     978        16819 :   if (start == NULL && end == NULL && !deferred)
     979              :     ref = NULL;
     980              :   else
     981              :     {
     982        16614 :       ref = gfc_get_ref ();
     983              : 
     984        16614 :       ref->type = REF_SUBSTRING;
     985        16614 :       if (start == NULL)
     986          671 :         start = gfc_get_int_expr (gfc_charlen_int_kind, NULL, 1);
     987        16614 :       ref->u.ss.start = start;
     988        16614 :       if (end == NULL && cl)
     989          722 :         end = gfc_copy_expr (cl->length);
     990        16614 :       ref->u.ss.end = end;
     991        16614 :       ref->u.ss.length = cl;
     992              :     }
     993              : 
     994        16819 :   *result = ref;
     995        16819 :   return MATCH_YES;
     996              : 
     997            5 : syntax:
     998            5 :   gfc_error ("Syntax error in SUBSTRING specification at %C");
     999            5 :   m = MATCH_ERROR;
    1000              : 
    1001          619 : cleanup:
    1002          619 :   gfc_free_expr (start);
    1003          619 :   gfc_free_expr (end);
    1004              : 
    1005          619 :   gfc_current_locus = old_loc;
    1006          619 :   return m;
    1007              : }
    1008              : 
    1009              : 
    1010              : /* Reads the next character of a string constant, taking care to
    1011              :    return doubled delimiters on the input as a single instance of
    1012              :    the delimiter.
    1013              : 
    1014              :    Special return values for "ret" argument are:
    1015              :      -1   End of the string, as determined by the delimiter
    1016              :      -2   Unterminated string detected
    1017              : 
    1018              :    Backslash codes are also expanded at this time.  */
    1019              : 
    1020              : static gfc_char_t
    1021      4361969 : next_string_char (gfc_char_t delimiter, int *ret)
    1022              : {
    1023      4361969 :   locus old_locus;
    1024      4361969 :   gfc_char_t c;
    1025              : 
    1026      4361969 :   c = gfc_next_char_literal (INSTRING_WARN);
    1027      4361969 :   *ret = 0;
    1028              : 
    1029      4361969 :   if (c == '\n')
    1030              :     {
    1031            4 :       *ret = -2;
    1032            4 :       return 0;
    1033              :     }
    1034              : 
    1035      4361965 :   if (flag_backslash && c == '\\')
    1036              :     {
    1037        12180 :       old_locus = gfc_current_locus;
    1038              : 
    1039        12180 :       if (gfc_match_special_char (&c) == MATCH_NO)
    1040            0 :         gfc_current_locus = old_locus;
    1041              : 
    1042        12180 :       if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
    1043            0 :         gfc_warning (0, "Extension: backslash character at %C");
    1044              :     }
    1045              : 
    1046      4361965 :   if (c != delimiter)
    1047              :     return c;
    1048              : 
    1049       626498 :   old_locus = gfc_current_locus;
    1050       626498 :   c = gfc_next_char_literal (NONSTRING);
    1051              : 
    1052       626498 :   if (c == delimiter)
    1053              :     return c;
    1054       625680 :   gfc_current_locus = old_locus;
    1055              : 
    1056       625680 :   *ret = -1;
    1057       625680 :   return 0;
    1058              : }
    1059              : 
    1060              : 
    1061              : /* Special case of gfc_match_name() that matches a parameter kind name
    1062              :    before a string constant.  This takes case of the weird but legal
    1063              :    case of:
    1064              : 
    1065              :      kind_____'string'
    1066              : 
    1067              :    where kind____ is a parameter. gfc_match_name() will happily slurp
    1068              :    up all the underscores, which leads to problems.  If we return
    1069              :    MATCH_YES, the parse pointer points to the final underscore, which
    1070              :    is not part of the name.  We never return MATCH_ERROR-- errors in
    1071              :    the name will be detected later.  */
    1072              : 
    1073              : static match
    1074      4588401 : match_charkind_name (char *name)
    1075              : {
    1076      4588401 :   locus old_loc;
    1077      4588401 :   char c, peek;
    1078      4588401 :   int len;
    1079              : 
    1080      4588401 :   gfc_gobble_whitespace ();
    1081      4588401 :   c = gfc_next_ascii_char ();
    1082      4588401 :   if (!ISALPHA (c))
    1083              :     return MATCH_NO;
    1084              : 
    1085      4171541 :   *name++ = c;
    1086      4171541 :   len = 1;
    1087              : 
    1088     17007948 :   for (;;)
    1089              :     {
    1090     17007948 :       old_loc = gfc_current_locus;
    1091     17007948 :       c = gfc_next_ascii_char ();
    1092              : 
    1093     17007948 :       if (c == '_')
    1094              :         {
    1095       552918 :           peek = gfc_peek_ascii_char ();
    1096              : 
    1097       552918 :           if (peek == '\'' || peek == '\"')
    1098              :             {
    1099          996 :               gfc_current_locus = old_loc;
    1100          996 :               *name = '\0';
    1101          996 :               return MATCH_YES;
    1102              :             }
    1103              :         }
    1104              : 
    1105     17006952 :       if (!ISALNUM (c)
    1106      4722467 :           && c != '_'
    1107      4170545 :           && (c != '$' || !flag_dollar_ok))
    1108              :         break;
    1109              : 
    1110     12836407 :       *name++ = c;
    1111     12836407 :       if (++len > GFC_MAX_SYMBOL_LEN)
    1112              :         break;
    1113              :     }
    1114              : 
    1115              :   return MATCH_NO;
    1116              : }
    1117              : 
    1118              : 
    1119              : /* See if the current input matches a character constant.  Lots of
    1120              :    contortions have to be done to match the kind parameter which comes
    1121              :    before the actual string.  The main consideration is that we don't
    1122              :    want to error out too quickly.  For example, we don't actually do
    1123              :    any validation of the kinds until we have actually seen a legal
    1124              :    delimiter.  Using match_kind_param() generates errors too quickly.  */
    1125              : 
    1126              : static match
    1127      7335941 : match_string_constant (gfc_expr **result)
    1128              : {
    1129      7335941 :   char name[GFC_MAX_SYMBOL_LEN + 1], peek;
    1130      7335941 :   size_t length;
    1131      7335941 :   int kind,save_warn_ampersand, ret;
    1132      7335941 :   locus old_locus, start_locus;
    1133      7335941 :   gfc_symbol *sym;
    1134      7335941 :   gfc_expr *e;
    1135      7335941 :   match m;
    1136      7335941 :   gfc_char_t c, delimiter, *p;
    1137              : 
    1138      7335941 :   old_locus = gfc_current_locus;
    1139              : 
    1140      7335941 :   gfc_gobble_whitespace ();
    1141              : 
    1142      7335941 :   c = gfc_next_char ();
    1143      7335941 :   if (c == '\'' || c == '"')
    1144              :     {
    1145       272284 :       kind = gfc_default_character_kind;
    1146       272284 :       start_locus = gfc_current_locus;
    1147       272284 :       goto got_delim;
    1148              :     }
    1149              : 
    1150      7063657 :   if (gfc_wide_is_digit (c))
    1151              :     {
    1152      2475256 :       kind = 0;
    1153              : 
    1154      5932114 :       while (gfc_wide_is_digit (c))
    1155              :         {
    1156      3470649 :           kind = kind * 10 + c - '0';
    1157      3470649 :           if (kind > 9999999)
    1158        13791 :             goto no_match;
    1159      3456858 :           c = gfc_next_char ();
    1160              :         }
    1161              : 
    1162              :     }
    1163              :   else
    1164              :     {
    1165      4588401 :       gfc_current_locus = old_locus;
    1166              : 
    1167      4588401 :       m = match_charkind_name (name);
    1168      4588401 :       if (m != MATCH_YES)
    1169      4587405 :         goto no_match;
    1170              : 
    1171          996 :       if (gfc_find_symbol (name, NULL, 1, &sym)
    1172          996 :           || sym == NULL
    1173         1991 :           || sym->attr.flavor != FL_PARAMETER)
    1174            1 :         goto no_match;
    1175              : 
    1176          995 :       kind = -1;
    1177          995 :       c = gfc_next_char ();
    1178              :     }
    1179              : 
    1180      2462460 :   if (c != '_')
    1181      2272341 :     goto no_match;
    1182              : 
    1183       190119 :   c = gfc_next_char ();
    1184       190119 :   if (c != '\'' && c != '"')
    1185       149544 :     goto no_match;
    1186              : 
    1187        40575 :   start_locus = gfc_current_locus;
    1188              : 
    1189        40575 :   if (kind == -1)
    1190              :     {
    1191          995 :       if (gfc_extract_int (sym->value, &kind, 1))
    1192              :         return MATCH_ERROR;
    1193          995 :       gfc_set_sym_referenced (sym);
    1194              :     }
    1195              : 
    1196        40575 :   if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
    1197              :     {
    1198            0 :       gfc_error ("Invalid kind %d for CHARACTER constant at %C", kind);
    1199            0 :       return MATCH_ERROR;
    1200              :     }
    1201              : 
    1202        40575 : got_delim:
    1203              :   /* Scan the string into a block of memory by first figuring out how
    1204              :      long it is, allocating the structure, then re-reading it.  This
    1205              :      isn't particularly efficient, but string constants aren't that
    1206              :      common in most code.  TODO: Use obstacks?  */
    1207              : 
    1208       312859 :   delimiter = c;
    1209       312859 :   length = 0;
    1210              : 
    1211      4049453 :   for (;;)
    1212              :     {
    1213      2181156 :       c = next_string_char (delimiter, &ret);
    1214      2181156 :       if (ret == -1)
    1215              :         break;
    1216      1868301 :       if (ret == -2)
    1217              :         {
    1218            4 :           gfc_current_locus = start_locus;
    1219            4 :           gfc_error ("Unterminated character constant beginning at %C");
    1220            4 :           return MATCH_ERROR;
    1221              :         }
    1222              : 
    1223      1868297 :       length++;
    1224              :     }
    1225              : 
    1226              :   /* Peek at the next character to see if it is a b, o, z, or x for the
    1227              :      postfixed BOZ literal constants.  */
    1228       312855 :   peek = gfc_peek_ascii_char ();
    1229       312855 :   if (peek == 'b' || peek == 'o' || peek =='z' || peek == 'x')
    1230           25 :     goto no_match;
    1231              : 
    1232       312830 :   e = gfc_get_character_expr (kind, &start_locus, NULL, length);
    1233              : 
    1234       312830 :   gfc_current_locus = start_locus;
    1235              : 
    1236              :   /* We disable the warning for the following loop as the warning has already
    1237              :      been printed in the loop above.  */
    1238       312830 :   save_warn_ampersand = warn_ampersand;
    1239       312830 :   warn_ampersand = false;
    1240              : 
    1241       312830 :   p = e->value.character.string;
    1242      2180813 :   for (size_t i = 0; i < length; i++)
    1243              :     {
    1244      1867988 :       c = next_string_char (delimiter, &ret);
    1245              : 
    1246      1867988 :       if (!gfc_check_character_range (c, kind))
    1247              :         {
    1248            5 :           gfc_free_expr (e);
    1249            5 :           gfc_error ("Character %qs in string at %C is not representable "
    1250              :                      "in character kind %d", gfc_print_wide_char (c), kind);
    1251            5 :           return MATCH_ERROR;
    1252              :         }
    1253              : 
    1254      1867983 :       *p++ = c;
    1255              :     }
    1256              : 
    1257       312825 :   *p = '\0';    /* TODO: C-style string is for development/debug purposes.  */
    1258       312825 :   warn_ampersand = save_warn_ampersand;
    1259              : 
    1260       312825 :   next_string_char (delimiter, &ret);
    1261       312825 :   if (ret != -1)
    1262            0 :     gfc_internal_error ("match_string_constant(): Delimiter not found");
    1263              : 
    1264       312825 :   if (match_substring (NULL, 0, &e->ref, false) != MATCH_NO)
    1265          307 :     e->expr_type = EXPR_SUBSTRING;
    1266              : 
    1267              :   /* Substrings with constant starting and ending points are eligible as
    1268              :      designators (F2018, section 9.1).  Simplify substrings to make them usable
    1269              :      e.g. in data statements.  */
    1270       312825 :   if (e->expr_type == EXPR_SUBSTRING
    1271          307 :       && e->ref && e->ref->type == REF_SUBSTRING
    1272          303 :       && e->ref->u.ss.start->expr_type == EXPR_CONSTANT
    1273           76 :       && (e->ref->u.ss.end == NULL
    1274           74 :           || e->ref->u.ss.end->expr_type == EXPR_CONSTANT))
    1275              :     {
    1276           74 :       gfc_expr *res;
    1277           74 :       ptrdiff_t istart, iend;
    1278           74 :       size_t length;
    1279           74 :       bool equal_length = false;
    1280              : 
    1281              :       /* Basic checks on substring starting and ending indices.  */
    1282           74 :       if (!gfc_resolve_substring (e->ref, &equal_length))
    1283            6 :         return MATCH_ERROR;
    1284              : 
    1285           71 :       length = e->value.character.length;
    1286           71 :       istart = gfc_mpz_get_hwi (e->ref->u.ss.start->value.integer);
    1287           71 :       if (e->ref->u.ss.end == NULL)
    1288              :         iend = length;
    1289              :       else
    1290           69 :         iend = gfc_mpz_get_hwi (e->ref->u.ss.end->value.integer);
    1291              : 
    1292           71 :       if (istart <= iend)
    1293              :         {
    1294           66 :           if (istart < 1)
    1295              :             {
    1296            2 :               gfc_error ("Substring start index (%td) at %L below 1",
    1297            2 :                          istart, &e->ref->u.ss.start->where);
    1298            2 :               return MATCH_ERROR;
    1299              :             }
    1300           64 :           if (iend > (ssize_t) length)
    1301              :             {
    1302            1 :               gfc_error ("Substring end index (%td) at %L exceeds string "
    1303            1 :                          "length", iend, &e->ref->u.ss.end->where);
    1304            1 :               return MATCH_ERROR;
    1305              :             }
    1306           63 :           length = iend - istart + 1;
    1307              :         }
    1308              :       else
    1309              :         length = 0;
    1310              : 
    1311           68 :       res = gfc_get_constant_expr (BT_CHARACTER, e->ts.kind, &e->where);
    1312           68 :       res->value.character.string = gfc_get_wide_string (length + 1);
    1313           68 :       res->value.character.length = length;
    1314           68 :       if (length > 0)
    1315           63 :         memcpy (res->value.character.string,
    1316           63 :                 &e->value.character.string[istart - 1],
    1317              :                 length * sizeof (gfc_char_t));
    1318           68 :       res->value.character.string[length] = '\0';
    1319           68 :       e = res;
    1320              :     }
    1321              : 
    1322       312819 :   *result = e;
    1323              : 
    1324       312819 :   return MATCH_YES;
    1325              : 
    1326      7023107 : no_match:
    1327      7023107 :   gfc_current_locus = old_locus;
    1328      7023107 :   return MATCH_NO;
    1329              : }
    1330              : 
    1331              : 
    1332              : /* Match a .true. or .false.  Returns 1 if a .true. was found,
    1333              :    0 if a .false. was found, and -1 otherwise.  */
    1334              : static int
    1335      4582210 : match_logical_constant_string (void)
    1336              : {
    1337      4582210 :   locus orig_loc = gfc_current_locus;
    1338              : 
    1339      4582210 :   gfc_gobble_whitespace ();
    1340      4582210 :   if (gfc_next_ascii_char () == '.')
    1341              :     {
    1342        57559 :       char ch = gfc_next_ascii_char ();
    1343        57559 :       if (ch == 'f')
    1344              :         {
    1345        29376 :           if (gfc_next_ascii_char () == 'a'
    1346        29376 :               && gfc_next_ascii_char () == 'l'
    1347        29376 :               && gfc_next_ascii_char () == 's'
    1348        29376 :               && gfc_next_ascii_char () == 'e'
    1349        58752 :               && gfc_next_ascii_char () == '.')
    1350              :             /* Matched ".false.".  */
    1351              :             return 0;
    1352              :         }
    1353        28183 :       else if (ch == 't')
    1354              :         {
    1355        28182 :           if (gfc_next_ascii_char () == 'r'
    1356        28182 :               && gfc_next_ascii_char () == 'u'
    1357        28182 :               && gfc_next_ascii_char () == 'e'
    1358        56364 :               && gfc_next_ascii_char () == '.')
    1359              :             /* Matched ".true.".  */
    1360              :             return 1;
    1361              :         }
    1362              :     }
    1363      4524652 :   gfc_current_locus = orig_loc;
    1364      4524652 :   return -1;
    1365              : }
    1366              : 
    1367              : /* Match a .true. or .false.  */
    1368              : 
    1369              : static match
    1370      4582210 : match_logical_constant (gfc_expr **result)
    1371              : {
    1372      4582210 :   gfc_expr *e;
    1373      4582210 :   int i, kind, is_iso_c;
    1374              : 
    1375      4582210 :   i = match_logical_constant_string ();
    1376      4582210 :   if (i == -1)
    1377              :     return MATCH_NO;
    1378              : 
    1379        57558 :   kind = get_kind (&is_iso_c);
    1380        57558 :   if (kind == -1)
    1381              :     return MATCH_ERROR;
    1382        57558 :   if (kind == -2)
    1383        57063 :     kind = gfc_default_logical_kind;
    1384              : 
    1385        57558 :   if (gfc_validate_kind (BT_LOGICAL, kind, true) < 0)
    1386              :     {
    1387            4 :       gfc_error ("Bad kind for logical constant at %C");
    1388            4 :       return MATCH_ERROR;
    1389              :     }
    1390              : 
    1391        57554 :   e = gfc_get_logical_expr (kind, &gfc_current_locus, i);
    1392        57554 :   e->ts.is_c_interop = is_iso_c;
    1393              : 
    1394        57554 :   *result = e;
    1395        57554 :   return MATCH_YES;
    1396              : }
    1397              : 
    1398              : 
    1399              : /* Match a real or imaginary part of a complex constant that is a
    1400              :    symbolic constant.  */
    1401              : 
    1402              : static match
    1403       142555 : match_sym_complex_part (gfc_expr **result)
    1404              : {
    1405       142555 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    1406       142555 :   gfc_symbol *sym;
    1407       142555 :   gfc_expr *e;
    1408       142555 :   match m;
    1409              : 
    1410       142555 :   m = gfc_match_name (name);
    1411       142555 :   if (m != MATCH_YES)
    1412              :     return m;
    1413              : 
    1414        40207 :   if (gfc_find_symbol (name, NULL, 1, &sym) || sym == NULL)
    1415              :     return MATCH_NO;
    1416              : 
    1417        37416 :   if (sym->attr.flavor != FL_PARAMETER)
    1418              :     {
    1419              :       /* Give the matcher for implied do-loops a chance to run.  This yields
    1420              :          a much saner error message for "write(*,*) (i, i=1, 6" where the
    1421              :          right parenthesis is missing.  */
    1422        35824 :       char c;
    1423        35824 :       gfc_gobble_whitespace ();
    1424        35824 :       c = gfc_peek_ascii_char ();
    1425        35824 :       if (c == '=' || c == ',')
    1426              :         {
    1427              :           m = MATCH_NO;
    1428              :         }
    1429              :       else
    1430              :         {
    1431        32697 :           gfc_error ("Expected PARAMETER symbol in complex constant at %C");
    1432        32697 :           m = MATCH_ERROR;
    1433              :         }
    1434              :       return m;
    1435              :     }
    1436              : 
    1437         1592 :   if (!sym->value)
    1438            2 :     goto error;
    1439              : 
    1440         1590 :   if (!gfc_numeric_ts (&sym->value->ts))
    1441              :     {
    1442          334 :       gfc_error ("Numeric PARAMETER required in complex constant at %C");
    1443          334 :       return MATCH_ERROR;
    1444              :     }
    1445              : 
    1446         1256 :   if (sym->value->rank != 0)
    1447              :     {
    1448          174 :       gfc_error ("Scalar PARAMETER required in complex constant at %C");
    1449          174 :       return MATCH_ERROR;
    1450              :     }
    1451              : 
    1452         1082 :   if (!gfc_notify_std (GFC_STD_F2003, "PARAMETER symbol in "
    1453              :                        "complex constant at %C"))
    1454              :     return MATCH_ERROR;
    1455              : 
    1456         1079 :   switch (sym->value->ts.type)
    1457              :     {
    1458           68 :     case BT_REAL:
    1459           68 :       e = gfc_copy_expr (sym->value);
    1460           68 :       break;
    1461              : 
    1462            1 :     case BT_COMPLEX:
    1463            1 :       e = gfc_complex2real (sym->value, sym->value->ts.kind);
    1464            1 :       if (e == NULL)
    1465            0 :         goto error;
    1466              :       break;
    1467              : 
    1468         1008 :     case BT_INTEGER:
    1469         1008 :       e = gfc_int2real (sym->value, gfc_default_real_kind);
    1470         1008 :       if (e == NULL)
    1471            0 :         goto error;
    1472              :       break;
    1473              : 
    1474            2 :     case BT_UNSIGNED:
    1475            2 :       goto error;
    1476              : 
    1477            0 :     default:
    1478            0 :       gfc_internal_error ("gfc_match_sym_complex_part(): Bad type");
    1479              :     }
    1480              : 
    1481         1077 :   *result = e;          /* e is a scalar, real, constant expression.  */
    1482         1077 :   return MATCH_YES;
    1483              : 
    1484            4 : error:
    1485            4 :   gfc_error ("Error converting PARAMETER constant in complex constant at %C");
    1486            4 :   return MATCH_ERROR;
    1487              : }
    1488              : 
    1489              : 
    1490              : /* Match a real or imaginary part of a complex number.  */
    1491              : 
    1492              : static match
    1493       142555 : match_complex_part (gfc_expr **result)
    1494              : {
    1495       142555 :   match m;
    1496              : 
    1497       142555 :   m = match_sym_complex_part (result);
    1498       142555 :   if (m != MATCH_NO)
    1499              :     return m;
    1500              : 
    1501       108266 :   m = match_real_constant (result, 1);
    1502       108266 :   if (m != MATCH_NO)
    1503              :     return m;
    1504              : 
    1505        93653 :   return match_integer_constant (result, 1);
    1506              : }
    1507              : 
    1508              : 
    1509              : /* Try to match a complex constant.  */
    1510              : 
    1511              : static match
    1512      7346355 : match_complex_constant (gfc_expr **result)
    1513              : {
    1514      7346355 :   gfc_expr *e, *real, *imag;
    1515      7346355 :   gfc_error_buffer old_error;
    1516      7346355 :   gfc_typespec target;
    1517      7346355 :   locus old_loc;
    1518      7346355 :   int kind;
    1519      7346355 :   match m;
    1520              : 
    1521      7346355 :   old_loc = gfc_current_locus;
    1522      7346355 :   real = imag = e = NULL;
    1523              : 
    1524      7346355 :   m = gfc_match_char ('(');
    1525      7346355 :   if (m != MATCH_YES)
    1526              :     return m;
    1527              : 
    1528       132145 :   gfc_push_error (&old_error);
    1529              : 
    1530       132145 :   m = match_complex_part (&real);
    1531       132145 :   if (m == MATCH_NO)
    1532              :     {
    1533        75298 :       gfc_free_error (&old_error);
    1534        75298 :       goto cleanup;
    1535              :     }
    1536              : 
    1537        56847 :   if (gfc_match_char (',') == MATCH_NO)
    1538              :     {
    1539              :       /* It is possible that gfc_int2real issued a warning when
    1540              :          converting an integer to real.  Throw this away here.  */
    1541              : 
    1542        46433 :       gfc_clear_warning ();
    1543        46433 :       gfc_pop_error (&old_error);
    1544        46433 :       m = MATCH_NO;
    1545        46433 :       goto cleanup;
    1546              :     }
    1547              : 
    1548              :   /* If m is error, then something was wrong with the real part and we
    1549              :      assume we have a complex constant because we've seen the ','.  An
    1550              :      ambiguous case here is the start of an iterator list of some
    1551              :      sort. These sort of lists are matched prior to coming here.  */
    1552              : 
    1553        10414 :   if (m == MATCH_ERROR)
    1554              :     {
    1555            4 :       gfc_free_error (&old_error);
    1556            4 :       goto cleanup;
    1557              :     }
    1558        10410 :   gfc_pop_error (&old_error);
    1559              : 
    1560        10410 :   m = match_complex_part (&imag);
    1561        10410 :   if (m == MATCH_NO)
    1562         3135 :     goto syntax;
    1563         7275 :   if (m == MATCH_ERROR)
    1564          153 :     goto cleanup;
    1565              : 
    1566         7122 :   m = gfc_match_char (')');
    1567         7122 :   if (m == MATCH_NO)
    1568              :     {
    1569              :       /* Give the matcher for implied do-loops a chance to run.  This
    1570              :          yields a much saner error message for (/ (i, 4=i, 6) /).  */
    1571           13 :       if (gfc_peek_ascii_char () == '=')
    1572              :         {
    1573            0 :           m = MATCH_ERROR;
    1574            0 :           goto cleanup;
    1575              :         }
    1576              :       else
    1577           13 :     goto syntax;
    1578              :     }
    1579              : 
    1580         7109 :   if (m == MATCH_ERROR)
    1581            0 :     goto cleanup;
    1582              : 
    1583              :   /* Decide on the kind of this complex number.  */
    1584         7109 :   if (real->ts.type == BT_REAL)
    1585              :     {
    1586         6675 :       if (imag->ts.type == BT_REAL)
    1587         6650 :         kind = gfc_kind_max (real, imag);
    1588              :       else
    1589           25 :         kind = real->ts.kind;
    1590              :     }
    1591              :   else
    1592              :     {
    1593          434 :       if (imag->ts.type == BT_REAL)
    1594            7 :         kind = imag->ts.kind;
    1595              :       else
    1596          427 :         kind = gfc_default_real_kind;
    1597              :     }
    1598         7109 :   gfc_clear_ts (&target);
    1599         7109 :   target.type = BT_REAL;
    1600         7109 :   target.kind = kind;
    1601              : 
    1602         7109 :   if (real->ts.type != BT_REAL || kind != real->ts.kind)
    1603          435 :     gfc_convert_type (real, &target, 2);
    1604         7109 :   if (imag->ts.type != BT_REAL || kind != imag->ts.kind)
    1605          490 :     gfc_convert_type (imag, &target, 2);
    1606              : 
    1607         7109 :   e = convert_complex (real, imag, kind);
    1608         7109 :   e->where = gfc_current_locus;
    1609              : 
    1610         7109 :   gfc_free_expr (real);
    1611         7109 :   gfc_free_expr (imag);
    1612              : 
    1613         7109 :   *result = e;
    1614         7109 :   return MATCH_YES;
    1615              : 
    1616         3148 : syntax:
    1617         3148 :   gfc_error ("Syntax error in COMPLEX constant at %C");
    1618         3148 :   m = MATCH_ERROR;
    1619              : 
    1620       125036 : cleanup:
    1621       125036 :   gfc_free_expr (e);
    1622       125036 :   gfc_free_expr (real);
    1623       125036 :   gfc_free_expr (imag);
    1624       125036 :   gfc_current_locus = old_loc;
    1625              : 
    1626       125036 :   return m;
    1627      7346355 : }
    1628              : 
    1629              : 
    1630              : /* Match constants in any of several forms.  Returns nonzero for a
    1631              :    match, zero for no match.  */
    1632              : 
    1633              : match
    1634      7346355 : gfc_match_literal_constant (gfc_expr **result, int signflag)
    1635              : {
    1636      7346355 :   match m;
    1637              : 
    1638      7346355 :   m = match_complex_constant (result);
    1639      7346355 :   if (m != MATCH_NO)
    1640              :     return m;
    1641              : 
    1642      7335941 :   m = match_string_constant (result);
    1643      7335941 :   if (m != MATCH_NO)
    1644              :     return m;
    1645              : 
    1646      7023107 :   m = match_boz_constant (result);
    1647      7023107 :   if (m != MATCH_NO)
    1648              :     return m;
    1649              : 
    1650      7018938 :   m = match_real_constant (result, signflag);
    1651      7018938 :   if (m != MATCH_NO)
    1652              :     return m;
    1653              : 
    1654      6809395 :   m = match_hollerith_constant (result);
    1655      6809395 :   if (m != MATCH_NO)
    1656              :     return m;
    1657              : 
    1658      6806759 :   if (flag_unsigned)
    1659              :     {
    1660       597690 :       m = match_unsigned_constant (result);
    1661       597690 :       if (m != MATCH_NO)
    1662              :         return m;
    1663              :     }
    1664              : 
    1665      6704469 :   m = match_integer_constant (result, signflag);
    1666      6704469 :   if (m != MATCH_NO)
    1667              :     return m;
    1668              : 
    1669      4582210 :   m = match_logical_constant (result);
    1670      4582210 :   if (m != MATCH_NO)
    1671              :     return m;
    1672              : 
    1673              :   return MATCH_NO;
    1674              : }
    1675              : 
    1676              : 
    1677              : /* This checks if a symbol is the return value of an encompassing function.
    1678              :    Function nesting can be maximally two levels deep, but we may have
    1679              :    additional local namespaces like BLOCK etc.  */
    1680              : 
    1681              : bool
    1682       800187 : gfc_is_function_return_value (gfc_symbol *sym, gfc_namespace *ns)
    1683              : {
    1684       800187 :   if (!sym->attr.function || (sym->result != sym))
    1685              :     return false;
    1686      1677994 :   while (ns)
    1687              :     {
    1688       950997 :       if (ns->proc_name == sym)
    1689              :         return true;
    1690       938946 :       ns = ns->parent;
    1691              :     }
    1692              :   return false;
    1693              : }
    1694              : 
    1695              : 
    1696              : /* Match a single actual argument value.  An actual argument is
    1697              :    usually an expression, but can also be a procedure name.  If the
    1698              :    argument is a single name, it is not always possible to tell
    1699              :    whether the name is a dummy procedure or not.  We treat these cases
    1700              :    by creating an argument that looks like a dummy procedure and
    1701              :    fixing things later during resolution.  */
    1702              : 
    1703              : static match
    1704      2074304 : match_actual_arg (gfc_expr **result)
    1705              : {
    1706      2074304 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    1707      2074304 :   gfc_symtree *symtree;
    1708      2074304 :   locus where, w;
    1709      2074304 :   gfc_expr *e;
    1710      2074304 :   char c;
    1711              : 
    1712      2074304 :   gfc_gobble_whitespace ();
    1713      2074304 :   where = gfc_current_locus;
    1714              : 
    1715      2074304 :   switch (gfc_match_name (name))
    1716              :     {
    1717              :     case MATCH_ERROR:
    1718              :       return MATCH_ERROR;
    1719              : 
    1720              :     case MATCH_NO:
    1721              :       break;
    1722              : 
    1723      1361663 :     case MATCH_YES:
    1724      1361663 :       w = gfc_current_locus;
    1725      1361663 :       gfc_gobble_whitespace ();
    1726      1361663 :       c = gfc_next_ascii_char ();
    1727      1361663 :       gfc_current_locus = w;
    1728              : 
    1729      1361663 :       if (c != ',' && c != ')')
    1730              :         break;
    1731              : 
    1732       711859 :       if (gfc_find_sym_tree (name, NULL, 1, &symtree))
    1733              :         break;
    1734              :       /* Handle error elsewhere.  */
    1735              : 
    1736              :       /* Eliminate a couple of common cases where we know we don't
    1737              :          have a function argument.  */
    1738       711859 :       if (symtree == NULL)
    1739              :         {
    1740        14247 :           gfc_get_sym_tree (name, NULL, &symtree, false);
    1741        14247 :           gfc_set_sym_referenced (symtree->n.sym);
    1742              :         }
    1743              :       else
    1744              :         {
    1745       697612 :           gfc_symbol *sym;
    1746              : 
    1747       697612 :           sym = symtree->n.sym;
    1748       697612 :           gfc_set_sym_referenced (sym);
    1749       697612 :           if (sym->attr.flavor == FL_NAMELIST)
    1750              :             {
    1751         1171 :               gfc_error ("Namelist %qs cannot be an argument at %L",
    1752              :               sym->name, &where);
    1753         1171 :               break;
    1754              :             }
    1755       696441 :           if (sym->attr.flavor != FL_PROCEDURE
    1756       656537 :               && sym->attr.flavor != FL_UNKNOWN)
    1757              :             break;
    1758              : 
    1759       197266 :           if (sym->attr.in_common && !sym->attr.proc_pointer)
    1760              :             {
    1761          224 :               if (!gfc_add_flavor (&sym->attr, FL_VARIABLE,
    1762              :                                    sym->name, &sym->declared_at))
    1763              :                 return MATCH_ERROR;
    1764              :               break;
    1765              :             }
    1766              : 
    1767              :           /* If the symbol is a function with itself as the result and
    1768              :              is being defined, then we have a variable.  */
    1769       197042 :           if (sym->attr.function && sym->result == sym)
    1770              :             {
    1771         3904 :               if (gfc_is_function_return_value (sym, gfc_current_ns))
    1772              :                 break;
    1773              : 
    1774         3250 :               if (sym->attr.entry
    1775           55 :                   && (sym->ns == gfc_current_ns
    1776            2 :                       || sym->ns == gfc_current_ns->parent))
    1777              :                 {
    1778           54 :                   gfc_entry_list *el = NULL;
    1779              : 
    1780           54 :                   for (el = sym->ns->entries; el; el = el->next)
    1781           54 :                     if (sym == el->sym)
    1782              :                       break;
    1783              : 
    1784           54 :                   if (el)
    1785              :                     break;
    1786              :                 }
    1787              :             }
    1788              :         }
    1789              : 
    1790       210581 :       e = gfc_get_expr ();      /* Leave it unknown for now */
    1791       210581 :       e->symtree = symtree;
    1792       210581 :       e->expr_type = EXPR_VARIABLE;
    1793       210581 :       e->ts.type = BT_PROCEDURE;
    1794       210581 :       e->where = where;
    1795              : 
    1796       210581 :       *result = e;
    1797       210581 :       return MATCH_YES;
    1798              :     }
    1799              : 
    1800      1863723 :   gfc_current_locus = where;
    1801      1863723 :   return gfc_match_expr (result);
    1802              : }
    1803              : 
    1804              : 
    1805              : /* Match a keyword argument or type parameter spec list..  */
    1806              : 
    1807              : static match
    1808      2066432 : match_keyword_arg (gfc_actual_arglist *actual, gfc_actual_arglist *base, bool pdt)
    1809              : {
    1810      2066432 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    1811      2066432 :   gfc_actual_arglist *a;
    1812      2066432 :   locus name_locus;
    1813      2066432 :   match m;
    1814              : 
    1815      2066432 :   name_locus = gfc_current_locus;
    1816      2066432 :   m = gfc_match_name (name);
    1817              : 
    1818      2066432 :   if (m != MATCH_YES)
    1819       601174 :     goto cleanup;
    1820      1465258 :   if (gfc_match_char ('=') != MATCH_YES)
    1821              :     {
    1822      1300722 :       m = MATCH_NO;
    1823      1300722 :       goto cleanup;
    1824              :     }
    1825              : 
    1826       164536 :   if (pdt)
    1827              :     {
    1828          604 :       if (gfc_match_char ('*') == MATCH_YES)
    1829              :         {
    1830           92 :           actual->spec_type = SPEC_ASSUMED;
    1831           92 :           goto add_name;
    1832              :         }
    1833          512 :       else if (gfc_match_char (':') == MATCH_YES)
    1834              :         {
    1835           61 :           actual->spec_type = SPEC_DEFERRED;
    1836           61 :           goto add_name;
    1837              :         }
    1838              :       else
    1839          451 :         actual->spec_type = SPEC_EXPLICIT;
    1840              :     }
    1841              : 
    1842       164383 :   m = match_actual_arg (&actual->expr);
    1843       164383 :   if (m != MATCH_YES)
    1844        11894 :     goto cleanup;
    1845              : 
    1846              :   /* Make sure this name has not appeared yet.  */
    1847       152489 : add_name:
    1848       152642 :   if (name[0] != '\0')
    1849              :     {
    1850       489770 :       for (a = base; a; a = a->next)
    1851       337142 :         if (a->name != NULL && strcmp (a->name, name) == 0)
    1852              :           {
    1853           14 :             gfc_error ("Keyword %qs at %C has already appeared in the "
    1854              :                        "current argument list", name);
    1855           14 :             return MATCH_ERROR;
    1856              :           }
    1857              :     }
    1858              : 
    1859       152628 :   actual->name = gfc_get_string ("%s", name);
    1860       152628 :   return MATCH_YES;
    1861              : 
    1862      1913790 : cleanup:
    1863      1913790 :   gfc_current_locus = name_locus;
    1864      1913790 :   return m;
    1865              : }
    1866              : 
    1867              : 
    1868              : /* Match an argument list function, such as %VAL.  */
    1869              : 
    1870              : static match
    1871      2025867 : match_arg_list_function (gfc_actual_arglist *result)
    1872              : {
    1873      2025867 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    1874      2025867 :   locus old_locus;
    1875      2025867 :   match m;
    1876              : 
    1877      2025867 :   old_locus = gfc_current_locus;
    1878              : 
    1879      2025867 :   if (gfc_match_char ('%') != MATCH_YES)
    1880              :     {
    1881      2025802 :       m = MATCH_NO;
    1882      2025802 :       goto cleanup;
    1883              :     }
    1884              : 
    1885           65 :   m = gfc_match ("%n (", name);
    1886           65 :   if (m != MATCH_YES)
    1887            0 :     goto cleanup;
    1888              : 
    1889           65 :   if (name[0] != '\0')
    1890              :     {
    1891           65 :       switch (name[0])
    1892              :         {
    1893           16 :         case 'l':
    1894           16 :           if (startswith (name, "loc"))
    1895              :             {
    1896           16 :               result->name = "%LOC";
    1897           16 :               break;
    1898              :             }
    1899              :           /* FALLTHRU */
    1900           12 :         case 'r':
    1901           12 :           if (startswith (name, "ref"))
    1902              :             {
    1903           12 :               result->name = "%REF";
    1904           12 :               break;
    1905              :             }
    1906              :           /* FALLTHRU */
    1907           37 :         case 'v':
    1908           37 :           if (startswith (name, "val"))
    1909              :             {
    1910           37 :               result->name = "%VAL";
    1911           37 :               break;
    1912              :             }
    1913              :           /* FALLTHRU */
    1914            0 :         default:
    1915            0 :           m = MATCH_ERROR;
    1916            0 :           goto cleanup;
    1917              :         }
    1918              :     }
    1919              : 
    1920           65 :   if (!gfc_notify_std (GFC_STD_GNU, "argument list function at %C"))
    1921              :     {
    1922            1 :       m = MATCH_ERROR;
    1923            1 :       goto cleanup;
    1924              :     }
    1925              : 
    1926           64 :   m = match_actual_arg (&result->expr);
    1927           64 :   if (m != MATCH_YES)
    1928            0 :     goto cleanup;
    1929              : 
    1930           64 :   if (gfc_match_char (')') != MATCH_YES)
    1931              :     {
    1932            0 :       m = MATCH_NO;
    1933            0 :       goto cleanup;
    1934              :     }
    1935              : 
    1936              :   return MATCH_YES;
    1937              : 
    1938      2025803 : cleanup:
    1939      2025803 :   gfc_current_locus = old_locus;
    1940      2025803 :   return m;
    1941              : }
    1942              : 
    1943              : 
    1944              : /* Matches an actual argument list of a function or subroutine, from
    1945              :    the opening parenthesis to the closing parenthesis.  The argument
    1946              :    list is assumed to allow keyword arguments because we don't know if
    1947              :    the symbol associated with the procedure has an implicit interface
    1948              :    or not.  We make sure keywords are unique. If sub_flag is set,
    1949              :    we're matching the argument list of a subroutine.
    1950              : 
    1951              :    NOTE: An alternative use for this function is to match type parameter
    1952              :    spec lists, which are so similar to actual argument lists that the
    1953              :    machinery can be reused. This use is flagged by the optional argument
    1954              :    'pdt'.  */
    1955              : 
    1956              : match
    1957      2168070 : gfc_match_actual_arglist (int sub_flag, gfc_actual_arglist **argp, bool pdt)
    1958              : {
    1959      2168070 :   gfc_actual_arglist *head, *tail;
    1960      2168070 :   int seen_keyword;
    1961      2168070 :   gfc_st_label *label;
    1962      2168070 :   locus old_loc;
    1963      2168070 :   match m;
    1964              : 
    1965      2168070 :   *argp = tail = NULL;
    1966      2168070 :   old_loc = gfc_current_locus;
    1967              : 
    1968      2168070 :   seen_keyword = 0;
    1969              : 
    1970      2168070 :   if (gfc_match_char ('(') == MATCH_NO)
    1971       665024 :     return (sub_flag) ? MATCH_YES : MATCH_NO;
    1972              : 
    1973      1503046 :   if (gfc_match_char (')') == MATCH_YES)
    1974              :     return MATCH_YES;
    1975              : 
    1976      1472556 :   head = NULL;
    1977              : 
    1978      1472556 :   matching_actual_arglist++;
    1979              : 
    1980      2065590 :   for (;;)
    1981              :     {
    1982      2065590 :       if (head == NULL)
    1983      1472556 :         head = tail = gfc_get_actual_arglist ();
    1984              :       else
    1985              :         {
    1986       593034 :           tail->next = gfc_get_actual_arglist ();
    1987       593034 :           tail = tail->next;
    1988              :         }
    1989              : 
    1990      2065590 :       if (sub_flag && !pdt && gfc_match_char ('*') == MATCH_YES)
    1991              :         {
    1992          238 :           m = gfc_match_st_label (&label);
    1993          238 :           if (m == MATCH_NO)
    1994            0 :             gfc_error ("Expected alternate return label at %C");
    1995          238 :           if (m != MATCH_YES)
    1996            0 :             goto cleanup;
    1997              : 
    1998          238 :           if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument "
    1999              :                                "at %C"))
    2000            0 :             goto cleanup;
    2001              : 
    2002          238 :           tail->label = label;
    2003          238 :           goto next;
    2004              :         }
    2005              : 
    2006      2065352 :       if (pdt && !seen_keyword)
    2007              :         {
    2008         2807 :           if (gfc_match_char (':') == MATCH_YES)
    2009              :             {
    2010          156 :               tail->spec_type = SPEC_DEFERRED;
    2011          156 :               goto next;
    2012              :             }
    2013         2651 :           else if (gfc_match_char ('*') == MATCH_YES)
    2014              :             {
    2015          459 :               tail->spec_type = SPEC_ASSUMED;
    2016          459 :               goto next;
    2017              :             }
    2018              :           else
    2019         2192 :             tail->spec_type = SPEC_EXPLICIT;
    2020              : 
    2021         2192 :           m = match_keyword_arg (tail, head, pdt);
    2022         2192 :           if (m == MATCH_YES)
    2023              :             {
    2024          432 :               seen_keyword = 1;
    2025          432 :               goto next;
    2026              :             }
    2027         1760 :           if (m == MATCH_ERROR)
    2028            0 :             goto cleanup;
    2029              :         }
    2030              : 
    2031              :       /* After the first keyword argument is seen, the following
    2032              :          arguments must also have keywords.  */
    2033      2064305 :       if (seen_keyword)
    2034              :         {
    2035        38438 :           m = match_keyword_arg (tail, head, pdt);
    2036              : 
    2037        38438 :           if (m == MATCH_ERROR)
    2038           34 :             goto cleanup;
    2039        38404 :           if (m == MATCH_NO)
    2040              :             {
    2041         1401 :               gfc_error ("Missing keyword name in actual argument list at %C");
    2042         1401 :               goto cleanup;
    2043              :             }
    2044              : 
    2045              :         }
    2046              :       else
    2047              :         {
    2048              :           /* Try an argument list function, like %VAL.  */
    2049      2025867 :           m = match_arg_list_function (tail);
    2050      2025867 :           if (m == MATCH_ERROR)
    2051            1 :             goto cleanup;
    2052              : 
    2053              :           /* See if we have the first keyword argument.  */
    2054      2025866 :           if (m == MATCH_NO)
    2055              :             {
    2056      2025802 :               m = match_keyword_arg (tail, head, false);
    2057      2025802 :               if (m == MATCH_YES)
    2058              :                 seen_keyword = 1;
    2059      1910609 :               if (m == MATCH_ERROR)
    2060          752 :                 goto cleanup;
    2061              :             }
    2062              : 
    2063      2025050 :           if (m == MATCH_NO)
    2064              :             {
    2065              :               /* Try for a non-keyword argument.  */
    2066      1909857 :               m = match_actual_arg (&tail->expr);
    2067      1909857 :               if (m == MATCH_ERROR)
    2068         2350 :                 goto cleanup;
    2069      1907507 :               if (m == MATCH_NO)
    2070        20669 :                 goto syntax;
    2071              :             }
    2072              :         }
    2073              : 
    2074              :     /* PDT kind expressions are acceptable as initialization expressions.
    2075              :        However, intrinsics with a KIND argument reject them. Convert the
    2076              :        expression now by use of the component initializer.  */
    2077      2039098 :     if (tail->expr
    2078      2039014 :         && tail->expr->expr_type == EXPR_VARIABLE
    2079      4078112 :         && gfc_expr_attr (tail->expr).pdt_kind)
    2080              :       {
    2081          665 :         gfc_ref *ref;
    2082          665 :         gfc_expr *tmp = NULL;
    2083          687 :         for (ref = tail->expr->ref; ref; ref = ref->next)
    2084           22 :              if (!ref->next && ref->type == REF_COMPONENT
    2085           22 :                  && ref->u.c.component->attr.pdt_kind
    2086           22 :                  && ref->u.c.component->initializer)
    2087           22 :           tmp = gfc_copy_expr (ref->u.c.component->initializer);
    2088          665 :         if (tmp)
    2089           22 :           gfc_replace_expr (tail->expr, tmp);
    2090              :       }
    2091              : 
    2092      2040383 :     next:
    2093      2040383 :       if (gfc_match_char (')') == MATCH_YES)
    2094              :         break;
    2095       602137 :       if (gfc_match_char (',') != MATCH_YES)
    2096         9103 :         goto syntax;
    2097              :     }
    2098              : 
    2099      1438246 :   *argp = head;
    2100      1438246 :   matching_actual_arglist--;
    2101      1438246 :   return MATCH_YES;
    2102              : 
    2103        29772 : syntax:
    2104        29772 :   gfc_error ("Syntax error in argument list at %C");
    2105              : 
    2106        34310 : cleanup:
    2107        34310 :   gfc_free_actual_arglist (head);
    2108        34310 :   gfc_current_locus = old_loc;
    2109        34310 :   matching_actual_arglist--;
    2110        34310 :   return MATCH_ERROR;
    2111              : }
    2112              : 
    2113              : 
    2114              : /* Used by gfc_match_varspec() to extend the reference list by one
    2115              :    element.  */
    2116              : 
    2117              : static gfc_ref *
    2118       765757 : extend_ref (gfc_expr *primary, gfc_ref *tail)
    2119              : {
    2120       765757 :   if (primary->ref == NULL)
    2121       689621 :     primary->ref = tail = gfc_get_ref ();
    2122        76136 :   else if (tail == NULL)
    2123              :     {
    2124              :       /* Set tail to end of reference chain.  */
    2125           47 :       for (gfc_ref *ref = primary->ref; ref; ref = ref->next)
    2126           47 :         if (ref->next == NULL)
    2127              :           {
    2128              :             tail = ref;
    2129              :             break;
    2130              :           }
    2131              :     }
    2132              :   else
    2133              :     {
    2134        76098 :       tail->next = gfc_get_ref ();
    2135        76098 :       tail = tail->next;
    2136              :     }
    2137              : 
    2138       765757 :   return tail;
    2139              : }
    2140              : 
    2141              : 
    2142              : /* Used by gfc_match_varspec() to match an inquiry reference.  */
    2143              : 
    2144              : bool
    2145         5467 : is_inquiry_ref (const char *name, gfc_ref **ref)
    2146              : {
    2147         5467 :   inquiry_type type;
    2148              : 
    2149         5467 :   if (name == NULL)
    2150              :     return false;
    2151              : 
    2152         5467 :   if (ref) *ref = NULL;
    2153              : 
    2154         5467 :   if (strcmp (name, "re") == 0)
    2155              :     type = INQUIRY_RE;
    2156         4008 :   else if (strcmp (name, "im") == 0)
    2157              :     type = INQUIRY_IM;
    2158         2982 :   else if (strcmp (name, "kind") == 0)
    2159              :     type = INQUIRY_KIND;
    2160         2271 :   else if (strcmp (name, "len") == 0)
    2161              :     type = INQUIRY_LEN;
    2162              :   else
    2163              :     return false;
    2164              : 
    2165         4084 :   if (ref)
    2166              :     {
    2167         2273 :       *ref = gfc_get_ref ();
    2168         2273 :       (*ref)->type = REF_INQUIRY;
    2169         2273 :       (*ref)->u.i = type;
    2170              :     }
    2171              : 
    2172              :   return true;
    2173              : }
    2174              : 
    2175              : 
    2176              : /* Skip any parenthesized or bracketed groups at the current locus.  */
    2177              : 
    2178              : static void
    2179         1500 : skip_ref_groups (void)
    2180              : {
    2181         1817 :   for (;;)
    2182              :     {
    2183         1817 :       gfc_gobble_whitespace ();
    2184         1817 :       gfc_char_t c = gfc_peek_char ();
    2185         1817 :       if (c != '(' && c != '[')
    2186              :         return;
    2187              : 
    2188              :       int depth = 0;
    2189              :       gfc_char_t quote = ' ';
    2190         1949 :       do
    2191              :         {
    2192         2045 :           c = gfc_next_char_literal (quote == ' ' ? NONSTRING
    2193              :                                                   : INSTRING_NOWARN);
    2194         1949 :           if (c == '\n')
    2195              :             return;
    2196         1949 :           if (quote != ' ')
    2197              :             {
    2198           96 :               if (c == quote)
    2199           24 :                 quote = ' ';
    2200              :             }
    2201         1853 :           else if (c == '\'' || c == '"')
    2202              :             quote = c;
    2203              :           else if (c == '(' || c == '[')
    2204          377 :             depth++;
    2205              :           else if (c == ')' || c == ']')
    2206          377 :             depth--;
    2207              :         }
    2208         1949 :       while (depth > 0);
    2209              :     }
    2210              : }
    2211              : 
    2212              : 
    2213              : /* Return true if the component references at the current locus can be
    2214              :    followed starting from derived type DT.  The locus is not changed.  */
    2215              : 
    2216              : static bool
    2217         1099 : inferred_type_chain_ok (gfc_symbol *dt)
    2218              : {
    2219         1099 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    2220         1099 :   locus old_loc = gfc_current_locus;
    2221         1099 :   bool ok = true;
    2222              : 
    2223         1653 :   while (gfc_match_name (name) == MATCH_YES)
    2224              :     {
    2225         1653 :       gfc_component *c = gfc_find_component (dt, name, true, true, NULL);
    2226         1653 :       if (!c)
    2227              :         {
    2228           68 :           ok = dt->f2k_derived
    2229           68 :                && gfc_find_typebound_proc (dt, NULL, name, true, NULL);
    2230              :           break;
    2231              :         }
    2232              : 
    2233              :       /* A subscript needs an array, a substring or a procedure pointer.  */
    2234         1585 :       gfc_gobble_whitespace ();
    2235         1585 :       if (gfc_peek_ascii_char () == '('
    2236          402 :           && !c->as && c->ts.type != BT_CHARACTER && !c->attr.proc_pointer
    2237         1824 :           && !(c->ts.type == BT_CLASS && c->attr.class_ok
    2238          154 :                && CLASS_DATA (c)->as))
    2239              :         {
    2240              :           ok = false;
    2241              :           break;
    2242              :         }
    2243         1500 :       skip_ref_groups ();
    2244              : 
    2245         1500 :       if (gfc_match_char ('%') != MATCH_YES)
    2246              :         break;
    2247              : 
    2248          626 :       if (c->ts.type == BT_DERIVED)
    2249          288 :         dt = c->ts.u.derived;
    2250          338 :       else if (c->ts.type == BT_CLASS)
    2251              :         {
    2252          266 :           if (!c->attr.class_ok || !CLASS_DATA (c))
    2253              :             break;
    2254          266 :           dt = CLASS_DATA (c)->ts.u.derived;
    2255              :         }
    2256              :       else
    2257              :         {
    2258           72 :           ok = gfc_match_name (name) == MATCH_YES
    2259           72 :                && is_inquiry_ref (name, NULL);
    2260              :           break;
    2261              :         }
    2262              :     }
    2263              : 
    2264         1099 :   gfc_current_locus = old_loc;
    2265         1099 :   return ok;
    2266              : }
    2267              : 
    2268              : 
    2269              : /* Check to see if functions in operator expressions can be resolved now.  */
    2270              : 
    2271              : static bool
    2272          126 : resolvable_fcns (gfc_expr *e,
    2273              :                   gfc_symbol *sym ATTRIBUTE_UNUSED,
    2274              :                   int *f ATTRIBUTE_UNUSED)
    2275              : {
    2276          126 :   bool p;
    2277          126 :   gfc_symbol *s;
    2278              : 
    2279          126 :   if (e->expr_type != EXPR_FUNCTION)
    2280              :     return false;
    2281              : 
    2282           54 :   s = e && e->symtree && e->symtree->n.sym ? e->symtree->n.sym : NULL;
    2283           54 :   p = s && (s->attr.use_assoc
    2284           54 :             || s->attr.host_assoc
    2285           54 :             || s->attr.if_source == IFSRC_DECL
    2286           54 :             || s->attr.proc == PROC_INTRINSIC
    2287           24 :             || gfc_is_intrinsic (s, 0, e->where));
    2288           54 :   return !p;
    2289              : }
    2290              : 
    2291              : 
    2292              : /* Match any additional specifications associated with the current
    2293              :    variable like member references or substrings.  If equiv_flag is
    2294              :    set we only match stuff that is allowed inside an EQUIVALENCE
    2295              :    statement.  sub_flag tells whether we expect a type-bound procedure found
    2296              :    to be a subroutine as part of CALL or a FUNCTION. For procedure pointer
    2297              :    components, 'ppc_arg' determines whether the PPC may be called (with an
    2298              :    argument list), or whether it may just be referred to as a pointer.  */
    2299              : 
    2300              : match
    2301      5276864 : gfc_match_varspec (gfc_expr *primary, int equiv_flag, bool sub_flag,
    2302              :                    bool ppc_arg)
    2303              : {
    2304      5276864 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    2305      5276864 :   gfc_ref *substring, *tail, *tmp;
    2306      5276864 :   gfc_component *component = NULL;
    2307      5276864 :   gfc_component *previous = NULL;
    2308      5276864 :   gfc_symbol *sym = primary->symtree->n.sym;
    2309      5276864 :   gfc_expr *tgt_expr = NULL;
    2310      5276864 :   match m;
    2311      5276864 :   bool unknown;
    2312      5276864 :   bool inquiry;
    2313      5276864 :   bool intrinsic;
    2314      5276864 :   bool inferred_type;
    2315      5276864 :   locus old_loc;
    2316      5276864 :   char peeked_char;
    2317              : 
    2318      5276864 :   tail = NULL;
    2319              : 
    2320      5276864 :   gfc_gobble_whitespace ();
    2321              : 
    2322      5276864 :   if (gfc_peek_ascii_char () == '[')
    2323              :     {
    2324         3368 :       if ((sym->ts.type != BT_CLASS && sym->attr.dimension)
    2325         3368 :           || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
    2326          141 :               && CLASS_DATA (sym)->attr.dimension))
    2327              :         {
    2328            0 :           gfc_error ("Array section designator, e.g. %<(:)%>, is required "
    2329              :                      "besides the coarray designator %<[...]%> at %C");
    2330            0 :           return MATCH_ERROR;
    2331              :         }
    2332         3368 :       if ((sym->ts.type != BT_CLASS && !sym->attr.codimension)
    2333         3367 :           || (sym->ts.type == BT_CLASS && CLASS_DATA (sym)
    2334          141 :               && !CLASS_DATA (sym)->attr.codimension))
    2335              :         {
    2336            1 :           gfc_error ("Coarray designator at %C but %qs is not a coarray",
    2337              :                      sym->name);
    2338            1 :           return MATCH_ERROR;
    2339              :         }
    2340              :     }
    2341              : 
    2342      5276863 :   if (sym->assoc && sym->assoc->target)
    2343      5276863 :     tgt_expr = sym->assoc->target;
    2344              : 
    2345      5276863 :   inferred_type = IS_INFERRED_TYPE (primary);
    2346              : 
    2347              :   /* SELECT TYPE temporaries within an ASSOCIATE block, whose selector has not
    2348              :      been parsed, can generate errors with array refs.. The SELECT TYPE
    2349              :      namespace is marked with 'assoc_name_inferred'. During resolution, this is
    2350              :      detected and gfc_fixup_inferred_type_refs is called.  */
    2351      5275562 :   if (!inferred_type
    2352      5275562 :       && sym->attr.select_type_temporary
    2353        25271 :       && sym->ns->assoc_name_inferred
    2354          368 :       && !sym->attr.select_rank_temporary)
    2355         1669 :     inferred_type = true;
    2356              : 
    2357              :   /* Try to resolve a typebound generic procedure so that the associate name
    2358              :      has a chance to get a type before being used in a second, nested associate
    2359              :      statement. Note that a copy is used for resolution so that failure does
    2360              :      not result in a mutilated selector expression further down the line.  */
    2361         8521 :   if (tgt_expr && !sym->assoc->dangling
    2362         8521 :       && tgt_expr->ts.type == BT_UNKNOWN
    2363         2300 :       && tgt_expr->symtree
    2364         1861 :       && tgt_expr->symtree->n.sym
    2365      5276936 :       && gfc_expr_attr (tgt_expr).generic
    2366      5276936 :       && ((sym->ts.type == BT_DERIVED && sym->ts.u.derived->attr.pdt_template)
    2367           72 :           || (sym->ts.type == BT_CLASS
    2368            0 :               && CLASS_DATA (sym)->ts.u.derived->attr.pdt_template)))
    2369              :     {
    2370            1 :         gfc_expr *cpy = gfc_copy_expr (tgt_expr);
    2371            1 :         if (gfc_resolve_expr (cpy)
    2372            1 :             && cpy->ts.type != BT_UNKNOWN)
    2373              :           {
    2374            1 :             gfc_replace_expr (tgt_expr, cpy);
    2375            1 :             sym->ts = tgt_expr->ts;
    2376              :           }
    2377              :         else
    2378            0 :           gfc_free_expr (cpy);
    2379            1 :         if (gfc_expr_attr (tgt_expr).generic)
    2380      5276863 :           inferred_type = true;
    2381              :     }
    2382              : 
    2383              :   /* For associate names, we may not yet know whether they are arrays or not.
    2384              :      If the selector expression is unambiguously an array; eg. a full array
    2385              :      or an array section, then the associate name must be an array and we can
    2386              :      fix it now. Otherwise, if parentheses follow and it is not a character
    2387              :      type, we have to assume that it actually is one for now.  The final
    2388              :      decision will be made at resolution, of course.  */
    2389      5276863 :   if (sym->assoc
    2390        33792 :       && gfc_peek_ascii_char () == '('
    2391         9981 :       && sym->ts.type != BT_CLASS
    2392      5286634 :       && !sym->attr.dimension)
    2393              :     {
    2394          468 :       gfc_ref *ref = NULL;
    2395              : 
    2396          468 :       if (!sym->assoc->dangling && tgt_expr)
    2397              :         {
    2398          408 :            if (tgt_expr->expr_type == EXPR_VARIABLE)
    2399           45 :              gfc_resolve_expr (tgt_expr);
    2400              : 
    2401          408 :            ref = tgt_expr->ref;
    2402          422 :            for (; ref; ref = ref->next)
    2403           14 :               if (ref->type == REF_ARRAY
    2404            7 :                   && (ref->u.ar.type == AR_FULL
    2405            7 :                       || ref->u.ar.type == AR_SECTION))
    2406              :                 break;
    2407              :         }
    2408              : 
    2409          468 :       if (ref || (!(sym->assoc->dangling || sym->ts.type == BT_CHARACTER)
    2410          294 :                   && sym->assoc->st
    2411          294 :                   && sym->assoc->st->n.sym
    2412          294 :                   && sym->assoc->st->n.sym->attr.dimension == 0))
    2413              :         {
    2414          294 :           sym->attr.dimension = 1;
    2415          294 :           if (sym->as == NULL
    2416          294 :               && sym->assoc->st
    2417          294 :               && sym->assoc->st->n.sym
    2418          294 :               && sym->assoc->st->n.sym->as)
    2419            0 :             sym->as = gfc_copy_array_spec (sym->assoc->st->n.sym->as);
    2420              :         }
    2421              :     }
    2422      5276395 :   else if (sym->ts.type == BT_CLASS
    2423        48631 :            && !(sym->assoc && sym->assoc->ar)
    2424        48559 :            && tgt_expr
    2425          294 :            && tgt_expr->expr_type == EXPR_VARIABLE
    2426          144 :            && sym->ts.u.derived != tgt_expr->ts.u.derived)
    2427              :     {
    2428           19 :       gfc_resolve_expr (tgt_expr);
    2429           19 :       if (tgt_expr->rank)
    2430            0 :         sym->ts.u.derived = tgt_expr->ts.u.derived;
    2431              :     }
    2432              : 
    2433      5276863 :   peeked_char = gfc_peek_ascii_char ();
    2434         1669 :   if ((inferred_type && !sym->as && peeked_char == '(')
    2435      5276558 :       || (equiv_flag && peeked_char == '(') || peeked_char == '['
    2436      5271620 :       || sym->attr.codimension
    2437      5253251 :       || (sym->attr.dimension && sym->ts.type != BT_CLASS
    2438       646223 :           && !sym->attr.proc_pointer && !gfc_is_proc_ptr_comp (primary)
    2439       646208 :           && !(gfc_matching_procptr_assignment
    2440           38 :                && sym->attr.flavor == FL_PROCEDURE))
    2441      9883932 :       || (sym->ts.type == BT_CLASS && sym->attr.class_ok
    2442        48441 :           && sym->ts.u.derived && CLASS_DATA (sym)
    2443        48437 :           && (CLASS_DATA (sym)->attr.dimension
    2444        29174 :               || CLASS_DATA (sym)->attr.codimension)))
    2445              :     {
    2446       689624 :       gfc_array_spec *as;
    2447        21595 :       bool coarray_only = sym->attr.codimension && !sym->attr.dimension
    2448       700741 :                           && sym->ts.type == BT_CHARACTER;
    2449       689624 :       gfc_ref *ref, *strarr = NULL;
    2450              : 
    2451       689624 :       tail = extend_ref (primary, tail);
    2452       689624 :       if (sym->ts.type == BT_CHARACTER && tail->type == REF_SUBSTRING)
    2453              :         {
    2454            3 :           gcc_assert (sym->attr.dimension);
    2455              :           /* Find array reference for substrings of character arrays.  */
    2456            3 :           for (ref = primary->ref; ref && ref->next; ref = ref->next)
    2457            3 :             if (ref->type == REF_ARRAY && ref->next->type == REF_SUBSTRING)
    2458              :               {
    2459              :                 strarr = ref;
    2460              :                 break;
    2461              :               }
    2462              :         }
    2463              :       else
    2464       689621 :         tail->type = REF_ARRAY;
    2465              : 
    2466              :       /* In EQUIVALENCE, we don't know yet whether we are seeing
    2467              :          an array, character variable or array of character
    2468              :          variables.  We'll leave the decision till resolve time.  */
    2469              : 
    2470       689624 :       if (equiv_flag)
    2471              :         as = NULL;
    2472       687623 :       else if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
    2473        19972 :         as = CLASS_DATA (sym)->as;
    2474              :       else
    2475       667651 :         as = sym->as;
    2476              : 
    2477       689624 :       ref = strarr ? strarr : tail;
    2478       689624 :       m = gfc_match_array_ref (&ref->u.ar, as, equiv_flag, as ? as->corank : 0,
    2479              :                                coarray_only);
    2480       689624 :       if (m != MATCH_YES)
    2481              :         return m;
    2482              : 
    2483       689532 :       gfc_gobble_whitespace ();
    2484       689532 :       if (coarray_only)
    2485              :         {
    2486         2012 :           primary->ts = sym->ts;
    2487         2012 :           goto check_substring;
    2488              :         }
    2489              : 
    2490       687520 :       if (equiv_flag && gfc_peek_ascii_char () == '(')
    2491              :         {
    2492           74 :           tail = extend_ref (primary, tail);
    2493           74 :           tail->type = REF_ARRAY;
    2494              : 
    2495           74 :           m = gfc_match_array_ref (&tail->u.ar, NULL, equiv_flag, 0);
    2496           74 :           if (m != MATCH_YES)
    2497              :             return m;
    2498              :         }
    2499              :     }
    2500              : 
    2501      5274759 :   primary->ts = sym->ts;
    2502              : 
    2503      5274759 :   if (equiv_flag)
    2504              :     return MATCH_YES;
    2505              : 
    2506              :   /* With DEC extensions, member separator may be '.' or '%'.  */
    2507      5271813 :   peeked_char = gfc_peek_ascii_char ();
    2508      5271813 :   m = gfc_match_member_sep (sym);
    2509      5271813 :   if (m == MATCH_ERROR)
    2510              :     return MATCH_ERROR;
    2511              : 
    2512      5271812 :   inquiry = false;
    2513      5271812 :   if (m == MATCH_YES && peeked_char == '%' && primary->ts.type != BT_CLASS
    2514       147777 :       && (primary->ts.type != BT_DERIVED || inferred_type))
    2515              :     {
    2516         3117 :       match mm;
    2517         3117 :       old_loc = gfc_current_locus;
    2518         3117 :       mm = gfc_match_name (name);
    2519              : 
    2520              :       /* Check to see if this has a default complex.  */
    2521          645 :       if (sym->ts.type == BT_UNKNOWN && tgt_expr == NULL
    2522         3138 :           && gfc_get_default_type (sym->name, sym->ns)->type != BT_UNKNOWN)
    2523              :         {
    2524            7 :           gfc_set_default_type (sym, 0, sym->ns);
    2525            7 :           primary->ts = sym->ts;
    2526              :         }
    2527              : 
    2528              :       /* This is a usable inquiry reference, if the symbol is already known
    2529              :          to have a type or no derived types with a component of this name
    2530              :          can be found.  If this was an inquiry reference with the same name
    2531              :          as a derived component and the associate-name type is not derived
    2532              :          or class, this is fixed up in 'gfc_fixup_inferred_type_refs'.  */
    2533         3117 :       if (mm == MATCH_YES && is_inquiry_ref (name, NULL)
    2534         5128 :           && !(sym->ts.type == BT_UNKNOWN
    2535          236 :                 && gfc_find_derived_types (sym, gfc_current_ns, name)))
    2536              :         inquiry = true;
    2537         3117 :       gfc_current_locus = old_loc;
    2538              :     }
    2539              : 
    2540              :   /* Use the default type if there is one.  */
    2541      2806212 :   if (sym->ts.type == BT_UNKNOWN && m == MATCH_YES
    2542      5272450 :       && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
    2543            0 :     gfc_set_default_type (sym, 0, sym->ns);
    2544              : 
    2545              :   /* See if the type can be determined by resolution of the selector expression,
    2546              :      if allowable now, or inferred from references.  */
    2547      5271812 :   if ((sym->ts.type == BT_UNKNOWN || inferred_type)
    2548      2807548 :       && m == MATCH_YES)
    2549              :     {
    2550         1763 :       bool sym_present, resolved = false;
    2551         1763 :       gfc_symbol *tgt_sym;
    2552         1763 :       int ndt;
    2553              : 
    2554         1763 :       sym_present = tgt_expr && tgt_expr->symtree && tgt_expr->symtree->n.sym;
    2555         1357 :       tgt_sym = sym_present ? tgt_expr->symtree->n.sym : NULL;
    2556              : 
    2557              :       /* These target expressions can be resolved at any time:
    2558              :          (i) With a declared symbol or intrinsic function; or
    2559              :          (ii) An operator expression,
    2560              :          just as long as (iii) all the functions in the expression have been
    2561              :          declared or are intrinsic.  */
    2562         1357 :       if (((sym_present                                               // (i)
    2563         1357 :             && (tgt_sym->attr.use_assoc
    2564         1357 :                 || tgt_sym->attr.host_assoc
    2565         1357 :                 || tgt_sym->attr.if_source == IFSRC_DECL
    2566         1357 :                 || tgt_sym->attr.proc == PROC_INTRINSIC
    2567         1357 :                 || gfc_is_intrinsic (tgt_sym, 0, tgt_expr->where)))
    2568         1751 :            || (tgt_expr && tgt_expr->expr_type == EXPR_OP))        // (ii)
    2569           24 :           && !gfc_traverse_expr (tgt_expr, NULL, resolvable_fcns, 0)  // (iii)
    2570           18 :           && gfc_resolve_expr (tgt_expr))
    2571              :         {
    2572           18 :           sym->ts = tgt_expr->ts;
    2573           18 :           primary->ts = sym->ts;
    2574           18 :           resolved = true;
    2575              :         }
    2576              : 
    2577              :       /* If this hasn't done the trick and the target expression is a function,
    2578              :          or an unresolved operator expression, then this must be a derived type
    2579              :          if 'name' matches an accessible type both in this namespace and in the
    2580              :          as yet unparsed contained function. In principle, the type could have
    2581              :          already been inferred to be complex and yet a derived type with a
    2582              :          component name 're' or 'im' could be found.  */
    2583           18 :       if (tgt_expr
    2584         1381 :           && (tgt_expr->expr_type == EXPR_FUNCTION
    2585           85 :               || tgt_expr->expr_type == EXPR_ARRAY
    2586           73 :               || (!resolved && tgt_expr->expr_type == EXPR_OP))
    2587         1314 :           && (sym->ts.type == BT_UNKNOWN
    2588          709 :               || (inferred_type && sym->ts.type != BT_COMPLEX))
    2589         2939 :           && (ndt = gfc_find_derived_types (sym, gfc_current_ns, name, true)))
    2590              :         {
    2591          978 :           sym->assoc->inferred_type = 1;
    2592              :           /* Pick a candidate type for now. The final determination is made in
    2593              :              'gfc_fixup_inferred_type_refs'.  Prefer the first candidate that
    2594              :              has the whole component chain.  */
    2595         1956 :           gfc_symbol **dts = &sym->assoc->derived_types;
    2596          978 :           gfc_symbol *dt = *dts;
    2597         1099 :           for (gfc_symbol *cand = *dts; cand && ndt-- > 0; cand = cand->dt_next)
    2598         1099 :             if (inferred_type_chain_ok (cand))
    2599              :               {
    2600              :                 dt = cand;
    2601              :                 break;
    2602              :               }
    2603          978 :           tgt_expr->ts.type = BT_DERIVED;
    2604          978 :           tgt_expr->ts.kind = 0;
    2605          978 :           tgt_expr->ts.u.derived = dt;
    2606          978 :           sym->ts = tgt_expr->ts;
    2607          978 :           primary->ts = sym->ts;
    2608              :           /* Delete the dt list even if this process has to be done again for
    2609              :              another primary expression.  */
    2610         2050 :           while (*dts && (*dts)->dt_next)
    2611              :             {
    2612         1072 :               gfc_symbol **tmp = &(*dts)->dt_next;
    2613         1072 :               *dts = NULL;
    2614         1072 :               dts = tmp;
    2615              :             }
    2616              :         }
    2617              :       /* If there is a usable inquiry reference not there are no matching
    2618              :          derived types, force the inquiry reference by setting unknown the
    2619              :          type of the primary expression.  */
    2620          356 :       else if (inquiry && (sym->ts.type == BT_DERIVED && inferred_type)
    2621          833 :                && !gfc_find_derived_types (sym, gfc_current_ns, name))
    2622           48 :         primary->ts.type = BT_UNKNOWN;
    2623              : 
    2624              :       /* Otherwise try resolving a copy of a component call. If it succeeds,
    2625              :          use that for the selector expression.  */
    2626          737 :       else if (tgt_expr && tgt_expr->expr_type == EXPR_COMPCALL)
    2627              :           {
    2628            1 :              gfc_expr *cpy = gfc_copy_expr (tgt_expr);
    2629            1 :              if (gfc_resolve_expr (cpy))
    2630              :                 {
    2631            1 :                   gfc_replace_expr (tgt_expr, cpy);
    2632            1 :                   sym->ts = tgt_expr->ts;
    2633              :                 }
    2634              :               else
    2635            0 :                 gfc_free_expr (cpy);
    2636              :           }
    2637              : 
    2638              :       /* An inquiry reference might determine the type, otherwise we have an
    2639              :          error.  */
    2640         1763 :       if (sym->ts.type == BT_UNKNOWN && !inquiry)
    2641              :         {
    2642           12 :           gfc_error ("Symbol %qs at %C has no IMPLICIT type", sym->name);
    2643           12 :           return MATCH_ERROR;
    2644              :         }
    2645              :     }
    2646      5270049 :   else if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
    2647      5017400 :            && m == MATCH_YES && !inquiry)
    2648              :     {
    2649            7 :       gfc_error ("Unexpected %<%c%> for nonderived-type variable %qs at %C",
    2650              :                  peeked_char, sym->name);
    2651            7 :       return MATCH_ERROR;
    2652              :     }
    2653              : 
    2654      5271793 :   if ((sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS && !inquiry)
    2655       255747 :       || m != MATCH_YES)
    2656      5102630 :     goto check_substring;
    2657              : 
    2658       169163 :   if (!inquiry)
    2659       167460 :     sym = sym->ts.u.derived;
    2660              :   else
    2661       169163 :     sym = NULL;
    2662              : 
    2663       196046 :   for (;;)
    2664              :     {
    2665       196046 :       bool t;
    2666       196046 :       gfc_symtree *tbp;
    2667       196046 :       gfc_typespec *ts = &primary->ts;
    2668              : 
    2669       196046 :       m = gfc_match_name (name);
    2670       196046 :       if (m == MATCH_NO)
    2671            0 :         gfc_error ("Expected structure component name at %C");
    2672       196046 :       if (m != MATCH_YES)
    2673          138 :         return MATCH_ERROR;
    2674              : 
    2675              :       /* For derived type components find typespec of ultimate component.  */
    2676       196046 :       if (ts->type == BT_DERIVED && primary->ref)
    2677              :         {
    2678       160021 :           for (gfc_ref *ref = primary->ref; ref; ref = ref->next)
    2679              :             {
    2680        91621 :               if (ref->type == REF_COMPONENT && ref->u.c.component)
    2681        26606 :                 ts = &ref->u.c.component->ts;
    2682              :             }
    2683              :         }
    2684              : 
    2685       196046 :       intrinsic = false;
    2686       196046 :       if (ts->type != BT_CLASS && ts->type != BT_DERIVED)
    2687              :         {
    2688         2266 :           inquiry = is_inquiry_ref (name, &tmp);
    2689         2266 :           if (inquiry)
    2690         2261 :             sym = NULL;
    2691              : 
    2692         2266 :           if (peeked_char == '%')
    2693              :             {
    2694         2266 :               if (tmp)
    2695              :                 {
    2696         2261 :                   gfc_symbol *s;
    2697         2261 :                   switch (tmp->u.i)
    2698              :                     {
    2699         1369 :                     case INQUIRY_RE:
    2700         1369 :                     case INQUIRY_IM:
    2701         1369 :                       if (!gfc_notify_std (GFC_STD_F2008,
    2702              :                                            "RE or IM part_ref at %C"))
    2703              :                         return MATCH_ERROR;
    2704              :                       break;
    2705              : 
    2706          414 :                     case INQUIRY_KIND:
    2707          414 :                       if (!gfc_notify_std (GFC_STD_F2003,
    2708              :                                            "KIND part_ref at %C"))
    2709              :                         return MATCH_ERROR;
    2710              :                       break;
    2711              : 
    2712          478 :                     case INQUIRY_LEN:
    2713          478 :                       if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
    2714              :                         return MATCH_ERROR;
    2715              :                       break;
    2716              :                     }
    2717              : 
    2718              :                   /* If necessary, infer the type of the primary expression
    2719              :                      and the associate-name using the the inquiry ref..  */
    2720         2252 :                   s = primary->symtree ? primary->symtree->n.sym : NULL;
    2721         2224 :                   if (s && s->assoc && s->assoc->target
    2722          594 :                       && (s->ts.type == BT_UNKNOWN
    2723          450 :                           || (primary->ts.type == BT_UNKNOWN
    2724           48 :                               && s->assoc->inferred_type
    2725           48 :                               && s->ts.type == BT_DERIVED)))
    2726              :                     {
    2727          192 :                       if (tmp->u.i == INQUIRY_RE || tmp->u.i == INQUIRY_IM)
    2728              :                         {
    2729           96 :                           s->ts.type = BT_COMPLEX;
    2730           96 :                           s->ts.kind = gfc_default_real_kind;;
    2731           96 :                           s->assoc->inferred_type = 1;
    2732           96 :                           primary->ts = s->ts;
    2733              :                         }
    2734           96 :                       else if (tmp->u.i == INQUIRY_LEN)
    2735              :                         {
    2736           48 :                           s->ts.type = BT_CHARACTER;
    2737           48 :                           s->ts.kind = gfc_default_character_kind;;
    2738           48 :                           s->assoc->inferred_type = 1;
    2739           48 :                           primary->ts = s->ts;
    2740              :                         }
    2741           48 :                       else if (s->ts.type == BT_UNKNOWN)
    2742              :                         {
    2743              :                           /* KIND inquiry gives no clue as to symbol type.  */
    2744           48 :                           primary->ref = tmp;
    2745           48 :                           primary->ts.type = BT_INTEGER;
    2746           48 :                           primary->ts.kind = gfc_default_integer_kind;
    2747           48 :                           return MATCH_YES;
    2748              :                         }
    2749              :                     }
    2750              : 
    2751         2204 :                   if ((tmp->u.i == INQUIRY_RE || tmp->u.i == INQUIRY_IM)
    2752         1365 :                       && primary->ts.type != BT_COMPLEX)
    2753              :                     {
    2754           13 :                         gfc_error ("The RE or IM part_ref at %C must be "
    2755              :                                    "applied to a COMPLEX expression");
    2756           13 :                         gfc_free_ref_list (tmp);
    2757           13 :                         return MATCH_ERROR;
    2758              :                     }
    2759         2191 :                   else if (tmp->u.i == INQUIRY_LEN
    2760          476 :                            && ts->type != BT_CHARACTER)
    2761              :                     {
    2762            6 :                         gfc_error ("The LEN part_ref at %C must be applied "
    2763              :                                    "to a CHARACTER expression");
    2764            6 :                         gfc_free_ref_list (tmp);
    2765            6 :                         return MATCH_ERROR;
    2766              :                     }
    2767              :                 }
    2768         2190 :               if (primary->ts.type != BT_UNKNOWN)
    2769       195970 :                 intrinsic = true;
    2770              :             }
    2771              :         }
    2772              :       else
    2773              :         inquiry = false;
    2774              : 
    2775       195970 :       if (sym && sym->f2k_derived)
    2776       190981 :         tbp = gfc_find_typebound_proc (sym, &t, name, false, &gfc_current_locus);
    2777              :       else
    2778              :         tbp = NULL;
    2779              : 
    2780       190981 :       if (tbp)
    2781              :         {
    2782         4238 :           gfc_symbol* tbp_sym;
    2783              : 
    2784         4238 :           if (!t)
    2785              :             return MATCH_ERROR;
    2786              : 
    2787         4236 :           gcc_assert (!tail || !tail->next);
    2788              : 
    2789         4236 :           if (!(primary->expr_type == EXPR_VARIABLE
    2790              :                 || (primary->expr_type == EXPR_STRUCTURE
    2791            1 :                     && primary->symtree && primary->symtree->n.sym
    2792            1 :                     && primary->symtree->n.sym->attr.flavor)))
    2793              :             return MATCH_ERROR;
    2794              : 
    2795         4234 :           if (tbp->n.tb->is_generic)
    2796              :             tbp_sym = NULL;
    2797              :           else
    2798         3350 :             tbp_sym = tbp->n.tb->u.specific->n.sym;
    2799              : 
    2800         4234 :           primary->expr_type = EXPR_COMPCALL;
    2801         4234 :           primary->value.compcall.tbp = tbp->n.tb;
    2802         4234 :           primary->value.compcall.name = tbp->name;
    2803         4234 :           primary->value.compcall.ignore_pass = 0;
    2804         4234 :           primary->value.compcall.assign = 0;
    2805         4234 :           primary->value.compcall.base_object = NULL;
    2806         4234 :           gcc_assert (primary->symtree->n.sym->attr.referenced);
    2807         4234 :           if (tbp_sym)
    2808         3350 :             primary->ts = tbp_sym->ts;
    2809              :           else
    2810          884 :             gfc_clear_ts (&primary->ts);
    2811              : 
    2812         4234 :           m = gfc_match_actual_arglist (tbp->n.tb->subroutine,
    2813              :                                         &primary->value.compcall.actual);
    2814         4234 :           if (m == MATCH_ERROR)
    2815              :             return MATCH_ERROR;
    2816         4234 :           if (m == MATCH_NO)
    2817              :             {
    2818          180 :               if (sub_flag)
    2819          179 :                 primary->value.compcall.actual = NULL;
    2820              :               else
    2821              :                 {
    2822              :                   /* Before erroring, check whether there is also a data
    2823              :                      component with this name.  Use noaccess=true so
    2824              :                      that private components are also found.  */
    2825            1 :                   if (sym && gfc_find_component (sym, name, true, true, NULL))
    2826              :                     {
    2827              :                       /* Restore expr to EXPR_VARIABLE and let the data
    2828              :                          component path below handle it.  */
    2829            0 :                       primary->expr_type = EXPR_VARIABLE;
    2830            0 :                       gfc_free_actual_arglist (primary->value.compcall.actual);
    2831            0 :                       primary->value.compcall.actual = NULL;
    2832            0 :                       tbp = NULL;
    2833            0 :                       goto try_data_component;
    2834              :                     }
    2835            1 :                   gfc_error ("Expected argument list at %C");
    2836            1 :                   return MATCH_ERROR;
    2837              :                 }
    2838              :             }
    2839              : 
    2840       169025 :           break;
    2841              :         }
    2842              : 
    2843       186743 :     try_data_component:
    2844              : 
    2845       191732 :       previous = component;
    2846              : 
    2847       191732 :       if (!inquiry && !intrinsic)
    2848              :         {
    2849       189544 :           component = gfc_find_component (sym, name, false, false, &tmp);
    2850              :           /* For inferred-type ASSOCIATE names the parse-time candidate type
    2851              :              may not be the final type; a private component in the candidate
    2852              :              type may correspond to a public component in the correct type.
    2853              :              Accept it tentatively so that resolution can fix up the type.  */
    2854       189544 :           if (!component && !tbp
    2855           48 :               && primary->symtree && primary->symtree->n.sym->assoc
    2856            1 :               && primary->symtree->n.sym->assoc->inferred_type)
    2857            1 :             component = gfc_find_component (sym, name, true, false, &tmp);
    2858              :         }
    2859              :       else
    2860              :         component = NULL;
    2861              : 
    2862       191732 :       if (previous && inquiry
    2863          499 :           && (previous->attr.pdt_kind || previous->attr.pdt_len))
    2864              :         {
    2865            4 :           gfc_error_now ("R901: A type parameter ref is not a designator and "
    2866              :                      "cannot be followed by the type inquiry ref at %C");
    2867            4 :           return MATCH_ERROR;
    2868              :         }
    2869              : 
    2870       191728 :       if (intrinsic && !inquiry)
    2871              :         {
    2872            3 :           if (previous)
    2873            2 :             gfc_error ("%qs at %C is not an inquiry reference to an intrinsic "
    2874              :                         "type component %qs", name, previous->name);
    2875              :           else
    2876            1 :             gfc_error ("%qs at %C is not an inquiry reference to an intrinsic "
    2877              :                         "type component", name);
    2878              :           return MATCH_ERROR;
    2879              :         }
    2880       191725 :       else if (component == NULL && !inquiry)
    2881              :         return MATCH_ERROR;
    2882              : 
    2883              :       /* Extend the reference chain determined by gfc_find_component or
    2884              :          is_inquiry_ref.  */
    2885       191678 :       if (primary->ref == NULL)
    2886       114884 :         primary->ref = tmp;
    2887              :       else
    2888              :         {
    2889              :           /* Find end of reference chain if inquiry reference and tail not
    2890              :              set.  */
    2891        76794 :           if (tail == NULL && inquiry && tmp)
    2892           35 :             tail = extend_ref (primary, tail);
    2893              : 
    2894              :           /* Set by the for loop below for the last component ref.  */
    2895        76794 :           gcc_assert (tail != NULL);
    2896        76794 :           tail->next = tmp;
    2897              :         }
    2898              : 
    2899              :       /* The reference chain may be longer than one hop for union
    2900              :          subcomponents; find the new tail.  */
    2901       193654 :       for (tail = tmp; tail->next; tail = tail->next)
    2902              :         ;
    2903              : 
    2904       191678 :       if (tmp && tmp->type == REF_INQUIRY)
    2905              :         {
    2906         2181 :           if (!primary->where.u.lb || !primary->where.nextc)
    2907         1997 :             primary->where = gfc_current_locus;
    2908         2181 :           gfc_simplify_expr (primary, 0);
    2909              : 
    2910         2181 :           if (primary->expr_type == EXPR_CONSTANT)
    2911          510 :             goto check_done;
    2912              : 
    2913         1671 :           if (primary->ref == NULL)
    2914           60 :             goto check_done;
    2915              : 
    2916         1611 :           switch (tmp->u.i)
    2917              :             {
    2918         1208 :             case INQUIRY_RE:
    2919         1208 :             case INQUIRY_IM:
    2920         1208 :               if (!gfc_notify_std (GFC_STD_F2008, "RE or IM part_ref at %C"))
    2921              :                 return MATCH_ERROR;
    2922              : 
    2923         1208 :               if (primary->ts.type != BT_COMPLEX)
    2924              :                 {
    2925            0 :                   gfc_error ("The RE or IM part_ref at %C must be "
    2926              :                              "applied to a COMPLEX expression");
    2927            0 :                   return MATCH_ERROR;
    2928              :                 }
    2929         1208 :               primary->ts.type = BT_REAL;
    2930         1208 :               break;
    2931              : 
    2932          351 :             case INQUIRY_LEN:
    2933          351 :               if (!gfc_notify_std (GFC_STD_F2003, "LEN part_ref at %C"))
    2934              :                 return MATCH_ERROR;
    2935              : 
    2936          351 :               if (primary->ts.type != BT_CHARACTER)
    2937              :                 {
    2938            0 :                   gfc_error ("The LEN part_ref at %C must be applied "
    2939              :                              "to a CHARACTER expression");
    2940            0 :                   return MATCH_ERROR;
    2941              :                 }
    2942          351 :               primary->ts.u.cl = NULL;
    2943          351 :               primary->ts.deferred = false;
    2944          351 :               primary->ts.type = BT_INTEGER;
    2945          351 :               primary->ts.kind = gfc_default_integer_kind;
    2946          351 :               break;
    2947              : 
    2948           52 :             case INQUIRY_KIND:
    2949           52 :               if (!gfc_notify_std (GFC_STD_F2003, "KIND part_ref at %C"))
    2950              :                 return MATCH_ERROR;
    2951              : 
    2952           52 :               if (primary->ts.type == BT_CLASS
    2953           52 :                   || primary->ts.type == BT_DERIVED)
    2954              :                 {
    2955            0 :                   gfc_error ("The KIND part_ref at %C must be applied "
    2956              :                              "to an expression of intrinsic type");
    2957            0 :                   return MATCH_ERROR;
    2958              :                 }
    2959           52 :               if (primary->ts.type == BT_CHARACTER)
    2960              :                 {
    2961            0 :                   primary->ts.u.cl = NULL;
    2962            0 :                   primary->ts.deferred = false;
    2963              :                 }
    2964           52 :               primary->ts.type = BT_INTEGER;
    2965           52 :               primary->ts.kind = gfc_default_integer_kind;
    2966           52 :               break;
    2967              : 
    2968            0 :             default:
    2969            0 :               gcc_unreachable ();
    2970              :             }
    2971              : 
    2972         1611 :           goto check_done;
    2973              :         }
    2974              : 
    2975       189497 :       primary->ts = component->ts;
    2976              : 
    2977       189497 :       if (component->attr.proc_pointer && ppc_arg)
    2978              :         {
    2979              :           /* Procedure pointer component call: Look for argument list.  */
    2980         1260 :           m = gfc_match_actual_arglist (sub_flag,
    2981              :                                         &primary->value.compcall.actual);
    2982         1260 :           if (m == MATCH_ERROR)
    2983              :             return MATCH_ERROR;
    2984              : 
    2985         1260 :           if (m == MATCH_NO && !gfc_matching_ptr_assignment
    2986          346 :               && !gfc_matching_procptr_assignment && !matching_actual_arglist)
    2987              :             {
    2988            3 :               gfc_error ("Procedure pointer component %qs requires an "
    2989              :                          "argument list at %C", component->name);
    2990            3 :               return MATCH_ERROR;
    2991              :             }
    2992              : 
    2993         1257 :           if (m == MATCH_YES)
    2994          913 :             primary->expr_type = EXPR_PPC;
    2995              : 
    2996              :           break;
    2997              :         }
    2998              : 
    2999       188237 :       if (component->as != NULL && !component->attr.proc_pointer)
    3000              :         {
    3001        70295 :           tail = extend_ref (primary, tail);
    3002        70295 :           tail->type = REF_ARRAY;
    3003              : 
    3004       140590 :           m = gfc_match_array_ref (&tail->u.ar, component->as, equiv_flag,
    3005        70295 :                           component->as->corank);
    3006        70295 :           if (m != MATCH_YES)
    3007              :             return m;
    3008              :         }
    3009       117942 :       else if (component->ts.type == BT_CLASS && component->attr.class_ok
    3010        11738 :                && CLASS_DATA (component)->as && !component->attr.proc_pointer)
    3011              :         {
    3012         5729 :           tail = extend_ref (primary, tail);
    3013         5729 :           tail->type = REF_ARRAY;
    3014              : 
    3015        11458 :           m = gfc_match_array_ref (&tail->u.ar, CLASS_DATA (component)->as,
    3016              :                                    equiv_flag,
    3017         5729 :                                    CLASS_DATA (component)->as->corank);
    3018         5729 :           if (m != MATCH_YES)
    3019              :             return m;
    3020              :         }
    3021              : 
    3022       112213 : check_done:
    3023              :       /* In principle, we could have eg. expr%re%kind so we must allow for
    3024              :          this possibility.  */
    3025       190418 :       if (gfc_match_char ('%') == MATCH_YES)
    3026              :         {
    3027        26513 :           if (component && (component->ts.type == BT_DERIVED
    3028         3844 :                             || component->ts.type == BT_CLASS))
    3029        25954 :             sym = component->ts.u.derived;
    3030        26513 :           continue;
    3031              :         }
    3032       163905 :       else if (inquiry)
    3033              :         break;
    3034              : 
    3035       151581 :       if ((component->ts.type != BT_DERIVED && component->ts.type != BT_CLASS)
    3036       170177 :           || gfc_match_member_sep (component->ts.u.derived) != MATCH_YES)
    3037              :         break;
    3038              : 
    3039          370 :       if (component->ts.type == BT_DERIVED || component->ts.type == BT_CLASS)
    3040          370 :         sym = component->ts.u.derived;
    3041              :     }
    3042              : 
    3043      5273667 : check_substring:
    3044      5273667 :   unknown = false;
    3045      5273667 :   if (primary->ts.type == BT_UNKNOWN && !gfc_fl_struct (sym->attr.flavor))
    3046              :     {
    3047      2805574 :       if (gfc_get_default_type (sym->name, sym->ns)->type == BT_CHARACTER)
    3048              :        {
    3049          352 :          gfc_set_default_type (sym, 0, sym->ns);
    3050          352 :          primary->ts = sym->ts;
    3051          352 :          unknown = true;
    3052              :        }
    3053              :     }
    3054              : 
    3055      5273667 :   if (primary->ts.type == BT_CHARACTER)
    3056              :     {
    3057       316018 :       bool def = primary->ts.deferred == 1;
    3058       316018 :       switch (match_substring (primary->ts.u.cl, equiv_flag, &substring, def))
    3059              :         {
    3060        15503 :         case MATCH_YES:
    3061        15503 :           if (tail == NULL)
    3062        10200 :             primary->ref = substring;
    3063              :           else
    3064         5303 :             tail->next = substring;
    3065              : 
    3066        15503 :           if (primary->expr_type == EXPR_CONSTANT)
    3067          758 :             primary->expr_type = EXPR_SUBSTRING;
    3068              : 
    3069        15503 :           if (substring)
    3070        15323 :             primary->ts.u.cl = NULL;
    3071              : 
    3072        15503 :           gfc_gobble_whitespace ();
    3073        15503 :           if (gfc_peek_ascii_char () == '(')
    3074              :             {
    3075            5 :               gfc_error_now ("Unexpected array/substring ref at %C");
    3076            5 :               return MATCH_ERROR;
    3077              :             }
    3078              :           break;
    3079              : 
    3080       300515 :         case MATCH_NO:
    3081       300515 :           if (unknown)
    3082              :             {
    3083          351 :               gfc_clear_ts (&primary->ts);
    3084          351 :               gfc_clear_ts (&sym->ts);
    3085              :             }
    3086              :           break;
    3087              : 
    3088              :         case MATCH_ERROR:
    3089              :           return MATCH_ERROR;
    3090              :         }
    3091              :     }
    3092              : 
    3093              :   /* F08:C611.  */
    3094      5273662 :   if (primary->ts.type == BT_DERIVED && primary->ref
    3095        30511 :       && primary->ts.u.derived && primary->ts.u.derived->attr.abstract)
    3096              :     {
    3097            6 :       gfc_error ("Nonpolymorphic reference to abstract type at %C");
    3098            6 :       return MATCH_ERROR;
    3099              :     }
    3100              : 
    3101              :   /* F08:C727.  */
    3102      5273656 :   if (primary->expr_type == EXPR_PPC && gfc_is_coindexed (primary))
    3103              :     {
    3104            3 :       gfc_error ("Coindexed procedure-pointer component at %C");
    3105            3 :       return MATCH_ERROR;
    3106              :     }
    3107              : 
    3108              :   return MATCH_YES;
    3109              : }
    3110              : 
    3111              : 
    3112              : /* Given an expression that is a variable, figure out what the
    3113              :    ultimate variable's attribute is, traversing the reference
    3114              :    structures if necessary.
    3115              : 
    3116              :    This subroutine is trickier than it looks.  We start at the base
    3117              :    symbol and store the attribute.  Component references load a
    3118              :    completely new attribute.
    3119              : 
    3120              :    A couple of rules come into play.  Subobjects of targets are always
    3121              :    targets themselves.  If we see a component that goes through a
    3122              :    pointer, then the expression must also be a target, since the
    3123              :    pointer is associated with something (if it isn't core will soon be
    3124              :    dumped).  If we see a full part or section of an array, the
    3125              :    expression is also an array.
    3126              : 
    3127              :    We can have at most one full array reference.  */
    3128              : 
    3129              : symbol_attribute
    3130      4258335 : gfc_variable_attr (gfc_expr *expr)
    3131              : {
    3132      4258335 :   int dimension, codimension, pointer, allocatable, target, optional;
    3133      4258335 :   symbol_attribute attr;
    3134      4258335 :   gfc_ref *ref;
    3135      4258335 :   gfc_symbol *sym;
    3136      4258335 :   gfc_component *comp;
    3137      4258335 :   gfc_typespec *current_ts;
    3138              : 
    3139      4258335 :   if (expr->expr_type != EXPR_VARIABLE
    3140        73322 :       && expr->expr_type != EXPR_FUNCTION
    3141            9 :       && !(expr->expr_type == EXPR_NULL && expr->ts.type != BT_UNKNOWN))
    3142            0 :     gfc_internal_error ("gfc_variable_attr(): Expression isn't a variable");
    3143              : 
    3144      4258335 :   sym = expr->symtree->n.sym;
    3145      4258335 :   attr = sym->attr;
    3146      4258335 :   current_ts = &sym->ts;
    3147              : 
    3148              :   /* If we are in the body of a function, the function name references the
    3149              :      function result, not the function itself.  */
    3150      4258335 :   if (attr.function
    3151       161598 :       && sym->result == sym
    3152       127755 :       && gfc_current_ns
    3153       127755 :       && gfc_current_ns->proc_name == sym)
    3154              :     {
    3155        46206 :       attr.function = 0;
    3156        46206 :       attr.elemental = 0;
    3157        46206 :       attr.pure = 0;
    3158        46206 :       attr.recursive = 0;
    3159        46206 :       attr.flavor = FL_VARIABLE;
    3160        46206 :       attr.result = 1;
    3161              :     }
    3162              : 
    3163      4258335 :   optional = attr.optional;
    3164      4258335 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok && sym->ts.u.derived)
    3165              :     {
    3166       146588 :       dimension = CLASS_DATA (sym)->attr.dimension;
    3167       146588 :       codimension = CLASS_DATA (sym)->attr.codimension;
    3168       146588 :       pointer = CLASS_DATA (sym)->attr.class_pointer;
    3169       146588 :       allocatable = CLASS_DATA (sym)->attr.allocatable;
    3170              :     }
    3171              :   else
    3172              :     {
    3173      4111747 :       dimension = attr.dimension;
    3174      4111747 :       codimension = attr.codimension;
    3175      4111747 :       pointer = attr.pointer;
    3176      4111747 :       allocatable = attr.allocatable;
    3177              :     }
    3178              : 
    3179      4258335 :   target = attr.target;
    3180      4258335 :   if (pointer || attr.proc_pointer)
    3181       211775 :     target = 1;
    3182              : 
    3183              :   /* F2023:11.1.3.3: Other attributes of associate names
    3184              :      "The associating entity does not have the ALLOCATABLE or POINTER
    3185              :      attributes; it has the TARGET attribute if and only if the selector is
    3186              :      a variable and has either the TARGET or POINTER attribute."  */
    3187      4258335 :   if (sym->attr.associate_var && sym->assoc && sym->assoc->target)
    3188              :     {
    3189        27677 :       gfc_expr *tgt = sym->assoc->target;
    3190              : 
    3191        27677 :       if (tgt->expr_type == EXPR_VARIABLE)
    3192              :         {
    3193        24615 :           symbol_attribute tgt_attr;
    3194              : 
    3195              :           /* The _data reference that narrows a SELECT TYPE selector is a
    3196              :              pointer whatever the selector is, so query the selector.  */
    3197        24615 :           if (tgt->ref && !tgt->ref->next && tgt->ref->type == REF_COMPONENT
    3198         5807 :               && tgt->ref->u.c.component
    3199         5807 :               && strcmp (tgt->ref->u.c.component->name, "_data") == 0)
    3200              :             {
    3201         4879 :               gfc_expr *bare = gfc_get_variable_expr (tgt->symtree);
    3202         4879 :               tgt_attr = gfc_expr_attr (bare);
    3203         4879 :               gfc_free_expr (bare);
    3204         4879 :             }
    3205              :           else
    3206        19736 :             tgt_attr = gfc_expr_attr (tgt);
    3207        30572 :           target = (tgt_attr.pointer || tgt_attr.target);
    3208              :         }
    3209              :       else
    3210              :         target = 0;
    3211              : 
    3212              :       /* SELECT RANK's associating entity has the selector's ALLOCATABLE
    3213              :          and POINTER attributes (F2023:11.1.10.3).  */
    3214        27677 :       if (!sym->attr.select_rank_temporary)
    3215        22595 :         pointer = allocatable = 0;
    3216              :     }
    3217              : 
    3218      5858486 :   for (ref = expr->ref; ref; ref = ref->next)
    3219      1601805 :     if (ref->type == REF_SUBSTRING)
    3220              :       optional = false;
    3221      1588312 :     else if (ref->type == REF_INQUIRY)
    3222              :       {
    3223              :         optional = false;
    3224              :         break;
    3225              :       }
    3226              : 
    3227      5860140 :   for (ref = expr->ref; ref; ref = ref->next)
    3228      1601805 :     switch (ref->type)
    3229              :       {
    3230      1231913 :       case REF_ARRAY:
    3231              : 
    3232      1231913 :         switch (ref->u.ar.type)
    3233              :           {
    3234              :           case AR_FULL:
    3235      1601805 :             dimension = 1;
    3236              :             break;
    3237              : 
    3238       101130 :           case AR_SECTION:
    3239       101130 :             allocatable = pointer = 0;
    3240       101130 :             dimension = 1;
    3241       101130 :             optional = false;
    3242       101130 :             break;
    3243              : 
    3244       232358 :           case AR_ELEMENT:
    3245              :             /* Handle coarrays.  */
    3246       232358 :             if (ref->u.ar.dimen > 0)
    3247      1601805 :               allocatable = pointer = optional = false;
    3248              :             break;
    3249              : 
    3250              :           case AR_UNKNOWN:
    3251              :             /* For standard conforming code, AR_UNKNOWN should not happen.
    3252              :                For nonconforming code, gfortran can end up here.  Treat it
    3253              :                as a no-op.  */
    3254              :             break;
    3255              :           }
    3256              : 
    3257              :         break;
    3258              : 
    3259       354745 :       case REF_COMPONENT:
    3260       354745 :         comp = ref->u.c.component;
    3261       354745 :         if (!(current_ts->type == BT_CLASS
    3262        66473 :               && current_ts->u.derived->attr.is_class
    3263        66458 :               && strcmp (comp->name, "_data") == 0))
    3264              :           {
    3265       322026 :             optional = false;
    3266       322026 :             attr = comp->attr;
    3267              :           }
    3268       354745 :         current_ts = &sym->ts;
    3269              : 
    3270       354745 :         if (comp->ts.type == BT_CLASS)
    3271              :           {
    3272        26838 :             dimension = CLASS_DATA (comp)->attr.dimension;
    3273        26838 :             codimension = CLASS_DATA (comp)->attr.codimension;
    3274        26838 :             pointer = CLASS_DATA (comp)->attr.class_pointer;
    3275        26838 :             allocatable = CLASS_DATA (comp)->attr.allocatable;
    3276              :           }
    3277              :         else
    3278              :           {
    3279       327907 :             dimension = comp->attr.dimension;
    3280       327907 :             codimension = comp->attr.codimension;
    3281       327907 :             if (expr->ts.type == BT_CLASS && strcmp (comp->name, "_data") == 0)
    3282        20181 :               pointer = comp->attr.class_pointer;
    3283              :             else
    3284       307726 :               pointer = comp->attr.pointer;
    3285       327907 :             allocatable = comp->attr.allocatable;
    3286              :           }
    3287       354745 :         if (pointer || attr.proc_pointer)
    3288        61344 :           target = 1;
    3289              : 
    3290              :         break;
    3291              : 
    3292        15147 :       case REF_INQUIRY:
    3293        15147 :       case REF_SUBSTRING:
    3294        15147 :         allocatable = pointer = optional = false;
    3295        15147 :         break;
    3296              :       }
    3297              : 
    3298      4258335 :   attr.dimension = dimension;
    3299      4258335 :   attr.codimension = codimension;
    3300      4258335 :   attr.pointer = pointer;
    3301      4258335 :   attr.allocatable = allocatable;
    3302      4258335 :   attr.target = target;
    3303      4258335 :   attr.save = sym->attr.save;
    3304      4258335 :   attr.optional = optional;
    3305              : 
    3306      4258335 :   return attr;
    3307              : }
    3308              : 
    3309              : 
    3310              : /* Return the attribute from a general expression.  */
    3311              : 
    3312              : symbol_attribute
    3313      5255502 : gfc_expr_attr (gfc_expr *e)
    3314              : {
    3315      5255502 :   symbol_attribute attr;
    3316              : 
    3317      5255502 :   switch (e->expr_type)
    3318              :     {
    3319      4175768 :     case EXPR_VARIABLE:
    3320      4175768 :       attr = gfc_variable_attr (e);
    3321      4175768 :       break;
    3322              : 
    3323       101244 :     case EXPR_FUNCTION:
    3324       101244 :       gfc_clear_attr (&attr);
    3325              : 
    3326       101244 :       if (e->value.function.esym && e->value.function.esym->result)
    3327              :         {
    3328        27595 :           gfc_symbol *sym = e->value.function.esym->result;
    3329        27595 :           attr = sym->attr;
    3330        27595 :           if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
    3331              :             {
    3332         2523 :               attr.dimension = CLASS_DATA (sym)->attr.dimension;
    3333         2523 :               attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
    3334         2523 :               attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
    3335              :             }
    3336              :         }
    3337        73649 :       else if (e->value.function.isym
    3338        70365 :                && e->value.function.isym->transformational
    3339        35218 :                && e->ts.type == BT_CLASS)
    3340          330 :         attr = CLASS_DATA (e)->attr;
    3341        73319 :       else if (e->symtree)
    3342        73313 :         attr = gfc_variable_attr (e);
    3343              : 
    3344              :       /* TODO: NULL() returns pointers.  May have to take care of this
    3345              :          here.  */
    3346              : 
    3347              :       break;
    3348              : 
    3349       978490 :     default:
    3350       978490 :       gfc_clear_attr (&attr);
    3351       978490 :       break;
    3352              :     }
    3353              : 
    3354      5255502 :   return attr;
    3355              : }
    3356              : 
    3357              : 
    3358              : /* Given an expression, figure out what the ultimate expression
    3359              :    attribute is.  This routine is similar to gfc_variable_attr with
    3360              :    parts of gfc_expr_attr, but focuses more on the needs of
    3361              :    coarrays.  For coarrays a codimension attribute is kind of
    3362              :    "infectious" being propagated once set and never cleared.
    3363              :    The coarray_comp is only set, when the expression refs a coarray
    3364              :    component.  REFS_COMP is set when present to true only, when this EXPR
    3365              :    refs a (non-_data) component.  To check whether EXPR refs an allocatable
    3366              :    component in a derived type coarray *refs_comp needs to be set and
    3367              :    coarray_comp has to false.  */
    3368              : 
    3369              : static symbol_attribute
    3370        17115 : caf_variable_attr (gfc_expr *expr, bool in_allocate, bool *refs_comp)
    3371              : {
    3372        17115 :   int dimension, codimension, pointer, allocatable, target, coarray_comp;
    3373        17115 :   symbol_attribute attr;
    3374        17115 :   gfc_ref *ref;
    3375        17115 :   gfc_symbol *sym;
    3376        17115 :   gfc_component *comp;
    3377              : 
    3378        17115 :   if (expr->expr_type != EXPR_VARIABLE && expr->expr_type != EXPR_FUNCTION)
    3379            0 :     gfc_internal_error ("gfc_caf_attr(): Expression isn't a variable");
    3380              : 
    3381        17115 :   sym = expr->symtree->n.sym;
    3382        17115 :   gfc_clear_attr (&attr);
    3383              : 
    3384        17115 :   if (refs_comp)
    3385        11699 :     *refs_comp = false;
    3386              : 
    3387        17115 :   if (sym->ts.type == BT_CLASS && sym->attr.class_ok)
    3388              :     {
    3389          430 :       dimension = CLASS_DATA (sym)->attr.dimension;
    3390          430 :       codimension = CLASS_DATA (sym)->attr.codimension;
    3391          430 :       pointer = CLASS_DATA (sym)->attr.class_pointer;
    3392          430 :       allocatable = CLASS_DATA (sym)->attr.allocatable;
    3393          430 :       attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
    3394          430 :       attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived->attr.pointer_comp;
    3395              :     }
    3396              :   else
    3397              :     {
    3398        16685 :       dimension = sym->attr.dimension;
    3399        16685 :       codimension = sym->attr.codimension;
    3400        16685 :       pointer = sym->attr.pointer;
    3401        16685 :       allocatable = sym->attr.allocatable;
    3402        33370 :       attr.alloc_comp = sym->ts.type == BT_DERIVED
    3403        16685 :           ? sym->ts.u.derived->attr.alloc_comp : 0;
    3404        16685 :       attr.pointer_comp = sym->ts.type == BT_DERIVED
    3405        16685 :           ? sym->ts.u.derived->attr.pointer_comp : 0;
    3406              :     }
    3407              : 
    3408        17115 :   target = coarray_comp = 0;
    3409        17115 :   if (pointer || attr.proc_pointer)
    3410          654 :     target = 1;
    3411              : 
    3412        29768 :   for (ref = expr->ref; ref; ref = ref->next)
    3413        12653 :     switch (ref->type)
    3414              :       {
    3415         8836 :       case REF_ARRAY:
    3416              : 
    3417         8836 :         switch (ref->u.ar.type)
    3418              :           {
    3419              :           case AR_FULL:
    3420              :           case AR_SECTION:
    3421              :             dimension = 1;
    3422         8836 :             break;
    3423              : 
    3424         4232 :           case AR_ELEMENT:
    3425              :             /* Handle coarrays.  */
    3426         4232 :             if (ref->u.ar.dimen > 0 && !in_allocate)
    3427         8836 :               allocatable = pointer = 0;
    3428              :             break;
    3429              : 
    3430            0 :           case AR_UNKNOWN:
    3431              :             /* If any of start, end or stride is not integer, there will
    3432              :                already have been an error issued.  */
    3433            0 :             int errors;
    3434            0 :             gfc_get_errors (NULL, &errors);
    3435            0 :             if (errors == 0)
    3436            0 :               gfc_internal_error ("gfc_caf_attr(): Bad array reference");
    3437              :           }
    3438              : 
    3439              :         break;
    3440              : 
    3441         3815 :       case REF_COMPONENT:
    3442         3815 :         comp = ref->u.c.component;
    3443              : 
    3444         3815 :         if (comp->ts.type == BT_CLASS)
    3445              :           {
    3446              :             /* Set coarray_comp only, when this component introduces the
    3447              :                coarray.  */
    3448           13 :             coarray_comp = !codimension && CLASS_DATA (comp)->attr.codimension;
    3449           13 :             codimension |= CLASS_DATA (comp)->attr.codimension;
    3450           13 :             pointer = CLASS_DATA (comp)->attr.class_pointer;
    3451           13 :             allocatable = CLASS_DATA (comp)->attr.allocatable;
    3452              :           }
    3453              :         else
    3454              :           {
    3455              :             /* Set coarray_comp only, when this component introduces the
    3456              :                coarray.  */
    3457         3802 :             coarray_comp = !codimension && comp->attr.codimension;
    3458         3802 :             codimension |= comp->attr.codimension;
    3459         3802 :             pointer = comp->attr.pointer;
    3460         3802 :             allocatable = comp->attr.allocatable;
    3461              :           }
    3462              : 
    3463         3815 :         if (refs_comp && strcmp (comp->name, "_data") != 0
    3464         2253 :             && (ref->next == NULL
    3465         1724 :                 || (ref->next->type == REF_ARRAY && ref->next->next == NULL)))
    3466         1684 :           *refs_comp = true;
    3467              : 
    3468         3815 :         if (pointer || attr.proc_pointer)
    3469          690 :           target = 1;
    3470              : 
    3471              :         break;
    3472              : 
    3473              :       case REF_SUBSTRING:
    3474              :       case REF_INQUIRY:
    3475        12653 :         allocatable = pointer = 0;
    3476              :         break;
    3477              :       }
    3478              : 
    3479        17115 :   attr.dimension = dimension;
    3480        17115 :   attr.codimension = codimension;
    3481        17115 :   attr.pointer = pointer;
    3482        17115 :   attr.allocatable = allocatable;
    3483        17115 :   attr.target = target;
    3484        17115 :   attr.save = sym->attr.save;
    3485        17115 :   attr.coarray_comp = coarray_comp;
    3486              : 
    3487        17115 :   return attr;
    3488              : }
    3489              : 
    3490              : 
    3491              : symbol_attribute
    3492        21281 : gfc_caf_attr (gfc_expr *e, bool in_allocate, bool *refs_comp)
    3493              : {
    3494        21281 :   symbol_attribute attr;
    3495              : 
    3496        21281 :   switch (e->expr_type)
    3497              :     {
    3498        15356 :     case EXPR_VARIABLE:
    3499        15356 :       attr = caf_variable_attr (e, in_allocate, refs_comp);
    3500        15356 :       break;
    3501              : 
    3502         1765 :     case EXPR_FUNCTION:
    3503         1765 :       gfc_clear_attr (&attr);
    3504              : 
    3505         1765 :       if (e->value.function.esym && e->value.function.esym->result)
    3506              :         {
    3507            6 :           gfc_symbol *sym = e->value.function.esym->result;
    3508            6 :           attr = sym->attr;
    3509            6 :           if (sym->ts.type == BT_CLASS)
    3510              :             {
    3511            0 :               attr.dimension = CLASS_DATA (sym)->attr.dimension;
    3512            0 :               attr.pointer = CLASS_DATA (sym)->attr.class_pointer;
    3513            0 :               attr.allocatable = CLASS_DATA (sym)->attr.allocatable;
    3514            0 :               attr.alloc_comp = CLASS_DATA (sym)->ts.u.derived->attr.alloc_comp;
    3515            0 :               attr.pointer_comp = CLASS_DATA (sym)->ts.u.derived
    3516            0 :                   ->attr.pointer_comp;
    3517              :             }
    3518              :         }
    3519         1759 :       else if (e->symtree)
    3520         1759 :         attr = caf_variable_attr (e, in_allocate, refs_comp);
    3521              :       else
    3522            0 :         gfc_clear_attr (&attr);
    3523              :       break;
    3524              : 
    3525         4160 :     default:
    3526         4160 :       gfc_clear_attr (&attr);
    3527         4160 :       break;
    3528              :     }
    3529              : 
    3530        21281 :   return attr;
    3531              : }
    3532              : 
    3533              : 
    3534              : /* Match a structure constructor.  The initial symbol has already been
    3535              :    seen.  */
    3536              : 
    3537              : typedef struct gfc_structure_ctor_component
    3538              : {
    3539              :   char* name;
    3540              :   gfc_expr* val;
    3541              :   locus where;
    3542              :   struct gfc_structure_ctor_component* next;
    3543              : }
    3544              : gfc_structure_ctor_component;
    3545              : 
    3546              : #define gfc_get_structure_ctor_component() XCNEW (gfc_structure_ctor_component)
    3547              : 
    3548              : static void
    3549        11844 : gfc_free_structure_ctor_component (gfc_structure_ctor_component *comp)
    3550              : {
    3551        11844 :   free (comp->name);
    3552        11844 :   gfc_free_expr (comp->val);
    3553        11844 :   free (comp);
    3554        11844 : }
    3555              : 
    3556              : 
    3557              : /* Translate the component list into the actual constructor by sorting it in
    3558              :    the order required; this also checks along the way that each and every
    3559              :    component actually has an initializer and handles default initializers
    3560              :    for components without explicit value given.  */
    3561              : static bool
    3562         8158 : build_actual_constructor (gfc_structure_ctor_component **comp_head,
    3563              :                           gfc_constructor_base *ctor_head, gfc_symbol *sym)
    3564              : {
    3565         8158 :   gfc_structure_ctor_component *comp_iter;
    3566         8158 :   gfc_component *comp;
    3567              : 
    3568        21512 :   for (comp = sym->components; comp; comp = comp->next)
    3569              :     {
    3570        13366 :       gfc_structure_ctor_component **next_ptr;
    3571        13366 :       gfc_expr *value = NULL;
    3572              : 
    3573              :       /* Try to find the initializer for the current component by name.  */
    3574        13366 :       next_ptr = comp_head;
    3575        14599 :       for (comp_iter = *comp_head; comp_iter; comp_iter = comp_iter->next)
    3576              :         {
    3577        13053 :           if (!strcmp (comp_iter->name, comp->name))
    3578              :             break;
    3579         1233 :           next_ptr = &comp_iter->next;
    3580              :         }
    3581              : 
    3582              :       /* If an extension, try building the parent derived type by building
    3583              :          a value expression for the parent derived type and calling self.  */
    3584        13366 :       if (!comp_iter && comp == sym->components && sym->attr.extension)
    3585              :         {
    3586          132 :           value = gfc_get_structure_constructor_expr (comp->ts.type,
    3587              :                                                       comp->ts.kind,
    3588              :                                                       &gfc_current_locus);
    3589          132 :           value->ts = comp->ts;
    3590              : 
    3591          132 :           if (!build_actual_constructor (comp_head,
    3592              :                                          &value->value.constructor,
    3593          132 :                                          comp->ts.u.derived))
    3594              :             {
    3595            0 :               gfc_free_expr (value);
    3596            0 :               return false;
    3597              :             }
    3598              : 
    3599          132 :           gfc_constructor_append_expr (ctor_head, value, NULL);
    3600          132 :           continue;
    3601              :         }
    3602              : 
    3603              :       /* If it was not found, apply NULL expression to set the component as
    3604              :          unallocated. Then try the default initializer if there's any;
    3605              :          otherwise, it's an error unless this is a deferred parameter.  */
    3606         1414 :       if (!comp_iter)
    3607              :         {
    3608              :           /* F2018 7.5.10: If an allocatable component has no corresponding
    3609              :              component-data-source, then that component has an allocation
    3610              :              status of unallocated....  */
    3611         1414 :           if (comp->attr.allocatable
    3612         1273 :               || (comp->ts.type == BT_CLASS
    3613           15 :                   && CLASS_DATA (comp)->attr.allocatable))
    3614              :             {
    3615          150 :               if (!gfc_notify_std (GFC_STD_F2008, "No initializer for "
    3616              :                                    "allocatable component %qs given in the "
    3617              :                                    "structure constructor at %C", comp->name))
    3618              :                 return false;
    3619          150 :               value = gfc_get_null_expr (&gfc_current_locus);
    3620              :             }
    3621              :           /* ....(Preceding sentence) If a component with default
    3622              :              initialization has no corresponding component-data-source, then
    3623              :              the default initialization is applied to that component.  */
    3624         1264 :           else if (comp->initializer)
    3625              :             {
    3626          738 :               if (!gfc_notify_std (GFC_STD_F2003, "Structure constructor "
    3627              :                                    "with missing optional arguments at %C"))
    3628              :                 return false;
    3629          736 :               value = gfc_copy_expr (comp->initializer);
    3630              :             }
    3631              :           /* Do not trap components such as the string length for deferred
    3632              :              length character components.  */
    3633          526 :           else if (!comp->attr.artificial)
    3634              :             {
    3635           10 :               gfc_error ("No initializer for component %qs given in the"
    3636              :                          " structure constructor at %C", comp->name);
    3637           10 :               return false;
    3638              :             }
    3639              :         }
    3640              :       else
    3641        11820 :         value = comp_iter->val;
    3642              : 
    3643              :       /* Add the value to the constructor chain built.  */
    3644        13222 :       gfc_constructor_append_expr (ctor_head, value, NULL);
    3645              : 
    3646              :       /* Remove the entry from the component list.  We don't want the expression
    3647              :          value to be free'd, so set it to NULL.  */
    3648        13222 :       if (comp_iter)
    3649              :         {
    3650        11820 :           *next_ptr = comp_iter->next;
    3651        11820 :           comp_iter->val = NULL;
    3652        11820 :           gfc_free_structure_ctor_component (comp_iter);
    3653              :         }
    3654              :     }
    3655              :   return true;
    3656              : }
    3657              : 
    3658              : 
    3659              : bool
    3660         8041 : gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **cexpr,
    3661              :                                       gfc_actual_arglist **arglist,
    3662              :                                       bool parent)
    3663              : {
    3664         8041 :   gfc_actual_arglist *actual;
    3665         8041 :   gfc_structure_ctor_component *comp_tail, *comp_head, *comp_iter;
    3666         8041 :   gfc_constructor_base ctor_head = NULL;
    3667         8041 :   gfc_component *comp; /* Is set NULL when named component is first seen */
    3668         8041 :   const char* last_name = NULL;
    3669         8041 :   locus old_locus;
    3670         8041 :   gfc_expr *expr;
    3671              : 
    3672         8041 :   expr = parent ? *cexpr : e;
    3673         8041 :   old_locus = gfc_current_locus;
    3674         8041 :   if (parent)
    3675              :     ; /* gfc_current_locus = *arglist->expr ? ->where;*/
    3676              :   else
    3677         7242 :     gfc_current_locus = expr->where;
    3678              : 
    3679         8041 :   comp_tail = comp_head = NULL;
    3680              : 
    3681         8041 :   if (!parent && sym->attr.abstract)
    3682              :     {
    3683            1 :       gfc_error ("Cannot construct ABSTRACT type %qs at %L",
    3684              :                  sym->name, &expr->where);
    3685            1 :       goto cleanup;
    3686              :     }
    3687              : 
    3688         8040 :   comp = sym->components;
    3689         8040 :   actual = parent ? *arglist : expr->value.function.actual;
    3690        19205 :   for ( ; actual; )
    3691              :     {
    3692        11844 :       gfc_component *this_comp = NULL;
    3693              : 
    3694        11844 :       if (!comp_head)
    3695         7598 :         comp_tail = comp_head = gfc_get_structure_ctor_component ();
    3696              :       else
    3697              :         {
    3698         4246 :           comp_tail->next = gfc_get_structure_ctor_component ();
    3699         4246 :           comp_tail = comp_tail->next;
    3700              :         }
    3701        11844 :       if (actual->name)
    3702              :         {
    3703         1697 :           if (!gfc_notify_std (GFC_STD_F2003, "Structure"
    3704              :                                " constructor with named arguments at %C"))
    3705            1 :             goto cleanup;
    3706              : 
    3707         1696 :           comp_tail->name = xstrdup (actual->name);
    3708         1696 :           last_name = comp_tail->name;
    3709         1696 :           comp = NULL;
    3710              :         }
    3711              :       else
    3712              :         {
    3713              :           /* Components without name are not allowed after the first named
    3714              :              component initializer!  */
    3715        10147 :           if (!comp || comp->attr.artificial)
    3716              :             {
    3717            2 :               if (last_name)
    3718            0 :                 gfc_error ("Component initializer without name after component"
    3719              :                            " named %s at %L", last_name,
    3720            0 :                            actual->expr ? &actual->expr->where
    3721              :                                         : &gfc_current_locus);
    3722              :               else
    3723            2 :                 gfc_error ("Too many components in structure constructor at "
    3724            2 :                            "%L", actual->expr ? &actual->expr->where
    3725              :                                               : &gfc_current_locus);
    3726            2 :               goto cleanup;
    3727              :             }
    3728              : 
    3729        10145 :           comp_tail->name = xstrdup (comp->name);
    3730              :         }
    3731              : 
    3732              :       /* Find the current component in the structure definition and check
    3733              :          its access is not private.  */
    3734        11841 :       if (comp)
    3735        10145 :         this_comp = gfc_find_component (sym, comp->name, false, false, NULL);
    3736              :       else
    3737              :         {
    3738         1696 :           this_comp = gfc_find_component (sym, (const char *)comp_tail->name,
    3739              :                                           false, false, NULL);
    3740         1696 :           comp = NULL; /* Reset needed!  */
    3741              :         }
    3742              : 
    3743              :       /* Here we can check if a component name is given which does not
    3744              :          correspond to any component of the defined structure.  */
    3745        11841 :       if (!this_comp)
    3746            8 :         goto cleanup;
    3747              : 
    3748              :       /* For a constant string constructor, make sure the length is
    3749              :          correct; truncate or fill with blanks if needed.  */
    3750        11833 :       if (this_comp->ts.type == BT_CHARACTER && !this_comp->attr.allocatable
    3751         1224 :           && this_comp->ts.u.cl && this_comp->ts.u.cl->length
    3752         1222 :           && this_comp->ts.u.cl->length->expr_type == EXPR_CONSTANT
    3753         1197 :           && this_comp->ts.u.cl->length->ts.type == BT_INTEGER
    3754         1196 :           && actual->expr
    3755         1192 :           && actual->expr->ts.type == BT_CHARACTER
    3756         1074 :           && actual->expr->expr_type == EXPR_CONSTANT)
    3757              :         {
    3758          821 :           ptrdiff_t c, e1;
    3759          821 :           c = gfc_mpz_get_hwi (this_comp->ts.u.cl->length->value.integer);
    3760          821 :           e1 = actual->expr->value.character.length;
    3761              : 
    3762          821 :           if (c != e1)
    3763              :             {
    3764          255 :               ptrdiff_t i, to;
    3765          255 :               gfc_char_t *dest;
    3766          255 :               dest = gfc_get_wide_string (c + 1);
    3767              : 
    3768          255 :               to = e1 < c ? e1 : c;
    3769         4500 :               for (i = 0; i < to; i++)
    3770         4245 :                 dest[i] = actual->expr->value.character.string[i];
    3771              : 
    3772         5818 :               for (i = e1; i < c; i++)
    3773         5563 :                 dest[i] = ' ';
    3774              : 
    3775          255 :               dest[c] = '\0';
    3776          255 :               free (actual->expr->value.character.string);
    3777              : 
    3778          255 :               actual->expr->value.character.length = c;
    3779          255 :               actual->expr->value.character.string = dest;
    3780              : 
    3781          255 :               if (warn_line_truncation && c < e1)
    3782           20 :                 gfc_warning_now (OPT_Wcharacter_truncation,
    3783              :                                  "CHARACTER expression will be truncated "
    3784              :                                  "in constructor (%td/%td) at %L", c,
    3785              :                                  e1, &actual->expr->where);
    3786              :             }
    3787              :         }
    3788              : 
    3789        11833 :       comp_tail->val = actual->expr;
    3790        11833 :       if (actual->expr != NULL)
    3791        11828 :         comp_tail->where = actual->expr->where;
    3792        11833 :       actual->expr = NULL;
    3793              : 
    3794              :       /* Check if this component is already given a value.  */
    3795        18716 :       for (comp_iter = comp_head; comp_iter != comp_tail;
    3796         6883 :            comp_iter = comp_iter->next)
    3797              :         {
    3798         6884 :           gcc_assert (comp_iter);
    3799         6884 :           if (!strcmp (comp_iter->name, comp_tail->name))
    3800              :             {
    3801            1 :               gfc_error ("Component %qs is initialized twice in the structure"
    3802              :                          " constructor at %L", comp_tail->name,
    3803              :                          comp_tail->val ? &comp_tail->where
    3804              :                                         : &gfc_current_locus);
    3805            1 :               goto cleanup;
    3806              :             }
    3807              :         }
    3808              : 
    3809              :       /* F2008, R457/C725, for PURE C1283.  */
    3810           72 :       if (this_comp->attr.pointer && comp_tail->val
    3811        11904 :           && gfc_is_coindexed (comp_tail->val))
    3812              :         {
    3813            2 :           gfc_error ("Coindexed expression to pointer component %qs in "
    3814              :                      "structure constructor at %L", comp_tail->name,
    3815              :                      &comp_tail->where);
    3816            2 :           goto cleanup;
    3817              :         }
    3818              : 
    3819              :           /* If not explicitly a parent constructor, gather up the components
    3820              :              and build one.  */
    3821        11830 :           if (comp && comp == sym->components
    3822         7019 :               && sym->attr.extension
    3823          847 :               && comp_tail->val
    3824          847 :               && (!gfc_bt_struct (comp_tail->val->ts.type)
    3825           78 :                   || comp_tail->val->ts.u.derived != this_comp->ts.u.derived))
    3826              :             {
    3827          799 :               bool m;
    3828          799 :               gfc_actual_arglist *arg_null = NULL;
    3829              : 
    3830          799 :               actual->expr = comp_tail->val;
    3831          799 :               comp_tail->val = NULL;
    3832              : #define shorter gfc_convert_to_structure_constructor
    3833          799 :               m = shorter (NULL, comp->ts.u.derived, &comp_tail->val,
    3834          799 :                            comp->ts.u.derived->attr.zero_comp ? &arg_null :
    3835              :                                                                 &actual, true);
    3836              : #undef shorter
    3837              : 
    3838          799 :               if (!m)
    3839            0 :                 goto cleanup;
    3840              : 
    3841          799 :               if (comp->ts.u.derived->attr.zero_comp)
    3842              :                 {
    3843          133 :                   comp = comp->next;
    3844          133 :                   continue;
    3845              :                 }
    3846              :             }
    3847              : 
    3848          666 :       if (comp)
    3849        10004 :         comp = comp->next;
    3850        11697 :       if (parent && !comp)
    3851              :         break;
    3852              : 
    3853        11032 :       if (actual)
    3854        11031 :         actual = actual->next;
    3855              :     }
    3856              : 
    3857         8026 :   if (!build_actual_constructor (&comp_head, &ctor_head, sym))
    3858           12 :     goto cleanup;
    3859              : 
    3860              :   /* No component should be left, as this should have caused an error in the
    3861              :      loop constructing the component-list (name that does not correspond to any
    3862              :      component in the structure definition).  */
    3863         8014 :   if (comp_head && sym->attr.extension)
    3864              :     {
    3865            2 :       for (comp_iter = comp_head; comp_iter; comp_iter = comp_iter->next)
    3866              :         {
    3867            1 :           gfc_error ("component %qs at %L has already been set by a "
    3868              :                      "parent derived type constructor", comp_iter->name,
    3869              :                      &comp_iter->where);
    3870              :         }
    3871            1 :       goto cleanup;
    3872              :     }
    3873              :   else
    3874         8013 :     gcc_assert (!comp_head);
    3875              : 
    3876         8013 :   if (parent)
    3877              :     {
    3878          799 :       expr = gfc_get_structure_constructor_expr (BT_DERIVED, 0, &gfc_current_locus);
    3879          799 :       expr->ts.u.derived = sym;
    3880          799 :       expr->value.constructor = ctor_head;
    3881          799 :       *cexpr = expr;
    3882              :     }
    3883              :   else
    3884              :     {
    3885         7214 :       expr->ts.u.derived = sym;
    3886         7214 :       expr->ts.kind = 0;
    3887         7214 :       expr->ts.type = BT_DERIVED;
    3888         7214 :       expr->value.constructor = ctor_head;
    3889         7214 :       expr->expr_type = EXPR_STRUCTURE;
    3890              :     }
    3891              : 
    3892         8013 :   gfc_current_locus = old_locus;
    3893         8013 :   if (parent)
    3894          799 :     *arglist = actual;
    3895              :   return true;
    3896              : 
    3897           28 :   cleanup:
    3898           28 :   gfc_current_locus = old_locus;
    3899              : 
    3900           52 :   for (comp_iter = comp_head; comp_iter; )
    3901              :     {
    3902           24 :       gfc_structure_ctor_component *next = comp_iter->next;
    3903           24 :       gfc_free_structure_ctor_component (comp_iter);
    3904           24 :       comp_iter = next;
    3905              :     }
    3906           28 :   gfc_constructor_free (ctor_head);
    3907              : 
    3908           28 :   return false;
    3909              : }
    3910              : 
    3911              : 
    3912              : match
    3913           60 : gfc_match_structure_constructor (gfc_symbol *sym, gfc_symtree *symtree,
    3914              :                                  gfc_expr **result)
    3915              : {
    3916           60 :   match m;
    3917           60 :   gfc_expr *e;
    3918           60 :   bool t = true;
    3919              : 
    3920           60 :   e = gfc_get_expr ();
    3921           60 :   e->expr_type = EXPR_FUNCTION;
    3922           60 :   e->symtree = symtree;
    3923           60 :   e->where = gfc_current_locus;
    3924              : 
    3925           60 :   gcc_assert (gfc_fl_struct (sym->attr.flavor)
    3926              :               && symtree->n.sym->attr.flavor == FL_PROCEDURE);
    3927           60 :   e->value.function.esym = sym;
    3928           60 :   e->symtree->n.sym->attr.generic = 1;
    3929              : 
    3930           60 :   m = gfc_match_actual_arglist (0, &e->value.function.actual);
    3931           60 :   if (m != MATCH_YES)
    3932              :     {
    3933            0 :       gfc_free_expr (e);
    3934            0 :       return m;
    3935              :     }
    3936              : 
    3937           60 :   if (!gfc_convert_to_structure_constructor (e, sym, NULL, NULL, false))
    3938              :     {
    3939            1 :       gfc_free_expr (e);
    3940            1 :       return MATCH_ERROR;
    3941              :     }
    3942              : 
    3943              :   /* If a structure constructor is in a DATA statement, then each entity
    3944              :      in the structure constructor must be a constant.  Try to reduce the
    3945              :      expression here.  */
    3946           59 :   if (gfc_in_match_data ())
    3947           59 :     t = gfc_reduce_init_expr (e);
    3948              : 
    3949           59 :   if (t)
    3950              :     {
    3951           49 :       *result = e;
    3952           49 :       return MATCH_YES;
    3953              :     }
    3954              :   else
    3955              :     {
    3956           10 :       gfc_free_expr (e);
    3957           10 :       return MATCH_ERROR;
    3958              :     }
    3959              : }
    3960              : 
    3961              : 
    3962              : /* If the symbol is an implicit do loop index and implicitly typed,
    3963              :    it should not be host associated.  Provide a symtree from the
    3964              :    current namespace.  */
    3965              : static match
    3966      7147512 : check_for_implicit_index (gfc_symtree **st, gfc_symbol **sym)
    3967              : {
    3968      7147512 :   if ((*sym)->attr.flavor == FL_VARIABLE
    3969      2046310 :       && (*sym)->ns != gfc_current_ns
    3970        66878 :       && (*sym)->attr.implied_index
    3971          618 :       && (*sym)->attr.implicit_type
    3972           32 :       && !(*sym)->attr.use_assoc)
    3973              :     {
    3974           32 :       int i;
    3975           32 :       i = gfc_get_sym_tree ((*sym)->name, NULL, st, false);
    3976           32 :       if (i)
    3977              :         return MATCH_ERROR;
    3978           32 :       *sym = (*st)->n.sym;
    3979              :     }
    3980              :   return MATCH_YES;
    3981              : }
    3982              : 
    3983              : 
    3984              : /* Procedure pointer as function result: Replace the function symbol by the
    3985              :    auto-generated hidden result variable named "ppr@".  */
    3986              : 
    3987              : static bool
    3988      5314809 : replace_hidden_procptr_result (gfc_symbol **sym, gfc_symtree **st)
    3989              : {
    3990              :   /* Check for procedure pointer result variable.  */
    3991      5314809 :   if ((*sym)->attr.function && !(*sym)->attr.external
    3992      1437975 :       && (*sym)->result && (*sym)->result != *sym
    3993        12980 :       && (*sym)->result->attr.proc_pointer
    3994          343 :       && (*sym) == gfc_current_ns->proc_name
    3995          285 :       && (*sym) == (*sym)->result->ns->proc_name
    3996          285 :       && strcmp ("ppr@", (*sym)->result->name) == 0)
    3997              :     {
    3998              :       /* Automatic replacement with "hidden" result variable.  */
    3999          285 :       (*sym)->result->attr.referenced = (*sym)->attr.referenced;
    4000          285 :       *sym = (*sym)->result;
    4001          285 :       *st = gfc_find_symtree ((*sym)->ns->sym_root, (*sym)->name);
    4002          285 :       return true;
    4003              :     }
    4004              :   return false;
    4005              : }
    4006              : 
    4007              : 
    4008              : /* Matches a variable name followed by anything that might follow it--
    4009              :    array reference, argument list of a function, etc.  */
    4010              : 
    4011              : match
    4012      4385008 : gfc_match_rvalue (gfc_expr **result)
    4013              : {
    4014      4385008 :   gfc_actual_arglist *actual_arglist;
    4015      4385008 :   char name[GFC_MAX_SYMBOL_LEN + 1], argname[GFC_MAX_SYMBOL_LEN + 1];
    4016      4385008 :   gfc_state_data *st;
    4017      4385008 :   gfc_symbol *sym;
    4018      4385008 :   gfc_symtree *symtree;
    4019      4385008 :   locus where, old_loc;
    4020      4385008 :   gfc_expr *e;
    4021      4385008 :   match m, m2;
    4022      4385008 :   int i;
    4023      4385008 :   gfc_typespec *ts;
    4024      4385008 :   bool implicit_char;
    4025      4385008 :   gfc_ref *ref;
    4026      4385008 :   gfc_symtree *pdt_st;
    4027              : 
    4028      4385008 :   m = gfc_match ("%%loc");
    4029      4385008 :   if (m == MATCH_YES)
    4030              :     {
    4031        10878 :       if (!gfc_notify_std (GFC_STD_LEGACY, "%%LOC() as an rvalue at %C"))
    4032              :         return MATCH_ERROR;
    4033        10877 :       strncpy (name, "loc", 4);
    4034              :     }
    4035              : 
    4036              :   else
    4037              :     {
    4038      4374130 :       m = gfc_match_name (name);
    4039      4374130 :       if (m != MATCH_YES)
    4040              :         return m;
    4041              :     }
    4042              : 
    4043              :   /* Check if the symbol exists.  */
    4044      4177382 :   if (gfc_find_sym_tree (name, NULL, 1, &symtree))
    4045              :     return MATCH_ERROR;
    4046              : 
    4047              :   /* If the symbol doesn't exist, create it unless the name matches a FL_STRUCT
    4048              :      type. For derived types we create a generic symbol which links to the
    4049              :      derived type symbol; STRUCTUREs are simpler and must not conflict with
    4050              :      variables.  */
    4051      4177380 :   if (!symtree)
    4052       189404 :     if (gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree))
    4053              :       return MATCH_ERROR;
    4054      4177380 :   if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
    4055              :     {
    4056      4177380 :       if (gfc_find_state (COMP_INTERFACE)
    4057      4177380 :           && !gfc_current_ns->has_import_set)
    4058       100193 :         i = gfc_get_sym_tree (name, NULL, &symtree, false);
    4059              :       else
    4060      4077187 :         i = gfc_get_ha_sym_tree (name, &symtree);
    4061      4177380 :       if (i)
    4062              :         return MATCH_ERROR;
    4063              :     }
    4064              : 
    4065              : 
    4066      4177380 :   sym = symtree->n.sym;
    4067      4177380 :   e = NULL;
    4068      4177380 :   where = gfc_current_locus;
    4069              : 
    4070      4177380 :   replace_hidden_procptr_result (&sym, &symtree);
    4071              : 
    4072              :   /* If this is an implicit do loop index and implicitly typed,
    4073              :      it should not be host associated.  */
    4074      4177380 :   m = check_for_implicit_index (&symtree, &sym);
    4075      4177380 :   if (m != MATCH_YES)
    4076              :     return m;
    4077              : 
    4078      4177380 :   gfc_set_sym_referenced (sym);
    4079      4177380 :   sym->attr.implied_index = 0;
    4080              : 
    4081      4177380 :   if (sym->attr.function && sym->result == sym)
    4082              :     {
    4083              :       /* See if this is a directly recursive function call.  */
    4084       721457 :       gfc_gobble_whitespace ();
    4085       721457 :       if (sym->attr.recursive
    4086          100 :           && gfc_peek_ascii_char () == '('
    4087           93 :           && gfc_current_ns->proc_name == sym
    4088       721464 :           && !sym->attr.dimension)
    4089              :         {
    4090            4 :           gfc_error ("%qs at %C is the name of a recursive function "
    4091              :                      "and so refers to the result variable. Use an "
    4092              :                      "explicit RESULT variable for direct recursion "
    4093              :                      "(12.5.2.1)", sym->name);
    4094            4 :           return MATCH_ERROR;
    4095              :         }
    4096              : 
    4097       721453 :       if (gfc_is_function_return_value (sym, gfc_current_ns))
    4098         1731 :         goto variable;
    4099              : 
    4100       719722 :       if (sym->attr.entry
    4101          187 :           && (sym->ns == gfc_current_ns
    4102           27 :               || sym->ns == gfc_current_ns->parent))
    4103              :         {
    4104          180 :           gfc_entry_list *el = NULL;
    4105              : 
    4106          180 :           for (el = sym->ns->entries; el; el = el->next)
    4107          180 :             if (sym == el->sym)
    4108          180 :               goto variable;
    4109              :         }
    4110              :     }
    4111              : 
    4112      4175465 :   if (gfc_matching_procptr_assignment)
    4113              :     {
    4114              :       /* It can be a procedure or a derived-type procedure or a not-yet-known
    4115              :          type.  */
    4116         1389 :       if (sym->attr.flavor != FL_UNKNOWN
    4117         1025 :           && sym->attr.flavor != FL_PROCEDURE
    4118           65 :           && sym->attr.flavor != FL_PARAMETER
    4119           65 :           && sym->attr.flavor != FL_VARIABLE)
    4120              :         {
    4121            2 :           gfc_error ("Symbol at %C is not appropriate for an expression");
    4122            2 :           return MATCH_ERROR;
    4123              :         }
    4124         1387 :       goto procptr0;
    4125              :     }
    4126              : 
    4127      4174076 :   if (sym->attr.function || sym->attr.external || sym->attr.intrinsic)
    4128       736326 :     goto function0;
    4129              : 
    4130      3437750 :   if (sym->attr.generic)
    4131        69128 :     goto generic_function;
    4132              : 
    4133      3368622 :   switch (sym->attr.flavor)
    4134              :     {
    4135      1773117 :     case FL_VARIABLE:
    4136      1773117 :     variable:
    4137      1773117 :       e = gfc_get_expr ();
    4138              : 
    4139      1773117 :       e->expr_type = EXPR_VARIABLE;
    4140      1773117 :       e->symtree = symtree;
    4141              : 
    4142      1773117 :       m = gfc_match_varspec (e, 0, false, true);
    4143      1773117 :       break;
    4144              : 
    4145       231268 :     case FL_PARAMETER:
    4146              :       /* A statement of the form "REAL, parameter :: a(0:10) = 1" will
    4147              :          end up here.  Unfortunately, sym->value->expr_type is set to
    4148              :          EXPR_CONSTANT, and so the if () branch would be followed without
    4149              :          the !sym->as check.  */
    4150       231268 :       if (sym->value && sym->value->expr_type != EXPR_ARRAY
    4151       196165 :           && sym->value->expr_type != EXPR_VARIABLE && !sym->as)
    4152       196155 :         e = gfc_copy_expr (sym->value);
    4153              :       else
    4154              :         {
    4155        35113 :           e = gfc_get_expr ();
    4156        35113 :           e->expr_type = EXPR_VARIABLE;
    4157              :         }
    4158              : 
    4159       231268 :       e->symtree = symtree;
    4160       231268 :       m = gfc_match_varspec (e, 0, false, true);
    4161              : 
    4162       231268 :       if (sym->ts.is_c_interop || sym->ts.is_iso_c)
    4163              :         break;
    4164              : 
    4165              :       /* Variable array references to derived type parameters cause
    4166              :          all sorts of headaches in simplification. Treating such
    4167              :          expressions as variable works just fine for all array
    4168              :          references.  */
    4169       179257 :       if (sym->value && sym->ts.type == BT_DERIVED && e->ref)
    4170              :         {
    4171         2868 :           for (ref = e->ref; ref; ref = ref->next)
    4172         2677 :             if (ref->type == REF_ARRAY)
    4173              :               break;
    4174              : 
    4175         2631 :           if (ref == NULL || ref->u.ar.type == AR_FULL)
    4176              :             break;
    4177              : 
    4178         1018 :           ref = e->ref;
    4179         1018 :           e->ref = NULL;
    4180         1018 :           gfc_free_expr (e);
    4181         1018 :           e = gfc_get_expr ();
    4182         1018 :           e->expr_type = EXPR_VARIABLE;
    4183         1018 :           e->symtree = symtree;
    4184         1018 :           e->ref = ref;
    4185              :         }
    4186              : 
    4187              :       break;
    4188              : 
    4189            0 :     case FL_STRUCT:
    4190            0 :     case FL_DERIVED:
    4191            0 :       sym = gfc_use_derived (sym);
    4192            0 :       if (sym == NULL)
    4193              :         m = MATCH_ERROR;
    4194              :       else
    4195            0 :         goto generic_function;
    4196              :       break;
    4197              : 
    4198              :     /* If we're here, then the name is known to be the name of a
    4199              :        procedure, yet it is not sure to be the name of a function.  */
    4200      1061898 :     case FL_PROCEDURE:
    4201              : 
    4202              :     /* Procedure Pointer Assignments.  */
    4203      1061898 :     procptr0:
    4204      1061898 :       if (gfc_matching_procptr_assignment)
    4205              :         {
    4206         1387 :           gfc_gobble_whitespace ();
    4207         1387 :           if (!sym->attr.dimension && gfc_peek_ascii_char () == '(')
    4208              :             /* Parse functions returning a procptr.  */
    4209          220 :             goto function0;
    4210              : 
    4211         1167 :           e = gfc_get_expr ();
    4212         1167 :           e->expr_type = EXPR_VARIABLE;
    4213         1167 :           e->symtree = symtree;
    4214         1167 :           m = gfc_match_varspec (e, 0, false, true);
    4215         1098 :           if (!e->ref && sym->attr.flavor == FL_UNKNOWN
    4216          209 :               && sym->ts.type == BT_UNKNOWN
    4217         1366 :               && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
    4218              :             {
    4219              :               m = MATCH_ERROR;
    4220              :               break;
    4221              :             }
    4222              :           break;
    4223              :         }
    4224              : 
    4225      1060511 :       if (sym->attr.subroutine)
    4226              :         {
    4227           57 :           gfc_error ("Unexpected use of subroutine name %qs at %C",
    4228              :                      sym->name);
    4229           57 :           m = MATCH_ERROR;
    4230           57 :           break;
    4231              :         }
    4232              : 
    4233              :       /* At this point, the name has to be a non-statement function.
    4234              :          If the name is the same as the current function being
    4235              :          compiled, then we have a variable reference (to the function
    4236              :          result) if the name is non-recursive.  */
    4237              : 
    4238      1060454 :       st = gfc_enclosing_unit (NULL);
    4239              : 
    4240      1060454 :       if (st != NULL
    4241      1014149 :           && st->state == COMP_FUNCTION
    4242        88989 :           && st->sym == sym
    4243            0 :           && !sym->attr.recursive)
    4244              :         {
    4245            0 :           e = gfc_get_expr ();
    4246            0 :           e->symtree = symtree;
    4247            0 :           e->expr_type = EXPR_VARIABLE;
    4248              : 
    4249            0 :           m = gfc_match_varspec (e, 0, false, true);
    4250            0 :           break;
    4251              :         }
    4252              : 
    4253              :     /* Match a function reference.  */
    4254      1060454 :     function0:
    4255      1797000 :       m = gfc_match_actual_arglist (0, &actual_arglist);
    4256      1797000 :       if (m == MATCH_NO)
    4257              :         {
    4258       628620 :           if (sym->attr.proc == PROC_ST_FUNCTION)
    4259            1 :             gfc_error ("Statement function %qs requires argument list at %C",
    4260              :                        sym->name);
    4261              :           else
    4262       628619 :             gfc_error ("Function %qs requires an argument list at %C",
    4263              :                        sym->name);
    4264              : 
    4265              :           m = MATCH_ERROR;
    4266              :           break;
    4267              :         }
    4268              : 
    4269      1168380 :       if (m != MATCH_YES)
    4270              :         {
    4271              :           m = MATCH_ERROR;
    4272              :           break;
    4273              :         }
    4274              : 
    4275              :       /* Check to see if this is a PDT constructor.  The format of these
    4276              :          constructors is rather unusual:
    4277              :                 name [(type_params)](component_values)
    4278              :          where, component_values excludes the type_params. With the present
    4279              :          gfortran representation this is rather awkward because the two are not
    4280              :          distinguished, other than by their attributes.
    4281              : 
    4282              :          Even if 'name' is that of a PDT template, priority has to be given to
    4283              :          specific procedures, other than the constructor, in the generic
    4284              :          interface.  */
    4285              : 
    4286      1134210 :       gfc_gobble_whitespace ();
    4287      1134210 :       gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &pdt_st);
    4288        12661 :       if (sym->attr.generic && pdt_st != NULL
    4289      1144728 :           && !(sym->generic->next && gfc_peek_ascii_char() != '('))
    4290              :         {
    4291        10218 :           gfc_symbol *pdt_sym;
    4292        10218 :           gfc_actual_arglist *ctr_arglist = NULL, *tmp;
    4293        10218 :           gfc_component *c;
    4294              : 
    4295              :           /* Use the template.  */
    4296        10218 :           if (pdt_st->n.sym && pdt_st->n.sym->attr.pdt_template)
    4297              :             {
    4298         1758 :               bool type_spec_list = false;
    4299         1758 :               pdt_sym = pdt_st->n.sym;
    4300         1758 :               gfc_gobble_whitespace ();
    4301              :               /* Look for a second actual arglist. If present, try the first
    4302              :                  for the type parameters. Otherwise, or if there is no match,
    4303              :                  depend on default values by setting the type parameters to
    4304              :                  NULL.  */
    4305         1758 :               if (gfc_peek_ascii_char() == '(')
    4306          370 :                 type_spec_list = true;
    4307         1758 :               if (!actual_arglist && !type_spec_list)
    4308              :                 {
    4309            3 :                   gfc_error_now ("F2023 R755: The empty type specification at %C "
    4310              :                                  "is not allowed");
    4311            3 :                   m = MATCH_ERROR;
    4312            3 :                   break;
    4313              :                 }
    4314              :               /* Generate this instance using the type parameters from the
    4315              :                  first argument list and return the parameter list in
    4316              :                  ctr_arglist.  */
    4317         1755 :               m = gfc_get_pdt_instance (actual_arglist, &pdt_sym, &ctr_arglist);
    4318         1755 :               if (m != MATCH_YES || !ctr_arglist)
    4319              :                 {
    4320           43 :                   if (ctr_arglist)
    4321            0 :                     gfc_free_actual_arglist (ctr_arglist);
    4322              :                   /* See if all the type parameters have default values.  */
    4323           43 :                   m = gfc_get_pdt_instance (NULL, &pdt_sym, &ctr_arglist);
    4324           43 :                   if (m != MATCH_YES)
    4325              :                     {
    4326              :                       m = MATCH_NO;
    4327              :                       break;
    4328              :                     }
    4329              :                 }
    4330              : 
    4331              :               /* Now match the component_values if the type parameters were
    4332              :                  present.  */
    4333         1746 :               if (type_spec_list)
    4334              :                 {
    4335          370 :                   m = gfc_match_actual_arglist (0, &actual_arglist);
    4336          370 :                   if (m != MATCH_YES)
    4337              :                     {
    4338              :                       m = MATCH_ERROR;
    4339              :                       break;
    4340              :                     }
    4341              :                 }
    4342              : 
    4343              :               /* Make sure that the component names are in place so that this
    4344              :                  list can be safely appended to the type parameters.  */
    4345         1746 :               tmp = actual_arglist;
    4346         5883 :               for (c = pdt_sym->components; c && tmp; c = c->next)
    4347              :                 {
    4348         4137 :                   if (c->attr.pdt_kind || c->attr.pdt_len)
    4349         2182 :                     continue;
    4350         1955 :                   if (!tmp->name)
    4351         1588 :                     tmp->name = c->name;
    4352         1955 :                   tmp = tmp->next;
    4353              :                 }
    4354              : 
    4355         1746 :               gfc_find_sym_tree (gfc_dt_lower_string (pdt_sym->name),
    4356              :                                  NULL, 1, &symtree);
    4357         1746 :               if (!symtree)
    4358              :                 {
    4359          917 :                   gfc_get_ha_sym_tree (gfc_dt_lower_string (pdt_sym->name) ,
    4360              :                                        &symtree);
    4361          917 :                   symtree->n.sym = pdt_sym;
    4362          917 :                   symtree->n.sym->ts.u.derived = pdt_sym;
    4363          917 :                   symtree->n.sym->ts.type = BT_DERIVED;
    4364              :                 }
    4365              : 
    4366         1746 :               if (type_spec_list)
    4367              :                 {
    4368              :                   /* Append the type_params and the component_values.  */
    4369          492 :                   for (tmp = ctr_arglist; tmp && tmp->next;)
    4370              :                     tmp = tmp->next;
    4371          370 :                   tmp->next = actual_arglist;
    4372          370 :                   actual_arglist = ctr_arglist;
    4373          370 :                   tmp = actual_arglist;
    4374              :                   /* Can now add all the component names.  */
    4375         1272 :                   for (c = pdt_sym->components; c && tmp; c = c->next)
    4376              :                     {
    4377          902 :                       if (!tmp->name)
    4378            1 :                         tmp->name = c->name;
    4379          902 :                       tmp = tmp->next;
    4380              :                     }
    4381              :                 }
    4382              :             }
    4383              :         }
    4384              : 
    4385      1134198 :       gfc_get_ha_sym_tree (name, &symtree); /* Can't fail */
    4386      1134198 :       sym = symtree->n.sym;
    4387              : 
    4388      1134198 :       replace_hidden_procptr_result (&sym, &symtree);
    4389              : 
    4390      1134198 :       e = gfc_get_expr ();
    4391      1134198 :       e->symtree = symtree;
    4392      1134198 :       e->expr_type = EXPR_FUNCTION;
    4393      1134198 :       e->value.function.actual = actual_arglist;
    4394      1134198 :       e->where = gfc_current_locus;
    4395              : 
    4396      1134198 :       if (sym->ts.type == BT_CLASS && sym->attr.class_ok
    4397          224 :           && CLASS_DATA (sym)->as)
    4398              :         {
    4399           97 :           e->rank = CLASS_DATA (sym)->as->rank;
    4400           97 :           e->corank = CLASS_DATA (sym)->as->corank;
    4401              :         }
    4402      1134101 :       else if (sym->as != NULL)
    4403              :         {
    4404         1703 :           e->rank = sym->as->rank;
    4405         1703 :           e->corank = sym->as->corank;
    4406              :         }
    4407              : 
    4408      1134198 :       if (!sym->attr.function
    4409      1134198 :           && !gfc_add_function (&sym->attr, sym->name, NULL))
    4410              :         {
    4411              :           m = MATCH_ERROR;
    4412              :           break;
    4413              :         }
    4414              : 
    4415              :       /* Check here for the existence of at least one argument for the
    4416              :          iso_c_binding functions C_LOC, C_FUNLOC, and C_ASSOCIATED.  */
    4417      1134198 :       if (sym->attr.is_iso_c == 1
    4418            2 :           && (sym->from_intmod == INTMOD_ISO_C_BINDING
    4419            2 :               && (sym->intmod_sym_id == ISOCBINDING_LOC
    4420            2 :                   || sym->intmod_sym_id == ISOCBINDING_F_C_STRING
    4421            2 :                   || sym->intmod_sym_id == ISOCBINDING_FUNLOC
    4422            2 :                   || sym->intmod_sym_id == ISOCBINDING_ASSOCIATED)))
    4423              :         {
    4424              :           /* make sure we were given a param */
    4425            0 :           if (actual_arglist == NULL)
    4426              :             {
    4427            0 :               gfc_error ("Missing argument to %qs at %C", sym->name);
    4428            0 :               m = MATCH_ERROR;
    4429            0 :               break;
    4430              :             }
    4431              :         }
    4432              : 
    4433      1134198 :       if (sym->result == NULL)
    4434       406676 :         sym->result = sym;
    4435              : 
    4436      1134198 :       gfc_gobble_whitespace ();
    4437              :       /* F08:C612.  */
    4438      1134198 :       if (gfc_peek_ascii_char() == '%')
    4439              :         {
    4440           12 :           gfc_error ("The leftmost part-ref in a data-ref cannot be a "
    4441              :                      "function reference at %C");
    4442           12 :           m = MATCH_ERROR;
    4443           12 :           break;
    4444              :         }
    4445              : 
    4446              :       m = MATCH_YES;
    4447              :       break;
    4448              : 
    4449       304040 :     case FL_UNKNOWN:
    4450              : 
    4451              :       /* Special case for derived type variables that get their types
    4452              :          via an IMPLICIT statement.  This can't wait for the
    4453              :          resolution phase.  */
    4454              : 
    4455       304040 :       old_loc = gfc_current_locus;
    4456       304040 :       if (gfc_match_member_sep (sym) == MATCH_YES
    4457        11868 :           && sym->ts.type == BT_UNKNOWN
    4458       304048 :           && gfc_get_default_type (sym->name, sym->ns)->type == BT_DERIVED)
    4459            0 :         gfc_set_default_type (sym, 0, sym->ns);
    4460       304040 :       gfc_current_locus = old_loc;
    4461              : 
    4462              :       /* If the symbol has a (co)dimension attribute, the expression is a
    4463              :          variable.  */
    4464              : 
    4465       304040 :       if (sym->attr.dimension || sym->attr.codimension)
    4466              :         {
    4467        37244 :           if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
    4468              :             {
    4469              :               m = MATCH_ERROR;
    4470              :               break;
    4471              :             }
    4472              : 
    4473        37244 :           e = gfc_get_expr ();
    4474        37244 :           e->symtree = symtree;
    4475        37244 :           e->expr_type = EXPR_VARIABLE;
    4476        37244 :           m = gfc_match_varspec (e, 0, false, true);
    4477        37244 :           break;
    4478              :         }
    4479              : 
    4480       266796 :       if (sym->ts.type == BT_CLASS && sym->attr.class_ok
    4481         5511 :           && (CLASS_DATA (sym)->attr.dimension
    4482         3729 :               || CLASS_DATA (sym)->attr.codimension))
    4483              :         {
    4484         1882 :           if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
    4485              :             {
    4486              :               m = MATCH_ERROR;
    4487              :               break;
    4488              :             }
    4489              : 
    4490         1882 :           e = gfc_get_expr ();
    4491         1882 :           e->symtree = symtree;
    4492         1882 :           e->expr_type = EXPR_VARIABLE;
    4493         1882 :           m = gfc_match_varspec (e, 0, false, true);
    4494         1882 :           break;
    4495              :         }
    4496              : 
    4497              :       /* Name is not an array, so we peek to see if a '(' implies a
    4498              :          function call or a substring reference.  Otherwise the
    4499              :          variable is just a scalar.  */
    4500              : 
    4501       264914 :       gfc_gobble_whitespace ();
    4502       264914 :       if (gfc_peek_ascii_char () != '(')
    4503              :         {
    4504              :           /* Assume a scalar variable */
    4505        80563 :           e = gfc_get_expr ();
    4506        80563 :           e->symtree = symtree;
    4507        80563 :           e->expr_type = EXPR_VARIABLE;
    4508              : 
    4509        80563 :           if (!gfc_add_flavor (&sym->attr, FL_VARIABLE, sym->name, NULL))
    4510              :             {
    4511              :               m = MATCH_ERROR;
    4512              :               break;
    4513              :             }
    4514              : 
    4515              :           /*FIXME:??? gfc_match_varspec does set this for us: */
    4516        80563 :           e->ts = sym->ts;
    4517        80563 :           m = gfc_match_varspec (e, 0, false, true);
    4518        80563 :           break;
    4519              :         }
    4520              : 
    4521              :       /* See if this is a function reference with a keyword argument
    4522              :          as first argument. We do this because otherwise a spurious
    4523              :          symbol would end up in the symbol table.  */
    4524              : 
    4525       184351 :       old_loc = gfc_current_locus;
    4526       184351 :       m2 = gfc_match (" ( %n =", argname);
    4527       184351 :       gfc_current_locus = old_loc;
    4528              : 
    4529       184351 :       e = gfc_get_expr ();
    4530       184351 :       e->symtree = symtree;
    4531              : 
    4532       184351 :       if (m2 != MATCH_YES)
    4533              :         {
    4534              :           /* Try to figure out whether we're dealing with a character type.
    4535              :              We're peeking ahead here, because we don't want to call
    4536              :              match_substring if we're dealing with an implicitly typed
    4537              :              non-character variable.  */
    4538       183258 :           implicit_char = false;
    4539       183258 :           if (sym->ts.type == BT_UNKNOWN)
    4540              :             {
    4541       178455 :               ts = gfc_get_default_type (sym->name, NULL);
    4542       178455 :               if (ts->type == BT_CHARACTER)
    4543              :                 implicit_char = true;
    4544              :             }
    4545              : 
    4546              :           /* See if this could possibly be a substring reference of a name
    4547              :              that we're not sure is a variable yet.  */
    4548              : 
    4549       183241 :           if ((implicit_char || sym->ts.type == BT_CHARACTER)
    4550         1477 :               && match_substring (sym->ts.u.cl, 0, &e->ref, false) == MATCH_YES)
    4551              :             {
    4552              : 
    4553         1013 :               e->expr_type = EXPR_VARIABLE;
    4554              : 
    4555         1013 :               if (sym->attr.flavor != FL_VARIABLE
    4556         1013 :                   && !gfc_add_flavor (&sym->attr, FL_VARIABLE,
    4557              :                                       sym->name, NULL))
    4558              :                 {
    4559              :                   m = MATCH_ERROR;
    4560              :                   break;
    4561              :                 }
    4562              : 
    4563         1013 :               if (sym->ts.type == BT_UNKNOWN
    4564         1013 :                   && !gfc_set_default_type (sym, 1, NULL))
    4565              :                 {
    4566              :                   m = MATCH_ERROR;
    4567              :                   break;
    4568              :                 }
    4569              : 
    4570         1013 :               e->ts = sym->ts;
    4571         1013 :               if (e->ref)
    4572          988 :                 e->ts.u.cl = NULL;
    4573              :               m = MATCH_YES;
    4574              :               break;
    4575              :             }
    4576              :         }
    4577              : 
    4578              :       /* Give up, assume we have a function.  */
    4579              : 
    4580       183338 :       gfc_get_sym_tree (name, NULL, &symtree, false);       /* Can't fail */
    4581       183338 :       sym = symtree->n.sym;
    4582       183338 :       e->expr_type = EXPR_FUNCTION;
    4583              : 
    4584       183338 :       if (!sym->attr.function
    4585       183338 :           && !gfc_add_function (&sym->attr, sym->name, NULL))
    4586              :         {
    4587              :           m = MATCH_ERROR;
    4588              :           break;
    4589              :         }
    4590              : 
    4591       183338 :       sym->result = sym;
    4592              : 
    4593       183338 :       m = gfc_match_actual_arglist (0, &e->value.function.actual);
    4594       183338 :       if (m == MATCH_NO)
    4595            0 :         gfc_error ("Missing argument list in function %qs at %C", sym->name);
    4596              : 
    4597       183338 :       if (m != MATCH_YES)
    4598              :         {
    4599              :           m = MATCH_ERROR;
    4600              :           break;
    4601              :         }
    4602              : 
    4603              :       /* If our new function returns a character, array or structure
    4604              :          type, it might have subsequent references.  */
    4605              : 
    4606       183208 :       m = gfc_match_varspec (e, 0, false, true);
    4607       183208 :       if (m == MATCH_NO)
    4608              :         m = MATCH_YES;
    4609              : 
    4610              :       break;
    4611              : 
    4612        69128 :     generic_function:
    4613              :       /* Look for symbol first; if not found, look for STRUCTURE type symbol
    4614              :          specially. Creates a generic symbol for derived types.  */
    4615        69128 :       gfc_find_sym_tree (name, NULL, 1, &symtree);
    4616        69128 :       if (!symtree)
    4617            0 :         gfc_find_sym_tree (gfc_dt_upper_string (name), NULL, 1, &symtree);
    4618        69128 :       if (!symtree || symtree->n.sym->attr.flavor != FL_STRUCT)
    4619        69128 :         gfc_get_sym_tree (name, NULL, &symtree, false); /* Can't fail */
    4620              : 
    4621        69128 :       e = gfc_get_expr ();
    4622        69128 :       e->symtree = symtree;
    4623        69128 :       e->expr_type = EXPR_FUNCTION;
    4624              : 
    4625        69128 :       if (gfc_fl_struct (sym->attr.flavor))
    4626              :         {
    4627            0 :           e->value.function.esym = sym;
    4628            0 :           e->symtree->n.sym->attr.generic = 1;
    4629              :         }
    4630              : 
    4631        69128 :       m = gfc_match_actual_arglist (0, &e->value.function.actual);
    4632        69128 :       break;
    4633              : 
    4634              :     case FL_NAMELIST:
    4635              :       m = MATCH_ERROR;
    4636              :       break;
    4637              : 
    4638            5 :     default:
    4639            5 :       gfc_error ("Symbol at %C is not appropriate for an expression");
    4640            5 :       return MATCH_ERROR;
    4641              :     }
    4642              : 
    4643              :   /* Scan for possible inquiry references.  */
    4644       629689 :   if (m == MATCH_YES
    4645      3512588 :       && e->expr_type == EXPR_VARIABLE
    4646      4303631 :       && gfc_peek_ascii_char () == '%')
    4647              :       {
    4648           38 :         m = gfc_match_varspec (e, 0, false, false);
    4649           38 :         if (m == MATCH_NO)
    4650              :           m = MATCH_YES;
    4651              :       }
    4652              : 
    4653      4177368 :   if (m == MATCH_YES)
    4654              :     {
    4655      3512588 :       e->where = where;
    4656      3512588 :       *result = e;
    4657              :     }
    4658              :   else
    4659       664780 :     gfc_free_expr (e);
    4660              : 
    4661              :   return m;
    4662              : }
    4663              : 
    4664              : 
    4665              : /* Match a variable, i.e. something that can be assigned to.  This
    4666              :    starts as a symbol, can be a structure component or an array
    4667              :    reference.  It can be a function if the function doesn't have a
    4668              :    separate RESULT variable.  If the symbol has not been previously
    4669              :    seen, we assume it is a variable.
    4670              : 
    4671              :    This function is called by two interface functions:
    4672              :    gfc_match_variable, which has host_flag = 1, and
    4673              :    gfc_match_equiv_variable, with host_flag = 0, to restrict the
    4674              :    match of the symbol to the local scope.  */
    4675              : 
    4676              : static match
    4677      2970164 : match_variable (gfc_expr **result, int equiv_flag, int host_flag)
    4678              : {
    4679      2970164 :   gfc_symbol *sym, *dt_sym;
    4680      2970164 :   gfc_symtree *st;
    4681      2970164 :   gfc_expr *expr;
    4682      2970164 :   locus where, old_loc;
    4683      2970164 :   match m;
    4684              : 
    4685      2970164 :   *result = NULL;
    4686              : 
    4687              :   /* Since nothing has any business being an lvalue in a module
    4688              :      specification block, an interface block or a contains section,
    4689              :      we force the changed_symbols mechanism to work by setting
    4690              :      host_flag to 0. This prevents valid symbols that have the name
    4691              :      of keywords, such as 'end', being turned into variables by
    4692              :      failed matching to assignments for, e.g., END INTERFACE.  */
    4693      2970164 :   if (gfc_current_state () == COMP_MODULE
    4694      2970164 :       || gfc_current_state () == COMP_SUBMODULE
    4695              :       || gfc_current_state () == COMP_INTERFACE
    4696              :       || gfc_current_state () == COMP_CONTAINS)
    4697       209279 :     host_flag = 0;
    4698              : 
    4699      2970164 :   where = gfc_current_locus;
    4700      2970164 :   m = gfc_match_sym_tree (&st, host_flag);
    4701      2970163 :   if (m != MATCH_YES)
    4702              :     return m;
    4703              : 
    4704      2970132 :   sym = st->n.sym;
    4705              : 
    4706              :   /* If this is an implicit do loop index and implicitly typed,
    4707              :      it should not be host associated.  */
    4708      2970132 :   m = check_for_implicit_index (&st, &sym);
    4709      2970132 :   if (m != MATCH_YES)
    4710              :     return m;
    4711              : 
    4712      2970132 :   sym->attr.implied_index = 0;
    4713              : 
    4714      2970132 :   gfc_set_sym_referenced (sym);
    4715              : 
    4716              :   /* STRUCTUREs may share names with variables, but derived types may not.  */
    4717        15043 :   if (sym->attr.flavor == FL_PROCEDURE && sym->generic
    4718      2970200 :       && (dt_sym = gfc_find_dt_in_generic (sym)))
    4719              :     {
    4720            7 :       if (dt_sym->attr.flavor == FL_DERIVED)
    4721            7 :         gfc_error ("Derived type %qs cannot be used as a variable at %C",
    4722              :                    sym->name);
    4723              :       return MATCH_ERROR;
    4724              :     }
    4725              : 
    4726      2970125 :   switch (sym->attr.flavor)
    4727              :     {
    4728              :     case FL_VARIABLE:
    4729              :       /* Everything is alright.  */
    4730              :       break;
    4731              : 
    4732      2679867 :     case FL_UNKNOWN:
    4733      2679867 :       {
    4734      2679867 :         sym_flavor flavor = FL_UNKNOWN;
    4735              : 
    4736      2679867 :         gfc_gobble_whitespace ();
    4737              : 
    4738      2679867 :         if (sym->attr.external || sym->attr.procedure
    4739      2679835 :             || sym->attr.function || sym->attr.subroutine)
    4740              :           flavor = FL_PROCEDURE;
    4741              : 
    4742              :         /* If it is not a procedure, is not typed and is host associated,
    4743              :            we cannot give it a flavor yet.  */
    4744      2679835 :         else if (sym->ns == gfc_current_ns->parent
    4745         3157 :                    && sym->ts.type == BT_UNKNOWN)
    4746              :           break;
    4747              : 
    4748              :         /* These are definitive indicators that this is a variable.  */
    4749      3567321 :         else if (gfc_peek_ascii_char () != '(' || sym->ts.type != BT_UNKNOWN
    4750      3548989 :                  || sym->attr.pointer || sym->as != NULL)
    4751              :           flavor = FL_VARIABLE;
    4752              : 
    4753              :         if (flavor != FL_UNKNOWN
    4754      1811314 :             && !gfc_add_flavor (&sym->attr, flavor, sym->name, NULL))
    4755              :           return MATCH_ERROR;
    4756              :       }
    4757              :       break;
    4758              : 
    4759           17 :     case FL_PARAMETER:
    4760           17 :       if (equiv_flag)
    4761              :         {
    4762            0 :           gfc_error ("Named constant at %C in an EQUIVALENCE");
    4763            0 :           return MATCH_ERROR;
    4764              :         }
    4765           17 :       if (gfc_in_match_data())
    4766              :         {
    4767            4 :           gfc_error ("PARAMETER %qs shall not appear in a DATA statement at %C",
    4768              :                       sym->name);
    4769            4 :           return MATCH_ERROR;
    4770              :         }
    4771              :         /* Otherwise this is checked for an error given in the
    4772              :            variable definition context checks.  */
    4773              :       break;
    4774              : 
    4775        15036 :     case FL_PROCEDURE:
    4776              :       /* Check for a nonrecursive function result variable.  */
    4777        15036 :       if (sym->attr.function
    4778        12631 :           && (!sym->attr.external || sym->abr_modproc_decl)
    4779        12230 :           && sym->result == sym
    4780        26901 :           && (gfc_is_function_return_value (sym, gfc_current_ns)
    4781         2201 :               || (sym->attr.entry
    4782          499 :                   && sym->ns == gfc_current_ns)
    4783         1709 :               || (sym->attr.entry
    4784            7 :                   && sym->ns == gfc_current_ns->parent)))
    4785              :         {
    4786              :           /* If a function result is a derived type, then the derived
    4787              :              type may still have to be resolved.  */
    4788              : 
    4789        10163 :           if (sym->ts.type == BT_DERIVED
    4790        10163 :               && gfc_use_derived (sym->ts.u.derived) == NULL)
    4791              :             return MATCH_ERROR;
    4792              :           break;
    4793              :         }
    4794              : 
    4795         4873 :       if (sym->attr.proc_pointer
    4796         4873 :           || replace_hidden_procptr_result (&sym, &st))
    4797              :         break;
    4798              : 
    4799              :       /* Fall through to error */
    4800         3196 :       gcc_fallthrough ();
    4801              : 
    4802         3196 :     default:
    4803         3196 :       gfc_error ("%qs at %C is not a variable", sym->name);
    4804         3196 :       return MATCH_ERROR;
    4805              :     }
    4806              : 
    4807              :   /* Special case for derived type variables that get their types
    4808              :      via an IMPLICIT statement.  This can't wait for the
    4809              :      resolution phase.  */
    4810              : 
    4811      2966921 :     {
    4812      2966921 :       gfc_namespace * implicit_ns;
    4813              : 
    4814      2966921 :       if (gfc_current_ns->proc_name == sym)
    4815              :         implicit_ns = gfc_current_ns;
    4816              :       else
    4817      2957635 :         implicit_ns = sym->ns;
    4818              : 
    4819      2966921 :       old_loc = gfc_current_locus;
    4820      2966921 :       if (gfc_match_member_sep (sym) == MATCH_YES
    4821        22747 :           && sym->ts.type == BT_UNKNOWN
    4822      2966933 :           && gfc_get_default_type (sym->name, implicit_ns)->type == BT_DERIVED)
    4823            3 :         gfc_set_default_type (sym, 0, implicit_ns);
    4824      2966921 :       gfc_current_locus = old_loc;
    4825              :     }
    4826              : 
    4827      2966921 :   expr = gfc_get_expr ();
    4828              : 
    4829      2966921 :   expr->expr_type = EXPR_VARIABLE;
    4830      2966921 :   expr->symtree = st;
    4831      2966921 :   expr->ts = sym->ts;
    4832              : 
    4833              :   /* Now see if we have to do more.  */
    4834      2966921 :   m = gfc_match_varspec (expr, equiv_flag, false, false);
    4835      2966921 :   if (m != MATCH_YES)
    4836              :     {
    4837           83 :       gfc_free_expr (expr);
    4838           83 :       return m;
    4839              :     }
    4840              : 
    4841      2966838 :   expr->where = gfc_get_location_range (NULL, 0, &where, 1, &gfc_current_locus);
    4842      2966838 :   *result = expr;
    4843      2966838 :   return MATCH_YES;
    4844              : }
    4845              : 
    4846              : 
    4847              : match
    4848      2967217 : gfc_match_variable (gfc_expr **result, int equiv_flag)
    4849              : {
    4850      2967217 :   return match_variable (result, equiv_flag, 1);
    4851              : }
    4852              : 
    4853              : 
    4854              : match
    4855         2947 : gfc_match_equiv_variable (gfc_expr **result)
    4856              : {
    4857         2947 :   return match_variable (result, 1, 0);
    4858              : }
        

Generated by: LCOV version 2.6-beta

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