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 == '\\')
144 locus old_locus = gfc_current_locus;
146 switch (gfc_next_char_literal (1))
174 /* Unknown backslash codes are simply not expanded. */
175 gfc_current_locus = old_locus;
179 if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
180 gfc_warning ("Extension: backslash character at %C");
183 if (mode == MODE_COPY)
184 *format_string++ = c;
191 /* Back up one character position. Only works once. */
199 /* Eat up the spaces and return a character. */
202 next_char_not_space (void)
209 while (gfc_is_whitespace (c));
213 static int value = 0;
215 /* Simple lexical analyzer for getting the next token in a FORMAT
226 if (saved_token != FMT_NONE)
229 saved_token = FMT_NONE;
233 c = next_char_not_space ();
241 c = next_char_not_space ();
252 c = next_char_not_space ();
254 value = 10 * value + c - '0';
263 token = FMT_SIGNED_INT;
282 c = next_char_not_space ();
286 value = 10 * value + c - '0';
291 token = zflag ? FMT_ZERO : FMT_POSINT;
315 c = next_char_not_space ();
316 if (c != 'L' && c != 'R')
335 c = next_char_not_space ();
336 if (c != 'P' && c != 'S')
343 c = next_char_not_space ();
344 if (c == 'N' || c == 'Z')
405 c = next_char_not_space ();
406 if (c == 'N' || c == 'S')
449 /* Check a format statement. The format string, either from a FORMAT
450 statement or a constant in an I/O statement has already been parsed
451 by itself, and we are checking it for validity. The dual origin
452 means that the warning message is a little less than great. */
457 const char *posint_required = _("Positive width required");
458 const char *nonneg_required = _("Nonnegative width required");
459 const char *unexpected_element = _("Unexpected element");
460 const char *unexpected_end = _("Unexpected end of format string");
469 saved_token = FMT_NONE;
477 error = _("Missing leading left parenthesis");
483 goto finished; /* Empty format is legal */
487 /* In this state, the next thing has to be a format item. */
511 /* Signed integer can only precede a P format. */
515 error = _("Expected P edit descriptor");
522 /* P requires a prior number. */
523 error = _("P descriptor requires leading scale factor");
527 /* X requires a prior number if we're being pedantic. */
528 if (gfc_notify_std (GFC_STD_GNU, "Extension: X descriptor "
529 "requires leading space count at %C")
539 goto extension_optional_comma;
548 if (gfc_notify_std (GFC_STD_GNU, "Extension: $ descriptor at %C")
551 if (t != FMT_RPAREN || level > 0)
553 gfc_warning ("$ should be the last specifier in format at %C");
554 goto optional_comma_1;
574 error = unexpected_end;
578 error = unexpected_element;
583 /* In this state, t must currently be a data descriptor.
584 Deal with things that can/must follow the descriptor. */
598 error = _("Repeat count cannot follow P descriptor");
613 switch (gfc_notification_std (GFC_STD_GNU))
616 gfc_warning ("Extension: Missing positive width after L "
622 error = posint_required;
647 error = posint_required;
654 /* Warn if -std=legacy, otherwise error. */
655 if (gfc_option.warn_std != 0)
656 gfc_error_now ("Period required in format specifier at %C");
658 gfc_warning ("Period required in format specifier at %C");
664 if (u != FMT_ZERO && u != FMT_POSINT)
666 error = nonneg_required;
673 /* Look for optional exponent. */
684 error = _("Positive exponent width required");
693 if (t != FMT_ZERO && t != FMT_POSINT)
695 error = nonneg_required;
702 /* Warn if -std=legacy, otherwise error. */
703 if (gfc_option.warn_std != 0)
704 gfc_error_now ("Period required in format specifier at %C");
706 gfc_warning ("Period required in format specifier at %C");
712 if (t != FMT_ZERO && t != FMT_POSINT)
714 error = nonneg_required;
721 if(mode == MODE_STRING)
723 format_string += value;
724 format_length -= value;
738 if (t != FMT_ZERO && t != FMT_POSINT)
740 error = nonneg_required;
752 if (t != FMT_ZERO && t != FMT_POSINT)
754 error = nonneg_required;
762 error = unexpected_element;
767 /* Between a descriptor and what comes next. */
786 error = unexpected_end;
790 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
797 /* Optional comma is a weird between state where we've just finished
798 reading a colon, slash, dollar or P descriptor. */
813 /* Assume that we have another format item. */
820 extension_optional_comma:
821 /* As a GNU extension, permit a missing comma after a string literal. */
839 error = unexpected_end;
843 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
853 /* Something went wrong. If the format we're checking is a string,
854 generate a warning, since the program is correct. If the format
855 is in a FORMAT statement, this messes up parsing, which is an
857 if (mode != MODE_STRING)
858 gfc_error ("%s in format string at %C", error);
861 gfc_warning ("%s in format string at %C", error);
863 /* TODO: More elaborate measures are needed to show where a problem
864 is within a format string that has been calculated. */
874 /* Given an expression node that is a constant string, see if it looks
875 like a format string. */
878 check_format_string (gfc_expr *e)
881 format_string = e->value.character.string;
886 /************ Fortran 95 I/O statement matchers *************/
888 /* Match a FORMAT statement. This amounts to actually parsing the
889 format descriptors in order to correctly locate the end of the
893 gfc_match_format (void)
898 if (gfc_current_ns->proc_name
899 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
901 gfc_error ("Format statement in module main block at %C");
905 if (gfc_statement_label == NULL)
907 gfc_error ("Missing format label at %C");
910 gfc_gobble_whitespace ();
915 start = gfc_current_locus;
917 if (check_format () == FAILURE)
920 if (gfc_match_eos () != MATCH_YES)
922 gfc_syntax_error (ST_FORMAT);
926 /* The label doesn't get created until after the statement is done
927 being matched, so we have to leave the string for later. */
929 gfc_current_locus = start; /* Back to the beginning */
932 new_st.op = EXEC_NOP;
935 e->expr_type = EXPR_CONSTANT;
936 e->ts.type = BT_CHARACTER;
937 e->ts.kind = gfc_default_character_kind;
939 e->value.character.string = format_string = gfc_getmem (format_length + 1);
940 e->value.character.length = format_length;
941 gfc_statement_label->format = e;
944 check_format (); /* Guaranteed to succeed */
945 gfc_match_eos (); /* Guaranteed to succeed */
951 /* Match an expression I/O tag of some sort. */
954 match_etag (const io_tag *tag, gfc_expr **v)
959 m = gfc_match (tag->spec, &result);
965 gfc_error ("Duplicate %s specification at %C", tag->name);
966 gfc_free_expr (result);
975 /* Match a variable I/O tag of some sort. */
978 match_vtag (const io_tag *tag, gfc_expr **v)
983 m = gfc_match (tag->spec, &result);
989 gfc_error ("Duplicate %s specification at %C", tag->name);
990 gfc_free_expr (result);
994 if (result->symtree->n.sym->attr.intent == INTENT_IN)
996 gfc_error ("Variable tag cannot be INTENT(IN) at %C");
997 gfc_free_expr (result);
1001 if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
1003 gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
1004 gfc_free_expr (result);
1013 /* Match I/O tags that cause variables to become redefined. */
1016 match_out_tag(const io_tag *tag, gfc_expr **result)
1020 m = match_vtag(tag, result);
1022 gfc_check_do_variable((*result)->symtree);
1028 /* Match a label I/O tag. */
1031 match_ltag (const io_tag *tag, gfc_st_label ** label)
1037 m = gfc_match (tag->spec, label);
1038 if (m == MATCH_YES && old != 0)
1040 gfc_error ("Duplicate %s label specification at %C", tag->name);
1045 && gfc_reference_st_label (*label, ST_LABEL_TARGET) == FAILURE)
1052 /* Do expression resolution and type-checking on an expression tag. */
1055 resolve_tag (const io_tag *tag, gfc_expr *e)
1060 if (gfc_resolve_expr (e) == FAILURE)
1063 if (e->ts.type != tag->type && tag != &tag_format)
1065 gfc_error ("%s tag at %L must be of type %s", tag->name,
1066 &e->where, gfc_basic_typename (tag->type));
1070 if (tag == &tag_format)
1072 if (e->expr_type == EXPR_CONSTANT
1073 && (e->ts.type != BT_CHARACTER
1074 || e->ts.kind != gfc_default_character_kind))
1076 gfc_error ("Constant expression in FORMAT tag at %L must be "
1077 "of type default CHARACTER", &e->where);
1081 /* If e's rank is zero and e is not an element of an array, it should be
1082 of integer or character type. The integer variable should be
1084 if (e->symtree == NULL || e->symtree->n.sym->as == NULL
1085 || e->symtree->n.sym->as->rank == 0)
1087 if (e->ts.type != BT_CHARACTER && e->ts.type != BT_INTEGER)
1089 gfc_error ("%s tag at %L must be of type %s or %s", tag->name,
1090 &e->where, gfc_basic_typename (BT_CHARACTER),
1091 gfc_basic_typename (BT_INTEGER));
1094 else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
1096 if (gfc_notify_std (GFC_STD_F95_DEL, "Obsolete: ASSIGNED "
1097 "variable in FORMAT tag at %L", &e->where)
1100 if (e->symtree->n.sym->attr.assign != 1)
1102 gfc_error ("Variable '%s' at %L has not been assigned a "
1103 "format label", e->symtree->n.sym->name,
1108 else if (e->ts.type == BT_INTEGER)
1110 gfc_error ("scalar '%s' FORMAT tag at %L is not an ASSIGNED "
1111 "variable", gfc_basic_typename (e->ts.type),
1120 /* if rank is nonzero, we allow the type to be character under
1121 GFC_STD_GNU and other type under GFC_STD_LEGACY. It may be
1122 assigned an Hollerith constant. */
1123 if (e->ts.type == BT_CHARACTER)
1125 if (gfc_notify_std (GFC_STD_GNU, "Extension: Character array "
1126 "in FORMAT tag at %L", &e->where)
1132 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: Non-character "
1133 "in FORMAT tag at %L", &e->where)
1144 gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
1148 if (tag == &tag_iomsg)
1150 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IOMSG tag at %L",
1151 &e->where) == FAILURE)
1155 if (tag == &tag_iostat && e->ts.kind != gfc_default_integer_kind)
1157 if (gfc_notify_std (GFC_STD_GNU, "Fortran 95 requires default "
1158 "INTEGER in IOSTAT tag at %L", &e->where)
1163 if (tag == &tag_size && e->ts.kind != gfc_default_integer_kind)
1165 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1166 "INTEGER in SIZE tag at %L", &e->where)
1171 if (tag == &tag_convert)
1173 if (gfc_notify_std (GFC_STD_GNU, "Extension: CONVERT tag at %L",
1174 &e->where) == FAILURE)
1178 if (tag == &tag_iolength && e->ts.kind != gfc_default_integer_kind)
1180 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1181 "INTEGER in IOLENGTH tag at %L", &e->where)
1191 /* Match a single tag of an OPEN statement. */
1194 match_open_element (gfc_open *open)
1198 m = match_etag (&tag_unit, &open->unit);
1201 m = match_out_tag (&tag_iomsg, &open->iomsg);
1204 m = match_out_tag (&tag_iostat, &open->iostat);
1207 m = match_etag (&tag_file, &open->file);
1210 m = match_etag (&tag_status, &open->status);
1213 m = match_etag (&tag_e_access, &open->access);
1216 m = match_etag (&tag_e_form, &open->form);
1219 m = match_etag (&tag_e_recl, &open->recl);
1222 m = match_etag (&tag_e_blank, &open->blank);
1225 m = match_etag (&tag_e_position, &open->position);
1228 m = match_etag (&tag_e_action, &open->action);
1231 m = match_etag (&tag_e_delim, &open->delim);
1234 m = match_etag (&tag_e_pad, &open->pad);
1237 m = match_ltag (&tag_err, &open->err);
1240 m = match_etag (&tag_convert, &open->convert);
1248 /* Free the gfc_open structure and all the expressions it contains. */
1251 gfc_free_open (gfc_open *open)
1256 gfc_free_expr (open->unit);
1257 gfc_free_expr (open->iomsg);
1258 gfc_free_expr (open->iostat);
1259 gfc_free_expr (open->file);
1260 gfc_free_expr (open->status);
1261 gfc_free_expr (open->access);
1262 gfc_free_expr (open->form);
1263 gfc_free_expr (open->recl);
1264 gfc_free_expr (open->blank);
1265 gfc_free_expr (open->position);
1266 gfc_free_expr (open->action);
1267 gfc_free_expr (open->delim);
1268 gfc_free_expr (open->pad);
1269 gfc_free_expr (open->convert);
1274 /* Resolve everything in a gfc_open structure. */
1277 gfc_resolve_open (gfc_open *open)
1280 RESOLVE_TAG (&tag_unit, open->unit);
1281 RESOLVE_TAG (&tag_iomsg, open->iomsg);
1282 RESOLVE_TAG (&tag_iostat, open->iostat);
1283 RESOLVE_TAG (&tag_file, open->file);
1284 RESOLVE_TAG (&tag_status, open->status);
1285 RESOLVE_TAG (&tag_e_access, open->access);
1286 RESOLVE_TAG (&tag_e_form, open->form);
1287 RESOLVE_TAG (&tag_e_recl, open->recl);
1288 RESOLVE_TAG (&tag_e_blank, open->blank);
1289 RESOLVE_TAG (&tag_e_position, open->position);
1290 RESOLVE_TAG (&tag_e_action, open->action);
1291 RESOLVE_TAG (&tag_e_delim, open->delim);
1292 RESOLVE_TAG (&tag_e_pad, open->pad);
1293 RESOLVE_TAG (&tag_convert, open->convert);
1295 if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1302 /* Check if a given value for a SPECIFIER is either in the list of values
1303 allowed in F95 or F2003, issuing an error message and returning a zero
1304 value if it is not allowed. */
1307 compare_to_allowed_values (const char *specifier, const char *allowed[],
1308 const char *allowed_f2003[],
1309 const char *allowed_gnu[], char *value,
1310 const char *statement, bool warn)
1315 len = strlen (value);
1318 for (len--; len > 0; len--)
1319 if (value[len] != ' ')
1324 for (i = 0; allowed[i]; i++)
1325 if (len == strlen (allowed[i])
1326 && strncasecmp (value, allowed[i], strlen (allowed[i])) == 0)
1329 for (i = 0; allowed_f2003 && allowed_f2003[i]; i++)
1330 if (len == strlen (allowed_f2003[i])
1331 && strncasecmp (value, allowed_f2003[i], strlen (allowed_f2003[i]))
1334 notification n = gfc_notification_std (GFC_STD_F2003);
1336 if (n == WARNING || (warn && n == ERROR))
1338 gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1339 "has value '%s'", specifier, statement,
1346 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: %s specifier in "
1347 "%s statement at %C has value '%s'", specifier,
1348 statement, allowed_f2003[i]);
1356 for (i = 0; allowed_gnu && allowed_gnu[i]; i++)
1357 if (len == strlen (allowed_gnu[i])
1358 && strncasecmp (value, allowed_gnu[i], strlen (allowed_gnu[i])) == 0)
1360 notification n = gfc_notification_std (GFC_STD_GNU);
1362 if (n == WARNING || (warn && n == ERROR))
1364 gfc_warning ("Extension: %s specifier in %s statement at %C "
1365 "has value '%s'", specifier, statement,
1372 gfc_notify_std (GFC_STD_GNU, "Extension: %s specifier in "
1373 "%s statement at %C has value '%s'", specifier,
1374 statement, allowed_gnu[i]);
1384 gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1385 specifier, statement, value);
1390 gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1391 specifier, statement, value);
1397 /* Match an OPEN statement. */
1400 gfc_match_open (void)
1406 m = gfc_match_char ('(');
1410 open = gfc_getmem (sizeof (gfc_open));
1412 m = match_open_element (open);
1414 if (m == MATCH_ERROR)
1418 m = gfc_match_expr (&open->unit);
1421 if (m == MATCH_ERROR)
1427 if (gfc_match_char (')') == MATCH_YES)
1429 if (gfc_match_char (',') != MATCH_YES)
1432 m = match_open_element (open);
1433 if (m == MATCH_ERROR)
1439 if (gfc_match_eos () == MATCH_NO)
1442 if (gfc_pure (NULL))
1444 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1448 warn = (open->err || open->iostat) ? true : false;
1449 /* Checks on the ACCESS specifier. */
1450 if (open->access && open->access->expr_type == EXPR_CONSTANT)
1452 static const char *access_f95[] = { "SEQUENTIAL", "DIRECT", NULL };
1453 static const char *access_f2003[] = { "STREAM", NULL };
1454 static const char *access_gnu[] = { "APPEND", NULL };
1456 if (!compare_to_allowed_values ("ACCESS", access_f95, access_f2003,
1458 open->access->value.character.string,
1463 /* Checks on the ACTION specifier. */
1464 if (open->action && open->action->expr_type == EXPR_CONSTANT)
1466 static const char *action[] = { "READ", "WRITE", "READWRITE", NULL };
1468 if (!compare_to_allowed_values ("ACTION", action, NULL, NULL,
1469 open->action->value.character.string,
1474 /* Checks on the ASYNCHRONOUS specifier. */
1475 /* TODO: code is ready, just needs uncommenting when async I/O support
1477 if (open->asynchronous && open->asynchronous->expr_type == EXPR_CONSTANT)
1479 static const char * asynchronous[] = { "YES", "NO", NULL };
1481 if (!compare_to_allowed_values
1482 ("action", asynchronous, NULL, NULL,
1483 open->asynchronous->value.character.string, "OPEN", warn))
1487 /* Checks on the BLANK specifier. */
1488 if (open->blank && open->blank->expr_type == EXPR_CONSTANT)
1490 static const char *blank[] = { "ZERO", "NULL", NULL };
1492 if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
1493 open->blank->value.character.string,
1498 /* Checks on the DECIMAL specifier. */
1499 /* TODO: uncomment this code when DECIMAL support is added
1500 if (open->decimal && open->decimal->expr_type == EXPR_CONSTANT)
1502 static const char * decimal[] = { "COMMA", "POINT", NULL };
1504 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1505 open->decimal->value.character.string,
1510 /* Checks on the DELIM specifier. */
1511 if (open->delim && open->delim->expr_type == EXPR_CONSTANT)
1513 static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
1515 if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
1516 open->delim->value.character.string,
1521 /* Checks on the ENCODING specifier. */
1522 /* TODO: uncomment this code when ENCODING support is added
1523 if (open->encoding && open->encoding->expr_type == EXPR_CONSTANT)
1525 static const char * encoding[] = { "UTF-8", "DEFAULT", NULL };
1527 if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1528 open->encoding->value.character.string,
1533 /* Checks on the FORM specifier. */
1534 if (open->form && open->form->expr_type == EXPR_CONSTANT)
1536 static const char *form[] = { "FORMATTED", "UNFORMATTED", NULL };
1538 if (!compare_to_allowed_values ("FORM", form, NULL, NULL,
1539 open->form->value.character.string,
1544 /* Checks on the PAD specifier. */
1545 if (open->pad && open->pad->expr_type == EXPR_CONSTANT)
1547 static const char *pad[] = { "YES", "NO", NULL };
1549 if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
1550 open->pad->value.character.string,
1555 /* Checks on the POSITION specifier. */
1556 if (open->position && open->position->expr_type == EXPR_CONSTANT)
1558 static const char *position[] = { "ASIS", "REWIND", "APPEND", NULL };
1560 if (!compare_to_allowed_values ("POSITION", position, NULL, NULL,
1561 open->position->value.character.string,
1566 /* Checks on the ROUND specifier. */
1567 /* TODO: uncomment this code when ROUND support is added
1568 if (open->round && open->round->expr_type == EXPR_CONSTANT)
1570 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1571 "COMPATIBLE", "PROCESSOR_DEFINED", NULL };
1573 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1574 open->round->value.character.string,
1579 /* Checks on the SIGN specifier. */
1580 /* TODO: uncomment this code when SIGN support is added
1581 if (open->sign && open->sign->expr_type == EXPR_CONSTANT)
1583 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1586 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1587 open->sign->value.character.string,
1592 #define warn_or_error(...) \
1595 gfc_warning (__VA_ARGS__); \
1598 gfc_error (__VA_ARGS__); \
1603 /* Checks on the RECL specifier. */
1604 if (open->recl && open->recl->expr_type == EXPR_CONSTANT
1605 && open->recl->ts.type == BT_INTEGER
1606 && mpz_sgn (open->recl->value.integer) != 1)
1608 warn_or_error ("RECL in OPEN statement at %C must be positive");
1611 /* Checks on the STATUS specifier. */
1612 if (open->status && open->status->expr_type == EXPR_CONSTANT)
1614 static const char *status[] = { "OLD", "NEW", "SCRATCH",
1615 "REPLACE", "UNKNOWN", NULL };
1617 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1618 open->status->value.character.string,
1622 /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1623 the FILE= specifier shall appear. */
1624 if (open->file == NULL
1625 && (strncasecmp (open->status->value.character.string, "replace", 7)
1627 || strncasecmp (open->status->value.character.string, "new", 3)
1630 warn_or_error ("The STATUS specified in OPEN statement at %C is "
1631 "'%s' and no FILE specifier is present",
1632 open->status->value.character.string);
1635 /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1636 the FILE= specifier shall not appear. */
1637 if (strncasecmp (open->status->value.character.string, "scratch", 7)
1640 warn_or_error ("The STATUS specified in OPEN statement at %C "
1641 "cannot have the value SCRATCH if a FILE specifier "
1646 /* Things that are not allowed for unformatted I/O. */
1647 if (open->form && open->form->expr_type == EXPR_CONSTANT
1649 /* TODO uncomment this code when F2003 support is finished */
1650 /* || open->decimal || open->encoding || open->round
1652 || open->pad || open->blank)
1653 && strncasecmp (open->form->value.character.string,
1654 "unformatted", 11) == 0)
1656 const char *spec = (open->delim ? "DELIM "
1657 : (open->pad ? "PAD " : open->blank
1660 warn_or_error ("%s specifier at %C not allowed in OPEN statement for "
1661 "unformatted I/O", spec);
1664 if (open->recl && open->access && open->access->expr_type == EXPR_CONSTANT
1665 && strncasecmp (open->access->value.character.string, "stream", 6) == 0)
1667 warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
1672 && open->access && open->access->expr_type == EXPR_CONSTANT
1673 && !(strncasecmp (open->access->value.character.string,
1674 "sequential", 10) == 0
1675 || strncasecmp (open->access->value.character.string,
1677 || strncasecmp (open->access->value.character.string,
1680 warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
1681 "for stream or sequential ACCESS");
1684 #undef warn_or_error
1686 new_st.op = EXEC_OPEN;
1687 new_st.ext.open = open;
1691 gfc_syntax_error (ST_OPEN);
1694 gfc_free_open (open);
1699 /* Free a gfc_close structure an all its expressions. */
1702 gfc_free_close (gfc_close *close)
1707 gfc_free_expr (close->unit);
1708 gfc_free_expr (close->iomsg);
1709 gfc_free_expr (close->iostat);
1710 gfc_free_expr (close->status);
1715 /* Match elements of a CLOSE statement. */
1718 match_close_element (gfc_close *close)
1722 m = match_etag (&tag_unit, &close->unit);
1725 m = match_etag (&tag_status, &close->status);
1728 m = match_out_tag (&tag_iomsg, &close->iomsg);
1731 m = match_out_tag (&tag_iostat, &close->iostat);
1734 m = match_ltag (&tag_err, &close->err);
1742 /* Match a CLOSE statement. */
1745 gfc_match_close (void)
1751 m = gfc_match_char ('(');
1755 close = gfc_getmem (sizeof (gfc_close));
1757 m = match_close_element (close);
1759 if (m == MATCH_ERROR)
1763 m = gfc_match_expr (&close->unit);
1766 if (m == MATCH_ERROR)
1772 if (gfc_match_char (')') == MATCH_YES)
1774 if (gfc_match_char (',') != MATCH_YES)
1777 m = match_close_element (close);
1778 if (m == MATCH_ERROR)
1784 if (gfc_match_eos () == MATCH_NO)
1787 if (gfc_pure (NULL))
1789 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1793 warn = (close->iostat || close->err) ? true : false;
1795 /* Checks on the STATUS specifier. */
1796 if (close->status && close->status->expr_type == EXPR_CONSTANT)
1798 static const char *status[] = { "KEEP", "DELETE", NULL };
1800 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1801 close->status->value.character.string,
1806 new_st.op = EXEC_CLOSE;
1807 new_st.ext.close = close;
1811 gfc_syntax_error (ST_CLOSE);
1814 gfc_free_close (close);
1819 /* Resolve everything in a gfc_close structure. */
1822 gfc_resolve_close (gfc_close *close)
1824 RESOLVE_TAG (&tag_unit, close->unit);
1825 RESOLVE_TAG (&tag_iomsg, close->iomsg);
1826 RESOLVE_TAG (&tag_iostat, close->iostat);
1827 RESOLVE_TAG (&tag_status, close->status);
1829 if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
1836 /* Free a gfc_filepos structure. */
1839 gfc_free_filepos (gfc_filepos *fp)
1841 gfc_free_expr (fp->unit);
1842 gfc_free_expr (fp->iomsg);
1843 gfc_free_expr (fp->iostat);
1848 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
1851 match_file_element (gfc_filepos *fp)
1855 m = match_etag (&tag_unit, &fp->unit);
1858 m = match_out_tag (&tag_iomsg, &fp->iomsg);
1861 m = match_out_tag (&tag_iostat, &fp->iostat);
1864 m = match_ltag (&tag_err, &fp->err);
1872 /* Match the second half of the file-positioning statements, REWIND,
1873 BACKSPACE, ENDFILE, or the FLUSH statement. */
1876 match_filepos (gfc_statement st, gfc_exec_op op)
1881 fp = gfc_getmem (sizeof (gfc_filepos));
1883 if (gfc_match_char ('(') == MATCH_NO)
1885 m = gfc_match_expr (&fp->unit);
1886 if (m == MATCH_ERROR)
1894 m = match_file_element (fp);
1895 if (m == MATCH_ERROR)
1899 m = gfc_match_expr (&fp->unit);
1900 if (m == MATCH_ERROR)
1908 if (gfc_match_char (')') == MATCH_YES)
1910 if (gfc_match_char (',') != MATCH_YES)
1913 m = match_file_element (fp);
1914 if (m == MATCH_ERROR)
1921 if (gfc_match_eos () != MATCH_YES)
1924 if (gfc_pure (NULL))
1926 gfc_error ("%s statement not allowed in PURE procedure at %C",
1927 gfc_ascii_statement (st));
1933 new_st.ext.filepos = fp;
1937 gfc_syntax_error (st);
1940 gfc_free_filepos (fp);
1946 gfc_resolve_filepos (gfc_filepos *fp)
1948 RESOLVE_TAG (&tag_unit, fp->unit);
1949 RESOLVE_TAG (&tag_iostat, fp->iostat);
1950 RESOLVE_TAG (&tag_iomsg, fp->iomsg);
1951 if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
1958 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
1959 and the FLUSH statement. */
1962 gfc_match_endfile (void)
1964 return match_filepos (ST_END_FILE, EXEC_ENDFILE);
1968 gfc_match_backspace (void)
1970 return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
1974 gfc_match_rewind (void)
1976 return match_filepos (ST_REWIND, EXEC_REWIND);
1980 gfc_match_flush (void)
1982 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: FLUSH statement at %C")
1986 return match_filepos (ST_FLUSH, EXEC_FLUSH);
1989 /******************** Data Transfer Statements *********************/
1992 { M_READ, M_WRITE, M_PRINT, M_INQUIRE }
1996 /* Return a default unit number. */
1999 default_unit (io_kind k)
2008 return gfc_int_expr (unit);
2012 /* Match a unit specification for a data transfer statement. */
2015 match_dt_unit (io_kind k, gfc_dt *dt)
2019 if (gfc_match_char ('*') == MATCH_YES)
2021 if (dt->io_unit != NULL)
2024 dt->io_unit = default_unit (k);
2028 if (gfc_match_expr (&e) == MATCH_YES)
2030 if (dt->io_unit != NULL)
2043 gfc_error ("Duplicate UNIT specification at %C");
2048 /* Match a format specification. */
2051 match_dt_format (gfc_dt *dt)
2055 gfc_st_label *label;
2057 where = gfc_current_locus;
2059 if (gfc_match_char ('*') == MATCH_YES)
2061 if (dt->format_expr != NULL || dt->format_label != NULL)
2064 dt->format_label = &format_asterisk;
2068 if (gfc_match_st_label (&label) == MATCH_YES)
2070 if (dt->format_expr != NULL || dt->format_label != NULL)
2072 gfc_free_st_label (label);
2076 if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
2079 dt->format_label = label;
2083 if (gfc_match_expr (&e) == MATCH_YES)
2085 if (dt->format_expr != NULL || dt->format_label != NULL)
2090 dt->format_expr = e;
2094 gfc_current_locus = where; /* The only case where we have to restore */
2099 gfc_error ("Duplicate format specification at %C");
2104 /* Traverse a namelist that is part of a READ statement to make sure
2105 that none of the variables in the namelist are INTENT(IN). Returns
2106 nonzero if we find such a variable. */
2109 check_namelist (gfc_symbol *sym)
2113 for (p = sym->namelist; p; p = p->next)
2114 if (p->sym->attr.intent == INTENT_IN)
2116 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2117 p->sym->name, sym->name);
2125 /* Match a single data transfer element. */
2128 match_dt_element (io_kind k, gfc_dt *dt)
2130 char name[GFC_MAX_SYMBOL_LEN + 1];
2134 if (gfc_match (" unit =") == MATCH_YES)
2136 m = match_dt_unit (k, dt);
2141 if (gfc_match (" fmt =") == MATCH_YES)
2143 m = match_dt_format (dt);
2148 if (gfc_match (" nml = %n", name) == MATCH_YES)
2150 if (dt->namelist != NULL)
2152 gfc_error ("Duplicate NML specification at %C");
2156 if (gfc_find_symbol (name, NULL, 1, &sym))
2159 if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
2161 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2162 sym != NULL ? sym->name : name);
2167 if (k == M_READ && check_namelist (sym))
2173 m = match_etag (&tag_rec, &dt->rec);
2176 m = match_etag (&tag_spos, &dt->rec);
2179 m = match_out_tag (&tag_iomsg, &dt->iomsg);
2182 m = match_out_tag (&tag_iostat, &dt->iostat);
2185 m = match_ltag (&tag_err, &dt->err);
2187 dt->err_where = gfc_current_locus;
2190 m = match_etag (&tag_advance, &dt->advance);
2193 m = match_out_tag (&tag_size, &dt->size);
2197 m = match_ltag (&tag_end, &dt->end);
2202 gfc_error ("END tag at %C not allowed in output statement");
2205 dt->end_where = gfc_current_locus;
2210 m = match_ltag (&tag_eor, &dt->eor);
2212 dt->eor_where = gfc_current_locus;
2220 /* Free a data transfer structure and everything below it. */
2223 gfc_free_dt (gfc_dt *dt)
2228 gfc_free_expr (dt->io_unit);
2229 gfc_free_expr (dt->format_expr);
2230 gfc_free_expr (dt->rec);
2231 gfc_free_expr (dt->advance);
2232 gfc_free_expr (dt->iomsg);
2233 gfc_free_expr (dt->iostat);
2234 gfc_free_expr (dt->size);
2239 /* Resolve everything in a gfc_dt structure. */
2242 gfc_resolve_dt (gfc_dt *dt)
2246 RESOLVE_TAG (&tag_format, dt->format_expr);
2247 RESOLVE_TAG (&tag_rec, dt->rec);
2248 RESOLVE_TAG (&tag_spos, dt->rec);
2249 RESOLVE_TAG (&tag_advance, dt->advance);
2250 RESOLVE_TAG (&tag_iomsg, dt->iomsg);
2251 RESOLVE_TAG (&tag_iostat, dt->iostat);
2252 RESOLVE_TAG (&tag_size, dt->size);
2255 if (gfc_resolve_expr (e) == SUCCESS
2256 && (e->ts.type != BT_INTEGER
2257 && (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_VARIABLE)))
2259 gfc_error ("UNIT specification at %L must be an INTEGER expression "
2260 "or a CHARACTER variable", &e->where);
2264 if (e->ts.type == BT_CHARACTER)
2266 if (gfc_has_vector_index (e))
2268 gfc_error ("Internal unit with vector subscript at %L", &e->where);
2273 if (e->rank && e->ts.type != BT_CHARACTER)
2275 gfc_error ("External IO UNIT cannot be an array at %L", &e->where);
2281 if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
2283 if (dt->err->defined == ST_LABEL_UNKNOWN)
2285 gfc_error ("ERR tag label %d at %L not defined",
2286 dt->err->value, &dt->err_where);
2293 if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
2295 if (dt->end->defined == ST_LABEL_UNKNOWN)
2297 gfc_error ("END tag label %d at %L not defined",
2298 dt->end->value, &dt->end_where);
2305 if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
2307 if (dt->eor->defined == ST_LABEL_UNKNOWN)
2309 gfc_error ("EOR tag label %d at %L not defined",
2310 dt->eor->value, &dt->eor_where);
2315 /* Check the format label actually exists. */
2316 if (dt->format_label && dt->format_label != &format_asterisk
2317 && dt->format_label->defined == ST_LABEL_UNKNOWN)
2319 gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
2320 &dt->format_label->where);
2327 /* Given an io_kind, return its name. */
2330 io_kind_name (io_kind k)
2349 gfc_internal_error ("io_kind_name(): bad I/O-kind");
2356 /* Match an IO iteration statement of the form:
2358 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2360 which is equivalent to a single IO element. This function is
2361 mutually recursive with match_io_element(). */
2363 static match match_io_element (io_kind, gfc_code **);
2366 match_io_iterator (io_kind k, gfc_code **result)
2368 gfc_code *head, *tail, *new;
2376 old_loc = gfc_current_locus;
2378 if (gfc_match_char ('(') != MATCH_YES)
2381 m = match_io_element (k, &head);
2384 if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
2390 /* Can't be anything but an IO iterator. Build a list. */
2391 iter = gfc_get_iterator ();
2395 m = gfc_match_iterator (iter, 0);
2396 if (m == MATCH_ERROR)
2400 gfc_check_do_variable (iter->var->symtree);
2404 m = match_io_element (k, &new);
2405 if (m == MATCH_ERROR)
2414 tail = gfc_append_code (tail, new);
2416 if (gfc_match_char (',') != MATCH_YES)
2425 if (gfc_match_char (')') != MATCH_YES)
2428 new = gfc_get_code ();
2430 new->ext.iterator = iter;
2432 new->block = gfc_get_code ();
2433 new->block->op = EXEC_DO;
2434 new->block->next = head;
2440 gfc_error ("Syntax error in I/O iterator at %C");
2444 gfc_free_iterator (iter, 1);
2445 gfc_free_statements (head);
2446 gfc_current_locus = old_loc;
2451 /* Match a single element of an IO list, which is either a single
2452 expression or an IO Iterator. */
2455 match_io_element (io_kind k, gfc_code **cpp)
2463 m = match_io_iterator (k, cpp);
2469 m = gfc_match_variable (&expr, 0);
2471 gfc_error ("Expected variable in READ statement at %C");
2475 m = gfc_match_expr (&expr);
2477 gfc_error ("Expected expression in %s statement at %C",
2485 if (expr->symtree->n.sym->attr.intent == INTENT_IN)
2487 gfc_error ("Variable '%s' in input list at %C cannot be "
2488 "INTENT(IN)", expr->symtree->n.sym->name);
2493 && gfc_impure_variable (expr->symtree->n.sym)
2494 && current_dt->io_unit->ts.type == BT_CHARACTER)
2496 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2497 expr->symtree->n.sym->name);
2501 if (gfc_check_do_variable (expr->symtree))
2507 if (current_dt->io_unit->ts.type == BT_CHARACTER
2509 && current_dt->io_unit->expr_type == EXPR_VARIABLE
2510 && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
2512 gfc_error ("Cannot write to internal file unit '%s' at %C "
2513 "inside a PURE procedure",
2514 current_dt->io_unit->symtree->n.sym->name);
2526 gfc_free_expr (expr);
2530 cp = gfc_get_code ();
2531 cp->op = EXEC_TRANSFER;
2539 /* Match an I/O list, building gfc_code structures as we go. */
2542 match_io_list (io_kind k, gfc_code **head_p)
2544 gfc_code *head, *tail, *new;
2547 *head_p = head = tail = NULL;
2548 if (gfc_match_eos () == MATCH_YES)
2553 m = match_io_element (k, &new);
2554 if (m == MATCH_ERROR)
2559 tail = gfc_append_code (tail, new);
2563 if (gfc_match_eos () == MATCH_YES)
2565 if (gfc_match_char (',') != MATCH_YES)
2573 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2576 gfc_free_statements (head);
2581 /* Attach the data transfer end node. */
2584 terminate_io (gfc_code *io_code)
2588 if (io_code == NULL)
2589 io_code = new_st.block;
2591 c = gfc_get_code ();
2592 c->op = EXEC_DT_END;
2594 /* Point to structure that is already there */
2595 c->ext.dt = new_st.ext.dt;
2596 gfc_append_code (io_code, c);
2600 /* Check the constraints for a data transfer statement. The majority of the
2601 constraints appearing in 9.4 of the standard appear here. Some are handled
2602 in resolve_tag and others in gfc_resolve_dt. */
2605 check_io_constraints (io_kind k, gfc_dt *dt, gfc_code *io_code,
2608 #define io_constraint(condition,msg,arg)\
2611 gfc_error(msg,arg);\
2617 gfc_symbol *sym = NULL;
2622 if (expr && expr->expr_type == EXPR_VARIABLE
2623 && expr->ts.type == BT_CHARACTER)
2625 sym = expr->symtree->n.sym;
2627 io_constraint (k == M_WRITE && sym->attr.intent == INTENT_IN,
2628 "Internal file at %L must not be INTENT(IN)",
2631 io_constraint (gfc_has_vector_index (dt->io_unit),
2632 "Internal file incompatible with vector subscript at %L",
2635 io_constraint (dt->rec != NULL,
2636 "REC tag at %L is incompatible with internal file",
2639 if (dt->namelist != NULL)
2641 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Internal file "
2642 "at %L with namelist", &expr->where)
2647 io_constraint (dt->advance != NULL,
2648 "ADVANCE tag at %L is incompatible with internal file",
2649 &dt->advance->where);
2652 if (expr && expr->ts.type != BT_CHARACTER)
2655 io_constraint (gfc_pure (NULL) && (k == M_READ || k == M_WRITE),
2656 "IO UNIT in %s statement at %C must be "
2657 "an internal file in a PURE procedure",
2664 io_constraint (dt->end, "END tag not allowed with output at %L",
2667 io_constraint (dt->eor, "EOR tag not allowed with output at %L",
2670 io_constraint (k != M_READ && dt->size,
2671 "SIZE=specifier not allowed with output at %L",
2676 io_constraint (dt->size && dt->advance == NULL,
2677 "SIZE tag at %L requires an ADVANCE tag",
2680 io_constraint (dt->eor && dt->advance == NULL,
2681 "EOR tag at %L requires an ADVANCE tag",
2689 io_constraint (io_code && dt->namelist,
2690 "NAMELIST cannot be followed by IO-list at %L",
2693 io_constraint (dt->format_expr,
2694 "IO spec-list cannot contain both NAMELIST group name "
2695 "and format specification at %L.",
2696 &dt->format_expr->where);
2698 io_constraint (dt->format_label,
2699 "IO spec-list cannot contain both NAMELIST group name "
2700 "and format label at %L", spec_end);
2702 io_constraint (dt->rec,
2703 "NAMELIST IO is not allowed with a REC=specifier "
2704 "at %L.", &dt->rec->where);
2706 io_constraint (dt->advance,
2707 "NAMELIST IO is not allowed with a ADVANCE=specifier "
2708 "at %L.", &dt->advance->where);
2713 io_constraint (dt->end,
2714 "An END tag is not allowed with a "
2715 "REC=specifier at %L.", &dt->end_where);
2718 io_constraint (dt->format_label == &format_asterisk,
2719 "FMT=* is not allowed with a REC=specifier "
2720 "at %L.", spec_end);
2725 int not_yes, not_no;
2728 io_constraint (dt->format_label == &format_asterisk,
2729 "List directed format(*) is not allowed with a "
2730 "ADVANCE=specifier at %L.", &expr->where);
2732 io_constraint (dt->format_expr == NULL && dt->format_label == NULL
2733 && dt->namelist == NULL,
2734 "the ADVANCE=specifier at %L must appear with an "
2735 "explicit format expression", &expr->where);
2737 if (expr->expr_type == EXPR_CONSTANT && expr->ts.type == BT_CHARACTER)
2739 const char * advance = expr->value.character.string;
2740 not_no = strcasecmp (advance, "no") != 0;
2741 not_yes = strcasecmp (advance, "yes") != 0;
2749 io_constraint (not_no && not_yes,
2750 "ADVANCE=specifier at %L must have value = "
2751 "YES or NO.", &expr->where);
2753 io_constraint (dt->size && not_no && k == M_READ,
2754 "SIZE tag at %L requires an ADVANCE = 'NO'",
2757 io_constraint (dt->eor && not_no && k == M_READ,
2758 "EOR tag at %L requires an ADVANCE = 'NO'",
2762 expr = dt->format_expr;
2763 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
2764 check_format_string (expr);
2768 #undef io_constraint
2771 /* Match a READ, WRITE or PRINT statement. */
2774 match_io (io_kind k)
2776 char name[GFC_MAX_SYMBOL_LEN + 1];
2785 where = gfc_current_locus;
2787 current_dt = dt = gfc_getmem (sizeof (gfc_dt));
2788 m = gfc_match_char ('(');
2791 where = gfc_current_locus;
2794 else if (k == M_PRINT)
2796 /* Treat the non-standard case of PRINT namelist. */
2797 if ((gfc_current_form == FORM_FIXED || gfc_peek_char () == ' ')
2798 && gfc_match_name (name) == MATCH_YES)
2800 gfc_find_symbol (name, NULL, 1, &sym);
2801 if (sym && sym->attr.flavor == FL_NAMELIST)
2803 if (gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
2804 "%C is an extension") == FAILURE)
2810 dt->io_unit = default_unit (k);
2815 gfc_current_locus = where;
2819 if (gfc_current_form == FORM_FREE)
2821 c = gfc_peek_char();
2822 if (c != ' ' && c != '*' && c != '\'' && c != '"')
2829 m = match_dt_format (dt);
2830 if (m == MATCH_ERROR)
2836 dt->io_unit = default_unit (k);
2841 /* Before issuing an error for a malformed 'print (1,*)' type of
2842 error, check for a default-char-expr of the form ('(I0)'). */
2843 if (k == M_PRINT && m == MATCH_YES)
2845 /* Reset current locus to get the initial '(' in an expression. */
2846 gfc_current_locus = where;
2847 dt->format_expr = NULL;
2848 m = match_dt_format (dt);
2850 if (m == MATCH_ERROR)
2852 if (m == MATCH_NO || dt->format_expr == NULL)
2856 dt->io_unit = default_unit (k);
2861 /* Match a control list */
2862 if (match_dt_element (k, dt) == MATCH_YES)
2864 if (match_dt_unit (k, dt) != MATCH_YES)
2867 if (gfc_match_char (')') == MATCH_YES)
2869 if (gfc_match_char (',') != MATCH_YES)
2872 m = match_dt_element (k, dt);
2875 if (m == MATCH_ERROR)
2878 m = match_dt_format (dt);
2881 if (m == MATCH_ERROR)
2884 where = gfc_current_locus;
2886 m = gfc_match_name (name);
2889 gfc_find_symbol (name, NULL, 1, &sym);
2890 if (sym && sym->attr.flavor == FL_NAMELIST)
2893 if (k == M_READ && check_namelist (sym))
2902 gfc_current_locus = where;
2904 goto loop; /* No matches, try regular elements */
2907 if (gfc_match_char (')') == MATCH_YES)
2909 if (gfc_match_char (',') != MATCH_YES)
2915 m = match_dt_element (k, dt);
2918 if (m == MATCH_ERROR)
2921 if (gfc_match_char (')') == MATCH_YES)
2923 if (gfc_match_char (',') != MATCH_YES)
2929 /* Used in check_io_constraints, where no locus is available. */
2930 spec_end = gfc_current_locus;
2932 /* Optional leading comma (non-standard). */
2934 && gfc_match_char (',') == MATCH_YES
2936 && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before output "
2937 "item list at %C is an extension") == FAILURE)
2941 if (gfc_match_eos () != MATCH_YES)
2943 if (comma_flag && gfc_match_char (',') != MATCH_YES)
2945 gfc_error ("Expected comma in I/O list at %C");
2950 m = match_io_list (k, &io_code);
2951 if (m == MATCH_ERROR)
2957 /* A full IO statement has been matched. Check the constraints. spec_end is
2958 supplied for cases where no locus is supplied. */
2959 m = check_io_constraints (k, dt, io_code, &spec_end);
2961 if (m == MATCH_ERROR)
2964 new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
2966 new_st.block = gfc_get_code ();
2967 new_st.block->op = new_st.op;
2968 new_st.block->next = io_code;
2970 terminate_io (io_code);
2975 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2985 gfc_match_read (void)
2987 return match_io (M_READ);
2991 gfc_match_write (void)
2993 return match_io (M_WRITE);
2997 gfc_match_print (void)
3001 m = match_io (M_PRINT);
3005 if (gfc_pure (NULL))
3007 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
3015 /* Free a gfc_inquire structure. */
3018 gfc_free_inquire (gfc_inquire *inquire)
3021 if (inquire == NULL)
3024 gfc_free_expr (inquire->unit);
3025 gfc_free_expr (inquire->file);
3026 gfc_free_expr (inquire->iomsg);
3027 gfc_free_expr (inquire->iostat);
3028 gfc_free_expr (inquire->exist);
3029 gfc_free_expr (inquire->opened);
3030 gfc_free_expr (inquire->number);
3031 gfc_free_expr (inquire->named);
3032 gfc_free_expr (inquire->name);
3033 gfc_free_expr (inquire->access);
3034 gfc_free_expr (inquire->sequential);
3035 gfc_free_expr (inquire->direct);
3036 gfc_free_expr (inquire->form);
3037 gfc_free_expr (inquire->formatted);
3038 gfc_free_expr (inquire->unformatted);
3039 gfc_free_expr (inquire->recl);
3040 gfc_free_expr (inquire->nextrec);
3041 gfc_free_expr (inquire->blank);
3042 gfc_free_expr (inquire->position);
3043 gfc_free_expr (inquire->action);
3044 gfc_free_expr (inquire->read);
3045 gfc_free_expr (inquire->write);
3046 gfc_free_expr (inquire->readwrite);
3047 gfc_free_expr (inquire->delim);
3048 gfc_free_expr (inquire->pad);
3049 gfc_free_expr (inquire->iolength);
3050 gfc_free_expr (inquire->convert);
3051 gfc_free_expr (inquire->strm_pos);
3056 /* Match an element of an INQUIRE statement. */
3058 #define RETM if (m != MATCH_NO) return m;
3061 match_inquire_element (gfc_inquire *inquire)
3065 m = match_etag (&tag_unit, &inquire->unit);
3066 RETM m = match_etag (&tag_file, &inquire->file);
3067 RETM m = match_ltag (&tag_err, &inquire->err);
3068 RETM m = match_out_tag (&tag_iomsg, &inquire->iomsg);
3069 RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
3070 RETM m = match_vtag (&tag_exist, &inquire->exist);
3071 RETM m = match_vtag (&tag_opened, &inquire->opened);
3072 RETM m = match_vtag (&tag_named, &inquire->named);
3073 RETM m = match_vtag (&tag_name, &inquire->name);
3074 RETM m = match_out_tag (&tag_number, &inquire->number);
3075 RETM m = match_vtag (&tag_s_access, &inquire->access);
3076 RETM m = match_vtag (&tag_sequential, &inquire->sequential);
3077 RETM m = match_vtag (&tag_direct, &inquire->direct);
3078 RETM m = match_vtag (&tag_s_form, &inquire->form);
3079 RETM m = match_vtag (&tag_formatted, &inquire->formatted);
3080 RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
3081 RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
3082 RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
3083 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
3084 RETM m = match_vtag (&tag_s_position, &inquire->position);
3085 RETM m = match_vtag (&tag_s_action, &inquire->action);
3086 RETM m = match_vtag (&tag_read, &inquire->read);
3087 RETM m = match_vtag (&tag_write, &inquire->write);
3088 RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
3089 RETM m = match_vtag (&tag_s_delim, &inquire->delim);
3090 RETM m = match_vtag (&tag_s_pad, &inquire->pad);
3091 RETM m = match_vtag (&tag_iolength, &inquire->iolength);
3092 RETM m = match_vtag (&tag_convert, &inquire->convert);
3093 RETM m = match_out_tag (&tag_strm_out, &inquire->strm_pos);
3094 RETM return MATCH_NO;
3101 gfc_match_inquire (void)
3103 gfc_inquire *inquire;
3108 m = gfc_match_char ('(');
3112 inquire = gfc_getmem (sizeof (gfc_inquire));
3114 loc = gfc_current_locus;
3116 m = match_inquire_element (inquire);
3117 if (m == MATCH_ERROR)
3121 m = gfc_match_expr (&inquire->unit);
3122 if (m == MATCH_ERROR)
3128 /* See if we have the IOLENGTH form of the inquire statement. */
3129 if (inquire->iolength != NULL)
3131 if (gfc_match_char (')') != MATCH_YES)
3134 m = match_io_list (M_INQUIRE, &code);
3135 if (m == MATCH_ERROR)
3140 new_st.op = EXEC_IOLENGTH;
3141 new_st.expr = inquire->iolength;
3142 new_st.ext.inquire = inquire;
3144 if (gfc_pure (NULL))
3146 gfc_free_statements (code);
3147 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3151 new_st.block = gfc_get_code ();
3152 new_st.block->op = EXEC_IOLENGTH;
3153 terminate_io (code);
3154 new_st.block->next = code;
3158 /* At this point, we have the non-IOLENGTH inquire statement. */
3161 if (gfc_match_char (')') == MATCH_YES)
3163 if (gfc_match_char (',') != MATCH_YES)
3166 m = match_inquire_element (inquire);
3167 if (m == MATCH_ERROR)
3172 if (inquire->iolength != NULL)
3174 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3179 if (gfc_match_eos () != MATCH_YES)
3182 if (inquire->unit != NULL && inquire->file != NULL)
3184 gfc_error ("INQUIRE statement at %L cannot contain both FILE and "
3185 "UNIT specifiers", &loc);
3189 if (inquire->unit == NULL && inquire->file == NULL)
3191 gfc_error ("INQUIRE statement at %L requires either FILE or "
3192 "UNIT specifier", &loc);
3196 if (gfc_pure (NULL))
3198 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3202 new_st.op = EXEC_INQUIRE;
3203 new_st.ext.inquire = inquire;
3207 gfc_syntax_error (ST_INQUIRE);
3210 gfc_free_inquire (inquire);
3215 /* Resolve everything in a gfc_inquire structure. */
3218 gfc_resolve_inquire (gfc_inquire *inquire)
3220 RESOLVE_TAG (&tag_unit, inquire->unit);
3221 RESOLVE_TAG (&tag_file, inquire->file);
3222 RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
3223 RESOLVE_TAG (&tag_iostat, inquire->iostat);
3224 RESOLVE_TAG (&tag_exist, inquire->exist);
3225 RESOLVE_TAG (&tag_opened, inquire->opened);
3226 RESOLVE_TAG (&tag_number, inquire->number);
3227 RESOLVE_TAG (&tag_named, inquire->named);
3228 RESOLVE_TAG (&tag_name, inquire->name);
3229 RESOLVE_TAG (&tag_s_access, inquire->access);
3230 RESOLVE_TAG (&tag_sequential, inquire->sequential);
3231 RESOLVE_TAG (&tag_direct, inquire->direct);
3232 RESOLVE_TAG (&tag_s_form, inquire->form);
3233 RESOLVE_TAG (&tag_formatted, inquire->formatted);
3234 RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
3235 RESOLVE_TAG (&tag_s_recl, inquire->recl);
3236 RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
3237 RESOLVE_TAG (&tag_s_blank, inquire->blank);
3238 RESOLVE_TAG (&tag_s_position, inquire->position);
3239 RESOLVE_TAG (&tag_s_action, inquire->action);
3240 RESOLVE_TAG (&tag_read, inquire->read);
3241 RESOLVE_TAG (&tag_write, inquire->write);
3242 RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
3243 RESOLVE_TAG (&tag_s_delim, inquire->delim);
3244 RESOLVE_TAG (&tag_s_pad, inquire->pad);
3245 RESOLVE_TAG (&tag_iolength, inquire->iolength);
3246 RESOLVE_TAG (&tag_convert, inquire->convert);
3247 RESOLVE_TAG (&tag_strm_out, inquire->strm_pos);
3249 if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)