* gcc.dg/20061124-1.c: Add exit() function prototype.
[official-gcc.git] / gcc / fortran / io.c
blobadf274ee118949ef301c87dc15c9f41fbc1c6e8d
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_GNU, "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;
1139 return SUCCESS;
1143 /* Match a single tag of an OPEN statement. */
1145 static match
1146 match_open_element (gfc_open * open)
1148 match m;
1150 m = match_etag (&tag_unit, &open->unit);
1151 if (m != MATCH_NO)
1152 return m;
1153 m = match_out_tag (&tag_iomsg, &open->iomsg);
1154 if (m != MATCH_NO)
1155 return m;
1156 m = match_out_tag (&tag_iostat, &open->iostat);
1157 if (m != MATCH_NO)
1158 return m;
1159 m = match_etag (&tag_file, &open->file);
1160 if (m != MATCH_NO)
1161 return m;
1162 m = match_etag (&tag_status, &open->status);
1163 if (m != MATCH_NO)
1164 return m;
1165 m = match_etag (&tag_e_access, &open->access);
1166 if (m != MATCH_NO)
1167 return m;
1168 m = match_etag (&tag_e_form, &open->form);
1169 if (m != MATCH_NO)
1170 return m;
1171 m = match_etag (&tag_e_recl, &open->recl);
1172 if (m != MATCH_NO)
1173 return m;
1174 m = match_etag (&tag_e_blank, &open->blank);
1175 if (m != MATCH_NO)
1176 return m;
1177 m = match_etag (&tag_e_position, &open->position);
1178 if (m != MATCH_NO)
1179 return m;
1180 m = match_etag (&tag_e_action, &open->action);
1181 if (m != MATCH_NO)
1182 return m;
1183 m = match_etag (&tag_e_delim, &open->delim);
1184 if (m != MATCH_NO)
1185 return m;
1186 m = match_etag (&tag_e_pad, &open->pad);
1187 if (m != MATCH_NO)
1188 return m;
1189 m = match_ltag (&tag_err, &open->err);
1190 if (m != MATCH_NO)
1191 return m;
1192 m = match_etag (&tag_convert, &open->convert);
1193 if (m != MATCH_NO)
1194 return m;
1196 return MATCH_NO;
1200 /* Free the gfc_open structure and all the expressions it contains. */
1202 void
1203 gfc_free_open (gfc_open * open)
1206 if (open == NULL)
1207 return;
1209 gfc_free_expr (open->unit);
1210 gfc_free_expr (open->iomsg);
1211 gfc_free_expr (open->iostat);
1212 gfc_free_expr (open->file);
1213 gfc_free_expr (open->status);
1214 gfc_free_expr (open->access);
1215 gfc_free_expr (open->form);
1216 gfc_free_expr (open->recl);
1217 gfc_free_expr (open->blank);
1218 gfc_free_expr (open->position);
1219 gfc_free_expr (open->action);
1220 gfc_free_expr (open->delim);
1221 gfc_free_expr (open->pad);
1222 gfc_free_expr (open->convert);
1224 gfc_free (open);
1228 /* Resolve everything in a gfc_open structure. */
1231 gfc_resolve_open (gfc_open * open)
1234 RESOLVE_TAG (&tag_unit, open->unit);
1235 RESOLVE_TAG (&tag_iomsg, open->iomsg);
1236 RESOLVE_TAG (&tag_iostat, open->iostat);
1237 RESOLVE_TAG (&tag_file, open->file);
1238 RESOLVE_TAG (&tag_status, open->status);
1239 RESOLVE_TAG (&tag_e_access, open->access);
1240 RESOLVE_TAG (&tag_e_form, open->form);
1241 RESOLVE_TAG (&tag_e_recl, open->recl);
1243 RESOLVE_TAG (&tag_e_blank, open->blank);
1244 RESOLVE_TAG (&tag_e_position, open->position);
1245 RESOLVE_TAG (&tag_e_action, open->action);
1246 RESOLVE_TAG (&tag_e_delim, open->delim);
1247 RESOLVE_TAG (&tag_e_pad, open->pad);
1248 RESOLVE_TAG (&tag_convert, open->convert);
1250 if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1251 return FAILURE;
1253 return SUCCESS;
1258 /* Check if a given value for a SPECIFIER is either in the list of values
1259 allowed in F95 or F2003, issuing an error message and returning a zero
1260 value if it is not allowed. */
1261 static int
1262 compare_to_allowed_values (const char * specifier, const char * allowed[],
1263 const char * allowed_f2003[],
1264 const char * allowed_gnu[], char * value,
1265 const char * statement, bool warn)
1267 int i;
1268 unsigned int len;
1270 len = strlen(value);
1271 if (len > 0)
1273 for (len--; len > 0; len--)
1274 if (value[len] != ' ')
1275 break;
1276 len++;
1279 for (i = 0; allowed[i]; i++)
1280 if (len == strlen(allowed[i])
1281 && strncasecmp (value, allowed[i], strlen(allowed[i])) == 0)
1282 return 1;
1284 for (i = 0; allowed_f2003 && allowed_f2003[i]; i++)
1285 if (len == strlen(allowed_f2003[i])
1286 && strncasecmp (value, allowed_f2003[i], strlen(allowed_f2003[i])) == 0)
1288 notification n = gfc_notification_std (GFC_STD_F2003);
1290 if (n == WARNING || (warn && n == ERROR))
1292 gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1293 "has value '%s'", specifier, statement,
1294 allowed_f2003[i]);
1295 return 1;
1297 else
1298 if (n == ERROR)
1300 gfc_notify_std (GFC_STD_F2003, "Fortran 2003: %s specifier in "
1301 "%s statement at %C has value '%s'", specifier,
1302 statement, allowed_f2003[i]);
1303 return 0;
1306 /* n == SILENT */
1307 return 1;
1310 for (i = 0; allowed_gnu && allowed_gnu[i]; i++)
1311 if (len == strlen(allowed_gnu[i])
1312 && strncasecmp (value, allowed_gnu[i], strlen(allowed_gnu[i])) == 0)
1314 notification n = gfc_notification_std (GFC_STD_GNU);
1316 if (n == WARNING || (warn && n == ERROR))
1318 gfc_warning ("Extension: %s specifier in %s statement at %C "
1319 "has value '%s'", specifier, statement,
1320 allowed_gnu[i]);
1321 return 1;
1323 else
1324 if (n == ERROR)
1326 gfc_notify_std (GFC_STD_GNU, "Extension: %s specifier in "
1327 "%s statement at %C has value '%s'", specifier,
1328 statement, allowed_gnu[i]);
1329 return 0;
1332 /* n == SILENT */
1333 return 1;
1336 if (warn)
1338 gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1339 specifier, statement, value);
1340 return 1;
1342 else
1344 gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1345 specifier, statement, value);
1346 return 0;
1350 /* Match an OPEN statement. */
1352 match
1353 gfc_match_open (void)
1355 gfc_open *open;
1356 match m;
1357 bool warn;
1359 m = gfc_match_char ('(');
1360 if (m == MATCH_NO)
1361 return m;
1363 open = gfc_getmem (sizeof (gfc_open));
1365 m = match_open_element (open);
1367 if (m == MATCH_ERROR)
1368 goto cleanup;
1369 if (m == MATCH_NO)
1371 m = gfc_match_expr (&open->unit);
1372 if (m == MATCH_NO)
1373 goto syntax;
1374 if (m == MATCH_ERROR)
1375 goto cleanup;
1378 for (;;)
1380 if (gfc_match_char (')') == MATCH_YES)
1381 break;
1382 if (gfc_match_char (',') != MATCH_YES)
1383 goto syntax;
1385 m = match_open_element (open);
1386 if (m == MATCH_ERROR)
1387 goto cleanup;
1388 if (m == MATCH_NO)
1389 goto syntax;
1392 if (gfc_match_eos () == MATCH_NO)
1393 goto syntax;
1395 if (gfc_pure (NULL))
1397 gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1398 goto cleanup;
1401 warn = (open->err || open->iostat) ? true : false;
1402 /* Checks on the ACCESS specifier. */
1403 if (open->access && open->access->expr_type == EXPR_CONSTANT)
1405 static const char * access_f95[] = { "SEQUENTIAL", "DIRECT", NULL };
1406 static const char * access_f2003[] = { "STREAM", NULL };
1407 static const char * access_gnu[] = { "APPEND", NULL };
1409 if (!compare_to_allowed_values ("ACCESS", access_f95, access_f2003,
1410 access_gnu,
1411 open->access->value.character.string,
1412 "OPEN", warn))
1413 goto cleanup;
1416 /* Checks on the ACTION specifier. */
1417 if (open->action && open->action->expr_type == EXPR_CONSTANT)
1419 static const char * action[] = { "READ", "WRITE", "READWRITE", NULL };
1421 if (!compare_to_allowed_values ("ACTION", action, NULL, NULL,
1422 open->action->value.character.string,
1423 "OPEN", warn))
1424 goto cleanup;
1427 /* Checks on the ASYNCHRONOUS specifier. */
1428 /* TODO: code is ready, just needs uncommenting when async I/O support
1429 is added ;-)
1430 if (open->asynchronous && open->asynchronous->expr_type == EXPR_CONSTANT)
1432 static const char * asynchronous[] = { "YES", "NO", NULL };
1434 if (!compare_to_allowed_values
1435 ("action", asynchronous, NULL, NULL,
1436 open->asynchronous->value.character.string, "OPEN", warn))
1437 goto cleanup;
1440 /* Checks on the BLANK specifier. */
1441 if (open->blank && open->blank->expr_type == EXPR_CONSTANT)
1443 static const char * blank[] = { "ZERO", "NULL", NULL };
1445 if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
1446 open->blank->value.character.string,
1447 "OPEN", warn))
1448 goto cleanup;
1451 /* Checks on the DECIMAL specifier. */
1452 /* TODO: uncomment this code when DECIMAL support is added
1453 if (open->decimal && open->decimal->expr_type == EXPR_CONSTANT)
1455 static const char * decimal[] = { "COMMA", "POINT", NULL };
1457 if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1458 open->decimal->value.character.string,
1459 "OPEN", warn))
1460 goto cleanup;
1461 } */
1463 /* Checks on the DELIM specifier. */
1464 if (open->delim && open->delim->expr_type == EXPR_CONSTANT)
1466 static const char * delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
1468 if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
1469 open->delim->value.character.string,
1470 "OPEN", warn))
1471 goto cleanup;
1474 /* Checks on the ENCODING specifier. */
1475 /* TODO: uncomment this code when ENCODING support is added
1476 if (open->encoding && open->encoding->expr_type == EXPR_CONSTANT)
1478 static const char * encoding[] = { "UTF-8", "DEFAULT", NULL };
1480 if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1481 open->encoding->value.character.string,
1482 "OPEN", warn))
1483 goto cleanup;
1484 } */
1486 /* Checks on the FORM specifier. */
1487 if (open->form && open->form->expr_type == EXPR_CONSTANT)
1489 static const char * form[] = { "FORMATTED", "UNFORMATTED", NULL };
1491 if (!compare_to_allowed_values ("FORM", form, NULL, NULL,
1492 open->form->value.character.string,
1493 "OPEN", warn))
1494 goto cleanup;
1497 /* Checks on the PAD specifier. */
1498 if (open->pad && open->pad->expr_type == EXPR_CONSTANT)
1500 static const char * pad[] = { "YES", "NO", NULL };
1502 if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
1503 open->pad->value.character.string,
1504 "OPEN", warn))
1505 goto cleanup;
1508 /* Checks on the POSITION specifier. */
1509 if (open->position && open->position->expr_type == EXPR_CONSTANT)
1511 static const char * position[] = { "ASIS", "REWIND", "APPEND", NULL };
1513 if (!compare_to_allowed_values ("POSITION", position, NULL, NULL,
1514 open->position->value.character.string,
1515 "OPEN", warn))
1516 goto cleanup;
1519 /* Checks on the ROUND specifier. */
1520 /* TODO: uncomment this code when ROUND support is added
1521 if (open->round && open->round->expr_type == EXPR_CONSTANT)
1523 static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1524 "COMPATIBLE", "PROCESSOR_DEFINED", NULL };
1526 if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1527 open->round->value.character.string,
1528 "OPEN", warn))
1529 goto cleanup;
1530 } */
1532 /* Checks on the SIGN specifier. */
1533 /* TODO: uncomment this code when SIGN support is added
1534 if (open->sign && open->sign->expr_type == EXPR_CONSTANT)
1536 static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1537 NULL };
1539 if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1540 open->sign->value.character.string,
1541 "OPEN", warn))
1542 goto cleanup;
1543 } */
1545 #define warn_or_error(...) \
1547 if (warn) \
1548 gfc_warning (__VA_ARGS__); \
1549 else \
1551 gfc_error (__VA_ARGS__); \
1552 goto cleanup; \
1556 /* Checks on the RECL specifier. */
1557 if (open->recl && open->recl->expr_type == EXPR_CONSTANT
1558 && open->recl->ts.type == BT_INTEGER
1559 && mpz_sgn (open->recl->value.integer) != 1)
1561 warn_or_error ("RECL in OPEN statement at %C must be positive");
1564 /* Checks on the STATUS specifier. */
1565 if (open->status && open->status->expr_type == EXPR_CONSTANT)
1567 static const char * status[] = { "OLD", "NEW", "SCRATCH",
1568 "REPLACE", "UNKNOWN", NULL };
1570 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1571 open->status->value.character.string,
1572 "OPEN", warn))
1573 goto cleanup;
1575 /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1576 the FILE= specifier shall appear. */
1577 if (open->file == NULL &&
1578 (strncasecmp (open->status->value.character.string, "replace", 7) == 0
1579 || strncasecmp (open->status->value.character.string, "new", 3) == 0))
1581 warn_or_error ("The STATUS specified in OPEN statement at %C is '%s' "
1582 "and no FILE specifier is present",
1583 open->status->value.character.string);
1586 /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1587 the FILE= specifier shall not appear. */
1588 if (strncasecmp (open->status->value.character.string, "scratch", 7) == 0
1589 && open->file)
1591 warn_or_error ("The STATUS specified in OPEN statement at %C cannot "
1592 "have the value SCRATCH if a FILE specifier "
1593 "is present");
1597 /* Things that are not allowed for unformatted I/O. */
1598 if (open->form && open->form->expr_type == EXPR_CONSTANT
1599 && (open->delim
1600 /* TODO uncomment this code when F2003 support is finished */
1601 /* || open->decimal || open->encoding || open->round
1602 || open->sign */
1603 || open->pad || open->blank)
1604 && strncasecmp (open->form->value.character.string,
1605 "unformatted", 11) == 0)
1607 const char * spec = (open->delim ? "DELIM " : (open->pad ? "PAD " :
1608 open->blank ? "BLANK " : ""));
1610 warn_or_error ("%sspecifier at %C not allowed in OPEN statement for "
1611 "unformatted I/O", spec);
1614 if (open->recl && open->access && open->access->expr_type == EXPR_CONSTANT
1615 && strncasecmp (open->access->value.character.string, "stream", 6) == 0)
1617 warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
1618 "stream I/O");
1621 if (open->position && open->access && open->access->expr_type == EXPR_CONSTANT
1622 && !(strncasecmp (open->access->value.character.string,
1623 "sequential", 10) == 0
1624 || strncasecmp (open->access->value.character.string,
1625 "stream", 6) == 0
1626 || strncasecmp (open->access->value.character.string,
1627 "append", 6) == 0))
1629 warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
1630 "for stream or sequential ACCESS");
1633 #undef warn_or_error
1635 new_st.op = EXEC_OPEN;
1636 new_st.ext.open = open;
1637 return MATCH_YES;
1639 syntax:
1640 gfc_syntax_error (ST_OPEN);
1642 cleanup:
1643 gfc_free_open (open);
1644 return MATCH_ERROR;
1648 /* Free a gfc_close structure an all its expressions. */
1650 void
1651 gfc_free_close (gfc_close * close)
1654 if (close == NULL)
1655 return;
1657 gfc_free_expr (close->unit);
1658 gfc_free_expr (close->iomsg);
1659 gfc_free_expr (close->iostat);
1660 gfc_free_expr (close->status);
1662 gfc_free (close);
1666 /* Match elements of a CLOSE statement. */
1668 static match
1669 match_close_element (gfc_close * close)
1671 match m;
1673 m = match_etag (&tag_unit, &close->unit);
1674 if (m != MATCH_NO)
1675 return m;
1676 m = match_etag (&tag_status, &close->status);
1677 if (m != MATCH_NO)
1678 return m;
1679 m = match_out_tag (&tag_iomsg, &close->iomsg);
1680 if (m != MATCH_NO)
1681 return m;
1682 m = match_out_tag (&tag_iostat, &close->iostat);
1683 if (m != MATCH_NO)
1684 return m;
1685 m = match_ltag (&tag_err, &close->err);
1686 if (m != MATCH_NO)
1687 return m;
1689 return MATCH_NO;
1693 /* Match a CLOSE statement. */
1695 match
1696 gfc_match_close (void)
1698 gfc_close *close;
1699 match m;
1700 bool warn;
1702 m = gfc_match_char ('(');
1703 if (m == MATCH_NO)
1704 return m;
1706 close = gfc_getmem (sizeof (gfc_close));
1708 m = match_close_element (close);
1710 if (m == MATCH_ERROR)
1711 goto cleanup;
1712 if (m == MATCH_NO)
1714 m = gfc_match_expr (&close->unit);
1715 if (m == MATCH_NO)
1716 goto syntax;
1717 if (m == MATCH_ERROR)
1718 goto cleanup;
1721 for (;;)
1723 if (gfc_match_char (')') == MATCH_YES)
1724 break;
1725 if (gfc_match_char (',') != MATCH_YES)
1726 goto syntax;
1728 m = match_close_element (close);
1729 if (m == MATCH_ERROR)
1730 goto cleanup;
1731 if (m == MATCH_NO)
1732 goto syntax;
1735 if (gfc_match_eos () == MATCH_NO)
1736 goto syntax;
1738 if (gfc_pure (NULL))
1740 gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1741 goto cleanup;
1744 warn = (close->iostat || close->err) ? true : false;
1746 /* Checks on the STATUS specifier. */
1747 if (close->status && close->status->expr_type == EXPR_CONSTANT)
1749 static const char * status[] = { "KEEP", "DELETE", NULL };
1751 if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1752 close->status->value.character.string,
1753 "CLOSE", warn))
1754 goto cleanup;
1757 new_st.op = EXEC_CLOSE;
1758 new_st.ext.close = close;
1759 return MATCH_YES;
1761 syntax:
1762 gfc_syntax_error (ST_CLOSE);
1764 cleanup:
1765 gfc_free_close (close);
1766 return MATCH_ERROR;
1770 /* Resolve everything in a gfc_close structure. */
1773 gfc_resolve_close (gfc_close * close)
1776 RESOLVE_TAG (&tag_unit, close->unit);
1777 RESOLVE_TAG (&tag_iomsg, close->iomsg);
1778 RESOLVE_TAG (&tag_iostat, close->iostat);
1779 RESOLVE_TAG (&tag_status, close->status);
1781 if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
1782 return FAILURE;
1784 return SUCCESS;
1788 /* Free a gfc_filepos structure. */
1790 void
1791 gfc_free_filepos (gfc_filepos * fp)
1794 gfc_free_expr (fp->unit);
1795 gfc_free_expr (fp->iomsg);
1796 gfc_free_expr (fp->iostat);
1797 gfc_free (fp);
1801 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement. */
1803 static match
1804 match_file_element (gfc_filepos * fp)
1806 match m;
1808 m = match_etag (&tag_unit, &fp->unit);
1809 if (m != MATCH_NO)
1810 return m;
1811 m = match_out_tag (&tag_iomsg, &fp->iomsg);
1812 if (m != MATCH_NO)
1813 return m;
1814 m = match_out_tag (&tag_iostat, &fp->iostat);
1815 if (m != MATCH_NO)
1816 return m;
1817 m = match_ltag (&tag_err, &fp->err);
1818 if (m != MATCH_NO)
1819 return m;
1821 return MATCH_NO;
1825 /* Match the second half of the file-positioning statements, REWIND,
1826 BACKSPACE, ENDFILE, or the FLUSH statement. */
1828 static match
1829 match_filepos (gfc_statement st, gfc_exec_op op)
1831 gfc_filepos *fp;
1832 match m;
1834 fp = gfc_getmem (sizeof (gfc_filepos));
1836 if (gfc_match_char ('(') == MATCH_NO)
1838 m = gfc_match_expr (&fp->unit);
1839 if (m == MATCH_ERROR)
1840 goto cleanup;
1841 if (m == MATCH_NO)
1842 goto syntax;
1844 goto done;
1847 m = match_file_element (fp);
1848 if (m == MATCH_ERROR)
1849 goto done;
1850 if (m == MATCH_NO)
1852 m = gfc_match_expr (&fp->unit);
1853 if (m == MATCH_ERROR)
1854 goto done;
1855 if (m == MATCH_NO)
1856 goto syntax;
1859 for (;;)
1861 if (gfc_match_char (')') == MATCH_YES)
1862 break;
1863 if (gfc_match_char (',') != MATCH_YES)
1864 goto syntax;
1866 m = match_file_element (fp);
1867 if (m == MATCH_ERROR)
1868 goto cleanup;
1869 if (m == MATCH_NO)
1870 goto syntax;
1873 done:
1874 if (gfc_match_eos () != MATCH_YES)
1875 goto syntax;
1877 if (gfc_pure (NULL))
1879 gfc_error ("%s statement not allowed in PURE procedure at %C",
1880 gfc_ascii_statement (st));
1882 goto cleanup;
1885 new_st.op = op;
1886 new_st.ext.filepos = fp;
1887 return MATCH_YES;
1889 syntax:
1890 gfc_syntax_error (st);
1892 cleanup:
1893 gfc_free_filepos (fp);
1894 return MATCH_ERROR;
1899 gfc_resolve_filepos (gfc_filepos * fp)
1902 RESOLVE_TAG (&tag_unit, fp->unit);
1903 RESOLVE_TAG (&tag_iostat, fp->iostat);
1904 RESOLVE_TAG (&tag_iomsg, fp->iomsg);
1905 if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
1906 return FAILURE;
1908 return SUCCESS;
1912 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
1913 and the FLUSH statement. */
1915 match
1916 gfc_match_endfile (void)
1919 return match_filepos (ST_END_FILE, EXEC_ENDFILE);
1922 match
1923 gfc_match_backspace (void)
1926 return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
1929 match
1930 gfc_match_rewind (void)
1933 return match_filepos (ST_REWIND, EXEC_REWIND);
1936 match
1937 gfc_match_flush (void)
1939 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: FLUSH statement at %C") == FAILURE)
1940 return MATCH_ERROR;
1942 return match_filepos (ST_FLUSH, EXEC_FLUSH);
1945 /******************** Data Transfer Statements *********************/
1947 typedef enum
1948 { M_READ, M_WRITE, M_PRINT, M_INQUIRE }
1949 io_kind;
1952 /* Return a default unit number. */
1954 static gfc_expr *
1955 default_unit (io_kind k)
1957 int unit;
1959 if (k == M_READ)
1960 unit = 5;
1961 else
1962 unit = 6;
1964 return gfc_int_expr (unit);
1968 /* Match a unit specification for a data transfer statement. */
1970 static match
1971 match_dt_unit (io_kind k, gfc_dt * dt)
1973 gfc_expr *e;
1975 if (gfc_match_char ('*') == MATCH_YES)
1977 if (dt->io_unit != NULL)
1978 goto conflict;
1980 dt->io_unit = default_unit (k);
1981 return MATCH_YES;
1984 if (gfc_match_expr (&e) == MATCH_YES)
1986 if (dt->io_unit != NULL)
1988 gfc_free_expr (e);
1989 goto conflict;
1992 dt->io_unit = e;
1993 return MATCH_YES;
1996 return MATCH_NO;
1998 conflict:
1999 gfc_error ("Duplicate UNIT specification at %C");
2000 return MATCH_ERROR;
2004 /* Match a format specification. */
2006 static match
2007 match_dt_format (gfc_dt * dt)
2009 locus where;
2010 gfc_expr *e;
2011 gfc_st_label *label;
2013 where = gfc_current_locus;
2015 if (gfc_match_char ('*') == MATCH_YES)
2017 if (dt->format_expr != NULL || dt->format_label != NULL)
2018 goto conflict;
2020 dt->format_label = &format_asterisk;
2021 return MATCH_YES;
2024 if (gfc_match_st_label (&label) == MATCH_YES)
2026 if (dt->format_expr != NULL || dt->format_label != NULL)
2028 gfc_free_st_label (label);
2029 goto conflict;
2032 if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
2033 return MATCH_ERROR;
2035 dt->format_label = label;
2036 return MATCH_YES;
2039 if (gfc_match_expr (&e) == MATCH_YES)
2041 if (dt->format_expr != NULL || dt->format_label != NULL)
2043 gfc_free_expr (e);
2044 goto conflict;
2046 dt->format_expr = e;
2047 return MATCH_YES;
2050 gfc_current_locus = where; /* The only case where we have to restore */
2052 return MATCH_NO;
2054 conflict:
2055 gfc_error ("Duplicate format specification at %C");
2056 return MATCH_ERROR;
2060 /* Traverse a namelist that is part of a READ statement to make sure
2061 that none of the variables in the namelist are INTENT(IN). Returns
2062 nonzero if we find such a variable. */
2064 static int
2065 check_namelist (gfc_symbol * sym)
2067 gfc_namelist *p;
2069 for (p = sym->namelist; p; p = p->next)
2070 if (p->sym->attr.intent == INTENT_IN)
2072 gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2073 p->sym->name, sym->name);
2074 return 1;
2077 return 0;
2081 /* Match a single data transfer element. */
2083 static match
2084 match_dt_element (io_kind k, gfc_dt * dt)
2086 char name[GFC_MAX_SYMBOL_LEN + 1];
2087 gfc_symbol *sym;
2088 match m;
2090 if (gfc_match (" unit =") == MATCH_YES)
2092 m = match_dt_unit (k, dt);
2093 if (m != MATCH_NO)
2094 return m;
2097 if (gfc_match (" fmt =") == MATCH_YES)
2099 m = match_dt_format (dt);
2100 if (m != MATCH_NO)
2101 return m;
2104 if (gfc_match (" nml = %n", name) == MATCH_YES)
2106 if (dt->namelist != NULL)
2108 gfc_error ("Duplicate NML specification at %C");
2109 return MATCH_ERROR;
2112 if (gfc_find_symbol (name, NULL, 1, &sym))
2113 return MATCH_ERROR;
2115 if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
2117 gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2118 sym != NULL ? sym->name : name);
2119 return MATCH_ERROR;
2122 dt->namelist = sym;
2123 if (k == M_READ && check_namelist (sym))
2124 return MATCH_ERROR;
2126 return MATCH_YES;
2129 m = match_etag (&tag_rec, &dt->rec);
2130 if (m != MATCH_NO)
2131 return m;
2132 m = match_etag (&tag_spos, &dt->rec);
2133 if (m != MATCH_NO)
2134 return m;
2135 m = match_out_tag (&tag_iomsg, &dt->iomsg);
2136 if (m != MATCH_NO)
2137 return m;
2138 m = match_out_tag (&tag_iostat, &dt->iostat);
2139 if (m != MATCH_NO)
2140 return m;
2141 m = match_ltag (&tag_err, &dt->err);
2142 if (m == MATCH_YES)
2143 dt->err_where = gfc_current_locus;
2144 if (m != MATCH_NO)
2145 return m;
2146 m = match_etag (&tag_advance, &dt->advance);
2147 if (m != MATCH_NO)
2148 return m;
2149 m = match_out_tag (&tag_size, &dt->size);
2150 if (m != MATCH_NO)
2151 return m;
2153 m = match_ltag (&tag_end, &dt->end);
2154 if (m == MATCH_YES)
2156 if (k == M_WRITE)
2158 gfc_error ("END tag at %C not allowed in output statement");
2159 return MATCH_ERROR;
2161 dt->end_where = gfc_current_locus;
2163 if (m != MATCH_NO)
2164 return m;
2166 m = match_ltag (&tag_eor, &dt->eor);
2167 if (m == MATCH_YES)
2168 dt->eor_where = gfc_current_locus;
2169 if (m != MATCH_NO)
2170 return m;
2172 return MATCH_NO;
2176 /* Free a data transfer structure and everything below it. */
2178 void
2179 gfc_free_dt (gfc_dt * dt)
2182 if (dt == NULL)
2183 return;
2185 gfc_free_expr (dt->io_unit);
2186 gfc_free_expr (dt->format_expr);
2187 gfc_free_expr (dt->rec);
2188 gfc_free_expr (dt->advance);
2189 gfc_free_expr (dt->iomsg);
2190 gfc_free_expr (dt->iostat);
2191 gfc_free_expr (dt->size);
2193 gfc_free (dt);
2197 /* Resolve everything in a gfc_dt structure. */
2200 gfc_resolve_dt (gfc_dt * dt)
2202 gfc_expr *e;
2204 RESOLVE_TAG (&tag_format, dt->format_expr);
2205 RESOLVE_TAG (&tag_rec, dt->rec);
2206 RESOLVE_TAG (&tag_spos, dt->rec);
2207 RESOLVE_TAG (&tag_advance, dt->advance);
2208 RESOLVE_TAG (&tag_iomsg, dt->iomsg);
2209 RESOLVE_TAG (&tag_iostat, dt->iostat);
2210 RESOLVE_TAG (&tag_size, dt->size);
2212 e = dt->io_unit;
2213 if (gfc_resolve_expr (e) == SUCCESS
2214 && (e->ts.type != BT_INTEGER
2215 && (e->ts.type != BT_CHARACTER
2216 || e->expr_type != EXPR_VARIABLE)))
2218 gfc_error
2219 ("UNIT specification at %L must be an INTEGER expression or a "
2220 "CHARACTER variable", &e->where);
2221 return FAILURE;
2224 if (e->ts.type == BT_CHARACTER)
2226 if (gfc_has_vector_index (e))
2228 gfc_error ("Internal unit with vector subscript at %L",
2229 &e->where);
2230 return FAILURE;
2234 if (e->rank && e->ts.type != BT_CHARACTER)
2236 gfc_error ("External IO UNIT cannot be an array at %L", &e->where);
2237 return FAILURE;
2240 if (dt->err)
2242 if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
2243 return FAILURE;
2244 if (dt->err->defined == ST_LABEL_UNKNOWN)
2246 gfc_error ("ERR tag label %d at %L not defined",
2247 dt->err->value, &dt->err_where);
2248 return FAILURE;
2252 if (dt->end)
2254 if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
2255 return FAILURE;
2256 if (dt->end->defined == ST_LABEL_UNKNOWN)
2258 gfc_error ("END tag label %d at %L not defined",
2259 dt->end->value, &dt->end_where);
2260 return FAILURE;
2264 if (dt->eor)
2266 if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
2267 return FAILURE;
2268 if (dt->eor->defined == ST_LABEL_UNKNOWN)
2270 gfc_error ("EOR tag label %d at %L not defined",
2271 dt->eor->value, &dt->eor_where);
2272 return FAILURE;
2276 /* Check the format label actually exists. */
2277 if (dt->format_label && dt->format_label != &format_asterisk
2278 && dt->format_label->defined == ST_LABEL_UNKNOWN)
2280 gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
2281 &dt->format_label->where);
2282 return FAILURE;
2284 return SUCCESS;
2288 /* Given an io_kind, return its name. */
2290 static const char *
2291 io_kind_name (io_kind k)
2293 const char *name;
2295 switch (k)
2297 case M_READ:
2298 name = "READ";
2299 break;
2300 case M_WRITE:
2301 name = "WRITE";
2302 break;
2303 case M_PRINT:
2304 name = "PRINT";
2305 break;
2306 case M_INQUIRE:
2307 name = "INQUIRE";
2308 break;
2309 default:
2310 gfc_internal_error ("io_kind_name(): bad I/O-kind");
2313 return name;
2317 /* Match an IO iteration statement of the form:
2319 ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2321 which is equivalent to a single IO element. This function is
2322 mutually recursive with match_io_element(). */
2324 static match match_io_element (io_kind k, gfc_code **);
2326 static match
2327 match_io_iterator (io_kind k, gfc_code ** result)
2329 gfc_code *head, *tail, *new;
2330 gfc_iterator *iter;
2331 locus old_loc;
2332 match m;
2333 int n;
2335 iter = NULL;
2336 head = NULL;
2337 old_loc = gfc_current_locus;
2339 if (gfc_match_char ('(') != MATCH_YES)
2340 return MATCH_NO;
2342 m = match_io_element (k, &head);
2343 tail = head;
2345 if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
2347 m = MATCH_NO;
2348 goto cleanup;
2351 /* Can't be anything but an IO iterator. Build a list. */
2352 iter = gfc_get_iterator ();
2354 for (n = 1;; n++)
2356 m = gfc_match_iterator (iter, 0);
2357 if (m == MATCH_ERROR)
2358 goto cleanup;
2359 if (m == MATCH_YES)
2361 gfc_check_do_variable (iter->var->symtree);
2362 break;
2365 m = match_io_element (k, &new);
2366 if (m == MATCH_ERROR)
2367 goto cleanup;
2368 if (m == MATCH_NO)
2370 if (n > 2)
2371 goto syntax;
2372 goto cleanup;
2375 tail = gfc_append_code (tail, new);
2377 if (gfc_match_char (',') != MATCH_YES)
2379 if (n > 2)
2380 goto syntax;
2381 m = MATCH_NO;
2382 goto cleanup;
2386 if (gfc_match_char (')') != MATCH_YES)
2387 goto syntax;
2389 new = gfc_get_code ();
2390 new->op = EXEC_DO;
2391 new->ext.iterator = iter;
2393 new->block = gfc_get_code ();
2394 new->block->op = EXEC_DO;
2395 new->block->next = head;
2397 *result = new;
2398 return MATCH_YES;
2400 syntax:
2401 gfc_error ("Syntax error in I/O iterator at %C");
2402 m = MATCH_ERROR;
2404 cleanup:
2405 gfc_free_iterator (iter, 1);
2406 gfc_free_statements (head);
2407 gfc_current_locus = old_loc;
2408 return m;
2412 /* Match a single element of an IO list, which is either a single
2413 expression or an IO Iterator. */
2415 static match
2416 match_io_element (io_kind k, gfc_code ** cpp)
2418 gfc_expr *expr;
2419 gfc_code *cp;
2420 match m;
2422 expr = NULL;
2424 m = match_io_iterator (k, cpp);
2425 if (m == MATCH_YES)
2426 return MATCH_YES;
2428 if (k == M_READ)
2430 m = gfc_match_variable (&expr, 0);
2431 if (m == MATCH_NO)
2432 gfc_error ("Expected variable in READ statement at %C");
2434 else
2436 m = gfc_match_expr (&expr);
2437 if (m == MATCH_NO)
2438 gfc_error ("Expected expression in %s statement at %C",
2439 io_kind_name (k));
2442 if (m == MATCH_YES)
2443 switch (k)
2445 case M_READ:
2446 if (expr->symtree->n.sym->attr.intent == INTENT_IN)
2448 gfc_error
2449 ("Variable '%s' in input list at %C cannot be INTENT(IN)",
2450 expr->symtree->n.sym->name);
2451 m = MATCH_ERROR;
2454 if (gfc_pure (NULL)
2455 && gfc_impure_variable (expr->symtree->n.sym)
2456 && current_dt->io_unit->ts.type == BT_CHARACTER)
2458 gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2459 expr->symtree->n.sym->name);
2460 m = MATCH_ERROR;
2463 if (gfc_check_do_variable (expr->symtree))
2464 m = MATCH_ERROR;
2466 break;
2468 case M_WRITE:
2469 if (current_dt->io_unit->ts.type == BT_CHARACTER
2470 && gfc_pure (NULL)
2471 && current_dt->io_unit->expr_type == EXPR_VARIABLE
2472 && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
2474 gfc_error
2475 ("Cannot write to internal file unit '%s' at %C inside a "
2476 "PURE procedure", current_dt->io_unit->symtree->n.sym->name);
2477 m = MATCH_ERROR;
2480 break;
2482 default:
2483 break;
2486 if (m != MATCH_YES)
2488 gfc_free_expr (expr);
2489 return MATCH_ERROR;
2492 cp = gfc_get_code ();
2493 cp->op = EXEC_TRANSFER;
2494 cp->expr = expr;
2496 *cpp = cp;
2497 return MATCH_YES;
2501 /* Match an I/O list, building gfc_code structures as we go. */
2503 static match
2504 match_io_list (io_kind k, gfc_code ** head_p)
2506 gfc_code *head, *tail, *new;
2507 match m;
2509 *head_p = head = tail = NULL;
2510 if (gfc_match_eos () == MATCH_YES)
2511 return MATCH_YES;
2513 for (;;)
2515 m = match_io_element (k, &new);
2516 if (m == MATCH_ERROR)
2517 goto cleanup;
2518 if (m == MATCH_NO)
2519 goto syntax;
2521 tail = gfc_append_code (tail, new);
2522 if (head == NULL)
2523 head = new;
2525 if (gfc_match_eos () == MATCH_YES)
2526 break;
2527 if (gfc_match_char (',') != MATCH_YES)
2528 goto syntax;
2531 *head_p = head;
2532 return MATCH_YES;
2534 syntax:
2535 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2537 cleanup:
2538 gfc_free_statements (head);
2539 return MATCH_ERROR;
2543 /* Attach the data transfer end node. */
2545 static void
2546 terminate_io (gfc_code * io_code)
2548 gfc_code *c;
2550 if (io_code == NULL)
2551 io_code = new_st.block;
2553 c = gfc_get_code ();
2554 c->op = EXEC_DT_END;
2556 /* Point to structure that is already there */
2557 c->ext.dt = new_st.ext.dt;
2558 gfc_append_code (io_code, c);
2562 /* Check the constraints for a data transfer statement. The majority of the
2563 constraints appearing in 9.4 of the standard appear here. Some are handled
2564 in resolve_tag and others in gfc_resolve_dt. */
2566 static match
2567 check_io_constraints (io_kind k, gfc_dt *dt, gfc_code * io_code, locus * spec_end)
2569 #define io_constraint(condition,msg,arg)\
2570 if (condition) \
2572 gfc_error(msg,arg);\
2573 m = MATCH_ERROR;\
2576 match m;
2577 gfc_expr * expr;
2578 gfc_symbol * sym = NULL;
2580 m = MATCH_YES;
2582 expr = dt->io_unit;
2583 if (expr && expr->expr_type == EXPR_VARIABLE
2584 && expr->ts.type == BT_CHARACTER)
2586 sym = expr->symtree->n.sym;
2588 io_constraint (k == M_WRITE && sym->attr.intent == INTENT_IN,
2589 "Internal file at %L must not be INTENT(IN)",
2590 &expr->where);
2592 io_constraint (gfc_has_vector_index (dt->io_unit),
2593 "Internal file incompatible with vector subscript at %L",
2594 &expr->where);
2596 io_constraint (dt->rec != NULL,
2597 "REC tag at %L is incompatible with internal file",
2598 &dt->rec->where);
2600 if (dt->namelist != NULL)
2602 if (gfc_notify_std(GFC_STD_F2003,
2603 "Fortran 2003: Internal file at %L with namelist",
2604 &expr->where) == FAILURE)
2605 m = MATCH_ERROR;
2608 io_constraint (dt->advance != NULL,
2609 "ADVANCE tag at %L is incompatible with internal file",
2610 &dt->advance->where);
2613 if (expr && expr->ts.type != BT_CHARACTER)
2616 io_constraint (gfc_pure (NULL)
2617 && (k == M_READ || k == M_WRITE),
2618 "IO UNIT in %s statement at %C must be "
2619 "an internal file in a PURE procedure",
2620 io_kind_name (k));
2624 if (k != M_READ)
2626 io_constraint (dt->end,
2627 "END tag not allowed with output at %L",
2628 &dt->end_where);
2630 io_constraint (dt->eor,
2631 "EOR tag not allowed with output at %L",
2632 &dt->eor_where);
2634 io_constraint (k != M_READ && dt->size,
2635 "SIZE=specifier not allowed with output at %L",
2636 &dt->size->where);
2638 else
2640 io_constraint (dt->size && dt->advance == NULL,
2641 "SIZE tag at %L requires an ADVANCE tag",
2642 &dt->size->where);
2644 io_constraint (dt->eor && dt->advance == NULL,
2645 "EOR tag at %L requires an ADVANCE tag",
2646 &dt->eor_where);
2651 if (dt->namelist)
2653 io_constraint (io_code && dt->namelist,
2654 "NAMELIST cannot be followed by IO-list at %L",
2655 &io_code->loc);
2657 io_constraint (dt->format_expr,
2658 "IO spec-list cannot contain both NAMELIST group name "
2659 "and format specification at %L.",
2660 &dt->format_expr->where);
2662 io_constraint (dt->format_label,
2663 "IO spec-list cannot contain both NAMELIST group name "
2664 "and format label at %L", spec_end);
2666 io_constraint (dt->rec,
2667 "NAMELIST IO is not allowed with a REC=specifier "
2668 "at %L.", &dt->rec->where);
2670 io_constraint (dt->advance,
2671 "NAMELIST IO is not allowed with a ADVANCE=specifier "
2672 "at %L.", &dt->advance->where);
2675 if (dt->rec)
2677 io_constraint (dt->end,
2678 "An END tag is not allowed with a "
2679 "REC=specifier at %L.", &dt->end_where);
2682 io_constraint (dt->format_label == &format_asterisk,
2683 "FMT=* is not allowed with a REC=specifier "
2684 "at %L.", spec_end);
2687 if (dt->advance)
2689 int not_yes, not_no;
2690 expr = dt->advance;
2692 io_constraint (dt->format_label == &format_asterisk,
2693 "List directed format(*) is not allowed with a "
2694 "ADVANCE=specifier at %L.", &expr->where);
2696 io_constraint (dt->format_expr == NULL
2697 && dt->format_label == NULL
2698 && dt->namelist == NULL,
2699 "the ADVANCE=specifier at %L must appear with an "
2700 "explicit format expression", &expr->where);
2702 if (expr->expr_type == EXPR_CONSTANT && expr->ts.type == BT_CHARACTER)
2704 const char * advance = expr->value.character.string;
2705 not_no = strcasecmp (advance, "no") != 0;
2706 not_yes = strcasecmp (advance, "yes") != 0;
2708 else
2710 not_no = 0;
2711 not_yes = 0;
2714 io_constraint (not_no && not_yes,
2715 "ADVANCE=specifier at %L must have value = "
2716 "YES or NO.", &expr->where);
2718 io_constraint (dt->size && not_no && k == M_READ,
2719 "SIZE tag at %L requires an ADVANCE = 'NO'",
2720 &dt->size->where);
2722 io_constraint (dt->eor && not_no && k == M_READ,
2723 "EOR tag at %L requires an ADVANCE = 'NO'",
2724 &dt->eor_where);
2727 expr = dt->format_expr;
2728 if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
2729 check_format_string (expr);
2731 return m;
2733 #undef io_constraint
2735 /* Match a READ, WRITE or PRINT statement. */
2737 static match
2738 match_io (io_kind k)
2740 char name[GFC_MAX_SYMBOL_LEN + 1];
2741 gfc_code *io_code;
2742 gfc_symbol *sym;
2743 int comma_flag, c;
2744 locus where;
2745 locus spec_end;
2746 gfc_dt *dt;
2747 match m;
2749 where = gfc_current_locus;
2750 comma_flag = 0;
2751 current_dt = dt = gfc_getmem (sizeof (gfc_dt));
2752 m = gfc_match_char ('(');
2753 if (m == MATCH_NO)
2755 where = gfc_current_locus;
2756 if (k == M_WRITE)
2757 goto syntax;
2758 else if (k == M_PRINT)
2760 /* Treat the non-standard case of PRINT namelist. */
2761 if ((gfc_current_form == FORM_FIXED || gfc_peek_char () == ' ')
2762 && gfc_match_name (name) == MATCH_YES)
2764 gfc_find_symbol (name, NULL, 1, &sym);
2765 if (sym && sym->attr.flavor == FL_NAMELIST)
2767 if (gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
2768 "%C is an extension") == FAILURE)
2770 m = MATCH_ERROR;
2771 goto cleanup;
2774 dt->io_unit = default_unit (k);
2775 dt->namelist = sym;
2776 goto get_io_list;
2778 else
2779 gfc_current_locus = where;
2783 if (gfc_current_form == FORM_FREE)
2785 c = gfc_peek_char();
2786 if (c != ' ' && c != '*' && c != '\'' && c != '"')
2788 m = MATCH_NO;
2789 goto cleanup;
2793 m = match_dt_format (dt);
2794 if (m == MATCH_ERROR)
2795 goto cleanup;
2796 if (m == MATCH_NO)
2797 goto syntax;
2799 comma_flag = 1;
2800 dt->io_unit = default_unit (k);
2801 goto get_io_list;
2803 else
2805 /* Before issuing an error for a malformed 'print (1,*)' type of
2806 error, check for a default-char-expr of the form ('(I0)'). */
2808 if (k == M_PRINT && m == MATCH_YES)
2810 /* Reset current locus to get the initial '(' in an expression. */
2811 gfc_current_locus = where;
2812 dt->format_expr = NULL;
2813 m = match_dt_format (dt);
2815 if (m == MATCH_ERROR)
2816 goto cleanup;
2817 if (m == MATCH_NO || dt->format_expr == NULL)
2818 goto syntax;
2820 comma_flag = 1;
2821 dt->io_unit = default_unit (k);
2822 goto get_io_list;
2826 /* Match a control list */
2827 if (match_dt_element (k, dt) == MATCH_YES)
2828 goto next;
2829 if (match_dt_unit (k, dt) != MATCH_YES)
2830 goto loop;
2832 if (gfc_match_char (')') == MATCH_YES)
2833 goto get_io_list;
2834 if (gfc_match_char (',') != MATCH_YES)
2835 goto syntax;
2837 m = match_dt_element (k, dt);
2838 if (m == MATCH_YES)
2839 goto next;
2840 if (m == MATCH_ERROR)
2841 goto cleanup;
2843 m = match_dt_format (dt);
2844 if (m == MATCH_YES)
2845 goto next;
2846 if (m == MATCH_ERROR)
2847 goto cleanup;
2849 where = gfc_current_locus;
2851 m = gfc_match_name (name);
2852 if (m == MATCH_YES)
2854 gfc_find_symbol (name, NULL, 1, &sym);
2855 if (sym && sym->attr.flavor == FL_NAMELIST)
2857 dt->namelist = sym;
2858 if (k == M_READ && check_namelist (sym))
2860 m = MATCH_ERROR;
2861 goto cleanup;
2863 goto next;
2867 gfc_current_locus = where;
2869 goto loop; /* No matches, try regular elements */
2871 next:
2872 if (gfc_match_char (')') == MATCH_YES)
2873 goto get_io_list;
2874 if (gfc_match_char (',') != MATCH_YES)
2875 goto syntax;
2877 loop:
2878 for (;;)
2880 m = match_dt_element (k, dt);
2881 if (m == MATCH_NO)
2882 goto syntax;
2883 if (m == MATCH_ERROR)
2884 goto cleanup;
2886 if (gfc_match_char (')') == MATCH_YES)
2887 break;
2888 if (gfc_match_char (',') != MATCH_YES)
2889 goto syntax;
2892 get_io_list:
2894 /* Used in check_io_constraints, where no locus is available. */
2895 spec_end = gfc_current_locus;
2897 /* Optional leading comma (non-standard). */
2898 if (!comma_flag
2899 && gfc_match_char (',') == MATCH_YES
2900 && k == M_WRITE
2901 && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before output "
2902 "item list at %C is an extension") == FAILURE)
2903 return MATCH_ERROR;
2905 io_code = NULL;
2906 if (gfc_match_eos () != MATCH_YES)
2908 if (comma_flag && gfc_match_char (',') != MATCH_YES)
2910 gfc_error ("Expected comma in I/O list at %C");
2911 m = MATCH_ERROR;
2912 goto cleanup;
2915 m = match_io_list (k, &io_code);
2916 if (m == MATCH_ERROR)
2917 goto cleanup;
2918 if (m == MATCH_NO)
2919 goto syntax;
2922 /* A full IO statement has been matched. Check the constraints. spec_end is
2923 supplied for cases where no locus is supplied. */
2924 m = check_io_constraints (k, dt, io_code, &spec_end);
2926 if (m == MATCH_ERROR)
2927 goto cleanup;
2929 new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
2930 new_st.ext.dt = dt;
2931 new_st.block = gfc_get_code ();
2932 new_st.block->op = new_st.op;
2933 new_st.block->next = io_code;
2935 terminate_io (io_code);
2937 return MATCH_YES;
2939 syntax:
2940 gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2941 m = MATCH_ERROR;
2943 cleanup:
2944 gfc_free_dt (dt);
2945 return m;
2949 match
2950 gfc_match_read (void)
2952 return match_io (M_READ);
2955 match
2956 gfc_match_write (void)
2958 return match_io (M_WRITE);
2961 match
2962 gfc_match_print (void)
2964 match m;
2966 m = match_io (M_PRINT);
2967 if (m != MATCH_YES)
2968 return m;
2970 if (gfc_pure (NULL))
2972 gfc_error ("PRINT statement at %C not allowed within PURE procedure");
2973 return MATCH_ERROR;
2976 return MATCH_YES;
2980 /* Free a gfc_inquire structure. */
2982 void
2983 gfc_free_inquire (gfc_inquire * inquire)
2986 if (inquire == NULL)
2987 return;
2989 gfc_free_expr (inquire->unit);
2990 gfc_free_expr (inquire->file);
2991 gfc_free_expr (inquire->iomsg);
2992 gfc_free_expr (inquire->iostat);
2993 gfc_free_expr (inquire->exist);
2994 gfc_free_expr (inquire->opened);
2995 gfc_free_expr (inquire->number);
2996 gfc_free_expr (inquire->named);
2997 gfc_free_expr (inquire->name);
2998 gfc_free_expr (inquire->access);
2999 gfc_free_expr (inquire->sequential);
3000 gfc_free_expr (inquire->direct);
3001 gfc_free_expr (inquire->form);
3002 gfc_free_expr (inquire->formatted);
3003 gfc_free_expr (inquire->unformatted);
3004 gfc_free_expr (inquire->recl);
3005 gfc_free_expr (inquire->nextrec);
3006 gfc_free_expr (inquire->blank);
3007 gfc_free_expr (inquire->position);
3008 gfc_free_expr (inquire->action);
3009 gfc_free_expr (inquire->read);
3010 gfc_free_expr (inquire->write);
3011 gfc_free_expr (inquire->readwrite);
3012 gfc_free_expr (inquire->delim);
3013 gfc_free_expr (inquire->pad);
3014 gfc_free_expr (inquire->iolength);
3015 gfc_free_expr (inquire->convert);
3016 gfc_free_expr (inquire->strm_pos);
3018 gfc_free (inquire);
3022 /* Match an element of an INQUIRE statement. */
3024 #define RETM if (m != MATCH_NO) return m;
3026 static match
3027 match_inquire_element (gfc_inquire * inquire)
3029 match m;
3031 m = match_etag (&tag_unit, &inquire->unit);
3032 RETM m = match_etag (&tag_file, &inquire->file);
3033 RETM m = match_ltag (&tag_err, &inquire->err);
3034 RETM m = match_out_tag (&tag_iomsg, &inquire->iomsg);
3035 RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
3036 RETM m = match_vtag (&tag_exist, &inquire->exist);
3037 RETM m = match_vtag (&tag_opened, &inquire->opened);
3038 RETM m = match_vtag (&tag_named, &inquire->named);
3039 RETM m = match_vtag (&tag_name, &inquire->name);
3040 RETM m = match_out_tag (&tag_number, &inquire->number);
3041 RETM m = match_vtag (&tag_s_access, &inquire->access);
3042 RETM m = match_vtag (&tag_sequential, &inquire->sequential);
3043 RETM m = match_vtag (&tag_direct, &inquire->direct);
3044 RETM m = match_vtag (&tag_s_form, &inquire->form);
3045 RETM m = match_vtag (&tag_formatted, &inquire->formatted);
3046 RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
3047 RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
3048 RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
3049 RETM m = match_vtag (&tag_s_blank, &inquire->blank);
3050 RETM m = match_vtag (&tag_s_position, &inquire->position);
3051 RETM m = match_vtag (&tag_s_action, &inquire->action);
3052 RETM m = match_vtag (&tag_read, &inquire->read);
3053 RETM m = match_vtag (&tag_write, &inquire->write);
3054 RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
3055 RETM m = match_vtag (&tag_s_delim, &inquire->delim);
3056 RETM m = match_vtag (&tag_s_pad, &inquire->pad);
3057 RETM m = match_vtag (&tag_iolength, &inquire->iolength);
3058 RETM m = match_vtag (&tag_convert, &inquire->convert);
3059 RETM m = match_out_tag (&tag_strm_out, &inquire->strm_pos);
3060 RETM return MATCH_NO;
3063 #undef RETM
3066 match
3067 gfc_match_inquire (void)
3069 gfc_inquire *inquire;
3070 gfc_code *code;
3071 match m;
3072 locus loc;
3074 m = gfc_match_char ('(');
3075 if (m == MATCH_NO)
3076 return m;
3078 inquire = gfc_getmem (sizeof (gfc_inquire));
3080 loc = gfc_current_locus;
3082 m = match_inquire_element (inquire);
3083 if (m == MATCH_ERROR)
3084 goto cleanup;
3085 if (m == MATCH_NO)
3087 m = gfc_match_expr (&inquire->unit);
3088 if (m == MATCH_ERROR)
3089 goto cleanup;
3090 if (m == MATCH_NO)
3091 goto syntax;
3094 /* See if we have the IOLENGTH form of the inquire statement. */
3095 if (inquire->iolength != NULL)
3097 if (gfc_match_char (')') != MATCH_YES)
3098 goto syntax;
3100 m = match_io_list (M_INQUIRE, &code);
3101 if (m == MATCH_ERROR)
3102 goto cleanup;
3103 if (m == MATCH_NO)
3104 goto syntax;
3106 new_st.op = EXEC_IOLENGTH;
3107 new_st.expr = inquire->iolength;
3108 new_st.ext.inquire = inquire;
3110 if (gfc_pure (NULL))
3112 gfc_free_statements (code);
3113 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3114 return MATCH_ERROR;
3117 new_st.block = gfc_get_code ();
3118 new_st.block->op = EXEC_IOLENGTH;
3119 terminate_io (code);
3120 new_st.block->next = code;
3121 return MATCH_YES;
3124 /* At this point, we have the non-IOLENGTH inquire statement. */
3125 for (;;)
3127 if (gfc_match_char (')') == MATCH_YES)
3128 break;
3129 if (gfc_match_char (',') != MATCH_YES)
3130 goto syntax;
3132 m = match_inquire_element (inquire);
3133 if (m == MATCH_ERROR)
3134 goto cleanup;
3135 if (m == MATCH_NO)
3136 goto syntax;
3138 if (inquire->iolength != NULL)
3140 gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3141 goto cleanup;
3145 if (gfc_match_eos () != MATCH_YES)
3146 goto syntax;
3148 if (inquire->unit != NULL && inquire->file != NULL)
3150 gfc_error ("INQUIRE statement at %L cannot contain both FILE and"
3151 " UNIT specifiers", &loc);
3152 goto cleanup;
3155 if (inquire->unit == NULL && inquire->file == NULL)
3157 gfc_error ("INQUIRE statement at %L requires either FILE or"
3158 " UNIT specifier", &loc);
3159 goto cleanup;
3162 if (gfc_pure (NULL))
3164 gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3165 goto cleanup;
3168 new_st.op = EXEC_INQUIRE;
3169 new_st.ext.inquire = inquire;
3170 return MATCH_YES;
3172 syntax:
3173 gfc_syntax_error (ST_INQUIRE);
3175 cleanup:
3176 gfc_free_inquire (inquire);
3177 return MATCH_ERROR;
3181 /* Resolve everything in a gfc_inquire structure. */
3184 gfc_resolve_inquire (gfc_inquire * inquire)
3187 RESOLVE_TAG (&tag_unit, inquire->unit);
3188 RESOLVE_TAG (&tag_file, inquire->file);
3189 RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
3190 RESOLVE_TAG (&tag_iostat, inquire->iostat);
3191 RESOLVE_TAG (&tag_exist, inquire->exist);
3192 RESOLVE_TAG (&tag_opened, inquire->opened);
3193 RESOLVE_TAG (&tag_number, inquire->number);
3194 RESOLVE_TAG (&tag_named, inquire->named);
3195 RESOLVE_TAG (&tag_name, inquire->name);
3196 RESOLVE_TAG (&tag_s_access, inquire->access);
3197 RESOLVE_TAG (&tag_sequential, inquire->sequential);
3198 RESOLVE_TAG (&tag_direct, inquire->direct);
3199 RESOLVE_TAG (&tag_s_form, inquire->form);
3200 RESOLVE_TAG (&tag_formatted, inquire->formatted);
3201 RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
3202 RESOLVE_TAG (&tag_s_recl, inquire->recl);
3203 RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
3204 RESOLVE_TAG (&tag_s_blank, inquire->blank);
3205 RESOLVE_TAG (&tag_s_position, inquire->position);
3206 RESOLVE_TAG (&tag_s_action, inquire->action);
3207 RESOLVE_TAG (&tag_read, inquire->read);
3208 RESOLVE_TAG (&tag_write, inquire->write);
3209 RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
3210 RESOLVE_TAG (&tag_s_delim, inquire->delim);
3211 RESOLVE_TAG (&tag_s_pad, inquire->pad);
3212 RESOLVE_TAG (&tag_iolength, inquire->iolength);
3213 RESOLVE_TAG (&tag_convert, inquire->convert);
3214 RESOLVE_TAG (&tag_strm_out, inquire->strm_pos);
3216 if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)
3217 return FAILURE;
3219 return SUCCESS;