OSDN Git Service

2007-07-03 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / fortran / io.c
1 /* Deal with I/O statements & related stuff.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3    Free Software Foundation, Inc.
4    Contributed by Andy Vaught
5
6 This file is part of GCC.
7
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.
12
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.
17
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.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "flags.h"
26 #include "gfortran.h"
27 #include "match.h"
28 #include "parse.h"
29
30 gfc_st_label
31 format_asterisk = {0, NULL, NULL, -1, ST_LABEL_FORMAT, ST_LABEL_FORMAT, NULL,
32                    0, {NULL, NULL}};
33
34 typedef struct
35 {
36   const char *name, *spec;
37   bt type;
38 }
39 io_tag;
40
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};
87
88 static gfc_dt *current_dt;
89
90 #define RESOLVE_TAG(x, y) if (resolve_tag(x, y) == FAILURE) return FAILURE;
91
92
93 /**************** Fortran 95 FORMAT parser  *****************/
94
95 /* FORMAT tokens returned by format_lex().  */
96 typedef enum
97 {
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
102 }
103 format_token;
104
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;
110
111 static format_token saved_token;
112
113 static enum
114 { MODE_STRING, MODE_FORMAT, MODE_COPY }
115 mode;
116
117
118 /* Return the next character in the format string.  */
119
120 static char
121 next_char (int in_string)
122 {
123   static char c;
124
125   if (use_last_char)
126     {
127       use_last_char = 0;
128       return c;
129     }
130
131   format_length++;
132
133   if (mode == MODE_STRING)
134     c = *format_string++;
135   else
136     {
137       c = gfc_next_char_literal (in_string);
138       if (c == '\n')
139         c = '\0';
140     }
141
142   if (gfc_option.flag_backslash && c == '\\')
143     {
144       int tmp;
145       locus old_locus = gfc_current_locus;
146
147       /* Use a temp variable to avoid side effects from gfc_match_special_char
148          since it uses an int * for its argument.  */
149       tmp = (int)c;
150
151       if (gfc_match_special_char (&tmp) == MATCH_NO)
152         gfc_current_locus = old_locus;
153
154       c = (char)tmp;
155
156       if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
157         gfc_warning ("Extension: backslash character at %C");
158     }
159
160   if (mode == MODE_COPY)
161     *format_string++ = c;
162
163   c = TOUPPER (c);
164   return c;
165 }
166
167
168 /* Back up one character position.  Only works once.  */
169
170 static void
171 unget_char (void)
172 {
173   use_last_char = 1;
174 }
175
176 /* Eat up the spaces and return a character.  */
177
178 static char
179 next_char_not_space (void)
180 {
181   char c;
182   do
183     {
184       c = next_char (0);
185     }
186   while (gfc_is_whitespace (c));
187   return c;
188 }
189
190 static int value = 0;
191
192 /* Simple lexical analyzer for getting the next token in a FORMAT
193    statement.  */
194
195 static format_token
196 format_lex (void)
197 {
198   format_token token;
199   char c, delim;
200   int zflag;
201   int negative_flag;
202
203   if (saved_token != FMT_NONE)
204     {
205       token = saved_token;
206       saved_token = FMT_NONE;
207       return token;
208     }
209
210   c = next_char_not_space ();
211   
212   negative_flag = 0;
213   switch (c)
214     {
215     case '-':
216       negative_flag = 1;
217     case '+':
218       c = next_char_not_space ();
219       if (!ISDIGIT (c))
220         {
221           token = FMT_UNKNOWN;
222           break;
223         }
224
225       value = c - '0';
226
227       do
228         {
229           c = next_char_not_space ();
230           if (ISDIGIT (c))
231             value = 10 * value + c - '0';
232         }
233       while (ISDIGIT (c));
234
235       unget_char ();
236
237       if (negative_flag)
238         value = -value;
239
240       token = FMT_SIGNED_INT;
241       break;
242
243     case '0':
244     case '1':
245     case '2':
246     case '3':
247     case '4':
248     case '5':
249     case '6':
250     case '7':
251     case '8':
252     case '9':
253       zflag = (c == '0');
254
255       value = c - '0';
256
257       do
258         {
259           c = next_char_not_space ();
260           if (ISDIGIT (c))
261             {
262               value = 10 * value + c - '0';
263               if (c != '0')
264                 zflag = 0;
265             }
266         }
267       while (ISDIGIT (c));
268
269       unget_char ();
270       token = zflag ? FMT_ZERO : FMT_POSINT;
271       break;
272
273     case '.':
274       token = FMT_PERIOD;
275       break;
276
277     case ',':
278       token = FMT_COMMA;
279       break;
280
281     case ':':
282       token = FMT_COLON;
283       break;
284
285     case '/':
286       token = FMT_SLASH;
287       break;
288
289     case '$':
290       token = FMT_DOLLAR;
291       break;
292
293     case 'T':
294       c = next_char_not_space ();
295       if (c != 'L' && c != 'R')
296         unget_char ();
297
298       token = FMT_POS;
299       break;
300
301     case '(':
302       token = FMT_LPAREN;
303       break;
304
305     case ')':
306       token = FMT_RPAREN;
307       break;
308
309     case 'X':
310       token = FMT_X;
311       break;
312
313     case 'S':
314       c = next_char_not_space ();
315       if (c != 'P' && c != 'S')
316         unget_char ();
317
318       token = FMT_SIGN;
319       break;
320
321     case 'B':
322       c = next_char_not_space ();
323       if (c == 'N' || c == 'Z')
324         token = FMT_BLANK;
325       else
326         {
327           unget_char ();
328           token = FMT_IBOZ;
329         }
330
331       break;
332
333     case '\'':
334     case '"':
335       delim = c;
336
337       value = 0;
338
339       for (;;)
340         {
341           c = next_char (1);
342           if (c == '\0')
343             {
344               token = FMT_END;
345               break;
346             }
347
348           if (c == delim)
349             {
350               c = next_char (1);
351
352               if (c == '\0')
353                 {
354                   token = FMT_END;
355                   break;
356                 }
357
358               if (c != delim)
359                 {
360                   unget_char ();
361                   token = FMT_CHAR;
362                   break;
363                 }
364             }
365           value++;
366         }
367       break;
368
369     case 'P':
370       token = FMT_P;
371       break;
372
373     case 'I':
374     case 'O':
375     case 'Z':
376       token = FMT_IBOZ;
377       break;
378
379     case 'F':
380       token = FMT_F;
381       break;
382
383     case 'E':
384       c = next_char_not_space ();
385       if (c == 'N' || c == 'S')
386         token = FMT_EXT;
387       else
388         {
389           token = FMT_E;
390           unget_char ();
391         }
392
393       break;
394
395     case 'G':
396       token = FMT_G;
397       break;
398
399     case 'H':
400       token = FMT_H;
401       break;
402
403     case 'L':
404       token = FMT_L;
405       break;
406
407     case 'A':
408       token = FMT_A;
409       break;
410
411     case 'D':
412       token = FMT_D;
413       break;
414
415     case '\0':
416       token = FMT_END;
417       break;
418
419     default:
420       token = FMT_UNKNOWN;
421       break;
422     }
423
424   return token;
425 }
426
427
428 /* Check a format statement.  The format string, either from a FORMAT
429    statement or a constant in an I/O statement has already been parsed
430    by itself, and we are checking it for validity.  The dual origin
431    means that the warning message is a little less than great.  */
432
433 static try
434 check_format (bool is_input)
435 {
436   const char *posint_required     = _("Positive width required");
437   const char *nonneg_required     = _("Nonnegative width required");
438   const char *unexpected_element  = _("Unexpected element");
439   const char *unexpected_end      = _("Unexpected end of format string");
440
441   const char *error;
442   format_token t, u;
443   int level;
444   int repeat;
445   try rv;
446
447   use_last_char = 0;
448   saved_token = FMT_NONE;
449   level = 0;
450   repeat = 0;
451   rv = SUCCESS;
452
453   t = format_lex ();
454   if (t != FMT_LPAREN)
455     {
456       error = _("Missing leading left parenthesis");
457       goto syntax;
458     }
459
460   t = format_lex ();
461   if (t == FMT_RPAREN)
462     goto finished;              /* Empty format is legal */
463   saved_token = t;
464
465 format_item:
466   /* In this state, the next thing has to be a format item.  */
467   t = format_lex ();
468 format_item_1:
469   switch (t)
470     {
471     case FMT_POSINT:
472       repeat = value;
473       t = format_lex ();
474       if (t == FMT_LPAREN)
475         {
476           level++;
477           goto format_item;
478         }
479
480       if (t == FMT_SLASH)
481         goto optional_comma;
482
483       goto data_desc;
484
485     case FMT_LPAREN:
486       level++;
487       goto format_item;
488
489     case FMT_SIGNED_INT:
490     case FMT_ZERO:
491       /* Signed integer can only precede a P format.  */
492       t = format_lex ();
493       if (t != FMT_P)
494         {
495           error = _("Expected P edit descriptor");
496           goto syntax;
497         }
498
499       goto data_desc;
500
501     case FMT_P:
502       /* P requires a prior number.  */
503       error = _("P descriptor requires leading scale factor");
504       goto syntax;
505
506     case FMT_X:
507       /* X requires a prior number if we're being pedantic.  */
508       if (gfc_notify_std (GFC_STD_GNU, "Extension: X descriptor "
509                           "requires leading space count at %C")
510           == FAILURE)
511         return FAILURE;
512       goto between_desc;
513
514     case FMT_SIGN:
515     case FMT_BLANK:
516       goto between_desc;
517
518     case FMT_CHAR:
519       goto extension_optional_comma;
520
521     case FMT_COLON:
522     case FMT_SLASH:
523       goto optional_comma;
524
525     case FMT_DOLLAR:
526       t = format_lex ();
527
528       if (gfc_notify_std (GFC_STD_GNU, "Extension: $ descriptor at %C")
529           == FAILURE)
530         return FAILURE;
531       if (t != FMT_RPAREN || level > 0)
532         {
533           gfc_warning ("$ should be the last specifier in format at %C");
534           goto optional_comma_1;
535         }
536
537       goto finished;
538
539     case FMT_POS:
540     case FMT_IBOZ:
541     case FMT_F:
542     case FMT_E:
543     case FMT_EXT:
544     case FMT_G:
545     case FMT_L:
546     case FMT_A:
547     case FMT_D:
548     case FMT_H:
549       goto data_desc;
550
551     case FMT_END:
552       error = unexpected_end;
553       goto syntax;
554
555     default:
556       error = unexpected_element;
557       goto syntax;
558     }
559
560 data_desc:
561   /* In this state, t must currently be a data descriptor.
562      Deal with things that can/must follow the descriptor.  */
563   switch (t)
564     {
565     case FMT_SIGN:
566     case FMT_BLANK:
567     case FMT_X:
568       break;
569
570     case FMT_P:
571       if (pedantic)
572         {
573           t = format_lex ();
574           if (t == FMT_POSINT)
575             {
576               error = _("Repeat count cannot follow P descriptor");
577               goto syntax;
578             }
579
580           saved_token = t;
581         }
582
583       goto optional_comma;
584
585     case FMT_POS:
586     case FMT_L:
587       t = format_lex ();
588       if (t == FMT_POSINT)
589         break;
590
591       switch (gfc_notification_std (GFC_STD_GNU))
592         {
593           case WARNING:
594             gfc_warning ("Extension: Missing positive width after L "
595                          "descriptor at %C");
596             saved_token = t;
597             break;
598
599           case ERROR:
600             error = posint_required;
601             goto syntax;
602
603           case SILENT:
604             saved_token = t;
605             break;
606
607           default:
608             gcc_unreachable ();
609         }
610       break;
611
612     case FMT_A:
613       t = format_lex ();
614       if (t != FMT_POSINT)
615         saved_token = t;
616       break;
617
618     case FMT_D:
619     case FMT_E:
620     case FMT_G:
621     case FMT_EXT:
622       u = format_lex ();
623       if (u != FMT_POSINT)
624         {
625           error = posint_required;
626           goto syntax;
627         }
628
629       u = format_lex ();
630       if (u != FMT_PERIOD)
631         {
632           /* Warn if -std=legacy, otherwise error.  */
633           if (gfc_option.warn_std != 0)
634             gfc_error_now ("Period required in format specifier at %C");
635           else
636             gfc_warning ("Period required in format specifier at %C");
637           saved_token = u;
638           break;
639         }
640
641       u = format_lex ();
642       if (u != FMT_ZERO && u != FMT_POSINT)
643         {
644           error = nonneg_required;
645           goto syntax;
646         }
647
648       if (t == FMT_D)
649         break;
650
651       /* Look for optional exponent.  */
652       u = format_lex ();
653       if (u != FMT_E)
654         {
655           saved_token = u;
656         }
657       else
658         {
659           u = format_lex ();
660           if (u != FMT_POSINT)
661             {
662               error = _("Positive exponent width required");
663               goto syntax;
664             }
665         }
666
667       break;
668
669     case FMT_F:
670       t = format_lex ();
671       if (t != FMT_ZERO && t != FMT_POSINT)
672         {
673           error = nonneg_required;
674           goto syntax;
675         }
676       else if (is_input && t == FMT_ZERO)
677         {
678           error = posint_required;
679           goto syntax;
680         }
681
682       t = format_lex ();
683       if (t != FMT_PERIOD)
684         {
685           /* Warn if -std=legacy, otherwise error.  */
686           if (gfc_option.warn_std != 0)
687             gfc_error_now ("Period required in format specifier at %C");
688           else
689             gfc_warning ("Period required in format specifier at %C");
690           saved_token = t;
691           break;
692         }
693
694       t = format_lex ();
695       if (t != FMT_ZERO && t != FMT_POSINT)
696         {
697           error = nonneg_required;
698           goto syntax;
699         }
700
701       break;
702
703     case FMT_H:
704       if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
705         gfc_warning ("The H format specifier at %C is"
706                      " a Fortran 95 deleted feature");
707
708       if(mode == MODE_STRING)
709         {
710           format_string += value;
711           format_length -= value;
712         }
713       else
714         {
715           while (repeat >0)
716            {
717              next_char (1);
718              repeat -- ;
719            }
720         }
721      break;
722
723     case FMT_IBOZ:
724       t = format_lex ();
725       if (t != FMT_ZERO && t != FMT_POSINT)
726         {
727           error = nonneg_required;
728           goto syntax;
729         }
730       else if (is_input && t == FMT_ZERO)
731         {
732           error = posint_required;
733           goto syntax;
734         }
735
736       t = format_lex ();
737       if (t != FMT_PERIOD)
738         {
739           saved_token = t;
740         }
741       else
742         {
743           t = format_lex ();
744           if (t != FMT_ZERO && t != FMT_POSINT)
745             {
746               error = nonneg_required;
747               goto syntax;
748             }
749         }
750
751       break;
752
753     default:
754       error = unexpected_element;
755       goto syntax;
756     }
757
758 between_desc:
759   /* Between a descriptor and what comes next.  */
760   t = format_lex ();
761   switch (t)
762     {
763
764     case FMT_COMMA:
765       goto format_item;
766
767     case FMT_RPAREN:
768       level--;
769       if (level < 0)
770         goto finished;
771       goto between_desc;
772
773     case FMT_COLON:
774     case FMT_SLASH:
775       goto optional_comma;
776
777     case FMT_END:
778       error = unexpected_end;
779       goto syntax;
780
781     default:
782       if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
783           == FAILURE)
784         return FAILURE;
785       goto format_item_1;
786     }
787
788 optional_comma:
789   /* Optional comma is a weird between state where we've just finished
790      reading a colon, slash, dollar or P descriptor.  */
791   t = format_lex ();
792 optional_comma_1:
793   switch (t)
794     {
795     case FMT_COMMA:
796       break;
797
798     case FMT_RPAREN:
799       level--;
800       if (level < 0)
801         goto finished;
802       goto between_desc;
803
804     default:
805       /* Assume that we have another format item.  */
806       saved_token = t;
807       break;
808     }
809
810   goto format_item;
811
812 extension_optional_comma:
813   /* As a GNU extension, permit a missing comma after a string literal.  */
814   t = format_lex ();
815   switch (t)
816     {
817     case FMT_COMMA:
818       break;
819
820     case FMT_RPAREN:
821       level--;
822       if (level < 0)
823         goto finished;
824       goto between_desc;
825
826     case FMT_COLON:
827     case FMT_SLASH:
828       goto optional_comma;
829
830     case FMT_END:
831       error = unexpected_end;
832       goto syntax;
833
834     default:
835       if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
836           == FAILURE)
837         return FAILURE;
838       saved_token = t;
839       break;
840     }
841
842   goto format_item;
843
844 syntax:
845   gfc_error ("%s in format string at %C", error);
846
847   /* TODO: More elaborate measures are needed to show where a problem
848      is within a format string that has been calculated.  */
849   rv = FAILURE;
850
851 finished:
852   return rv;
853 }
854
855
856 /* Given an expression node that is a constant string, see if it looks
857    like a format string.  */
858
859 static try
860 check_format_string (gfc_expr *e, bool is_input)
861 {
862   mode = MODE_STRING;
863   format_string = e->value.character.string;
864   return check_format (is_input);
865 }
866
867
868 /************ Fortran 95 I/O statement matchers *************/
869
870 /* Match a FORMAT statement.  This amounts to actually parsing the
871    format descriptors in order to correctly locate the end of the
872    format string.  */
873
874 match
875 gfc_match_format (void)
876 {
877   gfc_expr *e;
878   locus start;
879
880   if (gfc_current_ns->proc_name
881       && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
882     {
883       gfc_error ("Format statement in module main block at %C");
884       return MATCH_ERROR;
885     }
886
887   if (gfc_statement_label == NULL)
888     {
889       gfc_error ("Missing format label at %C");
890       return MATCH_ERROR;
891     }
892   gfc_gobble_whitespace ();
893
894   mode = MODE_FORMAT;
895   format_length = 0;
896
897   start = gfc_current_locus;
898
899   if (check_format (false) == FAILURE)
900     return MATCH_ERROR;
901
902   if (gfc_match_eos () != MATCH_YES)
903     {
904       gfc_syntax_error (ST_FORMAT);
905       return MATCH_ERROR;
906     }
907
908   /* The label doesn't get created until after the statement is done
909      being matched, so we have to leave the string for later.  */
910
911   gfc_current_locus = start;    /* Back to the beginning */
912
913   new_st.loc = start;
914   new_st.op = EXEC_NOP;
915
916   e = gfc_get_expr();
917   e->expr_type = EXPR_CONSTANT;
918   e->ts.type = BT_CHARACTER;
919   e->ts.kind = gfc_default_character_kind;
920   e->where = start;
921   e->value.character.string = format_string = gfc_getmem (format_length + 1);
922   e->value.character.length = format_length;
923   gfc_statement_label->format = e;
924
925   mode = MODE_COPY;
926   check_format (false);         /* Guaranteed to succeed */
927   gfc_match_eos ();             /* Guaranteed to succeed */
928
929   return MATCH_YES;
930 }
931
932
933 /* Match an expression I/O tag of some sort.  */
934
935 static match
936 match_etag (const io_tag *tag, gfc_expr **v)
937 {
938   gfc_expr *result;
939   match m;
940
941   m = gfc_match (tag->spec, &result);
942   if (m != MATCH_YES)
943     return m;
944
945   if (*v != NULL)
946     {
947       gfc_error ("Duplicate %s specification at %C", tag->name);
948       gfc_free_expr (result);
949       return MATCH_ERROR;
950     }
951
952   *v = result;
953   return MATCH_YES;
954 }
955
956
957 /* Match a variable I/O tag of some sort.  */
958
959 static match
960 match_vtag (const io_tag *tag, gfc_expr **v)
961 {
962   gfc_expr *result;
963   match m;
964
965   m = gfc_match (tag->spec, &result);
966   if (m != MATCH_YES)
967     return m;
968
969   if (*v != NULL)
970     {
971       gfc_error ("Duplicate %s specification at %C", tag->name);
972       gfc_free_expr (result);
973       return MATCH_ERROR;
974     }
975
976   if (result->symtree->n.sym->attr.intent == INTENT_IN)
977     {
978       gfc_error ("Variable tag cannot be INTENT(IN) at %C");
979       gfc_free_expr (result);
980       return MATCH_ERROR;
981     }
982
983   if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
984     {
985       gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
986       gfc_free_expr (result);
987       return MATCH_ERROR;
988     }
989
990   *v = result;
991   return MATCH_YES;
992 }
993
994
995 /* Match I/O tags that cause variables to become redefined.  */
996
997 static match
998 match_out_tag(const io_tag *tag, gfc_expr **result)
999 {
1000   match m;
1001
1002   m = match_vtag(tag, result);
1003   if (m == MATCH_YES)
1004     gfc_check_do_variable((*result)->symtree);
1005
1006   return m;
1007 }
1008
1009
1010 /* Match a label I/O tag.  */
1011
1012 static match
1013 match_ltag (const io_tag *tag, gfc_st_label ** label)
1014 {
1015   match m;
1016   gfc_st_label *old;
1017
1018   old = *label;
1019   m = gfc_match (tag->spec, label);
1020   if (m == MATCH_YES && old != 0)
1021     {
1022       gfc_error ("Duplicate %s label specification at %C", tag->name);
1023       return MATCH_ERROR;
1024     }
1025
1026   if (m == MATCH_YES 
1027       && gfc_reference_st_label (*label, ST_LABEL_TARGET) == FAILURE)
1028     return MATCH_ERROR;
1029
1030   return m;
1031 }
1032
1033
1034 /* Do expression resolution and type-checking on an expression tag.  */
1035
1036 static try
1037 resolve_tag (const io_tag *tag, gfc_expr *e)
1038 {
1039   if (e == NULL)
1040     return SUCCESS;
1041
1042   if (gfc_resolve_expr (e) == FAILURE)
1043     return FAILURE;
1044
1045   if (e->ts.type != tag->type && tag != &tag_format)
1046     {
1047       gfc_error ("%s tag at %L must be of type %s", tag->name,
1048                  &e->where, gfc_basic_typename (tag->type));
1049       return FAILURE;
1050     }
1051
1052   if (tag == &tag_format)
1053     {
1054       if (e->expr_type == EXPR_CONSTANT
1055           && (e->ts.type != BT_CHARACTER
1056               || e->ts.kind != gfc_default_character_kind))
1057         {
1058           gfc_error ("Constant expression in FORMAT tag at %L must be "
1059                      "of type default CHARACTER", &e->where);
1060           return FAILURE;
1061         }
1062
1063       /* If e's rank is zero and e is not an element of an array, it should be
1064          of integer or character type.  The integer variable should be
1065          ASSIGNED.  */
1066       if (e->symtree == NULL || e->symtree->n.sym->as == NULL
1067           || e->symtree->n.sym->as->rank == 0)
1068         {
1069           if (e->ts.type != BT_CHARACTER && e->ts.type != BT_INTEGER)
1070             {
1071               gfc_error ("%s tag at %L must be of type %s or %s", tag->name,
1072                          &e->where, gfc_basic_typename (BT_CHARACTER),
1073                          gfc_basic_typename (BT_INTEGER));
1074               return FAILURE;
1075             }
1076           else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
1077             {
1078               if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGNED "
1079                                   "variable in FORMAT tag at %L", &e->where)
1080                   == FAILURE)
1081                 return FAILURE;
1082               if (e->symtree->n.sym->attr.assign != 1)
1083                 {
1084                   gfc_error ("Variable '%s' at %L has not been assigned a "
1085                              "format label", e->symtree->n.sym->name,
1086                              &e->where);
1087                   return FAILURE;
1088                 }
1089             }
1090           else if (e->ts.type == BT_INTEGER)
1091             {
1092               gfc_error ("scalar '%s' FORMAT tag at %L is not an ASSIGNED "
1093                          "variable", gfc_basic_typename (e->ts.type),
1094                          &e->where);
1095               return FAILURE;
1096             }
1097
1098           return SUCCESS;
1099         }
1100       else
1101         {
1102           /* if rank is nonzero, we allow the type to be character under
1103              GFC_STD_GNU and other type under GFC_STD_LEGACY. It may be
1104              assigned an Hollerith constant.  */
1105           if (e->ts.type == BT_CHARACTER)
1106             {
1107               if (gfc_notify_std (GFC_STD_GNU, "Extension: Character array "
1108                                   "in FORMAT tag at %L", &e->where)
1109                   == FAILURE)
1110                 return FAILURE;
1111             }
1112           else
1113             {
1114               if (gfc_notify_std (GFC_STD_LEGACY, "Extension: Non-character "
1115                                   "in FORMAT tag at %L", &e->where)
1116                   == FAILURE)
1117                 return FAILURE;
1118             }
1119           return SUCCESS;
1120         }
1121     }
1122   else
1123     {
1124       if (e->rank != 0)
1125         {
1126           gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
1127           return FAILURE;
1128         }
1129
1130       if (tag == &tag_iomsg)
1131         {
1132           if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IOMSG tag at %L",
1133                               &e->where) == FAILURE)
1134             return FAILURE;
1135         }
1136
1137       if (tag == &tag_iostat && e->ts.kind != gfc_default_integer_kind)
1138         {
1139           if (gfc_notify_std (GFC_STD_GNU, "Fortran 95 requires default "
1140                               "INTEGER in IOSTAT tag at %L", &e->where)
1141               == FAILURE)
1142             return FAILURE;
1143         }
1144
1145       if (tag == &tag_size && e->ts.kind != gfc_default_integer_kind)
1146         {
1147           if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1148                               "INTEGER in SIZE tag at %L", &e->where)
1149               == FAILURE)
1150             return FAILURE;
1151         }
1152
1153       if (tag == &tag_convert)
1154         {
1155           if (gfc_notify_std (GFC_STD_GNU, "Extension: CONVERT tag at %L",
1156                               &e->where) == FAILURE)
1157             return FAILURE;
1158         }
1159     
1160       if (tag == &tag_iolength && e->ts.kind != gfc_default_integer_kind)
1161         {
1162           if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1163                               "INTEGER in IOLENGTH tag at %L", &e->where)
1164               == FAILURE)
1165             return FAILURE;
1166         }
1167     }
1168
1169   return SUCCESS;
1170 }
1171
1172
1173 /* Match a single tag of an OPEN statement.  */
1174
1175 static match
1176 match_open_element (gfc_open *open)
1177 {
1178   match m;
1179
1180   m = match_etag (&tag_unit, &open->unit);
1181   if (m != MATCH_NO)
1182     return m;
1183   m = match_out_tag (&tag_iomsg, &open->iomsg);
1184   if (m != MATCH_NO)
1185     return m;
1186   m = match_out_tag (&tag_iostat, &open->iostat);
1187   if (m != MATCH_NO)
1188     return m;
1189   m = match_etag (&tag_file, &open->file);
1190   if (m != MATCH_NO)
1191     return m;
1192   m = match_etag (&tag_status, &open->status);
1193   if (m != MATCH_NO)
1194     return m;
1195   m = match_etag (&tag_e_access, &open->access);
1196   if (m != MATCH_NO)
1197     return m;
1198   m = match_etag (&tag_e_form, &open->form);
1199   if (m != MATCH_NO)
1200     return m;
1201   m = match_etag (&tag_e_recl, &open->recl);
1202   if (m != MATCH_NO)
1203     return m;
1204   m = match_etag (&tag_e_blank, &open->blank);
1205   if (m != MATCH_NO)
1206     return m;
1207   m = match_etag (&tag_e_position, &open->position);
1208   if (m != MATCH_NO)
1209     return m;
1210   m = match_etag (&tag_e_action, &open->action);
1211   if (m != MATCH_NO)
1212     return m;
1213   m = match_etag (&tag_e_delim, &open->delim);
1214   if (m != MATCH_NO)
1215     return m;
1216   m = match_etag (&tag_e_pad, &open->pad);
1217   if (m != MATCH_NO)
1218     return m;
1219   m = match_ltag (&tag_err, &open->err);
1220   if (m != MATCH_NO)
1221     return m;
1222   m = match_etag (&tag_convert, &open->convert);
1223   if (m != MATCH_NO)
1224     return m;
1225
1226   return MATCH_NO;
1227 }
1228
1229
1230 /* Free the gfc_open structure and all the expressions it contains.  */
1231
1232 void
1233 gfc_free_open (gfc_open *open)
1234 {
1235   if (open == NULL)
1236     return;
1237
1238   gfc_free_expr (open->unit);
1239   gfc_free_expr (open->iomsg);
1240   gfc_free_expr (open->iostat);
1241   gfc_free_expr (open->file);
1242   gfc_free_expr (open->status);
1243   gfc_free_expr (open->access);
1244   gfc_free_expr (open->form);
1245   gfc_free_expr (open->recl);
1246   gfc_free_expr (open->blank);
1247   gfc_free_expr (open->position);
1248   gfc_free_expr (open->action);
1249   gfc_free_expr (open->delim);
1250   gfc_free_expr (open->pad);
1251   gfc_free_expr (open->convert);
1252   gfc_free (open);
1253 }
1254
1255
1256 /* Resolve everything in a gfc_open structure.  */
1257
1258 try
1259 gfc_resolve_open (gfc_open *open)
1260 {
1261
1262   RESOLVE_TAG (&tag_unit, open->unit);
1263   RESOLVE_TAG (&tag_iomsg, open->iomsg);
1264   RESOLVE_TAG (&tag_iostat, open->iostat);
1265   RESOLVE_TAG (&tag_file, open->file);
1266   RESOLVE_TAG (&tag_status, open->status);
1267   RESOLVE_TAG (&tag_e_access, open->access);
1268   RESOLVE_TAG (&tag_e_form, open->form);
1269   RESOLVE_TAG (&tag_e_recl, open->recl);
1270   RESOLVE_TAG (&tag_e_blank, open->blank);
1271   RESOLVE_TAG (&tag_e_position, open->position);
1272   RESOLVE_TAG (&tag_e_action, open->action);
1273   RESOLVE_TAG (&tag_e_delim, open->delim);
1274   RESOLVE_TAG (&tag_e_pad, open->pad);
1275   RESOLVE_TAG (&tag_convert, open->convert);
1276
1277   if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1278     return FAILURE;
1279
1280   return SUCCESS;
1281 }
1282
1283
1284 /* Check if a given value for a SPECIFIER is either in the list of values
1285    allowed in F95 or F2003, issuing an error message and returning a zero
1286    value if it is not allowed.  */
1287
1288 static int
1289 compare_to_allowed_values (const char *specifier, const char *allowed[],
1290                            const char *allowed_f2003[], 
1291                            const char *allowed_gnu[], char *value,
1292                            const char *statement, bool warn)
1293 {
1294   int i;
1295   unsigned int len;
1296
1297   len = strlen (value);
1298   if (len > 0)
1299   {
1300     for (len--; len > 0; len--)
1301       if (value[len] != ' ')
1302         break;
1303     len++;
1304   }
1305
1306   for (i = 0; allowed[i]; i++)
1307     if (len == strlen (allowed[i])
1308         && strncasecmp (value, allowed[i], strlen (allowed[i])) == 0)
1309       return 1;
1310
1311   for (i = 0; allowed_f2003 && allowed_f2003[i]; i++)
1312     if (len == strlen (allowed_f2003[i])
1313         && strncasecmp (value, allowed_f2003[i], strlen (allowed_f2003[i]))
1314            == 0)
1315       {
1316         notification n = gfc_notification_std (GFC_STD_F2003);
1317
1318         if (n == WARNING || (warn && n == ERROR))
1319           {
1320             gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1321                          "has value '%s'", specifier, statement,
1322                          allowed_f2003[i]);
1323             return 1;
1324           }
1325         else
1326           if (n == ERROR)
1327             {
1328               gfc_notify_std (GFC_STD_F2003, "Fortran 2003: %s specifier in "
1329                               "%s statement at %C has value '%s'", specifier,
1330                               statement, allowed_f2003[i]);
1331               return 0;
1332             }
1333
1334         /* n == SILENT */
1335         return 1;
1336       }
1337
1338   for (i = 0; allowed_gnu && allowed_gnu[i]; i++)
1339     if (len == strlen (allowed_gnu[i])
1340         && strncasecmp (value, allowed_gnu[i], strlen (allowed_gnu[i])) == 0)
1341       {
1342         notification n = gfc_notification_std (GFC_STD_GNU);
1343
1344         if (n == WARNING || (warn && n == ERROR))
1345           {
1346             gfc_warning ("Extension: %s specifier in %s statement at %C "
1347                          "has value '%s'", specifier, statement,
1348                          allowed_gnu[i]);
1349             return 1;
1350           }
1351         else
1352           if (n == ERROR)
1353             {
1354               gfc_notify_std (GFC_STD_GNU, "Extension: %s specifier in "
1355                               "%s statement at %C has value '%s'", specifier,
1356                               statement, allowed_gnu[i]);
1357               return 0;
1358             }
1359
1360         /* n == SILENT */
1361         return 1;
1362       }
1363
1364   if (warn)
1365     {
1366       gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1367                    specifier, statement, value);
1368       return 1;
1369     }
1370   else
1371     {
1372       gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1373                  specifier, statement, value);
1374       return 0;
1375     }
1376 }
1377
1378
1379 /* Match an OPEN statement.  */
1380
1381 match
1382 gfc_match_open (void)
1383 {
1384   gfc_open *open;
1385   match m;
1386   bool warn;
1387
1388   m = gfc_match_char ('(');
1389   if (m == MATCH_NO)
1390     return m;
1391
1392   open = gfc_getmem (sizeof (gfc_open));
1393
1394   m = match_open_element (open);
1395
1396   if (m == MATCH_ERROR)
1397     goto cleanup;
1398   if (m == MATCH_NO)
1399     {
1400       m = gfc_match_expr (&open->unit);
1401       if (m == MATCH_NO)
1402         goto syntax;
1403       if (m == MATCH_ERROR)
1404         goto cleanup;
1405     }
1406
1407   for (;;)
1408     {
1409       if (gfc_match_char (')') == MATCH_YES)
1410         break;
1411       if (gfc_match_char (',') != MATCH_YES)
1412         goto syntax;
1413
1414       m = match_open_element (open);
1415       if (m == MATCH_ERROR)
1416         goto cleanup;
1417       if (m == MATCH_NO)
1418         goto syntax;
1419     }
1420
1421   if (gfc_match_eos () == MATCH_NO)
1422     goto syntax;
1423
1424   if (gfc_pure (NULL))
1425     {
1426       gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1427       goto cleanup;
1428     }
1429
1430   warn = (open->err || open->iostat) ? true : false;
1431   /* Checks on the ACCESS specifier.  */
1432   if (open->access && open->access->expr_type == EXPR_CONSTANT)
1433     {
1434       static const char *access_f95[] = { "SEQUENTIAL", "DIRECT", NULL };
1435       static const char *access_f2003[] = { "STREAM", NULL };
1436       static const char *access_gnu[] = { "APPEND", NULL };
1437
1438       if (!compare_to_allowed_values ("ACCESS", access_f95, access_f2003,
1439                                       access_gnu,
1440                                       open->access->value.character.string,
1441                                       "OPEN", warn))
1442         goto cleanup;
1443     }
1444
1445   /* Checks on the ACTION specifier.  */
1446   if (open->action && open->action->expr_type == EXPR_CONSTANT)
1447     {
1448       static const char *action[] = { "READ", "WRITE", "READWRITE", NULL };
1449
1450       if (!compare_to_allowed_values ("ACTION", action, NULL, NULL,
1451                                       open->action->value.character.string,
1452                                       "OPEN", warn))
1453         goto cleanup;
1454     }
1455
1456   /* Checks on the ASYNCHRONOUS specifier.  */
1457   /* TODO: code is ready, just needs uncommenting when async I/O support
1458      is added ;-)
1459   if (open->asynchronous && open->asynchronous->expr_type == EXPR_CONSTANT)
1460     {
1461       static const char * asynchronous[] = { "YES", "NO", NULL };
1462
1463       if (!compare_to_allowed_values
1464                 ("action", asynchronous, NULL, NULL,
1465                  open->asynchronous->value.character.string, "OPEN", warn))
1466         goto cleanup;
1467     }*/
1468   
1469   /* Checks on the BLANK specifier.  */
1470   if (open->blank && open->blank->expr_type == EXPR_CONSTANT)
1471     {
1472       static const char *blank[] = { "ZERO", "NULL", NULL };
1473
1474       if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
1475                                       open->blank->value.character.string,
1476                                       "OPEN", warn))
1477         goto cleanup;
1478     }
1479
1480   /* Checks on the DECIMAL specifier.  */
1481   /* TODO: uncomment this code when DECIMAL support is added 
1482   if (open->decimal && open->decimal->expr_type == EXPR_CONSTANT)
1483     {
1484       static const char * decimal[] = { "COMMA", "POINT", NULL };
1485
1486       if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1487                                       open->decimal->value.character.string,
1488                                       "OPEN", warn))
1489         goto cleanup;
1490     } */
1491
1492   /* Checks on the DELIM specifier.  */
1493   if (open->delim && open->delim->expr_type == EXPR_CONSTANT)
1494     {
1495       static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
1496
1497       if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
1498                                       open->delim->value.character.string,
1499                                       "OPEN", warn))
1500         goto cleanup;
1501     }
1502
1503   /* Checks on the ENCODING specifier.  */
1504   /* TODO: uncomment this code when ENCODING support is added 
1505   if (open->encoding && open->encoding->expr_type == EXPR_CONSTANT)
1506     {
1507       static const char * encoding[] = { "UTF-8", "DEFAULT", NULL };
1508
1509       if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1510                                       open->encoding->value.character.string,
1511                                       "OPEN", warn))
1512         goto cleanup;
1513     } */
1514
1515   /* Checks on the FORM specifier.  */
1516   if (open->form && open->form->expr_type == EXPR_CONSTANT)
1517     {
1518       static const char *form[] = { "FORMATTED", "UNFORMATTED", NULL };
1519
1520       if (!compare_to_allowed_values ("FORM", form, NULL, NULL,
1521                                       open->form->value.character.string,
1522                                       "OPEN", warn))
1523         goto cleanup;
1524     }
1525
1526   /* Checks on the PAD specifier.  */
1527   if (open->pad && open->pad->expr_type == EXPR_CONSTANT)
1528     {
1529       static const char *pad[] = { "YES", "NO", NULL };
1530
1531       if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
1532                                       open->pad->value.character.string,
1533                                       "OPEN", warn))
1534         goto cleanup;
1535     }
1536
1537   /* Checks on the POSITION specifier.  */
1538   if (open->position && open->position->expr_type == EXPR_CONSTANT)
1539     {
1540       static const char *position[] = { "ASIS", "REWIND", "APPEND", NULL };
1541
1542       if (!compare_to_allowed_values ("POSITION", position, NULL, NULL,
1543                                       open->position->value.character.string,
1544                                       "OPEN", warn))
1545         goto cleanup;
1546     }
1547
1548   /* Checks on the ROUND specifier.  */
1549   /* TODO: uncomment this code when ROUND support is added 
1550   if (open->round && open->round->expr_type == EXPR_CONSTANT)
1551     {
1552       static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1553                                       "COMPATIBLE", "PROCESSOR_DEFINED", NULL };
1554
1555       if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1556                                       open->round->value.character.string,
1557                                       "OPEN", warn))
1558         goto cleanup;
1559     } */
1560
1561   /* Checks on the SIGN specifier.  */
1562   /* TODO: uncomment this code when SIGN support is added 
1563   if (open->sign && open->sign->expr_type == EXPR_CONSTANT)
1564     {
1565       static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1566                                      NULL };
1567
1568       if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1569                                       open->sign->value.character.string,
1570                                       "OPEN", warn))
1571         goto cleanup;
1572     } */
1573
1574 #define warn_or_error(...) \
1575 { \
1576   if (warn) \
1577     gfc_warning (__VA_ARGS__); \
1578   else \
1579     { \
1580       gfc_error (__VA_ARGS__); \
1581       goto cleanup; \
1582     } \
1583 }
1584
1585   /* Checks on the RECL specifier.  */
1586   if (open->recl && open->recl->expr_type == EXPR_CONSTANT
1587       && open->recl->ts.type == BT_INTEGER
1588       && mpz_sgn (open->recl->value.integer) != 1)
1589     {
1590       warn_or_error ("RECL in OPEN statement at %C must be positive");
1591     }
1592
1593   /* Checks on the STATUS specifier.  */
1594   if (open->status && open->status->expr_type == EXPR_CONSTANT)
1595     {
1596       static const char *status[] = { "OLD", "NEW", "SCRATCH",
1597         "REPLACE", "UNKNOWN", NULL };
1598
1599       if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1600                                       open->status->value.character.string,
1601                                       "OPEN", warn))
1602         goto cleanup;
1603
1604       /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1605          the FILE= specifier shall appear.  */
1606       if (open->file == NULL
1607           && (strncasecmp (open->status->value.character.string, "replace", 7)
1608               == 0
1609              || strncasecmp (open->status->value.character.string, "new", 3)
1610                 == 0))
1611         {
1612           warn_or_error ("The STATUS specified in OPEN statement at %C is "
1613                          "'%s' and no FILE specifier is present",
1614                          open->status->value.character.string);
1615         }
1616
1617       /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1618          the FILE= specifier shall not appear.  */
1619       if (strncasecmp (open->status->value.character.string, "scratch", 7)
1620           == 0 && open->file)
1621         {
1622           warn_or_error ("The STATUS specified in OPEN statement at %C "
1623                          "cannot have the value SCRATCH if a FILE specifier "
1624                          "is present");
1625         }
1626     }
1627
1628   /* Things that are not allowed for unformatted I/O.  */
1629   if (open->form && open->form->expr_type == EXPR_CONSTANT
1630       && (open->delim
1631           /* TODO uncomment this code when F2003 support is finished */
1632           /* || open->decimal || open->encoding || open->round
1633              || open->sign */
1634           || open->pad || open->blank)
1635       && strncasecmp (open->form->value.character.string,
1636                       "unformatted", 11) == 0)
1637     {
1638       const char *spec = (open->delim ? "DELIM "
1639                                       : (open->pad ? "PAD " : open->blank
1640                                                             ? "BLANK " : ""));
1641
1642       warn_or_error ("%s specifier at %C not allowed in OPEN statement for "
1643                      "unformatted I/O", spec);
1644     }
1645
1646   if (open->recl && open->access && open->access->expr_type == EXPR_CONSTANT
1647       && strncasecmp (open->access->value.character.string, "stream", 6) == 0)
1648     {
1649       warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
1650                      "stream I/O");
1651     }
1652
1653   if (open->position
1654       && open->access && open->access->expr_type == EXPR_CONSTANT
1655       && !(strncasecmp (open->access->value.character.string,
1656                         "sequential", 10) == 0
1657            || strncasecmp (open->access->value.character.string,
1658                            "stream", 6) == 0
1659            || strncasecmp (open->access->value.character.string,
1660                            "append", 6) == 0))
1661     {
1662       warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
1663                      "for stream or sequential ACCESS");
1664     }
1665
1666 #undef warn_or_error
1667
1668   new_st.op = EXEC_OPEN;
1669   new_st.ext.open = open;
1670   return MATCH_YES;
1671
1672 syntax:
1673   gfc_syntax_error (ST_OPEN);
1674
1675 cleanup:
1676   gfc_free_open (open);
1677   return MATCH_ERROR;
1678 }
1679
1680
1681 /* Free a gfc_close structure an all its expressions.  */
1682
1683 void
1684 gfc_free_close (gfc_close *close)
1685 {
1686   if (close == NULL)
1687     return;
1688
1689   gfc_free_expr (close->unit);
1690   gfc_free_expr (close->iomsg);
1691   gfc_free_expr (close->iostat);
1692   gfc_free_expr (close->status);
1693   gfc_free (close);
1694 }
1695
1696
1697 /* Match elements of a CLOSE statement.  */
1698
1699 static match
1700 match_close_element (gfc_close *close)
1701 {
1702   match m;
1703
1704   m = match_etag (&tag_unit, &close->unit);
1705   if (m != MATCH_NO)
1706     return m;
1707   m = match_etag (&tag_status, &close->status);
1708   if (m != MATCH_NO)
1709     return m;
1710   m = match_out_tag (&tag_iomsg, &close->iomsg);
1711   if (m != MATCH_NO)
1712     return m;
1713   m = match_out_tag (&tag_iostat, &close->iostat);
1714   if (m != MATCH_NO)
1715     return m;
1716   m = match_ltag (&tag_err, &close->err);
1717   if (m != MATCH_NO)
1718     return m;
1719
1720   return MATCH_NO;
1721 }
1722
1723
1724 /* Match a CLOSE statement.  */
1725
1726 match
1727 gfc_match_close (void)
1728 {
1729   gfc_close *close;
1730   match m;
1731   bool warn;
1732
1733   m = gfc_match_char ('(');
1734   if (m == MATCH_NO)
1735     return m;
1736
1737   close = gfc_getmem (sizeof (gfc_close));
1738
1739   m = match_close_element (close);
1740
1741   if (m == MATCH_ERROR)
1742     goto cleanup;
1743   if (m == MATCH_NO)
1744     {
1745       m = gfc_match_expr (&close->unit);
1746       if (m == MATCH_NO)
1747         goto syntax;
1748       if (m == MATCH_ERROR)
1749         goto cleanup;
1750     }
1751
1752   for (;;)
1753     {
1754       if (gfc_match_char (')') == MATCH_YES)
1755         break;
1756       if (gfc_match_char (',') != MATCH_YES)
1757         goto syntax;
1758
1759       m = match_close_element (close);
1760       if (m == MATCH_ERROR)
1761         goto cleanup;
1762       if (m == MATCH_NO)
1763         goto syntax;
1764     }
1765
1766   if (gfc_match_eos () == MATCH_NO)
1767     goto syntax;
1768
1769   if (gfc_pure (NULL))
1770     {
1771       gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1772       goto cleanup;
1773     }
1774
1775   warn = (close->iostat || close->err) ? true : false;
1776
1777   /* Checks on the STATUS specifier.  */
1778   if (close->status && close->status->expr_type == EXPR_CONSTANT)
1779     {
1780       static const char *status[] = { "KEEP", "DELETE", NULL };
1781
1782       if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1783                                       close->status->value.character.string,
1784                                       "CLOSE", warn))
1785         goto cleanup;
1786     }
1787
1788   new_st.op = EXEC_CLOSE;
1789   new_st.ext.close = close;
1790   return MATCH_YES;
1791
1792 syntax:
1793   gfc_syntax_error (ST_CLOSE);
1794
1795 cleanup:
1796   gfc_free_close (close);
1797   return MATCH_ERROR;
1798 }
1799
1800
1801 /* Resolve everything in a gfc_close structure.  */
1802
1803 try
1804 gfc_resolve_close (gfc_close *close)
1805 {
1806   RESOLVE_TAG (&tag_unit, close->unit);
1807   RESOLVE_TAG (&tag_iomsg, close->iomsg);
1808   RESOLVE_TAG (&tag_iostat, close->iostat);
1809   RESOLVE_TAG (&tag_status, close->status);
1810
1811   if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
1812     return FAILURE;
1813
1814   return SUCCESS;
1815 }
1816
1817
1818 /* Free a gfc_filepos structure.  */
1819
1820 void
1821 gfc_free_filepos (gfc_filepos *fp)
1822 {
1823   gfc_free_expr (fp->unit);
1824   gfc_free_expr (fp->iomsg);
1825   gfc_free_expr (fp->iostat);
1826   gfc_free (fp);
1827 }
1828
1829
1830 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement.  */
1831
1832 static match
1833 match_file_element (gfc_filepos *fp)
1834 {
1835   match m;
1836
1837   m = match_etag (&tag_unit, &fp->unit);
1838   if (m != MATCH_NO)
1839     return m;
1840   m = match_out_tag (&tag_iomsg, &fp->iomsg);
1841   if (m != MATCH_NO)
1842     return m;
1843   m = match_out_tag (&tag_iostat, &fp->iostat);
1844   if (m != MATCH_NO)
1845     return m;
1846   m = match_ltag (&tag_err, &fp->err);
1847   if (m != MATCH_NO)
1848     return m;
1849
1850   return MATCH_NO;
1851 }
1852
1853
1854 /* Match the second half of the file-positioning statements, REWIND,
1855    BACKSPACE, ENDFILE, or the FLUSH statement.  */
1856
1857 static match
1858 match_filepos (gfc_statement st, gfc_exec_op op)
1859 {
1860   gfc_filepos *fp;
1861   match m;
1862
1863   fp = gfc_getmem (sizeof (gfc_filepos));
1864
1865   if (gfc_match_char ('(') == MATCH_NO)
1866     {
1867       m = gfc_match_expr (&fp->unit);
1868       if (m == MATCH_ERROR)
1869         goto cleanup;
1870       if (m == MATCH_NO)
1871         goto syntax;
1872
1873       goto done;
1874     }
1875
1876   m = match_file_element (fp);
1877   if (m == MATCH_ERROR)
1878     goto done;
1879   if (m == MATCH_NO)
1880     {
1881       m = gfc_match_expr (&fp->unit);
1882       if (m == MATCH_ERROR)
1883         goto done;
1884       if (m == MATCH_NO)
1885         goto syntax;
1886     }
1887
1888   for (;;)
1889     {
1890       if (gfc_match_char (')') == MATCH_YES)
1891         break;
1892       if (gfc_match_char (',') != MATCH_YES)
1893         goto syntax;
1894
1895       m = match_file_element (fp);
1896       if (m == MATCH_ERROR)
1897         goto cleanup;
1898       if (m == MATCH_NO)
1899         goto syntax;
1900     }
1901
1902 done:
1903   if (gfc_match_eos () != MATCH_YES)
1904     goto syntax;
1905
1906   if (gfc_pure (NULL))
1907     {
1908       gfc_error ("%s statement not allowed in PURE procedure at %C",
1909                  gfc_ascii_statement (st));
1910
1911       goto cleanup;
1912     }
1913
1914   new_st.op = op;
1915   new_st.ext.filepos = fp;
1916   return MATCH_YES;
1917
1918 syntax:
1919   gfc_syntax_error (st);
1920
1921 cleanup:
1922   gfc_free_filepos (fp);
1923   return MATCH_ERROR;
1924 }
1925
1926
1927 try
1928 gfc_resolve_filepos (gfc_filepos *fp)
1929 {
1930   RESOLVE_TAG (&tag_unit, fp->unit);
1931   RESOLVE_TAG (&tag_iostat, fp->iostat);
1932   RESOLVE_TAG (&tag_iomsg, fp->iomsg);
1933   if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
1934     return FAILURE;
1935
1936   return SUCCESS;
1937 }
1938
1939
1940 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
1941    and the FLUSH statement.  */
1942
1943 match
1944 gfc_match_endfile (void)
1945 {
1946   return match_filepos (ST_END_FILE, EXEC_ENDFILE);
1947 }
1948
1949 match
1950 gfc_match_backspace (void)
1951 {
1952   return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
1953 }
1954
1955 match
1956 gfc_match_rewind (void)
1957 {
1958   return match_filepos (ST_REWIND, EXEC_REWIND);
1959 }
1960
1961 match
1962 gfc_match_flush (void)
1963 {
1964   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: FLUSH statement at %C")
1965       == FAILURE)
1966     return MATCH_ERROR;
1967
1968   return match_filepos (ST_FLUSH, EXEC_FLUSH);
1969 }
1970
1971 /******************** Data Transfer Statements *********************/
1972
1973 typedef enum
1974 { M_READ, M_WRITE, M_PRINT, M_INQUIRE }
1975 io_kind;
1976
1977
1978 /* Return a default unit number.  */
1979
1980 static gfc_expr *
1981 default_unit (io_kind k)
1982 {
1983   int unit;
1984
1985   if (k == M_READ)
1986     unit = 5;
1987   else
1988     unit = 6;
1989
1990   return gfc_int_expr (unit);
1991 }
1992
1993
1994 /* Match a unit specification for a data transfer statement.  */
1995
1996 static match
1997 match_dt_unit (io_kind k, gfc_dt *dt)
1998 {
1999   gfc_expr *e;
2000
2001   if (gfc_match_char ('*') == MATCH_YES)
2002     {
2003       if (dt->io_unit != NULL)
2004         goto conflict;
2005
2006       dt->io_unit = default_unit (k);
2007       return MATCH_YES;
2008     }
2009
2010   if (gfc_match_expr (&e) == MATCH_YES)
2011     {
2012       if (dt->io_unit != NULL)
2013         {
2014           gfc_free_expr (e);
2015           goto conflict;
2016         }
2017
2018       dt->io_unit = e;
2019       return MATCH_YES;
2020     }
2021
2022   return MATCH_NO;
2023
2024 conflict:
2025   gfc_error ("Duplicate UNIT specification at %C");
2026   return MATCH_ERROR;
2027 }
2028
2029
2030 /* Match a format specification.  */
2031
2032 static match
2033 match_dt_format (gfc_dt *dt)
2034 {
2035   locus where;
2036   gfc_expr *e;
2037   gfc_st_label *label;
2038
2039   where = gfc_current_locus;
2040
2041   if (gfc_match_char ('*') == MATCH_YES)
2042     {
2043       if (dt->format_expr != NULL || dt->format_label != NULL)
2044         goto conflict;
2045
2046       dt->format_label = &format_asterisk;
2047       return MATCH_YES;
2048     }
2049
2050   if (gfc_match_st_label (&label) == MATCH_YES)
2051     {
2052       if (dt->format_expr != NULL || dt->format_label != NULL)
2053         {
2054           gfc_free_st_label (label);
2055           goto conflict;
2056         }
2057
2058       if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
2059         return MATCH_ERROR;
2060
2061       dt->format_label = label;
2062       return MATCH_YES;
2063     }
2064
2065   if (gfc_match_expr (&e) == MATCH_YES)
2066     {
2067       if (dt->format_expr != NULL || dt->format_label != NULL)
2068         {
2069           gfc_free_expr (e);
2070           goto conflict;
2071         }
2072       dt->format_expr = e;
2073       return MATCH_YES;
2074     }
2075
2076   gfc_current_locus = where;    /* The only case where we have to restore */
2077
2078   return MATCH_NO;
2079
2080 conflict:
2081   gfc_error ("Duplicate format specification at %C");
2082   return MATCH_ERROR;
2083 }
2084
2085
2086 /* Traverse a namelist that is part of a READ statement to make sure
2087    that none of the variables in the namelist are INTENT(IN).  Returns
2088    nonzero if we find such a variable.  */
2089
2090 static int
2091 check_namelist (gfc_symbol *sym)
2092 {
2093   gfc_namelist *p;
2094
2095   for (p = sym->namelist; p; p = p->next)
2096     if (p->sym->attr.intent == INTENT_IN)
2097       {
2098         gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2099                    p->sym->name, sym->name);
2100         return 1;
2101       }
2102
2103   return 0;
2104 }
2105
2106
2107 /* Match a single data transfer element.  */
2108
2109 static match
2110 match_dt_element (io_kind k, gfc_dt *dt)
2111 {
2112   char name[GFC_MAX_SYMBOL_LEN + 1];
2113   gfc_symbol *sym;
2114   match m;
2115
2116   if (gfc_match (" unit =") == MATCH_YES)
2117     {
2118       m = match_dt_unit (k, dt);
2119       if (m != MATCH_NO)
2120         return m;
2121     }
2122
2123   if (gfc_match (" fmt =") == MATCH_YES)
2124     {
2125       m = match_dt_format (dt);
2126       if (m != MATCH_NO)
2127         return m;
2128     }
2129
2130   if (gfc_match (" nml = %n", name) == MATCH_YES)
2131     {
2132       if (dt->namelist != NULL)
2133         {
2134           gfc_error ("Duplicate NML specification at %C");
2135           return MATCH_ERROR;
2136         }
2137
2138       if (gfc_find_symbol (name, NULL, 1, &sym))
2139         return MATCH_ERROR;
2140
2141       if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
2142         {
2143           gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2144                      sym != NULL ? sym->name : name);
2145           return MATCH_ERROR;
2146         }
2147
2148       dt->namelist = sym;
2149       if (k == M_READ && check_namelist (sym))
2150         return MATCH_ERROR;
2151
2152       return MATCH_YES;
2153     }
2154
2155   m = match_etag (&tag_rec, &dt->rec);
2156   if (m != MATCH_NO)
2157     return m;
2158   m = match_etag (&tag_spos, &dt->rec);
2159   if (m != MATCH_NO)
2160     return m;
2161   m = match_out_tag (&tag_iomsg, &dt->iomsg);
2162   if (m != MATCH_NO)
2163     return m;
2164   m = match_out_tag (&tag_iostat, &dt->iostat);
2165   if (m != MATCH_NO)
2166     return m;
2167   m = match_ltag (&tag_err, &dt->err);
2168   if (m == MATCH_YES)
2169     dt->err_where = gfc_current_locus;
2170   if (m != MATCH_NO)
2171     return m;
2172   m = match_etag (&tag_advance, &dt->advance);
2173   if (m != MATCH_NO)
2174     return m;
2175   m = match_out_tag (&tag_size, &dt->size);
2176   if (m != MATCH_NO)
2177     return m;
2178
2179   m = match_ltag (&tag_end, &dt->end);
2180   if (m == MATCH_YES)
2181     {
2182       if (k == M_WRITE)
2183        {
2184          gfc_error ("END tag at %C not allowed in output statement");
2185          return MATCH_ERROR;
2186        }
2187       dt->end_where = gfc_current_locus;
2188     }
2189   if (m != MATCH_NO)
2190     return m;
2191
2192   m = match_ltag (&tag_eor, &dt->eor);
2193   if (m == MATCH_YES)
2194     dt->eor_where = gfc_current_locus;
2195   if (m != MATCH_NO)
2196     return m;
2197
2198   return MATCH_NO;
2199 }
2200
2201
2202 /* Free a data transfer structure and everything below it.  */
2203
2204 void
2205 gfc_free_dt (gfc_dt *dt)
2206 {
2207   if (dt == NULL)
2208     return;
2209
2210   gfc_free_expr (dt->io_unit);
2211   gfc_free_expr (dt->format_expr);
2212   gfc_free_expr (dt->rec);
2213   gfc_free_expr (dt->advance);
2214   gfc_free_expr (dt->iomsg);
2215   gfc_free_expr (dt->iostat);
2216   gfc_free_expr (dt->size);
2217   gfc_free (dt);
2218 }
2219
2220
2221 /* Resolve everything in a gfc_dt structure.  */
2222
2223 try
2224 gfc_resolve_dt (gfc_dt *dt)
2225 {
2226   gfc_expr *e;
2227
2228   RESOLVE_TAG (&tag_format, dt->format_expr);
2229   RESOLVE_TAG (&tag_rec, dt->rec);
2230   RESOLVE_TAG (&tag_spos, dt->rec);
2231   RESOLVE_TAG (&tag_advance, dt->advance);
2232   RESOLVE_TAG (&tag_iomsg, dt->iomsg);
2233   RESOLVE_TAG (&tag_iostat, dt->iostat);
2234   RESOLVE_TAG (&tag_size, dt->size);
2235
2236   e = dt->io_unit;
2237   if (gfc_resolve_expr (e) == SUCCESS
2238       && (e->ts.type != BT_INTEGER
2239           && (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_VARIABLE)))
2240     {
2241       gfc_error ("UNIT specification at %L must be an INTEGER expression "
2242                  "or a CHARACTER variable", &e->where);
2243       return FAILURE;
2244     }
2245
2246   if (e->ts.type == BT_CHARACTER)
2247     {
2248       if (gfc_has_vector_index (e))
2249         {
2250           gfc_error ("Internal unit with vector subscript at %L", &e->where);
2251           return FAILURE;
2252         }
2253     }
2254
2255   if (e->rank && e->ts.type != BT_CHARACTER)
2256     {
2257       gfc_error ("External IO UNIT cannot be an array at %L", &e->where);
2258       return FAILURE;
2259     }
2260
2261   if (dt->err)
2262     {
2263       if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
2264         return FAILURE;
2265       if (dt->err->defined == ST_LABEL_UNKNOWN)
2266         {
2267           gfc_error ("ERR tag label %d at %L not defined",
2268                       dt->err->value, &dt->err_where);
2269           return FAILURE;
2270         }
2271     }
2272
2273   if (dt->end)
2274     {
2275       if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
2276         return FAILURE;
2277       if (dt->end->defined == ST_LABEL_UNKNOWN)
2278         {
2279           gfc_error ("END tag label %d at %L not defined",
2280                       dt->end->value, &dt->end_where);
2281           return FAILURE;
2282         }
2283     }
2284
2285   if (dt->eor)
2286     {
2287       if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
2288         return FAILURE;
2289       if (dt->eor->defined == ST_LABEL_UNKNOWN)
2290         {
2291           gfc_error ("EOR tag label %d at %L not defined",
2292                       dt->eor->value, &dt->eor_where);
2293           return FAILURE;
2294         }
2295     }
2296
2297   /* Check the format label actually exists.  */
2298   if (dt->format_label && dt->format_label != &format_asterisk
2299       && dt->format_label->defined == ST_LABEL_UNKNOWN)
2300     {
2301       gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
2302                  &dt->format_label->where);
2303       return FAILURE;
2304     }
2305   return SUCCESS;
2306 }
2307
2308
2309 /* Given an io_kind, return its name.  */
2310
2311 static const char *
2312 io_kind_name (io_kind k)
2313 {
2314   const char *name;
2315
2316   switch (k)
2317     {
2318     case M_READ:
2319       name = "READ";
2320       break;
2321     case M_WRITE:
2322       name = "WRITE";
2323       break;
2324     case M_PRINT:
2325       name = "PRINT";
2326       break;
2327     case M_INQUIRE:
2328       name = "INQUIRE";
2329       break;
2330     default:
2331       gfc_internal_error ("io_kind_name(): bad I/O-kind");
2332     }
2333
2334   return name;
2335 }
2336
2337
2338 /* Match an IO iteration statement of the form:
2339
2340    ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2341
2342    which is equivalent to a single IO element.  This function is
2343    mutually recursive with match_io_element().  */
2344
2345 static match match_io_element (io_kind, gfc_code **);
2346
2347 static match
2348 match_io_iterator (io_kind k, gfc_code **result)
2349 {
2350   gfc_code *head, *tail, *new;
2351   gfc_iterator *iter;
2352   locus old_loc;
2353   match m;
2354   int n;
2355
2356   iter = NULL;
2357   head = NULL;
2358   old_loc = gfc_current_locus;
2359
2360   if (gfc_match_char ('(') != MATCH_YES)
2361     return MATCH_NO;
2362
2363   m = match_io_element (k, &head);
2364   tail = head;
2365
2366   if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
2367     {
2368       m = MATCH_NO;
2369       goto cleanup;
2370     }
2371
2372   /* Can't be anything but an IO iterator.  Build a list.  */
2373   iter = gfc_get_iterator ();
2374
2375   for (n = 1;; n++)
2376     {
2377       m = gfc_match_iterator (iter, 0);
2378       if (m == MATCH_ERROR)
2379         goto cleanup;
2380       if (m == MATCH_YES)
2381         {
2382           gfc_check_do_variable (iter->var->symtree);
2383           break;
2384         }
2385
2386       m = match_io_element (k, &new);
2387       if (m == MATCH_ERROR)
2388         goto cleanup;
2389       if (m == MATCH_NO)
2390         {
2391           if (n > 2)
2392             goto syntax;
2393           goto cleanup;
2394         }
2395
2396       tail = gfc_append_code (tail, new);
2397
2398       if (gfc_match_char (',') != MATCH_YES)
2399         {
2400           if (n > 2)
2401             goto syntax;
2402           m = MATCH_NO;
2403           goto cleanup;
2404         }
2405     }
2406
2407   if (gfc_match_char (')') != MATCH_YES)
2408     goto syntax;
2409
2410   new = gfc_get_code ();
2411   new->op = EXEC_DO;
2412   new->ext.iterator = iter;
2413
2414   new->block = gfc_get_code ();
2415   new->block->op = EXEC_DO;
2416   new->block->next = head;
2417
2418   *result = new;
2419   return MATCH_YES;
2420
2421 syntax:
2422   gfc_error ("Syntax error in I/O iterator at %C");
2423   m = MATCH_ERROR;
2424
2425 cleanup:
2426   gfc_free_iterator (iter, 1);
2427   gfc_free_statements (head);
2428   gfc_current_locus = old_loc;
2429   return m;
2430 }
2431
2432
2433 /* Match a single element of an IO list, which is either a single
2434    expression or an IO Iterator.  */
2435
2436 static match
2437 match_io_element (io_kind k, gfc_code **cpp)
2438 {
2439   gfc_expr *expr;
2440   gfc_code *cp;
2441   match m;
2442
2443   expr = NULL;
2444
2445   m = match_io_iterator (k, cpp);
2446   if (m == MATCH_YES)
2447     return MATCH_YES;
2448
2449   if (k == M_READ)
2450     {
2451       m = gfc_match_variable (&expr, 0);
2452       if (m == MATCH_NO)
2453         gfc_error ("Expected variable in READ statement at %C");
2454     }
2455   else
2456     {
2457       m = gfc_match_expr (&expr);
2458       if (m == MATCH_NO)
2459         gfc_error ("Expected expression in %s statement at %C",
2460                    io_kind_name (k));
2461     }
2462
2463   if (m == MATCH_YES)
2464     switch (k)
2465       {
2466       case M_READ:
2467         if (expr->symtree->n.sym->attr.intent == INTENT_IN)
2468           {
2469             gfc_error ("Variable '%s' in input list at %C cannot be "
2470                        "INTENT(IN)", expr->symtree->n.sym->name);
2471             m = MATCH_ERROR;
2472           }
2473
2474         if (gfc_pure (NULL)
2475             && gfc_impure_variable (expr->symtree->n.sym)
2476             && current_dt->io_unit->ts.type == BT_CHARACTER)
2477           {
2478             gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2479                        expr->symtree->n.sym->name);
2480             m = MATCH_ERROR;
2481           }
2482
2483         if (gfc_check_do_variable (expr->symtree))
2484           m = MATCH_ERROR;
2485
2486         break;
2487
2488       case M_WRITE:
2489         if (current_dt->io_unit->ts.type == BT_CHARACTER
2490             && gfc_pure (NULL)
2491             && current_dt->io_unit->expr_type == EXPR_VARIABLE
2492             && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
2493           {
2494             gfc_error ("Cannot write to internal file unit '%s' at %C "
2495                        "inside a PURE procedure",
2496                        current_dt->io_unit->symtree->n.sym->name);
2497             m = MATCH_ERROR;
2498           }
2499
2500         break;
2501
2502       default:
2503         break;
2504       }
2505
2506   if (m != MATCH_YES)
2507     {
2508       gfc_free_expr (expr);
2509       return MATCH_ERROR;
2510     }
2511
2512   cp = gfc_get_code ();
2513   cp->op = EXEC_TRANSFER;
2514   cp->expr = expr;
2515
2516   *cpp = cp;
2517   return MATCH_YES;
2518 }
2519
2520
2521 /* Match an I/O list, building gfc_code structures as we go.  */
2522
2523 static match
2524 match_io_list (io_kind k, gfc_code **head_p)
2525 {
2526   gfc_code *head, *tail, *new;
2527   match m;
2528
2529   *head_p = head = tail = NULL;
2530   if (gfc_match_eos () == MATCH_YES)
2531     return MATCH_YES;
2532
2533   for (;;)
2534     {
2535       m = match_io_element (k, &new);
2536       if (m == MATCH_ERROR)
2537         goto cleanup;
2538       if (m == MATCH_NO)
2539         goto syntax;
2540
2541       tail = gfc_append_code (tail, new);
2542       if (head == NULL)
2543         head = new;
2544
2545       if (gfc_match_eos () == MATCH_YES)
2546         break;
2547       if (gfc_match_char (',') != MATCH_YES)
2548         goto syntax;
2549     }
2550
2551   *head_p = head;
2552   return MATCH_YES;
2553
2554 syntax:
2555   gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2556
2557 cleanup:
2558   gfc_free_statements (head);
2559   return MATCH_ERROR;
2560 }
2561
2562
2563 /* Attach the data transfer end node.  */
2564
2565 static void
2566 terminate_io (gfc_code *io_code)
2567 {
2568   gfc_code *c;
2569
2570   if (io_code == NULL)
2571     io_code = new_st.block;
2572
2573   c = gfc_get_code ();
2574   c->op = EXEC_DT_END;
2575
2576   /* Point to structure that is already there */
2577   c->ext.dt = new_st.ext.dt;
2578   gfc_append_code (io_code, c);
2579 }
2580
2581
2582 /* Check the constraints for a data transfer statement.  The majority of the
2583    constraints appearing in 9.4 of the standard appear here.  Some are handled
2584    in resolve_tag and others in gfc_resolve_dt.  */
2585
2586 static match
2587 check_io_constraints (io_kind k, gfc_dt *dt, gfc_code *io_code,
2588                       locus *spec_end)
2589 {
2590 #define io_constraint(condition,msg,arg)\
2591 if (condition) \
2592   {\
2593     gfc_error(msg,arg);\
2594     m = MATCH_ERROR;\
2595   }
2596
2597   match m;
2598   gfc_expr *expr;
2599   gfc_symbol *sym = NULL;
2600
2601   m = MATCH_YES;
2602
2603   expr = dt->io_unit;
2604   if (expr && expr->expr_type == EXPR_VARIABLE
2605       && expr->ts.type == BT_CHARACTER)
2606     {
2607       sym = expr->symtree->n.sym;
2608
2609       io_constraint (k == M_WRITE && sym->attr.intent == INTENT_IN,
2610                      "Internal file at %L must not be INTENT(IN)",
2611                      &expr->where);
2612
2613       io_constraint (gfc_has_vector_index (dt->io_unit),
2614                      "Internal file incompatible with vector subscript at %L",
2615                      &expr->where);
2616
2617       io_constraint (dt->rec != NULL,
2618                      "REC tag at %L is incompatible with internal file",
2619                      &dt->rec->where);
2620
2621       if (dt->namelist != NULL)
2622         {
2623           if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Internal file "
2624                               "at %L with namelist", &expr->where)
2625               == FAILURE)
2626             m = MATCH_ERROR;
2627         }
2628
2629       io_constraint (dt->advance != NULL,
2630                      "ADVANCE tag at %L is incompatible with internal file",
2631                      &dt->advance->where);
2632     }
2633
2634   if (expr && expr->ts.type != BT_CHARACTER)
2635     {
2636
2637       io_constraint (gfc_pure (NULL) && (k == M_READ || k == M_WRITE),
2638                      "IO UNIT in %s statement at %C must be "
2639                      "an internal file in a PURE procedure",
2640                      io_kind_name (k));
2641     }
2642
2643
2644   if (k != M_READ)
2645     {
2646       io_constraint (dt->end, "END tag not allowed with output at %L",
2647                      &dt->end_where);
2648
2649       io_constraint (dt->eor, "EOR tag not allowed with output at %L",
2650                      &dt->eor_where);
2651
2652       io_constraint (k != M_READ && dt->size,
2653                      "SIZE=specifier not allowed with output at %L",
2654                      &dt->size->where);
2655     }
2656   else
2657     {
2658       io_constraint (dt->size && dt->advance == NULL,
2659                      "SIZE tag at %L requires an ADVANCE tag",
2660                      &dt->size->where);
2661
2662       io_constraint (dt->eor && dt->advance == NULL,
2663                      "EOR tag at %L requires an ADVANCE tag",
2664                      &dt->eor_where);
2665     }
2666
2667
2668
2669   if (dt->namelist)
2670     {
2671       io_constraint (io_code && dt->namelist,
2672                      "NAMELIST cannot be followed by IO-list at %L",
2673                      &io_code->loc);
2674
2675       io_constraint (dt->format_expr,
2676                      "IO spec-list cannot contain both NAMELIST group name "
2677                      "and format specification at %L.",
2678                      &dt->format_expr->where);
2679
2680       io_constraint (dt->format_label,
2681                      "IO spec-list cannot contain both NAMELIST group name "
2682                      "and format label at %L", spec_end);
2683
2684       io_constraint (dt->rec,
2685                      "NAMELIST IO is not allowed with a REC=specifier "
2686                      "at %L.", &dt->rec->where);
2687
2688       io_constraint (dt->advance,
2689                      "NAMELIST IO is not allowed with a ADVANCE=specifier "
2690                      "at %L.", &dt->advance->where);
2691     }
2692
2693   if (dt->rec)
2694     {
2695       io_constraint (dt->end,
2696                      "An END tag is not allowed with a "
2697                      "REC=specifier at %L.", &dt->end_where);
2698
2699
2700       io_constraint (dt->format_label == &format_asterisk,
2701                      "FMT=* is not allowed with a REC=specifier "
2702                      "at %L.", spec_end);
2703     }
2704
2705   if (dt->advance)
2706     {
2707       int not_yes, not_no;
2708       expr = dt->advance;
2709
2710       io_constraint (dt->format_label == &format_asterisk,
2711                      "List directed format(*) is not allowed with a "
2712                      "ADVANCE=specifier at %L.", &expr->where);
2713
2714       io_constraint (dt->format_expr == NULL && dt->format_label == NULL
2715                      && dt->namelist == NULL,
2716                      "the ADVANCE=specifier at %L must appear with an "
2717                      "explicit format expression", &expr->where);
2718
2719       if (expr->expr_type == EXPR_CONSTANT && expr->ts.type == BT_CHARACTER)
2720         {
2721           const char * advance = expr->value.character.string;
2722           not_no = strcasecmp (advance, "no") != 0;
2723           not_yes = strcasecmp (advance, "yes") != 0;
2724         }
2725       else
2726         {
2727           not_no = 0;
2728           not_yes = 0;
2729         }
2730
2731       io_constraint (not_no && not_yes,
2732                      "ADVANCE=specifier at %L must have value = "
2733                      "YES or NO.", &expr->where);
2734
2735       io_constraint (dt->size && not_no && k == M_READ,
2736                      "SIZE tag at %L requires an ADVANCE = 'NO'",
2737                      &dt->size->where);
2738
2739       io_constraint (dt->eor && not_no && k == M_READ,
2740                      "EOR tag at %L requires an ADVANCE = 'NO'",
2741                      &dt->eor_where);      
2742     }
2743
2744   expr = dt->format_expr;
2745   if (expr != NULL && expr->expr_type == EXPR_CONSTANT
2746       && check_format_string (expr, k == M_READ) == FAILURE)
2747     return MATCH_ERROR;
2748
2749   return m;
2750 }
2751 #undef io_constraint
2752
2753
2754 /* Match a READ, WRITE or PRINT statement.  */
2755
2756 static match
2757 match_io (io_kind k)
2758 {
2759   char name[GFC_MAX_SYMBOL_LEN + 1];
2760   gfc_code *io_code;
2761   gfc_symbol *sym;
2762   int comma_flag, c;
2763   locus where;
2764   locus spec_end;
2765   gfc_dt *dt;
2766   match m;
2767
2768   where = gfc_current_locus;
2769   comma_flag = 0;
2770   current_dt = dt = gfc_getmem (sizeof (gfc_dt));
2771   m = gfc_match_char ('(');
2772   if (m == MATCH_NO)
2773     {
2774       where = gfc_current_locus;
2775       if (k == M_WRITE)
2776         goto syntax;
2777       else if (k == M_PRINT)
2778         {
2779           /* Treat the non-standard case of PRINT namelist.  */
2780           if ((gfc_current_form == FORM_FIXED || gfc_peek_char () == ' ')
2781               && gfc_match_name (name) == MATCH_YES)
2782             {
2783               gfc_find_symbol (name, NULL, 1, &sym);
2784               if (sym && sym->attr.flavor == FL_NAMELIST)
2785                 {
2786                   if (gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
2787                                       "%C is an extension") == FAILURE)
2788                     {
2789                       m = MATCH_ERROR;
2790                       goto cleanup;
2791                     }
2792
2793                   dt->io_unit = default_unit (k);
2794                   dt->namelist = sym;
2795                   goto get_io_list;
2796                 }
2797               else
2798                 gfc_current_locus = where;
2799             }
2800         }
2801
2802       if (gfc_current_form == FORM_FREE)
2803         {
2804           c = gfc_peek_char();
2805           if (c != ' ' && c != '*' && c != '\'' && c != '"')
2806             {
2807               m = MATCH_NO;
2808               goto cleanup;
2809             }
2810         }
2811
2812       m = match_dt_format (dt);
2813       if (m == MATCH_ERROR)
2814         goto cleanup;
2815       if (m == MATCH_NO)
2816         goto syntax;
2817
2818       comma_flag = 1;
2819       dt->io_unit = default_unit (k);
2820       goto get_io_list;
2821     }
2822   else
2823     {
2824       /* Before issuing an error for a malformed 'print (1,*)' type of
2825          error, check for a default-char-expr of the form ('(I0)').  */
2826       if (k == M_PRINT && m == MATCH_YES)
2827         {
2828           /* Reset current locus to get the initial '(' in an expression.  */
2829           gfc_current_locus = where;
2830           dt->format_expr = NULL;
2831           m = match_dt_format (dt);
2832
2833           if (m == MATCH_ERROR)
2834             goto cleanup;
2835           if (m == MATCH_NO || dt->format_expr == NULL)
2836             goto syntax;
2837
2838           comma_flag = 1;
2839           dt->io_unit = default_unit (k);
2840           goto get_io_list;
2841         }
2842     }
2843
2844   /* Match a control list */
2845   if (match_dt_element (k, dt) == MATCH_YES)
2846     goto next;
2847   if (match_dt_unit (k, dt) != MATCH_YES)
2848     goto loop;
2849
2850   if (gfc_match_char (')') == MATCH_YES)
2851     goto get_io_list;
2852   if (gfc_match_char (',') != MATCH_YES)
2853     goto syntax;
2854
2855   m = match_dt_element (k, dt);
2856   if (m == MATCH_YES)
2857     goto next;
2858   if (m == MATCH_ERROR)
2859     goto cleanup;
2860
2861   m = match_dt_format (dt);
2862   if (m == MATCH_YES)
2863     goto next;
2864   if (m == MATCH_ERROR)
2865     goto cleanup;
2866
2867   where = gfc_current_locus;
2868
2869   m = gfc_match_name (name);
2870   if (m == MATCH_YES)
2871     {
2872       gfc_find_symbol (name, NULL, 1, &sym);
2873       if (sym && sym->attr.flavor == FL_NAMELIST)
2874         {
2875           dt->namelist = sym;
2876           if (k == M_READ && check_namelist (sym))
2877             {
2878               m = MATCH_ERROR;
2879               goto cleanup;
2880             }
2881           goto next;
2882         }
2883     }
2884
2885   gfc_current_locus = where;
2886
2887   goto loop;                    /* No matches, try regular elements */
2888
2889 next:
2890   if (gfc_match_char (')') == MATCH_YES)
2891     goto get_io_list;
2892   if (gfc_match_char (',') != MATCH_YES)
2893     goto syntax;
2894
2895 loop:
2896   for (;;)
2897     {
2898       m = match_dt_element (k, dt);
2899       if (m == MATCH_NO)
2900         goto syntax;
2901       if (m == MATCH_ERROR)
2902         goto cleanup;
2903
2904       if (gfc_match_char (')') == MATCH_YES)
2905         break;
2906       if (gfc_match_char (',') != MATCH_YES)
2907         goto syntax;
2908     }
2909
2910 get_io_list:
2911
2912   /* Used in check_io_constraints, where no locus is available.  */
2913   spec_end = gfc_current_locus;
2914
2915   /* Optional leading comma (non-standard).  */
2916   if (!comma_flag
2917       && gfc_match_char (',') == MATCH_YES
2918       && k == M_WRITE
2919       && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before output "
2920                          "item list at %C is an extension") == FAILURE)
2921     return MATCH_ERROR;
2922
2923   io_code = NULL;
2924   if (gfc_match_eos () != MATCH_YES)
2925     {
2926       if (comma_flag && gfc_match_char (',') != MATCH_YES)
2927         {
2928           gfc_error ("Expected comma in I/O list at %C");
2929           m = MATCH_ERROR;
2930           goto cleanup;
2931         }
2932
2933       m = match_io_list (k, &io_code);
2934       if (m == MATCH_ERROR)
2935         goto cleanup;
2936       if (m == MATCH_NO)
2937         goto syntax;
2938     }
2939
2940   /* A full IO statement has been matched.  Check the constraints.  spec_end is
2941      supplied for cases where no locus is supplied.  */
2942   m = check_io_constraints (k, dt, io_code, &spec_end);
2943
2944   if (m == MATCH_ERROR)
2945     goto cleanup;
2946
2947   new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
2948   new_st.ext.dt = dt;
2949   new_st.block = gfc_get_code ();
2950   new_st.block->op = new_st.op;
2951   new_st.block->next = io_code;
2952
2953   terminate_io (io_code);
2954
2955   return MATCH_YES;
2956
2957 syntax:
2958   gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2959   m = MATCH_ERROR;
2960
2961 cleanup:
2962   gfc_free_dt (dt);
2963   return m;
2964 }
2965
2966
2967 match
2968 gfc_match_read (void)
2969 {
2970   return match_io (M_READ);
2971 }
2972
2973 match
2974 gfc_match_write (void)
2975 {
2976   return match_io (M_WRITE);
2977 }
2978
2979 match
2980 gfc_match_print (void)
2981 {
2982   match m;
2983
2984   m = match_io (M_PRINT);
2985   if (m != MATCH_YES)
2986     return m;
2987
2988   if (gfc_pure (NULL))
2989     {
2990       gfc_error ("PRINT statement at %C not allowed within PURE procedure");
2991       return MATCH_ERROR;
2992     }
2993
2994   return MATCH_YES;
2995 }
2996
2997
2998 /* Free a gfc_inquire structure.  */
2999
3000 void
3001 gfc_free_inquire (gfc_inquire *inquire)
3002 {
3003
3004   if (inquire == NULL)
3005     return;
3006
3007   gfc_free_expr (inquire->unit);
3008   gfc_free_expr (inquire->file);
3009   gfc_free_expr (inquire->iomsg);
3010   gfc_free_expr (inquire->iostat);
3011   gfc_free_expr (inquire->exist);
3012   gfc_free_expr (inquire->opened);
3013   gfc_free_expr (inquire->number);
3014   gfc_free_expr (inquire->named);
3015   gfc_free_expr (inquire->name);
3016   gfc_free_expr (inquire->access);
3017   gfc_free_expr (inquire->sequential);
3018   gfc_free_expr (inquire->direct);
3019   gfc_free_expr (inquire->form);
3020   gfc_free_expr (inquire->formatted);
3021   gfc_free_expr (inquire->unformatted);
3022   gfc_free_expr (inquire->recl);
3023   gfc_free_expr (inquire->nextrec);
3024   gfc_free_expr (inquire->blank);
3025   gfc_free_expr (inquire->position);
3026   gfc_free_expr (inquire->action);
3027   gfc_free_expr (inquire->read);
3028   gfc_free_expr (inquire->write);
3029   gfc_free_expr (inquire->readwrite);
3030   gfc_free_expr (inquire->delim);
3031   gfc_free_expr (inquire->pad);
3032   gfc_free_expr (inquire->iolength);
3033   gfc_free_expr (inquire->convert);
3034   gfc_free_expr (inquire->strm_pos);
3035   gfc_free (inquire);
3036 }
3037
3038
3039 /* Match an element of an INQUIRE statement.  */
3040
3041 #define RETM   if (m != MATCH_NO) return m;
3042
3043 static match
3044 match_inquire_element (gfc_inquire *inquire)
3045 {
3046   match m;
3047
3048   m = match_etag (&tag_unit, &inquire->unit);
3049   RETM m = match_etag (&tag_file, &inquire->file);
3050   RETM m = match_ltag (&tag_err, &inquire->err);
3051   RETM m = match_out_tag (&tag_iomsg, &inquire->iomsg);
3052   RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
3053   RETM m = match_vtag (&tag_exist, &inquire->exist);
3054   RETM m = match_vtag (&tag_opened, &inquire->opened);
3055   RETM m = match_vtag (&tag_named, &inquire->named);
3056   RETM m = match_vtag (&tag_name, &inquire->name);
3057   RETM m = match_out_tag (&tag_number, &inquire->number);
3058   RETM m = match_vtag (&tag_s_access, &inquire->access);
3059   RETM m = match_vtag (&tag_sequential, &inquire->sequential);
3060   RETM m = match_vtag (&tag_direct, &inquire->direct);
3061   RETM m = match_vtag (&tag_s_form, &inquire->form);
3062   RETM m = match_vtag (&tag_formatted, &inquire->formatted);
3063   RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
3064   RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
3065   RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
3066   RETM m = match_vtag (&tag_s_blank, &inquire->blank);
3067   RETM m = match_vtag (&tag_s_position, &inquire->position);
3068   RETM m = match_vtag (&tag_s_action, &inquire->action);
3069   RETM m = match_vtag (&tag_read, &inquire->read);
3070   RETM m = match_vtag (&tag_write, &inquire->write);
3071   RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
3072   RETM m = match_vtag (&tag_s_delim, &inquire->delim);
3073   RETM m = match_vtag (&tag_s_pad, &inquire->pad);
3074   RETM m = match_vtag (&tag_iolength, &inquire->iolength);
3075   RETM m = match_vtag (&tag_convert, &inquire->convert);
3076   RETM m = match_out_tag (&tag_strm_out, &inquire->strm_pos);
3077   RETM return MATCH_NO;
3078 }
3079
3080 #undef RETM
3081
3082
3083 match
3084 gfc_match_inquire (void)
3085 {
3086   gfc_inquire *inquire;
3087   gfc_code *code;
3088   match m;
3089   locus loc;
3090
3091   m = gfc_match_char ('(');
3092   if (m == MATCH_NO)
3093     return m;
3094
3095   inquire = gfc_getmem (sizeof (gfc_inquire));
3096
3097   loc = gfc_current_locus;
3098
3099   m = match_inquire_element (inquire);
3100   if (m == MATCH_ERROR)
3101     goto cleanup;
3102   if (m == MATCH_NO)
3103     {
3104       m = gfc_match_expr (&inquire->unit);
3105       if (m == MATCH_ERROR)
3106         goto cleanup;
3107       if (m == MATCH_NO)
3108         goto syntax;
3109     }
3110
3111   /* See if we have the IOLENGTH form of the inquire statement.  */
3112   if (inquire->iolength != NULL)
3113     {
3114       if (gfc_match_char (')') != MATCH_YES)
3115         goto syntax;
3116
3117       m = match_io_list (M_INQUIRE, &code);
3118       if (m == MATCH_ERROR)
3119         goto cleanup;
3120       if (m == MATCH_NO)
3121         goto syntax;
3122
3123       new_st.op = EXEC_IOLENGTH;
3124       new_st.expr = inquire->iolength;
3125       new_st.ext.inquire = inquire;
3126
3127       if (gfc_pure (NULL))
3128         {
3129           gfc_free_statements (code);
3130           gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3131           return MATCH_ERROR;
3132         }
3133
3134       new_st.block = gfc_get_code ();
3135       new_st.block->op = EXEC_IOLENGTH;
3136       terminate_io (code);
3137       new_st.block->next = code;
3138       return MATCH_YES;
3139     }
3140
3141   /* At this point, we have the non-IOLENGTH inquire statement.  */
3142   for (;;)
3143     {
3144       if (gfc_match_char (')') == MATCH_YES)
3145         break;
3146       if (gfc_match_char (',') != MATCH_YES)
3147         goto syntax;
3148
3149       m = match_inquire_element (inquire);
3150       if (m == MATCH_ERROR)
3151         goto cleanup;
3152       if (m == MATCH_NO)
3153         goto syntax;
3154
3155       if (inquire->iolength != NULL)
3156         {
3157           gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3158           goto cleanup;
3159         }
3160     }
3161
3162   if (gfc_match_eos () != MATCH_YES)
3163     goto syntax;
3164
3165   if (inquire->unit != NULL && inquire->file != NULL)
3166     {
3167       gfc_error ("INQUIRE statement at %L cannot contain both FILE and "
3168                  "UNIT specifiers", &loc);
3169       goto cleanup;
3170     }
3171
3172   if (inquire->unit == NULL && inquire->file == NULL)
3173     {
3174       gfc_error ("INQUIRE statement at %L requires either FILE or "
3175                  "UNIT specifier", &loc);
3176       goto cleanup;
3177     }
3178
3179   if (gfc_pure (NULL))
3180     {
3181       gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3182       goto cleanup;
3183     }
3184
3185   new_st.op = EXEC_INQUIRE;
3186   new_st.ext.inquire = inquire;
3187   return MATCH_YES;
3188
3189 syntax:
3190   gfc_syntax_error (ST_INQUIRE);
3191
3192 cleanup:
3193   gfc_free_inquire (inquire);
3194   return MATCH_ERROR;
3195 }
3196
3197
3198 /* Resolve everything in a gfc_inquire structure.  */
3199
3200 try
3201 gfc_resolve_inquire (gfc_inquire *inquire)
3202 {
3203   RESOLVE_TAG (&tag_unit, inquire->unit);
3204   RESOLVE_TAG (&tag_file, inquire->file);
3205   RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
3206   RESOLVE_TAG (&tag_iostat, inquire->iostat);
3207   RESOLVE_TAG (&tag_exist, inquire->exist);
3208   RESOLVE_TAG (&tag_opened, inquire->opened);
3209   RESOLVE_TAG (&tag_number, inquire->number);
3210   RESOLVE_TAG (&tag_named, inquire->named);
3211   RESOLVE_TAG (&tag_name, inquire->name);
3212   RESOLVE_TAG (&tag_s_access, inquire->access);
3213   RESOLVE_TAG (&tag_sequential, inquire->sequential);
3214   RESOLVE_TAG (&tag_direct, inquire->direct);
3215   RESOLVE_TAG (&tag_s_form, inquire->form);
3216   RESOLVE_TAG (&tag_formatted, inquire->formatted);
3217   RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
3218   RESOLVE_TAG (&tag_s_recl, inquire->recl);
3219   RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
3220   RESOLVE_TAG (&tag_s_blank, inquire->blank);
3221   RESOLVE_TAG (&tag_s_position, inquire->position);
3222   RESOLVE_TAG (&tag_s_action, inquire->action);
3223   RESOLVE_TAG (&tag_read, inquire->read);
3224   RESOLVE_TAG (&tag_write, inquire->write);
3225   RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
3226   RESOLVE_TAG (&tag_s_delim, inquire->delim);
3227   RESOLVE_TAG (&tag_s_pad, inquire->pad);
3228   RESOLVE_TAG (&tag_iolength, inquire->iolength);
3229   RESOLVE_TAG (&tag_convert, inquire->convert);
3230   RESOLVE_TAG (&tag_strm_out, inquire->strm_pos);
3231
3232   if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)
3233     return FAILURE;
3234
3235   return SUCCESS;
3236 }