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
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 (void)
185 while (gfc_is_whitespace (c));
189 static int value = 0;
191 /* Simple lexical analyzer for getting the next token in a FORMAT
202 if (saved_token != FMT_NONE)
205 saved_token = FMT_NONE;
209 c = next_char_not_space ();
217 c = next_char_not_space ();
228 c = next_char_not_space ();
230 value = 10 * value + c - '0';
239 token = FMT_SIGNED_INT;
258 c = next_char_not_space ();
261 value = 10 * value + c - '0';
269 token = zflag ? FMT_ZERO : FMT_POSINT;
293 c = next_char_not_space ();
294 if (c != 'L' && c != 'R')
313 c = next_char_not_space ();
314 if (c != 'P' && c != 'S')
321 c = next_char_not_space ();
322 if (c == 'N' || c == 'Z')
383 c = next_char_not_space ();
384 if (c == 'N' || c == 'S')
427 /* Check a format statement. The format string, either from a FORMAT
428 statement or a constant in an I/O statement has already been parsed
429 by itself, and we are checking it for validity. The dual origin
430 means that the warning message is a little less than great. */
433 check_format (bool is_input)
435 const char *posint_required = _("Positive width required");
436 const char *nonneg_required = _("Nonnegative width required");
437 const char *unexpected_element = _("Unexpected element");
438 const char *unexpected_end = _("Unexpected end of format string");
447 saved_token = FMT_NONE;
455 error = _("Missing leading left parenthesis");
461 goto finished; /* Empty format is legal */
465 /* 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 gfc_error ("%s in format string at %C", error);
846 /* TODO: More elaborate measures are needed to show where a problem
847 is within a format string that has been calculated. */
855 /* Given an expression node that is a constant string, see if it looks
856 like a format string. */
859 check_format_string (gfc_expr *e, bool is_input)
862 format_string = e->value.character.string;
863 return check_format (is_input);
867 /************ Fortran 95 I/O statement matchers *************/
869 /* Match a FORMAT statement. This amounts to actually parsing the
870 format descriptors in order to correctly locate the end of the
874 gfc_match_format (void)
879 if (gfc_current_ns->proc_name
880 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
882 gfc_error ("Format statement in module main block at %C");
886 if (gfc_statement_label == NULL)
888 gfc_error ("Missing format label at %C");
891 gfc_gobble_whitespace ();
896 start = gfc_current_locus;
898 if (check_format (false) == FAILURE)
901 if (gfc_match_eos () != MATCH_YES)
903 gfc_syntax_error (ST_FORMAT);
907 /* The label doesn't get created until after the statement is done
908 being matched, so we have to leave the string for later. */
910 gfc_current_locus = start; /* Back to the beginning */
913 new_st.op = EXEC_NOP;
916 e->expr_type = EXPR_CONSTANT;
917 e->ts.type = BT_CHARACTER;
918 e->ts.kind = gfc_default_character_kind;
920 e->value.character.string = format_string = gfc_getmem (format_length + 1);
921 e->value.character.length = format_length;
922 gfc_statement_label->format = e;
925 check_format (false); /* Guaranteed to succeed */
926 gfc_match_eos (); /* Guaranteed to succeed */
932 /* Match an expression I/O tag of some sort. */
935 match_etag (const io_tag *tag, gfc_expr **v)
940 m = gfc_match (tag->spec, &result);
946 gfc_error ("Duplicate %s specification at %C", tag->name);
947 gfc_free_expr (result);
956 /* Match a variable I/O tag of some sort. */
959 match_vtag (const io_tag *tag, gfc_expr **v)
964 m = gfc_match (tag->spec, &result);
970 gfc_error ("Duplicate %s specification at %C", tag->name);
971 gfc_free_expr (result);
975 if (result->symtree->n.sym->attr.intent == INTENT_IN)
977 gfc_error ("Variable tag cannot be INTENT(IN) at %C");
978 gfc_free_expr (result);
982 if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
984 gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
985 gfc_free_expr (result);
994 /* Match I/O tags that cause variables to become redefined. */
997 match_out_tag(const io_tag *tag, gfc_expr **result)
1001 m = match_vtag(tag, result);
1003 gfc_check_do_variable((*result)->symtree);
1009 /* Match a label I/O tag. */
1012 match_ltag (const io_tag *tag, gfc_st_label ** label)
1018 m = gfc_match (tag->spec, label);
1019 if (m == MATCH_YES && old != 0)
1021 gfc_error ("Duplicate %s label specification at %C", tag->name);
1026 && gfc_reference_st_label (*label, ST_LABEL_TARGET) == FAILURE)
1033 /* Do expression resolution and type-checking on an expression tag. */
1036 resolve_tag (const io_tag *tag, gfc_expr *e)
1041 if (gfc_resolve_expr (e) == FAILURE)
1044 if (e->ts.type != tag->type && tag != &tag_format)
1046 gfc_error ("%s tag at %L must be of type %s", tag->name,
1047 &e->where, gfc_basic_typename (tag->type));
1051 if (tag == &tag_format)
1053 if (e->expr_type == EXPR_CONSTANT
1054 && (e->ts.type != BT_CHARACTER
1055 || e->ts.kind != gfc_default_character_kind))
1057 gfc_error ("Constant expression in FORMAT tag at %L must be "
1058 "of type default CHARACTER", &e->where);
1062 /* If e's rank is zero and e is not an element of an array, it should be
1063 of integer or character type. The integer variable should be
1065 if (e->symtree == NULL || e->symtree->n.sym->as == NULL
1066 || e->symtree->n.sym->as->rank == 0)
1068 if (e->ts.type != BT_CHARACTER && e->ts.type != BT_INTEGER)
1070 gfc_error ("%s tag at %L must be of type %s or %s", tag->name,
1071 &e->where, gfc_basic_typename (BT_CHARACTER),
1072 gfc_basic_typename (BT_INTEGER));
1075 else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
1077 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGNED "
1078 "variable in FORMAT tag at %L", &e->where)
1081 if (e->symtree->n.sym->attr.assign != 1)
1083 gfc_error ("Variable '%s' at %L has not been assigned a "
1084 "format label", e->symtree->n.sym->name,
1089 else if (e->ts.type == BT_INTEGER)
1091 gfc_error ("scalar '%s' FORMAT tag at %L is not an ASSIGNED "
1092 "variable", gfc_basic_typename (e->ts.type),
1101 /* if rank is nonzero, we allow the type to be character under
1102 GFC_STD_GNU and other type under GFC_STD_LEGACY. It may be
1103 assigned an Hollerith constant. */
1104 if (e->ts.type == BT_CHARACTER)
1106 if (gfc_notify_std (GFC_STD_GNU, "Extension: Character array "
1107 "in FORMAT tag at %L", &e->where)
1113 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: Non-character "
1114 "in FORMAT tag at %L", &e->where)
1125 gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
1129 if (tag == &tag_iomsg)
1131 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IOMSG tag at %L",
1132 &e->where) == FAILURE)
1136 if (tag == &tag_iostat && e->ts.kind != gfc_default_integer_kind)
1138 if (gfc_notify_std (GFC_STD_GNU, "Fortran 95 requires default "
1139 "INTEGER in IOSTAT tag at %L", &e->where)
1144 if (tag == &tag_size && e->ts.kind != gfc_default_integer_kind)
1146 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1147 "INTEGER in SIZE tag at %L", &e->where)
1152 if (tag == &tag_convert)
1154 if (gfc_notify_std (GFC_STD_GNU, "Extension: CONVERT tag at %L",
1155 &e->where) == FAILURE)
1159 if (tag == &tag_iolength && e->ts.kind != gfc_default_integer_kind)
1161 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1162 "INTEGER in IOLENGTH tag at %L", &e->where)
1172 /* Match a single tag of an OPEN statement. */
1175 match_open_element (gfc_open *open)
1179 m = match_etag (&tag_unit, &open->unit);
1182 m = match_out_tag (&tag_iomsg, &open->iomsg);
1185 m = match_out_tag (&tag_iostat, &open->iostat);
1188 m = match_etag (&tag_file, &open->file);
1191 m = match_etag (&tag_status, &open->status);
1194 m = match_etag (&tag_e_access, &open->access);
1197 m = match_etag (&tag_e_form, &open->form);
1200 m = match_etag (&tag_e_recl, &open->recl);
1203 m = match_etag (&tag_e_blank, &open->blank);
1206 m = match_etag (&tag_e_position, &open->position);
1209 m = match_etag (&tag_e_action, &open->action);
1212 m = match_etag (&tag_e_delim, &open->delim);
1215 m = match_etag (&tag_e_pad, &open->pad);
1218 m = match_ltag (&tag_err, &open->err);
1221 m = match_etag (&tag_convert, &open->convert);
1229 /* Free the gfc_open structure and all the expressions it contains. */
1232 gfc_free_open (gfc_open *open)
1237 gfc_free_expr (open->unit);
1238 gfc_free_expr (open->iomsg);
1239 gfc_free_expr (open->iostat);
1240 gfc_free_expr (open->file);
1241 gfc_free_expr (open->status);
1242 gfc_free_expr (open->access);
1243 gfc_free_expr (open->form);
1244 gfc_free_expr (open->recl);
1245 gfc_free_expr (open->blank);
1246 gfc_free_expr (open->position);
1247 gfc_free_expr (open->action);
1248 gfc_free_expr (open->delim);
1249 gfc_free_expr (open->pad);
1250 gfc_free_expr (open->convert);
1255 /* Resolve everything in a gfc_open structure. */
1258 gfc_resolve_open (gfc_open *open)
1261 RESOLVE_TAG (&tag_unit, open->unit);
1262 RESOLVE_TAG (&tag_iomsg, open->iomsg);
1263 RESOLVE_TAG (&tag_iostat, open->iostat);
1264 RESOLVE_TAG (&tag_file, open->file);
1265 RESOLVE_TAG (&tag_status, open->status);
1266 RESOLVE_TAG (&tag_e_access, open->access);
1267 RESOLVE_TAG (&tag_e_form, open->form);
1268 RESOLVE_TAG (&tag_e_recl, open->recl);
1269 RESOLVE_TAG (&tag_e_blank, open->blank);
1270 RESOLVE_TAG (&tag_e_position, open->position);
1271 RESOLVE_TAG (&tag_e_action, open->action);
1272 RESOLVE_TAG (&tag_e_delim, open->delim);
1273 RESOLVE_TAG (&tag_e_pad, open->pad);
1274 RESOLVE_TAG (&tag_convert, open->convert);
1276 if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1283 /* Check if a given value for a SPECIFIER is either in the list of values
1284 allowed in F95 or F2003, issuing an error message and returning a zero
1285 value if it is not allowed. */
1288 compare_to_allowed_values (const char *specifier, const char *allowed[],
1289 const char *allowed_f2003[],
1290 const char *allowed_gnu[], char *value,
1291 const char *statement, bool warn)
1296 len = strlen (value);
1299 for (len--; len > 0; len--)
1300 if (value[len] != ' ')
1305 for (i = 0; allowed[i]; i++)
1306 if (len == strlen (allowed[i])
1307 && strncasecmp (value, allowed[i], strlen (allowed[i])) == 0)
1310 for (i = 0; allowed_f2003 && allowed_f2003[i]; i++)
1311 if (len == strlen (allowed_f2003[i])
1312 && strncasecmp (value, allowed_f2003[i], strlen (allowed_f2003[i]))
1315 notification n = gfc_notification_std (GFC_STD_F2003);
1317 if (n == WARNING || (warn && n == ERROR))
1319 gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1320 "has value '%s'", specifier, statement,
1327 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: %s specifier in "
1328 "%s statement at %C has value '%s'", specifier,
1329 statement, allowed_f2003[i]);
1337 for (i = 0; allowed_gnu && allowed_gnu[i]; i++)
1338 if (len == strlen (allowed_gnu[i])
1339 && strncasecmp (value, allowed_gnu[i], strlen (allowed_gnu[i])) == 0)
1341 notification n = gfc_notification_std (GFC_STD_GNU);
1343 if (n == WARNING || (warn && n == ERROR))
1345 gfc_warning ("Extension: %s specifier in %s statement at %C "
1346 "has value '%s'", specifier, statement,
1353 gfc_notify_std (GFC_STD_GNU, "Extension: %s specifier in "
1354 "%s statement at %C has value '%s'", specifier,
1355 statement, allowed_gnu[i]);
1365 gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1366 specifier, statement, value);
1371 gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1372 specifier, statement, value);
1378 /* Match an OPEN statement. */
1381 gfc_match_open (void)
1387 m = gfc_match_char ('(');
1391 open = gfc_getmem (sizeof (gfc_open));
1393 m = match_open_element (open);
1395 if (m == MATCH_ERROR)
1399 m = gfc_match_expr (&open->unit);
1402 if (m == MATCH_ERROR)
1408 if (gfc_match_char (')') == MATCH_YES)
1410 if (gfc_match_char (',') != MATCH_YES)
1413 m = match_open_element (open);
1414 if (m == MATCH_ERROR)
1420 if (gfc_match_eos () == MATCH_NO)
1423 if (gfc_pure (NULL))
1425 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1429 warn = (open->err || open->iostat) ? true : false;
1430 /* Checks on the ACCESS specifier. */
1431 if (open->access && open->access->expr_type == EXPR_CONSTANT)
1433 static const char *access_f95[] = { "SEQUENTIAL", "DIRECT", NULL };
1434 static const char *access_f2003[] = { "STREAM", NULL };
1435 static const char *access_gnu[] = { "APPEND", NULL };
1437 if (!compare_to_allowed_values ("ACCESS", access_f95, access_f2003,
1439 open->access->value.character.string,
1444 /* Checks on the ACTION specifier. */
1445 if (open->action && open->action->expr_type == EXPR_CONSTANT)
1447 static const char *action[] = { "READ", "WRITE", "READWRITE", NULL };
1449 if (!compare_to_allowed_values ("ACTION", action, NULL, NULL,
1450 open->action->value.character.string,
1455 /* Checks on the ASYNCHRONOUS specifier. */
1456 /* TODO: code is ready, just needs uncommenting when async I/O support
1458 if (open->asynchronous && open->asynchronous->expr_type == EXPR_CONSTANT)
1460 static const char * asynchronous[] = { "YES", "NO", NULL };
1462 if (!compare_to_allowed_values
1463 ("action", asynchronous, NULL, NULL,
1464 open->asynchronous->value.character.string, "OPEN", warn))
1468 /* Checks on the BLANK specifier. */
1469 if (open->blank && open->blank->expr_type == EXPR_CONSTANT)
1471 static const char *blank[] = { "ZERO", "NULL", NULL };
1473 if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
1474 open->blank->value.character.string,
1479 /* Checks on the DECIMAL specifier. */
1480 /* TODO: uncomment this code when DECIMAL support is added
1481 if (open->decimal && open->decimal->expr_type == EXPR_CONSTANT)
1483 static const char * decimal[] = { "COMMA", "POINT", NULL };
1485 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1486 open->decimal->value.character.string,
1491 /* Checks on the DELIM specifier. */
1492 if (open->delim && open->delim->expr_type == EXPR_CONSTANT)
1494 static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
1496 if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
1497 open->delim->value.character.string,
1502 /* Checks on the ENCODING specifier. */
1503 /* TODO: uncomment this code when ENCODING support is added
1504 if (open->encoding && open->encoding->expr_type == EXPR_CONSTANT)
1506 static const char * encoding[] = { "UTF-8", "DEFAULT", NULL };
1508 if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1509 open->encoding->value.character.string,
1514 /* Checks on the FORM specifier. */
1515 if (open->form && open->form->expr_type == EXPR_CONSTANT)
1517 static const char *form[] = { "FORMATTED", "UNFORMATTED", NULL };
1519 if (!compare_to_allowed_values ("FORM", form, NULL, NULL,
1520 open->form->value.character.string,
1525 /* Checks on the PAD specifier. */
1526 if (open->pad && open->pad->expr_type == EXPR_CONSTANT)
1528 static const char *pad[] = { "YES", "NO", NULL };
1530 if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
1531 open->pad->value.character.string,
1536 /* Checks on the POSITION specifier. */
1537 if (open->position && open->position->expr_type == EXPR_CONSTANT)
1539 static const char *position[] = { "ASIS", "REWIND", "APPEND", NULL };
1541 if (!compare_to_allowed_values ("POSITION", position, NULL, NULL,
1542 open->position->value.character.string,
1547 /* Checks on the ROUND specifier. */
1548 /* TODO: uncomment this code when ROUND support is added
1549 if (open->round && open->round->expr_type == EXPR_CONSTANT)
1551 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1552 "COMPATIBLE", "PROCESSOR_DEFINED", NULL };
1554 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1555 open->round->value.character.string,
1560 /* Checks on the SIGN specifier. */
1561 /* TODO: uncomment this code when SIGN support is added
1562 if (open->sign && open->sign->expr_type == EXPR_CONSTANT)
1564 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1567 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1568 open->sign->value.character.string,
1573 #define warn_or_error(...) \
1576 gfc_warning (__VA_ARGS__); \
1579 gfc_error (__VA_ARGS__); \
1584 /* Checks on the RECL specifier. */
1585 if (open->recl && open->recl->expr_type == EXPR_CONSTANT
1586 && open->recl->ts.type == BT_INTEGER
1587 && mpz_sgn (open->recl->value.integer) != 1)
1589 warn_or_error ("RECL in OPEN statement at %C must be positive");
1592 /* Checks on the STATUS specifier. */
1593 if (open->status && open->status->expr_type == EXPR_CONSTANT)
1595 static const char *status[] = { "OLD", "NEW", "SCRATCH",
1596 "REPLACE", "UNKNOWN", NULL };
1598 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1599 open->status->value.character.string,
1603 /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1604 the FILE= specifier shall appear. */
1605 if (open->file == NULL
1606 && (strncasecmp (open->status->value.character.string, "replace", 7)
1608 || strncasecmp (open->status->value.character.string, "new", 3)
1611 warn_or_error ("The STATUS specified in OPEN statement at %C is "
1612 "'%s' and no FILE specifier is present",
1613 open->status->value.character.string);
1616 /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1617 the FILE= specifier shall not appear. */
1618 if (strncasecmp (open->status->value.character.string, "scratch", 7)
1621 warn_or_error ("The STATUS specified in OPEN statement at %C "
1622 "cannot have the value SCRATCH if a FILE specifier "
1627 /* Things that are not allowed for unformatted I/O. */
1628 if (open->form && open->form->expr_type == EXPR_CONSTANT
1630 /* TODO uncomment this code when F2003 support is finished */
1631 /* || open->decimal || open->encoding || open->round
1633 || open->pad || open->blank)
1634 && strncasecmp (open->form->value.character.string,
1635 "unformatted", 11) == 0)
1637 const char *spec = (open->delim ? "DELIM "
1638 : (open->pad ? "PAD " : open->blank
1641 warn_or_error ("%s specifier at %C not allowed in OPEN statement for "
1642 "unformatted I/O", spec);
1645 if (open->recl && open->access && open->access->expr_type == EXPR_CONSTANT
1646 && strncasecmp (open->access->value.character.string, "stream", 6) == 0)
1648 warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
1653 && open->access && open->access->expr_type == EXPR_CONSTANT
1654 && !(strncasecmp (open->access->value.character.string,
1655 "sequential", 10) == 0
1656 || strncasecmp (open->access->value.character.string,
1658 || strncasecmp (open->access->value.character.string,
1661 warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
1662 "for stream or sequential ACCESS");
1665 #undef warn_or_error
1667 new_st.op = EXEC_OPEN;
1668 new_st.ext.open = open;
1672 gfc_syntax_error (ST_OPEN);
1675 gfc_free_open (open);
1680 /* Free a gfc_close structure an all its expressions. */
1683 gfc_free_close (gfc_close *close)
1688 gfc_free_expr (close->unit);
1689 gfc_free_expr (close->iomsg);
1690 gfc_free_expr (close->iostat);
1691 gfc_free_expr (close->status);
1696 /* Match elements of a CLOSE statement. */
1699 match_close_element (gfc_close *close)
1703 m = match_etag (&tag_unit, &close->unit);
1706 m = match_etag (&tag_status, &close->status);
1709 m = match_out_tag (&tag_iomsg, &close->iomsg);
1712 m = match_out_tag (&tag_iostat, &close->iostat);
1715 m = match_ltag (&tag_err, &close->err);
1723 /* Match a CLOSE statement. */
1726 gfc_match_close (void)
1732 m = gfc_match_char ('(');
1736 close = gfc_getmem (sizeof (gfc_close));
1738 m = match_close_element (close);
1740 if (m == MATCH_ERROR)
1744 m = gfc_match_expr (&close->unit);
1747 if (m == MATCH_ERROR)
1753 if (gfc_match_char (')') == MATCH_YES)
1755 if (gfc_match_char (',') != MATCH_YES)
1758 m = match_close_element (close);
1759 if (m == MATCH_ERROR)
1765 if (gfc_match_eos () == MATCH_NO)
1768 if (gfc_pure (NULL))
1770 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1774 warn = (close->iostat || close->err) ? true : false;
1776 /* Checks on the STATUS specifier. */
1777 if (close->status && close->status->expr_type == EXPR_CONSTANT)
1779 static const char *status[] = { "KEEP", "DELETE", NULL };
1781 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1782 close->status->value.character.string,
1787 new_st.op = EXEC_CLOSE;
1788 new_st.ext.close = close;
1792 gfc_syntax_error (ST_CLOSE);
1795 gfc_free_close (close);
1800 /* Resolve everything in a gfc_close structure. */
1803 gfc_resolve_close (gfc_close *close)
1805 RESOLVE_TAG (&tag_unit, close->unit);
1806 RESOLVE_TAG (&tag_iomsg, close->iomsg);
1807 RESOLVE_TAG (&tag_iostat, close->iostat);
1808 RESOLVE_TAG (&tag_status, close->status);
1810 if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
1817 /* Free a gfc_filepos structure. */
1820 gfc_free_filepos (gfc_filepos *fp)
1822 gfc_free_expr (fp->unit);
1823 gfc_free_expr (fp->iomsg);
1824 gfc_free_expr (fp->iostat);
1829 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
1832 match_file_element (gfc_filepos *fp)
1836 m = match_etag (&tag_unit, &fp->unit);
1839 m = match_out_tag (&tag_iomsg, &fp->iomsg);
1842 m = match_out_tag (&tag_iostat, &fp->iostat);
1845 m = match_ltag (&tag_err, &fp->err);
1853 /* Match the second half of the file-positioning statements, REWIND,
1854 BACKSPACE, ENDFILE, or the FLUSH statement. */
1857 match_filepos (gfc_statement st, gfc_exec_op op)
1862 fp = gfc_getmem (sizeof (gfc_filepos));
1864 if (gfc_match_char ('(') == MATCH_NO)
1866 m = gfc_match_expr (&fp->unit);
1867 if (m == MATCH_ERROR)
1875 m = match_file_element (fp);
1876 if (m == MATCH_ERROR)
1880 m = gfc_match_expr (&fp->unit);
1881 if (m == MATCH_ERROR)
1889 if (gfc_match_char (')') == MATCH_YES)
1891 if (gfc_match_char (',') != MATCH_YES)
1894 m = match_file_element (fp);
1895 if (m == MATCH_ERROR)
1902 if (gfc_match_eos () != MATCH_YES)
1905 if (gfc_pure (NULL))
1907 gfc_error ("%s statement not allowed in PURE procedure at %C",
1908 gfc_ascii_statement (st));
1914 new_st.ext.filepos = fp;
1918 gfc_syntax_error (st);
1921 gfc_free_filepos (fp);
1927 gfc_resolve_filepos (gfc_filepos *fp)
1929 RESOLVE_TAG (&tag_unit, fp->unit);
1930 RESOLVE_TAG (&tag_iostat, fp->iostat);
1931 RESOLVE_TAG (&tag_iomsg, fp->iomsg);
1932 if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
1939 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
1940 and the FLUSH statement. */
1943 gfc_match_endfile (void)
1945 return match_filepos (ST_END_FILE, EXEC_ENDFILE);
1949 gfc_match_backspace (void)
1951 return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
1955 gfc_match_rewind (void)
1957 return match_filepos (ST_REWIND, EXEC_REWIND);
1961 gfc_match_flush (void)
1963 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: FLUSH statement at %C")
1967 return match_filepos (ST_FLUSH, EXEC_FLUSH);
1970 /******************** Data Transfer Statements *********************/
1973 { M_READ, M_WRITE, M_PRINT, M_INQUIRE }
1977 /* Return a default unit number. */
1980 default_unit (io_kind k)
1989 return gfc_int_expr (unit);
1993 /* Match a unit specification for a data transfer statement. */
1996 match_dt_unit (io_kind k, gfc_dt *dt)
2000 if (gfc_match_char ('*') == MATCH_YES)
2002 if (dt->io_unit != NULL)
2005 dt->io_unit = default_unit (k);
2009 if (gfc_match_expr (&e) == MATCH_YES)
2011 if (dt->io_unit != NULL)
2024 gfc_error ("Duplicate UNIT specification at %C");
2029 /* Match a format specification. */
2032 match_dt_format (gfc_dt *dt)
2036 gfc_st_label *label;
2038 where = gfc_current_locus;
2040 if (gfc_match_char ('*') == MATCH_YES)
2042 if (dt->format_expr != NULL || dt->format_label != NULL)
2045 dt->format_label = &format_asterisk;
2049 if (gfc_match_st_label (&label) == MATCH_YES)
2051 if (dt->format_expr != NULL || dt->format_label != NULL)
2053 gfc_free_st_label (label);
2057 if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
2060 dt->format_label = label;
2064 if (gfc_match_expr (&e) == MATCH_YES)
2066 if (dt->format_expr != NULL || dt->format_label != NULL)
2071 dt->format_expr = e;
2075 gfc_current_locus = where; /* The only case where we have to restore */
2080 gfc_error ("Duplicate format specification at %C");
2085 /* Traverse a namelist that is part of a READ statement to make sure
2086 that none of the variables in the namelist are INTENT(IN). Returns
2087 nonzero if we find such a variable. */
2090 check_namelist (gfc_symbol *sym)
2094 for (p = sym->namelist; p; p = p->next)
2095 if (p->sym->attr.intent == INTENT_IN)
2097 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2098 p->sym->name, sym->name);
2106 /* Match a single data transfer element. */
2109 match_dt_element (io_kind k, gfc_dt *dt)
2111 char name[GFC_MAX_SYMBOL_LEN + 1];
2115 if (gfc_match (" unit =") == MATCH_YES)
2117 m = match_dt_unit (k, dt);
2122 if (gfc_match (" fmt =") == MATCH_YES)
2124 m = match_dt_format (dt);
2129 if (gfc_match (" nml = %n", name) == MATCH_YES)
2131 if (dt->namelist != NULL)
2133 gfc_error ("Duplicate NML specification at %C");
2137 if (gfc_find_symbol (name, NULL, 1, &sym))
2140 if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
2142 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2143 sym != NULL ? sym->name : name);
2148 if (k == M_READ && check_namelist (sym))
2154 m = match_etag (&tag_rec, &dt->rec);
2157 m = match_etag (&tag_spos, &dt->rec);
2160 m = match_out_tag (&tag_iomsg, &dt->iomsg);
2163 m = match_out_tag (&tag_iostat, &dt->iostat);
2166 m = match_ltag (&tag_err, &dt->err);
2168 dt->err_where = gfc_current_locus;
2171 m = match_etag (&tag_advance, &dt->advance);
2174 m = match_out_tag (&tag_size, &dt->size);
2178 m = match_ltag (&tag_end, &dt->end);
2183 gfc_error ("END tag at %C not allowed in output statement");
2186 dt->end_where = gfc_current_locus;
2191 m = match_ltag (&tag_eor, &dt->eor);
2193 dt->eor_where = gfc_current_locus;
2201 /* Free a data transfer structure and everything below it. */
2204 gfc_free_dt (gfc_dt *dt)
2209 gfc_free_expr (dt->io_unit);
2210 gfc_free_expr (dt->format_expr);
2211 gfc_free_expr (dt->rec);
2212 gfc_free_expr (dt->advance);
2213 gfc_free_expr (dt->iomsg);
2214 gfc_free_expr (dt->iostat);
2215 gfc_free_expr (dt->size);
2220 /* Resolve everything in a gfc_dt structure. */
2223 gfc_resolve_dt (gfc_dt *dt)
2227 RESOLVE_TAG (&tag_format, dt->format_expr);
2228 RESOLVE_TAG (&tag_rec, dt->rec);
2229 RESOLVE_TAG (&tag_spos, dt->rec);
2230 RESOLVE_TAG (&tag_advance, dt->advance);
2231 RESOLVE_TAG (&tag_iomsg, dt->iomsg);
2232 RESOLVE_TAG (&tag_iostat, dt->iostat);
2233 RESOLVE_TAG (&tag_size, dt->size);
2236 if (gfc_resolve_expr (e) == SUCCESS
2237 && (e->ts.type != BT_INTEGER
2238 && (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_VARIABLE)))
2240 gfc_error ("UNIT specification at %L must be an INTEGER expression "
2241 "or a CHARACTER variable", &e->where);
2245 if (e->ts.type == BT_CHARACTER)
2247 if (gfc_has_vector_index (e))
2249 gfc_error ("Internal unit with vector subscript at %L", &e->where);
2254 if (e->rank && e->ts.type != BT_CHARACTER)
2256 gfc_error ("External IO UNIT cannot be an array at %L", &e->where);
2262 if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
2264 if (dt->err->defined == ST_LABEL_UNKNOWN)
2266 gfc_error ("ERR tag label %d at %L not defined",
2267 dt->err->value, &dt->err_where);
2274 if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
2276 if (dt->end->defined == ST_LABEL_UNKNOWN)
2278 gfc_error ("END tag label %d at %L not defined",
2279 dt->end->value, &dt->end_where);
2286 if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
2288 if (dt->eor->defined == ST_LABEL_UNKNOWN)
2290 gfc_error ("EOR tag label %d at %L not defined",
2291 dt->eor->value, &dt->eor_where);
2296 /* Check the format label actually exists. */
2297 if (dt->format_label && dt->format_label != &format_asterisk
2298 && dt->format_label->defined == ST_LABEL_UNKNOWN)
2300 gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
2301 &dt->format_label->where);
2308 /* Given an io_kind, return its name. */
2311 io_kind_name (io_kind k)
2330 gfc_internal_error ("io_kind_name(): bad I/O-kind");
2337 /* Match an IO iteration statement of the form:
2339 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2341 which is equivalent to a single IO element. This function is
2342 mutually recursive with match_io_element(). */
2344 static match match_io_element (io_kind, gfc_code **);
2347 match_io_iterator (io_kind k, gfc_code **result)
2349 gfc_code *head, *tail, *new;
2357 old_loc = gfc_current_locus;
2359 if (gfc_match_char ('(') != MATCH_YES)
2362 m = match_io_element (k, &head);
2365 if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
2371 /* Can't be anything but an IO iterator. Build a list. */
2372 iter = gfc_get_iterator ();
2376 m = gfc_match_iterator (iter, 0);
2377 if (m == MATCH_ERROR)
2381 gfc_check_do_variable (iter->var->symtree);
2385 m = match_io_element (k, &new);
2386 if (m == MATCH_ERROR)
2395 tail = gfc_append_code (tail, new);
2397 if (gfc_match_char (',') != MATCH_YES)
2406 if (gfc_match_char (')') != MATCH_YES)
2409 new = gfc_get_code ();
2411 new->ext.iterator = iter;
2413 new->block = gfc_get_code ();
2414 new->block->op = EXEC_DO;
2415 new->block->next = head;
2421 gfc_error ("Syntax error in I/O iterator at %C");
2425 gfc_free_iterator (iter, 1);
2426 gfc_free_statements (head);
2427 gfc_current_locus = old_loc;
2432 /* Match a single element of an IO list, which is either a single
2433 expression or an IO Iterator. */
2436 match_io_element (io_kind k, gfc_code **cpp)
2444 m = match_io_iterator (k, cpp);
2450 m = gfc_match_variable (&expr, 0);
2452 gfc_error ("Expected variable in READ statement at %C");
2456 m = gfc_match_expr (&expr);
2458 gfc_error ("Expected expression in %s statement at %C",
2466 if (expr->symtree->n.sym->attr.intent == INTENT_IN)
2468 gfc_error ("Variable '%s' in input list at %C cannot be "
2469 "INTENT(IN)", expr->symtree->n.sym->name);
2474 && gfc_impure_variable (expr->symtree->n.sym)
2475 && current_dt->io_unit->ts.type == BT_CHARACTER)
2477 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2478 expr->symtree->n.sym->name);
2482 if (gfc_check_do_variable (expr->symtree))
2488 if (current_dt->io_unit->ts.type == BT_CHARACTER
2490 && current_dt->io_unit->expr_type == EXPR_VARIABLE
2491 && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
2493 gfc_error ("Cannot write to internal file unit '%s' at %C "
2494 "inside a PURE procedure",
2495 current_dt->io_unit->symtree->n.sym->name);
2507 gfc_free_expr (expr);
2511 cp = gfc_get_code ();
2512 cp->op = EXEC_TRANSFER;
2520 /* Match an I/O list, building gfc_code structures as we go. */
2523 match_io_list (io_kind k, gfc_code **head_p)
2525 gfc_code *head, *tail, *new;
2528 *head_p = head = tail = NULL;
2529 if (gfc_match_eos () == MATCH_YES)
2534 m = match_io_element (k, &new);
2535 if (m == MATCH_ERROR)
2540 tail = gfc_append_code (tail, new);
2544 if (gfc_match_eos () == MATCH_YES)
2546 if (gfc_match_char (',') != MATCH_YES)
2554 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2557 gfc_free_statements (head);
2562 /* Attach the data transfer end node. */
2565 terminate_io (gfc_code *io_code)
2569 if (io_code == NULL)
2570 io_code = new_st.block;
2572 c = gfc_get_code ();
2573 c->op = EXEC_DT_END;
2575 /* Point to structure that is already there */
2576 c->ext.dt = new_st.ext.dt;
2577 gfc_append_code (io_code, c);
2581 /* Check the constraints for a data transfer statement. The majority of the
2582 constraints appearing in 9.4 of the standard appear here. Some are handled
2583 in resolve_tag and others in gfc_resolve_dt. */
2586 check_io_constraints (io_kind k, gfc_dt *dt, gfc_code *io_code,
2589 #define io_constraint(condition,msg,arg)\
2592 gfc_error(msg,arg);\
2598 gfc_symbol *sym = NULL;
2603 if (expr && expr->expr_type == EXPR_VARIABLE
2604 && expr->ts.type == BT_CHARACTER)
2606 sym = expr->symtree->n.sym;
2608 io_constraint (k == M_WRITE && sym->attr.intent == INTENT_IN,
2609 "Internal file at %L must not be INTENT(IN)",
2612 io_constraint (gfc_has_vector_index (dt->io_unit),
2613 "Internal file incompatible with vector subscript at %L",
2616 io_constraint (dt->rec != NULL,
2617 "REC tag at %L is incompatible with internal file",
2620 if (dt->namelist != NULL)
2622 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Internal file "
2623 "at %L with namelist", &expr->where)
2628 io_constraint (dt->advance != NULL,
2629 "ADVANCE tag at %L is incompatible with internal file",
2630 &dt->advance->where);
2633 if (expr && expr->ts.type != BT_CHARACTER)
2636 io_constraint (gfc_pure (NULL) && (k == M_READ || k == M_WRITE),
2637 "IO UNIT in %s statement at %C must be "
2638 "an internal file in a PURE procedure",
2645 io_constraint (dt->end, "END tag not allowed with output at %L",
2648 io_constraint (dt->eor, "EOR tag not allowed with output at %L",
2651 io_constraint (k != M_READ && dt->size,
2652 "SIZE=specifier not allowed with output at %L",
2657 io_constraint (dt->size && dt->advance == NULL,
2658 "SIZE tag at %L requires an ADVANCE tag",
2661 io_constraint (dt->eor && dt->advance == NULL,
2662 "EOR tag at %L requires an ADVANCE tag",
2670 io_constraint (io_code && dt->namelist,
2671 "NAMELIST cannot be followed by IO-list at %L",
2674 io_constraint (dt->format_expr,
2675 "IO spec-list cannot contain both NAMELIST group name "
2676 "and format specification at %L.",
2677 &dt->format_expr->where);
2679 io_constraint (dt->format_label,
2680 "IO spec-list cannot contain both NAMELIST group name "
2681 "and format label at %L", spec_end);
2683 io_constraint (dt->rec,
2684 "NAMELIST IO is not allowed with a REC=specifier "
2685 "at %L.", &dt->rec->where);
2687 io_constraint (dt->advance,
2688 "NAMELIST IO is not allowed with a ADVANCE=specifier "
2689 "at %L.", &dt->advance->where);
2694 io_constraint (dt->end,
2695 "An END tag is not allowed with a "
2696 "REC=specifier at %L.", &dt->end_where);
2699 io_constraint (dt->format_label == &format_asterisk,
2700 "FMT=* is not allowed with a REC=specifier "
2701 "at %L.", spec_end);
2706 int not_yes, not_no;
2709 io_constraint (dt->format_label == &format_asterisk,
2710 "List directed format(*) is not allowed with a "
2711 "ADVANCE=specifier at %L.", &expr->where);
2713 io_constraint (dt->format_expr == NULL && dt->format_label == NULL
2714 && dt->namelist == NULL,
2715 "the ADVANCE=specifier at %L must appear with an "
2716 "explicit format expression", &expr->where);
2718 if (expr->expr_type == EXPR_CONSTANT && expr->ts.type == BT_CHARACTER)
2720 const char * advance = expr->value.character.string;
2721 not_no = strcasecmp (advance, "no") != 0;
2722 not_yes = strcasecmp (advance, "yes") != 0;
2730 io_constraint (not_no && not_yes,
2731 "ADVANCE=specifier at %L must have value = "
2732 "YES or NO.", &expr->where);
2734 io_constraint (dt->size && not_no && k == M_READ,
2735 "SIZE tag at %L requires an ADVANCE = 'NO'",
2738 io_constraint (dt->eor && not_no && k == M_READ,
2739 "EOR tag at %L requires an ADVANCE = 'NO'",
2743 expr = dt->format_expr;
2744 if (expr != NULL && expr->expr_type == EXPR_CONSTANT
2745 && check_format_string (expr, k == M_READ) == FAILURE)
2750 #undef io_constraint
2753 /* Match a READ, WRITE or PRINT statement. */
2756 match_io (io_kind k)
2758 char name[GFC_MAX_SYMBOL_LEN + 1];
2767 where = gfc_current_locus;
2769 current_dt = dt = gfc_getmem (sizeof (gfc_dt));
2770 m = gfc_match_char ('(');
2773 where = gfc_current_locus;
2776 else if (k == M_PRINT)
2778 /* Treat the non-standard case of PRINT namelist. */
2779 if ((gfc_current_form == FORM_FIXED || gfc_peek_char () == ' ')
2780 && gfc_match_name (name) == MATCH_YES)
2782 gfc_find_symbol (name, NULL, 1, &sym);
2783 if (sym && sym->attr.flavor == FL_NAMELIST)
2785 if (gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
2786 "%C is an extension") == FAILURE)
2792 dt->io_unit = default_unit (k);
2797 gfc_current_locus = where;
2801 if (gfc_current_form == FORM_FREE)
2803 c = gfc_peek_char();
2804 if (c != ' ' && c != '*' && c != '\'' && c != '"')
2811 m = match_dt_format (dt);
2812 if (m == MATCH_ERROR)
2818 dt->io_unit = default_unit (k);
2823 /* Before issuing an error for a malformed 'print (1,*)' type of
2824 error, check for a default-char-expr of the form ('(I0)'). */
2825 if (k == M_PRINT && m == MATCH_YES)
2827 /* Reset current locus to get the initial '(' in an expression. */
2828 gfc_current_locus = where;
2829 dt->format_expr = NULL;
2830 m = match_dt_format (dt);
2832 if (m == MATCH_ERROR)
2834 if (m == MATCH_NO || dt->format_expr == NULL)
2838 dt->io_unit = default_unit (k);
2843 /* Match a control list */
2844 if (match_dt_element (k, dt) == MATCH_YES)
2846 if (match_dt_unit (k, dt) != MATCH_YES)
2849 if (gfc_match_char (')') == MATCH_YES)
2851 if (gfc_match_char (',') != MATCH_YES)
2854 m = match_dt_element (k, dt);
2857 if (m == MATCH_ERROR)
2860 m = match_dt_format (dt);
2863 if (m == MATCH_ERROR)
2866 where = gfc_current_locus;
2868 m = gfc_match_name (name);
2871 gfc_find_symbol (name, NULL, 1, &sym);
2872 if (sym && sym->attr.flavor == FL_NAMELIST)
2875 if (k == M_READ && check_namelist (sym))
2884 gfc_current_locus = where;
2886 goto loop; /* No matches, try regular elements */
2889 if (gfc_match_char (')') == MATCH_YES)
2891 if (gfc_match_char (',') != MATCH_YES)
2897 m = match_dt_element (k, dt);
2900 if (m == MATCH_ERROR)
2903 if (gfc_match_char (')') == MATCH_YES)
2905 if (gfc_match_char (',') != MATCH_YES)
2911 /* Used in check_io_constraints, where no locus is available. */
2912 spec_end = gfc_current_locus;
2914 /* Optional leading comma (non-standard). */
2916 && gfc_match_char (',') == MATCH_YES
2918 && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before output "
2919 "item list at %C is an extension") == FAILURE)
2923 if (gfc_match_eos () != MATCH_YES)
2925 if (comma_flag && gfc_match_char (',') != MATCH_YES)
2927 gfc_error ("Expected comma in I/O list at %C");
2932 m = match_io_list (k, &io_code);
2933 if (m == MATCH_ERROR)
2939 /* A full IO statement has been matched. Check the constraints. spec_end is
2940 supplied for cases where no locus is supplied. */
2941 m = check_io_constraints (k, dt, io_code, &spec_end);
2943 if (m == MATCH_ERROR)
2946 new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
2948 new_st.block = gfc_get_code ();
2949 new_st.block->op = new_st.op;
2950 new_st.block->next = io_code;
2952 terminate_io (io_code);
2957 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2967 gfc_match_read (void)
2969 return match_io (M_READ);
2973 gfc_match_write (void)
2975 return match_io (M_WRITE);
2979 gfc_match_print (void)
2983 m = match_io (M_PRINT);
2987 if (gfc_pure (NULL))
2989 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
2997 /* Free a gfc_inquire structure. */
3000 gfc_free_inquire (gfc_inquire *inquire)
3003 if (inquire == NULL)
3006 gfc_free_expr (inquire->unit);
3007 gfc_free_expr (inquire->file);
3008 gfc_free_expr (inquire->iomsg);
3009 gfc_free_expr (inquire->iostat);
3010 gfc_free_expr (inquire->exist);
3011 gfc_free_expr (inquire->opened);
3012 gfc_free_expr (inquire->number);
3013 gfc_free_expr (inquire->named);
3014 gfc_free_expr (inquire->name);
3015 gfc_free_expr (inquire->access);
3016 gfc_free_expr (inquire->sequential);
3017 gfc_free_expr (inquire->direct);
3018 gfc_free_expr (inquire->form);
3019 gfc_free_expr (inquire->formatted);
3020 gfc_free_expr (inquire->unformatted);
3021 gfc_free_expr (inquire->recl);
3022 gfc_free_expr (inquire->nextrec);
3023 gfc_free_expr (inquire->blank);
3024 gfc_free_expr (inquire->position);
3025 gfc_free_expr (inquire->action);
3026 gfc_free_expr (inquire->read);
3027 gfc_free_expr (inquire->write);
3028 gfc_free_expr (inquire->readwrite);
3029 gfc_free_expr (inquire->delim);
3030 gfc_free_expr (inquire->pad);
3031 gfc_free_expr (inquire->iolength);
3032 gfc_free_expr (inquire->convert);
3033 gfc_free_expr (inquire->strm_pos);
3038 /* Match an element of an INQUIRE statement. */
3040 #define RETM if (m != MATCH_NO) return m;
3043 match_inquire_element (gfc_inquire *inquire)
3047 m = match_etag (&tag_unit, &inquire->unit);
3048 RETM m = match_etag (&tag_file, &inquire->file);
3049 RETM m = match_ltag (&tag_err, &inquire->err);
3050 RETM m = match_out_tag (&tag_iomsg, &inquire->iomsg);
3051 RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
3052 RETM m = match_vtag (&tag_exist, &inquire->exist);
3053 RETM m = match_vtag (&tag_opened, &inquire->opened);
3054 RETM m = match_vtag (&tag_named, &inquire->named);
3055 RETM m = match_vtag (&tag_name, &inquire->name);
3056 RETM m = match_out_tag (&tag_number, &inquire->number);
3057 RETM m = match_vtag (&tag_s_access, &inquire->access);
3058 RETM m = match_vtag (&tag_sequential, &inquire->sequential);
3059 RETM m = match_vtag (&tag_direct, &inquire->direct);
3060 RETM m = match_vtag (&tag_s_form, &inquire->form);
3061 RETM m = match_vtag (&tag_formatted, &inquire->formatted);
3062 RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
3063 RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
3064 RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
3065 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
3066 RETM m = match_vtag (&tag_s_position, &inquire->position);
3067 RETM m = match_vtag (&tag_s_action, &inquire->action);
3068 RETM m = match_vtag (&tag_read, &inquire->read);
3069 RETM m = match_vtag (&tag_write, &inquire->write);
3070 RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
3071 RETM m = match_vtag (&tag_s_delim, &inquire->delim);
3072 RETM m = match_vtag (&tag_s_pad, &inquire->pad);
3073 RETM m = match_vtag (&tag_iolength, &inquire->iolength);
3074 RETM m = match_vtag (&tag_convert, &inquire->convert);
3075 RETM m = match_out_tag (&tag_strm_out, &inquire->strm_pos);
3076 RETM return MATCH_NO;
3083 gfc_match_inquire (void)
3085 gfc_inquire *inquire;
3090 m = gfc_match_char ('(');
3094 inquire = gfc_getmem (sizeof (gfc_inquire));
3096 loc = gfc_current_locus;
3098 m = match_inquire_element (inquire);
3099 if (m == MATCH_ERROR)
3103 m = gfc_match_expr (&inquire->unit);
3104 if (m == MATCH_ERROR)
3110 /* See if we have the IOLENGTH form of the inquire statement. */
3111 if (inquire->iolength != NULL)
3113 if (gfc_match_char (')') != MATCH_YES)
3116 m = match_io_list (M_INQUIRE, &code);
3117 if (m == MATCH_ERROR)
3122 new_st.op = EXEC_IOLENGTH;
3123 new_st.expr = inquire->iolength;
3124 new_st.ext.inquire = inquire;
3126 if (gfc_pure (NULL))
3128 gfc_free_statements (code);
3129 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3133 new_st.block = gfc_get_code ();
3134 new_st.block->op = EXEC_IOLENGTH;
3135 terminate_io (code);
3136 new_st.block->next = code;
3140 /* At this point, we have the non-IOLENGTH inquire statement. */
3143 if (gfc_match_char (')') == MATCH_YES)
3145 if (gfc_match_char (',') != MATCH_YES)
3148 m = match_inquire_element (inquire);
3149 if (m == MATCH_ERROR)
3154 if (inquire->iolength != NULL)
3156 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3161 if (gfc_match_eos () != MATCH_YES)
3164 if (inquire->unit != NULL && inquire->file != NULL)
3166 gfc_error ("INQUIRE statement at %L cannot contain both FILE and "
3167 "UNIT specifiers", &loc);
3171 if (inquire->unit == NULL && inquire->file == NULL)
3173 gfc_error ("INQUIRE statement at %L requires either FILE or "
3174 "UNIT specifier", &loc);
3178 if (gfc_pure (NULL))
3180 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3184 new_st.op = EXEC_INQUIRE;
3185 new_st.ext.inquire = inquire;
3189 gfc_syntax_error (ST_INQUIRE);
3192 gfc_free_inquire (inquire);
3197 /* Resolve everything in a gfc_inquire structure. */
3200 gfc_resolve_inquire (gfc_inquire *inquire)
3202 RESOLVE_TAG (&tag_unit, inquire->unit);
3203 RESOLVE_TAG (&tag_file, inquire->file);
3204 RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
3205 RESOLVE_TAG (&tag_iostat, inquire->iostat);
3206 RESOLVE_TAG (&tag_exist, inquire->exist);
3207 RESOLVE_TAG (&tag_opened, inquire->opened);
3208 RESOLVE_TAG (&tag_number, inquire->number);
3209 RESOLVE_TAG (&tag_named, inquire->named);
3210 RESOLVE_TAG (&tag_name, inquire->name);
3211 RESOLVE_TAG (&tag_s_access, inquire->access);
3212 RESOLVE_TAG (&tag_sequential, inquire->sequential);
3213 RESOLVE_TAG (&tag_direct, inquire->direct);
3214 RESOLVE_TAG (&tag_s_form, inquire->form);
3215 RESOLVE_TAG (&tag_formatted, inquire->formatted);
3216 RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
3217 RESOLVE_TAG (&tag_s_recl, inquire->recl);
3218 RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
3219 RESOLVE_TAG (&tag_s_blank, inquire->blank);
3220 RESOLVE_TAG (&tag_s_position, inquire->position);
3221 RESOLVE_TAG (&tag_s_action, inquire->action);
3222 RESOLVE_TAG (&tag_read, inquire->read);
3223 RESOLVE_TAG (&tag_write, inquire->write);
3224 RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
3225 RESOLVE_TAG (&tag_s_delim, inquire->delim);
3226 RESOLVE_TAG (&tag_s_pad, inquire->pad);
3227 RESOLVE_TAG (&tag_iolength, inquire->iolength);
3228 RESOLVE_TAG (&tag_convert, inquire->convert);
3229 RESOLVE_TAG (&tag_strm_out, inquire->strm_pos);
3231 if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)