OSDN Git Service

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