OSDN Git Service

PR libfortran/20006
[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 Free Software Foundation,
3    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, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, 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 format_asterisk =
31   { -1, ST_LABEL_FORMAT, ST_LABEL_FORMAT, NULL, 0,
32     {NULL, NULL}, 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_format      = {"FORMAT", NULL, BT_CHARACTER},
56         tag_iostat      = {"IOSTAT", " iostat = %v", BT_INTEGER},
57         tag_size        = {"SIZE", " size = %v", BT_INTEGER},
58         tag_exist       = {"EXIST", " exist = %v", BT_LOGICAL},
59         tag_opened      = {"OPENED", " opened = %v", BT_LOGICAL},
60         tag_named       = {"NAMED", " named = %v", BT_LOGICAL},
61         tag_name        = {"NAME", " name = %v", BT_CHARACTER},
62         tag_number      = {"NUMBER", " number = %v", BT_INTEGER},
63         tag_s_access    = {"ACCESS", " access = %v", BT_CHARACTER},
64         tag_sequential  = {"SEQUENTIAL", " sequential = %v", BT_CHARACTER},
65         tag_direct      = {"DIRECT", " direct = %v", BT_CHARACTER},
66         tag_s_form      = {"FORM", " form = %v", BT_CHARACTER},
67         tag_formatted   = {"FORMATTED", " formatted = %v", BT_CHARACTER},
68         tag_unformatted = {"UNFORMATTED", " unformatted = %v", BT_CHARACTER},
69         tag_s_recl      = {"RECL", " recl = %v", BT_INTEGER},
70         tag_nextrec     = {"NEXTREC", " nextrec = %v", BT_INTEGER},
71         tag_s_blank     = {"BLANK", " blank = %v", BT_CHARACTER},
72         tag_s_position  = {"POSITION", " position = %v", BT_CHARACTER},
73         tag_s_action    = {"ACTION", " action = %v", BT_CHARACTER},
74         tag_read        = {"READ", " read = %v", BT_CHARACTER},
75         tag_write       = {"WRITE", " write = %v", BT_CHARACTER},
76         tag_readwrite   = {"READWRITE", " readwrite = %v", BT_CHARACTER},
77         tag_s_delim     = {"DELIM", " delim = %v", BT_CHARACTER},
78         tag_s_pad       = {"PAD", " pad = %v", BT_CHARACTER},
79         tag_iolength    = {"IOLENGTH", " iolength = %v", BT_INTEGER},
80         tag_err         = {"ERR", " err = %l", BT_UNKNOWN},
81         tag_end         = {"END", " end = %l", BT_UNKNOWN},
82         tag_eor         = {"EOR", " eor = %l", BT_UNKNOWN};
83
84 static gfc_dt *current_dt;
85
86 #define RESOLVE_TAG(x, y) if (resolve_tag(x, y) == FAILURE) return FAILURE;
87
88
89 /**************** Fortran 95 FORMAT parser  *****************/
90
91 /* FORMAT tokens returned by format_lex().  */
92 typedef enum
93 {
94   FMT_NONE, FMT_UNKNOWN, FMT_SIGNED_INT, FMT_ZERO, FMT_POSINT, FMT_PERIOD,
95   FMT_COMMA, FMT_COLON, FMT_SLASH, FMT_DOLLAR, FMT_POS, FMT_LPAREN,
96   FMT_RPAREN, FMT_X, FMT_SIGN, FMT_BLANK, FMT_CHAR, FMT_P, FMT_IBOZ, FMT_F,
97   FMT_E, FMT_EXT, FMT_G, FMT_L, FMT_A, FMT_D, FMT_H, FMT_END
98 }
99 format_token;
100
101 /* Local variables for checking format strings.  The saved_token is
102    used to back up by a single format token during the parsing
103    process.  */
104 static char *format_string;
105 static int format_length, use_last_char;
106
107 static format_token saved_token;
108
109 static enum
110 { MODE_STRING, MODE_FORMAT, MODE_COPY }
111 mode;
112
113
114 /* Return the next character in the format string.  */
115
116 static char
117 next_char (int in_string)
118 {
119   static char c;
120
121   if (use_last_char)
122     {
123       use_last_char = 0;
124       return c;
125     }
126
127   format_length++;
128
129   if (mode == MODE_STRING)
130     c = *format_string++;
131   else
132     {
133       c = gfc_next_char_literal (in_string);
134       if (c == '\n')
135         c = '\0';
136
137       if (mode == MODE_COPY)
138         *format_string++ = c;
139     }
140
141   c = TOUPPER (c);
142   return c;
143 }
144
145
146 /* Back up one character position.  Only works once.  */
147
148 static void
149 unget_char (void)
150 {
151
152   use_last_char = 1;
153 }
154
155 static int value = 0;
156
157 /* Simple lexical analyzer for getting the next token in a FORMAT
158    statement.  */
159
160 static format_token
161 format_lex (void)
162 {
163   format_token token;
164   char c, delim;
165   int zflag;
166   int negative_flag;
167
168   if (saved_token != FMT_NONE)
169     {
170       token = saved_token;
171       saved_token = FMT_NONE;
172       return token;
173     }
174
175   do
176     {
177       c = next_char (0);
178     }
179   while (gfc_is_whitespace (c));
180
181   negative_flag = 0;
182   switch (c)
183     {
184     case '-':
185       negative_flag = 1;
186     case '+':
187       c = next_char (0);
188       if (!ISDIGIT (c))
189         {
190           token = FMT_UNKNOWN;
191           break;
192         }
193
194       value = c - '0';
195
196       do
197         {
198           c = next_char (0);
199           if(ISDIGIT (c))
200             value = 10 * value + c - '0';
201         }
202       while (ISDIGIT (c));
203
204       unget_char ();
205
206       if (negative_flag)
207         value = -value;
208
209       token = FMT_SIGNED_INT;
210       break;
211
212     case '0':
213     case '1':
214     case '2':
215     case '3':
216     case '4':
217     case '5':
218     case '6':
219     case '7':
220     case '8':
221     case '9':
222       zflag = (c == '0');
223
224       value = c - '0';
225
226       do
227         {
228           c = next_char (0);
229           if (c != '0')
230             zflag = 0;
231           if (ISDIGIT (c))
232             value = 10 * value + c - '0';
233         }
234       while (ISDIGIT (c));
235
236       unget_char ();
237       token = zflag ? FMT_ZERO : FMT_POSINT;
238       break;
239
240     case '.':
241       token = FMT_PERIOD;
242       break;
243
244     case ',':
245       token = FMT_COMMA;
246       break;
247
248     case ':':
249       token = FMT_COLON;
250       break;
251
252     case '/':
253       token = FMT_SLASH;
254       break;
255
256     case '$':
257       token = FMT_DOLLAR;
258       break;
259
260     case 'T':
261       c = next_char (0);
262       if (c != 'L' && c != 'R')
263         unget_char ();
264
265       token = FMT_POS;
266       break;
267
268     case '(':
269       token = FMT_LPAREN;
270       break;
271
272     case ')':
273       token = FMT_RPAREN;
274       break;
275
276     case 'X':
277       token = FMT_X;
278       break;
279
280     case 'S':
281       c = next_char (0);
282       if (c != 'P' && c != 'S')
283         unget_char ();
284
285       token = FMT_SIGN;
286       break;
287
288     case 'B':
289       c = next_char (0);
290       if (c == 'N' || c == 'Z')
291         token = FMT_BLANK;
292       else
293         {
294           unget_char ();
295           token = FMT_IBOZ;
296         }
297
298       break;
299
300     case '\'':
301     case '"':
302       delim = c;
303
304       value = 0;
305
306       for (;;)
307         {
308           c = next_char (1);
309           if (c == '\0')
310             {
311               token = FMT_END;
312               break;
313             }
314
315           if (c == delim)
316             {
317               c = next_char (1);
318
319               if (c == '\0')
320                 {
321                   token = FMT_END;
322                   break;
323                 }
324
325               if (c != delim)
326                 {
327                   unget_char ();
328                   token = FMT_CHAR;
329                   break;
330                 }
331             }
332           value++;
333         }
334       break;
335
336     case 'P':
337       token = FMT_P;
338       break;
339
340     case 'I':
341     case 'O':
342     case 'Z':
343       token = FMT_IBOZ;
344       break;
345
346     case 'F':
347       token = FMT_F;
348       break;
349
350     case 'E':
351       c = next_char (0);
352       if (c == 'N' || c == 'S')
353         token = FMT_EXT;
354       else
355         {
356           token = FMT_E;
357           unget_char ();
358         }
359
360       break;
361
362     case 'G':
363       token = FMT_G;
364       break;
365
366     case 'H':
367       token = FMT_H;
368       break;
369
370     case 'L':
371       token = FMT_L;
372       break;
373
374     case 'A':
375       token = FMT_A;
376       break;
377
378     case 'D':
379       token = FMT_D;
380       break;
381
382     case '\0':
383       token = FMT_END;
384       break;
385
386     default:
387       token = FMT_UNKNOWN;
388       break;
389     }
390
391   return token;
392 }
393
394
395 /* Check a format statement.  The format string, either from a FORMAT
396    statement or a constant in an I/O statement has already been parsed
397    by itself, and we are checking it for validity.  The dual origin
398    means that the warning message is a little less than great.  */
399
400 static try
401 check_format (void)
402 {
403   const char *posint_required     = "Positive width required";
404   const char *period_required     = "Period required";
405   const char *nonneg_required     = "Nonnegative width required";
406   const char *unexpected_element  = "Unexpected element";
407   const char *unexpected_end      = "Unexpected end of format string";
408
409   const char *error;
410   format_token t, u;
411   int level;
412   int repeat;
413   try rv;
414
415   use_last_char = 0;
416   saved_token = FMT_NONE;
417   level = 0;
418   repeat = 0;
419   rv = SUCCESS;
420
421   t = format_lex ();
422   if (t != FMT_LPAREN)
423     {
424       error = "Missing leading left parenthesis";
425       goto syntax;
426     }
427
428   t = format_lex ();
429   if (t == FMT_RPAREN)
430     goto finished;              /* Empty format is legal */
431   saved_token = t;
432
433 format_item:
434   /* In this state, the next thing has to be a format item.  */
435   t = format_lex ();
436 format_item_1:
437   switch (t)
438     {
439     case FMT_POSINT:
440       repeat = value;
441       t = format_lex ();
442       if (t == FMT_LPAREN)
443         {
444           level++;
445           goto format_item;
446         }
447
448       if (t == FMT_SLASH)
449         goto optional_comma;
450
451       goto data_desc;
452
453     case FMT_LPAREN:
454       level++;
455       goto format_item;
456
457     case FMT_SIGNED_INT:
458       /* Signed integer can only precede a P format.  */
459       t = format_lex ();
460       if (t != FMT_P)
461         {
462           error = "Expected P edit descriptor";
463           goto syntax;
464         }
465
466       goto data_desc;
467
468     case FMT_P:
469       /* P requires a prior number.  */
470       error = "P descriptor requires leading scale factor";
471       goto syntax;
472
473     case FMT_X:
474       /* X requires a prior number if we're being pedantic.  */
475       if (gfc_notify_std (GFC_STD_GNU, "Extension: X descriptor "
476                           "requires leading space count at %C")
477           == FAILURE)
478         return FAILURE;
479       goto between_desc;
480
481     case FMT_SIGN:
482     case FMT_BLANK:
483       goto between_desc;
484
485     case FMT_CHAR:
486       goto extension_optional_comma;
487
488     case FMT_COLON:
489     case FMT_SLASH:
490       goto optional_comma;
491
492     case FMT_DOLLAR:
493       t = format_lex ();
494
495       if (gfc_notify_std (GFC_STD_GNU, "Extension: $ descriptor at %C")
496           == FAILURE)
497         return FAILURE;
498       if (t != FMT_RPAREN || level > 0)
499         {
500           error = "$ must be the last specifier";
501           goto syntax;
502         }
503
504       goto finished;
505
506     case FMT_POS:
507     case FMT_IBOZ:
508     case FMT_F:
509     case FMT_E:
510     case FMT_EXT:
511     case FMT_G:
512     case FMT_L:
513     case FMT_A:
514     case FMT_D:
515       goto data_desc;
516
517     case FMT_H:
518       goto data_desc;
519
520     case FMT_END:
521       error = unexpected_end;
522       goto syntax;
523
524     default:
525       error = unexpected_element;
526       goto syntax;
527     }
528
529 data_desc:
530   /* In this state, t must currently be a data descriptor.
531      Deal with things that can/must follow the descriptor.  */
532   switch (t)
533     {
534     case FMT_SIGN:
535     case FMT_BLANK:
536     case FMT_X:
537       break;
538
539     case FMT_P:
540       if (pedantic)
541         {
542           t = format_lex ();
543           if (t == FMT_POSINT)
544             {
545               error = "Repeat count cannot follow P descriptor";
546               goto syntax;
547             }
548
549           saved_token = t;
550         }
551
552       goto optional_comma;
553
554     case FMT_POS:
555     case FMT_L:
556       t = format_lex ();
557       if (t == FMT_POSINT)
558         break;
559
560       error = posint_required;
561       goto syntax;
562
563     case FMT_A:
564       t = format_lex ();
565       if (t != FMT_POSINT)
566         saved_token = t;
567       break;
568
569     case FMT_D:
570     case FMT_E:
571     case FMT_G:
572     case FMT_EXT:
573       u = format_lex ();
574       if (u != FMT_POSINT)
575         {
576           error = posint_required;
577           goto syntax;
578         }
579
580       u = format_lex ();
581       if (u != FMT_PERIOD)
582         {
583           error = period_required;
584           goto syntax;
585         }
586
587       u = format_lex ();
588       if (u != FMT_ZERO && u != FMT_POSINT)
589         {
590           error = nonneg_required;
591           goto syntax;
592         }
593
594       if (t == FMT_D)
595         break;
596
597       /* Look for optional exponent.  */
598       u = format_lex ();
599       if (u != FMT_E)
600         {
601           saved_token = u;
602         }
603       else
604         {
605           u = format_lex ();
606           if (u != FMT_POSINT)
607             {
608               error = "Positive exponent width required";
609               goto syntax;
610             }
611         }
612
613       break;
614
615     case FMT_F:
616       t = format_lex ();
617       if (t != FMT_ZERO && t != FMT_POSINT)
618         {
619           error = nonneg_required;
620           goto syntax;
621         }
622
623       t = format_lex ();
624       if (t != FMT_PERIOD)
625         {
626           error = period_required;
627           goto syntax;
628         }
629
630       t = format_lex ();
631       if (t != FMT_ZERO && t != FMT_POSINT)
632         {
633           error = nonneg_required;
634           goto syntax;
635         }
636
637       break;
638
639     case FMT_H:
640       if(mode == MODE_STRING)
641       {
642         format_string += value;
643         format_length -= value;
644       }
645       else
646       {
647         while(repeat >0)
648          {
649           next_char(1);
650           repeat -- ;
651          }
652       }
653      break;
654
655     case FMT_IBOZ:
656       t = format_lex ();
657       if (t != FMT_ZERO && t != FMT_POSINT)
658         {
659           error = nonneg_required;
660           goto syntax;
661         }
662
663       t = format_lex ();
664       if (t != FMT_PERIOD)
665         {
666           saved_token = t;
667         }
668       else
669         {
670           t = format_lex ();
671           if (t != FMT_ZERO && t != FMT_POSINT)
672             {
673               error = nonneg_required;
674               goto syntax;
675             }
676         }
677
678       break;
679
680     default:
681       error = unexpected_element;
682       goto syntax;
683     }
684
685 between_desc:
686   /* Between a descriptor and what comes next.  */
687   t = format_lex ();
688   switch (t)
689     {
690
691     case FMT_COMMA:
692       goto format_item;
693
694     case FMT_RPAREN:
695       level--;
696       if (level < 0)
697         goto finished;
698       goto between_desc;
699
700     case FMT_COLON:
701     case FMT_SLASH:
702       goto optional_comma;
703
704     case FMT_END:
705       error = unexpected_end;
706       goto syntax;
707
708     default:
709       if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
710           == FAILURE)
711         return FAILURE;
712       goto format_item_1;
713     }
714
715 optional_comma:
716   /* Optional comma is a weird between state where we've just finished
717      reading a colon, slash or P descriptor.  */
718   t = format_lex ();
719   switch (t)
720     {
721     case FMT_COMMA:
722       break;
723
724     case FMT_RPAREN:
725       level--;
726       if (level < 0)
727         goto finished;
728       goto between_desc;
729
730     default:
731       /* Assume that we have another format item.  */
732       saved_token = t;
733       break;
734     }
735
736   goto format_item;
737
738 extension_optional_comma:
739   /* As a GNU extension, permit a missing comma after a string literal.  */
740   t = format_lex ();
741   switch (t)
742     {
743     case FMT_COMMA:
744       break;
745
746     case FMT_RPAREN:
747       level--;
748       if (level < 0)
749         goto finished;
750       goto between_desc;
751
752     case FMT_COLON:
753     case FMT_SLASH:
754       goto optional_comma;
755
756     case FMT_END:
757       error = unexpected_end;
758       goto syntax;
759
760     default:
761       if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %C")
762           == FAILURE)
763         return FAILURE;
764       saved_token = t;
765       break;
766     }
767
768   goto format_item;
769
770 syntax:
771   /* Something went wrong.  If the format we're checking is a string,
772      generate a warning, since the program is correct.  If the format
773      is in a FORMAT statement, this messes up parsing, which is an
774      error.  */
775   if (mode != MODE_STRING)
776     gfc_error ("%s in format string at %C", error);
777   else
778     {
779       gfc_warning ("%s in format string at %C", error);
780
781       /* TODO: More elaborate measures are needed to show where a problem
782          is within a format string that has been calculated.  */
783     }
784
785   rv = FAILURE;
786
787 finished:
788   return rv;
789 }
790
791
792 /* Given an expression node that is a constant string, see if it looks
793    like a format string.  */
794
795 static void
796 check_format_string (gfc_expr * e)
797 {
798
799   mode = MODE_STRING;
800   format_string = e->value.character.string;
801   check_format ();
802 }
803
804
805 /************ Fortran 95 I/O statement matchers *************/
806
807 /* Match a FORMAT statement.  This amounts to actually parsing the
808    format descriptors in order to correctly locate the end of the
809    format string.  */
810
811 match
812 gfc_match_format (void)
813 {
814   gfc_expr *e;
815   locus start;
816
817   if (gfc_statement_label == NULL)
818     {
819       gfc_error ("Missing format label at %C");
820       return MATCH_ERROR;
821     }
822   gfc_gobble_whitespace ();
823
824   mode = MODE_FORMAT;
825   format_length = 0;
826
827   start = gfc_current_locus;
828
829   if (check_format () == FAILURE)
830     return MATCH_ERROR;
831
832   if (gfc_match_eos () != MATCH_YES)
833     {
834       gfc_syntax_error (ST_FORMAT);
835       return MATCH_ERROR;
836     }
837
838   /* The label doesn't get created until after the statement is done
839      being matched, so we have to leave the string for later.  */
840
841   gfc_current_locus = start;    /* Back to the beginning */
842
843   new_st.loc = start;
844   new_st.op = EXEC_NOP;
845
846   e = gfc_get_expr();
847   e->expr_type = EXPR_CONSTANT;
848   e->ts.type = BT_CHARACTER;
849   e->ts.kind = gfc_default_character_kind;
850   e->where = start;
851   e->value.character.string = format_string = gfc_getmem(format_length+1);
852   e->value.character.length = format_length;
853   gfc_statement_label->format = e;
854
855   mode = MODE_COPY;
856   check_format ();              /* Guaranteed to succeed */
857   gfc_match_eos ();             /* Guaranteed to succeed */
858
859   return MATCH_YES;
860 }
861
862
863 /* Match an expression I/O tag of some sort.  */
864
865 static match
866 match_etag (const io_tag * tag, gfc_expr ** v)
867 {
868   gfc_expr *result;
869   match m;
870
871   m = gfc_match (tag->spec, &result);
872   if (m != MATCH_YES)
873     return m;
874
875   if (*v != NULL)
876     {
877       gfc_error ("Duplicate %s specification at %C", tag->name);
878       gfc_free_expr (result);
879       return MATCH_ERROR;
880     }
881
882   *v = result;
883   return MATCH_YES;
884 }
885
886
887 /* Match a variable I/O tag of some sort.  */
888
889 static match
890 match_vtag (const io_tag * tag, gfc_expr ** v)
891 {
892   gfc_expr *result;
893   match m;
894
895   m = gfc_match (tag->spec, &result);
896   if (m != MATCH_YES)
897     return m;
898
899   if (*v != NULL)
900     {
901       gfc_error ("Duplicate %s specification at %C", tag->name);
902       gfc_free_expr (result);
903       return MATCH_ERROR;
904     }
905
906   if (result->symtree->n.sym->attr.intent == INTENT_IN)
907     {
908       gfc_error ("Variable tag cannot be INTENT(IN) at %C");
909       gfc_free_expr (result);
910       return MATCH_ERROR;
911     }
912
913   if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
914     {
915       gfc_error ("Variable tag cannot be assigned in PURE procedure at %C");
916       gfc_free_expr (result);
917       return MATCH_ERROR;
918     }
919
920   *v = result;
921   return MATCH_YES;
922 }
923
924
925 /* Match I/O tags that cause variables to become redefined.  */
926
927 static match
928 match_out_tag(const io_tag *tag, gfc_expr **result)
929 {
930   match m;
931
932   m = match_vtag(tag, result);
933   if (m == MATCH_YES)
934     gfc_check_do_variable((*result)->symtree);
935
936   return m;
937 }
938
939
940 /* Match a label I/O tag.  */
941
942 static match
943 match_ltag (const io_tag * tag, gfc_st_label ** label)
944 {
945   match m;
946   gfc_st_label *old;
947
948   old = *label;
949   m = gfc_match (tag->spec, label);
950   if (m == MATCH_YES && old != 0)
951     {
952       gfc_error ("Duplicate %s label specification at %C", tag->name);
953       return MATCH_ERROR;
954     }
955
956   return m;
957 }
958
959
960 /* Do expression resolution and type-checking on an expression tag.  */
961
962 static try
963 resolve_tag (const io_tag * tag, gfc_expr * e)
964 {
965
966   if (e == NULL)
967     return SUCCESS;
968
969   if (gfc_resolve_expr (e) == FAILURE)
970     return FAILURE;
971
972   if (e->ts.type != tag->type)
973     {
974       /* Format label can be integer varibale.  */
975       if (tag != &tag_format || e->ts.type != BT_INTEGER)
976         {
977           gfc_error ("%s tag at %L must be of type %s", tag->name, &e->where,
978                      gfc_basic_typename (tag->type));
979           return FAILURE;
980         }
981     }
982
983   if (tag == &tag_format)
984     {
985       if (e->rank != 1 && e->rank != 0)
986         {
987           gfc_error ("FORMAT tag at %L cannot be array of strings",
988                      &e->where);
989           return FAILURE;
990         }
991       /* Check assigned label.  */
992       if (e->expr_type == EXPR_VARIABLE && e->ts.type == BT_INTEGER
993                 && e->symtree->n.sym->attr.assign != 1)
994         {
995           gfc_error ("Variable '%s' has not been assigned a format label at %L",
996                         e->symtree->n.sym->name, &e->where);
997           return FAILURE;
998         }
999     }
1000   else
1001     {
1002       if (e->rank != 0)
1003         {
1004           gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
1005           return FAILURE;
1006         }
1007     }
1008
1009   return SUCCESS;
1010 }
1011
1012
1013 /* Match a single tag of an OPEN statement.  */
1014
1015 static match
1016 match_open_element (gfc_open * open)
1017 {
1018   match m;
1019
1020   m = match_etag (&tag_unit, &open->unit);
1021   if (m != MATCH_NO)
1022     return m;
1023   m = match_out_tag (&tag_iostat, &open->iostat);
1024   if (m != MATCH_NO)
1025     return m;
1026   m = match_etag (&tag_file, &open->file);
1027   if (m != MATCH_NO)
1028     return m;
1029   m = match_etag (&tag_status, &open->status);
1030   if (m != MATCH_NO)
1031     return m;
1032   m = match_etag (&tag_e_access, &open->access);
1033   if (m != MATCH_NO)
1034     return m;
1035   m = match_etag (&tag_e_form, &open->form);
1036   if (m != MATCH_NO)
1037     return m;
1038   m = match_etag (&tag_e_recl, &open->recl);
1039   if (m != MATCH_NO)
1040     return m;
1041   m = match_etag (&tag_e_blank, &open->blank);
1042   if (m != MATCH_NO)
1043     return m;
1044   m = match_etag (&tag_e_position, &open->position);
1045   if (m != MATCH_NO)
1046     return m;
1047   m = match_etag (&tag_e_action, &open->action);
1048   if (m != MATCH_NO)
1049     return m;
1050   m = match_etag (&tag_e_delim, &open->delim);
1051   if (m != MATCH_NO)
1052     return m;
1053   m = match_etag (&tag_e_pad, &open->pad);
1054   if (m != MATCH_NO)
1055     return m;
1056   m = match_ltag (&tag_err, &open->err);
1057   if (m != MATCH_NO)
1058     return m;
1059
1060   return MATCH_NO;
1061 }
1062
1063
1064 /* Free the gfc_open structure and all the expressions it contains.  */
1065
1066 void
1067 gfc_free_open (gfc_open * open)
1068 {
1069
1070   if (open == NULL)
1071     return;
1072
1073   gfc_free_expr (open->unit);
1074   gfc_free_expr (open->iostat);
1075   gfc_free_expr (open->file);
1076   gfc_free_expr (open->status);
1077   gfc_free_expr (open->access);
1078   gfc_free_expr (open->form);
1079   gfc_free_expr (open->recl);
1080   gfc_free_expr (open->blank);
1081   gfc_free_expr (open->position);
1082   gfc_free_expr (open->action);
1083   gfc_free_expr (open->delim);
1084   gfc_free_expr (open->pad);
1085
1086   gfc_free (open);
1087 }
1088
1089
1090 /* Resolve everything in a gfc_open structure.  */
1091
1092 try
1093 gfc_resolve_open (gfc_open * open)
1094 {
1095
1096   RESOLVE_TAG (&tag_unit, open->unit);
1097   RESOLVE_TAG (&tag_iostat, open->iostat);
1098   RESOLVE_TAG (&tag_file, open->file);
1099   RESOLVE_TAG (&tag_status, open->status);
1100   RESOLVE_TAG (&tag_e_form, open->form);
1101   RESOLVE_TAG (&tag_e_recl, open->recl);
1102
1103   RESOLVE_TAG (&tag_e_blank, open->blank);
1104   RESOLVE_TAG (&tag_e_position, open->position);
1105   RESOLVE_TAG (&tag_e_action, open->action);
1106   RESOLVE_TAG (&tag_e_delim, open->delim);
1107   RESOLVE_TAG (&tag_e_pad, open->pad);
1108
1109   if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1110     return FAILURE;
1111
1112   return SUCCESS;
1113 }
1114
1115
1116 /* Match an OPEN statement.  */
1117
1118 match
1119 gfc_match_open (void)
1120 {
1121   gfc_open *open;
1122   match m;
1123
1124   m = gfc_match_char ('(');
1125   if (m == MATCH_NO)
1126     return m;
1127
1128   open = gfc_getmem (sizeof (gfc_open));
1129
1130   m = match_open_element (open);
1131
1132   if (m == MATCH_ERROR)
1133     goto cleanup;
1134   if (m == MATCH_NO)
1135     {
1136       m = gfc_match_expr (&open->unit);
1137       if (m == MATCH_NO)
1138         goto syntax;
1139       if (m == MATCH_ERROR)
1140         goto cleanup;
1141     }
1142
1143   for (;;)
1144     {
1145       if (gfc_match_char (')') == MATCH_YES)
1146         break;
1147       if (gfc_match_char (',') != MATCH_YES)
1148         goto syntax;
1149
1150       m = match_open_element (open);
1151       if (m == MATCH_ERROR)
1152         goto cleanup;
1153       if (m == MATCH_NO)
1154         goto syntax;
1155     }
1156
1157   if (gfc_match_eos () == MATCH_NO)
1158     goto syntax;
1159
1160   if (gfc_pure (NULL))
1161     {
1162       gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1163       goto cleanup;
1164     }
1165
1166   new_st.op = EXEC_OPEN;
1167   new_st.ext.open = open;
1168   return MATCH_YES;
1169
1170 syntax:
1171   gfc_syntax_error (ST_OPEN);
1172
1173 cleanup:
1174   gfc_free_open (open);
1175   return MATCH_ERROR;
1176 }
1177
1178
1179 /* Free a gfc_close structure an all its expressions.  */
1180
1181 void
1182 gfc_free_close (gfc_close * close)
1183 {
1184
1185   if (close == NULL)
1186     return;
1187
1188   gfc_free_expr (close->unit);
1189   gfc_free_expr (close->iostat);
1190   gfc_free_expr (close->status);
1191
1192   gfc_free (close);
1193 }
1194
1195
1196 /* Match elements of a CLOSE statement.  */
1197
1198 static match
1199 match_close_element (gfc_close * close)
1200 {
1201   match m;
1202
1203   m = match_etag (&tag_unit, &close->unit);
1204   if (m != MATCH_NO)
1205     return m;
1206   m = match_etag (&tag_status, &close->status);
1207   if (m != MATCH_NO)
1208     return m;
1209   m = match_out_tag (&tag_iostat, &close->iostat);
1210   if (m != MATCH_NO)
1211     return m;
1212   m = match_ltag (&tag_err, &close->err);
1213   if (m != MATCH_NO)
1214     return m;
1215
1216   return MATCH_NO;
1217 }
1218
1219
1220 /* Match a CLOSE statement.  */
1221
1222 match
1223 gfc_match_close (void)
1224 {
1225   gfc_close *close;
1226   match m;
1227
1228   m = gfc_match_char ('(');
1229   if (m == MATCH_NO)
1230     return m;
1231
1232   close = gfc_getmem (sizeof (gfc_close));
1233
1234   m = match_close_element (close);
1235
1236   if (m == MATCH_ERROR)
1237     goto cleanup;
1238   if (m == MATCH_NO)
1239     {
1240       m = gfc_match_expr (&close->unit);
1241       if (m == MATCH_NO)
1242         goto syntax;
1243       if (m == MATCH_ERROR)
1244         goto cleanup;
1245     }
1246
1247   for (;;)
1248     {
1249       if (gfc_match_char (')') == MATCH_YES)
1250         break;
1251       if (gfc_match_char (',') != MATCH_YES)
1252         goto syntax;
1253
1254       m = match_close_element (close);
1255       if (m == MATCH_ERROR)
1256         goto cleanup;
1257       if (m == MATCH_NO)
1258         goto syntax;
1259     }
1260
1261   if (gfc_match_eos () == MATCH_NO)
1262     goto syntax;
1263
1264   if (gfc_pure (NULL))
1265     {
1266       gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
1267       goto cleanup;
1268     }
1269
1270   new_st.op = EXEC_CLOSE;
1271   new_st.ext.close = close;
1272   return MATCH_YES;
1273
1274 syntax:
1275   gfc_syntax_error (ST_CLOSE);
1276
1277 cleanup:
1278   gfc_free_close (close);
1279   return MATCH_ERROR;
1280 }
1281
1282
1283 /* Resolve everything in a gfc_close structure.  */
1284
1285 try
1286 gfc_resolve_close (gfc_close * close)
1287 {
1288
1289   RESOLVE_TAG (&tag_unit, close->unit);
1290   RESOLVE_TAG (&tag_iostat, close->iostat);
1291   RESOLVE_TAG (&tag_status, close->status);
1292
1293   if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
1294     return FAILURE;
1295
1296   return SUCCESS;
1297 }
1298
1299
1300 /* Free a gfc_filepos structure.  */
1301
1302 void
1303 gfc_free_filepos (gfc_filepos * fp)
1304 {
1305
1306   gfc_free_expr (fp->unit);
1307   gfc_free_expr (fp->iostat);
1308   gfc_free (fp);
1309 }
1310
1311
1312 /* Match elements of a REWIND, BACKSPACE or ENDFILE statement.  */
1313
1314 static match
1315 match_file_element (gfc_filepos * fp)
1316 {
1317   match m;
1318
1319   m = match_etag (&tag_unit, &fp->unit);
1320   if (m != MATCH_NO)
1321     return m;
1322   m = match_out_tag (&tag_iostat, &fp->iostat);
1323   if (m != MATCH_NO)
1324     return m;
1325   m = match_ltag (&tag_err, &fp->err);
1326   if (m != MATCH_NO)
1327     return m;
1328
1329   return MATCH_NO;
1330 }
1331
1332
1333 /* Match the second half of the file-positioning statements, REWIND,
1334    BACKSPACE or ENDFILE.  */
1335
1336 static match
1337 match_filepos (gfc_statement st, gfc_exec_op op)
1338 {
1339   gfc_filepos *fp;
1340   match m;
1341
1342   fp = gfc_getmem (sizeof (gfc_filepos));
1343
1344   if (gfc_match_char ('(') == MATCH_NO)
1345     {
1346       m = gfc_match_expr (&fp->unit);
1347       if (m == MATCH_ERROR)
1348         goto cleanup;
1349       if (m == MATCH_NO)
1350         goto syntax;
1351
1352       goto done;
1353     }
1354
1355   m = match_file_element (fp);
1356   if (m == MATCH_ERROR)
1357     goto done;
1358   if (m == MATCH_NO)
1359     {
1360       m = gfc_match_expr (&fp->unit);
1361       if (m == MATCH_ERROR)
1362         goto done;
1363       if (m == MATCH_NO)
1364         goto syntax;
1365     }
1366
1367   for (;;)
1368     {
1369       if (gfc_match_char (')') == MATCH_YES)
1370         break;
1371       if (gfc_match_char (',') != MATCH_YES)
1372         goto syntax;
1373
1374       m = match_file_element (fp);
1375       if (m == MATCH_ERROR)
1376         goto cleanup;
1377       if (m == MATCH_NO)
1378         goto syntax;
1379     }
1380
1381 done:
1382   if (gfc_match_eos () != MATCH_YES)
1383     goto syntax;
1384
1385   if (gfc_pure (NULL))
1386     {
1387       gfc_error ("%s statement not allowed in PURE procedure at %C",
1388                  gfc_ascii_statement (st));
1389
1390       goto cleanup;
1391     }
1392
1393   new_st.op = op;
1394   new_st.ext.filepos = fp;
1395   return MATCH_YES;
1396
1397 syntax:
1398   gfc_syntax_error (st);
1399
1400 cleanup:
1401   gfc_free_filepos (fp);
1402   return MATCH_ERROR;
1403 }
1404
1405
1406 try
1407 gfc_resolve_filepos (gfc_filepos * fp)
1408 {
1409
1410   RESOLVE_TAG (&tag_unit, fp->unit);
1411   if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
1412     return FAILURE;
1413
1414   return SUCCESS;
1415 }
1416
1417
1418 /* Match the file positioning statements: ENDFILE, BACKSPACE or
1419    REWIND.  */
1420
1421 match
1422 gfc_match_endfile (void)
1423 {
1424
1425   return match_filepos (ST_END_FILE, EXEC_ENDFILE);
1426 }
1427
1428 match
1429 gfc_match_backspace (void)
1430 {
1431
1432   return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
1433 }
1434
1435 match
1436 gfc_match_rewind (void)
1437 {
1438
1439   return match_filepos (ST_REWIND, EXEC_REWIND);
1440 }
1441
1442
1443 /******************** Data Transfer Statements *********************/
1444
1445 typedef enum
1446 { M_READ, M_WRITE, M_PRINT, M_INQUIRE }
1447 io_kind;
1448
1449
1450 /* Return a default unit number.  */
1451
1452 static gfc_expr *
1453 default_unit (io_kind k)
1454 {
1455   int unit;
1456
1457   if (k == M_READ)
1458     unit = 5;
1459   else
1460     unit = 6;
1461
1462   return gfc_int_expr (unit);
1463 }
1464
1465
1466 /* Match a unit specification for a data transfer statement.  */
1467
1468 static match
1469 match_dt_unit (io_kind k, gfc_dt * dt)
1470 {
1471   gfc_expr *e;
1472
1473   if (gfc_match_char ('*') == MATCH_YES)
1474     {
1475       if (dt->io_unit != NULL)
1476         goto conflict;
1477
1478       dt->io_unit = default_unit (k);
1479       return MATCH_YES;
1480     }
1481
1482   if (gfc_match_expr (&e) == MATCH_YES)
1483     {
1484       if (dt->io_unit != NULL)
1485         {
1486           gfc_free_expr (e);
1487           goto conflict;
1488         }
1489
1490       dt->io_unit = e;
1491       return MATCH_YES;
1492     }
1493
1494   return MATCH_NO;
1495
1496 conflict:
1497   gfc_error ("Duplicate UNIT specification at %C");
1498   return MATCH_ERROR;
1499 }
1500
1501
1502 /* Match a format specification.  */
1503
1504 static match
1505 match_dt_format (gfc_dt * dt)
1506 {
1507   locus where;
1508   gfc_expr *e;
1509   gfc_st_label *label;
1510
1511   where = gfc_current_locus;
1512
1513   if (gfc_match_char ('*') == MATCH_YES)
1514     {
1515       if (dt->format_expr != NULL || dt->format_label != NULL)
1516         goto conflict;
1517
1518       dt->format_label = &format_asterisk;
1519       return MATCH_YES;
1520     }
1521
1522   if (gfc_match_st_label (&label, 0) == MATCH_YES)
1523     {
1524       if (dt->format_expr != NULL || dt->format_label != NULL)
1525         {
1526           gfc_free_st_label (label);
1527           goto conflict;
1528         }
1529
1530       if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
1531         return MATCH_ERROR;
1532
1533       dt->format_label = label;
1534       return MATCH_YES;
1535     }
1536
1537   if (gfc_match_expr (&e) == MATCH_YES)
1538     {
1539       if (dt->format_expr != NULL || dt->format_label != NULL)
1540         {
1541           gfc_free_expr (e);
1542           goto conflict;
1543         }
1544       dt->format_expr = e;
1545       return MATCH_YES;
1546     }
1547
1548   gfc_current_locus = where;    /* The only case where we have to restore */
1549
1550   return MATCH_NO;
1551
1552 conflict:
1553   gfc_error ("Duplicate format specification at %C");
1554   return MATCH_ERROR;
1555 }
1556
1557
1558 /* Traverse a namelist that is part of a READ statement to make sure
1559    that none of the variables in the namelist are INTENT(IN).  Returns
1560    nonzero if we find such a variable.  */
1561
1562 static int
1563 check_namelist (gfc_symbol * sym)
1564 {
1565   gfc_namelist *p;
1566
1567   for (p = sym->namelist; p; p = p->next)
1568     if (p->sym->attr.intent == INTENT_IN)
1569       {
1570         gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
1571                    p->sym->name, sym->name);
1572         return 1;
1573       }
1574
1575   return 0;
1576 }
1577
1578
1579 /* Match a single data transfer element.  */
1580
1581 static match
1582 match_dt_element (io_kind k, gfc_dt * dt)
1583 {
1584   char name[GFC_MAX_SYMBOL_LEN + 1];
1585   gfc_symbol *sym;
1586   match m;
1587
1588   if (gfc_match (" unit =") == MATCH_YES)
1589     {
1590       m = match_dt_unit (k, dt);
1591       if (m != MATCH_NO)
1592         return m;
1593     }
1594
1595   if (gfc_match (" fmt =") == MATCH_YES)
1596     {
1597       m = match_dt_format (dt);
1598       if (m != MATCH_NO)
1599         return m;
1600     }
1601
1602   if (gfc_match (" nml = %n", name) == MATCH_YES)
1603     {
1604       if (dt->namelist != NULL)
1605         {
1606           gfc_error ("Duplicate NML specification at %C");
1607           return MATCH_ERROR;
1608         }
1609
1610       if (gfc_find_symbol (name, NULL, 1, &sym))
1611         return MATCH_ERROR;
1612
1613       if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
1614         {
1615           gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
1616                      sym != NULL ? sym->name : name);
1617           return MATCH_ERROR;
1618         }
1619
1620       dt->namelist = sym;
1621       if (k == M_READ && check_namelist (sym))
1622         return MATCH_ERROR;
1623
1624       return MATCH_YES;
1625     }
1626
1627   m = match_etag (&tag_rec, &dt->rec);
1628   if (m != MATCH_NO)
1629     return m;
1630   m = match_out_tag (&tag_iostat, &dt->iostat);
1631   if (m != MATCH_NO)
1632     return m;
1633   m = match_ltag (&tag_err, &dt->err);
1634   if (m != MATCH_NO)
1635     return m;
1636   m = match_etag (&tag_advance, &dt->advance);
1637   if (m != MATCH_NO)
1638     return m;
1639   m = match_out_tag (&tag_size, &dt->size);
1640   if (m != MATCH_NO)
1641     return m;
1642
1643   m = match_ltag (&tag_end, &dt->end);
1644   if (m == MATCH_YES)
1645     dt->end_where = gfc_current_locus;
1646   if (m != MATCH_NO)
1647     return m;
1648
1649   m = match_ltag (&tag_eor, &dt->eor);
1650   if (m == MATCH_YES)
1651     dt->eor_where = gfc_current_locus;
1652   if (m != MATCH_NO)
1653     return m;
1654
1655   return MATCH_NO;
1656 }
1657
1658
1659 /* Free a data transfer structure and everything below it.  */
1660
1661 void
1662 gfc_free_dt (gfc_dt * dt)
1663 {
1664
1665   if (dt == NULL)
1666     return;
1667
1668   gfc_free_expr (dt->io_unit);
1669   gfc_free_expr (dt->format_expr);
1670   gfc_free_expr (dt->rec);
1671   gfc_free_expr (dt->advance);
1672   gfc_free_expr (dt->iostat);
1673   gfc_free_expr (dt->size);
1674
1675   gfc_free (dt);
1676 }
1677
1678
1679 /* Resolve everything in a gfc_dt structure.  */
1680
1681 try
1682 gfc_resolve_dt (gfc_dt * dt)
1683 {
1684   gfc_expr *e;
1685
1686   RESOLVE_TAG (&tag_format, dt->format_expr);
1687   RESOLVE_TAG (&tag_rec, dt->rec);
1688   RESOLVE_TAG (&tag_advance, dt->advance);
1689   RESOLVE_TAG (&tag_iostat, dt->iostat);
1690   RESOLVE_TAG (&tag_size, dt->size);
1691
1692   e = dt->io_unit;
1693   if (gfc_resolve_expr (e) == SUCCESS
1694       && (e->ts.type != BT_INTEGER
1695           && (e->ts.type != BT_CHARACTER
1696               || e->expr_type != EXPR_VARIABLE)))
1697     {
1698       gfc_error
1699         ("UNIT specification at %L must be an INTEGER expression or a "
1700          "CHARACTER variable", &e->where);
1701       return FAILURE;
1702     }
1703
1704   /* Sanity checks on data transfer statements.  */
1705   if (e->ts.type == BT_CHARACTER)
1706     {
1707       if (dt->rec != NULL)
1708         {
1709           gfc_error ("REC tag at %L is incompatible with internal file",
1710                      &dt->rec->where);
1711           return FAILURE;
1712         }
1713
1714       if (dt->namelist != NULL)
1715         {
1716           gfc_error ("Internal file at %L is incompatible with namelist",
1717                      &dt->io_unit->where);
1718           return FAILURE;
1719         }
1720
1721       if (dt->advance != NULL)
1722         {
1723           gfc_error ("ADVANCE tag at %L is incompatible with internal file",
1724                      &dt->advance->where);
1725           return FAILURE;
1726         }
1727     }
1728
1729   if (dt->rec != NULL)
1730     {
1731       if (dt->end != NULL)
1732         {
1733           gfc_error ("REC tag at %L is incompatible with END tag",
1734                      &dt->rec->where);
1735           return FAILURE;
1736         }
1737
1738       if (dt->format_label == &format_asterisk)
1739         {
1740           gfc_error
1741             ("END tag at %L is incompatible with list directed format (*)",
1742              &dt->end_where);
1743           return FAILURE;
1744         }
1745
1746       if (dt->namelist != NULL)
1747         {
1748           gfc_error ("REC tag at %L is incompatible with namelist",
1749                      &dt->rec->where);
1750           return FAILURE;
1751         }
1752     }
1753
1754   if (dt->advance != NULL && dt->format_label == &format_asterisk)
1755     {
1756       gfc_error ("ADVANCE tag at %L is incompatible with list directed "
1757                  "format (*)", &dt->advance->where);
1758       return FAILURE;
1759     }
1760
1761   if (dt->eor != 0 && dt->advance == NULL)
1762     {
1763       gfc_error ("EOR tag at %L requires an ADVANCE tag", &dt->eor_where);
1764       return FAILURE;
1765     }
1766
1767   if (dt->size != NULL && dt->advance == NULL)
1768     {
1769       gfc_error ("SIZE tag at %L requires an ADVANCE tag", &dt->size->where);
1770       return FAILURE;
1771     }
1772
1773   /* TODO: Make sure the ADVANCE tag is 'yes' or 'no' if it is a string
1774      constant.  */
1775
1776   if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
1777     return FAILURE;
1778
1779   if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
1780     return FAILURE;
1781
1782   if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
1783     return FAILURE;
1784
1785   /* Check the format label actually exists.  */
1786   if (dt->format_label && dt->format_label != &format_asterisk
1787       && dt->format_label->defined == ST_LABEL_UNKNOWN)
1788     {
1789       gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
1790                  &dt->format_label->where);
1791       return FAILURE;
1792     }
1793   return SUCCESS;
1794 }
1795
1796
1797 /* Given an io_kind, return its name.  */
1798
1799 static const char *
1800 io_kind_name (io_kind k)
1801 {
1802   const char *name;
1803
1804   switch (k)
1805     {
1806     case M_READ:
1807       name = "READ";
1808       break;
1809     case M_WRITE:
1810       name = "WRITE";
1811       break;
1812     case M_PRINT:
1813       name = "PRINT";
1814       break;
1815     case M_INQUIRE:
1816       name = "INQUIRE";
1817       break;
1818     default:
1819       gfc_internal_error ("io_kind_name(): bad I/O-kind");
1820     }
1821
1822   return name;
1823 }
1824
1825
1826 /* Match an IO iteration statement of the form:
1827
1828    ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
1829
1830    which is equivalent to a single IO element.  This function is
1831    mutually recursive with match_io_element().  */
1832
1833 static match match_io_element (io_kind k, gfc_code **);
1834
1835 static match
1836 match_io_iterator (io_kind k, gfc_code ** result)
1837 {
1838   gfc_code *head, *tail, *new;
1839   gfc_iterator *iter;
1840   locus old_loc;
1841   match m;
1842   int n;
1843
1844   iter = NULL;
1845   head = NULL;
1846   old_loc = gfc_current_locus;
1847
1848   if (gfc_match_char ('(') != MATCH_YES)
1849     return MATCH_NO;
1850
1851   m = match_io_element (k, &head);
1852   tail = head;
1853
1854   if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
1855     {
1856       m = MATCH_NO;
1857       goto cleanup;
1858     }
1859
1860   /* Can't be anything but an IO iterator.  Build a list.  */
1861   iter = gfc_get_iterator ();
1862
1863   for (n = 1;; n++)
1864     {
1865       m = gfc_match_iterator (iter, 0);
1866       if (m == MATCH_ERROR)
1867         goto cleanup;
1868       if (m == MATCH_YES)
1869         {
1870           gfc_check_do_variable (iter->var->symtree);
1871           break;
1872         }
1873
1874       m = match_io_element (k, &new);
1875       if (m == MATCH_ERROR)
1876         goto cleanup;
1877       if (m == MATCH_NO)
1878         {
1879           if (n > 2)
1880             goto syntax;
1881           goto cleanup;
1882         }
1883
1884       tail = gfc_append_code (tail, new);
1885
1886       if (gfc_match_char (',') != MATCH_YES)
1887         {
1888           if (n > 2)
1889             goto syntax;
1890           m = MATCH_NO;
1891           goto cleanup;
1892         }
1893     }
1894
1895   if (gfc_match_char (')') != MATCH_YES)
1896     goto syntax;
1897
1898   new = gfc_get_code ();
1899   new->op = EXEC_DO;
1900   new->ext.iterator = iter;
1901
1902   new->block = gfc_get_code ();
1903   new->block->op = EXEC_DO;
1904   new->block->next = head;
1905
1906   *result = new;
1907   return MATCH_YES;
1908
1909 syntax:
1910   gfc_error ("Syntax error in I/O iterator at %C");
1911   m = MATCH_ERROR;
1912
1913 cleanup:
1914   gfc_free_iterator (iter, 1);
1915   gfc_free_statements (head);
1916   gfc_current_locus = old_loc;
1917   return m;
1918 }
1919
1920
1921 /* Match a single element of an IO list, which is either a single
1922    expression or an IO Iterator.  */
1923
1924 static match
1925 match_io_element (io_kind k, gfc_code ** cpp)
1926 {
1927   gfc_expr *expr;
1928   gfc_code *cp;
1929   match m;
1930
1931   expr = NULL;
1932
1933   m = match_io_iterator (k, cpp);
1934   if (m == MATCH_YES)
1935     return MATCH_YES;
1936
1937   if (k == M_READ)
1938     {
1939       m = gfc_match_variable (&expr, 0);
1940       if (m == MATCH_NO)
1941         gfc_error ("Expected variable in READ statement at %C");
1942     }
1943   else
1944     {
1945       m = gfc_match_expr (&expr);
1946       if (m == MATCH_NO)
1947         gfc_error ("Expected expression in %s statement at %C",
1948                    io_kind_name (k));
1949     }
1950
1951   if (m == MATCH_YES)
1952     switch (k)
1953       {
1954       case M_READ:
1955         if (expr->symtree->n.sym->attr.intent == INTENT_IN)
1956           {
1957             gfc_error
1958               ("Variable '%s' in input list at %C cannot be INTENT(IN)",
1959                expr->symtree->n.sym->name);
1960             m = MATCH_ERROR;
1961           }
1962
1963         if (gfc_pure (NULL)
1964             && gfc_impure_variable (expr->symtree->n.sym)
1965             && current_dt->io_unit->ts.type == BT_CHARACTER)
1966           {
1967             gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
1968                        expr->symtree->n.sym->name);
1969             m = MATCH_ERROR;
1970           }
1971
1972         if (gfc_check_do_variable (expr->symtree))
1973           m = MATCH_ERROR;
1974
1975         break;
1976
1977       case M_WRITE:
1978         if (current_dt->io_unit->ts.type == BT_CHARACTER
1979             && gfc_pure (NULL)
1980             && current_dt->io_unit->expr_type == EXPR_VARIABLE
1981             && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
1982           {
1983             gfc_error
1984               ("Cannot write to internal file unit '%s' at %C inside a "
1985                "PURE procedure", current_dt->io_unit->symtree->n.sym->name);
1986             m = MATCH_ERROR;
1987           }
1988
1989         break;
1990
1991       default:
1992         break;
1993       }
1994
1995   if (m != MATCH_YES)
1996     {
1997       gfc_free_expr (expr);
1998       return MATCH_ERROR;
1999     }
2000
2001   cp = gfc_get_code ();
2002   cp->op = EXEC_TRANSFER;
2003   cp->expr = expr;
2004
2005   *cpp = cp;
2006   return MATCH_YES;
2007 }
2008
2009
2010 /* Match an I/O list, building gfc_code structures as we go.  */
2011
2012 static match
2013 match_io_list (io_kind k, gfc_code ** head_p)
2014 {
2015   gfc_code *head, *tail, *new;
2016   match m;
2017
2018   *head_p = head = tail = NULL;
2019   if (gfc_match_eos () == MATCH_YES)
2020     return MATCH_YES;
2021
2022   for (;;)
2023     {
2024       m = match_io_element (k, &new);
2025       if (m == MATCH_ERROR)
2026         goto cleanup;
2027       if (m == MATCH_NO)
2028         goto syntax;
2029
2030       tail = gfc_append_code (tail, new);
2031       if (head == NULL)
2032         head = new;
2033
2034       if (gfc_match_eos () == MATCH_YES)
2035         break;
2036       if (gfc_match_char (',') != MATCH_YES)
2037         goto syntax;
2038     }
2039
2040   *head_p = head;
2041   return MATCH_YES;
2042
2043 syntax:
2044   gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2045
2046 cleanup:
2047   gfc_free_statements (head);
2048   return MATCH_ERROR;
2049 }
2050
2051
2052 /* Attach the data transfer end node.  */
2053
2054 static void
2055 terminate_io (gfc_code * io_code)
2056 {
2057   gfc_code *c;
2058
2059   if (io_code == NULL)
2060     io_code = &new_st;
2061
2062   c = gfc_get_code ();
2063   c->op = EXEC_DT_END;
2064
2065   /* Point to structure that is already there */
2066   c->ext.dt = new_st.ext.dt;
2067   gfc_append_code (io_code, c);
2068 }
2069
2070
2071 /* Match a READ, WRITE or PRINT statement.  */
2072
2073 static match
2074 match_io (io_kind k)
2075 {
2076   char name[GFC_MAX_SYMBOL_LEN + 1];
2077   gfc_code *io_code;
2078   gfc_symbol *sym;
2079   gfc_expr *expr;
2080   int comma_flag, c;
2081   locus where;
2082   gfc_dt *dt;
2083   match m;
2084
2085   comma_flag = 0;
2086   current_dt = dt = gfc_getmem (sizeof (gfc_dt));
2087
2088   if (gfc_match_char ('(') == MATCH_NO)
2089     {
2090       if (k == M_WRITE)
2091         goto syntax;
2092
2093       if (gfc_current_form == FORM_FREE)
2094        {
2095          c = gfc_peek_char();
2096          if (c != ' ' && c != '*' && c != '\'' && c != '"')
2097            {
2098              m = MATCH_NO;
2099              goto cleanup;
2100            }
2101        }
2102
2103       m = match_dt_format (dt);
2104       if (m == MATCH_ERROR)
2105         goto cleanup;
2106       if (m == MATCH_NO)
2107         goto syntax;
2108
2109       comma_flag = 1;
2110       dt->io_unit = default_unit (k);
2111       goto get_io_list;
2112     }
2113
2114   /* Match a control list */
2115   if (match_dt_element (k, dt) == MATCH_YES)
2116     goto next;
2117   if (match_dt_unit (k, dt) != MATCH_YES)
2118     goto loop;
2119
2120   if (gfc_match_char (')') == MATCH_YES)
2121     goto get_io_list;
2122   if (gfc_match_char (',') != MATCH_YES)
2123     goto syntax;
2124
2125   m = match_dt_element (k, dt);
2126   if (m == MATCH_YES)
2127     goto next;
2128   if (m == MATCH_ERROR)
2129     goto cleanup;
2130
2131   m = match_dt_format (dt);
2132   if (m == MATCH_YES)
2133     goto next;
2134   if (m == MATCH_ERROR)
2135     goto cleanup;
2136
2137   where = gfc_current_locus;
2138
2139   if (gfc_match_name (name) == MATCH_YES
2140       && !gfc_find_symbol (name, NULL, 1, &sym)
2141       && sym->attr.flavor == FL_NAMELIST)
2142     {
2143       dt->namelist = sym;
2144       if (k == M_READ && check_namelist (sym))
2145         {
2146           m = MATCH_ERROR;
2147           goto cleanup;
2148         }
2149       goto next;
2150     }
2151
2152   gfc_current_locus = where;
2153
2154   goto loop;                    /* No matches, try regular elements */
2155
2156 next:
2157   if (gfc_match_char (')') == MATCH_YES)
2158     goto get_io_list;
2159   if (gfc_match_char (',') != MATCH_YES)
2160     goto syntax;
2161
2162 loop:
2163   for (;;)
2164     {
2165       m = match_dt_element (k, dt);
2166       if (m == MATCH_NO)
2167         goto syntax;
2168       if (m == MATCH_ERROR)
2169         goto cleanup;
2170
2171       if (gfc_match_char (')') == MATCH_YES)
2172         break;
2173       if (gfc_match_char (',') != MATCH_YES)
2174         goto syntax;
2175     }
2176
2177 get_io_list:
2178   /* Optional leading comma (non-standard).  */
2179   if (!comma_flag
2180       && gfc_match_char (',') == MATCH_YES
2181       && k == M_WRITE
2182       && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before output "
2183                          "item list at %C is an extension") == FAILURE)
2184     return MATCH_ERROR;
2185
2186   io_code = NULL;
2187   if (gfc_match_eos () != MATCH_YES)
2188     {
2189       if (comma_flag && gfc_match_char (',') != MATCH_YES)
2190         {
2191           gfc_error ("Expected comma in I/O list at %C");
2192           m = MATCH_ERROR;
2193           goto cleanup;
2194         }
2195
2196       m = match_io_list (k, &io_code);
2197       if (m == MATCH_ERROR)
2198         goto cleanup;
2199       if (m == MATCH_NO)
2200         goto syntax;
2201     }
2202
2203   /* A full IO statement has been matched.  */
2204   if (dt->io_unit->expr_type == EXPR_VARIABLE
2205       && k == M_WRITE
2206       && dt->io_unit->ts.type == BT_CHARACTER
2207       && dt->io_unit->symtree->n.sym->attr.intent == INTENT_IN)
2208     {
2209       gfc_error ("Internal file '%s' at %L is INTENT(IN)",
2210                  dt->io_unit->symtree->n.sym->name, &dt->io_unit->where);
2211       m = MATCH_ERROR;
2212       goto cleanup;
2213     }
2214
2215   expr = dt->format_expr;
2216
2217   if (expr != NULL && expr->expr_type == EXPR_CONSTANT)
2218     check_format_string (expr);
2219
2220   if (gfc_pure (NULL)
2221       && (k == M_READ || k == M_WRITE)
2222       && dt->io_unit->ts.type != BT_CHARACTER)
2223     {
2224       gfc_error
2225         ("io-unit in %s statement at %C must be an internal file in a "
2226          "PURE procedure", io_kind_name (k));
2227       m = MATCH_ERROR;
2228       goto cleanup;
2229     }
2230
2231   new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
2232   new_st.ext.dt = dt;
2233   new_st.next = io_code;
2234
2235   terminate_io (io_code);
2236
2237   return MATCH_YES;
2238
2239 syntax:
2240   gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
2241   m = MATCH_ERROR;
2242
2243 cleanup:
2244   gfc_free_dt (dt);
2245   return m;
2246 }
2247
2248
2249 match
2250 gfc_match_read (void)
2251 {
2252   return match_io (M_READ);
2253 }
2254
2255 match
2256 gfc_match_write (void)
2257 {
2258   return match_io (M_WRITE);
2259 }
2260
2261 match
2262 gfc_match_print (void)
2263 {
2264   match m;
2265
2266   m = match_io (M_PRINT);
2267   if (m != MATCH_YES)
2268     return m;
2269
2270   if (gfc_pure (NULL))
2271     {
2272       gfc_error ("PRINT statement at %C not allowed within PURE procedure");
2273       return MATCH_ERROR;
2274     }
2275
2276   return MATCH_YES;
2277 }
2278
2279
2280 /* Free a gfc_inquire structure.  */
2281
2282 void
2283 gfc_free_inquire (gfc_inquire * inquire)
2284 {
2285
2286   if (inquire == NULL)
2287     return;
2288
2289   gfc_free_expr (inquire->unit);
2290   gfc_free_expr (inquire->file);
2291   gfc_free_expr (inquire->iostat);
2292   gfc_free_expr (inquire->exist);
2293   gfc_free_expr (inquire->opened);
2294   gfc_free_expr (inquire->number);
2295   gfc_free_expr (inquire->named);
2296   gfc_free_expr (inquire->name);
2297   gfc_free_expr (inquire->access);
2298   gfc_free_expr (inquire->sequential);
2299   gfc_free_expr (inquire->direct);
2300   gfc_free_expr (inquire->form);
2301   gfc_free_expr (inquire->formatted);
2302   gfc_free_expr (inquire->unformatted);
2303   gfc_free_expr (inquire->recl);
2304   gfc_free_expr (inquire->nextrec);
2305   gfc_free_expr (inquire->blank);
2306   gfc_free_expr (inquire->position);
2307   gfc_free_expr (inquire->action);
2308   gfc_free_expr (inquire->read);
2309   gfc_free_expr (inquire->write);
2310   gfc_free_expr (inquire->readwrite);
2311   gfc_free_expr (inquire->delim);
2312   gfc_free_expr (inquire->pad);
2313   gfc_free_expr (inquire->iolength);
2314
2315   gfc_free (inquire);
2316 }
2317
2318
2319 /* Match an element of an INQUIRE statement.  */
2320
2321 #define RETM   if (m != MATCH_NO) return m;
2322
2323 static match
2324 match_inquire_element (gfc_inquire * inquire)
2325 {
2326   match m;
2327
2328   m = match_etag (&tag_unit, &inquire->unit);
2329   RETM m = match_etag (&tag_file, &inquire->file);
2330   RETM m = match_ltag (&tag_err, &inquire->err);
2331   RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
2332   RETM m = match_vtag (&tag_exist, &inquire->exist);
2333   RETM m = match_vtag (&tag_opened, &inquire->opened);
2334   RETM m = match_vtag (&tag_named, &inquire->named);
2335   RETM m = match_vtag (&tag_name, &inquire->name);
2336   RETM m = match_out_tag (&tag_number, &inquire->number);
2337   RETM m = match_vtag (&tag_s_access, &inquire->access);
2338   RETM m = match_vtag (&tag_sequential, &inquire->sequential);
2339   RETM m = match_vtag (&tag_direct, &inquire->direct);
2340   RETM m = match_vtag (&tag_s_form, &inquire->form);
2341   RETM m = match_vtag (&tag_formatted, &inquire->formatted);
2342   RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
2343   RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
2344   RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
2345   RETM m = match_vtag (&tag_s_blank, &inquire->blank);
2346   RETM m = match_vtag (&tag_s_position, &inquire->position);
2347   RETM m = match_vtag (&tag_s_action, &inquire->action);
2348   RETM m = match_vtag (&tag_read, &inquire->read);
2349   RETM m = match_vtag (&tag_write, &inquire->write);
2350   RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
2351   RETM m = match_vtag (&tag_s_delim, &inquire->delim);
2352   RETM m = match_vtag (&tag_s_pad, &inquire->pad);
2353   RETM m = match_vtag (&tag_iolength, &inquire->iolength);
2354   RETM return MATCH_NO;
2355 }
2356
2357 #undef RETM
2358
2359
2360 match
2361 gfc_match_inquire (void)
2362 {
2363   gfc_inquire *inquire;
2364   gfc_code *code;
2365   match m;
2366
2367   m = gfc_match_char ('(');
2368   if (m == MATCH_NO)
2369     return m;
2370
2371   inquire = gfc_getmem (sizeof (gfc_inquire));
2372
2373   m = match_inquire_element (inquire);
2374   if (m == MATCH_ERROR)
2375     goto cleanup;
2376   if (m == MATCH_NO)
2377     {
2378       m = gfc_match_expr (&inquire->unit);
2379       if (m == MATCH_ERROR)
2380         goto cleanup;
2381       if (m == MATCH_NO)
2382         goto syntax;
2383     }
2384
2385   /* See if we have the IOLENGTH form of the inquire statement.  */
2386   if (inquire->iolength != NULL)
2387     {
2388       if (gfc_match_char (')') != MATCH_YES)
2389         goto syntax;
2390
2391       m = match_io_list (M_INQUIRE, &code);
2392       if (m == MATCH_ERROR)
2393         goto cleanup;
2394       if (m == MATCH_NO)
2395         goto syntax;
2396
2397       terminate_io (code);
2398
2399       new_st.op = EXEC_IOLENGTH;
2400       new_st.expr = inquire->iolength;
2401       new_st.ext.inquire = inquire;
2402
2403       if (gfc_pure (NULL))
2404         {
2405           gfc_free_statements (code);
2406           gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
2407           return MATCH_ERROR;
2408         }
2409
2410       new_st.next = code;
2411       return MATCH_YES;
2412     }
2413
2414   /* At this point, we have the non-IOLENGTH inquire statement.  */
2415   for (;;)
2416     {
2417       if (gfc_match_char (')') == MATCH_YES)
2418         break;
2419       if (gfc_match_char (',') != MATCH_YES)
2420         goto syntax;
2421
2422       m = match_inquire_element (inquire);
2423       if (m == MATCH_ERROR)
2424         goto cleanup;
2425       if (m == MATCH_NO)
2426         goto syntax;
2427
2428       if (inquire->iolength != NULL)
2429         {
2430           gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
2431           goto cleanup;
2432         }
2433     }
2434
2435   if (gfc_match_eos () != MATCH_YES)
2436     goto syntax;
2437
2438   if (gfc_pure (NULL))
2439     {
2440       gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
2441       goto cleanup;
2442     }
2443
2444   new_st.op = EXEC_INQUIRE;
2445   new_st.ext.inquire = inquire;
2446   return MATCH_YES;
2447
2448 syntax:
2449   gfc_syntax_error (ST_INQUIRE);
2450
2451 cleanup:
2452   gfc_free_inquire (inquire);
2453   return MATCH_ERROR;
2454 }
2455
2456
2457 /* Resolve everything in a gfc_inquire structure.  */
2458
2459 try
2460 gfc_resolve_inquire (gfc_inquire * inquire)
2461 {
2462
2463   RESOLVE_TAG (&tag_unit, inquire->unit);
2464   RESOLVE_TAG (&tag_file, inquire->file);
2465   RESOLVE_TAG (&tag_iostat, inquire->iostat);
2466   RESOLVE_TAG (&tag_exist, inquire->exist);
2467   RESOLVE_TAG (&tag_opened, inquire->opened);
2468   RESOLVE_TAG (&tag_number, inquire->number);
2469   RESOLVE_TAG (&tag_named, inquire->named);
2470   RESOLVE_TAG (&tag_name, inquire->name);
2471   RESOLVE_TAG (&tag_s_access, inquire->access);
2472   RESOLVE_TAG (&tag_sequential, inquire->sequential);
2473   RESOLVE_TAG (&tag_direct, inquire->direct);
2474   RESOLVE_TAG (&tag_s_form, inquire->form);
2475   RESOLVE_TAG (&tag_formatted, inquire->formatted);
2476   RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
2477   RESOLVE_TAG (&tag_s_recl, inquire->recl);
2478   RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
2479   RESOLVE_TAG (&tag_s_blank, inquire->blank);
2480   RESOLVE_TAG (&tag_s_position, inquire->position);
2481   RESOLVE_TAG (&tag_s_action, inquire->action);
2482   RESOLVE_TAG (&tag_read, inquire->read);
2483   RESOLVE_TAG (&tag_write, inquire->write);
2484   RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
2485   RESOLVE_TAG (&tag_s_delim, inquire->delim);
2486   RESOLVE_TAG (&tag_s_pad, inquire->pad);
2487   RESOLVE_TAG (&tag_iolength, inquire->iolength);
2488
2489   if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)
2490     return FAILURE;
2491
2492   return SUCCESS;
2493 }