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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
31 format_asterisk = {0, NULL, NULL, -1, ST_LABEL_FORMAT, ST_LABEL_FORMAT, NULL,
36 const char *name, *spec;
42 tag_file = { "FILE", " file = %e", BT_CHARACTER },
43 tag_status = { "STATUS", " status = %e", BT_CHARACTER},
44 tag_e_access = {"ACCESS", " access = %e", BT_CHARACTER},
45 tag_e_form = {"FORM", " form = %e", BT_CHARACTER},
46 tag_e_recl = {"RECL", " recl = %e", BT_INTEGER},
47 tag_e_blank = {"BLANK", " blank = %e", BT_CHARACTER},
48 tag_e_position = {"POSITION", " position = %e", BT_CHARACTER},
49 tag_e_action = {"ACTION", " action = %e", BT_CHARACTER},
50 tag_e_delim = {"DELIM", " delim = %e", BT_CHARACTER},
51 tag_e_pad = {"PAD", " pad = %e", BT_CHARACTER},
52 tag_unit = {"UNIT", " unit = %e", BT_INTEGER},
53 tag_advance = {"ADVANCE", " advance = %e", BT_CHARACTER},
54 tag_rec = {"REC", " rec = %e", BT_INTEGER},
55 tag_spos = {"POSITION", " pos = %e", BT_INTEGER},
56 tag_format = {"FORMAT", NULL, BT_CHARACTER},
57 tag_iomsg = {"IOMSG", " iomsg = %e", BT_CHARACTER},
58 tag_iostat = {"IOSTAT", " iostat = %v", BT_INTEGER},
59 tag_size = {"SIZE", " size = %v", BT_INTEGER},
60 tag_exist = {"EXIST", " exist = %v", BT_LOGICAL},
61 tag_opened = {"OPENED", " opened = %v", BT_LOGICAL},
62 tag_named = {"NAMED", " named = %v", BT_LOGICAL},
63 tag_name = {"NAME", " name = %v", BT_CHARACTER},
64 tag_number = {"NUMBER", " number = %v", BT_INTEGER},
65 tag_s_access = {"ACCESS", " access = %v", BT_CHARACTER},
66 tag_sequential = {"SEQUENTIAL", " sequential = %v", BT_CHARACTER},
67 tag_direct = {"DIRECT", " direct = %v", BT_CHARACTER},
68 tag_s_form = {"FORM", " form = %v", BT_CHARACTER},
69 tag_formatted = {"FORMATTED", " formatted = %v", BT_CHARACTER},
70 tag_unformatted = {"UNFORMATTED", " unformatted = %v", BT_CHARACTER},
71 tag_s_recl = {"RECL", " recl = %v", BT_INTEGER},
72 tag_nextrec = {"NEXTREC", " nextrec = %v", BT_INTEGER},
73 tag_s_blank = {"BLANK", " blank = %v", BT_CHARACTER},
74 tag_s_position = {"POSITION", " position = %v", BT_CHARACTER},
75 tag_s_action = {"ACTION", " action = %v", BT_CHARACTER},
76 tag_read = {"READ", " read = %v", BT_CHARACTER},
77 tag_write = {"WRITE", " write = %v", BT_CHARACTER},
78 tag_readwrite = {"READWRITE", " readwrite = %v", BT_CHARACTER},
79 tag_s_delim = {"DELIM", " delim = %v", BT_CHARACTER},
80 tag_s_pad = {"PAD", " pad = %v", BT_CHARACTER},
81 tag_iolength = {"IOLENGTH", " iolength = %v", BT_INTEGER},
82 tag_convert = {"CONVERT", " convert = %e", BT_CHARACTER},
83 tag_strm_out = {"POS", " pos = %v", BT_INTEGER},
84 tag_err = {"ERR", " err = %l", BT_UNKNOWN},
85 tag_end = {"END", " end = %l", BT_UNKNOWN},
86 tag_eor = {"EOR", " eor = %l", BT_UNKNOWN};
88 static gfc_dt *current_dt;
90 #define RESOLVE_TAG(x, y) if (resolve_tag(x, y) == FAILURE) return FAILURE;
93 /**************** Fortran 95 FORMAT parser *****************/
95 /* FORMAT tokens returned by format_lex(). */
98 FMT_NONE, FMT_UNKNOWN, FMT_SIGNED_INT, FMT_ZERO, FMT_POSINT, FMT_PERIOD,
99 FMT_COMMA, FMT_COLON, FMT_SLASH, FMT_DOLLAR, FMT_POS, FMT_LPAREN,
100 FMT_RPAREN, FMT_X, FMT_SIGN, FMT_BLANK, FMT_CHAR, FMT_P, FMT_IBOZ, FMT_F,
101 FMT_E, FMT_EXT, FMT_G, FMT_L, FMT_A, FMT_D, FMT_H, FMT_END
105 /* Local variables for checking format strings. The saved_token is
106 used to back up by a single format token during the parsing
108 static char *format_string;
109 static int format_length, use_last_char;
111 static format_token saved_token;
114 { MODE_STRING, MODE_FORMAT, MODE_COPY }
118 /* Return the next character in the format string. */
121 next_char (int in_string)
133 if (mode == MODE_STRING)
134 c = *format_string++;
137 c = gfc_next_char_literal (in_string);
142 if (gfc_option.flag_backslash && c == '\\')
145 locus old_locus = gfc_current_locus;
147 /* Use a temp variable to avoid side effects from gfc_match_special_char
148 since it uses an int * for its argument. */
151 if (gfc_match_special_char (&tmp) == MATCH_NO)
152 gfc_current_locus = old_locus;
156 if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
157 gfc_warning ("Extension: backslash character at %C");
160 if (mode == MODE_COPY)
161 *format_string++ = c;
168 /* Back up one character position. Only works once. */
176 /* Eat up the spaces and return a character. */
179 next_char_not_space (void)
186 while (gfc_is_whitespace (c));
190 static int value = 0;
192 /* Simple lexical analyzer for getting the next token in a FORMAT
203 if (saved_token != FMT_NONE)
206 saved_token = FMT_NONE;
210 c = next_char_not_space ();
218 c = next_char_not_space ();
229 c = next_char_not_space ();
231 value = 10 * value + c - '0';
240 token = FMT_SIGNED_INT;
259 c = next_char_not_space ();
262 value = 10 * value + c - '0';
270 token = zflag ? FMT_ZERO : FMT_POSINT;
294 c = next_char_not_space ();
295 if (c != 'L' && c != 'R')
314 c = next_char_not_space ();
315 if (c != 'P' && c != 'S')
322 c = next_char_not_space ();
323 if (c == 'N' || c == 'Z')
384 c = next_char_not_space ();
385 if (c == 'N' || c == 'S')
428 /* Check a format statement. The format string, either from a FORMAT
429 statement or a constant in an I/O statement has already been parsed
430 by itself, and we are checking it for validity. The dual origin
431 means that the warning message is a little less than great. */
434 check_format (bool is_input)
436 const char *posint_required = _("Positive width required");
437 const char *nonneg_required = _("Nonnegative width required");
438 const char *unexpected_element = _("Unexpected element");
439 const char *unexpected_end = _("Unexpected end of format string");
448 saved_token = FMT_NONE;
456 error = _("Missing leading left parenthesis");
462 goto finished; /* Empty format is legal */
466 /* In this state, the next thing has to be a format item. */
490 /* Signed integer can only precede a P format. */
494 error = _("Expected P edit descriptor");
501 /* P requires a prior number. */
502 error = _("P descriptor requires leading scale factor");
506 /* X requires a prior number if we're being pedantic. */
507 if (gfc_notify_std (GFC_STD_GNU, "Extension: X descriptor "
508 "requires leading space count at %C")
518 goto extension_optional_comma;
527 if (gfc_notify_std (GFC_STD_GNU, "Extension: $ descriptor at %C")
530 if (t != FMT_RPAREN || level > 0)
532 gfc_warning ("$ should be the last specifier in format at %C");
533 goto optional_comma_1;
551 error = unexpected_end;
555 error = unexpected_element;
560 /* In this state, t must currently be a data descriptor.
561 Deal with things that can/must follow the descriptor. */
575 error = _("Repeat count cannot follow P descriptor");
590 switch (gfc_notification_std (GFC_STD_GNU))
593 gfc_warning ("Extension: Missing positive width after L "
599 error = posint_required;
624 error = posint_required;
631 /* Warn if -std=legacy, otherwise error. */
632 if (gfc_option.warn_std != 0)
633 gfc_error_now ("Period required in format specifier at %C");
635 gfc_warning ("Period required in format specifier at %C");
641 if (u != FMT_ZERO && u != FMT_POSINT)
643 error = nonneg_required;
650 /* Look for optional exponent. */
661 error = _("Positive exponent width required");
670 if (t != FMT_ZERO && t != FMT_POSINT)
672 error = nonneg_required;
675 else if (is_input && t == FMT_ZERO)
677 error = posint_required;
684 /* Warn if -std=legacy, otherwise error. */
685 if (gfc_option.warn_std != 0)
686 gfc_error_now ("Period required in format specifier at %C");
688 gfc_warning ("Period required in format specifier at %C");
694 if (t != FMT_ZERO && t != FMT_POSINT)
696 error = nonneg_required;
703 if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
704 gfc_warning ("The H format specifier at %C is"
705 " a Fortran 95 deleted feature");
707 if(mode == MODE_STRING)
709 format_string += value;
710 format_length -= value;
724 if (t != FMT_ZERO && t != FMT_POSINT)
726 error = nonneg_required;
729 else if (is_input && t == FMT_ZERO)
731 error = posint_required;
743 if (t != FMT_ZERO && t != FMT_POSINT)
745 error = nonneg_required;
753 error = unexpected_element;
758 /* Between a descriptor and what comes next. */
777 error = unexpected_end;
781 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
788 /* Optional comma is a weird between state where we've just finished
789 reading a colon, slash, dollar or P descriptor. */
804 /* Assume that we have another format item. */
811 extension_optional_comma:
812 /* As a GNU extension, permit a missing comma after a string literal. */
830 error = unexpected_end;
834 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
844 /* Something went wrong. If the format we're checking is a string,
845 generate a warning, since the program is correct. If the format
846 is in a FORMAT statement, this messes up parsing, which is an
848 if (mode != MODE_STRING)
849 gfc_error ("%s in format string at %C", error);
852 gfc_warning ("%s in format string at %C", error);
854 /* TODO: More elaborate measures are needed to show where a problem
855 is within a format string that has been calculated. */
865 /* Given an expression node that is a constant string, see if it looks
866 like a format string. */
869 check_format_string (gfc_expr *e, bool is_input)
872 format_string = e->value.character.string;
873 check_format (is_input);
877 /************ Fortran 95 I/O statement matchers *************/
879 /* Match a FORMAT statement. This amounts to actually parsing the
880 format descriptors in order to correctly locate the end of the
884 gfc_match_format (void)
889 if (gfc_current_ns->proc_name
890 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
892 gfc_error ("Format statement in module main block at %C");
896 if (gfc_statement_label == NULL)
898 gfc_error ("Missing format label at %C");
901 gfc_gobble_whitespace ();
906 start = gfc_current_locus;
908 if (check_format (false) == FAILURE)
911 if (gfc_match_eos () != MATCH_YES)
913 gfc_syntax_error (ST_FORMAT);
917 /* The label doesn't get created until after the statement is done
918 being matched, so we have to leave the string for later. */
920 gfc_current_locus = start; /* Back to the beginning */
923 new_st.op = EXEC_NOP;
926 e->expr_type = EXPR_CONSTANT;
927 e->ts.type = BT_CHARACTER;
928 e->ts.kind = gfc_default_character_kind;
930 e->value.character.string = format_string = gfc_getmem (format_length + 1);
931 e->value.character.length = format_length;
932 gfc_statement_label->format = e;
935 check_format (false); /* Guaranteed to succeed */
936 gfc_match_eos (); /* Guaranteed to succeed */
942 /* Match an expression I/O tag of some sort. */
945 match_etag (const io_tag *tag, gfc_expr **v)
950 m = gfc_match (tag->spec, &result);
956 gfc_error ("Duplicate %s specification at %C", tag->name);
957 gfc_free_expr (result);
966 /* Match a variable I/O tag of some sort. */
969 match_vtag (const io_tag *tag, gfc_expr **v)
974 m = gfc_match (tag->spec, &result);
980 gfc_error ("Duplicate %s specification at %C", tag->name);
981 gfc_free_expr (result);
985 if (result->symtree->n.sym->attr.intent == INTENT_IN)
987 gfc_error ("Variable tag cannot be INTENT(IN) at %C");
988 gfc_free_expr (result);
992 if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
994 gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
995 gfc_free_expr (result);
1004 /* Match I/O tags that cause variables to become redefined. */
1007 match_out_tag(const io_tag *tag, gfc_expr **result)
1011 m = match_vtag(tag, result);
1013 gfc_check_do_variable((*result)->symtree);
1019 /* Match a label I/O tag. */
1022 match_ltag (const io_tag *tag, gfc_st_label ** label)
1028 m = gfc_match (tag->spec, label);
1029 if (m == MATCH_YES && old != 0)
1031 gfc_error ("Duplicate %s label specification at %C", tag->name);
1036 && gfc_reference_st_label (*label, ST_LABEL_TARGET) == FAILURE)
1043 /* Do expression resolution and type-checking on an expression tag. */
1046 resolve_tag (const io_tag *tag, gfc_expr *e)
1051 if (gfc_resolve_expr (e) == FAILURE)
1054 if (e->ts.type != tag->type && tag != &tag_format)
1056 gfc_error ("%s tag at %L must be of type %s", tag->name,
1057 &e->where, gfc_basic_typename (tag->type));
1061 if (tag == &tag_format)
1063 if (e->expr_type == EXPR_CONSTANT
1064 && (e->ts.type != BT_CHARACTER
1065 || e->ts.kind != gfc_default_character_kind))
1067 gfc_error ("Constant expression in FORMAT tag at %L must be "
1068 "of type default CHARACTER", &e->where);
1072 /* If e's rank is zero and e is not an element of an array, it should be
1073 of integer or character type. The integer variable should be
1075 if (e->symtree == NULL || e->symtree->n.sym->as == NULL
1076 || e->symtree->n.sym->as->rank == 0)
1078 if (e->ts.type != BT_CHARACTER && e->ts.type != BT_INTEGER)
1080 gfc_error ("%s tag at %L must be of type %s or %s", tag->name,
1081 &e->where, gfc_basic_typename (BT_CHARACTER),
1082 gfc_basic_typename (BT_INTEGER));
1085 else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
1087 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGNED "
1088 "variable in FORMAT tag at %L", &e->where)
1091 if (e->symtree->n.sym->attr.assign != 1)
1093 gfc_error ("Variable '%s' at %L has not been assigned a "
1094 "format label", e->symtree->n.sym->name,
1099 else if (e->ts.type == BT_INTEGER)
1101 gfc_error ("scalar '%s' FORMAT tag at %L is not an ASSIGNED "
1102 "variable", gfc_basic_typename (e->ts.type),
1111 /* if rank is nonzero, we allow the type to be character under
1112 GFC_STD_GNU and other type under GFC_STD_LEGACY. It may be
1113 assigned an Hollerith constant. */
1114 if (e->ts.type == BT_CHARACTER)
1116 if (gfc_notify_std (GFC_STD_GNU, "Extension: Character array "
1117 "in FORMAT tag at %L", &e->where)
1123 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: Non-character "
1124 "in FORMAT tag at %L", &e->where)
1135 gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
1139 if (tag == &tag_iomsg)
1141 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IOMSG tag at %L",
1142 &e->where) == FAILURE)
1146 if (tag == &tag_iostat && e->ts.kind != gfc_default_integer_kind)
1148 if (gfc_notify_std (GFC_STD_GNU, "Fortran 95 requires default "
1149 "INTEGER in IOSTAT tag at %L", &e->where)
1154 if (tag == &tag_size && e->ts.kind != gfc_default_integer_kind)
1156 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1157 "INTEGER in SIZE tag at %L", &e->where)
1162 if (tag == &tag_convert)
1164 if (gfc_notify_std (GFC_STD_GNU, "Extension: CONVERT tag at %L",
1165 &e->where) == FAILURE)
1169 if (tag == &tag_iolength && e->ts.kind != gfc_default_integer_kind)
1171 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1172 "INTEGER in IOLENGTH tag at %L", &e->where)
1182 /* Match a single tag of an OPEN statement. */
1185 match_open_element (gfc_open *open)
1189 m = match_etag (&tag_unit, &open->unit);
1192 m = match_out_tag (&tag_iomsg, &open->iomsg);
1195 m = match_out_tag (&tag_iostat, &open->iostat);
1198 m = match_etag (&tag_file, &open->file);
1201 m = match_etag (&tag_status, &open->status);
1204 m = match_etag (&tag_e_access, &open->access);
1207 m = match_etag (&tag_e_form, &open->form);
1210 m = match_etag (&tag_e_recl, &open->recl);
1213 m = match_etag (&tag_e_blank, &open->blank);
1216 m = match_etag (&tag_e_position, &open->position);
1219 m = match_etag (&tag_e_action, &open->action);
1222 m = match_etag (&tag_e_delim, &open->delim);
1225 m = match_etag (&tag_e_pad, &open->pad);
1228 m = match_ltag (&tag_err, &open->err);
1231 m = match_etag (&tag_convert, &open->convert);
1239 /* Free the gfc_open structure and all the expressions it contains. */
1242 gfc_free_open (gfc_open *open)
1247 gfc_free_expr (open->unit);
1248 gfc_free_expr (open->iomsg);
1249 gfc_free_expr (open->iostat);
1250 gfc_free_expr (open->file);
1251 gfc_free_expr (open->status);
1252 gfc_free_expr (open->access);
1253 gfc_free_expr (open->form);
1254 gfc_free_expr (open->recl);
1255 gfc_free_expr (open->blank);
1256 gfc_free_expr (open->position);
1257 gfc_free_expr (open->action);
1258 gfc_free_expr (open->delim);
1259 gfc_free_expr (open->pad);
1260 gfc_free_expr (open->convert);
1265 /* Resolve everything in a gfc_open structure. */
1268 gfc_resolve_open (gfc_open *open)
1271 RESOLVE_TAG (&tag_unit, open->unit);
1272 RESOLVE_TAG (&tag_iomsg, open->iomsg);
1273 RESOLVE_TAG (&tag_iostat, open->iostat);
1274 RESOLVE_TAG (&tag_file, open->file);
1275 RESOLVE_TAG (&tag_status, open->status);
1276 RESOLVE_TAG (&tag_e_access, open->access);
1277 RESOLVE_TAG (&tag_e_form, open->form);
1278 RESOLVE_TAG (&tag_e_recl, open->recl);
1279 RESOLVE_TAG (&tag_e_blank, open->blank);
1280 RESOLVE_TAG (&tag_e_position, open->position);
1281 RESOLVE_TAG (&tag_e_action, open->action);
1282 RESOLVE_TAG (&tag_e_delim, open->delim);
1283 RESOLVE_TAG (&tag_e_pad, open->pad);
1284 RESOLVE_TAG (&tag_convert, open->convert);
1286 if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1293 /* Check if a given value for a SPECIFIER is either in the list of values
1294 allowed in F95 or F2003, issuing an error message and returning a zero
1295 value if it is not allowed. */
1298 compare_to_allowed_values (const char *specifier, const char *allowed[],
1299 const char *allowed_f2003[],
1300 const char *allowed_gnu[], char *value,
1301 const char *statement, bool warn)
1306 len = strlen (value);
1309 for (len--; len > 0; len--)
1310 if (value[len] != ' ')
1315 for (i = 0; allowed[i]; i++)
1316 if (len == strlen (allowed[i])
1317 && strncasecmp (value, allowed[i], strlen (allowed[i])) == 0)
1320 for (i = 0; allowed_f2003 && allowed_f2003[i]; i++)
1321 if (len == strlen (allowed_f2003[i])
1322 && strncasecmp (value, allowed_f2003[i], strlen (allowed_f2003[i]))
1325 notification n = gfc_notification_std (GFC_STD_F2003);
1327 if (n == WARNING || (warn && n == ERROR))
1329 gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1330 "has value '%s'", specifier, statement,
1337 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: %s specifier in "
1338 "%s statement at %C has value '%s'", specifier,
1339 statement, allowed_f2003[i]);
1347 for (i = 0; allowed_gnu && allowed_gnu[i]; i++)
1348 if (len == strlen (allowed_gnu[i])
1349 && strncasecmp (value, allowed_gnu[i], strlen (allowed_gnu[i])) == 0)
1351 notification n = gfc_notification_std (GFC_STD_GNU);
1353 if (n == WARNING || (warn && n == ERROR))
1355 gfc_warning ("Extension: %s specifier in %s statement at %C "
1356 "has value '%s'", specifier, statement,
1363 gfc_notify_std (GFC_STD_GNU, "Extension: %s specifier in "
1364 "%s statement at %C has value '%s'", specifier,
1365 statement, allowed_gnu[i]);
1375 gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1376 specifier, statement, value);
1381 gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1382 specifier, statement, value);
1388 /* Match an OPEN statement. */
1391 gfc_match_open (void)
1397 m = gfc_match_char ('(');
1401 open = gfc_getmem (sizeof (gfc_open));
1403 m = match_open_element (open);
1405 if (m == MATCH_ERROR)
1409 m = gfc_match_expr (&open->unit);
1412 if (m == MATCH_ERROR)
1418 if (gfc_match_char (')') == MATCH_YES)
1420 if (gfc_match_char (',') != MATCH_YES)
1423 m = match_open_element (open);
1424 if (m == MATCH_ERROR)
1430 if (gfc_match_eos () == MATCH_NO)
1433 if (gfc_pure (NULL))
1435 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1439 warn = (open->err || open->iostat) ? true : false;
1440 /* Checks on the ACCESS specifier. */
1441 if (open->access && open->access->expr_type == EXPR_CONSTANT)
1443 static const char *access_f95[] = { "SEQUENTIAL", "DIRECT", NULL };
1444 static const char *access_f2003[] = { "STREAM", NULL };
1445 static const char *access_gnu[] = { "APPEND", NULL };
1447 if (!compare_to_allowed_values ("ACCESS", access_f95, access_f2003,
1449 open->access->value.character.string,
1454 /* Checks on the ACTION specifier. */
1455 if (open->action && open->action->expr_type == EXPR_CONSTANT)
1457 static const char *action[] = { "READ", "WRITE", "READWRITE", NULL };
1459 if (!compare_to_allowed_values ("ACTION", action, NULL, NULL,
1460 open->action->value.character.string,
1465 /* Checks on the ASYNCHRONOUS specifier. */
1466 /* TODO: code is ready, just needs uncommenting when async I/O support
1468 if (open->asynchronous && open->asynchronous->expr_type == EXPR_CONSTANT)
1470 static const char * asynchronous[] = { "YES", "NO", NULL };
1472 if (!compare_to_allowed_values
1473 ("action", asynchronous, NULL, NULL,
1474 open->asynchronous->value.character.string, "OPEN", warn))
1478 /* Checks on the BLANK specifier. */
1479 if (open->blank && open->blank->expr_type == EXPR_CONSTANT)
1481 static const char *blank[] = { "ZERO", "NULL", NULL };
1483 if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
1484 open->blank->value.character.string,
1489 /* Checks on the DECIMAL specifier. */
1490 /* TODO: uncomment this code when DECIMAL support is added
1491 if (open->decimal && open->decimal->expr_type == EXPR_CONSTANT)
1493 static const char * decimal[] = { "COMMA", "POINT", NULL };
1495 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1496 open->decimal->value.character.string,
1501 /* Checks on the DELIM specifier. */
1502 if (open->delim && open->delim->expr_type == EXPR_CONSTANT)
1504 static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
1506 if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
1507 open->delim->value.character.string,
1512 /* Checks on the ENCODING specifier. */
1513 /* TODO: uncomment this code when ENCODING support is added
1514 if (open->encoding && open->encoding->expr_type == EXPR_CONSTANT)
1516 static const char * encoding[] = { "UTF-8", "DEFAULT", NULL };
1518 if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1519 open->encoding->value.character.string,
1524 /* Checks on the FORM specifier. */
1525 if (open->form && open->form->expr_type == EXPR_CONSTANT)
1527 static const char *form[] = { "FORMATTED", "UNFORMATTED", NULL };
1529 if (!compare_to_allowed_values ("FORM", form, NULL, NULL,
1530 open->form->value.character.string,
1535 /* Checks on the PAD specifier. */
1536 if (open->pad && open->pad->expr_type == EXPR_CONSTANT)
1538 static const char *pad[] = { "YES", "NO", NULL };
1540 if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
1541 open->pad->value.character.string,
1546 /* Checks on the POSITION specifier. */
1547 if (open->position && open->position->expr_type == EXPR_CONSTANT)
1549 static const char *position[] = { "ASIS", "REWIND", "APPEND", NULL };
1551 if (!compare_to_allowed_values ("POSITION", position, NULL, NULL,
1552 open->position->value.character.string,
1557 /* Checks on the ROUND specifier. */
1558 /* TODO: uncomment this code when ROUND support is added
1559 if (open->round && open->round->expr_type == EXPR_CONSTANT)
1561 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1562 "COMPATIBLE", "PROCESSOR_DEFINED", NULL };
1564 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1565 open->round->value.character.string,
1570 /* Checks on the SIGN specifier. */
1571 /* TODO: uncomment this code when SIGN support is added
1572 if (open->sign && open->sign->expr_type == EXPR_CONSTANT)
1574 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1577 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1578 open->sign->value.character.string,
1583 #define warn_or_error(...) \
1586 gfc_warning (__VA_ARGS__); \
1589 gfc_error (__VA_ARGS__); \
1594 /* Checks on the RECL specifier. */
1595 if (open->recl && open->recl->expr_type == EXPR_CONSTANT
1596 && open->recl->ts.type == BT_INTEGER
1597 && mpz_sgn (open->recl->value.integer) != 1)
1599 warn_or_error ("RECL in OPEN statement at %C must be positive");
1602 /* Checks on the STATUS specifier. */
1603 if (open->status && open->status->expr_type == EXPR_CONSTANT)
1605 static const char *status[] = { "OLD", "NEW", "SCRATCH",
1606 "REPLACE", "UNKNOWN", NULL };
1608 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1609 open->status->value.character.string,
1613 /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1614 the FILE= specifier shall appear. */
1615 if (open->file == NULL
1616 && (strncasecmp (open->status->value.character.string, "replace", 7)
1618 || strncasecmp (open->status->value.character.string, "new", 3)
1621 warn_or_error ("The STATUS specified in OPEN statement at %C is "
1622 "'%s' and no FILE specifier is present",
1623 open->status->value.character.string);
1626 /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1627 the FILE= specifier shall not appear. */
1628 if (strncasecmp (open->status->value.character.string, "scratch", 7)
1631 warn_or_error ("The STATUS specified in OPEN statement at %C "
1632 "cannot have the value SCRATCH if a FILE specifier "
1637 /* Things that are not allowed for unformatted I/O. */
1638 if (open->form && open->form->expr_type == EXPR_CONSTANT
1640 /* TODO uncomment this code when F2003 support is finished */
1641 /* || open->decimal || open->encoding || open->round
1643 || open->pad || open->blank)
1644 && strncasecmp (open->form->value.character.string,
1645 "unformatted", 11) == 0)
1647 const char *spec = (open->delim ? "DELIM "
1648 : (open->pad ? "PAD " : open->blank
1651 warn_or_error ("%s specifier at %C not allowed in OPEN statement for "
1652 "unformatted I/O", spec);
1655 if (open->recl && open->access && open->access->expr_type == EXPR_CONSTANT
1656 && strncasecmp (open->access->value.character.string, "stream", 6) == 0)
1658 warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
1663 && open->access && open->access->expr_type == EXPR_CONSTANT
1664 && !(strncasecmp (open->access->value.character.string,
1665 "sequential", 10) == 0
1666 || strncasecmp (open->access->value.character.string,
1668 || strncasecmp (open->access->value.character.string,
1671 warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
1672 "for stream or sequential ACCESS");
1675 #undef warn_or_error
1677 new_st.op = EXEC_OPEN;
1678 new_st.ext.open = open;
1682 gfc_syntax_error (ST_OPEN);
1685 gfc_free_open (open);
1690 /* Free a gfc_close structure an all its expressions. */
1693 gfc_free_close (gfc_close *close)
1698 gfc_free_expr (close->unit);
1699 gfc_free_expr (close->iomsg);
1700 gfc_free_expr (close->iostat);
1701 gfc_free_expr (close->status);
1706 /* Match elements of a CLOSE statement. */
1709 match_close_element (gfc_close *close)
1713 m = match_etag (&tag_unit, &close->unit);
1716 m = match_etag (&tag_status, &close->status);
1719 m = match_out_tag (&tag_iomsg, &close->iomsg);
1722 m = match_out_tag (&tag_iostat, &close->iostat);
1725 m = match_ltag (&tag_err, &close->err);
1733 /* Match a CLOSE statement. */
1736 gfc_match_close (void)
1742 m = gfc_match_char ('(');
1746 close = gfc_getmem (sizeof (gfc_close));
1748 m = match_close_element (close);
1750 if (m == MATCH_ERROR)
1754 m = gfc_match_expr (&close->unit);
1757 if (m == MATCH_ERROR)
1763 if (gfc_match_char (')') == MATCH_YES)
1765 if (gfc_match_char (',') != MATCH_YES)
1768 m = match_close_element (close);
1769 if (m == MATCH_ERROR)
1775 if (gfc_match_eos () == MATCH_NO)
1778 if (gfc_pure (NULL))
1780 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1784 warn = (close->iostat || close->err) ? true : false;
1786 /* Checks on the STATUS specifier. */
1787 if (close->status && close->status->expr_type == EXPR_CONSTANT)
1789 static const char *status[] = { "KEEP", "DELETE", NULL };
1791 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1792 close->status->value.character.string,
1797 new_st.op = EXEC_CLOSE;
1798 new_st.ext.close = close;
1802 gfc_syntax_error (ST_CLOSE);
1805 gfc_free_close (close);
1810 /* Resolve everything in a gfc_close structure. */
1813 gfc_resolve_close (gfc_close *close)
1815 RESOLVE_TAG (&tag_unit, close->unit);
1816 RESOLVE_TAG (&tag_iomsg, close->iomsg);
1817 RESOLVE_TAG (&tag_iostat, close->iostat);
1818 RESOLVE_TAG (&tag_status, close->status);
1820 if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
1827 /* Free a gfc_filepos structure. */
1830 gfc_free_filepos (gfc_filepos *fp)
1832 gfc_free_expr (fp->unit);
1833 gfc_free_expr (fp->iomsg);
1834 gfc_free_expr (fp->iostat);
1839 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
1842 match_file_element (gfc_filepos *fp)
1846 m = match_etag (&tag_unit, &fp->unit);
1849 m = match_out_tag (&tag_iomsg, &fp->iomsg);
1852 m = match_out_tag (&tag_iostat, &fp->iostat);
1855 m = match_ltag (&tag_err, &fp->err);
1863 /* Match the second half of the file-positioning statements, REWIND,
1864 BACKSPACE, ENDFILE, or the FLUSH statement. */
1867 match_filepos (gfc_statement st, gfc_exec_op op)
1872 fp = gfc_getmem (sizeof (gfc_filepos));
1874 if (gfc_match_char ('(') == MATCH_NO)
1876 m = gfc_match_expr (&fp->unit);
1877 if (m == MATCH_ERROR)
1885 m = match_file_element (fp);
1886 if (m == MATCH_ERROR)
1890 m = gfc_match_expr (&fp->unit);
1891 if (m == MATCH_ERROR)
1899 if (gfc_match_char (')') == MATCH_YES)
1901 if (gfc_match_char (',') != MATCH_YES)
1904 m = match_file_element (fp);
1905 if (m == MATCH_ERROR)
1912 if (gfc_match_eos () != MATCH_YES)
1915 if (gfc_pure (NULL))
1917 gfc_error ("%s statement not allowed in PURE procedure at %C",
1918 gfc_ascii_statement (st));
1924 new_st.ext.filepos = fp;
1928 gfc_syntax_error (st);
1931 gfc_free_filepos (fp);
1937 gfc_resolve_filepos (gfc_filepos *fp)
1939 RESOLVE_TAG (&tag_unit, fp->unit);
1940 RESOLVE_TAG (&tag_iostat, fp->iostat);
1941 RESOLVE_TAG (&tag_iomsg, fp->iomsg);
1942 if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
1949 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
1950 and the FLUSH statement. */
1953 gfc_match_endfile (void)
1955 return match_filepos (ST_END_FILE, EXEC_ENDFILE);
1959 gfc_match_backspace (void)
1961 return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
1965 gfc_match_rewind (void)
1967 return match_filepos (ST_REWIND, EXEC_REWIND);
1971 gfc_match_flush (void)
1973 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: FLUSH statement at %C")
1977 return match_filepos (ST_FLUSH, EXEC_FLUSH);
1980 /******************** Data Transfer Statements *********************/
1983 { M_READ, M_WRITE, M_PRINT, M_INQUIRE }
1987 /* Return a default unit number. */
1990 default_unit (io_kind k)
1999 return gfc_int_expr (unit);
2003 /* Match a unit specification for a data transfer statement. */
2006 match_dt_unit (io_kind k, gfc_dt *dt)
2010 if (gfc_match_char ('*') == MATCH_YES)
2012 if (dt->io_unit != NULL)
2015 dt->io_unit = default_unit (k);
2019 if (gfc_match_expr (&e) == MATCH_YES)
2021 if (dt->io_unit != NULL)
2034 gfc_error ("Duplicate UNIT specification at %C");
2039 /* Match a format specification. */
2042 match_dt_format (gfc_dt *dt)
2046 gfc_st_label *label;
2048 where = gfc_current_locus;
2050 if (gfc_match_char ('*') == MATCH_YES)
2052 if (dt->format_expr != NULL || dt->format_label != NULL)
2055 dt->format_label = &format_asterisk;
2059 if (gfc_match_st_label (&label) == MATCH_YES)
2061 if (dt->format_expr != NULL || dt->format_label != NULL)
2063 gfc_free_st_label (label);
2067 if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
2070 dt->format_label = label;
2074 if (gfc_match_expr (&e) == MATCH_YES)
2076 if (dt->format_expr != NULL || dt->format_label != NULL)
2081 dt->format_expr = e;
2085 gfc_current_locus = where; /* The only case where we have to restore */
2090 gfc_error ("Duplicate format specification at %C");
2095 /* Traverse a namelist that is part of a READ statement to make sure
2096 that none of the variables in the namelist are INTENT(IN). Returns
2097 nonzero if we find such a variable. */
2100 check_namelist (gfc_symbol *sym)
2104 for (p = sym->namelist; p; p = p->next)
2105 if (p->sym->attr.intent == INTENT_IN)
2107 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2108 p->sym->name, sym->name);
2116 /* Match a single data transfer element. */
2119 match_dt_element (io_kind k, gfc_dt *dt)
2121 char name[GFC_MAX_SYMBOL_LEN + 1];
2125 if (gfc_match (" unit =") == MATCH_YES)
2127 m = match_dt_unit (k, dt);
2132 if (gfc_match (" fmt =") == MATCH_YES)
2134 m = match_dt_format (dt);
2139 if (gfc_match (" nml = %n", name) == MATCH_YES)
2141 if (dt->namelist != NULL)
2143 gfc_error ("Duplicate NML specification at %C");
2147 if (gfc_find_symbol (name, NULL, 1, &sym))
2150 if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
2152 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2153 sym != NULL ? sym->name : name);
2158 if (k == M_READ && check_namelist (sym))
2164 m = match_etag (&tag_rec, &dt->rec);
2167 m = match_etag (&tag_spos, &dt->rec);
2170 m = match_out_tag (&tag_iomsg, &dt->iomsg);
2173 m = match_out_tag (&tag_iostat, &dt->iostat);
2176 m = match_ltag (&tag_err, &dt->err);
2178 dt->err_where = gfc_current_locus;
2181 m = match_etag (&tag_advance, &dt->advance);
2184 m = match_out_tag (&tag_size, &dt->size);
2188 m = match_ltag (&tag_end, &dt->end);
2193 gfc_error ("END tag at %C not allowed in output statement");
2196 dt->end_where = gfc_current_locus;
2201 m = match_ltag (&tag_eor, &dt->eor);
2203 dt->eor_where = gfc_current_locus;
2211 /* Free a data transfer structure and everything below it. */
2214 gfc_free_dt (gfc_dt *dt)
2219 gfc_free_expr (dt->io_unit);
2220 gfc_free_expr (dt->format_expr);
2221 gfc_free_expr (dt->rec);
2222 gfc_free_expr (dt->advance);
2223 gfc_free_expr (dt->iomsg);
2224 gfc_free_expr (dt->iostat);
2225 gfc_free_expr (dt->size);
2230 /* Resolve everything in a gfc_dt structure. */
2233 gfc_resolve_dt (gfc_dt *dt)
2237 RESOLVE_TAG (&tag_format, dt->format_expr);
2238 RESOLVE_TAG (&tag_rec, dt->rec);
2239 RESOLVE_TAG (&tag_spos, dt->rec);
2240 RESOLVE_TAG (&tag_advance, dt->advance);
2241 RESOLVE_TAG (&tag_iomsg, dt->iomsg);
2242 RESOLVE_TAG (&tag_iostat, dt->iostat);
2243 RESOLVE_TAG (&tag_size, dt->size);
2246 if (gfc_resolve_expr (e) == SUCCESS
2247 && (e->ts.type != BT_INTEGER
2248 && (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_VARIABLE)))
2250 gfc_error ("UNIT specification at %L must be an INTEGER expression "
2251 "or a CHARACTER variable", &e->where);
2255 if (e->ts.type == BT_CHARACTER)
2257 if (gfc_has_vector_index (e))
2259 gfc_error ("Internal unit with vector subscript at %L", &e->where);
2264 if (e->rank && e->ts.type != BT_CHARACTER)
2266 gfc_error ("External IO UNIT cannot be an array at %L", &e->where);
2272 if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
2274 if (dt->err->defined == ST_LABEL_UNKNOWN)
2276 gfc_error ("ERR tag label %d at %L not defined",
2277 dt->err->value, &dt->err_where);
2284 if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
2286 if (dt->end->defined == ST_LABEL_UNKNOWN)
2288 gfc_error ("END tag label %d at %L not defined",
2289 dt->end->value, &dt->end_where);
2296 if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
2298 if (dt->eor->defined == ST_LABEL_UNKNOWN)
2300 gfc_error ("EOR tag label %d at %L not defined",
2301 dt->eor->value, &dt->eor_where);
2306 /* Check the format label actually exists. */
2307 if (dt->format_label && dt->format_label != &format_asterisk
2308 && dt->format_label->defined == ST_LABEL_UNKNOWN)
2310 gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
2311 &dt->format_label->where);
2318 /* Given an io_kind, return its name. */
2321 io_kind_name (io_kind k)
2340 gfc_internal_error ("io_kind_name(): bad I/O-kind");
2347 /* Match an IO iteration statement of the form:
2349 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2351 which is equivalent to a single IO element. This function is
2352 mutually recursive with match_io_element(). */
2354 static match match_io_element (io_kind, gfc_code **);
2357 match_io_iterator (io_kind k, gfc_code **result)
2359 gfc_code *head, *tail, *new;
2367 old_loc = gfc_current_locus;
2369 if (gfc_match_char ('(') != MATCH_YES)
2372 m = match_io_element (k, &head);
2375 if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
2381 /* Can't be anything but an IO iterator. Build a list. */
2382 iter = gfc_get_iterator ();
2386 m = gfc_match_iterator (iter, 0);
2387 if (m == MATCH_ERROR)
2391 gfc_check_do_variable (iter->var->symtree);
2395 m = match_io_element (k, &new);
2396 if (m == MATCH_ERROR)
2405 tail = gfc_append_code (tail, new);
2407 if (gfc_match_char (',') != MATCH_YES)
2416 if (gfc_match_char (')') != MATCH_YES)
2419 new = gfc_get_code ();
2421 new->ext.iterator = iter;
2423 new->block = gfc_get_code ();
2424 new->block->op = EXEC_DO;
2425 new->block->next = head;
2431 gfc_error ("Syntax error in I/O iterator at %C");
2435 gfc_free_iterator (iter, 1);
2436 gfc_free_statements (head);
2437 gfc_current_locus = old_loc;
2442 /* Match a single element of an IO list, which is either a single
2443 expression or an IO Iterator. */
2446 match_io_element (io_kind k, gfc_code **cpp)
2454 m = match_io_iterator (k, cpp);
2460 m = gfc_match_variable (&expr, 0);
2462 gfc_error ("Expected variable in READ statement at %C");
2466 m = gfc_match_expr (&expr);
2468 gfc_error ("Expected expression in %s statement at %C",
2476 if (expr->symtree->n.sym->attr.intent == INTENT_IN)
2478 gfc_error ("Variable '%s' in input list at %C cannot be "
2479 "INTENT(IN)", expr->symtree->n.sym->name);
2484 && gfc_impure_variable (expr->symtree->n.sym)
2485 && current_dt->io_unit->ts.type == BT_CHARACTER)
2487 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2488 expr->symtree->n.sym->name);
2492 if (gfc_check_do_variable (expr->symtree))
2498 if (current_dt->io_unit->ts.type == BT_CHARACTER
2500 && current_dt->io_unit->expr_type == EXPR_VARIABLE
2501 && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
2503 gfc_error ("Cannot write to internal file unit '%s' at %C "
2504 "inside a PURE procedure",
2505 current_dt->io_unit->symtree->n.sym->name);
2517 gfc_free_expr (expr);
2521 cp = gfc_get_code ();
2522 cp->op = EXEC_TRANSFER;
2530 /* Match an I/O list, building gfc_code structures as we go. */
2533 match_io_list (io_kind k, gfc_code **head_p)
2535 gfc_code *head, *tail, *new;
2538 *head_p = head = tail = NULL;
2539 if (gfc_match_eos () == MATCH_YES)
2544 m = match_io_element (k, &new);
2545 if (m == MATCH_ERROR)
2550 tail = gfc_append_code (tail, new);
2554 if (gfc_match_eos () == MATCH_YES)
2556 if (gfc_match_char (',') != MATCH_YES)
2564 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2567 gfc_free_statements (head);
2572 /* Attach the data transfer end node. */
2575 terminate_io (gfc_code *io_code)
2579 if (io_code == NULL)
2580 io_code = new_st.block;
2582 c = gfc_get_code ();
2583 c->op = EXEC_DT_END;
2585 /* Point to structure that is already there */
2586 c->ext.dt = new_st.ext.dt;
2587 gfc_append_code (io_code, c);
2591 /* Check the constraints for a data transfer statement. The majority of the
2592 constraints appearing in 9.4 of the standard appear here. Some are handled
2593 in resolve_tag and others in gfc_resolve_dt. */
2596 check_io_constraints (io_kind k, gfc_dt *dt, gfc_code *io_code,
2599 #define io_constraint(condition,msg,arg)\
2602 gfc_error(msg,arg);\
2608 gfc_symbol *sym = NULL;
2613 if (expr && expr->expr_type == EXPR_VARIABLE
2614 && expr->ts.type == BT_CHARACTER)
2616 sym = expr->symtree->n.sym;
2618 io_constraint (k == M_WRITE && sym->attr.intent == INTENT_IN,
2619 "Internal file at %L must not be INTENT(IN)",
2622 io_constraint (gfc_has_vector_index (dt->io_unit),
2623 "Internal file incompatible with vector subscript at %L",
2626 io_constraint (dt->rec != NULL,
2627 "REC tag at %L is incompatible with internal file",
2630 if (dt->namelist != NULL)
2632 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Internal file "
2633 "at %L with namelist", &expr->where)
2638 io_constraint (dt->advance != NULL,
2639 "ADVANCE tag at %L is incompatible with internal file",
2640 &dt->advance->where);
2643 if (expr && expr->ts.type != BT_CHARACTER)
2646 io_constraint (gfc_pure (NULL) && (k == M_READ || k == M_WRITE),
2647 "IO UNIT in %s statement at %C must be "
2648 "an internal file in a PURE procedure",
2655 io_constraint (dt->end, "END tag not allowed with output at %L",
2658 io_constraint (dt->eor, "EOR tag not allowed with output at %L",
2661 io_constraint (k != M_READ && dt->size,
2662 "SIZE=specifier not allowed with output at %L",
2667 io_constraint (dt->size && dt->advance == NULL,
2668 "SIZE tag at %L requires an ADVANCE tag",
2671 io_constraint (dt->eor && dt->advance == NULL,
2672 "EOR tag at %L requires an ADVANCE tag",
2680 io_constraint (io_code && dt->namelist,
2681 "NAMELIST cannot be followed by IO-list at %L",
2684 io_constraint (dt->format_expr,
2685 "IO spec-list cannot contain both NAMELIST group name "
2686 "and format specification at %L.",
2687 &dt->format_expr->where);
2689 io_constraint (dt->format_label,
2690 "IO spec-list cannot contain both NAMELIST group name "
2691 "and format label at %L", spec_end);
2693 io_constraint (dt->rec,
2694 "NAMELIST IO is not allowed with a REC=specifier "
2695 "at %L.", &dt->rec->where);
2697 io_constraint (dt->advance,
2698 "NAMELIST IO is not allowed with a ADVANCE=specifier "
2699 "at %L.", &dt->advance->where);
2704 io_constraint (dt->end,
2705 "An END tag is not allowed with a "
2706 "REC=specifier at %L.", &dt->end_where);
2709 io_constraint (dt->format_label == &format_asterisk,
2710 "FMT=* is not allowed with a REC=specifier "
2711 "at %L.", spec_end);
2716 int not_yes, not_no;
2719 io_constraint (dt->format_label == &format_asterisk,
2720 "List directed format(*) is not allowed with a "
2721 "ADVANCE=specifier at %L.", &expr->where);
2723 io_constraint (dt->format_expr == NULL && dt->format_label == NULL
2724 && dt->namelist == NULL,
2725 "the ADVANCE=specifier at %L must appear with an "
2726 "explicit format expression", &expr->where);
2728 if (expr->expr_type == EXPR_CONSTANT && expr->ts.type == BT_CHARACTER)
2730 const char * advance = expr->value.character.string;
2731 not_no = strcasecmp (advance, "no") != 0;
2732 not_yes = strcasecmp (advance, "yes") != 0;
2740 io_constraint (not_no && not_yes,
2741 "ADVANCE=specifier at %L must have value = "
2742 "YES or NO.", &expr->where);
2744 io_constraint (dt->size && not_no && k == M_READ,
2745 "SIZE tag at %L requires an ADVANCE = 'NO'",
2748 io_constraint (dt->eor && not_no && k == M_READ,
2749 "EOR tag at %L requires an ADVANCE = 'NO'",
2753 expr = dt->format_expr;
2754 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
2755 check_format_string (expr, k == M_READ);
2759 #undef io_constraint
2762 /* Match a READ, WRITE or PRINT statement. */
2765 match_io (io_kind k)
2767 char name[GFC_MAX_SYMBOL_LEN + 1];
2776 where = gfc_current_locus;
2778 current_dt = dt = gfc_getmem (sizeof (gfc_dt));
2779 m = gfc_match_char ('(');
2782 where = gfc_current_locus;
2785 else if (k == M_PRINT)
2787 /* Treat the non-standard case of PRINT namelist. */
2788 if ((gfc_current_form == FORM_FIXED || gfc_peek_char () == ' ')
2789 && gfc_match_name (name) == MATCH_YES)
2791 gfc_find_symbol (name, NULL, 1, &sym);
2792 if (sym && sym->attr.flavor == FL_NAMELIST)
2794 if (gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
2795 "%C is an extension") == FAILURE)
2801 dt->io_unit = default_unit (k);
2806 gfc_current_locus = where;
2810 if (gfc_current_form == FORM_FREE)
2812 c = gfc_peek_char();
2813 if (c != ' ' && c != '*' && c != '\'' && c != '"')
2820 m = match_dt_format (dt);
2821 if (m == MATCH_ERROR)
2827 dt->io_unit = default_unit (k);
2832 /* Before issuing an error for a malformed 'print (1,*)' type of
2833 error, check for a default-char-expr of the form ('(I0)'). */
2834 if (k == M_PRINT && m == MATCH_YES)
2836 /* Reset current locus to get the initial '(' in an expression. */
2837 gfc_current_locus = where;
2838 dt->format_expr = NULL;
2839 m = match_dt_format (dt);
2841 if (m == MATCH_ERROR)
2843 if (m == MATCH_NO || dt->format_expr == NULL)
2847 dt->io_unit = default_unit (k);
2852 /* Match a control list */
2853 if (match_dt_element (k, dt) == MATCH_YES)
2855 if (match_dt_unit (k, dt) != MATCH_YES)
2858 if (gfc_match_char (')') == MATCH_YES)
2860 if (gfc_match_char (',') != MATCH_YES)
2863 m = match_dt_element (k, dt);
2866 if (m == MATCH_ERROR)
2869 m = match_dt_format (dt);
2872 if (m == MATCH_ERROR)
2875 where = gfc_current_locus;
2877 m = gfc_match_name (name);
2880 gfc_find_symbol (name, NULL, 1, &sym);
2881 if (sym && sym->attr.flavor == FL_NAMELIST)
2884 if (k == M_READ && check_namelist (sym))
2893 gfc_current_locus = where;
2895 goto loop; /* No matches, try regular elements */
2898 if (gfc_match_char (')') == MATCH_YES)
2900 if (gfc_match_char (',') != MATCH_YES)
2906 m = match_dt_element (k, dt);
2909 if (m == MATCH_ERROR)
2912 if (gfc_match_char (')') == MATCH_YES)
2914 if (gfc_match_char (',') != MATCH_YES)
2920 /* Used in check_io_constraints, where no locus is available. */
2921 spec_end = gfc_current_locus;
2923 /* Optional leading comma (non-standard). */
2925 && gfc_match_char (',') == MATCH_YES
2927 && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before output "
2928 "item list at %C is an extension") == FAILURE)
2932 if (gfc_match_eos () != MATCH_YES)
2934 if (comma_flag && gfc_match_char (',') != MATCH_YES)
2936 gfc_error ("Expected comma in I/O list at %C");
2941 m = match_io_list (k, &io_code);
2942 if (m == MATCH_ERROR)
2948 /* A full IO statement has been matched. Check the constraints. spec_end is
2949 supplied for cases where no locus is supplied. */
2950 m = check_io_constraints (k, dt, io_code, &spec_end);
2952 if (m == MATCH_ERROR)
2955 new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
2957 new_st.block = gfc_get_code ();
2958 new_st.block->op = new_st.op;
2959 new_st.block->next = io_code;
2961 terminate_io (io_code);
2966 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2976 gfc_match_read (void)
2978 return match_io (M_READ);
2982 gfc_match_write (void)
2984 return match_io (M_WRITE);
2988 gfc_match_print (void)
2992 m = match_io (M_PRINT);
2996 if (gfc_pure (NULL))
2998 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
3006 /* Free a gfc_inquire structure. */
3009 gfc_free_inquire (gfc_inquire *inquire)
3012 if (inquire == NULL)
3015 gfc_free_expr (inquire->unit);
3016 gfc_free_expr (inquire->file);
3017 gfc_free_expr (inquire->iomsg);
3018 gfc_free_expr (inquire->iostat);
3019 gfc_free_expr (inquire->exist);
3020 gfc_free_expr (inquire->opened);
3021 gfc_free_expr (inquire->number);
3022 gfc_free_expr (inquire->named);
3023 gfc_free_expr (inquire->name);
3024 gfc_free_expr (inquire->access);
3025 gfc_free_expr (inquire->sequential);
3026 gfc_free_expr (inquire->direct);
3027 gfc_free_expr (inquire->form);
3028 gfc_free_expr (inquire->formatted);
3029 gfc_free_expr (inquire->unformatted);
3030 gfc_free_expr (inquire->recl);
3031 gfc_free_expr (inquire->nextrec);
3032 gfc_free_expr (inquire->blank);
3033 gfc_free_expr (inquire->position);
3034 gfc_free_expr (inquire->action);
3035 gfc_free_expr (inquire->read);
3036 gfc_free_expr (inquire->write);
3037 gfc_free_expr (inquire->readwrite);
3038 gfc_free_expr (inquire->delim);
3039 gfc_free_expr (inquire->pad);
3040 gfc_free_expr (inquire->iolength);
3041 gfc_free_expr (inquire->convert);
3042 gfc_free_expr (inquire->strm_pos);
3047 /* Match an element of an INQUIRE statement. */
3049 #define RETM if (m != MATCH_NO) return m;
3052 match_inquire_element (gfc_inquire *inquire)
3056 m = match_etag (&tag_unit, &inquire->unit);
3057 RETM m = match_etag (&tag_file, &inquire->file);
3058 RETM m = match_ltag (&tag_err, &inquire->err);
3059 RETM m = match_out_tag (&tag_iomsg, &inquire->iomsg);
3060 RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
3061 RETM m = match_vtag (&tag_exist, &inquire->exist);
3062 RETM m = match_vtag (&tag_opened, &inquire->opened);
3063 RETM m = match_vtag (&tag_named, &inquire->named);
3064 RETM m = match_vtag (&tag_name, &inquire->name);
3065 RETM m = match_out_tag (&tag_number, &inquire->number);
3066 RETM m = match_vtag (&tag_s_access, &inquire->access);
3067 RETM m = match_vtag (&tag_sequential, &inquire->sequential);
3068 RETM m = match_vtag (&tag_direct, &inquire->direct);
3069 RETM m = match_vtag (&tag_s_form, &inquire->form);
3070 RETM m = match_vtag (&tag_formatted, &inquire->formatted);
3071 RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
3072 RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
3073 RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
3074 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
3075 RETM m = match_vtag (&tag_s_position, &inquire->position);
3076 RETM m = match_vtag (&tag_s_action, &inquire->action);
3077 RETM m = match_vtag (&tag_read, &inquire->read);
3078 RETM m = match_vtag (&tag_write, &inquire->write);
3079 RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
3080 RETM m = match_vtag (&tag_s_delim, &inquire->delim);
3081 RETM m = match_vtag (&tag_s_pad, &inquire->pad);
3082 RETM m = match_vtag (&tag_iolength, &inquire->iolength);
3083 RETM m = match_vtag (&tag_convert, &inquire->convert);
3084 RETM m = match_out_tag (&tag_strm_out, &inquire->strm_pos);
3085 RETM return MATCH_NO;
3092 gfc_match_inquire (void)
3094 gfc_inquire *inquire;
3099 m = gfc_match_char ('(');
3103 inquire = gfc_getmem (sizeof (gfc_inquire));
3105 loc = gfc_current_locus;
3107 m = match_inquire_element (inquire);
3108 if (m == MATCH_ERROR)
3112 m = gfc_match_expr (&inquire->unit);
3113 if (m == MATCH_ERROR)
3119 /* See if we have the IOLENGTH form of the inquire statement. */
3120 if (inquire->iolength != NULL)
3122 if (gfc_match_char (')') != MATCH_YES)
3125 m = match_io_list (M_INQUIRE, &code);
3126 if (m == MATCH_ERROR)
3131 new_st.op = EXEC_IOLENGTH;
3132 new_st.expr = inquire->iolength;
3133 new_st.ext.inquire = inquire;
3135 if (gfc_pure (NULL))
3137 gfc_free_statements (code);
3138 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3142 new_st.block = gfc_get_code ();
3143 new_st.block->op = EXEC_IOLENGTH;
3144 terminate_io (code);
3145 new_st.block->next = code;
3149 /* At this point, we have the non-IOLENGTH inquire statement. */
3152 if (gfc_match_char (')') == MATCH_YES)
3154 if (gfc_match_char (',') != MATCH_YES)
3157 m = match_inquire_element (inquire);
3158 if (m == MATCH_ERROR)
3163 if (inquire->iolength != NULL)
3165 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3170 if (gfc_match_eos () != MATCH_YES)
3173 if (inquire->unit != NULL && inquire->file != NULL)
3175 gfc_error ("INQUIRE statement at %L cannot contain both FILE and "
3176 "UNIT specifiers", &loc);
3180 if (inquire->unit == NULL && inquire->file == NULL)
3182 gfc_error ("INQUIRE statement at %L requires either FILE or "
3183 "UNIT specifier", &loc);
3187 if (gfc_pure (NULL))
3189 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3193 new_st.op = EXEC_INQUIRE;
3194 new_st.ext.inquire = inquire;
3198 gfc_syntax_error (ST_INQUIRE);
3201 gfc_free_inquire (inquire);
3206 /* Resolve everything in a gfc_inquire structure. */
3209 gfc_resolve_inquire (gfc_inquire *inquire)
3211 RESOLVE_TAG (&tag_unit, inquire->unit);
3212 RESOLVE_TAG (&tag_file, inquire->file);
3213 RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
3214 RESOLVE_TAG (&tag_iostat, inquire->iostat);
3215 RESOLVE_TAG (&tag_exist, inquire->exist);
3216 RESOLVE_TAG (&tag_opened, inquire->opened);
3217 RESOLVE_TAG (&tag_number, inquire->number);
3218 RESOLVE_TAG (&tag_named, inquire->named);
3219 RESOLVE_TAG (&tag_name, inquire->name);
3220 RESOLVE_TAG (&tag_s_access, inquire->access);
3221 RESOLVE_TAG (&tag_sequential, inquire->sequential);
3222 RESOLVE_TAG (&tag_direct, inquire->direct);
3223 RESOLVE_TAG (&tag_s_form, inquire->form);
3224 RESOLVE_TAG (&tag_formatted, inquire->formatted);
3225 RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
3226 RESOLVE_TAG (&tag_s_recl, inquire->recl);
3227 RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
3228 RESOLVE_TAG (&tag_s_blank, inquire->blank);
3229 RESOLVE_TAG (&tag_s_position, inquire->position);
3230 RESOLVE_TAG (&tag_s_action, inquire->action);
3231 RESOLVE_TAG (&tag_read, inquire->read);
3232 RESOLVE_TAG (&tag_write, inquire->write);
3233 RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
3234 RESOLVE_TAG (&tag_s_delim, inquire->delim);
3235 RESOLVE_TAG (&tag_s_pad, inquire->pad);
3236 RESOLVE_TAG (&tag_iolength, inquire->iolength);
3237 RESOLVE_TAG (&tag_convert, inquire->convert);
3238 RESOLVE_TAG (&tag_strm_out, inquire->strm_pos);
3240 if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)