LCOV - code coverage report
Current view: top level - gcc/fortran - io.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 89.2 % 2477 2210
Test Date: 2026-12-05 16:10:02 Functions: 100.0 % 64 64
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Deal with I/O statements & related stuff.
       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 "match.h"
      27              : #include "parse.h"
      28              : #include "constructor.h"
      29              : 
      30              : gfc_st_label
      31              : format_asterisk = {0, NULL, NULL, -1, ST_LABEL_FORMAT, ST_LABEL_FORMAT, NULL,
      32              :                    0, {NULL, {NULL}}, NULL, 0, false};
      33              : 
      34              : typedef struct
      35              : {
      36              :   const char *name, *spec, *value;
      37              :   bt type;
      38              : }
      39              : io_tag;
      40              : 
      41              : static const io_tag
      42              :         tag_readonly    = {"READONLY", " readonly", NULL, BT_UNKNOWN },
      43              :         tag_shared      = {"SHARE", " shared", NULL, BT_UNKNOWN },
      44              :         tag_noshared    = {"SHARE", " noshared", NULL, BT_UNKNOWN },
      45              :         tag_e_share     = {"SHARE", " share =", " %e", BT_CHARACTER },
      46              :         tag_v_share     = {"SHARE", " share =", " %v", BT_CHARACTER },
      47              :         tag_cc          = {"CARRIAGECONTROL", " carriagecontrol =", " %e",
      48              :                            BT_CHARACTER },
      49              :         tag_v_cc        = {"CARRIAGECONTROL", " carriagecontrol =", " %v",
      50              :                            BT_CHARACTER },
      51              :         tag_file        = {"FILE", " file =", " %e", BT_CHARACTER },
      52              :         tag_status      = {"STATUS", " status =", " %e", BT_CHARACTER},
      53              :         tag_e_access    = {"ACCESS", " access =", " %e", BT_CHARACTER},
      54              :         tag_e_form      = {"FORM", " form =", " %e", BT_CHARACTER},
      55              :         tag_e_recl      = {"RECL", " recl =", " %e", BT_INTEGER},
      56              :         tag_e_blank     = {"BLANK", " blank =", " %e", BT_CHARACTER},
      57              :         tag_e_position  = {"POSITION", " position =", " %e", BT_CHARACTER},
      58              :         tag_e_action    = {"ACTION", " action =", " %e", BT_CHARACTER},
      59              :         tag_e_delim     = {"DELIM", " delim =", " %e", BT_CHARACTER},
      60              :         tag_e_pad       = {"PAD", " pad =", " %e", BT_CHARACTER},
      61              :         tag_e_decimal   = {"DECIMAL", " decimal =", " %e", BT_CHARACTER},
      62              :         tag_e_encoding  = {"ENCODING", " encoding =", " %e", BT_CHARACTER},
      63              :         tag_e_async     = {"ASYNCHRONOUS", " asynchronous =", " %e", BT_CHARACTER},
      64              :         tag_e_round     = {"ROUND", " round =", " %e", BT_CHARACTER},
      65              :         tag_e_sign      = {"SIGN", " sign =", " %e", BT_CHARACTER},
      66              :         tag_unit        = {"UNIT", " unit =", " %e", BT_INTEGER},
      67              :         tag_advance     = {"ADVANCE", " advance =", " %e", BT_CHARACTER},
      68              :         tag_rec         = {"REC", " rec =", " %e", BT_INTEGER},
      69              :         tag_spos        = {"POSITION", " pos =", " %e", BT_INTEGER},
      70              :         tag_format      = {"FORMAT", NULL, NULL, BT_CHARACTER},
      71              :         tag_iomsg       = {"IOMSG", " iomsg =", " %e", BT_CHARACTER},
      72              :         tag_iostat      = {"IOSTAT", " iostat =", " %v", BT_INTEGER},
      73              :         tag_size        = {"SIZE", " size =", " %v", BT_INTEGER},
      74              :         tag_exist       = {"EXIST", " exist =", " %v", BT_LOGICAL},
      75              :         tag_opened      = {"OPENED", " opened =", " %v", BT_LOGICAL},
      76              :         tag_named       = {"NAMED", " named =", " %v", BT_LOGICAL},
      77              :         tag_name        = {"NAME", " name =", " %v", BT_CHARACTER},
      78              :         tag_number      = {"NUMBER", " number =", " %v", BT_INTEGER},
      79              :         tag_s_access    = {"ACCESS", " access =", " %v", BT_CHARACTER},
      80              :         tag_sequential  = {"SEQUENTIAL", " sequential =", " %v", BT_CHARACTER},
      81              :         tag_direct      = {"DIRECT", " direct =", " %v", BT_CHARACTER},
      82              :         tag_s_form      = {"FORM", " form =", " %v", BT_CHARACTER},
      83              :         tag_formatted   = {"FORMATTED", " formatted =", " %v", BT_CHARACTER},
      84              :         tag_unformatted = {"UNFORMATTED", " unformatted =", " %v", BT_CHARACTER},
      85              :         tag_s_recl      = {"RECL", " recl =", " %v", BT_INTEGER},
      86              :         tag_nextrec     = {"NEXTREC", " nextrec =", " %v", BT_INTEGER},
      87              :         tag_s_blank     = {"BLANK", " blank =", " %v", BT_CHARACTER},
      88              :         tag_s_position  = {"POSITION", " position =", " %v", BT_CHARACTER},
      89              :         tag_s_action    = {"ACTION", " action =", " %v", BT_CHARACTER},
      90              :         tag_read        = {"READ", " read =", " %v", BT_CHARACTER},
      91              :         tag_write       = {"WRITE", " write =", " %v", BT_CHARACTER},
      92              :         tag_readwrite   = {"READWRITE", " readwrite =", " %v", BT_CHARACTER},
      93              :         tag_s_delim     = {"DELIM", " delim =", " %v", BT_CHARACTER},
      94              :         tag_s_pad       = {"PAD", " pad =", " %v", BT_CHARACTER},
      95              :         tag_s_decimal   = {"DECIMAL", " decimal =", " %v", BT_CHARACTER},
      96              :         tag_s_encoding  = {"ENCODING", " encoding =", " %v", BT_CHARACTER},
      97              :         tag_s_async     = {"ASYNCHRONOUS", " asynchronous =", " %v", BT_CHARACTER},
      98              :         tag_s_round     = {"ROUND", " round =", " %v", BT_CHARACTER},
      99              :         tag_s_sign      = {"SIGN", " sign =", " %v", BT_CHARACTER},
     100              :         tag_iolength    = {"IOLENGTH", " iolength =", " %v", BT_INTEGER},
     101              :         tag_convert     = {"CONVERT", " convert =", " %e", BT_CHARACTER},
     102              :         tag_strm_out    = {"POS", " pos =", " %v", BT_INTEGER},
     103              :         tag_err         = {"ERR", " err =", " %l", BT_UNKNOWN},
     104              :         tag_end         = {"END", " end =", " %l", BT_UNKNOWN},
     105              :         tag_eor         = {"EOR", " eor =", " %l", BT_UNKNOWN},
     106              :         tag_id          = {"ID", " id =", " %e", BT_INTEGER},
     107              :         tag_pending     = {"PENDING", " pending =", " %v", BT_LOGICAL},
     108              :         tag_newunit     = {"NEWUNIT", " newunit =", " %v", BT_INTEGER},
     109              :         tag_s_iqstream  = {"STREAM", " stream =", " %v", BT_CHARACTER};
     110              : 
     111              : static gfc_dt *current_dt;
     112              : 
     113              : #define RESOLVE_TAG(x, y) if (!resolve_tag (x, y)) return false;
     114              : 
     115              : /**************** Fortran 95 FORMAT parser  *****************/
     116              : 
     117              : /* FORMAT tokens returned by format_lex().  */
     118              : enum format_token
     119              : {
     120              :   FMT_NONE, FMT_UNKNOWN, FMT_SIGNED_INT, FMT_ZERO, FMT_POSINT, FMT_PERIOD,
     121              :   FMT_COMMA, FMT_COLON, FMT_SLASH, FMT_DOLLAR, FMT_LPAREN, FMT_RPAREN, FMT_X,
     122              :   FMT_SIGN, FMT_BLANK, FMT_CHAR, FMT_P, FMT_IBOZ, FMT_F, FMT_E, FMT_EN, FMT_ES,
     123              :   FMT_G, FMT_L, FMT_A, FMT_D, FMT_H, FMT_END, FMT_ERROR, FMT_DC, FMT_DP, FMT_T,
     124              :   FMT_TR, FMT_TL, FMT_STAR, FMT_RC, FMT_RD, FMT_RN, FMT_RP, FMT_RU, FMT_RZ,
     125              :   FMT_DT, FMT_EX, FMT_LPS, FMT_LPZ, FMT_LZ
     126              : };
     127              : 
     128              : /* Local variables for checking format strings.  The saved_token is
     129              :    used to back up by a single format token during the parsing
     130              :    process.  */
     131              : static gfc_char_t *format_string;
     132              : static int format_string_pos;
     133              : static int format_length, use_last_char;
     134              : static char error_element;
     135              : static locus format_locus;
     136              : 
     137              : static format_token saved_token;
     138              : 
     139              : static enum
     140              : { MODE_STRING, MODE_FORMAT, MODE_COPY }
     141              : mode;
     142              : 
     143              : /* Set by check_format_string during an FMT_CHECK_WIDTH pass.  */
     144              : static bool checking_width_only = false;
     145              : 
     146              : 
     147              : /* Return the next character in the format string.  */
     148              : 
     149              : static char
     150       170689 : next_char (gfc_instring in_string)
     151              : {
     152       170689 :   static gfc_char_t c;
     153              : 
     154       170689 :   if (use_last_char)
     155              :     {
     156        29493 :       use_last_char = 0;
     157        29493 :       return c;
     158              :     }
     159              : 
     160       141196 :   format_length++;
     161              : 
     162       141196 :   if (mode == MODE_STRING)
     163        83334 :     c = *format_string++;
     164              :   else
     165              :     {
     166        57862 :       c = gfc_next_char_literal (in_string);
     167        57862 :       if (c == '\n')
     168            0 :         c = '\0';
     169              :     }
     170              : 
     171       141196 :   if (flag_backslash && c == '\\')
     172              :     {
     173           48 :       locus old_locus = gfc_current_locus;
     174              : 
     175           48 :       if (gfc_match_special_char (&c) == MATCH_NO)
     176            0 :         gfc_current_locus = old_locus;
     177              : 
     178           48 :       if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
     179            0 :         gfc_warning (0, "Extension: backslash character at %C");
     180              :     }
     181              : 
     182       141196 :   if (mode == MODE_COPY)
     183        28871 :     *format_string++ = c;
     184              : 
     185       141196 :   if (mode != MODE_STRING)
     186        57862 :     format_locus = gfc_current_locus;
     187              : 
     188       141196 :   format_string_pos++;
     189              : 
     190       141196 :   c = gfc_wide_toupper (c);
     191       141196 :   return c;
     192              : }
     193              : 
     194              : 
     195              : /* Back up one character position.  Only works once.  */
     196              : 
     197              : static void
     198        29499 : unget_char (void)
     199              : {
     200        29499 :   use_last_char = 1;
     201            0 : }
     202              : 
     203              : /* Eat up the spaces and return a character.  */
     204              : 
     205              : static char
     206       130906 : next_char_not_space ()
     207              : {
     208       139461 :   char c;
     209       139461 :   do
     210              :     {
     211       139461 :       error_element = c = next_char (NONSTRING);
     212       139461 :       if (c == '\t' && !checking_width_only)
     213            4 :         gfc_warning (OPT_Wtabs, "Nonconforming tab character in format at %C");
     214              :     }
     215       139461 :   while (gfc_is_whitespace (c));
     216       130906 :   return c;
     217              : }
     218              : 
     219              : static int value = 0;
     220              : 
     221              : /* Simple lexical analyzer for getting the next token in a FORMAT
     222              :    statement.  */
     223              : 
     224              : static format_token
     225       125426 : format_lex (void)
     226              : {
     227       125426 :   format_token token;
     228       125426 :   char c, delim;
     229       125426 :   int zflag;
     230       125426 :   int negative_flag;
     231              : 
     232       125426 :   if (saved_token != FMT_NONE)
     233              :     {
     234        28653 :       token = saved_token;
     235        28653 :       saved_token = FMT_NONE;
     236        28653 :       return token;
     237              :     }
     238              : 
     239        96773 :   c = next_char_not_space ();
     240              : 
     241        96773 :   negative_flag = 0;
     242        96773 :   switch (c)
     243              :     {
     244            6 :     case '-':
     245            6 :       negative_flag = 1;
     246              :       /* Falls through.  */
     247              : 
     248           18 :     case '+':
     249           18 :       c = next_char_not_space ();
     250           18 :       if (!ISDIGIT (c))
     251              :         {
     252              :           token = FMT_UNKNOWN;
     253              :           break;
     254              :         }
     255              : 
     256           18 :       value = c - '0';
     257              : 
     258           18 :       do
     259              :         {
     260           18 :           c = next_char_not_space ();
     261           18 :           if (ISDIGIT (c))
     262            0 :             value = 10 * value + (c - '0');
     263              :         }
     264           18 :       while (ISDIGIT (c));
     265              : 
     266           18 :       unget_char ();
     267              : 
     268           18 :       if (negative_flag)
     269            6 :         value = -value;
     270              : 
     271              :       token = FMT_SIGNED_INT;
     272              :       break;
     273              : 
     274        24231 :     case '0':
     275        24231 :     case '1':
     276        24231 :     case '2':
     277        24231 :     case '3':
     278        24231 :     case '4':
     279        24231 :     case '5':
     280        24231 :     case '6':
     281        24231 :     case '7':
     282        24231 :     case '8':
     283        24231 :     case '9':
     284        24231 :       zflag = (c == '0');
     285              : 
     286        24231 :       value = c - '0';
     287              : 
     288        29185 :       do
     289              :         {
     290        29185 :           c = next_char_not_space ();
     291        29185 :           if (ISDIGIT (c))
     292              :             {
     293         4954 :               value = 10 * value + (c - '0');
     294         4954 :               if (c != '0')
     295        29185 :                 zflag = 0;
     296              :             }
     297              :         }
     298        29185 :       while (ISDIGIT (c));
     299              : 
     300        24231 :       unget_char ();
     301        24231 :       token = zflag ? FMT_ZERO : FMT_POSINT;
     302              :       break;
     303              : 
     304              :     case '.':
     305              :       token = FMT_PERIOD;
     306              :       break;
     307              : 
     308        11341 :     case ',':
     309        11341 :       token = FMT_COMMA;
     310        11341 :       break;
     311              : 
     312          101 :     case ':':
     313          101 :       token = FMT_COLON;
     314          101 :       break;
     315              : 
     316              :     case '/':
     317          843 :       token = FMT_SLASH;
     318              :       break;
     319              : 
     320           72 :     case '$':
     321           72 :       token = FMT_DOLLAR;
     322           72 :       break;
     323              : 
     324         1088 :     case 'T':
     325         1088 :       c = next_char_not_space ();
     326         1088 :       switch (c)
     327              :         {
     328              :         case 'L':
     329              :           token = FMT_TL;
     330              :           break;
     331          204 :         case 'R':
     332          204 :           token = FMT_TR;
     333          204 :           break;
     334          647 :         default:
     335          647 :           token = FMT_T;
     336          647 :           unget_char ();
     337              :         }
     338              :       break;
     339              : 
     340        14810 :     case '(':
     341        14810 :       token = FMT_LPAREN;
     342        14810 :       break;
     343              : 
     344        14780 :     case ')':
     345        14780 :       token = FMT_RPAREN;
     346        14780 :       break;
     347              : 
     348         1696 :     case 'X':
     349         1696 :       token = FMT_X;
     350         1696 :       break;
     351              : 
     352          156 :     case 'S':
     353          156 :       c = next_char_not_space ();
     354          156 :       if (c != 'P' && c != 'S')
     355           18 :         unget_char ();
     356              : 
     357              :       token = FMT_SIGN;
     358              :       break;
     359              : 
     360          336 :     case 'B':
     361          336 :       c = next_char_not_space ();
     362          336 :       if (c == 'N' || c == 'Z')
     363              :         token = FMT_BLANK;
     364              :       else
     365              :         {
     366          114 :           unget_char ();
     367          114 :           token = FMT_IBOZ;
     368              :         }
     369              : 
     370              :       break;
     371              : 
     372         2438 :     case '\'':
     373         2438 :     case '"':
     374         2438 :       delim = c;
     375              : 
     376         2438 :       value = 0;
     377              : 
     378        51332 :       for (;;)
     379              :         {
     380        26885 :           c = next_char (INSTRING_WARN);
     381        26885 :           if (c == '\0')
     382              :             {
     383              :               token = FMT_END;
     384              :               break;
     385              :             }
     386              : 
     387        26885 :           if (c == delim)
     388              :             {
     389         2612 :               c = next_char (NONSTRING);
     390              : 
     391         2612 :               if (c == '\0')
     392              :                 {
     393              :                   token = FMT_END;
     394              :                   break;
     395              :                 }
     396              : 
     397         2612 :               if (c != delim)
     398              :                 {
     399         2438 :                   unget_char ();
     400         2438 :                   token = FMT_CHAR;
     401         2438 :                   break;
     402              :                 }
     403              :             }
     404        24447 :           value++;
     405              :         }
     406              :       break;
     407              : 
     408          522 :     case 'P':
     409          522 :       token = FMT_P;
     410          522 :       break;
     411              : 
     412              :     case 'I':
     413              :     case 'O':
     414              :     case 'Z':
     415         5024 :       token = FMT_IBOZ;
     416              :       break;
     417              : 
     418         1934 :     case 'F':
     419         1934 :       token = FMT_F;
     420         1934 :       break;
     421              : 
     422         2289 :     case 'E':
     423         2289 :       c = next_char_not_space ();
     424         2289 :       if (c == 'N' )
     425              :         token = FMT_EN;
     426              :       else if (c == 'S')
     427              :         token = FMT_ES;
     428              :       else if (c == 'X')
     429              :         token = FMT_EX;
     430              :       else
     431              :         {
     432         1335 :           token = FMT_E;
     433         1335 :           unget_char ();
     434              :         }
     435              : 
     436              :       break;
     437              : 
     438         1039 :     case 'G':
     439         1039 :       token = FMT_G;
     440         1039 :       break;
     441              : 
     442          260 :     case 'H':
     443          260 :       token = FMT_H;
     444          260 :       break;
     445              : 
     446          324 :     case 'L':
     447          324 :       c = next_char_not_space ();
     448          324 :       switch (c)
     449              :         {
     450            0 :         case 'P':
     451            0 :           c = next_char_not_space ();
     452            0 :           switch (c)
     453              :           {
     454              :             case 'S':
     455              :               token = FMT_LPS;
     456              :               break;
     457              : 
     458              :             case 'Z':
     459              :               token = FMT_LPZ;
     460              :               break;
     461              : 
     462            0 :             default:
     463            0 :               token = FMT_UNKNOWN;
     464            0 :               unget_char ();
     465            0 :               break;
     466              :           }
     467              :           break;
     468              : 
     469              :         case 'Z':
     470              :           token = FMT_LZ;
     471              :           break;
     472              : 
     473          324 :         default:
     474          324 :           token = FMT_UNKNOWN;
     475          324 :           unget_char ();
     476          324 :           break;
     477              :         }
     478              :       token = FMT_L;
     479              :       break;
     480              : 
     481         8132 :     case 'A':
     482         8132 :       token = FMT_A;
     483         8132 :       break;
     484              : 
     485          407 :     case 'D':
     486          407 :       c = next_char_not_space ();
     487          407 :       if (c == 'P')
     488              :         {
     489           19 :           if (!gfc_notify_std (GFC_STD_F2003, "DP format "
     490              :                                "specifier not allowed at %C"))
     491            0 :             return FMT_ERROR;
     492              :           token = FMT_DP;
     493              :         }
     494          388 :       else if (c == 'C')
     495              :         {
     496           13 :           if (!gfc_notify_std (GFC_STD_F2003, "DC format "
     497              :                                "specifier not allowed at %C"))
     498            0 :             return FMT_ERROR;
     499              :           token = FMT_DC;
     500              :         }
     501          375 :       else if (c == 'T')
     502              :         {
     503          240 :           if (!gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DT format "
     504              :               "specifier not allowed at %C"))
     505              :             return FMT_ERROR;
     506          240 :           token = FMT_DT;
     507          240 :           c = next_char_not_space ();
     508          240 :           if (c == '\'' || c == '"')
     509              :             {
     510           59 :               delim = c;
     511           59 :               value = 0;
     512              : 
     513          378 :               for (;;)
     514              :                 {
     515          378 :                   c = next_char (INSTRING_WARN);
     516          378 :                   if (c == '\0')
     517              :                     {
     518              :                       token = FMT_END;
     519              :                       break;
     520              :                     }
     521              : 
     522          378 :                   if (c == delim)
     523              :                     {
     524           60 :                       c = next_char (NONSTRING);
     525           60 :                       if (c == '\0')
     526              :                         {
     527              :                           token = FMT_END;
     528              :                           break;
     529              :                         }
     530           60 :                       if (c == '/')
     531              :                         {
     532              :                           token = FMT_SLASH;
     533              :                           break;
     534              :                         }
     535           59 :                       if (c == delim)
     536            1 :                         continue;
     537           58 :                       unget_char ();
     538           58 :                       break;
     539              :                     }
     540              :                 }
     541              :             }
     542          181 :           else if (c == '/')
     543              :             {
     544              :               token = FMT_SLASH;
     545              :               break;
     546              :             }
     547              :           else
     548          181 :             unget_char ();
     549              :         }
     550              :       else
     551              :         {
     552          135 :           token = FMT_D;
     553          135 :           unget_char ();
     554              :         }
     555              :       break;
     556              : 
     557           72 :     case 'R':
     558           72 :       c = next_char_not_space ();
     559           72 :       switch (c)
     560              :         {
     561              :         case 'C':
     562              :           token = FMT_RC;
     563              :           break;
     564            0 :         case 'D':
     565            0 :           token = FMT_RD;
     566            0 :           break;
     567           48 :         case 'N':
     568           48 :           token = FMT_RN;
     569           48 :           break;
     570            0 :         case 'P':
     571            0 :           token = FMT_RP;
     572            0 :           break;
     573           24 :         case 'U':
     574           24 :           token = FMT_RU;
     575           24 :           break;
     576            0 :         case 'Z':
     577            0 :           token = FMT_RZ;
     578            0 :           break;
     579            0 :         default:
     580            0 :           token = FMT_UNKNOWN;
     581            0 :           unget_char ();
     582            0 :           break;
     583              :         }
     584              :       break;
     585              : 
     586              :     case '\0':
     587            2 :       token = FMT_END;
     588              :       break;
     589              : 
     590          140 :     case '*':
     591          140 :       token = FMT_STAR;
     592          140 :       break;
     593              : 
     594              :     default:
     595            3 :       token = FMT_UNKNOWN;
     596              :       break;
     597              :     }
     598              : 
     599              :   return token;
     600              : }
     601              : 
     602              : 
     603              : static const char *
     604          103 : token_to_string (format_token t)
     605              : {
     606          103 :   switch (t)
     607              :     {
     608              :       case FMT_D:
     609              :         return "D";
     610           74 :       case FMT_G:
     611           74 :         return "G";
     612           10 :       case FMT_E:
     613           10 :         return "E";
     614            2 :       case FMT_EN:
     615            2 :         return "EN";
     616            9 :       case FMT_ES:
     617            9 :         return "ES";
     618            0 :       default:
     619            0 :         return "";
     620              :     }
     621              : }
     622              : 
     623              : /* Minimum F-style field width for D fractional digits.  */
     624              : 
     625              : static int
     626         1225 : f_min_width (int d)
     627              : {
     628         1064 :   return d == 0 ? 2 : d + 1;
     629              : }
     630              : 
     631              : /* Warn at WHERE if the field width W of the D, E, EN, ES or G edit
     632              :    descriptor T with D digits and exponent width E is too small for every
     633              :    output value.  E is 2 if not given.  */
     634              : 
     635              : static void
     636          953 : check_real_width (format_token t, int w, int d, int e, locus *where)
     637              : {
     638              :   /* Letter, sign and digits; Ee with E zero writes at least one digit.  */
     639          953 :   int exp = (e == 0 ? 1 : e) + 2;
     640          953 :   int min_w;
     641          953 :   switch (t)
     642              :     {
     643          662 :     case FMT_D:
     644          662 :     case FMT_E:
     645          662 :       min_w = d + 1 + exp;
     646          662 :       break;
     647           64 :     case FMT_EN:
     648           64 :     case FMT_ES:
     649              :       /* A nonzero digit precedes the decimal symbol.  */
     650           64 :       min_w = d + 2 + exp;
     651           64 :       break;
     652          185 :     case FMT_G:
     653              :       /* G with D == 0 edits like E.  Otherwise a value in range edits
     654              :          as F(w-n).(d-1) followed by n blanks, n being 4 for E zero or
     655              :          not given and E + 2 otherwise, and one out of range like E.  */
     656          185 :       min_w = d + 1 + exp;
     657          185 :       if (d != 0)
     658          320 :         min_w = MIN (min_w, (e == 0 ? 4 : e + 2) + f_min_width (d - 1));
     659              :       break;
     660              :     default:
     661              :       return;
     662              :     }
     663          911 :   if (w < min_w)
     664           84 :     gfc_warning (OPT_Wformat_width, "Field width of the %s edit "
     665              :                  "descriptor at %L too small for the number of digits",
     666              :                  token_to_string (t), where);
     667              : }
     668              : 
     669              : /* Which diagnostics check_format issues.  */
     670              : 
     671              : enum format_check_kind
     672              : {
     673              :   FMT_CHECK_NORMAL,     /* All of them.  */
     674              :   FMT_CHECK_DECL,       /* No width warnings; I/O direction unknown.  */
     675              :   FMT_CHECK_WIDTH       /* Only the width warnings, after FMT_CHECK_DECL.  */
     676              : };
     677              : 
     678              : /* Check a format statement.  The format string, either from a FORMAT
     679              :    statement or a constant in an I/O statement has already been parsed
     680              :    by itself, and we are checking it for validity.  The dual origin
     681              :    means that the warning message is a little less than great.  */
     682              : 
     683              : static bool
     684        13688 : check_format (bool is_input, format_check_kind kind = FMT_CHECK_NORMAL)
     685              : {
     686        13688 :   const char *posint_required
     687              :     = G_("Positive width required in format string at %L");
     688        13688 :   const char *nonneg_required
     689              :     = G_("Nonnegative width required in format string at %L");
     690        13688 :   const char *unexpected_element
     691              :     = G_("Unexpected element %qc in format string at %L");
     692        13688 :   const char *unexpected_end
     693              :     = G_("Unexpected end of format string in format string at %L");
     694        13688 :   const char *zero_width
     695              :     = G_("Zero width in format descriptor in format string at %L");
     696              : 
     697        13688 :   const char *error = NULL;
     698        13688 :   format_token t, u;
     699        13688 :   int level;
     700        13688 :   int repeat;
     701        13688 :   int value_w, value_d, value_e;
     702        13688 :   bool rv;
     703              : 
     704        13688 :   use_last_char = 0;
     705        13688 :   saved_token = FMT_NONE;
     706        13688 :   level = 0;
     707        13688 :   repeat = 0;
     708        13688 :   rv = true;
     709        13688 :   format_string_pos = 0;
     710              : 
     711        13688 :   t = format_lex ();
     712        13688 :   if (t == FMT_ERROR)
     713            0 :     goto fail;
     714        13688 :   if (t != FMT_LPAREN)
     715              :     {
     716            6 :       error = G_("Missing leading left parenthesis in format string at %L");
     717            6 :       goto syntax;
     718              :     }
     719              : 
     720        13682 :   t = format_lex ();
     721        13682 :   if (t == FMT_ERROR)
     722            0 :     goto fail;
     723        13682 :   if (t == FMT_RPAREN)
     724           52 :     goto finished;              /* Empty format is legal */
     725        13630 :   saved_token = t;
     726              : 
     727        13270 : format_item:
     728              :   /* In this state, the next thing has to be a format item.  */
     729        26900 :   t = format_lex ();
     730        26900 :   if (t == FMT_ERROR)
     731            0 :     goto fail;
     732        26941 : format_item_1:
     733        26941 :   switch (t)
     734              :     {
     735          140 :     case FMT_STAR:
     736          140 :       repeat = -1;
     737          140 :       t = format_lex ();
     738          140 :       if (t == FMT_ERROR)
     739            0 :         goto fail;
     740          140 :       if (t == FMT_LPAREN)
     741              :         {
     742          140 :           level++;
     743          140 :           goto format_item;
     744              :         }
     745            0 :       error = G_("Left parenthesis required after %<*%> in format string "
     746              :                  "at %L");
     747            0 :       goto syntax;
     748              : 
     749         6182 :     case FMT_POSINT:
     750         6182 :       repeat = value;
     751         6182 :       t = format_lex ();
     752         6182 :       if (t == FMT_ERROR)
     753            0 :         goto fail;
     754         6182 :       if (t == FMT_LPAREN)
     755              :         {
     756          800 :           level++;
     757          800 :           goto format_item;
     758              :         }
     759              : 
     760         5382 :       if (t == FMT_SLASH)
     761            6 :         goto optional_comma;
     762              : 
     763         5376 :       goto data_desc;
     764              : 
     765          164 :     case FMT_LPAREN:
     766          164 :       level++;
     767          164 :       goto format_item;
     768              : 
     769           30 :     case FMT_SIGNED_INT:
     770           30 :     case FMT_ZERO:
     771              :       /* Signed integer can only precede a P format.  */
     772           30 :       t = format_lex ();
     773           30 :       if (t == FMT_ERROR)
     774            0 :         goto fail;
     775           30 :       if (t != FMT_P)
     776              :         {
     777            1 :           error = G_("Expected P edit descriptor in format string at %L");
     778            1 :           goto syntax;
     779              :         }
     780              : 
     781           29 :       goto data_desc;
     782              : 
     783            0 :     case FMT_P:
     784              :       /* P requires a prior number.  */
     785            0 :       error = G_("P descriptor requires leading scale factor in format "
     786              :                  "string at %L");
     787            0 :       goto syntax;
     788              : 
     789            2 :     case FMT_X:
     790              :       /* X requires a prior number if we're being pedantic.  */
     791            2 :       if (mode != MODE_FORMAT)
     792            1 :         format_locus.nextc += format_string_pos;
     793            2 :       if (!gfc_notify_std (GFC_STD_GNU, "X descriptor requires leading "
     794              :                            "space count at %L", &format_locus))
     795              :         return false;
     796            2 :       goto between_desc;
     797              : 
     798          482 :     case FMT_SIGN:
     799          482 :     case FMT_BLANK:
     800          482 :     case FMT_DP:
     801          482 :     case FMT_DC:
     802          482 :     case FMT_RC:
     803          482 :     case FMT_RD:
     804          482 :     case FMT_RN:
     805          482 :     case FMT_RP:
     806          482 :     case FMT_RU:
     807          482 :     case FMT_RZ:
     808          482 :       goto between_desc;
     809              : 
     810         2438 :     case FMT_CHAR:
     811         2438 :       goto extension_optional_comma;
     812              : 
     813          645 :     case FMT_COLON:
     814          645 :     case FMT_SLASH:
     815          645 :       goto optional_comma;
     816              : 
     817           72 :     case FMT_DOLLAR:
     818           72 :       t = format_lex ();
     819           72 :       if (t == FMT_ERROR)
     820            0 :         goto fail;
     821              : 
     822           72 :       if (!gfc_notify_std (GFC_STD_GNU, "$ descriptor at %L", &format_locus))
     823              :         return false;
     824           72 :       if (t != FMT_RPAREN || level > 0)
     825              :         {
     826           18 :           if (kind != FMT_CHECK_WIDTH)
     827           18 :             gfc_warning (0, "$ should be the last specifier in format at %L",
     828              :                          &format_locus);
     829           18 :           goto optional_comma_1;
     830              :         }
     831              : 
     832           54 :       goto finished;
     833              : 
     834        16758 :     case FMT_T:
     835        16758 :     case FMT_TL:
     836        16758 :     case FMT_TR:
     837        16758 :     case FMT_IBOZ:
     838        16758 :     case FMT_F:
     839        16758 :     case FMT_E:
     840        16758 :     case FMT_EN:
     841        16758 :     case FMT_ES:
     842        16758 :     case FMT_EX:
     843        16758 :     case FMT_G:
     844        16758 :     case FMT_L:
     845        16758 :     case FMT_A:
     846        16758 :     case FMT_D:
     847        16758 :     case FMT_H:
     848        16758 :     case FMT_DT:
     849        16758 :       goto data_desc;
     850              : 
     851            0 :     case FMT_END:
     852            0 :       error = unexpected_end;
     853            0 :       goto syntax;
     854              : 
     855           25 :     case FMT_RPAREN:
     856           25 :       if (flag_dec_blank_format_item)
     857           24 :         goto finished;
     858              :       else
     859              :         {
     860            1 :           error = G_("Missing item in format string at %L");
     861            1 :           goto syntax;
     862              :         }
     863              : 
     864            3 :     default:
     865            3 :       error = unexpected_element;
     866            3 :       goto syntax;
     867              :     }
     868              : 
     869        22163 : data_desc:
     870              :   /* In this state, t must currently be a data descriptor.
     871              :      Deal with things that can/must follow the descriptor.  */
     872        22163 :   switch (t)
     873              :     {
     874              :     case FMT_SIGN:
     875              :     case FMT_BLANK:
     876              :     case FMT_DP:
     877              :     case FMT_DC:
     878              :     case FMT_X:
     879              :       break;
     880              : 
     881          522 :     case FMT_P:
     882              :       /* No comma after P allowed only for F, E, EN, ES, D, or G.
     883              :          10.1.1 (1).  */
     884          522 :       t = format_lex ();
     885          522 :       if (t == FMT_ERROR)
     886            0 :         goto fail;
     887          522 :       if (!(gfc_option.allow_std & GFC_STD_F2003) && t != FMT_COMMA
     888            6 :           && t != FMT_F && t != FMT_E && t != FMT_EN && t != FMT_ES
     889            4 :           && t != FMT_D && t != FMT_G && t != FMT_RPAREN && t != FMT_SLASH)
     890              :         {
     891            2 :           error = G_("Comma required after P descriptor in format string "
     892              :                      "at %L");
     893            2 :           goto syntax;
     894              :         }
     895          517 :       if (t != FMT_COMMA)
     896              :         {
     897          479 :           if (t == FMT_POSINT)
     898              :             {
     899            6 :               t = format_lex ();
     900            6 :               if (t == FMT_ERROR)
     901            0 :                 goto fail;
     902              :             }
     903          479 :           if (t != FMT_F && t != FMT_E && t != FMT_EN && t != FMT_ES
     904          479 :               && t != FMT_D && t != FMT_G && t != FMT_RPAREN && t != FMT_SLASH)
     905              :             {
     906            0 :               error = G_("Comma required after P descriptor in format string "
     907              :                          "at %L");
     908            0 :               goto syntax;
     909              :             }
     910              :         }
     911              : 
     912          520 :       saved_token = t;
     913          520 :       goto optional_comma;
     914              : 
     915         1087 :     case FMT_T:
     916         1087 :     case FMT_TL:
     917         1087 :     case FMT_TR:
     918         1087 :       t = format_lex ();
     919         1087 :       if (t != FMT_POSINT)
     920              :         {
     921            1 :           error = G_("Positive width required with T descriptor in format "
     922              :                      "string at %L");
     923            1 :           goto syntax;
     924              :         }
     925              :       break;
     926              : 
     927          324 :     case FMT_L:
     928          324 :       t = format_lex ();
     929          324 :       if (t == FMT_ERROR)
     930            0 :         goto fail;
     931          324 :       if (t == FMT_POSINT)
     932              :         break;
     933           96 :       if (mode != MODE_FORMAT)
     934           96 :         format_locus.nextc += format_string_pos;
     935           96 :       if (t == FMT_ZERO)
     936              :         {
     937            0 :           switch (gfc_notification_std (GFC_STD_GNU))
     938              :             {
     939            0 :               case WARNING:
     940            0 :                 if (kind != FMT_CHECK_WIDTH)
     941            0 :                   gfc_warning (0, "Extension: Zero width after L "
     942              :                                "descriptor at %L", &format_locus);
     943              :                 break;
     944            0 :               case ERROR:
     945            0 :                 gfc_error ("Extension: Zero width after L "
     946              :                              "descriptor at %L", &format_locus);
     947            0 :                 goto fail;
     948              :               case SILENT:
     949              :                 break;
     950            0 :               default:
     951            0 :                 gcc_unreachable ();
     952              :             }
     953              :         }
     954              :       else
     955              :         {
     956           96 :           saved_token = t;
     957           96 :           gfc_notify_std (GFC_STD_GNU, "Missing positive width after "
     958              :                           "L descriptor at %L", &format_locus);
     959              :         }
     960              :       break;
     961              : 
     962         8126 :     case FMT_A:
     963         8126 :       t = format_lex ();
     964         8126 :       if (t == FMT_ERROR)
     965            0 :         goto fail;
     966         8126 :       if (t == FMT_ZERO)
     967              :         {
     968            0 :           error = zero_width;
     969            0 :           goto syntax;
     970              :         }
     971         8126 :       if (t != FMT_POSINT)
     972         6535 :         saved_token = t;
     973              :       break;
     974              : 
     975         2954 :     case FMT_D:
     976         2954 :     case FMT_E:
     977         2954 :     case FMT_EX:
     978         2954 :     case FMT_G:
     979         2954 :     case FMT_EN:
     980         2954 :     case FMT_ES:
     981         2954 :       value_w = value_d = 0;
     982         2954 :       value_e = 2;
     983         2954 :       u = format_lex ();
     984         2954 :       if (t == FMT_G && u == FMT_ZERO)
     985              :         {
     986          403 :           if (is_input && kind != FMT_CHECK_WIDTH)
     987              :             {
     988            0 :               error = zero_width;
     989            0 :               goto syntax;
     990              :             }
     991          403 :           if (!gfc_notify_std (GFC_STD_F2008, "%<G0%> in format at %L",
     992              :                                &format_locus))
     993              :             return false;
     994          402 :           u = format_lex ();
     995          402 :           if (u != FMT_PERIOD)
     996              :             {
     997          360 :               saved_token = u;
     998          360 :               break;
     999              :             }
    1000           42 :           u = format_lex ();
    1001           42 :           if (u != FMT_POSINT)
    1002              :             {
    1003            0 :               error = posint_required;
    1004            0 :               goto syntax;
    1005              :             }
    1006           42 :           u = format_lex ();
    1007           42 :           if (u == FMT_E)
    1008              :             {
    1009            0 :               error = G_("E specifier not allowed with g0 descriptor in "
    1010              :                          "format string at %L");
    1011            0 :               goto syntax;
    1012              :             }
    1013           42 :           saved_token = u;
    1014           42 :           break;
    1015              :         }
    1016              : 
    1017         2551 :       if (u != FMT_POSINT)
    1018              :         {
    1019          792 :           if (flag_dec)
    1020              :             {
    1021           35 :               if (flag_dec_format_defaults)
    1022              :                 {
    1023              :                   /* Assume a default width based on the variable size.  */
    1024           30 :                   saved_token = u;
    1025           30 :                   break;
    1026              :                 }
    1027              :               else
    1028              :                 {
    1029            5 :                   gfc_error ("Positive width required in format "
    1030              :                              "specifier %s at %L", token_to_string (t),
    1031              :                              &format_locus);
    1032            5 :                   saved_token = u;
    1033            5 :                   goto fail;
    1034              :                 }
    1035              :             }
    1036              : 
    1037          757 :           format_locus.nextc += format_string_pos;
    1038          757 :           if (!gfc_notify_std (GFC_STD_F2018,
    1039              :                                "positive width required at %L",
    1040              :                                &format_locus))
    1041              :             {
    1042            1 :               saved_token = u;
    1043            1 :               goto fail;
    1044              :             }
    1045          756 :           if (flag_dec_format_defaults)
    1046              :             {
    1047              :               /* Assume a default width based on the variable size.  */
    1048           30 :               saved_token = u;
    1049           30 :               break;
    1050              :             }
    1051              :         }
    1052              : 
    1053         2485 :       value_w = value;
    1054              : 
    1055         2485 :       u = format_lex ();
    1056         2485 :       if (u == FMT_ERROR)
    1057            0 :         goto fail;
    1058         2485 :       if (u != FMT_PERIOD)
    1059              :         {
    1060              :           /* Warn if -std=legacy, otherwise error.  */
    1061           14 :           format_locus.nextc += format_string_pos;
    1062           14 :           if (gfc_option.warn_std != 0)
    1063              :             {
    1064            2 :               gfc_error ("Period required in format "
    1065              :                              "specifier %s at %L", token_to_string (t),
    1066              :                              &format_locus);
    1067            2 :               saved_token = u;
    1068            2 :               goto fail;
    1069              :             }
    1070           12 :           else if (kind != FMT_CHECK_WIDTH)
    1071           12 :             gfc_warning (0, "Period required in format "
    1072              :                          "specifier %s at %L", token_to_string (t),
    1073              :                           &format_locus);
    1074              :           /* If we go to finished, we need to unwind this
    1075              :              before the next round.  */
    1076           12 :           format_locus.nextc -= format_string_pos;
    1077           12 :           saved_token = u;
    1078           12 :           break;
    1079              :         }
    1080              : 
    1081         2471 :       u = format_lex ();
    1082         2471 :       if (u == FMT_ERROR)
    1083            0 :         goto fail;
    1084         2471 :       if (u != FMT_ZERO && u != FMT_POSINT)
    1085              :         {
    1086            0 :           error = nonneg_required;
    1087            0 :           goto syntax;
    1088              :         }
    1089              : 
    1090         2471 :       value_d = value;
    1091              : 
    1092         2471 :       if (t == FMT_D)
    1093              :         {
    1094          128 :           if (kind != FMT_CHECK_DECL && !is_input && value_w != 0)
    1095           66 :             check_real_width (t, value_w, value_d, value_e, &format_locus);
    1096              :           break;
    1097              :         }
    1098              : 
    1099              :       /* Look for optional exponent.  */
    1100         2343 :       u = format_lex ();
    1101         2343 :       if (u == FMT_ERROR)
    1102            0 :         goto fail;
    1103         2343 :       if (u != FMT_E)
    1104         1834 :         saved_token = u;
    1105              :       else
    1106              :         {
    1107          509 :           u = format_lex ();
    1108          509 :           if (u == FMT_ERROR)
    1109            0 :             goto fail;
    1110          509 :           if (u != FMT_POSINT)
    1111              :             {
    1112          117 :               if (u == FMT_ZERO)
    1113              :                 {
    1114          117 :                   if (!gfc_notify_std (GFC_STD_F2018,
    1115              :                                       "Positive exponent width required in "
    1116              :                                       "format string at %L", &format_locus))
    1117              :                     {
    1118            0 :                       saved_token = u;
    1119            0 :                       goto fail;
    1120              :                     }
    1121              :                   value_e = 0;
    1122              :                 }
    1123              :               else
    1124              :                 {
    1125            0 :                   error = G_("Positive exponent width required in format "
    1126              :                              "string at %L");
    1127            0 :                   goto syntax;
    1128              :                 }
    1129              :             }
    1130              :           else
    1131          392 :             value_e = value;
    1132              :         }
    1133              : 
    1134         2343 :       if (kind != FMT_CHECK_DECL && !is_input && value_w != 0)
    1135          887 :         check_real_width (t, value_w, value_d, value_e, &format_locus);
    1136              :       break;
    1137              : 
    1138          239 :     case FMT_DT:
    1139          239 :       t = format_lex ();
    1140          239 :       if (t == FMT_ERROR)
    1141            0 :         goto fail;
    1142          239 :       switch (t)
    1143              :         {
    1144          173 :         case FMT_RPAREN:
    1145          173 :           level--;
    1146          173 :           if (level < 0)
    1147          167 :             goto finished;
    1148            6 :           goto between_desc;
    1149              : 
    1150           36 :         case FMT_COMMA:
    1151           36 :           goto format_item;
    1152              : 
    1153            6 :         case FMT_COLON:
    1154            6 :           goto format_item_1;
    1155              : 
    1156           60 :         case FMT_LPAREN:
    1157              : 
    1158           60 :   dtio_vlist:
    1159           60 :           t = format_lex ();
    1160           60 :           if (t == FMT_ERROR)
    1161            0 :             goto fail;
    1162              : 
    1163           60 :           if (t != FMT_POSINT)
    1164              :             {
    1165            0 :               error = posint_required;
    1166            0 :               goto syntax;
    1167              :             }
    1168              : 
    1169           60 :           t = format_lex ();
    1170           60 :           if (t == FMT_ERROR)
    1171            0 :             goto fail;
    1172              : 
    1173           60 :           if (t == FMT_COMMA)
    1174           36 :             goto dtio_vlist;
    1175           24 :           if (t != FMT_RPAREN)
    1176              :             {
    1177            0 :               error = G_("Right parenthesis expected at %C in format string "
    1178              :                          "at %L");
    1179            0 :               goto syntax;
    1180              :             }
    1181           24 :           goto between_desc;
    1182              : 
    1183            0 :         default:
    1184            0 :           error = unexpected_element;
    1185            0 :           goto syntax;
    1186              :         }
    1187         1933 :       break;
    1188              : 
    1189         1933 :     case FMT_F:
    1190         1933 :       t = format_lex ();
    1191         1933 :       if (t == FMT_ERROR)
    1192            0 :         goto fail;
    1193         1933 :       if (t != FMT_ZERO && t != FMT_POSINT)
    1194              :         {
    1195           52 :           if (flag_dec_format_defaults)
    1196              :             {
    1197              :               /* Assume the default width is expected here and continue lexing.  */
    1198           48 :               value = 0; /* It doesn't matter what we set the value to here.  */
    1199           48 :               saved_token = t;
    1200           48 :               break;
    1201              :             }
    1202            4 :           error = nonneg_required;
    1203            4 :           goto syntax;
    1204              :         }
    1205         1881 :       else if (is_input && kind != FMT_CHECK_WIDTH && t == FMT_ZERO)
    1206              :         {
    1207            1 :           error = posint_required;
    1208            1 :           goto syntax;
    1209              :         }
    1210              : 
    1211         1880 :       value_w = value;
    1212              : 
    1213         1880 :       t = format_lex ();
    1214         1880 :       if (t == FMT_ERROR)
    1215            0 :         goto fail;
    1216         1880 :       if (t != FMT_PERIOD)
    1217              :         {
    1218              :           /* Warn if -std=legacy, otherwise error.  */
    1219            7 :           if (gfc_option.warn_std != 0)
    1220              :             {
    1221            1 :               error = G_("Period required in format specifier in format "
    1222              :                          "string at %L");
    1223            1 :               goto syntax;
    1224              :             }
    1225            6 :           if (mode != MODE_FORMAT)
    1226            6 :             format_locus.nextc += format_string_pos;
    1227            6 :           if (kind != FMT_CHECK_WIDTH)
    1228            6 :             gfc_warning (0, "Period required in format specifier at %L",
    1229              :                          &format_locus);
    1230            6 :           saved_token = t;
    1231            6 :           break;
    1232              :         }
    1233              : 
    1234         1873 :       t = format_lex ();
    1235         1873 :       if (t == FMT_ERROR)
    1236            0 :         goto fail;
    1237         1873 :       if (t != FMT_ZERO && t != FMT_POSINT)
    1238              :         {
    1239            0 :           error = nonneg_required;
    1240            0 :           goto syntax;
    1241              :         }
    1242              : 
    1243         1873 :       value_d = value;
    1244              : 
    1245         1196 :       if (kind != FMT_CHECK_DECL && !is_input && value_w != 0
    1246         2937 :           && value_w < f_min_width (value_d))
    1247           39 :         gfc_warning (OPT_Wformat_width, "Field width of the F edit "
    1248              :                      "descriptor at %L too small for the number of digits",
    1249              :                      &format_locus);
    1250              :       break;
    1251              : 
    1252          260 :     case FMT_H:
    1253          260 :       if (!(gfc_option.allow_std & GFC_STD_LEGACY))
    1254              :         {
    1255            0 :           error = G_("The H format specifier at %L is a Fortran 95 deleted"
    1256              :                      " feature");
    1257            0 :           goto syntax;
    1258              :         }
    1259          260 :       if (mode != MODE_FORMAT)
    1260          160 :         format_locus.nextc += format_string_pos;
    1261          260 :       if (kind != FMT_CHECK_WIDTH)
    1262          260 :         gfc_warning (0, "The H format specifier at %L is"
    1263              :                      " a Fortran 95 deleted feature", &format_locus);
    1264          260 :       if (mode == MODE_STRING)
    1265              :         {
    1266           61 :           format_string += value;
    1267           61 :           format_length -= value;
    1268           61 :           format_string_pos += repeat;
    1269              :         }
    1270              :       else
    1271              :         {
    1272         1492 :           while (repeat > 0)
    1273              :            {
    1274         1293 :              next_char (INSTRING_WARN);
    1275         1293 :              repeat -- ;
    1276              :            }
    1277              :         }
    1278              :      break;
    1279              : 
    1280         5024 :     case FMT_IBOZ:
    1281         5024 :       t = format_lex ();
    1282         5024 :       if (t == FMT_ERROR)
    1283            0 :         goto fail;
    1284         5024 :       if (t != FMT_ZERO && t != FMT_POSINT)
    1285              :         {
    1286           67 :           if (flag_dec_format_defaults)
    1287              :             {
    1288              :               /* Assume the default width is expected here and continue lexing.  */
    1289           60 :               value = 0; /* It doesn't matter what we set the value to here.  */
    1290           60 :               saved_token = t;
    1291              :             }
    1292              :           else
    1293              :             {
    1294            7 :               error = nonneg_required;
    1295            7 :               goto syntax;
    1296              :             }
    1297              :         }
    1298         4957 :       else if (is_input && kind != FMT_CHECK_WIDTH && t == FMT_ZERO)
    1299              :         {
    1300            2 :           error = posint_required;
    1301            2 :           goto syntax;
    1302              :         }
    1303              : 
    1304         5015 :       t = format_lex ();
    1305         5015 :       if (t == FMT_ERROR)
    1306            0 :         goto fail;
    1307         5015 :       if (t != FMT_PERIOD)
    1308         4571 :         saved_token = t;
    1309              :       else
    1310              :         {
    1311          444 :           t = format_lex ();
    1312          444 :           if (t == FMT_ERROR)
    1313            0 :             goto fail;
    1314          444 :           if (t != FMT_ZERO && t != FMT_POSINT)
    1315              :             {
    1316            0 :               error = nonneg_required;
    1317            0 :               goto syntax;
    1318              :             }
    1319              :         }
    1320              : 
    1321              :       break;
    1322              : 
    1323            0 :     default:
    1324            0 :       error = unexpected_element;
    1325            0 :       goto syntax;
    1326              :     }
    1327              : 
    1328        22175 : between_desc:
    1329              :   /* Between a descriptor and what comes next.  */
    1330        22989 :   t = format_lex ();
    1331        22989 :   if (t == FMT_ERROR)
    1332            0 :     goto fail;
    1333        22989 :   switch (t)
    1334              :     {
    1335              : 
    1336         9625 :     case FMT_COMMA:
    1337         9625 :       goto format_item;
    1338              : 
    1339        13077 :     case FMT_RPAREN:
    1340        13077 :       level--;
    1341        13077 :       if (level < 0)
    1342        12263 :         goto finished;
    1343          814 :       goto between_desc;
    1344              : 
    1345          250 :     case FMT_COLON:
    1346          250 :     case FMT_SLASH:
    1347          250 :       goto optional_comma;
    1348              : 
    1349            2 :     case FMT_END:
    1350            2 :       error = unexpected_end;
    1351            2 :       goto syntax;
    1352              : 
    1353           35 :     default:
    1354           35 :       if (mode != MODE_FORMAT)
    1355           26 :         format_locus.nextc += format_string_pos - 1;
    1356           35 :       if (!gfc_notify_std (GFC_STD_LEGACY,
    1357              :           "Missing comma in FORMAT string at %L", &format_locus))
    1358              :         return false;
    1359              :       /* If we do not actually return a failure, we need to unwind this
    1360              :          before the next round.  */
    1361           35 :       if (mode != MODE_FORMAT)
    1362           26 :         format_locus.nextc -= format_string_pos;
    1363           35 :       goto format_item_1;
    1364              :     }
    1365              : 
    1366         1464 : optional_comma:
    1367              :   /* Optional comma is a weird between state where we've just finished
    1368              :      reading a colon, slash, dollar or P descriptor.  */
    1369         1464 :   t = format_lex ();
    1370         1464 :   if (t == FMT_ERROR)
    1371            0 :     goto fail;
    1372         1464 : optional_comma_1:
    1373         1482 :   switch (t)
    1374              :     {
    1375              :     case FMT_COMMA:
    1376              :       break;
    1377              : 
    1378          244 :     case FMT_RPAREN:
    1379          244 :       level--;
    1380          244 :       if (level < 0)
    1381          110 :         goto finished;
    1382          134 :       goto between_desc;
    1383              : 
    1384          855 :     default:
    1385              :       /* Assume that we have another format item.  */
    1386          855 :       saved_token = t;
    1387          855 :       break;
    1388              :     }
    1389              : 
    1390         1238 :   goto format_item;
    1391              : 
    1392         2438 : extension_optional_comma:
    1393              :   /* As a GNU extension, permit a missing comma after a string literal.  */
    1394         2438 :   t = format_lex ();
    1395         2438 :   if (t == FMT_ERROR)
    1396            0 :     goto fail;
    1397         2438 :   switch (t)
    1398              :     {
    1399              :     case FMT_COMMA:
    1400              :       break;
    1401              : 
    1402         1128 :     case FMT_RPAREN:
    1403         1128 :       level--;
    1404         1128 :       if (level < 0)
    1405          978 :         goto finished;
    1406          150 :       goto between_desc;
    1407              : 
    1408           43 :     case FMT_COLON:
    1409           43 :     case FMT_SLASH:
    1410           43 :       goto optional_comma;
    1411              : 
    1412            0 :     case FMT_END:
    1413            0 :       error = unexpected_end;
    1414            0 :       goto syntax;
    1415              : 
    1416           24 :     default:
    1417           24 :       if (mode != MODE_FORMAT)
    1418           12 :         format_locus.nextc += format_string_pos;
    1419           24 :       if (!gfc_notify_std (GFC_STD_LEGACY,
    1420              :           "Missing comma in FORMAT string at %L", &format_locus))
    1421              :         return false;
    1422              :       /* If we do not actually return a failure, we need to unwind this
    1423              :          before the next round.  */
    1424           24 :       if (mode != MODE_FORMAT)
    1425           12 :         format_locus.nextc -= format_string_pos;
    1426           24 :       saved_token = t;
    1427           24 :       break;
    1428              :     }
    1429              : 
    1430         1267 :   goto format_item;
    1431              : 
    1432           31 : syntax:
    1433           31 :   if (mode != MODE_FORMAT)
    1434           26 :     format_locus.nextc += format_string_pos;
    1435           31 :   if (error == unexpected_element)
    1436            3 :     gfc_error (error, error_element, &format_locus);
    1437              :   else
    1438           28 :     gfc_error (error, &format_locus);
    1439        13688 : fail:
    1440              :   rv = false;
    1441              : 
    1442        13688 : finished:
    1443              :   return rv;
    1444              : }
    1445              : 
    1446              : 
    1447              : /* Given an expression node that is a constant string, see if it looks
    1448              :    like a format string.  */
    1449              : 
    1450              : static bool
    1451        12698 : check_format_string (gfc_expr *e, bool is_input,
    1452              :                       format_check_kind kind = FMT_CHECK_NORMAL)
    1453              : {
    1454        12698 :   bool rv;
    1455        12698 :   int i;
    1456        12698 :   if (!e || e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
    1457              :     return true;
    1458              : 
    1459        11615 :   mode = MODE_STRING;
    1460        11615 :   format_string = e->value.character.string;
    1461              : 
    1462              :   /* More elaborate measures are needed to show where a problem is within a
    1463              :      format string that has been calculated, but that's probably not worth the
    1464              :      effort.  */
    1465        11615 :   format_locus = e->where;
    1466              :   /* The first pass already reported the errors and standard notes.  */
    1467        11615 :   if (kind == FMT_CHECK_WIDTH)
    1468              :     {
    1469            4 :       gfc_push_suppress_errors ();
    1470            4 :       checking_width_only = true;
    1471              :     }
    1472        11615 :   rv = check_format (is_input, kind);
    1473        11615 :   if (kind == FMT_CHECK_WIDTH)
    1474              :     {
    1475            4 :       gfc_pop_suppress_errors ();
    1476            4 :       checking_width_only = false;
    1477              :     }
    1478              :   /* check for extraneous characters at the end of an otherwise valid format
    1479              :      string, like '(A10,I3)F5'
    1480              :      start at the end and move back to the last character processed,
    1481              :      spaces are OK */
    1482        11615 :   if (kind != FMT_CHECK_WIDTH
    1483        11615 :       && rv && e->value.character.length > format_string_pos)
    1484           75 :     for (i=e->value.character.length-1;i>format_string_pos-1;i--)
    1485           72 :       if (e->value.character.string[i] != ' ')
    1486              :         {
    1487            1 :           format_locus.nextc += format_length + 1;
    1488            1 :           gfc_warning (0,
    1489              :                        "Extraneous characters in format at %L", &format_locus);
    1490            1 :           break;
    1491              :         }
    1492              :   return rv;
    1493              : }
    1494              : 
    1495              : 
    1496              : /************ Fortran I/O statement matchers *************/
    1497              : 
    1498              : /* Match a FORMAT statement.  This amounts to actually parsing the
    1499              :    format descriptors in order to correctly locate the end of the
    1500              :    format string.  */
    1501              : 
    1502              : match
    1503         1041 : gfc_match_format (void)
    1504              : {
    1505         1041 :   gfc_expr *e;
    1506         1041 :   locus start;
    1507              : 
    1508         1041 :   if (gfc_current_ns->proc_name
    1509         1014 :       && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
    1510              :     {
    1511            1 :       gfc_error ("Format statement in module main block at %C");
    1512            1 :       return MATCH_ERROR;
    1513              :     }
    1514              : 
    1515              :   /* Before parsing the rest of a FORMAT statement, check F2008:c1206.  */
    1516         1040 :   if ((gfc_current_state () == COMP_FUNCTION
    1517         1040 :        || gfc_current_state () == COMP_SUBROUTINE)
    1518          183 :       && gfc_state_stack->previous->state == COMP_INTERFACE)
    1519              :     {
    1520            1 :       gfc_error ("FORMAT statement at %C cannot appear within an INTERFACE");
    1521            1 :       return MATCH_ERROR;
    1522              :     }
    1523              : 
    1524         1039 :   if (gfc_statement_label == NULL)
    1525              :     {
    1526            0 :       gfc_error ("Missing format label at %C");
    1527            0 :       return MATCH_ERROR;
    1528              :     }
    1529         1039 :   gfc_gobble_whitespace ();
    1530              : 
    1531         1039 :   mode = MODE_FORMAT;
    1532         1039 :   format_length = 0;
    1533              : 
    1534         1039 :   start = gfc_current_locus;
    1535              : 
    1536         1039 :   if (!check_format (false, FMT_CHECK_DECL))
    1537              :     return MATCH_ERROR;
    1538              : 
    1539         1034 :   if (gfc_match_eos () != MATCH_YES)
    1540              :     {
    1541            0 :       gfc_syntax_error (ST_FORMAT);
    1542            0 :       return MATCH_ERROR;
    1543              :     }
    1544              : 
    1545              :   /* The label doesn't get created until after the statement is done
    1546              :      being matched, so we have to leave the string for later.  */
    1547              : 
    1548         1034 :   gfc_current_locus = start;    /* Back to the beginning */
    1549              : 
    1550         1034 :   new_st.loc = start;
    1551         1034 :   new_st.op = EXEC_NOP;
    1552              : 
    1553         1034 :   e = gfc_get_character_expr (gfc_default_character_kind, &start,
    1554              :                               NULL, format_length);
    1555         1034 :   format_string = e->value.character.string;
    1556         1034 :   gfc_statement_label->format = e;
    1557              : 
    1558         1034 :   mode = MODE_COPY;
    1559         1034 :   check_format (false, FMT_CHECK_DECL); /* Guaranteed to succeed */
    1560         1034 :   gfc_match_eos ();             /* Guaranteed to succeed */
    1561              : 
    1562         1034 :   return MATCH_YES;
    1563              : }
    1564              : 
    1565              : 
    1566              : /* Match an expression I/O tag of some sort.  */
    1567              : 
    1568              : static match
    1569       688376 : match_etag (const io_tag *tag, gfc_expr **v)
    1570              : {
    1571       688376 :   gfc_expr *result;
    1572       688376 :   match m;
    1573              : 
    1574       688376 :   m = gfc_match (tag->spec);
    1575       688376 :   if (m != MATCH_YES)
    1576              :     return m;
    1577              : 
    1578        13103 :   m = gfc_match (tag->value, &result);
    1579        13103 :   if (m != MATCH_YES)
    1580              :     {
    1581            0 :       gfc_error ("Invalid value for %s specification at %C", tag->name);
    1582            0 :       return MATCH_ERROR;
    1583              :     }
    1584              : 
    1585        13103 :   if (*v != NULL)
    1586              :     {
    1587            2 :       gfc_error ("Duplicate %s specification at %C", tag->name);
    1588            2 :       gfc_free_expr (result);
    1589            2 :       return MATCH_ERROR;
    1590              :     }
    1591              : 
    1592        13101 :   *v = result;
    1593        13101 :   return MATCH_YES;
    1594              : }
    1595              : 
    1596              : 
    1597              : /* Match a variable I/O tag of some sort.  */
    1598              : 
    1599              : static match
    1600       202608 : match_vtag (const io_tag *tag, gfc_expr **v)
    1601              : {
    1602       202608 :   gfc_expr *result;
    1603       202608 :   match m;
    1604              : 
    1605       202608 :   m = gfc_match (tag->spec);
    1606       202608 :   if (m != MATCH_YES)
    1607              :     return m;
    1608              : 
    1609         4171 :   m = gfc_match (tag->value, &result);
    1610         4171 :   if (m != MATCH_YES)
    1611              :     {
    1612            1 :       gfc_error ("Invalid value for %s specification at %C", tag->name);
    1613            1 :       return MATCH_ERROR;
    1614              :     }
    1615              : 
    1616         4170 :   if (*v != NULL)
    1617              :     {
    1618            0 :       gfc_error ("Duplicate %s specification at %C", tag->name);
    1619            0 :       gfc_free_expr (result);
    1620            0 :       return MATCH_ERROR;
    1621              :     }
    1622              : 
    1623         4170 :   if (result->symtree)
    1624              :     {
    1625         4166 :       bool impure;
    1626              : 
    1627         4166 :       if (result->symtree->n.sym->attr.intent == INTENT_IN)
    1628              :         {
    1629            0 :           gfc_error ("Variable %s cannot be INTENT(IN) at %C", tag->name);
    1630            0 :           gfc_free_expr (result);
    1631            0 :           return MATCH_ERROR;
    1632              :         }
    1633              : 
    1634         4166 :       impure = gfc_impure_variable (result->symtree->n.sym);
    1635         4166 :       if (impure && gfc_pure (NULL))
    1636              :         {
    1637            0 :           gfc_error ("Variable %s cannot be assigned in PURE procedure at %C",
    1638            0 :                      tag->name);
    1639            0 :           gfc_free_expr (result);
    1640            0 :           return MATCH_ERROR;
    1641              :         }
    1642              : 
    1643         4166 :       if (impure)
    1644           28 :         gfc_unset_implicit_pure (NULL);
    1645              :     }
    1646              : 
    1647         4170 :   *v = result;
    1648         4170 :   return MATCH_YES;
    1649              : }
    1650              : 
    1651              : 
    1652              : /* Match I/O tags that cause variables to become redefined.  */
    1653              : 
    1654              : static match
    1655       173265 : match_out_tag (const io_tag *tag, gfc_expr **result)
    1656              : {
    1657       173265 :   match m;
    1658              : 
    1659       173265 :   m = match_vtag (tag, result);
    1660       173265 :   if (m == MATCH_YES)
    1661              :     {
    1662         2953 :       if ((*result)->symtree)
    1663         2949 :         gfc_check_do_variable ((*result)->symtree);
    1664              : 
    1665         2953 :       if ((*result)->expr_type == EXPR_CONSTANT)
    1666              :         {
    1667            4 :           gfc_error ("Expecting a variable at %L", &(*result)->where);
    1668            4 :           return MATCH_ERROR;
    1669              :         }
    1670              :     }
    1671              : 
    1672              :   return m;
    1673              : }
    1674              : 
    1675              : 
    1676              : /* Match a label I/O tag.  */
    1677              : 
    1678              : static match
    1679       150967 : match_ltag (const io_tag *tag, gfc_st_label ** label)
    1680              : {
    1681       150967 :   match m;
    1682       150967 :   gfc_st_label *old;
    1683              : 
    1684       150967 :   old = *label;
    1685       150967 :   m = gfc_match (tag->spec);
    1686       150967 :   if (m != MATCH_YES)
    1687              :     return m;
    1688              : 
    1689         1017 :   m = gfc_match (tag->value, label);
    1690         1017 :   if (m != MATCH_YES)
    1691              :     {
    1692            4 :       gfc_error ("Invalid value for %s specification at %C", tag->name);
    1693            4 :       return MATCH_ERROR;
    1694              :     }
    1695              : 
    1696         1013 :   if (old)
    1697              :     {
    1698            0 :       gfc_error ("Duplicate %s label specification at %C", tag->name);
    1699            0 :       return MATCH_ERROR;
    1700              :     }
    1701              : 
    1702         1013 :   if (!gfc_reference_st_label (*label, ST_LABEL_TARGET))
    1703            0 :     return MATCH_ERROR;
    1704              : 
    1705              :   return m;
    1706              : }
    1707              : 
    1708              : 
    1709              : /* Match a tag using match_etag, but only if -fdec is enabled.  */
    1710              : static match
    1711         5892 : match_dec_etag (const io_tag *tag, gfc_expr **e)
    1712              : {
    1713         5892 :   match m = match_etag (tag, e);
    1714         5892 :   if (flag_dec && m != MATCH_NO)
    1715              :     return m;
    1716         5729 :   else if (m != MATCH_NO)
    1717              :     {
    1718            7 :       gfc_error ("%s at %C is a DEC extension, enable with "
    1719            7 :                  "%<-fdec%>", tag->name);
    1720            7 :       return MATCH_ERROR;
    1721              :     }
    1722              :   return m;
    1723              : }
    1724              : 
    1725              : 
    1726              : /* Match a tag using match_vtag, but only if -fdec is enabled.  */
    1727              : static match
    1728          685 : match_dec_vtag (const io_tag *tag, gfc_expr **e)
    1729              : {
    1730          685 :   match m = match_vtag(tag, e);
    1731          685 :   if (flag_dec && m != MATCH_NO)
    1732              :     return m;
    1733          667 :   else if (m != MATCH_NO)
    1734              :     {
    1735            2 :       gfc_error ("%s at %C is a DEC extension, enable with "
    1736            2 :                  "%<-fdec%>", tag->name);
    1737            2 :       return MATCH_ERROR;
    1738              :     }
    1739              :   return m;
    1740              : }
    1741              : 
    1742              : 
    1743              : /* Match a DEC I/O flag tag - a tag with no expression such as READONLY.  */
    1744              : 
    1745              : static match
    1746         8659 : match_dec_ftag (const io_tag *tag, gfc_open *o)
    1747              : {
    1748         8659 :   match m;
    1749              : 
    1750         8659 :   m = gfc_match (tag->spec);
    1751         8659 :   if (m != MATCH_YES)
    1752              :     return m;
    1753              : 
    1754           44 :   if (!flag_dec)
    1755              :     {
    1756            6 :       gfc_error ("%s at %C is a DEC extension, enable with "
    1757            6 :                  "%<-fdec%>", tag->name);
    1758            6 :       return MATCH_ERROR;
    1759              :     }
    1760              : 
    1761              :   /* Just set the READONLY flag, which we use at runtime to avoid delete on
    1762              :      close.  */
    1763           38 :   if (tag == &tag_readonly)
    1764              :     {
    1765           22 :       o->readonly |= 1;
    1766           22 :       return MATCH_YES;
    1767              :     }
    1768              : 
    1769              :   /* Interpret SHARED as SHARE='DENYNONE' (read lock).  */
    1770           16 :   else if (tag == &tag_shared)
    1771              :     {
    1772            9 :       if (o->share != NULL)
    1773              :         {
    1774            2 :           gfc_error ("Duplicate %s specification at %C", tag->name);
    1775            2 :           return MATCH_ERROR;
    1776              :         }
    1777            7 :       o->share = gfc_get_character_expr (gfc_default_character_kind,
    1778              :           &gfc_current_locus, "denynone", 8);
    1779            7 :       return MATCH_YES;
    1780              :     }
    1781              : 
    1782              :   /* Interpret NOSHARED as SHARE='DENYRW' (exclusive lock).  */
    1783            7 :   else if (tag == &tag_noshared)
    1784              :     {
    1785            7 :       if (o->share != NULL)
    1786              :         {
    1787            0 :           gfc_error ("Duplicate %s specification at %C", tag->name);
    1788            0 :           return MATCH_ERROR;
    1789              :         }
    1790            7 :       o->share = gfc_get_character_expr (gfc_default_character_kind,
    1791              :           &gfc_current_locus, "denyrw", 6);
    1792            7 :       return MATCH_YES;
    1793              :     }
    1794              : 
    1795              :   /* We handle all DEC tags above.  */
    1796            0 :   gcc_unreachable ();
    1797              : }
    1798              : 
    1799              : 
    1800              : /* Resolution of the FORMAT tag, to be called from resolve_tag.  */
    1801              : 
    1802              : static bool
    1803        12655 : resolve_tag_format (gfc_expr *e)
    1804              : {
    1805        12655 :   if (e->expr_type == EXPR_CONSTANT
    1806        11580 :       && (e->ts.type != BT_CHARACTER
    1807        11576 :           || e->ts.kind != gfc_default_character_kind))
    1808              :     {
    1809            4 :       gfc_error ("Constant expression in FORMAT tag at %L must be "
    1810              :                  "of type default CHARACTER", &e->where);
    1811            4 :       return false;
    1812              :     }
    1813              : 
    1814              :   /* Concatenate a constant character array into a single character
    1815              :      expression.  */
    1816              : 
    1817        12629 :   if ((e->expr_type == EXPR_ARRAY || e->rank > 0)
    1818          111 :       && e->ts.type == BT_CHARACTER
    1819        12724 :       && gfc_is_constant_expr (e))
    1820              :     {
    1821            2 :       if (e->expr_type == EXPR_VARIABLE
    1822            0 :           && e->symtree->n.sym->attr.flavor == FL_PARAMETER)
    1823            0 :         gfc_simplify_expr (e, 1);
    1824              : 
    1825            2 :       if (e->expr_type == EXPR_ARRAY)
    1826              :         {
    1827            2 :           gfc_constructor *c;
    1828            2 :           gfc_charlen_t n, len;
    1829            2 :           gfc_expr *r;
    1830            2 :           gfc_char_t *dest, *src;
    1831              : 
    1832            2 :           if (e->value.constructor == NULL)
    1833              :            {
    1834            1 :              gfc_error ("FORMAT tag at %L cannot be a zero-sized array",
    1835              :                         &e->where);
    1836            1 :              return false;
    1837              :            }
    1838              : 
    1839            1 :           n = 0;
    1840            1 :           c = gfc_constructor_first (e->value.constructor);
    1841            1 :           len = c->expr->value.character.length;
    1842              : 
    1843           10 :           for ( ; c; c = gfc_constructor_next (c))
    1844            9 :             n += len;
    1845              : 
    1846            1 :           r = gfc_get_character_expr (e->ts.kind, &e->where, NULL, n);
    1847            1 :           dest = r->value.character.string;
    1848              : 
    1849            1 :           for (c = gfc_constructor_first (e->value.constructor);
    1850           10 :              c; c = gfc_constructor_next (c))
    1851              :             {
    1852            9 :               src = c->expr->value.character.string;
    1853           27 :               for (gfc_charlen_t i = 0 ; i < len; i++)
    1854           18 :                 *dest++ = *src++;
    1855              :             }
    1856              : 
    1857            1 :           gfc_replace_expr (e, r);
    1858            1 :           return true;
    1859              :         }
    1860              :     }
    1861              : 
    1862              :   /* If e's rank is zero and e is not an element of an array, it should be
    1863              :      of integer or character type.  The integer variable should be
    1864              :      ASSIGNED.  */
    1865        12649 :   if (e->rank == 0
    1866        12540 :       && (e->expr_type != EXPR_VARIABLE
    1867          934 :           || e->symtree == NULL
    1868          934 :           || e->symtree->n.sym->as == NULL
    1869           91 :           || e->symtree->n.sym->as->rank == 0))
    1870              :     {
    1871        12449 :       if ((e->ts.type != BT_CHARACTER
    1872        12440 :            || e->ts.kind != gfc_default_character_kind)
    1873           11 :           && e->ts.type != BT_INTEGER)
    1874              :         {
    1875            5 :           gfc_error ("FORMAT tag at %L must be of type default-kind CHARACTER "
    1876              :                      "or of INTEGER", &e->where);
    1877            5 :           return false;
    1878              :         }
    1879        12444 :       else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
    1880              :         {
    1881            3 :           if (!gfc_notify_std (GFC_STD_F95_DEL, "ASSIGNED variable in "
    1882              :                                "FORMAT tag at %L", &e->where))
    1883              :             return false;
    1884            3 :           if (e->symtree->n.sym->attr.assign != 1)
    1885              :             {
    1886            1 :               gfc_error ("Variable %qs at %L has not been assigned a "
    1887              :                          "format label", e->symtree->n.sym->name, &e->where);
    1888            1 :               return false;
    1889              :             }
    1890              :         }
    1891        12441 :       else if (e->ts.type == BT_INTEGER)
    1892              :         {
    1893            3 :           gfc_error ("Scalar %qs in FORMAT tag at %L is not an ASSIGNED "
    1894              :                      "variable", gfc_basic_typename (e->ts.type), &e->where);
    1895            3 :           return false;
    1896              :         }
    1897              : 
    1898        12440 :       gfc_value_used_expr (e, VALUE_USED);
    1899        12440 :       return true;
    1900              :     }
    1901              : 
    1902              :   /* If rank is nonzero and type is not character, we allow it under GFC_STD_LEGACY.
    1903              :      It may be assigned an Hollerith constant.  */
    1904          200 :   if (e->ts.type != BT_CHARACTER)
    1905              :     {
    1906              :       if (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS
    1907              :           || e->ts.type == BT_VOID || e->ts.type == BT_UNKNOWN)
    1908              :         {
    1909            5 :           gfc_error ("Non-character non-Hollerith in FORMAT tag at %L",
    1910              :                      &e->where);
    1911            5 :           return false;
    1912              :         }
    1913           62 :       if (!gfc_notify_std (GFC_STD_LEGACY, "Non-character in FORMAT tag "
    1914              :                            "at %L", &e->where))
    1915              :         return false;
    1916              : 
    1917           60 :       if (e->rank == 0 && e->symtree->n.sym->as->type == AS_ASSUMED_SHAPE)
    1918              :         {
    1919            1 :           gfc_error ("Non-character assumed shape array element in FORMAT"
    1920              :                      " tag at %L", &e->where);
    1921            1 :           return false;
    1922              :         }
    1923              : 
    1924           59 :       if (e->rank == 0 && e->symtree->n.sym->as->type == AS_ASSUMED_SIZE)
    1925              :         {
    1926            1 :           gfc_error ("Non-character assumed size array element in FORMAT"
    1927              :                      " tag at %L", &e->where);
    1928            1 :           return false;
    1929              :         }
    1930              : 
    1931           58 :       if (e->rank == 0 && e->symtree->n.sym->attr.pointer)
    1932              :         {
    1933            1 :           gfc_error ("Non-character pointer array element in FORMAT tag at %L",
    1934              :                      &e->where);
    1935            1 :           return false;
    1936              :         }
    1937              :     }
    1938              : 
    1939          190 :   gfc_value_used_expr (e, VALUE_USED);
    1940          190 :   return true;
    1941              : }
    1942              : 
    1943              : 
    1944              : /* Do expression resolution and type-checking on an expression tag.  */
    1945              : 
    1946              : static bool
    1947       655277 : resolve_tag (const io_tag *tag, gfc_expr *e)
    1948              : {
    1949       655277 :   if (e == NULL)
    1950              :     return true;
    1951              : 
    1952        38958 :   if (!gfc_resolve_expr (e))
    1953              :     return false;
    1954              : 
    1955        38954 :   if (tag == &tag_format)
    1956        12655 :     return resolve_tag_format (e);
    1957              : 
    1958        26299 :   if (e->ts.type != tag->type)
    1959              :     {
    1960          348 :       gfc_error ("%s tag at %L must be of type %s", tag->name,
    1961              :                  &e->where, gfc_basic_typename (tag->type));
    1962          348 :       return false;
    1963              :     }
    1964              : 
    1965        25951 :   if (e->ts.type == BT_CHARACTER && e->ts.kind != gfc_default_character_kind)
    1966              :     {
    1967           68 :       gfc_error ("%s tag at %L must be a character string of default kind",
    1968           68 :                  tag->name, &e->where);
    1969           68 :       return false;
    1970              :     }
    1971              : 
    1972        25883 :   if (e->rank != 0)
    1973              :     {
    1974           52 :       gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
    1975           52 :       return false;
    1976              :     }
    1977              : 
    1978        25831 :   if (tag == &tag_iomsg)
    1979              :     {
    1980          566 :       if (!gfc_notify_std (GFC_STD_F2003, "IOMSG tag at %L", &e->where))
    1981              :         return false;
    1982              :     }
    1983              : 
    1984        25831 :   if ((tag == &tag_iostat || tag == &tag_size || tag == &tag_iolength
    1985        23488 :        || tag == &tag_number || tag == &tag_nextrec || tag == &tag_s_recl)
    1986         2551 :       && e->ts.kind != gfc_default_integer_kind)
    1987              :     {
    1988          105 :       if (!gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
    1989          105 :                            "INTEGER in %s tag at %L", tag->name, &e->where))
    1990              :         return false;
    1991              :     }
    1992              : 
    1993        25815 :   if (e->ts.kind != gfc_default_logical_kind &&
    1994        11281 :       (tag == &tag_exist || tag == &tag_named || tag == &tag_opened
    1995        11275 :        || tag == &tag_pending))
    1996              :     {
    1997           39 :       if (!gfc_notify_std (GFC_STD_F2008, "Non-default LOGICAL kind "
    1998           39 :                            "in %s tag at %L", tag->name, &e->where))
    1999              :         return false;
    2000              :     }
    2001              : 
    2002        25807 :   if (tag == &tag_newunit)
    2003              :     {
    2004          230 :       if (!gfc_notify_std (GFC_STD_F2008, "NEWUNIT specifier at %L",
    2005              :                            &e->where))
    2006              :         return false;
    2007              :     }
    2008              : 
    2009        25806 :   if (tag == &tag_convert)
    2010              :     {
    2011           84 :       if (!gfc_notify_std (GFC_STD_GNU, "CONVERT tag at %L", &e->where))
    2012              :         return false;
    2013              :     }
    2014              : 
    2015              :   /* NEWUNIT, IOSTAT, SIZE and IOMSG are variable definition contexts.  */
    2016        25806 :   if (tag == &tag_newunit || tag == &tag_iostat
    2017        23441 :       || tag == &tag_size || tag == &tag_iomsg)
    2018              :     {
    2019         3035 :       char context[64];
    2020              : 
    2021         3035 :       sprintf (context, _("%s tag"), tag->name);
    2022         3035 :       if (!gfc_check_vardef_context (e, false, false, false, context))
    2023           32 :         return false;
    2024              : 
    2025         3003 :       gfc_expr_set_at (e, &e->where, VALUE_VARDEF);
    2026         3003 :     }
    2027              :   else
    2028        22771 :     gfc_value_used_expr (e, VALUE_USED);
    2029              : 
    2030              :   return true;
    2031              : }
    2032              : 
    2033              : 
    2034              : /* Match a single tag of an OPEN statement.  */
    2035              : 
    2036              : static match
    2037        11522 : match_open_element (gfc_open *open)
    2038              : {
    2039        11522 :   match m;
    2040              : 
    2041        11522 :   m = match_etag (&tag_e_async, &open->asynchronous);
    2042        11522 :   if (m != MATCH_NO)
    2043              :     return m;
    2044        11406 :   m = match_etag (&tag_unit, &open->unit);
    2045        11406 :   if (m != MATCH_NO)
    2046              :     return m;
    2047        10464 :   m = match_etag (&tag_iomsg, &open->iomsg);
    2048        10464 :   if (m != MATCH_NO)
    2049              :     return m;
    2050        10406 :   m = match_out_tag (&tag_iostat, &open->iostat);
    2051        10406 :   if (m != MATCH_NO)
    2052              :     return m;
    2053        10231 :   m = match_etag (&tag_file, &open->file);
    2054        10231 :   if (m != MATCH_NO)
    2055              :     return m;
    2056         8683 :   m = match_etag (&tag_status, &open->status);
    2057         8683 :   if (m != MATCH_NO)
    2058              :     return m;
    2059         6435 :   m = match_etag (&tag_e_access, &open->access);
    2060         6435 :   if (m != MATCH_NO)
    2061              :     return m;
    2062         5612 :   m = match_etag (&tag_e_form, &open->form);
    2063         5612 :   if (m != MATCH_NO)
    2064              :     return m;
    2065         4441 :   m = match_etag (&tag_e_recl, &open->recl);
    2066         4441 :   if (m != MATCH_NO)
    2067              :     return m;
    2068         4187 :   m = match_etag (&tag_e_blank, &open->blank);
    2069         4187 :   if (m != MATCH_NO)
    2070              :     return m;
    2071         4148 :   m = match_etag (&tag_e_position, &open->position);
    2072         4148 :   if (m != MATCH_NO)
    2073              :     return m;
    2074         4012 :   m = match_etag (&tag_e_action, &open->action);
    2075         4012 :   if (m != MATCH_NO)
    2076              :     return m;
    2077         3750 :   m = match_etag (&tag_e_delim, &open->delim);
    2078         3750 :   if (m != MATCH_NO)
    2079              :     return m;
    2080         3601 :   m = match_etag (&tag_e_pad, &open->pad);
    2081         3601 :   if (m != MATCH_NO)
    2082              :     return m;
    2083         3546 :   m = match_etag (&tag_e_decimal, &open->decimal);
    2084         3546 :   if (m != MATCH_NO)
    2085              :     return m;
    2086         3496 :   m = match_etag (&tag_e_encoding, &open->encoding);
    2087         3496 :   if (m != MATCH_NO)
    2088              :     return m;
    2089         3422 :   m = match_etag (&tag_e_round, &open->round);
    2090         3422 :   if (m != MATCH_NO)
    2091              :     return m;
    2092         3408 :   m = match_etag (&tag_e_sign, &open->sign);
    2093         3408 :   if (m != MATCH_NO)
    2094              :     return m;
    2095         3376 :   m = match_ltag (&tag_err, &open->err);
    2096         3376 :   if (m != MATCH_NO)
    2097              :     return m;
    2098         3260 :   m = match_etag (&tag_convert, &open->convert);
    2099         3260 :   if (m != MATCH_NO)
    2100              :     return m;
    2101         3188 :   m = match_out_tag (&tag_newunit, &open->newunit);
    2102         3188 :   if (m != MATCH_NO)
    2103              :     return m;
    2104              : 
    2105              :   /* The following are extensions enabled with -fdec.  */
    2106         2955 :   m = match_dec_etag (&tag_e_share, &open->share);
    2107         2955 :   if (m != MATCH_NO)
    2108              :     return m;
    2109         2937 :   m = match_dec_etag (&tag_cc, &open->cc);
    2110         2937 :   if (m != MATCH_NO)
    2111              :     return m;
    2112         2906 :   m = match_dec_ftag (&tag_readonly, open);
    2113         2906 :   if (m != MATCH_NO)
    2114              :     return m;
    2115         2882 :   m = match_dec_ftag (&tag_shared, open);
    2116         2882 :   if (m != MATCH_NO)
    2117              :     return m;
    2118         2871 :   m = match_dec_ftag (&tag_noshared, open);
    2119         2871 :   if (m != MATCH_NO)
    2120              :     return m;
    2121              : 
    2122              :   return MATCH_NO;
    2123              : }
    2124              : 
    2125              : 
    2126              : /* Free the gfc_open structure and all the expressions it contains.  */
    2127              : 
    2128              : void
    2129         4038 : gfc_free_open (gfc_open *open)
    2130              : {
    2131         4038 :   if (open == NULL)
    2132              :     return;
    2133              : 
    2134         4038 :   gfc_free_expr (open->unit);
    2135         4038 :   gfc_free_expr (open->iomsg);
    2136         4038 :   gfc_free_expr (open->iostat);
    2137         4038 :   gfc_free_expr (open->file);
    2138         4038 :   gfc_free_expr (open->status);
    2139         4038 :   gfc_free_expr (open->access);
    2140         4038 :   gfc_free_expr (open->form);
    2141         4038 :   gfc_free_expr (open->recl);
    2142         4038 :   gfc_free_expr (open->blank);
    2143         4038 :   gfc_free_expr (open->position);
    2144         4038 :   gfc_free_expr (open->action);
    2145         4038 :   gfc_free_expr (open->delim);
    2146         4038 :   gfc_free_expr (open->pad);
    2147         4038 :   gfc_free_expr (open->decimal);
    2148         4038 :   gfc_free_expr (open->encoding);
    2149         4038 :   gfc_free_expr (open->round);
    2150         4038 :   gfc_free_expr (open->sign);
    2151         4038 :   gfc_free_expr (open->convert);
    2152         4038 :   gfc_free_expr (open->asynchronous);
    2153         4038 :   gfc_free_expr (open->newunit);
    2154         4038 :   gfc_free_expr (open->share);
    2155         4038 :   gfc_free_expr (open->cc);
    2156         4038 :   free (open);
    2157              : }
    2158              : 
    2159              : static bool
    2160              : check_open_constraints (gfc_open *open, locus *where);
    2161              : 
    2162              : /* Resolve everything in a gfc_open structure.  */
    2163              : 
    2164              : bool
    2165         4021 : gfc_resolve_open (gfc_open *open, locus *where)
    2166              : {
    2167         4021 :   RESOLVE_TAG (&tag_unit, open->unit);
    2168         4021 :   RESOLVE_TAG (&tag_iomsg, open->iomsg);
    2169         4005 :   RESOLVE_TAG (&tag_iostat, open->iostat);
    2170         4004 :   RESOLVE_TAG (&tag_file, open->file);
    2171         4003 :   RESOLVE_TAG (&tag_status, open->status);
    2172         3989 :   RESOLVE_TAG (&tag_e_access, open->access);
    2173         3977 :   RESOLVE_TAG (&tag_e_form, open->form);
    2174         3966 :   RESOLVE_TAG (&tag_e_recl, open->recl);
    2175         3960 :   RESOLVE_TAG (&tag_e_blank, open->blank);
    2176         3948 :   RESOLVE_TAG (&tag_e_position, open->position);
    2177         3937 :   RESOLVE_TAG (&tag_e_action, open->action);
    2178         3926 :   RESOLVE_TAG (&tag_e_delim, open->delim);
    2179         3915 :   RESOLVE_TAG (&tag_e_pad, open->pad);
    2180         3904 :   RESOLVE_TAG (&tag_e_decimal, open->decimal);
    2181         3892 :   RESOLVE_TAG (&tag_e_encoding, open->encoding);
    2182         3880 :   RESOLVE_TAG (&tag_e_async, open->asynchronous);
    2183         3869 :   RESOLVE_TAG (&tag_e_round, open->round);
    2184         3857 :   RESOLVE_TAG (&tag_e_sign, open->sign);
    2185         3845 :   RESOLVE_TAG (&tag_convert, open->convert);
    2186         3845 :   RESOLVE_TAG (&tag_newunit, open->newunit);
    2187         3843 :   RESOLVE_TAG (&tag_e_share, open->share);
    2188         3842 :   RESOLVE_TAG (&tag_cc, open->cc);
    2189              : 
    2190         3841 :   if (!gfc_reference_st_label (open->err, ST_LABEL_TARGET))
    2191              :     return false;
    2192              : 
    2193         3841 :   return check_open_constraints (open, where);
    2194              : }
    2195              : 
    2196              : 
    2197              : /* Check if a given value for a SPECIFIER is either in the list of values
    2198              :    allowed in F95 or F2003, issuing an error message and returning a zero
    2199              :    value if it is not allowed.  */
    2200              : 
    2201              : 
    2202              : static bool
    2203         7183 : compare_to_allowed_values (const char *specifier, const char *allowed[],
    2204              :                            const char *allowed_f2003[],
    2205              :                            const char *allowed_gnu[], gfc_char_t *value,
    2206              :                            const char *statement, bool warn, locus *where,
    2207              :                            int *num = NULL)
    2208              : {
    2209         7183 :   int i;
    2210         7183 :   unsigned int len;
    2211              : 
    2212         7183 :   len = gfc_wide_strlen (value);
    2213         7183 :   if (len > 0)
    2214              :   {
    2215         7170 :     for (len--; len > 0; len--)
    2216         7170 :       if (value[len] != ' ')
    2217              :         break;
    2218         7152 :     len++;
    2219              :   }
    2220              : 
    2221        16106 :   for (i = 0; allowed[i]; i++)
    2222        15537 :     if (len == strlen (allowed[i])
    2223        15537 :         && gfc_wide_strncasecmp (value, allowed[i], strlen (allowed[i])) == 0)
    2224              :       {
    2225         6614 :         if (num)
    2226          199 :           *num = i;
    2227              :       return 1;
    2228              :       }
    2229              : 
    2230          569 :   if (!where)
    2231            0 :     where = &gfc_current_locus;
    2232              : 
    2233          598 :   for (i = 0; allowed_f2003 && allowed_f2003[i]; i++)
    2234          479 :     if (len == strlen (allowed_f2003[i])
    2235          479 :         && gfc_wide_strncasecmp (value, allowed_f2003[i],
    2236              :                                  strlen (allowed_f2003[i])) == 0)
    2237              :       {
    2238          450 :         notification n = gfc_notification_std (GFC_STD_F2003);
    2239              : 
    2240          450 :         if (n == WARNING || (warn && n == ERROR))
    2241              :           {
    2242            0 :             gfc_warning (0, "Fortran 2003: %s specifier in %s statement at %L "
    2243              :                          "has value %qs", specifier, statement, where,
    2244              :                          allowed_f2003[i]);
    2245            0 :             return 1;
    2246              :           }
    2247              :         else
    2248          450 :           if (n == ERROR)
    2249              :             {
    2250            0 :               gfc_notify_std (GFC_STD_F2003, "%s specifier in "
    2251              :                               "%s statement at %L has value %qs", specifier,
    2252              :                               statement, where, allowed_f2003[i]);
    2253            0 :               return 0;
    2254              :             }
    2255              : 
    2256              :         /* n == SILENT */
    2257              :         return 1;
    2258              :       }
    2259              : 
    2260          127 :   for (i = 0; allowed_gnu && allowed_gnu[i]; i++)
    2261           29 :     if (len == strlen (allowed_gnu[i])
    2262           29 :         && gfc_wide_strncasecmp (value, allowed_gnu[i],
    2263              :                                  strlen (allowed_gnu[i])) == 0)
    2264              :       {
    2265           21 :         notification n = gfc_notification_std (GFC_STD_GNU);
    2266              : 
    2267           21 :         if (n == WARNING || (warn && n == ERROR))
    2268              :           {
    2269           21 :             gfc_warning (0, "Extension: %s specifier in %s statement at %L "
    2270              :                          "has value %qs", specifier, statement, where,
    2271              :                          allowed_gnu[i]);
    2272           21 :             return 1;
    2273              :           }
    2274              :         else
    2275            0 :           if (n == ERROR)
    2276              :             {
    2277            0 :               gfc_notify_std (GFC_STD_GNU, "%s specifier in "
    2278              :                               "%s statement at %L has value %qs", specifier,
    2279              :                               statement, where, allowed_gnu[i]);
    2280            0 :               return 0;
    2281              :             }
    2282              : 
    2283              :         /* n == SILENT */
    2284              :         return 1;
    2285              :       }
    2286              : 
    2287           98 :   if (warn)
    2288              :     {
    2289           38 :       char *s = gfc_widechar_to_char (value, -1);
    2290           38 :       gfc_warning (0,
    2291              :                    "%s specifier in %s statement at %L has invalid value %qs",
    2292              :                    specifier, statement, where, s);
    2293           38 :       free (s);
    2294           38 :       return 1;
    2295              :     }
    2296              :   else
    2297              :     {
    2298           60 :       char *s = gfc_widechar_to_char (value, -1);
    2299           60 :       gfc_error ("%s specifier in %s statement at %L has invalid value %qs",
    2300              :                  specifier, statement, where, s);
    2301           60 :       free (s);
    2302           60 :       return 0;
    2303              :     }
    2304              : }
    2305              : 
    2306              : 
    2307              : /* Check constraints on the OPEN statement.
    2308              :    Similar to check_io_constraints for data transfer statements.
    2309              :    At this point all tags have already been resolved via resolve_tag, which,
    2310              :    among other things, verifies that BT_CHARACTER tags are of default kind.  */
    2311              : 
    2312              : static bool
    2313         3841 : check_open_constraints (gfc_open *open, locus *where)
    2314              : {
    2315              : #define warn_or_error(...) \
    2316              : { \
    2317              :   if (warn) \
    2318              :     gfc_warning (0, __VA_ARGS__); \
    2319              :   else \
    2320              :     { \
    2321              :       gfc_error (__VA_ARGS__); \
    2322              :       return false; \
    2323              :     } \
    2324              : }
    2325              : 
    2326         3841 :   bool warn = open->err || open->iostat;
    2327              : 
    2328              :   /* Checks on the ACCESS specifier.  */
    2329         3841 :   if (open->access && open->access->expr_type == EXPR_CONSTANT)
    2330              :     {
    2331          805 :       static const char *access_f95[] = { "SEQUENTIAL", "DIRECT", NULL };
    2332          805 :       static const char *access_f2003[] = { "STREAM", NULL };
    2333          805 :       static const char *access_gnu[] = { "APPEND", NULL };
    2334              : 
    2335          805 :       if (!compare_to_allowed_values ("ACCESS", access_f95, access_f2003,
    2336              :                                       access_gnu,
    2337              :                                       open->access->value.character.string,
    2338              :                                       "OPEN", warn, &open->access->where))
    2339              :         return false;
    2340              :     }
    2341              : 
    2342              :   /* Checks on the ACTION specifier.  */
    2343         3837 :   if (open->action && open->action->expr_type == EXPR_CONSTANT)
    2344              :     {
    2345          238 :       gfc_char_t *str = open->action->value.character.string;
    2346          238 :       static const char *action[] = { "READ", "WRITE", "READWRITE", NULL };
    2347              : 
    2348          238 :       if (!compare_to_allowed_values ("ACTION", action, NULL, NULL,
    2349              :                                       str, "OPEN", warn, &open->action->where))
    2350              :         return false;
    2351              : 
    2352              :       /* With READONLY, only allow ACTION='READ'.  */
    2353          235 :       if (open->readonly && (gfc_wide_strlen (str) != 4
    2354            8 :                              || gfc_wide_strncasecmp (str, "READ", 4) != 0))
    2355              :         {
    2356            2 :           gfc_error ("ACTION type conflicts with READONLY specifier at %L",
    2357            2 :                      &open->action->where);
    2358            2 :           return false;
    2359              :         }
    2360              :     }
    2361              : 
    2362              :   /* If we see READONLY and no ACTION, set ACTION='READ'.  */
    2363         3599 :   else if (open->readonly && open->action == NULL)
    2364              :     {
    2365            6 :       open->action = gfc_get_character_expr (gfc_default_character_kind,
    2366              :                                              &gfc_current_locus, "read", 4);
    2367              :     }
    2368              : 
    2369              :   /* Checks on the ASYNCHRONOUS specifier.  */
    2370         3832 :   if (open->asynchronous)
    2371              :     {
    2372          105 :       if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS= at %L "
    2373              :                            "not allowed in Fortran 95",
    2374              :                            &open->asynchronous->where))
    2375              :         return false;
    2376              : 
    2377          105 :       if (open->asynchronous->expr_type == EXPR_CONSTANT)
    2378              :         {
    2379          103 :           static const char * asynchronous[] = { "YES", "NO", NULL };
    2380              : 
    2381          103 :           if (!compare_to_allowed_values ("ASYNCHRONOUS", asynchronous,
    2382              :                         NULL, NULL, open->asynchronous->value.character.string,
    2383              :                         "OPEN", warn, &open->asynchronous->where))
    2384              :             return false;
    2385              :         }
    2386              :     }
    2387              : 
    2388              :   /* Checks on the BLANK specifier.  */
    2389         3831 :   if (open->blank)
    2390              :     {
    2391           27 :       if (open->blank->expr_type == EXPR_CONSTANT)
    2392              :         {
    2393           27 :           static const char *blank[] = { "ZERO", "NULL", NULL };
    2394              : 
    2395           27 :           if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
    2396              :                                           open->blank->value.character.string,
    2397              :                                           "OPEN", warn, &open->blank->where))
    2398              :             return false;
    2399              :         }
    2400              :     }
    2401              : 
    2402              :   /* Checks on the CARRIAGECONTROL specifier.  */
    2403         3828 :   if (open->cc && open->cc->expr_type == EXPR_CONSTANT)
    2404              :     {
    2405           18 :       static const char *cc[] = { "LIST", "FORTRAN", "NONE", NULL };
    2406           18 :       if (!compare_to_allowed_values ("CARRIAGECONTROL", cc, NULL, NULL,
    2407              :                                       open->cc->value.character.string,
    2408              :                                       "OPEN", warn, &open->cc->where))
    2409              :         return false;
    2410              :     }
    2411              : 
    2412              :   /* Checks on the DECIMAL specifier.  */
    2413         3828 :   if (open->decimal)
    2414              :     {
    2415           38 :       if (!gfc_notify_std (GFC_STD_F2003, "DECIMAL= at %L "
    2416              :                            "not allowed in Fortran 95", &open->decimal->where))
    2417              :         return false;
    2418              : 
    2419           38 :       if (open->decimal->expr_type == EXPR_CONSTANT)
    2420              :         {
    2421           38 :           static const char * decimal[] = { "COMMA", "POINT", NULL };
    2422              : 
    2423           38 :           if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
    2424              :                                           open->decimal->value.character.string,
    2425              :                                           "OPEN", warn, &open->decimal->where))
    2426              :             return false;
    2427              :         }
    2428              :     }
    2429              : 
    2430              :   /* Checks on the DELIM specifier.  */
    2431         3826 :   if (open->delim)
    2432              :     {
    2433          138 :       if (open->delim->expr_type == EXPR_CONSTANT)
    2434              :         {
    2435          138 :           static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
    2436              : 
    2437          138 :           if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
    2438              :                                           open->delim->value.character.string,
    2439              :                                           "OPEN", warn, &open->delim->where))
    2440              :             return false;
    2441              :         }
    2442              :     }
    2443              : 
    2444              :   /* Checks on the ENCODING specifier.  */
    2445         3823 :   if (open->encoding)
    2446              :     {
    2447           62 :       if (!gfc_notify_std (GFC_STD_F2003, "ENCODING= at %L "
    2448              :                            "not allowed in Fortran 95", &open->encoding->where))
    2449              :         return false;
    2450              : 
    2451           62 :       if (open->encoding->expr_type == EXPR_CONSTANT)
    2452              :         {
    2453           62 :           static const char * encoding[] = { "DEFAULT", "UTF-8", NULL };
    2454              : 
    2455           62 :           if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
    2456              :                                           open->encoding->value.character.string,
    2457              :                                           "OPEN", warn, &open->encoding->where))
    2458              :             return false;
    2459              :         }
    2460              :     }
    2461              : 
    2462              :   /* Checks on the FORM specifier.  */
    2463         3821 :   if (open->form && open->form->expr_type == EXPR_CONSTANT)
    2464              :     {
    2465         1154 :       static const char *form[] = { "FORMATTED", "UNFORMATTED", NULL };
    2466              : 
    2467         1154 :       if (!compare_to_allowed_values ("FORM", form, NULL, NULL,
    2468              :                                       open->form->value.character.string,
    2469              :                                       "OPEN", warn, &open->form->where))
    2470              :         return false;
    2471              :     }
    2472              : 
    2473              :   /* Checks on the PAD specifier.  */
    2474         3818 :   if (open->pad && open->pad->expr_type == EXPR_CONSTANT)
    2475              :     {
    2476           44 :       static const char *pad[] = { "YES", "NO", NULL };
    2477              : 
    2478           44 :       if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
    2479              :                                       open->pad->value.character.string,
    2480              :                                       "OPEN", warn, &open->pad->where))
    2481              :         return false;
    2482              :     }
    2483              : 
    2484              :   /* Checks on the POSITION specifier.  */
    2485         3816 :   if (open->position && open->position->expr_type == EXPR_CONSTANT)
    2486              :     {
    2487          125 :       static const char *position[] = { "ASIS", "REWIND", "APPEND", NULL };
    2488              : 
    2489          125 :       if (!compare_to_allowed_values ("POSITION", position, NULL, NULL,
    2490              :                                       open->position->value.character.string,
    2491              :                                       "OPEN", warn, &open->position->where))
    2492              :         return false;
    2493              :     }
    2494              : 
    2495              :   /* Checks on the ROUND specifier.  */
    2496         3813 :   if (open->round)
    2497              :     {
    2498            2 :       if (!gfc_notify_std (GFC_STD_F2003, "ROUND= at %L "
    2499              :                            "not allowed in Fortran 95", &open->round->where))
    2500              :         return false;
    2501              : 
    2502            2 :       if (open->round->expr_type == EXPR_CONSTANT)
    2503              :         {
    2504            2 :           static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
    2505              :                                           "COMPATIBLE", "PROCESSOR_DEFINED",
    2506              :                                            NULL };
    2507              : 
    2508            2 :           if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
    2509              :                                           open->round->value.character.string,
    2510              :                                           "OPEN", warn, &open->round->where))
    2511              :             return false;
    2512              :         }
    2513              :     }
    2514              : 
    2515              :   /* Checks on the SHARE specifier.  */
    2516         3811 :   if (open->share && open->share->expr_type == EXPR_CONSTANT)
    2517              :     {
    2518           24 :       static const char *share[] = { "DENYNONE", "DENYRW", NULL };
    2519           24 :       if (!compare_to_allowed_values ("SHARE", share, NULL, NULL,
    2520              :                                       open->share->value.character.string,
    2521              :                                       "OPEN", warn, &open->share->where))
    2522              :         return false;
    2523              :     }
    2524              : 
    2525              :   /* Checks on the SIGN specifier.  */
    2526         3811 :   if (open->sign)
    2527              :     {
    2528           20 :       if (!gfc_notify_std (GFC_STD_F2003, "SIGN= at %L "
    2529              :                            "not allowed in Fortran 95", &open->sign->where))
    2530              :         return false;
    2531              : 
    2532           20 :       if (open->sign->expr_type == EXPR_CONSTANT)
    2533              :         {
    2534           20 :           static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
    2535              :                                           NULL };
    2536              : 
    2537           20 :           if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
    2538              :                                           open->sign->value.character.string,
    2539              :                                           "OPEN", warn, &open->sign->where))
    2540              :             return false;
    2541              :         }
    2542              :     }
    2543              : 
    2544              :   /* Checks on the RECL specifier.  */
    2545         3809 :   if (open->recl && open->recl->expr_type == EXPR_CONSTANT
    2546          212 :       && open->recl->ts.type == BT_INTEGER
    2547          212 :       && mpz_sgn (open->recl->value.integer) != 1)
    2548              :     {
    2549            6 :       warn_or_error (G_("RECL in OPEN statement at %L must be positive"),
    2550            4 :                      &open->recl->where);
    2551              :     }
    2552              : 
    2553              :   /* Checks on the STATUS specifier.  */
    2554         3807 :   if (open->status && open->status->expr_type == EXPR_CONSTANT)
    2555              :     {
    2556         2226 :       static const char *status[] = { "OLD", "NEW", "SCRATCH",
    2557              :         "REPLACE", "UNKNOWN", NULL };
    2558              : 
    2559         2226 :       if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
    2560              :                                       open->status->value.character.string,
    2561              :                                       "OPEN", warn, &open->status->where))
    2562              :         return false;
    2563              : 
    2564              :       /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
    2565              :          the FILE= specifier shall appear.  */
    2566         2223 :       if (open->file == NULL
    2567         2223 :           && (gfc_wide_strncasecmp (open->status->value.character.string,
    2568              :                                     "replace", 7) == 0
    2569         1682 :               || gfc_wide_strncasecmp (open->status->value.character.string,
    2570              :                                        "new", 3) == 0))
    2571              :         {
    2572            6 :           char *s = gfc_widechar_to_char (open->status->value.character.string,
    2573              :                                           -1);
    2574            6 :           warn_or_error (G_("The STATUS specified in OPEN statement at %L is "
    2575              :                          "%qs and no FILE specifier is present"),
    2576            4 :                          &open->status->where, s);
    2577            4 :           free (s);
    2578              :         }
    2579              : 
    2580              :       /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
    2581              :          the FILE= specifier shall not appear.  */
    2582         2221 :       if (gfc_wide_strncasecmp (open->status->value.character.string,
    2583         2221 :                                 "scratch", 7) == 0 && open->file)
    2584              :         {
    2585            3 :           warn_or_error (G_("The STATUS specified in OPEN statement at %L "
    2586              :                          "cannot have the value SCRATCH if a FILE specifier "
    2587            2 :                          "is present"), &open->status->where);
    2588              :         }
    2589              :     }
    2590              : 
    2591              :   /* Checks on NEWUNIT specifier.  */
    2592         3801 :   if (open->newunit)
    2593              :     {
    2594          228 :       if (open->unit)
    2595              :         {
    2596            0 :           gfc_error ("UNIT specifier not allowed with NEWUNIT at %L",
    2597              :                      &open->newunit->where);
    2598            0 :           return false;
    2599              :         }
    2600              : 
    2601          228 :       if (!open->file &&
    2602           75 :           (!open->status ||
    2603           74 :            (open->status->expr_type == EXPR_CONSTANT
    2604           73 :              && gfc_wide_strncasecmp (open->status->value.character.string,
    2605              :                                       "scratch", 7) != 0)))
    2606              :         {
    2607            1 :              gfc_error ("NEWUNIT specifier must have FILE= "
    2608            1 :                         "or STATUS='scratch' at %L", &open->newunit->where);
    2609            1 :              return false;
    2610              :         }
    2611              :     }
    2612         3573 :   else if (!open->unit)
    2613              :     {
    2614            2 :       gfc_error ("OPEN statement at %L must have UNIT or NEWUNIT specified",
    2615              :                  where);
    2616            2 :       return false;
    2617              :     }
    2618              : 
    2619              :   /* Things that are not allowed for unformatted I/O.  */
    2620         1157 :   if (open->form && open->form->expr_type == EXPR_CONSTANT
    2621         1151 :       && (open->delim || open->decimal || open->encoding || open->round
    2622         1136 :           || open->sign || open->pad || open->blank)
    2623         3819 :       && gfc_wide_strncasecmp (open->form->value.character.string,
    2624              :                                "unformatted", 11) == 0)
    2625              :     {
    2626            9 :       locus *loc;
    2627            9 :       const char *spec;
    2628            9 :       if (open->delim)
    2629              :         {
    2630            3 :           loc = &open->delim->where;
    2631            3 :           spec = "DELIM ";
    2632              :         }
    2633            6 :       else if (open->pad)
    2634              :         {
    2635            3 :           loc = &open->pad->where;
    2636            3 :           spec = "PAD ";
    2637              :         }
    2638            3 :       else if (open->blank)
    2639              :         {
    2640            3 :           loc = &open->blank->where;
    2641            3 :           spec = "BLANK ";
    2642              :         }
    2643              :       else
    2644              :         {
    2645              :           loc = where;
    2646              :           spec = "";
    2647              :         }
    2648              : 
    2649            9 :       warn_or_error (G_("%sspecifier at %L not allowed in OPEN statement for "
    2650            6 :                      "unformatted I/O"), spec, loc);
    2651              :     }
    2652              : 
    2653          246 :   if (open->recl && open->access && open->access->expr_type == EXPR_CONSTANT
    2654         4019 :       && gfc_wide_strncasecmp (open->access->value.character.string,
    2655              :                                "stream", 6) == 0)
    2656              :     {
    2657            0 :       warn_or_error (G_("RECL specifier not allowed in OPEN statement at %L for "
    2658            0 :                      "stream I/O"), &open->recl->where);
    2659              :     }
    2660              : 
    2661         3795 :   if (open->position
    2662          122 :       && open->access && open->access->expr_type == EXPR_CONSTANT
    2663         3843 :       && !(gfc_wide_strncasecmp (open->access->value.character.string,
    2664              :                                  "sequential", 10) == 0
    2665           39 :            || gfc_wide_strncasecmp (open->access->value.character.string,
    2666              :                                     "stream", 6) == 0
    2667            9 :            || gfc_wide_strncasecmp (open->access->value.character.string,
    2668              :                                     "append", 6) == 0))
    2669              :     {
    2670            3 :       warn_or_error (G_("POSITION specifier in OPEN statement at %L only allowed "
    2671            2 :                      "for stream or sequential ACCESS"), &open->position->where);
    2672              :     }
    2673              : 
    2674              :   return true;
    2675              : #undef warn_or_error
    2676              : }
    2677              : 
    2678              : 
    2679              : /* Match an OPEN statement.  */
    2680              : 
    2681              : match
    2682         4038 : gfc_match_open (void)
    2683              : {
    2684         4038 :   gfc_open *open;
    2685         4038 :   match m;
    2686              : 
    2687         4038 :   m = gfc_match_char ('(');
    2688         4038 :   if (m == MATCH_NO)
    2689              :     return m;
    2690              : 
    2691         4038 :   open = XCNEW (gfc_open);
    2692              : 
    2693         4038 :   m = match_open_element (open);
    2694              : 
    2695         4038 :   if (m == MATCH_ERROR)
    2696            0 :     goto cleanup;
    2697         4038 :   if (m == MATCH_NO)
    2698              :     {
    2699         2862 :       m = gfc_match_expr (&open->unit);
    2700         2862 :       if (m == MATCH_ERROR)
    2701            0 :         goto cleanup;
    2702              :     }
    2703              : 
    2704        11505 :   for (;;)
    2705              :     {
    2706        11505 :       if (gfc_match_char (')') == MATCH_YES)
    2707              :         break;
    2708         7484 :       if (gfc_match_char (',') != MATCH_YES)
    2709            0 :         goto syntax;
    2710              : 
    2711         7484 :       m = match_open_element (open);
    2712         7484 :       if (m == MATCH_ERROR)
    2713           17 :         goto cleanup;
    2714         7467 :       if (m == MATCH_NO)
    2715            0 :         goto syntax;
    2716              :     }
    2717              : 
    2718         4021 :   if (gfc_match_eos () == MATCH_NO)
    2719            0 :     goto syntax;
    2720              : 
    2721         4021 :   if (gfc_pure (NULL))
    2722              :     {
    2723            0 :       gfc_error ("OPEN statement not allowed in PURE procedure at %C");
    2724            0 :       goto cleanup;
    2725              :     }
    2726              : 
    2727         4021 :   gfc_unset_implicit_pure (NULL);
    2728              : 
    2729         4021 :   new_st.op = EXEC_OPEN;
    2730         4021 :   new_st.ext.open = open;
    2731         4021 :   return MATCH_YES;
    2732              : 
    2733            0 : syntax:
    2734            0 :   gfc_syntax_error (ST_OPEN);
    2735              : 
    2736           17 : cleanup:
    2737           17 :   gfc_free_open (open);
    2738           17 :   return MATCH_ERROR;
    2739              : }
    2740              : 
    2741              : 
    2742              : /* Free a gfc_close structure an all its expressions.  */
    2743              : 
    2744              : void
    2745         3214 : gfc_free_close (gfc_close *close)
    2746              : {
    2747         3214 :   if (close == NULL)
    2748              :     return;
    2749              : 
    2750         3214 :   gfc_free_expr (close->unit);
    2751         3214 :   gfc_free_expr (close->iomsg);
    2752         3214 :   gfc_free_expr (close->iostat);
    2753         3214 :   gfc_free_expr (close->status);
    2754         3214 :   free (close);
    2755              : }
    2756              : 
    2757              : 
    2758              : /* Match elements of a CLOSE statement.  */
    2759              : 
    2760              : static match
    2761         4710 : match_close_element (gfc_close *close)
    2762              : {
    2763         4710 :   match m;
    2764              : 
    2765         4710 :   m = match_etag (&tag_unit, &close->unit);
    2766         4710 :   if (m != MATCH_NO)
    2767              :     return m;
    2768         4399 :   m = match_etag (&tag_status, &close->status);
    2769         4399 :   if (m != MATCH_NO)
    2770              :     return m;
    2771         2968 :   m = match_etag (&tag_iomsg, &close->iomsg);
    2772         2968 :   if (m != MATCH_NO)
    2773              :     return m;
    2774         2939 :   m = match_out_tag (&tag_iostat, &close->iostat);
    2775         2939 :   if (m != MATCH_NO)
    2776              :     return m;
    2777         2912 :   m = match_ltag (&tag_err, &close->err);
    2778         2912 :   if (m != MATCH_NO)
    2779              :     return m;
    2780              : 
    2781              :   return MATCH_NO;
    2782              : }
    2783              : 
    2784              : 
    2785              : /* Match a CLOSE statement.  */
    2786              : 
    2787              : match
    2788         3214 : gfc_match_close (void)
    2789              : {
    2790         3214 :   gfc_close *close;
    2791         3214 :   match m;
    2792              : 
    2793         3214 :   m = gfc_match_char ('(');
    2794         3214 :   if (m == MATCH_NO)
    2795              :     return m;
    2796              : 
    2797         3214 :   close = XCNEW (gfc_close);
    2798              : 
    2799         3214 :   m = match_close_element (close);
    2800              : 
    2801         3214 :   if (m == MATCH_ERROR)
    2802            0 :     goto cleanup;
    2803         3214 :   if (m == MATCH_NO)
    2804              :     {
    2805         2902 :       m = gfc_match_expr (&close->unit);
    2806         2902 :       if (m == MATCH_NO)
    2807            0 :         goto syntax;
    2808         2902 :       if (m == MATCH_ERROR)
    2809            0 :         goto cleanup;
    2810              :     }
    2811              : 
    2812         4710 :   for (;;)
    2813              :     {
    2814         4710 :       if (gfc_match_char (')') == MATCH_YES)
    2815              :         break;
    2816         1496 :       if (gfc_match_char (',') != MATCH_YES)
    2817            0 :         goto syntax;
    2818              : 
    2819         1496 :       m = match_close_element (close);
    2820         1496 :       if (m == MATCH_ERROR)
    2821            0 :         goto cleanup;
    2822         1496 :       if (m == MATCH_NO)
    2823            0 :         goto syntax;
    2824              :     }
    2825              : 
    2826         3214 :   if (gfc_match_eos () == MATCH_NO)
    2827            0 :     goto syntax;
    2828              : 
    2829         3214 :   if (gfc_pure (NULL))
    2830              :     {
    2831            0 :       gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
    2832            0 :       goto cleanup;
    2833              :     }
    2834              : 
    2835         3214 :   gfc_unset_implicit_pure (NULL);
    2836              : 
    2837         3214 :   new_st.op = EXEC_CLOSE;
    2838         3214 :   new_st.ext.close = close;
    2839         3214 :   return MATCH_YES;
    2840              : 
    2841            0 : syntax:
    2842            0 :   gfc_syntax_error (ST_CLOSE);
    2843              : 
    2844            0 : cleanup:
    2845            0 :   gfc_free_close (close);
    2846            0 :   return MATCH_ERROR;
    2847              : }
    2848              : 
    2849              : 
    2850              : static bool
    2851         3184 : check_close_constraints (gfc_close *close, locus *where)
    2852              : {
    2853         3184 :   bool warn = close->iostat || close->err;
    2854              : 
    2855         3184 :   if (close->unit == NULL)
    2856              :     {
    2857            1 :       gfc_error ("CLOSE statement at %L requires a UNIT number", where);
    2858            1 :       return false;
    2859              :     }
    2860              : 
    2861         3183 :   if (close->unit->expr_type == EXPR_CONSTANT
    2862         2804 :       && close->unit->ts.type == BT_INTEGER
    2863         2804 :       && mpz_sgn (close->unit->value.integer) < 0)
    2864              :     {
    2865            0 :       gfc_error ("UNIT number in CLOSE statement at %L must be non-negative",
    2866              :                  &close->unit->where);
    2867              :     }
    2868              : 
    2869              :   /* Checks on the STATUS specifier.  */
    2870         3183 :   if (close->status && close->status->expr_type == EXPR_CONSTANT)
    2871              :     {
    2872         1405 :       static const char *status[] = { "KEEP", "DELETE", NULL };
    2873              : 
    2874         1405 :       if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
    2875              :                                       close->status->value.character.string,
    2876              :                                       "CLOSE", warn, &close->status->where))
    2877              :         return false;
    2878              :     }
    2879              : 
    2880              :   return true;
    2881              : }
    2882              : 
    2883              : /* Resolve everything in a gfc_close structure.  */
    2884              : 
    2885              : bool
    2886         3214 : gfc_resolve_close (gfc_close *close, locus *where)
    2887              : {
    2888         3214 :   RESOLVE_TAG (&tag_unit, close->unit);
    2889         3214 :   RESOLVE_TAG (&tag_iomsg, close->iomsg);
    2890         3197 :   RESOLVE_TAG (&tag_iostat, close->iostat);
    2891         3195 :   RESOLVE_TAG (&tag_status, close->status);
    2892              : 
    2893         3184 :   if (!gfc_reference_st_label (close->err, ST_LABEL_TARGET))
    2894              :     return false;
    2895              : 
    2896         3184 :   return check_close_constraints (close, where);
    2897              : }
    2898              : 
    2899              : 
    2900              : /* Free a gfc_filepos structure.  */
    2901              : 
    2902              : void
    2903         2951 : gfc_free_filepos (gfc_filepos *fp)
    2904              : {
    2905         2951 :   gfc_free_expr (fp->unit);
    2906         2951 :   gfc_free_expr (fp->iomsg);
    2907         2951 :   gfc_free_expr (fp->iostat);
    2908         2951 :   free (fp);
    2909         2951 : }
    2910              : 
    2911              : 
    2912              : /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement.  */
    2913              : 
    2914              : static match
    2915         2648 : match_file_element (gfc_filepos *fp)
    2916              : {
    2917         2648 :   match m;
    2918              : 
    2919         2648 :   m = match_etag (&tag_unit, &fp->unit);
    2920         2648 :   if (m != MATCH_NO)
    2921              :     return m;
    2922         2615 :   m = match_etag (&tag_iomsg, &fp->iomsg);
    2923         2615 :   if (m != MATCH_NO)
    2924              :     return m;
    2925         2527 :   m = match_out_tag (&tag_iostat, &fp->iostat);
    2926         2527 :   if (m != MATCH_NO)
    2927              :     return m;
    2928         2449 :   m = match_ltag (&tag_err, &fp->err);
    2929         2449 :   if (m != MATCH_NO)
    2930              :     return m;
    2931              : 
    2932              :   return MATCH_NO;
    2933              : }
    2934              : 
    2935              : 
    2936              : /* Match the second half of the file-positioning statements, REWIND,
    2937              :    BACKSPACE, ENDFILE, or the FLUSH statement.  */
    2938              : 
    2939              : static match
    2940         2951 : match_filepos (gfc_statement st, gfc_exec_op op)
    2941              : {
    2942         2951 :   gfc_filepos *fp;
    2943         2951 :   match m;
    2944              : 
    2945         2951 :   fp = XCNEW (gfc_filepos);
    2946              : 
    2947         2951 :   if (gfc_match_char ('(') == MATCH_NO)
    2948              :     {
    2949          480 :       m = gfc_match_expr (&fp->unit);
    2950          480 :       if (m == MATCH_ERROR)
    2951            0 :         goto cleanup;
    2952          480 :       if (m == MATCH_NO)
    2953            0 :         goto syntax;
    2954              : 
    2955          480 :       goto done;
    2956              :     }
    2957              : 
    2958         2471 :   m = match_file_element (fp);
    2959         2471 :   if (m == MATCH_ERROR)
    2960            8 :     goto cleanup;
    2961         2463 :   if (m == MATCH_NO)
    2962              :     {
    2963         2425 :       m = gfc_match_expr (&fp->unit);
    2964         2425 :       if (m == MATCH_ERROR || m == MATCH_NO)
    2965            8 :         goto syntax;
    2966              :     }
    2967              : 
    2968         2632 :   for (;;)
    2969              :     {
    2970         2632 :       if (gfc_match_char (')') == MATCH_YES)
    2971              :         break;
    2972          177 :       if (gfc_match_char (',') != MATCH_YES)
    2973            0 :         goto syntax;
    2974              : 
    2975          177 :       m = match_file_element (fp);
    2976          177 :       if (m == MATCH_ERROR)
    2977            0 :         goto cleanup;
    2978          177 :       if (m == MATCH_NO)
    2979            0 :         goto syntax;
    2980              :     }
    2981              : 
    2982         2455 : done:
    2983         2935 :   if (gfc_match_eos () != MATCH_YES)
    2984            0 :     goto syntax;
    2985              : 
    2986         2935 :   if (gfc_pure (NULL))
    2987              :     {
    2988            0 :       gfc_error ("%s statement not allowed in PURE procedure at %C",
    2989              :                  gfc_ascii_statement (st));
    2990              : 
    2991            0 :       goto cleanup;
    2992              :     }
    2993              : 
    2994         2935 :   gfc_unset_implicit_pure (NULL);
    2995              : 
    2996         2935 :   new_st.op = op;
    2997         2935 :   new_st.ext.filepos = fp;
    2998         2935 :   return MATCH_YES;
    2999              : 
    3000            8 : syntax:
    3001            8 :   gfc_syntax_error (st);
    3002              : 
    3003           16 : cleanup:
    3004           16 :   gfc_free_filepos (fp);
    3005           16 :   return MATCH_ERROR;
    3006              : }
    3007              : 
    3008              : 
    3009              : bool
    3010         2935 : gfc_resolve_filepos (gfc_filepos *fp, locus *where)
    3011              : {
    3012         2935 :   RESOLVE_TAG (&tag_unit, fp->unit);
    3013         2935 :   RESOLVE_TAG (&tag_iostat, fp->iostat);
    3014         2932 :   RESOLVE_TAG (&tag_iomsg, fp->iomsg);
    3015              : 
    3016         2874 :   if (!fp->unit && (fp->iostat || fp->iomsg || fp->err))
    3017              :     {
    3018            5 :       gfc_error ("UNIT number missing in statement at %L", where);
    3019            5 :       return false;
    3020              :     }
    3021              : 
    3022         2869 :   if (!gfc_reference_st_label (fp->err, ST_LABEL_TARGET))
    3023              :     return false;
    3024              : 
    3025         2869 :   if (fp->unit->expr_type == EXPR_CONSTANT
    3026         2566 :       && fp->unit->ts.type == BT_INTEGER
    3027         2566 :       && mpz_sgn (fp->unit->value.integer) < 0)
    3028              :     {
    3029            0 :       gfc_error ("UNIT number in statement at %L must be non-negative",
    3030              :                  &fp->unit->where);
    3031            0 :       return false;
    3032              :     }
    3033              : 
    3034              :   return true;
    3035              : }
    3036              : 
    3037              : 
    3038              : /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
    3039              :    and the FLUSH statement.  */
    3040              : 
    3041              : match
    3042           75 : gfc_match_endfile (void)
    3043              : {
    3044           75 :   return match_filepos (ST_END_FILE, EXEC_ENDFILE);
    3045              : }
    3046              : 
    3047              : match
    3048          412 : gfc_match_backspace (void)
    3049              : {
    3050          412 :   return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
    3051              : }
    3052              : 
    3053              : match
    3054         2365 : gfc_match_rewind (void)
    3055              : {
    3056         2365 :   return match_filepos (ST_REWIND, EXEC_REWIND);
    3057              : }
    3058              : 
    3059              : match
    3060           99 : gfc_match_flush (void)
    3061              : {
    3062           99 :   if (!gfc_notify_std (GFC_STD_F2003, "FLUSH statement at %C"))
    3063              :     return MATCH_ERROR;
    3064              : 
    3065           99 :   return match_filepos (ST_FLUSH, EXEC_FLUSH);
    3066              : }
    3067              : 
    3068              : /******************** Data Transfer Statements *********************/
    3069              : 
    3070              : /* Return a default unit number.  */
    3071              : 
    3072              : static gfc_expr *
    3073        11907 : default_unit (io_kind k)
    3074              : {
    3075        11907 :   int unit;
    3076              : 
    3077        11907 :   if (k == M_READ)
    3078              :     unit = 5;
    3079              :   else
    3080        11826 :     unit = 6;
    3081              : 
    3082        11907 :   return gfc_get_int_expr (gfc_default_integer_kind, NULL, unit);
    3083              : }
    3084              : 
    3085              : 
    3086              : /* Match a unit specification for a data transfer statement.  */
    3087              : 
    3088              : static match
    3089        26270 : match_dt_unit (io_kind k, gfc_dt *dt)
    3090              : {
    3091        26270 :   gfc_expr *e;
    3092        26270 :   char c;
    3093              : 
    3094        26270 :   if (gfc_match_char ('*') == MATCH_YES)
    3095              :     {
    3096         3969 :       if (dt->io_unit != NULL)
    3097            0 :         goto conflict;
    3098              : 
    3099         3969 :       dt->io_unit = default_unit (k);
    3100              : 
    3101         3969 :       c = gfc_peek_ascii_char ();
    3102         3969 :       if (c == ')')
    3103            0 :         gfc_error_now ("Missing format with default unit at %C");
    3104              : 
    3105              :       return MATCH_YES;
    3106              :     }
    3107              : 
    3108        22301 :   if (gfc_match_expr (&e) == MATCH_YES)
    3109              :     {
    3110        22301 :       if (dt->io_unit != NULL)
    3111              :         {
    3112            0 :           gfc_free_expr (e);
    3113            0 :           goto conflict;
    3114              :         }
    3115              : 
    3116        22301 :       dt->io_unit = e;
    3117        22301 :       return MATCH_YES;
    3118              :     }
    3119              : 
    3120              :   return MATCH_NO;
    3121              : 
    3122            0 : conflict:
    3123            0 :   gfc_error ("Duplicate UNIT specification at %C");
    3124            0 :   return MATCH_ERROR;
    3125              : }
    3126              : 
    3127              : 
    3128              : /* Match a format specification.  */
    3129              : 
    3130              : static match
    3131        30790 : match_dt_format (gfc_dt *dt)
    3132              : {
    3133        30790 :   locus where;
    3134        30790 :   gfc_expr *e;
    3135        30790 :   gfc_st_label *label;
    3136        30790 :   match m;
    3137              : 
    3138        30790 :   where = gfc_current_locus;
    3139              : 
    3140        30790 :   if (gfc_match_char ('*') == MATCH_YES)
    3141              :     {
    3142        15859 :       if (dt->format_expr != NULL || dt->format_label != NULL)
    3143            0 :         goto conflict;
    3144              : 
    3145        15859 :       dt->format_label = &format_asterisk;
    3146        15859 :       return MATCH_YES;
    3147              :     }
    3148              : 
    3149        14931 :   if ((m = gfc_match_st_label (&label)) == MATCH_YES)
    3150              :     {
    3151         1809 :       char c;
    3152              : 
    3153              :       /* Need to check if the format label is actually either an operand
    3154              :          to a user-defined operator or is a kind type parameter.  That is,
    3155              :          print 2.ip.8      ! .ip. is a user-defined operator return CHARACTER.
    3156              :          print 1_'(I0)', i ! 1_'(I0)' is a default character string.  */
    3157              : 
    3158         1809 :       gfc_gobble_whitespace ();
    3159         1809 :       c = gfc_peek_ascii_char ();
    3160         1809 :       if (c == '.' || c == '_')
    3161            2 :         gfc_current_locus = where;
    3162              :       else
    3163              :         {
    3164         1807 :           if (dt->format_expr != NULL || dt->format_label != NULL)
    3165              :             {
    3166            0 :               gfc_free_st_label (label);
    3167            0 :               goto conflict;
    3168              :             }
    3169              : 
    3170         1807 :           if (!gfc_reference_st_label (label, ST_LABEL_FORMAT))
    3171              :             return MATCH_ERROR;
    3172              : 
    3173         1802 :           dt->format_label = label;
    3174         1802 :           return MATCH_YES;
    3175              :         }
    3176              :     }
    3177        13122 :   else if (m == MATCH_ERROR)
    3178              :     /* The label was zero or too large.  Emit the correct diagnosis.  */
    3179              :     return MATCH_ERROR;
    3180              : 
    3181        13122 :   if (gfc_match_expr (&e) == MATCH_YES)
    3182              :     {
    3183        12694 :       if (dt->format_expr != NULL || dt->format_label != NULL)
    3184              :         {
    3185            0 :           gfc_free_expr (e);
    3186            0 :           goto conflict;
    3187              :         }
    3188        12694 :       dt->format_expr = e;
    3189        12694 :       return MATCH_YES;
    3190              :     }
    3191              : 
    3192          428 :   gfc_current_locus = where;    /* The only case where we have to restore */
    3193              : 
    3194          428 :   return MATCH_NO;
    3195              : 
    3196            0 : conflict:
    3197            0 :   gfc_error ("Duplicate format specification at %C");
    3198            0 :   return MATCH_ERROR;
    3199              : }
    3200              : 
    3201              : /* Check for formatted read and write DTIO procedures.  */
    3202              : 
    3203              : static bool
    3204         3058 : dtio_procs_present (gfc_symbol *sym, io_kind k)
    3205              : {
    3206         3058 :   gfc_symbol *derived;
    3207              : 
    3208         3058 :   if (sym && sym->ts.u.derived)
    3209              :     {
    3210         1593 :       if (sym->ts.type == BT_CLASS && CLASS_DATA (sym))
    3211           22 :         derived = CLASS_DATA (sym)->ts.u.derived;
    3212         1571 :       else if (sym->ts.type == BT_DERIVED)
    3213              :         derived = sym->ts.u.derived;
    3214              :       else
    3215              :         return false;
    3216         1174 :       if ((k == M_WRITE || k == M_PRINT) &&
    3217          351 :           (gfc_find_specific_dtio_proc (derived, true, true) != NULL))
    3218              :         return true;
    3219         1240 :       if ((k == M_READ) &&
    3220          472 :           (gfc_find_specific_dtio_proc (derived, false, true) != NULL))
    3221           42 :         return true;
    3222              :     }
    3223              :   return false;
    3224              : }
    3225              : 
    3226              : /* Traverse a namelist that is part of a READ statement to make sure
    3227              :    that none of the variables in the namelist are INTENT(IN).  Returns
    3228              :    nonzero if we find such a variable.  */
    3229              : 
    3230              : static int
    3231          891 : check_namelist (gfc_symbol *sym)
    3232              : {
    3233          891 :   gfc_namelist *p;
    3234              : 
    3235         2957 :   for (p = sym->namelist; p; p = p->next)
    3236         2067 :     if (p->sym->attr.intent == INTENT_IN)
    3237              :       {
    3238            1 :         gfc_error ("Symbol %qs in namelist %qs is INTENT(IN) at %C",
    3239              :                    p->sym->name, sym->name);
    3240            1 :         return 1;
    3241              :       }
    3242              : 
    3243              :   return 0;
    3244              : }
    3245              : 
    3246              : 
    3247              : /* Match a single data transfer element.  */
    3248              : 
    3249              : static match
    3250        55134 : match_dt_element (io_kind k, gfc_dt *dt)
    3251              : {
    3252        55134 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    3253        55134 :   gfc_symbol *sym;
    3254        55134 :   match m;
    3255        55134 :   locus where;
    3256              : 
    3257        55134 :   if (gfc_match (" unit =") == MATCH_YES)
    3258              :     {
    3259         1211 :       m = match_dt_unit (k, dt);
    3260         1211 :       if (m != MATCH_NO)
    3261              :         return m;
    3262              :     }
    3263              : 
    3264        53923 :   if (gfc_match (" fmt =") == MATCH_YES)
    3265              :     {
    3266         2006 :       m = match_dt_format (dt);
    3267         2006 :       if (m != MATCH_NO)
    3268              :         return m;
    3269              :     }
    3270              : 
    3271        51917 :   where = gfc_current_locus;
    3272        51917 :   if (gfc_match (" nml = %n", name) == MATCH_YES)
    3273              :     {
    3274          801 :       if (dt->namelist != NULL)
    3275              :         {
    3276            0 :           gfc_error ("Duplicate NML specification at %C");
    3277            0 :           return MATCH_ERROR;
    3278              :         }
    3279              : 
    3280          801 :       if (gfc_find_symbol (name, NULL, 1, &sym))
    3281              :         return MATCH_ERROR;
    3282              : 
    3283          801 :       if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
    3284              :         {
    3285            0 :           gfc_error ("Symbol %qs at %C must be a NAMELIST group name",
    3286              :                      sym != NULL ? sym->name : name);
    3287            0 :           return MATCH_ERROR;
    3288              :         }
    3289              : 
    3290          801 :       dt->namelist = sym;
    3291          801 :       dt->nml_where = where;
    3292          801 :       if (k == M_READ && check_namelist (sym))
    3293              :         return MATCH_ERROR;
    3294              : 
    3295          801 :       return MATCH_YES;
    3296              :     }
    3297              : 
    3298        51116 :   m = match_etag (&tag_e_async, &dt->asynchronous);
    3299        51116 :   if (m != MATCH_NO)
    3300              :     return m;
    3301        50887 :   m = match_etag (&tag_e_blank, &dt->blank);
    3302        50887 :   if (m != MATCH_NO)
    3303              :     return m;
    3304        50846 :   m = match_etag (&tag_e_delim, &dt->delim);
    3305        50846 :   if (m != MATCH_NO)
    3306              :     return m;
    3307        50817 :   m = match_etag (&tag_e_pad, &dt->pad);
    3308        50817 :   if (m != MATCH_NO)
    3309              :     return m;
    3310        50712 :   m = match_etag (&tag_e_sign, &dt->sign);
    3311        50712 :   if (m != MATCH_NO)
    3312              :     return m;
    3313        50673 :   m = match_etag (&tag_e_round, &dt->round);
    3314        50673 :   if (m != MATCH_NO)
    3315              :     return m;
    3316        50334 :   m = match_out_tag (&tag_id, &dt->id);
    3317        50334 :   if (m != MATCH_NO)
    3318              :     return m;
    3319        50313 :   m = match_etag (&tag_e_decimal, &dt->decimal);
    3320        50313 :   if (m != MATCH_NO)
    3321              :     return m;
    3322        50145 :   m = match_etag (&tag_rec, &dt->rec);
    3323        50145 :   if (m != MATCH_NO)
    3324              :     return m;
    3325        49645 :   m = match_etag (&tag_spos, &dt->pos);
    3326        49645 :   if (m != MATCH_NO)
    3327              :     return m;
    3328        49467 :   m = match_etag (&tag_iomsg, &dt->iomsg);
    3329        49467 :   if (m != MATCH_NO)
    3330              :     return m;
    3331              : 
    3332        49012 :   m = match_out_tag (&tag_iostat, &dt->iostat);
    3333        49012 :   if (m != MATCH_NO)
    3334              :     return m;
    3335        47215 :   m = match_ltag (&tag_err, &dt->err);
    3336        47215 :   if (m == MATCH_YES)
    3337          250 :     dt->err_where = gfc_current_locus;
    3338        47215 :   if (m != MATCH_NO)
    3339              :     return m;
    3340        46965 :   m = match_etag (&tag_advance, &dt->advance);
    3341        46965 :   if (m != MATCH_NO)
    3342              :     return m;
    3343        46570 :   m = match_out_tag (&tag_size, &dt->size);
    3344        46570 :   if (m != MATCH_NO)
    3345              :     return m;
    3346              : 
    3347        46505 :   m = match_ltag (&tag_end, &dt->end);
    3348        46505 :   if (m == MATCH_YES)
    3349              :     {
    3350          562 :       if (k == M_WRITE)
    3351              :        {
    3352            4 :          gfc_error ("END tag at %C not allowed in output statement");
    3353            4 :          return MATCH_ERROR;
    3354              :        }
    3355          558 :       dt->end_where = gfc_current_locus;
    3356              :     }
    3357        46501 :   if (m != MATCH_NO)
    3358              :     return m;
    3359              : 
    3360        45943 :   m = match_ltag (&tag_eor, &dt->eor);
    3361        45943 :   if (m == MATCH_YES)
    3362           34 :     dt->eor_where = gfc_current_locus;
    3363              :   if (m != MATCH_NO)
    3364              :     return m;
    3365              : 
    3366              :   return MATCH_NO;
    3367              : }
    3368              : 
    3369              : 
    3370              : /* Free a data transfer structure and everything below it.  */
    3371              : 
    3372              : void
    3373        68099 : gfc_free_dt (gfc_dt *dt)
    3374              : {
    3375        68099 :   if (dt == NULL)
    3376              :     return;
    3377              : 
    3378        34119 :   gfc_free_expr (dt->io_unit);
    3379        34119 :   gfc_free_expr (dt->format_expr);
    3380        34119 :   gfc_free_expr (dt->rec);
    3381        34119 :   gfc_free_expr (dt->advance);
    3382        34119 :   gfc_free_expr (dt->iomsg);
    3383        34119 :   gfc_free_expr (dt->iostat);
    3384        34119 :   gfc_free_expr (dt->size);
    3385        34119 :   gfc_free_expr (dt->pad);
    3386        34119 :   gfc_free_expr (dt->delim);
    3387        34119 :   gfc_free_expr (dt->sign);
    3388        34119 :   gfc_free_expr (dt->round);
    3389        34119 :   gfc_free_expr (dt->blank);
    3390        34119 :   gfc_free_expr (dt->decimal);
    3391        34119 :   gfc_free_expr (dt->pos);
    3392        34119 :   gfc_free_expr (dt->dt_io_kind);
    3393              :   /* dt->extra_comma is a link to dt_io_kind if it is set.  */
    3394        34119 :   free (dt);
    3395              : }
    3396              : 
    3397              : 
    3398              : static const char *
    3399              : io_kind_name (io_kind k);
    3400              : 
    3401              : static bool
    3402              : check_io_constraints (io_kind k, gfc_dt *dt, gfc_code *io_code,
    3403              :                       locus *spec_end);
    3404              : 
    3405              : /* Resolve everything in a gfc_dt structure.  */
    3406              : 
    3407              : bool
    3408        34046 : gfc_resolve_dt (gfc_code *dt_code, gfc_dt *dt, locus *loc)
    3409              : {
    3410        34046 :   gfc_expr *e;
    3411        34046 :   io_kind k;
    3412        34046 :   bool internal_unit;
    3413              : 
    3414              :   /* This is set in any case.  */
    3415        34046 :   gcc_assert (dt->dt_io_kind);
    3416        34046 :   k = dt->dt_io_kind->value.iokind;
    3417              : 
    3418        34046 :   RESOLVE_TAG (&tag_format, dt->format_expr);
    3419        34018 :   RESOLVE_TAG (&tag_rec, dt->rec);
    3420        34018 :   RESOLVE_TAG (&tag_spos, dt->pos);
    3421        34018 :   RESOLVE_TAG (&tag_advance, dt->advance);
    3422        34015 :   RESOLVE_TAG (&tag_id, dt->id);
    3423        34015 :   RESOLVE_TAG (&tag_iomsg, dt->iomsg);
    3424        33986 :   RESOLVE_TAG (&tag_iostat, dt->iostat);
    3425        33983 :   RESOLVE_TAG (&tag_size, dt->size);
    3426        33979 :   RESOLVE_TAG (&tag_e_pad, dt->pad);
    3427        33957 :   RESOLVE_TAG (&tag_e_delim, dt->delim);
    3428        33935 :   RESOLVE_TAG (&tag_e_sign, dt->sign);
    3429        33913 :   RESOLVE_TAG (&tag_e_round, dt->round);
    3430        33891 :   RESOLVE_TAG (&tag_e_blank, dt->blank);
    3431        33869 :   RESOLVE_TAG (&tag_e_decimal, dt->decimal);
    3432        33849 :   RESOLVE_TAG (&tag_e_async, dt->asynchronous);
    3433              : 
    3434              :   /* Check I/O constraints.
    3435              :      To validate NAMELIST we need to check if we were also given an I/O list,
    3436              :      which is stored in code->block->next with op EXEC_TRANSFER.
    3437              :      Note that the I/O list was already resolved from resolve_transfer.  */
    3438        33826 :   gfc_code *io_code = NULL;
    3439        33826 :   if (dt_code && dt_code->block && dt_code->block->next
    3440        33826 :       && dt_code->block->next->op == EXEC_TRANSFER)
    3441        33826 :     io_code = dt_code->block->next;
    3442              : 
    3443        33826 :   if (!check_io_constraints (k, dt, io_code, loc))
    3444              :     return false;
    3445              : 
    3446        33767 :   e = dt->io_unit;
    3447        33767 :   if (e == NULL)
    3448              :     {
    3449            2 :       gfc_error ("UNIT not specified at %L", loc);
    3450            2 :       return false;
    3451              :     }
    3452              : 
    3453        33765 :   if (e->symtree && e->symtree->n.sym->attr.flavor == FL_PARAMETER
    3454          365 :       && e->ts.type == BT_CHARACTER)
    3455              :     {
    3456            4 :       gfc_error ("UNIT specification at %L must "
    3457              :       "not be a character PARAMETER", &e->where);
    3458            4 :       return false;
    3459              :     }
    3460              : 
    3461        33761 :   internal_unit = false;
    3462        33761 :   if (gfc_resolve_expr (e)
    3463        33761 :       && (e->ts.type != BT_INTEGER
    3464        10216 :           && (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_VARIABLE)))
    3465              :     {
    3466              :       /* If there is no extra comma signifying the "format" form of the IO
    3467              :          statement, then this must be an error.  */
    3468            2 :       if (!dt->extra_comma)
    3469              :         {
    3470            1 :           gfc_error ("UNIT specification at %L must be an INTEGER expression "
    3471              :                      "or a CHARACTER variable", &e->where);
    3472            1 :           return false;
    3473              :         }
    3474              :       else
    3475              :         {
    3476              :           /* At this point, we have an extra comma.  If io_unit has arrived as
    3477              :              type character, we assume its really the "format" form of the I/O
    3478              :              statement.  We set the io_unit to the default unit and format to
    3479              :              the character expression.  See F95 Standard section 9.4.  */
    3480            1 :           if (e->ts.type == BT_CHARACTER && (k == M_READ || k == M_PRINT))
    3481              :             {
    3482            0 :               dt->format_expr = dt->io_unit;
    3483            0 :               dt->io_unit = default_unit (k);
    3484              : 
    3485              :               /* Nullify this pointer now so that a warning/error is not
    3486              :                  triggered below for the "Extension".  */
    3487            0 :               dt->extra_comma = NULL;
    3488              :             }
    3489              : 
    3490            1 :           if (k == M_WRITE)
    3491              :             {
    3492            1 :               gfc_error ("Invalid form of WRITE statement at %L, UNIT required",
    3493            1 :                          &dt->extra_comma->where);
    3494            1 :               return false;
    3495              :             }
    3496              :         }
    3497              :     }
    3498              : 
    3499        33759 :   if (e->ts.type == BT_CHARACTER)
    3500              :     {
    3501        10214 :       internal_unit = true;
    3502        10214 :       if (gfc_has_vector_index (e))
    3503              :         {
    3504            3 :           gfc_error ("Internal unit with vector subscript at %L", &e->where);
    3505            3 :           return false;
    3506              :         }
    3507              : 
    3508              :       /* If we are writing, make sure the internal unit can be changed.  */
    3509        10211 :       gcc_assert (k != M_PRINT);
    3510        10211 :       if (k == M_WRITE)
    3511              :         {
    3512         8187 :           if (!gfc_check_vardef_context (e, false, false, false,
    3513         8187 :                                         _("internal unit in WRITE")))
    3514              :             return false;
    3515              : 
    3516         8186 :           gfc_expr_set_at (e, &e->where, VALUE_VARDEF);
    3517              :         }
    3518              :       else
    3519         2024 :         gfc_value_used_expr (e, VALUE_USED);
    3520              :     }
    3521              : 
    3522        33755 :   if (e->rank && e->ts.type != BT_CHARACTER)
    3523              :     {
    3524            1 :       gfc_error ("External IO UNIT cannot be an array at %L", &e->where);
    3525            1 :       return false;
    3526              :     }
    3527              : 
    3528        33754 :   if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_INTEGER
    3529        21765 :       && mpz_sgn (e->value.integer) < 0)
    3530              :     {
    3531            0 :       gfc_error ("UNIT number in statement at %L must be non-negative",
    3532              :                  &e->where);
    3533            0 :       return false;
    3534              :     }
    3535              : 
    3536        33754 :   if (!internal_unit)
    3537        23544 :     gfc_value_used_expr (e, VALUE_USED);
    3538              : 
    3539              :   /* If we are reading and have a namelist, check that all namelist symbols
    3540              :      can appear in a variable definition context.  */
    3541        33754 :   if (dt->namelist)
    3542              :     {
    3543         1225 :       gfc_namelist* n;
    3544         4278 :       for (n = dt->namelist->namelist; n; n = n->next)
    3545              :         {
    3546         3059 :           gfc_expr* e;
    3547         3059 :           bool t;
    3548              : 
    3549         3059 :           e = gfc_get_variable_expr (gfc_find_sym_in_symtree (n->sym));
    3550              : 
    3551         3059 :           if (k == M_READ)
    3552              :             {
    3553         2065 :               t = gfc_check_vardef_context (e, false, false, false, NULL);
    3554         2065 :               gfc_free_expr (e);
    3555              : 
    3556         2065 :               if (!t)
    3557              :                 {
    3558            1 :                   gfc_error ("NAMELIST %qs in READ statement at %L contains"
    3559              :                              " the symbol %qs which may not appear in a"
    3560              :                              " variable definition context",
    3561            1 :                              dt->namelist->name, loc, n->sym->name);
    3562            1 :                   return false;
    3563              :                 }
    3564         2064 :               gfc_value_set_at (n->sym, &dt->nml_where, VALUE_READ);
    3565              :             }
    3566          994 :           else if (k == M_WRITE || k == M_PRINT)
    3567              :             {
    3568          994 :               e->where = dt->nml_where;
    3569          994 :               gfc_value_used_expr (e,  VALUE_USED);
    3570          994 :               gfc_free_expr (e);
    3571              :             }
    3572              : 
    3573         3058 :           n->sym->attr.referenced = 1;
    3574              : 
    3575         3058 :           t = dtio_procs_present (n->sym, k);
    3576              : 
    3577         3058 :           if (n->sym->ts.type == BT_CLASS && !t)
    3578              :             {
    3579            3 :               gfc_error ("NAMELIST object %qs in namelist %qs at %L is "
    3580              :                          "polymorphic and requires a defined input/output "
    3581            3 :                          "procedure", n->sym->name, dt->namelist->name, loc);
    3582            3 :               return false;
    3583              :             }
    3584              : 
    3585         3055 :           if ((n->sym->ts.type == BT_DERIVED)
    3586          801 :               && (n->sym->ts.u.derived->attr.alloc_comp
    3587          799 :                   || n->sym->ts.u.derived->attr.pointer_comp))
    3588              :             {
    3589            2 :               if (!gfc_notify_std (GFC_STD_F2003, "NAMELIST object %qs in "
    3590              :                                    "namelist %qs at %L with ALLOCATABLE "
    3591              :                                    "or POINTER components", n->sym->name,
    3592            2 :                                    dt->namelist->name, loc))
    3593              :                 return false;
    3594              : 
    3595            2 :               if (!t)
    3596              :                 {
    3597            2 :                   gfc_error ("NAMELIST object %qs in namelist %qs at %L has "
    3598              :                              "ALLOCATABLE or POINTER components and thus requires "
    3599            2 :                              "a defined input/output procedure", n->sym->name,
    3600            2 :                              dt->namelist->name, loc);
    3601            2 :                   return false;
    3602              :                 }
    3603              :             }
    3604              :         }
    3605              :     }
    3606              : 
    3607        33748 :   if (dt->extra_comma
    3608        33748 :       && !gfc_notify_std (GFC_STD_LEGACY, "Comma before i/o item list at %L",
    3609              :                           &dt->extra_comma->where))
    3610              :     return false;
    3611              : 
    3612        33748 :   if (dt->err)
    3613              :     {
    3614          250 :       if (!gfc_reference_st_label (dt->err, ST_LABEL_TARGET))
    3615              :         return false;
    3616          250 :       if (dt->err->defined == ST_LABEL_UNKNOWN)
    3617              :         {
    3618            1 :           gfc_error ("ERR tag label %d at %L not defined",
    3619              :                       dt->err->value, &dt->err_where);
    3620            1 :           return false;
    3621              :         }
    3622              :     }
    3623              : 
    3624        33747 :   if (dt->end)
    3625              :     {
    3626          557 :       if (!gfc_reference_st_label (dt->end, ST_LABEL_TARGET))
    3627              :         return false;
    3628          557 :       if (dt->end->defined == ST_LABEL_UNKNOWN)
    3629              :         {
    3630            1 :           gfc_error ("END tag label %d at %L not defined",
    3631              :                       dt->end->value, &dt->end_where);
    3632            1 :           return false;
    3633              :         }
    3634              :     }
    3635              : 
    3636        33746 :   if (dt->eor)
    3637              :     {
    3638           31 :       if (!gfc_reference_st_label (dt->eor, ST_LABEL_TARGET))
    3639              :         return false;
    3640           31 :       if (dt->eor->defined == ST_LABEL_UNKNOWN)
    3641              :         {
    3642            1 :           gfc_error ("EOR tag label %d at %L not defined",
    3643              :                       dt->eor->value, &dt->eor_where);
    3644            1 :           return false;
    3645              :         }
    3646              :     }
    3647              : 
    3648              :   /* Check the format label actually exists.  */
    3649        33745 :   if (dt->format_label && dt->format_label != &format_asterisk
    3650         1804 :       && dt->format_label->defined == ST_LABEL_UNKNOWN)
    3651              :     {
    3652            7 :       gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
    3653              :                  loc);
    3654            7 :       return false;
    3655              :     }
    3656              : 
    3657              :   /* A labeled FORMAT's width warnings apply only to output; issue them
    3658              :      once per FORMAT.  */
    3659        33738 :   if (warn_format_width && k != M_READ
    3660          151 :       && dt->format_label && dt->format_label != &format_asterisk
    3661            5 :       && !dt->format_label->output_width_checked)
    3662              :     {
    3663            4 :       dt->format_label->output_width_checked = true;
    3664            4 :       check_format_string (dt->format_label->format, false, FMT_CHECK_WIDTH);
    3665              :     }
    3666              : 
    3667              :   return true;
    3668              : }
    3669              : 
    3670              : 
    3671              : /* Given an io_kind, return its name.  */
    3672              : 
    3673              : static const char *
    3674          811 : io_kind_name (io_kind k)
    3675              : {
    3676          811 :   const char *name;
    3677              : 
    3678          811 :   switch (k)
    3679              :     {
    3680              :     case M_READ:
    3681              :       name = "READ";
    3682              :       break;
    3683          499 :     case M_WRITE:
    3684          499 :       name = "WRITE";
    3685          499 :       break;
    3686           26 :     case M_PRINT:
    3687           26 :       name = "PRINT";
    3688           26 :       break;
    3689            0 :     case M_INQUIRE:
    3690            0 :       name = "INQUIRE";
    3691            0 :       break;
    3692            0 :     default:
    3693            0 :       gfc_internal_error ("io_kind_name(): bad I/O-kind");
    3694              :     }
    3695              : 
    3696          811 :   return name;
    3697              : }
    3698              : 
    3699              : 
    3700              : /* Match an IO iteration statement of the form:
    3701              : 
    3702              :    ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
    3703              : 
    3704              :    which is equivalent to a single IO element.  This function is
    3705              :    mutually recursive with match_io_element().  */
    3706              : 
    3707              : static match match_io_element (io_kind, gfc_code **);
    3708              : 
    3709              : static match
    3710        49264 : match_io_iterator (io_kind k, gfc_code **result)
    3711              : {
    3712        49264 :   gfc_code *head, *tail, *new_code;
    3713        49264 :   gfc_iterator *iter;
    3714        49264 :   locus old_loc;
    3715        49264 :   match m;
    3716        49264 :   int n;
    3717              : 
    3718        49264 :   iter = NULL;
    3719        49264 :   head = NULL;
    3720        49264 :   old_loc = gfc_current_locus;
    3721              : 
    3722        49264 :   if (gfc_match_char ('(') != MATCH_YES)
    3723              :     return MATCH_NO;
    3724              : 
    3725          764 :   m = match_io_element (k, &head);
    3726          764 :   tail = head;
    3727              : 
    3728          764 :   if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
    3729              :     {
    3730           93 :       m = MATCH_NO;
    3731           93 :       goto cleanup;
    3732              :     }
    3733              : 
    3734              :   /* Can't be anything but an IO iterator.  Build a list.  */
    3735          671 :   iter = gfc_get_iterator ();
    3736              : 
    3737          671 :   for (n = 1;; n++)
    3738              :     {
    3739          723 :       m = gfc_match_iterator (iter, 0);
    3740          723 :       if (m == MATCH_ERROR)
    3741            0 :         goto cleanup;
    3742          723 :       if (m == MATCH_YES)
    3743              :         {
    3744          654 :           gfc_check_do_variable (iter->var->symtree);
    3745          654 :           break;
    3746              :         }
    3747              : 
    3748           69 :       m = match_io_element (k, &new_code);
    3749           69 :       if (m == MATCH_ERROR)
    3750            0 :         goto cleanup;
    3751           69 :       if (m == MATCH_NO)
    3752              :         {
    3753              :           if (n > 2)
    3754              :             goto syntax;
    3755              :           goto cleanup;
    3756              :         }
    3757              : 
    3758           69 :       tail = gfc_append_code (tail, new_code);
    3759              : 
    3760           69 :       if (gfc_match_char (',') != MATCH_YES)
    3761              :         {
    3762           17 :           if (n > 2)
    3763            0 :             goto syntax;
    3764           17 :           m = MATCH_NO;
    3765           17 :           goto cleanup;
    3766              :         }
    3767              :     }
    3768              : 
    3769          654 :   if (gfc_match_char (')') != MATCH_YES)
    3770            1 :     goto syntax;
    3771              : 
    3772          653 :   new_code = gfc_get_code (EXEC_DO);
    3773          653 :   new_code->ext.iterator = iter;
    3774              : 
    3775          653 :   new_code->block = gfc_get_code (EXEC_DO);
    3776          653 :   new_code->block->next = head;
    3777              : 
    3778          653 :   *result = new_code;
    3779          653 :   return MATCH_YES;
    3780              : 
    3781            1 : syntax:
    3782            1 :   gfc_error ("Syntax error in I/O iterator at %C");
    3783            1 :   m = MATCH_ERROR;
    3784              : 
    3785          111 : cleanup:
    3786          111 :   gfc_free_iterator (iter, 1);
    3787          111 :   gfc_free_statements (head);
    3788          111 :   gfc_current_locus = old_loc;
    3789          111 :   return m;
    3790              : }
    3791              : 
    3792              : 
    3793              : /* Match a single element of an IO list, which is either a single
    3794              :    expression or an IO Iterator.  */
    3795              : 
    3796              : static match
    3797        49264 : match_io_element (io_kind k, gfc_code **cpp)
    3798              : {
    3799        49264 :   gfc_expr *expr;
    3800        49264 :   gfc_code *cp;
    3801        49264 :   match m;
    3802              : 
    3803        49264 :   expr = NULL;
    3804              : 
    3805        49264 :   m = match_io_iterator (k, cpp);
    3806        49264 :   if (m == MATCH_YES)
    3807              :     return MATCH_YES;
    3808              : 
    3809        48611 :   if (k == M_READ)
    3810              :     {
    3811         7783 :       m = gfc_match_variable (&expr, 0);
    3812         7783 :       if (m == MATCH_NO)
    3813              :         {
    3814            0 :           gfc_error ("Expecting variable in READ statement at %C");
    3815            0 :           m = MATCH_ERROR;
    3816              :         }
    3817              : 
    3818         7783 :       if (m == MATCH_YES && expr->expr_type == EXPR_CONSTANT)
    3819              :         {
    3820            1 :           gfc_error ("Expecting variable or io-implied-do in READ statement "
    3821              :                    "at %L", &expr->where);
    3822            1 :           m = MATCH_ERROR;
    3823              :         }
    3824              : 
    3825         7783 :       if (m == MATCH_YES
    3826         7782 :           && expr->expr_type == EXPR_VARIABLE
    3827         7782 :           && expr->symtree->n.sym->attr.external)
    3828              :         {
    3829            2 :           gfc_error ("Expecting variable or io-implied-do at %L",
    3830              :                      &expr->where);
    3831            2 :           m = MATCH_ERROR;
    3832              :         }
    3833              :     }
    3834              :   else
    3835              :     {
    3836        40828 :       m = gfc_match_expr (&expr);
    3837        40828 :       if (m == MATCH_NO)
    3838           41 :         gfc_error ("Expected expression in %s statement at %C",
    3839              :                    io_kind_name (k));
    3840              : 
    3841        40828 :       if (m == MATCH_YES && expr->ts.type == BT_BOZ)
    3842              :         {
    3843            6 :           if (gfc_invalid_boz (G_("BOZ literal constant at %L cannot appear in"
    3844              :                                 " an output IO list"), &gfc_current_locus))
    3845              :             return MATCH_ERROR;
    3846            3 :           if (!gfc_boz2int (expr, gfc_max_integer_kind))
    3847              :             return MATCH_ERROR;
    3848        48608 :         };
    3849              :     }
    3850              : 
    3851        48608 :   if (m == MATCH_YES && k == M_READ && gfc_check_do_variable (expr->symtree))
    3852              :     m = MATCH_ERROR;
    3853              : 
    3854        48607 :   if (m != MATCH_YES)
    3855              :     {
    3856          144 :       gfc_free_expr (expr);
    3857          144 :       return MATCH_ERROR;
    3858              :     }
    3859              : 
    3860        48464 :   cp = gfc_get_code (EXEC_TRANSFER);
    3861        48464 :   cp->expr1 = expr;
    3862        48464 :   if (k != M_INQUIRE)
    3863        48310 :     cp->ext.dt = current_dt;
    3864              : 
    3865        48464 :   *cpp = cp;
    3866        48464 :   return MATCH_YES;
    3867              : }
    3868              : 
    3869              : 
    3870              : /* Match an I/O list, building gfc_code structures as we go.  */
    3871              : 
    3872              : static match
    3873        32055 : match_io_list (io_kind k, gfc_code **head_p)
    3874              : {
    3875        32055 :   gfc_code *head, *tail, *new_code;
    3876        32055 :   match m;
    3877              : 
    3878        32055 :   *head_p = head = tail = NULL;
    3879        32055 :   if (gfc_match_eos () == MATCH_YES)
    3880              :     return MATCH_YES;
    3881              : 
    3882        48431 :   for (;;)
    3883              :     {
    3884        48431 :       m = match_io_element (k, &new_code);
    3885        48431 :       if (m == MATCH_ERROR)
    3886          106 :         goto cleanup;
    3887        48325 :       if (m == MATCH_NO)
    3888              :         goto syntax;
    3889              : 
    3890        48325 :       tail = gfc_append_code (tail, new_code);
    3891        48325 :       if (head == NULL)
    3892        31955 :         head = new_code;
    3893              : 
    3894        48325 :       if (gfc_match_eos () == MATCH_YES)
    3895              :         break;
    3896        16385 :       if (gfc_match_char (',') != MATCH_YES)
    3897            9 :         goto syntax;
    3898              :     }
    3899              : 
    3900        31940 :   *head_p = head;
    3901        31940 :   return MATCH_YES;
    3902              : 
    3903            9 : syntax:
    3904            9 :   gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
    3905              : 
    3906          115 : cleanup:
    3907          115 :   gfc_free_statements (head);
    3908          115 :   return MATCH_ERROR;
    3909              : }
    3910              : 
    3911              : 
    3912              : /* Attach the data transfer end node.  */
    3913              : 
    3914              : static void
    3915        34140 : terminate_io (gfc_code *io_code)
    3916              : {
    3917        34140 :   gfc_code *c;
    3918              : 
    3919        34140 :   if (io_code == NULL)
    3920         2235 :     io_code = new_st.block;
    3921              : 
    3922        34140 :   c = gfc_get_code (EXEC_DT_END);
    3923              : 
    3924              :   /* Point to structure that is already there */
    3925        34140 :   c->ext.dt = new_st.ext.dt;
    3926        34140 :   gfc_append_code (io_code, c);
    3927        34140 : }
    3928              : 
    3929              : 
    3930              : /* Check the constraints for a data transfer statement.  The majority of the
    3931              :    constraints appearing in 9.4 of the standard appear here.
    3932              : 
    3933              :    Tag expressions are already resolved by resolve_tag, which includes
    3934              :    verifying the type, that they are scalar, and verifying that BT_CHARACTER
    3935              :    tags are of default kind.  */
    3936              : 
    3937              : static bool
    3938        33826 : check_io_constraints (io_kind k, gfc_dt *dt, gfc_code *io_code,
    3939              :                       locus *spec_end)
    3940              : {
    3941              : #define io_constraint(condition, msg, where)\
    3942              : if (condition) \
    3943              :   {\
    3944              :     if (GFC_LOCUS_IS_SET (*where))\
    3945              :       gfc_error ((msg), (where));\
    3946              :     else\
    3947              :       gfc_error ((msg), spec_end);\
    3948              :     return false;\
    3949              :   }
    3950              : 
    3951        33826 :   gfc_expr *expr;
    3952        33826 :   gfc_symbol *sym = NULL;
    3953        33826 :   bool warn, unformatted;
    3954              : 
    3955        33826 :   warn = dt->err || dt->iostat;
    3956        21203 :   unformatted = dt->format_expr == NULL && dt->format_label == NULL
    3957        37497 :                 && dt->namelist == NULL;
    3958              : 
    3959        33826 :   expr = dt->io_unit;
    3960        33826 :   if (expr && expr->expr_type == EXPR_VARIABLE
    3961        12003 :       && expr->ts.type == BT_CHARACTER)
    3962              :     {
    3963        10221 :       sym = expr->symtree->n.sym;
    3964              : 
    3965        10221 :       io_constraint (k == M_WRITE && sym->attr.intent == INTENT_IN,
    3966              :                      "Internal file at %L must not be INTENT(IN)",
    3967        10221 :                      &expr->where);
    3968              : 
    3969        10221 :       io_constraint (gfc_has_vector_index (dt->io_unit),
    3970              :                      "Internal file incompatible with vector subscript at %L",
    3971        10221 :                      &expr->where);
    3972              : 
    3973        10221 :       io_constraint (dt->rec != NULL,
    3974              :                      "REC tag at %L is incompatible with internal file",
    3975        10220 :                      &dt->rec->where);
    3976              : 
    3977        10220 :       io_constraint (dt->pos != NULL,
    3978              :                      "POS tag at %L is incompatible with internal file",
    3979        10219 :                      &dt->pos->where);
    3980              : 
    3981        10219 :       io_constraint (unformatted,
    3982              :                      "Unformatted I/O not allowed with internal unit at %L",
    3983        10218 :                      &dt->io_unit->where);
    3984              : 
    3985        10218 :       io_constraint (dt->asynchronous != NULL,
    3986              :                      "ASYNCHRONOUS tag at %L not allowed with internal file",
    3987        10218 :                      &dt->asynchronous->where);
    3988              : 
    3989        10218 :       if (dt->namelist != NULL)
    3990              :         {
    3991          260 :           if (!gfc_notify_std (GFC_STD_F2003, "Internal file at %L with "
    3992              :                                "namelist", &expr->where))
    3993              :             return false;
    3994              :         }
    3995              : 
    3996        10217 :       io_constraint (dt->advance != NULL,
    3997              :                      "ADVANCE tag at %L is incompatible with internal file",
    3998              :                      &dt->advance->where);
    3999              :     }
    4000              : 
    4001        33819 :   if (expr && expr->ts.type != BT_CHARACTER)
    4002              :     {
    4003              : 
    4004        23600 :       if (gfc_pure (NULL) && (k == M_READ || k == M_WRITE))
    4005              :         {
    4006            0 :           gfc_error ("IO UNIT in %s statement at %L must be "
    4007              :                      "an internal file in a PURE procedure",
    4008              :                      io_kind_name (k), &expr->where);
    4009            0 :           return false;
    4010              :         }
    4011              : 
    4012        23600 :       if (k == M_READ || k == M_WRITE)
    4013        15811 :         gfc_unset_implicit_pure (NULL);
    4014              :     }
    4015              : 
    4016        33821 :   if (dt->asynchronous)
    4017              :     {
    4018          206 :       int num = -1;
    4019          206 :       static const char * asynchronous[] = { "YES", "NO", NULL };
    4020              : 
    4021              :       /* Note: gfc_reduce_init_expr reports an error if not init-expr.  */
    4022          206 :       if (!gfc_reduce_init_expr (dt->asynchronous))
    4023            7 :         return false;
    4024              : 
    4025          201 :       if (!compare_to_allowed_values
    4026          201 :                 ("ASYNCHRONOUS", asynchronous, NULL, NULL,
    4027              :                  dt->asynchronous->value.character.string,
    4028          201 :                  io_kind_name (k), warn, &dt->asynchronous->where, &num))
    4029              :         return false;
    4030              : 
    4031          199 :       gcc_checking_assert (num != -1);
    4032              : 
    4033              :       /* For "YES", mark related symbols as asynchronous.  */
    4034          199 :       if (num == 0)
    4035              :         {
    4036              :           /* SIZE variable.  */
    4037          195 :           if (dt->size)
    4038            0 :             dt->size->symtree->n.sym->attr.asynchronous = 1;
    4039              : 
    4040              :           /* Variables in a NAMELIST.  */
    4041          195 :           if (dt->namelist)
    4042            4 :             for (gfc_namelist *nl = dt->namelist->namelist; nl; nl = nl->next)
    4043            3 :               nl->sym->attr.asynchronous = 1;
    4044              : 
    4045              :           /* Variables in an I/O list.  */
    4046          430 :           for (gfc_code *xfer = io_code; xfer && xfer->op == EXEC_TRANSFER;
    4047          235 :                xfer = xfer->next)
    4048              :             {
    4049          235 :               gfc_expr *expr = xfer->expr1;
    4050          470 :               while (expr != NULL && expr->expr_type == EXPR_OP
    4051          241 :                      && expr->value.op.op == INTRINSIC_PARENTHESES)
    4052            0 :                 expr = expr->value.op.op1;
    4053              : 
    4054          235 :               if (expr && expr->expr_type == EXPR_VARIABLE)
    4055          157 :                 expr->symtree->n.sym->attr.asynchronous = 1;
    4056              :             }
    4057              :         }
    4058              :     }
    4059              : 
    4060        33814 :   if (dt->id)
    4061              :     {
    4062           21 :       bool not_yes
    4063           21 :         = !dt->asynchronous
    4064           20 :           || gfc_wide_strlen (dt->asynchronous->value.character.string) != 3
    4065           40 :           || gfc_wide_strncasecmp (dt->asynchronous->value.character.string,
    4066              :                                    "yes", 3) != 0;
    4067            2 :       io_constraint (not_yes,
    4068              :                      "ID= specifier at %L must be with ASYNCHRONOUS='yes' "
    4069              :                      "specifier", &dt->id->where);
    4070              :     }
    4071              : 
    4072        33812 :   if (dt->decimal)
    4073              :     {
    4074          145 :       if (!gfc_notify_std (GFC_STD_F2003, "DECIMAL= at %L "
    4075              :                            "not allowed in Fortran 95", &dt->decimal->where))
    4076              :         return false;
    4077              : 
    4078          145 :       if (dt->decimal->expr_type == EXPR_CONSTANT)
    4079              :         {
    4080          127 :           static const char * decimal[] = { "COMMA", "POINT", NULL };
    4081              : 
    4082          127 :           if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
    4083              :                                           dt->decimal->value.character.string,
    4084              :                                           io_kind_name (k), warn,
    4085              :                                           &dt->decimal->where))
    4086              :             return false;
    4087              : 
    4088          123 :           io_constraint (unformatted,
    4089              :                          "the DECIMAL= specifier at %L must be with an "
    4090              :                          "explicit format expression", &dt->decimal->where);
    4091              :         }
    4092              :     }
    4093              : 
    4094        33808 :   if (dt->blank)
    4095              :     {
    4096           17 :       if (!gfc_notify_std (GFC_STD_F2003, "BLANK= at %L "
    4097              :                            "not allowed in Fortran 95", &dt->blank->where))
    4098              :         return false;
    4099              : 
    4100           17 :       if (dt->blank->expr_type == EXPR_CONSTANT)
    4101              :         {
    4102           16 :           static const char * blank[] = { "NULL", "ZERO", NULL };
    4103              : 
    4104              : 
    4105           16 :           if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
    4106              :                                           dt->blank->value.character.string,
    4107              :                                           io_kind_name (k), warn,
    4108              :                                           &dt->blank->where))
    4109              :             return false;
    4110              : 
    4111           12 :           io_constraint (unformatted,
    4112              :                          "the BLANK= specifier at %L must be with an "
    4113              :                          "explicit format expression", &dt->blank->where);
    4114              :         }
    4115              :     }
    4116              : 
    4117        33804 :   if (dt->pad)
    4118              :     {
    4119           83 :       if (!gfc_notify_std (GFC_STD_F2003, "PAD= at %L "
    4120              :                            "not allowed in Fortran 95", &dt->pad->where))
    4121              :         return false;
    4122              : 
    4123           83 :       if (dt->pad->expr_type == EXPR_CONSTANT)
    4124              :         {
    4125           83 :           static const char * pad[] = { "YES", "NO", NULL };
    4126              : 
    4127           83 :           if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
    4128              :                                           dt->pad->value.character.string,
    4129              :                                           io_kind_name (k), warn,
    4130              :                                           &dt->pad->where))
    4131              :             return false;
    4132              : 
    4133           81 :           io_constraint (unformatted,
    4134              :                          "the PAD= specifier at %L must be with an "
    4135              :                          "explicit format expression", &dt->pad->where);
    4136              :         }
    4137              :     }
    4138              : 
    4139        33800 :   if (dt->round)
    4140              :     {
    4141          317 :       if (!gfc_notify_std (GFC_STD_F2003, "ROUND= at %L "
    4142              :                            "not allowed in Fortran 95", &dt->round->where))
    4143              :         return false;
    4144              : 
    4145          317 :       if (dt->round->expr_type == EXPR_CONSTANT)
    4146              :         {
    4147          305 :           static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
    4148              :                                           "COMPATIBLE", "PROCESSOR_DEFINED",
    4149              :                                           NULL };
    4150              : 
    4151          305 :           if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
    4152              :                                           dt->round->value.character.string,
    4153              :                                           io_kind_name (k), warn,
    4154              :                                           &dt->round->where))
    4155              :             return false;
    4156              :         }
    4157              :     }
    4158              : 
    4159        33796 :   if (dt->sign)
    4160              :     {
    4161              :       /* When implemented, change the following to use gfc_notify_std F2003.
    4162              :       if (gfc_notify_std (GFC_STD_F2003, "SIGN= at %L "
    4163              :           "not allowed in Fortran 95", &dt->sign->where) == false)
    4164              :         return false;  */
    4165              : 
    4166           17 :       if (dt->sign->expr_type == EXPR_CONSTANT)
    4167              :         {
    4168           16 :           static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
    4169              :                                          NULL };
    4170              : 
    4171           16 :           if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
    4172              :                                       dt->sign->value.character.string,
    4173              :                                       io_kind_name (k), warn, &dt->sign->where))
    4174              :             return false;
    4175              : 
    4176           12 :           io_constraint (unformatted,
    4177              :                          "SIGN= specifier at %L must be with an "
    4178           12 :                          "explicit format expression", &dt->sign->where);
    4179              : 
    4180           12 :           io_constraint (k == M_READ,
    4181              :                          "SIGN= specifier at %L not allowed in a "
    4182              :                          "READ statement", &dt->sign->where);
    4183              :         }
    4184              :     }
    4185              : 
    4186        33792 :   if (dt->delim)
    4187              :     {
    4188            7 :       if (!gfc_notify_std (GFC_STD_F2003, "DELIM= at %L "
    4189              :                            "not allowed in Fortran 95", &dt->delim->where))
    4190              :         return false;
    4191              : 
    4192            6 :       if (dt->delim->expr_type == EXPR_CONSTANT)
    4193              :         {
    4194            6 :           static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
    4195              : 
    4196            6 :           if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
    4197              :                                           dt->delim->value.character.string,
    4198              :                                           io_kind_name (k), warn,
    4199              :                                           &dt->delim->where))
    4200              :             return false;
    4201              : 
    4202            2 :           io_constraint (k == M_READ,
    4203              :                          "DELIM= specifier at %L not allowed in a "
    4204            2 :                          "READ statement", &dt->delim->where);
    4205              : 
    4206            2 :           io_constraint (dt->format_label != &format_asterisk
    4207              :                          && dt->namelist == NULL,
    4208              :                          "DELIM= specifier at %L must have FMT=*",
    4209            2 :                          &dt->delim->where);
    4210              : 
    4211            2 :           io_constraint (unformatted && dt->namelist == NULL,
    4212              :                          "DELIM= specifier at %L must be with FMT=* or "
    4213              :                          "NML= specifier", &dt->delim->where);
    4214              :         }
    4215              :     }
    4216              : 
    4217        33787 :   if (dt->namelist)
    4218              :     {
    4219         1229 :       io_constraint (io_code && dt->namelist,
    4220              :                      "NAMELIST cannot be followed by IO-list at %L",
    4221         1228 :                      &io_code->loc);
    4222              : 
    4223         1228 :       io_constraint (dt->format_expr,
    4224              :                      "IO spec-list cannot contain both NAMELIST group name "
    4225              :                      "and format specification at %L",
    4226         1227 :                      &dt->format_expr->where);
    4227              : 
    4228         1227 :       io_constraint (dt->format_label,
    4229              :                      "IO spec-list cannot contain both NAMELIST group name "
    4230         1226 :                      "and format label at %L", spec_end);
    4231              : 
    4232         1226 :       io_constraint (dt->rec,
    4233              :                      "NAMELIST IO is not allowed with a REC= specifier "
    4234         1226 :                      "at %L", &dt->rec->where);
    4235              : 
    4236         1226 :       io_constraint (dt->advance,
    4237              :                      "NAMELIST IO is not allowed with a ADVANCE= specifier "
    4238              :                      "at %L", &dt->advance->where);
    4239              :     }
    4240              : 
    4241        33783 :   if (dt->rec)
    4242              :     {
    4243          499 :       io_constraint (dt->end,
    4244              :                      "An END tag is not allowed with a "
    4245          498 :                      "REC= specifier at %L", &dt->end_where);
    4246              : 
    4247          498 :       io_constraint (dt->format_label == &format_asterisk,
    4248              :                      "FMT=* is not allowed with a REC= specifier "
    4249          497 :                      "at %L", spec_end);
    4250              : 
    4251          497 :       io_constraint (dt->pos,
    4252              :                      "POS= is not allowed with REC= specifier "
    4253              :                      "at %L", &dt->pos->where);
    4254              :     }
    4255              : 
    4256        33779 :   if (dt->advance)
    4257              :     {
    4258          385 :       int not_yes, not_no;
    4259          385 :       expr = dt->advance;
    4260              : 
    4261          385 :       io_constraint (dt->format_label == &format_asterisk,
    4262              :                      "List directed format(*) is not allowed with a "
    4263          385 :                      "ADVANCE= specifier at %L.", &expr->where);
    4264              : 
    4265          385 :       io_constraint (unformatted,
    4266              :                      "the ADVANCE= specifier at %L must appear with an "
    4267          384 :                      "explicit format expression", &expr->where);
    4268              : 
    4269          384 :       if (expr->expr_type == EXPR_CONSTANT && expr->ts.type == BT_CHARACTER)
    4270              :         {
    4271          365 :           const gfc_char_t *advance = expr->value.character.string;
    4272          365 :           not_no = gfc_wide_strlen (advance) != 2
    4273          365 :                    || gfc_wide_strncasecmp (advance, "no", 2) != 0;
    4274          365 :           not_yes = gfc_wide_strlen (advance) != 3
    4275          365 :                     || gfc_wide_strncasecmp (advance, "yes", 3) != 0;
    4276          365 :         }
    4277              :       else
    4278              :         {
    4279              :           not_no = 0;
    4280              :           not_yes = 0;
    4281              :         }
    4282              : 
    4283          384 :       io_constraint (not_no && not_yes,
    4284              :                      "ADVANCE= specifier at %L must have value = "
    4285          380 :                      "YES or NO.", &expr->where);
    4286              : 
    4287          380 :       io_constraint (dt->size && not_no && k == M_READ,
    4288              :                      "SIZE tag at %L requires an ADVANCE = %<NO%>",
    4289          379 :                      &dt->size->where);
    4290              : 
    4291          379 :       io_constraint (dt->eor && not_no && k == M_READ,
    4292              :                      "EOR tag at %L requires an ADVANCE = %<NO%>",
    4293              :                      &dt->eor_where);
    4294              :     }
    4295              : 
    4296        33772 :   if (k != M_READ)
    4297              :     {
    4298        27175 :       io_constraint (dt->end, "END tag not allowed with output at %L",
    4299        27174 :                      &dt->end_where);
    4300              : 
    4301        27174 :       io_constraint (dt->eor, "EOR tag not allowed with output at %L",
    4302        27172 :                      &dt->eor_where);
    4303              : 
    4304        27172 :       io_constraint (dt->blank,
    4305              :                      "BLANK= specifier not allowed with output at %L",
    4306        27172 :                      &dt->blank->where);
    4307              : 
    4308        27172 :       io_constraint (dt->pad, "PAD= specifier not allowed with output at %L",
    4309        27172 :                      &dt->pad->where);
    4310              : 
    4311        27172 :       io_constraint (dt->size, "SIZE= specifier not allowed with output at %L",
    4312              :                      &dt->size->where);
    4313              :     }
    4314              :   else
    4315              :     {
    4316         6597 :       io_constraint (dt->size && dt->advance == NULL,
    4317              :                      "SIZE tag at %L requires an ADVANCE tag",
    4318         6597 :                      &dt->size->where);
    4319              : 
    4320         6597 :       io_constraint (dt->eor && dt->advance == NULL,
    4321              :                      "EOR tag at %L requires an ADVANCE tag",
    4322              :                      &dt->eor_where);
    4323              :     }
    4324              : 
    4325              :   return true;
    4326              : #undef io_constraint
    4327              : }
    4328              : 
    4329              : 
    4330              : /* Match a READ, WRITE or PRINT statement.  */
    4331              : 
    4332              : static match
    4333        34222 : match_io (io_kind k)
    4334              : {
    4335        34222 :   char name[GFC_MAX_SYMBOL_LEN + 1];
    4336        34222 :   gfc_code *io_code;
    4337        34222 :   gfc_symbol *sym;
    4338        34222 :   int comma_flag;
    4339        34222 :   locus where;
    4340        34222 :   locus control;
    4341        34222 :   gfc_dt *dt;
    4342        34222 :   match m;
    4343              : 
    4344        34222 :   where = gfc_current_locus;
    4345        34222 :   comma_flag = 0;
    4346        34222 :   current_dt = dt = XCNEW (gfc_dt);
    4347        34222 :   m = gfc_match_char ('(');
    4348        34222 :   if (m == MATCH_NO)
    4349              :     {
    4350         7937 :       where = gfc_current_locus;
    4351         7937 :       if (k == M_WRITE)
    4352            0 :         goto syntax;
    4353         7937 :       else if (k == M_PRINT)
    4354              :         {
    4355              :           /* Treat the non-standard case of PRINT namelist.  */
    4356         7642 :           if ((gfc_current_form == FORM_FIXED || gfc_peek_ascii_char () == ' ')
    4357        15327 :               && gfc_match_name (name) == MATCH_YES)
    4358              :             {
    4359          131 :               gfc_find_symbol (name, NULL, 1, &sym);
    4360          131 :               if (sym && sym->attr.flavor == FL_NAMELIST)
    4361              :                 {
    4362           11 :                   if (!gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
    4363              :                                        "%C is an extension"))
    4364              :                     {
    4365            2 :                       m = MATCH_ERROR;
    4366            2 :                       goto cleanup;
    4367              :                     }
    4368              : 
    4369            9 :                   dt->io_unit = default_unit (k);
    4370            9 :                   dt->namelist = sym;
    4371            9 :                   goto get_io_list;
    4372              :                 }
    4373              :               else
    4374          120 :                 gfc_current_locus = where;
    4375              :             }
    4376              : 
    4377         7912 :           if (gfc_match_char ('*') == MATCH_YES
    4378         7912 :               && gfc_match_char(',') == MATCH_YES)
    4379              :             {
    4380         7021 :               locus where2 = gfc_current_locus;
    4381         7021 :               if (gfc_match_eos () == MATCH_YES)
    4382              :                 {
    4383            1 :                   gfc_current_locus = where2;
    4384            1 :                   gfc_error ("Comma after * at %C not allowed without I/O list");
    4385            1 :                   m = MATCH_ERROR;
    4386            1 :                   goto cleanup;
    4387              :                 }
    4388              :               else
    4389         7020 :                 gfc_current_locus = where;
    4390              :             }
    4391              :           else
    4392          891 :             gfc_current_locus = where;
    4393              :         }
    4394              : 
    4395         7925 :       if (gfc_current_form == FORM_FREE)
    4396              :         {
    4397         7646 :           char c = gfc_peek_ascii_char ();
    4398              : 
    4399              :           /* Issue a warning for an invalid tab in 'print<tab>*'.  After
    4400              :              the warning is issued, consume any other whitespace and check
    4401              :              that the next char is an *, ', or ".  */
    4402         7646 :           if (c == '\t')
    4403              :             {
    4404            2 :               gfc_gobble_whitespace ();
    4405            2 :               c = gfc_peek_ascii_char ();
    4406            2 :               if (c != '*' && c != '\'' && c != '"')
    4407              :                 {
    4408            0 :                   m = MATCH_NO;
    4409            0 :                   goto cleanup;
    4410              :                 }
    4411              :             }
    4412         7644 :           else if (c != ' ' && c != '*' && c != '\'' && c != '"')
    4413              :             {
    4414            2 :               m = MATCH_NO;
    4415            2 :               goto cleanup;
    4416              :             }
    4417              :         }
    4418              : 
    4419         7923 :       m = match_dt_format (dt);
    4420         7923 :       if (m == MATCH_ERROR)
    4421            0 :         goto cleanup;
    4422         7923 :       if (m == MATCH_NO)
    4423            4 :         goto syntax;
    4424              : 
    4425         7919 :       comma_flag = 1;
    4426         7919 :       dt->io_unit = default_unit (k);
    4427         7919 :       goto get_io_list;
    4428              :     }
    4429              :   else
    4430              :     {
    4431              :       /* Before issuing an error for a malformed 'print (1,*)' type of
    4432              :          error, check for a default-char-expr of the form ('(I0)').  */
    4433        26285 :       if (m == MATCH_YES)
    4434              :         {
    4435        26285 :           control = gfc_current_locus;
    4436        26285 :           if (k == M_PRINT)
    4437              :             {
    4438              :               /* Reset current locus to get the initial '(' in an expression.  */
    4439           10 :               gfc_current_locus = where;
    4440           10 :               dt->format_expr = NULL;
    4441           10 :               m = match_dt_format (dt);
    4442              : 
    4443           10 :               if (m == MATCH_ERROR)
    4444            0 :                 goto cleanup;
    4445           10 :               if (m == MATCH_NO || dt->format_expr == NULL)
    4446            3 :                 goto syntax;
    4447              : 
    4448            7 :               comma_flag = 1;
    4449            7 :               dt->io_unit = default_unit (k);
    4450            7 :               goto get_io_list;
    4451              :             }
    4452        26275 :           if (k == M_READ)
    4453              :             {
    4454              :               /* Commit any pending symbols now so that when we undo
    4455              :                  symbols later we wont lose them.  */
    4456         6716 :               gfc_commit_symbols ();
    4457              :               /* Reset current locus to get the initial '(' in an expression.  */
    4458         6716 :               gfc_current_locus = where;
    4459         6716 :               dt->format_expr = NULL;
    4460         6716 :               m = gfc_match_expr (&dt->format_expr);
    4461         6716 :               if (m == MATCH_YES)
    4462              :                 {
    4463          557 :                   if (dt->format_expr
    4464          557 :                       && dt->format_expr->ts.type == BT_CHARACTER)
    4465              :                     {
    4466            3 :                       comma_flag = 1;
    4467            3 :                       dt->io_unit = default_unit (k);
    4468            3 :                       goto get_io_list;
    4469              :                     }
    4470              :                   else
    4471              :                     {
    4472          554 :                       gfc_free_expr (dt->format_expr);
    4473          554 :                       dt->format_expr = NULL;
    4474          554 :                       gfc_current_locus = control;
    4475              :                     }
    4476              :                 }
    4477              :               else
    4478              :                 {
    4479         6159 :                   gfc_clear_error ();
    4480         6159 :                   gfc_undo_symbols ();
    4481         6159 :                   gfc_free_expr (dt->format_expr);
    4482         6159 :                   dt->format_expr = NULL;
    4483         6159 :                   gfc_current_locus = control;
    4484              :                 }
    4485              :             }
    4486              :         }
    4487              :     }
    4488              : 
    4489              :   /* Match a control list */
    4490        26272 :   if (match_dt_element (k, dt) == MATCH_YES)
    4491         1213 :     goto next;
    4492        25059 :   if (match_dt_unit (k, dt) != MATCH_YES)
    4493            0 :     goto loop;
    4494              : 
    4495        25059 :   if (gfc_match_char (')') == MATCH_YES)
    4496         1517 :     goto get_io_list;
    4497        23542 :   if (gfc_match_char (',') != MATCH_YES)
    4498            0 :     goto syntax;
    4499              : 
    4500        23542 :   m = match_dt_element (k, dt);
    4501        23542 :   if (m == MATCH_YES)
    4502         2691 :     goto next;
    4503        20851 :   if (m == MATCH_ERROR)
    4504            0 :     goto cleanup;
    4505              : 
    4506        20851 :   m = match_dt_format (dt);
    4507        20851 :   if (m == MATCH_YES)
    4508        20423 :     goto next;
    4509          428 :   if (m == MATCH_ERROR)
    4510            7 :     goto cleanup;
    4511              : 
    4512          421 :   where = gfc_current_locus;
    4513              : 
    4514          421 :   m = gfc_match_name (name);
    4515          421 :   if (m == MATCH_YES)
    4516              :     {
    4517          421 :       gfc_find_symbol (name, NULL, 1, &sym);
    4518          421 :       if (sym && sym->attr.flavor == FL_NAMELIST)
    4519              :         {
    4520          421 :           dt->namelist = sym;
    4521          421 :           if (k == M_READ && check_namelist (sym))
    4522              :             {
    4523            1 :               m = MATCH_ERROR;
    4524            1 :               goto cleanup;
    4525              :             }
    4526          420 :           dt->nml_where = where;
    4527          420 :           goto next;
    4528              :         }
    4529              :     }
    4530              : 
    4531            0 :   gfc_current_locus = where;
    4532              : 
    4533            0 :   goto loop;                    /* No matches, try regular elements */
    4534              : 
    4535        24747 : next:
    4536        24747 :   if (gfc_match_char (')') == MATCH_YES)
    4537        20535 :     goto get_io_list;
    4538         4212 :   if (gfc_match_char (',') != MATCH_YES)
    4539            0 :     goto syntax;
    4540              : 
    4541         4212 : loop:
    4542         5320 :   for (;;)
    4543              :     {
    4544         5320 :       m = match_dt_element (k, dt);
    4545         5320 :       if (m == MATCH_NO)
    4546            0 :         goto syntax;
    4547         5320 :       if (m == MATCH_ERROR)
    4548            4 :         goto cleanup;
    4549              : 
    4550         5316 :       if (gfc_match_char (')') == MATCH_YES)
    4551              :         break;
    4552         1108 :       if (gfc_match_char (',') != MATCH_YES)
    4553            0 :         goto syntax;
    4554              :     }
    4555              : 
    4556         4208 : get_io_list:
    4557              : 
    4558              :   /* Save the IO kind for later use.  */
    4559        34198 :   dt->dt_io_kind = gfc_get_iokind_expr (&gfc_current_locus, k);
    4560              : 
    4561              :   /* Optional leading comma (non-standard).  We use a gfc_expr structure here
    4562              :      to save the locus.  This is used later when resolving transfer statements
    4563              :      that might have a format expression without unit number.  */
    4564        34198 :   if (!comma_flag && gfc_match_char (',') == MATCH_YES)
    4565           87 :     dt->extra_comma = dt->dt_io_kind;
    4566              : 
    4567        34198 :   io_code = NULL;
    4568        34198 :   if (gfc_match_eos () != MATCH_YES)
    4569              :     {
    4570        31962 :       if (comma_flag && gfc_match_char (',') != MATCH_YES)
    4571              :         {
    4572            0 :           gfc_error ("Expected comma in I/O list at %C");
    4573            0 :           m = MATCH_ERROR;
    4574            0 :           goto cleanup;
    4575              :         }
    4576              : 
    4577        31962 :       m = match_io_list (k, &io_code);
    4578        31962 :       if (m == MATCH_ERROR)
    4579          115 :         goto cleanup;
    4580              :       if (m == MATCH_NO)
    4581              :         goto syntax;
    4582              :     }
    4583              : 
    4584              :   /* See if we want to use defaults for missing exponents in real transfers
    4585              :      and other DEC runtime extensions. */
    4586        34083 :   if (flag_dec_format_defaults)
    4587          484 :     dt->dec_ext = 1;
    4588              : 
    4589              :   /* Check the format string now.  */
    4590        34083 :   if (dt->format_expr
    4591        34083 :       && (!gfc_simplify_expr (dt->format_expr, 0)
    4592        12694 :           || !check_format_string (dt->format_expr, k == M_READ)))
    4593              :     return MATCH_ERROR;
    4594              : 
    4595        34048 :   new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
    4596        34048 :   new_st.ext.dt = dt;
    4597        34048 :   new_st.block = gfc_get_code (new_st.op);
    4598        34048 :   new_st.block->next = io_code;
    4599              : 
    4600        34048 :   terminate_io (io_code);
    4601              : 
    4602        34048 :   return MATCH_YES;
    4603              : 
    4604            7 : syntax:
    4605            7 :   gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
    4606            7 :   m = MATCH_ERROR;
    4607              : 
    4608          139 : cleanup:
    4609          139 :   gfc_free_dt (dt);
    4610          139 :   return m;
    4611              : }
    4612              : 
    4613              : 
    4614              : match
    4615         6730 : gfc_match_read (void)
    4616              : {
    4617         6730 :   return match_io (M_READ);
    4618              : }
    4619              : 
    4620              : 
    4621              : match
    4622        19559 : gfc_match_write (void)
    4623              : {
    4624        19559 :   return match_io (M_WRITE);
    4625              : }
    4626              : 
    4627              : 
    4628              : match
    4629         7933 : gfc_match_print (void)
    4630              : {
    4631         7933 :   match m;
    4632              : 
    4633         7933 :   m = match_io (M_PRINT);
    4634         7933 :   if (m != MATCH_YES)
    4635              :     return m;
    4636              : 
    4637         7806 :   if (gfc_pure (NULL))
    4638              :     {
    4639            0 :       gfc_error ("PRINT statement at %C not allowed within PURE procedure");
    4640            0 :       return MATCH_ERROR;
    4641              :     }
    4642              : 
    4643         7806 :   gfc_unset_implicit_pure (NULL);
    4644              : 
    4645         7806 :   return MATCH_YES;
    4646              : }
    4647              : 
    4648              : 
    4649              : /* Free a gfc_inquire structure.  */
    4650              : 
    4651              : void
    4652          882 : gfc_free_inquire (gfc_inquire *inquire)
    4653              : {
    4654              : 
    4655          882 :   if (inquire == NULL)
    4656              :     return;
    4657              : 
    4658          882 :   gfc_free_expr (inquire->unit);
    4659          882 :   gfc_free_expr (inquire->file);
    4660          882 :   gfc_free_expr (inquire->iomsg);
    4661          882 :   gfc_free_expr (inquire->iostat);
    4662          882 :   gfc_free_expr (inquire->exist);
    4663          882 :   gfc_free_expr (inquire->opened);
    4664          882 :   gfc_free_expr (inquire->number);
    4665          882 :   gfc_free_expr (inquire->named);
    4666          882 :   gfc_free_expr (inquire->name);
    4667          882 :   gfc_free_expr (inquire->access);
    4668          882 :   gfc_free_expr (inquire->sequential);
    4669          882 :   gfc_free_expr (inquire->direct);
    4670          882 :   gfc_free_expr (inquire->form);
    4671          882 :   gfc_free_expr (inquire->formatted);
    4672          882 :   gfc_free_expr (inquire->unformatted);
    4673          882 :   gfc_free_expr (inquire->recl);
    4674          882 :   gfc_free_expr (inquire->nextrec);
    4675          882 :   gfc_free_expr (inquire->blank);
    4676          882 :   gfc_free_expr (inquire->position);
    4677          882 :   gfc_free_expr (inquire->action);
    4678          882 :   gfc_free_expr (inquire->read);
    4679          882 :   gfc_free_expr (inquire->write);
    4680          882 :   gfc_free_expr (inquire->readwrite);
    4681          882 :   gfc_free_expr (inquire->delim);
    4682          882 :   gfc_free_expr (inquire->encoding);
    4683          882 :   gfc_free_expr (inquire->pad);
    4684          882 :   gfc_free_expr (inquire->iolength);
    4685          882 :   gfc_free_expr (inquire->convert);
    4686          882 :   gfc_free_expr (inquire->strm_pos);
    4687          882 :   gfc_free_expr (inquire->asynchronous);
    4688          882 :   gfc_free_expr (inquire->decimal);
    4689          882 :   gfc_free_expr (inquire->pending);
    4690          882 :   gfc_free_expr (inquire->id);
    4691          882 :   gfc_free_expr (inquire->sign);
    4692          882 :   gfc_free_expr (inquire->size);
    4693          882 :   gfc_free_expr (inquire->round);
    4694          882 :   gfc_free_expr (inquire->share);
    4695          882 :   gfc_free_expr (inquire->cc);
    4696          882 :   free (inquire);
    4697              : }
    4698              : 
    4699              : 
    4700              : /* Match an element of an INQUIRE statement.  */
    4701              : 
    4702              : #define RETM   if (m != MATCH_NO) return m;
    4703              : 
    4704              : static match
    4705         2679 : match_inquire_element (gfc_inquire *inquire)
    4706              : {
    4707         2679 :   match m;
    4708              : 
    4709         2679 :   m = match_etag (&tag_unit, &inquire->unit);
    4710         2679 :   RETM m = match_etag (&tag_file, &inquire->file);
    4711         2340 :   RETM m = match_ltag (&tag_err, &inquire->err);
    4712         2129 :   RETM m = match_etag (&tag_iomsg, &inquire->iomsg);
    4713         2122 :   RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
    4714         2097 :   RETM m = match_vtag (&tag_exist, &inquire->exist);
    4715         2042 :   RETM m = match_vtag (&tag_opened, &inquire->opened);
    4716         1900 :   RETM m = match_vtag (&tag_named, &inquire->named);
    4717         1748 :   RETM m = match_vtag (&tag_name, &inquire->name);
    4718         1725 :   RETM m = match_out_tag (&tag_number, &inquire->number);
    4719         1698 :   RETM m = match_vtag (&tag_s_access, &inquire->access);
    4720         1611 :   RETM m = match_vtag (&tag_sequential, &inquire->sequential);
    4721         1461 :   RETM m = match_vtag (&tag_direct, &inquire->direct);
    4722         1422 :   RETM m = match_vtag (&tag_s_form, &inquire->form);
    4723         1311 :   RETM m = match_vtag (&tag_formatted, &inquire->formatted);
    4724         1296 :   RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
    4725         1250 :   RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
    4726         1211 :   RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
    4727         1152 :   RETM m = match_vtag (&tag_s_blank, &inquire->blank);
    4728         1090 :   RETM m = match_vtag (&tag_s_position, &inquire->position);
    4729         1066 :   RETM m = match_vtag (&tag_s_action, &inquire->action);
    4730         1009 :   RETM m = match_vtag (&tag_read, &inquire->read);
    4731          987 :   RETM m = match_vtag (&tag_write, &inquire->write);
    4732          954 :   RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
    4733          921 :   RETM m = match_vtag (&tag_s_async, &inquire->asynchronous);
    4734          888 :   RETM m = match_vtag (&tag_s_delim, &inquire->delim);
    4735          861 :   RETM m = match_vtag (&tag_s_decimal, &inquire->decimal);
    4736          821 :   RETM m = match_out_tag (&tag_size, &inquire->size);
    4737          802 :   RETM m = match_vtag (&tag_s_encoding, &inquire->encoding);
    4738          759 :   RETM m = match_vtag (&tag_s_round, &inquire->round);
    4739          739 :   RETM m = match_vtag (&tag_s_sign, &inquire->sign);
    4740          720 :   RETM m = match_vtag (&tag_s_pad, &inquire->pad);
    4741          701 :   RETM m = match_out_tag (&tag_iolength, &inquire->iolength);
    4742          662 :   RETM m = match_vtag (&tag_convert, &inquire->convert);
    4743          569 :   RETM m = match_out_tag (&tag_strm_out, &inquire->strm_pos);
    4744          557 :   RETM m = match_vtag (&tag_pending, &inquire->pending);
    4745          419 :   RETM m = match_etag (&tag_id, &inquire->id);
    4746          399 :   RETM m = match_vtag (&tag_s_iqstream, &inquire->iqstream);
    4747          388 :   RETM m = match_dec_vtag (&tag_v_share, &inquire->share);
    4748          346 :   RETM m = match_dec_vtag (&tag_v_cc, &inquire->cc);
    4749          339 :   RETM return MATCH_NO;
    4750              : }
    4751              : 
    4752              : #undef RETM
    4753              : 
    4754              : 
    4755              : match
    4756          974 : gfc_match_inquire (void)
    4757              : {
    4758          974 :   gfc_inquire *inquire;
    4759          974 :   gfc_code *code;
    4760          974 :   match m;
    4761          974 :   locus loc;
    4762              : 
    4763          974 :   m = gfc_match_char ('(');
    4764          974 :   if (m == MATCH_NO)
    4765              :     return m;
    4766              : 
    4767          974 :   inquire = XCNEW (gfc_inquire);
    4768              : 
    4769          974 :   loc = gfc_current_locus;
    4770              : 
    4771          974 :   m = match_inquire_element (inquire);
    4772          974 :   if (m == MATCH_ERROR)
    4773            0 :     goto cleanup;
    4774          974 :   if (m == MATCH_NO)
    4775              :     {
    4776          332 :       m = gfc_match_expr (&inquire->unit);
    4777          332 :       if (m == MATCH_ERROR)
    4778            0 :         goto cleanup;
    4779          332 :       if (m == MATCH_NO)
    4780            0 :         goto syntax;
    4781              :     }
    4782              : 
    4783              :   /* See if we have the IOLENGTH form of the inquire statement.  */
    4784          974 :   if (inquire->iolength != NULL)
    4785              :     {
    4786           93 :       if (gfc_match_char (')') != MATCH_YES)
    4787            0 :         goto syntax;
    4788              : 
    4789           93 :       m = match_io_list (M_INQUIRE, &code);
    4790           93 :       if (m == MATCH_ERROR)
    4791            0 :         goto cleanup;
    4792           93 :       if (m == MATCH_NO)
    4793              :         goto syntax;
    4794              : 
    4795          246 :       for (gfc_code *c = code; c; c = c->next)
    4796          154 :         if (c->expr1 && c->expr1->expr_type == EXPR_FUNCTION
    4797            2 :             && c->expr1->symtree && c->expr1->symtree->n.sym->attr.function
    4798            2 :             && !c->expr1->symtree->n.sym->attr.external
    4799            1 :             && strcmp (c->expr1->symtree->name, "null") == 0)
    4800              :           {
    4801            1 :             gfc_error ("NULL() near %L cannot appear in INQUIRE statement",
    4802              :                        &c->expr1->where);
    4803            1 :             goto cleanup;
    4804              :           }
    4805              : 
    4806           92 :       new_st.op = EXEC_IOLENGTH;
    4807           92 :       new_st.expr1 = inquire->iolength;
    4808           92 :       new_st.ext.inquire = inquire;
    4809              : 
    4810           92 :       if (gfc_pure (NULL))
    4811              :         {
    4812            0 :           gfc_free_statements (code);
    4813            0 :           gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
    4814            0 :           return MATCH_ERROR;
    4815              :         }
    4816              : 
    4817           92 :       gfc_unset_implicit_pure (NULL);
    4818              : 
    4819           92 :       new_st.block = gfc_get_code (EXEC_IOLENGTH);
    4820           92 :       terminate_io (code);
    4821           92 :       new_st.block->next = code;
    4822           92 :       return MATCH_YES;
    4823              :     }
    4824              : 
    4825              :   /* At this point, we have the non-IOLENGTH inquire statement.  */
    4826         2584 :   for (;;)
    4827              :     {
    4828         2584 :       if (gfc_match_char (')') == MATCH_YES)
    4829              :         break;
    4830         1705 :       if (gfc_match_char (',') != MATCH_YES)
    4831            0 :         goto syntax;
    4832              : 
    4833         1705 :       m = match_inquire_element (inquire);
    4834         1705 :       if (m == MATCH_ERROR)
    4835            2 :         goto cleanup;
    4836         1703 :       if (m == MATCH_NO)
    4837            0 :         goto syntax;
    4838              : 
    4839         1703 :       if (inquire->iolength != NULL)
    4840              :         {
    4841            0 :           gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
    4842            0 :           goto cleanup;
    4843              :         }
    4844              :     }
    4845              : 
    4846          879 :   if (gfc_match_eos () != MATCH_YES)
    4847            0 :     goto syntax;
    4848              : 
    4849          879 :   if (inquire->unit != NULL && inquire->file != NULL)
    4850              :     {
    4851            2 :       gfc_error ("INQUIRE statement at %L cannot contain both FILE and "
    4852              :                  "UNIT specifiers", &loc);
    4853            2 :       goto cleanup;
    4854              :     }
    4855              : 
    4856          877 :   if (inquire->unit == NULL && inquire->file == NULL)
    4857              :     {
    4858            1 :       gfc_error ("INQUIRE statement at %L requires either FILE or "
    4859              :                  "UNIT specifier", &loc);
    4860            1 :       goto cleanup;
    4861              :     }
    4862              : 
    4863          876 :   if (inquire->unit != NULL && inquire->unit->expr_type == EXPR_CONSTANT
    4864          524 :       && inquire->unit->ts.type == BT_INTEGER
    4865          524 :       && ((mpz_get_si (inquire->unit->value.integer) == GFC_INTERNAL_UNIT4)
    4866          523 :       || (mpz_get_si (inquire->unit->value.integer) == GFC_INTERNAL_UNIT)))
    4867              :     {
    4868            2 :       gfc_error ("UNIT number in INQUIRE statement at %L cannot "
    4869              :                  "be %d", &loc, (int) mpz_get_si (inquire->unit->value.integer));
    4870            2 :       goto cleanup;
    4871              :     }
    4872              : 
    4873          874 :   if (gfc_pure (NULL))
    4874              :     {
    4875            0 :       gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
    4876            0 :       goto cleanup;
    4877              :     }
    4878              : 
    4879          874 :   gfc_unset_implicit_pure (NULL);
    4880              : 
    4881          874 :   if (inquire->id != NULL && inquire->pending == NULL)
    4882              :     {
    4883            0 :       gfc_error ("INQUIRE statement at %L requires a PENDING= specifier with "
    4884              :                  "the ID= specifier", &loc);
    4885            0 :       goto cleanup;
    4886              :     }
    4887              : 
    4888          874 :   new_st.op = EXEC_INQUIRE;
    4889          874 :   new_st.ext.inquire = inquire;
    4890          874 :   return MATCH_YES;
    4891              : 
    4892            0 : syntax:
    4893            0 :   gfc_syntax_error (ST_INQUIRE);
    4894              : 
    4895            8 : cleanup:
    4896            8 :   gfc_free_inquire (inquire);
    4897            8 :   return MATCH_ERROR;
    4898              : }
    4899              : 
    4900              : 
    4901              : /* Resolve everything in a gfc_inquire structure.  */
    4902              : 
    4903              : bool
    4904          966 : gfc_resolve_inquire (gfc_inquire *inquire)
    4905              : {
    4906          966 :   RESOLVE_TAG (&tag_unit, inquire->unit);
    4907          966 :   RESOLVE_TAG (&tag_file, inquire->file);
    4908          965 :   RESOLVE_TAG (&tag_id, inquire->id);
    4909              : 
    4910              :   /* For INQUIRE, all tags except FILE, ID and UNIT are variable definition
    4911              :      contexts.  Thus, use an extended RESOLVE_TAG macro for that.  */
    4912              : #define INQUIRE_RESOLVE_TAG(tag, expr) \
    4913              :   RESOLVE_TAG (tag, expr); \
    4914              :   if (expr) \
    4915              :     { \
    4916              :       char context[64]; \
    4917              :       sprintf (context, _("%s tag with INQUIRE"), (tag)->name); \
    4918              :       if (gfc_check_vardef_context ((expr), false, false, false, \
    4919              :                                     context) == false) \
    4920              :         return false; \
    4921              :       gfc_expr_set_at (expr, &expr->where, VALUE_VARDEF);        \
    4922              :     }
    4923          965 :   INQUIRE_RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
    4924          953 :   INQUIRE_RESOLVE_TAG (&tag_iostat, inquire->iostat);
    4925          952 :   INQUIRE_RESOLVE_TAG (&tag_exist, inquire->exist);
    4926          950 :   INQUIRE_RESOLVE_TAG (&tag_opened, inquire->opened);
    4927          948 :   INQUIRE_RESOLVE_TAG (&tag_number, inquire->number);
    4928          946 :   INQUIRE_RESOLVE_TAG (&tag_named, inquire->named);
    4929          944 :   INQUIRE_RESOLVE_TAG (&tag_name, inquire->name);
    4930          943 :   INQUIRE_RESOLVE_TAG (&tag_s_access, inquire->access);
    4931          942 :   INQUIRE_RESOLVE_TAG (&tag_sequential, inquire->sequential);
    4932          941 :   INQUIRE_RESOLVE_TAG (&tag_direct, inquire->direct);
    4933          940 :   INQUIRE_RESOLVE_TAG (&tag_s_form, inquire->form);
    4934          939 :   INQUIRE_RESOLVE_TAG (&tag_formatted, inquire->formatted);
    4935          937 :   INQUIRE_RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
    4936          936 :   INQUIRE_RESOLVE_TAG (&tag_s_recl, inquire->recl);
    4937          935 :   INQUIRE_RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
    4938          934 :   INQUIRE_RESOLVE_TAG (&tag_s_blank, inquire->blank);
    4939          933 :   INQUIRE_RESOLVE_TAG (&tag_s_position, inquire->position);
    4940          932 :   INQUIRE_RESOLVE_TAG (&tag_s_action, inquire->action);
    4941          931 :   INQUIRE_RESOLVE_TAG (&tag_read, inquire->read);
    4942          930 :   INQUIRE_RESOLVE_TAG (&tag_write, inquire->write);
    4943          929 :   INQUIRE_RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
    4944          928 :   INQUIRE_RESOLVE_TAG (&tag_s_delim, inquire->delim);
    4945          927 :   INQUIRE_RESOLVE_TAG (&tag_s_pad, inquire->pad);
    4946          926 :   INQUIRE_RESOLVE_TAG (&tag_s_encoding, inquire->encoding);
    4947          924 :   INQUIRE_RESOLVE_TAG (&tag_s_round, inquire->round);
    4948          923 :   INQUIRE_RESOLVE_TAG (&tag_iolength, inquire->iolength);
    4949          921 :   INQUIRE_RESOLVE_TAG (&tag_convert, inquire->convert);
    4950          921 :   INQUIRE_RESOLVE_TAG (&tag_strm_out, inquire->strm_pos);
    4951          921 :   INQUIRE_RESOLVE_TAG (&tag_s_async, inquire->asynchronous);
    4952          920 :   INQUIRE_RESOLVE_TAG (&tag_s_sign, inquire->sign);
    4953          919 :   INQUIRE_RESOLVE_TAG (&tag_s_round, inquire->round);
    4954          919 :   INQUIRE_RESOLVE_TAG (&tag_pending, inquire->pending);
    4955          917 :   INQUIRE_RESOLVE_TAG (&tag_size, inquire->size);
    4956          917 :   INQUIRE_RESOLVE_TAG (&tag_s_decimal, inquire->decimal);
    4957          916 :   INQUIRE_RESOLVE_TAG (&tag_s_iqstream, inquire->iqstream);
    4958          916 :   INQUIRE_RESOLVE_TAG (&tag_v_share, inquire->share);
    4959          916 :   INQUIRE_RESOLVE_TAG (&tag_v_cc, inquire->cc);
    4960              : #undef INQUIRE_RESOLVE_TAG
    4961              : 
    4962          916 :   if (!gfc_reference_st_label (inquire->err, ST_LABEL_TARGET))
    4963              :     return false;
    4964              : 
    4965              :   return true;
    4966              : }
    4967              : 
    4968              : 
    4969              : void
    4970           89 : gfc_free_wait (gfc_wait *wait)
    4971              : {
    4972           89 :   if (wait == NULL)
    4973              :     return;
    4974              : 
    4975           89 :   gfc_free_expr (wait->unit);
    4976           89 :   gfc_free_expr (wait->iostat);
    4977           89 :   gfc_free_expr (wait->iomsg);
    4978           89 :   gfc_free_expr (wait->id);
    4979           89 :   free (wait);
    4980              : }
    4981              : 
    4982              : 
    4983              : bool
    4984           89 : gfc_resolve_wait (gfc_wait *wait)
    4985              : {
    4986           89 :   RESOLVE_TAG (&tag_unit, wait->unit);
    4987           89 :   RESOLVE_TAG (&tag_iomsg, wait->iomsg);
    4988           74 :   RESOLVE_TAG (&tag_iostat, wait->iostat);
    4989           74 :   RESOLVE_TAG (&tag_id, wait->id);
    4990              : 
    4991           74 :   if (!gfc_reference_st_label (wait->err, ST_LABEL_TARGET))
    4992              :     return false;
    4993              : 
    4994           74 :   if (!gfc_reference_st_label (wait->end, ST_LABEL_TARGET))
    4995              :     return false;
    4996              : 
    4997              :   return true;
    4998              : }
    4999              : 
    5000              : /* Match an element of a WAIT statement.  */
    5001              : 
    5002              : #define RETM   if (m != MATCH_NO) return m;
    5003              : 
    5004              : static match
    5005          166 : match_wait_element (gfc_wait *wait)
    5006              : {
    5007          166 :   match m;
    5008              : 
    5009          166 :   m = match_etag (&tag_unit, &wait->unit);
    5010          166 :   RETM m = match_ltag (&tag_err, &wait->err);
    5011          153 :   RETM m = match_ltag (&tag_end, &wait->end);
    5012          146 :   RETM m = match_ltag (&tag_eor, &wait->eor);
    5013          139 :   RETM m = match_etag (&tag_iomsg, &wait->iomsg);
    5014          139 :   RETM m = match_out_tag (&tag_iostat, &wait->iostat);
    5015          110 :   RETM m = match_etag (&tag_id, &wait->id);
    5016           89 :   RETM return MATCH_NO;
    5017              : }
    5018              : 
    5019              : #undef RETM
    5020              : 
    5021              : 
    5022              : match
    5023           89 : gfc_match_wait (void)
    5024              : {
    5025           89 :   gfc_wait *wait;
    5026           89 :   match m;
    5027              : 
    5028           89 :   m = gfc_match_char ('(');
    5029           89 :   if (m == MATCH_NO)
    5030              :     return m;
    5031              : 
    5032           89 :   wait = XCNEW (gfc_wait);
    5033              : 
    5034           89 :   m = match_wait_element (wait);
    5035           89 :   if (m == MATCH_ERROR)
    5036            0 :     goto cleanup;
    5037           89 :   if (m == MATCH_NO)
    5038              :     {
    5039           76 :       m = gfc_match_expr (&wait->unit);
    5040           76 :       if (m == MATCH_ERROR)
    5041            0 :         goto cleanup;
    5042           76 :       if (m == MATCH_NO)
    5043            0 :         goto syntax;
    5044              :     }
    5045              : 
    5046          166 :   for (;;)
    5047              :     {
    5048          166 :       if (gfc_match_char (')') == MATCH_YES)
    5049              :         break;
    5050           77 :       if (gfc_match_char (',') != MATCH_YES)
    5051            0 :         goto syntax;
    5052              : 
    5053           77 :       m = match_wait_element (wait);
    5054           77 :       if (m == MATCH_ERROR)
    5055            0 :         goto cleanup;
    5056           77 :       if (m == MATCH_NO)
    5057            0 :         goto syntax;
    5058              :     }
    5059              : 
    5060           89 :   if (!gfc_notify_std (GFC_STD_F2003, "WAIT at %C "
    5061              :                        "not allowed in Fortran 95"))
    5062            0 :     goto cleanup;
    5063              : 
    5064           89 :   if (gfc_pure (NULL))
    5065              :     {
    5066            0 :       gfc_error ("WAIT statement not allowed in PURE procedure at %C");
    5067            0 :       goto cleanup;
    5068              :     }
    5069              : 
    5070           89 :   gfc_unset_implicit_pure (NULL);
    5071              : 
    5072           89 :   new_st.op = EXEC_WAIT;
    5073           89 :   new_st.ext.wait = wait;
    5074              : 
    5075           89 :   return MATCH_YES;
    5076              : 
    5077            0 : syntax:
    5078            0 :   gfc_syntax_error (ST_WAIT);
    5079              : 
    5080            0 : cleanup:
    5081            0 :   gfc_free_wait (wait);
    5082            0 :   return MATCH_ERROR;
    5083              : }
        

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.