1 /* Deal with I/O statements & related stuff.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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},
98 tag_newunit = {"NEWUNIT", " newunit =", " %v", BT_INTEGER};
100 static gfc_dt *current_dt;
102 #define RESOLVE_TAG(x, y) if (resolve_tag(x, y) == FAILURE) return FAILURE;
105 /**************** Fortran 95 FORMAT parser *****************/
107 /* FORMAT tokens returned by format_lex(). */
110 FMT_NONE, FMT_UNKNOWN, FMT_SIGNED_INT, FMT_ZERO, FMT_POSINT, FMT_PERIOD,
111 FMT_COMMA, FMT_COLON, FMT_SLASH, FMT_DOLLAR, FMT_LPAREN,
112 FMT_RPAREN, FMT_X, FMT_SIGN, FMT_BLANK, FMT_CHAR, FMT_P, FMT_IBOZ, FMT_F,
113 FMT_E, FMT_EN, FMT_ES, FMT_G, FMT_L, FMT_A, FMT_D, FMT_H, FMT_END,
114 FMT_ERROR, FMT_DC, FMT_DP, FMT_T, FMT_TR, FMT_TL, FMT_STAR
118 /* Local variables for checking format strings. The saved_token is
119 used to back up by a single format token during the parsing
121 static gfc_char_t *format_string;
122 static int format_string_pos;
123 static int format_length, use_last_char;
124 static char error_element;
125 static locus format_locus;
127 static format_token saved_token;
130 { MODE_STRING, MODE_FORMAT, MODE_COPY }
134 /* Return the next character in the format string. */
137 next_char (int in_string)
149 if (mode == MODE_STRING)
150 c = *format_string++;
153 c = gfc_next_char_literal (in_string);
158 if (gfc_option.flag_backslash && c == '\\')
160 locus old_locus = gfc_current_locus;
162 if (gfc_match_special_char (&c) == MATCH_NO)
163 gfc_current_locus = old_locus;
165 if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
166 gfc_warning ("Extension: backslash character at %C");
169 if (mode == MODE_COPY)
170 *format_string++ = c;
172 if (mode != MODE_STRING)
173 format_locus = gfc_current_locus;
177 c = gfc_wide_toupper (c);
182 /* Back up one character position. Only works once. */
190 /* Eat up the spaces and return a character. */
193 next_char_not_space (bool *error)
198 error_element = c = next_char (0);
201 if (gfc_option.allow_std & GFC_STD_GNU)
202 gfc_warning ("Extension: Tab character in format at %C");
205 gfc_error ("Extension: Tab character in format at %C");
211 while (gfc_is_whitespace (c));
215 static int value = 0;
217 /* Simple lexical analyzer for getting the next token in a FORMAT
229 if (saved_token != FMT_NONE)
232 saved_token = FMT_NONE;
236 c = next_char_not_space (&error);
244 c = next_char_not_space (&error);
255 c = next_char_not_space (&error);
257 value = 10 * value + c - '0';
266 token = FMT_SIGNED_INT;
285 c = next_char_not_space (&error);
288 value = 10 * value + c - '0';
296 token = zflag ? FMT_ZERO : FMT_POSINT;
320 c = next_char_not_space (&error);
348 c = next_char_not_space (&error);
349 if (c != 'P' && c != 'S')
356 c = next_char_not_space (&error);
357 if (c == 'N' || c == 'Z')
418 c = next_char_not_space (&error);
448 c = next_char_not_space (&error);
451 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DP format "
452 "specifier not allowed at %C") == FAILURE)
458 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DC format "
459 "specifier not allowed at %C") == FAILURE)
491 token_to_string (format_token t)
510 /* Check a format statement. The format string, either from a FORMAT
511 statement or a constant in an I/O statement has already been parsed
512 by itself, and we are checking it for validity. The dual origin
513 means that the warning message is a little less than great. */
516 check_format (bool is_input)
518 const char *posint_required = _("Positive width required");
519 const char *nonneg_required = _("Nonnegative width required");
520 const char *unexpected_element = _("Unexpected element '%c' in format string"
522 const char *unexpected_end = _("Unexpected end of format string");
523 const char *zero_width = _("Zero width in format descriptor");
532 saved_token = FMT_NONE;
536 format_string_pos = 0;
543 error = _("Missing leading left parenthesis");
551 goto finished; /* Empty format is legal */
555 /* In this state, the next thing has to be a format item. */
572 error = _("Left parenthesis required after '*'");
597 /* Signed integer can only precede a P format. */
603 error = _("Expected P edit descriptor");
610 /* P requires a prior number. */
611 error = _("P descriptor requires leading scale factor");
615 /* X requires a prior number if we're being pedantic. */
616 if (gfc_notify_std (GFC_STD_GNU, "Extension: X descriptor "
617 "requires leading space count at %L", &format_locus)
629 goto extension_optional_comma;
640 if (gfc_notify_std (GFC_STD_GNU, "Extension: $ descriptor at %L",
641 &format_locus) == FAILURE)
643 if (t != FMT_RPAREN || level > 0)
645 gfc_warning ("$ should be the last specifier in format at %L",
647 goto optional_comma_1;
668 error = unexpected_end;
672 error = unexpected_element;
677 /* In this state, t must currently be a data descriptor.
678 Deal with things that can/must follow the descriptor. */
689 /* Comma after P is allowed only for F, E, EN, ES, D, or G.
694 if (gfc_option.allow_std < GFC_STD_F2003 && t != FMT_COMMA
695 && t != FMT_F && t != FMT_E && t != FMT_EN && t != FMT_ES
696 && t != FMT_D && t != FMT_G && t != FMT_RPAREN && t != FMT_SLASH)
698 error = _("Comma required after P descriptor");
709 if (t != FMT_F && t != FMT_E && t != FMT_EN && t != FMT_ES && t != FMT_D
710 && t != FMT_G && t != FMT_RPAREN && t != FMT_SLASH)
712 error = _("Comma required after P descriptor");
726 error = _("Positive width required with T descriptor");
738 switch (gfc_notification_std (GFC_STD_GNU))
741 if (mode != MODE_FORMAT)
742 format_locus.nextc += format_string_pos;
743 gfc_warning ("Extension: Missing positive width after L "
744 "descriptor at %L", &format_locus);
749 error = posint_required;
780 if (t == FMT_G && u == FMT_ZERO)
787 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: 'G0' in "
788 "format at %L", &format_locus) == FAILURE)
799 error = posint_required;
805 error = _("E specifier not allowed with g0 descriptor");
814 format_locus.nextc += format_string_pos;
815 gfc_error_now ("Positive width required in format "
816 "specifier %s at %L", token_to_string (t),
827 /* Warn if -std=legacy, otherwise error. */
828 format_locus.nextc += format_string_pos;
829 if (gfc_option.warn_std != 0)
831 gfc_error_now ("Period required in format "
832 "specifier %s at %L", token_to_string (t),
838 gfc_warning ("Period required in format "
839 "specifier %s at %L", token_to_string (t),
841 /* If we go to finished, we need to unwind this
842 before the next round. */
843 format_locus.nextc -= format_string_pos;
851 if (u != FMT_ZERO && u != FMT_POSINT)
853 error = nonneg_required;
860 /* Look for optional exponent. */
875 error = _("Positive exponent width required");
886 if (t != FMT_ZERO && t != FMT_POSINT)
888 error = nonneg_required;
891 else if (is_input && t == FMT_ZERO)
893 error = posint_required;
902 /* Warn if -std=legacy, otherwise error. */
903 if (gfc_option.warn_std != 0)
905 error = _("Period required in format specifier");
908 if (mode != MODE_FORMAT)
909 format_locus.nextc += format_string_pos;
910 gfc_warning ("Period required in format specifier at %L",
919 if (t != FMT_ZERO && t != FMT_POSINT)
921 error = nonneg_required;
928 if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
930 if (mode != MODE_FORMAT)
931 format_locus.nextc += format_string_pos;
932 gfc_warning ("The H format specifier at %L is"
933 " a Fortran 95 deleted feature", &format_locus);
936 if (mode == MODE_STRING)
938 format_string += value;
939 format_length -= value;
955 if (t != FMT_ZERO && t != FMT_POSINT)
957 error = nonneg_required;
960 else if (is_input && t == FMT_ZERO)
962 error = posint_required;
978 if (t != FMT_ZERO && t != FMT_POSINT)
980 error = nonneg_required;
988 error = unexpected_element;
993 /* Between a descriptor and what comes next. */
1011 goto optional_comma;
1014 error = unexpected_end;
1018 if (mode != MODE_FORMAT)
1019 format_locus.nextc += format_string_pos;
1020 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %L",
1021 &format_locus) == FAILURE)
1023 /* If we do not actually return a failure, we need to unwind this
1024 before the next round. */
1025 if (mode != MODE_FORMAT)
1026 format_locus.nextc -= format_string_pos;
1031 /* Optional comma is a weird between state where we've just finished
1032 reading a colon, slash, dollar or P descriptor. */
1049 /* Assume that we have another format item. */
1056 extension_optional_comma:
1057 /* As a GNU extension, permit a missing comma after a string literal. */
1074 goto optional_comma;
1077 error = unexpected_end;
1081 if (mode != MODE_FORMAT)
1082 format_locus.nextc += format_string_pos;
1083 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %L",
1084 &format_locus) == FAILURE)
1086 /* If we do not actually return a failure, we need to unwind this
1087 before the next round. */
1088 if (mode != MODE_FORMAT)
1089 format_locus.nextc -= format_string_pos;
1097 if (mode != MODE_FORMAT)
1098 format_locus.nextc += format_string_pos;
1099 if (error == unexpected_element)
1100 gfc_error (error, error_element, &format_locus);
1102 gfc_error ("%s in format string at %L", error, &format_locus);
1111 /* Given an expression node that is a constant string, see if it looks
1112 like a format string. */
1115 check_format_string (gfc_expr *e, bool is_input)
1117 if (!e || e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1121 format_string = e->value.character.string;
1123 /* More elaborate measures are needed to show where a problem is within a
1124 format string that has been calculated, but that's probably not worth the
1126 format_locus = e->where;
1128 return check_format (is_input);
1132 /************ Fortran 95 I/O statement matchers *************/
1134 /* Match a FORMAT statement. This amounts to actually parsing the
1135 format descriptors in order to correctly locate the end of the
1139 gfc_match_format (void)
1144 if (gfc_current_ns->proc_name
1145 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
1147 gfc_error ("Format statement in module main block at %C");
1151 if (gfc_statement_label == NULL)
1153 gfc_error ("Missing format label at %C");
1156 gfc_gobble_whitespace ();
1161 start = gfc_current_locus;
1163 if (check_format (false) == FAILURE)
1166 if (gfc_match_eos () != MATCH_YES)
1168 gfc_syntax_error (ST_FORMAT);
1172 /* The label doesn't get created until after the statement is done
1173 being matched, so we have to leave the string for later. */
1175 gfc_current_locus = start; /* Back to the beginning */
1178 new_st.op = EXEC_NOP;
1181 e->expr_type = EXPR_CONSTANT;
1182 e->ts.type = BT_CHARACTER;
1183 e->ts.kind = gfc_default_character_kind;
1185 e->value.character.string = format_string
1186 = gfc_get_wide_string (format_length + 1);
1187 e->value.character.length = format_length;
1188 gfc_statement_label->format = e;
1191 check_format (false); /* Guaranteed to succeed */
1192 gfc_match_eos (); /* Guaranteed to succeed */
1198 /* Match an expression I/O tag of some sort. */
1201 match_etag (const io_tag *tag, gfc_expr **v)
1206 m = gfc_match (tag->spec);
1210 m = gfc_match (tag->value, &result);
1213 gfc_error ("Invalid value for %s specification at %C", tag->name);
1219 gfc_error ("Duplicate %s specification at %C", tag->name);
1220 gfc_free_expr (result);
1229 /* Match a variable I/O tag of some sort. */
1232 match_vtag (const io_tag *tag, gfc_expr **v)
1237 m = gfc_match (tag->spec);
1241 m = gfc_match (tag->value, &result);
1244 gfc_error ("Invalid value for %s specification at %C", tag->name);
1250 gfc_error ("Duplicate %s specification at %C", tag->name);
1251 gfc_free_expr (result);
1255 if (result->symtree->n.sym->attr.intent == INTENT_IN)
1257 gfc_error ("Variable %s cannot be INTENT(IN) at %C", tag->name);
1258 gfc_free_expr (result);
1262 if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
1264 gfc_error ("Variable %s cannot be assigned in PURE procedure at %C",
1266 gfc_free_expr (result);
1275 /* Match I/O tags that cause variables to become redefined. */
1278 match_out_tag (const io_tag *tag, gfc_expr **result)
1282 m = match_vtag (tag, result);
1284 gfc_check_do_variable ((*result)->symtree);
1290 /* Match a label I/O tag. */
1293 match_ltag (const io_tag *tag, gfc_st_label ** label)
1299 m = gfc_match (tag->spec);
1303 m = gfc_match (tag->value, label);
1306 gfc_error ("Invalid value for %s specification at %C", tag->name);
1312 gfc_error ("Duplicate %s label specification at %C", tag->name);
1316 if (gfc_reference_st_label (*label, ST_LABEL_TARGET) == FAILURE)
1323 /* Resolution of the FORMAT tag, to be called from resolve_tag. */
1326 resolve_tag_format (const gfc_expr *e)
1328 if (e->expr_type == EXPR_CONSTANT
1329 && (e->ts.type != BT_CHARACTER
1330 || e->ts.kind != gfc_default_character_kind))
1332 gfc_error ("Constant expression in FORMAT tag at %L must be "
1333 "of type default CHARACTER", &e->where);
1337 /* If e's rank is zero and e is not an element of an array, it should be
1338 of integer or character type. The integer variable should be
1341 && (e->expr_type != EXPR_VARIABLE
1342 || e->symtree == NULL
1343 || e->symtree->n.sym->as == NULL
1344 || e->symtree->n.sym->as->rank == 0))
1346 if (e->ts.type != BT_CHARACTER && e->ts.type != BT_INTEGER)
1348 gfc_error ("FORMAT tag at %L must be of type CHARACTER or INTEGER",
1352 else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
1354 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGNED "
1355 "variable in FORMAT tag at %L", &e->where)
1358 if (e->symtree->n.sym->attr.assign != 1)
1360 gfc_error ("Variable '%s' at %L has not been assigned a "
1361 "format label", e->symtree->n.sym->name, &e->where);
1365 else if (e->ts.type == BT_INTEGER)
1367 gfc_error ("Scalar '%s' in FORMAT tag at %L is not an ASSIGNED "
1368 "variable", gfc_basic_typename (e->ts.type), &e->where);
1375 /* If rank is nonzero and type is not character, we allow it under GFC_STD_LEGACY.
1376 It may be assigned an Hollerith constant. */
1377 if (e->ts.type != BT_CHARACTER)
1379 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: Non-character "
1380 "in FORMAT tag at %L", &e->where) == FAILURE)
1383 if (e->rank == 0 && e->symtree->n.sym->as->type == AS_ASSUMED_SHAPE)
1385 gfc_error ("Non-character assumed shape array element in FORMAT"
1386 " tag at %L", &e->where);
1390 if (e->rank == 0 && e->symtree->n.sym->as->type == AS_ASSUMED_SIZE)
1392 gfc_error ("Non-character assumed size array element in FORMAT"
1393 " tag at %L", &e->where);
1397 if (e->rank == 0 && e->symtree->n.sym->attr.pointer)
1399 gfc_error ("Non-character pointer array element in FORMAT tag at %L",
1409 /* Do expression resolution and type-checking on an expression tag. */
1412 resolve_tag (const io_tag *tag, gfc_expr *e)
1417 if (gfc_resolve_expr (e) == FAILURE)
1420 if (tag == &tag_format)
1421 return resolve_tag_format (e);
1423 if (e->ts.type != tag->type)
1425 gfc_error ("%s tag at %L must be of type %s", tag->name,
1426 &e->where, gfc_basic_typename (tag->type));
1432 gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
1436 if (tag == &tag_iomsg)
1438 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IOMSG tag at %L",
1439 &e->where) == FAILURE)
1443 if ((tag == &tag_iostat || tag == &tag_size || tag == &tag_iolength)
1444 && e->ts.kind != gfc_default_integer_kind)
1446 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1447 "INTEGER in %s tag at %L", tag->name, &e->where)
1452 if (tag == &tag_convert)
1454 if (gfc_notify_std (GFC_STD_GNU, "Extension: CONVERT tag at %L",
1455 &e->where) == FAILURE)
1463 /* Match a single tag of an OPEN statement. */
1466 match_open_element (gfc_open *open)
1470 m = match_etag (&tag_e_async, &open->asynchronous);
1473 m = match_etag (&tag_unit, &open->unit);
1476 m = match_out_tag (&tag_iomsg, &open->iomsg);
1479 m = match_out_tag (&tag_iostat, &open->iostat);
1482 m = match_etag (&tag_file, &open->file);
1485 m = match_etag (&tag_status, &open->status);
1488 m = match_etag (&tag_e_access, &open->access);
1491 m = match_etag (&tag_e_form, &open->form);
1494 m = match_etag (&tag_e_recl, &open->recl);
1497 m = match_etag (&tag_e_blank, &open->blank);
1500 m = match_etag (&tag_e_position, &open->position);
1503 m = match_etag (&tag_e_action, &open->action);
1506 m = match_etag (&tag_e_delim, &open->delim);
1509 m = match_etag (&tag_e_pad, &open->pad);
1512 m = match_etag (&tag_e_decimal, &open->decimal);
1515 m = match_etag (&tag_e_encoding, &open->encoding);
1518 m = match_etag (&tag_e_round, &open->round);
1521 m = match_etag (&tag_e_sign, &open->sign);
1524 m = match_ltag (&tag_err, &open->err);
1527 m = match_etag (&tag_convert, &open->convert);
1530 m = match_out_tag (&tag_newunit, &open->newunit);
1538 /* Free the gfc_open structure and all the expressions it contains. */
1541 gfc_free_open (gfc_open *open)
1546 gfc_free_expr (open->unit);
1547 gfc_free_expr (open->iomsg);
1548 gfc_free_expr (open->iostat);
1549 gfc_free_expr (open->file);
1550 gfc_free_expr (open->status);
1551 gfc_free_expr (open->access);
1552 gfc_free_expr (open->form);
1553 gfc_free_expr (open->recl);
1554 gfc_free_expr (open->blank);
1555 gfc_free_expr (open->position);
1556 gfc_free_expr (open->action);
1557 gfc_free_expr (open->delim);
1558 gfc_free_expr (open->pad);
1559 gfc_free_expr (open->decimal);
1560 gfc_free_expr (open->encoding);
1561 gfc_free_expr (open->round);
1562 gfc_free_expr (open->sign);
1563 gfc_free_expr (open->convert);
1564 gfc_free_expr (open->asynchronous);
1565 gfc_free_expr (open->newunit);
1570 /* Resolve everything in a gfc_open structure. */
1573 gfc_resolve_open (gfc_open *open)
1576 RESOLVE_TAG (&tag_unit, open->unit);
1577 RESOLVE_TAG (&tag_iomsg, open->iomsg);
1578 RESOLVE_TAG (&tag_iostat, open->iostat);
1579 RESOLVE_TAG (&tag_file, open->file);
1580 RESOLVE_TAG (&tag_status, open->status);
1581 RESOLVE_TAG (&tag_e_access, open->access);
1582 RESOLVE_TAG (&tag_e_form, open->form);
1583 RESOLVE_TAG (&tag_e_recl, open->recl);
1584 RESOLVE_TAG (&tag_e_blank, open->blank);
1585 RESOLVE_TAG (&tag_e_position, open->position);
1586 RESOLVE_TAG (&tag_e_action, open->action);
1587 RESOLVE_TAG (&tag_e_delim, open->delim);
1588 RESOLVE_TAG (&tag_e_pad, open->pad);
1589 RESOLVE_TAG (&tag_e_decimal, open->decimal);
1590 RESOLVE_TAG (&tag_e_encoding, open->encoding);
1591 RESOLVE_TAG (&tag_e_async, open->asynchronous);
1592 RESOLVE_TAG (&tag_e_round, open->round);
1593 RESOLVE_TAG (&tag_e_sign, open->sign);
1594 RESOLVE_TAG (&tag_convert, open->convert);
1595 RESOLVE_TAG (&tag_newunit, open->newunit);
1597 if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1604 /* Check if a given value for a SPECIFIER is either in the list of values
1605 allowed in F95 or F2003, issuing an error message and returning a zero
1606 value if it is not allowed. */
1609 compare_to_allowed_values (const char *specifier, const char *allowed[],
1610 const char *allowed_f2003[],
1611 const char *allowed_gnu[], gfc_char_t *value,
1612 const char *statement, bool warn)
1617 len = gfc_wide_strlen (value);
1620 for (len--; len > 0; len--)
1621 if (value[len] != ' ')
1626 for (i = 0; allowed[i]; i++)
1627 if (len == strlen (allowed[i])
1628 && gfc_wide_strncasecmp (value, allowed[i], strlen (allowed[i])) == 0)
1631 for (i = 0; allowed_f2003 && allowed_f2003[i]; i++)
1632 if (len == strlen (allowed_f2003[i])
1633 && gfc_wide_strncasecmp (value, allowed_f2003[i],
1634 strlen (allowed_f2003[i])) == 0)
1636 notification n = gfc_notification_std (GFC_STD_F2003);
1638 if (n == WARNING || (warn && n == ERROR))
1640 gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1641 "has value '%s'", specifier, statement,
1648 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: %s specifier in "
1649 "%s statement at %C has value '%s'", specifier,
1650 statement, allowed_f2003[i]);
1658 for (i = 0; allowed_gnu && allowed_gnu[i]; i++)
1659 if (len == strlen (allowed_gnu[i])
1660 && gfc_wide_strncasecmp (value, allowed_gnu[i],
1661 strlen (allowed_gnu[i])) == 0)
1663 notification n = gfc_notification_std (GFC_STD_GNU);
1665 if (n == WARNING || (warn && n == ERROR))
1667 gfc_warning ("Extension: %s specifier in %s statement at %C "
1668 "has value '%s'", specifier, statement,
1675 gfc_notify_std (GFC_STD_GNU, "Extension: %s specifier in "
1676 "%s statement at %C has value '%s'", specifier,
1677 statement, allowed_gnu[i]);
1687 char *s = gfc_widechar_to_char (value, -1);
1688 gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1689 specifier, statement, s);
1695 char *s = gfc_widechar_to_char (value, -1);
1696 gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1697 specifier, statement, s);
1704 /* Match an OPEN statement. */
1707 gfc_match_open (void)
1713 m = gfc_match_char ('(');
1717 open = XCNEW (gfc_open);
1719 m = match_open_element (open);
1721 if (m == MATCH_ERROR)
1725 m = gfc_match_expr (&open->unit);
1728 if (m == MATCH_ERROR)
1734 if (gfc_match_char (')') == MATCH_YES)
1736 if (gfc_match_char (',') != MATCH_YES)
1739 m = match_open_element (open);
1740 if (m == MATCH_ERROR)
1746 if (gfc_match_eos () == MATCH_NO)
1749 if (gfc_pure (NULL))
1751 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1755 warn = (open->err || open->iostat) ? true : false;
1757 /* Checks on NEWUNIT specifier. */
1762 gfc_error ("UNIT specifier not allowed with NEWUNIT at %C");
1766 if (!(open->file || (open->status
1767 && gfc_wide_strncasecmp (open->status->value.character.string,
1768 "scratch", 7) == 0)))
1770 gfc_error ("NEWUNIT specifier must have FILE= "
1771 "or STATUS='scratch' at %C");
1776 /* Checks on the ACCESS specifier. */
1777 if (open->access && open->access->expr_type == EXPR_CONSTANT)
1779 static const char *access_f95[] = { "SEQUENTIAL", "DIRECT", NULL };
1780 static const char *access_f2003[] = { "STREAM", NULL };
1781 static const char *access_gnu[] = { "APPEND", NULL };
1783 if (!compare_to_allowed_values ("ACCESS", access_f95, access_f2003,
1785 open->access->value.character.string,
1790 /* Checks on the ACTION specifier. */
1791 if (open->action && open->action->expr_type == EXPR_CONSTANT)
1793 static const char *action[] = { "READ", "WRITE", "READWRITE", NULL };
1795 if (!compare_to_allowed_values ("ACTION", action, NULL, NULL,
1796 open->action->value.character.string,
1801 /* Checks on the ASYNCHRONOUS specifier. */
1802 if (open->asynchronous)
1804 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASYNCHRONOUS= at %C "
1805 "not allowed in Fortran 95") == FAILURE)
1808 if (open->asynchronous->expr_type == EXPR_CONSTANT)
1810 static const char * asynchronous[] = { "YES", "NO", NULL };
1812 if (!compare_to_allowed_values ("ASYNCHRONOUS", asynchronous,
1813 NULL, NULL, open->asynchronous->value.character.string,
1819 /* Checks on the BLANK specifier. */
1822 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: BLANK= at %C "
1823 "not allowed in Fortran 95") == FAILURE)
1826 if (open->blank->expr_type == EXPR_CONSTANT)
1828 static const char *blank[] = { "ZERO", "NULL", NULL };
1830 if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
1831 open->blank->value.character.string,
1837 /* Checks on the DECIMAL specifier. */
1840 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DECIMAL= at %C "
1841 "not allowed in Fortran 95") == FAILURE)
1844 if (open->decimal->expr_type == EXPR_CONSTANT)
1846 static const char * decimal[] = { "COMMA", "POINT", NULL };
1848 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1849 open->decimal->value.character.string,
1855 /* Checks on the DELIM specifier. */
1858 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DELIM= at %C "
1859 "not allowed in Fortran 95") == FAILURE)
1862 if (open->delim->expr_type == EXPR_CONSTANT)
1864 static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
1866 if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
1867 open->delim->value.character.string,
1873 /* Checks on the ENCODING specifier. */
1876 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENCODING= at %C "
1877 "not allowed in Fortran 95") == FAILURE)
1880 if (open->encoding->expr_type == EXPR_CONSTANT)
1882 static const char * encoding[] = { "DEFAULT", "UTF-8", NULL };
1884 if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1885 open->encoding->value.character.string,
1891 /* Checks on the FORM specifier. */
1892 if (open->form && open->form->expr_type == EXPR_CONSTANT)
1894 static const char *form[] = { "FORMATTED", "UNFORMATTED", NULL };
1896 if (!compare_to_allowed_values ("FORM", form, NULL, NULL,
1897 open->form->value.character.string,
1902 /* Checks on the PAD specifier. */
1903 if (open->pad && open->pad->expr_type == EXPR_CONSTANT)
1905 static const char *pad[] = { "YES", "NO", NULL };
1907 if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
1908 open->pad->value.character.string,
1913 /* Checks on the POSITION specifier. */
1914 if (open->position && open->position->expr_type == EXPR_CONSTANT)
1916 static const char *position[] = { "ASIS", "REWIND", "APPEND", NULL };
1918 if (!compare_to_allowed_values ("POSITION", position, NULL, NULL,
1919 open->position->value.character.string,
1924 /* Checks on the ROUND specifier. */
1927 /* When implemented, change the following to use gfc_notify_std F2003. */
1928 gfc_error ("Fortran F2003: ROUND= specifier at %C not implemented");
1931 if (open->round->expr_type == EXPR_CONSTANT)
1933 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1934 "COMPATIBLE", "PROCESSOR_DEFINED",
1937 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1938 open->round->value.character.string,
1944 /* Checks on the SIGN specifier. */
1947 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: SIGN= at %C "
1948 "not allowed in Fortran 95") == FAILURE)
1951 if (open->sign->expr_type == EXPR_CONSTANT)
1953 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1956 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1957 open->sign->value.character.string,
1963 #define warn_or_error(...) \
1966 gfc_warning (__VA_ARGS__); \
1969 gfc_error (__VA_ARGS__); \
1974 /* Checks on the RECL specifier. */
1975 if (open->recl && open->recl->expr_type == EXPR_CONSTANT
1976 && open->recl->ts.type == BT_INTEGER
1977 && mpz_sgn (open->recl->value.integer) != 1)
1979 warn_or_error ("RECL in OPEN statement at %C must be positive");
1982 /* Checks on the STATUS specifier. */
1983 if (open->status && open->status->expr_type == EXPR_CONSTANT)
1985 static const char *status[] = { "OLD", "NEW", "SCRATCH",
1986 "REPLACE", "UNKNOWN", NULL };
1988 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1989 open->status->value.character.string,
1993 /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1994 the FILE= specifier shall appear. */
1995 if (open->file == NULL
1996 && (gfc_wide_strncasecmp (open->status->value.character.string,
1998 || gfc_wide_strncasecmp (open->status->value.character.string,
2001 char *s = gfc_widechar_to_char (open->status->value.character.string,
2003 warn_or_error ("The STATUS specified in OPEN statement at %C is "
2004 "'%s' and no FILE specifier is present", s);
2008 /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
2009 the FILE= specifier shall not appear. */
2010 if (gfc_wide_strncasecmp (open->status->value.character.string,
2011 "scratch", 7) == 0 && open->file)
2013 warn_or_error ("The STATUS specified in OPEN statement at %C "
2014 "cannot have the value SCRATCH if a FILE specifier "
2019 /* Things that are not allowed for unformatted I/O. */
2020 if (open->form && open->form->expr_type == EXPR_CONSTANT
2021 && (open->delim || open->decimal || open->encoding || open->round
2022 || open->sign || open->pad || open->blank)
2023 && gfc_wide_strncasecmp (open->form->value.character.string,
2024 "unformatted", 11) == 0)
2026 const char *spec = (open->delim ? "DELIM "
2027 : (open->pad ? "PAD " : open->blank
2030 warn_or_error ("%s specifier at %C not allowed in OPEN statement for "
2031 "unformatted I/O", spec);
2034 if (open->recl && open->access && open->access->expr_type == EXPR_CONSTANT
2035 && gfc_wide_strncasecmp (open->access->value.character.string,
2038 warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
2043 && open->access && open->access->expr_type == EXPR_CONSTANT
2044 && !(gfc_wide_strncasecmp (open->access->value.character.string,
2045 "sequential", 10) == 0
2046 || gfc_wide_strncasecmp (open->access->value.character.string,
2048 || gfc_wide_strncasecmp (open->access->value.character.string,
2051 warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
2052 "for stream or sequential ACCESS");
2055 #undef warn_or_error
2057 new_st.op = EXEC_OPEN;
2058 new_st.ext.open = open;
2062 gfc_syntax_error (ST_OPEN);
2065 gfc_free_open (open);
2070 /* Free a gfc_close structure an all its expressions. */
2073 gfc_free_close (gfc_close *close)
2078 gfc_free_expr (close->unit);
2079 gfc_free_expr (close->iomsg);
2080 gfc_free_expr (close->iostat);
2081 gfc_free_expr (close->status);
2086 /* Match elements of a CLOSE statement. */
2089 match_close_element (gfc_close *close)
2093 m = match_etag (&tag_unit, &close->unit);
2096 m = match_etag (&tag_status, &close->status);
2099 m = match_out_tag (&tag_iomsg, &close->iomsg);
2102 m = match_out_tag (&tag_iostat, &close->iostat);
2105 m = match_ltag (&tag_err, &close->err);
2113 /* Match a CLOSE statement. */
2116 gfc_match_close (void)
2122 m = gfc_match_char ('(');
2126 close = XCNEW (gfc_close);
2128 m = match_close_element (close);
2130 if (m == MATCH_ERROR)
2134 m = gfc_match_expr (&close->unit);
2137 if (m == MATCH_ERROR)
2143 if (gfc_match_char (')') == MATCH_YES)
2145 if (gfc_match_char (',') != MATCH_YES)
2148 m = match_close_element (close);
2149 if (m == MATCH_ERROR)
2155 if (gfc_match_eos () == MATCH_NO)
2158 if (gfc_pure (NULL))
2160 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
2164 warn = (close->iostat || close->err) ? true : false;
2166 /* Checks on the STATUS specifier. */
2167 if (close->status && close->status->expr_type == EXPR_CONSTANT)
2169 static const char *status[] = { "KEEP", "DELETE", NULL };
2171 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
2172 close->status->value.character.string,
2177 new_st.op = EXEC_CLOSE;
2178 new_st.ext.close = close;
2182 gfc_syntax_error (ST_CLOSE);
2185 gfc_free_close (close);
2190 /* Resolve everything in a gfc_close structure. */
2193 gfc_resolve_close (gfc_close *close)
2195 RESOLVE_TAG (&tag_unit, close->unit);
2196 RESOLVE_TAG (&tag_iomsg, close->iomsg);
2197 RESOLVE_TAG (&tag_iostat, close->iostat);
2198 RESOLVE_TAG (&tag_status, close->status);
2200 if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
2203 if (close->unit->expr_type == EXPR_CONSTANT
2204 && close->unit->ts.type == BT_INTEGER
2205 && mpz_sgn (close->unit->value.integer) < 0)
2207 gfc_error ("UNIT number in CLOSE statement at %L must be non-negative",
2208 &close->unit->where);
2215 /* Free a gfc_filepos structure. */
2218 gfc_free_filepos (gfc_filepos *fp)
2220 gfc_free_expr (fp->unit);
2221 gfc_free_expr (fp->iomsg);
2222 gfc_free_expr (fp->iostat);
2227 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
2230 match_file_element (gfc_filepos *fp)
2234 m = match_etag (&tag_unit, &fp->unit);
2237 m = match_out_tag (&tag_iomsg, &fp->iomsg);
2240 m = match_out_tag (&tag_iostat, &fp->iostat);
2243 m = match_ltag (&tag_err, &fp->err);
2251 /* Match the second half of the file-positioning statements, REWIND,
2252 BACKSPACE, ENDFILE, or the FLUSH statement. */
2255 match_filepos (gfc_statement st, gfc_exec_op op)
2260 fp = XCNEW (gfc_filepos);
2262 if (gfc_match_char ('(') == MATCH_NO)
2264 m = gfc_match_expr (&fp->unit);
2265 if (m == MATCH_ERROR)
2273 m = match_file_element (fp);
2274 if (m == MATCH_ERROR)
2278 m = gfc_match_expr (&fp->unit);
2279 if (m == MATCH_ERROR)
2287 if (gfc_match_char (')') == MATCH_YES)
2289 if (gfc_match_char (',') != MATCH_YES)
2292 m = match_file_element (fp);
2293 if (m == MATCH_ERROR)
2300 if (gfc_match_eos () != MATCH_YES)
2303 if (gfc_pure (NULL))
2305 gfc_error ("%s statement not allowed in PURE procedure at %C",
2306 gfc_ascii_statement (st));
2312 new_st.ext.filepos = fp;
2316 gfc_syntax_error (st);
2319 gfc_free_filepos (fp);
2325 gfc_resolve_filepos (gfc_filepos *fp)
2327 RESOLVE_TAG (&tag_unit, fp->unit);
2328 RESOLVE_TAG (&tag_iostat, fp->iostat);
2329 RESOLVE_TAG (&tag_iomsg, fp->iomsg);
2330 if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
2333 if (fp->unit->expr_type == EXPR_CONSTANT
2334 && fp->unit->ts.type == BT_INTEGER
2335 && mpz_sgn (fp->unit->value.integer) < 0)
2337 gfc_error ("UNIT number in statement at %L must be non-negative",
2345 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
2346 and the FLUSH statement. */
2349 gfc_match_endfile (void)
2351 return match_filepos (ST_END_FILE, EXEC_ENDFILE);
2355 gfc_match_backspace (void)
2357 return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
2361 gfc_match_rewind (void)
2363 return match_filepos (ST_REWIND, EXEC_REWIND);
2367 gfc_match_flush (void)
2369 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: FLUSH statement at %C")
2373 return match_filepos (ST_FLUSH, EXEC_FLUSH);
2376 /******************** Data Transfer Statements *********************/
2378 /* Return a default unit number. */
2381 default_unit (io_kind k)
2390 return gfc_int_expr (unit);
2394 /* Match a unit specification for a data transfer statement. */
2397 match_dt_unit (io_kind k, gfc_dt *dt)
2401 if (gfc_match_char ('*') == MATCH_YES)
2403 if (dt->io_unit != NULL)
2406 dt->io_unit = default_unit (k);
2410 if (gfc_match_expr (&e) == MATCH_YES)
2412 if (dt->io_unit != NULL)
2425 gfc_error ("Duplicate UNIT specification at %C");
2430 /* Match a format specification. */
2433 match_dt_format (gfc_dt *dt)
2437 gfc_st_label *label;
2440 where = gfc_current_locus;
2442 if (gfc_match_char ('*') == MATCH_YES)
2444 if (dt->format_expr != NULL || dt->format_label != NULL)
2447 dt->format_label = &format_asterisk;
2451 if ((m = gfc_match_st_label (&label)) == MATCH_YES)
2453 if (dt->format_expr != NULL || dt->format_label != NULL)
2455 gfc_free_st_label (label);
2459 if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
2462 dt->format_label = label;
2465 else if (m == MATCH_ERROR)
2466 /* The label was zero or too large. Emit the correct diagnosis. */
2469 if (gfc_match_expr (&e) == MATCH_YES)
2471 if (dt->format_expr != NULL || dt->format_label != NULL)
2476 dt->format_expr = e;
2480 gfc_current_locus = where; /* The only case where we have to restore */
2485 gfc_error ("Duplicate format specification at %C");
2490 /* Traverse a namelist that is part of a READ statement to make sure
2491 that none of the variables in the namelist are INTENT(IN). Returns
2492 nonzero if we find such a variable. */
2495 check_namelist (gfc_symbol *sym)
2499 for (p = sym->namelist; p; p = p->next)
2500 if (p->sym->attr.intent == INTENT_IN)
2502 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2503 p->sym->name, sym->name);
2511 /* Match a single data transfer element. */
2514 match_dt_element (io_kind k, gfc_dt *dt)
2516 char name[GFC_MAX_SYMBOL_LEN + 1];
2520 if (gfc_match (" unit =") == MATCH_YES)
2522 m = match_dt_unit (k, dt);
2527 if (gfc_match (" fmt =") == MATCH_YES)
2529 m = match_dt_format (dt);
2534 if (gfc_match (" nml = %n", name) == MATCH_YES)
2536 if (dt->namelist != NULL)
2538 gfc_error ("Duplicate NML specification at %C");
2542 if (gfc_find_symbol (name, NULL, 1, &sym))
2545 if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
2547 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2548 sym != NULL ? sym->name : name);
2553 if (k == M_READ && check_namelist (sym))
2559 m = match_etag (&tag_e_async, &dt->asynchronous);
2562 m = match_etag (&tag_e_blank, &dt->blank);
2565 m = match_etag (&tag_e_delim, &dt->delim);
2568 m = match_etag (&tag_e_pad, &dt->pad);
2571 m = match_etag (&tag_e_sign, &dt->sign);
2574 m = match_etag (&tag_e_round, &dt->round);
2577 m = match_out_tag (&tag_id, &dt->id);
2580 m = match_etag (&tag_e_decimal, &dt->decimal);
2583 m = match_etag (&tag_rec, &dt->rec);
2586 m = match_etag (&tag_spos, &dt->pos);
2589 m = match_out_tag (&tag_iomsg, &dt->iomsg);
2592 m = match_out_tag (&tag_iostat, &dt->iostat);
2595 m = match_ltag (&tag_err, &dt->err);
2597 dt->err_where = gfc_current_locus;
2600 m = match_etag (&tag_advance, &dt->advance);
2603 m = match_out_tag (&tag_size, &dt->size);
2607 m = match_ltag (&tag_end, &dt->end);
2612 gfc_error ("END tag at %C not allowed in output statement");
2615 dt->end_where = gfc_current_locus;
2620 m = match_ltag (&tag_eor, &dt->eor);
2622 dt->eor_where = gfc_current_locus;
2630 /* Free a data transfer structure and everything below it. */
2633 gfc_free_dt (gfc_dt *dt)
2638 gfc_free_expr (dt->io_unit);
2639 gfc_free_expr (dt->format_expr);
2640 gfc_free_expr (dt->rec);
2641 gfc_free_expr (dt->advance);
2642 gfc_free_expr (dt->iomsg);
2643 gfc_free_expr (dt->iostat);
2644 gfc_free_expr (dt->size);
2645 gfc_free_expr (dt->pad);
2646 gfc_free_expr (dt->delim);
2647 gfc_free_expr (dt->sign);
2648 gfc_free_expr (dt->round);
2649 gfc_free_expr (dt->blank);
2650 gfc_free_expr (dt->decimal);
2651 gfc_free_expr (dt->extra_comma);
2652 gfc_free_expr (dt->pos);
2657 /* Resolve everything in a gfc_dt structure. */
2660 gfc_resolve_dt (gfc_dt *dt, locus *loc)
2664 RESOLVE_TAG (&tag_format, dt->format_expr);
2665 RESOLVE_TAG (&tag_rec, dt->rec);
2666 RESOLVE_TAG (&tag_spos, dt->pos);
2667 RESOLVE_TAG (&tag_advance, dt->advance);
2668 RESOLVE_TAG (&tag_id, dt->id);
2669 RESOLVE_TAG (&tag_iomsg, dt->iomsg);
2670 RESOLVE_TAG (&tag_iostat, dt->iostat);
2671 RESOLVE_TAG (&tag_size, dt->size);
2672 RESOLVE_TAG (&tag_e_pad, dt->pad);
2673 RESOLVE_TAG (&tag_e_delim, dt->delim);
2674 RESOLVE_TAG (&tag_e_sign, dt->sign);
2675 RESOLVE_TAG (&tag_e_round, dt->round);
2676 RESOLVE_TAG (&tag_e_blank, dt->blank);
2677 RESOLVE_TAG (&tag_e_decimal, dt->decimal);
2678 RESOLVE_TAG (&tag_e_async, dt->asynchronous);
2683 gfc_error ("UNIT not specified at %L", loc);
2687 if (gfc_resolve_expr (e) == SUCCESS
2688 && (e->ts.type != BT_INTEGER
2689 && (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_VARIABLE)))
2691 /* If there is no extra comma signifying the "format" form of the IO
2692 statement, then this must be an error. */
2693 if (!dt->extra_comma)
2695 gfc_error ("UNIT specification at %L must be an INTEGER expression "
2696 "or a CHARACTER variable", &e->where);
2701 /* At this point, we have an extra comma. If io_unit has arrived as
2702 type character, we assume its really the "format" form of the I/O
2703 statement. We set the io_unit to the default unit and format to
2704 the character expression. See F95 Standard section 9.4. */
2706 k = dt->extra_comma->value.iokind;
2707 if (e->ts.type == BT_CHARACTER && (k == M_READ || k == M_PRINT))
2709 dt->format_expr = dt->io_unit;
2710 dt->io_unit = default_unit (k);
2712 /* Free this pointer now so that a warning/error is not triggered
2713 below for the "Extension". */
2714 gfc_free_expr (dt->extra_comma);
2715 dt->extra_comma = NULL;
2720 gfc_error ("Invalid form of WRITE statement at %L, UNIT required",
2721 &dt->extra_comma->where);
2727 if (e->ts.type == BT_CHARACTER)
2729 if (gfc_has_vector_index (e))
2731 gfc_error ("Internal unit with vector subscript at %L", &e->where);
2736 if (e->rank && e->ts.type != BT_CHARACTER)
2738 gfc_error ("External IO UNIT cannot be an array at %L", &e->where);
2742 if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_INTEGER
2743 && mpz_sgn (e->value.integer) < 0)
2745 gfc_error ("UNIT number in statement at %L must be non-negative", &e->where);
2750 && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before i/o "
2751 "item list at %L", &dt->extra_comma->where) == FAILURE)
2756 if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
2758 if (dt->err->defined == ST_LABEL_UNKNOWN)
2760 gfc_error ("ERR tag label %d at %L not defined",
2761 dt->err->value, &dt->err_where);
2768 if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
2770 if (dt->end->defined == ST_LABEL_UNKNOWN)
2772 gfc_error ("END tag label %d at %L not defined",
2773 dt->end->value, &dt->end_where);
2780 if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
2782 if (dt->eor->defined == ST_LABEL_UNKNOWN)
2784 gfc_error ("EOR tag label %d at %L not defined",
2785 dt->eor->value, &dt->eor_where);
2790 /* Check the format label actually exists. */
2791 if (dt->format_label && dt->format_label != &format_asterisk
2792 && dt->format_label->defined == ST_LABEL_UNKNOWN)
2794 gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
2795 &dt->format_label->where);
2802 /* Given an io_kind, return its name. */
2805 io_kind_name (io_kind k)
2824 gfc_internal_error ("io_kind_name(): bad I/O-kind");
2831 /* Match an IO iteration statement of the form:
2833 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2835 which is equivalent to a single IO element. This function is
2836 mutually recursive with match_io_element(). */
2838 static match match_io_element (io_kind, gfc_code **);
2841 match_io_iterator (io_kind k, gfc_code **result)
2843 gfc_code *head, *tail, *new_code;
2851 old_loc = gfc_current_locus;
2853 if (gfc_match_char ('(') != MATCH_YES)
2856 m = match_io_element (k, &head);
2859 if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
2865 /* Can't be anything but an IO iterator. Build a list. */
2866 iter = gfc_get_iterator ();
2870 m = gfc_match_iterator (iter, 0);
2871 if (m == MATCH_ERROR)
2875 gfc_check_do_variable (iter->var->symtree);
2879 m = match_io_element (k, &new_code);
2880 if (m == MATCH_ERROR)
2889 tail = gfc_append_code (tail, new_code);
2891 if (gfc_match_char (',') != MATCH_YES)
2900 if (gfc_match_char (')') != MATCH_YES)
2903 new_code = gfc_get_code ();
2904 new_code->op = EXEC_DO;
2905 new_code->ext.iterator = iter;
2907 new_code->block = gfc_get_code ();
2908 new_code->block->op = EXEC_DO;
2909 new_code->block->next = head;
2915 gfc_error ("Syntax error in I/O iterator at %C");
2919 gfc_free_iterator (iter, 1);
2920 gfc_free_statements (head);
2921 gfc_current_locus = old_loc;
2926 /* Match a single element of an IO list, which is either a single
2927 expression or an IO Iterator. */
2930 match_io_element (io_kind k, gfc_code **cpp)
2938 m = match_io_iterator (k, cpp);
2944 m = gfc_match_variable (&expr, 0);
2946 gfc_error ("Expected variable in READ statement at %C");
2950 m = gfc_match_expr (&expr);
2952 gfc_error ("Expected expression in %s statement at %C",
2960 if (expr->symtree->n.sym->attr.intent == INTENT_IN)
2962 gfc_error ("Variable '%s' in input list at %C cannot be "
2963 "INTENT(IN)", expr->symtree->n.sym->name);
2968 && gfc_impure_variable (expr->symtree->n.sym)
2969 && current_dt->io_unit
2970 && current_dt->io_unit->ts.type == BT_CHARACTER)
2972 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2973 expr->symtree->n.sym->name);
2977 if (gfc_check_do_variable (expr->symtree))
2983 if (current_dt->io_unit
2984 && current_dt->io_unit->ts.type == BT_CHARACTER
2986 && current_dt->io_unit->expr_type == EXPR_VARIABLE
2987 && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
2989 gfc_error ("Cannot write to internal file unit '%s' at %C "
2990 "inside a PURE procedure",
2991 current_dt->io_unit->symtree->n.sym->name);
3003 gfc_free_expr (expr);
3007 cp = gfc_get_code ();
3008 cp->op = EXEC_TRANSFER;
3016 /* Match an I/O list, building gfc_code structures as we go. */
3019 match_io_list (io_kind k, gfc_code **head_p)
3021 gfc_code *head, *tail, *new_code;
3024 *head_p = head = tail = NULL;
3025 if (gfc_match_eos () == MATCH_YES)
3030 m = match_io_element (k, &new_code);
3031 if (m == MATCH_ERROR)
3036 tail = gfc_append_code (tail, new_code);
3040 if (gfc_match_eos () == MATCH_YES)
3042 if (gfc_match_char (',') != MATCH_YES)
3050 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
3053 gfc_free_statements (head);
3058 /* Attach the data transfer end node. */
3061 terminate_io (gfc_code *io_code)
3065 if (io_code == NULL)
3066 io_code = new_st.block;
3068 c = gfc_get_code ();
3069 c->op = EXEC_DT_END;
3071 /* Point to structure that is already there */
3072 c->ext.dt = new_st.ext.dt;
3073 gfc_append_code (io_code, c);
3077 /* Check the constraints for a data transfer statement. The majority of the
3078 constraints appearing in 9.4 of the standard appear here. Some are handled
3079 in resolve_tag and others in gfc_resolve_dt. */
3082 check_io_constraints (io_kind k, gfc_dt *dt, gfc_code *io_code,
3085 #define io_constraint(condition,msg,arg)\
3088 gfc_error(msg,arg);\
3094 gfc_symbol *sym = NULL;
3095 bool warn, unformatted;
3097 warn = (dt->err || dt->iostat) ? true : false;
3098 unformatted = dt->format_expr == NULL && dt->format_label == NULL
3099 && dt->namelist == NULL;
3104 if (expr && expr->expr_type == EXPR_VARIABLE
3105 && expr->ts.type == BT_CHARACTER)
3107 sym = expr->symtree->n.sym;
3109 io_constraint (k == M_WRITE && sym->attr.intent == INTENT_IN,
3110 "Internal file at %L must not be INTENT(IN)",
3113 io_constraint (gfc_has_vector_index (dt->io_unit),
3114 "Internal file incompatible with vector subscript at %L",
3117 io_constraint (dt->rec != NULL,
3118 "REC tag at %L is incompatible with internal file",
3121 io_constraint (dt->pos != NULL,
3122 "POS tag at %L is incompatible with internal file",
3125 io_constraint (unformatted,
3126 "Unformatted I/O not allowed with internal unit at %L",
3127 &dt->io_unit->where);
3129 io_constraint (dt->asynchronous != NULL,
3130 "ASYNCHRONOUS tag at %L not allowed with internal file",
3131 &dt->asynchronous->where);
3133 if (dt->namelist != NULL)
3135 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Internal file "
3136 "at %L with namelist", &expr->where)
3141 io_constraint (dt->advance != NULL,
3142 "ADVANCE tag at %L is incompatible with internal file",
3143 &dt->advance->where);
3146 if (expr && expr->ts.type != BT_CHARACTER)
3149 io_constraint (gfc_pure (NULL) && (k == M_READ || k == M_WRITE),
3150 "IO UNIT in %s statement at %C must be "
3151 "an internal file in a PURE procedure",
3157 io_constraint (dt->end, "END tag not allowed with output at %L",
3160 io_constraint (dt->eor, "EOR tag not allowed with output at %L",
3163 io_constraint (dt->blank, "BLANK= specifier not allowed with output at %L",
3166 io_constraint (dt->pad, "PAD= specifier not allowed with output at %L",
3169 io_constraint (dt->size, "SIZE= specifier not allowed with output at %L",
3174 io_constraint (dt->size && dt->advance == NULL,
3175 "SIZE tag at %L requires an ADVANCE tag",
3178 io_constraint (dt->eor && dt->advance == NULL,
3179 "EOR tag at %L requires an ADVANCE tag",
3183 if (dt->asynchronous)
3185 static const char * asynchronous[] = { "YES", "NO", NULL };
3187 if (gfc_reduce_init_expr (dt->asynchronous) != SUCCESS)
3189 gfc_error ("ASYNCHRONOUS= specifier at %L must be an initialization "
3190 "expression", &dt->asynchronous->where);
3194 if (!compare_to_allowed_values
3195 ("ASYNCHRONOUS", asynchronous, NULL, NULL,
3196 dt->asynchronous->value.character.string,
3197 io_kind_name (k), warn))
3205 || gfc_wide_strlen (dt->asynchronous->value.character.string) != 3
3206 || gfc_wide_strncasecmp (dt->asynchronous->value.character.string,
3208 io_constraint (not_yes,
3209 "ID= specifier at %L must be with ASYNCHRONOUS='yes' "
3210 "specifier", &dt->id->where);
3215 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DECIMAL= at %C "
3216 "not allowed in Fortran 95") == FAILURE)
3219 if (dt->decimal->expr_type == EXPR_CONSTANT)
3221 static const char * decimal[] = { "COMMA", "POINT", NULL };
3223 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
3224 dt->decimal->value.character.string,
3225 io_kind_name (k), warn))
3228 io_constraint (unformatted,
3229 "the DECIMAL= specifier at %L must be with an "
3230 "explicit format expression", &dt->decimal->where);
3236 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: BLANK= at %C "
3237 "not allowed in Fortran 95") == FAILURE)
3240 if (dt->blank->expr_type == EXPR_CONSTANT)
3242 static const char * blank[] = { "NULL", "ZERO", NULL };
3244 if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
3245 dt->blank->value.character.string,
3246 io_kind_name (k), warn))
3249 io_constraint (unformatted,
3250 "the BLANK= specifier at %L must be with an "
3251 "explicit format expression", &dt->blank->where);
3257 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PAD= at %C "
3258 "not allowed in Fortran 95") == FAILURE)
3261 if (dt->pad->expr_type == EXPR_CONSTANT)
3263 static const char * pad[] = { "YES", "NO", NULL };
3265 if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
3266 dt->pad->value.character.string,
3267 io_kind_name (k), warn))
3270 io_constraint (unformatted,
3271 "the PAD= specifier at %L must be with an "
3272 "explicit format expression", &dt->pad->where);
3278 /* When implemented, change the following to use gfc_notify_std F2003.
3279 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ROUND= at %C "
3280 "not allowed in Fortran 95") == FAILURE)
3281 return MATCH_ERROR; */
3282 gfc_error ("F2003 Feature: ROUND= specifier at %C not implemented");
3285 if (dt->round->expr_type == EXPR_CONSTANT)
3287 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
3288 "COMPATIBLE", "PROCESSOR_DEFINED",
3291 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
3292 dt->round->value.character.string,
3293 io_kind_name (k), warn))
3300 /* When implemented, change the following to use gfc_notify_std F2003.
3301 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: SIGN= at %C "
3302 "not allowed in Fortran 95") == FAILURE)
3303 return MATCH_ERROR; */
3304 if (dt->sign->expr_type == EXPR_CONSTANT)
3306 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
3309 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
3310 dt->sign->value.character.string,
3311 io_kind_name (k), warn))
3314 io_constraint (unformatted,
3315 "SIGN= specifier at %L must be with an "
3316 "explicit format expression", &dt->sign->where);
3318 io_constraint (k == M_READ,
3319 "SIGN= specifier at %L not allowed in a "
3320 "READ statement", &dt->sign->where);
3326 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DELIM= at %C "
3327 "not allowed in Fortran 95") == FAILURE)
3330 if (dt->delim->expr_type == EXPR_CONSTANT)
3332 static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
3334 if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
3335 dt->delim->value.character.string,
3336 io_kind_name (k), warn))
3339 io_constraint (k == M_READ,
3340 "DELIM= specifier at %L not allowed in a "
3341 "READ statement", &dt->delim->where);
3343 io_constraint (dt->format_label != &format_asterisk
3344 && dt->namelist == NULL,
3345 "DELIM= specifier at %L must have FMT=*",
3348 io_constraint (unformatted && dt->namelist == NULL,
3349 "DELIM= specifier at %L must be with FMT=* or "
3350 "NML= specifier ", &dt->delim->where);
3356 io_constraint (io_code && dt->namelist,
3357 "NAMELIST cannot be followed by IO-list at %L",
3360 io_constraint (dt->format_expr,
3361 "IO spec-list cannot contain both NAMELIST group name "
3362 "and format specification at %L",
3363 &dt->format_expr->where);
3365 io_constraint (dt->format_label,
3366 "IO spec-list cannot contain both NAMELIST group name "
3367 "and format label at %L", spec_end);
3369 io_constraint (dt->rec,
3370 "NAMELIST IO is not allowed with a REC= specifier "
3371 "at %L", &dt->rec->where);
3373 io_constraint (dt->advance,
3374 "NAMELIST IO is not allowed with a ADVANCE= specifier "
3375 "at %L", &dt->advance->where);
3380 io_constraint (dt->end,
3381 "An END tag is not allowed with a "
3382 "REC= specifier at %L", &dt->end_where);
3384 io_constraint (dt->format_label == &format_asterisk,
3385 "FMT=* is not allowed with a REC= specifier "
3388 io_constraint (dt->pos,
3389 "POS= is not allowed with REC= specifier "
3390 "at %L", &dt->pos->where);
3395 int not_yes, not_no;
3398 io_constraint (dt->format_label == &format_asterisk,
3399 "List directed format(*) is not allowed with a "
3400 "ADVANCE= specifier at %L.", &expr->where);
3402 io_constraint (unformatted,
3403 "the ADVANCE= specifier at %L must appear with an "
3404 "explicit format expression", &expr->where);
3406 if (expr->expr_type == EXPR_CONSTANT && expr->ts.type == BT_CHARACTER)
3408 const gfc_char_t *advance = expr->value.character.string;
3409 not_no = gfc_wide_strlen (advance) != 2
3410 || gfc_wide_strncasecmp (advance, "no", 2) != 0;
3411 not_yes = gfc_wide_strlen (advance) != 3
3412 || gfc_wide_strncasecmp (advance, "yes", 3) != 0;
3420 io_constraint (not_no && not_yes,
3421 "ADVANCE= specifier at %L must have value = "
3422 "YES or NO.", &expr->where);
3424 io_constraint (dt->size && not_no && k == M_READ,
3425 "SIZE tag at %L requires an ADVANCE = 'NO'",
3428 io_constraint (dt->eor && not_no && k == M_READ,
3429 "EOR tag at %L requires an ADVANCE = 'NO'",
3433 expr = dt->format_expr;
3434 if (gfc_simplify_expr (expr, 0) == FAILURE
3435 || check_format_string (expr, k == M_READ) == FAILURE)
3440 #undef io_constraint
3443 /* Match a READ, WRITE or PRINT statement. */
3446 match_io (io_kind k)
3448 char name[GFC_MAX_SYMBOL_LEN + 1];
3457 where = gfc_current_locus;
3459 current_dt = dt = XCNEW (gfc_dt);
3460 m = gfc_match_char ('(');
3463 where = gfc_current_locus;
3466 else if (k == M_PRINT)
3468 /* Treat the non-standard case of PRINT namelist. */
3469 if ((gfc_current_form == FORM_FIXED || gfc_peek_ascii_char () == ' ')
3470 && gfc_match_name (name) == MATCH_YES)
3472 gfc_find_symbol (name, NULL, 1, &sym);
3473 if (sym && sym->attr.flavor == FL_NAMELIST)
3475 if (gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
3476 "%C is an extension") == FAILURE)
3482 dt->io_unit = default_unit (k);
3487 gfc_current_locus = where;
3491 if (gfc_current_form == FORM_FREE)
3493 char c = gfc_peek_ascii_char ();
3494 if (c != ' ' && c != '*' && c != '\'' && c != '"')
3501 m = match_dt_format (dt);
3502 if (m == MATCH_ERROR)
3508 dt->io_unit = default_unit (k);
3513 /* Before issuing an error for a malformed 'print (1,*)' type of
3514 error, check for a default-char-expr of the form ('(I0)'). */
3515 if (k == M_PRINT && m == MATCH_YES)
3517 /* Reset current locus to get the initial '(' in an expression. */
3518 gfc_current_locus = where;
3519 dt->format_expr = NULL;
3520 m = match_dt_format (dt);
3522 if (m == MATCH_ERROR)
3524 if (m == MATCH_NO || dt->format_expr == NULL)
3528 dt->io_unit = default_unit (k);
3533 /* Match a control list */
3534 if (match_dt_element (k, dt) == MATCH_YES)
3536 if (match_dt_unit (k, dt) != MATCH_YES)
3539 if (gfc_match_char (')') == MATCH_YES)
3541 if (gfc_match_char (',') != MATCH_YES)
3544 m = match_dt_element (k, dt);
3547 if (m == MATCH_ERROR)
3550 m = match_dt_format (dt);
3553 if (m == MATCH_ERROR)
3556 where = gfc_current_locus;
3558 m = gfc_match_name (name);
3561 gfc_find_symbol (name, NULL, 1, &sym);
3562 if (sym && sym->attr.flavor == FL_NAMELIST)
3565 if (k == M_READ && check_namelist (sym))
3574 gfc_current_locus = where;
3576 goto loop; /* No matches, try regular elements */
3579 if (gfc_match_char (')') == MATCH_YES)
3581 if (gfc_match_char (',') != MATCH_YES)
3587 m = match_dt_element (k, dt);
3590 if (m == MATCH_ERROR)
3593 if (gfc_match_char (')') == MATCH_YES)
3595 if (gfc_match_char (',') != MATCH_YES)
3601 /* Used in check_io_constraints, where no locus is available. */
3602 spec_end = gfc_current_locus;
3604 /* Optional leading comma (non-standard). We use a gfc_expr structure here
3605 to save the locus. This is used later when resolving transfer statements
3606 that might have a format expression without unit number. */
3607 if (!comma_flag && gfc_match_char (',') == MATCH_YES)
3609 dt->extra_comma = gfc_get_expr ();
3611 /* Set the types to something compatible with iokind. This is needed to
3612 get through gfc_free_expr later since iokind really has no Basic Type,
3614 dt->extra_comma->expr_type = EXPR_CONSTANT;
3615 dt->extra_comma->ts.type = BT_LOGICAL;
3617 /* Save the iokind and locus for later use in resolution. */
3618 dt->extra_comma->value.iokind = k;
3619 dt->extra_comma->where = gfc_current_locus;
3623 if (gfc_match_eos () != MATCH_YES)
3625 if (comma_flag && gfc_match_char (',') != MATCH_YES)
3627 gfc_error ("Expected comma in I/O list at %C");
3632 m = match_io_list (k, &io_code);
3633 if (m == MATCH_ERROR)
3639 /* A full IO statement has been matched. Check the constraints. spec_end is
3640 supplied for cases where no locus is supplied. */
3641 m = check_io_constraints (k, dt, io_code, &spec_end);
3643 if (m == MATCH_ERROR)
3646 new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
3648 new_st.block = gfc_get_code ();
3649 new_st.block->op = new_st.op;
3650 new_st.block->next = io_code;
3652 terminate_io (io_code);
3657 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
3667 gfc_match_read (void)
3669 return match_io (M_READ);
3674 gfc_match_write (void)
3676 return match_io (M_WRITE);
3681 gfc_match_print (void)
3685 m = match_io (M_PRINT);
3689 if (gfc_pure (NULL))
3691 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
3699 /* Free a gfc_inquire structure. */
3702 gfc_free_inquire (gfc_inquire *inquire)
3705 if (inquire == NULL)
3708 gfc_free_expr (inquire->unit);
3709 gfc_free_expr (inquire->file);
3710 gfc_free_expr (inquire->iomsg);
3711 gfc_free_expr (inquire->iostat);
3712 gfc_free_expr (inquire->exist);
3713 gfc_free_expr (inquire->opened);
3714 gfc_free_expr (inquire->number);
3715 gfc_free_expr (inquire->named);
3716 gfc_free_expr (inquire->name);
3717 gfc_free_expr (inquire->access);
3718 gfc_free_expr (inquire->sequential);
3719 gfc_free_expr (inquire->direct);
3720 gfc_free_expr (inquire->form);
3721 gfc_free_expr (inquire->formatted);
3722 gfc_free_expr (inquire->unformatted);
3723 gfc_free_expr (inquire->recl);
3724 gfc_free_expr (inquire->nextrec);
3725 gfc_free_expr (inquire->blank);
3726 gfc_free_expr (inquire->position);
3727 gfc_free_expr (inquire->action);
3728 gfc_free_expr (inquire->read);
3729 gfc_free_expr (inquire->write);
3730 gfc_free_expr (inquire->readwrite);
3731 gfc_free_expr (inquire->delim);
3732 gfc_free_expr (inquire->encoding);
3733 gfc_free_expr (inquire->pad);
3734 gfc_free_expr (inquire->iolength);
3735 gfc_free_expr (inquire->convert);
3736 gfc_free_expr (inquire->strm_pos);
3737 gfc_free_expr (inquire->asynchronous);
3738 gfc_free_expr (inquire->decimal);
3739 gfc_free_expr (inquire->pending);
3740 gfc_free_expr (inquire->id);
3741 gfc_free_expr (inquire->sign);
3742 gfc_free_expr (inquire->size);
3743 gfc_free_expr (inquire->round);
3748 /* Match an element of an INQUIRE statement. */
3750 #define RETM if (m != MATCH_NO) return m;
3753 match_inquire_element (gfc_inquire *inquire)
3757 m = match_etag (&tag_unit, &inquire->unit);
3758 RETM m = match_etag (&tag_file, &inquire->file);
3759 RETM m = match_ltag (&tag_err, &inquire->err);
3760 RETM m = match_out_tag (&tag_iomsg, &inquire->iomsg);
3761 RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
3762 RETM m = match_vtag (&tag_exist, &inquire->exist);
3763 RETM m = match_vtag (&tag_opened, &inquire->opened);
3764 RETM m = match_vtag (&tag_named, &inquire->named);
3765 RETM m = match_vtag (&tag_name, &inquire->name);
3766 RETM m = match_out_tag (&tag_number, &inquire->number);
3767 RETM m = match_vtag (&tag_s_access, &inquire->access);
3768 RETM m = match_vtag (&tag_sequential, &inquire->sequential);
3769 RETM m = match_vtag (&tag_direct, &inquire->direct);
3770 RETM m = match_vtag (&tag_s_form, &inquire->form);
3771 RETM m = match_vtag (&tag_formatted, &inquire->formatted);
3772 RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
3773 RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
3774 RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
3775 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
3776 RETM m = match_vtag (&tag_s_position, &inquire->position);
3777 RETM m = match_vtag (&tag_s_action, &inquire->action);
3778 RETM m = match_vtag (&tag_read, &inquire->read);
3779 RETM m = match_vtag (&tag_write, &inquire->write);
3780 RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
3781 RETM m = match_vtag (&tag_s_async, &inquire->asynchronous);
3782 RETM m = match_vtag (&tag_s_delim, &inquire->delim);
3783 RETM m = match_vtag (&tag_s_decimal, &inquire->decimal);
3784 RETM m = match_vtag (&tag_size, &inquire->size);
3785 RETM m = match_vtag (&tag_s_encoding, &inquire->encoding);
3786 RETM m = match_vtag (&tag_s_round, &inquire->round);
3787 RETM m = match_vtag (&tag_s_sign, &inquire->sign);
3788 RETM m = match_vtag (&tag_s_pad, &inquire->pad);
3789 RETM m = match_vtag (&tag_iolength, &inquire->iolength);
3790 RETM m = match_vtag (&tag_convert, &inquire->convert);
3791 RETM m = match_out_tag (&tag_strm_out, &inquire->strm_pos);
3792 RETM m = match_vtag (&tag_pending, &inquire->pending);
3793 RETM m = match_vtag (&tag_id, &inquire->id);
3794 RETM return MATCH_NO;
3801 gfc_match_inquire (void)
3803 gfc_inquire *inquire;
3808 m = gfc_match_char ('(');
3812 inquire = XCNEW (gfc_inquire);
3814 loc = gfc_current_locus;
3816 m = match_inquire_element (inquire);
3817 if (m == MATCH_ERROR)
3821 m = gfc_match_expr (&inquire->unit);
3822 if (m == MATCH_ERROR)
3828 /* See if we have the IOLENGTH form of the inquire statement. */
3829 if (inquire->iolength != NULL)
3831 if (gfc_match_char (')') != MATCH_YES)
3834 m = match_io_list (M_INQUIRE, &code);
3835 if (m == MATCH_ERROR)
3840 new_st.op = EXEC_IOLENGTH;
3841 new_st.expr1 = inquire->iolength;
3842 new_st.ext.inquire = inquire;
3844 if (gfc_pure (NULL))
3846 gfc_free_statements (code);
3847 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3851 new_st.block = gfc_get_code ();
3852 new_st.block->op = EXEC_IOLENGTH;
3853 terminate_io (code);
3854 new_st.block->next = code;
3858 /* At this point, we have the non-IOLENGTH inquire statement. */
3861 if (gfc_match_char (')') == MATCH_YES)
3863 if (gfc_match_char (',') != MATCH_YES)
3866 m = match_inquire_element (inquire);
3867 if (m == MATCH_ERROR)
3872 if (inquire->iolength != NULL)
3874 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3879 if (gfc_match_eos () != MATCH_YES)
3882 if (inquire->unit != NULL && inquire->file != NULL)
3884 gfc_error ("INQUIRE statement at %L cannot contain both FILE and "
3885 "UNIT specifiers", &loc);
3889 if (inquire->unit == NULL && inquire->file == NULL)
3891 gfc_error ("INQUIRE statement at %L requires either FILE or "
3892 "UNIT specifier", &loc);
3896 if (gfc_pure (NULL))
3898 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3902 if (inquire->id != NULL && inquire->pending == NULL)
3904 gfc_error ("INQUIRE statement at %L requires a PENDING= specifier with "
3905 "the ID= specifier", &loc);
3909 new_st.op = EXEC_INQUIRE;
3910 new_st.ext.inquire = inquire;
3914 gfc_syntax_error (ST_INQUIRE);
3917 gfc_free_inquire (inquire);
3922 /* Resolve everything in a gfc_inquire structure. */
3925 gfc_resolve_inquire (gfc_inquire *inquire)
3927 RESOLVE_TAG (&tag_unit, inquire->unit);
3928 RESOLVE_TAG (&tag_file, inquire->file);
3929 RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
3930 RESOLVE_TAG (&tag_iostat, inquire->iostat);
3931 RESOLVE_TAG (&tag_exist, inquire->exist);
3932 RESOLVE_TAG (&tag_opened, inquire->opened);
3933 RESOLVE_TAG (&tag_number, inquire->number);
3934 RESOLVE_TAG (&tag_named, inquire->named);
3935 RESOLVE_TAG (&tag_name, inquire->name);
3936 RESOLVE_TAG (&tag_s_access, inquire->access);
3937 RESOLVE_TAG (&tag_sequential, inquire->sequential);
3938 RESOLVE_TAG (&tag_direct, inquire->direct);
3939 RESOLVE_TAG (&tag_s_form, inquire->form);
3940 RESOLVE_TAG (&tag_formatted, inquire->formatted);
3941 RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
3942 RESOLVE_TAG (&tag_s_recl, inquire->recl);
3943 RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
3944 RESOLVE_TAG (&tag_s_blank, inquire->blank);
3945 RESOLVE_TAG (&tag_s_position, inquire->position);
3946 RESOLVE_TAG (&tag_s_action, inquire->action);
3947 RESOLVE_TAG (&tag_read, inquire->read);
3948 RESOLVE_TAG (&tag_write, inquire->write);
3949 RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
3950 RESOLVE_TAG (&tag_s_delim, inquire->delim);
3951 RESOLVE_TAG (&tag_s_pad, inquire->pad);
3952 RESOLVE_TAG (&tag_s_encoding, inquire->encoding);
3953 RESOLVE_TAG (&tag_s_round, inquire->round);
3954 RESOLVE_TAG (&tag_iolength, inquire->iolength);
3955 RESOLVE_TAG (&tag_convert, inquire->convert);
3956 RESOLVE_TAG (&tag_strm_out, inquire->strm_pos);
3957 RESOLVE_TAG (&tag_s_async, inquire->asynchronous);
3958 RESOLVE_TAG (&tag_s_sign, inquire->sign);
3959 RESOLVE_TAG (&tag_s_round, inquire->round);
3960 RESOLVE_TAG (&tag_pending, inquire->pending);
3961 RESOLVE_TAG (&tag_size, inquire->size);
3962 RESOLVE_TAG (&tag_id, inquire->id);
3964 if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)
3972 gfc_free_wait (gfc_wait *wait)
3977 gfc_free_expr (wait->unit);
3978 gfc_free_expr (wait->iostat);
3979 gfc_free_expr (wait->iomsg);
3980 gfc_free_expr (wait->id);
3985 gfc_resolve_wait (gfc_wait *wait)
3987 RESOLVE_TAG (&tag_unit, wait->unit);
3988 RESOLVE_TAG (&tag_iomsg, wait->iomsg);
3989 RESOLVE_TAG (&tag_iostat, wait->iostat);
3990 RESOLVE_TAG (&tag_id, wait->id);
3992 if (gfc_reference_st_label (wait->err, ST_LABEL_TARGET) == FAILURE)
3995 if (gfc_reference_st_label (wait->end, ST_LABEL_TARGET) == FAILURE)
4001 /* Match an element of a WAIT statement. */
4003 #define RETM if (m != MATCH_NO) return m;
4006 match_wait_element (gfc_wait *wait)
4010 m = match_etag (&tag_unit, &wait->unit);
4011 RETM m = match_ltag (&tag_err, &wait->err);
4012 RETM m = match_ltag (&tag_end, &wait->eor);
4013 RETM m = match_ltag (&tag_eor, &wait->end);
4014 RETM m = match_out_tag (&tag_iomsg, &wait->iomsg);
4015 RETM m = match_out_tag (&tag_iostat, &wait->iostat);
4016 RETM m = match_etag (&tag_id, &wait->id);
4017 RETM return MATCH_NO;
4024 gfc_match_wait (void)
4030 m = gfc_match_char ('(');
4034 wait = XCNEW (gfc_wait);
4036 loc = gfc_current_locus;
4038 m = match_wait_element (wait);
4039 if (m == MATCH_ERROR)
4043 m = gfc_match_expr (&wait->unit);
4044 if (m == MATCH_ERROR)
4052 if (gfc_match_char (')') == MATCH_YES)
4054 if (gfc_match_char (',') != MATCH_YES)
4057 m = match_wait_element (wait);
4058 if (m == MATCH_ERROR)
4064 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: WAIT at %C "
4065 "not allowed in Fortran 95") == FAILURE)
4068 if (gfc_pure (NULL))
4070 gfc_error ("WAIT statement not allowed in PURE procedure at %C");
4074 new_st.op = EXEC_WAIT;
4075 new_st.ext.wait = wait;
4080 gfc_syntax_error (ST_WAIT);
4083 gfc_free_wait (wait);