1 /* Deal with I/O statements & related stuff.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation,
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
30 gfc_st_label format_asterisk =
31 { -1, ST_LABEL_FORMAT, ST_LABEL_FORMAT, NULL, 0,
32 {NULL, NULL}, NULL, NULL};
36 const char *name, *spec;
42 tag_file = { "FILE", " file = %e", BT_CHARACTER },
43 tag_status = { "STATUS", " status = %e", BT_CHARACTER},
44 tag_e_access = {"ACCESS", " access = %e", BT_CHARACTER},
45 tag_e_form = {"FORM", " form = %e", BT_CHARACTER},
46 tag_e_recl = {"RECL", " recl = %e", BT_INTEGER},
47 tag_e_blank = {"BLANK", " blank = %e", BT_CHARACTER},
48 tag_e_position = {"POSITION", " position = %e", BT_CHARACTER},
49 tag_e_action = {"ACTION", " action = %e", BT_CHARACTER},
50 tag_e_delim = {"DELIM", " delim = %e", BT_CHARACTER},
51 tag_e_pad = {"PAD", " pad = %e", BT_CHARACTER},
52 tag_unit = {"UNIT", " unit = %e", BT_INTEGER},
53 tag_advance = {"ADVANCE", " advance = %e", BT_CHARACTER},
54 tag_rec = {"REC", " rec = %e", BT_INTEGER},
55 tag_format = {"FORMAT", NULL, BT_CHARACTER},
56 tag_iomsg = {"IOMSG", " iomsg = %e", BT_CHARACTER},
57 tag_iostat = {"IOSTAT", " iostat = %v", BT_INTEGER},
58 tag_size = {"SIZE", " size = %v", BT_INTEGER},
59 tag_exist = {"EXIST", " exist = %v", BT_LOGICAL},
60 tag_opened = {"OPENED", " opened = %v", BT_LOGICAL},
61 tag_named = {"NAMED", " named = %v", BT_LOGICAL},
62 tag_name = {"NAME", " name = %v", BT_CHARACTER},
63 tag_number = {"NUMBER", " number = %v", BT_INTEGER},
64 tag_s_access = {"ACCESS", " access = %v", BT_CHARACTER},
65 tag_sequential = {"SEQUENTIAL", " sequential = %v", BT_CHARACTER},
66 tag_direct = {"DIRECT", " direct = %v", BT_CHARACTER},
67 tag_s_form = {"FORM", " form = %v", BT_CHARACTER},
68 tag_formatted = {"FORMATTED", " formatted = %v", BT_CHARACTER},
69 tag_unformatted = {"UNFORMATTED", " unformatted = %v", BT_CHARACTER},
70 tag_s_recl = {"RECL", " recl = %v", BT_INTEGER},
71 tag_nextrec = {"NEXTREC", " nextrec = %v", BT_INTEGER},
72 tag_s_blank = {"BLANK", " blank = %v", BT_CHARACTER},
73 tag_s_position = {"POSITION", " position = %v", BT_CHARACTER},
74 tag_s_action = {"ACTION", " action = %v", BT_CHARACTER},
75 tag_read = {"READ", " read = %v", BT_CHARACTER},
76 tag_write = {"WRITE", " write = %v", BT_CHARACTER},
77 tag_readwrite = {"READWRITE", " readwrite = %v", BT_CHARACTER},
78 tag_s_delim = {"DELIM", " delim = %v", BT_CHARACTER},
79 tag_s_pad = {"PAD", " pad = %v", BT_CHARACTER},
80 tag_iolength = {"IOLENGTH", " iolength = %v", BT_INTEGER},
81 tag_convert = {"CONVERT", " convert = %e", BT_CHARACTER},
82 tag_err = {"ERR", " err = %l", BT_UNKNOWN},
83 tag_end = {"END", " end = %l", BT_UNKNOWN},
84 tag_eor = {"EOR", " eor = %l", BT_UNKNOWN};
86 static gfc_dt *current_dt;
88 #define RESOLVE_TAG(x, y) if (resolve_tag(x, y) == FAILURE) return FAILURE;
91 /**************** Fortran 95 FORMAT parser *****************/
93 /* FORMAT tokens returned by format_lex(). */
96 FMT_NONE, FMT_UNKNOWN, FMT_SIGNED_INT, FMT_ZERO, FMT_POSINT, FMT_PERIOD,
97 FMT_COMMA, FMT_COLON, FMT_SLASH, FMT_DOLLAR, FMT_POS, FMT_LPAREN,
98 FMT_RPAREN, FMT_X, FMT_SIGN, FMT_BLANK, FMT_CHAR, FMT_P, FMT_IBOZ, FMT_F,
99 FMT_E, FMT_EXT, FMT_G, FMT_L, FMT_A, FMT_D, FMT_H, FMT_END
103 /* Local variables for checking format strings. The saved_token is
104 used to back up by a single format token during the parsing
106 static char *format_string;
107 static int format_length, use_last_char;
109 static format_token saved_token;
112 { MODE_STRING, MODE_FORMAT, MODE_COPY }
116 /* Return the next character in the format string. */
119 next_char (int in_string)
131 if (mode == MODE_STRING)
132 c = *format_string++;
135 c = gfc_next_char_literal (in_string);
139 if (mode == MODE_COPY)
140 *format_string++ = c;
148 /* Back up one character position. Only works once. */
157 static int value = 0;
159 /* Simple lexical analyzer for getting the next token in a FORMAT
170 if (saved_token != FMT_NONE)
173 saved_token = FMT_NONE;
181 while (gfc_is_whitespace (c));
202 value = 10 * value + c - '0';
211 token = FMT_SIGNED_INT;
234 value = 10 * value + c - '0';
236 while (ISDIGIT (c) || gfc_is_whitespace(c));
239 token = zflag ? FMT_ZERO : FMT_POSINT;
264 if (c != 'L' && c != 'R')
284 if (c != 'P' && c != 'S')
292 if (c == 'N' || c == 'Z')
354 if (c == 'N' || c == 'S')
397 /* Check a format statement. The format string, either from a FORMAT
398 statement or a constant in an I/O statement has already been parsed
399 by itself, and we are checking it for validity. The dual origin
400 means that the warning message is a little less than great. */
405 const char *posint_required = _("Positive width required");
406 const char *period_required = _("Period required");
407 const char *nonneg_required = _("Nonnegative width required");
408 const char *unexpected_element = _("Unexpected element");
409 const char *unexpected_end = _("Unexpected end of format string");
418 saved_token = FMT_NONE;
426 error = _("Missing leading left parenthesis");
432 goto finished; /* Empty format is legal */
436 /* In this state, the next thing has to be a format item. */
460 /* Signed integer can only precede a P format. */
464 error = _("Expected P edit descriptor");
471 /* P requires a prior number. */
472 error = _("P descriptor requires leading scale factor");
476 /* X requires a prior number if we're being pedantic. */
477 if (gfc_notify_std (GFC_STD_GNU, "Extension: X descriptor "
478 "requires leading space count at %C")
488 goto extension_optional_comma;
497 if (gfc_notify_std (GFC_STD_GNU, "Extension: $ descriptor at %C")
500 if (t != FMT_RPAREN || level > 0)
502 error = _("$ must be the last specifier");
523 error = unexpected_end;
527 error = unexpected_element;
532 /* In this state, t must currently be a data descriptor.
533 Deal with things that can/must follow the descriptor. */
547 error = _("Repeat count cannot follow P descriptor");
562 error = posint_required;
578 error = posint_required;
585 error = period_required;
590 if (u != FMT_ZERO && u != FMT_POSINT)
592 error = nonneg_required;
599 /* Look for optional exponent. */
610 error = _("Positive exponent width required");
619 if (t != FMT_ZERO && t != FMT_POSINT)
621 error = nonneg_required;
628 error = period_required;
633 if (t != FMT_ZERO && t != FMT_POSINT)
635 error = nonneg_required;
642 if(mode == MODE_STRING)
644 format_string += value;
645 format_length -= value;
659 if (t != FMT_ZERO && t != FMT_POSINT)
661 error = nonneg_required;
673 if (t != FMT_ZERO && t != FMT_POSINT)
675 error = nonneg_required;
683 error = unexpected_element;
688 /* Between a descriptor and what comes next. */
707 error = unexpected_end;
711 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
718 /* Optional comma is a weird between state where we've just finished
719 reading a colon, slash or P descriptor. */
733 /* Assume that we have another format item. */
740 extension_optional_comma:
741 /* As a GNU extension, permit a missing comma after a string literal. */
759 error = unexpected_end;
763 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
773 /* Something went wrong. If the format we're checking is a string,
774 generate a warning, since the program is correct. If the format
775 is in a FORMAT statement, this messes up parsing, which is an
777 if (mode != MODE_STRING)
778 gfc_error ("%s in format string at %C", error);
781 gfc_warning ("%s in format string at %C", error);
783 /* TODO: More elaborate measures are needed to show where a problem
784 is within a format string that has been calculated. */
794 /* Given an expression node that is a constant string, see if it looks
795 like a format string. */
798 check_format_string (gfc_expr * e)
802 format_string = e->value.character.string;
807 /************ Fortran 95 I/O statement matchers *************/
809 /* Match a FORMAT statement. This amounts to actually parsing the
810 format descriptors in order to correctly locate the end of the
814 gfc_match_format (void)
819 if (gfc_statement_label == NULL)
821 gfc_error ("Missing format label at %C");
824 gfc_gobble_whitespace ();
829 start = gfc_current_locus;
831 if (check_format () == FAILURE)
834 if (gfc_match_eos () != MATCH_YES)
836 gfc_syntax_error (ST_FORMAT);
840 /* The label doesn't get created until after the statement is done
841 being matched, so we have to leave the string for later. */
843 gfc_current_locus = start; /* Back to the beginning */
846 new_st.op = EXEC_NOP;
849 e->expr_type = EXPR_CONSTANT;
850 e->ts.type = BT_CHARACTER;
851 e->ts.kind = gfc_default_character_kind;
853 e->value.character.string = format_string = gfc_getmem(format_length+1);
854 e->value.character.length = format_length;
855 gfc_statement_label->format = e;
858 check_format (); /* Guaranteed to succeed */
859 gfc_match_eos (); /* Guaranteed to succeed */
865 /* Match an expression I/O tag of some sort. */
868 match_etag (const io_tag * tag, gfc_expr ** v)
873 m = gfc_match (tag->spec, &result);
879 gfc_error ("Duplicate %s specification at %C", tag->name);
880 gfc_free_expr (result);
889 /* Match a variable I/O tag of some sort. */
892 match_vtag (const io_tag * tag, gfc_expr ** v)
897 m = gfc_match (tag->spec, &result);
903 gfc_error ("Duplicate %s specification at %C", tag->name);
904 gfc_free_expr (result);
908 if (result->symtree->n.sym->attr.intent == INTENT_IN)
910 gfc_error ("Variable tag cannot be INTENT(IN) at %C");
911 gfc_free_expr (result);
915 if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
917 gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
918 gfc_free_expr (result);
927 /* Match I/O tags that cause variables to become redefined. */
930 match_out_tag(const io_tag *tag, gfc_expr **result)
934 m = match_vtag(tag, result);
936 gfc_check_do_variable((*result)->symtree);
942 /* Match a label I/O tag. */
945 match_ltag (const io_tag * tag, gfc_st_label ** label)
951 m = gfc_match (tag->spec, label);
952 if (m == MATCH_YES && old != 0)
954 gfc_error ("Duplicate %s label specification at %C", tag->name);
962 /* Do expression resolution and type-checking on an expression tag. */
965 resolve_tag (const io_tag * tag, gfc_expr * e)
971 if (gfc_resolve_expr (e) == FAILURE)
974 if (e->ts.type != tag->type && tag != &tag_format)
976 gfc_error ("%s tag at %L must be of type %s", tag->name,
977 &e->where, gfc_basic_typename (tag->type));
981 if (tag == &tag_format)
983 if (e->expr_type == EXPR_CONSTANT
984 && (e->ts.type != BT_CHARACTER
985 || e->ts.kind != gfc_default_character_kind))
987 gfc_error ("Constant expression in FORMAT tag at %L must be "
988 "of type default CHARACTER", &e->where);
992 /* If e's rank is zero and e is not an element of an array, it should be
993 of integer or character type. The integer variable should be
995 if (e->symtree == NULL || e->symtree->n.sym->as == NULL
996 || e->symtree->n.sym->as->rank == 0)
998 if (e->ts.type != BT_CHARACTER && e->ts.type != BT_INTEGER)
1000 gfc_error ("%s tag at %L must be of type %s or %s", tag->name,
1001 &e->where, gfc_basic_typename (BT_CHARACTER),
1002 gfc_basic_typename (BT_INTEGER));
1005 else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
1007 if (gfc_notify_std (GFC_STD_F95_DEL,
1008 "Obsolete: ASSIGNED variable in FORMAT tag at %L",
1009 &e->where) == FAILURE)
1011 if (e->symtree->n.sym->attr.assign != 1)
1013 gfc_error ("Variable '%s' at %L has not been assigned a "
1014 "format label", e->symtree->n.sym->name, &e->where);
1022 /* if rank is nonzero, we allow the type to be character under
1023 GFC_STD_GNU and other type under GFC_STD_LEGACY. It may be
1024 assigned an Hollerith constant. */
1025 if (e->ts.type == BT_CHARACTER)
1027 if (gfc_notify_std (GFC_STD_GNU,
1028 "Extension: Character array in FORMAT tag at %L",
1029 &e->where) == FAILURE)
1034 if (gfc_notify_std (GFC_STD_LEGACY,
1035 "Extension: Non-character in FORMAT tag at %L",
1036 &e->where) == FAILURE)
1046 gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
1050 if (tag == &tag_iomsg)
1052 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IOMSG tag at %L",
1053 &e->where) == FAILURE)
1057 if (tag == &tag_iostat && e->ts.kind != gfc_default_integer_kind)
1059 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Non-default "
1060 "integer kind in IOSTAT tag at %L",
1061 &e->where) == FAILURE)
1065 if (tag == &tag_convert)
1067 if (gfc_notify_std (GFC_STD_GNU, "Extension: CONVERT tag at %L",
1068 &e->where) == FAILURE)
1077 /* Match a single tag of an OPEN statement. */
1080 match_open_element (gfc_open * open)
1084 m = match_etag (&tag_unit, &open->unit);
1087 m = match_out_tag (&tag_iomsg, &open->iomsg);
1090 m = match_out_tag (&tag_iostat, &open->iostat);
1093 m = match_etag (&tag_file, &open->file);
1096 m = match_etag (&tag_status, &open->status);
1099 m = match_etag (&tag_e_access, &open->access);
1102 m = match_etag (&tag_e_form, &open->form);
1105 m = match_etag (&tag_e_recl, &open->recl);
1108 m = match_etag (&tag_e_blank, &open->blank);
1111 m = match_etag (&tag_e_position, &open->position);
1114 m = match_etag (&tag_e_action, &open->action);
1117 m = match_etag (&tag_e_delim, &open->delim);
1120 m = match_etag (&tag_e_pad, &open->pad);
1123 m = match_ltag (&tag_err, &open->err);
1126 m = match_etag (&tag_convert, &open->convert);
1134 /* Free the gfc_open structure and all the expressions it contains. */
1137 gfc_free_open (gfc_open * open)
1143 gfc_free_expr (open->unit);
1144 gfc_free_expr (open->iomsg);
1145 gfc_free_expr (open->iostat);
1146 gfc_free_expr (open->file);
1147 gfc_free_expr (open->status);
1148 gfc_free_expr (open->access);
1149 gfc_free_expr (open->form);
1150 gfc_free_expr (open->recl);
1151 gfc_free_expr (open->blank);
1152 gfc_free_expr (open->position);
1153 gfc_free_expr (open->action);
1154 gfc_free_expr (open->delim);
1155 gfc_free_expr (open->pad);
1156 gfc_free_expr (open->convert);
1162 /* Resolve everything in a gfc_open structure. */
1165 gfc_resolve_open (gfc_open * open)
1168 RESOLVE_TAG (&tag_unit, open->unit);
1169 RESOLVE_TAG (&tag_iomsg, open->iomsg);
1170 RESOLVE_TAG (&tag_iostat, open->iostat);
1171 RESOLVE_TAG (&tag_file, open->file);
1172 RESOLVE_TAG (&tag_status, open->status);
1173 RESOLVE_TAG (&tag_e_access, open->access);
1174 RESOLVE_TAG (&tag_e_form, open->form);
1175 RESOLVE_TAG (&tag_e_recl, open->recl);
1177 RESOLVE_TAG (&tag_e_blank, open->blank);
1178 RESOLVE_TAG (&tag_e_position, open->position);
1179 RESOLVE_TAG (&tag_e_action, open->action);
1180 RESOLVE_TAG (&tag_e_delim, open->delim);
1181 RESOLVE_TAG (&tag_e_pad, open->pad);
1182 RESOLVE_TAG (&tag_convert, open->convert);
1184 if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1191 /* Match an OPEN statement. */
1194 gfc_match_open (void)
1199 m = gfc_match_char ('(');
1203 open = gfc_getmem (sizeof (gfc_open));
1205 m = match_open_element (open);
1207 if (m == MATCH_ERROR)
1211 m = gfc_match_expr (&open->unit);
1214 if (m == MATCH_ERROR)
1220 if (gfc_match_char (')') == MATCH_YES)
1222 if (gfc_match_char (',') != MATCH_YES)
1225 m = match_open_element (open);
1226 if (m == MATCH_ERROR)
1232 if (gfc_match_eos () == MATCH_NO)
1235 if (gfc_pure (NULL))
1237 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1241 new_st.op = EXEC_OPEN;
1242 new_st.ext.open = open;
1246 gfc_syntax_error (ST_OPEN);
1249 gfc_free_open (open);
1254 /* Free a gfc_close structure an all its expressions. */
1257 gfc_free_close (gfc_close * close)
1263 gfc_free_expr (close->unit);
1264 gfc_free_expr (close->iomsg);
1265 gfc_free_expr (close->iostat);
1266 gfc_free_expr (close->status);
1272 /* Match elements of a CLOSE statement. */
1275 match_close_element (gfc_close * close)
1279 m = match_etag (&tag_unit, &close->unit);
1282 m = match_etag (&tag_status, &close->status);
1285 m = match_out_tag (&tag_iomsg, &close->iomsg);
1288 m = match_out_tag (&tag_iostat, &close->iostat);
1291 m = match_ltag (&tag_err, &close->err);
1299 /* Match a CLOSE statement. */
1302 gfc_match_close (void)
1307 m = gfc_match_char ('(');
1311 close = gfc_getmem (sizeof (gfc_close));
1313 m = match_close_element (close);
1315 if (m == MATCH_ERROR)
1319 m = gfc_match_expr (&close->unit);
1322 if (m == MATCH_ERROR)
1328 if (gfc_match_char (')') == MATCH_YES)
1330 if (gfc_match_char (',') != MATCH_YES)
1333 m = match_close_element (close);
1334 if (m == MATCH_ERROR)
1340 if (gfc_match_eos () == MATCH_NO)
1343 if (gfc_pure (NULL))
1345 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1349 new_st.op = EXEC_CLOSE;
1350 new_st.ext.close = close;
1354 gfc_syntax_error (ST_CLOSE);
1357 gfc_free_close (close);
1362 /* Resolve everything in a gfc_close structure. */
1365 gfc_resolve_close (gfc_close * close)
1368 RESOLVE_TAG (&tag_unit, close->unit);
1369 RESOLVE_TAG (&tag_iomsg, close->iomsg);
1370 RESOLVE_TAG (&tag_iostat, close->iostat);
1371 RESOLVE_TAG (&tag_status, close->status);
1373 if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
1380 /* Free a gfc_filepos structure. */
1383 gfc_free_filepos (gfc_filepos * fp)
1386 gfc_free_expr (fp->unit);
1387 gfc_free_expr (fp->iomsg);
1388 gfc_free_expr (fp->iostat);
1393 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
1396 match_file_element (gfc_filepos * fp)
1400 m = match_etag (&tag_unit, &fp->unit);
1403 m = match_out_tag (&tag_iomsg, &fp->iomsg);
1406 m = match_out_tag (&tag_iostat, &fp->iostat);
1409 m = match_ltag (&tag_err, &fp->err);
1417 /* Match the second half of the file-positioning statements, REWIND,
1418 BACKSPACE, ENDFILE, or the FLUSH statement. */
1421 match_filepos (gfc_statement st, gfc_exec_op op)
1426 fp = gfc_getmem (sizeof (gfc_filepos));
1428 if (gfc_match_char ('(') == MATCH_NO)
1430 m = gfc_match_expr (&fp->unit);
1431 if (m == MATCH_ERROR)
1439 m = match_file_element (fp);
1440 if (m == MATCH_ERROR)
1444 m = gfc_match_expr (&fp->unit);
1445 if (m == MATCH_ERROR)
1453 if (gfc_match_char (')') == MATCH_YES)
1455 if (gfc_match_char (',') != MATCH_YES)
1458 m = match_file_element (fp);
1459 if (m == MATCH_ERROR)
1466 if (gfc_match_eos () != MATCH_YES)
1469 if (gfc_pure (NULL))
1471 gfc_error ("%s statement not allowed in PURE procedure at %C",
1472 gfc_ascii_statement (st));
1478 new_st.ext.filepos = fp;
1482 gfc_syntax_error (st);
1485 gfc_free_filepos (fp);
1491 gfc_resolve_filepos (gfc_filepos * fp)
1494 RESOLVE_TAG (&tag_unit, fp->unit);
1495 RESOLVE_TAG (&tag_iostat, fp->iostat);
1496 RESOLVE_TAG (&tag_iomsg, fp->iomsg);
1497 if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
1504 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
1505 and the FLUSH statement. */
1508 gfc_match_endfile (void)
1511 return match_filepos (ST_END_FILE, EXEC_ENDFILE);
1515 gfc_match_backspace (void)
1518 return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
1522 gfc_match_rewind (void)
1525 return match_filepos (ST_REWIND, EXEC_REWIND);
1529 gfc_match_flush (void)
1531 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: FLUSH statement at %C") == FAILURE)
1534 return match_filepos (ST_FLUSH, EXEC_FLUSH);
1537 /******************** Data Transfer Statements *********************/
1540 { M_READ, M_WRITE, M_PRINT, M_INQUIRE }
1544 /* Return a default unit number. */
1547 default_unit (io_kind k)
1556 return gfc_int_expr (unit);
1560 /* Match a unit specification for a data transfer statement. */
1563 match_dt_unit (io_kind k, gfc_dt * dt)
1567 if (gfc_match_char ('*') == MATCH_YES)
1569 if (dt->io_unit != NULL)
1572 dt->io_unit = default_unit (k);
1576 if (gfc_match_expr (&e) == MATCH_YES)
1578 if (dt->io_unit != NULL)
1591 gfc_error ("Duplicate UNIT specification at %C");
1596 /* Match a format specification. */
1599 match_dt_format (gfc_dt * dt)
1603 gfc_st_label *label;
1605 where = gfc_current_locus;
1607 if (gfc_match_char ('*') == MATCH_YES)
1609 if (dt->format_expr != NULL || dt->format_label != NULL)
1612 dt->format_label = &format_asterisk;
1616 if (gfc_match_st_label (&label) == MATCH_YES)
1618 if (dt->format_expr != NULL || dt->format_label != NULL)
1620 gfc_free_st_label (label);
1624 if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
1627 dt->format_label = label;
1631 if (gfc_match_expr (&e) == MATCH_YES)
1633 if (dt->format_expr != NULL || dt->format_label != NULL)
1638 dt->format_expr = e;
1642 gfc_current_locus = where; /* The only case where we have to restore */
1647 gfc_error ("Duplicate format specification at %C");
1652 /* Traverse a namelist that is part of a READ statement to make sure
1653 that none of the variables in the namelist are INTENT(IN). Returns
1654 nonzero if we find such a variable. */
1657 check_namelist (gfc_symbol * sym)
1661 for (p = sym->namelist; p; p = p->next)
1662 if (p->sym->attr.intent == INTENT_IN)
1664 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
1665 p->sym->name, sym->name);
1673 /* Match a single data transfer element. */
1676 match_dt_element (io_kind k, gfc_dt * dt)
1678 char name[GFC_MAX_SYMBOL_LEN + 1];
1682 if (gfc_match (" unit =") == MATCH_YES)
1684 m = match_dt_unit (k, dt);
1689 if (gfc_match (" fmt =") == MATCH_YES)
1691 m = match_dt_format (dt);
1696 if (gfc_match (" nml = %n", name) == MATCH_YES)
1698 if (dt->namelist != NULL)
1700 gfc_error ("Duplicate NML specification at %C");
1704 if (gfc_find_symbol (name, NULL, 1, &sym))
1707 if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
1709 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
1710 sym != NULL ? sym->name : name);
1715 if (k == M_READ && check_namelist (sym))
1721 m = match_etag (&tag_rec, &dt->rec);
1724 m = match_out_tag (&tag_iomsg, &dt->iomsg);
1727 m = match_out_tag (&tag_iostat, &dt->iostat);
1730 m = match_ltag (&tag_err, &dt->err);
1733 m = match_etag (&tag_advance, &dt->advance);
1736 m = match_out_tag (&tag_size, &dt->size);
1740 m = match_ltag (&tag_end, &dt->end);
1745 gfc_error ("END tag at %C not allowed in output statement");
1748 dt->end_where = gfc_current_locus;
1753 m = match_ltag (&tag_eor, &dt->eor);
1755 dt->eor_where = gfc_current_locus;
1763 /* Free a data transfer structure and everything below it. */
1766 gfc_free_dt (gfc_dt * dt)
1772 gfc_free_expr (dt->io_unit);
1773 gfc_free_expr (dt->format_expr);
1774 gfc_free_expr (dt->rec);
1775 gfc_free_expr (dt->advance);
1776 gfc_free_expr (dt->iomsg);
1777 gfc_free_expr (dt->iostat);
1778 gfc_free_expr (dt->size);
1784 /* Resolve everything in a gfc_dt structure. */
1787 gfc_resolve_dt (gfc_dt * dt)
1791 RESOLVE_TAG (&tag_format, dt->format_expr);
1792 RESOLVE_TAG (&tag_rec, dt->rec);
1793 RESOLVE_TAG (&tag_advance, dt->advance);
1794 RESOLVE_TAG (&tag_iomsg, dt->iomsg);
1795 RESOLVE_TAG (&tag_iostat, dt->iostat);
1796 RESOLVE_TAG (&tag_size, dt->size);
1799 if (gfc_resolve_expr (e) == SUCCESS
1800 && (e->ts.type != BT_INTEGER
1801 && (e->ts.type != BT_CHARACTER
1802 || e->expr_type != EXPR_VARIABLE)))
1805 ("UNIT specification at %L must be an INTEGER expression or a "
1806 "CHARACTER variable", &e->where);
1810 /* Sanity checks on data transfer statements. */
1811 if (e->ts.type == BT_CHARACTER)
1813 if (gfc_has_vector_index (e))
1815 gfc_error ("Internal unit with vector subscript at %L",
1820 if (dt->rec != NULL)
1822 gfc_error ("REC tag at %L is incompatible with internal file",
1827 if (dt->namelist != NULL)
1829 gfc_error ("Internal file at %L is incompatible with namelist",
1830 &dt->io_unit->where);
1834 if (dt->advance != NULL)
1836 gfc_error ("ADVANCE tag at %L is incompatible with internal file",
1837 &dt->advance->where);
1842 if (dt->rec != NULL)
1844 if (dt->end != NULL)
1846 gfc_error ("REC tag at %L is incompatible with END tag",
1851 if (dt->format_label == &format_asterisk)
1854 ("END tag at %L is incompatible with list directed format (*)",
1859 if (dt->namelist != NULL)
1861 gfc_error ("REC tag at %L is incompatible with namelist",
1867 if (dt->advance != NULL && dt->format_label == &format_asterisk)
1869 gfc_error ("ADVANCE tag at %L is incompatible with list directed "
1870 "format (*)", &dt->advance->where);
1874 if (dt->eor != 0 && dt->advance == NULL)
1876 gfc_error ("EOR tag at %L requires an ADVANCE tag", &dt->eor_where);
1880 if (dt->size != NULL && dt->advance == NULL)
1882 gfc_error ("SIZE tag at %L requires an ADVANCE tag", &dt->size->where);
1886 /* TODO: Make sure the ADVANCE tag is 'yes' or 'no' if it is a string
1889 if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
1892 if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
1895 if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
1898 /* Check the format label actually exists. */
1899 if (dt->format_label && dt->format_label != &format_asterisk
1900 && dt->format_label->defined == ST_LABEL_UNKNOWN)
1902 gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
1903 &dt->format_label->where);
1910 /* Given an io_kind, return its name. */
1913 io_kind_name (io_kind k)
1932 gfc_internal_error ("io_kind_name(): bad I/O-kind");
1939 /* Match an IO iteration statement of the form:
1941 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
1943 which is equivalent to a single IO element. This function is
1944 mutually recursive with match_io_element(). */
1946 static match match_io_element (io_kind k, gfc_code **);
1949 match_io_iterator (io_kind k, gfc_code ** result)
1951 gfc_code *head, *tail, *new;
1959 old_loc = gfc_current_locus;
1961 if (gfc_match_char ('(') != MATCH_YES)
1964 m = match_io_element (k, &head);
1967 if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
1973 /* Can't be anything but an IO iterator. Build a list. */
1974 iter = gfc_get_iterator ();
1978 m = gfc_match_iterator (iter, 0);
1979 if (m == MATCH_ERROR)
1983 gfc_check_do_variable (iter->var->symtree);
1987 m = match_io_element (k, &new);
1988 if (m == MATCH_ERROR)
1997 tail = gfc_append_code (tail, new);
1999 if (gfc_match_char (',') != MATCH_YES)
2008 if (gfc_match_char (')') != MATCH_YES)
2011 new = gfc_get_code ();
2013 new->ext.iterator = iter;
2015 new->block = gfc_get_code ();
2016 new->block->op = EXEC_DO;
2017 new->block->next = head;
2023 gfc_error ("Syntax error in I/O iterator at %C");
2027 gfc_free_iterator (iter, 1);
2028 gfc_free_statements (head);
2029 gfc_current_locus = old_loc;
2034 /* Match a single element of an IO list, which is either a single
2035 expression or an IO Iterator. */
2038 match_io_element (io_kind k, gfc_code ** cpp)
2046 m = match_io_iterator (k, cpp);
2052 m = gfc_match_variable (&expr, 0);
2054 gfc_error ("Expected variable in READ statement at %C");
2058 m = gfc_match_expr (&expr);
2060 gfc_error ("Expected expression in %s statement at %C",
2068 if (expr->symtree->n.sym->attr.intent == INTENT_IN)
2071 ("Variable '%s' in input list at %C cannot be INTENT(IN)",
2072 expr->symtree->n.sym->name);
2077 && gfc_impure_variable (expr->symtree->n.sym)
2078 && current_dt->io_unit->ts.type == BT_CHARACTER)
2080 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2081 expr->symtree->n.sym->name);
2085 if (gfc_check_do_variable (expr->symtree))
2091 if (current_dt->io_unit->ts.type == BT_CHARACTER
2093 && current_dt->io_unit->expr_type == EXPR_VARIABLE
2094 && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
2097 ("Cannot write to internal file unit '%s' at %C inside a "
2098 "PURE procedure", current_dt->io_unit->symtree->n.sym->name);
2110 gfc_free_expr (expr);
2114 cp = gfc_get_code ();
2115 cp->op = EXEC_TRANSFER;
2123 /* Match an I/O list, building gfc_code structures as we go. */
2126 match_io_list (io_kind k, gfc_code ** head_p)
2128 gfc_code *head, *tail, *new;
2131 *head_p = head = tail = NULL;
2132 if (gfc_match_eos () == MATCH_YES)
2137 m = match_io_element (k, &new);
2138 if (m == MATCH_ERROR)
2143 tail = gfc_append_code (tail, new);
2147 if (gfc_match_eos () == MATCH_YES)
2149 if (gfc_match_char (',') != MATCH_YES)
2157 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2160 gfc_free_statements (head);
2165 /* Attach the data transfer end node. */
2168 terminate_io (gfc_code * io_code)
2172 if (io_code == NULL)
2173 io_code = new_st.block;
2175 c = gfc_get_code ();
2176 c->op = EXEC_DT_END;
2178 /* Point to structure that is already there */
2179 c->ext.dt = new_st.ext.dt;
2180 gfc_append_code (io_code, c);
2184 /* Match a READ, WRITE or PRINT statement. */
2187 match_io (io_kind k)
2189 char name[GFC_MAX_SYMBOL_LEN + 1];
2199 current_dt = dt = gfc_getmem (sizeof (gfc_dt));
2200 if (gfc_match_char ('(') == MATCH_NO)
2202 where = gfc_current_locus;
2205 else if (k == M_PRINT)
2207 /* Treat the non-standard case of PRINT namelist. */
2208 if ((gfc_current_form == FORM_FIXED || gfc_peek_char () == ' ')
2209 && gfc_match_name (name) == MATCH_YES)
2211 gfc_find_symbol (name, NULL, 1, &sym);
2212 if (sym && sym->attr.flavor == FL_NAMELIST)
2214 if (gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
2215 "%C is an extension") == FAILURE)
2220 if (gfc_match_eos () == MATCH_NO)
2222 gfc_error ("Namelist followed by I/O list at %C");
2227 dt->io_unit = default_unit (k);
2232 gfc_current_locus = where;
2236 if (gfc_current_form == FORM_FREE)
2238 c = gfc_peek_char();
2239 if (c != ' ' && c != '*' && c != '\'' && c != '"')
2246 m = match_dt_format (dt);
2247 if (m == MATCH_ERROR)
2253 dt->io_unit = default_unit (k);
2257 /* Match a control list */
2258 if (match_dt_element (k, dt) == MATCH_YES)
2260 if (match_dt_unit (k, dt) != MATCH_YES)
2263 if (gfc_match_char (')') == MATCH_YES)
2265 if (gfc_match_char (',') != MATCH_YES)
2268 m = match_dt_element (k, dt);
2271 if (m == MATCH_ERROR)
2274 m = match_dt_format (dt);
2277 if (m == MATCH_ERROR)
2280 where = gfc_current_locus;
2282 m = gfc_match_name (name);
2285 gfc_find_symbol (name, NULL, 1, &sym);
2286 if (sym && sym->attr.flavor == FL_NAMELIST)
2289 if (k == M_READ && check_namelist (sym))
2298 gfc_current_locus = where;
2300 goto loop; /* No matches, try regular elements */
2303 if (gfc_match_char (')') == MATCH_YES)
2305 if (gfc_match_char (',') != MATCH_YES)
2311 m = match_dt_element (k, dt);
2314 if (m == MATCH_ERROR)
2317 if (gfc_match_char (')') == MATCH_YES)
2319 if (gfc_match_char (',') != MATCH_YES)
2324 /* Optional leading comma (non-standard). */
2326 && gfc_match_char (',') == MATCH_YES
2328 && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before output "
2329 "item list at %C is an extension") == FAILURE)
2333 if (gfc_match_eos () != MATCH_YES)
2335 if (comma_flag && gfc_match_char (',') != MATCH_YES)
2337 gfc_error ("Expected comma in I/O list at %C");
2342 m = match_io_list (k, &io_code);
2343 if (m == MATCH_ERROR)
2349 /* A full IO statement has been matched. */
2350 if (dt->io_unit->expr_type == EXPR_VARIABLE
2352 && dt->io_unit->ts.type == BT_CHARACTER
2353 && dt->io_unit->symtree->n.sym->attr.intent == INTENT_IN)
2355 gfc_error ("Internal file '%s' at %L is INTENT(IN)",
2356 dt->io_unit->symtree->n.sym->name, &dt->io_unit->where);
2361 expr = dt->format_expr;
2363 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
2364 check_format_string (expr);
2367 && (k == M_READ || k == M_WRITE)
2368 && dt->io_unit->ts.type != BT_CHARACTER)
2371 ("io-unit in %s statement at %C must be an internal file in a "
2372 "PURE procedure", io_kind_name (k));
2377 new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
2379 new_st.block = gfc_get_code ();
2380 new_st.block->op = new_st.op;
2381 new_st.block->next = io_code;
2383 terminate_io (io_code);
2388 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2398 gfc_match_read (void)
2400 return match_io (M_READ);
2404 gfc_match_write (void)
2406 return match_io (M_WRITE);
2410 gfc_match_print (void)
2414 m = match_io (M_PRINT);
2418 if (gfc_pure (NULL))
2420 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
2428 /* Free a gfc_inquire structure. */
2431 gfc_free_inquire (gfc_inquire * inquire)
2434 if (inquire == NULL)
2437 gfc_free_expr (inquire->unit);
2438 gfc_free_expr (inquire->file);
2439 gfc_free_expr (inquire->iomsg);
2440 gfc_free_expr (inquire->iostat);
2441 gfc_free_expr (inquire->exist);
2442 gfc_free_expr (inquire->opened);
2443 gfc_free_expr (inquire->number);
2444 gfc_free_expr (inquire->named);
2445 gfc_free_expr (inquire->name);
2446 gfc_free_expr (inquire->access);
2447 gfc_free_expr (inquire->sequential);
2448 gfc_free_expr (inquire->direct);
2449 gfc_free_expr (inquire->form);
2450 gfc_free_expr (inquire->formatted);
2451 gfc_free_expr (inquire->unformatted);
2452 gfc_free_expr (inquire->recl);
2453 gfc_free_expr (inquire->nextrec);
2454 gfc_free_expr (inquire->blank);
2455 gfc_free_expr (inquire->position);
2456 gfc_free_expr (inquire->action);
2457 gfc_free_expr (inquire->read);
2458 gfc_free_expr (inquire->write);
2459 gfc_free_expr (inquire->readwrite);
2460 gfc_free_expr (inquire->delim);
2461 gfc_free_expr (inquire->pad);
2462 gfc_free_expr (inquire->iolength);
2463 gfc_free_expr (inquire->convert);
2469 /* Match an element of an INQUIRE statement. */
2471 #define RETM if (m != MATCH_NO) return m;
2474 match_inquire_element (gfc_inquire * inquire)
2478 m = match_etag (&tag_unit, &inquire->unit);
2479 RETM m = match_etag (&tag_file, &inquire->file);
2480 RETM m = match_ltag (&tag_err, &inquire->err);
2481 RETM m = match_out_tag (&tag_iomsg, &inquire->iomsg);
2482 RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
2483 RETM m = match_vtag (&tag_exist, &inquire->exist);
2484 RETM m = match_vtag (&tag_opened, &inquire->opened);
2485 RETM m = match_vtag (&tag_named, &inquire->named);
2486 RETM m = match_vtag (&tag_name, &inquire->name);
2487 RETM m = match_out_tag (&tag_number, &inquire->number);
2488 RETM m = match_vtag (&tag_s_access, &inquire->access);
2489 RETM m = match_vtag (&tag_sequential, &inquire->sequential);
2490 RETM m = match_vtag (&tag_direct, &inquire->direct);
2491 RETM m = match_vtag (&tag_s_form, &inquire->form);
2492 RETM m = match_vtag (&tag_formatted, &inquire->formatted);
2493 RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
2494 RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
2495 RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
2496 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
2497 RETM m = match_vtag (&tag_s_position, &inquire->position);
2498 RETM m = match_vtag (&tag_s_action, &inquire->action);
2499 RETM m = match_vtag (&tag_read, &inquire->read);
2500 RETM m = match_vtag (&tag_write, &inquire->write);
2501 RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
2502 RETM m = match_vtag (&tag_s_delim, &inquire->delim);
2503 RETM m = match_vtag (&tag_s_pad, &inquire->pad);
2504 RETM m = match_vtag (&tag_iolength, &inquire->iolength);
2505 RETM m = match_vtag (&tag_convert, &inquire->convert);
2506 RETM return MATCH_NO;
2513 gfc_match_inquire (void)
2515 gfc_inquire *inquire;
2520 m = gfc_match_char ('(');
2524 inquire = gfc_getmem (sizeof (gfc_inquire));
2526 loc = gfc_current_locus;
2528 m = match_inquire_element (inquire);
2529 if (m == MATCH_ERROR)
2533 m = gfc_match_expr (&inquire->unit);
2534 if (m == MATCH_ERROR)
2540 /* See if we have the IOLENGTH form of the inquire statement. */
2541 if (inquire->iolength != NULL)
2543 if (gfc_match_char (')') != MATCH_YES)
2546 m = match_io_list (M_INQUIRE, &code);
2547 if (m == MATCH_ERROR)
2552 new_st.op = EXEC_IOLENGTH;
2553 new_st.expr = inquire->iolength;
2554 new_st.ext.inquire = inquire;
2556 if (gfc_pure (NULL))
2558 gfc_free_statements (code);
2559 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
2563 new_st.block = gfc_get_code ();
2564 new_st.block->op = EXEC_IOLENGTH;
2565 terminate_io (code);
2566 new_st.block->next = code;
2570 /* At this point, we have the non-IOLENGTH inquire statement. */
2573 if (gfc_match_char (')') == MATCH_YES)
2575 if (gfc_match_char (',') != MATCH_YES)
2578 m = match_inquire_element (inquire);
2579 if (m == MATCH_ERROR)
2584 if (inquire->iolength != NULL)
2586 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
2591 if (gfc_match_eos () != MATCH_YES)
2594 if (inquire->unit != NULL && inquire->file != NULL)
2596 gfc_error ("INQUIRE statement at %L cannot contain both FILE and"
2597 " UNIT specifiers", &loc);
2601 if (inquire->unit == NULL && inquire->file == NULL)
2603 gfc_error ("INQUIRE statement at %L requires either FILE or"
2604 " UNIT specifier", &loc);
2608 if (gfc_pure (NULL))
2610 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
2614 new_st.op = EXEC_INQUIRE;
2615 new_st.ext.inquire = inquire;
2619 gfc_syntax_error (ST_INQUIRE);
2622 gfc_free_inquire (inquire);
2627 /* Resolve everything in a gfc_inquire structure. */
2630 gfc_resolve_inquire (gfc_inquire * inquire)
2633 RESOLVE_TAG (&tag_unit, inquire->unit);
2634 RESOLVE_TAG (&tag_file, inquire->file);
2635 RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
2636 RESOLVE_TAG (&tag_iostat, inquire->iostat);
2637 RESOLVE_TAG (&tag_exist, inquire->exist);
2638 RESOLVE_TAG (&tag_opened, inquire->opened);
2639 RESOLVE_TAG (&tag_number, inquire->number);
2640 RESOLVE_TAG (&tag_named, inquire->named);
2641 RESOLVE_TAG (&tag_name, inquire->name);
2642 RESOLVE_TAG (&tag_s_access, inquire->access);
2643 RESOLVE_TAG (&tag_sequential, inquire->sequential);
2644 RESOLVE_TAG (&tag_direct, inquire->direct);
2645 RESOLVE_TAG (&tag_s_form, inquire->form);
2646 RESOLVE_TAG (&tag_formatted, inquire->formatted);
2647 RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
2648 RESOLVE_TAG (&tag_s_recl, inquire->recl);
2649 RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
2650 RESOLVE_TAG (&tag_s_blank, inquire->blank);
2651 RESOLVE_TAG (&tag_s_position, inquire->position);
2652 RESOLVE_TAG (&tag_s_action, inquire->action);
2653 RESOLVE_TAG (&tag_read, inquire->read);
2654 RESOLVE_TAG (&tag_write, inquire->write);
2655 RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
2656 RESOLVE_TAG (&tag_s_delim, inquire->delim);
2657 RESOLVE_TAG (&tag_s_pad, inquire->pad);
2658 RESOLVE_TAG (&tag_iolength, inquire->iolength);
2659 RESOLVE_TAG (&tag_convert, inquire->convert);
2661 if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)