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;
572 error = unexpected_end;
576 error = unexpected_element;
581 /* In this state, t must currently be a data descriptor.
582 Deal with things that can/must follow the descriptor. */
596 error = _("Repeat count cannot follow P descriptor");
611 switch (gfc_notification_std (GFC_STD_GNU))
614 gfc_warning ("Extension: Missing positive width after L "
620 error = posint_required;
645 error = posint_required;
652 /* Warn if -std=legacy, otherwise error. */
653 if (gfc_option.warn_std != 0)
654 gfc_error_now ("Period required in format specifier at %C");
656 gfc_warning ("Period required in format specifier at %C");
662 if (u != FMT_ZERO && u != FMT_POSINT)
664 error = nonneg_required;
671 /* Look for optional exponent. */
682 error = _("Positive exponent width required");
691 if (t != FMT_ZERO && t != FMT_POSINT)
693 error = nonneg_required;
700 /* Warn if -std=legacy, otherwise error. */
701 if (gfc_option.warn_std != 0)
702 gfc_error_now ("Period required in format specifier at %C");
704 gfc_warning ("Period required in format specifier at %C");
710 if (t != FMT_ZERO && t != FMT_POSINT)
712 error = nonneg_required;
719 if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
720 gfc_warning ("The H format specifier at %C is"
721 " a Fortran 95 deleted feature");
723 if(mode == MODE_STRING)
725 format_string += value;
726 format_length -= value;
740 if (t != FMT_ZERO && t != FMT_POSINT)
742 error = nonneg_required;
754 if (t != FMT_ZERO && t != FMT_POSINT)
756 error = nonneg_required;
764 error = unexpected_element;
769 /* Between a descriptor and what comes next. */
788 error = unexpected_end;
792 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
799 /* Optional comma is a weird between state where we've just finished
800 reading a colon, slash, dollar or P descriptor. */
815 /* Assume that we have another format item. */
822 extension_optional_comma:
823 /* As a GNU extension, permit a missing comma after a string literal. */
841 error = unexpected_end;
845 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
855 /* Something went wrong. If the format we're checking is a string,
856 generate a warning, since the program is correct. If the format
857 is in a FORMAT statement, this messes up parsing, which is an
859 if (mode != MODE_STRING)
860 gfc_error ("%s in format string at %C", error);
863 gfc_warning ("%s in format string at %C", error);
865 /* TODO: More elaborate measures are needed to show where a problem
866 is within a format string that has been calculated. */
876 /* Given an expression node that is a constant string, see if it looks
877 like a format string. */
880 check_format_string (gfc_expr *e)
883 format_string = e->value.character.string;
888 /************ Fortran 95 I/O statement matchers *************/
890 /* Match a FORMAT statement. This amounts to actually parsing the
891 format descriptors in order to correctly locate the end of the
895 gfc_match_format (void)
900 if (gfc_current_ns->proc_name
901 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
903 gfc_error ("Format statement in module main block at %C");
907 if (gfc_statement_label == NULL)
909 gfc_error ("Missing format label at %C");
912 gfc_gobble_whitespace ();
917 start = gfc_current_locus;
919 if (check_format () == FAILURE)
922 if (gfc_match_eos () != MATCH_YES)
924 gfc_syntax_error (ST_FORMAT);
928 /* The label doesn't get created until after the statement is done
929 being matched, so we have to leave the string for later. */
931 gfc_current_locus = start; /* Back to the beginning */
934 new_st.op = EXEC_NOP;
937 e->expr_type = EXPR_CONSTANT;
938 e->ts.type = BT_CHARACTER;
939 e->ts.kind = gfc_default_character_kind;
941 e->value.character.string = format_string = gfc_getmem (format_length + 1);
942 e->value.character.length = format_length;
943 gfc_statement_label->format = e;
946 check_format (); /* Guaranteed to succeed */
947 gfc_match_eos (); /* Guaranteed to succeed */
953 /* Match an expression I/O tag of some sort. */
956 match_etag (const io_tag *tag, gfc_expr **v)
961 m = gfc_match (tag->spec, &result);
967 gfc_error ("Duplicate %s specification at %C", tag->name);
968 gfc_free_expr (result);
977 /* Match a variable I/O tag of some sort. */
980 match_vtag (const io_tag *tag, gfc_expr **v)
985 m = gfc_match (tag->spec, &result);
991 gfc_error ("Duplicate %s specification at %C", tag->name);
992 gfc_free_expr (result);
996 if (result->symtree->n.sym->attr.intent == INTENT_IN)
998 gfc_error ("Variable tag cannot be INTENT(IN) at %C");
999 gfc_free_expr (result);
1003 if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
1005 gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
1006 gfc_free_expr (result);
1015 /* Match I/O tags that cause variables to become redefined. */
1018 match_out_tag(const io_tag *tag, gfc_expr **result)
1022 m = match_vtag(tag, result);
1024 gfc_check_do_variable((*result)->symtree);
1030 /* Match a label I/O tag. */
1033 match_ltag (const io_tag *tag, gfc_st_label ** label)
1039 m = gfc_match (tag->spec, label);
1040 if (m == MATCH_YES && old != 0)
1042 gfc_error ("Duplicate %s label specification at %C", tag->name);
1047 && gfc_reference_st_label (*label, ST_LABEL_TARGET) == FAILURE)
1054 /* Do expression resolution and type-checking on an expression tag. */
1057 resolve_tag (const io_tag *tag, gfc_expr *e)
1062 if (gfc_resolve_expr (e) == FAILURE)
1065 if (e->ts.type != tag->type && tag != &tag_format)
1067 gfc_error ("%s tag at %L must be of type %s", tag->name,
1068 &e->where, gfc_basic_typename (tag->type));
1072 if (tag == &tag_format)
1074 if (e->expr_type == EXPR_CONSTANT
1075 && (e->ts.type != BT_CHARACTER
1076 || e->ts.kind != gfc_default_character_kind))
1078 gfc_error ("Constant expression in FORMAT tag at %L must be "
1079 "of type default CHARACTER", &e->where);
1083 /* If e's rank is zero and e is not an element of an array, it should be
1084 of integer or character type. The integer variable should be
1086 if (e->symtree == NULL || e->symtree->n.sym->as == NULL
1087 || e->symtree->n.sym->as->rank == 0)
1089 if (e->ts.type != BT_CHARACTER && e->ts.type != BT_INTEGER)
1091 gfc_error ("%s tag at %L must be of type %s or %s", tag->name,
1092 &e->where, gfc_basic_typename (BT_CHARACTER),
1093 gfc_basic_typename (BT_INTEGER));
1096 else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
1098 if (gfc_notify_std (GFC_STD_F95_DEL, "Obsolete: ASSIGNED "
1099 "variable in FORMAT tag at %L", &e->where)
1102 if (e->symtree->n.sym->attr.assign != 1)
1104 gfc_error ("Variable '%s' at %L has not been assigned a "
1105 "format label", e->symtree->n.sym->name,
1110 else if (e->ts.type == BT_INTEGER)
1112 gfc_error ("scalar '%s' FORMAT tag at %L is not an ASSIGNED "
1113 "variable", gfc_basic_typename (e->ts.type),
1122 /* if rank is nonzero, we allow the type to be character under
1123 GFC_STD_GNU and other type under GFC_STD_LEGACY. It may be
1124 assigned an Hollerith constant. */
1125 if (e->ts.type == BT_CHARACTER)
1127 if (gfc_notify_std (GFC_STD_GNU, "Extension: Character array "
1128 "in FORMAT tag at %L", &e->where)
1134 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: Non-character "
1135 "in FORMAT tag at %L", &e->where)
1146 gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
1150 if (tag == &tag_iomsg)
1152 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IOMSG tag at %L",
1153 &e->where) == FAILURE)
1157 if (tag == &tag_iostat && e->ts.kind != gfc_default_integer_kind)
1159 if (gfc_notify_std (GFC_STD_GNU, "Fortran 95 requires default "
1160 "INTEGER in IOSTAT tag at %L", &e->where)
1165 if (tag == &tag_size && e->ts.kind != gfc_default_integer_kind)
1167 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1168 "INTEGER in SIZE tag at %L", &e->where)
1173 if (tag == &tag_convert)
1175 if (gfc_notify_std (GFC_STD_GNU, "Extension: CONVERT tag at %L",
1176 &e->where) == FAILURE)
1180 if (tag == &tag_iolength && e->ts.kind != gfc_default_integer_kind)
1182 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1183 "INTEGER in IOLENGTH tag at %L", &e->where)
1193 /* Match a single tag of an OPEN statement. */
1196 match_open_element (gfc_open *open)
1200 m = match_etag (&tag_unit, &open->unit);
1203 m = match_out_tag (&tag_iomsg, &open->iomsg);
1206 m = match_out_tag (&tag_iostat, &open->iostat);
1209 m = match_etag (&tag_file, &open->file);
1212 m = match_etag (&tag_status, &open->status);
1215 m = match_etag (&tag_e_access, &open->access);
1218 m = match_etag (&tag_e_form, &open->form);
1221 m = match_etag (&tag_e_recl, &open->recl);
1224 m = match_etag (&tag_e_blank, &open->blank);
1227 m = match_etag (&tag_e_position, &open->position);
1230 m = match_etag (&tag_e_action, &open->action);
1233 m = match_etag (&tag_e_delim, &open->delim);
1236 m = match_etag (&tag_e_pad, &open->pad);
1239 m = match_ltag (&tag_err, &open->err);
1242 m = match_etag (&tag_convert, &open->convert);
1250 /* Free the gfc_open structure and all the expressions it contains. */
1253 gfc_free_open (gfc_open *open)
1258 gfc_free_expr (open->unit);
1259 gfc_free_expr (open->iomsg);
1260 gfc_free_expr (open->iostat);
1261 gfc_free_expr (open->file);
1262 gfc_free_expr (open->status);
1263 gfc_free_expr (open->access);
1264 gfc_free_expr (open->form);
1265 gfc_free_expr (open->recl);
1266 gfc_free_expr (open->blank);
1267 gfc_free_expr (open->position);
1268 gfc_free_expr (open->action);
1269 gfc_free_expr (open->delim);
1270 gfc_free_expr (open->pad);
1271 gfc_free_expr (open->convert);
1276 /* Resolve everything in a gfc_open structure. */
1279 gfc_resolve_open (gfc_open *open)
1282 RESOLVE_TAG (&tag_unit, open->unit);
1283 RESOLVE_TAG (&tag_iomsg, open->iomsg);
1284 RESOLVE_TAG (&tag_iostat, open->iostat);
1285 RESOLVE_TAG (&tag_file, open->file);
1286 RESOLVE_TAG (&tag_status, open->status);
1287 RESOLVE_TAG (&tag_e_access, open->access);
1288 RESOLVE_TAG (&tag_e_form, open->form);
1289 RESOLVE_TAG (&tag_e_recl, open->recl);
1290 RESOLVE_TAG (&tag_e_blank, open->blank);
1291 RESOLVE_TAG (&tag_e_position, open->position);
1292 RESOLVE_TAG (&tag_e_action, open->action);
1293 RESOLVE_TAG (&tag_e_delim, open->delim);
1294 RESOLVE_TAG (&tag_e_pad, open->pad);
1295 RESOLVE_TAG (&tag_convert, open->convert);
1297 if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1304 /* Check if a given value for a SPECIFIER is either in the list of values
1305 allowed in F95 or F2003, issuing an error message and returning a zero
1306 value if it is not allowed. */
1309 compare_to_allowed_values (const char *specifier, const char *allowed[],
1310 const char *allowed_f2003[],
1311 const char *allowed_gnu[], char *value,
1312 const char *statement, bool warn)
1317 len = strlen (value);
1320 for (len--; len > 0; len--)
1321 if (value[len] != ' ')
1326 for (i = 0; allowed[i]; i++)
1327 if (len == strlen (allowed[i])
1328 && strncasecmp (value, allowed[i], strlen (allowed[i])) == 0)
1331 for (i = 0; allowed_f2003 && allowed_f2003[i]; i++)
1332 if (len == strlen (allowed_f2003[i])
1333 && strncasecmp (value, allowed_f2003[i], strlen (allowed_f2003[i]))
1336 notification n = gfc_notification_std (GFC_STD_F2003);
1338 if (n == WARNING || (warn && n == ERROR))
1340 gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1341 "has value '%s'", specifier, statement,
1348 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: %s specifier in "
1349 "%s statement at %C has value '%s'", specifier,
1350 statement, allowed_f2003[i]);
1358 for (i = 0; allowed_gnu && allowed_gnu[i]; i++)
1359 if (len == strlen (allowed_gnu[i])
1360 && strncasecmp (value, allowed_gnu[i], strlen (allowed_gnu[i])) == 0)
1362 notification n = gfc_notification_std (GFC_STD_GNU);
1364 if (n == WARNING || (warn && n == ERROR))
1366 gfc_warning ("Extension: %s specifier in %s statement at %C "
1367 "has value '%s'", specifier, statement,
1374 gfc_notify_std (GFC_STD_GNU, "Extension: %s specifier in "
1375 "%s statement at %C has value '%s'", specifier,
1376 statement, allowed_gnu[i]);
1386 gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1387 specifier, statement, value);
1392 gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1393 specifier, statement, value);
1399 /* Match an OPEN statement. */
1402 gfc_match_open (void)
1408 m = gfc_match_char ('(');
1412 open = gfc_getmem (sizeof (gfc_open));
1414 m = match_open_element (open);
1416 if (m == MATCH_ERROR)
1420 m = gfc_match_expr (&open->unit);
1423 if (m == MATCH_ERROR)
1429 if (gfc_match_char (')') == MATCH_YES)
1431 if (gfc_match_char (',') != MATCH_YES)
1434 m = match_open_element (open);
1435 if (m == MATCH_ERROR)
1441 if (gfc_match_eos () == MATCH_NO)
1444 if (gfc_pure (NULL))
1446 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1450 warn = (open->err || open->iostat) ? true : false;
1451 /* Checks on the ACCESS specifier. */
1452 if (open->access && open->access->expr_type == EXPR_CONSTANT)
1454 static const char *access_f95[] = { "SEQUENTIAL", "DIRECT", NULL };
1455 static const char *access_f2003[] = { "STREAM", NULL };
1456 static const char *access_gnu[] = { "APPEND", NULL };
1458 if (!compare_to_allowed_values ("ACCESS", access_f95, access_f2003,
1460 open->access->value.character.string,
1465 /* Checks on the ACTION specifier. */
1466 if (open->action && open->action->expr_type == EXPR_CONSTANT)
1468 static const char *action[] = { "READ", "WRITE", "READWRITE", NULL };
1470 if (!compare_to_allowed_values ("ACTION", action, NULL, NULL,
1471 open->action->value.character.string,
1476 /* Checks on the ASYNCHRONOUS specifier. */
1477 /* TODO: code is ready, just needs uncommenting when async I/O support
1479 if (open->asynchronous && open->asynchronous->expr_type == EXPR_CONSTANT)
1481 static const char * asynchronous[] = { "YES", "NO", NULL };
1483 if (!compare_to_allowed_values
1484 ("action", asynchronous, NULL, NULL,
1485 open->asynchronous->value.character.string, "OPEN", warn))
1489 /* Checks on the BLANK specifier. */
1490 if (open->blank && open->blank->expr_type == EXPR_CONSTANT)
1492 static const char *blank[] = { "ZERO", "NULL", NULL };
1494 if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
1495 open->blank->value.character.string,
1500 /* Checks on the DECIMAL specifier. */
1501 /* TODO: uncomment this code when DECIMAL support is added
1502 if (open->decimal && open->decimal->expr_type == EXPR_CONSTANT)
1504 static const char * decimal[] = { "COMMA", "POINT", NULL };
1506 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1507 open->decimal->value.character.string,
1512 /* Checks on the DELIM specifier. */
1513 if (open->delim && open->delim->expr_type == EXPR_CONSTANT)
1515 static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
1517 if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
1518 open->delim->value.character.string,
1523 /* Checks on the ENCODING specifier. */
1524 /* TODO: uncomment this code when ENCODING support is added
1525 if (open->encoding && open->encoding->expr_type == EXPR_CONSTANT)
1527 static const char * encoding[] = { "UTF-8", "DEFAULT", NULL };
1529 if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1530 open->encoding->value.character.string,
1535 /* Checks on the FORM specifier. */
1536 if (open->form && open->form->expr_type == EXPR_CONSTANT)
1538 static const char *form[] = { "FORMATTED", "UNFORMATTED", NULL };
1540 if (!compare_to_allowed_values ("FORM", form, NULL, NULL,
1541 open->form->value.character.string,
1546 /* Checks on the PAD specifier. */
1547 if (open->pad && open->pad->expr_type == EXPR_CONSTANT)
1549 static const char *pad[] = { "YES", "NO", NULL };
1551 if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
1552 open->pad->value.character.string,
1557 /* Checks on the POSITION specifier. */
1558 if (open->position && open->position->expr_type == EXPR_CONSTANT)
1560 static const char *position[] = { "ASIS", "REWIND", "APPEND", NULL };
1562 if (!compare_to_allowed_values ("POSITION", position, NULL, NULL,
1563 open->position->value.character.string,
1568 /* Checks on the ROUND specifier. */
1569 /* TODO: uncomment this code when ROUND support is added
1570 if (open->round && open->round->expr_type == EXPR_CONSTANT)
1572 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1573 "COMPATIBLE", "PROCESSOR_DEFINED", NULL };
1575 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1576 open->round->value.character.string,
1581 /* Checks on the SIGN specifier. */
1582 /* TODO: uncomment this code when SIGN support is added
1583 if (open->sign && open->sign->expr_type == EXPR_CONSTANT)
1585 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1588 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1589 open->sign->value.character.string,
1594 #define warn_or_error(...) \
1597 gfc_warning (__VA_ARGS__); \
1600 gfc_error (__VA_ARGS__); \
1605 /* Checks on the RECL specifier. */
1606 if (open->recl && open->recl->expr_type == EXPR_CONSTANT
1607 && open->recl->ts.type == BT_INTEGER
1608 && mpz_sgn (open->recl->value.integer) != 1)
1610 warn_or_error ("RECL in OPEN statement at %C must be positive");
1613 /* Checks on the STATUS specifier. */
1614 if (open->status && open->status->expr_type == EXPR_CONSTANT)
1616 static const char *status[] = { "OLD", "NEW", "SCRATCH",
1617 "REPLACE", "UNKNOWN", NULL };
1619 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1620 open->status->value.character.string,
1624 /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1625 the FILE= specifier shall appear. */
1626 if (open->file == NULL
1627 && (strncasecmp (open->status->value.character.string, "replace", 7)
1629 || strncasecmp (open->status->value.character.string, "new", 3)
1632 warn_or_error ("The STATUS specified in OPEN statement at %C is "
1633 "'%s' and no FILE specifier is present",
1634 open->status->value.character.string);
1637 /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1638 the FILE= specifier shall not appear. */
1639 if (strncasecmp (open->status->value.character.string, "scratch", 7)
1642 warn_or_error ("The STATUS specified in OPEN statement at %C "
1643 "cannot have the value SCRATCH if a FILE specifier "
1648 /* Things that are not allowed for unformatted I/O. */
1649 if (open->form && open->form->expr_type == EXPR_CONSTANT
1651 /* TODO uncomment this code when F2003 support is finished */
1652 /* || open->decimal || open->encoding || open->round
1654 || open->pad || open->blank)
1655 && strncasecmp (open->form->value.character.string,
1656 "unformatted", 11) == 0)
1658 const char *spec = (open->delim ? "DELIM "
1659 : (open->pad ? "PAD " : open->blank
1662 warn_or_error ("%s specifier at %C not allowed in OPEN statement for "
1663 "unformatted I/O", spec);
1666 if (open->recl && open->access && open->access->expr_type == EXPR_CONSTANT
1667 && strncasecmp (open->access->value.character.string, "stream", 6) == 0)
1669 warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
1674 && open->access && open->access->expr_type == EXPR_CONSTANT
1675 && !(strncasecmp (open->access->value.character.string,
1676 "sequential", 10) == 0
1677 || strncasecmp (open->access->value.character.string,
1679 || strncasecmp (open->access->value.character.string,
1682 warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
1683 "for stream or sequential ACCESS");
1686 #undef warn_or_error
1688 new_st.op = EXEC_OPEN;
1689 new_st.ext.open = open;
1693 gfc_syntax_error (ST_OPEN);
1696 gfc_free_open (open);
1701 /* Free a gfc_close structure an all its expressions. */
1704 gfc_free_close (gfc_close *close)
1709 gfc_free_expr (close->unit);
1710 gfc_free_expr (close->iomsg);
1711 gfc_free_expr (close->iostat);
1712 gfc_free_expr (close->status);
1717 /* Match elements of a CLOSE statement. */
1720 match_close_element (gfc_close *close)
1724 m = match_etag (&tag_unit, &close->unit);
1727 m = match_etag (&tag_status, &close->status);
1730 m = match_out_tag (&tag_iomsg, &close->iomsg);
1733 m = match_out_tag (&tag_iostat, &close->iostat);
1736 m = match_ltag (&tag_err, &close->err);
1744 /* Match a CLOSE statement. */
1747 gfc_match_close (void)
1753 m = gfc_match_char ('(');
1757 close = gfc_getmem (sizeof (gfc_close));
1759 m = match_close_element (close);
1761 if (m == MATCH_ERROR)
1765 m = gfc_match_expr (&close->unit);
1768 if (m == MATCH_ERROR)
1774 if (gfc_match_char (')') == MATCH_YES)
1776 if (gfc_match_char (',') != MATCH_YES)
1779 m = match_close_element (close);
1780 if (m == MATCH_ERROR)
1786 if (gfc_match_eos () == MATCH_NO)
1789 if (gfc_pure (NULL))
1791 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1795 warn = (close->iostat || close->err) ? true : false;
1797 /* Checks on the STATUS specifier. */
1798 if (close->status && close->status->expr_type == EXPR_CONSTANT)
1800 static const char *status[] = { "KEEP", "DELETE", NULL };
1802 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1803 close->status->value.character.string,
1808 new_st.op = EXEC_CLOSE;
1809 new_st.ext.close = close;
1813 gfc_syntax_error (ST_CLOSE);
1816 gfc_free_close (close);
1821 /* Resolve everything in a gfc_close structure. */
1824 gfc_resolve_close (gfc_close *close)
1826 RESOLVE_TAG (&tag_unit, close->unit);
1827 RESOLVE_TAG (&tag_iomsg, close->iomsg);
1828 RESOLVE_TAG (&tag_iostat, close->iostat);
1829 RESOLVE_TAG (&tag_status, close->status);
1831 if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
1838 /* Free a gfc_filepos structure. */
1841 gfc_free_filepos (gfc_filepos *fp)
1843 gfc_free_expr (fp->unit);
1844 gfc_free_expr (fp->iomsg);
1845 gfc_free_expr (fp->iostat);
1850 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
1853 match_file_element (gfc_filepos *fp)
1857 m = match_etag (&tag_unit, &fp->unit);
1860 m = match_out_tag (&tag_iomsg, &fp->iomsg);
1863 m = match_out_tag (&tag_iostat, &fp->iostat);
1866 m = match_ltag (&tag_err, &fp->err);
1874 /* Match the second half of the file-positioning statements, REWIND,
1875 BACKSPACE, ENDFILE, or the FLUSH statement. */
1878 match_filepos (gfc_statement st, gfc_exec_op op)
1883 fp = gfc_getmem (sizeof (gfc_filepos));
1885 if (gfc_match_char ('(') == MATCH_NO)
1887 m = gfc_match_expr (&fp->unit);
1888 if (m == MATCH_ERROR)
1896 m = match_file_element (fp);
1897 if (m == MATCH_ERROR)
1901 m = gfc_match_expr (&fp->unit);
1902 if (m == MATCH_ERROR)
1910 if (gfc_match_char (')') == MATCH_YES)
1912 if (gfc_match_char (',') != MATCH_YES)
1915 m = match_file_element (fp);
1916 if (m == MATCH_ERROR)
1923 if (gfc_match_eos () != MATCH_YES)
1926 if (gfc_pure (NULL))
1928 gfc_error ("%s statement not allowed in PURE procedure at %C",
1929 gfc_ascii_statement (st));
1935 new_st.ext.filepos = fp;
1939 gfc_syntax_error (st);
1942 gfc_free_filepos (fp);
1948 gfc_resolve_filepos (gfc_filepos *fp)
1950 RESOLVE_TAG (&tag_unit, fp->unit);
1951 RESOLVE_TAG (&tag_iostat, fp->iostat);
1952 RESOLVE_TAG (&tag_iomsg, fp->iomsg);
1953 if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
1960 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
1961 and the FLUSH statement. */
1964 gfc_match_endfile (void)
1966 return match_filepos (ST_END_FILE, EXEC_ENDFILE);
1970 gfc_match_backspace (void)
1972 return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
1976 gfc_match_rewind (void)
1978 return match_filepos (ST_REWIND, EXEC_REWIND);
1982 gfc_match_flush (void)
1984 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: FLUSH statement at %C")
1988 return match_filepos (ST_FLUSH, EXEC_FLUSH);
1991 /******************** Data Transfer Statements *********************/
1994 { M_READ, M_WRITE, M_PRINT, M_INQUIRE }
1998 /* Return a default unit number. */
2001 default_unit (io_kind k)
2010 return gfc_int_expr (unit);
2014 /* Match a unit specification for a data transfer statement. */
2017 match_dt_unit (io_kind k, gfc_dt *dt)
2021 if (gfc_match_char ('*') == MATCH_YES)
2023 if (dt->io_unit != NULL)
2026 dt->io_unit = default_unit (k);
2030 if (gfc_match_expr (&e) == MATCH_YES)
2032 if (dt->io_unit != NULL)
2045 gfc_error ("Duplicate UNIT specification at %C");
2050 /* Match a format specification. */
2053 match_dt_format (gfc_dt *dt)
2057 gfc_st_label *label;
2059 where = gfc_current_locus;
2061 if (gfc_match_char ('*') == MATCH_YES)
2063 if (dt->format_expr != NULL || dt->format_label != NULL)
2066 dt->format_label = &format_asterisk;
2070 if (gfc_match_st_label (&label) == MATCH_YES)
2072 if (dt->format_expr != NULL || dt->format_label != NULL)
2074 gfc_free_st_label (label);
2078 if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
2081 dt->format_label = label;
2085 if (gfc_match_expr (&e) == MATCH_YES)
2087 if (dt->format_expr != NULL || dt->format_label != NULL)
2092 dt->format_expr = e;
2096 gfc_current_locus = where; /* The only case where we have to restore */
2101 gfc_error ("Duplicate format specification at %C");
2106 /* Traverse a namelist that is part of a READ statement to make sure
2107 that none of the variables in the namelist are INTENT(IN). Returns
2108 nonzero if we find such a variable. */
2111 check_namelist (gfc_symbol *sym)
2115 for (p = sym->namelist; p; p = p->next)
2116 if (p->sym->attr.intent == INTENT_IN)
2118 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2119 p->sym->name, sym->name);
2127 /* Match a single data transfer element. */
2130 match_dt_element (io_kind k, gfc_dt *dt)
2132 char name[GFC_MAX_SYMBOL_LEN + 1];
2136 if (gfc_match (" unit =") == MATCH_YES)
2138 m = match_dt_unit (k, dt);
2143 if (gfc_match (" fmt =") == MATCH_YES)
2145 m = match_dt_format (dt);
2150 if (gfc_match (" nml = %n", name) == MATCH_YES)
2152 if (dt->namelist != NULL)
2154 gfc_error ("Duplicate NML specification at %C");
2158 if (gfc_find_symbol (name, NULL, 1, &sym))
2161 if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
2163 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2164 sym != NULL ? sym->name : name);
2169 if (k == M_READ && check_namelist (sym))
2175 m = match_etag (&tag_rec, &dt->rec);
2178 m = match_etag (&tag_spos, &dt->rec);
2181 m = match_out_tag (&tag_iomsg, &dt->iomsg);
2184 m = match_out_tag (&tag_iostat, &dt->iostat);
2187 m = match_ltag (&tag_err, &dt->err);
2189 dt->err_where = gfc_current_locus;
2192 m = match_etag (&tag_advance, &dt->advance);
2195 m = match_out_tag (&tag_size, &dt->size);
2199 m = match_ltag (&tag_end, &dt->end);
2204 gfc_error ("END tag at %C not allowed in output statement");
2207 dt->end_where = gfc_current_locus;
2212 m = match_ltag (&tag_eor, &dt->eor);
2214 dt->eor_where = gfc_current_locus;
2222 /* Free a data transfer structure and everything below it. */
2225 gfc_free_dt (gfc_dt *dt)
2230 gfc_free_expr (dt->io_unit);
2231 gfc_free_expr (dt->format_expr);
2232 gfc_free_expr (dt->rec);
2233 gfc_free_expr (dt->advance);
2234 gfc_free_expr (dt->iomsg);
2235 gfc_free_expr (dt->iostat);
2236 gfc_free_expr (dt->size);
2241 /* Resolve everything in a gfc_dt structure. */
2244 gfc_resolve_dt (gfc_dt *dt)
2248 RESOLVE_TAG (&tag_format, dt->format_expr);
2249 RESOLVE_TAG (&tag_rec, dt->rec);
2250 RESOLVE_TAG (&tag_spos, dt->rec);
2251 RESOLVE_TAG (&tag_advance, dt->advance);
2252 RESOLVE_TAG (&tag_iomsg, dt->iomsg);
2253 RESOLVE_TAG (&tag_iostat, dt->iostat);
2254 RESOLVE_TAG (&tag_size, dt->size);
2257 if (gfc_resolve_expr (e) == SUCCESS
2258 && (e->ts.type != BT_INTEGER
2259 && (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_VARIABLE)))
2261 gfc_error ("UNIT specification at %L must be an INTEGER expression "
2262 "or a CHARACTER variable", &e->where);
2266 if (e->ts.type == BT_CHARACTER)
2268 if (gfc_has_vector_index (e))
2270 gfc_error ("Internal unit with vector subscript at %L", &e->where);
2275 if (e->rank && e->ts.type != BT_CHARACTER)
2277 gfc_error ("External IO UNIT cannot be an array at %L", &e->where);
2283 if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
2285 if (dt->err->defined == ST_LABEL_UNKNOWN)
2287 gfc_error ("ERR tag label %d at %L not defined",
2288 dt->err->value, &dt->err_where);
2295 if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
2297 if (dt->end->defined == ST_LABEL_UNKNOWN)
2299 gfc_error ("END tag label %d at %L not defined",
2300 dt->end->value, &dt->end_where);
2307 if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
2309 if (dt->eor->defined == ST_LABEL_UNKNOWN)
2311 gfc_error ("EOR tag label %d at %L not defined",
2312 dt->eor->value, &dt->eor_where);
2317 /* Check the format label actually exists. */
2318 if (dt->format_label && dt->format_label != &format_asterisk
2319 && dt->format_label->defined == ST_LABEL_UNKNOWN)
2321 gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
2322 &dt->format_label->where);
2329 /* Given an io_kind, return its name. */
2332 io_kind_name (io_kind k)
2351 gfc_internal_error ("io_kind_name(): bad I/O-kind");
2358 /* Match an IO iteration statement of the form:
2360 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2362 which is equivalent to a single IO element. This function is
2363 mutually recursive with match_io_element(). */
2365 static match match_io_element (io_kind, gfc_code **);
2368 match_io_iterator (io_kind k, gfc_code **result)
2370 gfc_code *head, *tail, *new;
2378 old_loc = gfc_current_locus;
2380 if (gfc_match_char ('(') != MATCH_YES)
2383 m = match_io_element (k, &head);
2386 if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
2392 /* Can't be anything but an IO iterator. Build a list. */
2393 iter = gfc_get_iterator ();
2397 m = gfc_match_iterator (iter, 0);
2398 if (m == MATCH_ERROR)
2402 gfc_check_do_variable (iter->var->symtree);
2406 m = match_io_element (k, &new);
2407 if (m == MATCH_ERROR)
2416 tail = gfc_append_code (tail, new);
2418 if (gfc_match_char (',') != MATCH_YES)
2427 if (gfc_match_char (')') != MATCH_YES)
2430 new = gfc_get_code ();
2432 new->ext.iterator = iter;
2434 new->block = gfc_get_code ();
2435 new->block->op = EXEC_DO;
2436 new->block->next = head;
2442 gfc_error ("Syntax error in I/O iterator at %C");
2446 gfc_free_iterator (iter, 1);
2447 gfc_free_statements (head);
2448 gfc_current_locus = old_loc;
2453 /* Match a single element of an IO list, which is either a single
2454 expression or an IO Iterator. */
2457 match_io_element (io_kind k, gfc_code **cpp)
2465 m = match_io_iterator (k, cpp);
2471 m = gfc_match_variable (&expr, 0);
2473 gfc_error ("Expected variable in READ statement at %C");
2477 m = gfc_match_expr (&expr);
2479 gfc_error ("Expected expression in %s statement at %C",
2487 if (expr->symtree->n.sym->attr.intent == INTENT_IN)
2489 gfc_error ("Variable '%s' in input list at %C cannot be "
2490 "INTENT(IN)", expr->symtree->n.sym->name);
2495 && gfc_impure_variable (expr->symtree->n.sym)
2496 && current_dt->io_unit->ts.type == BT_CHARACTER)
2498 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2499 expr->symtree->n.sym->name);
2503 if (gfc_check_do_variable (expr->symtree))
2509 if (current_dt->io_unit->ts.type == BT_CHARACTER
2511 && current_dt->io_unit->expr_type == EXPR_VARIABLE
2512 && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
2514 gfc_error ("Cannot write to internal file unit '%s' at %C "
2515 "inside a PURE procedure",
2516 current_dt->io_unit->symtree->n.sym->name);
2528 gfc_free_expr (expr);
2532 cp = gfc_get_code ();
2533 cp->op = EXEC_TRANSFER;
2541 /* Match an I/O list, building gfc_code structures as we go. */
2544 match_io_list (io_kind k, gfc_code **head_p)
2546 gfc_code *head, *tail, *new;
2549 *head_p = head = tail = NULL;
2550 if (gfc_match_eos () == MATCH_YES)
2555 m = match_io_element (k, &new);
2556 if (m == MATCH_ERROR)
2561 tail = gfc_append_code (tail, new);
2565 if (gfc_match_eos () == MATCH_YES)
2567 if (gfc_match_char (',') != MATCH_YES)
2575 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2578 gfc_free_statements (head);
2583 /* Attach the data transfer end node. */
2586 terminate_io (gfc_code *io_code)
2590 if (io_code == NULL)
2591 io_code = new_st.block;
2593 c = gfc_get_code ();
2594 c->op = EXEC_DT_END;
2596 /* Point to structure that is already there */
2597 c->ext.dt = new_st.ext.dt;
2598 gfc_append_code (io_code, c);
2602 /* Check the constraints for a data transfer statement. The majority of the
2603 constraints appearing in 9.4 of the standard appear here. Some are handled
2604 in resolve_tag and others in gfc_resolve_dt. */
2607 check_io_constraints (io_kind k, gfc_dt *dt, gfc_code *io_code,
2610 #define io_constraint(condition,msg,arg)\
2613 gfc_error(msg,arg);\
2619 gfc_symbol *sym = NULL;
2624 if (expr && expr->expr_type == EXPR_VARIABLE
2625 && expr->ts.type == BT_CHARACTER)
2627 sym = expr->symtree->n.sym;
2629 io_constraint (k == M_WRITE && sym->attr.intent == INTENT_IN,
2630 "Internal file at %L must not be INTENT(IN)",
2633 io_constraint (gfc_has_vector_index (dt->io_unit),
2634 "Internal file incompatible with vector subscript at %L",
2637 io_constraint (dt->rec != NULL,
2638 "REC tag at %L is incompatible with internal file",
2641 if (dt->namelist != NULL)
2643 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Internal file "
2644 "at %L with namelist", &expr->where)
2649 io_constraint (dt->advance != NULL,
2650 "ADVANCE tag at %L is incompatible with internal file",
2651 &dt->advance->where);
2654 if (expr && expr->ts.type != BT_CHARACTER)
2657 io_constraint (gfc_pure (NULL) && (k == M_READ || k == M_WRITE),
2658 "IO UNIT in %s statement at %C must be "
2659 "an internal file in a PURE procedure",
2666 io_constraint (dt->end, "END tag not allowed with output at %L",
2669 io_constraint (dt->eor, "EOR tag not allowed with output at %L",
2672 io_constraint (k != M_READ && dt->size,
2673 "SIZE=specifier not allowed with output at %L",
2678 io_constraint (dt->size && dt->advance == NULL,
2679 "SIZE tag at %L requires an ADVANCE tag",
2682 io_constraint (dt->eor && dt->advance == NULL,
2683 "EOR tag at %L requires an ADVANCE tag",
2691 io_constraint (io_code && dt->namelist,
2692 "NAMELIST cannot be followed by IO-list at %L",
2695 io_constraint (dt->format_expr,
2696 "IO spec-list cannot contain both NAMELIST group name "
2697 "and format specification at %L.",
2698 &dt->format_expr->where);
2700 io_constraint (dt->format_label,
2701 "IO spec-list cannot contain both NAMELIST group name "
2702 "and format label at %L", spec_end);
2704 io_constraint (dt->rec,
2705 "NAMELIST IO is not allowed with a REC=specifier "
2706 "at %L.", &dt->rec->where);
2708 io_constraint (dt->advance,
2709 "NAMELIST IO is not allowed with a ADVANCE=specifier "
2710 "at %L.", &dt->advance->where);
2715 io_constraint (dt->end,
2716 "An END tag is not allowed with a "
2717 "REC=specifier at %L.", &dt->end_where);
2720 io_constraint (dt->format_label == &format_asterisk,
2721 "FMT=* is not allowed with a REC=specifier "
2722 "at %L.", spec_end);
2727 int not_yes, not_no;
2730 io_constraint (dt->format_label == &format_asterisk,
2731 "List directed format(*) is not allowed with a "
2732 "ADVANCE=specifier at %L.", &expr->where);
2734 io_constraint (dt->format_expr == NULL && dt->format_label == NULL
2735 && dt->namelist == NULL,
2736 "the ADVANCE=specifier at %L must appear with an "
2737 "explicit format expression", &expr->where);
2739 if (expr->expr_type == EXPR_CONSTANT && expr->ts.type == BT_CHARACTER)
2741 const char * advance = expr->value.character.string;
2742 not_no = strcasecmp (advance, "no") != 0;
2743 not_yes = strcasecmp (advance, "yes") != 0;
2751 io_constraint (not_no && not_yes,
2752 "ADVANCE=specifier at %L must have value = "
2753 "YES or NO.", &expr->where);
2755 io_constraint (dt->size && not_no && k == M_READ,
2756 "SIZE tag at %L requires an ADVANCE = 'NO'",
2759 io_constraint (dt->eor && not_no && k == M_READ,
2760 "EOR tag at %L requires an ADVANCE = 'NO'",
2764 expr = dt->format_expr;
2765 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
2766 check_format_string (expr);
2770 #undef io_constraint
2773 /* Match a READ, WRITE or PRINT statement. */
2776 match_io (io_kind k)
2778 char name[GFC_MAX_SYMBOL_LEN + 1];
2787 where = gfc_current_locus;
2789 current_dt = dt = gfc_getmem (sizeof (gfc_dt));
2790 m = gfc_match_char ('(');
2793 where = gfc_current_locus;
2796 else if (k == M_PRINT)
2798 /* Treat the non-standard case of PRINT namelist. */
2799 if ((gfc_current_form == FORM_FIXED || gfc_peek_char () == ' ')
2800 && gfc_match_name (name) == MATCH_YES)
2802 gfc_find_symbol (name, NULL, 1, &sym);
2803 if (sym && sym->attr.flavor == FL_NAMELIST)
2805 if (gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
2806 "%C is an extension") == FAILURE)
2812 dt->io_unit = default_unit (k);
2817 gfc_current_locus = where;
2821 if (gfc_current_form == FORM_FREE)
2823 c = gfc_peek_char();
2824 if (c != ' ' && c != '*' && c != '\'' && c != '"')
2831 m = match_dt_format (dt);
2832 if (m == MATCH_ERROR)
2838 dt->io_unit = default_unit (k);
2843 /* Before issuing an error for a malformed 'print (1,*)' type of
2844 error, check for a default-char-expr of the form ('(I0)'). */
2845 if (k == M_PRINT && m == MATCH_YES)
2847 /* Reset current locus to get the initial '(' in an expression. */
2848 gfc_current_locus = where;
2849 dt->format_expr = NULL;
2850 m = match_dt_format (dt);
2852 if (m == MATCH_ERROR)
2854 if (m == MATCH_NO || dt->format_expr == NULL)
2858 dt->io_unit = default_unit (k);
2863 /* Match a control list */
2864 if (match_dt_element (k, dt) == MATCH_YES)
2866 if (match_dt_unit (k, dt) != MATCH_YES)
2869 if (gfc_match_char (')') == MATCH_YES)
2871 if (gfc_match_char (',') != MATCH_YES)
2874 m = match_dt_element (k, dt);
2877 if (m == MATCH_ERROR)
2880 m = match_dt_format (dt);
2883 if (m == MATCH_ERROR)
2886 where = gfc_current_locus;
2888 m = gfc_match_name (name);
2891 gfc_find_symbol (name, NULL, 1, &sym);
2892 if (sym && sym->attr.flavor == FL_NAMELIST)
2895 if (k == M_READ && check_namelist (sym))
2904 gfc_current_locus = where;
2906 goto loop; /* No matches, try regular elements */
2909 if (gfc_match_char (')') == MATCH_YES)
2911 if (gfc_match_char (',') != MATCH_YES)
2917 m = match_dt_element (k, dt);
2920 if (m == MATCH_ERROR)
2923 if (gfc_match_char (')') == MATCH_YES)
2925 if (gfc_match_char (',') != MATCH_YES)
2931 /* Used in check_io_constraints, where no locus is available. */
2932 spec_end = gfc_current_locus;
2934 /* Optional leading comma (non-standard). */
2936 && gfc_match_char (',') == MATCH_YES
2938 && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before output "
2939 "item list at %C is an extension") == FAILURE)
2943 if (gfc_match_eos () != MATCH_YES)
2945 if (comma_flag && gfc_match_char (',') != MATCH_YES)
2947 gfc_error ("Expected comma in I/O list at %C");
2952 m = match_io_list (k, &io_code);
2953 if (m == MATCH_ERROR)
2959 /* A full IO statement has been matched. Check the constraints. spec_end is
2960 supplied for cases where no locus is supplied. */
2961 m = check_io_constraints (k, dt, io_code, &spec_end);
2963 if (m == MATCH_ERROR)
2966 new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
2968 new_st.block = gfc_get_code ();
2969 new_st.block->op = new_st.op;
2970 new_st.block->next = io_code;
2972 terminate_io (io_code);
2977 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2987 gfc_match_read (void)
2989 return match_io (M_READ);
2993 gfc_match_write (void)
2995 return match_io (M_WRITE);
2999 gfc_match_print (void)
3003 m = match_io (M_PRINT);
3007 if (gfc_pure (NULL))
3009 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
3017 /* Free a gfc_inquire structure. */
3020 gfc_free_inquire (gfc_inquire *inquire)
3023 if (inquire == NULL)
3026 gfc_free_expr (inquire->unit);
3027 gfc_free_expr (inquire->file);
3028 gfc_free_expr (inquire->iomsg);
3029 gfc_free_expr (inquire->iostat);
3030 gfc_free_expr (inquire->exist);
3031 gfc_free_expr (inquire->opened);
3032 gfc_free_expr (inquire->number);
3033 gfc_free_expr (inquire->named);
3034 gfc_free_expr (inquire->name);
3035 gfc_free_expr (inquire->access);
3036 gfc_free_expr (inquire->sequential);
3037 gfc_free_expr (inquire->direct);
3038 gfc_free_expr (inquire->form);
3039 gfc_free_expr (inquire->formatted);
3040 gfc_free_expr (inquire->unformatted);
3041 gfc_free_expr (inquire->recl);
3042 gfc_free_expr (inquire->nextrec);
3043 gfc_free_expr (inquire->blank);
3044 gfc_free_expr (inquire->position);
3045 gfc_free_expr (inquire->action);
3046 gfc_free_expr (inquire->read);
3047 gfc_free_expr (inquire->write);
3048 gfc_free_expr (inquire->readwrite);
3049 gfc_free_expr (inquire->delim);
3050 gfc_free_expr (inquire->pad);
3051 gfc_free_expr (inquire->iolength);
3052 gfc_free_expr (inquire->convert);
3053 gfc_free_expr (inquire->strm_pos);
3058 /* Match an element of an INQUIRE statement. */
3060 #define RETM if (m != MATCH_NO) return m;
3063 match_inquire_element (gfc_inquire *inquire)
3067 m = match_etag (&tag_unit, &inquire->unit);
3068 RETM m = match_etag (&tag_file, &inquire->file);
3069 RETM m = match_ltag (&tag_err, &inquire->err);
3070 RETM m = match_out_tag (&tag_iomsg, &inquire->iomsg);
3071 RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
3072 RETM m = match_vtag (&tag_exist, &inquire->exist);
3073 RETM m = match_vtag (&tag_opened, &inquire->opened);
3074 RETM m = match_vtag (&tag_named, &inquire->named);
3075 RETM m = match_vtag (&tag_name, &inquire->name);
3076 RETM m = match_out_tag (&tag_number, &inquire->number);
3077 RETM m = match_vtag (&tag_s_access, &inquire->access);
3078 RETM m = match_vtag (&tag_sequential, &inquire->sequential);
3079 RETM m = match_vtag (&tag_direct, &inquire->direct);
3080 RETM m = match_vtag (&tag_s_form, &inquire->form);
3081 RETM m = match_vtag (&tag_formatted, &inquire->formatted);
3082 RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
3083 RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
3084 RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
3085 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
3086 RETM m = match_vtag (&tag_s_position, &inquire->position);
3087 RETM m = match_vtag (&tag_s_action, &inquire->action);
3088 RETM m = match_vtag (&tag_read, &inquire->read);
3089 RETM m = match_vtag (&tag_write, &inquire->write);
3090 RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
3091 RETM m = match_vtag (&tag_s_delim, &inquire->delim);
3092 RETM m = match_vtag (&tag_s_pad, &inquire->pad);
3093 RETM m = match_vtag (&tag_iolength, &inquire->iolength);
3094 RETM m = match_vtag (&tag_convert, &inquire->convert);
3095 RETM m = match_out_tag (&tag_strm_out, &inquire->strm_pos);
3096 RETM return MATCH_NO;
3103 gfc_match_inquire (void)
3105 gfc_inquire *inquire;
3110 m = gfc_match_char ('(');
3114 inquire = gfc_getmem (sizeof (gfc_inquire));
3116 loc = gfc_current_locus;
3118 m = match_inquire_element (inquire);
3119 if (m == MATCH_ERROR)
3123 m = gfc_match_expr (&inquire->unit);
3124 if (m == MATCH_ERROR)
3130 /* See if we have the IOLENGTH form of the inquire statement. */
3131 if (inquire->iolength != NULL)
3133 if (gfc_match_char (')') != MATCH_YES)
3136 m = match_io_list (M_INQUIRE, &code);
3137 if (m == MATCH_ERROR)
3142 new_st.op = EXEC_IOLENGTH;
3143 new_st.expr = inquire->iolength;
3144 new_st.ext.inquire = inquire;
3146 if (gfc_pure (NULL))
3148 gfc_free_statements (code);
3149 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3153 new_st.block = gfc_get_code ();
3154 new_st.block->op = EXEC_IOLENGTH;
3155 terminate_io (code);
3156 new_st.block->next = code;
3160 /* At this point, we have the non-IOLENGTH inquire statement. */
3163 if (gfc_match_char (')') == MATCH_YES)
3165 if (gfc_match_char (',') != MATCH_YES)
3168 m = match_inquire_element (inquire);
3169 if (m == MATCH_ERROR)
3174 if (inquire->iolength != NULL)
3176 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3181 if (gfc_match_eos () != MATCH_YES)
3184 if (inquire->unit != NULL && inquire->file != NULL)
3186 gfc_error ("INQUIRE statement at %L cannot contain both FILE and "
3187 "UNIT specifiers", &loc);
3191 if (inquire->unit == NULL && inquire->file == NULL)
3193 gfc_error ("INQUIRE statement at %L requires either FILE or "
3194 "UNIT specifier", &loc);
3198 if (gfc_pure (NULL))
3200 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3204 new_st.op = EXEC_INQUIRE;
3205 new_st.ext.inquire = inquire;
3209 gfc_syntax_error (ST_INQUIRE);
3212 gfc_free_inquire (inquire);
3217 /* Resolve everything in a gfc_inquire structure. */
3220 gfc_resolve_inquire (gfc_inquire *inquire)
3222 RESOLVE_TAG (&tag_unit, inquire->unit);
3223 RESOLVE_TAG (&tag_file, inquire->file);
3224 RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
3225 RESOLVE_TAG (&tag_iostat, inquire->iostat);
3226 RESOLVE_TAG (&tag_exist, inquire->exist);
3227 RESOLVE_TAG (&tag_opened, inquire->opened);
3228 RESOLVE_TAG (&tag_number, inquire->number);
3229 RESOLVE_TAG (&tag_named, inquire->named);
3230 RESOLVE_TAG (&tag_name, inquire->name);
3231 RESOLVE_TAG (&tag_s_access, inquire->access);
3232 RESOLVE_TAG (&tag_sequential, inquire->sequential);
3233 RESOLVE_TAG (&tag_direct, inquire->direct);
3234 RESOLVE_TAG (&tag_s_form, inquire->form);
3235 RESOLVE_TAG (&tag_formatted, inquire->formatted);
3236 RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
3237 RESOLVE_TAG (&tag_s_recl, inquire->recl);
3238 RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
3239 RESOLVE_TAG (&tag_s_blank, inquire->blank);
3240 RESOLVE_TAG (&tag_s_position, inquire->position);
3241 RESOLVE_TAG (&tag_s_action, inquire->action);
3242 RESOLVE_TAG (&tag_read, inquire->read);
3243 RESOLVE_TAG (&tag_write, inquire->write);
3244 RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
3245 RESOLVE_TAG (&tag_s_delim, inquire->delim);
3246 RESOLVE_TAG (&tag_s_pad, inquire->pad);
3247 RESOLVE_TAG (&tag_iolength, inquire->iolength);
3248 RESOLVE_TAG (&tag_convert, inquire->convert);
3249 RESOLVE_TAG (&tag_strm_out, inquire->strm_pos);
3251 if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)