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 /* Do expression resolution and type-checking on an expression tag. */
1097 resolve_tag (const io_tag *tag, gfc_expr *e)
1102 if (gfc_resolve_expr (e) == FAILURE)
1105 if (e->ts.type != tag->type && tag != &tag_format)
1107 gfc_error ("%s tag at %L must be of type %s", tag->name,
1108 &e->where, gfc_basic_typename (tag->type));
1112 if (tag == &tag_format)
1114 if (e->expr_type == EXPR_CONSTANT
1115 && (e->ts.type != BT_CHARACTER
1116 || e->ts.kind != gfc_default_character_kind))
1118 gfc_error ("Constant expression in FORMAT tag at %L must be "
1119 "of type default CHARACTER", &e->where);
1123 /* If e's rank is zero and e is not an element of an array, it should be
1124 of integer or character type. The integer variable should be
1126 if (e->symtree == NULL || e->symtree->n.sym->as == NULL
1127 || e->symtree->n.sym->as->rank == 0)
1129 if (e->ts.type != BT_CHARACTER && e->ts.type != BT_INTEGER)
1131 gfc_error ("%s tag at %L must be of type %s or %s", tag->name,
1132 &e->where, gfc_basic_typename (BT_CHARACTER),
1133 gfc_basic_typename (BT_INTEGER));
1136 else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
1138 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGNED "
1139 "variable in FORMAT tag at %L", &e->where)
1142 if (e->symtree->n.sym->attr.assign != 1)
1144 gfc_error ("Variable '%s' at %L has not been assigned a "
1145 "format label", e->symtree->n.sym->name,
1150 else if (e->ts.type == BT_INTEGER)
1152 gfc_error ("scalar '%s' FORMAT tag at %L is not an ASSIGNED "
1153 "variable", gfc_basic_typename (e->ts.type),
1162 /* if rank is nonzero, we allow the type to be character under
1163 GFC_STD_GNU and other type under GFC_STD_LEGACY. It may be
1164 assigned an Hollerith constant. */
1165 if (e->ts.type == BT_CHARACTER)
1167 if (gfc_notify_std (GFC_STD_GNU, "Extension: Character array "
1168 "in FORMAT tag at %L", &e->where)
1174 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: Non-character "
1175 "in FORMAT tag at %L", &e->where)
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 && e->ts.kind != gfc_default_integer_kind)
1199 if (gfc_notify_std (GFC_STD_GNU, "Fortran 95 requires default "
1200 "INTEGER in IOSTAT tag at %L", &e->where)
1205 if (tag == &tag_size && e->ts.kind != gfc_default_integer_kind)
1207 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1208 "INTEGER in SIZE tag at %L", &e->where)
1213 if (tag == &tag_convert)
1215 if (gfc_notify_std (GFC_STD_GNU, "Extension: CONVERT tag at %L",
1216 &e->where) == FAILURE)
1220 if (tag == &tag_iolength && e->ts.kind != gfc_default_integer_kind)
1222 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1223 "INTEGER in IOLENGTH tag at %L", &e->where)
1233 /* Match a single tag of an OPEN statement. */
1236 match_open_element (gfc_open *open)
1240 m = match_etag (&tag_unit, &open->unit);
1243 m = match_out_tag (&tag_iomsg, &open->iomsg);
1246 m = match_out_tag (&tag_iostat, &open->iostat);
1249 m = match_etag (&tag_file, &open->file);
1252 m = match_etag (&tag_status, &open->status);
1255 m = match_etag (&tag_e_access, &open->access);
1258 m = match_etag (&tag_e_form, &open->form);
1261 m = match_etag (&tag_e_recl, &open->recl);
1264 m = match_etag (&tag_e_blank, &open->blank);
1267 m = match_etag (&tag_e_position, &open->position);
1270 m = match_etag (&tag_e_action, &open->action);
1273 m = match_etag (&tag_e_delim, &open->delim);
1276 m = match_etag (&tag_e_pad, &open->pad);
1279 m = match_ltag (&tag_err, &open->err);
1282 m = match_etag (&tag_convert, &open->convert);
1290 /* Free the gfc_open structure and all the expressions it contains. */
1293 gfc_free_open (gfc_open *open)
1298 gfc_free_expr (open->unit);
1299 gfc_free_expr (open->iomsg);
1300 gfc_free_expr (open->iostat);
1301 gfc_free_expr (open->file);
1302 gfc_free_expr (open->status);
1303 gfc_free_expr (open->access);
1304 gfc_free_expr (open->form);
1305 gfc_free_expr (open->recl);
1306 gfc_free_expr (open->blank);
1307 gfc_free_expr (open->position);
1308 gfc_free_expr (open->action);
1309 gfc_free_expr (open->delim);
1310 gfc_free_expr (open->pad);
1311 gfc_free_expr (open->convert);
1316 /* Resolve everything in a gfc_open structure. */
1319 gfc_resolve_open (gfc_open *open)
1322 RESOLVE_TAG (&tag_unit, open->unit);
1323 RESOLVE_TAG (&tag_iomsg, open->iomsg);
1324 RESOLVE_TAG (&tag_iostat, open->iostat);
1325 RESOLVE_TAG (&tag_file, open->file);
1326 RESOLVE_TAG (&tag_status, open->status);
1327 RESOLVE_TAG (&tag_e_access, open->access);
1328 RESOLVE_TAG (&tag_e_form, open->form);
1329 RESOLVE_TAG (&tag_e_recl, open->recl);
1330 RESOLVE_TAG (&tag_e_blank, open->blank);
1331 RESOLVE_TAG (&tag_e_position, open->position);
1332 RESOLVE_TAG (&tag_e_action, open->action);
1333 RESOLVE_TAG (&tag_e_delim, open->delim);
1334 RESOLVE_TAG (&tag_e_pad, open->pad);
1335 RESOLVE_TAG (&tag_convert, open->convert);
1337 if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1344 /* Check if a given value for a SPECIFIER is either in the list of values
1345 allowed in F95 or F2003, issuing an error message and returning a zero
1346 value if it is not allowed. */
1349 compare_to_allowed_values (const char *specifier, const char *allowed[],
1350 const char *allowed_f2003[],
1351 const char *allowed_gnu[], char *value,
1352 const char *statement, bool warn)
1357 len = strlen (value);
1360 for (len--; len > 0; len--)
1361 if (value[len] != ' ')
1366 for (i = 0; allowed[i]; i++)
1367 if (len == strlen (allowed[i])
1368 && strncasecmp (value, allowed[i], strlen (allowed[i])) == 0)
1371 for (i = 0; allowed_f2003 && allowed_f2003[i]; i++)
1372 if (len == strlen (allowed_f2003[i])
1373 && strncasecmp (value, allowed_f2003[i], strlen (allowed_f2003[i]))
1376 notification n = gfc_notification_std (GFC_STD_F2003);
1378 if (n == WARNING || (warn && n == ERROR))
1380 gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1381 "has value '%s'", specifier, statement,
1388 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: %s specifier in "
1389 "%s statement at %C has value '%s'", specifier,
1390 statement, allowed_f2003[i]);
1398 for (i = 0; allowed_gnu && allowed_gnu[i]; i++)
1399 if (len == strlen (allowed_gnu[i])
1400 && strncasecmp (value, allowed_gnu[i], strlen (allowed_gnu[i])) == 0)
1402 notification n = gfc_notification_std (GFC_STD_GNU);
1404 if (n == WARNING || (warn && n == ERROR))
1406 gfc_warning ("Extension: %s specifier in %s statement at %C "
1407 "has value '%s'", specifier, statement,
1414 gfc_notify_std (GFC_STD_GNU, "Extension: %s specifier in "
1415 "%s statement at %C has value '%s'", specifier,
1416 statement, allowed_gnu[i]);
1426 gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1427 specifier, statement, value);
1432 gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1433 specifier, statement, value);
1439 /* Match an OPEN statement. */
1442 gfc_match_open (void)
1448 m = gfc_match_char ('(');
1452 open = gfc_getmem (sizeof (gfc_open));
1454 m = match_open_element (open);
1456 if (m == MATCH_ERROR)
1460 m = gfc_match_expr (&open->unit);
1463 if (m == MATCH_ERROR)
1469 if (gfc_match_char (')') == MATCH_YES)
1471 if (gfc_match_char (',') != MATCH_YES)
1474 m = match_open_element (open);
1475 if (m == MATCH_ERROR)
1481 if (gfc_match_eos () == MATCH_NO)
1484 if (gfc_pure (NULL))
1486 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1490 warn = (open->err || open->iostat) ? true : false;
1491 /* Checks on the ACCESS specifier. */
1492 if (open->access && open->access->expr_type == EXPR_CONSTANT)
1494 static const char *access_f95[] = { "SEQUENTIAL", "DIRECT", NULL };
1495 static const char *access_f2003[] = { "STREAM", NULL };
1496 static const char *access_gnu[] = { "APPEND", NULL };
1498 if (!compare_to_allowed_values ("ACCESS", access_f95, access_f2003,
1500 open->access->value.character.string,
1505 /* Checks on the ACTION specifier. */
1506 if (open->action && open->action->expr_type == EXPR_CONSTANT)
1508 static const char *action[] = { "READ", "WRITE", "READWRITE", NULL };
1510 if (!compare_to_allowed_values ("ACTION", action, NULL, NULL,
1511 open->action->value.character.string,
1516 /* Checks on the ASYNCHRONOUS specifier. */
1517 /* TODO: code is ready, just needs uncommenting when async I/O support
1519 if (open->asynchronous && open->asynchronous->expr_type == EXPR_CONSTANT)
1521 static const char * asynchronous[] = { "YES", "NO", NULL };
1523 if (!compare_to_allowed_values
1524 ("action", asynchronous, NULL, NULL,
1525 open->asynchronous->value.character.string, "OPEN", warn))
1529 /* Checks on the BLANK specifier. */
1530 if (open->blank && open->blank->expr_type == EXPR_CONSTANT)
1532 static const char *blank[] = { "ZERO", "NULL", NULL };
1534 if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
1535 open->blank->value.character.string,
1540 /* Checks on the DECIMAL specifier. */
1541 /* TODO: uncomment this code when DECIMAL support is added
1542 if (open->decimal && open->decimal->expr_type == EXPR_CONSTANT)
1544 static const char * decimal[] = { "COMMA", "POINT", NULL };
1546 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1547 open->decimal->value.character.string,
1552 /* Checks on the DELIM specifier. */
1553 if (open->delim && open->delim->expr_type == EXPR_CONSTANT)
1555 static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
1557 if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
1558 open->delim->value.character.string,
1563 /* Checks on the ENCODING specifier. */
1564 /* TODO: uncomment this code when ENCODING support is added
1565 if (open->encoding && open->encoding->expr_type == EXPR_CONSTANT)
1567 static const char * encoding[] = { "UTF-8", "DEFAULT", NULL };
1569 if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1570 open->encoding->value.character.string,
1575 /* Checks on the FORM specifier. */
1576 if (open->form && open->form->expr_type == EXPR_CONSTANT)
1578 static const char *form[] = { "FORMATTED", "UNFORMATTED", NULL };
1580 if (!compare_to_allowed_values ("FORM", form, NULL, NULL,
1581 open->form->value.character.string,
1586 /* Checks on the PAD specifier. */
1587 if (open->pad && open->pad->expr_type == EXPR_CONSTANT)
1589 static const char *pad[] = { "YES", "NO", NULL };
1591 if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
1592 open->pad->value.character.string,
1597 /* Checks on the POSITION specifier. */
1598 if (open->position && open->position->expr_type == EXPR_CONSTANT)
1600 static const char *position[] = { "ASIS", "REWIND", "APPEND", NULL };
1602 if (!compare_to_allowed_values ("POSITION", position, NULL, NULL,
1603 open->position->value.character.string,
1608 /* Checks on the ROUND specifier. */
1609 /* TODO: uncomment this code when ROUND support is added
1610 if (open->round && open->round->expr_type == EXPR_CONSTANT)
1612 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1613 "COMPATIBLE", "PROCESSOR_DEFINED", NULL };
1615 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1616 open->round->value.character.string,
1621 /* Checks on the SIGN specifier. */
1622 /* TODO: uncomment this code when SIGN support is added
1623 if (open->sign && open->sign->expr_type == EXPR_CONSTANT)
1625 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1628 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1629 open->sign->value.character.string,
1634 #define warn_or_error(...) \
1637 gfc_warning (__VA_ARGS__); \
1640 gfc_error (__VA_ARGS__); \
1645 /* Checks on the RECL specifier. */
1646 if (open->recl && open->recl->expr_type == EXPR_CONSTANT
1647 && open->recl->ts.type == BT_INTEGER
1648 && mpz_sgn (open->recl->value.integer) != 1)
1650 warn_or_error ("RECL in OPEN statement at %C must be positive");
1653 /* Checks on the STATUS specifier. */
1654 if (open->status && open->status->expr_type == EXPR_CONSTANT)
1656 static const char *status[] = { "OLD", "NEW", "SCRATCH",
1657 "REPLACE", "UNKNOWN", NULL };
1659 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1660 open->status->value.character.string,
1664 /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1665 the FILE= specifier shall appear. */
1666 if (open->file == NULL
1667 && (strncasecmp (open->status->value.character.string, "replace", 7)
1669 || strncasecmp (open->status->value.character.string, "new", 3)
1672 warn_or_error ("The STATUS specified in OPEN statement at %C is "
1673 "'%s' and no FILE specifier is present",
1674 open->status->value.character.string);
1677 /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1678 the FILE= specifier shall not appear. */
1679 if (strncasecmp (open->status->value.character.string, "scratch", 7)
1682 warn_or_error ("The STATUS specified in OPEN statement at %C "
1683 "cannot have the value SCRATCH if a FILE specifier "
1688 /* Things that are not allowed for unformatted I/O. */
1689 if (open->form && open->form->expr_type == EXPR_CONSTANT
1691 /* TODO uncomment this code when F2003 support is finished */
1692 /* || open->decimal || open->encoding || open->round
1694 || open->pad || open->blank)
1695 && strncasecmp (open->form->value.character.string,
1696 "unformatted", 11) == 0)
1698 const char *spec = (open->delim ? "DELIM "
1699 : (open->pad ? "PAD " : open->blank
1702 warn_or_error ("%s specifier at %C not allowed in OPEN statement for "
1703 "unformatted I/O", spec);
1706 if (open->recl && open->access && open->access->expr_type == EXPR_CONSTANT
1707 && strncasecmp (open->access->value.character.string, "stream", 6) == 0)
1709 warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
1714 && open->access && open->access->expr_type == EXPR_CONSTANT
1715 && !(strncasecmp (open->access->value.character.string,
1716 "sequential", 10) == 0
1717 || strncasecmp (open->access->value.character.string,
1719 || strncasecmp (open->access->value.character.string,
1722 warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
1723 "for stream or sequential ACCESS");
1726 #undef warn_or_error
1728 new_st.op = EXEC_OPEN;
1729 new_st.ext.open = open;
1733 gfc_syntax_error (ST_OPEN);
1736 gfc_free_open (open);
1741 /* Free a gfc_close structure an all its expressions. */
1744 gfc_free_close (gfc_close *close)
1749 gfc_free_expr (close->unit);
1750 gfc_free_expr (close->iomsg);
1751 gfc_free_expr (close->iostat);
1752 gfc_free_expr (close->status);
1757 /* Match elements of a CLOSE statement. */
1760 match_close_element (gfc_close *close)
1764 m = match_etag (&tag_unit, &close->unit);
1767 m = match_etag (&tag_status, &close->status);
1770 m = match_out_tag (&tag_iomsg, &close->iomsg);
1773 m = match_out_tag (&tag_iostat, &close->iostat);
1776 m = match_ltag (&tag_err, &close->err);
1784 /* Match a CLOSE statement. */
1787 gfc_match_close (void)
1793 m = gfc_match_char ('(');
1797 close = gfc_getmem (sizeof (gfc_close));
1799 m = match_close_element (close);
1801 if (m == MATCH_ERROR)
1805 m = gfc_match_expr (&close->unit);
1808 if (m == MATCH_ERROR)
1814 if (gfc_match_char (')') == MATCH_YES)
1816 if (gfc_match_char (',') != MATCH_YES)
1819 m = match_close_element (close);
1820 if (m == MATCH_ERROR)
1826 if (gfc_match_eos () == MATCH_NO)
1829 if (gfc_pure (NULL))
1831 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1835 warn = (close->iostat || close->err) ? true : false;
1837 /* Checks on the STATUS specifier. */
1838 if (close->status && close->status->expr_type == EXPR_CONSTANT)
1840 static const char *status[] = { "KEEP", "DELETE", NULL };
1842 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1843 close->status->value.character.string,
1848 new_st.op = EXEC_CLOSE;
1849 new_st.ext.close = close;
1853 gfc_syntax_error (ST_CLOSE);
1856 gfc_free_close (close);
1861 /* Resolve everything in a gfc_close structure. */
1864 gfc_resolve_close (gfc_close *close)
1866 RESOLVE_TAG (&tag_unit, close->unit);
1867 RESOLVE_TAG (&tag_iomsg, close->iomsg);
1868 RESOLVE_TAG (&tag_iostat, close->iostat);
1869 RESOLVE_TAG (&tag_status, close->status);
1871 if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
1878 /* Free a gfc_filepos structure. */
1881 gfc_free_filepos (gfc_filepos *fp)
1883 gfc_free_expr (fp->unit);
1884 gfc_free_expr (fp->iomsg);
1885 gfc_free_expr (fp->iostat);
1890 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
1893 match_file_element (gfc_filepos *fp)
1897 m = match_etag (&tag_unit, &fp->unit);
1900 m = match_out_tag (&tag_iomsg, &fp->iomsg);
1903 m = match_out_tag (&tag_iostat, &fp->iostat);
1906 m = match_ltag (&tag_err, &fp->err);
1914 /* Match the second half of the file-positioning statements, REWIND,
1915 BACKSPACE, ENDFILE, or the FLUSH statement. */
1918 match_filepos (gfc_statement st, gfc_exec_op op)
1923 fp = gfc_getmem (sizeof (gfc_filepos));
1925 if (gfc_match_char ('(') == MATCH_NO)
1927 m = gfc_match_expr (&fp->unit);
1928 if (m == MATCH_ERROR)
1936 m = match_file_element (fp);
1937 if (m == MATCH_ERROR)
1941 m = gfc_match_expr (&fp->unit);
1942 if (m == MATCH_ERROR)
1950 if (gfc_match_char (')') == MATCH_YES)
1952 if (gfc_match_char (',') != MATCH_YES)
1955 m = match_file_element (fp);
1956 if (m == MATCH_ERROR)
1963 if (gfc_match_eos () != MATCH_YES)
1966 if (gfc_pure (NULL))
1968 gfc_error ("%s statement not allowed in PURE procedure at %C",
1969 gfc_ascii_statement (st));
1975 new_st.ext.filepos = fp;
1979 gfc_syntax_error (st);
1982 gfc_free_filepos (fp);
1988 gfc_resolve_filepos (gfc_filepos *fp)
1990 RESOLVE_TAG (&tag_unit, fp->unit);
1991 RESOLVE_TAG (&tag_iostat, fp->iostat);
1992 RESOLVE_TAG (&tag_iomsg, fp->iomsg);
1993 if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
2000 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
2001 and the FLUSH statement. */
2004 gfc_match_endfile (void)
2006 return match_filepos (ST_END_FILE, EXEC_ENDFILE);
2010 gfc_match_backspace (void)
2012 return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
2016 gfc_match_rewind (void)
2018 return match_filepos (ST_REWIND, EXEC_REWIND);
2022 gfc_match_flush (void)
2024 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: FLUSH statement at %C")
2028 return match_filepos (ST_FLUSH, EXEC_FLUSH);
2031 /******************** Data Transfer Statements *********************/
2034 { M_READ, M_WRITE, M_PRINT, M_INQUIRE }
2038 /* Return a default unit number. */
2041 default_unit (io_kind k)
2050 return gfc_int_expr (unit);
2054 /* Match a unit specification for a data transfer statement. */
2057 match_dt_unit (io_kind k, gfc_dt *dt)
2061 if (gfc_match_char ('*') == MATCH_YES)
2063 if (dt->io_unit != NULL)
2066 dt->io_unit = default_unit (k);
2070 if (gfc_match_expr (&e) == MATCH_YES)
2072 if (dt->io_unit != NULL)
2085 gfc_error ("Duplicate UNIT specification at %C");
2090 /* Match a format specification. */
2093 match_dt_format (gfc_dt *dt)
2097 gfc_st_label *label;
2099 where = gfc_current_locus;
2101 if (gfc_match_char ('*') == MATCH_YES)
2103 if (dt->format_expr != NULL || dt->format_label != NULL)
2106 dt->format_label = &format_asterisk;
2110 if (gfc_match_st_label (&label) == MATCH_YES)
2112 if (dt->format_expr != NULL || dt->format_label != NULL)
2114 gfc_free_st_label (label);
2118 if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
2121 dt->format_label = label;
2125 if (gfc_match_expr (&e) == MATCH_YES)
2127 if (dt->format_expr != NULL || dt->format_label != NULL)
2132 dt->format_expr = e;
2136 gfc_current_locus = where; /* The only case where we have to restore */
2141 gfc_error ("Duplicate format specification at %C");
2146 /* Traverse a namelist that is part of a READ statement to make sure
2147 that none of the variables in the namelist are INTENT(IN). Returns
2148 nonzero if we find such a variable. */
2151 check_namelist (gfc_symbol *sym)
2155 for (p = sym->namelist; p; p = p->next)
2156 if (p->sym->attr.intent == INTENT_IN)
2158 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2159 p->sym->name, sym->name);
2167 /* Match a single data transfer element. */
2170 match_dt_element (io_kind k, gfc_dt *dt)
2172 char name[GFC_MAX_SYMBOL_LEN + 1];
2176 if (gfc_match (" unit =") == MATCH_YES)
2178 m = match_dt_unit (k, dt);
2183 if (gfc_match (" fmt =") == MATCH_YES)
2185 m = match_dt_format (dt);
2190 if (gfc_match (" nml = %n", name) == MATCH_YES)
2192 if (dt->namelist != NULL)
2194 gfc_error ("Duplicate NML specification at %C");
2198 if (gfc_find_symbol (name, NULL, 1, &sym))
2201 if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
2203 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2204 sym != NULL ? sym->name : name);
2209 if (k == M_READ && check_namelist (sym))
2215 m = match_etag (&tag_rec, &dt->rec);
2218 m = match_etag (&tag_spos, &dt->rec);
2221 m = match_out_tag (&tag_iomsg, &dt->iomsg);
2224 m = match_out_tag (&tag_iostat, &dt->iostat);
2227 m = match_ltag (&tag_err, &dt->err);
2229 dt->err_where = gfc_current_locus;
2232 m = match_etag (&tag_advance, &dt->advance);
2235 m = match_out_tag (&tag_size, &dt->size);
2239 m = match_ltag (&tag_end, &dt->end);
2244 gfc_error ("END tag at %C not allowed in output statement");
2247 dt->end_where = gfc_current_locus;
2252 m = match_ltag (&tag_eor, &dt->eor);
2254 dt->eor_where = gfc_current_locus;
2262 /* Free a data transfer structure and everything below it. */
2265 gfc_free_dt (gfc_dt *dt)
2270 gfc_free_expr (dt->io_unit);
2271 gfc_free_expr (dt->format_expr);
2272 gfc_free_expr (dt->rec);
2273 gfc_free_expr (dt->advance);
2274 gfc_free_expr (dt->iomsg);
2275 gfc_free_expr (dt->iostat);
2276 gfc_free_expr (dt->size);
2281 /* Resolve everything in a gfc_dt structure. */
2284 gfc_resolve_dt (gfc_dt *dt)
2288 RESOLVE_TAG (&tag_format, dt->format_expr);
2289 RESOLVE_TAG (&tag_rec, dt->rec);
2290 RESOLVE_TAG (&tag_spos, dt->rec);
2291 RESOLVE_TAG (&tag_advance, dt->advance);
2292 RESOLVE_TAG (&tag_iomsg, dt->iomsg);
2293 RESOLVE_TAG (&tag_iostat, dt->iostat);
2294 RESOLVE_TAG (&tag_size, dt->size);
2297 if (gfc_resolve_expr (e) == SUCCESS
2298 && (e->ts.type != BT_INTEGER
2299 && (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_VARIABLE)))
2301 gfc_error ("UNIT specification at %L must be an INTEGER expression "
2302 "or a CHARACTER variable", &e->where);
2306 if (e->ts.type == BT_CHARACTER)
2308 if (gfc_has_vector_index (e))
2310 gfc_error ("Internal unit with vector subscript at %L", &e->where);
2315 if (e->rank && e->ts.type != BT_CHARACTER)
2317 gfc_error ("External IO UNIT cannot be an array at %L", &e->where);
2323 if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
2325 if (dt->err->defined == ST_LABEL_UNKNOWN)
2327 gfc_error ("ERR tag label %d at %L not defined",
2328 dt->err->value, &dt->err_where);
2335 if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
2337 if (dt->end->defined == ST_LABEL_UNKNOWN)
2339 gfc_error ("END tag label %d at %L not defined",
2340 dt->end->value, &dt->end_where);
2347 if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
2349 if (dt->eor->defined == ST_LABEL_UNKNOWN)
2351 gfc_error ("EOR tag label %d at %L not defined",
2352 dt->eor->value, &dt->eor_where);
2357 /* Check the format label actually exists. */
2358 if (dt->format_label && dt->format_label != &format_asterisk
2359 && dt->format_label->defined == ST_LABEL_UNKNOWN)
2361 gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
2362 &dt->format_label->where);
2369 /* Given an io_kind, return its name. */
2372 io_kind_name (io_kind k)
2391 gfc_internal_error ("io_kind_name(): bad I/O-kind");
2398 /* Match an IO iteration statement of the form:
2400 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2402 which is equivalent to a single IO element. This function is
2403 mutually recursive with match_io_element(). */
2405 static match match_io_element (io_kind, gfc_code **);
2408 match_io_iterator (io_kind k, gfc_code **result)
2410 gfc_code *head, *tail, *new;
2418 old_loc = gfc_current_locus;
2420 if (gfc_match_char ('(') != MATCH_YES)
2423 m = match_io_element (k, &head);
2426 if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
2432 /* Can't be anything but an IO iterator. Build a list. */
2433 iter = gfc_get_iterator ();
2437 m = gfc_match_iterator (iter, 0);
2438 if (m == MATCH_ERROR)
2442 gfc_check_do_variable (iter->var->symtree);
2446 m = match_io_element (k, &new);
2447 if (m == MATCH_ERROR)
2456 tail = gfc_append_code (tail, new);
2458 if (gfc_match_char (',') != MATCH_YES)
2467 if (gfc_match_char (')') != MATCH_YES)
2470 new = gfc_get_code ();
2472 new->ext.iterator = iter;
2474 new->block = gfc_get_code ();
2475 new->block->op = EXEC_DO;
2476 new->block->next = head;
2482 gfc_error ("Syntax error in I/O iterator at %C");
2486 gfc_free_iterator (iter, 1);
2487 gfc_free_statements (head);
2488 gfc_current_locus = old_loc;
2493 /* Match a single element of an IO list, which is either a single
2494 expression or an IO Iterator. */
2497 match_io_element (io_kind k, gfc_code **cpp)
2505 m = match_io_iterator (k, cpp);
2511 m = gfc_match_variable (&expr, 0);
2513 gfc_error ("Expected variable in READ statement at %C");
2517 m = gfc_match_expr (&expr);
2519 gfc_error ("Expected expression in %s statement at %C",
2527 if (expr->symtree->n.sym->attr.intent == INTENT_IN)
2529 gfc_error ("Variable '%s' in input list at %C cannot be "
2530 "INTENT(IN)", expr->symtree->n.sym->name);
2535 && gfc_impure_variable (expr->symtree->n.sym)
2536 && current_dt->io_unit->ts.type == BT_CHARACTER)
2538 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2539 expr->symtree->n.sym->name);
2543 if (gfc_check_do_variable (expr->symtree))
2549 if (current_dt->io_unit->ts.type == BT_CHARACTER
2551 && current_dt->io_unit->expr_type == EXPR_VARIABLE
2552 && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
2554 gfc_error ("Cannot write to internal file unit '%s' at %C "
2555 "inside a PURE procedure",
2556 current_dt->io_unit->symtree->n.sym->name);
2568 gfc_free_expr (expr);
2572 cp = gfc_get_code ();
2573 cp->op = EXEC_TRANSFER;
2581 /* Match an I/O list, building gfc_code structures as we go. */
2584 match_io_list (io_kind k, gfc_code **head_p)
2586 gfc_code *head, *tail, *new;
2589 *head_p = head = tail = NULL;
2590 if (gfc_match_eos () == MATCH_YES)
2595 m = match_io_element (k, &new);
2596 if (m == MATCH_ERROR)
2601 tail = gfc_append_code (tail, new);
2605 if (gfc_match_eos () == MATCH_YES)
2607 if (gfc_match_char (',') != MATCH_YES)
2615 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2618 gfc_free_statements (head);
2623 /* Attach the data transfer end node. */
2626 terminate_io (gfc_code *io_code)
2630 if (io_code == NULL)
2631 io_code = new_st.block;
2633 c = gfc_get_code ();
2634 c->op = EXEC_DT_END;
2636 /* Point to structure that is already there */
2637 c->ext.dt = new_st.ext.dt;
2638 gfc_append_code (io_code, c);
2642 /* Check the constraints for a data transfer statement. The majority of the
2643 constraints appearing in 9.4 of the standard appear here. Some are handled
2644 in resolve_tag and others in gfc_resolve_dt. */
2647 check_io_constraints (io_kind k, gfc_dt *dt, gfc_code *io_code,
2650 #define io_constraint(condition,msg,arg)\
2653 gfc_error(msg,arg);\
2659 gfc_symbol *sym = NULL;
2664 if (expr && expr->expr_type == EXPR_VARIABLE
2665 && expr->ts.type == BT_CHARACTER)
2667 sym = expr->symtree->n.sym;
2669 io_constraint (k == M_WRITE && sym->attr.intent == INTENT_IN,
2670 "Internal file at %L must not be INTENT(IN)",
2673 io_constraint (gfc_has_vector_index (dt->io_unit),
2674 "Internal file incompatible with vector subscript at %L",
2677 io_constraint (dt->rec != NULL,
2678 "REC tag at %L is incompatible with internal file",
2681 if (dt->namelist != NULL)
2683 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Internal file "
2684 "at %L with namelist", &expr->where)
2689 io_constraint (dt->advance != NULL,
2690 "ADVANCE tag at %L is incompatible with internal file",
2691 &dt->advance->where);
2694 if (expr && expr->ts.type != BT_CHARACTER)
2697 io_constraint (gfc_pure (NULL) && (k == M_READ || k == M_WRITE),
2698 "IO UNIT in %s statement at %C must be "
2699 "an internal file in a PURE procedure",
2706 io_constraint (dt->end, "END tag not allowed with output at %L",
2709 io_constraint (dt->eor, "EOR tag not allowed with output at %L",
2712 io_constraint (k != M_READ && dt->size,
2713 "SIZE=specifier not allowed with output at %L",
2718 io_constraint (dt->size && dt->advance == NULL,
2719 "SIZE tag at %L requires an ADVANCE tag",
2722 io_constraint (dt->eor && dt->advance == NULL,
2723 "EOR tag at %L requires an ADVANCE tag",
2731 io_constraint (io_code && dt->namelist,
2732 "NAMELIST cannot be followed by IO-list at %L",
2735 io_constraint (dt->format_expr,
2736 "IO spec-list cannot contain both NAMELIST group name "
2737 "and format specification at %L.",
2738 &dt->format_expr->where);
2740 io_constraint (dt->format_label,
2741 "IO spec-list cannot contain both NAMELIST group name "
2742 "and format label at %L", spec_end);
2744 io_constraint (dt->rec,
2745 "NAMELIST IO is not allowed with a REC=specifier "
2746 "at %L.", &dt->rec->where);
2748 io_constraint (dt->advance,
2749 "NAMELIST IO is not allowed with a ADVANCE=specifier "
2750 "at %L.", &dt->advance->where);
2755 io_constraint (dt->end,
2756 "An END tag is not allowed with a "
2757 "REC=specifier at %L.", &dt->end_where);
2760 io_constraint (dt->format_label == &format_asterisk,
2761 "FMT=* is not allowed with a REC=specifier "
2762 "at %L.", spec_end);
2767 int not_yes, not_no;
2770 io_constraint (dt->format_label == &format_asterisk,
2771 "List directed format(*) is not allowed with a "
2772 "ADVANCE=specifier at %L.", &expr->where);
2774 io_constraint (dt->format_expr == NULL && dt->format_label == NULL
2775 && dt->namelist == NULL,
2776 "the ADVANCE=specifier at %L must appear with an "
2777 "explicit format expression", &expr->where);
2779 if (expr->expr_type == EXPR_CONSTANT && expr->ts.type == BT_CHARACTER)
2781 const char * advance = expr->value.character.string;
2782 not_no = strcasecmp (advance, "no") != 0;
2783 not_yes = strcasecmp (advance, "yes") != 0;
2791 io_constraint (not_no && not_yes,
2792 "ADVANCE=specifier at %L must have value = "
2793 "YES or NO.", &expr->where);
2795 io_constraint (dt->size && not_no && k == M_READ,
2796 "SIZE tag at %L requires an ADVANCE = 'NO'",
2799 io_constraint (dt->eor && not_no && k == M_READ,
2800 "EOR tag at %L requires an ADVANCE = 'NO'",
2804 expr = dt->format_expr;
2805 if (expr != NULL && expr->expr_type == EXPR_CONSTANT
2806 && check_format_string (expr, k == M_READ) == FAILURE)
2811 #undef io_constraint
2814 /* Match a READ, WRITE or PRINT statement. */
2817 match_io (io_kind k)
2819 char name[GFC_MAX_SYMBOL_LEN + 1];
2828 where = gfc_current_locus;
2830 current_dt = dt = gfc_getmem (sizeof (gfc_dt));
2831 m = gfc_match_char ('(');
2834 where = gfc_current_locus;
2837 else if (k == M_PRINT)
2839 /* Treat the non-standard case of PRINT namelist. */
2840 if ((gfc_current_form == FORM_FIXED || gfc_peek_char () == ' ')
2841 && gfc_match_name (name) == MATCH_YES)
2843 gfc_find_symbol (name, NULL, 1, &sym);
2844 if (sym && sym->attr.flavor == FL_NAMELIST)
2846 if (gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
2847 "%C is an extension") == FAILURE)
2853 dt->io_unit = default_unit (k);
2858 gfc_current_locus = where;
2862 if (gfc_current_form == FORM_FREE)
2864 c = gfc_peek_char();
2865 if (c != ' ' && c != '*' && c != '\'' && c != '"')
2872 m = match_dt_format (dt);
2873 if (m == MATCH_ERROR)
2879 dt->io_unit = default_unit (k);
2884 /* Before issuing an error for a malformed 'print (1,*)' type of
2885 error, check for a default-char-expr of the form ('(I0)'). */
2886 if (k == M_PRINT && m == MATCH_YES)
2888 /* Reset current locus to get the initial '(' in an expression. */
2889 gfc_current_locus = where;
2890 dt->format_expr = NULL;
2891 m = match_dt_format (dt);
2893 if (m == MATCH_ERROR)
2895 if (m == MATCH_NO || dt->format_expr == NULL)
2899 dt->io_unit = default_unit (k);
2904 /* Match a control list */
2905 if (match_dt_element (k, dt) == MATCH_YES)
2907 if (match_dt_unit (k, dt) != MATCH_YES)
2910 if (gfc_match_char (')') == MATCH_YES)
2912 if (gfc_match_char (',') != MATCH_YES)
2915 m = match_dt_element (k, dt);
2918 if (m == MATCH_ERROR)
2921 m = match_dt_format (dt);
2924 if (m == MATCH_ERROR)
2927 where = gfc_current_locus;
2929 m = gfc_match_name (name);
2932 gfc_find_symbol (name, NULL, 1, &sym);
2933 if (sym && sym->attr.flavor == FL_NAMELIST)
2936 if (k == M_READ && check_namelist (sym))
2945 gfc_current_locus = where;
2947 goto loop; /* No matches, try regular elements */
2950 if (gfc_match_char (')') == MATCH_YES)
2952 if (gfc_match_char (',') != MATCH_YES)
2958 m = match_dt_element (k, dt);
2961 if (m == MATCH_ERROR)
2964 if (gfc_match_char (')') == MATCH_YES)
2966 if (gfc_match_char (',') != MATCH_YES)
2972 /* Used in check_io_constraints, where no locus is available. */
2973 spec_end = gfc_current_locus;
2975 /* Optional leading comma (non-standard). */
2977 && gfc_match_char (',') == MATCH_YES
2978 && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before i/o "
2979 "item list at %C") == FAILURE)
2983 if (gfc_match_eos () != MATCH_YES)
2985 if (comma_flag && gfc_match_char (',') != MATCH_YES)
2987 gfc_error ("Expected comma in I/O list at %C");
2992 m = match_io_list (k, &io_code);
2993 if (m == MATCH_ERROR)
2999 /* A full IO statement has been matched. Check the constraints. spec_end is
3000 supplied for cases where no locus is supplied. */
3001 m = check_io_constraints (k, dt, io_code, &spec_end);
3003 if (m == MATCH_ERROR)
3006 new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
3008 new_st.block = gfc_get_code ();
3009 new_st.block->op = new_st.op;
3010 new_st.block->next = io_code;
3012 terminate_io (io_code);
3017 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
3027 gfc_match_read (void)
3029 return match_io (M_READ);
3033 gfc_match_write (void)
3035 return match_io (M_WRITE);
3039 gfc_match_print (void)
3043 m = match_io (M_PRINT);
3047 if (gfc_pure (NULL))
3049 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
3057 /* Free a gfc_inquire structure. */
3060 gfc_free_inquire (gfc_inquire *inquire)
3063 if (inquire == NULL)
3066 gfc_free_expr (inquire->unit);
3067 gfc_free_expr (inquire->file);
3068 gfc_free_expr (inquire->iomsg);
3069 gfc_free_expr (inquire->iostat);
3070 gfc_free_expr (inquire->exist);
3071 gfc_free_expr (inquire->opened);
3072 gfc_free_expr (inquire->number);
3073 gfc_free_expr (inquire->named);
3074 gfc_free_expr (inquire->name);
3075 gfc_free_expr (inquire->access);
3076 gfc_free_expr (inquire->sequential);
3077 gfc_free_expr (inquire->direct);
3078 gfc_free_expr (inquire->form);
3079 gfc_free_expr (inquire->formatted);
3080 gfc_free_expr (inquire->unformatted);
3081 gfc_free_expr (inquire->recl);
3082 gfc_free_expr (inquire->nextrec);
3083 gfc_free_expr (inquire->blank);
3084 gfc_free_expr (inquire->position);
3085 gfc_free_expr (inquire->action);
3086 gfc_free_expr (inquire->read);
3087 gfc_free_expr (inquire->write);
3088 gfc_free_expr (inquire->readwrite);
3089 gfc_free_expr (inquire->delim);
3090 gfc_free_expr (inquire->pad);
3091 gfc_free_expr (inquire->iolength);
3092 gfc_free_expr (inquire->convert);
3093 gfc_free_expr (inquire->strm_pos);
3098 /* Match an element of an INQUIRE statement. */
3100 #define RETM if (m != MATCH_NO) return m;
3103 match_inquire_element (gfc_inquire *inquire)
3107 m = match_etag (&tag_unit, &inquire->unit);
3108 RETM m = match_etag (&tag_file, &inquire->file);
3109 RETM m = match_ltag (&tag_err, &inquire->err);
3110 RETM m = match_out_tag (&tag_iomsg, &inquire->iomsg);
3111 RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
3112 RETM m = match_vtag (&tag_exist, &inquire->exist);
3113 RETM m = match_vtag (&tag_opened, &inquire->opened);
3114 RETM m = match_vtag (&tag_named, &inquire->named);
3115 RETM m = match_vtag (&tag_name, &inquire->name);
3116 RETM m = match_out_tag (&tag_number, &inquire->number);
3117 RETM m = match_vtag (&tag_s_access, &inquire->access);
3118 RETM m = match_vtag (&tag_sequential, &inquire->sequential);
3119 RETM m = match_vtag (&tag_direct, &inquire->direct);
3120 RETM m = match_vtag (&tag_s_form, &inquire->form);
3121 RETM m = match_vtag (&tag_formatted, &inquire->formatted);
3122 RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
3123 RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
3124 RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
3125 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
3126 RETM m = match_vtag (&tag_s_position, &inquire->position);
3127 RETM m = match_vtag (&tag_s_action, &inquire->action);
3128 RETM m = match_vtag (&tag_read, &inquire->read);
3129 RETM m = match_vtag (&tag_write, &inquire->write);
3130 RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
3131 RETM m = match_vtag (&tag_s_delim, &inquire->delim);
3132 RETM m = match_vtag (&tag_s_pad, &inquire->pad);
3133 RETM m = match_vtag (&tag_iolength, &inquire->iolength);
3134 RETM m = match_vtag (&tag_convert, &inquire->convert);
3135 RETM m = match_out_tag (&tag_strm_out, &inquire->strm_pos);
3136 RETM return MATCH_NO;
3143 gfc_match_inquire (void)
3145 gfc_inquire *inquire;
3150 m = gfc_match_char ('(');
3154 inquire = gfc_getmem (sizeof (gfc_inquire));
3156 loc = gfc_current_locus;
3158 m = match_inquire_element (inquire);
3159 if (m == MATCH_ERROR)
3163 m = gfc_match_expr (&inquire->unit);
3164 if (m == MATCH_ERROR)
3170 /* See if we have the IOLENGTH form of the inquire statement. */
3171 if (inquire->iolength != NULL)
3173 if (gfc_match_char (')') != MATCH_YES)
3176 m = match_io_list (M_INQUIRE, &code);
3177 if (m == MATCH_ERROR)
3182 new_st.op = EXEC_IOLENGTH;
3183 new_st.expr = inquire->iolength;
3184 new_st.ext.inquire = inquire;
3186 if (gfc_pure (NULL))
3188 gfc_free_statements (code);
3189 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3193 new_st.block = gfc_get_code ();
3194 new_st.block->op = EXEC_IOLENGTH;
3195 terminate_io (code);
3196 new_st.block->next = code;
3200 /* At this point, we have the non-IOLENGTH inquire statement. */
3203 if (gfc_match_char (')') == MATCH_YES)
3205 if (gfc_match_char (',') != MATCH_YES)
3208 m = match_inquire_element (inquire);
3209 if (m == MATCH_ERROR)
3214 if (inquire->iolength != NULL)
3216 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3221 if (gfc_match_eos () != MATCH_YES)
3224 if (inquire->unit != NULL && inquire->file != NULL)
3226 gfc_error ("INQUIRE statement at %L cannot contain both FILE and "
3227 "UNIT specifiers", &loc);
3231 if (inquire->unit == NULL && inquire->file == NULL)
3233 gfc_error ("INQUIRE statement at %L requires either FILE or "
3234 "UNIT specifier", &loc);
3238 if (gfc_pure (NULL))
3240 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3244 new_st.op = EXEC_INQUIRE;
3245 new_st.ext.inquire = inquire;
3249 gfc_syntax_error (ST_INQUIRE);
3252 gfc_free_inquire (inquire);
3257 /* Resolve everything in a gfc_inquire structure. */
3260 gfc_resolve_inquire (gfc_inquire *inquire)
3262 RESOLVE_TAG (&tag_unit, inquire->unit);
3263 RESOLVE_TAG (&tag_file, inquire->file);
3264 RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
3265 RESOLVE_TAG (&tag_iostat, inquire->iostat);
3266 RESOLVE_TAG (&tag_exist, inquire->exist);
3267 RESOLVE_TAG (&tag_opened, inquire->opened);
3268 RESOLVE_TAG (&tag_number, inquire->number);
3269 RESOLVE_TAG (&tag_named, inquire->named);
3270 RESOLVE_TAG (&tag_name, inquire->name);
3271 RESOLVE_TAG (&tag_s_access, inquire->access);
3272 RESOLVE_TAG (&tag_sequential, inquire->sequential);
3273 RESOLVE_TAG (&tag_direct, inquire->direct);
3274 RESOLVE_TAG (&tag_s_form, inquire->form);
3275 RESOLVE_TAG (&tag_formatted, inquire->formatted);
3276 RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
3277 RESOLVE_TAG (&tag_s_recl, inquire->recl);
3278 RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
3279 RESOLVE_TAG (&tag_s_blank, inquire->blank);
3280 RESOLVE_TAG (&tag_s_position, inquire->position);
3281 RESOLVE_TAG (&tag_s_action, inquire->action);
3282 RESOLVE_TAG (&tag_read, inquire->read);
3283 RESOLVE_TAG (&tag_write, inquire->write);
3284 RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
3285 RESOLVE_TAG (&tag_s_delim, inquire->delim);
3286 RESOLVE_TAG (&tag_s_pad, inquire->pad);
3287 RESOLVE_TAG (&tag_iolength, inquire->iolength);
3288 RESOLVE_TAG (&tag_convert, inquire->convert);
3289 RESOLVE_TAG (&tag_strm_out, inquire->strm_pos);
3291 if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)