blob: fc97df79ecab10ea423573dcdb87c76ba08ceae8 [file] [log] [blame]
/* Deal with I/O statements & related stuff.
Copyright (C) 2000-2021 Free Software Foundation, Inc.
Contributed by Andy Vaught
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "options.h"
#include "gfortran.h"
#include "match.h"
#include "parse.h"
#include "constructor.h"
gfc_st_label
format_asterisk = {0, NULL, NULL, -1, ST_LABEL_FORMAT, ST_LABEL_FORMAT, NULL,
0, {NULL, NULL}, NULL};
typedef struct
{
const char *name, *spec, *value;
bt type;
}
io_tag;
static const io_tag
tag_readonly = {"READONLY", " readonly", NULL, BT_UNKNOWN },
tag_shared = {"SHARE", " shared", NULL, BT_UNKNOWN },
tag_noshared = {"SHARE", " noshared", NULL, BT_UNKNOWN },
tag_e_share = {"SHARE", " share =", " %e", BT_CHARACTER },
tag_v_share = {"SHARE", " share =", " %v", BT_CHARACTER },
tag_cc = {"CARRIAGECONTROL", " carriagecontrol =", " %e",
BT_CHARACTER },
tag_v_cc = {"CARRIAGECONTROL", " carriagecontrol =", " %v",
BT_CHARACTER },
tag_file = {"FILE", " file =", " %e", BT_CHARACTER },
tag_status = {"STATUS", " status =", " %e", BT_CHARACTER},
tag_e_access = {"ACCESS", " access =", " %e", BT_CHARACTER},
tag_e_form = {"FORM", " form =", " %e", BT_CHARACTER},
tag_e_recl = {"RECL", " recl =", " %e", BT_INTEGER},
tag_e_blank = {"BLANK", " blank =", " %e", BT_CHARACTER},
tag_e_position = {"POSITION", " position =", " %e", BT_CHARACTER},
tag_e_action = {"ACTION", " action =", " %e", BT_CHARACTER},
tag_e_delim = {"DELIM", " delim =", " %e", BT_CHARACTER},
tag_e_pad = {"PAD", " pad =", " %e", BT_CHARACTER},
tag_e_decimal = {"DECIMAL", " decimal =", " %e", BT_CHARACTER},
tag_e_encoding = {"ENCODING", " encoding =", " %e", BT_CHARACTER},
tag_e_async = {"ASYNCHRONOUS", " asynchronous =", " %e", BT_CHARACTER},
tag_e_round = {"ROUND", " round =", " %e", BT_CHARACTER},
tag_e_sign = {"SIGN", " sign =", " %e", BT_CHARACTER},
tag_unit = {"UNIT", " unit =", " %e", BT_INTEGER},
tag_advance = {"ADVANCE", " advance =", " %e", BT_CHARACTER},
tag_rec = {"REC", " rec =", " %e", BT_INTEGER},
tag_spos = {"POSITION", " pos =", " %e", BT_INTEGER},
tag_format = {"FORMAT", NULL, NULL, BT_CHARACTER},
tag_iomsg = {"IOMSG", " iomsg =", " %e", BT_CHARACTER},
tag_iostat = {"IOSTAT", " iostat =", " %v", BT_INTEGER},
tag_size = {"SIZE", " size =", " %v", BT_INTEGER},
tag_exist = {"EXIST", " exist =", " %v", BT_LOGICAL},
tag_opened = {"OPENED", " opened =", " %v", BT_LOGICAL},
tag_named = {"NAMED", " named =", " %v", BT_LOGICAL},
tag_name = {"NAME", " name =", " %v", BT_CHARACTER},
tag_number = {"NUMBER", " number =", " %v", BT_INTEGER},
tag_s_access = {"ACCESS", " access =", " %v", BT_CHARACTER},
tag_sequential = {"SEQUENTIAL", " sequential =", " %v", BT_CHARACTER},
tag_direct = {"DIRECT", " direct =", " %v", BT_CHARACTER},
tag_s_form = {"FORM", " form =", " %v", BT_CHARACTER},
tag_formatted = {"FORMATTED", " formatted =", " %v", BT_CHARACTER},
tag_unformatted = {"UNFORMATTED", " unformatted =", " %v", BT_CHARACTER},
tag_s_recl = {"RECL", " recl =", " %v", BT_INTEGER},
tag_nextrec = {"NEXTREC", " nextrec =", " %v", BT_INTEGER},
tag_s_blank = {"BLANK", " blank =", " %v", BT_CHARACTER},
tag_s_position = {"POSITION", " position =", " %v", BT_CHARACTER},
tag_s_action = {"ACTION", " action =", " %v", BT_CHARACTER},
tag_read = {"READ", " read =", " %v", BT_CHARACTER},
tag_write = {"WRITE", " write =", " %v", BT_CHARACTER},
tag_readwrite = {"READWRITE", " readwrite =", " %v", BT_CHARACTER},
tag_s_delim = {"DELIM", " delim =", " %v", BT_CHARACTER},
tag_s_pad = {"PAD", " pad =", " %v", BT_CHARACTER},
tag_s_decimal = {"DECIMAL", " decimal =", " %v", BT_CHARACTER},
tag_s_encoding = {"ENCODING", " encoding =", " %v", BT_CHARACTER},
tag_s_async = {"ASYNCHRONOUS", " asynchronous =", " %v", BT_CHARACTER},
tag_s_round = {"ROUND", " round =", " %v", BT_CHARACTER},
tag_s_sign = {"SIGN", " sign =", " %v", BT_CHARACTER},
tag_iolength = {"IOLENGTH", " iolength =", " %v", BT_INTEGER},
tag_convert = {"CONVERT", " convert =", " %e", BT_CHARACTER},
tag_strm_out = {"POS", " pos =", " %v", BT_INTEGER},
tag_err = {"ERR", " err =", " %l", BT_UNKNOWN},
tag_end = {"END", " end =", " %l", BT_UNKNOWN},
tag_eor = {"EOR", " eor =", " %l", BT_UNKNOWN},
tag_id = {"ID", " id =", " %v", BT_INTEGER},
tag_pending = {"PENDING", " pending =", " %v", BT_LOGICAL},
tag_newunit = {"NEWUNIT", " newunit =", " %v", BT_INTEGER},
tag_s_iqstream = {"STREAM", " stream =", " %v", BT_CHARACTER};
static gfc_dt *current_dt;
#define RESOLVE_TAG(x, y) if (!resolve_tag (x, y)) return false;
/**************** Fortran 95 FORMAT parser *****************/
/* FORMAT tokens returned by format_lex(). */
enum format_token
{
FMT_NONE, FMT_UNKNOWN, FMT_SIGNED_INT, FMT_ZERO, FMT_POSINT, FMT_PERIOD,
FMT_COMMA, FMT_COLON, FMT_SLASH, FMT_DOLLAR, FMT_LPAREN,
FMT_RPAREN, FMT_X, FMT_SIGN, FMT_BLANK, FMT_CHAR, FMT_P, FMT_IBOZ, FMT_F,
FMT_E, FMT_EN, FMT_ES, FMT_G, FMT_L, FMT_A, FMT_D, FMT_H, FMT_END,
FMT_ERROR, FMT_DC, FMT_DP, FMT_T, FMT_TR, FMT_TL, FMT_STAR, FMT_RC,
FMT_RD, FMT_RN, FMT_RP, FMT_RU, FMT_RZ, FMT_DT
};
/* Local variables for checking format strings. The saved_token is
used to back up by a single format token during the parsing
process. */
static gfc_char_t *format_string;
static int format_string_pos;
static int format_length, use_last_char;
static char error_element;
static locus format_locus;
static format_token saved_token;
static enum
{ MODE_STRING, MODE_FORMAT, MODE_COPY }
mode;
/* Return the next character in the format string. */
static char
next_char (gfc_instring in_string)
{
static gfc_char_t c;
if (use_last_char)
{
use_last_char = 0;
return c;
}
format_length++;
if (mode == MODE_STRING)
c = *format_string++;
else
{
c = gfc_next_char_literal (in_string);
if (c == '\n')
c = '\0';
}
if (flag_backslash && c == '\\')
{
locus old_locus = gfc_current_locus;
if (gfc_match_special_char (&c) == MATCH_NO)
gfc_current_locus = old_locus;
if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
gfc_warning (0, "Extension: backslash character at %C");
}
if (mode == MODE_COPY)
*format_string++ = c;
if (mode != MODE_STRING)
format_locus = gfc_current_locus;
format_string_pos++;
c = gfc_wide_toupper (c);
return c;
}
/* Back up one character position. Only works once. */
static void
unget_char (void)
{
use_last_char = 1;
}
/* Eat up the spaces and return a character. */
static char
next_char_not_space ()
{
char c;
do
{
error_element = c = next_char (NONSTRING);
if (c == '\t')
gfc_warning (OPT_Wtabs, "Nonconforming tab character in format at %C");
}
while (gfc_is_whitespace (c));
return c;
}
static int value = 0;
/* Simple lexical analyzer for getting the next token in a FORMAT
statement. */
static format_token
format_lex (void)
{
format_token token;
char c, delim;
int zflag;
int negative_flag;
if (saved_token != FMT_NONE)
{
token = saved_token;
saved_token = FMT_NONE;
return token;
}
c = next_char_not_space ();
negative_flag = 0;
switch (c)
{
case '-':
negative_flag = 1;
/* Falls through. */
case '+':
c = next_char_not_space ();
if (!ISDIGIT (c))
{
token = FMT_UNKNOWN;
break;
}
value = c - '0';
do
{
c = next_char_not_space ();
if (ISDIGIT (c))
value = 10 * value + c - '0';
}
while (ISDIGIT (c));
unget_char ();
if (negative_flag)
value = -value;
token = FMT_SIGNED_INT;
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
zflag = (c == '0');
value = c - '0';
do
{
c = next_char_not_space ();
if (ISDIGIT (c))
{
value = 10 * value + c - '0';
if (c != '0')
zflag = 0;
}
}
while (ISDIGIT (c));
unget_char ();
token = zflag ? FMT_ZERO : FMT_POSINT;
break;
case '.':
token = FMT_PERIOD;
break;
case ',':
token = FMT_COMMA;
break;
case ':':
token = FMT_COLON;
break;
case '/':
token = FMT_SLASH;
break;
case '$':
token = FMT_DOLLAR;
break;
case 'T':
c = next_char_not_space ();
switch (c)
{
case 'L':
token = FMT_TL;
break;
case 'R':
token = FMT_TR;
break;
default:
token = FMT_T;
unget_char ();
}
break;
case '(':
token = FMT_LPAREN;
break;
case ')':
token = FMT_RPAREN;
break;
case 'X':
token = FMT_X;
break;
case 'S':
c = next_char_not_space ();
if (c != 'P' && c != 'S')
unget_char ();
token = FMT_SIGN;
break;
case 'B':
c = next_char_not_space ();
if (c == 'N' || c == 'Z')
token = FMT_BLANK;
else
{
unget_char ();
token = FMT_IBOZ;
}
break;
case '\'':
case '"':
delim = c;
value = 0;
for (;;)
{
c = next_char (INSTRING_WARN);
if (c == '\0')
{
token = FMT_END;
break;
}
if (c == delim)
{
c = next_char (NONSTRING);
if (c == '\0')
{
token = FMT_END;
break;
}
if (c != delim)
{
unget_char ();
token = FMT_CHAR;
break;
}
}
value++;
}
break;
case 'P':
token = FMT_P;
break;
case 'I':
case 'O':
case 'Z':
token = FMT_IBOZ;
break;
case 'F':
token = FMT_F;
break;
case 'E':
c = next_char_not_space ();
if (c == 'N' )
token = FMT_EN;
else if (c == 'S')
token = FMT_ES;
else
{
token = FMT_E;
unget_char ();
}
break;
case 'G':
token = FMT_G;
break;
case 'H':
token = FMT_H;
break;
case 'L':
token = FMT_L;
break;
case 'A':
token = FMT_A;
break;
case 'D':
c = next_char_not_space ();
if (c == 'P')
{
if (!gfc_notify_std (GFC_STD_F2003, "DP format "
"specifier not allowed at %C"))
return FMT_ERROR;
token = FMT_DP;
}
else if (c == 'C')
{
if (!gfc_notify_std (GFC_STD_F2003, "DC format "
"specifier not allowed at %C"))
return FMT_ERROR;
token = FMT_DC;
}
else if (c == 'T')
{
if (!gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DT format "
"specifier not allowed at %C"))
return FMT_ERROR;
token = FMT_DT;
c = next_char_not_space ();
if (c == '\'' || c == '"')
{
delim = c;
value = 0;
for (;;)
{
c = next_char (INSTRING_WARN);
if (c == '\0')
{
token = FMT_END;
break;
}
if (c == delim)
{
c = next_char (NONSTRING);
if (c == '\0')
{
token = FMT_END;
break;
}
if (c == '/')
{
token = FMT_SLASH;
break;
}
if (c == delim)
continue;
unget_char ();
break;
}
}
}
else if (c == '/')
{
token = FMT_SLASH;
break;
}
else
unget_char ();
}
else
{
token = FMT_D;
unget_char ();
}
break;
case 'R':
c = next_char_not_space ();
switch (c)
{
case 'C':
token = FMT_RC;
break;
case 'D':
token = FMT_RD;
break;
case 'N':
token = FMT_RN;
break;
case 'P':
token = FMT_RP;
break;
case 'U':
token = FMT_RU;
break;
case 'Z':
token = FMT_RZ;
break;
default:
token = FMT_UNKNOWN;
unget_char ();
break;
}
break;
case '\0':
token = FMT_END;
break;
case '*':
token = FMT_STAR;
break;
default:
token = FMT_UNKNOWN;
break;
}
return token;
}
static const char *
token_to_string (format_token t)
{
switch (t)
{
case FMT_D:
return "D";
case FMT_G:
return "G";
case FMT_E:
return "E";
case FMT_EN:
return "EN";
case FMT_ES:
return "ES";
default:
return "";
}
}
/* Check a format statement. The format string, either from a FORMAT
statement or a constant in an I/O statement has already been parsed
by itself, and we are checking it for validity. The dual origin
means that the warning message is a little less than great. */
static bool
check_format (bool is_input)
{
const char *posint_required
= G_("Positive width required in format string at %L");
const char *nonneg_required
= G_("Nonnegative width required in format string at %L");
const char *unexpected_element
= G_("Unexpected element %qc in format string at %L");
const char *unexpected_end
= G_("Unexpected end of format string in format string at %L");
const char *zero_width
= G_("Zero width in format descriptor in format string at %L");
const char *error = NULL;
format_token t, u;
int level;
int repeat;
bool rv;
use_last_char = 0;
saved_token = FMT_NONE;
level = 0;
repeat = 0;
rv = true;
format_string_pos = 0;
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t != FMT_LPAREN)
{
error = G_("Missing leading left parenthesis in format string at %L");
goto syntax;
}
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t == FMT_RPAREN)
goto finished; /* Empty format is legal */
saved_token = t;
format_item:
/* In this state, the next thing has to be a format item. */
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
format_item_1:
switch (t)
{
case FMT_STAR:
repeat = -1;
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t == FMT_LPAREN)
{
level++;
goto format_item;
}
error = G_("Left parenthesis required after %<*%> in format string "
"at %L");
goto syntax;
case FMT_POSINT:
repeat = value;
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t == FMT_LPAREN)
{
level++;
goto format_item;
}
if (t == FMT_SLASH)
goto optional_comma;
goto data_desc;
case FMT_LPAREN:
level++;
goto format_item;
case FMT_SIGNED_INT:
case FMT_ZERO:
/* Signed integer can only precede a P format. */
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t != FMT_P)
{
error = G_("Expected P edit descriptor in format string at %L");
goto syntax;
}
goto data_desc;
case FMT_P:
/* P requires a prior number. */
error = G_("P descriptor requires leading scale factor in format "
"string at %L");
goto syntax;
case FMT_X:
/* X requires a prior number if we're being pedantic. */
if (mode != MODE_FORMAT)
format_locus.nextc += format_string_pos;
if (!gfc_notify_std (GFC_STD_GNU, "X descriptor requires leading "
"space count at %L", &format_locus))
return false;
goto between_desc;
case FMT_SIGN:
case FMT_BLANK:
case FMT_DP:
case FMT_DC:
case FMT_RC:
case FMT_RD:
case FMT_RN:
case FMT_RP:
case FMT_RU:
case FMT_RZ:
goto between_desc;
case FMT_CHAR:
goto extension_optional_comma;
case FMT_COLON:
case FMT_SLASH:
goto optional_comma;
case FMT_DOLLAR:
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (!gfc_notify_std (GFC_STD_GNU, "$ descriptor at %L", &format_locus))
return false;
if (t != FMT_RPAREN || level > 0)
{
gfc_warning (0, "$ should be the last specifier in format at %L",
&format_locus);
goto optional_comma_1;
}
goto finished;
case FMT_T:
case FMT_TL:
case FMT_TR:
case FMT_IBOZ:
case FMT_F:
case FMT_E:
case FMT_EN:
case FMT_ES:
case FMT_G:
case FMT_L:
case FMT_A:
case FMT_D:
case FMT_H:
case FMT_DT:
goto data_desc;
case FMT_END:
error = unexpected_end;
goto syntax;
case FMT_RPAREN:
if (flag_dec_blank_format_item)
goto finished;
else
{
error = G_("Missing item in format string at %L");
goto syntax;
}
default:
error = unexpected_element;
goto syntax;
}
data_desc:
/* In this state, t must currently be a data descriptor.
Deal with things that can/must follow the descriptor. */
switch (t)
{
case FMT_SIGN:
case FMT_BLANK:
case FMT_DP:
case FMT_DC:
case FMT_X:
break;
case FMT_P:
/* No comma after P allowed only for F, E, EN, ES, D, or G.
10.1.1 (1). */
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (!(gfc_option.allow_std & GFC_STD_F2003) && t != FMT_COMMA
&& t != FMT_F && t != FMT_E && t != FMT_EN && t != FMT_ES
&& t != FMT_D && t != FMT_G && t != FMT_RPAREN && t != FMT_SLASH)
{
error = G_("Comma required after P descriptor in format string "
"at %L");
goto syntax;
}
if (t != FMT_COMMA)
{
if (t == FMT_POSINT)
{
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
}
if (t != FMT_F && t != FMT_E && t != FMT_EN && t != FMT_ES
&& t != FMT_D && t != FMT_G && t != FMT_RPAREN && t != FMT_SLASH)
{
error = G_("Comma required after P descriptor in format string "
"at %L");
goto syntax;
}
}
saved_token = t;
goto optional_comma;
case FMT_T:
case FMT_TL:
case FMT_TR:
t = format_lex ();
if (t != FMT_POSINT)
{
error = G_("Positive width required with T descriptor in format "
"string at %L");
goto syntax;
}
break;
case FMT_L:
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t == FMT_POSINT)
break;
if (mode != MODE_FORMAT)
format_locus.nextc += format_string_pos;
if (t == FMT_ZERO)
{
switch (gfc_notification_std (GFC_STD_GNU))
{
case WARNING:
gfc_warning (0, "Extension: Zero width after L "
"descriptor at %L", &format_locus);
break;
case ERROR:
gfc_error ("Extension: Zero width after L "
"descriptor at %L", &format_locus);
goto fail;
case SILENT:
break;
default:
gcc_unreachable ();
}
}
else
{
saved_token = t;
gfc_notify_std (GFC_STD_GNU, "Missing positive width after "
"L descriptor at %L", &format_locus);
}
break;
case FMT_A:
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t == FMT_ZERO)
{
error = zero_width;
goto syntax;
}
if (t != FMT_POSINT)
saved_token = t;
break;
case FMT_D:
case FMT_E:
case FMT_G:
case FMT_EN:
case FMT_ES:
u = format_lex ();
if (t == FMT_G && u == FMT_ZERO)
{
if (is_input)
{
error = zero_width;
goto syntax;
}
if (!gfc_notify_std (GFC_STD_F2008, "%<G0%> in format at %L",
&format_locus))
return false;
u = format_lex ();
if (u != FMT_PERIOD)
{
saved_token = u;
break;
}
u = format_lex ();
if (u != FMT_POSINT)
{
error = posint_required;
goto syntax;
}
u = format_lex ();
if (u == FMT_E)
{
error = G_("E specifier not allowed with g0 descriptor in "
"format string at %L");
goto syntax;
}
saved_token = u;
break;
}
if (u != FMT_POSINT)
{
if (flag_dec)
{
if (flag_dec_format_defaults)
{
/* Assume a default width based on the variable size. */
saved_token = u;
break;
}
else
{
gfc_error ("Positive width required in format "
"specifier %s at %L", token_to_string (t),
&format_locus);
saved_token = u;
goto fail;
}
}
format_locus.nextc += format_string_pos;
if (!gfc_notify_std (GFC_STD_F2018,
"positive width required at %L",
&format_locus))
{
saved_token = u;
goto fail;
}
if (flag_dec_format_defaults)
{
/* Assume a default width based on the variable size. */
saved_token = u;
break;
}
}
u = format_lex ();
if (u == FMT_ERROR)
goto fail;
if (u != FMT_PERIOD)
{
/* Warn if -std=legacy, otherwise error. */
format_locus.nextc += format_string_pos;
if (gfc_option.warn_std != 0)
{
gfc_error ("Period required in format "
"specifier %s at %L", token_to_string (t),
&format_locus);
saved_token = u;
goto fail;
}
else
gfc_warning (0, "Period required in format "
"specifier %s at %L", token_to_string (t),
&format_locus);
/* If we go to finished, we need to unwind this
before the next round. */
format_locus.nextc -= format_string_pos;
saved_token = u;
break;
}
u = format_lex ();
if (u == FMT_ERROR)
goto fail;
if (u != FMT_ZERO && u != FMT_POSINT)
{
error = nonneg_required;
goto syntax;
}
if (t == FMT_D)
break;
/* Look for optional exponent. */
u = format_lex ();
if (u == FMT_ERROR)
goto fail;
if (u != FMT_E)
saved_token = u;
else
{
u = format_lex ();
if (u == FMT_ERROR)
goto fail;
if (u != FMT_POSINT)
{
if (u == FMT_ZERO)
{
if (!gfc_notify_std (GFC_STD_F2018,
"Positive exponent width required in "
"format string at %L", &format_locus))
{
saved_token = u;
goto fail;
}
}
else
{
error = G_("Positive exponent width required in format "
"string at %L");
goto syntax;
}
}
}
break;
case FMT_DT:
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
switch (t)
{
case FMT_RPAREN:
level--;
if (level < 0)
goto finished;
goto between_desc;
case FMT_COMMA:
goto format_item;
case FMT_COLON:
goto format_item_1;
case FMT_LPAREN:
dtio_vlist:
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t != FMT_POSINT)
{
error = posint_required;
goto syntax;
}
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t == FMT_COMMA)
goto dtio_vlist;
if (t != FMT_RPAREN)
{
error = G_("Right parenthesis expected at %C in format string "
"at %L");
goto syntax;
}
goto between_desc;
default:
error = unexpected_element;
goto syntax;
}
break;
case FMT_F:
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t != FMT_ZERO && t != FMT_POSINT)
{
if (flag_dec_format_defaults)
{
/* Assume the default width is expected here and continue lexing. */
value = 0; /* It doesn't matter what we set the value to here. */
saved_token = t;
break;
}
error = nonneg_required;
goto syntax;
}
else if (is_input && t == FMT_ZERO)
{
error = posint_required;
goto syntax;
}
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t != FMT_PERIOD)
{
/* Warn if -std=legacy, otherwise error. */
if (gfc_option.warn_std != 0)
{
error = G_("Period required in format specifier in format "
"string at %L");
goto syntax;
}
if (mode != MODE_FORMAT)
format_locus.nextc += format_string_pos;
gfc_warning (0, "Period required in format specifier at %L",
&format_locus);
saved_token = t;
break;
}
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t != FMT_ZERO && t != FMT_POSINT)
{
error = nonneg_required;
goto syntax;
}
break;
case FMT_H:
if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
{
if (mode != MODE_FORMAT)
format_locus.nextc += format_string_pos;
gfc_warning (0, "The H format specifier at %L is"
" a Fortran 95 deleted feature", &format_locus);
}
if (mode == MODE_STRING)
{
format_string += value;
format_length -= value;
format_string_pos += repeat;
}
else
{
while (repeat >0)
{
next_char (INSTRING_WARN);
repeat -- ;
}
}
break;
case FMT_IBOZ:
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t != FMT_ZERO && t != FMT_POSINT)
{
if (flag_dec_format_defaults)
{
/* Assume the default width is expected here and continue lexing. */
value = 0; /* It doesn't matter what we set the value to here. */
saved_token = t;
}
else
{
error = nonneg_required;
goto syntax;
}
}
else if (is_input && t == FMT_ZERO)
{
error = posint_required;
goto syntax;
}
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t != FMT_PERIOD)
saved_token = t;
else
{
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
if (t != FMT_ZERO && t != FMT_POSINT)
{
error = nonneg_required;
goto syntax;
}
}
break;
default:
error = unexpected_element;
goto syntax;
}
between_desc:
/* Between a descriptor and what comes next. */
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
switch (t)
{
case FMT_COMMA:
goto format_item;
case FMT_RPAREN:
level--;
if (level < 0)
goto finished;
goto between_desc;
case FMT_COLON:
case FMT_SLASH:
goto optional_comma;
case FMT_END:
error = unexpected_end;
goto syntax;
default:
if (mode != MODE_FORMAT)
format_locus.nextc += format_string_pos - 1;
if (!gfc_notify_std (GFC_STD_GNU, "Missing comma at %L", &format_locus))
return false;
/* If we do not actually return a failure, we need to unwind this
before the next round. */
if (mode != MODE_FORMAT)
format_locus.nextc -= format_string_pos;
goto format_item_1;
}
optional_comma:
/* Optional comma is a weird between state where we've just finished
reading a colon, slash, dollar or P descriptor. */
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
optional_comma_1:
switch (t)
{
case FMT_COMMA:
break;
case FMT_RPAREN:
level--;
if (level < 0)
goto finished;
goto between_desc;
default:
/* Assume that we have another format item. */
saved_token = t;
break;
}
goto format_item;
extension_optional_comma:
/* As a GNU extension, permit a missing comma after a string literal. */
t = format_lex ();
if (t == FMT_ERROR)
goto fail;
switch (t)
{
case FMT_COMMA:
break;
case FMT_RPAREN:
level--;
if (level < 0)
goto finished;
goto between_desc;
case FMT_COLON:
case FMT_SLASH:
goto optional_comma;
case FMT_END:
error = unexpected_end;
goto syntax;
default:
if (mode != MODE_FORMAT)
format_locus.nextc += format_string_pos;
if (!gfc_notify_std (GFC_STD_GNU, "Missing comma at %L", &format_locus))
return false;
/* If we do not actually return a failure, we need to unwind this
before the next round. */
if (mode != MODE_FORMAT)
format_locus.nextc -= format_string_pos;
saved_token = t;
break;
}
goto format_item;
syntax:
if (mode != MODE_FORMAT)
format_locus.nextc += format_string_pos;
if (error == unexpected_element)
gfc_error (error, error_element, &format_locus);
else
gfc_error (error, &format_locus);
fail:
rv = false;
finished:
return rv;
}
/* Given an expression node that is a constant string, see if it looks
like a format string. */
static bool
check_format_string (gfc_expr *e, bool is_input)
{
bool rv;
int i;
if (!e || e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
return true;
mode = MODE_STRING;
format_string = e->value.character.string;
/* More elaborate measures are needed to show where a problem is within a
format string that has been calculated, but that's probably not worth the
effort. */
format_locus = e->where;
rv = check_format (is_input);
/* check for extraneous characters at the end of an otherwise valid format
string, like '(A10,I3)F5'
start at the end and move back to the last character processed,
spaces are OK */
if (rv && e->value.character.length > format_string_pos)
for (i=e->value.character.length-1;i>format_string_pos-1;i--)
if (e->value.character.string[i] != ' ')
{
format_locus.nextc += format_length + 1;
gfc_warning (0,
"Extraneous characters in format at %L", &format_locus);
break;
}
return rv;
}
/************ Fortran I/O statement matchers *************/
/* Match a FORMAT statement. This amounts to actually parsing the
format descriptors in order to correctly locate the end of the
format string. */
match
gfc_match_format (void)
{
gfc_expr *e;
locus start;
if (gfc_current_ns->proc_name
&& gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
{
gfc_error ("Format statement in module main block at %C");
return MATCH_ERROR;
}
/* Before parsing the rest of a FORMAT statement, check F2008:c1206. */
if ((gfc_current_state () == COMP_FUNCTION
|| gfc_current_state () == COMP_SUBROUTINE)
&& gfc_state_stack->previous->state == COMP_INTERFACE)
{
gfc_error ("FORMAT statement at %C cannot appear within an INTERFACE");
return MATCH_ERROR;
}
if (gfc_statement_label == NULL)
{
gfc_error ("Missing format label at %C");
return MATCH_ERROR;
}
gfc_gobble_whitespace ();
mode = MODE_FORMAT;
format_length = 0;
start = gfc_current_locus;
if (!check_format (false))
return MATCH_ERROR;
if (gfc_match_eos () != MATCH_YES)
{
gfc_syntax_error (ST_FORMAT);
return MATCH_ERROR;
}
/* The label doesn't get created until after the statement is done
being matched, so we have to leave the string for later. */
gfc_current_locus = start; /* Back to the beginning */
new_st.loc = start;
new_st.op = EXEC_NOP;
e = gfc_get_character_expr (gfc_default_character_kind, &start,
NULL, format_length);
format_string = e->value.character.string;
gfc_statement_label->format = e;
mode = MODE_COPY;
check_format (false); /* Guaranteed to succeed */
gfc_match_eos (); /* Guaranteed to succeed */
return MATCH_YES;
}
/* Match an expression I/O tag of some sort. */
static match
match_etag (const io_tag *tag, gfc_expr **v)
{
gfc_expr *result;
match m;
m = gfc_match (tag->spec);
if (m != MATCH_YES)
return m;
m = gfc_match (tag->value, &result);
if (m != MATCH_YES)
{
gfc_error ("Invalid value for %s specification at %C", tag->name);
return MATCH_ERROR;
}
if (*v != NULL)
{
gfc_error ("Duplicate %s specification at %C", tag->name);
gfc_free_expr (result);
return MATCH_ERROR;
}
*v = result;
return MATCH_YES;
}
/* Match a variable I/O tag of some sort. */
static match
match_vtag (const io_tag *tag, gfc_expr **v)
{
gfc_expr *result;
match m;
m = gfc_match (tag->spec);
if (m != MATCH_YES)
return m;
m = gfc_match (tag->value, &result);
if (m != MATCH_YES)
{
gfc_error ("Invalid value for %s specification at %C", tag->name);
return MATCH_ERROR;
}
if (*v != NULL)
{
gfc_error ("Duplicate %s specification at %C", tag->name);
gfc_free_expr (result);
return MATCH_ERROR;
}
if (result->symtree)
{
bool impure;
if (result->symtree->n.sym->attr.intent == INTENT_IN)
{
gfc_error ("Variable %s cannot be INTENT(IN) at %C", tag->name);
gfc_free_expr (result);
return MATCH_ERROR;
}
impure = gfc_impure_variable (result->symtree->n.sym);
if (impure && gfc_pure (NULL))
{
gfc_error ("Variable %s cannot be assigned in PURE procedure at %C",
tag->name);
gfc_free_expr (result);
return MATCH_ERROR;
}
if (impure)
gfc_unset_implicit_pure (NULL);
}
*v = result;
return MATCH_YES;
}
/* Match I/O tags that cause variables to become redefined. */
static match
match_out_tag (const io_tag *tag, gfc_expr **result)
{
match m;
m = match_vtag (tag, result);
if (m == MATCH_YES)
{
if ((*result)->symtree)
gfc_check_do_variable ((*result)->symtree);
if ((*result)->expr_type == EXPR_CONSTANT)
{
gfc_error ("Expecting a variable at %L", &(*result)->where);
return MATCH_ERROR;
}
}
return m;
}
/* Match a label I/O tag. */
static match
match_ltag (const io_tag *tag, gfc_st_label ** label)
{
match m;
gfc_st_label *old;
old = *label;
m = gfc_match (tag->spec);
if (m != MATCH_YES)
return m;
m = gfc_match (tag->value, label);
if (m != MATCH_YES)
{
gfc_error ("Invalid value for %s specification at %C", tag->name);
return MATCH_ERROR;
}
if (old)
{
gfc_error ("Duplicate %s label specification at %C", tag->name);
return MATCH_ERROR;
}
if (!gfc_reference_st_label (*label, ST_LABEL_TARGET))
return MATCH_ERROR;
return m;
}
/* Match a tag using match_etag, but only if -fdec is enabled. */
static match
match_dec_etag (const io_tag *tag, gfc_expr **e)
{
match m = match_etag (tag, e);
if (flag_dec && m != MATCH_NO)
return m;
else if (m != MATCH_NO)
{
gfc_error ("%s at %C is a DEC extension, enable with "
"%<-fdec%>", tag->name);
return MATCH_ERROR;
}
return m;
}
/* Match a tag using match_vtag, but only if -fdec is enabled. */
static match
match_dec_vtag (const io_tag *tag, gfc_expr **e)
{
match m = match_vtag(tag, e);
if (flag_dec && m != MATCH_NO)
return m;
else if (m != MATCH_NO)
{
gfc_error ("%s at %C is a DEC extension, enable with "
"%<-fdec%>", tag->name);
return MATCH_ERROR;
}
return m;
}
/* Match a DEC I/O flag tag - a tag with no expression such as READONLY. */
static match
match_dec_ftag (const io_tag *tag, gfc_open *o)
{
match m;
m = gfc_match (tag->spec);
if (m != MATCH_YES)
return m;
if (!flag_dec)
{
gfc_error ("%s at %C is a DEC extension, enable with "
"%<-fdec%>", tag->name);
return MATCH_ERROR;
}
/* Just set the READONLY flag, which we use at runtime to avoid delete on
close. */
if (tag == &tag_readonly)
{
o->readonly |= 1;
return MATCH_YES;
}
/* Interpret SHARED as SHARE='DENYNONE' (read lock). */
else if (tag == &tag_shared)
{
if (o->share != NULL)
{
gfc_error ("Duplicate %s specification at %C", tag->name);
return MATCH_ERROR;
}
o->share = gfc_get_character_expr (gfc_default_character_kind,
&gfc_current_locus, "denynone", 8);
return MATCH_YES;
}
/* Interpret NOSHARED as SHARE='DENYRW' (exclusive lock). */
else if (tag == &tag_noshared)
{
if (o->share != NULL)
{
gfc_error ("Duplicate %s specification at %C", tag->name);
return MATCH_ERROR;
}
o->share = gfc_get_character_expr (gfc_default_character_kind,
&gfc_current_locus, "denyrw", 6);
return MATCH_YES;
}
/* We handle all DEC tags above. */
gcc_unreachable ();
}
/* Resolution of the FORMAT tag, to be called from resolve_tag. */
static bool
resolve_tag_format (gfc_expr *e)
{
if (e->expr_type == EXPR_CONSTANT
&& (e->ts.type != BT_CHARACTER
|| e->ts.kind != gfc_default_character_kind))
{
gfc_error ("Constant expression in FORMAT tag at %L must be "
"of type default CHARACTER", &e->where);
return false;
}
/* Concatenate a constant character array into a single character
expression. */
if ((e->expr_type == EXPR_ARRAY || e->rank > 0)
&& e->ts.type == BT_CHARACTER
&& gfc_is_constant_expr (e))
{
if (e->expr_type == EXPR_VARIABLE
&& e->symtree->n.sym->attr.flavor == FL_PARAMETER)
gfc_simplify_expr (e, 1);
if (e->expr_type == EXPR_ARRAY)
{
gfc_constructor *c;
gfc_charlen_t n, len;
gfc_expr *r;
gfc_char_t *dest, *src;
if (e->value.constructor == NULL)
{
gfc_error ("FORMAT tag at %L cannot be a zero-sized array",
&e->where);
return false;
}
n = 0;
c = gfc_constructor_first (e->value.constructor);
len = c->expr->value.character.length;
for ( ; c; c = gfc_constructor_next (c))
n += len;
r = gfc_get_character_expr (e->ts.kind, &e->where, NULL, n);
dest = r->value.character.string;
for (c = gfc_constructor_first (e->value.constructor);
c; c = gfc_constructor_next (c))
{
src = c->expr->value.character.string;
for (gfc_charlen_t i = 0 ; i < len; i++)
*dest++ = *src++;
}
gfc_replace_expr (e, r);
return true;
}
}
/* If e's rank is zero and e is not an element of an array, it should be
of integer or character type. The integer variable should be
ASSIGNED. */
if (e->rank == 0
&& (e->expr_type != EXPR_VARIABLE
|| e->symtree == NULL
|| e->symtree->n.sym->as == NULL
|| e->symtree->n.sym->as->rank == 0))
{
if ((e->ts.type != BT_CHARACTER
|| e->ts.kind != gfc_default_character_kind)
&& e->ts.type != BT_INTEGER)
{
gfc_error ("FORMAT tag at %L must be of type default-kind CHARACTER "
"or of INTEGER", &e->where);
return false;
}
else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
{
if (!gfc_notify_std (GFC_STD_F95_DEL, "ASSIGNED variable in "
"FORMAT tag at %L", &e->where))
return false;
if (e->symtree->n.sym->attr.assign != 1)
{
gfc_error ("Variable %qs at %L has not been assigned a "
"format label", e->symtree->n.sym->name, &e->where);
return false;
}
}
else if (e->ts.type == BT_INTEGER)
{
gfc_error ("Scalar %qs in FORMAT tag at %L is not an ASSIGNED "
"variable", gfc_basic_typename (e->ts.type), &e->where);
return false;
}
return true;
}
/* If rank is nonzero and type is not character, we allow it under GFC_STD_LEGACY.
It may be assigned an Hollerith constant. */
if (e->ts.type != BT_CHARACTER)
{
if (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS
|| e->ts.type == BT_VOID || e->ts.type == BT_UNKNOWN)
{
gfc_error ("Non-character non-Hollerith in FORMAT tag at %L",
&e->where);
return false;
}
if (!gfc_notify_std (GFC_STD_LEGACY, "Non-character in FORMAT tag "
"at %L", &e->where))
return false;
if (e->rank == 0 && e->symtree->n.sym->as->type == AS_ASSUMED_SHAPE)
{
gfc_error ("Non-character assumed shape array element in FORMAT"
" tag at %L", &e->where);
return false;
}
if (e->rank == 0 && e->symtree->n.sym->as->type == AS_ASSUMED_SIZE)
{
gfc_error ("Non-character assumed size array element in FORMAT"
" tag at %L", &e->where);
return false;
}
if (e->rank == 0 && e->symtree->n.sym->attr.pointer)
{
gfc_error ("Non-character pointer array element in FORMAT tag at %L",
&e->where);
return false;
}
}
return true;
}
/* Do expression resolution and type-checking on an expression tag. */
static bool
resolve_tag (const io_tag *tag, gfc_expr *e)
{
if (e == NULL)
return true;
if (!gfc_resolve_expr (e))
return false;
if (tag == &tag_format)
return resolve_tag_format (e);
if (e->ts.type != tag->type)
{
gfc_error ("%s tag at %L must be of type %s", tag->name,
&e->where, gfc_basic_typename (tag->type));
return false;
}
if (e->ts.type == BT_CHARACTER && e->ts.kind != gfc_default_character_kind)
{
gfc_error ("%s tag at %L must be a character string of default kind",
tag->name, &e->where);
return false;
}
if (e->rank != 0)
{
gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
return false;
}
if (tag == &tag_iomsg)
{
if (!gfc_notify_std (GFC_STD_F2003, "IOMSG tag at %L", &e->where))
return false;
}
if ((tag == &tag_iostat || tag == &tag_size || tag == &tag_iolength
|| tag == &tag_number || tag == &tag_nextrec || tag == &tag_s_recl)
&& e->ts.kind != gfc_default_integer_kind)
{
if (!gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
"INTEGER in %s tag at %L", tag->name, &e->where))
return false;
}
if (e->ts.kind != gfc_default_logical_kind &&
(tag == &tag_exist || tag == &tag_named || tag == &tag_opened
|| tag == &tag_pending))
{
if (!gfc_notify_std (GFC_STD_F2008, "Non-default LOGICAL kind "
"in %s tag at %L", tag->name, &e->where))
return false;
}
if (tag == &tag_newunit)
{
if (!gfc_notify_std (GFC_STD_F2008, "NEWUNIT specifier at %L",
&e->where))
return false;
}
/* NEWUNIT, IOSTAT, SIZE and IOMSG are variable definition contexts. */
if (tag == &tag_newunit || tag == &tag_iostat
|| tag == &tag_size || tag == &tag_iomsg)
{
char context[64];
sprintf (context, _("%s tag"), tag->name);
if (!gfc_check_vardef_context (e, false, false, false, context))
return false;
}
if (tag == &tag_convert)
{
if (!gfc_notify_std (GFC_STD_GNU, "CONVERT tag at %L", &e->where))
return false;
}
return true;
}
/* Match a single tag of an OPEN statement. */
static match
match_open_element (gfc_open *open)
{
match m;
m = match_etag (&tag_e_async, &open->asynchronous);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_unit, &open->unit);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_iomsg, &open->iomsg);
if (m != MATCH_NO)
return m;
m = match_out_tag (&tag_iostat, &open->iostat);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_file, &open->file);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_status, &open->status);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_access, &open->access);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_form, &open->form);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_recl, &open->recl);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_blank, &open->blank);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_position, &open->position);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_action, &open->action);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_delim, &open->delim);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_pad, &open->pad);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_decimal, &open->decimal);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_encoding, &open->encoding);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_round, &open->round);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_e_sign, &open->sign);
if (m != MATCH_NO)
return m;
m = match_ltag (&tag_err, &open->err);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_convert, &open->convert);
if (m != MATCH_NO)
return m;
m = match_out_tag (&tag_newunit, &open->newunit);
if (m != MATCH_NO)
return m;
/* The following are extensions enabled with -fdec. */
m = match_dec_etag (&tag_e_share, &open->share);
if (m != MATCH_NO)
return m;
m = match_dec_etag (&tag_cc, &open->cc);
if (m != MATCH_NO)
return m;
m = match_dec_ftag (&tag_readonly, open);
if (m != MATCH_NO)
return m;
m = match_dec_ftag (&tag_shared, open);
if (m != MATCH_NO)
return m;
m = match_dec_ftag (&tag_noshared, open);
if (m != MATCH_NO)
return m;
return MATCH_NO;
}
/* Free the gfc_open structure and all the expressions it contains. */
void
gfc_free_open (gfc_open *open)
{
if (open == NULL)
return;
gfc_free_expr (open->unit);
gfc_free_expr (open->iomsg);
gfc_free_expr (open->iostat);
gfc_free_expr (open->file);
gfc_free_expr (open->status);
gfc_free_expr (open->access);
gfc_free_expr (open->form);
gfc_free_expr (open->recl);
gfc_free_expr (open->blank);
gfc_free_expr (open->position);
gfc_free_expr (open->action);
gfc_free_expr (open->delim);
gfc_free_expr (open->pad);
gfc_free_expr (open->decimal);
gfc_free_expr (open->encoding);
gfc_free_expr (open->round);
gfc_free_expr (open->sign);
gfc_free_expr (open->convert);
gfc_free_expr (open->asynchronous);
gfc_free_expr (open->newunit);
gfc_free_expr (open->share);
gfc_free_expr (open->cc);
free (open);
}
static int
compare_to_allowed_values (const char *specifier, const char *allowed[],
const char *allowed_f2003[],
const char *allowed_gnu[], gfc_char_t *value,
const char *statement, bool warn, locus *where,
int *num = NULL);
static bool
check_open_constraints (gfc_open *open, locus *where);
/* Resolve everything in a gfc_open structure. */
bool
gfc_resolve_open (gfc_open *open, locus *where)
{
RESOLVE_TAG (&tag_unit, open->unit);
RESOLVE_TAG (&tag_iomsg, open->iomsg);
RESOLVE_TAG (&tag_iostat, open->iostat);
RESOLVE_TAG (&tag_file, open->file);
RESOLVE_TAG (&tag_status, open->status);
RESOLVE_TAG (&tag_e_access, open->access);
RESOLVE_TAG (&tag_e_form, open->form);
RESOLVE_TAG (&tag_e_recl, open->recl);
RESOLVE_TAG (&tag_e_blank, open->blank);
RESOLVE_TAG (&tag_e_position, open->position);
RESOLVE_TAG (&tag_e_action, open->action);
RESOLVE_TAG (&tag_e_delim, open->delim);
RESOLVE_TAG (&tag_e_pad, open->pad);
RESOLVE_TAG (&tag_e_decimal, open->decimal);
RESOLVE_TAG (&tag_e_encoding, open->encoding);
RESOLVE_TAG (&tag_e_async, open->asynchronous);
RESOLVE_TAG (&tag_e_round, open->round);
RESOLVE_TAG (&tag_e_sign, open->sign);
RESOLVE_TAG (&tag_convert, open->convert);
RESOLVE_TAG (&tag_newunit, open->newunit);
RESOLVE_TAG (&tag_e_share, open->share);
RESOLVE_TAG (&tag_cc, open->cc);
if (!gfc_reference_st_label (open->err, ST_LABEL_TARGET))
return false;
return check_open_constraints (open, where);
}
/* Check if a given value for a SPECIFIER is either in the list of values
allowed in F95 or F2003, issuing an error message and returning a zero
value if it is not allowed. */
static int
compare_to_allowed_values (const char *specifier, const char *allowed[],
const char *allowed_f2003[],
const char *allowed_gnu[], gfc_char_t *value,
const char *statement, bool warn, locus *where,
int *num)
{
int i;
unsigned int len;
len = gfc_wide_strlen (value);
if (len > 0)
{
for (len--; len > 0; len--)
if (value[len] != ' ')
break;
len++;
}
for (i = 0; allowed[i]; i++)
if (len == strlen (allowed[i])
&& gfc_wide_strncasecmp (value, allowed[i], strlen (allowed[i])) == 0)
{
if (num)
*num = i;
return 1;
}
if (!where)
where = &gfc_current_locus;
for (i = 0; allowed_f2003 && allowed_f2003[i]; i++)
if (len == strlen (allowed_f2003[i])
&& gfc_wide_strncasecmp (value, allowed_f2003[i],
strlen (allowed_f2003[i])) == 0)
{
notification n = gfc_notification_std (GFC_STD_F2003);
if (n == WARNING || (warn && n == ERROR))
{
gfc_warning (0, "Fortran 2003: %s specifier in %s statement at %L "
"has value %qs", specifier, statement, where,
allowed_f2003[i]);
return 1;
}
else
if (n == ERROR)
{
gfc_notify_std (GFC_STD_F2003, "%s specifier in "
"%s statement at %L has value %qs", specifier,
statement, where, allowed_f2003[i]);
return 0;
}
/* n == SILENT */
return 1;
}
for (i = 0; allowed_gnu && allowed_gnu[i]; i++)
if (len == strlen (allowed_gnu[i])
&& gfc_wide_strncasecmp (value, allowed_gnu[i],
strlen (allowed_gnu[i])) == 0)
{
notification n = gfc_notification_std (GFC_STD_GNU);
if (n == WARNING || (warn && n == ERROR))
{
gfc_warning (0, "Extension: %s specifier in %s statement at %L "
"has value %qs", specifier, statement, where,
allowed_gnu[i]);
return 1;
}
else
if (n == ERROR)
{
gfc_notify_std (GFC_STD_GNU, "%s specifier in "
"%s statement at %L has value %qs", specifier,
statement, where, allowed_gnu[i]);
return 0;
}
/* n == SILENT */
return 1;
}
if (warn)
{
char *s = gfc_widechar_to_char (value, -1);
gfc_warning (0,
"%s specifier in %s statement at %L has invalid value %qs",
specifier, statement, where, s);
free (s);
return 1;
}
else
{
char *s = gfc_widechar_to_char (value, -1);
gfc_error ("%s specifier in %s statement at %L has invalid value %qs",
specifier, statement, where, s);
free (s);
return 0;
}
}
/* Check constraints on the OPEN statement.
Similar to check_io_constraints for data transfer statements.
At this point all tags have already been resolved via resolve_tag, which,
among other things, verifies that BT_CHARACTER tags are of default kind. */
static bool
check_open_constraints (gfc_open *open, locus *where)
{
#define warn_or_error(...) \
{ \
if (warn) \
gfc_warning (0, __VA_ARGS__); \
else \
{ \
gfc_error (__VA_ARGS__); \
return false; \
} \
}
bool warn = (open->err || open->iostat) ? true : false;
/* Checks on the ACCESS specifier. */
if (open->access && open->access->expr_type == EXPR_CONSTANT)
{
static const char *access_f95[] = { "SEQUENTIAL", "DIRECT", NULL };
static const char *access_f2003[] = { "STREAM", NULL };
static const char *access_gnu[] = { "APPEND", NULL };
if (!compare_to_allowed_values ("ACCESS", access_f95, access_f2003,
access_gnu,
open->access->value.character.string,
"OPEN", warn, &open->access->where))
return false;
}
/* Checks on the ACTION specifier. */
if (open->action && open->action->expr_type == EXPR_CONSTANT)
{
gfc_char_t *str = open->action->value.character.string;
static const char *action[] = { "READ", "WRITE", "READWRITE", NULL };
if (!compare_to_allowed_values ("ACTION", action, NULL, NULL,
str, "OPEN", warn, &open->action->where))
return false;
/* With READONLY, only allow ACTION='READ'. */
if (open->readonly && (gfc_wide_strlen (str) != 4
|| gfc_wide_strncasecmp (str, "READ", 4) != 0))
{
gfc_error ("ACTION type conflicts with READONLY specifier at %L",
&open->action->where);
return false;
}
}
/* If we see READONLY and no ACTION, set ACTION='READ'. */
else if (open->readonly && open->action == NULL)
{
open->action = gfc_get_character_expr (gfc_default_character_kind,
&gfc_current_locus, "read", 4);
}
/* Checks on the ASYNCHRONOUS specifier. */
if (open->asynchronous)
{
if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS= at %L "
"not allowed in Fortran 95",
&open->asynchronous->where))
return false;
if (open->asynchronous->expr_type == EXPR_CONSTANT)
{
static const char * asynchronous[] = { "YES", "NO", NULL };
if (!compare_to_allowed_values ("ASYNCHRONOUS", asynchronous,
NULL, NULL, open->asynchronous->value.character.string,
"OPEN", warn, &open->asynchronous->where))
return false;
}
}
/* Checks on the BLANK specifier. */
if (open->blank)
{
if (!gfc_notify_std (GFC_STD_F2003, "BLANK= at %L "
"not allowed in Fortran 95", &open->blank->where))
return false;
if (open->blank->expr_type == EXPR_CONSTANT)
{
static const char *blank[] = { "ZERO", "NULL", NULL };
if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
open->blank->value.character.string,
"OPEN", warn, &open->blank->where))
return false;
}
}
/* Checks on the CARRIAGECONTROL specifier. */
if (open->cc && open->cc->expr_type == EXPR_CONSTANT)
{
static const char *cc[] = { "LIST", "FORTRAN", "NONE", NULL };
if (!compare_to_allowed_values ("CARRIAGECONTROL", cc, NULL, NULL,
open->cc->value.character.string,
"OPEN", warn, &open->cc->where))
return false;
}
/* Checks on the DECIMAL specifier. */
if (open->decimal)
{
if (!gfc_notify_std (GFC_STD_F2003, "DECIMAL= at %L "
"not allowed in Fortran 95", &open->decimal->where))
return false;
if (open->decimal->expr_type == EXPR_CONSTANT)
{
static const char * decimal[] = { "COMMA", "POINT", NULL };
if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
open->decimal->value.character.string,
"OPEN", warn, &open->decimal->where))
return false;
}
}
/* Checks on the DELIM specifier. */
if (open->delim)
{
if (open->delim->expr_type == EXPR_CONSTANT)
{
static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
open->delim->value.character.string,
"OPEN", warn, &open->delim->where))
return false;
}
}
/* Checks on the ENCODING specifier. */
if (open->encoding)
{
if (!gfc_notify_std (GFC_STD_F2003, "ENCODING= at %L "
"not allowed in Fortran 95", &open->encoding->where))
return false;
if (open->encoding->expr_type == EXPR_CONSTANT)
{
static const char * encoding[] = { "DEFAULT", "UTF-8", NULL };
if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
open->encoding->value.character.string,
"OPEN", warn, &open->encoding->where))
return false;
}
}
/* Checks on the FORM specifier. */
if (open->form && open->form->expr_type == EXPR_CONSTANT)
{
static const char *form[] = { "FORMATTED", "UNFORMATTED", NULL };
if (!compare_to_allowed_values ("FORM", form, NULL, NULL,
open->form->value.character.string,
"OPEN", warn, &open->form->where))
return false;
}
/* Checks on the PAD specifier. */
if (open->pad && open->pad->expr_type == EXPR_CONSTANT)
{
static const char *pad[] = { "YES", "NO", NULL };
if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
open->pad->value.character.string,
"OPEN", warn, &open->pad->where))
return false;
}
/* Checks on the POSITION specifier. */
if (open->position && open->position->expr_type == EXPR_CONSTANT)
{
static const char *position[] = { "ASIS", "REWIND", "APPEND", NULL };
if (!compare_to_allowed_values ("POSITION", position, NULL, NULL,
open->position->value.character.string,
"OPEN", warn, &open->position->where))
return false;
}
/* Checks on the ROUND specifier. */
if (open->round)
{
if (!gfc_notify_std (GFC_STD_F2003, "ROUND= at %L "
"not allowed in Fortran 95", &open->round->where))
return false;
if (open->round->expr_type == EXPR_CONSTANT)
{
static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
"COMPATIBLE", "PROCESSOR_DEFINED",
NULL };
if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
open->round->value.character.string,
"OPEN", warn, &open->round->where))
return false;
}
}
/* Checks on the SHARE specifier. */
if (open->share && open->share->expr_type == EXPR_CONSTANT)
{
static const char *share[] = { "DENYNONE", "DENYRW", NULL };
if (!compare_to_allowed_values ("SHARE", share, NULL, NULL,
open->share->value.character.string,
"OPEN", warn, &open->share->where))
return false;
}
/* Checks on the SIGN specifier. */
if (open->sign)
{
if (!gfc_notify_std (GFC_STD_F2003, "SIGN= at %L "
"not allowed in Fortran 95", &open->sign->where))
return false;
if (open->sign->expr_type == EXPR_CONSTANT)
{
static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
NULL };
if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
open->sign->value.character.string,
"OPEN", warn, &open->sign->where))
return false;
}
}
/* Checks on the RECL specifier. */
if (open->recl && open->recl->expr_type == EXPR_CONSTANT
&& open->recl->ts.type == BT_INTEGER
&& mpz_sgn (open->recl->value.integer) != 1)
{
warn_or_error (G_("RECL in OPEN statement at %L must be positive"),
&open->recl->where);
}
/* Checks on the STATUS specifier. */
if (open->status && open->status->expr_type == EXPR_CONSTANT)
{
static const char *status[] = { "OLD", "NEW", "SCRATCH",
"REPLACE", "UNKNOWN", NULL };
if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
open->status->value.character.string,
"OPEN", warn, &open->status->where))
return false;
/* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
the FILE= specifier shall appear. */
if (open->file == NULL
&& (gfc_wide_strncasecmp (open->status->value.character.string,
"replace", 7) == 0
|| gfc_wide_strncasecmp (open->status->value.character.string,
"new", 3) == 0))
{
char *s = gfc_widechar_to_char (open->status->value.character.string,
-1);
warn_or_error (G_("The STATUS specified in OPEN statement at %L is "
"%qs and no FILE specifier is present"),
&open->status->where, s);
free (s);
}
/* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
the FILE= specifier shall not appear. */
if (gfc_wide_strncasecmp (open->status->value.character.string,
"scratch", 7) == 0 && open->file)
{
warn_or_error (G_("The STATUS specified in OPEN statement at %L "
"cannot have the value SCRATCH if a FILE specifier "
"is present"), &open->status->where);
}
}
/* Checks on NEWUNIT specifier. */
if (open->newunit)
{
if (open->unit)
{
gfc_error ("UNIT specifier not allowed with NEWUNIT at %L",
&open->newunit->where);
return false;
}
if (!open->file &&
(!open->status ||
(open->status->expr_type == EXPR_CONSTANT
&& gfc_wide_strncasecmp (open->status->value.character.string,
"scratch", 7) != 0)))
{
gfc_error ("NEWUNIT specifier must have FILE= "
"or STATUS='scratch' at %L", &open->newunit->where);
return false;
}
}
else if (!open->unit)
{
gfc_error ("OPEN statement at %L must have UNIT or NEWUNIT specified",
where);
return false;
}
/* Things that are not allowed for unformatted I/O. */
if (open->form && open->form->expr_type == EXPR_CONSTANT
&& (open->delim || open->decimal || open->encoding || open->round
|| open->sign || open->pad || open->blank)
&& gfc_wide_strncasecmp (open->form->value.character.string,
"unformatted", 11) == 0)
{
locus *loc;
const char *spec;
if (open->delim)
{
loc = &open->delim->where;
spec = "DELIM ";
}
else if (open->pad)
{
loc = &open->pad->where;
spec = "PAD ";
}
else if (open->blank)
{
loc = &open->blank->where;
spec = "BLANK ";
}
else
{
loc = where;
spec = "";
}
warn_or_error (G_("%s specifier at %L not allowed in OPEN statement for "
"unformatted I/O"), spec, loc);
}
if (open->recl && open->access && open->access->expr_type == EXPR_CONSTANT
&& gfc_wide_strncasecmp (open->access->value.character.string,
"stream", 6) == 0)
{
warn_or_error (G_("RECL specifier not allowed in OPEN statement at %L for "
"stream I/O"), &open->recl->where);
}
if (open->position
&& open->access && open->access->expr_type == EXPR_CONSTANT
&& !(gfc_wide_strncasecmp (open->access->value.character.string,
"sequential", 10) == 0
|| gfc_wide_strncasecmp (open->access->value.character.string,
"stream", 6) == 0
|| gfc_wide_strncasecmp (open->access->value.character.string,
"append", 6) == 0))
{
warn_or_error (G_("POSITION specifier in OPEN statement at %L only allowed "
"for stream or sequential ACCESS"), &open->position->where);
}
return true;
#undef warn_or_error
}
/* Match an OPEN statement. */
match
gfc_match_open (void)
{
gfc_open *open;
match m;
m = gfc_match_char ('(');
if (m == MATCH_NO)
return m;
open = XCNEW (gfc_open);
m = match_open_element (open);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
{
m = gfc_match_expr (&open->unit);
if (m == MATCH_ERROR)
goto cleanup;
}
for (;;)
{
if (gfc_match_char (')') == MATCH_YES)
break;
if (gfc_match_char (',') != MATCH_YES)
goto syntax;
m = match_open_element (open);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
goto syntax;
}
if (gfc_match_eos () == MATCH_NO)
goto syntax;
if (gfc_pure (NULL))
{
gfc_error ("OPEN statement not allowed in PURE procedure at %C");
goto cleanup;
}
gfc_unset_implicit_pure (NULL);
new_st.op = EXEC_OPEN;
new_st.ext.open = open;
return MATCH_YES;
syntax:
gfc_syntax_error (ST_OPEN);
cleanup:
gfc_free_open (open);
return MATCH_ERROR;
}
/* Free a gfc_close structure an all its expressions. */
void
gfc_free_close (gfc_close *close)
{
if (close == NULL)
return;
gfc_free_expr (close->unit);
gfc_free_expr (close->iomsg);
gfc_free_expr (close->iostat);
gfc_free_expr (close->status);
free (close);
}
/* Match elements of a CLOSE statement. */
static match
match_close_element (gfc_close *close)
{
match m;
m = match_etag (&tag_unit, &close->unit);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_status, &close->status);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_iomsg, &close->iomsg);
if (m != MATCH_NO)
return m;
m = match_out_tag (&tag_iostat, &close->iostat);
if (m != MATCH_NO)
return m;
m = match_ltag (&tag_err, &close->err);
if (m != MATCH_NO)
return m;
return MATCH_NO;
}
/* Match a CLOSE statement. */
match
gfc_match_close (void)
{
gfc_close *close;
match m;
m = gfc_match_char ('(');
if (m == MATCH_NO)
return m;
close = XCNEW (gfc_close);
m = match_close_element (close);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
{
m = gfc_match_expr (&close->unit);
if (m == MATCH_NO)
goto syntax;
if (m == MATCH_ERROR)
goto cleanup;
}
for (;;)
{
if (gfc_match_char (')') == MATCH_YES)
break;
if (gfc_match_char (',') != MATCH_YES)
goto syntax;
m = match_close_element (close);
if (m == MATCH_ERROR)
goto cleanup;
if (m == MATCH_NO)
goto syntax;
}
if (gfc_match_eos () == MATCH_NO)
goto syntax;
if (gfc_pure (NULL))
{
gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
goto cleanup;
}
gfc_unset_implicit_pure (NULL);
new_st.op = EXEC_CLOSE;
new_st.ext.close = close;
return MATCH_YES;
syntax:
gfc_syntax_error (ST_CLOSE);
cleanup:
gfc_free_close (close);
return MATCH_ERROR;
}
static bool
check_close_constraints (gfc_close *close, locus *where)
{
bool warn = (close->iostat || close->err) ? true : false;
if (close->unit == NULL)
{
gfc_error ("CLOSE statement at %L requires a UNIT number", where);
return false;
}
if (close->unit->expr_type == EXPR_CONSTANT
&& close->unit->ts.type == BT_INTEGER
&& mpz_sgn (close->unit->value.integer) < 0)
{
gfc_error ("UNIT number in CLOSE statement at %L must be non-negative",
&close->unit->where);
}
/* Checks on the STATUS specifier. */
if (close->status && close->status->expr_type == EXPR_CONSTANT)
{
static const char *status[] = { "KEEP", "DELETE", NULL };
if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
close->status->value.character.string,
"CLOSE", warn, &close->status->where))
return false;
}
return true;
}
/* Resolve everything in a gfc_close structure. */
bool
gfc_resolve_close (gfc_close *close, locus *where)
{
RESOLVE_TAG (&tag_unit, close->unit);
RESOLVE_TAG (&tag_iomsg, close->iomsg);
RESOLVE_TAG (&tag_iostat, close->iostat);
RESOLVE_TAG (&tag_status, close->status);
if (!gfc_reference_st_label (close->err, ST_LABEL_TARGET))
return false;
return check_close_constraints (close, where);
}
/* Free a gfc_filepos structure. */
void
gfc_free_filepos (gfc_filepos *fp)
{
gfc_free_expr (fp->unit);
gfc_free_expr (fp->iomsg);
gfc_free_expr (fp->iostat);
free (fp);
}
/* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
static match
match_file_element (gfc_filepos *fp)
{
match m;
m = match_etag (&tag_unit, &fp->unit);
if (m != MATCH_NO)
return m;
m = match_etag (&tag_iomsg, &fp->iomsg);
if (m != MATCH_NO)
return m;
m = match_out_tag (&tag_iostat, &fp->iostat);
if (m != MATCH_NO)
return m;
m = match_ltag (&tag_err, &fp->err);
if (m != MATCH_NO)
return m;