1 /* Deal with I/O statements & related stuff.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 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, 59 Temple Place - Suite 330, 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_iostat = {"IOSTAT", " iostat = %v", BT_INTEGER},
57 tag_size = {"SIZE", " size = %v", BT_INTEGER},
58 tag_exist = {"EXIST", " exist = %v", BT_LOGICAL},
59 tag_opened = {"OPENED", " opened = %v", BT_LOGICAL},
60 tag_named = {"NAMED", " named = %v", BT_LOGICAL},
61 tag_name = {"NAME", " name = %v", BT_CHARACTER},
62 tag_number = {"NUMBER", " number = %v", BT_INTEGER},
63 tag_s_access = {"ACCESS", " access = %v", BT_CHARACTER},
64 tag_sequential = {"SEQUENTIAL", " sequential = %v", BT_CHARACTER},
65 tag_direct = {"DIRECT", " direct = %v", BT_CHARACTER},
66 tag_s_form = {"FORM", " form = %v", BT_CHARACTER},
67 tag_formatted = {"FORMATTED", " formatted = %v", BT_CHARACTER},
68 tag_unformatted = {"UNFORMATTED", " unformatted = %v", BT_CHARACTER},
69 tag_s_recl = {"RECL", " recl = %v", BT_INTEGER},
70 tag_nextrec = {"NEXTREC", " nextrec = %v", BT_INTEGER},
71 tag_s_blank = {"BLANK", " blank = %v", BT_CHARACTER},
72 tag_s_position = {"POSITION", " position = %v", BT_CHARACTER},
73 tag_s_action = {"ACTION", " action = %v", BT_CHARACTER},
74 tag_read = {"READ", " read = %v", BT_CHARACTER},
75 tag_write = {"WRITE", " write = %v", BT_CHARACTER},
76 tag_readwrite = {"READWRITE", " readwrite = %v", BT_CHARACTER},
77 tag_s_delim = {"DELIM", " delim = %v", BT_CHARACTER},
78 tag_s_pad = {"PAD", " pad = %v", BT_CHARACTER},
79 tag_iolength = {"IOLENGTH", " iolength = %v", BT_INTEGER},
80 tag_err = {"ERR", " err = %l", BT_UNKNOWN},
81 tag_end = {"END", " end = %l", BT_UNKNOWN},
82 tag_eor = {"EOR", " eor = %l", BT_UNKNOWN};
84 static gfc_dt *current_dt;
86 #define RESOLVE_TAG(x, y) if (resolve_tag(x, y) == FAILURE) return FAILURE;
89 /**************** Fortran 95 FORMAT parser *****************/
91 /* FORMAT tokens returned by format_lex(). */
94 FMT_NONE, FMT_UNKNOWN, FMT_SIGNED_INT, FMT_ZERO, FMT_POSINT, FMT_PERIOD,
95 FMT_COMMA, FMT_COLON, FMT_SLASH, FMT_DOLLAR, FMT_POS, FMT_LPAREN,
96 FMT_RPAREN, FMT_X, FMT_SIGN, FMT_BLANK, FMT_CHAR, FMT_P, FMT_IBOZ, FMT_F,
97 FMT_E, FMT_EXT, FMT_G, FMT_L, FMT_A, FMT_D, FMT_H, FMT_END
101 /* Local variables for checking format strings. The saved_token is
102 used to back up by a single format token during the parsing
104 static char *format_string;
105 static int format_length, use_last_char;
107 static format_token saved_token;
110 { MODE_STRING, MODE_FORMAT, MODE_COPY }
114 /* Return the next character in the format string. */
117 next_char (int in_string)
129 if (mode == MODE_STRING)
130 c = *format_string++;
133 c = gfc_next_char_literal (in_string);
137 if (mode == MODE_COPY)
138 *format_string++ = c;
146 /* Back up one character position. Only works once. */
155 static int value = 0;
157 /* Simple lexical analyzer for getting the next token in a FORMAT
168 if (saved_token != FMT_NONE)
171 saved_token = FMT_NONE;
179 while (gfc_is_whitespace (c));
200 value = 10 * value + c - '0';
209 token = FMT_SIGNED_INT;
232 value = 10 * value + c - '0';
237 token = zflag ? FMT_ZERO : FMT_POSINT;
262 if (c != 'L' && c != 'R')
282 if (c != 'P' && c != 'S')
290 if (c == 'N' || c == 'Z')
352 if (c == 'N' || c == 'S')
395 /* Check a format statement. The format string, either from a FORMAT
396 statement or a constant in an I/O statement has already been parsed
397 by itself, and we are checking it for validity. The dual origin
398 means that the warning message is a little less than great. */
403 const char *posint_required = "Positive width required";
404 const char *period_required = "Period required";
405 const char *nonneg_required = "Nonnegative width required";
406 const char *unexpected_element = "Unexpected element";
407 const char *unexpected_end = "Unexpected end of format string";
416 saved_token = FMT_NONE;
424 error = "Missing leading left parenthesis";
430 goto finished; /* Empty format is legal */
434 /* In this state, the next thing has to be a format item. */
457 /* Signed integer can only precede a P format. */
461 error = "Expected P edit descriptor";
468 /* P requires a prior number. */
469 error = "P descriptor requires leading scale factor";
473 /* X requires a prior number if we're being pedantic. */
474 if (gfc_notify_std (GFC_STD_GNU, "Extension: X descriptor "
475 "requires leading space count at %C")
485 goto extension_optional_comma;
493 if (t != FMT_RPAREN || level > 0)
495 error = "$ must the last specifier";
516 error = unexpected_end;
520 error = unexpected_element;
525 /* In this state, t must currently be a data descriptor.
526 Deal with things that can/must follow the descriptor. */
540 error = "Repeat count cannot follow P descriptor";
555 error = posint_required;
571 error = posint_required;
578 error = period_required;
583 if (u != FMT_ZERO && u != FMT_POSINT)
585 error = nonneg_required;
592 /* Look for optional exponent. */
603 error = "Positive exponent width required";
612 if (t != FMT_ZERO && t != FMT_POSINT)
614 error = nonneg_required;
621 error = period_required;
626 if (t != FMT_ZERO && t != FMT_POSINT)
628 error = nonneg_required;
635 if(mode == MODE_STRING)
637 format_string += value;
638 format_length -= value;
652 if (t != FMT_ZERO && t != FMT_POSINT)
654 error = nonneg_required;
666 if (t != FMT_ZERO && t != FMT_POSINT)
668 error = nonneg_required;
676 error = unexpected_element;
681 /* Between a descriptor and what comes next. */
700 error = unexpected_end;
704 error = "Missing comma";
709 /* Optional comma is a weird between state where we've just finished
710 reading a colon, slash or P descriptor. */
724 /* Assume that we have another format item. */
731 extension_optional_comma:
732 /* As a GNU extension, permit a missing comma after a string literal. */
750 error = unexpected_end;
754 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
764 /* Something went wrong. If the format we're checking is a string,
765 generate a warning, since the program is correct. If the format
766 is in a FORMAT statement, this messes up parsing, which is an
768 if (mode != MODE_STRING)
769 gfc_error ("%s in format string at %C", error);
772 gfc_warning ("%s in format string at %C", error);
774 /* TODO: More elaborate measures are needed to show where a problem
775 is within a format string that has been calculated. */
785 /* Given an expression node that is a constant string, see if it looks
786 like a format string. */
789 check_format_string (gfc_expr * e)
793 format_string = e->value.character.string;
798 /************ Fortran 95 I/O statement matchers *************/
800 /* Match a FORMAT statement. This amounts to actually parsing the
801 format descriptors in order to correctly locate the end of the
805 gfc_match_format (void)
810 if (gfc_statement_label == NULL)
812 gfc_error ("Missing format label at %C");
815 gfc_gobble_whitespace ();
820 start = gfc_current_locus;
822 if (check_format () == FAILURE)
825 if (gfc_match_eos () != MATCH_YES)
827 gfc_syntax_error (ST_FORMAT);
831 /* The label doesn't get created until after the statement is done
832 being matched, so we have to leave the string for later. */
834 gfc_current_locus = start; /* Back to the beginning */
837 new_st.op = EXEC_NOP;
840 e->expr_type = EXPR_CONSTANT;
841 e->ts.type = BT_CHARACTER;
842 e->ts.kind = gfc_default_character_kind;
844 e->value.character.string = format_string = gfc_getmem(format_length+1);
845 e->value.character.length = format_length;
846 gfc_statement_label->format = e;
849 check_format (); /* Guaranteed to succeed */
850 gfc_match_eos (); /* Guaranteed to succeed */
856 /* Match an expression I/O tag of some sort. */
859 match_etag (const io_tag * tag, gfc_expr ** v)
864 m = gfc_match (tag->spec, &result);
870 gfc_error ("Duplicate %s specification at %C", tag->name);
871 gfc_free_expr (result);
880 /* Match a variable I/O tag of some sort. */
883 match_vtag (const io_tag * tag, gfc_expr ** v)
888 m = gfc_match (tag->spec, &result);
894 gfc_error ("Duplicate %s specification at %C", tag->name);
895 gfc_free_expr (result);
899 if (result->symtree->n.sym->attr.intent == INTENT_IN)
901 gfc_error ("Variable tag cannot be INTENT(IN) at %C");
902 gfc_free_expr (result);
906 if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
908 gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
909 gfc_free_expr (result);
918 /* Match I/O tags that cause variables to become redefined. */
921 match_out_tag(const io_tag *tag, gfc_expr **result)
925 m = match_vtag(tag, result);
927 gfc_check_do_variable((*result)->symtree);
933 /* Match a label I/O tag. */
936 match_ltag (const io_tag * tag, gfc_st_label ** label)
942 m = gfc_match (tag->spec, label);
943 if (m == MATCH_YES && old != 0)
945 gfc_error ("Duplicate %s label specification at %C", tag->name);
953 /* Do expression resolution and type-checking on an expression tag. */
956 resolve_tag (const io_tag * tag, gfc_expr * e)
962 if (gfc_resolve_expr (e) == FAILURE)
965 if (e->ts.type != tag->type)
967 /* Format label can be integer varibale. */
968 if (tag != &tag_format || e->ts.type != BT_INTEGER)
970 gfc_error ("%s tag at %L must be of type %s", tag->name, &e->where,
971 gfc_basic_typename (tag->type));
976 if (tag == &tag_format)
978 if (e->rank != 1 && e->rank != 0)
980 gfc_error ("FORMAT tag at %L cannot be array of strings",
989 gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
998 /* Match a single tag of an OPEN statement. */
1001 match_open_element (gfc_open * open)
1005 m = match_etag (&tag_unit, &open->unit);
1008 m = match_out_tag (&tag_iostat, &open->iostat);
1011 m = match_etag (&tag_file, &open->file);
1014 m = match_etag (&tag_status, &open->status);
1017 m = match_etag (&tag_e_access, &open->access);
1020 m = match_etag (&tag_e_form, &open->form);
1023 m = match_etag (&tag_e_recl, &open->recl);
1026 m = match_etag (&tag_e_blank, &open->blank);
1029 m = match_etag (&tag_e_position, &open->position);
1032 m = match_etag (&tag_e_action, &open->action);
1035 m = match_etag (&tag_e_delim, &open->delim);
1038 m = match_etag (&tag_e_pad, &open->pad);
1041 m = match_ltag (&tag_err, &open->err);
1049 /* Free the gfc_open structure and all the expressions it contains. */
1052 gfc_free_open (gfc_open * open)
1058 gfc_free_expr (open->unit);
1059 gfc_free_expr (open->iostat);
1060 gfc_free_expr (open->file);
1061 gfc_free_expr (open->status);
1062 gfc_free_expr (open->access);
1063 gfc_free_expr (open->form);
1064 gfc_free_expr (open->recl);
1065 gfc_free_expr (open->blank);
1066 gfc_free_expr (open->position);
1067 gfc_free_expr (open->action);
1068 gfc_free_expr (open->delim);
1069 gfc_free_expr (open->pad);
1075 /* Resolve everything in a gfc_open structure. */
1078 gfc_resolve_open (gfc_open * open)
1081 RESOLVE_TAG (&tag_unit, open->unit);
1082 RESOLVE_TAG (&tag_iostat, open->iostat);
1083 RESOLVE_TAG (&tag_file, open->file);
1084 RESOLVE_TAG (&tag_status, open->status);
1085 RESOLVE_TAG (&tag_e_form, open->form);
1086 RESOLVE_TAG (&tag_e_recl, open->recl);
1088 RESOLVE_TAG (&tag_e_blank, open->blank);
1089 RESOLVE_TAG (&tag_e_position, open->position);
1090 RESOLVE_TAG (&tag_e_action, open->action);
1091 RESOLVE_TAG (&tag_e_delim, open->delim);
1092 RESOLVE_TAG (&tag_e_pad, open->pad);
1094 if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1101 /* Match an OPEN statmement. */
1104 gfc_match_open (void)
1109 m = gfc_match_char ('(');
1113 open = gfc_getmem (sizeof (gfc_open));
1115 m = match_open_element (open);
1117 if (m == MATCH_ERROR)
1121 m = gfc_match_expr (&open->unit);
1124 if (m == MATCH_ERROR)
1130 if (gfc_match_char (')') == MATCH_YES)
1132 if (gfc_match_char (',') != MATCH_YES)
1135 m = match_open_element (open);
1136 if (m == MATCH_ERROR)
1142 if (gfc_match_eos () == MATCH_NO)
1145 if (gfc_pure (NULL))
1147 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1151 new_st.op = EXEC_OPEN;
1152 new_st.ext.open = open;
1156 gfc_syntax_error (ST_OPEN);
1159 gfc_free_open (open);
1164 /* Free a gfc_close structure an all its expressions. */
1167 gfc_free_close (gfc_close * close)
1173 gfc_free_expr (close->unit);
1174 gfc_free_expr (close->iostat);
1175 gfc_free_expr (close->status);
1181 /* Match elements of a CLOSE statment. */
1184 match_close_element (gfc_close * close)
1188 m = match_etag (&tag_unit, &close->unit);
1191 m = match_etag (&tag_status, &close->status);
1194 m = match_out_tag (&tag_iostat, &close->iostat);
1197 m = match_ltag (&tag_err, &close->err);
1205 /* Match a CLOSE statement. */
1208 gfc_match_close (void)
1213 m = gfc_match_char ('(');
1217 close = gfc_getmem (sizeof (gfc_close));
1219 m = match_close_element (close);
1221 if (m == MATCH_ERROR)
1225 m = gfc_match_expr (&close->unit);
1228 if (m == MATCH_ERROR)
1234 if (gfc_match_char (')') == MATCH_YES)
1236 if (gfc_match_char (',') != MATCH_YES)
1239 m = match_close_element (close);
1240 if (m == MATCH_ERROR)
1246 if (gfc_match_eos () == MATCH_NO)
1249 if (gfc_pure (NULL))
1251 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1255 new_st.op = EXEC_CLOSE;
1256 new_st.ext.close = close;
1260 gfc_syntax_error (ST_CLOSE);
1263 gfc_free_close (close);
1268 /* Resolve everything in a gfc_close structure. */
1271 gfc_resolve_close (gfc_close * close)
1274 RESOLVE_TAG (&tag_unit, close->unit);
1275 RESOLVE_TAG (&tag_iostat, close->iostat);
1276 RESOLVE_TAG (&tag_status, close->status);
1278 if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
1285 /* Free a gfc_filepos structure. */
1288 gfc_free_filepos (gfc_filepos * fp)
1291 gfc_free_expr (fp->unit);
1292 gfc_free_expr (fp->iostat);
1297 /* Match elements of a REWIND, BACKSPACE or ENDFILE statement. */
1300 match_file_element (gfc_filepos * fp)
1304 m = match_etag (&tag_unit, &fp->unit);
1307 m = match_out_tag (&tag_iostat, &fp->iostat);
1310 m = match_ltag (&tag_err, &fp->err);
1318 /* Match the second half of the file-positioning statements, REWIND,
1319 BACKSPACE or ENDFILE. */
1322 match_filepos (gfc_statement st, gfc_exec_op op)
1327 fp = gfc_getmem (sizeof (gfc_filepos));
1329 if (gfc_match_char ('(') == MATCH_NO)
1331 m = gfc_match_expr (&fp->unit);
1332 if (m == MATCH_ERROR)
1340 m = match_file_element (fp);
1341 if (m == MATCH_ERROR)
1345 m = gfc_match_expr (&fp->unit);
1346 if (m == MATCH_ERROR)
1354 if (gfc_match_char (')') == MATCH_YES)
1356 if (gfc_match_char (',') != MATCH_YES)
1359 m = match_file_element (fp);
1360 if (m == MATCH_ERROR)
1367 if (gfc_match_eos () != MATCH_YES)
1370 if (gfc_pure (NULL))
1372 gfc_error ("%s statement not allowed in PURE procedure at %C",
1373 gfc_ascii_statement (st));
1379 new_st.ext.filepos = fp;
1383 gfc_syntax_error (st);
1386 gfc_free_filepos (fp);
1392 gfc_resolve_filepos (gfc_filepos * fp)
1395 RESOLVE_TAG (&tag_unit, fp->unit);
1396 if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
1403 /* Match the file positioning statements: ENDFILE, BACKSPACE or
1407 gfc_match_endfile (void)
1410 return match_filepos (ST_END_FILE, EXEC_ENDFILE);
1414 gfc_match_backspace (void)
1417 return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
1421 gfc_match_rewind (void)
1424 return match_filepos (ST_REWIND, EXEC_REWIND);
1428 /******************** Data Transfer Statments *********************/
1431 { M_READ, M_WRITE, M_PRINT, M_INQUIRE }
1435 /* Return a default unit number. */
1438 default_unit (io_kind k)
1447 return gfc_int_expr (unit);
1451 /* Match a unit specification for a data transfer statement. */
1454 match_dt_unit (io_kind k, gfc_dt * dt)
1458 if (gfc_match_char ('*') == MATCH_YES)
1460 if (dt->io_unit != NULL)
1463 dt->io_unit = default_unit (k);
1467 if (gfc_match_expr (&e) == MATCH_YES)
1469 if (dt->io_unit != NULL)
1482 gfc_error ("Duplicate UNIT specification at %C");
1487 /* Match a format specification. */
1490 match_dt_format (gfc_dt * dt)
1494 gfc_st_label *label;
1496 where = gfc_current_locus;
1498 if (gfc_match_char ('*') == MATCH_YES)
1500 if (dt->format_expr != NULL || dt->format_label != NULL)
1503 dt->format_label = &format_asterisk;
1507 if (gfc_match_st_label (&label, 0) == MATCH_YES)
1509 if (dt->format_expr != NULL || dt->format_label != NULL)
1511 gfc_free_st_label (label);
1515 if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
1518 dt->format_label = label;
1522 if (gfc_match_expr (&e) == MATCH_YES)
1524 if (dt->format_expr != NULL || dt->format_label != NULL)
1529 if (e->ts.type == BT_INTEGER && e->rank == 0)
1530 e->symtree->n.sym->attr.assign = 1;
1532 dt->format_expr = e;
1536 gfc_current_locus = where; /* The only case where we have to restore */
1541 gfc_error ("Duplicate format specification at %C");
1546 /* Traverse a namelist that is part of a READ statement to make sure
1547 that none of the variables in the namelist are INTENT(IN). Returns
1548 nonzero if we find such a variable. */
1551 check_namelist (gfc_symbol * sym)
1555 for (p = sym->namelist; p; p = p->next)
1556 if (p->sym->attr.intent == INTENT_IN)
1558 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
1559 p->sym->name, sym->name);
1567 /* Match a single data transfer element. */
1570 match_dt_element (io_kind k, gfc_dt * dt)
1572 char name[GFC_MAX_SYMBOL_LEN + 1];
1576 if (gfc_match (" unit =") == MATCH_YES)
1578 m = match_dt_unit (k, dt);
1583 if (gfc_match (" fmt =") == MATCH_YES)
1585 m = match_dt_format (dt);
1590 if (gfc_match (" nml = %n", name) == MATCH_YES)
1592 if (dt->namelist != NULL)
1594 gfc_error ("Duplicate NML specification at %C");
1598 if (gfc_find_symbol (name, NULL, 1, &sym))
1601 if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
1603 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
1604 sym != NULL ? sym->name : name);
1609 if (k == M_READ && check_namelist (sym))
1615 m = match_etag (&tag_rec, &dt->rec);
1618 m = match_out_tag (&tag_iostat, &dt->iostat);
1621 m = match_ltag (&tag_err, &dt->err);
1624 m = match_etag (&tag_advance, &dt->advance);
1627 m = match_out_tag (&tag_size, &dt->size);
1631 m = match_ltag (&tag_end, &dt->end);
1633 dt->end_where = gfc_current_locus;
1637 m = match_ltag (&tag_eor, &dt->eor);
1639 dt->eor_where = gfc_current_locus;
1647 /* Free a data transfer structure and everything below it. */
1650 gfc_free_dt (gfc_dt * dt)
1656 gfc_free_expr (dt->io_unit);
1657 gfc_free_expr (dt->format_expr);
1658 gfc_free_expr (dt->rec);
1659 gfc_free_expr (dt->advance);
1660 gfc_free_expr (dt->iostat);
1661 gfc_free_expr (dt->size);
1667 /* Resolve everything in a gfc_dt structure. */
1670 gfc_resolve_dt (gfc_dt * dt)
1674 RESOLVE_TAG (&tag_format, dt->format_expr);
1675 RESOLVE_TAG (&tag_rec, dt->rec);
1676 RESOLVE_TAG (&tag_advance, dt->advance);
1677 RESOLVE_TAG (&tag_iostat, dt->iostat);
1678 RESOLVE_TAG (&tag_size, dt->size);
1681 if (gfc_resolve_expr (e) == SUCCESS
1682 && (e->ts.type != BT_INTEGER
1683 && (e->ts.type != BT_CHARACTER
1684 || e->expr_type != EXPR_VARIABLE)))
1687 ("UNIT specification at %L must be an INTEGER expression or a "
1688 "CHARACTER variable", &e->where);
1692 /* Sanity checks on data transfer statements. */
1693 if (e->ts.type == BT_CHARACTER)
1695 if (dt->rec != NULL)
1697 gfc_error ("REC tag at %L is incompatible with internal file",
1702 if (dt->namelist != NULL)
1704 gfc_error ("Internal file at %L is incompatible with namelist",
1705 &dt->io_unit->where);
1709 if (dt->advance != NULL)
1711 gfc_error ("ADVANCE tag at %L is incompatible with internal file",
1712 &dt->advance->where);
1717 if (dt->rec != NULL)
1719 if (dt->end != NULL)
1721 gfc_error ("REC tag at %L is incompatible with END tag",
1726 if (dt->format_label == &format_asterisk)
1729 ("END tag at %L is incompatible with list directed format (*)",
1734 if (dt->namelist != NULL)
1736 gfc_error ("REC tag at %L is incompatible with namelist",
1742 if (dt->advance != NULL && dt->format_label == &format_asterisk)
1744 gfc_error ("ADVANCE tag at %L is incompatible with list directed "
1745 "format (*)", &dt->advance->where);
1749 if (dt->eor != 0 && dt->advance == NULL)
1751 gfc_error ("EOR tag at %L requires an ADVANCE tag", &dt->eor_where);
1755 if (dt->size != NULL && dt->advance == NULL)
1757 gfc_error ("SIZE tag at %L requires an ADVANCE tag", &dt->size->where);
1761 /* TODO: Make sure the ADVANCE tag is 'yes' or 'no' if it is a string
1764 if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
1767 if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
1770 if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
1773 /* Check the format label actually exists. */
1774 if (dt->format_label && dt->format_label != &format_asterisk
1775 && dt->format_label->defined == ST_LABEL_UNKNOWN)
1777 gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
1778 &dt->format_label->where);
1785 /* Given an io_kind, return its name. */
1788 io_kind_name (io_kind k)
1807 gfc_internal_error ("io_kind_name(): bad I/O-kind");
1814 /* Match an IO iteration statement of the form:
1816 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
1818 which is equivalent to a single IO element. This function is
1819 mutually recursive with match_io_element(). */
1821 static match match_io_element (io_kind k, gfc_code **);
1824 match_io_iterator (io_kind k, gfc_code ** result)
1826 gfc_code *head, *tail, *new;
1834 old_loc = gfc_current_locus;
1836 if (gfc_match_char ('(') != MATCH_YES)
1839 m = match_io_element (k, &head);
1842 if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
1848 /* Can't be anything but an IO iterator. Build a list. */
1849 iter = gfc_get_iterator ();
1853 m = gfc_match_iterator (iter, 0);
1854 if (m == MATCH_ERROR)
1858 gfc_check_do_variable (iter->var->symtree);
1862 m = match_io_element (k, &new);
1863 if (m == MATCH_ERROR)
1872 tail = gfc_append_code (tail, new);
1874 if (gfc_match_char (',') != MATCH_YES)
1883 if (gfc_match_char (')') != MATCH_YES)
1886 new = gfc_get_code ();
1888 new->ext.iterator = iter;
1890 new->block = gfc_get_code ();
1891 new->block->op = EXEC_DO;
1892 new->block->next = head;
1898 gfc_error ("Syntax error in I/O iterator at %C");
1902 gfc_free_iterator (iter, 1);
1903 gfc_free_statements (head);
1904 gfc_current_locus = old_loc;
1909 /* Match a single element of an IO list, which is either a single
1910 expression or an IO Iterator. */
1913 match_io_element (io_kind k, gfc_code ** cpp)
1921 m = match_io_iterator (k, cpp);
1927 m = gfc_match_variable (&expr, 0);
1929 gfc_error ("Expected variable in READ statement at %C");
1933 m = gfc_match_expr (&expr);
1935 gfc_error ("Expected expression in %s statement at %C",
1943 if (expr->symtree->n.sym->attr.intent == INTENT_IN)
1946 ("Variable '%s' in input list at %C cannot be INTENT(IN)",
1947 expr->symtree->n.sym->name);
1952 && gfc_impure_variable (expr->symtree->n.sym)
1953 && current_dt->io_unit->ts.type == BT_CHARACTER)
1955 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
1956 expr->symtree->n.sym->name);
1960 if (gfc_check_do_variable (expr->symtree))
1966 if (current_dt->io_unit->ts.type == BT_CHARACTER
1968 && current_dt->io_unit->expr_type == EXPR_VARIABLE
1969 && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
1972 ("Cannot write to internal file unit '%s' at %C inside a "
1973 "PURE procedure", current_dt->io_unit->symtree->n.sym->name);
1985 gfc_free_expr (expr);
1989 cp = gfc_get_code ();
1990 cp->op = EXEC_TRANSFER;
1998 /* Match an I/O list, building gfc_code structures as we go. */
2001 match_io_list (io_kind k, gfc_code ** head_p)
2003 gfc_code *head, *tail, *new;
2006 *head_p = head = tail = NULL;
2007 if (gfc_match_eos () == MATCH_YES)
2012 m = match_io_element (k, &new);
2013 if (m == MATCH_ERROR)
2018 tail = gfc_append_code (tail, new);
2022 if (gfc_match_eos () == MATCH_YES)
2024 if (gfc_match_char (',') != MATCH_YES)
2032 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2035 gfc_free_statements (head);
2040 /* Attach the data transfer end node. */
2043 terminate_io (gfc_code * io_code)
2047 if (io_code == NULL)
2050 c = gfc_get_code ();
2051 c->op = EXEC_DT_END;
2053 /* Point to structure that is already there */
2054 c->ext.dt = new_st.ext.dt;
2055 gfc_append_code (io_code, c);
2059 /* Match a READ, WRITE or PRINT statement. */
2062 match_io (io_kind k)
2064 char name[GFC_MAX_SYMBOL_LEN + 1];
2074 current_dt = dt = gfc_getmem (sizeof (gfc_dt));
2076 if (gfc_match_char ('(') == MATCH_NO)
2081 if (gfc_current_form == FORM_FREE)
2083 c = gfc_peek_char();
2084 if (c != ' ' && c != '*' && c != '\'' && c != '"')
2091 m = match_dt_format (dt);
2092 if (m == MATCH_ERROR)
2098 dt->io_unit = default_unit (k);
2102 /* Match a control list */
2103 if (match_dt_element (k, dt) == MATCH_YES)
2105 if (match_dt_unit (k, dt) != MATCH_YES)
2108 if (gfc_match_char (')') == MATCH_YES)
2110 if (gfc_match_char (',') != MATCH_YES)
2113 m = match_dt_element (k, dt);
2116 if (m == MATCH_ERROR)
2119 m = match_dt_format (dt);
2122 if (m == MATCH_ERROR)
2125 where = gfc_current_locus;
2127 if (gfc_match_name (name) == MATCH_YES
2128 && !gfc_find_symbol (name, NULL, 1, &sym)
2129 && sym->attr.flavor == FL_NAMELIST)
2132 if (k == M_READ && check_namelist (sym))
2140 gfc_current_locus = where;
2142 goto loop; /* No matches, try regular elements */
2145 if (gfc_match_char (')') == MATCH_YES)
2147 if (gfc_match_char (',') != MATCH_YES)
2153 m = match_dt_element (k, dt);
2156 if (m == MATCH_ERROR)
2159 if (gfc_match_char (')') == MATCH_YES)
2161 if (gfc_match_char (',') != MATCH_YES)
2166 /* Optional leading comma (non-standard). */
2168 && gfc_match_char (',') == MATCH_YES
2170 && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before output "
2171 "item list at %C is an extension") == FAILURE)
2175 if (gfc_match_eos () != MATCH_YES)
2177 if (comma_flag && gfc_match_char (',') != MATCH_YES)
2179 gfc_error ("Expected comma in I/O list at %C");
2184 m = match_io_list (k, &io_code);
2185 if (m == MATCH_ERROR)
2191 /* A full IO statement has been matched. */
2192 if (dt->io_unit->expr_type == EXPR_VARIABLE
2194 && dt->io_unit->ts.type == BT_CHARACTER
2195 && dt->io_unit->symtree->n.sym->attr.intent == INTENT_IN)
2197 gfc_error ("Internal file '%s' at %L is INTENT(IN)",
2198 dt->io_unit->symtree->n.sym->name, &dt->io_unit->where);
2203 expr = dt->format_expr;
2205 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
2206 check_format_string (expr);
2209 && (k == M_READ || k == M_WRITE)
2210 && dt->io_unit->ts.type != BT_CHARACTER)
2213 ("io-unit in %s statement at %C must be an internal file in a "
2214 "PURE procedure", io_kind_name (k));
2219 new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
2221 new_st.next = io_code;
2223 terminate_io (io_code);
2228 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2238 gfc_match_read (void)
2240 return match_io (M_READ);
2244 gfc_match_write (void)
2246 return match_io (M_WRITE);
2250 gfc_match_print (void)
2254 m = match_io (M_PRINT);
2258 if (gfc_pure (NULL))
2260 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
2268 /* Free a gfc_inquire structure. */
2271 gfc_free_inquire (gfc_inquire * inquire)
2274 if (inquire == NULL)
2277 gfc_free_expr (inquire->unit);
2278 gfc_free_expr (inquire->file);
2279 gfc_free_expr (inquire->iostat);
2280 gfc_free_expr (inquire->exist);
2281 gfc_free_expr (inquire->opened);
2282 gfc_free_expr (inquire->number);
2283 gfc_free_expr (inquire->named);
2284 gfc_free_expr (inquire->name);
2285 gfc_free_expr (inquire->access);
2286 gfc_free_expr (inquire->sequential);
2287 gfc_free_expr (inquire->direct);
2288 gfc_free_expr (inquire->form);
2289 gfc_free_expr (inquire->formatted);
2290 gfc_free_expr (inquire->unformatted);
2291 gfc_free_expr (inquire->recl);
2292 gfc_free_expr (inquire->nextrec);
2293 gfc_free_expr (inquire->blank);
2294 gfc_free_expr (inquire->position);
2295 gfc_free_expr (inquire->action);
2296 gfc_free_expr (inquire->read);
2297 gfc_free_expr (inquire->write);
2298 gfc_free_expr (inquire->readwrite);
2299 gfc_free_expr (inquire->delim);
2300 gfc_free_expr (inquire->pad);
2301 gfc_free_expr (inquire->iolength);
2307 /* Match an element of an INQUIRE statement. */
2309 #define RETM if (m != MATCH_NO) return m;
2312 match_inquire_element (gfc_inquire * inquire)
2316 m = match_etag (&tag_unit, &inquire->unit);
2317 RETM m = match_etag (&tag_file, &inquire->file);
2318 RETM m = match_ltag (&tag_err, &inquire->err);
2319 RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
2320 RETM m = match_vtag (&tag_exist, &inquire->exist);
2321 RETM m = match_vtag (&tag_opened, &inquire->opened);
2322 RETM m = match_vtag (&tag_named, &inquire->named);
2323 RETM m = match_vtag (&tag_name, &inquire->name);
2324 RETM m = match_out_tag (&tag_number, &inquire->number);
2325 RETM m = match_vtag (&tag_s_access, &inquire->access);
2326 RETM m = match_vtag (&tag_sequential, &inquire->sequential);
2327 RETM m = match_vtag (&tag_direct, &inquire->direct);
2328 RETM m = match_vtag (&tag_s_form, &inquire->form);
2329 RETM m = match_vtag (&tag_formatted, &inquire->formatted);
2330 RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
2331 RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
2332 RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
2333 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
2334 RETM m = match_vtag (&tag_s_position, &inquire->position);
2335 RETM m = match_vtag (&tag_s_action, &inquire->action);
2336 RETM m = match_vtag (&tag_read, &inquire->read);
2337 RETM m = match_vtag (&tag_write, &inquire->write);
2338 RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
2339 RETM m = match_vtag (&tag_s_delim, &inquire->delim);
2340 RETM m = match_vtag (&tag_s_pad, &inquire->pad);
2341 RETM m = match_vtag (&tag_iolength, &inquire->iolength);
2342 RETM return MATCH_NO;
2349 gfc_match_inquire (void)
2351 gfc_inquire *inquire;
2355 m = gfc_match_char ('(');
2359 inquire = gfc_getmem (sizeof (gfc_inquire));
2361 m = match_inquire_element (inquire);
2362 if (m == MATCH_ERROR)
2366 m = gfc_match_expr (&inquire->unit);
2367 if (m == MATCH_ERROR)
2373 /* See if we have the IOLENGTH form of the inquire statement. */
2374 if (inquire->iolength != NULL)
2376 if (gfc_match_char (')') != MATCH_YES)
2379 m = match_io_list (M_INQUIRE, &code);
2380 if (m == MATCH_ERROR)
2385 terminate_io (code);
2387 new_st.op = EXEC_IOLENGTH;
2388 new_st.expr = inquire->iolength;
2389 new_st.ext.inquire = inquire;
2391 if (gfc_pure (NULL))
2393 gfc_free_statements (code);
2394 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
2402 /* At this point, we have the non-IOLENGTH inquire statement. */
2405 if (gfc_match_char (')') == MATCH_YES)
2407 if (gfc_match_char (',') != MATCH_YES)
2410 m = match_inquire_element (inquire);
2411 if (m == MATCH_ERROR)
2416 if (inquire->iolength != NULL)
2418 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
2423 if (gfc_match_eos () != MATCH_YES)
2426 if (gfc_pure (NULL))
2428 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
2432 new_st.op = EXEC_INQUIRE;
2433 new_st.ext.inquire = inquire;
2437 gfc_syntax_error (ST_INQUIRE);
2440 gfc_free_inquire (inquire);
2445 /* Resolve everything in a gfc_inquire structure. */
2448 gfc_resolve_inquire (gfc_inquire * inquire)
2451 RESOLVE_TAG (&tag_unit, inquire->unit);
2452 RESOLVE_TAG (&tag_file, inquire->file);
2453 RESOLVE_TAG (&tag_iostat, inquire->iostat);
2454 RESOLVE_TAG (&tag_exist, inquire->exist);
2455 RESOLVE_TAG (&tag_opened, inquire->opened);
2456 RESOLVE_TAG (&tag_number, inquire->number);
2457 RESOLVE_TAG (&tag_named, inquire->named);
2458 RESOLVE_TAG (&tag_name, inquire->name);
2459 RESOLVE_TAG (&tag_s_access, inquire->access);
2460 RESOLVE_TAG (&tag_sequential, inquire->sequential);
2461 RESOLVE_TAG (&tag_direct, inquire->direct);
2462 RESOLVE_TAG (&tag_s_form, inquire->form);
2463 RESOLVE_TAG (&tag_formatted, inquire->formatted);
2464 RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
2465 RESOLVE_TAG (&tag_s_recl, inquire->recl);
2466 RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
2467 RESOLVE_TAG (&tag_s_blank, inquire->blank);
2468 RESOLVE_TAG (&tag_s_position, inquire->position);
2469 RESOLVE_TAG (&tag_s_action, inquire->action);
2470 RESOLVE_TAG (&tag_read, inquire->read);
2471 RESOLVE_TAG (&tag_write, inquire->write);
2472 RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
2473 RESOLVE_TAG (&tag_s_delim, inquire->delim);
2474 RESOLVE_TAG (&tag_s_pad, inquire->pad);
2475 RESOLVE_TAG (&tag_iolength, inquire->iolength);
2477 if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)