* config/alpha/alpha.md, arm/arm.c, darwin.c, frv/frv.md,
[official-gcc.git] / gcc / fortran / io.c
blobcb424c4877965d7b059030e47cbd603218747360
1 /* Deal with I/O statements & related stuff.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
3 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 format_asterisk =
31 {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';
141 if (mode == MODE_COPY)
142 *format_string++ = c;
145 c = TOUPPER (c);
146 return c;
150 /* Back up one character position. Only works once. */
152 static void
153 unget_char (void)
156 use_last_char = 1;
159 /* Eat up the spaces and return a character. */
161 static char
162 next_char_not_space(void)
164 char c;
167 c = next_char (0);
169 while (gfc_is_whitespace (c));
170 return c;
173 static int value = 0;
175 /* Simple lexical analyzer for getting the next token in a FORMAT
176 statement. */
178 static format_token
179 format_lex (void)
181 format_token token;
182 char c, delim;
183 int zflag;
184 int negative_flag;
186 if (saved_token != FMT_NONE)
188 token = saved_token;
189 saved_token = FMT_NONE;
190 return token;
193 c = next_char_not_space ();
195 negative_flag = 0;
196 switch (c)
198 case '-':
199 negative_flag = 1;
200 case '+':
201 c = next_char_not_space ();
202 if (!ISDIGIT (c))
204 token = FMT_UNKNOWN;
205 break;
208 value = c - '0';
212 c = next_char_not_space ();
213 if(ISDIGIT (c))
214 value = 10 * value + c - '0';
216 while (ISDIGIT (c));
218 unget_char ();
220 if (negative_flag)
221 value = -value;
223 token = FMT_SIGNED_INT;
224 break;
226 case '0':
227 case '1':
228 case '2':
229 case '3':
230 case '4':
231 case '5':
232 case '6':
233 case '7':
234 case '8':
235 case '9':
236 zflag = (c == '0');
238 value = c - '0';
242 c = next_char_not_space ();
243 if (c != '0')
244 zflag = 0;
245 if (ISDIGIT (c))
246 value = 10 * value + c - '0';
248 while (ISDIGIT (c));
250 unget_char ();
251 token = zflag ? FMT_ZERO : FMT_POSINT;
252 break;
254 case '.':
255 token = FMT_PERIOD;
256 break;
258 case ',':
259 token = FMT_COMMA;
260 break;
262 case ':':
263 token = FMT_COLON;
264 break;
266 case '/':
267 token = FMT_SLASH;
268 break;
270 case '$':
271 token = FMT_DOLLAR;
272 break;
274 case 'T':
275 c = next_char_not_space ();
276 if (c != 'L' && c != 'R')
277 unget_char ();
279 token = FMT_POS;
280 break;
282 case '(':
283 token = FMT_LPAREN;
284 break;
286 case ')':
287 token = FMT_RPAREN;
288 break;
290 case 'X':
291 token = FMT_X;
292 break;
294 case 'S':
295 c = next_char_not_space ();
296 if (c != 'P' && c != 'S')
297 unget_char ();
299 token = FMT_SIGN;
300 break;
302 case 'B':
303 c = next_char_not_space ();
304 if (c == 'N' || c == 'Z')
305 token = FMT_BLANK;
306 else
308 unget_char ();
309 token = FMT_IBOZ;
312 break;
314 case '\'':
315 case '"':
316 delim = c;
318 value = 0;
320 for (;;)
322 c = next_char (1);
323 if (c == '\0')
325 token = FMT_END;
326 break;
329 if (c == delim)
331 c = next_char (1);
333 if (c == '\0')
335 token = FMT_END;
336 break;
339 if (c != delim)
341 unget_char ();
342 token = FMT_CHAR;
343 break;
346 value++;
348 break;
350 case 'P':
351 token = FMT_P;
352 break;
354 case 'I':
355 case 'O':
356 case 'Z':
357 token = FMT_IBOZ;
358 break;
360 case 'F':
361 token = FMT_F;
362 break;
364 case 'E':
365 c = next_char_not_space ();
366 if (c == 'N' || c == 'S')
367 token = FMT_EXT;
368 else
370 token = FMT_E;
371 unget_char ();
374 break;
376 case 'G':
377 token = FMT_G;
378 break;
380 case 'H':
381 token = FMT_H;
382 break;
384 case 'L':
385 token = FMT_L;
386 break;
388 case 'A':
389 token = FMT_A;
390 break;
392 case 'D':
393 token = FMT_D;
394 break;
396 case '\0':
397 token = FMT_END;
398 break;
400 default:
401 token = FMT_UNKNOWN;
402 break;
405 return token;
409 /* Check a format statement. The format string, either from a FORMAT
410 statement or a constant in an I/O statement has already been parsed
411 by itself, and we are checking it for validity. The dual origin
412 means that the warning message is a little less than great. */
414 static try
415 check_format (void)
417 const char *posint_required = _("Positive width required");
418 const char *nonneg_required = _("Nonnegative width required");
419 const char *unexpected_element = _("Unexpected element");
420 const char *unexpected_end = _("Unexpected end of format string");
422 const char *error;
423 format_token t, u;
424 int level;
425 int repeat;
426 try rv;
428 use_last_char = 0;
429 saved_token = FMT_NONE;
430 level = 0;
431 repeat = 0;
432 rv = SUCCESS;
434 t = format_lex ();
435 if (t != FMT_LPAREN)
437 error = _("Missing leading left parenthesis");
438 goto syntax;
441 t = format_lex ();
442 if (t == FMT_RPAREN)
443 goto finished; /* Empty format is legal */
444 saved_token = t;
446 format_item:
447 /* In this state, the next thing has to be a format item. */
448 t = format_lex ();
449 format_item_1:
450 switch (t)
452 case FMT_POSINT:
453 repeat = value;
454 t = format_lex ();
455 if (t == FMT_LPAREN)
457 level++;
458 goto format_item;
461 if (t == FMT_SLASH)
462 goto optional_comma;
464 goto data_desc;
466 case FMT_LPAREN:
467 level++;
468 goto format_item;
470 case FMT_SIGNED_INT:
471 /* Signed integer can only precede a P format. */
472 t = format_lex ();
473 if (t != FMT_P)
475 error = _("Expected P edit descriptor");
476 goto syntax;
479 goto data_desc;
481 case FMT_P:
482 /* P requires a prior number. */
483 error = _("P descriptor requires leading scale factor");
484 goto syntax;
486 case FMT_X:
487 /* X requires a prior number if we're being pedantic. */
488 if (gfc_notify_std (GFC_STD_GNU, "Extension: X descriptor "
489 "requires leading space count at %C")
490 == FAILURE)
491 return FAILURE;
492 goto between_desc;
494 case FMT_SIGN:
495 case FMT_BLANK:
496 goto between_desc;
498 case FMT_CHAR:
499 goto extension_optional_comma;
501 case FMT_COLON:
502 case FMT_SLASH:
503 goto optional_comma;
505 case FMT_DOLLAR:
506 t = format_lex ();
508 if (gfc_notify_std (GFC_STD_GNU, "Extension: $ descriptor at %C")
509 == FAILURE)
510 return FAILURE;
511 if (t != FMT_RPAREN || level > 0)
513 gfc_warning ("$ should be the last specifier in format at %C");
514 goto optional_comma_1;
517 goto finished;
519 case FMT_POS:
520 case FMT_IBOZ:
521 case FMT_F:
522 case FMT_E:
523 case FMT_EXT:
524 case FMT_G:
525 case FMT_L:
526 case FMT_A:
527 case FMT_D:
528 goto data_desc;
530 case FMT_H:
531 goto data_desc;
533 case FMT_END:
534 error = unexpected_end;
535 goto syntax;
537 default:
538 error = unexpected_element;
539 goto syntax;
542 data_desc:
543 /* In this state, t must currently be a data descriptor.
544 Deal with things that can/must follow the descriptor. */
545 switch (t)
547 case FMT_SIGN:
548 case FMT_BLANK:
549 case FMT_X:
550 break;
552 case FMT_P:
553 if (pedantic)
555 t = format_lex ();
556 if (t == FMT_POSINT)
558 error = _("Repeat count cannot follow P descriptor");
559 goto syntax;
562 saved_token = t;
565 goto optional_comma;
567 case FMT_POS:
568 case FMT_L:
569 t = format_lex ();
570 if (t == FMT_POSINT)
571 break;
573 switch (gfc_notification_std (GFC_STD_GNU))
575 case WARNING:
576 gfc_warning
577 ("Extension: Missing positive width after L descriptor at %C");
578 saved_token = t;
579 break;
581 case ERROR:
582 error = posint_required;
583 goto syntax;
585 case SILENT:
586 saved_token = t;
587 break;
589 default:
590 gcc_unreachable ();
592 break;
594 case FMT_A:
595 t = format_lex ();
596 if (t != FMT_POSINT)
597 saved_token = t;
598 break;
600 case FMT_D:
601 case FMT_E:
602 case FMT_G:
603 case FMT_EXT:
604 u = format_lex ();
605 if (u != FMT_POSINT)
607 error = posint_required;
608 goto syntax;
611 u = format_lex ();
612 if (u != FMT_PERIOD)
614 /* Warn if -std=legacy, otherwise error. */
615 if (gfc_option.warn_std != 0)
616 gfc_error_now ("Period required in format specifier at %C");
617 else
618 gfc_warning ("Period required in format specifier at %C");
619 saved_token = u;
620 break;
623 u = format_lex ();
624 if (u != FMT_ZERO && u != FMT_POSINT)
626 error = nonneg_required;
627 goto syntax;
630 if (t == FMT_D)
631 break;
633 /* Look for optional exponent. */
634 u = format_lex ();
635 if (u != FMT_E)
637 saved_token = u;
639 else
641 u = format_lex ();
642 if (u != FMT_POSINT)
644 error = _("Positive exponent width required");
645 goto syntax;
649 break;
651 case FMT_F:
652 t = format_lex ();
653 if (t != FMT_ZERO && t != FMT_POSINT)
655 error = nonneg_required;
656 goto syntax;
659 t = format_lex ();
660 if (t != FMT_PERIOD)
662 /* Warn if -std=legacy, otherwise error. */
663 if (gfc_option.warn_std != 0)
664 gfc_error_now ("Period required in format specifier at %C");
665 else
666 gfc_warning ("Period required in format specifier at %C");
667 saved_token = t;
668 break;
671 t = format_lex ();
672 if (t != FMT_ZERO && t != FMT_POSINT)
674 error = nonneg_required;
675 goto syntax;
678 break;
680 case FMT_H:
681 if(mode == MODE_STRING)
683 format_string += value;
684 format_length -= value;
686 else
688 while(repeat >0)
690 next_char(1);
691 repeat -- ;
694 break;
696 case FMT_IBOZ:
697 t = format_lex ();
698 if (t != FMT_ZERO && t != FMT_POSINT)
700 error = nonneg_required;
701 goto syntax;
704 t = format_lex ();
705 if (t != FMT_PERIOD)
707 saved_token = t;
709 else
711 t = format_lex ();
712 if (t != FMT_ZERO && t != FMT_POSINT)
714 error = nonneg_required;
715 goto syntax;
719 break;
721 default:
722 error = unexpected_element;
723 goto syntax;
726 between_desc:
727 /* Between a descriptor and what comes next. */
728 t = format_lex ();
729 switch (t)
732 case FMT_COMMA:
733 goto format_item;
735 case FMT_RPAREN:
736 level--;
737 if (level < 0)
738 goto finished;
739 goto between_desc;
741 case FMT_COLON:
742 case FMT_SLASH:
743 goto optional_comma;
745 case FMT_END:
746 error = unexpected_end;
747 goto syntax;
749 default:
750 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
751 == FAILURE)
752 return FAILURE;
753 goto format_item_1;
756 optional_comma:
757 /* Optional comma is a weird between state where we've just finished
758 reading a colon, slash, dollar or P descriptor. */
759 t = format_lex ();
760 optional_comma_1:
761 switch (t)
763 case FMT_COMMA:
764 break;
766 case FMT_RPAREN:
767 level--;
768 if (level < 0)
769 goto finished;
770 goto between_desc;
772 default:
773 /* Assume that we have another format item. */
774 saved_token = t;
775 break;
778 goto format_item;
780 extension_optional_comma:
781 /* As a GNU extension, permit a missing comma after a string literal. */
782 t = format_lex ();
783 switch (t)
785 case FMT_COMMA:
786 break;
788 case FMT_RPAREN:
789 level--;
790 if (level < 0)
791 goto finished;
792 goto between_desc;
794 case FMT_COLON:
795 case FMT_SLASH:
796 goto optional_comma;
798 case FMT_END:
799 error = unexpected_end;
800 goto syntax;
802 default:
803 if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
804 == FAILURE)
805 return FAILURE;
806 saved_token = t;
807 break;
810 goto format_item;
812 syntax:
813 /* Something went wrong. If the format we're checking is a string,
814 generate a warning, since the program is correct. If the format
815 is in a FORMAT statement, this messes up parsing, which is an
816 error. */
817 if (mode != MODE_STRING)
818 gfc_error ("%s in format string at %C", error);
819 else
821 gfc_warning ("%s in format string at %C", error);
823 /* TODO: More elaborate measures are needed to show where a problem
824 is within a format string that has been calculated. */
827 rv = FAILURE;
829 finished:
830 return rv;
834 /* Given an expression node that is a constant string, see if it looks
835 like a format string. */
837 static void
838 check_format_string (gfc_expr * e)
841 mode = MODE_STRING;
842 format_string = e->value.character.string;
843 check_format ();
847 /************ Fortran 95 I/O statement matchers *************/
849 /* Match a FORMAT statement. This amounts to actually parsing the
850 format descriptors in order to correctly locate the end of the
851 format string. */
853 match
854 gfc_match_format (void)
856 gfc_expr *e;
857 locus start;
859 if (gfc_current_ns->proc_name
860 && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
862 gfc_error ("Format statement in module main block at %C");
863 return MATCH_ERROR;
866 if (gfc_statement_label == NULL)
868 gfc_error ("Missing format label at %C");
869 return MATCH_ERROR;
871 gfc_gobble_whitespace ();
873 mode = MODE_FORMAT;
874 format_length = 0;
876 start = gfc_current_locus;
878 if (check_format () == FAILURE)
879 return MATCH_ERROR;
881 if (gfc_match_eos () != MATCH_YES)
883 gfc_syntax_error (ST_FORMAT);
884 return MATCH_ERROR;
887 /* The label doesn't get created until after the statement is done
888 being matched, so we have to leave the string for later. */
890 gfc_current_locus = start; /* Back to the beginning */
892 new_st.loc = start;
893 new_st.op = EXEC_NOP;
895 e = gfc_get_expr();
896 e->expr_type = EXPR_CONSTANT;
897 e->ts.type = BT_CHARACTER;
898 e->ts.kind = gfc_default_character_kind;
899 e->where = start;
900 e->value.character.string = format_string = gfc_getmem(format_length+1);
901 e->value.character.length = format_length;
902 gfc_statement_label->format = e;
904 mode = MODE_COPY;
905 check_format (); /* Guaranteed to succeed */
906 gfc_match_eos (); /* Guaranteed to succeed */
908 return MATCH_YES;
912 /* Match an expression I/O tag of some sort. */
914 static match
915 match_etag (const io_tag * tag, gfc_expr ** v)
917 gfc_expr *result;
918 match m;
920 m = gfc_match (tag->spec, &result);
921 if (m != MATCH_YES)
922 return m;
924 if (*v != NULL)
926 gfc_error ("Duplicate %s specification at %C", tag->name);
927 gfc_free_expr (result);
928 return MATCH_ERROR;
931 *v = result;
932 return MATCH_YES;
936 /* Match a variable I/O tag of some sort. */
938 static match
939 match_vtag (const io_tag * tag, gfc_expr ** v)
941 gfc_expr *result;
942 match m;
944 m = gfc_match (tag->spec, &result);
945 if (m != MATCH_YES)
946 return m;
948 if (*v != NULL)
950 gfc_error ("Duplicate %s specification at %C", tag->name);
951 gfc_free_expr (result);
952 return MATCH_ERROR;
955 if (result->symtree->n.sym->attr.intent == INTENT_IN)
957 gfc_error ("Variable tag cannot be INTENT(IN) at %C");
958 gfc_free_expr (result);
959 return MATCH_ERROR;
962 if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
964 gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
965 gfc_free_expr (result);
966 return MATCH_ERROR;
969 *v = result;
970 return MATCH_YES;
974 /* Match I/O tags that cause variables to become redefined. */
976 static match
977 match_out_tag(const io_tag *tag, gfc_expr **result)
979 match m;
981 m = match_vtag(tag, result);
982 if (m == MATCH_YES)
983 gfc_check_do_variable((*result)->symtree);
985 return m;
989 /* Match a label I/O tag. */
991 static match
992 match_ltag (const io_tag * tag, gfc_st_label ** label)
994 match m;
995 gfc_st_label *old;
997 old = *label;
998 m = gfc_match (tag->spec, label);
999 if (m == MATCH_YES && old != 0)
1001 gfc_error ("Duplicate %s label specification at %C", tag->name);
1002 return MATCH_ERROR;
1005 if (m == MATCH_YES
1006 && gfc_reference_st_label (*label, ST_LABEL_TARGET) == FAILURE)
1007 return MATCH_ERROR;
1009 return m;
1013 /* Do expression resolution and type-checking on an expression tag. */
1015 static try
1016 resolve_tag (const io_tag * tag, gfc_expr * e)
1019 if (e == NULL)
1020 return SUCCESS;
1022 if (gfc_resolve_expr (e) == FAILURE)
1023 return FAILURE;
1025 if (e->ts.type != tag->type && tag != &tag_format)
1027 gfc_error ("%s tag at %L must be of type %s", tag->name,
1028 &e->where, gfc_basic_typename (tag->type));
1029 return FAILURE;
1032 if (tag == &tag_format)
1034 if (e->expr_type == EXPR_CONSTANT
1035 && (e->ts.type != BT_CHARACTER
1036 || e->ts.kind != gfc_default_character_kind))
1038 gfc_error ("Constant expression in FORMAT tag at %L must be "
1039 "of type default CHARACTER", &e->where);
1040 return FAILURE;
1043 /* If e's rank is zero and e is not an element of an array, it should be
1044 of integer or character type. The integer variable should be
1045 ASSIGNED. */
1046 if (e->symtree == NULL || e->symtree->n.sym->as == NULL
1047 || e->symtree->n.sym->as->rank == 0)
1049 if (e->ts.type != BT_CHARACTER && e->ts.type != BT_INTEGER)
1051 gfc_error ("%s tag at %L must be of type %s or %s", tag->name,
1052 &e->where, gfc_basic_typename (BT_CHARACTER),
1053 gfc_basic_typename (BT_INTEGER));
1054 return FAILURE;
1056 else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
1058 if (gfc_notify_std (GFC_STD_F95_DEL,
1059 "Obsolete: ASSIGNED variable in FORMAT tag at %L",
1060 &e->where) == FAILURE)
1061 return FAILURE;
1062 if (e->symtree->n.sym->attr.assign != 1)
1064 gfc_error ("Variable '%s' at %L has not been assigned a "
1065 "format label", e->symtree->n.sym->name, &e->where);
1066 return FAILURE;
1069 else if (e->ts.type == BT_INTEGER)
1071 gfc_error ("scalar '%s' FORMAT tag at %L is not an ASSIGNED "
1072 "variable", gfc_basic_typename (e->ts.type), &e->where);
1073 return FAILURE;
1076 return SUCCESS;
1078 else
1080 /* if rank is nonzero, we allow the type to be character under
1081 GFC_STD_GNU and other type under GFC_STD_LEGACY. It may be
1082 assigned an Hollerith constant. */
1083 if (e->ts.type == BT_CHARACTER)
1085 if (gfc_notify_std (GFC_STD_GNU,
1086 "Extension: Character array in FORMAT tag at %L",
1087 &e->where) == FAILURE)
1088 return FAILURE;
1090 else
1092 if (gfc_notify_std (GFC_STD_LEGACY,
1093 "Extension: Non-character in FORMAT tag at %L",
1094 &e->where) == FAILURE)
1095 return FAILURE;
1097 return SUCCESS;
1100 else
1102 if (e->rank != 0)
1104 gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
1105 return FAILURE;
1108 if (tag == &tag_iomsg)
1110 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IOMSG tag at %L",
1111 &e->where) == FAILURE)
1112 return FAILURE;
1115 if (tag == &tag_iostat && e->ts.kind != gfc_default_integer_kind)
1117 if (gfc_notify_std (GFC_STD_GNU, "Fortran 95 requires default "
1118 "INTEGER in IOSTAT tag at %L",
1119 &e->where) == FAILURE)
1120 return FAILURE;
1123 if (tag == &tag_size && e->ts.kind != gfc_default_integer_kind)
1125 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1126 "INTEGER in SIZE tag at %L",
1127 &e->where) == FAILURE)
1128 return FAILURE;
1131 if (tag == &tag_convert)
1133 if (gfc_notify_std (GFC_STD_GNU, "Extension: CONVERT tag at %L",
1134 &e->where) == FAILURE)
1135 return FAILURE;
1138 if (tag == &tag_iolength && e->ts.kind != gfc_default_integer_kind)
1140 if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1141 "INTEGER in IOLENGTH tag at %L",
1142 &e->where) == FAILURE)
1143 return FAILURE;
1147 return SUCCESS;
1151 /* Match a single tag of an OPEN statement. */
1153 static match
1154 match_open_element (gfc_open * open)
1156 match m;
1158 m = match_etag (&tag_unit, &open->unit);
1159 if (m != MATCH_NO)
1160 return m;
1161 m = match_out_tag (&tag_iomsg, &open->iomsg);
1162 if (m != MATCH_NO)
1163 return m;
1164 m = match_out_tag (&tag_iostat, &open->iostat);
1165 if (m != MATCH_NO)
1166 return m;
1167 m = match_etag (&tag_file, &open->file);
1168 if (m != MATCH_NO)
1169 return m;
1170 m = match_etag (&tag_status, &open->status);
1171 if (m != MATCH_NO)
1172 return m;
1173 m = match_etag (&tag_e_access, &open->access);
1174 if (m != MATCH_NO)
1175 return m;
1176 m = match_etag (&tag_e_form, &open->form);
1177 if (m != MATCH_NO)
1178 return m;
1179 m = match_etag (&tag_e_recl, &open->recl);
1180 if (m != MATCH_NO)
1181 return m;
1182 m = match_etag (&tag_e_blank, &open->blank);
1183 if (m != MATCH_NO)
1184 return m;
1185 m = match_etag (&tag_e_position, &open->position);
1186 if (m != MATCH_NO)
1187 return m;
1188 m = match_etag (&tag_e_action, &open->action);
1189 if (m != MATCH_NO)
1190 return m;
1191 m = match_etag (&tag_e_delim, &open->delim);
1192 if (m != MATCH_NO)
1193 return m;
1194 m = match_etag (&tag_e_pad, &open->pad);
1195 if (m != MATCH_NO)
1196 return m;
1197 m = match_ltag (&tag_err, &open->err);
1198 if (m != MATCH_NO)
1199 return m;
1200 m = match_etag (&tag_convert, &open->convert);
1201 if (m != MATCH_NO)
1202 return m;
1204 return MATCH_NO;
1208 /* Free the gfc_open structure and all the expressions it contains. */
1210 void
1211 gfc_free_open (gfc_open * open)
1214 if (open == NULL)
1215 return;
1217 gfc_free_expr (open->unit);
1218 gfc_free_expr (open->iomsg);
1219 gfc_free_expr (open->iostat);
1220 gfc_free_expr (open->file);
1221 gfc_free_expr (open->status);
1222 gfc_free_expr (open->access);
1223 gfc_free_expr (open->form);
1224 gfc_free_expr (open->recl);
1225 gfc_free_expr (open->blank);
1226 gfc_free_expr (open->position);
1227 gfc_free_expr (open->action);
1228 gfc_free_expr (open->delim);
1229 gfc_free_expr (open->pad);
1230 gfc_free_expr (open->convert);
1232 gfc_free (open);
1236 /* Resolve everything in a gfc_open structure. */
1239 gfc_resolve_open (gfc_open * open)
1242 RESOLVE_TAG (&tag_unit, open->unit);
1243 RESOLVE_TAG (&tag_iomsg, open->iomsg);
1244 RESOLVE_TAG (&tag_iostat, open->iostat);
1245 RESOLVE_TAG (&tag_file, open->file);
1246 RESOLVE_TAG (&tag_status, open->status);
1247 RESOLVE_TAG (&tag_e_access, open->access);
1248 RESOLVE_TAG (&tag_e_form, open->form);
1249 RESOLVE_TAG (&tag_e_recl, open->recl);
1251 RESOLVE_TAG (&tag_e_blank, open->blank);
1252 RESOLVE_TAG (&tag_e_position, open->position);
1253 RESOLVE_TAG (&tag_e_action, open->action);
1254 RESOLVE_TAG (&tag_e_delim, open->delim);
1255 RESOLVE_TAG (&tag_e_pad, open->pad);
1256 RESOLVE_TAG (&tag_convert, open->convert);
1258 if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1259 return FAILURE;
1261 return SUCCESS;
1266 /* Check if a given value for a SPECIFIER is either in the list of values
1267 allowed in F95 or F2003, issuing an error message and returning a zero
1268 value if it is not allowed. */
1269 static int
1270 compare_to_allowed_values (const char * specifier, const char * allowed[],
1271 const char * allowed_f2003[],
1272 const char * allowed_gnu[], char * value,
1273 const char * statement, bool warn)
1275 int i;
1276 unsigned int len;
1278 len = strlen(value);
1279 if (len > 0)
1281 for (len--; len > 0; len--)
1282 if (value[len] != ' ')
1283 break;
1284 len++;
1287 for (i = 0; allowed[i]; i++)
1288 if (len == strlen(allowed[i])
1289 && strncasecmp (value, allowed[i], strlen(allowed[i])) == 0)
1290 return 1;
1292 for (i = 0; allowed_f2003 && allowed_f2003[i]; i++)
1293 if (len == strlen(allowed_f2003[i])
1294 && strncasecmp (value, allowed_f2003[i], strlen(allowed_f2003[i])) == 0)
1296 notification n = gfc_notification_std (GFC_STD_F2003);
1298 if (n == WARNING || (warn && n == ERROR))
1300 gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1301 "has value '%s'", specifier, statement,
1302 allowed_f2003[i]);
1303 return 1;
1305 else
1306 if (n == ERROR)
1308 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: %s specifier in "
1309 "%s statement at %C has value '%s'", specifier,
1310 statement, allowed_f2003[i]);
1311 return 0;
1314 /* n == SILENT */
1315 return 1;
1318 for (i = 0; allowed_gnu && allowed_gnu[i]; i++)
1319 if (len == strlen(allowed_gnu[i])
1320 && strncasecmp (value, allowed_gnu[i], strlen(allowed_gnu[i])) == 0)
1322 notification n = gfc_notification_std (GFC_STD_GNU);
1324 if (n == WARNING || (warn && n == ERROR))
1326 gfc_warning ("Extension: %s specifier in %s statement at %C "
1327 "has value '%s'", specifier, statement,
1328 allowed_gnu[i]);
1329 return 1;
1331 else
1332 if (n == ERROR)
1334 gfc_notify_std (GFC_STD_GNU, "Extension: %s specifier in "
1335 "%s statement at %C has value '%s'", specifier,
1336 statement, allowed_gnu[i]);
1337 return 0;
1340 /* n == SILENT */
1341 return 1;
1344 if (warn)
1346 gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1347 specifier, statement, value);
1348 return 1;
1350 else
1352 gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1353 specifier, statement, value);
1354 return 0;
1358 /* Match an OPEN statement. */
1360 match
1361 gfc_match_open (void)
1363 gfc_open *open;
1364 match m;
1365 bool warn;
1367 m = gfc_match_char ('(');
1368 if (m == MATCH_NO)
1369 return m;
1371 open = gfc_getmem (sizeof (gfc_open));
1373 m = match_open_element (open);
1375 if (m == MATCH_ERROR)
1376 goto cleanup;
1377 if (m == MATCH_NO)
1379 m = gfc_match_expr (&open->unit);
1380 if (m == MATCH_NO)
1381 goto syntax;
1382 if (m == MATCH_ERROR)
1383 goto cleanup;
1386 for (;;)
1388 if (gfc_match_char (')') == MATCH_YES)
1389 break;
1390 if (gfc_match_char (',') != MATCH_YES)
1391 goto syntax;
1393 m = match_open_element (open);
1394 if (m == MATCH_ERROR)
1395 goto cleanup;
1396 if (m == MATCH_NO)
1397 goto syntax;
1400 if (gfc_match_eos () == MATCH_NO)
1401 goto syntax;
1403 if (gfc_pure (NULL))
1405 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1406 goto cleanup;
1409 warn = (open->err || open->iostat) ? true : false;
1410 /* Checks on the ACCESS specifier. */
1411 if (open->access && open->access->expr_type == EXPR_CONSTANT)
1413 static const char * access_f95[] = { "SEQUENTIAL", "DIRECT", NULL };
1414 static const char * access_f2003[] = { "STREAM", NULL };
1415 static const char * access_gnu[] = { "APPEND", NULL };
1417 if (!compare_to_allowed_values ("ACCESS", access_f95, access_f2003,
1418 access_gnu,
1419 open->access->value.character.string,
1420 "OPEN", warn))
1421 goto cleanup;
1424 /* Checks on the ACTION specifier. */
1425 if (open->action && open->action->expr_type == EXPR_CONSTANT)
1427 static const char * action[] = { "READ", "WRITE", "READWRITE", NULL };
1429 if (!compare_to_allowed_values ("ACTION", action, NULL, NULL,
1430 open->action->value.character.string,
1431 "OPEN", warn))
1432 goto cleanup;
1435 /* Checks on the ASYNCHRONOUS specifier. */
1436 /* TODO: code is ready, just needs uncommenting when async I/O support
1437 is added ;-)
1438 if (open->asynchronous && open->asynchronous->expr_type == EXPR_CONSTANT)
1440 static const char * asynchronous[] = { "YES", "NO", NULL };
1442 if (!compare_to_allowed_values
1443 ("action", asynchronous, NULL, NULL,
1444 open->asynchronous->value.character.string, "OPEN", warn))
1445 goto cleanup;
1448 /* Checks on the BLANK specifier. */
1449 if (open->blank && open->blank->expr_type == EXPR_CONSTANT)
1451 static const char * blank[] = { "ZERO", "NULL", NULL };
1453 if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
1454 open->blank->value.character.string,
1455 "OPEN", warn))
1456 goto cleanup;
1459 /* Checks on the DECIMAL specifier. */
1460 /* TODO: uncomment this code when DECIMAL support is added
1461 if (open->decimal && open->decimal->expr_type == EXPR_CONSTANT)
1463 static const char * decimal[] = { "COMMA", "POINT", NULL };
1465 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1466 open->decimal->value.character.string,
1467 "OPEN", warn))
1468 goto cleanup;
1469 } */
1471 /* Checks on the DELIM specifier. */
1472 if (open->delim && open->delim->expr_type == EXPR_CONSTANT)
1474 static const char * delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
1476 if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
1477 open->delim->value.character.string,
1478 "OPEN", warn))
1479 goto cleanup;
1482 /* Checks on the ENCODING specifier. */
1483 /* TODO: uncomment this code when ENCODING support is added
1484 if (open->encoding && open->encoding->expr_type == EXPR_CONSTANT)
1486 static const char * encoding[] = { "UTF-8", "DEFAULT", NULL };
1488 if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1489 open->encoding->value.character.string,
1490 "OPEN", warn))
1491 goto cleanup;
1492 } */
1494 /* Checks on the FORM specifier. */
1495 if (open->form && open->form->expr_type == EXPR_CONSTANT)
1497 static const char * form[] = { "FORMATTED", "UNFORMATTED", NULL };
1499 if (!compare_to_allowed_values ("FORM", form, NULL, NULL,
1500 open->form->value.character.string,
1501 "OPEN", warn))
1502 goto cleanup;
1505 /* Checks on the PAD specifier. */
1506 if (open->pad && open->pad->expr_type == EXPR_CONSTANT)
1508 static const char * pad[] = { "YES", "NO", NULL };
1510 if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
1511 open->pad->value.character.string,
1512 "OPEN", warn))
1513 goto cleanup;
1516 /* Checks on the POSITION specifier. */
1517 if (open->position && open->position->expr_type == EXPR_CONSTANT)
1519 static const char * position[] = { "ASIS", "REWIND", "APPEND", NULL };
1521 if (!compare_to_allowed_values ("POSITION", position, NULL, NULL,
1522 open->position->value.character.string,
1523 "OPEN", warn))
1524 goto cleanup;
1527 /* Checks on the ROUND specifier. */
1528 /* TODO: uncomment this code when ROUND support is added
1529 if (open->round && open->round->expr_type == EXPR_CONSTANT)
1531 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1532 "COMPATIBLE", "PROCESSOR_DEFINED", NULL };
1534 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1535 open->round->value.character.string,
1536 "OPEN", warn))
1537 goto cleanup;
1538 } */
1540 /* Checks on the SIGN specifier. */
1541 /* TODO: uncomment this code when SIGN support is added
1542 if (open->sign && open->sign->expr_type == EXPR_CONSTANT)
1544 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1545 NULL };
1547 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1548 open->sign->value.character.string,
1549 "OPEN", warn))
1550 goto cleanup;
1551 } */
1553 #define warn_or_error(...) \
1555 if (warn) \
1556 gfc_warning (__VA_ARGS__); \
1557 else \
1559 gfc_error (__VA_ARGS__); \
1560 goto cleanup; \
1564 /* Checks on the RECL specifier. */
1565 if (open->recl && open->recl->expr_type == EXPR_CONSTANT
1566 && open->recl->ts.type == BT_INTEGER
1567 && mpz_sgn (open->recl->value.integer) != 1)
1569 warn_or_error ("RECL in OPEN statement at %C must be positive");
1572 /* Checks on the STATUS specifier. */
1573 if (open->status && open->status->expr_type == EXPR_CONSTANT)
1575 static const char * status[] = { "OLD", "NEW", "SCRATCH",
1576 "REPLACE", "UNKNOWN", NULL };
1578 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1579 open->status->value.character.string,
1580 "OPEN", warn))
1581 goto cleanup;
1583 /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1584 the FILE= specifier shall appear. */
1585 if (open->file == NULL &&
1586 (strncasecmp (open->status->value.character.string, "replace", 7) == 0
1587 || strncasecmp (open->status->value.character.string, "new", 3) == 0))
1589 warn_or_error ("The STATUS specified in OPEN statement at %C is '%s' "
1590 "and no FILE specifier is present",
1591 open->status->value.character.string);
1594 /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1595 the FILE= specifier shall not appear. */
1596 if (strncasecmp (open->status->value.character.string, "scratch", 7) == 0
1597 && open->file)
1599 warn_or_error ("The STATUS specified in OPEN statement at %C cannot "
1600 "have the value SCRATCH if a FILE specifier "
1601 "is present");
1605 /* Things that are not allowed for unformatted I/O. */
1606 if (open->form && open->form->expr_type == EXPR_CONSTANT
1607 && (open->delim
1608 /* TODO uncomment this code when F2003 support is finished */
1609 /* || open->decimal || open->encoding || open->round
1610 || open->sign */
1611 || open->pad || open->blank)
1612 && strncasecmp (open->form->value.character.string,
1613 "unformatted", 11) == 0)
1615 const char * spec = (open->delim ? "DELIM " : (open->pad ? "PAD " :
1616 open->blank ? "BLANK " : ""));
1618 warn_or_error ("%sspecifier at %C not allowed in OPEN statement for "
1619 "unformatted I/O", spec);
1622 if (open->recl && open->access && open->access->expr_type == EXPR_CONSTANT
1623 && strncasecmp (open->access->value.character.string, "stream", 6) == 0)
1625 warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
1626 "stream I/O");
1629 if (open->position && open->access && open->access->expr_type == EXPR_CONSTANT
1630 && !(strncasecmp (open->access->value.character.string,
1631 "sequential", 10) == 0
1632 || strncasecmp (open->access->value.character.string,
1633 "stream", 6) == 0
1634 || strncasecmp (open->access->value.character.string,
1635 "append", 6) == 0))
1637 warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
1638 "for stream or sequential ACCESS");
1641 #undef warn_or_error
1643 new_st.op = EXEC_OPEN;
1644 new_st.ext.open = open;
1645 return MATCH_YES;
1647 syntax:
1648 gfc_syntax_error (ST_OPEN);
1650 cleanup:
1651 gfc_free_open (open);
1652 return MATCH_ERROR;
1656 /* Free a gfc_close structure an all its expressions. */
1658 void
1659 gfc_free_close (gfc_close * close)
1662 if (close == NULL)
1663 return;
1665 gfc_free_expr (close->unit);
1666 gfc_free_expr (close->iomsg);
1667 gfc_free_expr (close->iostat);
1668 gfc_free_expr (close->status);
1670 gfc_free (close);
1674 /* Match elements of a CLOSE statement. */
1676 static match
1677 match_close_element (gfc_close * close)
1679 match m;
1681 m = match_etag (&tag_unit, &close->unit);
1682 if (m != MATCH_NO)
1683 return m;
1684 m = match_etag (&tag_status, &close->status);
1685 if (m != MATCH_NO)
1686 return m;
1687 m = match_out_tag (&tag_iomsg, &close->iomsg);
1688 if (m != MATCH_NO)
1689 return m;
1690 m = match_out_tag (&tag_iostat, &close->iostat);
1691 if (m != MATCH_NO)
1692 return m;
1693 m = match_ltag (&tag_err, &close->err);
1694 if (m != MATCH_NO)
1695 return m;
1697 return MATCH_NO;
1701 /* Match a CLOSE statement. */
1703 match
1704 gfc_match_close (void)
1706 gfc_close *close;
1707 match m;
1708 bool warn;
1710 m = gfc_match_char ('(');
1711 if (m == MATCH_NO)
1712 return m;
1714 close = gfc_getmem (sizeof (gfc_close));
1716 m = match_close_element (close);
1718 if (m == MATCH_ERROR)
1719 goto cleanup;
1720 if (m == MATCH_NO)
1722 m = gfc_match_expr (&close->unit);
1723 if (m == MATCH_NO)
1724 goto syntax;
1725 if (m == MATCH_ERROR)
1726 goto cleanup;
1729 for (;;)
1731 if (gfc_match_char (')') == MATCH_YES)
1732 break;
1733 if (gfc_match_char (',') != MATCH_YES)
1734 goto syntax;
1736 m = match_close_element (close);
1737 if (m == MATCH_ERROR)
1738 goto cleanup;
1739 if (m == MATCH_NO)
1740 goto syntax;
1743 if (gfc_match_eos () == MATCH_NO)
1744 goto syntax;
1746 if (gfc_pure (NULL))
1748 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1749 goto cleanup;
1752 warn = (close->iostat || close->err) ? true : false;
1754 /* Checks on the STATUS specifier. */
1755 if (close->status && close->status->expr_type == EXPR_CONSTANT)
1757 static const char * status[] = { "KEEP", "DELETE", NULL };
1759 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1760 close->status->value.character.string,
1761 "CLOSE", warn))
1762 goto cleanup;
1765 new_st.op = EXEC_CLOSE;
1766 new_st.ext.close = close;
1767 return MATCH_YES;
1769 syntax:
1770 gfc_syntax_error (ST_CLOSE);
1772 cleanup:
1773 gfc_free_close (close);
1774 return MATCH_ERROR;
1778 /* Resolve everything in a gfc_close structure. */
1781 gfc_resolve_close (gfc_close * close)
1784 RESOLVE_TAG (&tag_unit, close->unit);
1785 RESOLVE_TAG (&tag_iomsg, close->iomsg);
1786 RESOLVE_TAG (&tag_iostat, close->iostat);
1787 RESOLVE_TAG (&tag_status, close->status);
1789 if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
1790 return FAILURE;
1792 return SUCCESS;
1796 /* Free a gfc_filepos structure. */
1798 void
1799 gfc_free_filepos (gfc_filepos * fp)
1802 gfc_free_expr (fp->unit);
1803 gfc_free_expr (fp->iomsg);
1804 gfc_free_expr (fp->iostat);
1805 gfc_free (fp);
1809 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
1811 static match
1812 match_file_element (gfc_filepos * fp)
1814 match m;
1816 m = match_etag (&tag_unit, &fp->unit);
1817 if (m != MATCH_NO)
1818 return m;
1819 m = match_out_tag (&tag_iomsg, &fp->iomsg);
1820 if (m != MATCH_NO)
1821 return m;
1822 m = match_out_tag (&tag_iostat, &fp->iostat);
1823 if (m != MATCH_NO)
1824 return m;
1825 m = match_ltag (&tag_err, &fp->err);
1826 if (m != MATCH_NO)
1827 return m;
1829 return MATCH_NO;
1833 /* Match the second half of the file-positioning statements, REWIND,
1834 BACKSPACE, ENDFILE, or the FLUSH statement. */
1836 static match
1837 match_filepos (gfc_statement st, gfc_exec_op op)
1839 gfc_filepos *fp;
1840 match m;
1842 fp = gfc_getmem (sizeof (gfc_filepos));
1844 if (gfc_match_char ('(') == MATCH_NO)
1846 m = gfc_match_expr (&fp->unit);
1847 if (m == MATCH_ERROR)
1848 goto cleanup;
1849 if (m == MATCH_NO)
1850 goto syntax;
1852 goto done;
1855 m = match_file_element (fp);
1856 if (m == MATCH_ERROR)
1857 goto done;
1858 if (m == MATCH_NO)
1860 m = gfc_match_expr (&fp->unit);
1861 if (m == MATCH_ERROR)
1862 goto done;
1863 if (m == MATCH_NO)
1864 goto syntax;
1867 for (;;)
1869 if (gfc_match_char (')') == MATCH_YES)
1870 break;
1871 if (gfc_match_char (',') != MATCH_YES)
1872 goto syntax;
1874 m = match_file_element (fp);
1875 if (m == MATCH_ERROR)
1876 goto cleanup;
1877 if (m == MATCH_NO)
1878 goto syntax;
1881 done:
1882 if (gfc_match_eos () != MATCH_YES)
1883 goto syntax;
1885 if (gfc_pure (NULL))
1887 gfc_error ("%s statement not allowed in PURE procedure at %C",
1888 gfc_ascii_statement (st));
1890 goto cleanup;
1893 new_st.op = op;
1894 new_st.ext.filepos = fp;
1895 return MATCH_YES;
1897 syntax:
1898 gfc_syntax_error (st);
1900 cleanup:
1901 gfc_free_filepos (fp);
1902 return MATCH_ERROR;
1907 gfc_resolve_filepos (gfc_filepos * fp)
1910 RESOLVE_TAG (&tag_unit, fp->unit);
1911 RESOLVE_TAG (&tag_iostat, fp->iostat);
1912 RESOLVE_TAG (&tag_iomsg, fp->iomsg);
1913 if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
1914 return FAILURE;
1916 return SUCCESS;
1920 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
1921 and the FLUSH statement. */
1923 match
1924 gfc_match_endfile (void)
1927 return match_filepos (ST_END_FILE, EXEC_ENDFILE);
1930 match
1931 gfc_match_backspace (void)
1934 return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
1937 match
1938 gfc_match_rewind (void)
1941 return match_filepos (ST_REWIND, EXEC_REWIND);
1944 match
1945 gfc_match_flush (void)
1947 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: FLUSH statement at %C") == FAILURE)
1948 return MATCH_ERROR;
1950 return match_filepos (ST_FLUSH, EXEC_FLUSH);
1953 /******************** Data Transfer Statements *********************/
1955 typedef enum
1956 { M_READ, M_WRITE, M_PRINT, M_INQUIRE }
1957 io_kind;
1960 /* Return a default unit number. */
1962 static gfc_expr *
1963 default_unit (io_kind k)
1965 int unit;
1967 if (k == M_READ)
1968 unit = 5;
1969 else
1970 unit = 6;
1972 return gfc_int_expr (unit);
1976 /* Match a unit specification for a data transfer statement. */
1978 static match
1979 match_dt_unit (io_kind k, gfc_dt * dt)
1981 gfc_expr *e;
1983 if (gfc_match_char ('*') == MATCH_YES)
1985 if (dt->io_unit != NULL)
1986 goto conflict;
1988 dt->io_unit = default_unit (k);
1989 return MATCH_YES;
1992 if (gfc_match_expr (&e) == MATCH_YES)
1994 if (dt->io_unit != NULL)
1996 gfc_free_expr (e);
1997 goto conflict;
2000 dt->io_unit = e;
2001 return MATCH_YES;
2004 return MATCH_NO;
2006 conflict:
2007 gfc_error ("Duplicate UNIT specification at %C");
2008 return MATCH_ERROR;
2012 /* Match a format specification. */
2014 static match
2015 match_dt_format (gfc_dt * dt)
2017 locus where;
2018 gfc_expr *e;
2019 gfc_st_label *label;
2021 where = gfc_current_locus;
2023 if (gfc_match_char ('*') == MATCH_YES)
2025 if (dt->format_expr != NULL || dt->format_label != NULL)
2026 goto conflict;
2028 dt->format_label = &format_asterisk;
2029 return MATCH_YES;
2032 if (gfc_match_st_label (&label) == MATCH_YES)
2034 if (dt->format_expr != NULL || dt->format_label != NULL)
2036 gfc_free_st_label (label);
2037 goto conflict;
2040 if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
2041 return MATCH_ERROR;
2043 dt->format_label = label;
2044 return MATCH_YES;
2047 if (gfc_match_expr (&e) == MATCH_YES)
2049 if (dt->format_expr != NULL || dt->format_label != NULL)
2051 gfc_free_expr (e);
2052 goto conflict;
2054 dt->format_expr = e;
2055 return MATCH_YES;
2058 gfc_current_locus = where; /* The only case where we have to restore */
2060 return MATCH_NO;
2062 conflict:
2063 gfc_error ("Duplicate format specification at %C");
2064 return MATCH_ERROR;
2068 /* Traverse a namelist that is part of a READ statement to make sure
2069 that none of the variables in the namelist are INTENT(IN). Returns
2070 nonzero if we find such a variable. */
2072 static int
2073 check_namelist (gfc_symbol * sym)
2075 gfc_namelist *p;
2077 for (p = sym->namelist; p; p = p->next)
2078 if (p->sym->attr.intent == INTENT_IN)
2080 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2081 p->sym->name, sym->name);
2082 return 1;
2085 return 0;
2089 /* Match a single data transfer element. */
2091 static match
2092 match_dt_element (io_kind k, gfc_dt * dt)
2094 char name[GFC_MAX_SYMBOL_LEN + 1];
2095 gfc_symbol *sym;
2096 match m;
2098 if (gfc_match (" unit =") == MATCH_YES)
2100 m = match_dt_unit (k, dt);
2101 if (m != MATCH_NO)
2102 return m;
2105 if (gfc_match (" fmt =") == MATCH_YES)
2107 m = match_dt_format (dt);
2108 if (m != MATCH_NO)
2109 return m;
2112 if (gfc_match (" nml = %n", name) == MATCH_YES)
2114 if (dt->namelist != NULL)
2116 gfc_error ("Duplicate NML specification at %C");
2117 return MATCH_ERROR;
2120 if (gfc_find_symbol (name, NULL, 1, &sym))
2121 return MATCH_ERROR;
2123 if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
2125 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2126 sym != NULL ? sym->name : name);
2127 return MATCH_ERROR;
2130 dt->namelist = sym;
2131 if (k == M_READ && check_namelist (sym))
2132 return MATCH_ERROR;
2134 return MATCH_YES;
2137 m = match_etag (&tag_rec, &dt->rec);
2138 if (m != MATCH_NO)
2139 return m;
2140 m = match_etag (&tag_spos, &dt->rec);
2141 if (m != MATCH_NO)
2142 return m;
2143 m = match_out_tag (&tag_iomsg, &dt->iomsg);
2144 if (m != MATCH_NO)
2145 return m;
2146 m = match_out_tag (&tag_iostat, &dt->iostat);
2147 if (m != MATCH_NO)
2148 return m;
2149 m = match_ltag (&tag_err, &dt->err);
2150 if (m == MATCH_YES)
2151 dt->err_where = gfc_current_locus;
2152 if (m != MATCH_NO)
2153 return m;
2154 m = match_etag (&tag_advance, &dt->advance);
2155 if (m != MATCH_NO)
2156 return m;
2157 m = match_out_tag (&tag_size, &dt->size);
2158 if (m != MATCH_NO)
2159 return m;
2161 m = match_ltag (&tag_end, &dt->end);
2162 if (m == MATCH_YES)
2164 if (k == M_WRITE)
2166 gfc_error ("END tag at %C not allowed in output statement");
2167 return MATCH_ERROR;
2169 dt->end_where = gfc_current_locus;
2171 if (m != MATCH_NO)
2172 return m;
2174 m = match_ltag (&tag_eor, &dt->eor);
2175 if (m == MATCH_YES)
2176 dt->eor_where = gfc_current_locus;
2177 if (m != MATCH_NO)
2178 return m;
2180 return MATCH_NO;
2184 /* Free a data transfer structure and everything below it. */
2186 void
2187 gfc_free_dt (gfc_dt * dt)
2190 if (dt == NULL)
2191 return;
2193 gfc_free_expr (dt->io_unit);
2194 gfc_free_expr (dt->format_expr);
2195 gfc_free_expr (dt->rec);
2196 gfc_free_expr (dt->advance);
2197 gfc_free_expr (dt->iomsg);
2198 gfc_free_expr (dt->iostat);
2199 gfc_free_expr (dt->size);
2201 gfc_free (dt);
2205 /* Resolve everything in a gfc_dt structure. */
2208 gfc_resolve_dt (gfc_dt * dt)
2210 gfc_expr *e;
2212 RESOLVE_TAG (&tag_format, dt->format_expr);
2213 RESOLVE_TAG (&tag_rec, dt->rec);
2214 RESOLVE_TAG (&tag_spos, dt->rec);
2215 RESOLVE_TAG (&tag_advance, dt->advance);
2216 RESOLVE_TAG (&tag_iomsg, dt->iomsg);
2217 RESOLVE_TAG (&tag_iostat, dt->iostat);
2218 RESOLVE_TAG (&tag_size, dt->size);
2220 e = dt->io_unit;
2221 if (gfc_resolve_expr (e) == SUCCESS
2222 && (e->ts.type != BT_INTEGER
2223 && (e->ts.type != BT_CHARACTER
2224 || e->expr_type != EXPR_VARIABLE)))
2226 gfc_error
2227 ("UNIT specification at %L must be an INTEGER expression or a "
2228 "CHARACTER variable", &e->where);
2229 return FAILURE;
2232 if (e->ts.type == BT_CHARACTER)
2234 if (gfc_has_vector_index (e))
2236 gfc_error ("Internal unit with vector subscript at %L",
2237 &e->where);
2238 return FAILURE;
2242 if (e->rank && e->ts.type != BT_CHARACTER)
2244 gfc_error ("External IO UNIT cannot be an array at %L", &e->where);
2245 return FAILURE;
2248 if (dt->err)
2250 if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
2251 return FAILURE;
2252 if (dt->err->defined == ST_LABEL_UNKNOWN)
2254 gfc_error ("ERR tag label %d at %L not defined",
2255 dt->err->value, &dt->err_where);
2256 return FAILURE;
2260 if (dt->end)
2262 if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
2263 return FAILURE;
2264 if (dt->end->defined == ST_LABEL_UNKNOWN)
2266 gfc_error ("END tag label %d at %L not defined",
2267 dt->end->value, &dt->end_where);
2268 return FAILURE;
2272 if (dt->eor)
2274 if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
2275 return FAILURE;
2276 if (dt->eor->defined == ST_LABEL_UNKNOWN)
2278 gfc_error ("EOR tag label %d at %L not defined",
2279 dt->eor->value, &dt->eor_where);
2280 return FAILURE;
2284 /* Check the format label actually exists. */
2285 if (dt->format_label && dt->format_label != &format_asterisk
2286 && dt->format_label->defined == ST_LABEL_UNKNOWN)
2288 gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
2289 &dt->format_label->where);
2290 return FAILURE;
2292 return SUCCESS;
2296 /* Given an io_kind, return its name. */
2298 static const char *
2299 io_kind_name (io_kind k)
2301 const char *name;
2303 switch (k)
2305 case M_READ:
2306 name = "READ";
2307 break;
2308 case M_WRITE:
2309 name = "WRITE";
2310 break;
2311 case M_PRINT:
2312 name = "PRINT";
2313 break;
2314 case M_INQUIRE:
2315 name = "INQUIRE";
2316 break;
2317 default:
2318 gfc_internal_error ("io_kind_name(): bad I/O-kind");
2321 return name;
2325 /* Match an IO iteration statement of the form:
2327 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2329 which is equivalent to a single IO element. This function is
2330 mutually recursive with match_io_element(). */
2332 static match match_io_element (io_kind k, gfc_code **);
2334 static match
2335 match_io_iterator (io_kind k, gfc_code ** result)
2337 gfc_code *head, *tail, *new;
2338 gfc_iterator *iter;
2339 locus old_loc;
2340 match m;
2341 int n;
2343 iter = NULL;
2344 head = NULL;
2345 old_loc = gfc_current_locus;
2347 if (gfc_match_char ('(') != MATCH_YES)
2348 return MATCH_NO;
2350 m = match_io_element (k, &head);
2351 tail = head;
2353 if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
2355 m = MATCH_NO;
2356 goto cleanup;
2359 /* Can't be anything but an IO iterator. Build a list. */
2360 iter = gfc_get_iterator ();
2362 for (n = 1;; n++)
2364 m = gfc_match_iterator (iter, 0);
2365 if (m == MATCH_ERROR)
2366 goto cleanup;
2367 if (m == MATCH_YES)
2369 gfc_check_do_variable (iter->var->symtree);
2370 break;
2373 m = match_io_element (k, &new);
2374 if (m == MATCH_ERROR)
2375 goto cleanup;
2376 if (m == MATCH_NO)
2378 if (n > 2)
2379 goto syntax;
2380 goto cleanup;
2383 tail = gfc_append_code (tail, new);
2385 if (gfc_match_char (',') != MATCH_YES)
2387 if (n > 2)
2388 goto syntax;
2389 m = MATCH_NO;
2390 goto cleanup;
2394 if (gfc_match_char (')') != MATCH_YES)
2395 goto syntax;
2397 new = gfc_get_code ();
2398 new->op = EXEC_DO;
2399 new->ext.iterator = iter;
2401 new->block = gfc_get_code ();
2402 new->block->op = EXEC_DO;
2403 new->block->next = head;
2405 *result = new;
2406 return MATCH_YES;
2408 syntax:
2409 gfc_error ("Syntax error in I/O iterator at %C");
2410 m = MATCH_ERROR;
2412 cleanup:
2413 gfc_free_iterator (iter, 1);
2414 gfc_free_statements (head);
2415 gfc_current_locus = old_loc;
2416 return m;
2420 /* Match a single element of an IO list, which is either a single
2421 expression or an IO Iterator. */
2423 static match
2424 match_io_element (io_kind k, gfc_code ** cpp)
2426 gfc_expr *expr;
2427 gfc_code *cp;
2428 match m;
2430 expr = NULL;
2432 m = match_io_iterator (k, cpp);
2433 if (m == MATCH_YES)
2434 return MATCH_YES;
2436 if (k == M_READ)
2438 m = gfc_match_variable (&expr, 0);
2439 if (m == MATCH_NO)
2440 gfc_error ("Expected variable in READ statement at %C");
2442 else
2444 m = gfc_match_expr (&expr);
2445 if (m == MATCH_NO)
2446 gfc_error ("Expected expression in %s statement at %C",
2447 io_kind_name (k));
2450 if (m == MATCH_YES)
2451 switch (k)
2453 case M_READ:
2454 if (expr->symtree->n.sym->attr.intent == INTENT_IN)
2456 gfc_error
2457 ("Variable '%s' in input list at %C cannot be INTENT(IN)",
2458 expr->symtree->n.sym->name);
2459 m = MATCH_ERROR;
2462 if (gfc_pure (NULL)
2463 && gfc_impure_variable (expr->symtree->n.sym)
2464 && current_dt->io_unit->ts.type == BT_CHARACTER)
2466 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2467 expr->symtree->n.sym->name);
2468 m = MATCH_ERROR;
2471 if (gfc_check_do_variable (expr->symtree))
2472 m = MATCH_ERROR;
2474 break;
2476 case M_WRITE:
2477 if (current_dt->io_unit->ts.type == BT_CHARACTER
2478 && gfc_pure (NULL)
2479 && current_dt->io_unit->expr_type == EXPR_VARIABLE
2480 && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
2482 gfc_error
2483 ("Cannot write to internal file unit '%s' at %C inside a "
2484 "PURE procedure", current_dt->io_unit->symtree->n.sym->name);
2485 m = MATCH_ERROR;
2488 break;
2490 default:
2491 break;
2494 if (m != MATCH_YES)
2496 gfc_free_expr (expr);
2497 return MATCH_ERROR;
2500 cp = gfc_get_code ();
2501 cp->op = EXEC_TRANSFER;
2502 cp->expr = expr;
2504 *cpp = cp;
2505 return MATCH_YES;
2509 /* Match an I/O list, building gfc_code structures as we go. */
2511 static match
2512 match_io_list (io_kind k, gfc_code ** head_p)
2514 gfc_code *head, *tail, *new;
2515 match m;
2517 *head_p = head = tail = NULL;
2518 if (gfc_match_eos () == MATCH_YES)
2519 return MATCH_YES;
2521 for (;;)
2523 m = match_io_element (k, &new);
2524 if (m == MATCH_ERROR)
2525 goto cleanup;
2526 if (m == MATCH_NO)
2527 goto syntax;
2529 tail = gfc_append_code (tail, new);
2530 if (head == NULL)
2531 head = new;
2533 if (gfc_match_eos () == MATCH_YES)
2534 break;
2535 if (gfc_match_char (',') != MATCH_YES)
2536 goto syntax;
2539 *head_p = head;
2540 return MATCH_YES;
2542 syntax:
2543 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2545 cleanup:
2546 gfc_free_statements (head);
2547 return MATCH_ERROR;
2551 /* Attach the data transfer end node. */
2553 static void
2554 terminate_io (gfc_code * io_code)
2556 gfc_code *c;
2558 if (io_code == NULL)
2559 io_code = new_st.block;
2561 c = gfc_get_code ();
2562 c->op = EXEC_DT_END;
2564 /* Point to structure that is already there */
2565 c->ext.dt = new_st.ext.dt;
2566 gfc_append_code (io_code, c);
2570 /* Check the constraints for a data transfer statement. The majority of the
2571 constraints appearing in 9.4 of the standard appear here. Some are handled
2572 in resolve_tag and others in gfc_resolve_dt. */
2574 static match
2575 check_io_constraints (io_kind k, gfc_dt *dt, gfc_code * io_code, locus * spec_end)
2577 #define io_constraint(condition,msg,arg)\
2578 if (condition) \
2580 gfc_error(msg,arg);\
2581 m = MATCH_ERROR;\
2584 match m;
2585 gfc_expr * expr;
2586 gfc_symbol * sym = NULL;
2588 m = MATCH_YES;
2590 expr = dt->io_unit;
2591 if (expr && expr->expr_type == EXPR_VARIABLE
2592 && expr->ts.type == BT_CHARACTER)
2594 sym = expr->symtree->n.sym;
2596 io_constraint (k == M_WRITE && sym->attr.intent == INTENT_IN,
2597 "Internal file at %L must not be INTENT(IN)",
2598 &expr->where);
2600 io_constraint (gfc_has_vector_index (dt->io_unit),
2601 "Internal file incompatible with vector subscript at %L",
2602 &expr->where);
2604 io_constraint (dt->rec != NULL,
2605 "REC tag at %L is incompatible with internal file",
2606 &dt->rec->where);
2608 if (dt->namelist != NULL)
2610 if (gfc_notify_std(GFC_STD_F2003,
2611 "Fortran 2003: Internal file at %L with namelist",
2612 &expr->where) == FAILURE)
2613 m = MATCH_ERROR;
2616 io_constraint (dt->advance != NULL,
2617 "ADVANCE tag at %L is incompatible with internal file",
2618 &dt->advance->where);
2621 if (expr && expr->ts.type != BT_CHARACTER)
2624 io_constraint (gfc_pure (NULL)
2625 && (k == M_READ || k == M_WRITE),
2626 "IO UNIT in %s statement at %C must be "
2627 "an internal file in a PURE procedure",
2628 io_kind_name (k));
2632 if (k != M_READ)
2634 io_constraint (dt->end,
2635 "END tag not allowed with output at %L",
2636 &dt->end_where);
2638 io_constraint (dt->eor,
2639 "EOR tag not allowed with output at %L",
2640 &dt->eor_where);
2642 io_constraint (k != M_READ && dt->size,
2643 "SIZE=specifier not allowed with output at %L",
2644 &dt->size->where);
2646 else
2648 io_constraint (dt->size && dt->advance == NULL,
2649 "SIZE tag at %L requires an ADVANCE tag",
2650 &dt->size->where);
2652 io_constraint (dt->eor && dt->advance == NULL,
2653 "EOR tag at %L requires an ADVANCE tag",
2654 &dt->eor_where);
2659 if (dt->namelist)
2661 io_constraint (io_code && dt->namelist,
2662 "NAMELIST cannot be followed by IO-list at %L",
2663 &io_code->loc);
2665 io_constraint (dt->format_expr,
2666 "IO spec-list cannot contain both NAMELIST group name "
2667 "and format specification at %L.",
2668 &dt->format_expr->where);
2670 io_constraint (dt->format_label,
2671 "IO spec-list cannot contain both NAMELIST group name "
2672 "and format label at %L", spec_end);
2674 io_constraint (dt->rec,
2675 "NAMELIST IO is not allowed with a REC=specifier "
2676 "at %L.", &dt->rec->where);
2678 io_constraint (dt->advance,
2679 "NAMELIST IO is not allowed with a ADVANCE=specifier "
2680 "at %L.", &dt->advance->where);
2683 if (dt->rec)
2685 io_constraint (dt->end,
2686 "An END tag is not allowed with a "
2687 "REC=specifier at %L.", &dt->end_where);
2690 io_constraint (dt->format_label == &format_asterisk,
2691 "FMT=* is not allowed with a REC=specifier "
2692 "at %L.", spec_end);
2695 if (dt->advance)
2697 int not_yes, not_no;
2698 expr = dt->advance;
2700 io_constraint (dt->format_label == &format_asterisk,
2701 "List directed format(*) is not allowed with a "
2702 "ADVANCE=specifier at %L.", &expr->where);
2704 io_constraint (dt->format_expr == NULL
2705 && dt->format_label == NULL
2706 && dt->namelist == NULL,
2707 "the ADVANCE=specifier at %L must appear with an "
2708 "explicit format expression", &expr->where);
2710 if (expr->expr_type == EXPR_CONSTANT && expr->ts.type == BT_CHARACTER)
2712 const char * advance = expr->value.character.string;
2713 not_no = strcasecmp (advance, "no") != 0;
2714 not_yes = strcasecmp (advance, "yes") != 0;
2716 else
2718 not_no = 0;
2719 not_yes = 0;
2722 io_constraint (not_no && not_yes,
2723 "ADVANCE=specifier at %L must have value = "
2724 "YES or NO.", &expr->where);
2726 io_constraint (dt->size && not_no && k == M_READ,
2727 "SIZE tag at %L requires an ADVANCE = 'NO'",
2728 &dt->size->where);
2730 io_constraint (dt->eor && not_no && k == M_READ,
2731 "EOR tag at %L requires an ADVANCE = 'NO'",
2732 &dt->eor_where);
2735 expr = dt->format_expr;
2736 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
2737 check_format_string (expr);
2739 return m;
2741 #undef io_constraint
2743 /* Match a READ, WRITE or PRINT statement. */
2745 static match
2746 match_io (io_kind k)
2748 char name[GFC_MAX_SYMBOL_LEN + 1];
2749 gfc_code *io_code;
2750 gfc_symbol *sym;
2751 int comma_flag, c;
2752 locus where;
2753 locus spec_end;
2754 gfc_dt *dt;
2755 match m;
2757 where = gfc_current_locus;
2758 comma_flag = 0;
2759 current_dt = dt = gfc_getmem (sizeof (gfc_dt));
2760 m = gfc_match_char ('(');
2761 if (m == MATCH_NO)
2763 where = gfc_current_locus;
2764 if (k == M_WRITE)
2765 goto syntax;
2766 else if (k == M_PRINT)
2768 /* Treat the non-standard case of PRINT namelist. */
2769 if ((gfc_current_form == FORM_FIXED || gfc_peek_char () == ' ')
2770 && gfc_match_name (name) == MATCH_YES)
2772 gfc_find_symbol (name, NULL, 1, &sym);
2773 if (sym && sym->attr.flavor == FL_NAMELIST)
2775 if (gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
2776 "%C is an extension") == FAILURE)
2778 m = MATCH_ERROR;
2779 goto cleanup;
2782 dt->io_unit = default_unit (k);
2783 dt->namelist = sym;
2784 goto get_io_list;
2786 else
2787 gfc_current_locus = where;
2791 if (gfc_current_form == FORM_FREE)
2793 c = gfc_peek_char();
2794 if (c != ' ' && c != '*' && c != '\'' && c != '"')
2796 m = MATCH_NO;
2797 goto cleanup;
2801 m = match_dt_format (dt);
2802 if (m == MATCH_ERROR)
2803 goto cleanup;
2804 if (m == MATCH_NO)
2805 goto syntax;
2807 comma_flag = 1;
2808 dt->io_unit = default_unit (k);
2809 goto get_io_list;
2811 else
2813 /* Before issuing an error for a malformed 'print (1,*)' type of
2814 error, check for a default-char-expr of the form ('(I0)'). */
2816 if (k == M_PRINT && m == MATCH_YES)
2818 /* Reset current locus to get the initial '(' in an expression. */
2819 gfc_current_locus = where;
2820 dt->format_expr = NULL;
2821 m = match_dt_format (dt);
2823 if (m == MATCH_ERROR)
2824 goto cleanup;
2825 if (m == MATCH_NO || dt->format_expr == NULL)
2826 goto syntax;
2828 comma_flag = 1;
2829 dt->io_unit = default_unit (k);
2830 goto get_io_list;
2834 /* Match a control list */
2835 if (match_dt_element (k, dt) == MATCH_YES)
2836 goto next;
2837 if (match_dt_unit (k, dt) != MATCH_YES)
2838 goto loop;
2840 if (gfc_match_char (')') == MATCH_YES)
2841 goto get_io_list;
2842 if (gfc_match_char (',') != MATCH_YES)
2843 goto syntax;
2845 m = match_dt_element (k, dt);
2846 if (m == MATCH_YES)
2847 goto next;
2848 if (m == MATCH_ERROR)
2849 goto cleanup;
2851 m = match_dt_format (dt);
2852 if (m == MATCH_YES)
2853 goto next;
2854 if (m == MATCH_ERROR)
2855 goto cleanup;
2857 where = gfc_current_locus;
2859 m = gfc_match_name (name);
2860 if (m == MATCH_YES)
2862 gfc_find_symbol (name, NULL, 1, &sym);
2863 if (sym && sym->attr.flavor == FL_NAMELIST)
2865 dt->namelist = sym;
2866 if (k == M_READ && check_namelist (sym))
2868 m = MATCH_ERROR;
2869 goto cleanup;
2871 goto next;
2875 gfc_current_locus = where;
2877 goto loop; /* No matches, try regular elements */
2879 next:
2880 if (gfc_match_char (')') == MATCH_YES)
2881 goto get_io_list;
2882 if (gfc_match_char (',') != MATCH_YES)
2883 goto syntax;
2885 loop:
2886 for (;;)
2888 m = match_dt_element (k, dt);
2889 if (m == MATCH_NO)
2890 goto syntax;
2891 if (m == MATCH_ERROR)
2892 goto cleanup;
2894 if (gfc_match_char (')') == MATCH_YES)
2895 break;
2896 if (gfc_match_char (',') != MATCH_YES)
2897 goto syntax;
2900 get_io_list:
2902 /* Used in check_io_constraints, where no locus is available. */
2903 spec_end = gfc_current_locus;
2905 /* Optional leading comma (non-standard). */
2906 if (!comma_flag
2907 && gfc_match_char (',') == MATCH_YES
2908 && k == M_WRITE
2909 && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before output "
2910 "item list at %C is an extension") == FAILURE)
2911 return MATCH_ERROR;
2913 io_code = NULL;
2914 if (gfc_match_eos () != MATCH_YES)
2916 if (comma_flag && gfc_match_char (',') != MATCH_YES)
2918 gfc_error ("Expected comma in I/O list at %C");
2919 m = MATCH_ERROR;
2920 goto cleanup;
2923 m = match_io_list (k, &io_code);
2924 if (m == MATCH_ERROR)
2925 goto cleanup;
2926 if (m == MATCH_NO)
2927 goto syntax;
2930 /* A full IO statement has been matched. Check the constraints. spec_end is
2931 supplied for cases where no locus is supplied. */
2932 m = check_io_constraints (k, dt, io_code, &spec_end);
2934 if (m == MATCH_ERROR)
2935 goto cleanup;
2937 new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
2938 new_st.ext.dt = dt;
2939 new_st.block = gfc_get_code ();
2940 new_st.block->op = new_st.op;
2941 new_st.block->next = io_code;
2943 terminate_io (io_code);
2945 return MATCH_YES;
2947 syntax:
2948 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2949 m = MATCH_ERROR;
2951 cleanup:
2952 gfc_free_dt (dt);
2953 return m;
2957 match
2958 gfc_match_read (void)
2960 return match_io (M_READ);
2963 match
2964 gfc_match_write (void)
2966 return match_io (M_WRITE);
2969 match
2970 gfc_match_print (void)
2972 match m;
2974 m = match_io (M_PRINT);
2975 if (m != MATCH_YES)
2976 return m;
2978 if (gfc_pure (NULL))
2980 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
2981 return MATCH_ERROR;
2984 return MATCH_YES;
2988 /* Free a gfc_inquire structure. */
2990 void
2991 gfc_free_inquire (gfc_inquire * inquire)
2994 if (inquire == NULL)
2995 return;
2997 gfc_free_expr (inquire->unit);
2998 gfc_free_expr (inquire->file);
2999 gfc_free_expr (inquire->iomsg);
3000 gfc_free_expr (inquire->iostat);
3001 gfc_free_expr (inquire->exist);
3002 gfc_free_expr (inquire->opened);
3003 gfc_free_expr (inquire->number);
3004 gfc_free_expr (inquire->named);
3005 gfc_free_expr (inquire->name);
3006 gfc_free_expr (inquire->access);
3007 gfc_free_expr (inquire->sequential);
3008 gfc_free_expr (inquire->direct);
3009 gfc_free_expr (inquire->form);
3010 gfc_free_expr (inquire->formatted);
3011 gfc_free_expr (inquire->unformatted);
3012 gfc_free_expr (inquire->recl);
3013 gfc_free_expr (inquire->nextrec);
3014 gfc_free_expr (inquire->blank);
3015 gfc_free_expr (inquire->position);
3016 gfc_free_expr (inquire->action);
3017 gfc_free_expr (inquire->read);
3018 gfc_free_expr (inquire->write);
3019 gfc_free_expr (inquire->readwrite);
3020 gfc_free_expr (inquire->delim);
3021 gfc_free_expr (inquire->pad);
3022 gfc_free_expr (inquire->iolength);
3023 gfc_free_expr (inquire->convert);
3024 gfc_free_expr (inquire->strm_pos);
3026 gfc_free (inquire);
3030 /* Match an element of an INQUIRE statement. */
3032 #define RETM if (m != MATCH_NO) return m;
3034 static match
3035 match_inquire_element (gfc_inquire * inquire)
3037 match m;
3039 m = match_etag (&tag_unit, &inquire->unit);
3040 RETM m = match_etag (&tag_file, &inquire->file);
3041 RETM m = match_ltag (&tag_err, &inquire->err);
3042 RETM m = match_out_tag (&tag_iomsg, &inquire->iomsg);
3043 RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
3044 RETM m = match_vtag (&tag_exist, &inquire->exist);
3045 RETM m = match_vtag (&tag_opened, &inquire->opened);
3046 RETM m = match_vtag (&tag_named, &inquire->named);
3047 RETM m = match_vtag (&tag_name, &inquire->name);
3048 RETM m = match_out_tag (&tag_number, &inquire->number);
3049 RETM m = match_vtag (&tag_s_access, &inquire->access);
3050 RETM m = match_vtag (&tag_sequential, &inquire->sequential);
3051 RETM m = match_vtag (&tag_direct, &inquire->direct);
3052 RETM m = match_vtag (&tag_s_form, &inquire->form);
3053 RETM m = match_vtag (&tag_formatted, &inquire->formatted);
3054 RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
3055 RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
3056 RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
3057 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
3058 RETM m = match_vtag (&tag_s_position, &inquire->position);
3059 RETM m = match_vtag (&tag_s_action, &inquire->action);
3060 RETM m = match_vtag (&tag_read, &inquire->read);
3061 RETM m = match_vtag (&tag_write, &inquire->write);
3062 RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
3063 RETM m = match_vtag (&tag_s_delim, &inquire->delim);
3064 RETM m = match_vtag (&tag_s_pad, &inquire->pad);
3065 RETM m = match_vtag (&tag_iolength, &inquire->iolength);
3066 RETM m = match_vtag (&tag_convert, &inquire->convert);
3067 RETM m = match_out_tag (&tag_strm_out, &inquire->strm_pos);
3068 RETM return MATCH_NO;
3071 #undef RETM
3074 match
3075 gfc_match_inquire (void)
3077 gfc_inquire *inquire;
3078 gfc_code *code;
3079 match m;
3080 locus loc;
3082 m = gfc_match_char ('(');
3083 if (m == MATCH_NO)
3084 return m;
3086 inquire = gfc_getmem (sizeof (gfc_inquire));
3088 loc = gfc_current_locus;
3090 m = match_inquire_element (inquire);
3091 if (m == MATCH_ERROR)
3092 goto cleanup;
3093 if (m == MATCH_NO)
3095 m = gfc_match_expr (&inquire->unit);
3096 if (m == MATCH_ERROR)
3097 goto cleanup;
3098 if (m == MATCH_NO)
3099 goto syntax;
3102 /* See if we have the IOLENGTH form of the inquire statement. */
3103 if (inquire->iolength != NULL)
3105 if (gfc_match_char (')') != MATCH_YES)
3106 goto syntax;
3108 m = match_io_list (M_INQUIRE, &code);
3109 if (m == MATCH_ERROR)
3110 goto cleanup;
3111 if (m == MATCH_NO)
3112 goto syntax;
3114 new_st.op = EXEC_IOLENGTH;
3115 new_st.expr = inquire->iolength;
3116 new_st.ext.inquire = inquire;
3118 if (gfc_pure (NULL))
3120 gfc_free_statements (code);
3121 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3122 return MATCH_ERROR;
3125 new_st.block = gfc_get_code ();
3126 new_st.block->op = EXEC_IOLENGTH;
3127 terminate_io (code);
3128 new_st.block->next = code;
3129 return MATCH_YES;
3132 /* At this point, we have the non-IOLENGTH inquire statement. */
3133 for (;;)
3135 if (gfc_match_char (')') == MATCH_YES)
3136 break;
3137 if (gfc_match_char (',') != MATCH_YES)
3138 goto syntax;
3140 m = match_inquire_element (inquire);
3141 if (m == MATCH_ERROR)
3142 goto cleanup;
3143 if (m == MATCH_NO)
3144 goto syntax;
3146 if (inquire->iolength != NULL)
3148 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3149 goto cleanup;
3153 if (gfc_match_eos () != MATCH_YES)
3154 goto syntax;
3156 if (inquire->unit != NULL && inquire->file != NULL)
3158 gfc_error ("INQUIRE statement at %L cannot contain both FILE and"
3159 " UNIT specifiers", &loc);
3160 goto cleanup;
3163 if (inquire->unit == NULL && inquire->file == NULL)
3165 gfc_error ("INQUIRE statement at %L requires either FILE or"
3166 " UNIT specifier", &loc);
3167 goto cleanup;
3170 if (gfc_pure (NULL))
3172 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3173 goto cleanup;
3176 new_st.op = EXEC_INQUIRE;
3177 new_st.ext.inquire = inquire;
3178 return MATCH_YES;
3180 syntax:
3181 gfc_syntax_error (ST_INQUIRE);
3183 cleanup:
3184 gfc_free_inquire (inquire);
3185 return MATCH_ERROR;
3189 /* Resolve everything in a gfc_inquire structure. */
3192 gfc_resolve_inquire (gfc_inquire * inquire)
3195 RESOLVE_TAG (&tag_unit, inquire->unit);
3196 RESOLVE_TAG (&tag_file, inquire->file);
3197 RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
3198 RESOLVE_TAG (&tag_iostat, inquire->iostat);
3199 RESOLVE_TAG (&tag_exist, inquire->exist);
3200 RESOLVE_TAG (&tag_opened, inquire->opened);
3201 RESOLVE_TAG (&tag_number, inquire->number);
3202 RESOLVE_TAG (&tag_named, inquire->named);
3203 RESOLVE_TAG (&tag_name, inquire->name);
3204 RESOLVE_TAG (&tag_s_access, inquire->access);
3205 RESOLVE_TAG (&tag_sequential, inquire->sequential);
3206 RESOLVE_TAG (&tag_direct, inquire->direct);
3207 RESOLVE_TAG (&tag_s_form, inquire->form);
3208 RESOLVE_TAG (&tag_formatted, inquire->formatted);
3209 RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
3210 RESOLVE_TAG (&tag_s_recl, inquire->recl);
3211 RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
3212 RESOLVE_TAG (&tag_s_blank, inquire->blank);
3213 RESOLVE_TAG (&tag_s_position, inquire->position);
3214 RESOLVE_TAG (&tag_s_action, inquire->action);
3215 RESOLVE_TAG (&tag_read, inquire->read);
3216 RESOLVE_TAG (&tag_write, inquire->write);
3217 RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
3218 RESOLVE_TAG (&tag_s_delim, inquire->delim);
3219 RESOLVE_TAG (&tag_s_pad, inquire->pad);
3220 RESOLVE_TAG (&tag_iolength, inquire->iolength);
3221 RESOLVE_TAG (&tag_convert, inquire->convert);
3222 RESOLVE_TAG (&tag_strm_out, inquire->strm_pos);
3224 if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)
3225 return FAILURE;
3227 return SUCCESS;