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);
141 if (mode == MODE_COPY)
142 *format_string++ = c;
150 /* Back up one character position. Only works once. */
158 /* Eat up the spaces and return a character. */
161 next_char_not_space (void)
168 while (gfc_is_whitespace (c));
172 static int value = 0;
174 /* Simple lexical analyzer for getting the next token in a FORMAT
185 if (saved_token != FMT_NONE)
188 saved_token = FMT_NONE;
192 c = next_char_not_space ();
200 c = next_char_not_space ();
211 c = next_char_not_space ();
213 value = 10 * value + c - '0';
222 token = FMT_SIGNED_INT;
241 c = next_char_not_space ();
245 value = 10 * value + c - '0';
250 token = zflag ? FMT_ZERO : FMT_POSINT;
274 c = next_char_not_space ();
275 if (c != 'L' && c != 'R')
294 c = next_char_not_space ();
295 if (c != 'P' && c != 'S')
302 c = next_char_not_space ();
303 if (c == 'N' || c == 'Z')
364 c = next_char_not_space ();
365 if (c == 'N' || c == 'S')
408 /* Check a format statement. The format string, either from a FORMAT
409 statement or a constant in an I/O statement has already been parsed
410 by itself, and we are checking it for validity. The dual origin
411 means that the warning message is a little less than great. */
416 const char *posint_required = _("Positive width required");
417 const char *nonneg_required = _("Nonnegative width required");
418 const char *unexpected_element = _("Unexpected element");
419 const char *unexpected_end = _("Unexpected end of format string");
428 saved_token = FMT_NONE;
436 error = _("Missing leading left parenthesis");
442 goto finished; /* Empty format is legal */
446 /* In this state, the next thing has to be a format item. */
470 /* Signed integer can only precede a P format. */
474 error = _("Expected P edit descriptor");
481 /* P requires a prior number. */
482 error = _("P descriptor requires leading scale factor");
486 /* X requires a prior number if we're being pedantic. */
487 if (gfc_notify_std (GFC_STD_GNU, "Extension: X descriptor "
488 "requires leading space count at %C")
498 goto extension_optional_comma;
507 if (gfc_notify_std (GFC_STD_GNU, "Extension: $ descriptor at %C")
510 if (t != FMT_RPAREN || level > 0)
512 gfc_warning ("$ should be the last specifier in format at %C");
513 goto optional_comma_1;
533 error = unexpected_end;
537 error = unexpected_element;
542 /* In this state, t must currently be a data descriptor.
543 Deal with things that can/must follow the descriptor. */
557 error = _("Repeat count cannot follow P descriptor");
572 switch (gfc_notification_std (GFC_STD_GNU))
575 gfc_warning ("Extension: Missing positive width after L "
581 error = posint_required;
606 error = posint_required;
613 /* Warn if -std=legacy, otherwise error. */
614 if (gfc_option.warn_std != 0)
615 gfc_error_now ("Period required in format specifier at %C");
617 gfc_warning ("Period required in format specifier at %C");
623 if (u != FMT_ZERO && u != FMT_POSINT)
625 error = nonneg_required;
632 /* Look for optional exponent. */
643 error = _("Positive exponent width required");
652 if (t != FMT_ZERO && t != FMT_POSINT)
654 error = nonneg_required;
661 /* Warn if -std=legacy, otherwise error. */
662 if (gfc_option.warn_std != 0)
663 gfc_error_now ("Period required in format specifier at %C");
665 gfc_warning ("Period required in format specifier at %C");
671 if (t != FMT_ZERO && t != FMT_POSINT)
673 error = nonneg_required;
680 if(mode == MODE_STRING)
682 format_string += value;
683 format_length -= value;
697 if (t != FMT_ZERO && t != FMT_POSINT)
699 error = nonneg_required;
711 if (t != FMT_ZERO && t != FMT_POSINT)
713 error = nonneg_required;
721 error = unexpected_element;
726 /* Between a descriptor and what comes next. */
745 error = unexpected_end;
749 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
756 /* Optional comma is a weird between state where we've just finished
757 reading a colon, slash, dollar or P descriptor. */
772 /* Assume that we have another format item. */
779 extension_optional_comma:
780 /* As a GNU extension, permit a missing comma after a string literal. */
798 error = unexpected_end;
802 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
812 /* Something went wrong. If the format we're checking is a string,
813 generate a warning, since the program is correct. If the format
814 is in a FORMAT statement, this messes up parsing, which is an
816 if (mode != MODE_STRING)
817 gfc_error ("%s in format string at %C", error);
820 gfc_warning ("%s in format string at %C", error);
822 /* TODO: More elaborate measures are needed to show where a problem
823 is within a format string that has been calculated. */
833 /* Given an expression node that is a constant string, see if it looks
834 like a format string. */
837 check_format_string (gfc_expr *e)
840 format_string = e->value.character.string;
845 /************ Fortran 95 I/O statement matchers *************/
847 /* Match a FORMAT statement. This amounts to actually parsing the
848 format descriptors in order to correctly locate the end of the
852 gfc_match_format (void)
857 if (gfc_current_ns->proc_name
858 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
860 gfc_error ("Format statement in module main block at %C");
864 if (gfc_statement_label == NULL)
866 gfc_error ("Missing format label at %C");
869 gfc_gobble_whitespace ();
874 start = gfc_current_locus;
876 if (check_format () == FAILURE)
879 if (gfc_match_eos () != MATCH_YES)
881 gfc_syntax_error (ST_FORMAT);
885 /* The label doesn't get created until after the statement is done
886 being matched, so we have to leave the string for later. */
888 gfc_current_locus = start; /* Back to the beginning */
891 new_st.op = EXEC_NOP;
894 e->expr_type = EXPR_CONSTANT;
895 e->ts.type = BT_CHARACTER;
896 e->ts.kind = gfc_default_character_kind;
898 e->value.character.string = format_string = gfc_getmem (format_length + 1);
899 e->value.character.length = format_length;
900 gfc_statement_label->format = e;
903 check_format (); /* Guaranteed to succeed */
904 gfc_match_eos (); /* Guaranteed to succeed */
910 /* Match an expression I/O tag of some sort. */
913 match_etag (const io_tag *tag, gfc_expr **v)
918 m = gfc_match (tag->spec, &result);
924 gfc_error ("Duplicate %s specification at %C", tag->name);
925 gfc_free_expr (result);
934 /* Match a variable I/O tag of some sort. */
937 match_vtag (const io_tag *tag, gfc_expr **v)
942 m = gfc_match (tag->spec, &result);
948 gfc_error ("Duplicate %s specification at %C", tag->name);
949 gfc_free_expr (result);
953 if (result->symtree->n.sym->attr.intent == INTENT_IN)
955 gfc_error ("Variable tag cannot be INTENT(IN) at %C");
956 gfc_free_expr (result);
960 if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
962 gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
963 gfc_free_expr (result);
972 /* Match I/O tags that cause variables to become redefined. */
975 match_out_tag(const io_tag *tag, gfc_expr **result)
979 m = match_vtag(tag, result);
981 gfc_check_do_variable((*result)->symtree);
987 /* Match a label I/O tag. */
990 match_ltag (const io_tag *tag, gfc_st_label ** label)
996 m = gfc_match (tag->spec, label);
997 if (m == MATCH_YES && old != 0)
999 gfc_error ("Duplicate %s label specification at %C", tag->name);
1004 && gfc_reference_st_label (*label, ST_LABEL_TARGET) == FAILURE)
1011 /* Do expression resolution and type-checking on an expression tag. */
1014 resolve_tag (const io_tag *tag, gfc_expr *e)
1019 if (gfc_resolve_expr (e) == FAILURE)
1022 if (e->ts.type != tag->type && tag != &tag_format)
1024 gfc_error ("%s tag at %L must be of type %s", tag->name,
1025 &e->where, gfc_basic_typename (tag->type));
1029 if (tag == &tag_format)
1031 if (e->expr_type == EXPR_CONSTANT
1032 && (e->ts.type != BT_CHARACTER
1033 || e->ts.kind != gfc_default_character_kind))
1035 gfc_error ("Constant expression in FORMAT tag at %L must be "
1036 "of type default CHARACTER", &e->where);
1040 /* If e's rank is zero and e is not an element of an array, it should be
1041 of integer or character type. The integer variable should be
1043 if (e->symtree == NULL || e->symtree->n.sym->as == NULL
1044 || e->symtree->n.sym->as->rank == 0)
1046 if (e->ts.type != BT_CHARACTER && e->ts.type != BT_INTEGER)
1048 gfc_error ("%s tag at %L must be of type %s or %s", tag->name,
1049 &e->where, gfc_basic_typename (BT_CHARACTER),
1050 gfc_basic_typename (BT_INTEGER));
1053 else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
1055 if (gfc_notify_std (GFC_STD_F95_DEL, "Obsolete: ASSIGNED "
1056 "variable in FORMAT tag at %L", &e->where)
1059 if (e->symtree->n.sym->attr.assign != 1)
1061 gfc_error ("Variable '%s' at %L has not been assigned a "
1062 "format label", e->symtree->n.sym->name,
1067 else if (e->ts.type == BT_INTEGER)
1069 gfc_error ("scalar '%s' FORMAT tag at %L is not an ASSIGNED "
1070 "variable", gfc_basic_typename (e->ts.type),
1079 /* if rank is nonzero, we allow the type to be character under
1080 GFC_STD_GNU and other type under GFC_STD_LEGACY. It may be
1081 assigned an Hollerith constant. */
1082 if (e->ts.type == BT_CHARACTER)
1084 if (gfc_notify_std (GFC_STD_GNU, "Extension: Character array "
1085 "in FORMAT tag at %L", &e->where)
1091 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: Non-character "
1092 "in FORMAT tag at %L", &e->where)
1103 gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
1107 if (tag == &tag_iomsg)
1109 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IOMSG tag at %L",
1110 &e->where) == FAILURE)
1114 if (tag == &tag_iostat && e->ts.kind != gfc_default_integer_kind)
1116 if (gfc_notify_std (GFC_STD_GNU, "Fortran 95 requires default "
1117 "INTEGER in IOSTAT tag at %L", &e->where)
1122 if (tag == &tag_size && e->ts.kind != gfc_default_integer_kind)
1124 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1125 "INTEGER in SIZE tag at %L", &e->where)
1130 if (tag == &tag_convert)
1132 if (gfc_notify_std (GFC_STD_GNU, "Extension: CONVERT tag at %L",
1133 &e->where) == FAILURE)
1137 if (tag == &tag_iolength && e->ts.kind != gfc_default_integer_kind)
1139 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1140 "INTEGER in IOLENGTH tag at %L", &e->where)
1150 /* Match a single tag of an OPEN statement. */
1153 match_open_element (gfc_open *open)
1157 m = match_etag (&tag_unit, &open->unit);
1160 m = match_out_tag (&tag_iomsg, &open->iomsg);
1163 m = match_out_tag (&tag_iostat, &open->iostat);
1166 m = match_etag (&tag_file, &open->file);
1169 m = match_etag (&tag_status, &open->status);
1172 m = match_etag (&tag_e_access, &open->access);
1175 m = match_etag (&tag_e_form, &open->form);
1178 m = match_etag (&tag_e_recl, &open->recl);
1181 m = match_etag (&tag_e_blank, &open->blank);
1184 m = match_etag (&tag_e_position, &open->position);
1187 m = match_etag (&tag_e_action, &open->action);
1190 m = match_etag (&tag_e_delim, &open->delim);
1193 m = match_etag (&tag_e_pad, &open->pad);
1196 m = match_ltag (&tag_err, &open->err);
1199 m = match_etag (&tag_convert, &open->convert);
1207 /* Free the gfc_open structure and all the expressions it contains. */
1210 gfc_free_open (gfc_open *open)
1215 gfc_free_expr (open->unit);
1216 gfc_free_expr (open->iomsg);
1217 gfc_free_expr (open->iostat);
1218 gfc_free_expr (open->file);
1219 gfc_free_expr (open->status);
1220 gfc_free_expr (open->access);
1221 gfc_free_expr (open->form);
1222 gfc_free_expr (open->recl);
1223 gfc_free_expr (open->blank);
1224 gfc_free_expr (open->position);
1225 gfc_free_expr (open->action);
1226 gfc_free_expr (open->delim);
1227 gfc_free_expr (open->pad);
1228 gfc_free_expr (open->convert);
1233 /* Resolve everything in a gfc_open structure. */
1236 gfc_resolve_open (gfc_open *open)
1239 RESOLVE_TAG (&tag_unit, open->unit);
1240 RESOLVE_TAG (&tag_iomsg, open->iomsg);
1241 RESOLVE_TAG (&tag_iostat, open->iostat);
1242 RESOLVE_TAG (&tag_file, open->file);
1243 RESOLVE_TAG (&tag_status, open->status);
1244 RESOLVE_TAG (&tag_e_access, open->access);
1245 RESOLVE_TAG (&tag_e_form, open->form);
1246 RESOLVE_TAG (&tag_e_recl, open->recl);
1247 RESOLVE_TAG (&tag_e_blank, open->blank);
1248 RESOLVE_TAG (&tag_e_position, open->position);
1249 RESOLVE_TAG (&tag_e_action, open->action);
1250 RESOLVE_TAG (&tag_e_delim, open->delim);
1251 RESOLVE_TAG (&tag_e_pad, open->pad);
1252 RESOLVE_TAG (&tag_convert, open->convert);
1254 if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1261 /* Check if a given value for a SPECIFIER is either in the list of values
1262 allowed in F95 or F2003, issuing an error message and returning a zero
1263 value if it is not allowed. */
1266 compare_to_allowed_values (const char *specifier, const char *allowed[],
1267 const char *allowed_f2003[],
1268 const char *allowed_gnu[], char *value,
1269 const char *statement, bool warn)
1274 len = strlen (value);
1277 for (len--; len > 0; len--)
1278 if (value[len] != ' ')
1283 for (i = 0; allowed[i]; i++)
1284 if (len == strlen (allowed[i])
1285 && strncasecmp (value, allowed[i], strlen (allowed[i])) == 0)
1288 for (i = 0; allowed_f2003 && allowed_f2003[i]; i++)
1289 if (len == strlen (allowed_f2003[i])
1290 && strncasecmp (value, allowed_f2003[i], strlen (allowed_f2003[i]))
1293 notification n = gfc_notification_std (GFC_STD_F2003);
1295 if (n == WARNING || (warn && n == ERROR))
1297 gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1298 "has value '%s'", specifier, statement,
1305 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: %s specifier in "
1306 "%s statement at %C has value '%s'", specifier,
1307 statement, allowed_f2003[i]);
1315 for (i = 0; allowed_gnu && allowed_gnu[i]; i++)
1316 if (len == strlen (allowed_gnu[i])
1317 && strncasecmp (value, allowed_gnu[i], strlen (allowed_gnu[i])) == 0)
1319 notification n = gfc_notification_std (GFC_STD_GNU);
1321 if (n == WARNING || (warn && n == ERROR))
1323 gfc_warning ("Extension: %s specifier in %s statement at %C "
1324 "has value '%s'", specifier, statement,
1331 gfc_notify_std (GFC_STD_GNU, "Extension: %s specifier in "
1332 "%s statement at %C has value '%s'", specifier,
1333 statement, allowed_gnu[i]);
1343 gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1344 specifier, statement, value);
1349 gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1350 specifier, statement, value);
1356 /* Match an OPEN statement. */
1359 gfc_match_open (void)
1365 m = gfc_match_char ('(');
1369 open = gfc_getmem (sizeof (gfc_open));
1371 m = match_open_element (open);
1373 if (m == MATCH_ERROR)
1377 m = gfc_match_expr (&open->unit);
1380 if (m == MATCH_ERROR)
1386 if (gfc_match_char (')') == MATCH_YES)
1388 if (gfc_match_char (',') != MATCH_YES)
1391 m = match_open_element (open);
1392 if (m == MATCH_ERROR)
1398 if (gfc_match_eos () == MATCH_NO)
1401 if (gfc_pure (NULL))
1403 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1407 warn = (open->err || open->iostat) ? true : false;
1408 /* Checks on the ACCESS specifier. */
1409 if (open->access && open->access->expr_type == EXPR_CONSTANT)
1411 static const char *access_f95[] = { "SEQUENTIAL", "DIRECT", NULL };
1412 static const char *access_f2003[] = { "STREAM", NULL };
1413 static const char *access_gnu[] = { "APPEND", NULL };
1415 if (!compare_to_allowed_values ("ACCESS", access_f95, access_f2003,
1417 open->access->value.character.string,
1422 /* Checks on the ACTION specifier. */
1423 if (open->action && open->action->expr_type == EXPR_CONSTANT)
1425 static const char *action[] = { "READ", "WRITE", "READWRITE", NULL };
1427 if (!compare_to_allowed_values ("ACTION", action, NULL, NULL,
1428 open->action->value.character.string,
1433 /* Checks on the ASYNCHRONOUS specifier. */
1434 /* TODO: code is ready, just needs uncommenting when async I/O support
1436 if (open->asynchronous && open->asynchronous->expr_type == EXPR_CONSTANT)
1438 static const char * asynchronous[] = { "YES", "NO", NULL };
1440 if (!compare_to_allowed_values
1441 ("action", asynchronous, NULL, NULL,
1442 open->asynchronous->value.character.string, "OPEN", warn))
1446 /* Checks on the BLANK specifier. */
1447 if (open->blank && open->blank->expr_type == EXPR_CONSTANT)
1449 static const char *blank[] = { "ZERO", "NULL", NULL };
1451 if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
1452 open->blank->value.character.string,
1457 /* Checks on the DECIMAL specifier. */
1458 /* TODO: uncomment this code when DECIMAL support is added
1459 if (open->decimal && open->decimal->expr_type == EXPR_CONSTANT)
1461 static const char * decimal[] = { "COMMA", "POINT", NULL };
1463 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1464 open->decimal->value.character.string,
1469 /* Checks on the DELIM specifier. */
1470 if (open->delim && open->delim->expr_type == EXPR_CONSTANT)
1472 static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
1474 if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
1475 open->delim->value.character.string,
1480 /* Checks on the ENCODING specifier. */
1481 /* TODO: uncomment this code when ENCODING support is added
1482 if (open->encoding && open->encoding->expr_type == EXPR_CONSTANT)
1484 static const char * encoding[] = { "UTF-8", "DEFAULT", NULL };
1486 if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1487 open->encoding->value.character.string,
1492 /* Checks on the FORM specifier. */
1493 if (open->form && open->form->expr_type == EXPR_CONSTANT)
1495 static const char *form[] = { "FORMATTED", "UNFORMATTED", NULL };
1497 if (!compare_to_allowed_values ("FORM", form, NULL, NULL,
1498 open->form->value.character.string,
1503 /* Checks on the PAD specifier. */
1504 if (open->pad && open->pad->expr_type == EXPR_CONSTANT)
1506 static const char *pad[] = { "YES", "NO", NULL };
1508 if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
1509 open->pad->value.character.string,
1514 /* Checks on the POSITION specifier. */
1515 if (open->position && open->position->expr_type == EXPR_CONSTANT)
1517 static const char *position[] = { "ASIS", "REWIND", "APPEND", NULL };
1519 if (!compare_to_allowed_values ("POSITION", position, NULL, NULL,
1520 open->position->value.character.string,
1525 /* Checks on the ROUND specifier. */
1526 /* TODO: uncomment this code when ROUND support is added
1527 if (open->round && open->round->expr_type == EXPR_CONSTANT)
1529 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1530 "COMPATIBLE", "PROCESSOR_DEFINED", NULL };
1532 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1533 open->round->value.character.string,
1538 /* Checks on the SIGN specifier. */
1539 /* TODO: uncomment this code when SIGN support is added
1540 if (open->sign && open->sign->expr_type == EXPR_CONSTANT)
1542 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1545 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1546 open->sign->value.character.string,
1551 #define warn_or_error(...) \
1554 gfc_warning (__VA_ARGS__); \
1557 gfc_error (__VA_ARGS__); \
1562 /* Checks on the RECL specifier. */
1563 if (open->recl && open->recl->expr_type == EXPR_CONSTANT
1564 && open->recl->ts.type == BT_INTEGER
1565 && mpz_sgn (open->recl->value.integer) != 1)
1567 warn_or_error ("RECL in OPEN statement at %C must be positive");
1570 /* Checks on the STATUS specifier. */
1571 if (open->status && open->status->expr_type == EXPR_CONSTANT)
1573 static const char *status[] = { "OLD", "NEW", "SCRATCH",
1574 "REPLACE", "UNKNOWN", NULL };
1576 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1577 open->status->value.character.string,
1581 /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1582 the FILE= specifier shall appear. */
1583 if (open->file == NULL
1584 && (strncasecmp (open->status->value.character.string, "replace", 7)
1586 || strncasecmp (open->status->value.character.string, "new", 3)
1589 warn_or_error ("The STATUS specified in OPEN statement at %C is "
1590 "'%s' and no FILE specifier is present",
1591 open->status->value.character.string);
1594 /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1595 the FILE= specifier shall not appear. */
1596 if (strncasecmp (open->status->value.character.string, "scratch", 7)
1599 warn_or_error ("The STATUS specified in OPEN statement at %C "
1600 "cannot have the value SCRATCH if a FILE specifier "
1605 /* Things that are not allowed for unformatted I/O. */
1606 if (open->form && open->form->expr_type == EXPR_CONSTANT
1608 /* TODO uncomment this code when F2003 support is finished */
1609 /* || open->decimal || open->encoding || open->round
1611 || open->pad || open->blank)
1612 && strncasecmp (open->form->value.character.string,
1613 "unformatted", 11) == 0)
1615 const char *spec = (open->delim ? "DELIM "
1616 : (open->pad ? "PAD " : open->blank
1619 warn_or_error ("%s specifier at %C not allowed in OPEN statement for "
1620 "unformatted I/O", spec);
1623 if (open->recl && open->access && open->access->expr_type == EXPR_CONSTANT
1624 && strncasecmp (open->access->value.character.string, "stream", 6) == 0)
1626 warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
1631 && open->access && open->access->expr_type == EXPR_CONSTANT
1632 && !(strncasecmp (open->access->value.character.string,
1633 "sequential", 10) == 0
1634 || strncasecmp (open->access->value.character.string,
1636 || strncasecmp (open->access->value.character.string,
1639 warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
1640 "for stream or sequential ACCESS");
1643 #undef warn_or_error
1645 new_st.op = EXEC_OPEN;
1646 new_st.ext.open = open;
1650 gfc_syntax_error (ST_OPEN);
1653 gfc_free_open (open);
1658 /* Free a gfc_close structure an all its expressions. */
1661 gfc_free_close (gfc_close *close)
1666 gfc_free_expr (close->unit);
1667 gfc_free_expr (close->iomsg);
1668 gfc_free_expr (close->iostat);
1669 gfc_free_expr (close->status);
1674 /* Match elements of a CLOSE statement. */
1677 match_close_element (gfc_close *close)
1681 m = match_etag (&tag_unit, &close->unit);
1684 m = match_etag (&tag_status, &close->status);
1687 m = match_out_tag (&tag_iomsg, &close->iomsg);
1690 m = match_out_tag (&tag_iostat, &close->iostat);
1693 m = match_ltag (&tag_err, &close->err);
1701 /* Match a CLOSE statement. */
1704 gfc_match_close (void)
1710 m = gfc_match_char ('(');
1714 close = gfc_getmem (sizeof (gfc_close));
1716 m = match_close_element (close);
1718 if (m == MATCH_ERROR)
1722 m = gfc_match_expr (&close->unit);
1725 if (m == MATCH_ERROR)
1731 if (gfc_match_char (')') == MATCH_YES)
1733 if (gfc_match_char (',') != MATCH_YES)
1736 m = match_close_element (close);
1737 if (m == MATCH_ERROR)
1743 if (gfc_match_eos () == MATCH_NO)
1746 if (gfc_pure (NULL))
1748 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1752 warn = (close->iostat || close->err) ? true : false;
1754 /* Checks on the STATUS specifier. */
1755 if (close->status && close->status->expr_type == EXPR_CONSTANT)
1757 static const char *status[] = { "KEEP", "DELETE", NULL };
1759 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1760 close->status->value.character.string,
1765 new_st.op = EXEC_CLOSE;
1766 new_st.ext.close = close;
1770 gfc_syntax_error (ST_CLOSE);
1773 gfc_free_close (close);
1778 /* Resolve everything in a gfc_close structure. */
1781 gfc_resolve_close (gfc_close *close)
1783 RESOLVE_TAG (&tag_unit, close->unit);
1784 RESOLVE_TAG (&tag_iomsg, close->iomsg);
1785 RESOLVE_TAG (&tag_iostat, close->iostat);
1786 RESOLVE_TAG (&tag_status, close->status);
1788 if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
1795 /* Free a gfc_filepos structure. */
1798 gfc_free_filepos (gfc_filepos *fp)
1800 gfc_free_expr (fp->unit);
1801 gfc_free_expr (fp->iomsg);
1802 gfc_free_expr (fp->iostat);
1807 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
1810 match_file_element (gfc_filepos *fp)
1814 m = match_etag (&tag_unit, &fp->unit);
1817 m = match_out_tag (&tag_iomsg, &fp->iomsg);
1820 m = match_out_tag (&tag_iostat, &fp->iostat);
1823 m = match_ltag (&tag_err, &fp->err);
1831 /* Match the second half of the file-positioning statements, REWIND,
1832 BACKSPACE, ENDFILE, or the FLUSH statement. */
1835 match_filepos (gfc_statement st, gfc_exec_op op)
1840 fp = gfc_getmem (sizeof (gfc_filepos));
1842 if (gfc_match_char ('(') == MATCH_NO)
1844 m = gfc_match_expr (&fp->unit);
1845 if (m == MATCH_ERROR)
1853 m = match_file_element (fp);
1854 if (m == MATCH_ERROR)
1858 m = gfc_match_expr (&fp->unit);
1859 if (m == MATCH_ERROR)
1867 if (gfc_match_char (')') == MATCH_YES)
1869 if (gfc_match_char (',') != MATCH_YES)
1872 m = match_file_element (fp);
1873 if (m == MATCH_ERROR)
1880 if (gfc_match_eos () != MATCH_YES)
1883 if (gfc_pure (NULL))
1885 gfc_error ("%s statement not allowed in PURE procedure at %C",
1886 gfc_ascii_statement (st));
1892 new_st.ext.filepos = fp;
1896 gfc_syntax_error (st);
1899 gfc_free_filepos (fp);
1905 gfc_resolve_filepos (gfc_filepos *fp)
1907 RESOLVE_TAG (&tag_unit, fp->unit);
1908 RESOLVE_TAG (&tag_iostat, fp->iostat);
1909 RESOLVE_TAG (&tag_iomsg, fp->iomsg);
1910 if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
1917 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
1918 and the FLUSH statement. */
1921 gfc_match_endfile (void)
1923 return match_filepos (ST_END_FILE, EXEC_ENDFILE);
1927 gfc_match_backspace (void)
1929 return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
1933 gfc_match_rewind (void)
1935 return match_filepos (ST_REWIND, EXEC_REWIND);
1939 gfc_match_flush (void)
1941 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: FLUSH statement at %C")
1945 return match_filepos (ST_FLUSH, EXEC_FLUSH);
1948 /******************** Data Transfer Statements *********************/
1951 { M_READ, M_WRITE, M_PRINT, M_INQUIRE }
1955 /* Return a default unit number. */
1958 default_unit (io_kind k)
1967 return gfc_int_expr (unit);
1971 /* Match a unit specification for a data transfer statement. */
1974 match_dt_unit (io_kind k, gfc_dt *dt)
1978 if (gfc_match_char ('*') == MATCH_YES)
1980 if (dt->io_unit != NULL)
1983 dt->io_unit = default_unit (k);
1987 if (gfc_match_expr (&e) == MATCH_YES)
1989 if (dt->io_unit != NULL)
2002 gfc_error ("Duplicate UNIT specification at %C");
2007 /* Match a format specification. */
2010 match_dt_format (gfc_dt *dt)
2014 gfc_st_label *label;
2016 where = gfc_current_locus;
2018 if (gfc_match_char ('*') == MATCH_YES)
2020 if (dt->format_expr != NULL || dt->format_label != NULL)
2023 dt->format_label = &format_asterisk;
2027 if (gfc_match_st_label (&label) == MATCH_YES)
2029 if (dt->format_expr != NULL || dt->format_label != NULL)
2031 gfc_free_st_label (label);
2035 if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
2038 dt->format_label = label;
2042 if (gfc_match_expr (&e) == MATCH_YES)
2044 if (dt->format_expr != NULL || dt->format_label != NULL)
2049 dt->format_expr = e;
2053 gfc_current_locus = where; /* The only case where we have to restore */
2058 gfc_error ("Duplicate format specification at %C");
2063 /* Traverse a namelist that is part of a READ statement to make sure
2064 that none of the variables in the namelist are INTENT(IN). Returns
2065 nonzero if we find such a variable. */
2068 check_namelist (gfc_symbol *sym)
2072 for (p = sym->namelist; p; p = p->next)
2073 if (p->sym->attr.intent == INTENT_IN)
2075 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2076 p->sym->name, sym->name);
2084 /* Match a single data transfer element. */
2087 match_dt_element (io_kind k, gfc_dt *dt)
2089 char name[GFC_MAX_SYMBOL_LEN + 1];
2093 if (gfc_match (" unit =") == MATCH_YES)
2095 m = match_dt_unit (k, dt);
2100 if (gfc_match (" fmt =") == MATCH_YES)
2102 m = match_dt_format (dt);
2107 if (gfc_match (" nml = %n", name) == MATCH_YES)
2109 if (dt->namelist != NULL)
2111 gfc_error ("Duplicate NML specification at %C");
2115 if (gfc_find_symbol (name, NULL, 1, &sym))
2118 if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
2120 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2121 sym != NULL ? sym->name : name);
2126 if (k == M_READ && check_namelist (sym))
2132 m = match_etag (&tag_rec, &dt->rec);
2135 m = match_etag (&tag_spos, &dt->rec);
2138 m = match_out_tag (&tag_iomsg, &dt->iomsg);
2141 m = match_out_tag (&tag_iostat, &dt->iostat);
2144 m = match_ltag (&tag_err, &dt->err);
2146 dt->err_where = gfc_current_locus;
2149 m = match_etag (&tag_advance, &dt->advance);
2152 m = match_out_tag (&tag_size, &dt->size);
2156 m = match_ltag (&tag_end, &dt->end);
2161 gfc_error ("END tag at %C not allowed in output statement");
2164 dt->end_where = gfc_current_locus;
2169 m = match_ltag (&tag_eor, &dt->eor);
2171 dt->eor_where = gfc_current_locus;
2179 /* Free a data transfer structure and everything below it. */
2182 gfc_free_dt (gfc_dt *dt)
2187 gfc_free_expr (dt->io_unit);
2188 gfc_free_expr (dt->format_expr);
2189 gfc_free_expr (dt->rec);
2190 gfc_free_expr (dt->advance);
2191 gfc_free_expr (dt->iomsg);
2192 gfc_free_expr (dt->iostat);
2193 gfc_free_expr (dt->size);
2198 /* Resolve everything in a gfc_dt structure. */
2201 gfc_resolve_dt (gfc_dt *dt)
2205 RESOLVE_TAG (&tag_format, dt->format_expr);
2206 RESOLVE_TAG (&tag_rec, dt->rec);
2207 RESOLVE_TAG (&tag_spos, dt->rec);
2208 RESOLVE_TAG (&tag_advance, dt->advance);
2209 RESOLVE_TAG (&tag_iomsg, dt->iomsg);
2210 RESOLVE_TAG (&tag_iostat, dt->iostat);
2211 RESOLVE_TAG (&tag_size, dt->size);
2214 if (gfc_resolve_expr (e) == SUCCESS
2215 && (e->ts.type != BT_INTEGER
2216 && (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_VARIABLE)))
2218 gfc_error ("UNIT specification at %L must be an INTEGER expression "
2219 "or a CHARACTER variable", &e->where);
2223 if (e->ts.type == BT_CHARACTER)
2225 if (gfc_has_vector_index (e))
2227 gfc_error ("Internal unit with vector subscript at %L", &e->where);
2232 if (e->rank && e->ts.type != BT_CHARACTER)
2234 gfc_error ("External IO UNIT cannot be an array at %L", &e->where);
2240 if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
2242 if (dt->err->defined == ST_LABEL_UNKNOWN)
2244 gfc_error ("ERR tag label %d at %L not defined",
2245 dt->err->value, &dt->err_where);
2252 if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
2254 if (dt->end->defined == ST_LABEL_UNKNOWN)
2256 gfc_error ("END tag label %d at %L not defined",
2257 dt->end->value, &dt->end_where);
2264 if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
2266 if (dt->eor->defined == ST_LABEL_UNKNOWN)
2268 gfc_error ("EOR tag label %d at %L not defined",
2269 dt->eor->value, &dt->eor_where);
2274 /* Check the format label actually exists. */
2275 if (dt->format_label && dt->format_label != &format_asterisk
2276 && dt->format_label->defined == ST_LABEL_UNKNOWN)
2278 gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
2279 &dt->format_label->where);
2286 /* Given an io_kind, return its name. */
2289 io_kind_name (io_kind k)
2308 gfc_internal_error ("io_kind_name(): bad I/O-kind");
2315 /* Match an IO iteration statement of the form:
2317 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2319 which is equivalent to a single IO element. This function is
2320 mutually recursive with match_io_element(). */
2322 static match match_io_element (io_kind, gfc_code **);
2325 match_io_iterator (io_kind k, gfc_code **result)
2327 gfc_code *head, *tail, *new;
2335 old_loc = gfc_current_locus;
2337 if (gfc_match_char ('(') != MATCH_YES)
2340 m = match_io_element (k, &head);
2343 if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
2349 /* Can't be anything but an IO iterator. Build a list. */
2350 iter = gfc_get_iterator ();
2354 m = gfc_match_iterator (iter, 0);
2355 if (m == MATCH_ERROR)
2359 gfc_check_do_variable (iter->var->symtree);
2363 m = match_io_element (k, &new);
2364 if (m == MATCH_ERROR)
2373 tail = gfc_append_code (tail, new);
2375 if (gfc_match_char (',') != MATCH_YES)
2384 if (gfc_match_char (')') != MATCH_YES)
2387 new = gfc_get_code ();
2389 new->ext.iterator = iter;
2391 new->block = gfc_get_code ();
2392 new->block->op = EXEC_DO;
2393 new->block->next = head;
2399 gfc_error ("Syntax error in I/O iterator at %C");
2403 gfc_free_iterator (iter, 1);
2404 gfc_free_statements (head);
2405 gfc_current_locus = old_loc;
2410 /* Match a single element of an IO list, which is either a single
2411 expression or an IO Iterator. */
2414 match_io_element (io_kind k, gfc_code **cpp)
2422 m = match_io_iterator (k, cpp);
2428 m = gfc_match_variable (&expr, 0);
2430 gfc_error ("Expected variable in READ statement at %C");
2434 m = gfc_match_expr (&expr);
2436 gfc_error ("Expected expression in %s statement at %C",
2444 if (expr->symtree->n.sym->attr.intent == INTENT_IN)
2446 gfc_error ("Variable '%s' in input list at %C cannot be "
2447 "INTENT(IN)", expr->symtree->n.sym->name);
2452 && gfc_impure_variable (expr->symtree->n.sym)
2453 && current_dt->io_unit->ts.type == BT_CHARACTER)
2455 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2456 expr->symtree->n.sym->name);
2460 if (gfc_check_do_variable (expr->symtree))
2466 if (current_dt->io_unit->ts.type == BT_CHARACTER
2468 && current_dt->io_unit->expr_type == EXPR_VARIABLE
2469 && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
2471 gfc_error ("Cannot write to internal file unit '%s' at %C "
2472 "inside a PURE procedure",
2473 current_dt->io_unit->symtree->n.sym->name);
2485 gfc_free_expr (expr);
2489 cp = gfc_get_code ();
2490 cp->op = EXEC_TRANSFER;
2498 /* Match an I/O list, building gfc_code structures as we go. */
2501 match_io_list (io_kind k, gfc_code **head_p)
2503 gfc_code *head, *tail, *new;
2506 *head_p = head = tail = NULL;
2507 if (gfc_match_eos () == MATCH_YES)
2512 m = match_io_element (k, &new);
2513 if (m == MATCH_ERROR)
2518 tail = gfc_append_code (tail, new);
2522 if (gfc_match_eos () == MATCH_YES)
2524 if (gfc_match_char (',') != MATCH_YES)
2532 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2535 gfc_free_statements (head);
2540 /* Attach the data transfer end node. */
2543 terminate_io (gfc_code *io_code)
2547 if (io_code == NULL)
2548 io_code = new_st.block;
2550 c = gfc_get_code ();
2551 c->op = EXEC_DT_END;
2553 /* Point to structure that is already there */
2554 c->ext.dt = new_st.ext.dt;
2555 gfc_append_code (io_code, c);
2559 /* Check the constraints for a data transfer statement. The majority of the
2560 constraints appearing in 9.4 of the standard appear here. Some are handled
2561 in resolve_tag and others in gfc_resolve_dt. */
2564 check_io_constraints (io_kind k, gfc_dt *dt, gfc_code *io_code,
2567 #define io_constraint(condition,msg,arg)\
2570 gfc_error(msg,arg);\
2576 gfc_symbol *sym = NULL;
2581 if (expr && expr->expr_type == EXPR_VARIABLE
2582 && expr->ts.type == BT_CHARACTER)
2584 sym = expr->symtree->n.sym;
2586 io_constraint (k == M_WRITE && sym->attr.intent == INTENT_IN,
2587 "Internal file at %L must not be INTENT(IN)",
2590 io_constraint (gfc_has_vector_index (dt->io_unit),
2591 "Internal file incompatible with vector subscript at %L",
2594 io_constraint (dt->rec != NULL,
2595 "REC tag at %L is incompatible with internal file",
2598 if (dt->namelist != NULL)
2600 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Internal file "
2601 "at %L with namelist", &expr->where)
2606 io_constraint (dt->advance != NULL,
2607 "ADVANCE tag at %L is incompatible with internal file",
2608 &dt->advance->where);
2611 if (expr && expr->ts.type != BT_CHARACTER)
2614 io_constraint (gfc_pure (NULL) && (k == M_READ || k == M_WRITE),
2615 "IO UNIT in %s statement at %C must be "
2616 "an internal file in a PURE procedure",
2623 io_constraint (dt->end, "END tag not allowed with output at %L",
2626 io_constraint (dt->eor, "EOR tag not allowed with output at %L",
2629 io_constraint (k != M_READ && dt->size,
2630 "SIZE=specifier not allowed with output at %L",
2635 io_constraint (dt->size && dt->advance == NULL,
2636 "SIZE tag at %L requires an ADVANCE tag",
2639 io_constraint (dt->eor && dt->advance == NULL,
2640 "EOR tag at %L requires an ADVANCE tag",
2648 io_constraint (io_code && dt->namelist,
2649 "NAMELIST cannot be followed by IO-list at %L",
2652 io_constraint (dt->format_expr,
2653 "IO spec-list cannot contain both NAMELIST group name "
2654 "and format specification at %L.",
2655 &dt->format_expr->where);
2657 io_constraint (dt->format_label,
2658 "IO spec-list cannot contain both NAMELIST group name "
2659 "and format label at %L", spec_end);
2661 io_constraint (dt->rec,
2662 "NAMELIST IO is not allowed with a REC=specifier "
2663 "at %L.", &dt->rec->where);
2665 io_constraint (dt->advance,
2666 "NAMELIST IO is not allowed with a ADVANCE=specifier "
2667 "at %L.", &dt->advance->where);
2672 io_constraint (dt->end,
2673 "An END tag is not allowed with a "
2674 "REC=specifier at %L.", &dt->end_where);
2677 io_constraint (dt->format_label == &format_asterisk,
2678 "FMT=* is not allowed with a REC=specifier "
2679 "at %L.", spec_end);
2684 int not_yes, not_no;
2687 io_constraint (dt->format_label == &format_asterisk,
2688 "List directed format(*) is not allowed with a "
2689 "ADVANCE=specifier at %L.", &expr->where);
2691 io_constraint (dt->format_expr == NULL && dt->format_label == NULL
2692 && dt->namelist == NULL,
2693 "the ADVANCE=specifier at %L must appear with an "
2694 "explicit format expression", &expr->where);
2696 if (expr->expr_type == EXPR_CONSTANT && expr->ts.type == BT_CHARACTER)
2698 const char * advance = expr->value.character.string;
2699 not_no = strcasecmp (advance, "no") != 0;
2700 not_yes = strcasecmp (advance, "yes") != 0;
2708 io_constraint (not_no && not_yes,
2709 "ADVANCE=specifier at %L must have value = "
2710 "YES or NO.", &expr->where);
2712 io_constraint (dt->size && not_no && k == M_READ,
2713 "SIZE tag at %L requires an ADVANCE = 'NO'",
2716 io_constraint (dt->eor && not_no && k == M_READ,
2717 "EOR tag at %L requires an ADVANCE = 'NO'",
2721 expr = dt->format_expr;
2722 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
2723 check_format_string (expr);
2727 #undef io_constraint
2730 /* Match a READ, WRITE or PRINT statement. */
2733 match_io (io_kind k)
2735 char name[GFC_MAX_SYMBOL_LEN + 1];
2744 where = gfc_current_locus;
2746 current_dt = dt = gfc_getmem (sizeof (gfc_dt));
2747 m = gfc_match_char ('(');
2750 where = gfc_current_locus;
2753 else if (k == M_PRINT)
2755 /* Treat the non-standard case of PRINT namelist. */
2756 if ((gfc_current_form == FORM_FIXED || gfc_peek_char () == ' ')
2757 && gfc_match_name (name) == MATCH_YES)
2759 gfc_find_symbol (name, NULL, 1, &sym);
2760 if (sym && sym->attr.flavor == FL_NAMELIST)
2762 if (gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
2763 "%C is an extension") == FAILURE)
2769 dt->io_unit = default_unit (k);
2774 gfc_current_locus = where;
2778 if (gfc_current_form == FORM_FREE)
2780 c = gfc_peek_char();
2781 if (c != ' ' && c != '*' && c != '\'' && c != '"')
2788 m = match_dt_format (dt);
2789 if (m == MATCH_ERROR)
2795 dt->io_unit = default_unit (k);
2800 /* Before issuing an error for a malformed 'print (1,*)' type of
2801 error, check for a default-char-expr of the form ('(I0)'). */
2802 if (k == M_PRINT && m == MATCH_YES)
2804 /* Reset current locus to get the initial '(' in an expression. */
2805 gfc_current_locus = where;
2806 dt->format_expr = NULL;
2807 m = match_dt_format (dt);
2809 if (m == MATCH_ERROR)
2811 if (m == MATCH_NO || dt->format_expr == NULL)
2815 dt->io_unit = default_unit (k);
2820 /* Match a control list */
2821 if (match_dt_element (k, dt) == MATCH_YES)
2823 if (match_dt_unit (k, dt) != MATCH_YES)
2826 if (gfc_match_char (')') == MATCH_YES)
2828 if (gfc_match_char (',') != MATCH_YES)
2831 m = match_dt_element (k, dt);
2834 if (m == MATCH_ERROR)
2837 m = match_dt_format (dt);
2840 if (m == MATCH_ERROR)
2843 where = gfc_current_locus;
2845 m = gfc_match_name (name);
2848 gfc_find_symbol (name, NULL, 1, &sym);
2849 if (sym && sym->attr.flavor == FL_NAMELIST)
2852 if (k == M_READ && check_namelist (sym))
2861 gfc_current_locus = where;
2863 goto loop; /* No matches, try regular elements */
2866 if (gfc_match_char (')') == MATCH_YES)
2868 if (gfc_match_char (',') != MATCH_YES)
2874 m = match_dt_element (k, dt);
2877 if (m == MATCH_ERROR)
2880 if (gfc_match_char (')') == MATCH_YES)
2882 if (gfc_match_char (',') != MATCH_YES)
2888 /* Used in check_io_constraints, where no locus is available. */
2889 spec_end = gfc_current_locus;
2891 /* Optional leading comma (non-standard). */
2893 && gfc_match_char (',') == MATCH_YES
2895 && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before output "
2896 "item list at %C is an extension") == FAILURE)
2900 if (gfc_match_eos () != MATCH_YES)
2902 if (comma_flag && gfc_match_char (',') != MATCH_YES)
2904 gfc_error ("Expected comma in I/O list at %C");
2909 m = match_io_list (k, &io_code);
2910 if (m == MATCH_ERROR)
2916 /* A full IO statement has been matched. Check the constraints. spec_end is
2917 supplied for cases where no locus is supplied. */
2918 m = check_io_constraints (k, dt, io_code, &spec_end);
2920 if (m == MATCH_ERROR)
2923 new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
2925 new_st.block = gfc_get_code ();
2926 new_st.block->op = new_st.op;
2927 new_st.block->next = io_code;
2929 terminate_io (io_code);
2934 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2944 gfc_match_read (void)
2946 return match_io (M_READ);
2950 gfc_match_write (void)
2952 return match_io (M_WRITE);
2956 gfc_match_print (void)
2960 m = match_io (M_PRINT);
2964 if (gfc_pure (NULL))
2966 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
2974 /* Free a gfc_inquire structure. */
2977 gfc_free_inquire (gfc_inquire *inquire)
2980 if (inquire == NULL)
2983 gfc_free_expr (inquire->unit);
2984 gfc_free_expr (inquire->file);
2985 gfc_free_expr (inquire->iomsg);
2986 gfc_free_expr (inquire->iostat);
2987 gfc_free_expr (inquire->exist);
2988 gfc_free_expr (inquire->opened);
2989 gfc_free_expr (inquire->number);
2990 gfc_free_expr (inquire->named);
2991 gfc_free_expr (inquire->name);
2992 gfc_free_expr (inquire->access);
2993 gfc_free_expr (inquire->sequential);
2994 gfc_free_expr (inquire->direct);
2995 gfc_free_expr (inquire->form);
2996 gfc_free_expr (inquire->formatted);
2997 gfc_free_expr (inquire->unformatted);
2998 gfc_free_expr (inquire->recl);
2999 gfc_free_expr (inquire->nextrec);
3000 gfc_free_expr (inquire->blank);
3001 gfc_free_expr (inquire->position);
3002 gfc_free_expr (inquire->action);
3003 gfc_free_expr (inquire->read);
3004 gfc_free_expr (inquire->write);
3005 gfc_free_expr (inquire->readwrite);
3006 gfc_free_expr (inquire->delim);
3007 gfc_free_expr (inquire->pad);
3008 gfc_free_expr (inquire->iolength);
3009 gfc_free_expr (inquire->convert);
3010 gfc_free_expr (inquire->strm_pos);
3015 /* Match an element of an INQUIRE statement. */
3017 #define RETM if (m != MATCH_NO) return m;
3020 match_inquire_element (gfc_inquire *inquire)
3024 m = match_etag (&tag_unit, &inquire->unit);
3025 RETM m = match_etag (&tag_file, &inquire->file);
3026 RETM m = match_ltag (&tag_err, &inquire->err);
3027 RETM m = match_out_tag (&tag_iomsg, &inquire->iomsg);
3028 RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
3029 RETM m = match_vtag (&tag_exist, &inquire->exist);
3030 RETM m = match_vtag (&tag_opened, &inquire->opened);
3031 RETM m = match_vtag (&tag_named, &inquire->named);
3032 RETM m = match_vtag (&tag_name, &inquire->name);
3033 RETM m = match_out_tag (&tag_number, &inquire->number);
3034 RETM m = match_vtag (&tag_s_access, &inquire->access);
3035 RETM m = match_vtag (&tag_sequential, &inquire->sequential);
3036 RETM m = match_vtag (&tag_direct, &inquire->direct);
3037 RETM m = match_vtag (&tag_s_form, &inquire->form);
3038 RETM m = match_vtag (&tag_formatted, &inquire->formatted);
3039 RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
3040 RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
3041 RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
3042 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
3043 RETM m = match_vtag (&tag_s_position, &inquire->position);
3044 RETM m = match_vtag (&tag_s_action, &inquire->action);
3045 RETM m = match_vtag (&tag_read, &inquire->read);
3046 RETM m = match_vtag (&tag_write, &inquire->write);
3047 RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
3048 RETM m = match_vtag (&tag_s_delim, &inquire->delim);
3049 RETM m = match_vtag (&tag_s_pad, &inquire->pad);
3050 RETM m = match_vtag (&tag_iolength, &inquire->iolength);
3051 RETM m = match_vtag (&tag_convert, &inquire->convert);
3052 RETM m = match_out_tag (&tag_strm_out, &inquire->strm_pos);
3053 RETM return MATCH_NO;
3060 gfc_match_inquire (void)
3062 gfc_inquire *inquire;
3067 m = gfc_match_char ('(');
3071 inquire = gfc_getmem (sizeof (gfc_inquire));
3073 loc = gfc_current_locus;
3075 m = match_inquire_element (inquire);
3076 if (m == MATCH_ERROR)
3080 m = gfc_match_expr (&inquire->unit);
3081 if (m == MATCH_ERROR)
3087 /* See if we have the IOLENGTH form of the inquire statement. */
3088 if (inquire->iolength != NULL)
3090 if (gfc_match_char (')') != MATCH_YES)
3093 m = match_io_list (M_INQUIRE, &code);
3094 if (m == MATCH_ERROR)
3099 new_st.op = EXEC_IOLENGTH;
3100 new_st.expr = inquire->iolength;
3101 new_st.ext.inquire = inquire;
3103 if (gfc_pure (NULL))
3105 gfc_free_statements (code);
3106 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3110 new_st.block = gfc_get_code ();
3111 new_st.block->op = EXEC_IOLENGTH;
3112 terminate_io (code);
3113 new_st.block->next = code;
3117 /* At this point, we have the non-IOLENGTH inquire statement. */
3120 if (gfc_match_char (')') == MATCH_YES)
3122 if (gfc_match_char (',') != MATCH_YES)
3125 m = match_inquire_element (inquire);
3126 if (m == MATCH_ERROR)
3131 if (inquire->iolength != NULL)
3133 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3138 if (gfc_match_eos () != MATCH_YES)
3141 if (inquire->unit != NULL && inquire->file != NULL)
3143 gfc_error ("INQUIRE statement at %L cannot contain both FILE and "
3144 "UNIT specifiers", &loc);
3148 if (inquire->unit == NULL && inquire->file == NULL)
3150 gfc_error ("INQUIRE statement at %L requires either FILE or "
3151 "UNIT specifier", &loc);
3155 if (gfc_pure (NULL))
3157 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3161 new_st.op = EXEC_INQUIRE;
3162 new_st.ext.inquire = inquire;
3166 gfc_syntax_error (ST_INQUIRE);
3169 gfc_free_inquire (inquire);
3174 /* Resolve everything in a gfc_inquire structure. */
3177 gfc_resolve_inquire (gfc_inquire *inquire)
3179 RESOLVE_TAG (&tag_unit, inquire->unit);
3180 RESOLVE_TAG (&tag_file, inquire->file);
3181 RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
3182 RESOLVE_TAG (&tag_iostat, inquire->iostat);
3183 RESOLVE_TAG (&tag_exist, inquire->exist);
3184 RESOLVE_TAG (&tag_opened, inquire->opened);
3185 RESOLVE_TAG (&tag_number, inquire->number);
3186 RESOLVE_TAG (&tag_named, inquire->named);
3187 RESOLVE_TAG (&tag_name, inquire->name);
3188 RESOLVE_TAG (&tag_s_access, inquire->access);
3189 RESOLVE_TAG (&tag_sequential, inquire->sequential);
3190 RESOLVE_TAG (&tag_direct, inquire->direct);
3191 RESOLVE_TAG (&tag_s_form, inquire->form);
3192 RESOLVE_TAG (&tag_formatted, inquire->formatted);
3193 RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
3194 RESOLVE_TAG (&tag_s_recl, inquire->recl);
3195 RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
3196 RESOLVE_TAG (&tag_s_blank, inquire->blank);
3197 RESOLVE_TAG (&tag_s_position, inquire->position);
3198 RESOLVE_TAG (&tag_s_action, inquire->action);
3199 RESOLVE_TAG (&tag_read, inquire->read);
3200 RESOLVE_TAG (&tag_write, inquire->write);
3201 RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
3202 RESOLVE_TAG (&tag_s_delim, inquire->delim);
3203 RESOLVE_TAG (&tag_s_pad, inquire->pad);
3204 RESOLVE_TAG (&tag_iolength, inquire->iolength);
3205 RESOLVE_TAG (&tag_convert, inquire->convert);
3206 RESOLVE_TAG (&tag_strm_out, inquire->strm_pos);
3208 if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)