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_EXT, FMT_G, FMT_L, FMT_A, FMT_D, FMT_H, FMT_END, FMT_ERROR, FMT_DC,
114 FMT_DP, FMT_T, FMT_TR, FMT_TL
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);
419 if (c == 'N' || c == 'S')
446 c = next_char_not_space (&error);
449 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DP format "
450 "specifier not allowed at %C") == FAILURE)
456 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DC format "
457 "specifier not allowed at %C") == FAILURE)
484 /* Check a format statement. The format string, either from a FORMAT
485 statement or a constant in an I/O statement has already been parsed
486 by itself, and we are checking it for validity. The dual origin
487 means that the warning message is a little less than great. */
490 check_format (bool is_input)
492 const char *posint_required = _("Positive width required");
493 const char *nonneg_required = _("Nonnegative width required");
494 const char *unexpected_element = _("Unexpected element '%c' in format string"
496 const char *unexpected_end = _("Unexpected end of format string");
497 const char *zero_width = _("Zero width in format descriptor");
506 saved_token = FMT_NONE;
510 format_string_pos = 0;
517 error = _("Missing leading left parenthesis");
525 goto finished; /* Empty format is legal */
529 /* In this state, the next thing has to be a format item. */
558 /* Signed integer can only precede a P format. */
564 error = _("Expected P edit descriptor");
571 /* P requires a prior number. */
572 error = _("P descriptor requires leading scale factor");
576 /* X requires a prior number if we're being pedantic. */
577 if (gfc_notify_std (GFC_STD_GNU, "Extension: X descriptor "
578 "requires leading space count at %C")
590 goto extension_optional_comma;
601 if (gfc_notify_std (GFC_STD_GNU, "Extension: $ descriptor at %C")
604 if (t != FMT_RPAREN || level > 0)
606 gfc_warning ("$ should be the last specifier in format at %C");
607 goto optional_comma_1;
627 error = unexpected_end;
631 error = unexpected_element;
636 /* In this state, t must currently be a data descriptor.
637 Deal with things that can/must follow the descriptor. */
655 error = _("Repeat count cannot follow P descriptor");
670 error = _("Positive width required with T descriptor");
682 switch (gfc_notification_std (GFC_STD_GNU))
685 gfc_warning ("Extension: Missing positive width after L "
691 error = posint_required;
721 if (t == FMT_G && u == FMT_ZERO)
728 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: 'G0' in "
729 "format at %C") == FAILURE)
740 error = posint_required;
746 error = _("E specifier not allowed with g0 descriptor");
758 /* Warn if -std=legacy, otherwise error. */
759 if (gfc_option.warn_std != 0)
760 gfc_error_now ("Period required in format specifier at %C");
762 gfc_warning ("Period required in format specifier at %C");
770 if (u != FMT_ZERO && u != FMT_POSINT)
772 error = nonneg_required;
779 /* Look for optional exponent. */
794 error = _("Positive exponent width required");
805 if (t != FMT_ZERO && t != FMT_POSINT)
807 error = nonneg_required;
810 else if (is_input && t == FMT_ZERO)
812 error = posint_required;
821 /* Warn if -std=legacy, otherwise error. */
822 if (gfc_option.warn_std != 0)
823 gfc_error_now ("Period required in format specifier at %C");
825 gfc_warning ("Period required in format specifier at %C");
833 if (t != FMT_ZERO && t != FMT_POSINT)
835 error = nonneg_required;
842 if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
843 gfc_warning ("The H format specifier at %C is"
844 " a Fortran 95 deleted feature");
846 if (mode == MODE_STRING)
848 format_string += value;
849 format_length -= value;
865 if (t != FMT_ZERO && t != FMT_POSINT)
867 error = nonneg_required;
870 else if (is_input && t == FMT_ZERO)
872 error = posint_required;
888 if (t != FMT_ZERO && t != FMT_POSINT)
890 error = nonneg_required;
898 error = unexpected_element;
903 /* Between a descriptor and what comes next. */
924 error = unexpected_end;
928 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
935 /* Optional comma is a weird between state where we've just finished
936 reading a colon, slash, dollar or P descriptor. */
953 /* Assume that we have another format item. */
960 extension_optional_comma:
961 /* As a GNU extension, permit a missing comma after a string literal. */
981 error = unexpected_end;
985 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
995 if (mode != MODE_FORMAT)
996 format_locus.nextc += format_string_pos;
997 if (error == unexpected_element)
998 gfc_error (error, error_element, &format_locus);
1000 gfc_error ("%s in format string at %L", error, &format_locus);
1009 /* Given an expression node that is a constant string, see if it looks
1010 like a format string. */
1013 check_format_string (gfc_expr *e, bool is_input)
1015 if (!e || e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1019 format_string = e->value.character.string;
1021 /* More elaborate measures are needed to show where a problem is within a
1022 format string that has been calculated, but that's probably not worth the
1024 format_locus = e->where;
1026 return check_format (is_input);
1030 /************ Fortran 95 I/O statement matchers *************/
1032 /* Match a FORMAT statement. This amounts to actually parsing the
1033 format descriptors in order to correctly locate the end of the
1037 gfc_match_format (void)
1042 if (gfc_current_ns->proc_name
1043 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
1045 gfc_error ("Format statement in module main block at %C");
1049 if (gfc_statement_label == NULL)
1051 gfc_error ("Missing format label at %C");
1054 gfc_gobble_whitespace ();
1059 start = gfc_current_locus;
1061 if (check_format (false) == FAILURE)
1064 if (gfc_match_eos () != MATCH_YES)
1066 gfc_syntax_error (ST_FORMAT);
1070 /* The label doesn't get created until after the statement is done
1071 being matched, so we have to leave the string for later. */
1073 gfc_current_locus = start; /* Back to the beginning */
1076 new_st.op = EXEC_NOP;
1079 e->expr_type = EXPR_CONSTANT;
1080 e->ts.type = BT_CHARACTER;
1081 e->ts.kind = gfc_default_character_kind;
1083 e->value.character.string = format_string
1084 = gfc_get_wide_string (format_length + 1);
1085 e->value.character.length = format_length;
1086 gfc_statement_label->format = e;
1089 check_format (false); /* Guaranteed to succeed */
1090 gfc_match_eos (); /* Guaranteed to succeed */
1096 /* Match an expression I/O tag of some sort. */
1099 match_etag (const io_tag *tag, gfc_expr **v)
1104 m = gfc_match (tag->spec);
1108 m = gfc_match (tag->value, &result);
1111 gfc_error ("Invalid value for %s specification at %C", tag->name);
1117 gfc_error ("Duplicate %s specification at %C", tag->name);
1118 gfc_free_expr (result);
1127 /* Match a variable I/O tag of some sort. */
1130 match_vtag (const io_tag *tag, gfc_expr **v)
1135 m = gfc_match (tag->spec);
1139 m = gfc_match (tag->value, &result);
1142 gfc_error ("Invalid value for %s specification at %C", tag->name);
1148 gfc_error ("Duplicate %s specification at %C", tag->name);
1149 gfc_free_expr (result);
1153 if (result->symtree->n.sym->attr.intent == INTENT_IN)
1155 gfc_error ("Variable %s cannot be INTENT(IN) at %C", tag->name);
1156 gfc_free_expr (result);
1160 if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
1162 gfc_error ("Variable %s cannot be assigned in PURE procedure at %C",
1164 gfc_free_expr (result);
1173 /* Match I/O tags that cause variables to become redefined. */
1176 match_out_tag (const io_tag *tag, gfc_expr **result)
1180 m = match_vtag (tag, result);
1182 gfc_check_do_variable ((*result)->symtree);
1188 /* Match a label I/O tag. */
1191 match_ltag (const io_tag *tag, gfc_st_label ** label)
1197 m = gfc_match (tag->spec);
1201 m = gfc_match (tag->value, label);
1204 gfc_error ("Invalid value for %s specification at %C", tag->name);
1210 gfc_error ("Duplicate %s label specification at %C", tag->name);
1214 if (gfc_reference_st_label (*label, ST_LABEL_TARGET) == FAILURE)
1221 /* Resolution of the FORMAT tag, to be called from resolve_tag. */
1224 resolve_tag_format (const gfc_expr *e)
1226 if (e->expr_type == EXPR_CONSTANT
1227 && (e->ts.type != BT_CHARACTER
1228 || e->ts.kind != gfc_default_character_kind))
1230 gfc_error ("Constant expression in FORMAT tag at %L must be "
1231 "of type default CHARACTER", &e->where);
1235 /* If e's rank is zero and e is not an element of an array, it should be
1236 of integer or character type. The integer variable should be
1239 && (e->expr_type != EXPR_VARIABLE
1240 || e->symtree == NULL
1241 || e->symtree->n.sym->as == NULL
1242 || e->symtree->n.sym->as->rank == 0))
1244 if (e->ts.type != BT_CHARACTER && e->ts.type != BT_INTEGER)
1246 gfc_error ("FORMAT tag at %L must be of type CHARACTER or INTEGER",
1250 else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
1252 if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGNED "
1253 "variable in FORMAT tag at %L", &e->where)
1256 if (e->symtree->n.sym->attr.assign != 1)
1258 gfc_error ("Variable '%s' at %L has not been assigned a "
1259 "format label", e->symtree->n.sym->name, &e->where);
1263 else if (e->ts.type == BT_INTEGER)
1265 gfc_error ("Scalar '%s' in FORMAT tag at %L is not an ASSIGNED "
1266 "variable", gfc_basic_typename (e->ts.type), &e->where);
1273 /* If rank is nonzero and type is not character, we allow it under GFC_STD_LEGACY.
1274 It may be assigned an Hollerith constant. */
1275 if (e->ts.type != BT_CHARACTER)
1277 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: Non-character "
1278 "in FORMAT tag at %L", &e->where) == FAILURE)
1281 if (e->rank == 0 && e->symtree->n.sym->as->type == AS_ASSUMED_SHAPE)
1283 gfc_error ("Non-character assumed shape array element in FORMAT"
1284 " tag at %L", &e->where);
1288 if (e->rank == 0 && e->symtree->n.sym->as->type == AS_ASSUMED_SIZE)
1290 gfc_error ("Non-character assumed size array element in FORMAT"
1291 " tag at %L", &e->where);
1295 if (e->rank == 0 && e->symtree->n.sym->attr.pointer)
1297 gfc_error ("Non-character pointer array element in FORMAT tag at %L",
1307 /* Do expression resolution and type-checking on an expression tag. */
1310 resolve_tag (const io_tag *tag, gfc_expr *e)
1315 if (gfc_resolve_expr (e) == FAILURE)
1318 if (tag == &tag_format)
1319 return resolve_tag_format (e);
1321 if (e->ts.type != tag->type)
1323 gfc_error ("%s tag at %L must be of type %s", tag->name,
1324 &e->where, gfc_basic_typename (tag->type));
1330 gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
1334 if (tag == &tag_iomsg)
1336 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IOMSG tag at %L",
1337 &e->where) == FAILURE)
1341 if ((tag == &tag_iostat || tag == &tag_size || tag == &tag_iolength)
1342 && e->ts.kind != gfc_default_integer_kind)
1344 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1345 "INTEGER in %s tag at %L", tag->name, &e->where)
1350 if (tag == &tag_convert)
1352 if (gfc_notify_std (GFC_STD_GNU, "Extension: CONVERT tag at %L",
1353 &e->where) == FAILURE)
1361 /* Match a single tag of an OPEN statement. */
1364 match_open_element (gfc_open *open)
1368 m = match_etag (&tag_e_async, &open->asynchronous);
1371 m = match_etag (&tag_unit, &open->unit);
1374 m = match_out_tag (&tag_iomsg, &open->iomsg);
1377 m = match_out_tag (&tag_iostat, &open->iostat);
1380 m = match_etag (&tag_file, &open->file);
1383 m = match_etag (&tag_status, &open->status);
1386 m = match_etag (&tag_e_access, &open->access);
1389 m = match_etag (&tag_e_form, &open->form);
1392 m = match_etag (&tag_e_recl, &open->recl);
1395 m = match_etag (&tag_e_blank, &open->blank);
1398 m = match_etag (&tag_e_position, &open->position);
1401 m = match_etag (&tag_e_action, &open->action);
1404 m = match_etag (&tag_e_delim, &open->delim);
1407 m = match_etag (&tag_e_pad, &open->pad);
1410 m = match_etag (&tag_e_decimal, &open->decimal);
1413 m = match_etag (&tag_e_encoding, &open->encoding);
1416 m = match_etag (&tag_e_round, &open->round);
1419 m = match_etag (&tag_e_sign, &open->sign);
1422 m = match_ltag (&tag_err, &open->err);
1425 m = match_etag (&tag_convert, &open->convert);
1428 m = match_out_tag (&tag_newunit, &open->newunit);
1436 /* Free the gfc_open structure and all the expressions it contains. */
1439 gfc_free_open (gfc_open *open)
1444 gfc_free_expr (open->unit);
1445 gfc_free_expr (open->iomsg);
1446 gfc_free_expr (open->iostat);
1447 gfc_free_expr (open->file);
1448 gfc_free_expr (open->status);
1449 gfc_free_expr (open->access);
1450 gfc_free_expr (open->form);
1451 gfc_free_expr (open->recl);
1452 gfc_free_expr (open->blank);
1453 gfc_free_expr (open->position);
1454 gfc_free_expr (open->action);
1455 gfc_free_expr (open->delim);
1456 gfc_free_expr (open->pad);
1457 gfc_free_expr (open->decimal);
1458 gfc_free_expr (open->encoding);
1459 gfc_free_expr (open->round);
1460 gfc_free_expr (open->sign);
1461 gfc_free_expr (open->convert);
1462 gfc_free_expr (open->asynchronous);
1463 gfc_free_expr (open->newunit);
1468 /* Resolve everything in a gfc_open structure. */
1471 gfc_resolve_open (gfc_open *open)
1474 RESOLVE_TAG (&tag_unit, open->unit);
1475 RESOLVE_TAG (&tag_iomsg, open->iomsg);
1476 RESOLVE_TAG (&tag_iostat, open->iostat);
1477 RESOLVE_TAG (&tag_file, open->file);
1478 RESOLVE_TAG (&tag_status, open->status);
1479 RESOLVE_TAG (&tag_e_access, open->access);
1480 RESOLVE_TAG (&tag_e_form, open->form);
1481 RESOLVE_TAG (&tag_e_recl, open->recl);
1482 RESOLVE_TAG (&tag_e_blank, open->blank);
1483 RESOLVE_TAG (&tag_e_position, open->position);
1484 RESOLVE_TAG (&tag_e_action, open->action);
1485 RESOLVE_TAG (&tag_e_delim, open->delim);
1486 RESOLVE_TAG (&tag_e_pad, open->pad);
1487 RESOLVE_TAG (&tag_e_decimal, open->decimal);
1488 RESOLVE_TAG (&tag_e_encoding, open->encoding);
1489 RESOLVE_TAG (&tag_e_async, open->asynchronous);
1490 RESOLVE_TAG (&tag_e_round, open->round);
1491 RESOLVE_TAG (&tag_e_sign, open->sign);
1492 RESOLVE_TAG (&tag_convert, open->convert);
1493 RESOLVE_TAG (&tag_newunit, open->newunit);
1495 if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1502 /* Check if a given value for a SPECIFIER is either in the list of values
1503 allowed in F95 or F2003, issuing an error message and returning a zero
1504 value if it is not allowed. */
1507 compare_to_allowed_values (const char *specifier, const char *allowed[],
1508 const char *allowed_f2003[],
1509 const char *allowed_gnu[], gfc_char_t *value,
1510 const char *statement, bool warn)
1515 len = gfc_wide_strlen (value);
1518 for (len--; len > 0; len--)
1519 if (value[len] != ' ')
1524 for (i = 0; allowed[i]; i++)
1525 if (len == strlen (allowed[i])
1526 && gfc_wide_strncasecmp (value, allowed[i], strlen (allowed[i])) == 0)
1529 for (i = 0; allowed_f2003 && allowed_f2003[i]; i++)
1530 if (len == strlen (allowed_f2003[i])
1531 && gfc_wide_strncasecmp (value, allowed_f2003[i],
1532 strlen (allowed_f2003[i])) == 0)
1534 notification n = gfc_notification_std (GFC_STD_F2003);
1536 if (n == WARNING || (warn && n == ERROR))
1538 gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1539 "has value '%s'", specifier, statement,
1546 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: %s specifier in "
1547 "%s statement at %C has value '%s'", specifier,
1548 statement, allowed_f2003[i]);
1556 for (i = 0; allowed_gnu && allowed_gnu[i]; i++)
1557 if (len == strlen (allowed_gnu[i])
1558 && gfc_wide_strncasecmp (value, allowed_gnu[i],
1559 strlen (allowed_gnu[i])) == 0)
1561 notification n = gfc_notification_std (GFC_STD_GNU);
1563 if (n == WARNING || (warn && n == ERROR))
1565 gfc_warning ("Extension: %s specifier in %s statement at %C "
1566 "has value '%s'", specifier, statement,
1573 gfc_notify_std (GFC_STD_GNU, "Extension: %s specifier in "
1574 "%s statement at %C has value '%s'", specifier,
1575 statement, allowed_gnu[i]);
1585 char *s = gfc_widechar_to_char (value, -1);
1586 gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1587 specifier, statement, s);
1593 char *s = gfc_widechar_to_char (value, -1);
1594 gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1595 specifier, statement, s);
1602 /* Match an OPEN statement. */
1605 gfc_match_open (void)
1611 m = gfc_match_char ('(');
1615 open = XCNEW (gfc_open);
1617 m = match_open_element (open);
1619 if (m == MATCH_ERROR)
1623 m = gfc_match_expr (&open->unit);
1626 if (m == MATCH_ERROR)
1632 if (gfc_match_char (')') == MATCH_YES)
1634 if (gfc_match_char (',') != MATCH_YES)
1637 m = match_open_element (open);
1638 if (m == MATCH_ERROR)
1644 if (gfc_match_eos () == MATCH_NO)
1647 if (gfc_pure (NULL))
1649 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1653 warn = (open->err || open->iostat) ? true : false;
1655 /* Checks on NEWUNIT specifier. */
1660 gfc_error ("UNIT specifier not allowed with NEWUNIT at %C");
1664 if (!(open->file || (open->status
1665 && gfc_wide_strncasecmp (open->status->value.character.string,
1666 "scratch", 7) == 0)))
1668 gfc_error ("NEWUNIT specifier must have FILE= "
1669 "or STATUS='scratch' at %C");
1674 /* Checks on the ACCESS specifier. */
1675 if (open->access && open->access->expr_type == EXPR_CONSTANT)
1677 static const char *access_f95[] = { "SEQUENTIAL", "DIRECT", NULL };
1678 static const char *access_f2003[] = { "STREAM", NULL };
1679 static const char *access_gnu[] = { "APPEND", NULL };
1681 if (!compare_to_allowed_values ("ACCESS", access_f95, access_f2003,
1683 open->access->value.character.string,
1688 /* Checks on the ACTION specifier. */
1689 if (open->action && open->action->expr_type == EXPR_CONSTANT)
1691 static const char *action[] = { "READ", "WRITE", "READWRITE", NULL };
1693 if (!compare_to_allowed_values ("ACTION", action, NULL, NULL,
1694 open->action->value.character.string,
1699 /* Checks on the ASYNCHRONOUS specifier. */
1700 if (open->asynchronous)
1702 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASYNCHRONOUS= at %C "
1703 "not allowed in Fortran 95") == FAILURE)
1706 if (open->asynchronous->expr_type == EXPR_CONSTANT)
1708 static const char * asynchronous[] = { "YES", "NO", NULL };
1710 if (!compare_to_allowed_values ("ASYNCHRONOUS", asynchronous,
1711 NULL, NULL, open->asynchronous->value.character.string,
1717 /* Checks on the BLANK specifier. */
1720 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: BLANK= at %C "
1721 "not allowed in Fortran 95") == FAILURE)
1724 if (open->blank->expr_type == EXPR_CONSTANT)
1726 static const char *blank[] = { "ZERO", "NULL", NULL };
1728 if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
1729 open->blank->value.character.string,
1735 /* Checks on the DECIMAL specifier. */
1738 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DECIMAL= at %C "
1739 "not allowed in Fortran 95") == FAILURE)
1742 if (open->decimal->expr_type == EXPR_CONSTANT)
1744 static const char * decimal[] = { "COMMA", "POINT", NULL };
1746 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1747 open->decimal->value.character.string,
1753 /* Checks on the DELIM specifier. */
1756 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DELIM= at %C "
1757 "not allowed in Fortran 95") == FAILURE)
1760 if (open->delim->expr_type == EXPR_CONSTANT)
1762 static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
1764 if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
1765 open->delim->value.character.string,
1771 /* Checks on the ENCODING specifier. */
1774 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENCODING= at %C "
1775 "not allowed in Fortran 95") == FAILURE)
1778 if (open->encoding->expr_type == EXPR_CONSTANT)
1780 static const char * encoding[] = { "DEFAULT", "UTF-8", NULL };
1782 if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1783 open->encoding->value.character.string,
1789 /* Checks on the FORM specifier. */
1790 if (open->form && open->form->expr_type == EXPR_CONSTANT)
1792 static const char *form[] = { "FORMATTED", "UNFORMATTED", NULL };
1794 if (!compare_to_allowed_values ("FORM", form, NULL, NULL,
1795 open->form->value.character.string,
1800 /* Checks on the PAD specifier. */
1801 if (open->pad && open->pad->expr_type == EXPR_CONSTANT)
1803 static const char *pad[] = { "YES", "NO", NULL };
1805 if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
1806 open->pad->value.character.string,
1811 /* Checks on the POSITION specifier. */
1812 if (open->position && open->position->expr_type == EXPR_CONSTANT)
1814 static const char *position[] = { "ASIS", "REWIND", "APPEND", NULL };
1816 if (!compare_to_allowed_values ("POSITION", position, NULL, NULL,
1817 open->position->value.character.string,
1822 /* Checks on the ROUND specifier. */
1825 /* When implemented, change the following to use gfc_notify_std F2003. */
1826 gfc_error ("Fortran F2003: ROUND= specifier at %C not implemented");
1829 if (open->round->expr_type == EXPR_CONSTANT)
1831 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1832 "COMPATIBLE", "PROCESSOR_DEFINED",
1835 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1836 open->round->value.character.string,
1842 /* Checks on the SIGN specifier. */
1845 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: SIGN= at %C "
1846 "not allowed in Fortran 95") == FAILURE)
1849 if (open->sign->expr_type == EXPR_CONSTANT)
1851 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1854 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1855 open->sign->value.character.string,
1861 #define warn_or_error(...) \
1864 gfc_warning (__VA_ARGS__); \
1867 gfc_error (__VA_ARGS__); \
1872 /* Checks on the RECL specifier. */
1873 if (open->recl && open->recl->expr_type == EXPR_CONSTANT
1874 && open->recl->ts.type == BT_INTEGER
1875 && mpz_sgn (open->recl->value.integer) != 1)
1877 warn_or_error ("RECL in OPEN statement at %C must be positive");
1880 /* Checks on the STATUS specifier. */
1881 if (open->status && open->status->expr_type == EXPR_CONSTANT)
1883 static const char *status[] = { "OLD", "NEW", "SCRATCH",
1884 "REPLACE", "UNKNOWN", NULL };
1886 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1887 open->status->value.character.string,
1891 /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1892 the FILE= specifier shall appear. */
1893 if (open->file == NULL
1894 && (gfc_wide_strncasecmp (open->status->value.character.string,
1896 || gfc_wide_strncasecmp (open->status->value.character.string,
1899 char *s = gfc_widechar_to_char (open->status->value.character.string,
1901 warn_or_error ("The STATUS specified in OPEN statement at %C is "
1902 "'%s' and no FILE specifier is present", s);
1906 /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1907 the FILE= specifier shall not appear. */
1908 if (gfc_wide_strncasecmp (open->status->value.character.string,
1909 "scratch", 7) == 0 && open->file)
1911 warn_or_error ("The STATUS specified in OPEN statement at %C "
1912 "cannot have the value SCRATCH if a FILE specifier "
1917 /* Things that are not allowed for unformatted I/O. */
1918 if (open->form && open->form->expr_type == EXPR_CONSTANT
1919 && (open->delim || open->decimal || open->encoding || open->round
1920 || open->sign || open->pad || open->blank)
1921 && gfc_wide_strncasecmp (open->form->value.character.string,
1922 "unformatted", 11) == 0)
1924 const char *spec = (open->delim ? "DELIM "
1925 : (open->pad ? "PAD " : open->blank
1928 warn_or_error ("%s specifier at %C not allowed in OPEN statement for "
1929 "unformatted I/O", spec);
1932 if (open->recl && open->access && open->access->expr_type == EXPR_CONSTANT
1933 && gfc_wide_strncasecmp (open->access->value.character.string,
1936 warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
1941 && open->access && open->access->expr_type == EXPR_CONSTANT
1942 && !(gfc_wide_strncasecmp (open->access->value.character.string,
1943 "sequential", 10) == 0
1944 || gfc_wide_strncasecmp (open->access->value.character.string,
1946 || gfc_wide_strncasecmp (open->access->value.character.string,
1949 warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
1950 "for stream or sequential ACCESS");
1953 #undef warn_or_error
1955 new_st.op = EXEC_OPEN;
1956 new_st.ext.open = open;
1960 gfc_syntax_error (ST_OPEN);
1963 gfc_free_open (open);
1968 /* Free a gfc_close structure an all its expressions. */
1971 gfc_free_close (gfc_close *close)
1976 gfc_free_expr (close->unit);
1977 gfc_free_expr (close->iomsg);
1978 gfc_free_expr (close->iostat);
1979 gfc_free_expr (close->status);
1984 /* Match elements of a CLOSE statement. */
1987 match_close_element (gfc_close *close)
1991 m = match_etag (&tag_unit, &close->unit);
1994 m = match_etag (&tag_status, &close->status);
1997 m = match_out_tag (&tag_iomsg, &close->iomsg);
2000 m = match_out_tag (&tag_iostat, &close->iostat);
2003 m = match_ltag (&tag_err, &close->err);
2011 /* Match a CLOSE statement. */
2014 gfc_match_close (void)
2020 m = gfc_match_char ('(');
2024 close = XCNEW (gfc_close);
2026 m = match_close_element (close);
2028 if (m == MATCH_ERROR)
2032 m = gfc_match_expr (&close->unit);
2035 if (m == MATCH_ERROR)
2041 if (gfc_match_char (')') == MATCH_YES)
2043 if (gfc_match_char (',') != MATCH_YES)
2046 m = match_close_element (close);
2047 if (m == MATCH_ERROR)
2053 if (gfc_match_eos () == MATCH_NO)
2056 if (gfc_pure (NULL))
2058 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
2062 warn = (close->iostat || close->err) ? true : false;
2064 /* Checks on the STATUS specifier. */
2065 if (close->status && close->status->expr_type == EXPR_CONSTANT)
2067 static const char *status[] = { "KEEP", "DELETE", NULL };
2069 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
2070 close->status->value.character.string,
2075 new_st.op = EXEC_CLOSE;
2076 new_st.ext.close = close;
2080 gfc_syntax_error (ST_CLOSE);
2083 gfc_free_close (close);
2088 /* Resolve everything in a gfc_close structure. */
2091 gfc_resolve_close (gfc_close *close)
2093 RESOLVE_TAG (&tag_unit, close->unit);
2094 RESOLVE_TAG (&tag_iomsg, close->iomsg);
2095 RESOLVE_TAG (&tag_iostat, close->iostat);
2096 RESOLVE_TAG (&tag_status, close->status);
2098 if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
2101 if (close->unit->expr_type == EXPR_CONSTANT
2102 && close->unit->ts.type == BT_INTEGER
2103 && mpz_sgn (close->unit->value.integer) < 0)
2105 gfc_error ("UNIT number in CLOSE statement at %L must be non-negative",
2106 &close->unit->where);
2113 /* Free a gfc_filepos structure. */
2116 gfc_free_filepos (gfc_filepos *fp)
2118 gfc_free_expr (fp->unit);
2119 gfc_free_expr (fp->iomsg);
2120 gfc_free_expr (fp->iostat);
2125 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
2128 match_file_element (gfc_filepos *fp)
2132 m = match_etag (&tag_unit, &fp->unit);
2135 m = match_out_tag (&tag_iomsg, &fp->iomsg);
2138 m = match_out_tag (&tag_iostat, &fp->iostat);
2141 m = match_ltag (&tag_err, &fp->err);
2149 /* Match the second half of the file-positioning statements, REWIND,
2150 BACKSPACE, ENDFILE, or the FLUSH statement. */
2153 match_filepos (gfc_statement st, gfc_exec_op op)
2158 fp = XCNEW (gfc_filepos);
2160 if (gfc_match_char ('(') == MATCH_NO)
2162 m = gfc_match_expr (&fp->unit);
2163 if (m == MATCH_ERROR)
2171 m = match_file_element (fp);
2172 if (m == MATCH_ERROR)
2176 m = gfc_match_expr (&fp->unit);
2177 if (m == MATCH_ERROR)
2185 if (gfc_match_char (')') == MATCH_YES)
2187 if (gfc_match_char (',') != MATCH_YES)
2190 m = match_file_element (fp);
2191 if (m == MATCH_ERROR)
2198 if (gfc_match_eos () != MATCH_YES)
2201 if (gfc_pure (NULL))
2203 gfc_error ("%s statement not allowed in PURE procedure at %C",
2204 gfc_ascii_statement (st));
2210 new_st.ext.filepos = fp;
2214 gfc_syntax_error (st);
2217 gfc_free_filepos (fp);
2223 gfc_resolve_filepos (gfc_filepos *fp)
2225 RESOLVE_TAG (&tag_unit, fp->unit);
2226 RESOLVE_TAG (&tag_iostat, fp->iostat);
2227 RESOLVE_TAG (&tag_iomsg, fp->iomsg);
2228 if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
2231 if (fp->unit->expr_type == EXPR_CONSTANT
2232 && fp->unit->ts.type == BT_INTEGER
2233 && mpz_sgn (fp->unit->value.integer) < 0)
2235 gfc_error ("UNIT number in statement at %L must be non-negative",
2243 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
2244 and the FLUSH statement. */
2247 gfc_match_endfile (void)
2249 return match_filepos (ST_END_FILE, EXEC_ENDFILE);
2253 gfc_match_backspace (void)
2255 return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
2259 gfc_match_rewind (void)
2261 return match_filepos (ST_REWIND, EXEC_REWIND);
2265 gfc_match_flush (void)
2267 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: FLUSH statement at %C")
2271 return match_filepos (ST_FLUSH, EXEC_FLUSH);
2274 /******************** Data Transfer Statements *********************/
2276 /* Return a default unit number. */
2279 default_unit (io_kind k)
2288 return gfc_int_expr (unit);
2292 /* Match a unit specification for a data transfer statement. */
2295 match_dt_unit (io_kind k, gfc_dt *dt)
2299 if (gfc_match_char ('*') == MATCH_YES)
2301 if (dt->io_unit != NULL)
2304 dt->io_unit = default_unit (k);
2308 if (gfc_match_expr (&e) == MATCH_YES)
2310 if (dt->io_unit != NULL)
2323 gfc_error ("Duplicate UNIT specification at %C");
2328 /* Match a format specification. */
2331 match_dt_format (gfc_dt *dt)
2335 gfc_st_label *label;
2338 where = gfc_current_locus;
2340 if (gfc_match_char ('*') == MATCH_YES)
2342 if (dt->format_expr != NULL || dt->format_label != NULL)
2345 dt->format_label = &format_asterisk;
2349 if ((m = gfc_match_st_label (&label)) == MATCH_YES)
2351 if (dt->format_expr != NULL || dt->format_label != NULL)
2353 gfc_free_st_label (label);
2357 if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
2360 dt->format_label = label;
2363 else if (m == MATCH_ERROR)
2364 /* The label was zero or too large. Emit the correct diagnosis. */
2367 if (gfc_match_expr (&e) == MATCH_YES)
2369 if (dt->format_expr != NULL || dt->format_label != NULL)
2374 dt->format_expr = e;
2378 gfc_current_locus = where; /* The only case where we have to restore */
2383 gfc_error ("Duplicate format specification at %C");
2388 /* Traverse a namelist that is part of a READ statement to make sure
2389 that none of the variables in the namelist are INTENT(IN). Returns
2390 nonzero if we find such a variable. */
2393 check_namelist (gfc_symbol *sym)
2397 for (p = sym->namelist; p; p = p->next)
2398 if (p->sym->attr.intent == INTENT_IN)
2400 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2401 p->sym->name, sym->name);
2409 /* Match a single data transfer element. */
2412 match_dt_element (io_kind k, gfc_dt *dt)
2414 char name[GFC_MAX_SYMBOL_LEN + 1];
2418 if (gfc_match (" unit =") == MATCH_YES)
2420 m = match_dt_unit (k, dt);
2425 if (gfc_match (" fmt =") == MATCH_YES)
2427 m = match_dt_format (dt);
2432 if (gfc_match (" nml = %n", name) == MATCH_YES)
2434 if (dt->namelist != NULL)
2436 gfc_error ("Duplicate NML specification at %C");
2440 if (gfc_find_symbol (name, NULL, 1, &sym))
2443 if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
2445 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2446 sym != NULL ? sym->name : name);
2451 if (k == M_READ && check_namelist (sym))
2457 m = match_etag (&tag_e_async, &dt->asynchronous);
2460 m = match_etag (&tag_e_blank, &dt->blank);
2463 m = match_etag (&tag_e_delim, &dt->delim);
2466 m = match_etag (&tag_e_pad, &dt->pad);
2469 m = match_etag (&tag_e_sign, &dt->sign);
2472 m = match_etag (&tag_e_round, &dt->round);
2475 m = match_out_tag (&tag_id, &dt->id);
2478 m = match_etag (&tag_e_decimal, &dt->decimal);
2481 m = match_etag (&tag_rec, &dt->rec);
2484 m = match_etag (&tag_spos, &dt->pos);
2487 m = match_out_tag (&tag_iomsg, &dt->iomsg);
2490 m = match_out_tag (&tag_iostat, &dt->iostat);
2493 m = match_ltag (&tag_err, &dt->err);
2495 dt->err_where = gfc_current_locus;
2498 m = match_etag (&tag_advance, &dt->advance);
2501 m = match_out_tag (&tag_size, &dt->size);
2505 m = match_ltag (&tag_end, &dt->end);
2510 gfc_error ("END tag at %C not allowed in output statement");
2513 dt->end_where = gfc_current_locus;
2518 m = match_ltag (&tag_eor, &dt->eor);
2520 dt->eor_where = gfc_current_locus;
2528 /* Free a data transfer structure and everything below it. */
2531 gfc_free_dt (gfc_dt *dt)
2536 gfc_free_expr (dt->io_unit);
2537 gfc_free_expr (dt->format_expr);
2538 gfc_free_expr (dt->rec);
2539 gfc_free_expr (dt->advance);
2540 gfc_free_expr (dt->iomsg);
2541 gfc_free_expr (dt->iostat);
2542 gfc_free_expr (dt->size);
2543 gfc_free_expr (dt->pad);
2544 gfc_free_expr (dt->delim);
2545 gfc_free_expr (dt->sign);
2546 gfc_free_expr (dt->round);
2547 gfc_free_expr (dt->blank);
2548 gfc_free_expr (dt->decimal);
2549 gfc_free_expr (dt->extra_comma);
2550 gfc_free_expr (dt->pos);
2555 /* Resolve everything in a gfc_dt structure. */
2558 gfc_resolve_dt (gfc_dt *dt)
2562 RESOLVE_TAG (&tag_format, dt->format_expr);
2563 RESOLVE_TAG (&tag_rec, dt->rec);
2564 RESOLVE_TAG (&tag_spos, dt->pos);
2565 RESOLVE_TAG (&tag_advance, dt->advance);
2566 RESOLVE_TAG (&tag_id, dt->id);
2567 RESOLVE_TAG (&tag_iomsg, dt->iomsg);
2568 RESOLVE_TAG (&tag_iostat, dt->iostat);
2569 RESOLVE_TAG (&tag_size, dt->size);
2570 RESOLVE_TAG (&tag_e_pad, dt->pad);
2571 RESOLVE_TAG (&tag_e_delim, dt->delim);
2572 RESOLVE_TAG (&tag_e_sign, dt->sign);
2573 RESOLVE_TAG (&tag_e_round, dt->round);
2574 RESOLVE_TAG (&tag_e_blank, dt->blank);
2575 RESOLVE_TAG (&tag_e_decimal, dt->decimal);
2576 RESOLVE_TAG (&tag_e_async, dt->asynchronous);
2579 if (gfc_resolve_expr (e) == SUCCESS
2580 && (e->ts.type != BT_INTEGER
2581 && (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_VARIABLE)))
2583 /* If there is no extra comma signifying the "format" form of the IO
2584 statement, then this must be an error. */
2585 if (!dt->extra_comma)
2587 gfc_error ("UNIT specification at %L must be an INTEGER expression "
2588 "or a CHARACTER variable", &e->where);
2593 /* At this point, we have an extra comma. If io_unit has arrived as
2594 type character, we assume its really the "format" form of the I/O
2595 statement. We set the io_unit to the default unit and format to
2596 the character expression. See F95 Standard section 9.4. */
2598 k = dt->extra_comma->value.iokind;
2599 if (e->ts.type == BT_CHARACTER && (k == M_READ || k == M_PRINT))
2601 dt->format_expr = dt->io_unit;
2602 dt->io_unit = default_unit (k);
2604 /* Free this pointer now so that a warning/error is not triggered
2605 below for the "Extension". */
2606 gfc_free_expr (dt->extra_comma);
2607 dt->extra_comma = NULL;
2612 gfc_error ("Invalid form of WRITE statement at %L, UNIT required",
2613 &dt->extra_comma->where);
2619 if (e->ts.type == BT_CHARACTER)
2621 if (gfc_has_vector_index (e))
2623 gfc_error ("Internal unit with vector subscript at %L", &e->where);
2628 if (e->rank && e->ts.type != BT_CHARACTER)
2630 gfc_error ("External IO UNIT cannot be an array at %L", &e->where);
2634 if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_INTEGER
2635 && mpz_sgn (e->value.integer) < 0)
2637 gfc_error ("UNIT number in statement at %L must be non-negative", &e->where);
2641 && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before i/o "
2642 "item list at %L", &dt->extra_comma->where) == FAILURE)
2647 if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
2649 if (dt->err->defined == ST_LABEL_UNKNOWN)
2651 gfc_error ("ERR tag label %d at %L not defined",
2652 dt->err->value, &dt->err_where);
2659 if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
2661 if (dt->end->defined == ST_LABEL_UNKNOWN)
2663 gfc_error ("END tag label %d at %L not defined",
2664 dt->end->value, &dt->end_where);
2671 if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
2673 if (dt->eor->defined == ST_LABEL_UNKNOWN)
2675 gfc_error ("EOR tag label %d at %L not defined",
2676 dt->eor->value, &dt->eor_where);
2681 /* Check the format label actually exists. */
2682 if (dt->format_label && dt->format_label != &format_asterisk
2683 && dt->format_label->defined == ST_LABEL_UNKNOWN)
2685 gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
2686 &dt->format_label->where);
2693 /* Given an io_kind, return its name. */
2696 io_kind_name (io_kind k)
2715 gfc_internal_error ("io_kind_name(): bad I/O-kind");
2722 /* Match an IO iteration statement of the form:
2724 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2726 which is equivalent to a single IO element. This function is
2727 mutually recursive with match_io_element(). */
2729 static match match_io_element (io_kind, gfc_code **);
2732 match_io_iterator (io_kind k, gfc_code **result)
2734 gfc_code *head, *tail, *new_code;
2742 old_loc = gfc_current_locus;
2744 if (gfc_match_char ('(') != MATCH_YES)
2747 m = match_io_element (k, &head);
2750 if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
2756 /* Can't be anything but an IO iterator. Build a list. */
2757 iter = gfc_get_iterator ();
2761 m = gfc_match_iterator (iter, 0);
2762 if (m == MATCH_ERROR)
2766 gfc_check_do_variable (iter->var->symtree);
2770 m = match_io_element (k, &new_code);
2771 if (m == MATCH_ERROR)
2780 tail = gfc_append_code (tail, new_code);
2782 if (gfc_match_char (',') != MATCH_YES)
2791 if (gfc_match_char (')') != MATCH_YES)
2794 new_code = gfc_get_code ();
2795 new_code->op = EXEC_DO;
2796 new_code->ext.iterator = iter;
2798 new_code->block = gfc_get_code ();
2799 new_code->block->op = EXEC_DO;
2800 new_code->block->next = head;
2806 gfc_error ("Syntax error in I/O iterator at %C");
2810 gfc_free_iterator (iter, 1);
2811 gfc_free_statements (head);
2812 gfc_current_locus = old_loc;
2817 /* Match a single element of an IO list, which is either a single
2818 expression or an IO Iterator. */
2821 match_io_element (io_kind k, gfc_code **cpp)
2829 m = match_io_iterator (k, cpp);
2835 m = gfc_match_variable (&expr, 0);
2837 gfc_error ("Expected variable in READ statement at %C");
2841 m = gfc_match_expr (&expr);
2843 gfc_error ("Expected expression in %s statement at %C",
2851 if (expr->symtree->n.sym->attr.intent == INTENT_IN)
2853 gfc_error ("Variable '%s' in input list at %C cannot be "
2854 "INTENT(IN)", expr->symtree->n.sym->name);
2859 && gfc_impure_variable (expr->symtree->n.sym)
2860 && current_dt->io_unit->ts.type == BT_CHARACTER)
2862 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2863 expr->symtree->n.sym->name);
2867 if (gfc_check_do_variable (expr->symtree))
2873 if (current_dt->io_unit->ts.type == BT_CHARACTER
2875 && current_dt->io_unit->expr_type == EXPR_VARIABLE
2876 && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
2878 gfc_error ("Cannot write to internal file unit '%s' at %C "
2879 "inside a PURE procedure",
2880 current_dt->io_unit->symtree->n.sym->name);
2892 gfc_free_expr (expr);
2896 cp = gfc_get_code ();
2897 cp->op = EXEC_TRANSFER;
2905 /* Match an I/O list, building gfc_code structures as we go. */
2908 match_io_list (io_kind k, gfc_code **head_p)
2910 gfc_code *head, *tail, *new_code;
2913 *head_p = head = tail = NULL;
2914 if (gfc_match_eos () == MATCH_YES)
2919 m = match_io_element (k, &new_code);
2920 if (m == MATCH_ERROR)
2925 tail = gfc_append_code (tail, new_code);
2929 if (gfc_match_eos () == MATCH_YES)
2931 if (gfc_match_char (',') != MATCH_YES)
2939 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2942 gfc_free_statements (head);
2947 /* Attach the data transfer end node. */
2950 terminate_io (gfc_code *io_code)
2954 if (io_code == NULL)
2955 io_code = new_st.block;
2957 c = gfc_get_code ();
2958 c->op = EXEC_DT_END;
2960 /* Point to structure that is already there */
2961 c->ext.dt = new_st.ext.dt;
2962 gfc_append_code (io_code, c);
2966 /* Check the constraints for a data transfer statement. The majority of the
2967 constraints appearing in 9.4 of the standard appear here. Some are handled
2968 in resolve_tag and others in gfc_resolve_dt. */
2971 check_io_constraints (io_kind k, gfc_dt *dt, gfc_code *io_code,
2974 #define io_constraint(condition,msg,arg)\
2977 gfc_error(msg,arg);\
2983 gfc_symbol *sym = NULL;
2984 bool warn, unformatted;
2986 warn = (dt->err || dt->iostat) ? true : false;
2987 unformatted = dt->format_expr == NULL && dt->format_label == NULL
2988 && dt->namelist == NULL;
2993 if (expr && expr->expr_type == EXPR_VARIABLE
2994 && expr->ts.type == BT_CHARACTER)
2996 sym = expr->symtree->n.sym;
2998 io_constraint (k == M_WRITE && sym->attr.intent == INTENT_IN,
2999 "Internal file at %L must not be INTENT(IN)",
3002 io_constraint (gfc_has_vector_index (dt->io_unit),
3003 "Internal file incompatible with vector subscript at %L",
3006 io_constraint (dt->rec != NULL,
3007 "REC tag at %L is incompatible with internal file",
3010 io_constraint (dt->pos != NULL,
3011 "POS tag at %L is incompatible with internal file",
3014 io_constraint (unformatted,
3015 "Unformatted I/O not allowed with internal unit at %L",
3016 &dt->io_unit->where);
3018 io_constraint (dt->asynchronous != NULL,
3019 "ASYNCHRONOUS tag at %L not allowed with internal file",
3020 &dt->asynchronous->where);
3022 if (dt->namelist != NULL)
3024 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Internal file "
3025 "at %L with namelist", &expr->where)
3030 io_constraint (dt->advance != NULL,
3031 "ADVANCE tag at %L is incompatible with internal file",
3032 &dt->advance->where);
3035 if (expr && expr->ts.type != BT_CHARACTER)
3038 io_constraint (gfc_pure (NULL) && (k == M_READ || k == M_WRITE),
3039 "IO UNIT in %s statement at %C must be "
3040 "an internal file in a PURE procedure",
3046 io_constraint (dt->end, "END tag not allowed with output at %L",
3049 io_constraint (dt->eor, "EOR tag not allowed with output at %L",
3052 io_constraint (dt->blank, "BLANK= specifier not allowed with output at %L",
3055 io_constraint (dt->pad, "PAD= specifier not allowed with output at %L",
3058 io_constraint (dt->size, "SIZE= specifier not allowed with output at %L",
3063 io_constraint (dt->size && dt->advance == NULL,
3064 "SIZE tag at %L requires an ADVANCE tag",
3067 io_constraint (dt->eor && dt->advance == NULL,
3068 "EOR tag at %L requires an ADVANCE tag",
3072 if (dt->asynchronous)
3074 static const char * asynchronous[] = { "YES", "NO", NULL };
3076 if (gfc_reduce_init_expr (dt->asynchronous) != SUCCESS)
3078 gfc_error ("ASYNCHRONOUS= specifier at %L must be an initialization "
3079 "expression", &dt->asynchronous->where);
3083 if (!compare_to_allowed_values
3084 ("ASYNCHRONOUS", asynchronous, NULL, NULL,
3085 dt->asynchronous->value.character.string,
3086 io_kind_name (k), warn))
3094 || gfc_wide_strlen (dt->asynchronous->value.character.string) != 3
3095 || gfc_wide_strncasecmp (dt->asynchronous->value.character.string,
3097 io_constraint (not_yes,
3098 "ID= specifier at %L must be with ASYNCHRONOUS='yes' "
3099 "specifier", &dt->id->where);
3104 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DECIMAL= at %C "
3105 "not allowed in Fortran 95") == FAILURE)
3108 if (dt->decimal->expr_type == EXPR_CONSTANT)
3110 static const char * decimal[] = { "COMMA", "POINT", NULL };
3112 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
3113 dt->decimal->value.character.string,
3114 io_kind_name (k), warn))
3117 io_constraint (unformatted,
3118 "the DECIMAL= specifier at %L must be with an "
3119 "explicit format expression", &dt->decimal->where);
3125 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: BLANK= at %C "
3126 "not allowed in Fortran 95") == FAILURE)
3129 if (dt->blank->expr_type == EXPR_CONSTANT)
3131 static const char * blank[] = { "NULL", "ZERO", NULL };
3133 if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
3134 dt->blank->value.character.string,
3135 io_kind_name (k), warn))
3138 io_constraint (unformatted,
3139 "the BLANK= specifier at %L must be with an "
3140 "explicit format expression", &dt->blank->where);
3146 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PAD= at %C "
3147 "not allowed in Fortran 95") == FAILURE)
3150 if (dt->pad->expr_type == EXPR_CONSTANT)
3152 static const char * pad[] = { "YES", "NO", NULL };
3154 if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
3155 dt->pad->value.character.string,
3156 io_kind_name (k), warn))
3159 io_constraint (unformatted,
3160 "the PAD= specifier at %L must be with an "
3161 "explicit format expression", &dt->pad->where);
3167 /* When implemented, change the following to use gfc_notify_std F2003.
3168 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ROUND= at %C "
3169 "not allowed in Fortran 95") == FAILURE)
3170 return MATCH_ERROR; */
3171 gfc_error ("F2003 Feature: ROUND= specifier at %C not implemented");
3174 if (dt->round->expr_type == EXPR_CONSTANT)
3176 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
3177 "COMPATIBLE", "PROCESSOR_DEFINED",
3180 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
3181 dt->round->value.character.string,
3182 io_kind_name (k), warn))
3189 /* When implemented, change the following to use gfc_notify_std F2003.
3190 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: SIGN= at %C "
3191 "not allowed in Fortran 95") == FAILURE)
3192 return MATCH_ERROR; */
3193 if (dt->sign->expr_type == EXPR_CONSTANT)
3195 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
3198 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
3199 dt->sign->value.character.string,
3200 io_kind_name (k), warn))
3203 io_constraint (unformatted,
3204 "SIGN= specifier at %L must be with an "
3205 "explicit format expression", &dt->sign->where);
3207 io_constraint (k == M_READ,
3208 "SIGN= specifier at %L not allowed in a "
3209 "READ statement", &dt->sign->where);
3215 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DELIM= at %C "
3216 "not allowed in Fortran 95") == FAILURE)
3219 if (dt->delim->expr_type == EXPR_CONSTANT)
3221 static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
3223 if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
3224 dt->delim->value.character.string,
3225 io_kind_name (k), warn))
3228 io_constraint (k == M_READ,
3229 "DELIM= specifier at %L not allowed in a "
3230 "READ statement", &dt->delim->where);
3232 io_constraint (dt->format_label != &format_asterisk
3233 && dt->namelist == NULL,
3234 "DELIM= specifier at %L must have FMT=*",
3237 io_constraint (unformatted && dt->namelist == NULL,
3238 "DELIM= specifier at %L must be with FMT=* or "
3239 "NML= specifier ", &dt->delim->where);
3245 io_constraint (io_code && dt->namelist,
3246 "NAMELIST cannot be followed by IO-list at %L",
3249 io_constraint (dt->format_expr,
3250 "IO spec-list cannot contain both NAMELIST group name "
3251 "and format specification at %L",
3252 &dt->format_expr->where);
3254 io_constraint (dt->format_label,
3255 "IO spec-list cannot contain both NAMELIST group name "
3256 "and format label at %L", spec_end);
3258 io_constraint (dt->rec,
3259 "NAMELIST IO is not allowed with a REC= specifier "
3260 "at %L", &dt->rec->where);
3262 io_constraint (dt->advance,
3263 "NAMELIST IO is not allowed with a ADVANCE= specifier "
3264 "at %L", &dt->advance->where);
3269 io_constraint (dt->end,
3270 "An END tag is not allowed with a "
3271 "REC= specifier at %L", &dt->end_where);
3273 io_constraint (dt->format_label == &format_asterisk,
3274 "FMT=* is not allowed with a REC= specifier "
3277 io_constraint (dt->pos,
3278 "POS= is not allowed with REC= specifier "
3279 "at %L", &dt->pos->where);
3284 int not_yes, not_no;
3287 io_constraint (dt->format_label == &format_asterisk,
3288 "List directed format(*) is not allowed with a "
3289 "ADVANCE= specifier at %L.", &expr->where);
3291 io_constraint (unformatted,
3292 "the ADVANCE= specifier at %L must appear with an "
3293 "explicit format expression", &expr->where);
3295 if (expr->expr_type == EXPR_CONSTANT && expr->ts.type == BT_CHARACTER)
3297 const gfc_char_t *advance = expr->value.character.string;
3298 not_no = gfc_wide_strlen (advance) != 2
3299 || gfc_wide_strncasecmp (advance, "no", 2) != 0;
3300 not_yes = gfc_wide_strlen (advance) != 3
3301 || gfc_wide_strncasecmp (advance, "yes", 3) != 0;
3309 io_constraint (not_no && not_yes,
3310 "ADVANCE= specifier at %L must have value = "
3311 "YES or NO.", &expr->where);
3313 io_constraint (dt->size && not_no && k == M_READ,
3314 "SIZE tag at %L requires an ADVANCE = 'NO'",
3317 io_constraint (dt->eor && not_no && k == M_READ,
3318 "EOR tag at %L requires an ADVANCE = 'NO'",
3322 expr = dt->format_expr;
3323 if (gfc_simplify_expr (expr, 0) == FAILURE
3324 || check_format_string (expr, k == M_READ) == FAILURE)
3329 #undef io_constraint
3332 /* Match a READ, WRITE or PRINT statement. */
3335 match_io (io_kind k)
3337 char name[GFC_MAX_SYMBOL_LEN + 1];
3346 where = gfc_current_locus;
3348 current_dt = dt = XCNEW (gfc_dt);
3349 m = gfc_match_char ('(');
3352 where = gfc_current_locus;
3355 else if (k == M_PRINT)
3357 /* Treat the non-standard case of PRINT namelist. */
3358 if ((gfc_current_form == FORM_FIXED || gfc_peek_ascii_char () == ' ')
3359 && gfc_match_name (name) == MATCH_YES)
3361 gfc_find_symbol (name, NULL, 1, &sym);
3362 if (sym && sym->attr.flavor == FL_NAMELIST)
3364 if (gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
3365 "%C is an extension") == FAILURE)
3371 dt->io_unit = default_unit (k);
3376 gfc_current_locus = where;
3380 if (gfc_current_form == FORM_FREE)
3382 char c = gfc_peek_ascii_char ();
3383 if (c != ' ' && c != '*' && c != '\'' && c != '"')
3390 m = match_dt_format (dt);
3391 if (m == MATCH_ERROR)
3397 dt->io_unit = default_unit (k);
3402 /* Before issuing an error for a malformed 'print (1,*)' type of
3403 error, check for a default-char-expr of the form ('(I0)'). */
3404 if (k == M_PRINT && m == MATCH_YES)
3406 /* Reset current locus to get the initial '(' in an expression. */
3407 gfc_current_locus = where;
3408 dt->format_expr = NULL;
3409 m = match_dt_format (dt);
3411 if (m == MATCH_ERROR)
3413 if (m == MATCH_NO || dt->format_expr == NULL)
3417 dt->io_unit = default_unit (k);
3422 /* Match a control list */
3423 if (match_dt_element (k, dt) == MATCH_YES)
3425 if (match_dt_unit (k, dt) != MATCH_YES)
3428 if (gfc_match_char (')') == MATCH_YES)
3430 if (gfc_match_char (',') != MATCH_YES)
3433 m = match_dt_element (k, dt);
3436 if (m == MATCH_ERROR)
3439 m = match_dt_format (dt);
3442 if (m == MATCH_ERROR)
3445 where = gfc_current_locus;
3447 m = gfc_match_name (name);
3450 gfc_find_symbol (name, NULL, 1, &sym);
3451 if (sym && sym->attr.flavor == FL_NAMELIST)
3454 if (k == M_READ && check_namelist (sym))
3463 gfc_current_locus = where;
3465 goto loop; /* No matches, try regular elements */
3468 if (gfc_match_char (')') == MATCH_YES)
3470 if (gfc_match_char (',') != MATCH_YES)
3476 m = match_dt_element (k, dt);
3479 if (m == MATCH_ERROR)
3482 if (gfc_match_char (')') == MATCH_YES)
3484 if (gfc_match_char (',') != MATCH_YES)
3490 /* Used in check_io_constraints, where no locus is available. */
3491 spec_end = gfc_current_locus;
3493 /* Optional leading comma (non-standard). We use a gfc_expr structure here
3494 to save the locus. This is used later when resolving transfer statements
3495 that might have a format expression without unit number. */
3496 if (!comma_flag && gfc_match_char (',') == MATCH_YES)
3498 dt->extra_comma = gfc_get_expr ();
3500 /* Set the types to something compatible with iokind. This is needed to
3501 get through gfc_free_expr later since iokind really has no Basic Type,
3503 dt->extra_comma->expr_type = EXPR_CONSTANT;
3504 dt->extra_comma->ts.type = BT_LOGICAL;
3506 /* Save the iokind and locus for later use in resolution. */
3507 dt->extra_comma->value.iokind = k;
3508 dt->extra_comma->where = gfc_current_locus;
3512 if (gfc_match_eos () != MATCH_YES)
3514 if (comma_flag && gfc_match_char (',') != MATCH_YES)
3516 gfc_error ("Expected comma in I/O list at %C");
3521 m = match_io_list (k, &io_code);
3522 if (m == MATCH_ERROR)
3528 /* A full IO statement has been matched. Check the constraints. spec_end is
3529 supplied for cases where no locus is supplied. */
3530 m = check_io_constraints (k, dt, io_code, &spec_end);
3532 if (m == MATCH_ERROR)
3535 new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
3537 new_st.block = gfc_get_code ();
3538 new_st.block->op = new_st.op;
3539 new_st.block->next = io_code;
3541 terminate_io (io_code);
3546 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
3556 gfc_match_read (void)
3558 return match_io (M_READ);
3563 gfc_match_write (void)
3565 return match_io (M_WRITE);
3570 gfc_match_print (void)
3574 m = match_io (M_PRINT);
3578 if (gfc_pure (NULL))
3580 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
3588 /* Free a gfc_inquire structure. */
3591 gfc_free_inquire (gfc_inquire *inquire)
3594 if (inquire == NULL)
3597 gfc_free_expr (inquire->unit);
3598 gfc_free_expr (inquire->file);
3599 gfc_free_expr (inquire->iomsg);
3600 gfc_free_expr (inquire->iostat);
3601 gfc_free_expr (inquire->exist);
3602 gfc_free_expr (inquire->opened);
3603 gfc_free_expr (inquire->number);
3604 gfc_free_expr (inquire->named);
3605 gfc_free_expr (inquire->name);
3606 gfc_free_expr (inquire->access);
3607 gfc_free_expr (inquire->sequential);
3608 gfc_free_expr (inquire->direct);
3609 gfc_free_expr (inquire->form);
3610 gfc_free_expr (inquire->formatted);
3611 gfc_free_expr (inquire->unformatted);
3612 gfc_free_expr (inquire->recl);
3613 gfc_free_expr (inquire->nextrec);
3614 gfc_free_expr (inquire->blank);
3615 gfc_free_expr (inquire->position);
3616 gfc_free_expr (inquire->action);
3617 gfc_free_expr (inquire->read);
3618 gfc_free_expr (inquire->write);
3619 gfc_free_expr (inquire->readwrite);
3620 gfc_free_expr (inquire->delim);
3621 gfc_free_expr (inquire->encoding);
3622 gfc_free_expr (inquire->pad);
3623 gfc_free_expr (inquire->iolength);
3624 gfc_free_expr (inquire->convert);
3625 gfc_free_expr (inquire->strm_pos);
3626 gfc_free_expr (inquire->asynchronous);
3627 gfc_free_expr (inquire->decimal);
3628 gfc_free_expr (inquire->pending);
3629 gfc_free_expr (inquire->id);
3630 gfc_free_expr (inquire->sign);
3631 gfc_free_expr (inquire->size);
3632 gfc_free_expr (inquire->round);
3637 /* Match an element of an INQUIRE statement. */
3639 #define RETM if (m != MATCH_NO) return m;
3642 match_inquire_element (gfc_inquire *inquire)
3646 m = match_etag (&tag_unit, &inquire->unit);
3647 RETM m = match_etag (&tag_file, &inquire->file);
3648 RETM m = match_ltag (&tag_err, &inquire->err);
3649 RETM m = match_out_tag (&tag_iomsg, &inquire->iomsg);
3650 RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
3651 RETM m = match_vtag (&tag_exist, &inquire->exist);
3652 RETM m = match_vtag (&tag_opened, &inquire->opened);
3653 RETM m = match_vtag (&tag_named, &inquire->named);
3654 RETM m = match_vtag (&tag_name, &inquire->name);
3655 RETM m = match_out_tag (&tag_number, &inquire->number);
3656 RETM m = match_vtag (&tag_s_access, &inquire->access);
3657 RETM m = match_vtag (&tag_sequential, &inquire->sequential);
3658 RETM m = match_vtag (&tag_direct, &inquire->direct);
3659 RETM m = match_vtag (&tag_s_form, &inquire->form);
3660 RETM m = match_vtag (&tag_formatted, &inquire->formatted);
3661 RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
3662 RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
3663 RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
3664 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
3665 RETM m = match_vtag (&tag_s_position, &inquire->position);
3666 RETM m = match_vtag (&tag_s_action, &inquire->action);
3667 RETM m = match_vtag (&tag_read, &inquire->read);
3668 RETM m = match_vtag (&tag_write, &inquire->write);
3669 RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
3670 RETM m = match_vtag (&tag_s_async, &inquire->asynchronous);
3671 RETM m = match_vtag (&tag_s_delim, &inquire->delim);
3672 RETM m = match_vtag (&tag_s_decimal, &inquire->decimal);
3673 RETM m = match_vtag (&tag_size, &inquire->size);
3674 RETM m = match_vtag (&tag_s_encoding, &inquire->encoding);
3675 RETM m = match_vtag (&tag_s_round, &inquire->round);
3676 RETM m = match_vtag (&tag_s_sign, &inquire->sign);
3677 RETM m = match_vtag (&tag_s_pad, &inquire->pad);
3678 RETM m = match_vtag (&tag_iolength, &inquire->iolength);
3679 RETM m = match_vtag (&tag_convert, &inquire->convert);
3680 RETM m = match_out_tag (&tag_strm_out, &inquire->strm_pos);
3681 RETM m = match_vtag (&tag_pending, &inquire->pending);
3682 RETM m = match_vtag (&tag_id, &inquire->id);
3683 RETM return MATCH_NO;
3690 gfc_match_inquire (void)
3692 gfc_inquire *inquire;
3697 m = gfc_match_char ('(');
3701 inquire = XCNEW (gfc_inquire);
3703 loc = gfc_current_locus;
3705 m = match_inquire_element (inquire);
3706 if (m == MATCH_ERROR)
3710 m = gfc_match_expr (&inquire->unit);
3711 if (m == MATCH_ERROR)
3717 /* See if we have the IOLENGTH form of the inquire statement. */
3718 if (inquire->iolength != NULL)
3720 if (gfc_match_char (')') != MATCH_YES)
3723 m = match_io_list (M_INQUIRE, &code);
3724 if (m == MATCH_ERROR)
3729 new_st.op = EXEC_IOLENGTH;
3730 new_st.expr1 = inquire->iolength;
3731 new_st.ext.inquire = inquire;
3733 if (gfc_pure (NULL))
3735 gfc_free_statements (code);
3736 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3740 new_st.block = gfc_get_code ();
3741 new_st.block->op = EXEC_IOLENGTH;
3742 terminate_io (code);
3743 new_st.block->next = code;
3747 /* At this point, we have the non-IOLENGTH inquire statement. */
3750 if (gfc_match_char (')') == MATCH_YES)
3752 if (gfc_match_char (',') != MATCH_YES)
3755 m = match_inquire_element (inquire);
3756 if (m == MATCH_ERROR)
3761 if (inquire->iolength != NULL)
3763 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3768 if (gfc_match_eos () != MATCH_YES)
3771 if (inquire->unit != NULL && inquire->file != NULL)
3773 gfc_error ("INQUIRE statement at %L cannot contain both FILE and "
3774 "UNIT specifiers", &loc);
3778 if (inquire->unit == NULL && inquire->file == NULL)
3780 gfc_error ("INQUIRE statement at %L requires either FILE or "
3781 "UNIT specifier", &loc);
3785 if (gfc_pure (NULL))
3787 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3791 if (inquire->id != NULL && inquire->pending == NULL)
3793 gfc_error ("INQUIRE statement at %L requires a PENDING= specifier with "
3794 "the ID= specifier", &loc);
3798 new_st.op = EXEC_INQUIRE;
3799 new_st.ext.inquire = inquire;
3803 gfc_syntax_error (ST_INQUIRE);
3806 gfc_free_inquire (inquire);
3811 /* Resolve everything in a gfc_inquire structure. */
3814 gfc_resolve_inquire (gfc_inquire *inquire)
3816 RESOLVE_TAG (&tag_unit, inquire->unit);
3817 RESOLVE_TAG (&tag_file, inquire->file);
3818 RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
3819 RESOLVE_TAG (&tag_iostat, inquire->iostat);
3820 RESOLVE_TAG (&tag_exist, inquire->exist);
3821 RESOLVE_TAG (&tag_opened, inquire->opened);
3822 RESOLVE_TAG (&tag_number, inquire->number);
3823 RESOLVE_TAG (&tag_named, inquire->named);
3824 RESOLVE_TAG (&tag_name, inquire->name);
3825 RESOLVE_TAG (&tag_s_access, inquire->access);
3826 RESOLVE_TAG (&tag_sequential, inquire->sequential);
3827 RESOLVE_TAG (&tag_direct, inquire->direct);
3828 RESOLVE_TAG (&tag_s_form, inquire->form);
3829 RESOLVE_TAG (&tag_formatted, inquire->formatted);
3830 RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
3831 RESOLVE_TAG (&tag_s_recl, inquire->recl);
3832 RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
3833 RESOLVE_TAG (&tag_s_blank, inquire->blank);
3834 RESOLVE_TAG (&tag_s_position, inquire->position);
3835 RESOLVE_TAG (&tag_s_action, inquire->action);
3836 RESOLVE_TAG (&tag_read, inquire->read);
3837 RESOLVE_TAG (&tag_write, inquire->write);
3838 RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
3839 RESOLVE_TAG (&tag_s_delim, inquire->delim);
3840 RESOLVE_TAG (&tag_s_pad, inquire->pad);
3841 RESOLVE_TAG (&tag_s_encoding, inquire->encoding);
3842 RESOLVE_TAG (&tag_s_round, inquire->round);
3843 RESOLVE_TAG (&tag_iolength, inquire->iolength);
3844 RESOLVE_TAG (&tag_convert, inquire->convert);
3845 RESOLVE_TAG (&tag_strm_out, inquire->strm_pos);
3846 RESOLVE_TAG (&tag_s_async, inquire->asynchronous);
3847 RESOLVE_TAG (&tag_s_sign, inquire->sign);
3848 RESOLVE_TAG (&tag_s_round, inquire->round);
3849 RESOLVE_TAG (&tag_pending, inquire->pending);
3850 RESOLVE_TAG (&tag_size, inquire->size);
3851 RESOLVE_TAG (&tag_id, inquire->id);
3853 if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)
3861 gfc_free_wait (gfc_wait *wait)
3866 gfc_free_expr (wait->unit);
3867 gfc_free_expr (wait->iostat);
3868 gfc_free_expr (wait->iomsg);
3869 gfc_free_expr (wait->id);
3874 gfc_resolve_wait (gfc_wait *wait)
3876 RESOLVE_TAG (&tag_unit, wait->unit);
3877 RESOLVE_TAG (&tag_iomsg, wait->iomsg);
3878 RESOLVE_TAG (&tag_iostat, wait->iostat);
3879 RESOLVE_TAG (&tag_id, wait->id);
3881 if (gfc_reference_st_label (wait->err, ST_LABEL_TARGET) == FAILURE)
3884 if (gfc_reference_st_label (wait->end, ST_LABEL_TARGET) == FAILURE)
3890 /* Match an element of a WAIT statement. */
3892 #define RETM if (m != MATCH_NO) return m;
3895 match_wait_element (gfc_wait *wait)
3899 m = match_etag (&tag_unit, &wait->unit);
3900 RETM m = match_ltag (&tag_err, &wait->err);
3901 RETM m = match_ltag (&tag_end, &wait->eor);
3902 RETM m = match_ltag (&tag_eor, &wait->end);
3903 RETM m = match_out_tag (&tag_iomsg, &wait->iomsg);
3904 RETM m = match_out_tag (&tag_iostat, &wait->iostat);
3905 RETM m = match_etag (&tag_id, &wait->id);
3906 RETM return MATCH_NO;
3913 gfc_match_wait (void)
3919 m = gfc_match_char ('(');
3923 wait = XCNEW (gfc_wait);
3925 loc = gfc_current_locus;
3927 m = match_wait_element (wait);
3928 if (m == MATCH_ERROR)
3932 m = gfc_match_expr (&wait->unit);
3933 if (m == MATCH_ERROR)
3941 if (gfc_match_char (')') == MATCH_YES)
3943 if (gfc_match_char (',') != MATCH_YES)
3946 m = match_wait_element (wait);
3947 if (m == MATCH_ERROR)
3953 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: WAIT at %C "
3954 "not allowed in Fortran 95") == FAILURE)
3957 if (gfc_pure (NULL))
3959 gfc_error ("WAIT statement not allowed in PURE procedure at %C");
3963 new_st.op = EXEC_WAIT;
3964 new_st.ext.wait = wait;
3969 gfc_syntax_error (ST_WAIT);
3972 gfc_free_wait (wait);