OSDN Git Service

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