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_err = {"ERR", " err = %l", BT_UNKNOWN},
82 tag_end = {"END", " end = %l", BT_UNKNOWN},
83 tag_eor = {"EOR", " eor = %l", BT_UNKNOWN};
85 static gfc_dt *current_dt;
87 #define RESOLVE_TAG(x, y) if (resolve_tag(x, y) == FAILURE) return FAILURE;
90 /**************** Fortran 95 FORMAT parser *****************/
92 /* FORMAT tokens returned by format_lex(). */
95 FMT_NONE, FMT_UNKNOWN, FMT_SIGNED_INT, FMT_ZERO, FMT_POSINT, FMT_PERIOD,
96 FMT_COMMA, FMT_COLON, FMT_SLASH, FMT_DOLLAR, FMT_POS, FMT_LPAREN,
97 FMT_RPAREN, FMT_X, FMT_SIGN, FMT_BLANK, FMT_CHAR, FMT_P, FMT_IBOZ, FMT_F,
98 FMT_E, FMT_EXT, FMT_G, FMT_L, FMT_A, FMT_D, FMT_H, FMT_END
102 /* Local variables for checking format strings. The saved_token is
103 used to back up by a single format token during the parsing
105 static char *format_string;
106 static int format_length, use_last_char;
108 static format_token saved_token;
111 { MODE_STRING, MODE_FORMAT, MODE_COPY }
115 /* Return the next character in the format string. */
118 next_char (int in_string)
130 if (mode == MODE_STRING)
131 c = *format_string++;
134 c = gfc_next_char_literal (in_string);
138 if (mode == MODE_COPY)
139 *format_string++ = c;
147 /* Back up one character position. Only works once. */
156 static int value = 0;
158 /* Simple lexical analyzer for getting the next token in a FORMAT
169 if (saved_token != FMT_NONE)
172 saved_token = FMT_NONE;
180 while (gfc_is_whitespace (c));
201 value = 10 * value + c - '0';
210 token = FMT_SIGNED_INT;
233 value = 10 * value + c - '0';
238 token = zflag ? FMT_ZERO : FMT_POSINT;
263 if (c != 'L' && c != 'R')
283 if (c != 'P' && c != 'S')
291 if (c == 'N' || c == 'Z')
353 if (c == 'N' || c == 'S')
396 /* Check a format statement. The format string, either from a FORMAT
397 statement or a constant in an I/O statement has already been parsed
398 by itself, and we are checking it for validity. The dual origin
399 means that the warning message is a little less than great. */
404 const char *posint_required = _("Positive width required");
405 const char *period_required = _("Period required");
406 const char *nonneg_required = _("Nonnegative width required");
407 const char *unexpected_element = _("Unexpected element");
408 const char *unexpected_end = _("Unexpected end of format string");
417 saved_token = FMT_NONE;
425 error = _("Missing leading left parenthesis");
431 goto finished; /* Empty format is legal */
435 /* 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;
496 if (gfc_notify_std (GFC_STD_GNU, "Extension: $ descriptor at %C")
499 if (t != FMT_RPAREN || level > 0)
501 error = _("$ must be the last specifier");
522 error = unexpected_end;
526 error = unexpected_element;
531 /* In this state, t must currently be a data descriptor.
532 Deal with things that can/must follow the descriptor. */
546 error = _("Repeat count cannot follow P descriptor");
561 error = posint_required;
577 error = posint_required;
584 error = period_required;
589 if (u != FMT_ZERO && u != FMT_POSINT)
591 error = nonneg_required;
598 /* Look for optional exponent. */
609 error = _("Positive exponent width required");
618 if (t != FMT_ZERO && t != FMT_POSINT)
620 error = nonneg_required;
627 error = period_required;
632 if (t != FMT_ZERO && t != FMT_POSINT)
634 error = nonneg_required;
641 if(mode == MODE_STRING)
643 format_string += value;
644 format_length -= value;
658 if (t != FMT_ZERO && t != FMT_POSINT)
660 error = nonneg_required;
672 if (t != FMT_ZERO && t != FMT_POSINT)
674 error = nonneg_required;
682 error = unexpected_element;
687 /* Between a descriptor and what comes next. */
706 error = unexpected_end;
710 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
717 /* Optional comma is a weird between state where we've just finished
718 reading a colon, slash or P descriptor. */
732 /* Assume that we have another format item. */
739 extension_optional_comma:
740 /* As a GNU extension, permit a missing comma after a string literal. */
758 error = unexpected_end;
762 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
772 /* Something went wrong. If the format we're checking is a string,
773 generate a warning, since the program is correct. If the format
774 is in a FORMAT statement, this messes up parsing, which is an
776 if (mode != MODE_STRING)
777 gfc_error ("%s in format string at %C", error);
780 gfc_warning ("%s in format string at %C", error);
782 /* TODO: More elaborate measures are needed to show where a problem
783 is within a format string that has been calculated. */
793 /* Given an expression node that is a constant string, see if it looks
794 like a format string. */
797 check_format_string (gfc_expr * e)
801 format_string = e->value.character.string;
806 /************ Fortran 95 I/O statement matchers *************/
808 /* Match a FORMAT statement. This amounts to actually parsing the
809 format descriptors in order to correctly locate the end of the
813 gfc_match_format (void)
818 if (gfc_statement_label == NULL)
820 gfc_error ("Missing format label at %C");
823 gfc_gobble_whitespace ();
828 start = gfc_current_locus;
830 if (check_format () == FAILURE)
833 if (gfc_match_eos () != MATCH_YES)
835 gfc_syntax_error (ST_FORMAT);
839 /* The label doesn't get created until after the statement is done
840 being matched, so we have to leave the string for later. */
842 gfc_current_locus = start; /* Back to the beginning */
845 new_st.op = EXEC_NOP;
848 e->expr_type = EXPR_CONSTANT;
849 e->ts.type = BT_CHARACTER;
850 e->ts.kind = gfc_default_character_kind;
852 e->value.character.string = format_string = gfc_getmem(format_length+1);
853 e->value.character.length = format_length;
854 gfc_statement_label->format = e;
857 check_format (); /* Guaranteed to succeed */
858 gfc_match_eos (); /* Guaranteed to succeed */
864 /* Match an expression I/O tag of some sort. */
867 match_etag (const io_tag * tag, gfc_expr ** v)
872 m = gfc_match (tag->spec, &result);
878 gfc_error ("Duplicate %s specification at %C", tag->name);
879 gfc_free_expr (result);
888 /* Match a variable I/O tag of some sort. */
891 match_vtag (const io_tag * tag, gfc_expr ** v)
896 m = gfc_match (tag->spec, &result);
902 gfc_error ("Duplicate %s specification at %C", tag->name);
903 gfc_free_expr (result);
907 if (result->symtree->n.sym->attr.intent == INTENT_IN)
909 gfc_error ("Variable tag cannot be INTENT(IN) at %C");
910 gfc_free_expr (result);
914 if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
916 gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
917 gfc_free_expr (result);
926 /* Match I/O tags that cause variables to become redefined. */
929 match_out_tag(const io_tag *tag, gfc_expr **result)
933 m = match_vtag(tag, result);
935 gfc_check_do_variable((*result)->symtree);
941 /* Match a label I/O tag. */
944 match_ltag (const io_tag * tag, gfc_st_label ** label)
950 m = gfc_match (tag->spec, label);
951 if (m == MATCH_YES && old != 0)
953 gfc_error ("Duplicate %s label specification at %C", tag->name);
961 /* Do expression resolution and type-checking on an expression tag. */
964 resolve_tag (const io_tag * tag, gfc_expr * e)
970 if (gfc_resolve_expr (e) == FAILURE)
973 if (e->ts.type != tag->type && tag != &tag_format)
975 gfc_error ("%s tag at %L must be of type %s", tag->name,
976 &e->where, gfc_basic_typename (tag->type));
980 if (tag == &tag_format)
982 if (e->expr_type == EXPR_CONSTANT
983 && (e->ts.type != BT_CHARACTER
984 || e->ts.kind != gfc_default_character_kind))
986 gfc_error ("Constant expression in FORMAT tag at %L must be "
987 "of type default CHARACTER", &e->where);
991 /* If e's rank is zero and e is not an element of an array, it should be
992 of integer or character type. The integer variable should be
994 if (e->symtree == NULL || e->symtree->n.sym->as == NULL
995 || e->symtree->n.sym->as->rank == 0)
997 if (e->ts.type != BT_CHARACTER && e->ts.type != BT_INTEGER)
999 gfc_error ("%s tag at %L must be of type %s or %s", tag->name,
1000 &e->where, gfc_basic_typename (BT_CHARACTER),
1001 gfc_basic_typename (BT_INTEGER));
1004 else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
1006 if (gfc_notify_std (GFC_STD_F95_DEL,
1007 "Obsolete: ASSIGNED variable in FORMAT tag at %L",
1008 &e->where) == FAILURE)
1010 if (e->symtree->n.sym->attr.assign != 1)
1012 gfc_error ("Variable '%s' at %L has not been assigned a "
1013 "format label", e->symtree->n.sym->name, &e->where);
1021 /* if rank is nonzero, we allow the type to be character under
1022 GFC_STD_GNU and other type under GFC_STD_LEGACY. It may be
1023 assigned an Hollerith constant. */
1024 if (e->ts.type == BT_CHARACTER)
1026 if (gfc_notify_std (GFC_STD_GNU,
1027 "Extension: Character array in FORMAT tag at %L",
1028 &e->where) == FAILURE)
1033 if (gfc_notify_std (GFC_STD_LEGACY,
1034 "Extension: Non-character in FORMAT tag at %L",
1035 &e->where) == FAILURE)
1045 gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
1048 if (tag == &tag_iomsg)
1050 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IOMSG tag at %L",
1051 &e->where) == FAILURE)
1060 /* Match a single tag of an OPEN statement. */
1063 match_open_element (gfc_open * open)
1067 m = match_etag (&tag_unit, &open->unit);
1070 m = match_out_tag (&tag_iomsg, &open->iomsg);
1073 m = match_out_tag (&tag_iostat, &open->iostat);
1076 m = match_etag (&tag_file, &open->file);
1079 m = match_etag (&tag_status, &open->status);
1082 m = match_etag (&tag_e_access, &open->access);
1085 m = match_etag (&tag_e_form, &open->form);
1088 m = match_etag (&tag_e_recl, &open->recl);
1091 m = match_etag (&tag_e_blank, &open->blank);
1094 m = match_etag (&tag_e_position, &open->position);
1097 m = match_etag (&tag_e_action, &open->action);
1100 m = match_etag (&tag_e_delim, &open->delim);
1103 m = match_etag (&tag_e_pad, &open->pad);
1106 m = match_ltag (&tag_err, &open->err);
1114 /* Free the gfc_open structure and all the expressions it contains. */
1117 gfc_free_open (gfc_open * open)
1123 gfc_free_expr (open->unit);
1124 gfc_free_expr (open->iomsg);
1125 gfc_free_expr (open->iostat);
1126 gfc_free_expr (open->file);
1127 gfc_free_expr (open->status);
1128 gfc_free_expr (open->access);
1129 gfc_free_expr (open->form);
1130 gfc_free_expr (open->recl);
1131 gfc_free_expr (open->blank);
1132 gfc_free_expr (open->position);
1133 gfc_free_expr (open->action);
1134 gfc_free_expr (open->delim);
1135 gfc_free_expr (open->pad);
1141 /* Resolve everything in a gfc_open structure. */
1144 gfc_resolve_open (gfc_open * open)
1147 RESOLVE_TAG (&tag_unit, open->unit);
1148 RESOLVE_TAG (&tag_iomsg, open->iomsg);
1149 RESOLVE_TAG (&tag_iostat, open->iostat);
1150 RESOLVE_TAG (&tag_file, open->file);
1151 RESOLVE_TAG (&tag_status, open->status);
1152 RESOLVE_TAG (&tag_e_form, open->form);
1153 RESOLVE_TAG (&tag_e_recl, open->recl);
1155 RESOLVE_TAG (&tag_e_blank, open->blank);
1156 RESOLVE_TAG (&tag_e_position, open->position);
1157 RESOLVE_TAG (&tag_e_action, open->action);
1158 RESOLVE_TAG (&tag_e_delim, open->delim);
1159 RESOLVE_TAG (&tag_e_pad, open->pad);
1161 if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1168 /* Match an OPEN statement. */
1171 gfc_match_open (void)
1176 m = gfc_match_char ('(');
1180 open = gfc_getmem (sizeof (gfc_open));
1182 m = match_open_element (open);
1184 if (m == MATCH_ERROR)
1188 m = gfc_match_expr (&open->unit);
1191 if (m == MATCH_ERROR)
1197 if (gfc_match_char (')') == MATCH_YES)
1199 if (gfc_match_char (',') != MATCH_YES)
1202 m = match_open_element (open);
1203 if (m == MATCH_ERROR)
1209 if (gfc_match_eos () == MATCH_NO)
1212 if (gfc_pure (NULL))
1214 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1218 new_st.op = EXEC_OPEN;
1219 new_st.ext.open = open;
1223 gfc_syntax_error (ST_OPEN);
1226 gfc_free_open (open);
1231 /* Free a gfc_close structure an all its expressions. */
1234 gfc_free_close (gfc_close * close)
1240 gfc_free_expr (close->unit);
1241 gfc_free_expr (close->iomsg);
1242 gfc_free_expr (close->iostat);
1243 gfc_free_expr (close->status);
1249 /* Match elements of a CLOSE statement. */
1252 match_close_element (gfc_close * close)
1256 m = match_etag (&tag_unit, &close->unit);
1259 m = match_etag (&tag_status, &close->status);
1262 m = match_out_tag (&tag_iomsg, &close->iomsg);
1265 m = match_out_tag (&tag_iostat, &close->iostat);
1268 m = match_ltag (&tag_err, &close->err);
1276 /* Match a CLOSE statement. */
1279 gfc_match_close (void)
1284 m = gfc_match_char ('(');
1288 close = gfc_getmem (sizeof (gfc_close));
1290 m = match_close_element (close);
1292 if (m == MATCH_ERROR)
1296 m = gfc_match_expr (&close->unit);
1299 if (m == MATCH_ERROR)
1305 if (gfc_match_char (')') == MATCH_YES)
1307 if (gfc_match_char (',') != MATCH_YES)
1310 m = match_close_element (close);
1311 if (m == MATCH_ERROR)
1317 if (gfc_match_eos () == MATCH_NO)
1320 if (gfc_pure (NULL))
1322 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1326 new_st.op = EXEC_CLOSE;
1327 new_st.ext.close = close;
1331 gfc_syntax_error (ST_CLOSE);
1334 gfc_free_close (close);
1339 /* Resolve everything in a gfc_close structure. */
1342 gfc_resolve_close (gfc_close * close)
1345 RESOLVE_TAG (&tag_unit, close->unit);
1346 RESOLVE_TAG (&tag_iomsg, close->iomsg);
1347 RESOLVE_TAG (&tag_iostat, close->iostat);
1348 RESOLVE_TAG (&tag_status, close->status);
1350 if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
1357 /* Free a gfc_filepos structure. */
1360 gfc_free_filepos (gfc_filepos * fp)
1363 gfc_free_expr (fp->unit);
1364 gfc_free_expr (fp->iomsg);
1365 gfc_free_expr (fp->iostat);
1370 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
1373 match_file_element (gfc_filepos * fp)
1377 m = match_etag (&tag_unit, &fp->unit);
1380 m = match_out_tag (&tag_iomsg, &fp->iomsg);
1383 m = match_out_tag (&tag_iostat, &fp->iostat);
1386 m = match_ltag (&tag_err, &fp->err);
1394 /* Match the second half of the file-positioning statements, REWIND,
1395 BACKSPACE, ENDFILE, or the FLUSH statement. */
1398 match_filepos (gfc_statement st, gfc_exec_op op)
1403 fp = gfc_getmem (sizeof (gfc_filepos));
1405 if (gfc_match_char ('(') == MATCH_NO)
1407 m = gfc_match_expr (&fp->unit);
1408 if (m == MATCH_ERROR)
1416 m = match_file_element (fp);
1417 if (m == MATCH_ERROR)
1421 m = gfc_match_expr (&fp->unit);
1422 if (m == MATCH_ERROR)
1430 if (gfc_match_char (')') == MATCH_YES)
1432 if (gfc_match_char (',') != MATCH_YES)
1435 m = match_file_element (fp);
1436 if (m == MATCH_ERROR)
1443 if (gfc_match_eos () != MATCH_YES)
1446 if (gfc_pure (NULL))
1448 gfc_error ("%s statement not allowed in PURE procedure at %C",
1449 gfc_ascii_statement (st));
1455 new_st.ext.filepos = fp;
1459 gfc_syntax_error (st);
1462 gfc_free_filepos (fp);
1468 gfc_resolve_filepos (gfc_filepos * fp)
1471 RESOLVE_TAG (&tag_unit, fp->unit);
1472 RESOLVE_TAG (&tag_iostat, fp->iostat);
1473 RESOLVE_TAG (&tag_iomsg, fp->iomsg);
1474 if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
1481 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
1482 and the FLUSH statement. */
1485 gfc_match_endfile (void)
1488 return match_filepos (ST_END_FILE, EXEC_ENDFILE);
1492 gfc_match_backspace (void)
1495 return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
1499 gfc_match_rewind (void)
1502 return match_filepos (ST_REWIND, EXEC_REWIND);
1506 gfc_match_flush (void)
1508 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: FLUSH statement at %C") == FAILURE)
1511 return match_filepos (ST_FLUSH, EXEC_FLUSH);
1514 /******************** Data Transfer Statements *********************/
1517 { M_READ, M_WRITE, M_PRINT, M_INQUIRE }
1521 /* Return a default unit number. */
1524 default_unit (io_kind k)
1533 return gfc_int_expr (unit);
1537 /* Match a unit specification for a data transfer statement. */
1540 match_dt_unit (io_kind k, gfc_dt * dt)
1544 if (gfc_match_char ('*') == MATCH_YES)
1546 if (dt->io_unit != NULL)
1549 dt->io_unit = default_unit (k);
1553 if (gfc_match_expr (&e) == MATCH_YES)
1555 if (dt->io_unit != NULL)
1568 gfc_error ("Duplicate UNIT specification at %C");
1573 /* Match a format specification. */
1576 match_dt_format (gfc_dt * dt)
1580 gfc_st_label *label;
1582 where = gfc_current_locus;
1584 if (gfc_match_char ('*') == MATCH_YES)
1586 if (dt->format_expr != NULL || dt->format_label != NULL)
1589 dt->format_label = &format_asterisk;
1593 if (gfc_match_st_label (&label, 0) == MATCH_YES)
1595 if (dt->format_expr != NULL || dt->format_label != NULL)
1597 gfc_free_st_label (label);
1601 if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
1604 dt->format_label = label;
1608 if (gfc_match_expr (&e) == MATCH_YES)
1610 if (dt->format_expr != NULL || dt->format_label != NULL)
1615 dt->format_expr = e;
1619 gfc_current_locus = where; /* The only case where we have to restore */
1624 gfc_error ("Duplicate format specification at %C");
1629 /* Traverse a namelist that is part of a READ statement to make sure
1630 that none of the variables in the namelist are INTENT(IN). Returns
1631 nonzero if we find such a variable. */
1634 check_namelist (gfc_symbol * sym)
1638 for (p = sym->namelist; p; p = p->next)
1639 if (p->sym->attr.intent == INTENT_IN)
1641 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
1642 p->sym->name, sym->name);
1650 /* Match a single data transfer element. */
1653 match_dt_element (io_kind k, gfc_dt * dt)
1655 char name[GFC_MAX_SYMBOL_LEN + 1];
1659 if (gfc_match (" unit =") == MATCH_YES)
1661 m = match_dt_unit (k, dt);
1666 if (gfc_match (" fmt =") == MATCH_YES)
1668 m = match_dt_format (dt);
1673 if (gfc_match (" nml = %n", name) == MATCH_YES)
1675 if (dt->namelist != NULL)
1677 gfc_error ("Duplicate NML specification at %C");
1681 if (gfc_find_symbol (name, NULL, 1, &sym))
1684 if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
1686 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
1687 sym != NULL ? sym->name : name);
1692 if (k == M_READ && check_namelist (sym))
1698 m = match_etag (&tag_rec, &dt->rec);
1701 m = match_out_tag (&tag_iomsg, &dt->iomsg);
1704 m = match_out_tag (&tag_iostat, &dt->iostat);
1707 m = match_ltag (&tag_err, &dt->err);
1710 m = match_etag (&tag_advance, &dt->advance);
1713 m = match_out_tag (&tag_size, &dt->size);
1717 m = match_ltag (&tag_end, &dt->end);
1722 gfc_error ("END tag at %C not allowed in output statement");
1725 dt->end_where = gfc_current_locus;
1730 m = match_ltag (&tag_eor, &dt->eor);
1732 dt->eor_where = gfc_current_locus;
1740 /* Free a data transfer structure and everything below it. */
1743 gfc_free_dt (gfc_dt * dt)
1749 gfc_free_expr (dt->io_unit);
1750 gfc_free_expr (dt->format_expr);
1751 gfc_free_expr (dt->rec);
1752 gfc_free_expr (dt->advance);
1753 gfc_free_expr (dt->iomsg);
1754 gfc_free_expr (dt->iostat);
1755 gfc_free_expr (dt->size);
1761 /* Resolve everything in a gfc_dt structure. */
1764 gfc_resolve_dt (gfc_dt * dt)
1768 RESOLVE_TAG (&tag_format, dt->format_expr);
1769 RESOLVE_TAG (&tag_rec, dt->rec);
1770 RESOLVE_TAG (&tag_advance, dt->advance);
1771 RESOLVE_TAG (&tag_iomsg, dt->iomsg);
1772 RESOLVE_TAG (&tag_iostat, dt->iostat);
1773 RESOLVE_TAG (&tag_size, dt->size);
1776 if (gfc_resolve_expr (e) == SUCCESS
1777 && (e->ts.type != BT_INTEGER
1778 && (e->ts.type != BT_CHARACTER
1779 || e->expr_type != EXPR_VARIABLE)))
1782 ("UNIT specification at %L must be an INTEGER expression or a "
1783 "CHARACTER variable", &e->where);
1787 /* Sanity checks on data transfer statements. */
1788 if (e->ts.type == BT_CHARACTER)
1790 if (gfc_has_vector_index (e))
1792 gfc_error ("Internal unit with vector subscript at %L",
1797 if (dt->rec != NULL)
1799 gfc_error ("REC tag at %L is incompatible with internal file",
1804 if (dt->namelist != NULL)
1806 gfc_error ("Internal file at %L is incompatible with namelist",
1807 &dt->io_unit->where);
1811 if (dt->advance != NULL)
1813 gfc_error ("ADVANCE tag at %L is incompatible with internal file",
1814 &dt->advance->where);
1819 if (dt->rec != NULL)
1821 if (dt->end != NULL)
1823 gfc_error ("REC tag at %L is incompatible with END tag",
1828 if (dt->format_label == &format_asterisk)
1831 ("END tag at %L is incompatible with list directed format (*)",
1836 if (dt->namelist != NULL)
1838 gfc_error ("REC tag at %L is incompatible with namelist",
1844 if (dt->advance != NULL && dt->format_label == &format_asterisk)
1846 gfc_error ("ADVANCE tag at %L is incompatible with list directed "
1847 "format (*)", &dt->advance->where);
1851 if (dt->eor != 0 && dt->advance == NULL)
1853 gfc_error ("EOR tag at %L requires an ADVANCE tag", &dt->eor_where);
1857 if (dt->size != NULL && dt->advance == NULL)
1859 gfc_error ("SIZE tag at %L requires an ADVANCE tag", &dt->size->where);
1863 /* TODO: Make sure the ADVANCE tag is 'yes' or 'no' if it is a string
1866 if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
1869 if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
1872 if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
1875 /* Check the format label actually exists. */
1876 if (dt->format_label && dt->format_label != &format_asterisk
1877 && dt->format_label->defined == ST_LABEL_UNKNOWN)
1879 gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
1880 &dt->format_label->where);
1887 /* Given an io_kind, return its name. */
1890 io_kind_name (io_kind k)
1909 gfc_internal_error ("io_kind_name(): bad I/O-kind");
1916 /* Match an IO iteration statement of the form:
1918 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
1920 which is equivalent to a single IO element. This function is
1921 mutually recursive with match_io_element(). */
1923 static match match_io_element (io_kind k, gfc_code **);
1926 match_io_iterator (io_kind k, gfc_code ** result)
1928 gfc_code *head, *tail, *new;
1936 old_loc = gfc_current_locus;
1938 if (gfc_match_char ('(') != MATCH_YES)
1941 m = match_io_element (k, &head);
1944 if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
1950 /* Can't be anything but an IO iterator. Build a list. */
1951 iter = gfc_get_iterator ();
1955 m = gfc_match_iterator (iter, 0);
1956 if (m == MATCH_ERROR)
1960 gfc_check_do_variable (iter->var->symtree);
1964 m = match_io_element (k, &new);
1965 if (m == MATCH_ERROR)
1974 tail = gfc_append_code (tail, new);
1976 if (gfc_match_char (',') != MATCH_YES)
1985 if (gfc_match_char (')') != MATCH_YES)
1988 new = gfc_get_code ();
1990 new->ext.iterator = iter;
1992 new->block = gfc_get_code ();
1993 new->block->op = EXEC_DO;
1994 new->block->next = head;
2000 gfc_error ("Syntax error in I/O iterator at %C");
2004 gfc_free_iterator (iter, 1);
2005 gfc_free_statements (head);
2006 gfc_current_locus = old_loc;
2011 /* Match a single element of an IO list, which is either a single
2012 expression or an IO Iterator. */
2015 match_io_element (io_kind k, gfc_code ** cpp)
2023 m = match_io_iterator (k, cpp);
2029 m = gfc_match_variable (&expr, 0);
2031 gfc_error ("Expected variable in READ statement at %C");
2035 m = gfc_match_expr (&expr);
2037 gfc_error ("Expected expression in %s statement at %C",
2045 if (expr->symtree->n.sym->attr.intent == INTENT_IN)
2048 ("Variable '%s' in input list at %C cannot be INTENT(IN)",
2049 expr->symtree->n.sym->name);
2054 && gfc_impure_variable (expr->symtree->n.sym)
2055 && current_dt->io_unit->ts.type == BT_CHARACTER)
2057 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2058 expr->symtree->n.sym->name);
2062 if (gfc_check_do_variable (expr->symtree))
2068 if (current_dt->io_unit->ts.type == BT_CHARACTER
2070 && current_dt->io_unit->expr_type == EXPR_VARIABLE
2071 && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
2074 ("Cannot write to internal file unit '%s' at %C inside a "
2075 "PURE procedure", current_dt->io_unit->symtree->n.sym->name);
2087 gfc_free_expr (expr);
2091 cp = gfc_get_code ();
2092 cp->op = EXEC_TRANSFER;
2100 /* Match an I/O list, building gfc_code structures as we go. */
2103 match_io_list (io_kind k, gfc_code ** head_p)
2105 gfc_code *head, *tail, *new;
2108 *head_p = head = tail = NULL;
2109 if (gfc_match_eos () == MATCH_YES)
2114 m = match_io_element (k, &new);
2115 if (m == MATCH_ERROR)
2120 tail = gfc_append_code (tail, new);
2124 if (gfc_match_eos () == MATCH_YES)
2126 if (gfc_match_char (',') != MATCH_YES)
2134 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2137 gfc_free_statements (head);
2142 /* Attach the data transfer end node. */
2145 terminate_io (gfc_code * io_code)
2149 if (io_code == NULL)
2152 c = gfc_get_code ();
2153 c->op = EXEC_DT_END;
2155 /* Point to structure that is already there */
2156 c->ext.dt = new_st.ext.dt;
2157 gfc_append_code (io_code, c);
2161 /* Match a READ, WRITE or PRINT statement. */
2164 match_io (io_kind k)
2166 char name[GFC_MAX_SYMBOL_LEN + 1];
2176 current_dt = dt = gfc_getmem (sizeof (gfc_dt));
2177 if (gfc_match_char ('(') == MATCH_NO)
2179 where = gfc_current_locus;
2182 else if (k == M_PRINT)
2184 /* Treat the non-standard case of PRINT namelist. */
2185 if ((gfc_current_form == FORM_FIXED || gfc_peek_char () == ' ')
2186 && gfc_match_name (name) == MATCH_YES)
2188 gfc_find_symbol (name, NULL, 1, &sym);
2189 if (sym && sym->attr.flavor == FL_NAMELIST)
2191 if (gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
2192 "%C is an extension") == FAILURE)
2197 if (gfc_match_eos () == MATCH_NO)
2199 gfc_error ("Namelist followed by I/O list at %C");
2204 dt->io_unit = default_unit (k);
2209 gfc_current_locus = where;
2213 if (gfc_current_form == FORM_FREE)
2215 c = gfc_peek_char();
2216 if (c != ' ' && c != '*' && c != '\'' && c != '"')
2223 m = match_dt_format (dt);
2224 if (m == MATCH_ERROR)
2230 dt->io_unit = default_unit (k);
2234 /* Match a control list */
2235 if (match_dt_element (k, dt) == MATCH_YES)
2237 if (match_dt_unit (k, dt) != MATCH_YES)
2240 if (gfc_match_char (')') == MATCH_YES)
2242 if (gfc_match_char (',') != MATCH_YES)
2245 m = match_dt_element (k, dt);
2248 if (m == MATCH_ERROR)
2251 m = match_dt_format (dt);
2254 if (m == MATCH_ERROR)
2257 where = gfc_current_locus;
2259 m = gfc_match_name (name);
2262 gfc_find_symbol (name, NULL, 1, &sym);
2263 if (sym && sym->attr.flavor == FL_NAMELIST)
2266 if (k == M_READ && check_namelist (sym))
2275 gfc_current_locus = where;
2277 goto loop; /* No matches, try regular elements */
2280 if (gfc_match_char (')') == MATCH_YES)
2282 if (gfc_match_char (',') != MATCH_YES)
2288 m = match_dt_element (k, dt);
2291 if (m == MATCH_ERROR)
2294 if (gfc_match_char (')') == MATCH_YES)
2296 if (gfc_match_char (',') != MATCH_YES)
2301 /* Optional leading comma (non-standard). */
2303 && gfc_match_char (',') == MATCH_YES
2305 && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before output "
2306 "item list at %C is an extension") == FAILURE)
2310 if (gfc_match_eos () != MATCH_YES)
2312 if (comma_flag && gfc_match_char (',') != MATCH_YES)
2314 gfc_error ("Expected comma in I/O list at %C");
2319 m = match_io_list (k, &io_code);
2320 if (m == MATCH_ERROR)
2326 /* A full IO statement has been matched. */
2327 if (dt->io_unit->expr_type == EXPR_VARIABLE
2329 && dt->io_unit->ts.type == BT_CHARACTER
2330 && dt->io_unit->symtree->n.sym->attr.intent == INTENT_IN)
2332 gfc_error ("Internal file '%s' at %L is INTENT(IN)",
2333 dt->io_unit->symtree->n.sym->name, &dt->io_unit->where);
2338 expr = dt->format_expr;
2340 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
2341 check_format_string (expr);
2344 && (k == M_READ || k == M_WRITE)
2345 && dt->io_unit->ts.type != BT_CHARACTER)
2348 ("io-unit in %s statement at %C must be an internal file in a "
2349 "PURE procedure", io_kind_name (k));
2354 new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
2356 new_st.next = io_code;
2358 terminate_io (io_code);
2363 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2373 gfc_match_read (void)
2375 return match_io (M_READ);
2379 gfc_match_write (void)
2381 return match_io (M_WRITE);
2385 gfc_match_print (void)
2389 m = match_io (M_PRINT);
2393 if (gfc_pure (NULL))
2395 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
2403 /* Free a gfc_inquire structure. */
2406 gfc_free_inquire (gfc_inquire * inquire)
2409 if (inquire == NULL)
2412 gfc_free_expr (inquire->unit);
2413 gfc_free_expr (inquire->file);
2414 gfc_free_expr (inquire->iomsg);
2415 gfc_free_expr (inquire->iostat);
2416 gfc_free_expr (inquire->exist);
2417 gfc_free_expr (inquire->opened);
2418 gfc_free_expr (inquire->number);
2419 gfc_free_expr (inquire->named);
2420 gfc_free_expr (inquire->name);
2421 gfc_free_expr (inquire->access);
2422 gfc_free_expr (inquire->sequential);
2423 gfc_free_expr (inquire->direct);
2424 gfc_free_expr (inquire->form);
2425 gfc_free_expr (inquire->formatted);
2426 gfc_free_expr (inquire->unformatted);
2427 gfc_free_expr (inquire->recl);
2428 gfc_free_expr (inquire->nextrec);
2429 gfc_free_expr (inquire->blank);
2430 gfc_free_expr (inquire->position);
2431 gfc_free_expr (inquire->action);
2432 gfc_free_expr (inquire->read);
2433 gfc_free_expr (inquire->write);
2434 gfc_free_expr (inquire->readwrite);
2435 gfc_free_expr (inquire->delim);
2436 gfc_free_expr (inquire->pad);
2437 gfc_free_expr (inquire->iolength);
2443 /* Match an element of an INQUIRE statement. */
2445 #define RETM if (m != MATCH_NO) return m;
2448 match_inquire_element (gfc_inquire * inquire)
2452 m = match_etag (&tag_unit, &inquire->unit);
2453 RETM m = match_etag (&tag_file, &inquire->file);
2454 RETM m = match_ltag (&tag_err, &inquire->err);
2455 RETM m = match_out_tag (&tag_iomsg, &inquire->iomsg);
2456 RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
2457 RETM m = match_vtag (&tag_exist, &inquire->exist);
2458 RETM m = match_vtag (&tag_opened, &inquire->opened);
2459 RETM m = match_vtag (&tag_named, &inquire->named);
2460 RETM m = match_vtag (&tag_name, &inquire->name);
2461 RETM m = match_out_tag (&tag_number, &inquire->number);
2462 RETM m = match_vtag (&tag_s_access, &inquire->access);
2463 RETM m = match_vtag (&tag_sequential, &inquire->sequential);
2464 RETM m = match_vtag (&tag_direct, &inquire->direct);
2465 RETM m = match_vtag (&tag_s_form, &inquire->form);
2466 RETM m = match_vtag (&tag_formatted, &inquire->formatted);
2467 RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
2468 RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
2469 RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
2470 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
2471 RETM m = match_vtag (&tag_s_position, &inquire->position);
2472 RETM m = match_vtag (&tag_s_action, &inquire->action);
2473 RETM m = match_vtag (&tag_read, &inquire->read);
2474 RETM m = match_vtag (&tag_write, &inquire->write);
2475 RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
2476 RETM m = match_vtag (&tag_s_delim, &inquire->delim);
2477 RETM m = match_vtag (&tag_s_pad, &inquire->pad);
2478 RETM m = match_vtag (&tag_iolength, &inquire->iolength);
2479 RETM return MATCH_NO;
2486 gfc_match_inquire (void)
2488 gfc_inquire *inquire;
2493 m = gfc_match_char ('(');
2497 inquire = gfc_getmem (sizeof (gfc_inquire));
2499 loc = gfc_current_locus;
2501 m = match_inquire_element (inquire);
2502 if (m == MATCH_ERROR)
2506 m = gfc_match_expr (&inquire->unit);
2507 if (m == MATCH_ERROR)
2513 /* See if we have the IOLENGTH form of the inquire statement. */
2514 if (inquire->iolength != NULL)
2516 if (gfc_match_char (')') != MATCH_YES)
2519 m = match_io_list (M_INQUIRE, &code);
2520 if (m == MATCH_ERROR)
2525 terminate_io (code);
2527 new_st.op = EXEC_IOLENGTH;
2528 new_st.expr = inquire->iolength;
2529 new_st.ext.inquire = inquire;
2531 if (gfc_pure (NULL))
2533 gfc_free_statements (code);
2534 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
2542 /* At this point, we have the non-IOLENGTH inquire statement. */
2545 if (gfc_match_char (')') == MATCH_YES)
2547 if (gfc_match_char (',') != MATCH_YES)
2550 m = match_inquire_element (inquire);
2551 if (m == MATCH_ERROR)
2556 if (inquire->iolength != NULL)
2558 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
2563 if (gfc_match_eos () != MATCH_YES)
2566 if (inquire->unit != NULL && inquire->file != NULL)
2568 gfc_error ("INQUIRE statement at %L cannot contain both FILE and"
2569 " UNIT specifiers", &loc);
2573 if (inquire->unit == NULL && inquire->file == NULL)
2575 gfc_error ("INQUIRE statement at %L requires either FILE or"
2576 " UNIT specifier", &loc);
2580 if (gfc_pure (NULL))
2582 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
2586 new_st.op = EXEC_INQUIRE;
2587 new_st.ext.inquire = inquire;
2591 gfc_syntax_error (ST_INQUIRE);
2594 gfc_free_inquire (inquire);
2599 /* Resolve everything in a gfc_inquire structure. */
2602 gfc_resolve_inquire (gfc_inquire * inquire)
2605 RESOLVE_TAG (&tag_unit, inquire->unit);
2606 RESOLVE_TAG (&tag_file, inquire->file);
2607 RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
2608 RESOLVE_TAG (&tag_iostat, inquire->iostat);
2609 RESOLVE_TAG (&tag_exist, inquire->exist);
2610 RESOLVE_TAG (&tag_opened, inquire->opened);
2611 RESOLVE_TAG (&tag_number, inquire->number);
2612 RESOLVE_TAG (&tag_named, inquire->named);
2613 RESOLVE_TAG (&tag_name, inquire->name);
2614 RESOLVE_TAG (&tag_s_access, inquire->access);
2615 RESOLVE_TAG (&tag_sequential, inquire->sequential);
2616 RESOLVE_TAG (&tag_direct, inquire->direct);
2617 RESOLVE_TAG (&tag_s_form, inquire->form);
2618 RESOLVE_TAG (&tag_formatted, inquire->formatted);
2619 RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
2620 RESOLVE_TAG (&tag_s_recl, inquire->recl);
2621 RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
2622 RESOLVE_TAG (&tag_s_blank, inquire->blank);
2623 RESOLVE_TAG (&tag_s_position, inquire->position);
2624 RESOLVE_TAG (&tag_s_action, inquire->action);
2625 RESOLVE_TAG (&tag_read, inquire->read);
2626 RESOLVE_TAG (&tag_write, inquire->write);
2627 RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
2628 RESOLVE_TAG (&tag_s_delim, inquire->delim);
2629 RESOLVE_TAG (&tag_s_pad, inquire->pad);
2630 RESOLVE_TAG (&tag_iolength, inquire->iolength);
2632 if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)