1 /* Deal with I/O statements & related stuff.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
30 format_asterisk = {0, NULL, NULL, -1, ST_LABEL_FORMAT, ST_LABEL_FORMAT, NULL,
35 const char *name, *spec;
41 tag_file = { "FILE", " file = %e", BT_CHARACTER },
42 tag_status = { "STATUS", " status = %e", BT_CHARACTER},
43 tag_e_access = {"ACCESS", " access = %e", BT_CHARACTER},
44 tag_e_form = {"FORM", " form = %e", BT_CHARACTER},
45 tag_e_recl = {"RECL", " recl = %e", BT_INTEGER},
46 tag_e_blank = {"BLANK", " blank = %e", BT_CHARACTER},
47 tag_e_position = {"POSITION", " position = %e", BT_CHARACTER},
48 tag_e_action = {"ACTION", " action = %e", BT_CHARACTER},
49 tag_e_delim = {"DELIM", " delim = %e", BT_CHARACTER},
50 tag_e_pad = {"PAD", " pad = %e", BT_CHARACTER},
51 tag_unit = {"UNIT", " unit = %e", BT_INTEGER},
52 tag_advance = {"ADVANCE", " advance = %e", BT_CHARACTER},
53 tag_rec = {"REC", " rec = %e", BT_INTEGER},
54 tag_spos = {"POSITION", " pos = %e", BT_INTEGER},
55 tag_format = {"FORMAT", NULL, BT_CHARACTER},
56 tag_iomsg = {"IOMSG", " iomsg = %e", BT_CHARACTER},
57 tag_iostat = {"IOSTAT", " iostat = %v", BT_INTEGER},
58 tag_size = {"SIZE", " size = %v", BT_INTEGER},
59 tag_exist = {"EXIST", " exist = %v", BT_LOGICAL},
60 tag_opened = {"OPENED", " opened = %v", BT_LOGICAL},
61 tag_named = {"NAMED", " named = %v", BT_LOGICAL},
62 tag_name = {"NAME", " name = %v", BT_CHARACTER},
63 tag_number = {"NUMBER", " number = %v", BT_INTEGER},
64 tag_s_access = {"ACCESS", " access = %v", BT_CHARACTER},
65 tag_sequential = {"SEQUENTIAL", " sequential = %v", BT_CHARACTER},
66 tag_direct = {"DIRECT", " direct = %v", BT_CHARACTER},
67 tag_s_form = {"FORM", " form = %v", BT_CHARACTER},
68 tag_formatted = {"FORMATTED", " formatted = %v", BT_CHARACTER},
69 tag_unformatted = {"UNFORMATTED", " unformatted = %v", BT_CHARACTER},
70 tag_s_recl = {"RECL", " recl = %v", BT_INTEGER},
71 tag_nextrec = {"NEXTREC", " nextrec = %v", BT_INTEGER},
72 tag_s_blank = {"BLANK", " blank = %v", BT_CHARACTER},
73 tag_s_position = {"POSITION", " position = %v", BT_CHARACTER},
74 tag_s_action = {"ACTION", " action = %v", BT_CHARACTER},
75 tag_read = {"READ", " read = %v", BT_CHARACTER},
76 tag_write = {"WRITE", " write = %v", BT_CHARACTER},
77 tag_readwrite = {"READWRITE", " readwrite = %v", BT_CHARACTER},
78 tag_s_delim = {"DELIM", " delim = %v", BT_CHARACTER},
79 tag_s_pad = {"PAD", " pad = %v", BT_CHARACTER},
80 tag_iolength = {"IOLENGTH", " iolength = %v", BT_INTEGER},
81 tag_convert = {"CONVERT", " convert = %e", BT_CHARACTER},
82 tag_strm_out = {"POS", " pos = %v", BT_INTEGER},
83 tag_err = {"ERR", " err = %l", BT_UNKNOWN},
84 tag_end = {"END", " end = %l", BT_UNKNOWN},
85 tag_eor = {"EOR", " eor = %l", BT_UNKNOWN};
87 static gfc_dt *current_dt;
89 #define RESOLVE_TAG(x, y) if (resolve_tag(x, y) == FAILURE) return FAILURE;
92 /**************** Fortran 95 FORMAT parser *****************/
94 /* FORMAT tokens returned by format_lex(). */
97 FMT_NONE, FMT_UNKNOWN, FMT_SIGNED_INT, FMT_ZERO, FMT_POSINT, FMT_PERIOD,
98 FMT_COMMA, FMT_COLON, FMT_SLASH, FMT_DOLLAR, FMT_POS, FMT_LPAREN,
99 FMT_RPAREN, FMT_X, FMT_SIGN, FMT_BLANK, FMT_CHAR, FMT_P, FMT_IBOZ, FMT_F,
100 FMT_E, FMT_EXT, FMT_G, FMT_L, FMT_A, FMT_D, FMT_H, FMT_END, FMT_ERROR
104 /* Local variables for checking format strings. The saved_token is
105 used to back up by a single format token during the parsing
107 static char *format_string;
108 static int format_length, use_last_char;
110 static format_token saved_token;
113 { MODE_STRING, MODE_FORMAT, MODE_COPY }
117 /* Return the next character in the format string. */
120 next_char (int in_string)
132 if (mode == MODE_STRING)
133 c = *format_string++;
136 c = gfc_next_char_literal (in_string);
141 if (gfc_option.flag_backslash && c == '\\')
144 locus old_locus = gfc_current_locus;
146 /* Use a temp variable to avoid side effects from gfc_match_special_char
147 since it uses an int * for its argument. */
150 if (gfc_match_special_char (&tmp) == MATCH_NO)
151 gfc_current_locus = old_locus;
155 if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
156 gfc_warning ("Extension: backslash character at %C");
159 if (mode == MODE_COPY)
160 *format_string++ = c;
167 /* Back up one character position. Only works once. */
175 /* Eat up the spaces and return a character. */
178 next_char_not_space (bool *error)
186 if (gfc_option.allow_std & GFC_STD_GNU)
187 gfc_warning ("Extension: Tab character in format at %C");
190 gfc_error ("Extension: Tab character in format at %C");
196 while (gfc_is_whitespace (c));
200 static int value = 0;
202 /* Simple lexical analyzer for getting the next token in a FORMAT
214 if (saved_token != FMT_NONE)
217 saved_token = FMT_NONE;
221 c = next_char_not_space (&error);
229 c = next_char_not_space (&error);
240 c = next_char_not_space (&error);
242 value = 10 * value + c - '0';
251 token = FMT_SIGNED_INT;
270 c = next_char_not_space (&error);
273 value = 10 * value + c - '0';
281 token = zflag ? FMT_ZERO : FMT_POSINT;
305 c = next_char_not_space (&error);
306 if (c != 'L' && c != 'R')
325 c = next_char_not_space (&error);
326 if (c != 'P' && c != 'S')
333 c = next_char_not_space (&error);
334 if (c == 'N' || c == 'Z')
395 c = next_char_not_space (&error);
396 if (c == 'N' || c == 'S')
442 /* Check a format statement. The format string, either from a FORMAT
443 statement or a constant in an I/O statement has already been parsed
444 by itself, and we are checking it for validity. The dual origin
445 means that the warning message is a little less than great. */
448 check_format (bool is_input)
450 const char *posint_required = _("Positive width required");
451 const char *nonneg_required = _("Nonnegative width required");
452 const char *unexpected_element = _("Unexpected element");
453 const char *unexpected_end = _("Unexpected end of format string");
462 saved_token = FMT_NONE;
472 error = _("Missing leading left parenthesis");
480 goto finished; /* Empty format is legal */
484 /* In this state, the next thing has to be a format item. */
513 /* Signed integer can only precede a P format. */
519 error = _("Expected P edit descriptor");
526 /* P requires a prior number. */
527 error = _("P descriptor requires leading scale factor");
531 /* X requires a prior number if we're being pedantic. */
532 if (gfc_notify_std (GFC_STD_GNU, "Extension: X descriptor "
533 "requires leading space count at %C")
543 goto extension_optional_comma;
554 if (gfc_notify_std (GFC_STD_GNU, "Extension: $ descriptor at %C")
557 if (t != FMT_RPAREN || level > 0)
559 gfc_warning ("$ should be the last specifier in format at %C");
560 goto optional_comma_1;
578 error = unexpected_end;
582 error = unexpected_element;
587 /* In this state, t must currently be a data descriptor.
588 Deal with things that can/must follow the descriptor. */
604 error = _("Repeat count cannot follow P descriptor");
621 switch (gfc_notification_std (GFC_STD_GNU))
624 gfc_warning ("Extension: Missing positive width after L "
630 error = posint_required;
659 error = posint_required;
668 /* Warn if -std=legacy, otherwise error. */
669 if (gfc_option.warn_std != 0)
670 gfc_error_now ("Period required in format specifier at %C");
672 gfc_warning ("Period required in format specifier at %C");
680 if (u != FMT_ZERO && u != FMT_POSINT)
682 error = nonneg_required;
689 /* Look for optional exponent. */
704 error = _("Positive exponent width required");
715 if (t != FMT_ZERO && t != FMT_POSINT)
717 error = nonneg_required;
720 else if (is_input && t == FMT_ZERO)
722 error = posint_required;
731 /* Warn if -std=legacy, otherwise error. */
732 if (gfc_option.warn_std != 0)
733 gfc_error_now ("Period required in format specifier at %C");
735 gfc_warning ("Period required in format specifier at %C");
743 if (t != FMT_ZERO && t != FMT_POSINT)
745 error = nonneg_required;
752 if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
753 gfc_warning ("The H format specifier at %C is"
754 " a Fortran 95 deleted feature");
756 if(mode == MODE_STRING)
758 format_string += value;
759 format_length -= value;
775 if (t != FMT_ZERO && t != FMT_POSINT)
777 error = nonneg_required;
780 else if (is_input && t == FMT_ZERO)
782 error = posint_required;
798 if (t != FMT_ZERO && t != FMT_POSINT)
800 error = nonneg_required;
808 error = unexpected_element;
813 /* Between a descriptor and what comes next. */
834 error = unexpected_end;
838 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
845 /* Optional comma is a weird between state where we've just finished
846 reading a colon, slash, dollar or P descriptor. */
863 /* Assume that we have another format item. */
870 extension_optional_comma:
871 /* As a GNU extension, permit a missing comma after a string literal. */
891 error = unexpected_end;
895 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
905 gfc_error ("%s in format string at %C", error);
907 /* TODO: More elaborate measures are needed to show where a problem
908 is within a format string that has been calculated. */
916 /* Given an expression node that is a constant string, see if it looks
917 like a format string. */
920 check_format_string (gfc_expr *e, bool is_input)
923 format_string = e->value.character.string;
924 return check_format (is_input);
928 /************ Fortran 95 I/O statement matchers *************/
930 /* Match a FORMAT statement. This amounts to actually parsing the
931 format descriptors in order to correctly locate the end of the
935 gfc_match_format (void)
940 if (gfc_current_ns->proc_name
941 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
943 gfc_error ("Format statement in module main block at %C");
947 if (gfc_statement_label == NULL)
949 gfc_error ("Missing format label at %C");
952 gfc_gobble_whitespace ();
957 start = gfc_current_locus;
959 if (check_format (false) == FAILURE)
962 if (gfc_match_eos () != MATCH_YES)
964 gfc_syntax_error (ST_FORMAT);
968 /* The label doesn't get created until after the statement is done
969 being matched, so we have to leave the string for later. */
971 gfc_current_locus = start; /* Back to the beginning */
974 new_st.op = EXEC_NOP;
977 e->expr_type = EXPR_CONSTANT;
978 e->ts.type = BT_CHARACTER;
979 e->ts.kind = gfc_default_character_kind;
981 e->value.character.string = format_string = gfc_getmem (format_length + 1);
982 e->value.character.length = format_length;
983 gfc_statement_label->format = e;
986 check_format (false); /* Guaranteed to succeed */
987 gfc_match_eos (); /* Guaranteed to succeed */
993 /* Match an expression I/O tag of some sort. */
996 match_etag (const io_tag *tag, gfc_expr **v)
1001 m = gfc_match (tag->spec, &result);
1007 gfc_error ("Duplicate %s specification at %C", tag->name);
1008 gfc_free_expr (result);
1017 /* Match a variable I/O tag of some sort. */
1020 match_vtag (const io_tag *tag, gfc_expr **v)
1025 m = gfc_match (tag->spec, &result);
1031 gfc_error ("Duplicate %s specification at %C", tag->name);
1032 gfc_free_expr (result);
1036 if (result->symtree->n.sym->attr.intent == INTENT_IN)
1038 gfc_error ("Variable tag cannot be INTENT(IN) at %C");
1039 gfc_free_expr (result);
1043 if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
1045 gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
1046 gfc_free_expr (result);
1055 /* Match I/O tags that cause variables to become redefined. */
1058 match_out_tag(const io_tag *tag, gfc_expr **result)
1062 m = match_vtag(tag, result);
1064 gfc_check_do_variable((*result)->symtree);
1070 /* Match a label I/O tag. */
1073 match_ltag (const io_tag *tag, gfc_st_label ** label)
1079 m = gfc_match (tag->spec, label);
1080 if (m == MATCH_YES && old != 0)
1082 gfc_error ("Duplicate %s label specification at %C", tag->name);
1087 && gfc_reference_st_label (*label, ST_LABEL_TARGET) == FAILURE)
1094 /* Resolution of the FORMAT tag, to be called from resolve_tag. */
1097 resolve_tag_format (const gfc_expr *e)
1099 if (e->expr_type == EXPR_CONSTANT
1100 && (e->ts.type != BT_CHARACTER
1101 || e->ts.kind != gfc_default_character_kind))
1103 gfc_error ("Constant expression in FORMAT tag at %L must be "
1104 "of type default CHARACTER", &e->where);
1108 /* If e's rank is zero and e is not an element of an array, it should be
1109 of integer or character type. The integer variable should be
1111 if (e->symtree == NULL || e->symtree->n.sym->as == NULL
1112 || e->symtree->n.sym->as->rank == 0)
1114 if (e->ts.type != BT_CHARACTER && e->ts.type != BT_INTEGER)
1116 gfc_error ("FORMAT tag at %L must be of type CHARACTER or INTEGER",
1120 else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
1122 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGNED "
1123 "variable in FORMAT tag at %L", &e->where)
1126 if (e->symtree->n.sym->attr.assign != 1)
1128 gfc_error ("Variable '%s' at %L has not been assigned a "
1129 "format label", e->symtree->n.sym->name, &e->where);
1133 else if (e->ts.type == BT_INTEGER)
1135 gfc_error ("Scalar '%s' in FORMAT tag at %L is not an ASSIGNED "
1136 "variable", gfc_basic_typename (e->ts.type), &e->where);
1143 /* If rank is nonzero, we allow the type to be character under GFC_STD_GNU
1144 and other type under GFC_STD_LEGACY. It may be assigned an Hollerith
1146 if (e->ts.type == BT_CHARACTER)
1148 if (gfc_notify_std (GFC_STD_GNU, "Extension: Character array "
1149 "in FORMAT tag at %L", &e->where) == FAILURE)
1154 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: Non-character "
1155 "in FORMAT tag at %L", &e->where) == FAILURE)
1163 /* Do expression resolution and type-checking on an expression tag. */
1166 resolve_tag (const io_tag *tag, gfc_expr *e)
1171 if (gfc_resolve_expr (e) == FAILURE)
1174 if (tag == &tag_format)
1175 return resolve_tag_format (e);
1177 if (e->ts.type != tag->type)
1179 gfc_error ("%s tag at %L must be of type %s", tag->name,
1180 &e->where, gfc_basic_typename (tag->type));
1186 gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
1190 if (tag == &tag_iomsg)
1192 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IOMSG tag at %L",
1193 &e->where) == FAILURE)
1197 if ((tag == &tag_iostat || tag == &tag_size || tag == &tag_iolength)
1198 && e->ts.kind != gfc_default_integer_kind)
1200 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1201 "INTEGER in %s tag at %L", tag->name, &e->where)
1206 if (tag == &tag_convert)
1208 if (gfc_notify_std (GFC_STD_GNU, "Extension: CONVERT tag at %L",
1209 &e->where) == FAILURE)
1217 /* Match a single tag of an OPEN statement. */
1220 match_open_element (gfc_open *open)
1224 m = match_etag (&tag_unit, &open->unit);
1227 m = match_out_tag (&tag_iomsg, &open->iomsg);
1230 m = match_out_tag (&tag_iostat, &open->iostat);
1233 m = match_etag (&tag_file, &open->file);
1236 m = match_etag (&tag_status, &open->status);
1239 m = match_etag (&tag_e_access, &open->access);
1242 m = match_etag (&tag_e_form, &open->form);
1245 m = match_etag (&tag_e_recl, &open->recl);
1248 m = match_etag (&tag_e_blank, &open->blank);
1251 m = match_etag (&tag_e_position, &open->position);
1254 m = match_etag (&tag_e_action, &open->action);
1257 m = match_etag (&tag_e_delim, &open->delim);
1260 m = match_etag (&tag_e_pad, &open->pad);
1263 m = match_ltag (&tag_err, &open->err);
1266 m = match_etag (&tag_convert, &open->convert);
1274 /* Free the gfc_open structure and all the expressions it contains. */
1277 gfc_free_open (gfc_open *open)
1282 gfc_free_expr (open->unit);
1283 gfc_free_expr (open->iomsg);
1284 gfc_free_expr (open->iostat);
1285 gfc_free_expr (open->file);
1286 gfc_free_expr (open->status);
1287 gfc_free_expr (open->access);
1288 gfc_free_expr (open->form);
1289 gfc_free_expr (open->recl);
1290 gfc_free_expr (open->blank);
1291 gfc_free_expr (open->position);
1292 gfc_free_expr (open->action);
1293 gfc_free_expr (open->delim);
1294 gfc_free_expr (open->pad);
1295 gfc_free_expr (open->convert);
1300 /* Resolve everything in a gfc_open structure. */
1303 gfc_resolve_open (gfc_open *open)
1306 RESOLVE_TAG (&tag_unit, open->unit);
1307 RESOLVE_TAG (&tag_iomsg, open->iomsg);
1308 RESOLVE_TAG (&tag_iostat, open->iostat);
1309 RESOLVE_TAG (&tag_file, open->file);
1310 RESOLVE_TAG (&tag_status, open->status);
1311 RESOLVE_TAG (&tag_e_access, open->access);
1312 RESOLVE_TAG (&tag_e_form, open->form);
1313 RESOLVE_TAG (&tag_e_recl, open->recl);
1314 RESOLVE_TAG (&tag_e_blank, open->blank);
1315 RESOLVE_TAG (&tag_e_position, open->position);
1316 RESOLVE_TAG (&tag_e_action, open->action);
1317 RESOLVE_TAG (&tag_e_delim, open->delim);
1318 RESOLVE_TAG (&tag_e_pad, open->pad);
1319 RESOLVE_TAG (&tag_convert, open->convert);
1321 if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1328 /* Check if a given value for a SPECIFIER is either in the list of values
1329 allowed in F95 or F2003, issuing an error message and returning a zero
1330 value if it is not allowed. */
1333 compare_to_allowed_values (const char *specifier, const char *allowed[],
1334 const char *allowed_f2003[],
1335 const char *allowed_gnu[], char *value,
1336 const char *statement, bool warn)
1341 len = strlen (value);
1344 for (len--; len > 0; len--)
1345 if (value[len] != ' ')
1350 for (i = 0; allowed[i]; i++)
1351 if (len == strlen (allowed[i])
1352 && strncasecmp (value, allowed[i], strlen (allowed[i])) == 0)
1355 for (i = 0; allowed_f2003 && allowed_f2003[i]; i++)
1356 if (len == strlen (allowed_f2003[i])
1357 && strncasecmp (value, allowed_f2003[i], strlen (allowed_f2003[i]))
1360 notification n = gfc_notification_std (GFC_STD_F2003);
1362 if (n == WARNING || (warn && n == ERROR))
1364 gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1365 "has value '%s'", specifier, statement,
1372 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: %s specifier in "
1373 "%s statement at %C has value '%s'", specifier,
1374 statement, allowed_f2003[i]);
1382 for (i = 0; allowed_gnu && allowed_gnu[i]; i++)
1383 if (len == strlen (allowed_gnu[i])
1384 && strncasecmp (value, allowed_gnu[i], strlen (allowed_gnu[i])) == 0)
1386 notification n = gfc_notification_std (GFC_STD_GNU);
1388 if (n == WARNING || (warn && n == ERROR))
1390 gfc_warning ("Extension: %s specifier in %s statement at %C "
1391 "has value '%s'", specifier, statement,
1398 gfc_notify_std (GFC_STD_GNU, "Extension: %s specifier in "
1399 "%s statement at %C has value '%s'", specifier,
1400 statement, allowed_gnu[i]);
1410 gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1411 specifier, statement, value);
1416 gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1417 specifier, statement, value);
1423 /* Match an OPEN statement. */
1426 gfc_match_open (void)
1432 m = gfc_match_char ('(');
1436 open = gfc_getmem (sizeof (gfc_open));
1438 m = match_open_element (open);
1440 if (m == MATCH_ERROR)
1444 m = gfc_match_expr (&open->unit);
1447 if (m == MATCH_ERROR)
1453 if (gfc_match_char (')') == MATCH_YES)
1455 if (gfc_match_char (',') != MATCH_YES)
1458 m = match_open_element (open);
1459 if (m == MATCH_ERROR)
1465 if (gfc_match_eos () == MATCH_NO)
1468 if (gfc_pure (NULL))
1470 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1474 warn = (open->err || open->iostat) ? true : false;
1475 /* Checks on the ACCESS specifier. */
1476 if (open->access && open->access->expr_type == EXPR_CONSTANT)
1478 static const char *access_f95[] = { "SEQUENTIAL", "DIRECT", NULL };
1479 static const char *access_f2003[] = { "STREAM", NULL };
1480 static const char *access_gnu[] = { "APPEND", NULL };
1482 if (!compare_to_allowed_values ("ACCESS", access_f95, access_f2003,
1484 open->access->value.character.string,
1489 /* Checks on the ACTION specifier. */
1490 if (open->action && open->action->expr_type == EXPR_CONSTANT)
1492 static const char *action[] = { "READ", "WRITE", "READWRITE", NULL };
1494 if (!compare_to_allowed_values ("ACTION", action, NULL, NULL,
1495 open->action->value.character.string,
1500 /* Checks on the ASYNCHRONOUS specifier. */
1501 /* TODO: code is ready, just needs uncommenting when async I/O support
1503 if (open->asynchronous && open->asynchronous->expr_type == EXPR_CONSTANT)
1505 static const char * asynchronous[] = { "YES", "NO", NULL };
1507 if (!compare_to_allowed_values
1508 ("action", asynchronous, NULL, NULL,
1509 open->asynchronous->value.character.string, "OPEN", warn))
1513 /* Checks on the BLANK specifier. */
1514 if (open->blank && open->blank->expr_type == EXPR_CONSTANT)
1516 static const char *blank[] = { "ZERO", "NULL", NULL };
1518 if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
1519 open->blank->value.character.string,
1524 /* Checks on the DECIMAL specifier. */
1525 /* TODO: uncomment this code when DECIMAL support is added
1526 if (open->decimal && open->decimal->expr_type == EXPR_CONSTANT)
1528 static const char * decimal[] = { "COMMA", "POINT", NULL };
1530 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1531 open->decimal->value.character.string,
1536 /* Checks on the DELIM specifier. */
1537 if (open->delim && open->delim->expr_type == EXPR_CONSTANT)
1539 static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
1541 if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
1542 open->delim->value.character.string,
1547 /* Checks on the ENCODING specifier. */
1548 /* TODO: uncomment this code when ENCODING support is added
1549 if (open->encoding && open->encoding->expr_type == EXPR_CONSTANT)
1551 static const char * encoding[] = { "UTF-8", "DEFAULT", NULL };
1553 if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1554 open->encoding->value.character.string,
1559 /* Checks on the FORM specifier. */
1560 if (open->form && open->form->expr_type == EXPR_CONSTANT)
1562 static const char *form[] = { "FORMATTED", "UNFORMATTED", NULL };
1564 if (!compare_to_allowed_values ("FORM", form, NULL, NULL,
1565 open->form->value.character.string,
1570 /* Checks on the PAD specifier. */
1571 if (open->pad && open->pad->expr_type == EXPR_CONSTANT)
1573 static const char *pad[] = { "YES", "NO", NULL };
1575 if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
1576 open->pad->value.character.string,
1581 /* Checks on the POSITION specifier. */
1582 if (open->position && open->position->expr_type == EXPR_CONSTANT)
1584 static const char *position[] = { "ASIS", "REWIND", "APPEND", NULL };
1586 if (!compare_to_allowed_values ("POSITION", position, NULL, NULL,
1587 open->position->value.character.string,
1592 /* Checks on the ROUND specifier. */
1593 /* TODO: uncomment this code when ROUND support is added
1594 if (open->round && open->round->expr_type == EXPR_CONSTANT)
1596 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1597 "COMPATIBLE", "PROCESSOR_DEFINED", NULL };
1599 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1600 open->round->value.character.string,
1605 /* Checks on the SIGN specifier. */
1606 /* TODO: uncomment this code when SIGN support is added
1607 if (open->sign && open->sign->expr_type == EXPR_CONSTANT)
1609 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1612 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1613 open->sign->value.character.string,
1618 #define warn_or_error(...) \
1621 gfc_warning (__VA_ARGS__); \
1624 gfc_error (__VA_ARGS__); \
1629 /* Checks on the RECL specifier. */
1630 if (open->recl && open->recl->expr_type == EXPR_CONSTANT
1631 && open->recl->ts.type == BT_INTEGER
1632 && mpz_sgn (open->recl->value.integer) != 1)
1634 warn_or_error ("RECL in OPEN statement at %C must be positive");
1637 /* Checks on the STATUS specifier. */
1638 if (open->status && open->status->expr_type == EXPR_CONSTANT)
1640 static const char *status[] = { "OLD", "NEW", "SCRATCH",
1641 "REPLACE", "UNKNOWN", NULL };
1643 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1644 open->status->value.character.string,
1648 /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1649 the FILE= specifier shall appear. */
1650 if (open->file == NULL
1651 && (strncasecmp (open->status->value.character.string, "replace", 7)
1653 || strncasecmp (open->status->value.character.string, "new", 3)
1656 warn_or_error ("The STATUS specified in OPEN statement at %C is "
1657 "'%s' and no FILE specifier is present",
1658 open->status->value.character.string);
1661 /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1662 the FILE= specifier shall not appear. */
1663 if (strncasecmp (open->status->value.character.string, "scratch", 7)
1666 warn_or_error ("The STATUS specified in OPEN statement at %C "
1667 "cannot have the value SCRATCH if a FILE specifier "
1672 /* Things that are not allowed for unformatted I/O. */
1673 if (open->form && open->form->expr_type == EXPR_CONSTANT
1675 /* TODO uncomment this code when F2003 support is finished */
1676 /* || open->decimal || open->encoding || open->round
1678 || open->pad || open->blank)
1679 && strncasecmp (open->form->value.character.string,
1680 "unformatted", 11) == 0)
1682 const char *spec = (open->delim ? "DELIM "
1683 : (open->pad ? "PAD " : open->blank
1686 warn_or_error ("%s specifier at %C not allowed in OPEN statement for "
1687 "unformatted I/O", spec);
1690 if (open->recl && open->access && open->access->expr_type == EXPR_CONSTANT
1691 && strncasecmp (open->access->value.character.string, "stream", 6) == 0)
1693 warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
1698 && open->access && open->access->expr_type == EXPR_CONSTANT
1699 && !(strncasecmp (open->access->value.character.string,
1700 "sequential", 10) == 0
1701 || strncasecmp (open->access->value.character.string,
1703 || strncasecmp (open->access->value.character.string,
1706 warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
1707 "for stream or sequential ACCESS");
1710 #undef warn_or_error
1712 new_st.op = EXEC_OPEN;
1713 new_st.ext.open = open;
1717 gfc_syntax_error (ST_OPEN);
1720 gfc_free_open (open);
1725 /* Free a gfc_close structure an all its expressions. */
1728 gfc_free_close (gfc_close *close)
1733 gfc_free_expr (close->unit);
1734 gfc_free_expr (close->iomsg);
1735 gfc_free_expr (close->iostat);
1736 gfc_free_expr (close->status);
1741 /* Match elements of a CLOSE statement. */
1744 match_close_element (gfc_close *close)
1748 m = match_etag (&tag_unit, &close->unit);
1751 m = match_etag (&tag_status, &close->status);
1754 m = match_out_tag (&tag_iomsg, &close->iomsg);
1757 m = match_out_tag (&tag_iostat, &close->iostat);
1760 m = match_ltag (&tag_err, &close->err);
1768 /* Match a CLOSE statement. */
1771 gfc_match_close (void)
1777 m = gfc_match_char ('(');
1781 close = gfc_getmem (sizeof (gfc_close));
1783 m = match_close_element (close);
1785 if (m == MATCH_ERROR)
1789 m = gfc_match_expr (&close->unit);
1792 if (m == MATCH_ERROR)
1798 if (gfc_match_char (')') == MATCH_YES)
1800 if (gfc_match_char (',') != MATCH_YES)
1803 m = match_close_element (close);
1804 if (m == MATCH_ERROR)
1810 if (gfc_match_eos () == MATCH_NO)
1813 if (gfc_pure (NULL))
1815 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1819 warn = (close->iostat || close->err) ? true : false;
1821 /* Checks on the STATUS specifier. */
1822 if (close->status && close->status->expr_type == EXPR_CONSTANT)
1824 static const char *status[] = { "KEEP", "DELETE", NULL };
1826 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1827 close->status->value.character.string,
1832 new_st.op = EXEC_CLOSE;
1833 new_st.ext.close = close;
1837 gfc_syntax_error (ST_CLOSE);
1840 gfc_free_close (close);
1845 /* Resolve everything in a gfc_close structure. */
1848 gfc_resolve_close (gfc_close *close)
1850 RESOLVE_TAG (&tag_unit, close->unit);
1851 RESOLVE_TAG (&tag_iomsg, close->iomsg);
1852 RESOLVE_TAG (&tag_iostat, close->iostat);
1853 RESOLVE_TAG (&tag_status, close->status);
1855 if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
1862 /* Free a gfc_filepos structure. */
1865 gfc_free_filepos (gfc_filepos *fp)
1867 gfc_free_expr (fp->unit);
1868 gfc_free_expr (fp->iomsg);
1869 gfc_free_expr (fp->iostat);
1874 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
1877 match_file_element (gfc_filepos *fp)
1881 m = match_etag (&tag_unit, &fp->unit);
1884 m = match_out_tag (&tag_iomsg, &fp->iomsg);
1887 m = match_out_tag (&tag_iostat, &fp->iostat);
1890 m = match_ltag (&tag_err, &fp->err);
1898 /* Match the second half of the file-positioning statements, REWIND,
1899 BACKSPACE, ENDFILE, or the FLUSH statement. */
1902 match_filepos (gfc_statement st, gfc_exec_op op)
1907 fp = gfc_getmem (sizeof (gfc_filepos));
1909 if (gfc_match_char ('(') == MATCH_NO)
1911 m = gfc_match_expr (&fp->unit);
1912 if (m == MATCH_ERROR)
1920 m = match_file_element (fp);
1921 if (m == MATCH_ERROR)
1925 m = gfc_match_expr (&fp->unit);
1926 if (m == MATCH_ERROR)
1934 if (gfc_match_char (')') == MATCH_YES)
1936 if (gfc_match_char (',') != MATCH_YES)
1939 m = match_file_element (fp);
1940 if (m == MATCH_ERROR)
1947 if (gfc_match_eos () != MATCH_YES)
1950 if (gfc_pure (NULL))
1952 gfc_error ("%s statement not allowed in PURE procedure at %C",
1953 gfc_ascii_statement (st));
1959 new_st.ext.filepos = fp;
1963 gfc_syntax_error (st);
1966 gfc_free_filepos (fp);
1972 gfc_resolve_filepos (gfc_filepos *fp)
1974 RESOLVE_TAG (&tag_unit, fp->unit);
1975 RESOLVE_TAG (&tag_iostat, fp->iostat);
1976 RESOLVE_TAG (&tag_iomsg, fp->iomsg);
1977 if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
1984 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
1985 and the FLUSH statement. */
1988 gfc_match_endfile (void)
1990 return match_filepos (ST_END_FILE, EXEC_ENDFILE);
1994 gfc_match_backspace (void)
1996 return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
2000 gfc_match_rewind (void)
2002 return match_filepos (ST_REWIND, EXEC_REWIND);
2006 gfc_match_flush (void)
2008 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: FLUSH statement at %C")
2012 return match_filepos (ST_FLUSH, EXEC_FLUSH);
2015 /******************** Data Transfer Statements *********************/
2018 { M_READ, M_WRITE, M_PRINT, M_INQUIRE }
2022 /* Return a default unit number. */
2025 default_unit (io_kind k)
2034 return gfc_int_expr (unit);
2038 /* Match a unit specification for a data transfer statement. */
2041 match_dt_unit (io_kind k, gfc_dt *dt)
2045 if (gfc_match_char ('*') == MATCH_YES)
2047 if (dt->io_unit != NULL)
2050 dt->io_unit = default_unit (k);
2054 if (gfc_match_expr (&e) == MATCH_YES)
2056 if (dt->io_unit != NULL)
2069 gfc_error ("Duplicate UNIT specification at %C");
2074 /* Match a format specification. */
2077 match_dt_format (gfc_dt *dt)
2081 gfc_st_label *label;
2083 where = gfc_current_locus;
2085 if (gfc_match_char ('*') == MATCH_YES)
2087 if (dt->format_expr != NULL || dt->format_label != NULL)
2090 dt->format_label = &format_asterisk;
2094 if (gfc_match_st_label (&label) == MATCH_YES)
2096 if (dt->format_expr != NULL || dt->format_label != NULL)
2098 gfc_free_st_label (label);
2102 if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
2105 dt->format_label = label;
2109 if (gfc_match_expr (&e) == MATCH_YES)
2111 if (dt->format_expr != NULL || dt->format_label != NULL)
2116 dt->format_expr = e;
2120 gfc_current_locus = where; /* The only case where we have to restore */
2125 gfc_error ("Duplicate format specification at %C");
2130 /* Traverse a namelist that is part of a READ statement to make sure
2131 that none of the variables in the namelist are INTENT(IN). Returns
2132 nonzero if we find such a variable. */
2135 check_namelist (gfc_symbol *sym)
2139 for (p = sym->namelist; p; p = p->next)
2140 if (p->sym->attr.intent == INTENT_IN)
2142 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2143 p->sym->name, sym->name);
2151 /* Match a single data transfer element. */
2154 match_dt_element (io_kind k, gfc_dt *dt)
2156 char name[GFC_MAX_SYMBOL_LEN + 1];
2160 if (gfc_match (" unit =") == MATCH_YES)
2162 m = match_dt_unit (k, dt);
2167 if (gfc_match (" fmt =") == MATCH_YES)
2169 m = match_dt_format (dt);
2174 if (gfc_match (" nml = %n", name) == MATCH_YES)
2176 if (dt->namelist != NULL)
2178 gfc_error ("Duplicate NML specification at %C");
2182 if (gfc_find_symbol (name, NULL, 1, &sym))
2185 if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
2187 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2188 sym != NULL ? sym->name : name);
2193 if (k == M_READ && check_namelist (sym))
2199 m = match_etag (&tag_rec, &dt->rec);
2202 m = match_etag (&tag_spos, &dt->rec);
2205 m = match_out_tag (&tag_iomsg, &dt->iomsg);
2208 m = match_out_tag (&tag_iostat, &dt->iostat);
2211 m = match_ltag (&tag_err, &dt->err);
2213 dt->err_where = gfc_current_locus;
2216 m = match_etag (&tag_advance, &dt->advance);
2219 m = match_out_tag (&tag_size, &dt->size);
2223 m = match_ltag (&tag_end, &dt->end);
2228 gfc_error ("END tag at %C not allowed in output statement");
2231 dt->end_where = gfc_current_locus;
2236 m = match_ltag (&tag_eor, &dt->eor);
2238 dt->eor_where = gfc_current_locus;
2246 /* Free a data transfer structure and everything below it. */
2249 gfc_free_dt (gfc_dt *dt)
2254 gfc_free_expr (dt->io_unit);
2255 gfc_free_expr (dt->format_expr);
2256 gfc_free_expr (dt->rec);
2257 gfc_free_expr (dt->advance);
2258 gfc_free_expr (dt->iomsg);
2259 gfc_free_expr (dt->iostat);
2260 gfc_free_expr (dt->size);
2265 /* Resolve everything in a gfc_dt structure. */
2268 gfc_resolve_dt (gfc_dt *dt)
2272 RESOLVE_TAG (&tag_format, dt->format_expr);
2273 RESOLVE_TAG (&tag_rec, dt->rec);
2274 RESOLVE_TAG (&tag_spos, dt->rec);
2275 RESOLVE_TAG (&tag_advance, dt->advance);
2276 RESOLVE_TAG (&tag_iomsg, dt->iomsg);
2277 RESOLVE_TAG (&tag_iostat, dt->iostat);
2278 RESOLVE_TAG (&tag_size, dt->size);
2281 if (gfc_resolve_expr (e) == SUCCESS
2282 && (e->ts.type != BT_INTEGER
2283 && (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_VARIABLE)))
2285 gfc_error ("UNIT specification at %L must be an INTEGER expression "
2286 "or a CHARACTER variable", &e->where);
2290 if (e->ts.type == BT_CHARACTER)
2292 if (gfc_has_vector_index (e))
2294 gfc_error ("Internal unit with vector subscript at %L", &e->where);
2299 if (e->rank && e->ts.type != BT_CHARACTER)
2301 gfc_error ("External IO UNIT cannot be an array at %L", &e->where);
2307 if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
2309 if (dt->err->defined == ST_LABEL_UNKNOWN)
2311 gfc_error ("ERR tag label %d at %L not defined",
2312 dt->err->value, &dt->err_where);
2319 if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
2321 if (dt->end->defined == ST_LABEL_UNKNOWN)
2323 gfc_error ("END tag label %d at %L not defined",
2324 dt->end->value, &dt->end_where);
2331 if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
2333 if (dt->eor->defined == ST_LABEL_UNKNOWN)
2335 gfc_error ("EOR tag label %d at %L not defined",
2336 dt->eor->value, &dt->eor_where);
2341 /* Check the format label actually exists. */
2342 if (dt->format_label && dt->format_label != &format_asterisk
2343 && dt->format_label->defined == ST_LABEL_UNKNOWN)
2345 gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
2346 &dt->format_label->where);
2353 /* Given an io_kind, return its name. */
2356 io_kind_name (io_kind k)
2375 gfc_internal_error ("io_kind_name(): bad I/O-kind");
2382 /* Match an IO iteration statement of the form:
2384 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2386 which is equivalent to a single IO element. This function is
2387 mutually recursive with match_io_element(). */
2389 static match match_io_element (io_kind, gfc_code **);
2392 match_io_iterator (io_kind k, gfc_code **result)
2394 gfc_code *head, *tail, *new;
2402 old_loc = gfc_current_locus;
2404 if (gfc_match_char ('(') != MATCH_YES)
2407 m = match_io_element (k, &head);
2410 if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
2416 /* Can't be anything but an IO iterator. Build a list. */
2417 iter = gfc_get_iterator ();
2421 m = gfc_match_iterator (iter, 0);
2422 if (m == MATCH_ERROR)
2426 gfc_check_do_variable (iter->var->symtree);
2430 m = match_io_element (k, &new);
2431 if (m == MATCH_ERROR)
2440 tail = gfc_append_code (tail, new);
2442 if (gfc_match_char (',') != MATCH_YES)
2451 if (gfc_match_char (')') != MATCH_YES)
2454 new = gfc_get_code ();
2456 new->ext.iterator = iter;
2458 new->block = gfc_get_code ();
2459 new->block->op = EXEC_DO;
2460 new->block->next = head;
2466 gfc_error ("Syntax error in I/O iterator at %C");
2470 gfc_free_iterator (iter, 1);
2471 gfc_free_statements (head);
2472 gfc_current_locus = old_loc;
2477 /* Match a single element of an IO list, which is either a single
2478 expression or an IO Iterator. */
2481 match_io_element (io_kind k, gfc_code **cpp)
2489 m = match_io_iterator (k, cpp);
2495 m = gfc_match_variable (&expr, 0);
2497 gfc_error ("Expected variable in READ statement at %C");
2501 m = gfc_match_expr (&expr);
2503 gfc_error ("Expected expression in %s statement at %C",
2511 if (expr->symtree->n.sym->attr.intent == INTENT_IN)
2513 gfc_error ("Variable '%s' in input list at %C cannot be "
2514 "INTENT(IN)", expr->symtree->n.sym->name);
2519 && gfc_impure_variable (expr->symtree->n.sym)
2520 && current_dt->io_unit->ts.type == BT_CHARACTER)
2522 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2523 expr->symtree->n.sym->name);
2527 if (gfc_check_do_variable (expr->symtree))
2533 if (current_dt->io_unit->ts.type == BT_CHARACTER
2535 && current_dt->io_unit->expr_type == EXPR_VARIABLE
2536 && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
2538 gfc_error ("Cannot write to internal file unit '%s' at %C "
2539 "inside a PURE procedure",
2540 current_dt->io_unit->symtree->n.sym->name);
2552 gfc_free_expr (expr);
2556 cp = gfc_get_code ();
2557 cp->op = EXEC_TRANSFER;
2565 /* Match an I/O list, building gfc_code structures as we go. */
2568 match_io_list (io_kind k, gfc_code **head_p)
2570 gfc_code *head, *tail, *new;
2573 *head_p = head = tail = NULL;
2574 if (gfc_match_eos () == MATCH_YES)
2579 m = match_io_element (k, &new);
2580 if (m == MATCH_ERROR)
2585 tail = gfc_append_code (tail, new);
2589 if (gfc_match_eos () == MATCH_YES)
2591 if (gfc_match_char (',') != MATCH_YES)
2599 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2602 gfc_free_statements (head);
2607 /* Attach the data transfer end node. */
2610 terminate_io (gfc_code *io_code)
2614 if (io_code == NULL)
2615 io_code = new_st.block;
2617 c = gfc_get_code ();
2618 c->op = EXEC_DT_END;
2620 /* Point to structure that is already there */
2621 c->ext.dt = new_st.ext.dt;
2622 gfc_append_code (io_code, c);
2626 /* Check the constraints for a data transfer statement. The majority of the
2627 constraints appearing in 9.4 of the standard appear here. Some are handled
2628 in resolve_tag and others in gfc_resolve_dt. */
2631 check_io_constraints (io_kind k, gfc_dt *dt, gfc_code *io_code,
2634 #define io_constraint(condition,msg,arg)\
2637 gfc_error(msg,arg);\
2643 gfc_symbol *sym = NULL;
2648 if (expr && expr->expr_type == EXPR_VARIABLE
2649 && expr->ts.type == BT_CHARACTER)
2651 sym = expr->symtree->n.sym;
2653 io_constraint (k == M_WRITE && sym->attr.intent == INTENT_IN,
2654 "Internal file at %L must not be INTENT(IN)",
2657 io_constraint (gfc_has_vector_index (dt->io_unit),
2658 "Internal file incompatible with vector subscript at %L",
2661 io_constraint (dt->rec != NULL,
2662 "REC tag at %L is incompatible with internal file",
2665 if (dt->namelist != NULL)
2667 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Internal file "
2668 "at %L with namelist", &expr->where)
2673 io_constraint (dt->advance != NULL,
2674 "ADVANCE tag at %L is incompatible with internal file",
2675 &dt->advance->where);
2678 if (expr && expr->ts.type != BT_CHARACTER)
2681 io_constraint (gfc_pure (NULL) && (k == M_READ || k == M_WRITE),
2682 "IO UNIT in %s statement at %C must be "
2683 "an internal file in a PURE procedure",
2690 io_constraint (dt->end, "END tag not allowed with output at %L",
2693 io_constraint (dt->eor, "EOR tag not allowed with output at %L",
2696 io_constraint (k != M_READ && dt->size,
2697 "SIZE=specifier not allowed with output at %L",
2702 io_constraint (dt->size && dt->advance == NULL,
2703 "SIZE tag at %L requires an ADVANCE tag",
2706 io_constraint (dt->eor && dt->advance == NULL,
2707 "EOR tag at %L requires an ADVANCE tag",
2715 io_constraint (io_code && dt->namelist,
2716 "NAMELIST cannot be followed by IO-list at %L",
2719 io_constraint (dt->format_expr,
2720 "IO spec-list cannot contain both NAMELIST group name "
2721 "and format specification at %L.",
2722 &dt->format_expr->where);
2724 io_constraint (dt->format_label,
2725 "IO spec-list cannot contain both NAMELIST group name "
2726 "and format label at %L", spec_end);
2728 io_constraint (dt->rec,
2729 "NAMELIST IO is not allowed with a REC=specifier "
2730 "at %L.", &dt->rec->where);
2732 io_constraint (dt->advance,
2733 "NAMELIST IO is not allowed with a ADVANCE=specifier "
2734 "at %L.", &dt->advance->where);
2739 io_constraint (dt->end,
2740 "An END tag is not allowed with a "
2741 "REC=specifier at %L.", &dt->end_where);
2744 io_constraint (dt->format_label == &format_asterisk,
2745 "FMT=* is not allowed with a REC=specifier "
2746 "at %L.", spec_end);
2751 int not_yes, not_no;
2754 io_constraint (dt->format_label == &format_asterisk,
2755 "List directed format(*) is not allowed with a "
2756 "ADVANCE=specifier at %L.", &expr->where);
2758 io_constraint (dt->format_expr == NULL && dt->format_label == NULL
2759 && dt->namelist == NULL,
2760 "the ADVANCE=specifier at %L must appear with an "
2761 "explicit format expression", &expr->where);
2763 if (expr->expr_type == EXPR_CONSTANT && expr->ts.type == BT_CHARACTER)
2765 const char * advance = expr->value.character.string;
2766 not_no = strcasecmp (advance, "no") != 0;
2767 not_yes = strcasecmp (advance, "yes") != 0;
2775 io_constraint (not_no && not_yes,
2776 "ADVANCE=specifier at %L must have value = "
2777 "YES or NO.", &expr->where);
2779 io_constraint (dt->size && not_no && k == M_READ,
2780 "SIZE tag at %L requires an ADVANCE = 'NO'",
2783 io_constraint (dt->eor && not_no && k == M_READ,
2784 "EOR tag at %L requires an ADVANCE = 'NO'",
2788 expr = dt->format_expr;
2789 if (expr != NULL && expr->expr_type == EXPR_CONSTANT
2790 && check_format_string (expr, k == M_READ) == FAILURE)
2795 #undef io_constraint
2798 /* Match a READ, WRITE or PRINT statement. */
2801 match_io (io_kind k)
2803 char name[GFC_MAX_SYMBOL_LEN + 1];
2812 where = gfc_current_locus;
2814 current_dt = dt = gfc_getmem (sizeof (gfc_dt));
2815 m = gfc_match_char ('(');
2818 where = gfc_current_locus;
2821 else if (k == M_PRINT)
2823 /* Treat the non-standard case of PRINT namelist. */
2824 if ((gfc_current_form == FORM_FIXED || gfc_peek_char () == ' ')
2825 && gfc_match_name (name) == MATCH_YES)
2827 gfc_find_symbol (name, NULL, 1, &sym);
2828 if (sym && sym->attr.flavor == FL_NAMELIST)
2830 if (gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
2831 "%C is an extension") == FAILURE)
2837 dt->io_unit = default_unit (k);
2842 gfc_current_locus = where;
2846 if (gfc_current_form == FORM_FREE)
2848 c = gfc_peek_char();
2849 if (c != ' ' && c != '*' && c != '\'' && c != '"')
2856 m = match_dt_format (dt);
2857 if (m == MATCH_ERROR)
2863 dt->io_unit = default_unit (k);
2868 /* Before issuing an error for a malformed 'print (1,*)' type of
2869 error, check for a default-char-expr of the form ('(I0)'). */
2870 if (k == M_PRINT && m == MATCH_YES)
2872 /* Reset current locus to get the initial '(' in an expression. */
2873 gfc_current_locus = where;
2874 dt->format_expr = NULL;
2875 m = match_dt_format (dt);
2877 if (m == MATCH_ERROR)
2879 if (m == MATCH_NO || dt->format_expr == NULL)
2883 dt->io_unit = default_unit (k);
2888 /* Match a control list */
2889 if (match_dt_element (k, dt) == MATCH_YES)
2891 if (match_dt_unit (k, dt) != MATCH_YES)
2894 if (gfc_match_char (')') == MATCH_YES)
2896 if (gfc_match_char (',') != MATCH_YES)
2899 m = match_dt_element (k, dt);
2902 if (m == MATCH_ERROR)
2905 m = match_dt_format (dt);
2908 if (m == MATCH_ERROR)
2911 where = gfc_current_locus;
2913 m = gfc_match_name (name);
2916 gfc_find_symbol (name, NULL, 1, &sym);
2917 if (sym && sym->attr.flavor == FL_NAMELIST)
2920 if (k == M_READ && check_namelist (sym))
2929 gfc_current_locus = where;
2931 goto loop; /* No matches, try regular elements */
2934 if (gfc_match_char (')') == MATCH_YES)
2936 if (gfc_match_char (',') != MATCH_YES)
2942 m = match_dt_element (k, dt);
2945 if (m == MATCH_ERROR)
2948 if (gfc_match_char (')') == MATCH_YES)
2950 if (gfc_match_char (',') != MATCH_YES)
2956 /* Used in check_io_constraints, where no locus is available. */
2957 spec_end = gfc_current_locus;
2959 /* Optional leading comma (non-standard). */
2961 && gfc_match_char (',') == MATCH_YES
2962 && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before i/o "
2963 "item list at %C") == FAILURE)
2967 if (gfc_match_eos () != MATCH_YES)
2969 if (comma_flag && gfc_match_char (',') != MATCH_YES)
2971 gfc_error ("Expected comma in I/O list at %C");
2976 m = match_io_list (k, &io_code);
2977 if (m == MATCH_ERROR)
2983 /* A full IO statement has been matched. Check the constraints. spec_end is
2984 supplied for cases where no locus is supplied. */
2985 m = check_io_constraints (k, dt, io_code, &spec_end);
2987 if (m == MATCH_ERROR)
2990 new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
2992 new_st.block = gfc_get_code ();
2993 new_st.block->op = new_st.op;
2994 new_st.block->next = io_code;
2996 terminate_io (io_code);
3001 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
3011 gfc_match_read (void)
3013 return match_io (M_READ);
3017 gfc_match_write (void)
3019 return match_io (M_WRITE);
3023 gfc_match_print (void)
3027 m = match_io (M_PRINT);
3031 if (gfc_pure (NULL))
3033 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
3041 /* Free a gfc_inquire structure. */
3044 gfc_free_inquire (gfc_inquire *inquire)
3047 if (inquire == NULL)
3050 gfc_free_expr (inquire->unit);
3051 gfc_free_expr (inquire->file);
3052 gfc_free_expr (inquire->iomsg);
3053 gfc_free_expr (inquire->iostat);
3054 gfc_free_expr (inquire->exist);
3055 gfc_free_expr (inquire->opened);
3056 gfc_free_expr (inquire->number);
3057 gfc_free_expr (inquire->named);
3058 gfc_free_expr (inquire->name);
3059 gfc_free_expr (inquire->access);
3060 gfc_free_expr (inquire->sequential);
3061 gfc_free_expr (inquire->direct);
3062 gfc_free_expr (inquire->form);
3063 gfc_free_expr (inquire->formatted);
3064 gfc_free_expr (inquire->unformatted);
3065 gfc_free_expr (inquire->recl);
3066 gfc_free_expr (inquire->nextrec);
3067 gfc_free_expr (inquire->blank);
3068 gfc_free_expr (inquire->position);
3069 gfc_free_expr (inquire->action);
3070 gfc_free_expr (inquire->read);
3071 gfc_free_expr (inquire->write);
3072 gfc_free_expr (inquire->readwrite);
3073 gfc_free_expr (inquire->delim);
3074 gfc_free_expr (inquire->pad);
3075 gfc_free_expr (inquire->iolength);
3076 gfc_free_expr (inquire->convert);
3077 gfc_free_expr (inquire->strm_pos);
3082 /* Match an element of an INQUIRE statement. */
3084 #define RETM if (m != MATCH_NO) return m;
3087 match_inquire_element (gfc_inquire *inquire)
3091 m = match_etag (&tag_unit, &inquire->unit);
3092 RETM m = match_etag (&tag_file, &inquire->file);
3093 RETM m = match_ltag (&tag_err, &inquire->err);
3094 RETM m = match_out_tag (&tag_iomsg, &inquire->iomsg);
3095 RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
3096 RETM m = match_vtag (&tag_exist, &inquire->exist);
3097 RETM m = match_vtag (&tag_opened, &inquire->opened);
3098 RETM m = match_vtag (&tag_named, &inquire->named);
3099 RETM m = match_vtag (&tag_name, &inquire->name);
3100 RETM m = match_out_tag (&tag_number, &inquire->number);
3101 RETM m = match_vtag (&tag_s_access, &inquire->access);
3102 RETM m = match_vtag (&tag_sequential, &inquire->sequential);
3103 RETM m = match_vtag (&tag_direct, &inquire->direct);
3104 RETM m = match_vtag (&tag_s_form, &inquire->form);
3105 RETM m = match_vtag (&tag_formatted, &inquire->formatted);
3106 RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
3107 RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
3108 RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
3109 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
3110 RETM m = match_vtag (&tag_s_position, &inquire->position);
3111 RETM m = match_vtag (&tag_s_action, &inquire->action);
3112 RETM m = match_vtag (&tag_read, &inquire->read);
3113 RETM m = match_vtag (&tag_write, &inquire->write);
3114 RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
3115 RETM m = match_vtag (&tag_s_delim, &inquire->delim);
3116 RETM m = match_vtag (&tag_s_pad, &inquire->pad);
3117 RETM m = match_vtag (&tag_iolength, &inquire->iolength);
3118 RETM m = match_vtag (&tag_convert, &inquire->convert);
3119 RETM m = match_out_tag (&tag_strm_out, &inquire->strm_pos);
3120 RETM return MATCH_NO;
3127 gfc_match_inquire (void)
3129 gfc_inquire *inquire;
3134 m = gfc_match_char ('(');
3138 inquire = gfc_getmem (sizeof (gfc_inquire));
3140 loc = gfc_current_locus;
3142 m = match_inquire_element (inquire);
3143 if (m == MATCH_ERROR)
3147 m = gfc_match_expr (&inquire->unit);
3148 if (m == MATCH_ERROR)
3154 /* See if we have the IOLENGTH form of the inquire statement. */
3155 if (inquire->iolength != NULL)
3157 if (gfc_match_char (')') != MATCH_YES)
3160 m = match_io_list (M_INQUIRE, &code);
3161 if (m == MATCH_ERROR)
3166 new_st.op = EXEC_IOLENGTH;
3167 new_st.expr = inquire->iolength;
3168 new_st.ext.inquire = inquire;
3170 if (gfc_pure (NULL))
3172 gfc_free_statements (code);
3173 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3177 new_st.block = gfc_get_code ();
3178 new_st.block->op = EXEC_IOLENGTH;
3179 terminate_io (code);
3180 new_st.block->next = code;
3184 /* At this point, we have the non-IOLENGTH inquire statement. */
3187 if (gfc_match_char (')') == MATCH_YES)
3189 if (gfc_match_char (',') != MATCH_YES)
3192 m = match_inquire_element (inquire);
3193 if (m == MATCH_ERROR)
3198 if (inquire->iolength != NULL)
3200 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3205 if (gfc_match_eos () != MATCH_YES)
3208 if (inquire->unit != NULL && inquire->file != NULL)
3210 gfc_error ("INQUIRE statement at %L cannot contain both FILE and "
3211 "UNIT specifiers", &loc);
3215 if (inquire->unit == NULL && inquire->file == NULL)
3217 gfc_error ("INQUIRE statement at %L requires either FILE or "
3218 "UNIT specifier", &loc);
3222 if (gfc_pure (NULL))
3224 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3228 new_st.op = EXEC_INQUIRE;
3229 new_st.ext.inquire = inquire;
3233 gfc_syntax_error (ST_INQUIRE);
3236 gfc_free_inquire (inquire);
3241 /* Resolve everything in a gfc_inquire structure. */
3244 gfc_resolve_inquire (gfc_inquire *inquire)
3246 RESOLVE_TAG (&tag_unit, inquire->unit);
3247 RESOLVE_TAG (&tag_file, inquire->file);
3248 RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
3249 RESOLVE_TAG (&tag_iostat, inquire->iostat);
3250 RESOLVE_TAG (&tag_exist, inquire->exist);
3251 RESOLVE_TAG (&tag_opened, inquire->opened);
3252 RESOLVE_TAG (&tag_number, inquire->number);
3253 RESOLVE_TAG (&tag_named, inquire->named);
3254 RESOLVE_TAG (&tag_name, inquire->name);
3255 RESOLVE_TAG (&tag_s_access, inquire->access);
3256 RESOLVE_TAG (&tag_sequential, inquire->sequential);
3257 RESOLVE_TAG (&tag_direct, inquire->direct);
3258 RESOLVE_TAG (&tag_s_form, inquire->form);
3259 RESOLVE_TAG (&tag_formatted, inquire->formatted);
3260 RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
3261 RESOLVE_TAG (&tag_s_recl, inquire->recl);
3262 RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
3263 RESOLVE_TAG (&tag_s_blank, inquire->blank);
3264 RESOLVE_TAG (&tag_s_position, inquire->position);
3265 RESOLVE_TAG (&tag_s_action, inquire->action);
3266 RESOLVE_TAG (&tag_read, inquire->read);
3267 RESOLVE_TAG (&tag_write, inquire->write);
3268 RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
3269 RESOLVE_TAG (&tag_s_delim, inquire->delim);
3270 RESOLVE_TAG (&tag_s_pad, inquire->pad);
3271 RESOLVE_TAG (&tag_iolength, inquire->iolength);
3272 RESOLVE_TAG (&tag_convert, inquire->convert);
3273 RESOLVE_TAG (&tag_strm_out, inquire->strm_pos);
3275 if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)