1 /* Deal with I/O statements & related stuff.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
30 format_asterisk = {0, NULL, NULL, -1, ST_LABEL_FORMAT, ST_LABEL_FORMAT, NULL,
35 const char *name, *spec, *value;
41 tag_file = { "FILE", " file =", " %e", BT_CHARACTER },
42 tag_status = { "STATUS", " status =", " %e", BT_CHARACTER},
43 tag_e_access = {"ACCESS", " access =", " %e", BT_CHARACTER},
44 tag_e_form = {"FORM", " form =", " %e", BT_CHARACTER},
45 tag_e_recl = {"RECL", " recl =", " %e", BT_INTEGER},
46 tag_e_blank = {"BLANK", " blank =", " %e", BT_CHARACTER},
47 tag_e_position = {"POSITION", " position =", " %e", BT_CHARACTER},
48 tag_e_action = {"ACTION", " action =", " %e", BT_CHARACTER},
49 tag_e_delim = {"DELIM", " delim =", " %e", BT_CHARACTER},
50 tag_e_pad = {"PAD", " pad =", " %e", BT_CHARACTER},
51 tag_e_decimal = {"DECIMAL", " decimal =", " %e", BT_CHARACTER},
52 tag_e_encoding = {"ENCODING", " encoding =", " %e", BT_CHARACTER},
53 tag_e_async = {"ASYNCHRONOUS", " asynchronous =", " %e", BT_CHARACTER},
54 tag_e_round = {"ROUND", " round =", " %e", BT_CHARACTER},
55 tag_e_sign = {"SIGN", " sign =", " %e", BT_CHARACTER},
56 tag_unit = {"UNIT", " unit =", " %e", BT_INTEGER},
57 tag_advance = {"ADVANCE", " advance =", " %e", BT_CHARACTER},
58 tag_rec = {"REC", " rec =", " %e", BT_INTEGER},
59 tag_spos = {"POSITION", " pos =", " %e", BT_INTEGER},
60 tag_format = {"FORMAT", NULL, NULL, BT_CHARACTER},
61 tag_iomsg = {"IOMSG", " iomsg =", " %e", BT_CHARACTER},
62 tag_iostat = {"IOSTAT", " iostat =", " %v", BT_INTEGER},
63 tag_size = {"SIZE", " size =", " %v", BT_INTEGER},
64 tag_exist = {"EXIST", " exist =", " %v", BT_LOGICAL},
65 tag_opened = {"OPENED", " opened =", " %v", BT_LOGICAL},
66 tag_named = {"NAMED", " named =", " %v", BT_LOGICAL},
67 tag_name = {"NAME", " name =", " %v", BT_CHARACTER},
68 tag_number = {"NUMBER", " number =", " %v", BT_INTEGER},
69 tag_s_access = {"ACCESS", " access =", " %v", BT_CHARACTER},
70 tag_sequential = {"SEQUENTIAL", " sequential =", " %v", BT_CHARACTER},
71 tag_direct = {"DIRECT", " direct =", " %v", BT_CHARACTER},
72 tag_s_form = {"FORM", " form =", " %v", BT_CHARACTER},
73 tag_formatted = {"FORMATTED", " formatted =", " %v", BT_CHARACTER},
74 tag_unformatted = {"UNFORMATTED", " unformatted =", " %v", BT_CHARACTER},
75 tag_s_recl = {"RECL", " recl =", " %v", BT_INTEGER},
76 tag_nextrec = {"NEXTREC", " nextrec =", " %v", BT_INTEGER},
77 tag_s_blank = {"BLANK", " blank =", " %v", BT_CHARACTER},
78 tag_s_position = {"POSITION", " position =", " %v", BT_CHARACTER},
79 tag_s_action = {"ACTION", " action =", " %v", BT_CHARACTER},
80 tag_read = {"READ", " read =", " %v", BT_CHARACTER},
81 tag_write = {"WRITE", " write =", " %v", BT_CHARACTER},
82 tag_readwrite = {"READWRITE", " readwrite =", " %v", BT_CHARACTER},
83 tag_s_delim = {"DELIM", " delim =", " %v", BT_CHARACTER},
84 tag_s_pad = {"PAD", " pad =", " %v", BT_CHARACTER},
85 tag_s_decimal = {"DECIMAL", " decimal =", " %v", BT_CHARACTER},
86 tag_s_encoding = {"ENCODING", " encoding =", " %v", BT_CHARACTER},
87 tag_s_async = {"ASYNCHRONOUS", " asynchronous =", " %v", BT_CHARACTER},
88 tag_s_round = {"ROUND", " round =", " %v", BT_CHARACTER},
89 tag_s_sign = {"SIGN", " sign =", " %v", BT_CHARACTER},
90 tag_iolength = {"IOLENGTH", " iolength =", " %v", BT_INTEGER},
91 tag_convert = {"CONVERT", " convert =", " %e", BT_CHARACTER},
92 tag_strm_out = {"POS", " pos =", " %v", BT_INTEGER},
93 tag_err = {"ERR", " err =", " %l", BT_UNKNOWN},
94 tag_end = {"END", " end =", " %l", BT_UNKNOWN},
95 tag_eor = {"EOR", " eor =", " %l", BT_UNKNOWN},
96 tag_id = {"ID", " id =", " %v", BT_INTEGER},
97 tag_pending = {"PENDING", " pending =", " %v", BT_LOGICAL};
99 static gfc_dt *current_dt;
101 #define RESOLVE_TAG(x, y) if (resolve_tag(x, y) == FAILURE) return FAILURE;
104 /**************** Fortran 95 FORMAT parser *****************/
106 /* FORMAT tokens returned by format_lex(). */
109 FMT_NONE, FMT_UNKNOWN, FMT_SIGNED_INT, FMT_ZERO, FMT_POSINT, FMT_PERIOD,
110 FMT_COMMA, FMT_COLON, FMT_SLASH, FMT_DOLLAR, FMT_POS, FMT_LPAREN,
111 FMT_RPAREN, FMT_X, FMT_SIGN, FMT_BLANK, FMT_CHAR, FMT_P, FMT_IBOZ, FMT_F,
112 FMT_E, FMT_EXT, FMT_G, FMT_L, FMT_A, FMT_D, FMT_H, FMT_END, FMT_ERROR, FMT_DC,
117 /* Local variables for checking format strings. The saved_token is
118 used to back up by a single format token during the parsing
120 static gfc_char_t *format_string;
121 static int format_length, use_last_char;
123 static format_token saved_token;
126 { MODE_STRING, MODE_FORMAT, MODE_COPY }
130 /* Return the next character in the format string. */
133 next_char (int in_string)
145 if (mode == MODE_STRING)
146 c = *format_string++;
149 c = gfc_next_char_literal (in_string);
154 if (gfc_option.flag_backslash && c == '\\')
156 locus old_locus = gfc_current_locus;
158 if (gfc_match_special_char (&c) == MATCH_NO)
159 gfc_current_locus = old_locus;
161 if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
162 gfc_warning ("Extension: backslash character at %C");
165 if (mode == MODE_COPY)
166 *format_string++ = c;
168 c = gfc_wide_toupper (c);
173 /* Back up one character position. Only works once. */
181 /* Eat up the spaces and return a character. */
184 next_char_not_space (bool *error)
192 if (gfc_option.allow_std & GFC_STD_GNU)
193 gfc_warning ("Extension: Tab character in format at %C");
196 gfc_error ("Extension: Tab character in format at %C");
202 while (gfc_is_whitespace (c));
206 static int value = 0;
208 /* Simple lexical analyzer for getting the next token in a FORMAT
220 if (saved_token != FMT_NONE)
223 saved_token = FMT_NONE;
227 c = next_char_not_space (&error);
235 c = next_char_not_space (&error);
246 c = next_char_not_space (&error);
248 value = 10 * value + c - '0';
257 token = FMT_SIGNED_INT;
276 c = next_char_not_space (&error);
279 value = 10 * value + c - '0';
287 token = zflag ? FMT_ZERO : FMT_POSINT;
311 c = next_char_not_space (&error);
312 if (c != 'L' && c != 'R')
331 c = next_char_not_space (&error);
332 if (c != 'P' && c != 'S')
339 c = next_char_not_space (&error);
340 if (c == 'N' || c == 'Z')
401 c = next_char_not_space (&error);
402 if (c == 'N' || c == 'S')
429 c = next_char_not_space (&error);
432 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DP format "
433 "specifier not allowed at %C") == FAILURE)
439 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DC format "
440 "specifier not allowed at %C") == FAILURE)
467 /* Check a format statement. The format string, either from a FORMAT
468 statement or a constant in an I/O statement has already been parsed
469 by itself, and we are checking it for validity. The dual origin
470 means that the warning message is a little less than great. */
473 check_format (bool is_input)
475 const char *posint_required = _("Positive width required");
476 const char *nonneg_required = _("Nonnegative width required");
477 const char *unexpected_element = _("Unexpected element");
478 const char *unexpected_end = _("Unexpected end of format string");
479 const char *zero_width = _("Zero width in format descriptor");
480 const char *g0_precision = _("Specifying precision with G0 not allowed");
489 saved_token = FMT_NONE;
499 error = _("Missing leading left parenthesis");
507 goto finished; /* Empty format is legal */
511 /* In this state, the next thing has to be a format item. */
540 /* Signed integer can only precede a P format. */
546 error = _("Expected P edit descriptor");
553 /* P requires a prior number. */
554 error = _("P descriptor requires leading scale factor");
558 /* X requires a prior number if we're being pedantic. */
559 if (gfc_notify_std (GFC_STD_GNU, "Extension: X descriptor "
560 "requires leading space count at %C")
572 goto extension_optional_comma;
583 if (gfc_notify_std (GFC_STD_GNU, "Extension: $ descriptor at %C")
586 if (t != FMT_RPAREN || level > 0)
588 gfc_warning ("$ should be the last specifier in format at %C");
589 goto optional_comma_1;
607 error = unexpected_end;
611 error = unexpected_element;
616 /* In this state, t must currently be a data descriptor.
617 Deal with things that can/must follow the descriptor. */
635 error = _("Repeat count cannot follow P descriptor");
652 switch (gfc_notification_std (GFC_STD_GNU))
655 gfc_warning ("Extension: Missing positive width after L "
661 error = posint_required;
691 if (t == FMT_G && u == FMT_ZERO)
699 if (gfc_notify_std (GFC_STD_F2008, "Fortran F2008: 'G0' in "
700 "format at %C") == FAILURE)
706 error = g0_precision;
717 error = posint_required;
726 /* Warn if -std=legacy, otherwise error. */
727 if (gfc_option.warn_std != 0)
728 gfc_error_now ("Period required in format specifier at %C");
730 gfc_warning ("Period required in format specifier at %C");
738 if (u != FMT_ZERO && u != FMT_POSINT)
740 error = nonneg_required;
747 /* Look for optional exponent. */
762 error = _("Positive exponent width required");
773 if (t != FMT_ZERO && t != FMT_POSINT)
775 error = nonneg_required;
778 else if (is_input && t == FMT_ZERO)
780 error = posint_required;
789 /* Warn if -std=legacy, otherwise error. */
790 if (gfc_option.warn_std != 0)
791 gfc_error_now ("Period required in format specifier at %C");
793 gfc_warning ("Period required in format specifier at %C");
801 if (t != FMT_ZERO && t != FMT_POSINT)
803 error = nonneg_required;
810 if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
811 gfc_warning ("The H format specifier at %C is"
812 " a Fortran 95 deleted feature");
814 if (mode == MODE_STRING)
816 format_string += value;
817 format_length -= value;
833 if (t != FMT_ZERO && t != FMT_POSINT)
835 error = nonneg_required;
838 else if (is_input && t == FMT_ZERO)
840 error = posint_required;
856 if (t != FMT_ZERO && t != FMT_POSINT)
858 error = nonneg_required;
866 error = unexpected_element;
871 /* Between a descriptor and what comes next. */
892 error = unexpected_end;
896 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
903 /* Optional comma is a weird between state where we've just finished
904 reading a colon, slash, dollar or P descriptor. */
921 /* Assume that we have another format item. */
928 extension_optional_comma:
929 /* As a GNU extension, permit a missing comma after a string literal. */
949 error = unexpected_end;
953 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
963 gfc_error ("%s in format string at %C", error);
965 /* TODO: More elaborate measures are needed to show where a problem
966 is within a format string that has been calculated. */
974 /* Given an expression node that is a constant string, see if it looks
975 like a format string. */
978 check_format_string (gfc_expr *e, bool is_input)
980 if (!e || e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
984 format_string = e->value.character.string;
985 return check_format (is_input);
989 /************ Fortran 95 I/O statement matchers *************/
991 /* Match a FORMAT statement. This amounts to actually parsing the
992 format descriptors in order to correctly locate the end of the
996 gfc_match_format (void)
1001 if (gfc_current_ns->proc_name
1002 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
1004 gfc_error ("Format statement in module main block at %C");
1008 if (gfc_statement_label == NULL)
1010 gfc_error ("Missing format label at %C");
1013 gfc_gobble_whitespace ();
1018 start = gfc_current_locus;
1020 if (check_format (false) == FAILURE)
1023 if (gfc_match_eos () != MATCH_YES)
1025 gfc_syntax_error (ST_FORMAT);
1029 /* The label doesn't get created until after the statement is done
1030 being matched, so we have to leave the string for later. */
1032 gfc_current_locus = start; /* Back to the beginning */
1035 new_st.op = EXEC_NOP;
1038 e->expr_type = EXPR_CONSTANT;
1039 e->ts.type = BT_CHARACTER;
1040 e->ts.kind = gfc_default_character_kind;
1042 e->value.character.string = format_string
1043 = gfc_get_wide_string (format_length + 1);
1044 e->value.character.length = format_length;
1045 gfc_statement_label->format = e;
1048 check_format (false); /* Guaranteed to succeed */
1049 gfc_match_eos (); /* Guaranteed to succeed */
1055 /* Match an expression I/O tag of some sort. */
1058 match_etag (const io_tag *tag, gfc_expr **v)
1063 m = gfc_match (tag->spec);
1067 m = gfc_match (tag->value, &result);
1070 gfc_error ("Invalid value for %s specification at %C", tag->name);
1076 gfc_error ("Duplicate %s specification at %C", tag->name);
1077 gfc_free_expr (result);
1086 /* Match a variable I/O tag of some sort. */
1089 match_vtag (const io_tag *tag, gfc_expr **v)
1094 m = gfc_match (tag->spec);
1098 m = gfc_match (tag->value, &result);
1101 gfc_error ("Invalid value for %s specification at %C", tag->name);
1107 gfc_error ("Duplicate %s specification at %C", tag->name);
1108 gfc_free_expr (result);
1112 if (result->symtree->n.sym->attr.intent == INTENT_IN)
1114 gfc_error ("Variable tag cannot be INTENT(IN) at %C");
1115 gfc_free_expr (result);
1119 if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
1121 gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
1122 gfc_free_expr (result);
1131 /* Match I/O tags that cause variables to become redefined. */
1134 match_out_tag(const io_tag *tag, gfc_expr **result)
1138 m = match_vtag(tag, result);
1140 gfc_check_do_variable((*result)->symtree);
1146 /* Match a label I/O tag. */
1149 match_ltag (const io_tag *tag, gfc_st_label ** label)
1155 m = gfc_match (tag->spec);
1159 m = gfc_match (tag->value, label);
1162 gfc_error ("Invalid value for %s specification at %C", tag->name);
1168 gfc_error ("Duplicate %s label specification at %C", tag->name);
1172 if (gfc_reference_st_label (*label, ST_LABEL_TARGET) == FAILURE)
1179 /* Resolution of the FORMAT tag, to be called from resolve_tag. */
1182 resolve_tag_format (const gfc_expr *e)
1184 if (e->expr_type == EXPR_CONSTANT
1185 && (e->ts.type != BT_CHARACTER
1186 || e->ts.kind != gfc_default_character_kind))
1188 gfc_error ("Constant expression in FORMAT tag at %L must be "
1189 "of type default CHARACTER", &e->where);
1193 /* If e's rank is zero and e is not an element of an array, it should be
1194 of integer or character type. The integer variable should be
1196 if (e->symtree == NULL || e->symtree->n.sym->as == NULL
1197 || e->symtree->n.sym->as->rank == 0)
1199 if (e->ts.type != BT_CHARACTER && e->ts.type != BT_INTEGER)
1201 gfc_error ("FORMAT tag at %L must be of type CHARACTER or INTEGER",
1205 else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
1207 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGNED "
1208 "variable in FORMAT tag at %L", &e->where)
1211 if (e->symtree->n.sym->attr.assign != 1)
1213 gfc_error ("Variable '%s' at %L has not been assigned a "
1214 "format label", e->symtree->n.sym->name, &e->where);
1218 else if (e->ts.type == BT_INTEGER)
1220 gfc_error ("Scalar '%s' in FORMAT tag at %L is not an ASSIGNED "
1221 "variable", gfc_basic_typename (e->ts.type), &e->where);
1228 /* If rank is nonzero, we allow the type to be character under GFC_STD_GNU
1229 and other type under GFC_STD_LEGACY. It may be assigned an Hollerith
1231 if (e->ts.type == BT_CHARACTER)
1233 if (gfc_notify_std (GFC_STD_GNU, "Extension: Character array "
1234 "in FORMAT tag at %L", &e->where) == FAILURE)
1239 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: Non-character "
1240 "in FORMAT tag at %L", &e->where) == FAILURE)
1248 /* Do expression resolution and type-checking on an expression tag. */
1251 resolve_tag (const io_tag *tag, gfc_expr *e)
1256 if (gfc_resolve_expr (e) == FAILURE)
1259 if (tag == &tag_format)
1260 return resolve_tag_format (e);
1262 if (e->ts.type != tag->type)
1264 gfc_error ("%s tag at %L must be of type %s", tag->name,
1265 &e->where, gfc_basic_typename (tag->type));
1271 gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
1275 if (tag == &tag_iomsg)
1277 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IOMSG tag at %L",
1278 &e->where) == FAILURE)
1282 if ((tag == &tag_iostat || tag == &tag_size || tag == &tag_iolength)
1283 && e->ts.kind != gfc_default_integer_kind)
1285 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1286 "INTEGER in %s tag at %L", tag->name, &e->where)
1291 if (tag == &tag_convert)
1293 if (gfc_notify_std (GFC_STD_GNU, "Extension: CONVERT tag at %L",
1294 &e->where) == FAILURE)
1302 /* Match a single tag of an OPEN statement. */
1305 match_open_element (gfc_open *open)
1309 m = match_etag (&tag_e_async, &open->asynchronous);
1312 m = match_etag (&tag_unit, &open->unit);
1315 m = match_out_tag (&tag_iomsg, &open->iomsg);
1318 m = match_out_tag (&tag_iostat, &open->iostat);
1321 m = match_etag (&tag_file, &open->file);
1324 m = match_etag (&tag_status, &open->status);
1327 m = match_etag (&tag_e_access, &open->access);
1330 m = match_etag (&tag_e_form, &open->form);
1333 m = match_etag (&tag_e_recl, &open->recl);
1336 m = match_etag (&tag_e_blank, &open->blank);
1339 m = match_etag (&tag_e_position, &open->position);
1342 m = match_etag (&tag_e_action, &open->action);
1345 m = match_etag (&tag_e_delim, &open->delim);
1348 m = match_etag (&tag_e_pad, &open->pad);
1351 m = match_etag (&tag_e_decimal, &open->decimal);
1354 m = match_etag (&tag_e_encoding, &open->encoding);
1357 m = match_etag (&tag_e_round, &open->round);
1360 m = match_etag (&tag_e_sign, &open->sign);
1363 m = match_ltag (&tag_err, &open->err);
1366 m = match_etag (&tag_convert, &open->convert);
1374 /* Free the gfc_open structure and all the expressions it contains. */
1377 gfc_free_open (gfc_open *open)
1382 gfc_free_expr (open->unit);
1383 gfc_free_expr (open->iomsg);
1384 gfc_free_expr (open->iostat);
1385 gfc_free_expr (open->file);
1386 gfc_free_expr (open->status);
1387 gfc_free_expr (open->access);
1388 gfc_free_expr (open->form);
1389 gfc_free_expr (open->recl);
1390 gfc_free_expr (open->blank);
1391 gfc_free_expr (open->position);
1392 gfc_free_expr (open->action);
1393 gfc_free_expr (open->delim);
1394 gfc_free_expr (open->pad);
1395 gfc_free_expr (open->decimal);
1396 gfc_free_expr (open->encoding);
1397 gfc_free_expr (open->round);
1398 gfc_free_expr (open->sign);
1399 gfc_free_expr (open->convert);
1400 gfc_free_expr (open->asynchronous);
1405 /* Resolve everything in a gfc_open structure. */
1408 gfc_resolve_open (gfc_open *open)
1411 RESOLVE_TAG (&tag_unit, open->unit);
1412 RESOLVE_TAG (&tag_iomsg, open->iomsg);
1413 RESOLVE_TAG (&tag_iostat, open->iostat);
1414 RESOLVE_TAG (&tag_file, open->file);
1415 RESOLVE_TAG (&tag_status, open->status);
1416 RESOLVE_TAG (&tag_e_access, open->access);
1417 RESOLVE_TAG (&tag_e_form, open->form);
1418 RESOLVE_TAG (&tag_e_recl, open->recl);
1419 RESOLVE_TAG (&tag_e_blank, open->blank);
1420 RESOLVE_TAG (&tag_e_position, open->position);
1421 RESOLVE_TAG (&tag_e_action, open->action);
1422 RESOLVE_TAG (&tag_e_delim, open->delim);
1423 RESOLVE_TAG (&tag_e_pad, open->pad);
1424 RESOLVE_TAG (&tag_e_decimal, open->decimal);
1425 RESOLVE_TAG (&tag_e_encoding, open->encoding);
1426 RESOLVE_TAG (&tag_e_async, open->asynchronous);
1427 RESOLVE_TAG (&tag_e_round, open->round);
1428 RESOLVE_TAG (&tag_e_sign, open->sign);
1429 RESOLVE_TAG (&tag_convert, open->convert);
1431 if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1438 /* Check if a given value for a SPECIFIER is either in the list of values
1439 allowed in F95 or F2003, issuing an error message and returning a zero
1440 value if it is not allowed. */
1443 compare_to_allowed_values (const char *specifier, const char *allowed[],
1444 const char *allowed_f2003[],
1445 const char *allowed_gnu[], gfc_char_t *value,
1446 const char *statement, bool warn)
1451 len = gfc_wide_strlen (value);
1454 for (len--; len > 0; len--)
1455 if (value[len] != ' ')
1460 for (i = 0; allowed[i]; i++)
1461 if (len == strlen (allowed[i])
1462 && gfc_wide_strncasecmp (value, allowed[i], strlen (allowed[i])) == 0)
1465 for (i = 0; allowed_f2003 && allowed_f2003[i]; i++)
1466 if (len == strlen (allowed_f2003[i])
1467 && gfc_wide_strncasecmp (value, allowed_f2003[i],
1468 strlen (allowed_f2003[i])) == 0)
1470 notification n = gfc_notification_std (GFC_STD_F2003);
1472 if (n == WARNING || (warn && n == ERROR))
1474 gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1475 "has value '%s'", specifier, statement,
1482 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: %s specifier in "
1483 "%s statement at %C has value '%s'", specifier,
1484 statement, allowed_f2003[i]);
1492 for (i = 0; allowed_gnu && allowed_gnu[i]; i++)
1493 if (len == strlen (allowed_gnu[i])
1494 && gfc_wide_strncasecmp (value, allowed_gnu[i],
1495 strlen (allowed_gnu[i])) == 0)
1497 notification n = gfc_notification_std (GFC_STD_GNU);
1499 if (n == WARNING || (warn && n == ERROR))
1501 gfc_warning ("Extension: %s specifier in %s statement at %C "
1502 "has value '%s'", specifier, statement,
1509 gfc_notify_std (GFC_STD_GNU, "Extension: %s specifier in "
1510 "%s statement at %C has value '%s'", specifier,
1511 statement, allowed_gnu[i]);
1521 char *s = gfc_widechar_to_char (value, -1);
1522 gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1523 specifier, statement, s);
1529 char *s = gfc_widechar_to_char (value, -1);
1530 gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1531 specifier, statement, s);
1538 /* Match an OPEN statement. */
1541 gfc_match_open (void)
1547 m = gfc_match_char ('(');
1551 open = XCNEW (gfc_open);
1553 m = match_open_element (open);
1555 if (m == MATCH_ERROR)
1559 m = gfc_match_expr (&open->unit);
1562 if (m == MATCH_ERROR)
1568 if (gfc_match_char (')') == MATCH_YES)
1570 if (gfc_match_char (',') != MATCH_YES)
1573 m = match_open_element (open);
1574 if (m == MATCH_ERROR)
1580 if (gfc_match_eos () == MATCH_NO)
1583 if (gfc_pure (NULL))
1585 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1589 warn = (open->err || open->iostat) ? true : false;
1590 /* Checks on the ACCESS specifier. */
1591 if (open->access && open->access->expr_type == EXPR_CONSTANT)
1593 static const char *access_f95[] = { "SEQUENTIAL", "DIRECT", NULL };
1594 static const char *access_f2003[] = { "STREAM", NULL };
1595 static const char *access_gnu[] = { "APPEND", NULL };
1597 if (!compare_to_allowed_values ("ACCESS", access_f95, access_f2003,
1599 open->access->value.character.string,
1604 /* Checks on the ACTION specifier. */
1605 if (open->action && open->action->expr_type == EXPR_CONSTANT)
1607 static const char *action[] = { "READ", "WRITE", "READWRITE", NULL };
1609 if (!compare_to_allowed_values ("ACTION", action, NULL, NULL,
1610 open->action->value.character.string,
1615 /* Checks on the ASYNCHRONOUS specifier. */
1616 if (open->asynchronous)
1618 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASYNCHRONOUS= at %C "
1619 "not allowed in Fortran 95") == FAILURE)
1622 if (open->asynchronous->expr_type == EXPR_CONSTANT)
1624 static const char * asynchronous[] = { "YES", "NO", NULL };
1626 if (!compare_to_allowed_values ("ASYNCHRONOUS", asynchronous,
1627 NULL, NULL, open->asynchronous->value.character.string,
1633 /* Checks on the BLANK specifier. */
1636 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: BLANK= at %C "
1637 "not allowed in Fortran 95") == FAILURE)
1640 if (open->blank->expr_type == EXPR_CONSTANT)
1642 static const char *blank[] = { "ZERO", "NULL", NULL };
1644 if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
1645 open->blank->value.character.string,
1651 /* Checks on the DECIMAL specifier. */
1654 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DECIMAL= at %C "
1655 "not allowed in Fortran 95") == FAILURE)
1658 if (open->decimal->expr_type == EXPR_CONSTANT)
1660 static const char * decimal[] = { "COMMA", "POINT", NULL };
1662 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1663 open->decimal->value.character.string,
1669 /* Checks on the DELIM specifier. */
1672 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DELIM= at %C "
1673 "not allowed in Fortran 95") == FAILURE)
1676 if (open->delim->expr_type == EXPR_CONSTANT)
1678 static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
1680 if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
1681 open->delim->value.character.string,
1687 /* Checks on the ENCODING specifier. */
1690 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENCODING= at %C "
1691 "not allowed in Fortran 95") == FAILURE)
1694 if (open->encoding->expr_type == EXPR_CONSTANT)
1696 /* TODO: Implement UTF-8 here. */
1697 static const char * encoding[] = { "DEFAULT", NULL };
1699 if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1700 open->encoding->value.character.string,
1706 /* Checks on the FORM specifier. */
1707 if (open->form && open->form->expr_type == EXPR_CONSTANT)
1709 static const char *form[] = { "FORMATTED", "UNFORMATTED", NULL };
1711 if (!compare_to_allowed_values ("FORM", form, NULL, NULL,
1712 open->form->value.character.string,
1717 /* Checks on the PAD specifier. */
1718 if (open->pad && open->pad->expr_type == EXPR_CONSTANT)
1720 static const char *pad[] = { "YES", "NO", NULL };
1722 if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
1723 open->pad->value.character.string,
1728 /* Checks on the POSITION specifier. */
1729 if (open->position && open->position->expr_type == EXPR_CONSTANT)
1731 static const char *position[] = { "ASIS", "REWIND", "APPEND", NULL };
1733 if (!compare_to_allowed_values ("POSITION", position, NULL, NULL,
1734 open->position->value.character.string,
1739 /* Checks on the ROUND specifier. */
1742 /* When implemented, change the following to use gfc_notify_std F2003. */
1743 gfc_error ("Fortran F2003: ROUND= specifier at %C not implemented");
1746 if (open->round->expr_type == EXPR_CONSTANT)
1748 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1749 "COMPATIBLE", "PROCESSOR_DEFINED",
1752 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1753 open->round->value.character.string,
1759 /* Checks on the SIGN specifier. */
1762 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: SIGN= at %C "
1763 "not allowed in Fortran 95") == FAILURE)
1766 if (open->sign->expr_type == EXPR_CONSTANT)
1768 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1771 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1772 open->sign->value.character.string,
1778 #define warn_or_error(...) \
1781 gfc_warning (__VA_ARGS__); \
1784 gfc_error (__VA_ARGS__); \
1789 /* Checks on the RECL specifier. */
1790 if (open->recl && open->recl->expr_type == EXPR_CONSTANT
1791 && open->recl->ts.type == BT_INTEGER
1792 && mpz_sgn (open->recl->value.integer) != 1)
1794 warn_or_error ("RECL in OPEN statement at %C must be positive");
1797 /* Checks on the STATUS specifier. */
1798 if (open->status && open->status->expr_type == EXPR_CONSTANT)
1800 static const char *status[] = { "OLD", "NEW", "SCRATCH",
1801 "REPLACE", "UNKNOWN", NULL };
1803 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1804 open->status->value.character.string,
1808 /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1809 the FILE= specifier shall appear. */
1810 if (open->file == NULL
1811 && (gfc_wide_strncasecmp (open->status->value.character.string,
1813 || gfc_wide_strncasecmp (open->status->value.character.string,
1816 char *s = gfc_widechar_to_char (open->status->value.character.string,
1818 warn_or_error ("The STATUS specified in OPEN statement at %C is "
1819 "'%s' and no FILE specifier is present", s);
1823 /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1824 the FILE= specifier shall not appear. */
1825 if (gfc_wide_strncasecmp (open->status->value.character.string,
1826 "scratch", 7) == 0 && open->file)
1828 warn_or_error ("The STATUS specified in OPEN statement at %C "
1829 "cannot have the value SCRATCH if a FILE specifier "
1834 /* Things that are not allowed for unformatted I/O. */
1835 if (open->form && open->form->expr_type == EXPR_CONSTANT
1836 && (open->delim || open->decimal || open->encoding || open->round
1837 || open->sign || open->pad || open->blank)
1838 && gfc_wide_strncasecmp (open->form->value.character.string,
1839 "unformatted", 11) == 0)
1841 const char *spec = (open->delim ? "DELIM "
1842 : (open->pad ? "PAD " : open->blank
1845 warn_or_error ("%s specifier at %C not allowed in OPEN statement for "
1846 "unformatted I/O", spec);
1849 if (open->recl && open->access && open->access->expr_type == EXPR_CONSTANT
1850 && gfc_wide_strncasecmp (open->access->value.character.string,
1853 warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
1858 && open->access && open->access->expr_type == EXPR_CONSTANT
1859 && !(gfc_wide_strncasecmp (open->access->value.character.string,
1860 "sequential", 10) == 0
1861 || gfc_wide_strncasecmp (open->access->value.character.string,
1863 || gfc_wide_strncasecmp (open->access->value.character.string,
1866 warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
1867 "for stream or sequential ACCESS");
1870 #undef warn_or_error
1872 new_st.op = EXEC_OPEN;
1873 new_st.ext.open = open;
1877 gfc_syntax_error (ST_OPEN);
1880 gfc_free_open (open);
1885 /* Free a gfc_close structure an all its expressions. */
1888 gfc_free_close (gfc_close *close)
1893 gfc_free_expr (close->unit);
1894 gfc_free_expr (close->iomsg);
1895 gfc_free_expr (close->iostat);
1896 gfc_free_expr (close->status);
1901 /* Match elements of a CLOSE statement. */
1904 match_close_element (gfc_close *close)
1908 m = match_etag (&tag_unit, &close->unit);
1911 m = match_etag (&tag_status, &close->status);
1914 m = match_out_tag (&tag_iomsg, &close->iomsg);
1917 m = match_out_tag (&tag_iostat, &close->iostat);
1920 m = match_ltag (&tag_err, &close->err);
1928 /* Match a CLOSE statement. */
1931 gfc_match_close (void)
1937 m = gfc_match_char ('(');
1941 close = XCNEW (gfc_close);
1943 m = match_close_element (close);
1945 if (m == MATCH_ERROR)
1949 m = gfc_match_expr (&close->unit);
1952 if (m == MATCH_ERROR)
1958 if (gfc_match_char (')') == MATCH_YES)
1960 if (gfc_match_char (',') != MATCH_YES)
1963 m = match_close_element (close);
1964 if (m == MATCH_ERROR)
1970 if (gfc_match_eos () == MATCH_NO)
1973 if (gfc_pure (NULL))
1975 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1979 warn = (close->iostat || close->err) ? true : false;
1981 /* Checks on the STATUS specifier. */
1982 if (close->status && close->status->expr_type == EXPR_CONSTANT)
1984 static const char *status[] = { "KEEP", "DELETE", NULL };
1986 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1987 close->status->value.character.string,
1992 new_st.op = EXEC_CLOSE;
1993 new_st.ext.close = close;
1997 gfc_syntax_error (ST_CLOSE);
2000 gfc_free_close (close);
2005 /* Resolve everything in a gfc_close structure. */
2008 gfc_resolve_close (gfc_close *close)
2010 RESOLVE_TAG (&tag_unit, close->unit);
2011 RESOLVE_TAG (&tag_iomsg, close->iomsg);
2012 RESOLVE_TAG (&tag_iostat, close->iostat);
2013 RESOLVE_TAG (&tag_status, close->status);
2015 if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
2022 /* Free a gfc_filepos structure. */
2025 gfc_free_filepos (gfc_filepos *fp)
2027 gfc_free_expr (fp->unit);
2028 gfc_free_expr (fp->iomsg);
2029 gfc_free_expr (fp->iostat);
2034 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
2037 match_file_element (gfc_filepos *fp)
2041 m = match_etag (&tag_unit, &fp->unit);
2044 m = match_out_tag (&tag_iomsg, &fp->iomsg);
2047 m = match_out_tag (&tag_iostat, &fp->iostat);
2050 m = match_ltag (&tag_err, &fp->err);
2058 /* Match the second half of the file-positioning statements, REWIND,
2059 BACKSPACE, ENDFILE, or the FLUSH statement. */
2062 match_filepos (gfc_statement st, gfc_exec_op op)
2067 fp = XCNEW (gfc_filepos);
2069 if (gfc_match_char ('(') == MATCH_NO)
2071 m = gfc_match_expr (&fp->unit);
2072 if (m == MATCH_ERROR)
2080 m = match_file_element (fp);
2081 if (m == MATCH_ERROR)
2085 m = gfc_match_expr (&fp->unit);
2086 if (m == MATCH_ERROR)
2094 if (gfc_match_char (')') == MATCH_YES)
2096 if (gfc_match_char (',') != MATCH_YES)
2099 m = match_file_element (fp);
2100 if (m == MATCH_ERROR)
2107 if (gfc_match_eos () != MATCH_YES)
2110 if (gfc_pure (NULL))
2112 gfc_error ("%s statement not allowed in PURE procedure at %C",
2113 gfc_ascii_statement (st));
2119 new_st.ext.filepos = fp;
2123 gfc_syntax_error (st);
2126 gfc_free_filepos (fp);
2132 gfc_resolve_filepos (gfc_filepos *fp)
2134 RESOLVE_TAG (&tag_unit, fp->unit);
2135 RESOLVE_TAG (&tag_iostat, fp->iostat);
2136 RESOLVE_TAG (&tag_iomsg, fp->iomsg);
2137 if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
2144 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
2145 and the FLUSH statement. */
2148 gfc_match_endfile (void)
2150 return match_filepos (ST_END_FILE, EXEC_ENDFILE);
2154 gfc_match_backspace (void)
2156 return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
2160 gfc_match_rewind (void)
2162 return match_filepos (ST_REWIND, EXEC_REWIND);
2166 gfc_match_flush (void)
2168 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: FLUSH statement at %C")
2172 return match_filepos (ST_FLUSH, EXEC_FLUSH);
2175 /******************** Data Transfer Statements *********************/
2177 /* Return a default unit number. */
2180 default_unit (io_kind k)
2189 return gfc_int_expr (unit);
2193 /* Match a unit specification for a data transfer statement. */
2196 match_dt_unit (io_kind k, gfc_dt *dt)
2200 if (gfc_match_char ('*') == MATCH_YES)
2202 if (dt->io_unit != NULL)
2205 dt->io_unit = default_unit (k);
2209 if (gfc_match_expr (&e) == MATCH_YES)
2211 if (dt->io_unit != NULL)
2224 gfc_error ("Duplicate UNIT specification at %C");
2229 /* Match a format specification. */
2232 match_dt_format (gfc_dt *dt)
2236 gfc_st_label *label;
2239 where = gfc_current_locus;
2241 if (gfc_match_char ('*') == MATCH_YES)
2243 if (dt->format_expr != NULL || dt->format_label != NULL)
2246 dt->format_label = &format_asterisk;
2250 if ((m = gfc_match_st_label (&label)) == MATCH_YES)
2252 if (dt->format_expr != NULL || dt->format_label != NULL)
2254 gfc_free_st_label (label);
2258 if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
2261 dt->format_label = label;
2264 else if (m == MATCH_ERROR)
2265 /* The label was zero or too large. Emit the correct diagnosis. */
2268 if (gfc_match_expr (&e) == MATCH_YES)
2270 if (dt->format_expr != NULL || dt->format_label != NULL)
2275 dt->format_expr = e;
2279 gfc_current_locus = where; /* The only case where we have to restore */
2284 gfc_error ("Duplicate format specification at %C");
2289 /* Traverse a namelist that is part of a READ statement to make sure
2290 that none of the variables in the namelist are INTENT(IN). Returns
2291 nonzero if we find such a variable. */
2294 check_namelist (gfc_symbol *sym)
2298 for (p = sym->namelist; p; p = p->next)
2299 if (p->sym->attr.intent == INTENT_IN)
2301 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2302 p->sym->name, sym->name);
2310 /* Match a single data transfer element. */
2313 match_dt_element (io_kind k, gfc_dt *dt)
2315 char name[GFC_MAX_SYMBOL_LEN + 1];
2319 if (gfc_match (" unit =") == MATCH_YES)
2321 m = match_dt_unit (k, dt);
2326 if (gfc_match (" fmt =") == MATCH_YES)
2328 m = match_dt_format (dt);
2333 if (gfc_match (" nml = %n", name) == MATCH_YES)
2335 if (dt->namelist != NULL)
2337 gfc_error ("Duplicate NML specification at %C");
2341 if (gfc_find_symbol (name, NULL, 1, &sym))
2344 if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
2346 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2347 sym != NULL ? sym->name : name);
2352 if (k == M_READ && check_namelist (sym))
2358 m = match_etag (&tag_e_async, &dt->asynchronous);
2361 m = match_etag (&tag_e_blank, &dt->blank);
2364 m = match_etag (&tag_e_delim, &dt->delim);
2367 m = match_etag (&tag_e_pad, &dt->pad);
2370 m = match_etag (&tag_e_sign, &dt->sign);
2373 m = match_etag (&tag_e_round, &dt->round);
2376 m = match_out_tag (&tag_id, &dt->id);
2379 m = match_etag (&tag_e_decimal, &dt->decimal);
2382 m = match_etag (&tag_rec, &dt->rec);
2385 m = match_etag (&tag_spos, &dt->rec);
2388 m = match_out_tag (&tag_iomsg, &dt->iomsg);
2391 m = match_out_tag (&tag_iostat, &dt->iostat);
2394 m = match_ltag (&tag_err, &dt->err);
2396 dt->err_where = gfc_current_locus;
2399 m = match_etag (&tag_advance, &dt->advance);
2402 m = match_out_tag (&tag_size, &dt->size);
2406 m = match_ltag (&tag_end, &dt->end);
2411 gfc_error ("END tag at %C not allowed in output statement");
2414 dt->end_where = gfc_current_locus;
2419 m = match_ltag (&tag_eor, &dt->eor);
2421 dt->eor_where = gfc_current_locus;
2429 /* Free a data transfer structure and everything below it. */
2432 gfc_free_dt (gfc_dt *dt)
2437 gfc_free_expr (dt->io_unit);
2438 gfc_free_expr (dt->format_expr);
2439 gfc_free_expr (dt->rec);
2440 gfc_free_expr (dt->advance);
2441 gfc_free_expr (dt->iomsg);
2442 gfc_free_expr (dt->iostat);
2443 gfc_free_expr (dt->size);
2444 gfc_free_expr (dt->pad);
2445 gfc_free_expr (dt->delim);
2446 gfc_free_expr (dt->sign);
2447 gfc_free_expr (dt->round);
2448 gfc_free_expr (dt->blank);
2449 gfc_free_expr (dt->decimal);
2450 gfc_free_expr (dt->extra_comma);
2455 /* Resolve everything in a gfc_dt structure. */
2458 gfc_resolve_dt (gfc_dt *dt)
2462 RESOLVE_TAG (&tag_format, dt->format_expr);
2463 RESOLVE_TAG (&tag_rec, dt->rec);
2464 RESOLVE_TAG (&tag_spos, dt->rec);
2465 RESOLVE_TAG (&tag_advance, dt->advance);
2466 RESOLVE_TAG (&tag_iomsg, dt->iomsg);
2467 RESOLVE_TAG (&tag_iostat, dt->iostat);
2468 RESOLVE_TAG (&tag_size, dt->size);
2469 RESOLVE_TAG (&tag_e_pad, dt->pad);
2470 RESOLVE_TAG (&tag_e_delim, dt->delim);
2471 RESOLVE_TAG (&tag_e_sign, dt->sign);
2472 RESOLVE_TAG (&tag_e_round, dt->round);
2473 RESOLVE_TAG (&tag_e_blank, dt->blank);
2474 RESOLVE_TAG (&tag_e_decimal, dt->decimal);
2477 if (gfc_resolve_expr (e) == SUCCESS
2478 && (e->ts.type != BT_INTEGER
2479 && (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_VARIABLE)))
2481 /* If there is no extra comma signifying the "format" form of the IO
2482 statement, then this must be an error. */
2483 if (!dt->extra_comma)
2485 gfc_error ("UNIT specification at %L must be an INTEGER expression "
2486 "or a CHARACTER variable", &e->where);
2491 /* At this point, we have an extra comma. If io_unit has arrived as
2492 type chracter, we assume its really the "format" form of the I/O
2493 statement. We set the io_unit to the default unit and format to
2494 the chracter expression. See F95 Standard section 9.4. */
2496 k = dt->extra_comma->value.iokind;
2497 if (e->ts.type == BT_CHARACTER && (k == M_READ || k == M_PRINT))
2499 dt->format_expr = dt->io_unit;
2500 dt->io_unit = default_unit (k);
2502 /* Free this pointer now so that a warning/error is not triggered
2503 below for the "Extension". */
2504 gfc_free_expr (dt->extra_comma);
2505 dt->extra_comma = NULL;
2510 gfc_error ("Invalid form of WRITE statement at %L, UNIT required",
2511 &dt->extra_comma->where);
2517 if (e->ts.type == BT_CHARACTER)
2519 if (gfc_has_vector_index (e))
2521 gfc_error ("Internal unit with vector subscript at %L", &e->where);
2526 if (e->rank && e->ts.type != BT_CHARACTER)
2528 gfc_error ("External IO UNIT cannot be an array at %L", &e->where);
2533 && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before i/o "
2534 "item list at %L", &dt->extra_comma->where) == FAILURE)
2539 if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
2541 if (dt->err->defined == ST_LABEL_UNKNOWN)
2543 gfc_error ("ERR tag label %d at %L not defined",
2544 dt->err->value, &dt->err_where);
2551 if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
2553 if (dt->end->defined == ST_LABEL_UNKNOWN)
2555 gfc_error ("END tag label %d at %L not defined",
2556 dt->end->value, &dt->end_where);
2563 if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
2565 if (dt->eor->defined == ST_LABEL_UNKNOWN)
2567 gfc_error ("EOR tag label %d at %L not defined",
2568 dt->eor->value, &dt->eor_where);
2573 /* Check the format label actually exists. */
2574 if (dt->format_label && dt->format_label != &format_asterisk
2575 && dt->format_label->defined == ST_LABEL_UNKNOWN)
2577 gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
2578 &dt->format_label->where);
2585 /* Given an io_kind, return its name. */
2588 io_kind_name (io_kind k)
2607 gfc_internal_error ("io_kind_name(): bad I/O-kind");
2614 /* Match an IO iteration statement of the form:
2616 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2618 which is equivalent to a single IO element. This function is
2619 mutually recursive with match_io_element(). */
2621 static match match_io_element (io_kind, gfc_code **);
2624 match_io_iterator (io_kind k, gfc_code **result)
2626 gfc_code *head, *tail, *new;
2634 old_loc = gfc_current_locus;
2636 if (gfc_match_char ('(') != MATCH_YES)
2639 m = match_io_element (k, &head);
2642 if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
2648 /* Can't be anything but an IO iterator. Build a list. */
2649 iter = gfc_get_iterator ();
2653 m = gfc_match_iterator (iter, 0);
2654 if (m == MATCH_ERROR)
2658 gfc_check_do_variable (iter->var->symtree);
2662 m = match_io_element (k, &new);
2663 if (m == MATCH_ERROR)
2672 tail = gfc_append_code (tail, new);
2674 if (gfc_match_char (',') != MATCH_YES)
2683 if (gfc_match_char (')') != MATCH_YES)
2686 new = gfc_get_code ();
2688 new->ext.iterator = iter;
2690 new->block = gfc_get_code ();
2691 new->block->op = EXEC_DO;
2692 new->block->next = head;
2698 gfc_error ("Syntax error in I/O iterator at %C");
2702 gfc_free_iterator (iter, 1);
2703 gfc_free_statements (head);
2704 gfc_current_locus = old_loc;
2709 /* Match a single element of an IO list, which is either a single
2710 expression or an IO Iterator. */
2713 match_io_element (io_kind k, gfc_code **cpp)
2721 m = match_io_iterator (k, cpp);
2727 m = gfc_match_variable (&expr, 0);
2729 gfc_error ("Expected variable in READ statement at %C");
2733 m = gfc_match_expr (&expr);
2735 gfc_error ("Expected expression in %s statement at %C",
2743 if (expr->symtree->n.sym->attr.intent == INTENT_IN)
2745 gfc_error ("Variable '%s' in input list at %C cannot be "
2746 "INTENT(IN)", expr->symtree->n.sym->name);
2751 && gfc_impure_variable (expr->symtree->n.sym)
2752 && current_dt->io_unit->ts.type == BT_CHARACTER)
2754 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2755 expr->symtree->n.sym->name);
2759 if (gfc_check_do_variable (expr->symtree))
2765 if (current_dt->io_unit->ts.type == BT_CHARACTER
2767 && current_dt->io_unit->expr_type == EXPR_VARIABLE
2768 && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
2770 gfc_error ("Cannot write to internal file unit '%s' at %C "
2771 "inside a PURE procedure",
2772 current_dt->io_unit->symtree->n.sym->name);
2784 gfc_free_expr (expr);
2788 cp = gfc_get_code ();
2789 cp->op = EXEC_TRANSFER;
2797 /* Match an I/O list, building gfc_code structures as we go. */
2800 match_io_list (io_kind k, gfc_code **head_p)
2802 gfc_code *head, *tail, *new;
2805 *head_p = head = tail = NULL;
2806 if (gfc_match_eos () == MATCH_YES)
2811 m = match_io_element (k, &new);
2812 if (m == MATCH_ERROR)
2817 tail = gfc_append_code (tail, new);
2821 if (gfc_match_eos () == MATCH_YES)
2823 if (gfc_match_char (',') != MATCH_YES)
2831 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2834 gfc_free_statements (head);
2839 /* Attach the data transfer end node. */
2842 terminate_io (gfc_code *io_code)
2846 if (io_code == NULL)
2847 io_code = new_st.block;
2849 c = gfc_get_code ();
2850 c->op = EXEC_DT_END;
2852 /* Point to structure that is already there */
2853 c->ext.dt = new_st.ext.dt;
2854 gfc_append_code (io_code, c);
2858 /* Check the constraints for a data transfer statement. The majority of the
2859 constraints appearing in 9.4 of the standard appear here. Some are handled
2860 in resolve_tag and others in gfc_resolve_dt. */
2863 check_io_constraints (io_kind k, gfc_dt *dt, gfc_code *io_code,
2866 #define io_constraint(condition,msg,arg)\
2869 gfc_error(msg,arg);\
2875 gfc_symbol *sym = NULL;
2876 bool warn, unformatted;
2878 warn = (dt->err || dt->iostat) ? true : false;
2879 unformatted = dt->format_expr == NULL && dt->format_label == NULL
2880 && dt->namelist == NULL;
2885 if (expr && expr->expr_type == EXPR_VARIABLE
2886 && expr->ts.type == BT_CHARACTER)
2888 sym = expr->symtree->n.sym;
2890 io_constraint (k == M_WRITE && sym->attr.intent == INTENT_IN,
2891 "Internal file at %L must not be INTENT(IN)",
2894 io_constraint (gfc_has_vector_index (dt->io_unit),
2895 "Internal file incompatible with vector subscript at %L",
2898 io_constraint (dt->rec != NULL,
2899 "REC tag at %L is incompatible with internal file",
2902 io_constraint (unformatted,
2903 "Unformatted I/O not allowed with internal unit at %L",
2904 &dt->io_unit->where);
2906 io_constraint (dt->asynchronous != NULL,
2907 "ASYNCHRONOUS tag at %L not allowed with internal file",
2908 &dt->asynchronous->where);
2910 if (dt->namelist != NULL)
2912 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Internal file "
2913 "at %L with namelist", &expr->where)
2918 io_constraint (dt->advance != NULL,
2919 "ADVANCE tag at %L is incompatible with internal file",
2920 &dt->advance->where);
2923 if (expr && expr->ts.type != BT_CHARACTER)
2926 io_constraint (gfc_pure (NULL) && (k == M_READ || k == M_WRITE),
2927 "IO UNIT in %s statement at %C must be "
2928 "an internal file in a PURE procedure",
2934 io_constraint (dt->end, "END tag not allowed with output at %L",
2937 io_constraint (dt->eor, "EOR tag not allowed with output at %L",
2940 io_constraint (dt->blank, "BLANK= specifier not allowed with output at %L",
2943 io_constraint (dt->pad, "PAD= specifier not allowed with output at %L",
2946 io_constraint (dt->size, "SIZE= specifier not allowed with output at %L",
2951 io_constraint (dt->size && dt->advance == NULL,
2952 "SIZE tag at %L requires an ADVANCE tag",
2955 io_constraint (dt->eor && dt->advance == NULL,
2956 "EOR tag at %L requires an ADVANCE tag",
2960 if (dt->asynchronous)
2962 static const char * asynchronous[] = { "YES", "NO", NULL };
2964 if (dt->asynchronous->expr_type != EXPR_CONSTANT)
2966 gfc_error ("ASYNCHRONOUS= specifier at %L must be an initialization "
2967 "expression", &dt->asynchronous->where);
2971 if (!compare_to_allowed_values
2972 ("ASYNCHRONOUS", asynchronous, NULL, NULL,
2973 dt->asynchronous->value.character.string,
2974 io_kind_name (k), warn))
2982 || gfc_wide_strlen (dt->asynchronous->value.character.string) != 3
2983 || gfc_wide_strncasecmp (dt->asynchronous->value.character.string,
2985 io_constraint (not_yes,
2986 "ID= specifier at %L must be with ASYNCHRONOUS='yes' "
2987 "specifier", &dt->id->where);
2992 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DECIMAL= at %C "
2993 "not allowed in Fortran 95") == FAILURE)
2996 if (dt->decimal->expr_type == EXPR_CONSTANT)
2998 static const char * decimal[] = { "COMMA", "POINT", NULL };
3000 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
3001 dt->decimal->value.character.string,
3002 io_kind_name (k), warn))
3005 io_constraint (unformatted,
3006 "the DECIMAL= specifier at %L must be with an "
3007 "explicit format expression", &dt->decimal->where);
3013 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: BLANK= at %C "
3014 "not allowed in Fortran 95") == FAILURE)
3017 if (dt->blank->expr_type == EXPR_CONSTANT)
3019 static const char * blank[] = { "NULL", "ZERO", NULL };
3021 if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
3022 dt->blank->value.character.string,
3023 io_kind_name (k), warn))
3026 io_constraint (unformatted,
3027 "the BLANK= specifier at %L must be with an "
3028 "explicit format expression", &dt->blank->where);
3034 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PAD= at %C "
3035 "not allowed in Fortran 95") == FAILURE)
3038 if (dt->pad->expr_type == EXPR_CONSTANT)
3040 static const char * pad[] = { "YES", "NO", NULL };
3042 if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
3043 dt->pad->value.character.string,
3044 io_kind_name (k), warn))
3047 io_constraint (unformatted,
3048 "the PAD= specifier at %L must be with an "
3049 "explicit format expression", &dt->pad->where);
3055 /* When implemented, change the following to use gfc_notify_std F2003.
3056 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ROUND= at %C "
3057 "not allowed in Fortran 95") == FAILURE)
3058 return MATCH_ERROR; */
3059 gfc_error ("F2003 Feature: ROUND= specifier at %C not implemented");
3062 if (dt->round->expr_type == EXPR_CONSTANT)
3064 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
3065 "COMPATIBLE", "PROCESSOR_DEFINED",
3068 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
3069 dt->round->value.character.string,
3070 io_kind_name (k), warn))
3077 /* When implemented, change the following to use gfc_notify_std F2003.
3078 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: SIGN= at %C "
3079 "not allowed in Fortran 95") == FAILURE)
3080 return MATCH_ERROR; */
3081 if (dt->sign->expr_type == EXPR_CONSTANT)
3083 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
3086 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
3087 dt->sign->value.character.string,
3088 io_kind_name (k), warn))
3091 io_constraint (unformatted,
3092 "SIGN= specifier at %L must be with an "
3093 "explicit format expression", &dt->sign->where);
3095 io_constraint (k == M_READ,
3096 "SIGN= specifier at %L not allowed in a "
3097 "READ statement", &dt->sign->where);
3103 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DELIM= at %C "
3104 "not allowed in Fortran 95") == FAILURE)
3107 if (dt->delim->expr_type == EXPR_CONSTANT)
3109 static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
3111 if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
3112 dt->delim->value.character.string,
3113 io_kind_name (k), warn))
3116 io_constraint (k == M_READ,
3117 "DELIM= specifier at %L not allowed in a "
3118 "READ statement", &dt->delim->where);
3120 io_constraint (dt->format_label != &format_asterisk
3121 && dt->namelist == NULL,
3122 "DELIM= specifier at %L must have FMT=*",
3125 io_constraint (unformatted && dt->namelist == NULL,
3126 "DELIM= specifier at %L must be with FMT=* or "
3127 "NML= specifier ", &dt->delim->where);
3133 io_constraint (io_code && dt->namelist,
3134 "NAMELIST cannot be followed by IO-list at %L",
3137 io_constraint (dt->format_expr,
3138 "IO spec-list cannot contain both NAMELIST group name "
3139 "and format specification at %L.",
3140 &dt->format_expr->where);
3142 io_constraint (dt->format_label,
3143 "IO spec-list cannot contain both NAMELIST group name "
3144 "and format label at %L", spec_end);
3146 io_constraint (dt->rec,
3147 "NAMELIST IO is not allowed with a REC= specifier "
3148 "at %L.", &dt->rec->where);
3150 io_constraint (dt->advance,
3151 "NAMELIST IO is not allowed with a ADVANCE= specifier "
3152 "at %L.", &dt->advance->where);
3157 io_constraint (dt->end,
3158 "An END tag is not allowed with a "
3159 "REC= specifier at %L.", &dt->end_where);
3161 io_constraint (dt->format_label == &format_asterisk,
3162 "FMT=* is not allowed with a REC= specifier "
3163 "at %L.", spec_end);
3168 int not_yes, not_no;
3171 io_constraint (dt->format_label == &format_asterisk,
3172 "List directed format(*) is not allowed with a "
3173 "ADVANCE= specifier at %L.", &expr->where);
3175 io_constraint (unformatted,
3176 "the ADVANCE= specifier at %L must appear with an "
3177 "explicit format expression", &expr->where);
3179 if (expr->expr_type == EXPR_CONSTANT && expr->ts.type == BT_CHARACTER)
3181 const gfc_char_t *advance = expr->value.character.string;
3182 not_no = gfc_wide_strlen (advance) != 2
3183 || gfc_wide_strncasecmp (advance, "no", 2) != 0;
3184 not_yes = gfc_wide_strlen (advance) != 3
3185 || gfc_wide_strncasecmp (advance, "yes", 3) != 0;
3193 io_constraint (not_no && not_yes,
3194 "ADVANCE= specifier at %L must have value = "
3195 "YES or NO.", &expr->where);
3197 io_constraint (dt->size && not_no && k == M_READ,
3198 "SIZE tag at %L requires an ADVANCE = 'NO'",
3201 io_constraint (dt->eor && not_no && k == M_READ,
3202 "EOR tag at %L requires an ADVANCE = 'NO'",
3206 expr = dt->format_expr;
3207 if (gfc_simplify_expr (expr, 0) == FAILURE
3208 || check_format_string (expr, k == M_READ) == FAILURE)
3213 #undef io_constraint
3216 /* Match a READ, WRITE or PRINT statement. */
3219 match_io (io_kind k)
3221 char name[GFC_MAX_SYMBOL_LEN + 1];
3230 where = gfc_current_locus;
3232 current_dt = dt = XCNEW (gfc_dt);
3233 m = gfc_match_char ('(');
3236 where = gfc_current_locus;
3239 else if (k == M_PRINT)
3241 /* Treat the non-standard case of PRINT namelist. */
3242 if ((gfc_current_form == FORM_FIXED || gfc_peek_ascii_char () == ' ')
3243 && gfc_match_name (name) == MATCH_YES)
3245 gfc_find_symbol (name, NULL, 1, &sym);
3246 if (sym && sym->attr.flavor == FL_NAMELIST)
3248 if (gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
3249 "%C is an extension") == FAILURE)
3255 dt->io_unit = default_unit (k);
3260 gfc_current_locus = where;
3264 if (gfc_current_form == FORM_FREE)
3266 char c = gfc_peek_ascii_char ();
3267 if (c != ' ' && c != '*' && c != '\'' && c != '"')
3274 m = match_dt_format (dt);
3275 if (m == MATCH_ERROR)
3281 dt->io_unit = default_unit (k);
3286 /* Before issuing an error for a malformed 'print (1,*)' type of
3287 error, check for a default-char-expr of the form ('(I0)'). */
3288 if (k == M_PRINT && m == MATCH_YES)
3290 /* Reset current locus to get the initial '(' in an expression. */
3291 gfc_current_locus = where;
3292 dt->format_expr = NULL;
3293 m = match_dt_format (dt);
3295 if (m == MATCH_ERROR)
3297 if (m == MATCH_NO || dt->format_expr == NULL)
3301 dt->io_unit = default_unit (k);
3306 /* Match a control list */
3307 if (match_dt_element (k, dt) == MATCH_YES)
3309 if (match_dt_unit (k, dt) != MATCH_YES)
3312 if (gfc_match_char (')') == MATCH_YES)
3314 if (gfc_match_char (',') != MATCH_YES)
3317 m = match_dt_element (k, dt);
3320 if (m == MATCH_ERROR)
3323 m = match_dt_format (dt);
3326 if (m == MATCH_ERROR)
3329 where = gfc_current_locus;
3331 m = gfc_match_name (name);
3334 gfc_find_symbol (name, NULL, 1, &sym);
3335 if (sym && sym->attr.flavor == FL_NAMELIST)
3338 if (k == M_READ && check_namelist (sym))
3347 gfc_current_locus = where;
3349 goto loop; /* No matches, try regular elements */
3352 if (gfc_match_char (')') == MATCH_YES)
3354 if (gfc_match_char (',') != MATCH_YES)
3360 m = match_dt_element (k, dt);
3363 if (m == MATCH_ERROR)
3366 if (gfc_match_char (')') == MATCH_YES)
3368 if (gfc_match_char (',') != MATCH_YES)
3374 /* Used in check_io_constraints, where no locus is available. */
3375 spec_end = gfc_current_locus;
3377 /* Optional leading comma (non-standard). We use a gfc_expr structure here
3378 to save the locus. This is used later when resolving transfer statements
3379 that might have a format expression without unit number. */
3380 if (!comma_flag && gfc_match_char (',') == MATCH_YES)
3382 dt->extra_comma = gfc_get_expr ();
3384 /* Set the types to something compatible with iokind. This is needed to
3385 get through gfc_free_expr later since iokind really has no Basic Type,
3387 dt->extra_comma->expr_type = EXPR_CONSTANT;
3388 dt->extra_comma->ts.type = BT_LOGICAL;
3390 /* Save the iokind and locus for later use in resolution. */
3391 dt->extra_comma->value.iokind = k;
3392 dt->extra_comma->where = gfc_current_locus;
3396 if (gfc_match_eos () != MATCH_YES)
3398 if (comma_flag && gfc_match_char (',') != MATCH_YES)
3400 gfc_error ("Expected comma in I/O list at %C");
3405 m = match_io_list (k, &io_code);
3406 if (m == MATCH_ERROR)
3412 /* A full IO statement has been matched. Check the constraints. spec_end is
3413 supplied for cases where no locus is supplied. */
3414 m = check_io_constraints (k, dt, io_code, &spec_end);
3416 if (m == MATCH_ERROR)
3419 new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
3421 new_st.block = gfc_get_code ();
3422 new_st.block->op = new_st.op;
3423 new_st.block->next = io_code;
3425 terminate_io (io_code);
3430 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
3440 gfc_match_read (void)
3442 return match_io (M_READ);
3447 gfc_match_write (void)
3449 return match_io (M_WRITE);
3454 gfc_match_print (void)
3458 m = match_io (M_PRINT);
3462 if (gfc_pure (NULL))
3464 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
3472 /* Free a gfc_inquire structure. */
3475 gfc_free_inquire (gfc_inquire *inquire)
3478 if (inquire == NULL)
3481 gfc_free_expr (inquire->unit);
3482 gfc_free_expr (inquire->file);
3483 gfc_free_expr (inquire->iomsg);
3484 gfc_free_expr (inquire->iostat);
3485 gfc_free_expr (inquire->exist);
3486 gfc_free_expr (inquire->opened);
3487 gfc_free_expr (inquire->number);
3488 gfc_free_expr (inquire->named);
3489 gfc_free_expr (inquire->name);
3490 gfc_free_expr (inquire->access);
3491 gfc_free_expr (inquire->sequential);
3492 gfc_free_expr (inquire->direct);
3493 gfc_free_expr (inquire->form);
3494 gfc_free_expr (inquire->formatted);
3495 gfc_free_expr (inquire->unformatted);
3496 gfc_free_expr (inquire->recl);
3497 gfc_free_expr (inquire->nextrec);
3498 gfc_free_expr (inquire->blank);
3499 gfc_free_expr (inquire->position);
3500 gfc_free_expr (inquire->action);
3501 gfc_free_expr (inquire->read);
3502 gfc_free_expr (inquire->write);
3503 gfc_free_expr (inquire->readwrite);
3504 gfc_free_expr (inquire->delim);
3505 gfc_free_expr (inquire->encoding);
3506 gfc_free_expr (inquire->pad);
3507 gfc_free_expr (inquire->iolength);
3508 gfc_free_expr (inquire->convert);
3509 gfc_free_expr (inquire->strm_pos);
3510 gfc_free_expr (inquire->asynchronous);
3511 gfc_free_expr (inquire->pending);
3512 gfc_free_expr (inquire->id);
3513 gfc_free_expr (inquire->sign);
3514 gfc_free_expr (inquire->round);
3519 /* Match an element of an INQUIRE statement. */
3521 #define RETM if (m != MATCH_NO) return m;
3524 match_inquire_element (gfc_inquire *inquire)
3528 m = match_etag (&tag_unit, &inquire->unit);
3529 RETM m = match_etag (&tag_file, &inquire->file);
3530 RETM m = match_ltag (&tag_err, &inquire->err);
3531 RETM m = match_out_tag (&tag_iomsg, &inquire->iomsg);
3532 RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
3533 RETM m = match_vtag (&tag_exist, &inquire->exist);
3534 RETM m = match_vtag (&tag_opened, &inquire->opened);
3535 RETM m = match_vtag (&tag_named, &inquire->named);
3536 RETM m = match_vtag (&tag_name, &inquire->name);
3537 RETM m = match_out_tag (&tag_number, &inquire->number);
3538 RETM m = match_vtag (&tag_s_access, &inquire->access);
3539 RETM m = match_vtag (&tag_sequential, &inquire->sequential);
3540 RETM m = match_vtag (&tag_direct, &inquire->direct);
3541 RETM m = match_vtag (&tag_s_form, &inquire->form);
3542 RETM m = match_vtag (&tag_formatted, &inquire->formatted);
3543 RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
3544 RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
3545 RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
3546 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
3547 RETM m = match_vtag (&tag_s_position, &inquire->position);
3548 RETM m = match_vtag (&tag_s_action, &inquire->action);
3549 RETM m = match_vtag (&tag_read, &inquire->read);
3550 RETM m = match_vtag (&tag_write, &inquire->write);
3551 RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
3552 RETM m = match_vtag (&tag_s_async, &inquire->asynchronous);
3553 RETM m = match_vtag (&tag_s_delim, &inquire->delim);
3554 RETM m = match_vtag (&tag_s_decimal, &inquire->decimal);
3555 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
3556 RETM m = match_vtag (&tag_s_encoding, &inquire->encoding);
3557 RETM m = match_vtag (&tag_s_round, &inquire->round);
3558 RETM m = match_vtag (&tag_s_sign, &inquire->sign);
3559 RETM m = match_vtag (&tag_s_pad, &inquire->pad);
3560 RETM m = match_vtag (&tag_iolength, &inquire->iolength);
3561 RETM m = match_vtag (&tag_convert, &inquire->convert);
3562 RETM m = match_out_tag (&tag_strm_out, &inquire->strm_pos);
3563 RETM m = match_vtag (&tag_pending, &inquire->pending);
3564 RETM m = match_vtag (&tag_id, &inquire->id);
3565 RETM return MATCH_NO;
3572 gfc_match_inquire (void)
3574 gfc_inquire *inquire;
3579 m = gfc_match_char ('(');
3583 inquire = XCNEW (gfc_inquire);
3585 loc = gfc_current_locus;
3587 m = match_inquire_element (inquire);
3588 if (m == MATCH_ERROR)
3592 m = gfc_match_expr (&inquire->unit);
3593 if (m == MATCH_ERROR)
3599 /* See if we have the IOLENGTH form of the inquire statement. */
3600 if (inquire->iolength != NULL)
3602 if (gfc_match_char (')') != MATCH_YES)
3605 m = match_io_list (M_INQUIRE, &code);
3606 if (m == MATCH_ERROR)
3611 new_st.op = EXEC_IOLENGTH;
3612 new_st.expr = inquire->iolength;
3613 new_st.ext.inquire = inquire;
3615 if (gfc_pure (NULL))
3617 gfc_free_statements (code);
3618 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3622 new_st.block = gfc_get_code ();
3623 new_st.block->op = EXEC_IOLENGTH;
3624 terminate_io (code);
3625 new_st.block->next = code;
3629 /* At this point, we have the non-IOLENGTH inquire statement. */
3632 if (gfc_match_char (')') == MATCH_YES)
3634 if (gfc_match_char (',') != MATCH_YES)
3637 m = match_inquire_element (inquire);
3638 if (m == MATCH_ERROR)
3643 if (inquire->iolength != NULL)
3645 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3650 if (gfc_match_eos () != MATCH_YES)
3653 if (inquire->unit != NULL && inquire->file != NULL)
3655 gfc_error ("INQUIRE statement at %L cannot contain both FILE and "
3656 "UNIT specifiers", &loc);
3660 if (inquire->unit == NULL && inquire->file == NULL)
3662 gfc_error ("INQUIRE statement at %L requires either FILE or "
3663 "UNIT specifier", &loc);
3667 if (gfc_pure (NULL))
3669 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3673 if (inquire->id != NULL && inquire->pending == NULL)
3675 gfc_error ("INQUIRE statement at %L requires a PENDING= specifier with "
3676 "the ID= specifier", &loc);
3680 new_st.op = EXEC_INQUIRE;
3681 new_st.ext.inquire = inquire;
3685 gfc_syntax_error (ST_INQUIRE);
3688 gfc_free_inquire (inquire);
3693 /* Resolve everything in a gfc_inquire structure. */
3696 gfc_resolve_inquire (gfc_inquire *inquire)
3698 RESOLVE_TAG (&tag_unit, inquire->unit);
3699 RESOLVE_TAG (&tag_file, inquire->file);
3700 RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
3701 RESOLVE_TAG (&tag_iostat, inquire->iostat);
3702 RESOLVE_TAG (&tag_exist, inquire->exist);
3703 RESOLVE_TAG (&tag_opened, inquire->opened);
3704 RESOLVE_TAG (&tag_number, inquire->number);
3705 RESOLVE_TAG (&tag_named, inquire->named);
3706 RESOLVE_TAG (&tag_name, inquire->name);
3707 RESOLVE_TAG (&tag_s_access, inquire->access);
3708 RESOLVE_TAG (&tag_sequential, inquire->sequential);
3709 RESOLVE_TAG (&tag_direct, inquire->direct);
3710 RESOLVE_TAG (&tag_s_form, inquire->form);
3711 RESOLVE_TAG (&tag_formatted, inquire->formatted);
3712 RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
3713 RESOLVE_TAG (&tag_s_recl, inquire->recl);
3714 RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
3715 RESOLVE_TAG (&tag_s_blank, inquire->blank);
3716 RESOLVE_TAG (&tag_s_position, inquire->position);
3717 RESOLVE_TAG (&tag_s_action, inquire->action);
3718 RESOLVE_TAG (&tag_read, inquire->read);
3719 RESOLVE_TAG (&tag_write, inquire->write);
3720 RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
3721 RESOLVE_TAG (&tag_s_delim, inquire->delim);
3722 RESOLVE_TAG (&tag_s_pad, inquire->pad);
3723 RESOLVE_TAG (&tag_s_encoding, inquire->encoding);
3724 RESOLVE_TAG (&tag_s_round, inquire->round);
3725 RESOLVE_TAG (&tag_iolength, inquire->iolength);
3726 RESOLVE_TAG (&tag_convert, inquire->convert);
3727 RESOLVE_TAG (&tag_strm_out, inquire->strm_pos);
3728 RESOLVE_TAG (&tag_s_async, inquire->asynchronous);
3729 RESOLVE_TAG (&tag_s_sign, inquire->sign);
3730 RESOLVE_TAG (&tag_s_round, inquire->round);
3731 RESOLVE_TAG (&tag_pending, inquire->pending);
3732 RESOLVE_TAG (&tag_id, inquire->id);
3734 if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)
3742 gfc_free_wait (gfc_wait *wait)
3747 gfc_free_expr (wait->unit);
3748 gfc_free_expr (wait->iostat);
3749 gfc_free_expr (wait->iomsg);
3750 gfc_free_expr (wait->id);
3755 gfc_resolve_wait (gfc_wait *wait)
3757 RESOLVE_TAG (&tag_unit, wait->unit);
3758 RESOLVE_TAG (&tag_iomsg, wait->iomsg);
3759 RESOLVE_TAG (&tag_iostat, wait->iostat);
3760 RESOLVE_TAG (&tag_id, wait->id);
3762 if (gfc_reference_st_label (wait->err, ST_LABEL_TARGET) == FAILURE)
3765 if (gfc_reference_st_label (wait->end, ST_LABEL_TARGET) == FAILURE)
3771 /* Match an element of a WAIT statement. */
3773 #define RETM if (m != MATCH_NO) return m;
3776 match_wait_element (gfc_wait *wait)
3780 m = match_etag (&tag_unit, &wait->unit);
3781 RETM m = match_ltag (&tag_err, &wait->err);
3782 RETM m = match_ltag (&tag_end, &wait->eor);
3783 RETM m = match_ltag (&tag_eor, &wait->end);
3784 RETM m = match_out_tag (&tag_iomsg, &wait->iomsg);
3785 RETM m = match_out_tag (&tag_iostat, &wait->iostat);
3786 RETM m = match_etag (&tag_id, &wait->id);
3787 RETM return MATCH_NO;
3794 gfc_match_wait (void)
3800 m = gfc_match_char ('(');
3804 wait = XCNEW (gfc_wait);
3806 loc = gfc_current_locus;
3808 m = match_wait_element (wait);
3809 if (m == MATCH_ERROR)
3813 m = gfc_match_expr (&wait->unit);
3814 if (m == MATCH_ERROR)
3822 if (gfc_match_char (')') == MATCH_YES)
3824 if (gfc_match_char (',') != MATCH_YES)
3827 m = match_wait_element (wait);
3828 if (m == MATCH_ERROR)
3834 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: WAIT at %C "
3835 "not allowed in Fortran 95") == FAILURE)
3838 if (gfc_pure (NULL))
3840 gfc_error ("WAIT statement not allowed in PURE procedure at %C");
3844 new_st.op = EXEC_WAIT;
3845 new_st.ext.wait = wait;
3850 gfc_syntax_error (ST_WAIT);
3853 gfc_free_wait (wait);