OSDN Git Service

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