Makefile.in: Add dummy "install-info" target.
[official-gcc.git] / gcc / fortran / io.c
blob8c17b3a8584beb3a19137a2322cccdc6dd6f6a93
1 /* Deal with I/O statements & related stuff.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
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
11 version.
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
16 for more details.
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
21 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "flags.h"
26 #include "gfortran.h"
27 #include "match.h"
28 #include "parse.h"
30 gfc_st_label
31 format_asterisk = {0, NULL, NULL, -1, ST_LABEL_FORMAT, ST_LABEL_FORMAT, NULL,
32 0, {NULL, NULL}};
34 typedef struct
36 const char *name, *spec;
37 bt type;
39 io_tag;
41 static const io_tag
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_spos = {"POSITION", " pos = %e", BT_INTEGER},
56 tag_format = {"FORMAT", NULL, BT_CHARACTER},
57 tag_iomsg = {"IOMSG", " iomsg = %e", 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_convert = {"CONVERT", " convert = %e", BT_CHARACTER},
83 tag_strm_out = {"POS", " pos = %v", BT_INTEGER},
84 tag_err = {"ERR", " err = %l", BT_UNKNOWN},
85 tag_end = {"END", " end = %l", BT_UNKNOWN},
86 tag_eor = {"EOR", " eor = %l", BT_UNKNOWN};
88 static gfc_dt *current_dt;
90 #define RESOLVE_TAG(x, y) if (resolve_tag(x, y) == FAILURE) return FAILURE;
93 /**************** Fortran 95 FORMAT parser *****************/
95 /* FORMAT tokens returned by format_lex(). */
96 typedef enum
98 FMT_NONE, FMT_UNKNOWN, FMT_SIGNED_INT, FMT_ZERO, FMT_POSINT, FMT_PERIOD,
99 FMT_COMMA, FMT_COLON, FMT_SLASH, FMT_DOLLAR, FMT_POS, FMT_LPAREN,
100 FMT_RPAREN, FMT_X, FMT_SIGN, FMT_BLANK, FMT_CHAR, FMT_P, FMT_IBOZ, FMT_F,
101 FMT_E, FMT_EXT, FMT_G, FMT_L, FMT_A, FMT_D, FMT_H, FMT_END
103 format_token;
105 /* Local variables for checking format strings. The saved_token is
106 used to back up by a single format token during the parsing
107 process. */
108 static char *format_string;
109 static int format_length, use_last_char;
111 static format_token saved_token;
113 static enum
114 { MODE_STRING, MODE_FORMAT, MODE_COPY }
115 mode;
118 /* Return the next character in the format string. */
120 static char
121 next_char (int in_string)
123 static char c;
125 if (use_last_char)
127 use_last_char = 0;
128 return c;
131 format_length++;
133 if (mode == MODE_STRING)
134 c = *format_string++;
135 else
137 c = gfc_next_char_literal (in_string);
138 if (c == '\n')
139 c = '\0';
142 if (gfc_option.flag_backslash && c == '\\')
144 locus old_locus = gfc_current_locus;
146 switch (gfc_next_char_literal (1))
148 case 'a':
149 c = '\a';
150 break;
151 case 'b':
152 c = '\b';
153 break;
154 case 't':
155 c = '\t';
156 break;
157 case 'f':
158 c = '\f';
159 break;
160 case 'n':
161 c = '\n';
162 break;
163 case 'r':
164 c = '\r';
165 break;
166 case 'v':
167 c = '\v';
168 break;
169 case '\\':
170 c = '\\';
171 break;
173 default:
174 /* Unknown backslash codes are simply not expanded. */
175 gfc_current_locus = old_locus;
176 break;
179 if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
180 gfc_warning ("Extension: backslash character at %C");
183 if (mode == MODE_COPY)
184 *format_string++ = c;
186 c = TOUPPER (c);
187 return c;
191 /* Back up one character position. Only works once. */
193 static void
194 unget_char (void)
196 use_last_char = 1;
199 /* Eat up the spaces and return a character. */
201 static char
202 next_char_not_space (void)
204 char c;
207 c = next_char (0);
209 while (gfc_is_whitespace (c));
210 return c;
213 static int value = 0;
215 /* Simple lexical analyzer for getting the next token in a FORMAT
216 statement. */
218 static format_token
219 format_lex (void)
221 format_token token;
222 char c, delim;
223 int zflag;
224 int negative_flag;
226 if (saved_token != FMT_NONE)
228 token = saved_token;
229 saved_token = FMT_NONE;
230 return token;
233 c = next_char_not_space ();
235 negative_flag = 0;
236 switch (c)
238 case '-':
239 negative_flag = 1;
240 case '+':
241 c = next_char_not_space ();
242 if (!ISDIGIT (c))
244 token = FMT_UNKNOWN;
245 break;
248 value = c - '0';
252 c = next_char_not_space ();
253 if (ISDIGIT (c))
254 value = 10 * value + c - '0';
256 while (ISDIGIT (c));
258 unget_char ();
260 if (negative_flag)
261 value = -value;
263 token = FMT_SIGNED_INT;
264 break;
266 case '0':
267 case '1':
268 case '2':
269 case '3':
270 case '4':
271 case '5':
272 case '6':
273 case '7':
274 case '8':
275 case '9':
276 zflag = (c == '0');
278 value = c - '0';
282 c = next_char_not_space ();
283 if (c != '0')
284 zflag = 0;
285 if (ISDIGIT (c))
286 value = 10 * value + c - '0';
288 while (ISDIGIT (c));
290 unget_char ();
291 token = zflag ? FMT_ZERO : FMT_POSINT;
292 break;
294 case '.':
295 token = FMT_PERIOD;
296 break;
298 case ',':
299 token = FMT_COMMA;
300 break;
302 case ':':
303 token = FMT_COLON;
304 break;
306 case '/':
307 token = FMT_SLASH;
308 break;
310 case '$':
311 token = FMT_DOLLAR;
312 break;
314 case 'T':
315 c = next_char_not_space ();
316 if (c != 'L' && c != 'R')
317 unget_char ();
319 token = FMT_POS;
320 break;
322 case '(':
323 token = FMT_LPAREN;
324 break;
326 case ')':
327 token = FMT_RPAREN;
328 break;
330 case 'X':
331 token = FMT_X;
332 break;
334 case 'S':
335 c = next_char_not_space ();
336 if (c != 'P' && c != 'S')
337 unget_char ();
339 token = FMT_SIGN;
340 break;
342 case 'B':
343 c = next_char_not_space ();
344 if (c == 'N' || c == 'Z')
345 token = FMT_BLANK;
346 else
348 unget_char ();
349 token = FMT_IBOZ;
352 break;
354 case '\'':
355 case '"':
356 delim = c;
358 value = 0;
360 for (;;)
362 c = next_char (1);
363 if (c == '\0')
365 token = FMT_END;
366 break;
369 if (c == delim)
371 c = next_char (1);
373 if (c == '\0')
375 token = FMT_END;
376 break;
379 if (c != delim)
381 unget_char ();
382 token = FMT_CHAR;
383 break;
386 value++;
388 break;
390 case 'P':
391 token = FMT_P;
392 break;
394 case 'I':
395 case 'O':
396 case 'Z':
397 token = FMT_IBOZ;
398 break;
400 case 'F':
401 token = FMT_F;
402 break;
404 case 'E':
405 c = next_char_not_space ();
406 if (c == 'N' || c == 'S')
407 token = FMT_EXT;
408 else
410 token = FMT_E;
411 unget_char ();
414 break;
416 case 'G':
417 token = FMT_G;
418 break;
420 case 'H':
421 token = FMT_H;
422 break;
424 case 'L':
425 token = FMT_L;
426 break;
428 case 'A':
429 token = FMT_A;
430 break;
432 case 'D':
433 token = FMT_D;
434 break;
436 case '\0':
437 token = FMT_END;
438 break;
440 default:
441 token = FMT_UNKNOWN;
442 break;
445 return token;
449 /* Check a format statement. The format string, either from a FORMAT
450 statement or a constant in an I/O statement has already been parsed
451 by itself, and we are checking it for validity. The dual origin
452 means that the warning message is a little less than great. */
454 static try
455 check_format (void)
457 const char *posint_required = _("Positive width required");
458 const char *nonneg_required = _("Nonnegative width required");
459 const char *unexpected_element = _("Unexpected element");
460 const char *unexpected_end = _("Unexpected end of format string");
462 const char *error;
463 format_token t, u;
464 int level;
465 int repeat;
466 try rv;
468 use_last_char = 0;
469 saved_token = FMT_NONE;
470 level = 0;
471 repeat = 0;
472 rv = SUCCESS;
474 t = format_lex ();
475 if (t != FMT_LPAREN)
477 error = _("Missing leading left parenthesis");
478 goto syntax;
481 t = format_lex ();
482 if (t == FMT_RPAREN)
483 goto finished; /* Empty format is legal */
484 saved_token = t;
486 format_item:
487 /* In this state, the next thing has to be a format item. */
488 t = format_lex ();
489 format_item_1:
490 switch (t)
492 case FMT_POSINT:
493 repeat = value;
494 t = format_lex ();
495 if (t == FMT_LPAREN)
497 level++;
498 goto format_item;
501 if (t == FMT_SLASH)
502 goto optional_comma;
504 goto data_desc;
506 case FMT_LPAREN:
507 level++;
508 goto format_item;
510 case FMT_SIGNED_INT:
511 /* Signed integer can only precede a P format. */
512 t = format_lex ();
513 if (t != FMT_P)
515 error = _("Expected P edit descriptor");
516 goto syntax;
519 goto data_desc;
521 case FMT_P:
522 /* P requires a prior number. */
523 error = _("P descriptor requires leading scale factor");
524 goto syntax;
526 case FMT_X:
527 /* X requires a prior number if we're being pedantic. */
528 if (gfc_notify_std (GFC_STD_GNU, "Extension: X descriptor "
529 "requires leading space count at %C")
530 == FAILURE)
531 return FAILURE;
532 goto between_desc;
534 case FMT_SIGN:
535 case FMT_BLANK:
536 goto between_desc;
538 case FMT_CHAR:
539 goto extension_optional_comma;
541 case FMT_COLON:
542 case FMT_SLASH:
543 goto optional_comma;
545 case FMT_DOLLAR:
546 t = format_lex ();
548 if (gfc_notify_std (GFC_STD_GNU, "Extension: $ descriptor at %C")
549 == FAILURE)
550 return FAILURE;
551 if (t != FMT_RPAREN || level > 0)
553 gfc_warning ("$ should be the last specifier in format at %C");
554 goto optional_comma_1;
557 goto finished;
559 case FMT_POS:
560 case FMT_IBOZ:
561 case FMT_F:
562 case FMT_E:
563 case FMT_EXT:
564 case FMT_G:
565 case FMT_L:
566 case FMT_A:
567 case FMT_D:
568 goto data_desc;
570 case FMT_H:
571 goto data_desc;
573 case FMT_END:
574 error = unexpected_end;
575 goto syntax;
577 default:
578 error = unexpected_element;
579 goto syntax;
582 data_desc:
583 /* In this state, t must currently be a data descriptor.
584 Deal with things that can/must follow the descriptor. */
585 switch (t)
587 case FMT_SIGN:
588 case FMT_BLANK:
589 case FMT_X:
590 break;
592 case FMT_P:
593 if (pedantic)
595 t = format_lex ();
596 if (t == FMT_POSINT)
598 error = _("Repeat count cannot follow P descriptor");
599 goto syntax;
602 saved_token = t;
605 goto optional_comma;
607 case FMT_POS:
608 case FMT_L:
609 t = format_lex ();
610 if (t == FMT_POSINT)
611 break;
613 switch (gfc_notification_std (GFC_STD_GNU))
615 case WARNING:
616 gfc_warning ("Extension: Missing positive width after L "
617 "descriptor at %C");
618 saved_token = t;
619 break;
621 case ERROR:
622 error = posint_required;
623 goto syntax;
625 case SILENT:
626 saved_token = t;
627 break;
629 default:
630 gcc_unreachable ();
632 break;
634 case FMT_A:
635 t = format_lex ();
636 if (t != FMT_POSINT)
637 saved_token = t;
638 break;
640 case FMT_D:
641 case FMT_E:
642 case FMT_G:
643 case FMT_EXT:
644 u = format_lex ();
645 if (u != FMT_POSINT)
647 error = posint_required;
648 goto syntax;
651 u = format_lex ();
652 if (u != FMT_PERIOD)
654 /* Warn if -std=legacy, otherwise error. */
655 if (gfc_option.warn_std != 0)
656 gfc_error_now ("Period required in format specifier at %C");
657 else
658 gfc_warning ("Period required in format specifier at %C");
659 saved_token = u;
660 break;
663 u = format_lex ();
664 if (u != FMT_ZERO && u != FMT_POSINT)
666 error = nonneg_required;
667 goto syntax;
670 if (t == FMT_D)
671 break;
673 /* Look for optional exponent. */
674 u = format_lex ();
675 if (u != FMT_E)
677 saved_token = u;
679 else
681 u = format_lex ();
682 if (u != FMT_POSINT)
684 error = _("Positive exponent width required");
685 goto syntax;
689 break;
691 case FMT_F:
692 t = format_lex ();
693 if (t != FMT_ZERO && t != FMT_POSINT)
695 error = nonneg_required;
696 goto syntax;
699 t = format_lex ();
700 if (t != FMT_PERIOD)
702 /* Warn if -std=legacy, otherwise error. */
703 if (gfc_option.warn_std != 0)
704 gfc_error_now ("Period required in format specifier at %C");
705 else
706 gfc_warning ("Period required in format specifier at %C");
707 saved_token = t;
708 break;
711 t = format_lex ();
712 if (t != FMT_ZERO && t != FMT_POSINT)
714 error = nonneg_required;
715 goto syntax;
718 break;
720 case FMT_H:
721 if(mode == MODE_STRING)
723 format_string += value;
724 format_length -= value;
726 else
728 while (repeat >0)
730 next_char (1);
731 repeat -- ;
734 break;
736 case FMT_IBOZ:
737 t = format_lex ();
738 if (t != FMT_ZERO && t != FMT_POSINT)
740 error = nonneg_required;
741 goto syntax;
744 t = format_lex ();
745 if (t != FMT_PERIOD)
747 saved_token = t;
749 else
751 t = format_lex ();
752 if (t != FMT_ZERO && t != FMT_POSINT)
754 error = nonneg_required;
755 goto syntax;
759 break;
761 default:
762 error = unexpected_element;
763 goto syntax;
766 between_desc:
767 /* Between a descriptor and what comes next. */
768 t = format_lex ();
769 switch (t)
772 case FMT_COMMA:
773 goto format_item;
775 case FMT_RPAREN:
776 level--;
777 if (level < 0)
778 goto finished;
779 goto between_desc;
781 case FMT_COLON:
782 case FMT_SLASH:
783 goto optional_comma;
785 case FMT_END:
786 error = unexpected_end;
787 goto syntax;
789 default:
790 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
791 == FAILURE)
792 return FAILURE;
793 goto format_item_1;
796 optional_comma:
797 /* Optional comma is a weird between state where we've just finished
798 reading a colon, slash, dollar or P descriptor. */
799 t = format_lex ();
800 optional_comma_1:
801 switch (t)
803 case FMT_COMMA:
804 break;
806 case FMT_RPAREN:
807 level--;
808 if (level < 0)
809 goto finished;
810 goto between_desc;
812 default:
813 /* Assume that we have another format item. */
814 saved_token = t;
815 break;
818 goto format_item;
820 extension_optional_comma:
821 /* As a GNU extension, permit a missing comma after a string literal. */
822 t = format_lex ();
823 switch (t)
825 case FMT_COMMA:
826 break;
828 case FMT_RPAREN:
829 level--;
830 if (level < 0)
831 goto finished;
832 goto between_desc;
834 case FMT_COLON:
835 case FMT_SLASH:
836 goto optional_comma;
838 case FMT_END:
839 error = unexpected_end;
840 goto syntax;
842 default:
843 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
844 == FAILURE)
845 return FAILURE;
846 saved_token = t;
847 break;
850 goto format_item;
852 syntax:
853 /* Something went wrong. If the format we're checking is a string,
854 generate a warning, since the program is correct. If the format
855 is in a FORMAT statement, this messes up parsing, which is an
856 error. */
857 if (mode != MODE_STRING)
858 gfc_error ("%s in format string at %C", error);
859 else
861 gfc_warning ("%s in format string at %C", error);
863 /* TODO: More elaborate measures are needed to show where a problem
864 is within a format string that has been calculated. */
867 rv = FAILURE;
869 finished:
870 return rv;
874 /* Given an expression node that is a constant string, see if it looks
875 like a format string. */
877 static void
878 check_format_string (gfc_expr *e)
880 mode = MODE_STRING;
881 format_string = e->value.character.string;
882 check_format ();
886 /************ Fortran 95 I/O statement matchers *************/
888 /* Match a FORMAT statement. This amounts to actually parsing the
889 format descriptors in order to correctly locate the end of the
890 format string. */
892 match
893 gfc_match_format (void)
895 gfc_expr *e;
896 locus start;
898 if (gfc_current_ns->proc_name
899 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
901 gfc_error ("Format statement in module main block at %C");
902 return MATCH_ERROR;
905 if (gfc_statement_label == NULL)
907 gfc_error ("Missing format label at %C");
908 return MATCH_ERROR;
910 gfc_gobble_whitespace ();
912 mode = MODE_FORMAT;
913 format_length = 0;
915 start = gfc_current_locus;
917 if (check_format () == FAILURE)
918 return MATCH_ERROR;
920 if (gfc_match_eos () != MATCH_YES)
922 gfc_syntax_error (ST_FORMAT);
923 return MATCH_ERROR;
926 /* The label doesn't get created until after the statement is done
927 being matched, so we have to leave the string for later. */
929 gfc_current_locus = start; /* Back to the beginning */
931 new_st.loc = start;
932 new_st.op = EXEC_NOP;
934 e = gfc_get_expr();
935 e->expr_type = EXPR_CONSTANT;
936 e->ts.type = BT_CHARACTER;
937 e->ts.kind = gfc_default_character_kind;
938 e->where = start;
939 e->value.character.string = format_string = gfc_getmem (format_length + 1);
940 e->value.character.length = format_length;
941 gfc_statement_label->format = e;
943 mode = MODE_COPY;
944 check_format (); /* Guaranteed to succeed */
945 gfc_match_eos (); /* Guaranteed to succeed */
947 return MATCH_YES;
951 /* Match an expression I/O tag of some sort. */
953 static match
954 match_etag (const io_tag *tag, gfc_expr **v)
956 gfc_expr *result;
957 match m;
959 m = gfc_match (tag->spec, &result);
960 if (m != MATCH_YES)
961 return m;
963 if (*v != NULL)
965 gfc_error ("Duplicate %s specification at %C", tag->name);
966 gfc_free_expr (result);
967 return MATCH_ERROR;
970 *v = result;
971 return MATCH_YES;
975 /* Match a variable I/O tag of some sort. */
977 static match
978 match_vtag (const io_tag *tag, gfc_expr **v)
980 gfc_expr *result;
981 match m;
983 m = gfc_match (tag->spec, &result);
984 if (m != MATCH_YES)
985 return m;
987 if (*v != NULL)
989 gfc_error ("Duplicate %s specification at %C", tag->name);
990 gfc_free_expr (result);
991 return MATCH_ERROR;
994 if (result->symtree->n.sym->attr.intent == INTENT_IN)
996 gfc_error ("Variable tag cannot be INTENT(IN) at %C");
997 gfc_free_expr (result);
998 return MATCH_ERROR;
1001 if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
1003 gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
1004 gfc_free_expr (result);
1005 return MATCH_ERROR;
1008 *v = result;
1009 return MATCH_YES;
1013 /* Match I/O tags that cause variables to become redefined. */
1015 static match
1016 match_out_tag(const io_tag *tag, gfc_expr **result)
1018 match m;
1020 m = match_vtag(tag, result);
1021 if (m == MATCH_YES)
1022 gfc_check_do_variable((*result)->symtree);
1024 return m;
1028 /* Match a label I/O tag. */
1030 static match
1031 match_ltag (const io_tag *tag, gfc_st_label ** label)
1033 match m;
1034 gfc_st_label *old;
1036 old = *label;
1037 m = gfc_match (tag->spec, label);
1038 if (m == MATCH_YES && old != 0)
1040 gfc_error ("Duplicate %s label specification at %C", tag->name);
1041 return MATCH_ERROR;
1044 if (m == MATCH_YES
1045 && gfc_reference_st_label (*label, ST_LABEL_TARGET) == FAILURE)
1046 return MATCH_ERROR;
1048 return m;
1052 /* Do expression resolution and type-checking on an expression tag. */
1054 static try
1055 resolve_tag (const io_tag *tag, gfc_expr *e)
1057 if (e == NULL)
1058 return SUCCESS;
1060 if (gfc_resolve_expr (e) == FAILURE)
1061 return FAILURE;
1063 if (e->ts.type != tag->type && tag != &tag_format)
1065 gfc_error ("%s tag at %L must be of type %s", tag->name,
1066 &e->where, gfc_basic_typename (tag->type));
1067 return FAILURE;
1070 if (tag == &tag_format)
1072 if (e->expr_type == EXPR_CONSTANT
1073 && (e->ts.type != BT_CHARACTER
1074 || e->ts.kind != gfc_default_character_kind))
1076 gfc_error ("Constant expression in FORMAT tag at %L must be "
1077 "of type default CHARACTER", &e->where);
1078 return FAILURE;
1081 /* If e's rank is zero and e is not an element of an array, it should be
1082 of integer or character type. The integer variable should be
1083 ASSIGNED. */
1084 if (e->symtree == NULL || e->symtree->n.sym->as == NULL
1085 || e->symtree->n.sym->as->rank == 0)
1087 if (e->ts.type != BT_CHARACTER && e->ts.type != BT_INTEGER)
1089 gfc_error ("%s tag at %L must be of type %s or %s", tag->name,
1090 &e->where, gfc_basic_typename (BT_CHARACTER),
1091 gfc_basic_typename (BT_INTEGER));
1092 return FAILURE;
1094 else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
1096 if (gfc_notify_std (GFC_STD_F95_DEL, "Obsolete: ASSIGNED "
1097 "variable in FORMAT tag at %L", &e->where)
1098 == FAILURE)
1099 return FAILURE;
1100 if (e->symtree->n.sym->attr.assign != 1)
1102 gfc_error ("Variable '%s' at %L has not been assigned a "
1103 "format label", e->symtree->n.sym->name,
1104 &e->where);
1105 return FAILURE;
1108 else if (e->ts.type == BT_INTEGER)
1110 gfc_error ("scalar '%s' FORMAT tag at %L is not an ASSIGNED "
1111 "variable", gfc_basic_typename (e->ts.type),
1112 &e->where);
1113 return FAILURE;
1116 return SUCCESS;
1118 else
1120 /* if rank is nonzero, we allow the type to be character under
1121 GFC_STD_GNU and other type under GFC_STD_LEGACY. It may be
1122 assigned an Hollerith constant. */
1123 if (e->ts.type == BT_CHARACTER)
1125 if (gfc_notify_std (GFC_STD_GNU, "Extension: Character array "
1126 "in FORMAT tag at %L", &e->where)
1127 == FAILURE)
1128 return FAILURE;
1130 else
1132 if (gfc_notify_std (GFC_STD_LEGACY, "Extension: Non-character "
1133 "in FORMAT tag at %L", &e->where)
1134 == FAILURE)
1135 return FAILURE;
1137 return SUCCESS;
1140 else
1142 if (e->rank != 0)
1144 gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
1145 return FAILURE;
1148 if (tag == &tag_iomsg)
1150 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IOMSG tag at %L",
1151 &e->where) == FAILURE)
1152 return FAILURE;
1155 if (tag == &tag_iostat && e->ts.kind != gfc_default_integer_kind)
1157 if (gfc_notify_std (GFC_STD_GNU, "Fortran 95 requires default "
1158 "INTEGER in IOSTAT tag at %L", &e->where)
1159 == FAILURE)
1160 return FAILURE;
1163 if (tag == &tag_size && e->ts.kind != gfc_default_integer_kind)
1165 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1166 "INTEGER in SIZE tag at %L", &e->where)
1167 == FAILURE)
1168 return FAILURE;
1171 if (tag == &tag_convert)
1173 if (gfc_notify_std (GFC_STD_GNU, "Extension: CONVERT tag at %L",
1174 &e->where) == FAILURE)
1175 return FAILURE;
1178 if (tag == &tag_iolength && e->ts.kind != gfc_default_integer_kind)
1180 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1181 "INTEGER in IOLENGTH tag at %L", &e->where)
1182 == FAILURE)
1183 return FAILURE;
1187 return SUCCESS;
1191 /* Match a single tag of an OPEN statement. */
1193 static match
1194 match_open_element (gfc_open *open)
1196 match m;
1198 m = match_etag (&tag_unit, &open->unit);
1199 if (m != MATCH_NO)
1200 return m;
1201 m = match_out_tag (&tag_iomsg, &open->iomsg);
1202 if (m != MATCH_NO)
1203 return m;
1204 m = match_out_tag (&tag_iostat, &open->iostat);
1205 if (m != MATCH_NO)
1206 return m;
1207 m = match_etag (&tag_file, &open->file);
1208 if (m != MATCH_NO)
1209 return m;
1210 m = match_etag (&tag_status, &open->status);
1211 if (m != MATCH_NO)
1212 return m;
1213 m = match_etag (&tag_e_access, &open->access);
1214 if (m != MATCH_NO)
1215 return m;
1216 m = match_etag (&tag_e_form, &open->form);
1217 if (m != MATCH_NO)
1218 return m;
1219 m = match_etag (&tag_e_recl, &open->recl);
1220 if (m != MATCH_NO)
1221 return m;
1222 m = match_etag (&tag_e_blank, &open->blank);
1223 if (m != MATCH_NO)
1224 return m;
1225 m = match_etag (&tag_e_position, &open->position);
1226 if (m != MATCH_NO)
1227 return m;
1228 m = match_etag (&tag_e_action, &open->action);
1229 if (m != MATCH_NO)
1230 return m;
1231 m = match_etag (&tag_e_delim, &open->delim);
1232 if (m != MATCH_NO)
1233 return m;
1234 m = match_etag (&tag_e_pad, &open->pad);
1235 if (m != MATCH_NO)
1236 return m;
1237 m = match_ltag (&tag_err, &open->err);
1238 if (m != MATCH_NO)
1239 return m;
1240 m = match_etag (&tag_convert, &open->convert);
1241 if (m != MATCH_NO)
1242 return m;
1244 return MATCH_NO;
1248 /* Free the gfc_open structure and all the expressions it contains. */
1250 void
1251 gfc_free_open (gfc_open *open)
1253 if (open == NULL)
1254 return;
1256 gfc_free_expr (open->unit);
1257 gfc_free_expr (open->iomsg);
1258 gfc_free_expr (open->iostat);
1259 gfc_free_expr (open->file);
1260 gfc_free_expr (open->status);
1261 gfc_free_expr (open->access);
1262 gfc_free_expr (open->form);
1263 gfc_free_expr (open->recl);
1264 gfc_free_expr (open->blank);
1265 gfc_free_expr (open->position);
1266 gfc_free_expr (open->action);
1267 gfc_free_expr (open->delim);
1268 gfc_free_expr (open->pad);
1269 gfc_free_expr (open->convert);
1270 gfc_free (open);
1274 /* Resolve everything in a gfc_open structure. */
1277 gfc_resolve_open (gfc_open *open)
1280 RESOLVE_TAG (&tag_unit, open->unit);
1281 RESOLVE_TAG (&tag_iomsg, open->iomsg);
1282 RESOLVE_TAG (&tag_iostat, open->iostat);
1283 RESOLVE_TAG (&tag_file, open->file);
1284 RESOLVE_TAG (&tag_status, open->status);
1285 RESOLVE_TAG (&tag_e_access, open->access);
1286 RESOLVE_TAG (&tag_e_form, open->form);
1287 RESOLVE_TAG (&tag_e_recl, open->recl);
1288 RESOLVE_TAG (&tag_e_blank, open->blank);
1289 RESOLVE_TAG (&tag_e_position, open->position);
1290 RESOLVE_TAG (&tag_e_action, open->action);
1291 RESOLVE_TAG (&tag_e_delim, open->delim);
1292 RESOLVE_TAG (&tag_e_pad, open->pad);
1293 RESOLVE_TAG (&tag_convert, open->convert);
1295 if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1296 return FAILURE;
1298 return SUCCESS;
1302 /* Check if a given value for a SPECIFIER is either in the list of values
1303 allowed in F95 or F2003, issuing an error message and returning a zero
1304 value if it is not allowed. */
1306 static int
1307 compare_to_allowed_values (const char *specifier, const char *allowed[],
1308 const char *allowed_f2003[],
1309 const char *allowed_gnu[], char *value,
1310 const char *statement, bool warn)
1312 int i;
1313 unsigned int len;
1315 len = strlen (value);
1316 if (len > 0)
1318 for (len--; len > 0; len--)
1319 if (value[len] != ' ')
1320 break;
1321 len++;
1324 for (i = 0; allowed[i]; i++)
1325 if (len == strlen (allowed[i])
1326 && strncasecmp (value, allowed[i], strlen (allowed[i])) == 0)
1327 return 1;
1329 for (i = 0; allowed_f2003 && allowed_f2003[i]; i++)
1330 if (len == strlen (allowed_f2003[i])
1331 && strncasecmp (value, allowed_f2003[i], strlen (allowed_f2003[i]))
1332 == 0)
1334 notification n = gfc_notification_std (GFC_STD_F2003);
1336 if (n == WARNING || (warn && n == ERROR))
1338 gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1339 "has value '%s'", specifier, statement,
1340 allowed_f2003[i]);
1341 return 1;
1343 else
1344 if (n == ERROR)
1346 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: %s specifier in "
1347 "%s statement at %C has value '%s'", specifier,
1348 statement, allowed_f2003[i]);
1349 return 0;
1352 /* n == SILENT */
1353 return 1;
1356 for (i = 0; allowed_gnu && allowed_gnu[i]; i++)
1357 if (len == strlen (allowed_gnu[i])
1358 && strncasecmp (value, allowed_gnu[i], strlen (allowed_gnu[i])) == 0)
1360 notification n = gfc_notification_std (GFC_STD_GNU);
1362 if (n == WARNING || (warn && n == ERROR))
1364 gfc_warning ("Extension: %s specifier in %s statement at %C "
1365 "has value '%s'", specifier, statement,
1366 allowed_gnu[i]);
1367 return 1;
1369 else
1370 if (n == ERROR)
1372 gfc_notify_std (GFC_STD_GNU, "Extension: %s specifier in "
1373 "%s statement at %C has value '%s'", specifier,
1374 statement, allowed_gnu[i]);
1375 return 0;
1378 /* n == SILENT */
1379 return 1;
1382 if (warn)
1384 gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1385 specifier, statement, value);
1386 return 1;
1388 else
1390 gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1391 specifier, statement, value);
1392 return 0;
1397 /* Match an OPEN statement. */
1399 match
1400 gfc_match_open (void)
1402 gfc_open *open;
1403 match m;
1404 bool warn;
1406 m = gfc_match_char ('(');
1407 if (m == MATCH_NO)
1408 return m;
1410 open = gfc_getmem (sizeof (gfc_open));
1412 m = match_open_element (open);
1414 if (m == MATCH_ERROR)
1415 goto cleanup;
1416 if (m == MATCH_NO)
1418 m = gfc_match_expr (&open->unit);
1419 if (m == MATCH_NO)
1420 goto syntax;
1421 if (m == MATCH_ERROR)
1422 goto cleanup;
1425 for (;;)
1427 if (gfc_match_char (')') == MATCH_YES)
1428 break;
1429 if (gfc_match_char (',') != MATCH_YES)
1430 goto syntax;
1432 m = match_open_element (open);
1433 if (m == MATCH_ERROR)
1434 goto cleanup;
1435 if (m == MATCH_NO)
1436 goto syntax;
1439 if (gfc_match_eos () == MATCH_NO)
1440 goto syntax;
1442 if (gfc_pure (NULL))
1444 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1445 goto cleanup;
1448 warn = (open->err || open->iostat) ? true : false;
1449 /* Checks on the ACCESS specifier. */
1450 if (open->access && open->access->expr_type == EXPR_CONSTANT)
1452 static const char *access_f95[] = { "SEQUENTIAL", "DIRECT", NULL };
1453 static const char *access_f2003[] = { "STREAM", NULL };
1454 static const char *access_gnu[] = { "APPEND", NULL };
1456 if (!compare_to_allowed_values ("ACCESS", access_f95, access_f2003,
1457 access_gnu,
1458 open->access->value.character.string,
1459 "OPEN", warn))
1460 goto cleanup;
1463 /* Checks on the ACTION specifier. */
1464 if (open->action && open->action->expr_type == EXPR_CONSTANT)
1466 static const char *action[] = { "READ", "WRITE", "READWRITE", NULL };
1468 if (!compare_to_allowed_values ("ACTION", action, NULL, NULL,
1469 open->action->value.character.string,
1470 "OPEN", warn))
1471 goto cleanup;
1474 /* Checks on the ASYNCHRONOUS specifier. */
1475 /* TODO: code is ready, just needs uncommenting when async I/O support
1476 is added ;-)
1477 if (open->asynchronous && open->asynchronous->expr_type == EXPR_CONSTANT)
1479 static const char * asynchronous[] = { "YES", "NO", NULL };
1481 if (!compare_to_allowed_values
1482 ("action", asynchronous, NULL, NULL,
1483 open->asynchronous->value.character.string, "OPEN", warn))
1484 goto cleanup;
1487 /* Checks on the BLANK specifier. */
1488 if (open->blank && open->blank->expr_type == EXPR_CONSTANT)
1490 static const char *blank[] = { "ZERO", "NULL", NULL };
1492 if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
1493 open->blank->value.character.string,
1494 "OPEN", warn))
1495 goto cleanup;
1498 /* Checks on the DECIMAL specifier. */
1499 /* TODO: uncomment this code when DECIMAL support is added
1500 if (open->decimal && open->decimal->expr_type == EXPR_CONSTANT)
1502 static const char * decimal[] = { "COMMA", "POINT", NULL };
1504 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1505 open->decimal->value.character.string,
1506 "OPEN", warn))
1507 goto cleanup;
1508 } */
1510 /* Checks on the DELIM specifier. */
1511 if (open->delim && open->delim->expr_type == EXPR_CONSTANT)
1513 static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
1515 if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
1516 open->delim->value.character.string,
1517 "OPEN", warn))
1518 goto cleanup;
1521 /* Checks on the ENCODING specifier. */
1522 /* TODO: uncomment this code when ENCODING support is added
1523 if (open->encoding && open->encoding->expr_type == EXPR_CONSTANT)
1525 static const char * encoding[] = { "UTF-8", "DEFAULT", NULL };
1527 if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1528 open->encoding->value.character.string,
1529 "OPEN", warn))
1530 goto cleanup;
1531 } */
1533 /* Checks on the FORM specifier. */
1534 if (open->form && open->form->expr_type == EXPR_CONSTANT)
1536 static const char *form[] = { "FORMATTED", "UNFORMATTED", NULL };
1538 if (!compare_to_allowed_values ("FORM", form, NULL, NULL,
1539 open->form->value.character.string,
1540 "OPEN", warn))
1541 goto cleanup;
1544 /* Checks on the PAD specifier. */
1545 if (open->pad && open->pad->expr_type == EXPR_CONSTANT)
1547 static const char *pad[] = { "YES", "NO", NULL };
1549 if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
1550 open->pad->value.character.string,
1551 "OPEN", warn))
1552 goto cleanup;
1555 /* Checks on the POSITION specifier. */
1556 if (open->position && open->position->expr_type == EXPR_CONSTANT)
1558 static const char *position[] = { "ASIS", "REWIND", "APPEND", NULL };
1560 if (!compare_to_allowed_values ("POSITION", position, NULL, NULL,
1561 open->position->value.character.string,
1562 "OPEN", warn))
1563 goto cleanup;
1566 /* Checks on the ROUND specifier. */
1567 /* TODO: uncomment this code when ROUND support is added
1568 if (open->round && open->round->expr_type == EXPR_CONSTANT)
1570 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1571 "COMPATIBLE", "PROCESSOR_DEFINED", NULL };
1573 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1574 open->round->value.character.string,
1575 "OPEN", warn))
1576 goto cleanup;
1577 } */
1579 /* Checks on the SIGN specifier. */
1580 /* TODO: uncomment this code when SIGN support is added
1581 if (open->sign && open->sign->expr_type == EXPR_CONSTANT)
1583 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1584 NULL };
1586 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1587 open->sign->value.character.string,
1588 "OPEN", warn))
1589 goto cleanup;
1590 } */
1592 #define warn_or_error(...) \
1594 if (warn) \
1595 gfc_warning (__VA_ARGS__); \
1596 else \
1598 gfc_error (__VA_ARGS__); \
1599 goto cleanup; \
1603 /* Checks on the RECL specifier. */
1604 if (open->recl && open->recl->expr_type == EXPR_CONSTANT
1605 && open->recl->ts.type == BT_INTEGER
1606 && mpz_sgn (open->recl->value.integer) != 1)
1608 warn_or_error ("RECL in OPEN statement at %C must be positive");
1611 /* Checks on the STATUS specifier. */
1612 if (open->status && open->status->expr_type == EXPR_CONSTANT)
1614 static const char *status[] = { "OLD", "NEW", "SCRATCH",
1615 "REPLACE", "UNKNOWN", NULL };
1617 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1618 open->status->value.character.string,
1619 "OPEN", warn))
1620 goto cleanup;
1622 /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1623 the FILE= specifier shall appear. */
1624 if (open->file == NULL
1625 && (strncasecmp (open->status->value.character.string, "replace", 7)
1626 == 0
1627 || strncasecmp (open->status->value.character.string, "new", 3)
1628 == 0))
1630 warn_or_error ("The STATUS specified in OPEN statement at %C is "
1631 "'%s' and no FILE specifier is present",
1632 open->status->value.character.string);
1635 /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1636 the FILE= specifier shall not appear. */
1637 if (strncasecmp (open->status->value.character.string, "scratch", 7)
1638 == 0 && open->file)
1640 warn_or_error ("The STATUS specified in OPEN statement at %C "
1641 "cannot have the value SCRATCH if a FILE specifier "
1642 "is present");
1646 /* Things that are not allowed for unformatted I/O. */
1647 if (open->form && open->form->expr_type == EXPR_CONSTANT
1648 && (open->delim
1649 /* TODO uncomment this code when F2003 support is finished */
1650 /* || open->decimal || open->encoding || open->round
1651 || open->sign */
1652 || open->pad || open->blank)
1653 && strncasecmp (open->form->value.character.string,
1654 "unformatted", 11) == 0)
1656 const char *spec = (open->delim ? "DELIM "
1657 : (open->pad ? "PAD " : open->blank
1658 ? "BLANK " : ""));
1660 warn_or_error ("%s specifier at %C not allowed in OPEN statement for "
1661 "unformatted I/O", spec);
1664 if (open->recl && open->access && open->access->expr_type == EXPR_CONSTANT
1665 && strncasecmp (open->access->value.character.string, "stream", 6) == 0)
1667 warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
1668 "stream I/O");
1671 if (open->position
1672 && open->access && open->access->expr_type == EXPR_CONSTANT
1673 && !(strncasecmp (open->access->value.character.string,
1674 "sequential", 10) == 0
1675 || strncasecmp (open->access->value.character.string,
1676 "stream", 6) == 0
1677 || strncasecmp (open->access->value.character.string,
1678 "append", 6) == 0))
1680 warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
1681 "for stream or sequential ACCESS");
1684 #undef warn_or_error
1686 new_st.op = EXEC_OPEN;
1687 new_st.ext.open = open;
1688 return MATCH_YES;
1690 syntax:
1691 gfc_syntax_error (ST_OPEN);
1693 cleanup:
1694 gfc_free_open (open);
1695 return MATCH_ERROR;
1699 /* Free a gfc_close structure an all its expressions. */
1701 void
1702 gfc_free_close (gfc_close *close)
1704 if (close == NULL)
1705 return;
1707 gfc_free_expr (close->unit);
1708 gfc_free_expr (close->iomsg);
1709 gfc_free_expr (close->iostat);
1710 gfc_free_expr (close->status);
1711 gfc_free (close);
1715 /* Match elements of a CLOSE statement. */
1717 static match
1718 match_close_element (gfc_close *close)
1720 match m;
1722 m = match_etag (&tag_unit, &close->unit);
1723 if (m != MATCH_NO)
1724 return m;
1725 m = match_etag (&tag_status, &close->status);
1726 if (m != MATCH_NO)
1727 return m;
1728 m = match_out_tag (&tag_iomsg, &close->iomsg);
1729 if (m != MATCH_NO)
1730 return m;
1731 m = match_out_tag (&tag_iostat, &close->iostat);
1732 if (m != MATCH_NO)
1733 return m;
1734 m = match_ltag (&tag_err, &close->err);
1735 if (m != MATCH_NO)
1736 return m;
1738 return MATCH_NO;
1742 /* Match a CLOSE statement. */
1744 match
1745 gfc_match_close (void)
1747 gfc_close *close;
1748 match m;
1749 bool warn;
1751 m = gfc_match_char ('(');
1752 if (m == MATCH_NO)
1753 return m;
1755 close = gfc_getmem (sizeof (gfc_close));
1757 m = match_close_element (close);
1759 if (m == MATCH_ERROR)
1760 goto cleanup;
1761 if (m == MATCH_NO)
1763 m = gfc_match_expr (&close->unit);
1764 if (m == MATCH_NO)
1765 goto syntax;
1766 if (m == MATCH_ERROR)
1767 goto cleanup;
1770 for (;;)
1772 if (gfc_match_char (')') == MATCH_YES)
1773 break;
1774 if (gfc_match_char (',') != MATCH_YES)
1775 goto syntax;
1777 m = match_close_element (close);
1778 if (m == MATCH_ERROR)
1779 goto cleanup;
1780 if (m == MATCH_NO)
1781 goto syntax;
1784 if (gfc_match_eos () == MATCH_NO)
1785 goto syntax;
1787 if (gfc_pure (NULL))
1789 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1790 goto cleanup;
1793 warn = (close->iostat || close->err) ? true : false;
1795 /* Checks on the STATUS specifier. */
1796 if (close->status && close->status->expr_type == EXPR_CONSTANT)
1798 static const char *status[] = { "KEEP", "DELETE", NULL };
1800 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1801 close->status->value.character.string,
1802 "CLOSE", warn))
1803 goto cleanup;
1806 new_st.op = EXEC_CLOSE;
1807 new_st.ext.close = close;
1808 return MATCH_YES;
1810 syntax:
1811 gfc_syntax_error (ST_CLOSE);
1813 cleanup:
1814 gfc_free_close (close);
1815 return MATCH_ERROR;
1819 /* Resolve everything in a gfc_close structure. */
1822 gfc_resolve_close (gfc_close *close)
1824 RESOLVE_TAG (&tag_unit, close->unit);
1825 RESOLVE_TAG (&tag_iomsg, close->iomsg);
1826 RESOLVE_TAG (&tag_iostat, close->iostat);
1827 RESOLVE_TAG (&tag_status, close->status);
1829 if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
1830 return FAILURE;
1832 return SUCCESS;
1836 /* Free a gfc_filepos structure. */
1838 void
1839 gfc_free_filepos (gfc_filepos *fp)
1841 gfc_free_expr (fp->unit);
1842 gfc_free_expr (fp->iomsg);
1843 gfc_free_expr (fp->iostat);
1844 gfc_free (fp);
1848 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
1850 static match
1851 match_file_element (gfc_filepos *fp)
1853 match m;
1855 m = match_etag (&tag_unit, &fp->unit);
1856 if (m != MATCH_NO)
1857 return m;
1858 m = match_out_tag (&tag_iomsg, &fp->iomsg);
1859 if (m != MATCH_NO)
1860 return m;
1861 m = match_out_tag (&tag_iostat, &fp->iostat);
1862 if (m != MATCH_NO)
1863 return m;
1864 m = match_ltag (&tag_err, &fp->err);
1865 if (m != MATCH_NO)
1866 return m;
1868 return MATCH_NO;
1872 /* Match the second half of the file-positioning statements, REWIND,
1873 BACKSPACE, ENDFILE, or the FLUSH statement. */
1875 static match
1876 match_filepos (gfc_statement st, gfc_exec_op op)
1878 gfc_filepos *fp;
1879 match m;
1881 fp = gfc_getmem (sizeof (gfc_filepos));
1883 if (gfc_match_char ('(') == MATCH_NO)
1885 m = gfc_match_expr (&fp->unit);
1886 if (m == MATCH_ERROR)
1887 goto cleanup;
1888 if (m == MATCH_NO)
1889 goto syntax;
1891 goto done;
1894 m = match_file_element (fp);
1895 if (m == MATCH_ERROR)
1896 goto done;
1897 if (m == MATCH_NO)
1899 m = gfc_match_expr (&fp->unit);
1900 if (m == MATCH_ERROR)
1901 goto done;
1902 if (m == MATCH_NO)
1903 goto syntax;
1906 for (;;)
1908 if (gfc_match_char (')') == MATCH_YES)
1909 break;
1910 if (gfc_match_char (',') != MATCH_YES)
1911 goto syntax;
1913 m = match_file_element (fp);
1914 if (m == MATCH_ERROR)
1915 goto cleanup;
1916 if (m == MATCH_NO)
1917 goto syntax;
1920 done:
1921 if (gfc_match_eos () != MATCH_YES)
1922 goto syntax;
1924 if (gfc_pure (NULL))
1926 gfc_error ("%s statement not allowed in PURE procedure at %C",
1927 gfc_ascii_statement (st));
1929 goto cleanup;
1932 new_st.op = op;
1933 new_st.ext.filepos = fp;
1934 return MATCH_YES;
1936 syntax:
1937 gfc_syntax_error (st);
1939 cleanup:
1940 gfc_free_filepos (fp);
1941 return MATCH_ERROR;
1946 gfc_resolve_filepos (gfc_filepos *fp)
1948 RESOLVE_TAG (&tag_unit, fp->unit);
1949 RESOLVE_TAG (&tag_iostat, fp->iostat);
1950 RESOLVE_TAG (&tag_iomsg, fp->iomsg);
1951 if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
1952 return FAILURE;
1954 return SUCCESS;
1958 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
1959 and the FLUSH statement. */
1961 match
1962 gfc_match_endfile (void)
1964 return match_filepos (ST_END_FILE, EXEC_ENDFILE);
1967 match
1968 gfc_match_backspace (void)
1970 return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
1973 match
1974 gfc_match_rewind (void)
1976 return match_filepos (ST_REWIND, EXEC_REWIND);
1979 match
1980 gfc_match_flush (void)
1982 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: FLUSH statement at %C")
1983 == FAILURE)
1984 return MATCH_ERROR;
1986 return match_filepos (ST_FLUSH, EXEC_FLUSH);
1989 /******************** Data Transfer Statements *********************/
1991 typedef enum
1992 { M_READ, M_WRITE, M_PRINT, M_INQUIRE }
1993 io_kind;
1996 /* Return a default unit number. */
1998 static gfc_expr *
1999 default_unit (io_kind k)
2001 int unit;
2003 if (k == M_READ)
2004 unit = 5;
2005 else
2006 unit = 6;
2008 return gfc_int_expr (unit);
2012 /* Match a unit specification for a data transfer statement. */
2014 static match
2015 match_dt_unit (io_kind k, gfc_dt *dt)
2017 gfc_expr *e;
2019 if (gfc_match_char ('*') == MATCH_YES)
2021 if (dt->io_unit != NULL)
2022 goto conflict;
2024 dt->io_unit = default_unit (k);
2025 return MATCH_YES;
2028 if (gfc_match_expr (&e) == MATCH_YES)
2030 if (dt->io_unit != NULL)
2032 gfc_free_expr (e);
2033 goto conflict;
2036 dt->io_unit = e;
2037 return MATCH_YES;
2040 return MATCH_NO;
2042 conflict:
2043 gfc_error ("Duplicate UNIT specification at %C");
2044 return MATCH_ERROR;
2048 /* Match a format specification. */
2050 static match
2051 match_dt_format (gfc_dt *dt)
2053 locus where;
2054 gfc_expr *e;
2055 gfc_st_label *label;
2057 where = gfc_current_locus;
2059 if (gfc_match_char ('*') == MATCH_YES)
2061 if (dt->format_expr != NULL || dt->format_label != NULL)
2062 goto conflict;
2064 dt->format_label = &format_asterisk;
2065 return MATCH_YES;
2068 if (gfc_match_st_label (&label) == MATCH_YES)
2070 if (dt->format_expr != NULL || dt->format_label != NULL)
2072 gfc_free_st_label (label);
2073 goto conflict;
2076 if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
2077 return MATCH_ERROR;
2079 dt->format_label = label;
2080 return MATCH_YES;
2083 if (gfc_match_expr (&e) == MATCH_YES)
2085 if (dt->format_expr != NULL || dt->format_label != NULL)
2087 gfc_free_expr (e);
2088 goto conflict;
2090 dt->format_expr = e;
2091 return MATCH_YES;
2094 gfc_current_locus = where; /* The only case where we have to restore */
2096 return MATCH_NO;
2098 conflict:
2099 gfc_error ("Duplicate format specification at %C");
2100 return MATCH_ERROR;
2104 /* Traverse a namelist that is part of a READ statement to make sure
2105 that none of the variables in the namelist are INTENT(IN). Returns
2106 nonzero if we find such a variable. */
2108 static int
2109 check_namelist (gfc_symbol *sym)
2111 gfc_namelist *p;
2113 for (p = sym->namelist; p; p = p->next)
2114 if (p->sym->attr.intent == INTENT_IN)
2116 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2117 p->sym->name, sym->name);
2118 return 1;
2121 return 0;
2125 /* Match a single data transfer element. */
2127 static match
2128 match_dt_element (io_kind k, gfc_dt *dt)
2130 char name[GFC_MAX_SYMBOL_LEN + 1];
2131 gfc_symbol *sym;
2132 match m;
2134 if (gfc_match (" unit =") == MATCH_YES)
2136 m = match_dt_unit (k, dt);
2137 if (m != MATCH_NO)
2138 return m;
2141 if (gfc_match (" fmt =") == MATCH_YES)
2143 m = match_dt_format (dt);
2144 if (m != MATCH_NO)
2145 return m;
2148 if (gfc_match (" nml = %n", name) == MATCH_YES)
2150 if (dt->namelist != NULL)
2152 gfc_error ("Duplicate NML specification at %C");
2153 return MATCH_ERROR;
2156 if (gfc_find_symbol (name, NULL, 1, &sym))
2157 return MATCH_ERROR;
2159 if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
2161 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2162 sym != NULL ? sym->name : name);
2163 return MATCH_ERROR;
2166 dt->namelist = sym;
2167 if (k == M_READ && check_namelist (sym))
2168 return MATCH_ERROR;
2170 return MATCH_YES;
2173 m = match_etag (&tag_rec, &dt->rec);
2174 if (m != MATCH_NO)
2175 return m;
2176 m = match_etag (&tag_spos, &dt->rec);
2177 if (m != MATCH_NO)
2178 return m;
2179 m = match_out_tag (&tag_iomsg, &dt->iomsg);
2180 if (m != MATCH_NO)
2181 return m;
2182 m = match_out_tag (&tag_iostat, &dt->iostat);
2183 if (m != MATCH_NO)
2184 return m;
2185 m = match_ltag (&tag_err, &dt->err);
2186 if (m == MATCH_YES)
2187 dt->err_where = gfc_current_locus;
2188 if (m != MATCH_NO)
2189 return m;
2190 m = match_etag (&tag_advance, &dt->advance);
2191 if (m != MATCH_NO)
2192 return m;
2193 m = match_out_tag (&tag_size, &dt->size);
2194 if (m != MATCH_NO)
2195 return m;
2197 m = match_ltag (&tag_end, &dt->end);
2198 if (m == MATCH_YES)
2200 if (k == M_WRITE)
2202 gfc_error ("END tag at %C not allowed in output statement");
2203 return MATCH_ERROR;
2205 dt->end_where = gfc_current_locus;
2207 if (m != MATCH_NO)
2208 return m;
2210 m = match_ltag (&tag_eor, &dt->eor);
2211 if (m == MATCH_YES)
2212 dt->eor_where = gfc_current_locus;
2213 if (m != MATCH_NO)
2214 return m;
2216 return MATCH_NO;
2220 /* Free a data transfer structure and everything below it. */
2222 void
2223 gfc_free_dt (gfc_dt *dt)
2225 if (dt == NULL)
2226 return;
2228 gfc_free_expr (dt->io_unit);
2229 gfc_free_expr (dt->format_expr);
2230 gfc_free_expr (dt->rec);
2231 gfc_free_expr (dt->advance);
2232 gfc_free_expr (dt->iomsg);
2233 gfc_free_expr (dt->iostat);
2234 gfc_free_expr (dt->size);
2235 gfc_free (dt);
2239 /* Resolve everything in a gfc_dt structure. */
2242 gfc_resolve_dt (gfc_dt *dt)
2244 gfc_expr *e;
2246 RESOLVE_TAG (&tag_format, dt->format_expr);
2247 RESOLVE_TAG (&tag_rec, dt->rec);
2248 RESOLVE_TAG (&tag_spos, dt->rec);
2249 RESOLVE_TAG (&tag_advance, dt->advance);
2250 RESOLVE_TAG (&tag_iomsg, dt->iomsg);
2251 RESOLVE_TAG (&tag_iostat, dt->iostat);
2252 RESOLVE_TAG (&tag_size, dt->size);
2254 e = dt->io_unit;
2255 if (gfc_resolve_expr (e) == SUCCESS
2256 && (e->ts.type != BT_INTEGER
2257 && (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_VARIABLE)))
2259 gfc_error ("UNIT specification at %L must be an INTEGER expression "
2260 "or a CHARACTER variable", &e->where);
2261 return FAILURE;
2264 if (e->ts.type == BT_CHARACTER)
2266 if (gfc_has_vector_index (e))
2268 gfc_error ("Internal unit with vector subscript at %L", &e->where);
2269 return FAILURE;
2273 if (e->rank && e->ts.type != BT_CHARACTER)
2275 gfc_error ("External IO UNIT cannot be an array at %L", &e->where);
2276 return FAILURE;
2279 if (dt->err)
2281 if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
2282 return FAILURE;
2283 if (dt->err->defined == ST_LABEL_UNKNOWN)
2285 gfc_error ("ERR tag label %d at %L not defined",
2286 dt->err->value, &dt->err_where);
2287 return FAILURE;
2291 if (dt->end)
2293 if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
2294 return FAILURE;
2295 if (dt->end->defined == ST_LABEL_UNKNOWN)
2297 gfc_error ("END tag label %d at %L not defined",
2298 dt->end->value, &dt->end_where);
2299 return FAILURE;
2303 if (dt->eor)
2305 if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
2306 return FAILURE;
2307 if (dt->eor->defined == ST_LABEL_UNKNOWN)
2309 gfc_error ("EOR tag label %d at %L not defined",
2310 dt->eor->value, &dt->eor_where);
2311 return FAILURE;
2315 /* Check the format label actually exists. */
2316 if (dt->format_label && dt->format_label != &format_asterisk
2317 && dt->format_label->defined == ST_LABEL_UNKNOWN)
2319 gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
2320 &dt->format_label->where);
2321 return FAILURE;
2323 return SUCCESS;
2327 /* Given an io_kind, return its name. */
2329 static const char *
2330 io_kind_name (io_kind k)
2332 const char *name;
2334 switch (k)
2336 case M_READ:
2337 name = "READ";
2338 break;
2339 case M_WRITE:
2340 name = "WRITE";
2341 break;
2342 case M_PRINT:
2343 name = "PRINT";
2344 break;
2345 case M_INQUIRE:
2346 name = "INQUIRE";
2347 break;
2348 default:
2349 gfc_internal_error ("io_kind_name(): bad I/O-kind");
2352 return name;
2356 /* Match an IO iteration statement of the form:
2358 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2360 which is equivalent to a single IO element. This function is
2361 mutually recursive with match_io_element(). */
2363 static match match_io_element (io_kind, gfc_code **);
2365 static match
2366 match_io_iterator (io_kind k, gfc_code **result)
2368 gfc_code *head, *tail, *new;
2369 gfc_iterator *iter;
2370 locus old_loc;
2371 match m;
2372 int n;
2374 iter = NULL;
2375 head = NULL;
2376 old_loc = gfc_current_locus;
2378 if (gfc_match_char ('(') != MATCH_YES)
2379 return MATCH_NO;
2381 m = match_io_element (k, &head);
2382 tail = head;
2384 if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
2386 m = MATCH_NO;
2387 goto cleanup;
2390 /* Can't be anything but an IO iterator. Build a list. */
2391 iter = gfc_get_iterator ();
2393 for (n = 1;; n++)
2395 m = gfc_match_iterator (iter, 0);
2396 if (m == MATCH_ERROR)
2397 goto cleanup;
2398 if (m == MATCH_YES)
2400 gfc_check_do_variable (iter->var->symtree);
2401 break;
2404 m = match_io_element (k, &new);
2405 if (m == MATCH_ERROR)
2406 goto cleanup;
2407 if (m == MATCH_NO)
2409 if (n > 2)
2410 goto syntax;
2411 goto cleanup;
2414 tail = gfc_append_code (tail, new);
2416 if (gfc_match_char (',') != MATCH_YES)
2418 if (n > 2)
2419 goto syntax;
2420 m = MATCH_NO;
2421 goto cleanup;
2425 if (gfc_match_char (')') != MATCH_YES)
2426 goto syntax;
2428 new = gfc_get_code ();
2429 new->op = EXEC_DO;
2430 new->ext.iterator = iter;
2432 new->block = gfc_get_code ();
2433 new->block->op = EXEC_DO;
2434 new->block->next = head;
2436 *result = new;
2437 return MATCH_YES;
2439 syntax:
2440 gfc_error ("Syntax error in I/O iterator at %C");
2441 m = MATCH_ERROR;
2443 cleanup:
2444 gfc_free_iterator (iter, 1);
2445 gfc_free_statements (head);
2446 gfc_current_locus = old_loc;
2447 return m;
2451 /* Match a single element of an IO list, which is either a single
2452 expression or an IO Iterator. */
2454 static match
2455 match_io_element (io_kind k, gfc_code **cpp)
2457 gfc_expr *expr;
2458 gfc_code *cp;
2459 match m;
2461 expr = NULL;
2463 m = match_io_iterator (k, cpp);
2464 if (m == MATCH_YES)
2465 return MATCH_YES;
2467 if (k == M_READ)
2469 m = gfc_match_variable (&expr, 0);
2470 if (m == MATCH_NO)
2471 gfc_error ("Expected variable in READ statement at %C");
2473 else
2475 m = gfc_match_expr (&expr);
2476 if (m == MATCH_NO)
2477 gfc_error ("Expected expression in %s statement at %C",
2478 io_kind_name (k));
2481 if (m == MATCH_YES)
2482 switch (k)
2484 case M_READ:
2485 if (expr->symtree->n.sym->attr.intent == INTENT_IN)
2487 gfc_error ("Variable '%s' in input list at %C cannot be "
2488 "INTENT(IN)", expr->symtree->n.sym->name);
2489 m = MATCH_ERROR;
2492 if (gfc_pure (NULL)
2493 && gfc_impure_variable (expr->symtree->n.sym)
2494 && current_dt->io_unit->ts.type == BT_CHARACTER)
2496 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2497 expr->symtree->n.sym->name);
2498 m = MATCH_ERROR;
2501 if (gfc_check_do_variable (expr->symtree))
2502 m = MATCH_ERROR;
2504 break;
2506 case M_WRITE:
2507 if (current_dt->io_unit->ts.type == BT_CHARACTER
2508 && gfc_pure (NULL)
2509 && current_dt->io_unit->expr_type == EXPR_VARIABLE
2510 && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
2512 gfc_error ("Cannot write to internal file unit '%s' at %C "
2513 "inside a PURE procedure",
2514 current_dt->io_unit->symtree->n.sym->name);
2515 m = MATCH_ERROR;
2518 break;
2520 default:
2521 break;
2524 if (m != MATCH_YES)
2526 gfc_free_expr (expr);
2527 return MATCH_ERROR;
2530 cp = gfc_get_code ();
2531 cp->op = EXEC_TRANSFER;
2532 cp->expr = expr;
2534 *cpp = cp;
2535 return MATCH_YES;
2539 /* Match an I/O list, building gfc_code structures as we go. */
2541 static match
2542 match_io_list (io_kind k, gfc_code **head_p)
2544 gfc_code *head, *tail, *new;
2545 match m;
2547 *head_p = head = tail = NULL;
2548 if (gfc_match_eos () == MATCH_YES)
2549 return MATCH_YES;
2551 for (;;)
2553 m = match_io_element (k, &new);
2554 if (m == MATCH_ERROR)
2555 goto cleanup;
2556 if (m == MATCH_NO)
2557 goto syntax;
2559 tail = gfc_append_code (tail, new);
2560 if (head == NULL)
2561 head = new;
2563 if (gfc_match_eos () == MATCH_YES)
2564 break;
2565 if (gfc_match_char (',') != MATCH_YES)
2566 goto syntax;
2569 *head_p = head;
2570 return MATCH_YES;
2572 syntax:
2573 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2575 cleanup:
2576 gfc_free_statements (head);
2577 return MATCH_ERROR;
2581 /* Attach the data transfer end node. */
2583 static void
2584 terminate_io (gfc_code *io_code)
2586 gfc_code *c;
2588 if (io_code == NULL)
2589 io_code = new_st.block;
2591 c = gfc_get_code ();
2592 c->op = EXEC_DT_END;
2594 /* Point to structure that is already there */
2595 c->ext.dt = new_st.ext.dt;
2596 gfc_append_code (io_code, c);
2600 /* Check the constraints for a data transfer statement. The majority of the
2601 constraints appearing in 9.4 of the standard appear here. Some are handled
2602 in resolve_tag and others in gfc_resolve_dt. */
2604 static match
2605 check_io_constraints (io_kind k, gfc_dt *dt, gfc_code *io_code,
2606 locus *spec_end)
2608 #define io_constraint(condition,msg,arg)\
2609 if (condition) \
2611 gfc_error(msg,arg);\
2612 m = MATCH_ERROR;\
2615 match m;
2616 gfc_expr *expr;
2617 gfc_symbol *sym = NULL;
2619 m = MATCH_YES;
2621 expr = dt->io_unit;
2622 if (expr && expr->expr_type == EXPR_VARIABLE
2623 && expr->ts.type == BT_CHARACTER)
2625 sym = expr->symtree->n.sym;
2627 io_constraint (k == M_WRITE && sym->attr.intent == INTENT_IN,
2628 "Internal file at %L must not be INTENT(IN)",
2629 &expr->where);
2631 io_constraint (gfc_has_vector_index (dt->io_unit),
2632 "Internal file incompatible with vector subscript at %L",
2633 &expr->where);
2635 io_constraint (dt->rec != NULL,
2636 "REC tag at %L is incompatible with internal file",
2637 &dt->rec->where);
2639 if (dt->namelist != NULL)
2641 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Internal file "
2642 "at %L with namelist", &expr->where)
2643 == FAILURE)
2644 m = MATCH_ERROR;
2647 io_constraint (dt->advance != NULL,
2648 "ADVANCE tag at %L is incompatible with internal file",
2649 &dt->advance->where);
2652 if (expr && expr->ts.type != BT_CHARACTER)
2655 io_constraint (gfc_pure (NULL) && (k == M_READ || k == M_WRITE),
2656 "IO UNIT in %s statement at %C must be "
2657 "an internal file in a PURE procedure",
2658 io_kind_name (k));
2662 if (k != M_READ)
2664 io_constraint (dt->end, "END tag not allowed with output at %L",
2665 &dt->end_where);
2667 io_constraint (dt->eor, "EOR tag not allowed with output at %L",
2668 &dt->eor_where);
2670 io_constraint (k != M_READ && dt->size,
2671 "SIZE=specifier not allowed with output at %L",
2672 &dt->size->where);
2674 else
2676 io_constraint (dt->size && dt->advance == NULL,
2677 "SIZE tag at %L requires an ADVANCE tag",
2678 &dt->size->where);
2680 io_constraint (dt->eor && dt->advance == NULL,
2681 "EOR tag at %L requires an ADVANCE tag",
2682 &dt->eor_where);
2687 if (dt->namelist)
2689 io_constraint (io_code && dt->namelist,
2690 "NAMELIST cannot be followed by IO-list at %L",
2691 &io_code->loc);
2693 io_constraint (dt->format_expr,
2694 "IO spec-list cannot contain both NAMELIST group name "
2695 "and format specification at %L.",
2696 &dt->format_expr->where);
2698 io_constraint (dt->format_label,
2699 "IO spec-list cannot contain both NAMELIST group name "
2700 "and format label at %L", spec_end);
2702 io_constraint (dt->rec,
2703 "NAMELIST IO is not allowed with a REC=specifier "
2704 "at %L.", &dt->rec->where);
2706 io_constraint (dt->advance,
2707 "NAMELIST IO is not allowed with a ADVANCE=specifier "
2708 "at %L.", &dt->advance->where);
2711 if (dt->rec)
2713 io_constraint (dt->end,
2714 "An END tag is not allowed with a "
2715 "REC=specifier at %L.", &dt->end_where);
2718 io_constraint (dt->format_label == &format_asterisk,
2719 "FMT=* is not allowed with a REC=specifier "
2720 "at %L.", spec_end);
2723 if (dt->advance)
2725 int not_yes, not_no;
2726 expr = dt->advance;
2728 io_constraint (dt->format_label == &format_asterisk,
2729 "List directed format(*) is not allowed with a "
2730 "ADVANCE=specifier at %L.", &expr->where);
2732 io_constraint (dt->format_expr == NULL && dt->format_label == NULL
2733 && dt->namelist == NULL,
2734 "the ADVANCE=specifier at %L must appear with an "
2735 "explicit format expression", &expr->where);
2737 if (expr->expr_type == EXPR_CONSTANT && expr->ts.type == BT_CHARACTER)
2739 const char * advance = expr->value.character.string;
2740 not_no = strcasecmp (advance, "no") != 0;
2741 not_yes = strcasecmp (advance, "yes") != 0;
2743 else
2745 not_no = 0;
2746 not_yes = 0;
2749 io_constraint (not_no && not_yes,
2750 "ADVANCE=specifier at %L must have value = "
2751 "YES or NO.", &expr->where);
2753 io_constraint (dt->size && not_no && k == M_READ,
2754 "SIZE tag at %L requires an ADVANCE = 'NO'",
2755 &dt->size->where);
2757 io_constraint (dt->eor && not_no && k == M_READ,
2758 "EOR tag at %L requires an ADVANCE = 'NO'",
2759 &dt->eor_where);
2762 expr = dt->format_expr;
2763 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
2764 check_format_string (expr);
2766 return m;
2768 #undef io_constraint
2771 /* Match a READ, WRITE or PRINT statement. */
2773 static match
2774 match_io (io_kind k)
2776 char name[GFC_MAX_SYMBOL_LEN + 1];
2777 gfc_code *io_code;
2778 gfc_symbol *sym;
2779 int comma_flag, c;
2780 locus where;
2781 locus spec_end;
2782 gfc_dt *dt;
2783 match m;
2785 where = gfc_current_locus;
2786 comma_flag = 0;
2787 current_dt = dt = gfc_getmem (sizeof (gfc_dt));
2788 m = gfc_match_char ('(');
2789 if (m == MATCH_NO)
2791 where = gfc_current_locus;
2792 if (k == M_WRITE)
2793 goto syntax;
2794 else if (k == M_PRINT)
2796 /* Treat the non-standard case of PRINT namelist. */
2797 if ((gfc_current_form == FORM_FIXED || gfc_peek_char () == ' ')
2798 && gfc_match_name (name) == MATCH_YES)
2800 gfc_find_symbol (name, NULL, 1, &sym);
2801 if (sym && sym->attr.flavor == FL_NAMELIST)
2803 if (gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
2804 "%C is an extension") == FAILURE)
2806 m = MATCH_ERROR;
2807 goto cleanup;
2810 dt->io_unit = default_unit (k);
2811 dt->namelist = sym;
2812 goto get_io_list;
2814 else
2815 gfc_current_locus = where;
2819 if (gfc_current_form == FORM_FREE)
2821 c = gfc_peek_char();
2822 if (c != ' ' && c != '*' && c != '\'' && c != '"')
2824 m = MATCH_NO;
2825 goto cleanup;
2829 m = match_dt_format (dt);
2830 if (m == MATCH_ERROR)
2831 goto cleanup;
2832 if (m == MATCH_NO)
2833 goto syntax;
2835 comma_flag = 1;
2836 dt->io_unit = default_unit (k);
2837 goto get_io_list;
2839 else
2841 /* Before issuing an error for a malformed 'print (1,*)' type of
2842 error, check for a default-char-expr of the form ('(I0)'). */
2843 if (k == M_PRINT && m == MATCH_YES)
2845 /* Reset current locus to get the initial '(' in an expression. */
2846 gfc_current_locus = where;
2847 dt->format_expr = NULL;
2848 m = match_dt_format (dt);
2850 if (m == MATCH_ERROR)
2851 goto cleanup;
2852 if (m == MATCH_NO || dt->format_expr == NULL)
2853 goto syntax;
2855 comma_flag = 1;
2856 dt->io_unit = default_unit (k);
2857 goto get_io_list;
2861 /* Match a control list */
2862 if (match_dt_element (k, dt) == MATCH_YES)
2863 goto next;
2864 if (match_dt_unit (k, dt) != MATCH_YES)
2865 goto loop;
2867 if (gfc_match_char (')') == MATCH_YES)
2868 goto get_io_list;
2869 if (gfc_match_char (',') != MATCH_YES)
2870 goto syntax;
2872 m = match_dt_element (k, dt);
2873 if (m == MATCH_YES)
2874 goto next;
2875 if (m == MATCH_ERROR)
2876 goto cleanup;
2878 m = match_dt_format (dt);
2879 if (m == MATCH_YES)
2880 goto next;
2881 if (m == MATCH_ERROR)
2882 goto cleanup;
2884 where = gfc_current_locus;
2886 m = gfc_match_name (name);
2887 if (m == MATCH_YES)
2889 gfc_find_symbol (name, NULL, 1, &sym);
2890 if (sym && sym->attr.flavor == FL_NAMELIST)
2892 dt->namelist = sym;
2893 if (k == M_READ && check_namelist (sym))
2895 m = MATCH_ERROR;
2896 goto cleanup;
2898 goto next;
2902 gfc_current_locus = where;
2904 goto loop; /* No matches, try regular elements */
2906 next:
2907 if (gfc_match_char (')') == MATCH_YES)
2908 goto get_io_list;
2909 if (gfc_match_char (',') != MATCH_YES)
2910 goto syntax;
2912 loop:
2913 for (;;)
2915 m = match_dt_element (k, dt);
2916 if (m == MATCH_NO)
2917 goto syntax;
2918 if (m == MATCH_ERROR)
2919 goto cleanup;
2921 if (gfc_match_char (')') == MATCH_YES)
2922 break;
2923 if (gfc_match_char (',') != MATCH_YES)
2924 goto syntax;
2927 get_io_list:
2929 /* Used in check_io_constraints, where no locus is available. */
2930 spec_end = gfc_current_locus;
2932 /* Optional leading comma (non-standard). */
2933 if (!comma_flag
2934 && gfc_match_char (',') == MATCH_YES
2935 && k == M_WRITE
2936 && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before output "
2937 "item list at %C is an extension") == FAILURE)
2938 return MATCH_ERROR;
2940 io_code = NULL;
2941 if (gfc_match_eos () != MATCH_YES)
2943 if (comma_flag && gfc_match_char (',') != MATCH_YES)
2945 gfc_error ("Expected comma in I/O list at %C");
2946 m = MATCH_ERROR;
2947 goto cleanup;
2950 m = match_io_list (k, &io_code);
2951 if (m == MATCH_ERROR)
2952 goto cleanup;
2953 if (m == MATCH_NO)
2954 goto syntax;
2957 /* A full IO statement has been matched. Check the constraints. spec_end is
2958 supplied for cases where no locus is supplied. */
2959 m = check_io_constraints (k, dt, io_code, &spec_end);
2961 if (m == MATCH_ERROR)
2962 goto cleanup;
2964 new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
2965 new_st.ext.dt = dt;
2966 new_st.block = gfc_get_code ();
2967 new_st.block->op = new_st.op;
2968 new_st.block->next = io_code;
2970 terminate_io (io_code);
2972 return MATCH_YES;
2974 syntax:
2975 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2976 m = MATCH_ERROR;
2978 cleanup:
2979 gfc_free_dt (dt);
2980 return m;
2984 match
2985 gfc_match_read (void)
2987 return match_io (M_READ);
2990 match
2991 gfc_match_write (void)
2993 return match_io (M_WRITE);
2996 match
2997 gfc_match_print (void)
2999 match m;
3001 m = match_io (M_PRINT);
3002 if (m != MATCH_YES)
3003 return m;
3005 if (gfc_pure (NULL))
3007 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
3008 return MATCH_ERROR;
3011 return MATCH_YES;
3015 /* Free a gfc_inquire structure. */
3017 void
3018 gfc_free_inquire (gfc_inquire *inquire)
3021 if (inquire == NULL)
3022 return;
3024 gfc_free_expr (inquire->unit);
3025 gfc_free_expr (inquire->file);
3026 gfc_free_expr (inquire->iomsg);
3027 gfc_free_expr (inquire->iostat);
3028 gfc_free_expr (inquire->exist);
3029 gfc_free_expr (inquire->opened);
3030 gfc_free_expr (inquire->number);
3031 gfc_free_expr (inquire->named);
3032 gfc_free_expr (inquire->name);
3033 gfc_free_expr (inquire->access);
3034 gfc_free_expr (inquire->sequential);
3035 gfc_free_expr (inquire->direct);
3036 gfc_free_expr (inquire->form);
3037 gfc_free_expr (inquire->formatted);
3038 gfc_free_expr (inquire->unformatted);
3039 gfc_free_expr (inquire->recl);
3040 gfc_free_expr (inquire->nextrec);
3041 gfc_free_expr (inquire->blank);
3042 gfc_free_expr (inquire->position);
3043 gfc_free_expr (inquire->action);
3044 gfc_free_expr (inquire->read);
3045 gfc_free_expr (inquire->write);
3046 gfc_free_expr (inquire->readwrite);
3047 gfc_free_expr (inquire->delim);
3048 gfc_free_expr (inquire->pad);
3049 gfc_free_expr (inquire->iolength);
3050 gfc_free_expr (inquire->convert);
3051 gfc_free_expr (inquire->strm_pos);
3052 gfc_free (inquire);
3056 /* Match an element of an INQUIRE statement. */
3058 #define RETM if (m != MATCH_NO) return m;
3060 static match
3061 match_inquire_element (gfc_inquire *inquire)
3063 match m;
3065 m = match_etag (&tag_unit, &inquire->unit);
3066 RETM m = match_etag (&tag_file, &inquire->file);
3067 RETM m = match_ltag (&tag_err, &inquire->err);
3068 RETM m = match_out_tag (&tag_iomsg, &inquire->iomsg);
3069 RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
3070 RETM m = match_vtag (&tag_exist, &inquire->exist);
3071 RETM m = match_vtag (&tag_opened, &inquire->opened);
3072 RETM m = match_vtag (&tag_named, &inquire->named);
3073 RETM m = match_vtag (&tag_name, &inquire->name);
3074 RETM m = match_out_tag (&tag_number, &inquire->number);
3075 RETM m = match_vtag (&tag_s_access, &inquire->access);
3076 RETM m = match_vtag (&tag_sequential, &inquire->sequential);
3077 RETM m = match_vtag (&tag_direct, &inquire->direct);
3078 RETM m = match_vtag (&tag_s_form, &inquire->form);
3079 RETM m = match_vtag (&tag_formatted, &inquire->formatted);
3080 RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
3081 RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
3082 RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
3083 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
3084 RETM m = match_vtag (&tag_s_position, &inquire->position);
3085 RETM m = match_vtag (&tag_s_action, &inquire->action);
3086 RETM m = match_vtag (&tag_read, &inquire->read);
3087 RETM m = match_vtag (&tag_write, &inquire->write);
3088 RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
3089 RETM m = match_vtag (&tag_s_delim, &inquire->delim);
3090 RETM m = match_vtag (&tag_s_pad, &inquire->pad);
3091 RETM m = match_vtag (&tag_iolength, &inquire->iolength);
3092 RETM m = match_vtag (&tag_convert, &inquire->convert);
3093 RETM m = match_out_tag (&tag_strm_out, &inquire->strm_pos);
3094 RETM return MATCH_NO;
3097 #undef RETM
3100 match
3101 gfc_match_inquire (void)
3103 gfc_inquire *inquire;
3104 gfc_code *code;
3105 match m;
3106 locus loc;
3108 m = gfc_match_char ('(');
3109 if (m == MATCH_NO)
3110 return m;
3112 inquire = gfc_getmem (sizeof (gfc_inquire));
3114 loc = gfc_current_locus;
3116 m = match_inquire_element (inquire);
3117 if (m == MATCH_ERROR)
3118 goto cleanup;
3119 if (m == MATCH_NO)
3121 m = gfc_match_expr (&inquire->unit);
3122 if (m == MATCH_ERROR)
3123 goto cleanup;
3124 if (m == MATCH_NO)
3125 goto syntax;
3128 /* See if we have the IOLENGTH form of the inquire statement. */
3129 if (inquire->iolength != NULL)
3131 if (gfc_match_char (')') != MATCH_YES)
3132 goto syntax;
3134 m = match_io_list (M_INQUIRE, &code);
3135 if (m == MATCH_ERROR)
3136 goto cleanup;
3137 if (m == MATCH_NO)
3138 goto syntax;
3140 new_st.op = EXEC_IOLENGTH;
3141 new_st.expr = inquire->iolength;
3142 new_st.ext.inquire = inquire;
3144 if (gfc_pure (NULL))
3146 gfc_free_statements (code);
3147 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3148 return MATCH_ERROR;
3151 new_st.block = gfc_get_code ();
3152 new_st.block->op = EXEC_IOLENGTH;
3153 terminate_io (code);
3154 new_st.block->next = code;
3155 return MATCH_YES;
3158 /* At this point, we have the non-IOLENGTH inquire statement. */
3159 for (;;)
3161 if (gfc_match_char (')') == MATCH_YES)
3162 break;
3163 if (gfc_match_char (',') != MATCH_YES)
3164 goto syntax;
3166 m = match_inquire_element (inquire);
3167 if (m == MATCH_ERROR)
3168 goto cleanup;
3169 if (m == MATCH_NO)
3170 goto syntax;
3172 if (inquire->iolength != NULL)
3174 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3175 goto cleanup;
3179 if (gfc_match_eos () != MATCH_YES)
3180 goto syntax;
3182 if (inquire->unit != NULL && inquire->file != NULL)
3184 gfc_error ("INQUIRE statement at %L cannot contain both FILE and "
3185 "UNIT specifiers", &loc);
3186 goto cleanup;
3189 if (inquire->unit == NULL && inquire->file == NULL)
3191 gfc_error ("INQUIRE statement at %L requires either FILE or "
3192 "UNIT specifier", &loc);
3193 goto cleanup;
3196 if (gfc_pure (NULL))
3198 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3199 goto cleanup;
3202 new_st.op = EXEC_INQUIRE;
3203 new_st.ext.inquire = inquire;
3204 return MATCH_YES;
3206 syntax:
3207 gfc_syntax_error (ST_INQUIRE);
3209 cleanup:
3210 gfc_free_inquire (inquire);
3211 return MATCH_ERROR;
3215 /* Resolve everything in a gfc_inquire structure. */
3218 gfc_resolve_inquire (gfc_inquire *inquire)
3220 RESOLVE_TAG (&tag_unit, inquire->unit);
3221 RESOLVE_TAG (&tag_file, inquire->file);
3222 RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
3223 RESOLVE_TAG (&tag_iostat, inquire->iostat);
3224 RESOLVE_TAG (&tag_exist, inquire->exist);
3225 RESOLVE_TAG (&tag_opened, inquire->opened);
3226 RESOLVE_TAG (&tag_number, inquire->number);
3227 RESOLVE_TAG (&tag_named, inquire->named);
3228 RESOLVE_TAG (&tag_name, inquire->name);
3229 RESOLVE_TAG (&tag_s_access, inquire->access);
3230 RESOLVE_TAG (&tag_sequential, inquire->sequential);
3231 RESOLVE_TAG (&tag_direct, inquire->direct);
3232 RESOLVE_TAG (&tag_s_form, inquire->form);
3233 RESOLVE_TAG (&tag_formatted, inquire->formatted);
3234 RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
3235 RESOLVE_TAG (&tag_s_recl, inquire->recl);
3236 RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
3237 RESOLVE_TAG (&tag_s_blank, inquire->blank);
3238 RESOLVE_TAG (&tag_s_position, inquire->position);
3239 RESOLVE_TAG (&tag_s_action, inquire->action);
3240 RESOLVE_TAG (&tag_read, inquire->read);
3241 RESOLVE_TAG (&tag_write, inquire->write);
3242 RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
3243 RESOLVE_TAG (&tag_s_delim, inquire->delim);
3244 RESOLVE_TAG (&tag_s_pad, inquire->pad);
3245 RESOLVE_TAG (&tag_iolength, inquire->iolength);
3246 RESOLVE_TAG (&tag_convert, inquire->convert);
3247 RESOLVE_TAG (&tag_strm_out, inquire->strm_pos);
3249 if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)
3250 return FAILURE;
3252 return SUCCESS;