1 /* Deal with I/O statements & related stuff.
2 Copyright (C) 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
32 gfc_st_label format_asterisk =
33 { -1, ST_LABEL_FORMAT, ST_LABEL_FORMAT, NULL, 0,
34 {NULL, NULL}, NULL, NULL};
38 const char *name, *spec;
44 tag_file = { "FILE", " file = %e", BT_CHARACTER },
45 tag_status = { "STATUS", " status = %e", BT_CHARACTER},
46 tag_e_access = {"ACCESS", " access = %e", BT_CHARACTER},
47 tag_e_form = {"FORM", " form = %e", BT_CHARACTER},
48 tag_e_recl = {"RECL", " recl = %e", BT_INTEGER},
49 tag_e_blank = {"BLANK", " blank = %e", BT_CHARACTER},
50 tag_e_position = {"POSITION", " position = %e", BT_CHARACTER},
51 tag_e_action = {"ACTION", " action = %e", BT_CHARACTER},
52 tag_e_delim = {"DELIM", " delim = %e", BT_CHARACTER},
53 tag_e_pad = {"PAD", " pad = %e", BT_CHARACTER},
54 tag_unit = {"UNIT", " unit = %e", BT_INTEGER},
55 tag_advance = {"ADVANCE", " advance = %e", BT_CHARACTER},
56 tag_rec = {"REC", " rec = %e", BT_INTEGER},
57 tag_format = {"FORMAT", NULL, BT_CHARACTER},
58 tag_iostat = {"IOSTAT", " iostat = %v", BT_INTEGER},
59 tag_size = {"SIZE", " size = %v", BT_INTEGER},
60 tag_exist = {"EXIST", " exist = %v", BT_LOGICAL},
61 tag_opened = {"OPENED", " opened = %v", BT_LOGICAL},
62 tag_named = {"NAMED", " named = %v", BT_LOGICAL},
63 tag_name = {"NAME", " name = %v", BT_CHARACTER},
64 tag_number = {"NUMBER", " number = %v", BT_INTEGER},
65 tag_s_access = {"ACCESS", " access = %v", BT_CHARACTER},
66 tag_sequential = {"SEQUENTIAL", " sequential = %v", BT_CHARACTER},
67 tag_direct = {"DIRECT", " direct = %v", BT_CHARACTER},
68 tag_s_form = {"FORM", " form = %v", BT_CHARACTER},
69 tag_formatted = {"FORMATTED", " formatted = %v", BT_CHARACTER},
70 tag_unformatted = {"UNFORMATTED", " unformatted = %v", BT_CHARACTER},
71 tag_s_recl = {"RECL", " recl = %v", BT_INTEGER},
72 tag_nextrec = {"NEXTREC", " nextrec = %v", BT_INTEGER},
73 tag_s_blank = {"BLANK", " blank = %v", BT_CHARACTER},
74 tag_s_position = {"POSITION", " position = %v", BT_CHARACTER},
75 tag_s_action = {"ACTION", " action = %v", BT_CHARACTER},
76 tag_read = {"READ", " read = %v", BT_CHARACTER},
77 tag_write = {"WRITE", " write = %v", BT_CHARACTER},
78 tag_readwrite = {"READWRITE", " readwrite = %v", BT_CHARACTER},
79 tag_s_delim = {"DELIM", " delim = %v", BT_CHARACTER},
80 tag_s_pad = {"PAD", " pad = %v", BT_CHARACTER},
81 tag_iolength = {"IOLENGTH", " iolength = %v", BT_INTEGER},
82 tag_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';
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. */
459 /* Signed integer can only precede a P format. */
463 error = "Expected P edit descriptor";
470 /* P requires a prior number. */
471 error = "P descriptor requires leading scale factor";
475 /* X requires a prior number if we're being pedantic. */
476 if (gfc_notify_std (GFC_STD_GNU, "Extension: X descriptor "
477 "requires leading space count at %C")
487 goto extension_optional_comma;
495 if (t != FMT_RPAREN || level > 0)
497 error = "$ must the last specifier";
518 error = unexpected_end;
522 error = unexpected_element;
527 /* In this state, t must currently be a data descriptor.
528 Deal with things that can/must follow the descriptor. */
542 error = "Repeat count cannot follow P descriptor";
557 error = posint_required;
573 error = posint_required;
580 error = period_required;
585 if (u != FMT_ZERO && u != FMT_POSINT)
587 error = nonneg_required;
594 /* Look for optional exponent. */
605 error = "Positive exponent width required";
614 if (t != FMT_ZERO && t != FMT_POSINT)
616 error = nonneg_required;
623 error = period_required;
628 if (t != FMT_ZERO && t != FMT_POSINT)
630 error = nonneg_required;
637 if(mode == MODE_STRING)
639 format_string += value;
640 format_length -= value;
654 if (t != FMT_ZERO && t != FMT_POSINT)
656 error = nonneg_required;
668 if (t != FMT_ZERO && t != FMT_POSINT)
670 error = nonneg_required;
678 error = unexpected_element;
683 /* Between a descriptor and what comes next. */
702 error = unexpected_end;
706 error = "Missing comma";
711 /* Optional comma is a weird between state where we've just finished
712 reading a colon, slash or P descriptor. */
726 /* Assume that we have another format item. */
733 extension_optional_comma:
734 /* As a GNU extension, permit a missing comma after a string literal. */
752 error = unexpected_end;
756 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
766 /* Something went wrong. If the format we're checking is a string,
767 generate a warning, since the program is correct. If the format
768 is in a FORMAT statement, this messes up parsing, which is an
770 if (mode != MODE_STRING)
771 gfc_error ("%s in format string at %C", error);
774 gfc_warning ("%s in format string at %C", error);
776 /* TODO: More elaborate measures are needed to show where a problem
777 is within a format string that has been calculated. */
787 /* Given an expression node that is a constant string, see if it looks
788 like a format string. */
791 check_format_string (gfc_expr * e)
795 format_string = e->value.character.string;
800 /************ Fortran 95 I/O statement matchers *************/
802 /* Match a FORMAT statement. This amounts to actually parsing the
803 format descriptors in order to correctly locate the end of the
807 gfc_match_format (void)
812 if (gfc_statement_label == NULL)
814 gfc_error ("Missing format label at %C");
817 gfc_gobble_whitespace ();
822 start = gfc_current_locus;
824 if (check_format () == FAILURE)
827 if (gfc_match_eos () != MATCH_YES)
829 gfc_syntax_error (ST_FORMAT);
833 /* The label doesn't get created until after the statement is done
834 being matched, so we have to leave the string for later. */
836 gfc_current_locus = start; /* Back to the beginning */
839 new_st.op = EXEC_NOP;
842 e->expr_type = EXPR_CONSTANT;
843 e->ts.type = BT_CHARACTER;
844 e->ts.kind = gfc_default_character_kind();
846 e->value.character.string = format_string = gfc_getmem(format_length+1);
847 e->value.character.length = format_length;
848 gfc_statement_label->format = e;
851 check_format (); /* Guaranteed to succeed */
852 gfc_match_eos (); /* Guaranteed to succeed */
858 /* Match an expression I/O tag of some sort. */
861 match_etag (const io_tag * tag, gfc_expr ** v)
866 m = gfc_match (tag->spec, &result);
872 gfc_error ("Duplicate %s specification at %C", tag->name);
873 gfc_free_expr (result);
882 /* Match a variable I/O tag of some sort. */
885 match_vtag (const io_tag * tag, gfc_expr ** v)
890 m = gfc_match (tag->spec, &result);
896 gfc_error ("Duplicate %s specification at %C", tag->name);
897 gfc_free_expr (result);
901 if (result->symtree->n.sym->attr.intent == INTENT_IN)
903 gfc_error ("Variable tag cannot be INTENT(IN) at %C");
904 gfc_free_expr (result);
908 if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
910 gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
911 gfc_free_expr (result);
920 /* Match a label I/O tag. */
923 match_ltag (const io_tag * tag, gfc_st_label ** label)
929 m = gfc_match (tag->spec, label);
930 if (m == MATCH_YES && old != 0)
932 gfc_error ("Duplicate %s label specification at %C", tag->name);
940 /* Do expression resolution and type-checking on an expression tag. */
943 resolve_tag (const io_tag * tag, gfc_expr * e)
949 if (gfc_resolve_expr (e) == FAILURE)
952 if (e->ts.type != tag->type)
954 /* Format label can be integer varibale. */
955 if (tag != &tag_format)
957 gfc_error ("%s tag at %L must be of type %s", tag->name, &e->where,
958 gfc_basic_typename (tag->type));
963 if (tag == &tag_format)
965 if (e->rank != 1 && e->rank != 0)
967 gfc_error ("FORMAT tag at %L cannot be array of strings",
976 gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
985 /* Match a single tag of an OPEN statement. */
988 match_open_element (gfc_open * open)
992 m = match_etag (&tag_unit, &open->unit);
995 m = match_vtag (&tag_iostat, &open->iostat);
998 m = match_etag (&tag_file, &open->file);
1001 m = match_etag (&tag_status, &open->status);
1004 m = match_etag (&tag_e_access, &open->access);
1007 m = match_etag (&tag_e_form, &open->form);
1010 m = match_etag (&tag_e_recl, &open->recl);
1013 m = match_etag (&tag_e_blank, &open->blank);
1016 m = match_etag (&tag_e_position, &open->position);
1019 m = match_etag (&tag_e_action, &open->action);
1022 m = match_etag (&tag_e_delim, &open->delim);
1025 m = match_etag (&tag_e_pad, &open->pad);
1028 m = match_ltag (&tag_err, &open->err);
1036 /* Free the gfc_open structure and all the expressions it contains. */
1039 gfc_free_open (gfc_open * open)
1045 gfc_free_expr (open->unit);
1046 gfc_free_expr (open->iostat);
1047 gfc_free_expr (open->file);
1048 gfc_free_expr (open->status);
1049 gfc_free_expr (open->access);
1050 gfc_free_expr (open->form);
1051 gfc_free_expr (open->recl);
1052 gfc_free_expr (open->blank);
1053 gfc_free_expr (open->position);
1054 gfc_free_expr (open->action);
1055 gfc_free_expr (open->delim);
1056 gfc_free_expr (open->pad);
1062 /* Resolve everything in a gfc_open structure. */
1065 gfc_resolve_open (gfc_open * open)
1068 RESOLVE_TAG (&tag_unit, open->unit);
1069 RESOLVE_TAG (&tag_iostat, open->iostat);
1070 RESOLVE_TAG (&tag_file, open->file);
1071 RESOLVE_TAG (&tag_status, open->status);
1072 RESOLVE_TAG (&tag_e_form, open->form);
1073 RESOLVE_TAG (&tag_e_recl, open->recl);
1075 RESOLVE_TAG (&tag_e_blank, open->blank);
1076 RESOLVE_TAG (&tag_e_position, open->position);
1077 RESOLVE_TAG (&tag_e_action, open->action);
1078 RESOLVE_TAG (&tag_e_delim, open->delim);
1079 RESOLVE_TAG (&tag_e_pad, open->pad);
1081 if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1088 /* Match an OPEN statmement. */
1091 gfc_match_open (void)
1096 m = gfc_match_char ('(');
1100 open = gfc_getmem (sizeof (gfc_open));
1102 m = match_open_element (open);
1104 if (m == MATCH_ERROR)
1108 m = gfc_match_expr (&open->unit);
1111 if (m == MATCH_ERROR)
1117 if (gfc_match_char (')') == MATCH_YES)
1119 if (gfc_match_char (',') != MATCH_YES)
1122 m = match_open_element (open);
1123 if (m == MATCH_ERROR)
1129 if (gfc_match_eos () == MATCH_NO)
1132 if (gfc_pure (NULL))
1134 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1138 new_st.op = EXEC_OPEN;
1139 new_st.ext.open = open;
1143 gfc_syntax_error (ST_OPEN);
1146 gfc_free_open (open);
1151 /* Free a gfc_close structure an all its expressions. */
1154 gfc_free_close (gfc_close * close)
1160 gfc_free_expr (close->unit);
1161 gfc_free_expr (close->iostat);
1162 gfc_free_expr (close->status);
1168 /* Match elements of a CLOSE statment. */
1171 match_close_element (gfc_close * close)
1175 m = match_etag (&tag_unit, &close->unit);
1178 m = match_etag (&tag_status, &close->status);
1181 m = match_vtag (&tag_iostat, &close->iostat);
1184 m = match_ltag (&tag_err, &close->err);
1192 /* Match a CLOSE statement. */
1195 gfc_match_close (void)
1200 m = gfc_match_char ('(');
1204 close = gfc_getmem (sizeof (gfc_close));
1206 m = match_close_element (close);
1208 if (m == MATCH_ERROR)
1212 m = gfc_match_expr (&close->unit);
1215 if (m == MATCH_ERROR)
1221 if (gfc_match_char (')') == MATCH_YES)
1223 if (gfc_match_char (',') != MATCH_YES)
1226 m = match_close_element (close);
1227 if (m == MATCH_ERROR)
1233 if (gfc_match_eos () == MATCH_NO)
1236 if (gfc_pure (NULL))
1238 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1242 new_st.op = EXEC_CLOSE;
1243 new_st.ext.close = close;
1247 gfc_syntax_error (ST_CLOSE);
1250 gfc_free_close (close);
1255 /* Resolve everything in a gfc_close structure. */
1258 gfc_resolve_close (gfc_close * close)
1261 RESOLVE_TAG (&tag_unit, close->unit);
1262 RESOLVE_TAG (&tag_iostat, close->iostat);
1263 RESOLVE_TAG (&tag_status, close->status);
1265 if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
1272 /* Free a gfc_filepos structure. */
1275 gfc_free_filepos (gfc_filepos * fp)
1278 gfc_free_expr (fp->unit);
1279 gfc_free_expr (fp->iostat);
1284 /* Match elements of a REWIND, BACKSPACE or ENDFILE statement. */
1287 match_file_element (gfc_filepos * fp)
1291 m = match_etag (&tag_unit, &fp->unit);
1294 m = match_vtag (&tag_iostat, &fp->iostat);
1297 m = match_ltag (&tag_err, &fp->err);
1305 /* Match the second half of the file-positioning statements, REWIND,
1306 BACKSPACE or ENDFILE. */
1309 match_filepos (gfc_statement st, gfc_exec_op op)
1314 fp = gfc_getmem (sizeof (gfc_filepos));
1316 if (gfc_match_char ('(') == MATCH_NO)
1318 m = gfc_match_expr (&fp->unit);
1319 if (m == MATCH_ERROR)
1327 m = match_file_element (fp);
1328 if (m == MATCH_ERROR)
1332 m = gfc_match_expr (&fp->unit);
1333 if (m == MATCH_ERROR)
1341 if (gfc_match_char (')') == MATCH_YES)
1343 if (gfc_match_char (',') != MATCH_YES)
1346 m = match_file_element (fp);
1347 if (m == MATCH_ERROR)
1354 if (gfc_match_eos () != MATCH_YES)
1357 if (gfc_pure (NULL))
1359 gfc_error ("%s statement not allowed in PURE procedure at %C",
1360 gfc_ascii_statement (st));
1366 new_st.ext.filepos = fp;
1370 gfc_syntax_error (st);
1373 gfc_free_filepos (fp);
1379 gfc_resolve_filepos (gfc_filepos * fp)
1382 RESOLVE_TAG (&tag_unit, fp->unit);
1383 if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
1390 /* Match the file positioning statements: ENDFILE, BACKSPACE or
1394 gfc_match_endfile (void)
1397 return match_filepos (ST_END_FILE, EXEC_ENDFILE);
1401 gfc_match_backspace (void)
1404 return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
1408 gfc_match_rewind (void)
1411 return match_filepos (ST_REWIND, EXEC_REWIND);
1415 /******************** Data Transfer Statments *********************/
1418 { M_READ, M_WRITE, M_PRINT, M_INQUIRE }
1422 /* Return a default unit number. */
1425 default_unit (io_kind k)
1434 return gfc_int_expr (unit);
1438 /* Match a unit specification for a data transfer statement. */
1441 match_dt_unit (io_kind k, gfc_dt * dt)
1445 if (gfc_match_char ('*') == MATCH_YES)
1447 if (dt->io_unit != NULL)
1450 dt->io_unit = default_unit (k);
1454 if (gfc_match_expr (&e) == MATCH_YES)
1456 if (dt->io_unit != NULL)
1469 gfc_error ("Duplicate UNIT specification at %C");
1474 /* Match a format specification. */
1477 match_dt_format (gfc_dt * dt)
1481 gfc_st_label *label;
1483 where = gfc_current_locus;
1485 if (gfc_match_char ('*') == MATCH_YES)
1487 if (dt->format_expr != NULL || dt->format_label != NULL)
1490 dt->format_label = &format_asterisk;
1494 if (gfc_match_st_label (&label, 0) == MATCH_YES)
1496 if (dt->format_expr != NULL || dt->format_label != NULL)
1498 gfc_free_st_label (label);
1502 if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
1505 dt->format_label = label;
1509 if (gfc_match_expr (&e) == MATCH_YES)
1511 if (dt->format_expr != NULL || dt->format_label != NULL)
1516 if (e->ts.type == BT_INTEGER && e->rank == 0)
1517 e->symtree->n.sym->attr.assign = 1;
1519 dt->format_expr = e;
1523 gfc_current_locus = where; /* The only case where we have to restore */
1528 gfc_error ("Duplicate format specification at %C");
1533 /* Traverse a namelist that is part of a READ statement to make sure
1534 that none of the variables in the namelist are INTENT(IN). Returns
1535 nonzero if we find such a variable. */
1538 check_namelist (gfc_symbol * sym)
1542 for (p = sym->namelist; p; p = p->next)
1543 if (p->sym->attr.intent == INTENT_IN)
1545 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
1546 p->sym->name, sym->name);
1554 /* Match a single data transfer element. */
1557 match_dt_element (io_kind k, gfc_dt * dt)
1559 char name[GFC_MAX_SYMBOL_LEN + 1];
1563 if (gfc_match (" unit =") == MATCH_YES)
1565 m = match_dt_unit (k, dt);
1570 if (gfc_match (" fmt =") == MATCH_YES)
1572 m = match_dt_format (dt);
1577 if (gfc_match (" nml = %n", name) == MATCH_YES)
1579 if (dt->namelist != NULL)
1581 gfc_error ("Duplicate NML specification at %C");
1585 if (gfc_find_symbol (name, NULL, 1, &sym))
1588 if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
1590 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
1591 sym != NULL ? sym->name : name);
1596 if (k == M_READ && check_namelist (sym))
1602 m = match_etag (&tag_rec, &dt->rec);
1605 m = match_vtag (&tag_iostat, &dt->iostat);
1608 m = match_ltag (&tag_err, &dt->err);
1611 m = match_etag (&tag_advance, &dt->advance);
1614 m = match_vtag (&tag_size, &dt->size);
1618 m = match_ltag (&tag_end, &dt->end);
1620 dt->end_where = gfc_current_locus;
1624 m = match_ltag (&tag_eor, &dt->eor);
1626 dt->eor_where = gfc_current_locus;
1634 /* Free a data transfer structure and everything below it. */
1637 gfc_free_dt (gfc_dt * dt)
1643 gfc_free_expr (dt->io_unit);
1644 gfc_free_expr (dt->format_expr);
1645 gfc_free_expr (dt->rec);
1646 gfc_free_expr (dt->advance);
1647 gfc_free_expr (dt->iostat);
1648 gfc_free_expr (dt->size);
1654 /* Resolve everything in a gfc_dt structure. */
1657 gfc_resolve_dt (gfc_dt * dt)
1661 RESOLVE_TAG (&tag_format, dt->format_expr);
1662 RESOLVE_TAG (&tag_rec, dt->rec);
1663 RESOLVE_TAG (&tag_advance, dt->advance);
1664 RESOLVE_TAG (&tag_iostat, dt->iostat);
1665 RESOLVE_TAG (&tag_size, dt->size);
1668 if (gfc_resolve_expr (e) == SUCCESS
1669 && (e->ts.type != BT_INTEGER
1670 && (e->ts.type != BT_CHARACTER
1671 || e->expr_type != EXPR_VARIABLE)))
1674 ("UNIT specification at %L must be an INTEGER expression or a "
1675 "CHARACTER variable", &e->where);
1679 /* Sanity checks on data transfer statements. */
1680 if (e->ts.type == BT_CHARACTER)
1682 if (dt->rec != NULL)
1684 gfc_error ("REC tag at %L is incompatible with internal file",
1689 if (dt->namelist != NULL)
1691 gfc_error ("Internal file at %L is incompatible with namelist",
1692 &dt->io_unit->where);
1696 if (dt->advance != NULL)
1698 gfc_error ("ADVANCE tag at %L is incompatible with internal file",
1699 &dt->advance->where);
1704 if (dt->rec != NULL)
1706 if (dt->end != NULL)
1708 gfc_error ("REC tag at %L is incompatible with END tag",
1713 if (dt->format_label == &format_asterisk)
1716 ("END tag at %L is incompatible with list directed format (*)",
1721 if (dt->namelist != NULL)
1723 gfc_error ("REC tag at %L is incompatible with namelist",
1729 if (dt->advance != NULL && dt->format_label == &format_asterisk)
1731 gfc_error ("ADVANCE tag at %L is incompatible with list directed "
1732 "format (*)", &dt->advance->where);
1736 if (dt->eor != 0 && dt->advance == NULL)
1738 gfc_error ("EOR tag at %L requires an ADVANCE tag", &dt->eor_where);
1742 if (dt->size != NULL && dt->advance == NULL)
1744 gfc_error ("SIZE tag at %L requires an ADVANCE tag", &dt->size->where);
1748 /* TODO: Make sure the ADVANCE tag is 'yes' or 'no' if it is a string
1751 if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
1754 if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
1757 if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
1760 /* Check the format label ectually exists. */
1761 if (dt->format_label && dt->format_label != &format_asterisk
1762 && dt->format_label->defined == ST_LABEL_UNKNOWN)
1764 gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
1765 &dt->format_label->where);
1772 /* Given an io_kind, return its name. */
1775 io_kind_name (io_kind k)
1794 gfc_internal_error ("io_kind_name(): bad I/O-kind");
1801 /* Match an IO iteration statement of the form:
1803 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
1805 which is equivalent to a single IO element. This function is
1806 mutually recursive with match_io_element(). */
1808 static match match_io_element (io_kind k, gfc_code **);
1811 match_io_iterator (io_kind k, gfc_code ** result)
1813 gfc_code *head, *tail, *new;
1821 old_loc = gfc_current_locus;
1823 if (gfc_match_char ('(') != MATCH_YES)
1826 m = match_io_element (k, &head);
1829 if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
1835 /* Can't be anything but an IO iterator. Build a list. */
1836 iter = gfc_get_iterator ();
1840 m = gfc_match_iterator (iter, 0);
1841 if (m == MATCH_ERROR)
1846 m = match_io_element (k, &new);
1847 if (m == MATCH_ERROR)
1856 tail = gfc_append_code (tail, new);
1858 if (gfc_match_char (',') != MATCH_YES)
1867 if (gfc_match_char (')') != MATCH_YES)
1870 new = gfc_get_code ();
1872 new->ext.iterator = iter;
1874 new->block = gfc_get_code ();
1875 new->block->op = EXEC_DO;
1876 new->block->next = head;
1882 gfc_error ("Syntax error in I/O iterator at %C");
1886 gfc_free_iterator (iter, 1);
1887 gfc_free_statements (head);
1888 gfc_current_locus = old_loc;
1893 /* Match a single element of an IO list, which is either a single
1894 expression or an IO Iterator. */
1897 match_io_element (io_kind k, gfc_code ** cpp)
1905 m = match_io_iterator (k, cpp);
1911 m = gfc_match_variable (&expr, 0);
1913 gfc_error ("Expected variable in READ statement at %C");
1917 m = gfc_match_expr (&expr);
1919 gfc_error ("Expected expression in %s statement at %C",
1927 if (expr->symtree->n.sym->attr.intent == INTENT_IN)
1930 ("Variable '%s' in input list at %C cannot be INTENT(IN)",
1931 expr->symtree->n.sym->name);
1936 && gfc_impure_variable (expr->symtree->n.sym)
1937 && current_dt->io_unit->ts.type == BT_CHARACTER)
1939 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
1940 expr->symtree->n.sym->name);
1947 if (current_dt->io_unit->ts.type == BT_CHARACTER
1949 && current_dt->io_unit->expr_type == EXPR_VARIABLE
1950 && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
1953 ("Cannot write to internal file unit '%s' at %C inside a "
1954 "PURE procedure", current_dt->io_unit->symtree->n.sym->name);
1966 gfc_free_expr (expr);
1970 cp = gfc_get_code ();
1971 cp->op = EXEC_TRANSFER;
1979 /* Match an I/O list, building gfc_code structures as we go. */
1982 match_io_list (io_kind k, gfc_code ** head_p)
1984 gfc_code *head, *tail, *new;
1987 *head_p = head = tail = NULL;
1988 if (gfc_match_eos () == MATCH_YES)
1993 m = match_io_element (k, &new);
1994 if (m == MATCH_ERROR)
1999 tail = gfc_append_code (tail, new);
2003 if (gfc_match_eos () == MATCH_YES)
2005 if (gfc_match_char (',') != MATCH_YES)
2013 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2016 gfc_free_statements (head);
2021 /* Attach the data transfer end node. */
2024 terminate_io (gfc_code * io_code)
2028 if (io_code == NULL)
2031 c = gfc_get_code ();
2032 c->op = EXEC_DT_END;
2034 /* Point to structure that is already there */
2035 c->ext.dt = new_st.ext.dt;
2036 gfc_append_code (io_code, c);
2040 /* Match a READ, WRITE or PRINT statement. */
2043 match_io (io_kind k)
2045 char name[GFC_MAX_SYMBOL_LEN + 1];
2055 current_dt = dt = gfc_getmem (sizeof (gfc_dt));
2057 if (gfc_match_char ('(') == MATCH_NO)
2062 m = match_dt_format (dt);
2063 if (m == MATCH_ERROR)
2069 dt->io_unit = default_unit (k);
2073 /* Match a control list */
2074 if (match_dt_element (k, dt) == MATCH_YES)
2076 if (match_dt_unit (k, dt) != MATCH_YES)
2079 if (gfc_match_char (')') == MATCH_YES)
2081 if (gfc_match_char (',') != MATCH_YES)
2084 m = match_dt_element (k, dt);
2087 if (m == MATCH_ERROR)
2090 m = match_dt_format (dt);
2093 if (m == MATCH_ERROR)
2096 where = gfc_current_locus;
2098 if (gfc_match_name (name) == MATCH_YES
2099 && !gfc_find_symbol (name, NULL, 1, &sym)
2100 && sym->attr.flavor == FL_NAMELIST)
2103 if (k == M_READ && check_namelist (sym))
2111 gfc_current_locus = where;
2113 goto loop; /* No matches, try regular elements */
2116 if (gfc_match_char (')') == MATCH_YES)
2118 if (gfc_match_char (',') != MATCH_YES)
2124 m = match_dt_element (k, dt);
2127 if (m == MATCH_ERROR)
2130 if (gfc_match_char (')') == MATCH_YES)
2132 if (gfc_match_char (',') != MATCH_YES)
2137 /* Optional leading comma (non-standard). */
2139 gfc_match_char (',');
2142 if (gfc_match_eos () != MATCH_YES)
2144 if (comma_flag && gfc_match_char (',') != MATCH_YES)
2146 gfc_error ("Expected comma in I/O list at %C");
2151 m = match_io_list (k, &io_code);
2152 if (m == MATCH_ERROR)
2158 /* A full IO statement has been matched. */
2159 if (dt->io_unit->expr_type == EXPR_VARIABLE
2161 && dt->io_unit->ts.type == BT_CHARACTER
2162 && dt->io_unit->symtree->n.sym->attr.intent == INTENT_IN)
2164 gfc_error ("Internal file '%s' at %L is INTENT(IN)",
2165 dt->io_unit->symtree->n.sym->name, &dt->io_unit->where);
2170 expr = dt->format_expr;
2172 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
2173 check_format_string (expr);
2176 && (k == M_READ || k == M_WRITE)
2177 && dt->io_unit->ts.type != BT_CHARACTER)
2180 ("io-unit in %s statement at %C must be an internal file in a "
2181 "PURE procedure", io_kind_name (k));
2186 new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
2188 new_st.next = io_code;
2190 terminate_io (io_code);
2195 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2205 gfc_match_read (void)
2207 return match_io (M_READ);
2211 gfc_match_write (void)
2213 return match_io (M_WRITE);
2217 gfc_match_print (void)
2221 m = match_io (M_PRINT);
2225 if (gfc_pure (NULL))
2227 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
2235 /* Free a gfc_inquire structure. */
2238 gfc_free_inquire (gfc_inquire * inquire)
2241 if (inquire == NULL)
2244 gfc_free_expr (inquire->unit);
2245 gfc_free_expr (inquire->file);
2246 gfc_free_expr (inquire->iostat);
2247 gfc_free_expr (inquire->exist);
2248 gfc_free_expr (inquire->opened);
2249 gfc_free_expr (inquire->number);
2250 gfc_free_expr (inquire->named);
2251 gfc_free_expr (inquire->name);
2252 gfc_free_expr (inquire->access);
2253 gfc_free_expr (inquire->sequential);
2254 gfc_free_expr (inquire->direct);
2255 gfc_free_expr (inquire->form);
2256 gfc_free_expr (inquire->formatted);
2257 gfc_free_expr (inquire->unformatted);
2258 gfc_free_expr (inquire->recl);
2259 gfc_free_expr (inquire->nextrec);
2260 gfc_free_expr (inquire->blank);
2261 gfc_free_expr (inquire->position);
2262 gfc_free_expr (inquire->action);
2263 gfc_free_expr (inquire->read);
2264 gfc_free_expr (inquire->write);
2265 gfc_free_expr (inquire->readwrite);
2266 gfc_free_expr (inquire->delim);
2267 gfc_free_expr (inquire->pad);
2268 gfc_free_expr (inquire->iolength);
2274 /* Match an element of an INQUIRE statement. */
2276 #define RETM if (m != MATCH_NO) return m;
2279 match_inquire_element (gfc_inquire * inquire)
2283 m = match_etag (&tag_unit, &inquire->unit);
2284 RETM m = match_etag (&tag_file, &inquire->file);
2285 RETM m = match_ltag (&tag_err, &inquire->err);
2286 RETM m = match_vtag (&tag_iostat, &inquire->iostat);
2287 RETM m = match_vtag (&tag_exist, &inquire->exist);
2288 RETM m = match_vtag (&tag_opened, &inquire->opened);
2289 RETM m = match_vtag (&tag_named, &inquire->named);
2290 RETM m = match_vtag (&tag_name, &inquire->name);
2291 RETM m = match_vtag (&tag_number, &inquire->number);
2292 RETM m = match_vtag (&tag_s_access, &inquire->access);
2293 RETM m = match_vtag (&tag_sequential, &inquire->sequential);
2294 RETM m = match_vtag (&tag_direct, &inquire->direct);
2295 RETM m = match_vtag (&tag_s_form, &inquire->form);
2296 RETM m = match_vtag (&tag_formatted, &inquire->formatted);
2297 RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
2298 RETM m = match_vtag (&tag_s_recl, &inquire->recl);
2299 RETM m = match_vtag (&tag_nextrec, &inquire->nextrec);
2300 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
2301 RETM m = match_vtag (&tag_s_position, &inquire->position);
2302 RETM m = match_vtag (&tag_s_action, &inquire->action);
2303 RETM m = match_vtag (&tag_read, &inquire->read);
2304 RETM m = match_vtag (&tag_write, &inquire->write);
2305 RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
2306 RETM m = match_vtag (&tag_s_delim, &inquire->delim);
2307 RETM m = match_vtag (&tag_s_pad, &inquire->pad);
2308 RETM m = match_vtag (&tag_iolength, &inquire->iolength);
2309 RETM return MATCH_NO;
2316 gfc_match_inquire (void)
2318 gfc_inquire *inquire;
2322 m = gfc_match_char ('(');
2326 inquire = gfc_getmem (sizeof (gfc_inquire));
2328 m = match_inquire_element (inquire);
2329 if (m == MATCH_ERROR)
2333 m = gfc_match_expr (&inquire->unit);
2334 if (m == MATCH_ERROR)
2340 /* See if we have the IOLENGTH form of the inquire statement. */
2341 if (inquire->iolength != NULL)
2343 if (gfc_match_char (')') != MATCH_YES)
2346 m = match_io_list (M_INQUIRE, &code);
2347 if (m == MATCH_ERROR)
2352 terminate_io (code);
2354 new_st.op = EXEC_IOLENGTH;
2355 new_st.expr = inquire->iolength;
2358 if (gfc_pure (NULL))
2360 gfc_free_statements (code);
2361 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
2369 /* At this point, we have the non-IOLENGTH inquire statement. */
2372 if (gfc_match_char (')') == MATCH_YES)
2374 if (gfc_match_char (',') != MATCH_YES)
2377 m = match_inquire_element (inquire);
2378 if (m == MATCH_ERROR)
2383 if (inquire->iolength != NULL)
2385 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
2390 if (gfc_match_eos () != MATCH_YES)
2393 if (gfc_pure (NULL))
2395 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
2399 new_st.op = EXEC_INQUIRE;
2400 new_st.ext.inquire = inquire;
2404 gfc_syntax_error (ST_INQUIRE);
2407 gfc_free_inquire (inquire);
2412 /* Resolve everything in a gfc_inquire structure. */
2415 gfc_resolve_inquire (gfc_inquire * inquire)
2418 RESOLVE_TAG (&tag_unit, inquire->unit);
2419 RESOLVE_TAG (&tag_file, inquire->file);
2420 RESOLVE_TAG (&tag_iostat, inquire->iostat);
2421 RESOLVE_TAG (&tag_exist, inquire->exist);
2422 RESOLVE_TAG (&tag_opened, inquire->opened);
2423 RESOLVE_TAG (&tag_number, inquire->number);
2424 RESOLVE_TAG (&tag_named, inquire->named);
2425 RESOLVE_TAG (&tag_name, inquire->name);
2426 RESOLVE_TAG (&tag_s_access, inquire->access);
2427 RESOLVE_TAG (&tag_sequential, inquire->sequential);
2428 RESOLVE_TAG (&tag_direct, inquire->direct);
2429 RESOLVE_TAG (&tag_s_form, inquire->form);
2430 RESOLVE_TAG (&tag_formatted, inquire->formatted);
2431 RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
2432 RESOLVE_TAG (&tag_s_recl, inquire->recl);
2433 RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
2434 RESOLVE_TAG (&tag_s_blank, inquire->blank);
2435 RESOLVE_TAG (&tag_s_position, inquire->position);
2436 RESOLVE_TAG (&tag_s_action, inquire->action);
2437 RESOLVE_TAG (&tag_read, inquire->read);
2438 RESOLVE_TAG (&tag_write, inquire->write);
2439 RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
2440 RESOLVE_TAG (&tag_s_delim, inquire->delim);
2441 RESOLVE_TAG (&tag_s_pad, inquire->pad);
2443 if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)