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, 0, 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 and X require a prior number. */
471 error = "P descriptor requires leading scale factor";
475 error = "X descriptor requires leading space count";
489 if (t != FMT_RPAREN || level > 0)
491 error = "$ must the last specifier";
512 error = unexpected_end;
516 error = unexpected_element;
521 /* In this state, t must currently be a data descriptor.
522 Deal with things that can/must follow the descriptor. */
536 error = "Repeat count cannot follow P descriptor";
551 error = posint_required;
567 error = posint_required;
574 error = period_required;
579 if (u != FMT_ZERO && u != FMT_POSINT)
581 error = nonneg_required;
588 /* Look for optional exponent. */
599 error = "Positive exponent width required";
608 if (t != FMT_ZERO && t != FMT_POSINT)
610 error = nonneg_required;
617 error = period_required;
622 if (t != FMT_ZERO && t != FMT_POSINT)
624 error = nonneg_required;
631 if(mode == MODE_STRING)
633 format_string += value;
634 format_length -= value;
648 if (t != FMT_ZERO && t != FMT_POSINT)
650 error = nonneg_required;
662 if (t != FMT_ZERO && t != FMT_POSINT)
664 error = nonneg_required;
672 error = unexpected_element;
677 /* Between a descriptor and what comes next. */
696 error = unexpected_end;
700 error = "Missing comma";
705 /* Optional comma is a weird between state where we've just finished
706 reading a colon, slash or P descriptor. */
720 /* Assume that we have another format item. */
728 /* Something went wrong. If the format we're checking is a string,
729 generate a warning, since the program is correct. If the format
730 is in a FORMAT statement, this messes up parsing, which is an
732 if (mode != MODE_STRING)
733 gfc_error ("%s in format string at %C", error);
736 gfc_warning ("%s in format string at %C", error);
738 /* TODO: More elaborate measures are needed to show where a problem
739 is within a format string that has been calculated. */
749 /* Given an expression node that is a constant string, see if it looks
750 like a format string. */
753 check_format_string (gfc_expr * e)
757 format_string = e->value.character.string;
762 /************ Fortran 95 I/O statement matchers *************/
764 /* Match a FORMAT statement. This amounts to actually parsing the
765 format descriptors in order to correctly locate the end of the
769 gfc_match_format (void)
774 if (gfc_statement_label == NULL)
776 gfc_error ("Missing format label at %C");
779 gfc_gobble_whitespace ();
784 start = *gfc_current_locus ();
786 if (check_format () == FAILURE)
789 if (gfc_match_eos () != MATCH_YES)
791 gfc_syntax_error (ST_FORMAT);
795 /* The label doesn't get created until after the statement is done
796 being matched, so we have to leave the string for later. */
798 gfc_set_locus (&start); /* Back to the beginning */
801 new_st.op = EXEC_NOP;
804 e->expr_type = EXPR_CONSTANT;
805 e->ts.type = BT_CHARACTER;
806 e->ts.kind = gfc_default_character_kind();
808 e->value.character.string = format_string = gfc_getmem(format_length+1);
809 e->value.character.length = format_length;
810 gfc_statement_label->format = e;
813 check_format (); /* Guaranteed to succeed */
814 gfc_match_eos (); /* Guaranteed to succeed */
820 /* Match an expression I/O tag of some sort. */
823 match_etag (const io_tag * tag, gfc_expr ** v)
828 m = gfc_match (tag->spec, &result);
834 gfc_error ("Duplicate %s specification at %C", tag->name);
835 gfc_free_expr (result);
844 /* Match a variable I/O tag of some sort. */
847 match_vtag (const io_tag * tag, gfc_expr ** v)
852 m = gfc_match (tag->spec, &result);
858 gfc_error ("Duplicate %s specification at %C", tag->name);
859 gfc_free_expr (result);
863 if (result->symtree->n.sym->attr.intent == INTENT_IN)
865 gfc_error ("Variable tag cannot be INTENT(IN) at %C");
866 gfc_free_expr (result);
870 if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
872 gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
873 gfc_free_expr (result);
882 /* Match a label I/O tag. */
885 match_ltag (const io_tag * tag, gfc_st_label ** label)
891 m = gfc_match (tag->spec, label);
892 if (m == MATCH_YES && old != 0)
894 gfc_error ("Duplicate %s label specification at %C", tag->name);
902 /* Do expression resolution and type-checking on an expression tag. */
905 resolve_tag (const io_tag * tag, gfc_expr * e)
911 if (gfc_resolve_expr (e) == FAILURE)
914 if (e->ts.type != tag->type)
916 /* Format label can be integer varibale. */
917 if (tag != &tag_format)
919 gfc_error ("%s tag at %L must be of type %s", tag->name, &e->where,
920 gfc_basic_typename (tag->type));
925 if (tag == &tag_format)
927 if (e->rank != 1 && e->rank != 0)
929 gfc_error ("FORMAT tag at %L cannot be array of strings",
938 gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
947 /* Match a single tag of an OPEN statement. */
950 match_open_element (gfc_open * open)
954 m = match_etag (&tag_unit, &open->unit);
957 m = match_vtag (&tag_iostat, &open->iostat);
960 m = match_etag (&tag_file, &open->file);
963 m = match_etag (&tag_status, &open->status);
966 m = match_etag (&tag_e_access, &open->access);
969 m = match_etag (&tag_e_form, &open->form);
972 m = match_etag (&tag_e_recl, &open->recl);
975 m = match_etag (&tag_e_blank, &open->blank);
978 m = match_etag (&tag_e_position, &open->position);
981 m = match_etag (&tag_e_action, &open->action);
984 m = match_etag (&tag_e_delim, &open->delim);
987 m = match_etag (&tag_e_pad, &open->pad);
990 m = match_ltag (&tag_err, &open->err);
998 /* Free the gfc_open structure and all the expressions it contains. */
1001 gfc_free_open (gfc_open * open)
1007 gfc_free_expr (open->unit);
1008 gfc_free_expr (open->iostat);
1009 gfc_free_expr (open->file);
1010 gfc_free_expr (open->status);
1011 gfc_free_expr (open->access);
1012 gfc_free_expr (open->form);
1013 gfc_free_expr (open->recl);
1014 gfc_free_expr (open->blank);
1015 gfc_free_expr (open->position);
1016 gfc_free_expr (open->action);
1017 gfc_free_expr (open->delim);
1018 gfc_free_expr (open->pad);
1024 /* Resolve everything in a gfc_open structure. */
1027 gfc_resolve_open (gfc_open * open)
1030 RESOLVE_TAG (&tag_unit, open->unit);
1031 RESOLVE_TAG (&tag_iostat, open->iostat);
1032 RESOLVE_TAG (&tag_file, open->file);
1033 RESOLVE_TAG (&tag_status, open->status);
1034 RESOLVE_TAG (&tag_e_form, open->form);
1035 RESOLVE_TAG (&tag_e_recl, open->recl);
1037 RESOLVE_TAG (&tag_e_blank, open->blank);
1038 RESOLVE_TAG (&tag_e_position, open->position);
1039 RESOLVE_TAG (&tag_e_action, open->action);
1040 RESOLVE_TAG (&tag_e_delim, open->delim);
1041 RESOLVE_TAG (&tag_e_pad, open->pad);
1043 if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1050 /* Match an OPEN statmement. */
1053 gfc_match_open (void)
1058 m = gfc_match_char ('(');
1062 open = gfc_getmem (sizeof (gfc_open));
1064 m = match_open_element (open);
1066 if (m == MATCH_ERROR)
1070 m = gfc_match_expr (&open->unit);
1073 if (m == MATCH_ERROR)
1079 if (gfc_match_char (')') == MATCH_YES)
1081 if (gfc_match_char (',') != MATCH_YES)
1084 m = match_open_element (open);
1085 if (m == MATCH_ERROR)
1091 if (gfc_match_eos () == MATCH_NO)
1094 if (gfc_pure (NULL))
1096 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1100 new_st.op = EXEC_OPEN;
1101 new_st.ext.open = open;
1105 gfc_syntax_error (ST_OPEN);
1108 gfc_free_open (open);
1113 /* Free a gfc_close structure an all its expressions. */
1116 gfc_free_close (gfc_close * close)
1122 gfc_free_expr (close->unit);
1123 gfc_free_expr (close->iostat);
1124 gfc_free_expr (close->status);
1130 /* Match elements of a CLOSE statment. */
1133 match_close_element (gfc_close * close)
1137 m = match_etag (&tag_unit, &close->unit);
1140 m = match_etag (&tag_status, &close->status);
1143 m = match_vtag (&tag_iostat, &close->iostat);
1146 m = match_ltag (&tag_err, &close->err);
1154 /* Match a CLOSE statement. */
1157 gfc_match_close (void)
1162 m = gfc_match_char ('(');
1166 close = gfc_getmem (sizeof (gfc_close));
1168 m = match_close_element (close);
1170 if (m == MATCH_ERROR)
1174 m = gfc_match_expr (&close->unit);
1177 if (m == MATCH_ERROR)
1183 if (gfc_match_char (')') == MATCH_YES)
1185 if (gfc_match_char (',') != MATCH_YES)
1188 m = match_close_element (close);
1189 if (m == MATCH_ERROR)
1195 if (gfc_match_eos () == MATCH_NO)
1198 if (gfc_pure (NULL))
1200 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1204 new_st.op = EXEC_CLOSE;
1205 new_st.ext.close = close;
1209 gfc_syntax_error (ST_CLOSE);
1212 gfc_free_close (close);
1217 /* Resolve everything in a gfc_close structure. */
1220 gfc_resolve_close (gfc_close * close)
1223 RESOLVE_TAG (&tag_unit, close->unit);
1224 RESOLVE_TAG (&tag_iostat, close->iostat);
1225 RESOLVE_TAG (&tag_status, close->status);
1227 if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
1234 /* Free a gfc_filepos structure. */
1237 gfc_free_filepos (gfc_filepos * fp)
1240 gfc_free_expr (fp->unit);
1241 gfc_free_expr (fp->iostat);
1246 /* Match elements of a REWIND, BACKSPACE or ENDFILE statement. */
1249 match_file_element (gfc_filepos * fp)
1253 m = match_etag (&tag_unit, &fp->unit);
1256 m = match_vtag (&tag_iostat, &fp->iostat);
1259 m = match_ltag (&tag_err, &fp->err);
1267 /* Match the second half of the file-positioning statements, REWIND,
1268 BACKSPACE or ENDFILE. */
1271 match_filepos (gfc_statement st, gfc_exec_op op)
1276 fp = gfc_getmem (sizeof (gfc_filepos));
1278 if (gfc_match_char ('(') == MATCH_NO)
1280 m = gfc_match_expr (&fp->unit);
1281 if (m == MATCH_ERROR)
1289 m = match_file_element (fp);
1290 if (m == MATCH_ERROR)
1294 m = gfc_match_expr (&fp->unit);
1295 if (m == MATCH_ERROR)
1303 if (gfc_match_char (')') == MATCH_YES)
1305 if (gfc_match_char (',') != MATCH_YES)
1308 m = match_file_element (fp);
1309 if (m == MATCH_ERROR)
1316 if (gfc_match_eos () != MATCH_YES)
1319 if (gfc_pure (NULL))
1321 gfc_error ("%s statement not allowed in PURE procedure at %C",
1322 gfc_ascii_statement (st));
1328 new_st.ext.filepos = fp;
1332 gfc_syntax_error (st);
1335 gfc_free_filepos (fp);
1341 gfc_resolve_filepos (gfc_filepos * fp)
1344 RESOLVE_TAG (&tag_unit, fp->unit);
1345 if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
1352 /* Match the file positioning statements: ENDFILE, BACKSPACE or
1356 gfc_match_endfile (void)
1359 return match_filepos (ST_END_FILE, EXEC_ENDFILE);
1363 gfc_match_backspace (void)
1366 return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
1370 gfc_match_rewind (void)
1373 return match_filepos (ST_REWIND, EXEC_REWIND);
1377 /******************** Data Transfer Statments *********************/
1380 { M_READ, M_WRITE, M_PRINT, M_INQUIRE }
1384 /* Return a default unit number. */
1387 default_unit (io_kind k)
1396 return gfc_int_expr (unit);
1400 /* Match a unit specification for a data transfer statement. */
1403 match_dt_unit (io_kind k, gfc_dt * dt)
1407 if (gfc_match_char ('*') == MATCH_YES)
1409 if (dt->io_unit != NULL)
1412 dt->io_unit = default_unit (k);
1416 if (gfc_match_expr (&e) == MATCH_YES)
1418 if (dt->io_unit != NULL)
1431 gfc_error ("Duplicate UNIT specification at %C");
1436 /* Match a format specification. */
1439 match_dt_format (gfc_dt * dt)
1443 gfc_st_label *label;
1445 where = *gfc_current_locus ();
1447 if (gfc_match_char ('*') == MATCH_YES)
1449 if (dt->format_expr != NULL || dt->format_label != NULL)
1452 dt->format_label = &format_asterisk;
1456 if (gfc_match_st_label (&label, 0) == MATCH_YES)
1458 if (dt->format_expr != NULL || dt->format_label != NULL)
1460 gfc_free_st_label (label);
1464 if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
1467 dt->format_label = label;
1471 if (gfc_match_expr (&e) == MATCH_YES)
1473 if (dt->format_expr != NULL || dt->format_label != NULL)
1478 if (e->ts.type == BT_INTEGER && e->rank == 0)
1479 e->symtree->n.sym->attr.assign = 1;
1481 dt->format_expr = e;
1485 gfc_set_locus (&where); /* The only case where we have to restore */
1490 gfc_error ("Duplicate format specification at %C");
1495 /* Traverse a namelist that is part of a READ statement to make sure
1496 that none of the variables in the namelist are INTENT(IN). Returns
1497 nonzero if we find such a variable. */
1500 check_namelist (gfc_symbol * sym)
1504 for (p = sym->namelist; p; p = p->next)
1505 if (p->sym->attr.intent == INTENT_IN)
1507 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
1508 p->sym->name, sym->name);
1516 /* Match a single data transfer element. */
1519 match_dt_element (io_kind k, gfc_dt * dt)
1521 char name[GFC_MAX_SYMBOL_LEN + 1];
1525 if (gfc_match (" unit =") == MATCH_YES)
1527 m = match_dt_unit (k, dt);
1532 if (gfc_match (" fmt =") == MATCH_YES)
1534 m = match_dt_format (dt);
1539 if (gfc_match (" nml = %n", name) == MATCH_YES)
1541 if (dt->namelist != NULL)
1543 gfc_error ("Duplicate NML specification at %C");
1547 if (gfc_find_symbol (name, NULL, 1, &sym))
1550 if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
1552 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
1553 sym != NULL ? sym->name : name);
1558 if (k == M_READ && check_namelist (sym))
1564 m = match_etag (&tag_rec, &dt->rec);
1567 m = match_vtag (&tag_iostat, &dt->iostat);
1570 m = match_ltag (&tag_err, &dt->err);
1573 m = match_etag (&tag_advance, &dt->advance);
1576 m = match_vtag (&tag_size, &dt->size);
1580 m = match_ltag (&tag_end, &dt->end);
1582 dt->end_where = *gfc_current_locus ();
1586 m = match_ltag (&tag_eor, &dt->eor);
1588 dt->eor_where = *gfc_current_locus ();
1596 /* Free a data transfer structure and everything below it. */
1599 gfc_free_dt (gfc_dt * dt)
1605 gfc_free_expr (dt->io_unit);
1606 gfc_free_expr (dt->format_expr);
1607 gfc_free_expr (dt->rec);
1608 gfc_free_expr (dt->advance);
1609 gfc_free_expr (dt->iostat);
1610 gfc_free_expr (dt->size);
1616 /* Resolve everything in a gfc_dt structure. */
1619 gfc_resolve_dt (gfc_dt * dt)
1623 RESOLVE_TAG (&tag_format, dt->format_expr);
1624 RESOLVE_TAG (&tag_rec, dt->rec);
1625 RESOLVE_TAG (&tag_advance, dt->advance);
1626 RESOLVE_TAG (&tag_iostat, dt->iostat);
1627 RESOLVE_TAG (&tag_size, dt->size);
1630 if (gfc_resolve_expr (e) == SUCCESS
1631 && (e->ts.type != BT_INTEGER
1632 && (e->ts.type != BT_CHARACTER
1633 || e->expr_type != EXPR_VARIABLE)))
1636 ("UNIT specification at %L must be an INTEGER expression or a "
1637 "CHARACTER variable", &e->where);
1641 /* Sanity checks on data transfer statements. */
1642 if (e->ts.type == BT_CHARACTER)
1644 if (dt->rec != NULL)
1646 gfc_error ("REC tag at %L is incompatible with internal file",
1651 if (dt->namelist != NULL)
1653 gfc_error ("Internal file at %L is incompatible with namelist",
1654 &dt->io_unit->where);
1658 if (dt->advance != NULL)
1660 gfc_error ("ADVANCE tag at %L is incompatible with internal file",
1661 &dt->advance->where);
1666 if (dt->rec != NULL)
1668 if (dt->end != NULL)
1670 gfc_error ("REC tag at %L is incompatible with END tag",
1675 if (dt->format_label == &format_asterisk)
1678 ("END tag at %L is incompatible with list directed format (*)",
1683 if (dt->namelist != NULL)
1685 gfc_error ("REC tag at %L is incompatible with namelist",
1691 if (dt->advance != NULL && dt->format_label == &format_asterisk)
1693 gfc_error ("ADVANCE tag at %L is incompatible with list directed "
1694 "format (*)", &dt->advance->where);
1698 if (dt->eor != 0 && dt->advance == NULL)
1700 gfc_error ("EOR tag at %L requires an ADVANCE tag", &dt->eor_where);
1704 if (dt->size != NULL && dt->advance == NULL)
1706 gfc_error ("SIZE tag at %L requires an ADVANCE tag", &dt->size->where);
1710 /* TODO: Make sure the ADVANCE tag is 'yes' or 'no' if it is a string
1713 if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
1716 if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
1719 if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
1722 /* Check the format label ectually exists. */
1723 if (dt->format_label && dt->format_label != &format_asterisk
1724 && dt->format_label->defined == ST_LABEL_UNKNOWN)
1726 gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
1727 &dt->format_label->where);
1734 /* Given an io_kind, return its name. */
1737 io_kind_name (io_kind k)
1756 gfc_internal_error ("io_kind_name(): bad I/O-kind");
1763 /* Match an IO iteration statement of the form:
1765 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
1767 which is equivalent to a single IO element. This function is
1768 mutually recursive with match_io_element(). */
1770 static match match_io_element (io_kind k, gfc_code **);
1773 match_io_iterator (io_kind k, gfc_code ** result)
1775 gfc_code *head, *tail, *new;
1783 old_loc = *gfc_current_locus ();
1785 if (gfc_match_char ('(') != MATCH_YES)
1788 m = match_io_element (k, &head);
1791 if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
1797 /* Can't be anything but an IO iterator. Build a list. */
1798 iter = gfc_get_iterator ();
1802 m = gfc_match_iterator (iter, 0);
1803 if (m == MATCH_ERROR)
1808 m = match_io_element (k, &new);
1809 if (m == MATCH_ERROR)
1818 tail = gfc_append_code (tail, new);
1820 if (gfc_match_char (',') != MATCH_YES)
1829 if (gfc_match_char (')') != MATCH_YES)
1832 new = gfc_get_code ();
1834 new->ext.iterator = iter;
1836 new->block = gfc_get_code ();
1837 new->block->op = EXEC_DO;
1838 new->block->next = head;
1844 gfc_error ("Syntax error in I/O iterator at %C");
1848 gfc_free_iterator (iter, 1);
1849 gfc_free_statements (head);
1850 gfc_set_locus (&old_loc);
1855 /* Match a single element of an IO list, which is either a single
1856 expression or an IO Iterator. */
1859 match_io_element (io_kind k, gfc_code ** cpp)
1867 m = match_io_iterator (k, cpp);
1873 m = gfc_match_variable (&expr, 0);
1875 gfc_error ("Expected variable in READ statement at %C");
1879 m = gfc_match_expr (&expr);
1881 gfc_error ("Expected expression in %s statement at %C",
1889 if (expr->symtree->n.sym->attr.intent == INTENT_IN)
1892 ("Variable '%s' in input list at %C cannot be INTENT(IN)",
1893 expr->symtree->n.sym->name);
1898 && gfc_impure_variable (expr->symtree->n.sym)
1899 && current_dt->io_unit->ts.type == BT_CHARACTER)
1901 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
1902 expr->symtree->n.sym->name);
1909 if (current_dt->io_unit->ts.type == BT_CHARACTER
1911 && current_dt->io_unit->expr_type == EXPR_VARIABLE
1912 && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
1915 ("Cannot write to internal file unit '%s' at %C inside a "
1916 "PURE procedure", current_dt->io_unit->symtree->n.sym->name);
1928 gfc_free_expr (expr);
1932 cp = gfc_get_code ();
1933 cp->op = EXEC_TRANSFER;
1941 /* Match an I/O list, building gfc_code structures as we go. */
1944 match_io_list (io_kind k, gfc_code ** head_p)
1946 gfc_code *head, *tail, *new;
1949 *head_p = head = tail = NULL;
1950 if (gfc_match_eos () == MATCH_YES)
1955 m = match_io_element (k, &new);
1956 if (m == MATCH_ERROR)
1961 tail = gfc_append_code (tail, new);
1965 if (gfc_match_eos () == MATCH_YES)
1967 if (gfc_match_char (',') != MATCH_YES)
1975 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
1978 gfc_free_statements (head);
1983 /* Attach the data transfer end node. */
1986 terminate_io (gfc_code * io_code)
1990 if (io_code == NULL)
1993 c = gfc_get_code ();
1994 c->op = EXEC_DT_END;
1996 /* Point to structure that is already there */
1997 c->ext.dt = new_st.ext.dt;
1998 gfc_append_code (io_code, c);
2002 /* Match a READ, WRITE or PRINT statement. */
2005 match_io (io_kind k)
2007 char name[GFC_MAX_SYMBOL_LEN + 1];
2017 current_dt = dt = gfc_getmem (sizeof (gfc_dt));
2019 if (gfc_match_char ('(') == MATCH_NO)
2024 m = match_dt_format (dt);
2025 if (m == MATCH_ERROR)
2031 dt->io_unit = default_unit (k);
2035 /* Match a control list */
2036 if (match_dt_element (k, dt) == MATCH_YES)
2038 if (match_dt_unit (k, dt) != MATCH_YES)
2041 if (gfc_match_char (')') == MATCH_YES)
2043 if (gfc_match_char (',') != MATCH_YES)
2046 m = match_dt_element (k, dt);
2049 if (m == MATCH_ERROR)
2052 m = match_dt_format (dt);
2055 if (m == MATCH_ERROR)
2058 where = *gfc_current_locus ();
2060 if (gfc_match_name (name) == MATCH_YES
2061 && !gfc_find_symbol (name, NULL, 1, &sym)
2062 && sym->attr.flavor == FL_NAMELIST)
2065 if (k == M_READ && check_namelist (sym))
2073 gfc_set_locus (&where);
2075 goto loop; /* No matches, try regular elements */
2078 if (gfc_match_char (')') == MATCH_YES)
2080 if (gfc_match_char (',') != MATCH_YES)
2086 m = match_dt_element (k, dt);
2089 if (m == MATCH_ERROR)
2092 if (gfc_match_char (')') == MATCH_YES)
2094 if (gfc_match_char (',') != MATCH_YES)
2099 /* Optional leading comma (non-standard). */
2101 gfc_match_char (',');
2104 if (gfc_match_eos () != MATCH_YES)
2106 if (comma_flag && gfc_match_char (',') != MATCH_YES)
2108 gfc_error ("Expected comma in I/O list at %C");
2113 m = match_io_list (k, &io_code);
2114 if (m == MATCH_ERROR)
2120 /* A full IO statement has been matched. */
2121 if (dt->io_unit->expr_type == EXPR_VARIABLE
2123 && dt->io_unit->ts.type == BT_CHARACTER
2124 && dt->io_unit->symtree->n.sym->attr.intent == INTENT_IN)
2126 gfc_error ("Internal file '%s' at %L is INTENT(IN)",
2127 dt->io_unit->symtree->n.sym->name, &dt->io_unit->where);
2132 expr = dt->format_expr;
2134 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
2135 check_format_string (expr);
2138 && (k == M_READ || k == M_WRITE)
2139 && dt->io_unit->ts.type != BT_CHARACTER)
2142 ("io-unit in %s statement at %C must be an internal file in a "
2143 "PURE procedure", io_kind_name (k));
2148 new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
2150 new_st.next = io_code;
2152 terminate_io (io_code);
2157 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2167 gfc_match_read (void)
2169 return match_io (M_READ);
2173 gfc_match_write (void)
2175 return match_io (M_WRITE);
2179 gfc_match_print (void)
2183 m = match_io (M_PRINT);
2187 if (gfc_pure (NULL))
2189 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
2197 /* Free a gfc_inquire structure. */
2200 gfc_free_inquire (gfc_inquire * inquire)
2203 if (inquire == NULL)
2206 gfc_free_expr (inquire->unit);
2207 gfc_free_expr (inquire->file);
2208 gfc_free_expr (inquire->iostat);
2209 gfc_free_expr (inquire->exist);
2210 gfc_free_expr (inquire->opened);
2211 gfc_free_expr (inquire->number);
2212 gfc_free_expr (inquire->named);
2213 gfc_free_expr (inquire->name);
2214 gfc_free_expr (inquire->access);
2215 gfc_free_expr (inquire->sequential);
2216 gfc_free_expr (inquire->direct);
2217 gfc_free_expr (inquire->form);
2218 gfc_free_expr (inquire->formatted);
2219 gfc_free_expr (inquire->unformatted);
2220 gfc_free_expr (inquire->recl);
2221 gfc_free_expr (inquire->nextrec);
2222 gfc_free_expr (inquire->blank);
2223 gfc_free_expr (inquire->position);
2224 gfc_free_expr (inquire->action);
2225 gfc_free_expr (inquire->read);
2226 gfc_free_expr (inquire->write);
2227 gfc_free_expr (inquire->readwrite);
2228 gfc_free_expr (inquire->delim);
2229 gfc_free_expr (inquire->pad);
2230 gfc_free_expr (inquire->iolength);
2236 /* Match an element of an INQUIRE statement. */
2238 #define RETM if (m != MATCH_NO) return m;
2241 match_inquire_element (gfc_inquire * inquire)
2245 m = match_etag (&tag_unit, &inquire->unit);
2246 RETM m = match_etag (&tag_file, &inquire->file);
2247 RETM m = match_ltag (&tag_err, &inquire->err);
2248 RETM m = match_vtag (&tag_iostat, &inquire->iostat);
2249 RETM m = match_vtag (&tag_exist, &inquire->exist);
2250 RETM m = match_vtag (&tag_opened, &inquire->opened);
2251 RETM m = match_vtag (&tag_named, &inquire->named);
2252 RETM m = match_vtag (&tag_name, &inquire->name);
2253 RETM m = match_vtag (&tag_number, &inquire->number);
2254 RETM m = match_vtag (&tag_s_access, &inquire->access);
2255 RETM m = match_vtag (&tag_sequential, &inquire->sequential);
2256 RETM m = match_vtag (&tag_direct, &inquire->direct);
2257 RETM m = match_vtag (&tag_s_form, &inquire->form);
2258 RETM m = match_vtag (&tag_formatted, &inquire->formatted);
2259 RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
2260 RETM m = match_vtag (&tag_s_recl, &inquire->recl);
2261 RETM m = match_vtag (&tag_nextrec, &inquire->nextrec);
2262 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
2263 RETM m = match_vtag (&tag_s_position, &inquire->position);
2264 RETM m = match_vtag (&tag_s_action, &inquire->action);
2265 RETM m = match_vtag (&tag_read, &inquire->read);
2266 RETM m = match_vtag (&tag_write, &inquire->write);
2267 RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
2268 RETM m = match_vtag (&tag_s_delim, &inquire->delim);
2269 RETM m = match_vtag (&tag_s_pad, &inquire->pad);
2270 RETM m = match_vtag (&tag_iolength, &inquire->iolength);
2271 RETM return MATCH_NO;
2278 gfc_match_inquire (void)
2280 gfc_inquire *inquire;
2284 m = gfc_match_char ('(');
2288 inquire = gfc_getmem (sizeof (gfc_inquire));
2290 m = match_inquire_element (inquire);
2291 if (m == MATCH_ERROR)
2295 m = gfc_match_expr (&inquire->unit);
2296 if (m == MATCH_ERROR)
2302 /* See if we have the IOLENGTH form of the inquire statement. */
2303 if (inquire->iolength != NULL)
2305 if (gfc_match_char (')') != MATCH_YES)
2308 m = match_io_list (M_INQUIRE, &code);
2309 if (m == MATCH_ERROR)
2314 terminate_io (code);
2316 new_st.op = EXEC_IOLENGTH;
2317 new_st.expr = inquire->iolength;
2320 if (gfc_pure (NULL))
2322 gfc_free_statements (code);
2323 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
2331 /* At this point, we have the non-IOLENGTH inquire statement. */
2334 if (gfc_match_char (')') == MATCH_YES)
2336 if (gfc_match_char (',') != MATCH_YES)
2339 m = match_inquire_element (inquire);
2340 if (m == MATCH_ERROR)
2345 if (inquire->iolength != NULL)
2347 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
2352 if (gfc_match_eos () != MATCH_YES)
2355 if (gfc_pure (NULL))
2357 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
2361 new_st.op = EXEC_INQUIRE;
2362 new_st.ext.inquire = inquire;
2366 gfc_syntax_error (ST_INQUIRE);
2369 gfc_free_inquire (inquire);
2374 /* Resolve everything in a gfc_inquire structure. */
2377 gfc_resolve_inquire (gfc_inquire * inquire)
2380 RESOLVE_TAG (&tag_unit, inquire->unit);
2381 RESOLVE_TAG (&tag_file, inquire->file);
2382 RESOLVE_TAG (&tag_iostat, inquire->iostat);
2383 RESOLVE_TAG (&tag_exist, inquire->exist);
2384 RESOLVE_TAG (&tag_opened, inquire->opened);
2385 RESOLVE_TAG (&tag_number, inquire->number);
2386 RESOLVE_TAG (&tag_named, inquire->named);
2387 RESOLVE_TAG (&tag_name, inquire->name);
2388 RESOLVE_TAG (&tag_s_access, inquire->access);
2389 RESOLVE_TAG (&tag_sequential, inquire->sequential);
2390 RESOLVE_TAG (&tag_direct, inquire->direct);
2391 RESOLVE_TAG (&tag_s_form, inquire->form);
2392 RESOLVE_TAG (&tag_formatted, inquire->formatted);
2393 RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
2394 RESOLVE_TAG (&tag_s_recl, inquire->recl);
2395 RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
2396 RESOLVE_TAG (&tag_s_blank, inquire->blank);
2397 RESOLVE_TAG (&tag_s_position, inquire->position);
2398 RESOLVE_TAG (&tag_s_action, inquire->action);
2399 RESOLVE_TAG (&tag_read, inquire->read);
2400 RESOLVE_TAG (&tag_write, inquire->write);
2401 RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
2402 RESOLVE_TAG (&tag_s_delim, inquire->delim);
2403 RESOLVE_TAG (&tag_s_pad, inquire->pad);
2405 if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)