OSDN Git Service

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