OSDN Git Service

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