OSDN Git Service

2009-08-24 Jerry DeLisle <jvdelisle@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / fortran / io.c
1 /* Deal with I/O statements & related stuff.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3    Free Software Foundation, Inc.
4    Contributed by Andy Vaught
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, 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 COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "flags.h"
25 #include "gfortran.h"
26 #include "match.h"
27 #include "parse.h"
28
29 gfc_st_label
30 format_asterisk = {0, NULL, NULL, -1, ST_LABEL_FORMAT, ST_LABEL_FORMAT, NULL,
31                    0, {NULL, NULL}};
32
33 typedef struct
34 {
35   const char *name, *spec, *value;
36   bt type;
37 }
38 io_tag;
39
40 static const io_tag
41         tag_file        = {"FILE", " file =", " %e", BT_CHARACTER },
42         tag_status      = {"STATUS", " status =", " %e", BT_CHARACTER},
43         tag_e_access    = {"ACCESS", " access =", " %e", BT_CHARACTER},
44         tag_e_form      = {"FORM", " form =", " %e", BT_CHARACTER},
45         tag_e_recl      = {"RECL", " recl =", " %e", BT_INTEGER},
46         tag_e_blank     = {"BLANK", " blank =", " %e", BT_CHARACTER},
47         tag_e_position  = {"POSITION", " position =", " %e", BT_CHARACTER},
48         tag_e_action    = {"ACTION", " action =", " %e", BT_CHARACTER},
49         tag_e_delim     = {"DELIM", " delim =", " %e", BT_CHARACTER},
50         tag_e_pad       = {"PAD", " pad =", " %e", BT_CHARACTER},
51         tag_e_decimal   = {"DECIMAL", " decimal =", " %e", BT_CHARACTER},
52         tag_e_encoding  = {"ENCODING", " encoding =", " %e", BT_CHARACTER},
53         tag_e_async     = {"ASYNCHRONOUS", " asynchronous =", " %e", BT_CHARACTER},
54         tag_e_round     = {"ROUND", " round =", " %e", BT_CHARACTER},
55         tag_e_sign      = {"SIGN", " sign =", " %e", BT_CHARACTER},
56         tag_unit        = {"UNIT", " unit =", " %e", BT_INTEGER},
57         tag_advance     = {"ADVANCE", " advance =", " %e", BT_CHARACTER},
58         tag_rec         = {"REC", " rec =", " %e", BT_INTEGER},
59         tag_spos        = {"POSITION", " pos =", " %e", BT_INTEGER},
60         tag_format      = {"FORMAT", NULL, NULL, BT_CHARACTER},
61         tag_iomsg       = {"IOMSG", " iomsg =", " %e", BT_CHARACTER},
62         tag_iostat      = {"IOSTAT", " iostat =", " %v", BT_INTEGER},
63         tag_size        = {"SIZE", " size =", " %v", BT_INTEGER},
64         tag_exist       = {"EXIST", " exist =", " %v", BT_LOGICAL},
65         tag_opened      = {"OPENED", " opened =", " %v", BT_LOGICAL},
66         tag_named       = {"NAMED", " named =", " %v", BT_LOGICAL},
67         tag_name        = {"NAME", " name =", " %v", BT_CHARACTER},
68         tag_number      = {"NUMBER", " number =", " %v", BT_INTEGER},
69         tag_s_access    = {"ACCESS", " access =", " %v", BT_CHARACTER},
70         tag_sequential  = {"SEQUENTIAL", " sequential =", " %v", BT_CHARACTER},
71         tag_direct      = {"DIRECT", " direct =", " %v", BT_CHARACTER},
72         tag_s_form      = {"FORM", " form =", " %v", BT_CHARACTER},
73         tag_formatted   = {"FORMATTED", " formatted =", " %v", BT_CHARACTER},
74         tag_unformatted = {"UNFORMATTED", " unformatted =", " %v", BT_CHARACTER},
75         tag_s_recl      = {"RECL", " recl =", " %v", BT_INTEGER},
76         tag_nextrec     = {"NEXTREC", " nextrec =", " %v", BT_INTEGER},
77         tag_s_blank     = {"BLANK", " blank =", " %v", BT_CHARACTER},
78         tag_s_position  = {"POSITION", " position =", " %v", BT_CHARACTER},
79         tag_s_action    = {"ACTION", " action =", " %v", BT_CHARACTER},
80         tag_read        = {"READ", " read =", " %v", BT_CHARACTER},
81         tag_write       = {"WRITE", " write =", " %v", BT_CHARACTER},
82         tag_readwrite   = {"READWRITE", " readwrite =", " %v", BT_CHARACTER},
83         tag_s_delim     = {"DELIM", " delim =", " %v", BT_CHARACTER},
84         tag_s_pad       = {"PAD", " pad =", " %v", BT_CHARACTER},
85         tag_s_decimal   = {"DECIMAL", " decimal =", " %v", BT_CHARACTER},
86         tag_s_encoding  = {"ENCODING", " encoding =", " %v", BT_CHARACTER},
87         tag_s_async     = {"ASYNCHRONOUS", " asynchronous =", " %v", BT_CHARACTER},
88         tag_s_round     = {"ROUND", " round =", " %v", BT_CHARACTER},
89         tag_s_sign      = {"SIGN", " sign =", " %v", BT_CHARACTER},
90         tag_iolength    = {"IOLENGTH", " iolength =", " %v", BT_INTEGER},
91         tag_convert     = {"CONVERT", " convert =", " %e", BT_CHARACTER},
92         tag_strm_out    = {"POS", " pos =", " %v", BT_INTEGER},
93         tag_err         = {"ERR", " err =", " %l", BT_UNKNOWN},
94         tag_end         = {"END", " end =", " %l", BT_UNKNOWN},
95         tag_eor         = {"EOR", " eor =", " %l", BT_UNKNOWN},
96         tag_id          = {"ID", " id =", " %v", BT_INTEGER},
97         tag_pending     = {"PENDING", " pending =", " %v", BT_LOGICAL},
98         tag_newunit     = {"NEWUNIT", " newunit =", " %v", BT_INTEGER};
99
100 static gfc_dt *current_dt;
101
102 #define RESOLVE_TAG(x, y) if (resolve_tag(x, y) == FAILURE) return FAILURE;
103
104
105 /**************** Fortran 95 FORMAT parser  *****************/
106
107 /* FORMAT tokens returned by format_lex().  */
108 typedef enum
109 {
110   FMT_NONE, FMT_UNKNOWN, FMT_SIGNED_INT, FMT_ZERO, FMT_POSINT, FMT_PERIOD,
111   FMT_COMMA, FMT_COLON, FMT_SLASH, FMT_DOLLAR, FMT_LPAREN,
112   FMT_RPAREN, FMT_X, FMT_SIGN, FMT_BLANK, FMT_CHAR, FMT_P, FMT_IBOZ, FMT_F,
113   FMT_E, FMT_EN, FMT_ES, FMT_G, FMT_L, FMT_A, FMT_D, FMT_H, FMT_END,
114   FMT_ERROR, FMT_DC, FMT_DP, FMT_T, FMT_TR, FMT_TL, FMT_STAR
115 }
116 format_token;
117
118 /* Local variables for checking format strings.  The saved_token is
119    used to back up by a single format token during the parsing
120    process.  */
121 static gfc_char_t *format_string;
122 static int format_string_pos;
123 static int format_length, use_last_char;
124 static int starting_format_length;
125 static char error_element;
126 static locus format_locus;
127
128 static format_token saved_token;
129
130 static enum
131 { MODE_STRING, MODE_FORMAT, MODE_COPY }
132 mode;
133
134
135 /* Return the next character in the format string.  */
136
137 static char
138 next_char (int in_string)
139 {
140   static gfc_char_t c;
141
142   if (use_last_char)
143     {
144       use_last_char = 0;
145       return c;
146     }
147
148   format_length++;
149
150   if (mode == MODE_STRING)
151     c = *format_string++;
152   else
153     {
154       c = gfc_next_char_literal (in_string);
155       if (c == '\n')
156         c = '\0';
157     }
158
159   if (gfc_option.flag_backslash && c == '\\')
160     {
161       locus old_locus = gfc_current_locus;
162
163       if (gfc_match_special_char (&c) == MATCH_NO)
164         gfc_current_locus = old_locus;
165
166       if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
167         gfc_warning ("Extension: backslash character at %C");
168     }
169
170   if (mode == MODE_COPY)
171     *format_string++ = c;
172
173   if (mode != MODE_STRING)
174     format_locus = gfc_current_locus;
175
176   format_string_pos++;
177
178   c = gfc_wide_toupper (c);
179   return c;
180 }
181
182
183 /* Back up one character position.  Only works once.  */
184
185 static void
186 unget_char (void)
187 {
188   use_last_char = 1;
189 }
190
191 /* Eat up the spaces and return a character.  */
192
193 static char
194 next_char_not_space (bool *error)
195 {
196   char c;
197   do
198     {
199       error_element = c = next_char (0);
200       if (c == '\t')
201         {
202           if (gfc_option.allow_std & GFC_STD_GNU)
203             gfc_warning ("Extension: Tab character in format at %C");
204           else
205             {
206               gfc_error ("Extension: Tab character in format at %C");
207               *error = true;
208               return c;
209             }
210         }
211     }
212   while (gfc_is_whitespace (c));
213   return c;
214 }
215
216 static int value = 0;
217
218 /* Simple lexical analyzer for getting the next token in a FORMAT
219    statement.  */
220
221 static format_token
222 format_lex (void)
223 {
224   format_token token;
225   char c, delim;
226   int zflag;
227   int negative_flag;
228   bool error = false;
229
230   if (saved_token != FMT_NONE)
231     {
232       token = saved_token;
233       saved_token = FMT_NONE;
234       return token;
235     }
236
237   c = next_char_not_space (&error);
238   
239   negative_flag = 0;
240   switch (c)
241     {
242     case '-':
243       negative_flag = 1;
244     case '+':
245       c = next_char_not_space (&error);
246       if (!ISDIGIT (c))
247         {
248           token = FMT_UNKNOWN;
249           break;
250         }
251
252       value = c - '0';
253
254       do
255         {
256           c = next_char_not_space (&error);
257           if (ISDIGIT (c))
258             value = 10 * value + c - '0';
259         }
260       while (ISDIGIT (c));
261
262       unget_char ();
263
264       if (negative_flag)
265         value = -value;
266
267       token = FMT_SIGNED_INT;
268       break;
269
270     case '0':
271     case '1':
272     case '2':
273     case '3':
274     case '4':
275     case '5':
276     case '6':
277     case '7':
278     case '8':
279     case '9':
280       zflag = (c == '0');
281
282       value = c - '0';
283
284       do
285         {
286           c = next_char_not_space (&error);
287           if (ISDIGIT (c))
288             {
289               value = 10 * value + c - '0';
290               if (c != '0')
291                 zflag = 0;
292             }
293         }
294       while (ISDIGIT (c));
295
296       unget_char ();
297       token = zflag ? FMT_ZERO : FMT_POSINT;
298       break;
299
300     case '.':
301       token = FMT_PERIOD;
302       break;
303
304     case ',':
305       token = FMT_COMMA;
306       break;
307
308     case ':':
309       token = FMT_COLON;
310       break;
311
312     case '/':
313       token = FMT_SLASH;
314       break;
315
316     case '$':
317       token = FMT_DOLLAR;
318       break;
319
320     case 'T':
321       c = next_char_not_space (&error);
322       switch (c)
323         {
324         case 'L':
325           token = FMT_TL;
326           break;
327         case 'R':
328           token = FMT_TR;
329           break;
330         default:
331           token = FMT_T;
332           unget_char ();
333         }
334       break;
335
336     case '(':
337       token = FMT_LPAREN;
338       break;
339
340     case ')':
341       token = FMT_RPAREN;
342       break;
343
344     case 'X':
345       token = FMT_X;
346       break;
347
348     case 'S':
349       c = next_char_not_space (&error);
350       if (c != 'P' && c != 'S')
351         unget_char ();
352
353       token = FMT_SIGN;
354       break;
355
356     case 'B':
357       c = next_char_not_space (&error);
358       if (c == 'N' || c == 'Z')
359         token = FMT_BLANK;
360       else
361         {
362           unget_char ();
363           token = FMT_IBOZ;
364         }
365
366       break;
367
368     case '\'':
369     case '"':
370       delim = c;
371
372       value = 0;
373
374       for (;;)
375         {
376           c = next_char (1);
377           if (c == '\0')
378             {
379               token = FMT_END;
380               break;
381             }
382
383           if (c == delim)
384             {
385               c = next_char (1);
386
387               if (c == '\0')
388                 {
389                   token = FMT_END;
390                   break;
391                 }
392
393               if (c != delim)
394                 {
395                   unget_char ();
396                   token = FMT_CHAR;
397                   break;
398                 }
399             }
400           value++;
401         }
402       break;
403
404     case 'P':
405       token = FMT_P;
406       break;
407
408     case 'I':
409     case 'O':
410     case 'Z':
411       token = FMT_IBOZ;
412       break;
413
414     case 'F':
415       token = FMT_F;
416       break;
417
418     case 'E':
419       c = next_char_not_space (&error);
420       if (c == 'N' )
421         token = FMT_EN;
422       else if (c == 'S')
423         token = FMT_ES;
424       else
425         {
426           token = FMT_E;
427           unget_char ();
428         }
429
430       break;
431
432     case 'G':
433       token = FMT_G;
434       break;
435
436     case 'H':
437       token = FMT_H;
438       break;
439
440     case 'L':
441       token = FMT_L;
442       break;
443
444     case 'A':
445       token = FMT_A;
446       break;
447
448     case 'D':
449       c = next_char_not_space (&error);
450       if (c == 'P')
451         {
452           if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DP format "
453               "specifier not allowed at %C") == FAILURE)
454             return FMT_ERROR;
455           token = FMT_DP;
456         }
457       else if (c == 'C')
458         {
459           if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DC format "
460               "specifier not allowed at %C") == FAILURE)
461             return FMT_ERROR;
462           token = FMT_DC;
463         }
464       else
465         {
466           token = FMT_D;
467           unget_char ();
468         }
469       break;
470
471     case '\0':
472       token = FMT_END;
473       break;
474
475     case '*':
476       token = FMT_STAR;
477       break;
478
479     default:
480       token = FMT_UNKNOWN;
481       break;
482     }
483
484   if (error)
485     return FMT_ERROR;
486
487   return token;
488 }
489
490
491 static const char *
492 token_to_string (format_token t)
493 {
494   switch (t)
495     {
496       case FMT_D:
497         return "D";
498       case FMT_G:
499         return "G";
500       case FMT_E:
501         return "E";
502       case FMT_EN:
503         return "EN";
504       case FMT_ES:
505         return "ES";
506       default:
507         return "";
508     }
509 }
510
511 /* Check a format statement.  The format string, either from a FORMAT
512    statement or a constant in an I/O statement has already been parsed
513    by itself, and we are checking it for validity.  The dual origin
514    means that the warning message is a little less than great.  */
515
516 static gfc_try
517 check_format (bool is_input)
518 {
519   const char *posint_required     = _("Positive width required");
520   const char *nonneg_required     = _("Nonnegative width required");
521   const char *unexpected_element  = _("Unexpected element '%c' in format string"
522                                       " at %L");
523   const char *unexpected_end      = _("Unexpected end of format string");
524   const char *zero_width          = _("Zero width in format descriptor");
525
526   const char *error;
527   format_token t, u;
528   int level;
529   int repeat;
530   gfc_try rv;
531
532   use_last_char = 0;
533   saved_token = FMT_NONE;
534   level = 0;
535   repeat = 0;
536   rv = SUCCESS;
537   format_string_pos = 0;
538
539   t = format_lex ();
540   if (t == FMT_ERROR)
541     goto fail;
542   if (t != FMT_LPAREN)
543     {
544       error = _("Missing leading left parenthesis");
545       goto syntax;
546     }
547
548   t = format_lex ();
549   if (t == FMT_ERROR)
550     goto fail;
551   if (t == FMT_RPAREN)
552     goto finished;              /* Empty format is legal */
553   saved_token = t;
554
555 format_item:
556   /* In this state, the next thing has to be a format item.  */
557   t = format_lex ();
558   if (t == FMT_ERROR)
559     goto fail;
560 format_item_1:
561   switch (t)
562     {
563     case FMT_STAR:
564       repeat = -1;
565       t = format_lex ();
566       if (t == FMT_ERROR)
567         goto fail;
568       if (t == FMT_LPAREN)
569         {
570           level++;
571           goto format_item;
572         }
573       error = _("Left parenthesis required after '*'");
574       goto syntax;
575
576     case FMT_POSINT:
577       repeat = value;
578       t = format_lex ();
579       if (t == FMT_ERROR)
580         goto fail;
581       if (t == FMT_LPAREN)
582         {
583           level++;
584           goto format_item;
585         }
586
587       if (t == FMT_SLASH)
588         goto optional_comma;
589
590       goto data_desc;
591
592     case FMT_LPAREN:
593       level++;
594       goto format_item;
595
596     case FMT_SIGNED_INT:
597     case FMT_ZERO:
598       /* Signed integer can only precede a P format.  */
599       t = format_lex ();
600       if (t == FMT_ERROR)
601         goto fail;
602       if (t != FMT_P)
603         {
604           error = _("Expected P edit descriptor");
605           goto syntax;
606         }
607
608       goto data_desc;
609
610     case FMT_P:
611       /* P requires a prior number.  */
612       error = _("P descriptor requires leading scale factor");
613       goto syntax;
614
615     case FMT_X:
616       /* X requires a prior number if we're being pedantic.  */
617       if (gfc_notify_std (GFC_STD_GNU, "Extension: X descriptor "
618                           "requires leading space count at %L", &format_locus)
619           == FAILURE)
620         return FAILURE;
621       goto between_desc;
622
623     case FMT_SIGN:
624     case FMT_BLANK:
625     case FMT_DP:
626     case FMT_DC:
627       goto between_desc;
628
629     case FMT_CHAR:
630       goto extension_optional_comma;
631
632     case FMT_COLON:
633     case FMT_SLASH:
634       goto optional_comma;
635
636     case FMT_DOLLAR:
637       t = format_lex ();
638       if (t == FMT_ERROR)
639         goto fail;
640
641       if (gfc_notify_std (GFC_STD_GNU, "Extension: $ descriptor at %L",
642           &format_locus) == FAILURE)
643         return FAILURE;
644       if (t != FMT_RPAREN || level > 0)
645         {
646           gfc_warning ("$ should be the last specifier in format at %L",
647                        &format_locus);
648           goto optional_comma_1;
649         }
650
651       goto finished;
652
653     case FMT_T:
654     case FMT_TL:
655     case FMT_TR:
656     case FMT_IBOZ:
657     case FMT_F:
658     case FMT_E:
659     case FMT_EN:
660     case FMT_ES:
661     case FMT_G:
662     case FMT_L:
663     case FMT_A:
664     case FMT_D:
665     case FMT_H:
666       goto data_desc;
667
668     case FMT_END:
669       error = unexpected_end;
670       goto syntax;
671
672     default:
673       error = unexpected_element;
674       goto syntax;
675     }
676
677 data_desc:
678   /* In this state, t must currently be a data descriptor.
679      Deal with things that can/must follow the descriptor.  */
680   switch (t)
681     {
682     case FMT_SIGN:
683     case FMT_BLANK:
684     case FMT_DP:
685     case FMT_DC:
686     case FMT_X:
687       break;
688
689     case FMT_P:
690       /* Comma after P is allowed only for F, E, EN, ES, D, or G.
691          10.1.1 (1).  */
692       t = format_lex ();
693       if (t == FMT_ERROR)
694         goto fail;
695       if (gfc_option.allow_std < GFC_STD_F2003 && t != FMT_COMMA
696           && t != FMT_F && t != FMT_E && t != FMT_EN && t != FMT_ES
697           && t != FMT_D && t != FMT_G && t != FMT_RPAREN)
698         {
699           error = _("Comma required after P descriptor");
700           goto syntax;
701         }
702       if (t != FMT_COMMA)
703         {
704           if (t == FMT_POSINT)
705             {
706               t = format_lex ();
707               if (t == FMT_ERROR)
708                 goto fail;
709             }
710           if (t != FMT_F && t != FMT_E && t != FMT_EN && t != FMT_ES && t != FMT_D
711               && t != FMT_G && t != FMT_RPAREN)
712             {
713               error = _("Comma required after P descriptor");
714               goto syntax;
715             }
716         }
717
718       saved_token = t;
719       goto optional_comma;
720
721     case FMT_T:
722     case FMT_TL:
723     case FMT_TR:
724       t = format_lex ();
725       if (t != FMT_POSINT)
726         {
727           error = _("Positive width required with T descriptor");
728           goto syntax;
729         }
730       break;
731
732     case FMT_L:
733       t = format_lex ();
734       if (t == FMT_ERROR)
735         goto fail;
736       if (t == FMT_POSINT)
737         break;
738
739       switch (gfc_notification_std (GFC_STD_GNU))
740         {
741           case WARNING:
742             if (mode != MODE_FORMAT)
743               format_locus.nextc += format_string_pos;
744             gfc_warning ("Extension: Missing positive width after L "
745                          "descriptor at %L", &format_locus);
746             saved_token = t;
747             break;
748
749           case ERROR:
750             error = posint_required;
751             goto syntax;
752
753           case SILENT:
754             saved_token = t;
755             break;
756
757           default:
758             gcc_unreachable ();
759         }
760       break;
761
762     case FMT_A:
763       t = format_lex ();
764       if (t == FMT_ERROR)
765         goto fail;
766       if (t == FMT_ZERO)
767         {
768           error = zero_width;
769           goto syntax;
770         }
771       if (t != FMT_POSINT)
772         saved_token = t;
773       break;
774
775     case FMT_D:
776     case FMT_E:
777     case FMT_G:
778     case FMT_EN:
779     case FMT_ES:
780       u = format_lex ();
781       if (t == FMT_G && u == FMT_ZERO)
782         {
783           if (is_input)
784             {
785               error = zero_width;
786               goto syntax;
787             }
788           if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: 'G0' in "
789                               "format at %L", &format_locus) == FAILURE)
790             return FAILURE;
791           u = format_lex ();
792           if (u != FMT_PERIOD)
793             {
794               saved_token = u;
795               break;
796             }
797           u = format_lex ();
798           if (u != FMT_POSINT)
799             {
800               error = posint_required;
801               goto syntax;
802             }
803           u = format_lex ();
804           if (u == FMT_E)
805             {
806               error = _("E specifier not allowed with g0 descriptor");
807               goto syntax;
808             }
809           saved_token = u;
810           break;
811         }
812
813       if (u != FMT_POSINT)
814         {
815           format_locus.nextc += format_string_pos;
816           gfc_error_now ("Positive width required in format "
817                          "specifier %s at %L", token_to_string (t),
818                          &format_locus);
819           saved_token = u;
820           goto finished;
821         }
822
823       u = format_lex ();
824       if (u == FMT_ERROR)
825         goto fail;
826       if (u != FMT_PERIOD)
827         {
828           /* Warn if -std=legacy, otherwise error.  */
829           format_locus.nextc += format_string_pos;
830           if (gfc_option.warn_std != 0)
831             {
832               gfc_error_now ("Period required in format "
833                              "specifier %s at %L", token_to_string (t),
834                              &format_locus);
835               saved_token = u;
836               goto finished;
837             }
838           else
839             gfc_warning ("Period required in format "
840                          "specifier %s at %L", token_to_string (t),
841                           &format_locus);
842           saved_token = u;
843           break;
844         }
845
846       u = format_lex ();
847       if (u == FMT_ERROR)
848         goto fail;
849       if (u != FMT_ZERO && u != FMT_POSINT)
850         {
851           error = nonneg_required;
852           goto syntax;
853         }
854
855       if (t == FMT_D)
856         break;
857
858       /* Look for optional exponent.  */
859       u = format_lex ();
860       if (u == FMT_ERROR)
861         goto fail;
862       if (u != FMT_E)
863         {
864           saved_token = u;
865         }
866       else
867         {
868           u = format_lex ();
869           if (u == FMT_ERROR)
870             goto fail;
871           if (u != FMT_POSINT)
872             {
873               error = _("Positive exponent width required");
874               goto syntax;
875             }
876         }
877
878       break;
879
880     case FMT_F:
881       t = format_lex ();
882       if (t == FMT_ERROR)
883         goto fail;
884       if (t != FMT_ZERO && t != FMT_POSINT)
885         {
886           error = nonneg_required;
887           goto syntax;
888         }
889       else if (is_input && t == FMT_ZERO)
890         {
891           error = posint_required;
892           goto syntax;
893         }
894
895       t = format_lex ();
896       if (t == FMT_ERROR)
897         goto fail;
898       if (t != FMT_PERIOD)
899         {
900           /* Warn if -std=legacy, otherwise error.  */
901           if (gfc_option.warn_std != 0)
902             {
903               error = _("Period required in format specifier");
904               goto syntax;
905             }
906           if (mode != MODE_FORMAT)
907             format_locus.nextc += format_string_pos;
908           gfc_warning ("Period required in format specifier at %L",
909                        &format_locus);
910           saved_token = t;
911           break;
912         }
913
914       t = format_lex ();
915       if (t == FMT_ERROR)
916         goto fail;
917       if (t != FMT_ZERO && t != FMT_POSINT)
918         {
919           error = nonneg_required;
920           goto syntax;
921         }
922
923       break;
924
925     case FMT_H:
926       if (!(gfc_option.allow_std & GFC_STD_GNU) && !inhibit_warnings)
927         {
928           if (mode != MODE_FORMAT)
929             format_locus.nextc += format_string_pos;
930           gfc_warning ("The H format specifier at %L is"
931                        " a Fortran 95 deleted feature", &format_locus);
932         }
933       while (repeat >0)
934        {
935           next_char (1);
936           repeat -- ;
937        }
938      break;
939
940     case FMT_IBOZ:
941       t = format_lex ();
942       if (t == FMT_ERROR)
943         goto fail;
944       if (t != FMT_ZERO && t != FMT_POSINT)
945         {
946           error = nonneg_required;
947           goto syntax;
948         }
949       else if (is_input && t == FMT_ZERO)
950         {
951           error = posint_required;
952           goto syntax;
953         }
954
955       t = format_lex ();
956       if (t == FMT_ERROR)
957         goto fail;
958       if (t != FMT_PERIOD)
959         {
960           saved_token = t;
961         }
962       else
963         {
964           t = format_lex ();
965           if (t == FMT_ERROR)
966             goto fail;
967           if (t != FMT_ZERO && t != FMT_POSINT)
968             {
969               error = nonneg_required;
970               goto syntax;
971             }
972         }
973
974       break;
975
976     default:
977       error = unexpected_element;
978       goto syntax;
979     }
980
981 between_desc:
982   /* Between a descriptor and what comes next.  */
983   t = format_lex ();
984   if (t == FMT_ERROR)
985     goto fail;
986   switch (t)
987     {
988
989     case FMT_COMMA:
990       goto format_item;
991
992     case FMT_RPAREN:
993       level--;
994       if (level < 0)
995         goto finished;
996       goto between_desc;
997
998     case FMT_COLON:
999     case FMT_SLASH:
1000       goto optional_comma;
1001
1002     case FMT_END:
1003       error = unexpected_end;
1004       goto syntax;
1005
1006     default:
1007       if (mode != MODE_FORMAT)
1008         format_locus.nextc += format_string_pos;
1009       if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %L",
1010           &format_locus) == FAILURE)
1011         return FAILURE;
1012       goto format_item_1;
1013     }
1014
1015 optional_comma:
1016   /* Optional comma is a weird between state where we've just finished
1017      reading a colon, slash, dollar or P descriptor.  */
1018   t = format_lex ();
1019   if (t == FMT_ERROR)
1020     goto fail;
1021 optional_comma_1:
1022   switch (t)
1023     {
1024     case FMT_COMMA:
1025       break;
1026
1027     case FMT_RPAREN:
1028       level--;
1029       if (level < 0)
1030         goto finished;
1031       goto between_desc;
1032
1033     default:
1034       /* Assume that we have another format item.  */
1035       saved_token = t;
1036       break;
1037     }
1038
1039   goto format_item;
1040
1041 extension_optional_comma:
1042   /* As a GNU extension, permit a missing comma after a string literal.  */
1043   t = format_lex ();
1044   if (t == FMT_ERROR)
1045     goto fail;
1046   switch (t)
1047     {
1048     case FMT_COMMA:
1049       break;
1050
1051     case FMT_RPAREN:
1052       level--;
1053       if (level < 0)
1054         goto finished;
1055       goto between_desc;
1056
1057     case FMT_COLON:
1058     case FMT_SLASH:
1059       goto optional_comma;
1060
1061     case FMT_END:
1062       error = unexpected_end;
1063       goto syntax;
1064
1065     default:
1066       if (mode != MODE_FORMAT)
1067         format_locus.nextc += format_string_pos;
1068       if (gfc_notify_std (GFC_STD_GNU, "Extension: Missing comma at %L",
1069           &format_locus) == FAILURE)
1070         return FAILURE;
1071       saved_token = t;
1072       break;
1073     }
1074
1075   goto format_item;
1076   
1077 syntax:
1078   if (mode != MODE_FORMAT)
1079     format_locus.nextc += format_string_pos;
1080   if (error == unexpected_element)
1081     gfc_error (error, error_element, &format_locus);
1082   else
1083     gfc_error ("%s in format string at %L", error, &format_locus);
1084 fail:
1085   rv = FAILURE;
1086
1087 finished:
1088   /* check for extraneous characters at end of valid format string */
1089   if ( starting_format_length > format_length )
1090     {
1091        format_locus.nextc += format_length + 1; /* point to the extra */
1092        gfc_warning ("Extraneous characters in format at %L", &format_locus); 
1093     }
1094     
1095   return rv;
1096 }
1097
1098
1099 /* Given an expression node that is a constant string, see if it looks
1100    like a format string.  */
1101
1102 static gfc_try
1103 check_format_string (gfc_expr *e, bool is_input)
1104 {
1105   if (!e || e->ts.type != BT_CHARACTER || e->expr_type != EXPR_CONSTANT)
1106     return SUCCESS;
1107
1108   mode = MODE_STRING;
1109   format_string = e->value.character.string;
1110   starting_format_length = e->value.character.length;
1111   /* More elaborate measures are needed to show where a problem is within a
1112      format string that has been calculated, but that's probably not worth the
1113      effort.  */
1114   format_locus = e->where;
1115
1116   return check_format (is_input);
1117 }
1118
1119
1120 /************ Fortran 95 I/O statement matchers *************/
1121
1122 /* Match a FORMAT statement.  This amounts to actually parsing the
1123    format descriptors in order to correctly locate the end of the
1124    format string.  */
1125
1126 match
1127 gfc_match_format (void)
1128 {
1129   gfc_expr *e;
1130   locus start;
1131
1132   if (gfc_current_ns->proc_name
1133       && gfc_current_ns->proc_name->attr.flavor == FL_MODULE)
1134     {
1135       gfc_error ("Format statement in module main block at %C");
1136       return MATCH_ERROR;
1137     }
1138
1139   if (gfc_statement_label == NULL)
1140     {
1141       gfc_error ("Missing format label at %C");
1142       return MATCH_ERROR;
1143     }
1144   gfc_gobble_whitespace ();
1145
1146   mode = MODE_FORMAT;
1147   format_length = 0;
1148
1149   start = gfc_current_locus;
1150
1151   if (check_format (false) == FAILURE)
1152     return MATCH_ERROR;
1153
1154   if (gfc_match_eos () != MATCH_YES)
1155     {
1156       gfc_syntax_error (ST_FORMAT);
1157       return MATCH_ERROR;
1158     }
1159
1160   /* The label doesn't get created until after the statement is done
1161      being matched, so we have to leave the string for later.  */
1162
1163   gfc_current_locus = start;    /* Back to the beginning */
1164
1165   new_st.loc = start;
1166   new_st.op = EXEC_NOP;
1167
1168   e = gfc_get_expr();
1169   e->expr_type = EXPR_CONSTANT;
1170   e->ts.type = BT_CHARACTER;
1171   e->ts.kind = gfc_default_character_kind;
1172   e->where = start;
1173   e->value.character.string = format_string
1174                             = gfc_get_wide_string (format_length + 1);
1175   e->value.character.length = format_length;
1176   gfc_statement_label->format = e;
1177
1178   mode = MODE_COPY;
1179   check_format (false);         /* Guaranteed to succeed */
1180   gfc_match_eos ();             /* Guaranteed to succeed */
1181
1182   return MATCH_YES;
1183 }
1184
1185
1186 /* Match an expression I/O tag of some sort.  */
1187
1188 static match
1189 match_etag (const io_tag *tag, gfc_expr **v)
1190 {
1191   gfc_expr *result;
1192   match m;
1193
1194   m = gfc_match (tag->spec);
1195   if (m != MATCH_YES)
1196     return m;
1197
1198   m = gfc_match (tag->value, &result);
1199   if (m != MATCH_YES)
1200     {
1201       gfc_error ("Invalid value for %s specification at %C", tag->name);
1202       return MATCH_ERROR;
1203     }
1204
1205   if (*v != NULL)
1206     {
1207       gfc_error ("Duplicate %s specification at %C", tag->name);
1208       gfc_free_expr (result);
1209       return MATCH_ERROR;
1210     }
1211
1212   *v = result;
1213   return MATCH_YES;
1214 }
1215
1216
1217 /* Match a variable I/O tag of some sort.  */
1218
1219 static match
1220 match_vtag (const io_tag *tag, gfc_expr **v)
1221 {
1222   gfc_expr *result;
1223   match m;
1224
1225   m = gfc_match (tag->spec);
1226   if (m != MATCH_YES)
1227     return m;
1228
1229   m = gfc_match (tag->value, &result);
1230   if (m != MATCH_YES)
1231     {
1232       gfc_error ("Invalid value for %s specification at %C", tag->name);
1233       return MATCH_ERROR;
1234     }
1235
1236   if (*v != NULL)
1237     {
1238       gfc_error ("Duplicate %s specification at %C", tag->name);
1239       gfc_free_expr (result);
1240       return MATCH_ERROR;
1241     }
1242
1243   if (result->symtree->n.sym->attr.intent == INTENT_IN)
1244     {
1245       gfc_error ("Variable %s cannot be INTENT(IN) at %C", tag->name);
1246       gfc_free_expr (result);
1247       return MATCH_ERROR;
1248     }
1249
1250   if (gfc_pure (NULL) && gfc_impure_variable (result->symtree->n.sym))
1251     {
1252       gfc_error ("Variable %s cannot be assigned in PURE procedure at %C",
1253                  tag->name);
1254       gfc_free_expr (result);
1255       return MATCH_ERROR;
1256     }
1257
1258   *v = result;
1259   return MATCH_YES;
1260 }
1261
1262
1263 /* Match I/O tags that cause variables to become redefined.  */
1264
1265 static match
1266 match_out_tag (const io_tag *tag, gfc_expr **result)
1267 {
1268   match m;
1269
1270   m = match_vtag (tag, result);
1271   if (m == MATCH_YES)
1272     gfc_check_do_variable ((*result)->symtree);
1273
1274   return m;
1275 }
1276
1277
1278 /* Match a label I/O tag.  */
1279
1280 static match
1281 match_ltag (const io_tag *tag, gfc_st_label ** label)
1282 {
1283   match m;
1284   gfc_st_label *old;
1285
1286   old = *label;
1287   m = gfc_match (tag->spec);
1288   if (m != MATCH_YES)
1289     return m;
1290
1291   m = gfc_match (tag->value, label);
1292   if (m != MATCH_YES)
1293     {
1294       gfc_error ("Invalid value for %s specification at %C", tag->name);
1295       return MATCH_ERROR;
1296     }
1297
1298   if (old)
1299     {
1300       gfc_error ("Duplicate %s label specification at %C", tag->name);
1301       return MATCH_ERROR;
1302     }
1303
1304   if (gfc_reference_st_label (*label, ST_LABEL_TARGET) == FAILURE)
1305     return MATCH_ERROR;
1306
1307   return m;
1308 }
1309
1310
1311 /* Resolution of the FORMAT tag, to be called from resolve_tag.  */
1312
1313 static gfc_try
1314 resolve_tag_format (const gfc_expr *e)
1315 {
1316   if (e->expr_type == EXPR_CONSTANT
1317       && (e->ts.type != BT_CHARACTER
1318           || e->ts.kind != gfc_default_character_kind))
1319     {
1320       gfc_error ("Constant expression in FORMAT tag at %L must be "
1321                  "of type default CHARACTER", &e->where);
1322       return FAILURE;
1323     }
1324
1325   /* If e's rank is zero and e is not an element of an array, it should be
1326      of integer or character type.  The integer variable should be
1327      ASSIGNED.  */
1328   if (e->rank == 0
1329       && (e->expr_type != EXPR_VARIABLE
1330           || e->symtree == NULL
1331           || e->symtree->n.sym->as == NULL
1332           || e->symtree->n.sym->as->rank == 0))
1333     {
1334       if (e->ts.type != BT_CHARACTER && e->ts.type != BT_INTEGER)
1335         {
1336           gfc_error ("FORMAT tag at %L must be of type CHARACTER or INTEGER",
1337                      &e->where);
1338           return FAILURE;
1339         }
1340       else if (e->ts.type == BT_INTEGER && e->expr_type == EXPR_VARIABLE)
1341         {
1342           if (gfc_notify_std (GFC_STD_F95_DEL, "Deleted feature: ASSIGNED "
1343                               "variable in FORMAT tag at %L", &e->where)
1344               == FAILURE)
1345             return FAILURE;
1346           if (e->symtree->n.sym->attr.assign != 1)
1347             {
1348               gfc_error ("Variable '%s' at %L has not been assigned a "
1349                          "format label", e->symtree->n.sym->name, &e->where);
1350               return FAILURE;
1351             }
1352         }
1353       else if (e->ts.type == BT_INTEGER)
1354         {
1355           gfc_error ("Scalar '%s' in FORMAT tag at %L is not an ASSIGNED "
1356                      "variable", gfc_basic_typename (e->ts.type), &e->where);
1357           return FAILURE;
1358         }
1359
1360       return SUCCESS;
1361     }
1362
1363   /* If rank is nonzero and type is not character, we allow it under GFC_STD_LEGACY.
1364      It may be assigned an Hollerith constant.  */
1365   if (e->ts.type != BT_CHARACTER)
1366     {
1367       if (gfc_notify_std (GFC_STD_LEGACY, "Extension: Non-character "
1368                           "in FORMAT tag at %L", &e->where) == FAILURE)
1369         return FAILURE;
1370
1371       if (e->rank == 0 && e->symtree->n.sym->as->type == AS_ASSUMED_SHAPE)
1372         {
1373           gfc_error ("Non-character assumed shape array element in FORMAT"
1374                      " tag at %L", &e->where);
1375           return FAILURE;
1376         }
1377
1378       if (e->rank == 0 && e->symtree->n.sym->as->type == AS_ASSUMED_SIZE)
1379         {
1380           gfc_error ("Non-character assumed size array element in FORMAT"
1381                      " tag at %L", &e->where);
1382           return FAILURE;
1383         }
1384
1385       if (e->rank == 0 && e->symtree->n.sym->attr.pointer)
1386         {
1387           gfc_error ("Non-character pointer array element in FORMAT tag at %L",
1388                      &e->where);
1389           return FAILURE;
1390         }
1391     }
1392
1393   return SUCCESS;
1394 }
1395
1396
1397 /* Do expression resolution and type-checking on an expression tag.  */
1398
1399 static gfc_try
1400 resolve_tag (const io_tag *tag, gfc_expr *e)
1401 {
1402   if (e == NULL)
1403     return SUCCESS;
1404
1405   if (gfc_resolve_expr (e) == FAILURE)
1406     return FAILURE;
1407
1408   if (tag == &tag_format)
1409     return resolve_tag_format (e);
1410
1411   if (e->ts.type != tag->type)
1412     {
1413       gfc_error ("%s tag at %L must be of type %s", tag->name,
1414                  &e->where, gfc_basic_typename (tag->type));
1415       return FAILURE;
1416     }
1417
1418   if (e->rank != 0)
1419     {
1420       gfc_error ("%s tag at %L must be scalar", tag->name, &e->where);
1421       return FAILURE;
1422     }
1423
1424   if (tag == &tag_iomsg)
1425     {
1426       if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IOMSG tag at %L",
1427                           &e->where) == FAILURE)
1428         return FAILURE;
1429     }
1430
1431   if ((tag == &tag_iostat || tag == &tag_size || tag == &tag_iolength)
1432       && e->ts.kind != gfc_default_integer_kind)
1433     {
1434       if (gfc_notify_std (GFC_STD_F2003, "Fortran 95 requires default "
1435                           "INTEGER in %s tag at %L", tag->name, &e->where)
1436           == FAILURE)
1437         return FAILURE;
1438     }
1439
1440   if (tag == &tag_convert)
1441     {
1442       if (gfc_notify_std (GFC_STD_GNU, "Extension: CONVERT tag at %L",
1443                           &e->where) == FAILURE)
1444         return FAILURE;
1445     }
1446   
1447   return SUCCESS;
1448 }
1449
1450
1451 /* Match a single tag of an OPEN statement.  */
1452
1453 static match
1454 match_open_element (gfc_open *open)
1455 {
1456   match m;
1457
1458   m = match_etag (&tag_e_async, &open->asynchronous);
1459   if (m != MATCH_NO)
1460     return m;
1461   m = match_etag (&tag_unit, &open->unit);
1462   if (m != MATCH_NO)
1463     return m;
1464   m = match_out_tag (&tag_iomsg, &open->iomsg);
1465   if (m != MATCH_NO)
1466     return m;
1467   m = match_out_tag (&tag_iostat, &open->iostat);
1468   if (m != MATCH_NO)
1469     return m;
1470   m = match_etag (&tag_file, &open->file);
1471   if (m != MATCH_NO)
1472     return m;
1473   m = match_etag (&tag_status, &open->status);
1474   if (m != MATCH_NO)
1475     return m;
1476   m = match_etag (&tag_e_access, &open->access);
1477   if (m != MATCH_NO)
1478     return m;
1479   m = match_etag (&tag_e_form, &open->form);
1480   if (m != MATCH_NO)
1481     return m;
1482   m = match_etag (&tag_e_recl, &open->recl);
1483   if (m != MATCH_NO)
1484     return m;
1485   m = match_etag (&tag_e_blank, &open->blank);
1486   if (m != MATCH_NO)
1487     return m;
1488   m = match_etag (&tag_e_position, &open->position);
1489   if (m != MATCH_NO)
1490     return m;
1491   m = match_etag (&tag_e_action, &open->action);
1492   if (m != MATCH_NO)
1493     return m;
1494   m = match_etag (&tag_e_delim, &open->delim);
1495   if (m != MATCH_NO)
1496     return m;
1497   m = match_etag (&tag_e_pad, &open->pad);
1498   if (m != MATCH_NO)
1499     return m;
1500   m = match_etag (&tag_e_decimal, &open->decimal);
1501   if (m != MATCH_NO)
1502     return m;
1503   m = match_etag (&tag_e_encoding, &open->encoding);
1504   if (m != MATCH_NO)
1505     return m;
1506   m = match_etag (&tag_e_round, &open->round);
1507   if (m != MATCH_NO)
1508     return m;
1509   m = match_etag (&tag_e_sign, &open->sign);
1510   if (m != MATCH_NO)
1511     return m;
1512   m = match_ltag (&tag_err, &open->err);
1513   if (m != MATCH_NO)
1514     return m;
1515   m = match_etag (&tag_convert, &open->convert);
1516   if (m != MATCH_NO)
1517     return m;
1518   m = match_out_tag (&tag_newunit, &open->newunit);
1519   if (m != MATCH_NO)
1520     return m;
1521
1522   return MATCH_NO;
1523 }
1524
1525
1526 /* Free the gfc_open structure and all the expressions it contains.  */
1527
1528 void
1529 gfc_free_open (gfc_open *open)
1530 {
1531   if (open == NULL)
1532     return;
1533
1534   gfc_free_expr (open->unit);
1535   gfc_free_expr (open->iomsg);
1536   gfc_free_expr (open->iostat);
1537   gfc_free_expr (open->file);
1538   gfc_free_expr (open->status);
1539   gfc_free_expr (open->access);
1540   gfc_free_expr (open->form);
1541   gfc_free_expr (open->recl);
1542   gfc_free_expr (open->blank);
1543   gfc_free_expr (open->position);
1544   gfc_free_expr (open->action);
1545   gfc_free_expr (open->delim);
1546   gfc_free_expr (open->pad);
1547   gfc_free_expr (open->decimal);
1548   gfc_free_expr (open->encoding);
1549   gfc_free_expr (open->round);
1550   gfc_free_expr (open->sign);
1551   gfc_free_expr (open->convert);
1552   gfc_free_expr (open->asynchronous);
1553   gfc_free_expr (open->newunit);
1554   gfc_free (open);
1555 }
1556
1557
1558 /* Resolve everything in a gfc_open structure.  */
1559
1560 gfc_try
1561 gfc_resolve_open (gfc_open *open)
1562 {
1563
1564   RESOLVE_TAG (&tag_unit, open->unit);
1565   RESOLVE_TAG (&tag_iomsg, open->iomsg);
1566   RESOLVE_TAG (&tag_iostat, open->iostat);
1567   RESOLVE_TAG (&tag_file, open->file);
1568   RESOLVE_TAG (&tag_status, open->status);
1569   RESOLVE_TAG (&tag_e_access, open->access);
1570   RESOLVE_TAG (&tag_e_form, open->form);
1571   RESOLVE_TAG (&tag_e_recl, open->recl);
1572   RESOLVE_TAG (&tag_e_blank, open->blank);
1573   RESOLVE_TAG (&tag_e_position, open->position);
1574   RESOLVE_TAG (&tag_e_action, open->action);
1575   RESOLVE_TAG (&tag_e_delim, open->delim);
1576   RESOLVE_TAG (&tag_e_pad, open->pad);
1577   RESOLVE_TAG (&tag_e_decimal, open->decimal);
1578   RESOLVE_TAG (&tag_e_encoding, open->encoding);
1579   RESOLVE_TAG (&tag_e_async, open->asynchronous);
1580   RESOLVE_TAG (&tag_e_round, open->round);
1581   RESOLVE_TAG (&tag_e_sign, open->sign);
1582   RESOLVE_TAG (&tag_convert, open->convert);
1583   RESOLVE_TAG (&tag_newunit, open->newunit);
1584
1585   if (gfc_reference_st_label (open->err, ST_LABEL_TARGET) == FAILURE)
1586     return FAILURE;
1587
1588   return SUCCESS;
1589 }
1590
1591
1592 /* Check if a given value for a SPECIFIER is either in the list of values
1593    allowed in F95 or F2003, issuing an error message and returning a zero
1594    value if it is not allowed.  */
1595
1596 static int
1597 compare_to_allowed_values (const char *specifier, const char *allowed[],
1598                            const char *allowed_f2003[], 
1599                            const char *allowed_gnu[], gfc_char_t *value,
1600                            const char *statement, bool warn)
1601 {
1602   int i;
1603   unsigned int len;
1604
1605   len = gfc_wide_strlen (value);
1606   if (len > 0)
1607   {
1608     for (len--; len > 0; len--)
1609       if (value[len] != ' ')
1610         break;
1611     len++;
1612   }
1613
1614   for (i = 0; allowed[i]; i++)
1615     if (len == strlen (allowed[i])
1616         && gfc_wide_strncasecmp (value, allowed[i], strlen (allowed[i])) == 0)
1617       return 1;
1618
1619   for (i = 0; allowed_f2003 && allowed_f2003[i]; i++)
1620     if (len == strlen (allowed_f2003[i])
1621         && gfc_wide_strncasecmp (value, allowed_f2003[i],
1622                                  strlen (allowed_f2003[i])) == 0)
1623       {
1624         notification n = gfc_notification_std (GFC_STD_F2003);
1625
1626         if (n == WARNING || (warn && n == ERROR))
1627           {
1628             gfc_warning ("Fortran 2003: %s specifier in %s statement at %C "
1629                          "has value '%s'", specifier, statement,
1630                          allowed_f2003[i]);
1631             return 1;
1632           }
1633         else
1634           if (n == ERROR)
1635             {
1636               gfc_notify_std (GFC_STD_F2003, "Fortran 2003: %s specifier in "
1637                               "%s statement at %C has value '%s'", specifier,
1638                               statement, allowed_f2003[i]);
1639               return 0;
1640             }
1641
1642         /* n == SILENT */
1643         return 1;
1644       }
1645
1646   for (i = 0; allowed_gnu && allowed_gnu[i]; i++)
1647     if (len == strlen (allowed_gnu[i])
1648         && gfc_wide_strncasecmp (value, allowed_gnu[i],
1649                                  strlen (allowed_gnu[i])) == 0)
1650       {
1651         notification n = gfc_notification_std (GFC_STD_GNU);
1652
1653         if (n == WARNING || (warn && n == ERROR))
1654           {
1655             gfc_warning ("Extension: %s specifier in %s statement at %C "
1656                          "has value '%s'", specifier, statement,
1657                          allowed_gnu[i]);
1658             return 1;
1659           }
1660         else
1661           if (n == ERROR)
1662             {
1663               gfc_notify_std (GFC_STD_GNU, "Extension: %s specifier in "
1664                               "%s statement at %C has value '%s'", specifier,
1665                               statement, allowed_gnu[i]);
1666               return 0;
1667             }
1668
1669         /* n == SILENT */
1670         return 1;
1671       }
1672
1673   if (warn)
1674     {
1675       char *s = gfc_widechar_to_char (value, -1);
1676       gfc_warning ("%s specifier in %s statement at %C has invalid value '%s'",
1677                    specifier, statement, s);
1678       gfc_free (s);
1679       return 1;
1680     }
1681   else
1682     {
1683       char *s = gfc_widechar_to_char (value, -1);
1684       gfc_error ("%s specifier in %s statement at %C has invalid value '%s'",
1685                  specifier, statement, s);
1686       gfc_free (s);
1687       return 0;
1688     }
1689 }
1690
1691
1692 /* Match an OPEN statement.  */
1693
1694 match
1695 gfc_match_open (void)
1696 {
1697   gfc_open *open;
1698   match m;
1699   bool warn;
1700
1701   m = gfc_match_char ('(');
1702   if (m == MATCH_NO)
1703     return m;
1704
1705   open = XCNEW (gfc_open);
1706
1707   m = match_open_element (open);
1708
1709   if (m == MATCH_ERROR)
1710     goto cleanup;
1711   if (m == MATCH_NO)
1712     {
1713       m = gfc_match_expr (&open->unit);
1714       if (m == MATCH_NO)
1715         goto syntax;
1716       if (m == MATCH_ERROR)
1717         goto cleanup;
1718     }
1719
1720   for (;;)
1721     {
1722       if (gfc_match_char (')') == MATCH_YES)
1723         break;
1724       if (gfc_match_char (',') != MATCH_YES)
1725         goto syntax;
1726
1727       m = match_open_element (open);
1728       if (m == MATCH_ERROR)
1729         goto cleanup;
1730       if (m == MATCH_NO)
1731         goto syntax;
1732     }
1733
1734   if (gfc_match_eos () == MATCH_NO)
1735     goto syntax;
1736
1737   if (gfc_pure (NULL))
1738     {
1739       gfc_error ("OPEN statement not allowed in PURE procedure at %C");
1740       goto cleanup;
1741     }
1742
1743   warn = (open->err || open->iostat) ? true : false;
1744
1745   /* Checks on NEWUNIT specifier.  */
1746   if (open->newunit)
1747     {
1748       if (open->unit)
1749         {
1750           gfc_error ("UNIT specifier not allowed with NEWUNIT at %C");
1751           goto cleanup;
1752         }
1753
1754       if (!(open->file || (open->status
1755           && gfc_wide_strncasecmp (open->status->value.character.string,
1756                                    "scratch", 7) == 0)))
1757         {
1758           gfc_error ("NEWUNIT specifier must have FILE= "
1759                      "or STATUS='scratch' at %C");
1760           goto cleanup;
1761         }
1762     }
1763
1764   /* Checks on the ACCESS specifier.  */
1765   if (open->access && open->access->expr_type == EXPR_CONSTANT)
1766     {
1767       static const char *access_f95[] = { "SEQUENTIAL", "DIRECT", NULL };
1768       static const char *access_f2003[] = { "STREAM", NULL };
1769       static const char *access_gnu[] = { "APPEND", NULL };
1770
1771       if (!compare_to_allowed_values ("ACCESS", access_f95, access_f2003,
1772                                       access_gnu,
1773                                       open->access->value.character.string,
1774                                       "OPEN", warn))
1775         goto cleanup;
1776     }
1777
1778   /* Checks on the ACTION specifier.  */
1779   if (open->action && open->action->expr_type == EXPR_CONSTANT)
1780     {
1781       static const char *action[] = { "READ", "WRITE", "READWRITE", NULL };
1782
1783       if (!compare_to_allowed_values ("ACTION", action, NULL, NULL,
1784                                       open->action->value.character.string,
1785                                       "OPEN", warn))
1786         goto cleanup;
1787     }
1788
1789   /* Checks on the ASYNCHRONOUS specifier.  */
1790   if (open->asynchronous)
1791     {
1792       if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASYNCHRONOUS= at %C "
1793           "not allowed in Fortran 95") == FAILURE)
1794         goto cleanup;
1795
1796       if (open->asynchronous->expr_type == EXPR_CONSTANT)
1797         {
1798           static const char * asynchronous[] = { "YES", "NO", NULL };
1799
1800           if (!compare_to_allowed_values ("ASYNCHRONOUS", asynchronous,
1801                         NULL, NULL, open->asynchronous->value.character.string,
1802                         "OPEN", warn))
1803             goto cleanup;
1804         }
1805     }
1806
1807   /* Checks on the BLANK specifier.  */
1808   if (open->blank)
1809     {
1810       if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: BLANK= at %C "
1811           "not allowed in Fortran 95") == FAILURE)
1812         goto cleanup;
1813
1814       if (open->blank->expr_type == EXPR_CONSTANT)
1815         {
1816           static const char *blank[] = { "ZERO", "NULL", NULL };
1817
1818           if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
1819                                           open->blank->value.character.string,
1820                                           "OPEN", warn))
1821             goto cleanup;
1822         }
1823     }
1824
1825   /* Checks on the DECIMAL specifier.  */
1826   if (open->decimal)
1827     {
1828       if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DECIMAL= at %C "
1829           "not allowed in Fortran 95") == FAILURE)
1830         goto cleanup;
1831
1832       if (open->decimal->expr_type == EXPR_CONSTANT)
1833         {
1834           static const char * decimal[] = { "COMMA", "POINT", NULL };
1835
1836           if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
1837                                           open->decimal->value.character.string,
1838                                           "OPEN", warn))
1839             goto cleanup;
1840         }
1841     }
1842
1843   /* Checks on the DELIM specifier.  */
1844   if (open->delim)
1845     {
1846       if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DELIM= at %C "
1847           "not allowed in Fortran 95") == FAILURE)
1848         goto cleanup;
1849
1850       if (open->delim->expr_type == EXPR_CONSTANT)
1851         {
1852           static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
1853
1854           if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
1855                                           open->delim->value.character.string,
1856                                           "OPEN", warn))
1857           goto cleanup;
1858         }
1859     }
1860
1861   /* Checks on the ENCODING specifier.  */
1862   if (open->encoding)
1863     {
1864       if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENCODING= at %C "
1865           "not allowed in Fortran 95") == FAILURE)
1866         goto cleanup;
1867     
1868       if (open->encoding->expr_type == EXPR_CONSTANT)
1869         {
1870           static const char * encoding[] = { "DEFAULT", "UTF-8", NULL };
1871
1872           if (!compare_to_allowed_values ("ENCODING", encoding, NULL, NULL,
1873                                           open->encoding->value.character.string,
1874                                           "OPEN", warn))
1875           goto cleanup;
1876         }
1877     }
1878
1879   /* Checks on the FORM specifier.  */
1880   if (open->form && open->form->expr_type == EXPR_CONSTANT)
1881     {
1882       static const char *form[] = { "FORMATTED", "UNFORMATTED", NULL };
1883
1884       if (!compare_to_allowed_values ("FORM", form, NULL, NULL,
1885                                       open->form->value.character.string,
1886                                       "OPEN", warn))
1887         goto cleanup;
1888     }
1889
1890   /* Checks on the PAD specifier.  */
1891   if (open->pad && open->pad->expr_type == EXPR_CONSTANT)
1892     {
1893       static const char *pad[] = { "YES", "NO", NULL };
1894
1895       if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
1896                                       open->pad->value.character.string,
1897                                       "OPEN", warn))
1898         goto cleanup;
1899     }
1900
1901   /* Checks on the POSITION specifier.  */
1902   if (open->position && open->position->expr_type == EXPR_CONSTANT)
1903     {
1904       static const char *position[] = { "ASIS", "REWIND", "APPEND", NULL };
1905
1906       if (!compare_to_allowed_values ("POSITION", position, NULL, NULL,
1907                                       open->position->value.character.string,
1908                                       "OPEN", warn))
1909         goto cleanup;
1910     }
1911
1912   /* Checks on the ROUND specifier.  */
1913   if (open->round)
1914     {
1915       /* When implemented, change the following to use gfc_notify_std F2003.  */
1916       gfc_error ("Fortran F2003: ROUND= specifier at %C not implemented");
1917       goto cleanup;
1918
1919       if (open->round->expr_type == EXPR_CONSTANT)
1920         {
1921           static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
1922                                           "COMPATIBLE", "PROCESSOR_DEFINED",
1923                                            NULL };
1924
1925           if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
1926                                           open->round->value.character.string,
1927                                           "OPEN", warn))
1928           goto cleanup;
1929         }
1930     }
1931
1932   /* Checks on the SIGN specifier.  */
1933   if (open->sign) 
1934     {
1935       if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: SIGN= at %C "
1936           "not allowed in Fortran 95") == FAILURE)
1937         goto cleanup;
1938
1939       if (open->sign->expr_type == EXPR_CONSTANT)
1940         {
1941           static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
1942                                           NULL };
1943
1944           if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
1945                                           open->sign->value.character.string,
1946                                           "OPEN", warn))
1947           goto cleanup;
1948         }
1949     }
1950
1951 #define warn_or_error(...) \
1952 { \
1953   if (warn) \
1954     gfc_warning (__VA_ARGS__); \
1955   else \
1956     { \
1957       gfc_error (__VA_ARGS__); \
1958       goto cleanup; \
1959     } \
1960 }
1961
1962   /* Checks on the RECL specifier.  */
1963   if (open->recl && open->recl->expr_type == EXPR_CONSTANT
1964       && open->recl->ts.type == BT_INTEGER
1965       && mpz_sgn (open->recl->value.integer) != 1)
1966     {
1967       warn_or_error ("RECL in OPEN statement at %C must be positive");
1968     }
1969
1970   /* Checks on the STATUS specifier.  */
1971   if (open->status && open->status->expr_type == EXPR_CONSTANT)
1972     {
1973       static const char *status[] = { "OLD", "NEW", "SCRATCH",
1974         "REPLACE", "UNKNOWN", NULL };
1975
1976       if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
1977                                       open->status->value.character.string,
1978                                       "OPEN", warn))
1979         goto cleanup;
1980
1981       /* F2003, 9.4.5: If the STATUS= specifier has the value NEW or REPLACE,
1982          the FILE= specifier shall appear.  */
1983       if (open->file == NULL
1984           && (gfc_wide_strncasecmp (open->status->value.character.string,
1985                                     "replace", 7) == 0
1986               || gfc_wide_strncasecmp (open->status->value.character.string,
1987                                        "new", 3) == 0))
1988         {
1989           char *s = gfc_widechar_to_char (open->status->value.character.string,
1990                                           -1);
1991           warn_or_error ("The STATUS specified in OPEN statement at %C is "
1992                          "'%s' and no FILE specifier is present", s);
1993           gfc_free (s);
1994         }
1995
1996       /* F2003, 9.4.5: If the STATUS= specifier has the value SCRATCH,
1997          the FILE= specifier shall not appear.  */
1998       if (gfc_wide_strncasecmp (open->status->value.character.string,
1999                                 "scratch", 7) == 0 && open->file)
2000         {
2001           warn_or_error ("The STATUS specified in OPEN statement at %C "
2002                          "cannot have the value SCRATCH if a FILE specifier "
2003                          "is present");
2004         }
2005     }
2006
2007   /* Things that are not allowed for unformatted I/O.  */
2008   if (open->form && open->form->expr_type == EXPR_CONSTANT
2009       && (open->delim || open->decimal || open->encoding || open->round
2010           || open->sign || open->pad || open->blank)
2011       && gfc_wide_strncasecmp (open->form->value.character.string,
2012                                "unformatted", 11) == 0)
2013     {
2014       const char *spec = (open->delim ? "DELIM "
2015                                       : (open->pad ? "PAD " : open->blank
2016                                                             ? "BLANK " : ""));
2017
2018       warn_or_error ("%s specifier at %C not allowed in OPEN statement for "
2019                      "unformatted I/O", spec);
2020     }
2021
2022   if (open->recl && open->access && open->access->expr_type == EXPR_CONSTANT
2023       && gfc_wide_strncasecmp (open->access->value.character.string,
2024                                "stream", 6) == 0)
2025     {
2026       warn_or_error ("RECL specifier not allowed in OPEN statement at %C for "
2027                      "stream I/O");
2028     }
2029
2030   if (open->position
2031       && open->access && open->access->expr_type == EXPR_CONSTANT
2032       && !(gfc_wide_strncasecmp (open->access->value.character.string,
2033                                  "sequential", 10) == 0
2034            || gfc_wide_strncasecmp (open->access->value.character.string,
2035                                     "stream", 6) == 0
2036            || gfc_wide_strncasecmp (open->access->value.character.string,
2037                                     "append", 6) == 0))
2038     {
2039       warn_or_error ("POSITION specifier in OPEN statement at %C only allowed "
2040                      "for stream or sequential ACCESS");
2041     }
2042
2043 #undef warn_or_error
2044
2045   new_st.op = EXEC_OPEN;
2046   new_st.ext.open = open;
2047   return MATCH_YES;
2048
2049 syntax:
2050   gfc_syntax_error (ST_OPEN);
2051
2052 cleanup:
2053   gfc_free_open (open);
2054   return MATCH_ERROR;
2055 }
2056
2057
2058 /* Free a gfc_close structure an all its expressions.  */
2059
2060 void
2061 gfc_free_close (gfc_close *close)
2062 {
2063   if (close == NULL)
2064     return;
2065
2066   gfc_free_expr (close->unit);
2067   gfc_free_expr (close->iomsg);
2068   gfc_free_expr (close->iostat);
2069   gfc_free_expr (close->status);
2070   gfc_free (close);
2071 }
2072
2073
2074 /* Match elements of a CLOSE statement.  */
2075
2076 static match
2077 match_close_element (gfc_close *close)
2078 {
2079   match m;
2080
2081   m = match_etag (&tag_unit, &close->unit);
2082   if (m != MATCH_NO)
2083     return m;
2084   m = match_etag (&tag_status, &close->status);
2085   if (m != MATCH_NO)
2086     return m;
2087   m = match_out_tag (&tag_iomsg, &close->iomsg);
2088   if (m != MATCH_NO)
2089     return m;
2090   m = match_out_tag (&tag_iostat, &close->iostat);
2091   if (m != MATCH_NO)
2092     return m;
2093   m = match_ltag (&tag_err, &close->err);
2094   if (m != MATCH_NO)
2095     return m;
2096
2097   return MATCH_NO;
2098 }
2099
2100
2101 /* Match a CLOSE statement.  */
2102
2103 match
2104 gfc_match_close (void)
2105 {
2106   gfc_close *close;
2107   match m;
2108   bool warn;
2109
2110   m = gfc_match_char ('(');
2111   if (m == MATCH_NO)
2112     return m;
2113
2114   close = XCNEW (gfc_close);
2115
2116   m = match_close_element (close);
2117
2118   if (m == MATCH_ERROR)
2119     goto cleanup;
2120   if (m == MATCH_NO)
2121     {
2122       m = gfc_match_expr (&close->unit);
2123       if (m == MATCH_NO)
2124         goto syntax;
2125       if (m == MATCH_ERROR)
2126         goto cleanup;
2127     }
2128
2129   for (;;)
2130     {
2131       if (gfc_match_char (')') == MATCH_YES)
2132         break;
2133       if (gfc_match_char (',') != MATCH_YES)
2134         goto syntax;
2135
2136       m = match_close_element (close);
2137       if (m == MATCH_ERROR)
2138         goto cleanup;
2139       if (m == MATCH_NO)
2140         goto syntax;
2141     }
2142
2143   if (gfc_match_eos () == MATCH_NO)
2144     goto syntax;
2145
2146   if (gfc_pure (NULL))
2147     {
2148       gfc_error ("CLOSE statement not allowed in PURE procedure at %C");
2149       goto cleanup;
2150     }
2151
2152   warn = (close->iostat || close->err) ? true : false;
2153
2154   /* Checks on the STATUS specifier.  */
2155   if (close->status && close->status->expr_type == EXPR_CONSTANT)
2156     {
2157       static const char *status[] = { "KEEP", "DELETE", NULL };
2158
2159       if (!compare_to_allowed_values ("STATUS", status, NULL, NULL,
2160                                       close->status->value.character.string,
2161                                       "CLOSE", warn))
2162         goto cleanup;
2163     }
2164
2165   new_st.op = EXEC_CLOSE;
2166   new_st.ext.close = close;
2167   return MATCH_YES;
2168
2169 syntax:
2170   gfc_syntax_error (ST_CLOSE);
2171
2172 cleanup:
2173   gfc_free_close (close);
2174   return MATCH_ERROR;
2175 }
2176
2177
2178 /* Resolve everything in a gfc_close structure.  */
2179
2180 gfc_try
2181 gfc_resolve_close (gfc_close *close)
2182 {
2183   RESOLVE_TAG (&tag_unit, close->unit);
2184   RESOLVE_TAG (&tag_iomsg, close->iomsg);
2185   RESOLVE_TAG (&tag_iostat, close->iostat);
2186   RESOLVE_TAG (&tag_status, close->status);
2187
2188   if (gfc_reference_st_label (close->err, ST_LABEL_TARGET) == FAILURE)
2189     return FAILURE;
2190
2191   if (close->unit->expr_type == EXPR_CONSTANT
2192       && close->unit->ts.type == BT_INTEGER
2193       && mpz_sgn (close->unit->value.integer) < 0)
2194     {
2195       gfc_error ("UNIT number in CLOSE statement at %L must be non-negative",
2196                  &close->unit->where);
2197     }
2198
2199   return SUCCESS;
2200 }
2201
2202
2203 /* Free a gfc_filepos structure.  */
2204
2205 void
2206 gfc_free_filepos (gfc_filepos *fp)
2207 {
2208   gfc_free_expr (fp->unit);
2209   gfc_free_expr (fp->iomsg);
2210   gfc_free_expr (fp->iostat);
2211   gfc_free (fp);
2212 }
2213
2214
2215 /* Match elements of a REWIND, BACKSPACE, ENDFILE, or FLUSH statement.  */
2216
2217 static match
2218 match_file_element (gfc_filepos *fp)
2219 {
2220   match m;
2221
2222   m = match_etag (&tag_unit, &fp->unit);
2223   if (m != MATCH_NO)
2224     return m;
2225   m = match_out_tag (&tag_iomsg, &fp->iomsg);
2226   if (m != MATCH_NO)
2227     return m;
2228   m = match_out_tag (&tag_iostat, &fp->iostat);
2229   if (m != MATCH_NO)
2230     return m;
2231   m = match_ltag (&tag_err, &fp->err);
2232   if (m != MATCH_NO)
2233     return m;
2234
2235   return MATCH_NO;
2236 }
2237
2238
2239 /* Match the second half of the file-positioning statements, REWIND,
2240    BACKSPACE, ENDFILE, or the FLUSH statement.  */
2241
2242 static match
2243 match_filepos (gfc_statement st, gfc_exec_op op)
2244 {
2245   gfc_filepos *fp;
2246   match m;
2247
2248   fp = XCNEW (gfc_filepos);
2249
2250   if (gfc_match_char ('(') == MATCH_NO)
2251     {
2252       m = gfc_match_expr (&fp->unit);
2253       if (m == MATCH_ERROR)
2254         goto cleanup;
2255       if (m == MATCH_NO)
2256         goto syntax;
2257
2258       goto done;
2259     }
2260
2261   m = match_file_element (fp);
2262   if (m == MATCH_ERROR)
2263     goto done;
2264   if (m == MATCH_NO)
2265     {
2266       m = gfc_match_expr (&fp->unit);
2267       if (m == MATCH_ERROR)
2268         goto done;
2269       if (m == MATCH_NO)
2270         goto syntax;
2271     }
2272
2273   for (;;)
2274     {
2275       if (gfc_match_char (')') == MATCH_YES)
2276         break;
2277       if (gfc_match_char (',') != MATCH_YES)
2278         goto syntax;
2279
2280       m = match_file_element (fp);
2281       if (m == MATCH_ERROR)
2282         goto cleanup;
2283       if (m == MATCH_NO)
2284         goto syntax;
2285     }
2286
2287 done:
2288   if (gfc_match_eos () != MATCH_YES)
2289     goto syntax;
2290
2291   if (gfc_pure (NULL))
2292     {
2293       gfc_error ("%s statement not allowed in PURE procedure at %C",
2294                  gfc_ascii_statement (st));
2295
2296       goto cleanup;
2297     }
2298
2299   new_st.op = op;
2300   new_st.ext.filepos = fp;
2301   return MATCH_YES;
2302
2303 syntax:
2304   gfc_syntax_error (st);
2305
2306 cleanup:
2307   gfc_free_filepos (fp);
2308   return MATCH_ERROR;
2309 }
2310
2311
2312 gfc_try
2313 gfc_resolve_filepos (gfc_filepos *fp)
2314 {
2315   RESOLVE_TAG (&tag_unit, fp->unit);
2316   RESOLVE_TAG (&tag_iostat, fp->iostat);
2317   RESOLVE_TAG (&tag_iomsg, fp->iomsg);
2318   if (gfc_reference_st_label (fp->err, ST_LABEL_TARGET) == FAILURE)
2319     return FAILURE;
2320
2321   if (fp->unit->expr_type == EXPR_CONSTANT
2322       && fp->unit->ts.type == BT_INTEGER
2323       && mpz_sgn (fp->unit->value.integer) < 0)
2324     {
2325       gfc_error ("UNIT number in statement at %L must be non-negative",
2326                  &fp->unit->where);
2327     }
2328
2329   return SUCCESS;
2330 }
2331
2332
2333 /* Match the file positioning statements: ENDFILE, BACKSPACE, REWIND,
2334    and the FLUSH statement.  */
2335
2336 match
2337 gfc_match_endfile (void)
2338 {
2339   return match_filepos (ST_END_FILE, EXEC_ENDFILE);
2340 }
2341
2342 match
2343 gfc_match_backspace (void)
2344 {
2345   return match_filepos (ST_BACKSPACE, EXEC_BACKSPACE);
2346 }
2347
2348 match
2349 gfc_match_rewind (void)
2350 {
2351   return match_filepos (ST_REWIND, EXEC_REWIND);
2352 }
2353
2354 match
2355 gfc_match_flush (void)
2356 {
2357   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: FLUSH statement at %C")
2358       == FAILURE)
2359     return MATCH_ERROR;
2360
2361   return match_filepos (ST_FLUSH, EXEC_FLUSH);
2362 }
2363
2364 /******************** Data Transfer Statements *********************/
2365
2366 /* Return a default unit number.  */
2367
2368 static gfc_expr *
2369 default_unit (io_kind k)
2370 {
2371   int unit;
2372
2373   if (k == M_READ)
2374     unit = 5;
2375   else
2376     unit = 6;
2377
2378   return gfc_int_expr (unit);
2379 }
2380
2381
2382 /* Match a unit specification for a data transfer statement.  */
2383
2384 static match
2385 match_dt_unit (io_kind k, gfc_dt *dt)
2386 {
2387   gfc_expr *e;
2388
2389   if (gfc_match_char ('*') == MATCH_YES)
2390     {
2391       if (dt->io_unit != NULL)
2392         goto conflict;
2393
2394       dt->io_unit = default_unit (k);
2395       return MATCH_YES;
2396     }
2397
2398   if (gfc_match_expr (&e) == MATCH_YES)
2399     {
2400       if (dt->io_unit != NULL)
2401         {
2402           gfc_free_expr (e);
2403           goto conflict;
2404         }
2405
2406       dt->io_unit = e;
2407       return MATCH_YES;
2408     }
2409
2410   return MATCH_NO;
2411
2412 conflict:
2413   gfc_error ("Duplicate UNIT specification at %C");
2414   return MATCH_ERROR;
2415 }
2416
2417
2418 /* Match a format specification.  */
2419
2420 static match
2421 match_dt_format (gfc_dt *dt)
2422 {
2423   locus where;
2424   gfc_expr *e;
2425   gfc_st_label *label;
2426   match m;
2427
2428   where = gfc_current_locus;
2429
2430   if (gfc_match_char ('*') == MATCH_YES)
2431     {
2432       if (dt->format_expr != NULL || dt->format_label != NULL)
2433         goto conflict;
2434
2435       dt->format_label = &format_asterisk;
2436       return MATCH_YES;
2437     }
2438
2439   if ((m = gfc_match_st_label (&label)) == MATCH_YES)
2440     {
2441       if (dt->format_expr != NULL || dt->format_label != NULL)
2442         {
2443           gfc_free_st_label (label);
2444           goto conflict;
2445         }
2446
2447       if (gfc_reference_st_label (label, ST_LABEL_FORMAT) == FAILURE)
2448         return MATCH_ERROR;
2449
2450       dt->format_label = label;
2451       return MATCH_YES;
2452     }
2453   else if (m == MATCH_ERROR)
2454     /* The label was zero or too large.  Emit the correct diagnosis.  */
2455     return MATCH_ERROR;
2456
2457   if (gfc_match_expr (&e) == MATCH_YES)
2458     {
2459       if (dt->format_expr != NULL || dt->format_label != NULL)
2460         {
2461           gfc_free_expr (e);
2462           goto conflict;
2463         }
2464       dt->format_expr = e;
2465       return MATCH_YES;
2466     }
2467
2468   gfc_current_locus = where;    /* The only case where we have to restore */
2469
2470   return MATCH_NO;
2471
2472 conflict:
2473   gfc_error ("Duplicate format specification at %C");
2474   return MATCH_ERROR;
2475 }
2476
2477
2478 /* Traverse a namelist that is part of a READ statement to make sure
2479    that none of the variables in the namelist are INTENT(IN).  Returns
2480    nonzero if we find such a variable.  */
2481
2482 static int
2483 check_namelist (gfc_symbol *sym)
2484 {
2485   gfc_namelist *p;
2486
2487   for (p = sym->namelist; p; p = p->next)
2488     if (p->sym->attr.intent == INTENT_IN)
2489       {
2490         gfc_error ("Symbol '%s' in namelist '%s' is INTENT(IN) at %C",
2491                    p->sym->name, sym->name);
2492         return 1;
2493       }
2494
2495   return 0;
2496 }
2497
2498
2499 /* Match a single data transfer element.  */
2500
2501 static match
2502 match_dt_element (io_kind k, gfc_dt *dt)
2503 {
2504   char name[GFC_MAX_SYMBOL_LEN + 1];
2505   gfc_symbol *sym;
2506   match m;
2507
2508   if (gfc_match (" unit =") == MATCH_YES)
2509     {
2510       m = match_dt_unit (k, dt);
2511       if (m != MATCH_NO)
2512         return m;
2513     }
2514
2515   if (gfc_match (" fmt =") == MATCH_YES)
2516     {
2517       m = match_dt_format (dt);
2518       if (m != MATCH_NO)
2519         return m;
2520     }
2521
2522   if (gfc_match (" nml = %n", name) == MATCH_YES)
2523     {
2524       if (dt->namelist != NULL)
2525         {
2526           gfc_error ("Duplicate NML specification at %C");
2527           return MATCH_ERROR;
2528         }
2529
2530       if (gfc_find_symbol (name, NULL, 1, &sym))
2531         return MATCH_ERROR;
2532
2533       if (sym == NULL || sym->attr.flavor != FL_NAMELIST)
2534         {
2535           gfc_error ("Symbol '%s' at %C must be a NAMELIST group name",
2536                      sym != NULL ? sym->name : name);
2537           return MATCH_ERROR;
2538         }
2539
2540       dt->namelist = sym;
2541       if (k == M_READ && check_namelist (sym))
2542         return MATCH_ERROR;
2543
2544       return MATCH_YES;
2545     }
2546
2547   m = match_etag (&tag_e_async, &dt->asynchronous);
2548   if (m != MATCH_NO)
2549     return m;
2550   m = match_etag (&tag_e_blank, &dt->blank);
2551   if (m != MATCH_NO)
2552     return m;
2553   m = match_etag (&tag_e_delim, &dt->delim);
2554   if (m != MATCH_NO)
2555     return m;
2556   m = match_etag (&tag_e_pad, &dt->pad);
2557   if (m != MATCH_NO)
2558     return m;
2559   m = match_etag (&tag_e_sign, &dt->sign);
2560   if (m != MATCH_NO)
2561     return m;
2562   m = match_etag (&tag_e_round, &dt->round);
2563   if (m != MATCH_NO)
2564     return m;
2565   m = match_out_tag (&tag_id, &dt->id);
2566   if (m != MATCH_NO)
2567     return m;
2568   m = match_etag (&tag_e_decimal, &dt->decimal);
2569   if (m != MATCH_NO)
2570     return m;
2571   m = match_etag (&tag_rec, &dt->rec);
2572   if (m != MATCH_NO)
2573     return m;
2574   m = match_etag (&tag_spos, &dt->pos);
2575   if (m != MATCH_NO)
2576     return m;
2577   m = match_out_tag (&tag_iomsg, &dt->iomsg);
2578   if (m != MATCH_NO)
2579     return m;
2580   m = match_out_tag (&tag_iostat, &dt->iostat);
2581   if (m != MATCH_NO)
2582     return m;
2583   m = match_ltag (&tag_err, &dt->err);
2584   if (m == MATCH_YES)
2585     dt->err_where = gfc_current_locus;
2586   if (m != MATCH_NO)
2587     return m;
2588   m = match_etag (&tag_advance, &dt->advance);
2589   if (m != MATCH_NO)
2590     return m;
2591   m = match_out_tag (&tag_size, &dt->size);
2592   if (m != MATCH_NO)
2593     return m;
2594
2595   m = match_ltag (&tag_end, &dt->end);
2596   if (m == MATCH_YES)
2597     {
2598       if (k == M_WRITE)
2599        {
2600          gfc_error ("END tag at %C not allowed in output statement");
2601          return MATCH_ERROR;
2602        }
2603       dt->end_where = gfc_current_locus;
2604     }
2605   if (m != MATCH_NO)
2606     return m;
2607
2608   m = match_ltag (&tag_eor, &dt->eor);
2609   if (m == MATCH_YES)
2610     dt->eor_where = gfc_current_locus;
2611   if (m != MATCH_NO)
2612     return m;
2613
2614   return MATCH_NO;
2615 }
2616
2617
2618 /* Free a data transfer structure and everything below it.  */
2619
2620 void
2621 gfc_free_dt (gfc_dt *dt)
2622 {
2623   if (dt == NULL)
2624     return;
2625
2626   gfc_free_expr (dt->io_unit);
2627   gfc_free_expr (dt->format_expr);
2628   gfc_free_expr (dt->rec);
2629   gfc_free_expr (dt->advance);
2630   gfc_free_expr (dt->iomsg);
2631   gfc_free_expr (dt->iostat);
2632   gfc_free_expr (dt->size);
2633   gfc_free_expr (dt->pad);
2634   gfc_free_expr (dt->delim);
2635   gfc_free_expr (dt->sign);
2636   gfc_free_expr (dt->round);
2637   gfc_free_expr (dt->blank);
2638   gfc_free_expr (dt->decimal);
2639   gfc_free_expr (dt->extra_comma);
2640   gfc_free_expr (dt->pos);
2641   gfc_free (dt);
2642 }
2643
2644
2645 /* Resolve everything in a gfc_dt structure.  */
2646
2647 gfc_try
2648 gfc_resolve_dt (gfc_dt *dt, locus *loc)
2649 {
2650   gfc_expr *e;
2651
2652   RESOLVE_TAG (&tag_format, dt->format_expr);
2653   RESOLVE_TAG (&tag_rec, dt->rec);
2654   RESOLVE_TAG (&tag_spos, dt->pos);
2655   RESOLVE_TAG (&tag_advance, dt->advance);
2656   RESOLVE_TAG (&tag_id, dt->id);
2657   RESOLVE_TAG (&tag_iomsg, dt->iomsg);
2658   RESOLVE_TAG (&tag_iostat, dt->iostat);
2659   RESOLVE_TAG (&tag_size, dt->size);
2660   RESOLVE_TAG (&tag_e_pad, dt->pad);
2661   RESOLVE_TAG (&tag_e_delim, dt->delim);
2662   RESOLVE_TAG (&tag_e_sign, dt->sign);
2663   RESOLVE_TAG (&tag_e_round, dt->round);
2664   RESOLVE_TAG (&tag_e_blank, dt->blank);
2665   RESOLVE_TAG (&tag_e_decimal, dt->decimal);
2666   RESOLVE_TAG (&tag_e_async, dt->asynchronous);
2667
2668   e = dt->io_unit;
2669   if (e == NULL)
2670     {
2671       gfc_error ("UNIT not specified at %L", loc);
2672       return FAILURE;
2673     }
2674
2675   if (gfc_resolve_expr (e) == SUCCESS
2676       && (e->ts.type != BT_INTEGER
2677           && (e->ts.type != BT_CHARACTER || e->expr_type != EXPR_VARIABLE)))
2678     {
2679       /* If there is no extra comma signifying the "format" form of the IO
2680          statement, then this must be an error.  */
2681       if (!dt->extra_comma)
2682         {
2683           gfc_error ("UNIT specification at %L must be an INTEGER expression "
2684                      "or a CHARACTER variable", &e->where);
2685           return FAILURE;
2686         }
2687       else
2688         {
2689           /* At this point, we have an extra comma.  If io_unit has arrived as
2690              type character, we assume its really the "format" form of the I/O
2691              statement.  We set the io_unit to the default unit and format to
2692              the character expression.  See F95 Standard section 9.4.  */
2693           io_kind k;
2694           k = dt->extra_comma->value.iokind;
2695           if (e->ts.type == BT_CHARACTER && (k == M_READ || k == M_PRINT))
2696             {
2697               dt->format_expr = dt->io_unit;
2698               dt->io_unit = default_unit (k);
2699
2700               /* Free this pointer now so that a warning/error is not triggered
2701                  below for the "Extension".  */
2702               gfc_free_expr (dt->extra_comma);
2703               dt->extra_comma = NULL;
2704             }
2705
2706           if (k == M_WRITE)
2707             {
2708               gfc_error ("Invalid form of WRITE statement at %L, UNIT required",
2709                          &dt->extra_comma->where);
2710               return FAILURE;
2711             }
2712         }
2713     }
2714
2715   if (e->ts.type == BT_CHARACTER)
2716     {
2717       if (gfc_has_vector_index (e))
2718         {
2719           gfc_error ("Internal unit with vector subscript at %L", &e->where);
2720           return FAILURE;
2721         }
2722     }
2723
2724   if (e->rank && e->ts.type != BT_CHARACTER)
2725     {
2726       gfc_error ("External IO UNIT cannot be an array at %L", &e->where);
2727       return FAILURE;
2728     }
2729
2730   if (e->expr_type == EXPR_CONSTANT && e->ts.type == BT_INTEGER
2731       && mpz_sgn (e->value.integer) < 0)
2732     {
2733       gfc_error ("UNIT number in statement at %L must be non-negative", &e->where);
2734       return FAILURE;
2735     }
2736
2737   if (dt->extra_comma
2738       && gfc_notify_std (GFC_STD_GNU, "Extension: Comma before i/o "
2739                          "item list at %L", &dt->extra_comma->where) == FAILURE)
2740     return FAILURE;
2741
2742   if (dt->err)
2743     {
2744       if (gfc_reference_st_label (dt->err, ST_LABEL_TARGET) == FAILURE)
2745         return FAILURE;
2746       if (dt->err->defined == ST_LABEL_UNKNOWN)
2747         {
2748           gfc_error ("ERR tag label %d at %L not defined",
2749                       dt->err->value, &dt->err_where);
2750           return FAILURE;
2751         }
2752     }
2753
2754   if (dt->end)
2755     {
2756       if (gfc_reference_st_label (dt->end, ST_LABEL_TARGET) == FAILURE)
2757         return FAILURE;
2758       if (dt->end->defined == ST_LABEL_UNKNOWN)
2759         {
2760           gfc_error ("END tag label %d at %L not defined",
2761                       dt->end->value, &dt->end_where);
2762           return FAILURE;
2763         }
2764     }
2765
2766   if (dt->eor)
2767     {
2768       if (gfc_reference_st_label (dt->eor, ST_LABEL_TARGET) == FAILURE)
2769         return FAILURE;
2770       if (dt->eor->defined == ST_LABEL_UNKNOWN)
2771         {
2772           gfc_error ("EOR tag label %d at %L not defined",
2773                       dt->eor->value, &dt->eor_where);
2774           return FAILURE;
2775         }
2776     }
2777
2778   /* Check the format label actually exists.  */
2779   if (dt->format_label && dt->format_label != &format_asterisk
2780       && dt->format_label->defined == ST_LABEL_UNKNOWN)
2781     {
2782       gfc_error ("FORMAT label %d at %L not defined", dt->format_label->value,
2783                  &dt->format_label->where);
2784       return FAILURE;
2785     }
2786   return SUCCESS;
2787 }
2788
2789
2790 /* Given an io_kind, return its name.  */
2791
2792 static const char *
2793 io_kind_name (io_kind k)
2794 {
2795   const char *name;
2796
2797   switch (k)
2798     {
2799     case M_READ:
2800       name = "READ";
2801       break;
2802     case M_WRITE:
2803       name = "WRITE";
2804       break;
2805     case M_PRINT:
2806       name = "PRINT";
2807       break;
2808     case M_INQUIRE:
2809       name = "INQUIRE";
2810       break;
2811     default:
2812       gfc_internal_error ("io_kind_name(): bad I/O-kind");
2813     }
2814
2815   return name;
2816 }
2817
2818
2819 /* Match an IO iteration statement of the form:
2820
2821    ( [<IO element> ,] <IO element>, I = <expr>, <expr> [, <expr> ] )
2822
2823    which is equivalent to a single IO element.  This function is
2824    mutually recursive with match_io_element().  */
2825
2826 static match match_io_element (io_kind, gfc_code **);
2827
2828 static match
2829 match_io_iterator (io_kind k, gfc_code **result)
2830 {
2831   gfc_code *head, *tail, *new_code;
2832   gfc_iterator *iter;
2833   locus old_loc;
2834   match m;
2835   int n;
2836
2837   iter = NULL;
2838   head = NULL;
2839   old_loc = gfc_current_locus;
2840
2841   if (gfc_match_char ('(') != MATCH_YES)
2842     return MATCH_NO;
2843
2844   m = match_io_element (k, &head);
2845   tail = head;
2846
2847   if (m != MATCH_YES || gfc_match_char (',') != MATCH_YES)
2848     {
2849       m = MATCH_NO;
2850       goto cleanup;
2851     }
2852
2853   /* Can't be anything but an IO iterator.  Build a list.  */
2854   iter = gfc_get_iterator ();
2855
2856   for (n = 1;; n++)
2857     {
2858       m = gfc_match_iterator (iter, 0);
2859       if (m == MATCH_ERROR)
2860         goto cleanup;
2861       if (m == MATCH_YES)
2862         {
2863           gfc_check_do_variable (iter->var->symtree);
2864           break;
2865         }
2866
2867       m = match_io_element (k, &new_code);
2868       if (m == MATCH_ERROR)
2869         goto cleanup;
2870       if (m == MATCH_NO)
2871         {
2872           if (n > 2)
2873             goto syntax;
2874           goto cleanup;
2875         }
2876
2877       tail = gfc_append_code (tail, new_code);
2878
2879       if (gfc_match_char (',') != MATCH_YES)
2880         {
2881           if (n > 2)
2882             goto syntax;
2883           m = MATCH_NO;
2884           goto cleanup;
2885         }
2886     }
2887
2888   if (gfc_match_char (')') != MATCH_YES)
2889     goto syntax;
2890
2891   new_code = gfc_get_code ();
2892   new_code->op = EXEC_DO;
2893   new_code->ext.iterator = iter;
2894
2895   new_code->block = gfc_get_code ();
2896   new_code->block->op = EXEC_DO;
2897   new_code->block->next = head;
2898
2899   *result = new_code;
2900   return MATCH_YES;
2901
2902 syntax:
2903   gfc_error ("Syntax error in I/O iterator at %C");
2904   m = MATCH_ERROR;
2905
2906 cleanup:
2907   gfc_free_iterator (iter, 1);
2908   gfc_free_statements (head);
2909   gfc_current_locus = old_loc;
2910   return m;
2911 }
2912
2913
2914 /* Match a single element of an IO list, which is either a single
2915    expression or an IO Iterator.  */
2916
2917 static match
2918 match_io_element (io_kind k, gfc_code **cpp)
2919 {
2920   gfc_expr *expr;
2921   gfc_code *cp;
2922   match m;
2923
2924   expr = NULL;
2925
2926   m = match_io_iterator (k, cpp);
2927   if (m == MATCH_YES)
2928     return MATCH_YES;
2929
2930   if (k == M_READ)
2931     {
2932       m = gfc_match_variable (&expr, 0);
2933       if (m == MATCH_NO)
2934         gfc_error ("Expected variable in READ statement at %C");
2935     }
2936   else
2937     {
2938       m = gfc_match_expr (&expr);
2939       if (m == MATCH_NO)
2940         gfc_error ("Expected expression in %s statement at %C",
2941                    io_kind_name (k));
2942     }
2943
2944   if (m == MATCH_YES)
2945     switch (k)
2946       {
2947       case M_READ:
2948         if (expr->symtree->n.sym->attr.intent == INTENT_IN)
2949           {
2950             gfc_error ("Variable '%s' in input list at %C cannot be "
2951                        "INTENT(IN)", expr->symtree->n.sym->name);
2952             m = MATCH_ERROR;
2953           }
2954
2955         if (gfc_pure (NULL)
2956             && gfc_impure_variable (expr->symtree->n.sym)
2957             && current_dt->io_unit
2958             && current_dt->io_unit->ts.type == BT_CHARACTER)
2959           {
2960             gfc_error ("Cannot read to variable '%s' in PURE procedure at %C",
2961                        expr->symtree->n.sym->name);
2962             m = MATCH_ERROR;
2963           }
2964
2965         if (gfc_check_do_variable (expr->symtree))
2966           m = MATCH_ERROR;
2967
2968         break;
2969
2970       case M_WRITE:
2971         if (current_dt->io_unit
2972             && current_dt->io_unit->ts.type == BT_CHARACTER
2973             && gfc_pure (NULL)
2974             && current_dt->io_unit->expr_type == EXPR_VARIABLE
2975             && gfc_impure_variable (current_dt->io_unit->symtree->n.sym))
2976           {
2977             gfc_error ("Cannot write to internal file unit '%s' at %C "
2978                        "inside a PURE procedure",
2979                        current_dt->io_unit->symtree->n.sym->name);
2980             m = MATCH_ERROR;
2981           }
2982
2983         break;
2984
2985       default:
2986         break;
2987       }
2988
2989   if (m != MATCH_YES)
2990     {
2991       gfc_free_expr (expr);
2992       return MATCH_ERROR;
2993     }
2994
2995   cp = gfc_get_code ();
2996   cp->op = EXEC_TRANSFER;
2997   cp->expr1 = expr;
2998
2999   *cpp = cp;
3000   return MATCH_YES;
3001 }
3002
3003
3004 /* Match an I/O list, building gfc_code structures as we go.  */
3005
3006 static match
3007 match_io_list (io_kind k, gfc_code **head_p)
3008 {
3009   gfc_code *head, *tail, *new_code;
3010   match m;
3011
3012   *head_p = head = tail = NULL;
3013   if (gfc_match_eos () == MATCH_YES)
3014     return MATCH_YES;
3015
3016   for (;;)
3017     {
3018       m = match_io_element (k, &new_code);
3019       if (m == MATCH_ERROR)
3020         goto cleanup;
3021       if (m == MATCH_NO)
3022         goto syntax;
3023
3024       tail = gfc_append_code (tail, new_code);
3025       if (head == NULL)
3026         head = new_code;
3027
3028       if (gfc_match_eos () == MATCH_YES)
3029         break;
3030       if (gfc_match_char (',') != MATCH_YES)
3031         goto syntax;
3032     }
3033
3034   *head_p = head;
3035   return MATCH_YES;
3036
3037 syntax:
3038   gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
3039
3040 cleanup:
3041   gfc_free_statements (head);
3042   return MATCH_ERROR;
3043 }
3044
3045
3046 /* Attach the data transfer end node.  */
3047
3048 static void
3049 terminate_io (gfc_code *io_code)
3050 {
3051   gfc_code *c;
3052
3053   if (io_code == NULL)
3054     io_code = new_st.block;
3055
3056   c = gfc_get_code ();
3057   c->op = EXEC_DT_END;
3058
3059   /* Point to structure that is already there */
3060   c->ext.dt = new_st.ext.dt;
3061   gfc_append_code (io_code, c);
3062 }
3063
3064
3065 /* Check the constraints for a data transfer statement.  The majority of the
3066    constraints appearing in 9.4 of the standard appear here.  Some are handled
3067    in resolve_tag and others in gfc_resolve_dt.  */
3068
3069 static match
3070 check_io_constraints (io_kind k, gfc_dt *dt, gfc_code *io_code,
3071                       locus *spec_end)
3072 {
3073 #define io_constraint(condition,msg,arg)\
3074 if (condition) \
3075   {\
3076     gfc_error(msg,arg);\
3077     m = MATCH_ERROR;\
3078   }
3079
3080   match m;
3081   gfc_expr *expr;
3082   gfc_symbol *sym = NULL;
3083   bool warn, unformatted;
3084
3085   warn = (dt->err || dt->iostat) ? true : false;
3086   unformatted = dt->format_expr == NULL && dt->format_label == NULL
3087                 && dt->namelist == NULL;
3088
3089   m = MATCH_YES;
3090
3091   expr = dt->io_unit;
3092   if (expr && expr->expr_type == EXPR_VARIABLE
3093       && expr->ts.type == BT_CHARACTER)
3094     {
3095       sym = expr->symtree->n.sym;
3096
3097       io_constraint (k == M_WRITE && sym->attr.intent == INTENT_IN,
3098                      "Internal file at %L must not be INTENT(IN)",
3099                      &expr->where);
3100
3101       io_constraint (gfc_has_vector_index (dt->io_unit),
3102                      "Internal file incompatible with vector subscript at %L",
3103                      &expr->where);
3104
3105       io_constraint (dt->rec != NULL,
3106                      "REC tag at %L is incompatible with internal file",
3107                      &dt->rec->where);
3108     
3109       io_constraint (dt->pos != NULL,
3110                      "POS tag at %L is incompatible with internal file",
3111                      &dt->pos->where);
3112
3113       io_constraint (unformatted,
3114                      "Unformatted I/O not allowed with internal unit at %L",
3115                      &dt->io_unit->where);
3116
3117       io_constraint (dt->asynchronous != NULL,
3118                      "ASYNCHRONOUS tag at %L not allowed with internal file",
3119                      &dt->asynchronous->where);
3120
3121       if (dt->namelist != NULL)
3122         {
3123           if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Internal file "
3124                               "at %L with namelist", &expr->where)
3125               == FAILURE)
3126             m = MATCH_ERROR;
3127         }
3128
3129       io_constraint (dt->advance != NULL,
3130                      "ADVANCE tag at %L is incompatible with internal file",
3131                      &dt->advance->where);
3132     }
3133
3134   if (expr && expr->ts.type != BT_CHARACTER)
3135     {
3136
3137       io_constraint (gfc_pure (NULL) && (k == M_READ || k == M_WRITE),
3138                      "IO UNIT in %s statement at %C must be "
3139                      "an internal file in a PURE procedure",
3140                      io_kind_name (k));
3141     }
3142
3143   if (k != M_READ)
3144     {
3145       io_constraint (dt->end, "END tag not allowed with output at %L",
3146                      &dt->end_where);
3147
3148       io_constraint (dt->eor, "EOR tag not allowed with output at %L",
3149                      &dt->eor_where);
3150
3151       io_constraint (dt->blank, "BLANK= specifier not allowed with output at %L",
3152                      &dt->blank->where);
3153
3154       io_constraint (dt->pad, "PAD= specifier not allowed with output at %L",
3155                      &dt->pad->where);
3156
3157       io_constraint (dt->size, "SIZE= specifier not allowed with output at %L",
3158                      &dt->size->where);
3159     }
3160   else
3161     {
3162       io_constraint (dt->size && dt->advance == NULL,
3163                      "SIZE tag at %L requires an ADVANCE tag",
3164                      &dt->size->where);
3165
3166       io_constraint (dt->eor && dt->advance == NULL,
3167                      "EOR tag at %L requires an ADVANCE tag",
3168                      &dt->eor_where);
3169     }
3170
3171   if (dt->asynchronous) 
3172     {
3173       static const char * asynchronous[] = { "YES", "NO", NULL };
3174
3175       if (gfc_reduce_init_expr (dt->asynchronous) != SUCCESS)
3176         {
3177           gfc_error ("ASYNCHRONOUS= specifier at %L must be an initialization "
3178                      "expression", &dt->asynchronous->where);
3179           return MATCH_ERROR;
3180         }
3181
3182       if (!compare_to_allowed_values
3183                 ("ASYNCHRONOUS", asynchronous, NULL, NULL,
3184                  dt->asynchronous->value.character.string,
3185                  io_kind_name (k), warn))
3186         return MATCH_ERROR;
3187     }
3188
3189   if (dt->id)
3190     {
3191       bool not_yes
3192         = !dt->asynchronous
3193           || gfc_wide_strlen (dt->asynchronous->value.character.string) != 3
3194           || gfc_wide_strncasecmp (dt->asynchronous->value.character.string,
3195                                    "yes", 3) != 0;
3196       io_constraint (not_yes,
3197                      "ID= specifier at %L must be with ASYNCHRONOUS='yes' "
3198                      "specifier", &dt->id->where);
3199     }
3200
3201   if (dt->decimal)
3202     {
3203       if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DECIMAL= at %C "
3204           "not allowed in Fortran 95") == FAILURE)
3205         return MATCH_ERROR;
3206
3207       if (dt->decimal->expr_type == EXPR_CONSTANT)
3208         {
3209           static const char * decimal[] = { "COMMA", "POINT", NULL };
3210
3211           if (!compare_to_allowed_values ("DECIMAL", decimal, NULL, NULL,
3212                                           dt->decimal->value.character.string,
3213                                           io_kind_name (k), warn))
3214             return MATCH_ERROR;
3215
3216           io_constraint (unformatted,
3217                          "the DECIMAL= specifier at %L must be with an "
3218                          "explicit format expression", &dt->decimal->where);
3219         }
3220     }
3221   
3222   if (dt->blank)
3223     {
3224       if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: BLANK= at %C "
3225           "not allowed in Fortran 95") == FAILURE)
3226         return MATCH_ERROR;
3227
3228       if (dt->blank->expr_type == EXPR_CONSTANT)
3229         {
3230           static const char * blank[] = { "NULL", "ZERO", NULL };
3231
3232           if (!compare_to_allowed_values ("BLANK", blank, NULL, NULL,
3233                                           dt->blank->value.character.string,
3234                                           io_kind_name (k), warn))
3235             return MATCH_ERROR;
3236
3237           io_constraint (unformatted,
3238                          "the BLANK= specifier at %L must be with an "
3239                          "explicit format expression", &dt->blank->where);
3240         }
3241     }
3242
3243   if (dt->pad)
3244     {
3245       if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PAD= at %C "
3246           "not allowed in Fortran 95") == FAILURE)
3247         return MATCH_ERROR;
3248
3249       if (dt->pad->expr_type == EXPR_CONSTANT)
3250         {
3251           static const char * pad[] = { "YES", "NO", NULL };
3252
3253           if (!compare_to_allowed_values ("PAD", pad, NULL, NULL,
3254                                           dt->pad->value.character.string,
3255                                           io_kind_name (k), warn))
3256             return MATCH_ERROR;
3257
3258           io_constraint (unformatted,
3259                          "the PAD= specifier at %L must be with an "
3260                          "explicit format expression", &dt->pad->where);
3261         }
3262     }
3263
3264   if (dt->round)
3265     {
3266       /* When implemented, change the following to use gfc_notify_std F2003.
3267       if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ROUND= at %C "
3268           "not allowed in Fortran 95") == FAILURE)
3269         return MATCH_ERROR;  */
3270       gfc_error ("F2003 Feature: ROUND= specifier at %C not implemented");
3271       return MATCH_ERROR;
3272
3273       if (dt->round->expr_type == EXPR_CONSTANT)
3274         {
3275           static const char * round[] = { "UP", "DOWN", "ZERO", "NEAREST",
3276                                           "COMPATIBLE", "PROCESSOR_DEFINED",
3277                                           NULL };
3278
3279           if (!compare_to_allowed_values ("ROUND", round, NULL, NULL,
3280                                           dt->round->value.character.string,
3281                                           io_kind_name (k), warn))
3282             return MATCH_ERROR;
3283         }
3284     }
3285   
3286   if (dt->sign)
3287     {
3288       /* When implemented, change the following to use gfc_notify_std F2003.
3289       if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: SIGN= at %C "
3290           "not allowed in Fortran 95") == FAILURE)
3291         return MATCH_ERROR;  */
3292       if (dt->sign->expr_type == EXPR_CONSTANT)
3293         {
3294           static const char * sign[] = { "PLUS", "SUPPRESS", "PROCESSOR_DEFINED",
3295                                          NULL };
3296
3297           if (!compare_to_allowed_values ("SIGN", sign, NULL, NULL,
3298                                       dt->sign->value.character.string,
3299                                       io_kind_name (k), warn))
3300             return MATCH_ERROR;
3301
3302           io_constraint (unformatted,
3303                          "SIGN= specifier at %L must be with an "
3304                          "explicit format expression", &dt->sign->where);
3305
3306           io_constraint (k == M_READ,
3307                          "SIGN= specifier at %L not allowed in a "
3308                          "READ statement", &dt->sign->where);
3309         }
3310     }
3311
3312   if (dt->delim)
3313     {
3314       if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: DELIM= at %C "
3315           "not allowed in Fortran 95") == FAILURE)
3316         return MATCH_ERROR;
3317
3318       if (dt->delim->expr_type == EXPR_CONSTANT)
3319         {
3320           static const char *delim[] = { "APOSTROPHE", "QUOTE", "NONE", NULL };
3321
3322           if (!compare_to_allowed_values ("DELIM", delim, NULL, NULL,
3323                                           dt->delim->value.character.string,
3324                                           io_kind_name (k), warn))
3325             return MATCH_ERROR;
3326
3327           io_constraint (k == M_READ,
3328                          "DELIM= specifier at %L not allowed in a "
3329                          "READ statement", &dt->delim->where);
3330       
3331           io_constraint (dt->format_label != &format_asterisk
3332                          && dt->namelist == NULL,
3333                          "DELIM= specifier at %L must have FMT=*",
3334                          &dt->delim->where);
3335
3336           io_constraint (unformatted && dt->namelist == NULL,
3337                          "DELIM= specifier at %L must be with FMT=* or "
3338                          "NML= specifier ", &dt->delim->where);
3339         }
3340     }
3341   
3342   if (dt->namelist)
3343     {
3344       io_constraint (io_code && dt->namelist,
3345                      "NAMELIST cannot be followed by IO-list at %L",
3346                      &io_code->loc);
3347
3348       io_constraint (dt->format_expr,
3349                      "IO spec-list cannot contain both NAMELIST group name "
3350                      "and format specification at %L",
3351                      &dt->format_expr->where);
3352
3353       io_constraint (dt->format_label,
3354                      "IO spec-list cannot contain both NAMELIST group name "
3355                      "and format label at %L", spec_end);
3356
3357       io_constraint (dt->rec,
3358                      "NAMELIST IO is not allowed with a REC= specifier "
3359                      "at %L", &dt->rec->where);
3360
3361       io_constraint (dt->advance,
3362                      "NAMELIST IO is not allowed with a ADVANCE= specifier "
3363                      "at %L", &dt->advance->where);
3364     }
3365
3366   if (dt->rec)
3367     {
3368       io_constraint (dt->end,
3369                      "An END tag is not allowed with a "
3370                      "REC= specifier at %L", &dt->end_where);
3371
3372       io_constraint (dt->format_label == &format_asterisk,
3373                      "FMT=* is not allowed with a REC= specifier "
3374                      "at %L", spec_end);
3375
3376       io_constraint (dt->pos,
3377                      "POS= is not allowed with REC= specifier "
3378                      "at %L", &dt->pos->where);
3379     }
3380
3381   if (dt->advance)
3382     {
3383       int not_yes, not_no;
3384       expr = dt->advance;
3385
3386       io_constraint (dt->format_label == &format_asterisk,
3387                      "List directed format(*) is not allowed with a "
3388                      "ADVANCE= specifier at %L.", &expr->where);
3389
3390       io_constraint (unformatted,
3391                      "the ADVANCE= specifier at %L must appear with an "
3392                      "explicit format expression", &expr->where);
3393
3394       if (expr->expr_type == EXPR_CONSTANT && expr->ts.type == BT_CHARACTER)
3395         {
3396           const gfc_char_t *advance = expr->value.character.string;
3397           not_no = gfc_wide_strlen (advance) != 2
3398                    || gfc_wide_strncasecmp (advance, "no", 2) != 0;
3399           not_yes = gfc_wide_strlen (advance) != 3
3400                     || gfc_wide_strncasecmp (advance, "yes", 3) != 0;
3401         }
3402       else
3403         {
3404           not_no = 0;
3405           not_yes = 0;
3406         }
3407
3408       io_constraint (not_no && not_yes,
3409                      "ADVANCE= specifier at %L must have value = "
3410                      "YES or NO.", &expr->where);
3411
3412       io_constraint (dt->size && not_no && k == M_READ,
3413                      "SIZE tag at %L requires an ADVANCE = 'NO'",
3414                      &dt->size->where);
3415
3416       io_constraint (dt->eor && not_no && k == M_READ,
3417                      "EOR tag at %L requires an ADVANCE = 'NO'",
3418                      &dt->eor_where);      
3419     }
3420
3421   expr = dt->format_expr;
3422   if (gfc_simplify_expr (expr, 0) == FAILURE
3423       || check_format_string (expr, k == M_READ) == FAILURE)
3424     return MATCH_ERROR;
3425
3426   return m;
3427 }
3428 #undef io_constraint
3429
3430
3431 /* Match a READ, WRITE or PRINT statement.  */
3432
3433 static match
3434 match_io (io_kind k)
3435 {
3436   char name[GFC_MAX_SYMBOL_LEN + 1];
3437   gfc_code *io_code;
3438   gfc_symbol *sym;
3439   int comma_flag;
3440   locus where;
3441   locus spec_end;
3442   gfc_dt *dt;
3443   match m;
3444
3445   where = gfc_current_locus;
3446   comma_flag = 0;
3447   current_dt = dt = XCNEW (gfc_dt);
3448   m = gfc_match_char ('(');
3449   if (m == MATCH_NO)
3450     {
3451       where = gfc_current_locus;
3452       if (k == M_WRITE)
3453         goto syntax;
3454       else if (k == M_PRINT)
3455         {
3456           /* Treat the non-standard case of PRINT namelist.  */
3457           if ((gfc_current_form == FORM_FIXED || gfc_peek_ascii_char () == ' ')
3458               && gfc_match_name (name) == MATCH_YES)
3459             {
3460               gfc_find_symbol (name, NULL, 1, &sym);
3461               if (sym && sym->attr.flavor == FL_NAMELIST)
3462                 {
3463                   if (gfc_notify_std (GFC_STD_GNU, "PRINT namelist at "
3464                                       "%C is an extension") == FAILURE)
3465                     {
3466                       m = MATCH_ERROR;
3467                       goto cleanup;
3468                     }
3469
3470                   dt->io_unit = default_unit (k);
3471                   dt->namelist = sym;
3472                   goto get_io_list;
3473                 }
3474               else
3475                 gfc_current_locus = where;
3476             }
3477         }
3478
3479       if (gfc_current_form == FORM_FREE)
3480         {
3481           char c = gfc_peek_ascii_char ();
3482           if (c != ' ' && c != '*' && c != '\'' && c != '"')
3483             {
3484               m = MATCH_NO;
3485               goto cleanup;
3486             }
3487         }
3488
3489       m = match_dt_format (dt);
3490       if (m == MATCH_ERROR)
3491         goto cleanup;
3492       if (m == MATCH_NO)
3493         goto syntax;
3494
3495       comma_flag = 1;
3496       dt->io_unit = default_unit (k);
3497       goto get_io_list;
3498     }
3499   else
3500     {
3501       /* Before issuing an error for a malformed 'print (1,*)' type of
3502          error, check for a default-char-expr of the form ('(I0)').  */
3503       if (k == M_PRINT && m == MATCH_YES)
3504         {
3505           /* Reset current locus to get the initial '(' in an expression.  */
3506           gfc_current_locus = where;
3507           dt->format_expr = NULL;
3508           m = match_dt_format (dt);
3509
3510           if (m == MATCH_ERROR)
3511             goto cleanup;
3512           if (m == MATCH_NO || dt->format_expr == NULL)
3513             goto syntax;
3514
3515           comma_flag = 1;
3516           dt->io_unit = default_unit (k);
3517           goto get_io_list;
3518         }
3519     }
3520
3521   /* Match a control list */
3522   if (match_dt_element (k, dt) == MATCH_YES)
3523     goto next;
3524   if (match_dt_unit (k, dt) != MATCH_YES)
3525     goto loop;
3526
3527   if (gfc_match_char (')') == MATCH_YES)
3528     goto get_io_list;
3529   if (gfc_match_char (',') != MATCH_YES)
3530     goto syntax;
3531
3532   m = match_dt_element (k, dt);
3533   if (m == MATCH_YES)
3534     goto next;
3535   if (m == MATCH_ERROR)
3536     goto cleanup;
3537
3538   m = match_dt_format (dt);
3539   if (m == MATCH_YES)
3540     goto next;
3541   if (m == MATCH_ERROR)
3542     goto cleanup;
3543
3544   where = gfc_current_locus;
3545
3546   m = gfc_match_name (name);
3547   if (m == MATCH_YES)
3548     {
3549       gfc_find_symbol (name, NULL, 1, &sym);
3550       if (sym && sym->attr.flavor == FL_NAMELIST)
3551         {
3552           dt->namelist = sym;
3553           if (k == M_READ && check_namelist (sym))
3554             {
3555               m = MATCH_ERROR;
3556               goto cleanup;
3557             }
3558           goto next;
3559         }
3560     }
3561
3562   gfc_current_locus = where;
3563
3564   goto loop;                    /* No matches, try regular elements */
3565
3566 next:
3567   if (gfc_match_char (')') == MATCH_YES)
3568     goto get_io_list;
3569   if (gfc_match_char (',') != MATCH_YES)
3570     goto syntax;
3571
3572 loop:
3573   for (;;)
3574     {
3575       m = match_dt_element (k, dt);
3576       if (m == MATCH_NO)
3577         goto syntax;
3578       if (m == MATCH_ERROR)
3579         goto cleanup;
3580
3581       if (gfc_match_char (')') == MATCH_YES)
3582         break;
3583       if (gfc_match_char (',') != MATCH_YES)
3584         goto syntax;
3585     }
3586
3587 get_io_list:
3588
3589   /* Used in check_io_constraints, where no locus is available.  */
3590   spec_end = gfc_current_locus;
3591
3592   /* Optional leading comma (non-standard).  We use a gfc_expr structure here
3593      to save the locus.  This is used later when resolving transfer statements
3594      that might have a format expression without unit number.  */
3595   if (!comma_flag && gfc_match_char (',') == MATCH_YES)
3596     {
3597       dt->extra_comma = gfc_get_expr ();
3598
3599       /* Set the types to something compatible with iokind. This is needed to
3600          get through gfc_free_expr later since iokind really has no Basic Type,
3601          BT, of its own.  */
3602       dt->extra_comma->expr_type = EXPR_CONSTANT;
3603       dt->extra_comma->ts.type = BT_LOGICAL;
3604
3605       /* Save the iokind and locus for later use in resolution.  */
3606       dt->extra_comma->value.iokind = k;
3607       dt->extra_comma->where = gfc_current_locus;
3608     }
3609
3610   io_code = NULL;
3611   if (gfc_match_eos () != MATCH_YES)
3612     {
3613       if (comma_flag && gfc_match_char (',') != MATCH_YES)
3614         {
3615           gfc_error ("Expected comma in I/O list at %C");
3616           m = MATCH_ERROR;
3617           goto cleanup;
3618         }
3619
3620       m = match_io_list (k, &io_code);
3621       if (m == MATCH_ERROR)
3622         goto cleanup;
3623       if (m == MATCH_NO)
3624         goto syntax;
3625     }
3626
3627   /* A full IO statement has been matched.  Check the constraints.  spec_end is
3628      supplied for cases where no locus is supplied.  */
3629   m = check_io_constraints (k, dt, io_code, &spec_end);
3630
3631   if (m == MATCH_ERROR)
3632     goto cleanup;
3633
3634   new_st.op = (k == M_READ) ? EXEC_READ : EXEC_WRITE;
3635   new_st.ext.dt = dt;
3636   new_st.block = gfc_get_code ();
3637   new_st.block->op = new_st.op;
3638   new_st.block->next = io_code;
3639
3640   terminate_io (io_code);
3641
3642   return MATCH_YES;
3643
3644 syntax:
3645   gfc_error ("Syntax error in %s statement at %C", io_kind_name (k));
3646   m = MATCH_ERROR;
3647
3648 cleanup:
3649   gfc_free_dt (dt);
3650   return m;
3651 }
3652
3653
3654 match
3655 gfc_match_read (void)
3656 {
3657   return match_io (M_READ);
3658 }
3659
3660
3661 match
3662 gfc_match_write (void)
3663 {
3664   return match_io (M_WRITE);
3665 }
3666
3667
3668 match
3669 gfc_match_print (void)
3670 {
3671   match m;
3672
3673   m = match_io (M_PRINT);
3674   if (m != MATCH_YES)
3675     return m;
3676
3677   if (gfc_pure (NULL))
3678     {
3679       gfc_error ("PRINT statement at %C not allowed within PURE procedure");
3680       return MATCH_ERROR;
3681     }
3682
3683   return MATCH_YES;
3684 }
3685
3686
3687 /* Free a gfc_inquire structure.  */
3688
3689 void
3690 gfc_free_inquire (gfc_inquire *inquire)
3691 {
3692
3693   if (inquire == NULL)
3694     return;
3695
3696   gfc_free_expr (inquire->unit);
3697   gfc_free_expr (inquire->file);
3698   gfc_free_expr (inquire->iomsg);
3699   gfc_free_expr (inquire->iostat);
3700   gfc_free_expr (inquire->exist);
3701   gfc_free_expr (inquire->opened);
3702   gfc_free_expr (inquire->number);
3703   gfc_free_expr (inquire->named);
3704   gfc_free_expr (inquire->name);
3705   gfc_free_expr (inquire->access);
3706   gfc_free_expr (inquire->sequential);
3707   gfc_free_expr (inquire->direct);
3708   gfc_free_expr (inquire->form);
3709   gfc_free_expr (inquire->formatted);
3710   gfc_free_expr (inquire->unformatted);
3711   gfc_free_expr (inquire->recl);
3712   gfc_free_expr (inquire->nextrec);
3713   gfc_free_expr (inquire->blank);
3714   gfc_free_expr (inquire->position);
3715   gfc_free_expr (inquire->action);
3716   gfc_free_expr (inquire->read);
3717   gfc_free_expr (inquire->write);
3718   gfc_free_expr (inquire->readwrite);
3719   gfc_free_expr (inquire->delim);
3720   gfc_free_expr (inquire->encoding);
3721   gfc_free_expr (inquire->pad);
3722   gfc_free_expr (inquire->iolength);
3723   gfc_free_expr (inquire->convert);
3724   gfc_free_expr (inquire->strm_pos);
3725   gfc_free_expr (inquire->asynchronous);
3726   gfc_free_expr (inquire->decimal);
3727   gfc_free_expr (inquire->pending);
3728   gfc_free_expr (inquire->id);
3729   gfc_free_expr (inquire->sign);
3730   gfc_free_expr (inquire->size);
3731   gfc_free_expr (inquire->round);
3732   gfc_free (inquire);
3733 }
3734
3735
3736 /* Match an element of an INQUIRE statement.  */
3737
3738 #define RETM   if (m != MATCH_NO) return m;
3739
3740 static match
3741 match_inquire_element (gfc_inquire *inquire)
3742 {
3743   match m;
3744
3745   m = match_etag (&tag_unit, &inquire->unit);
3746   RETM m = match_etag (&tag_file, &inquire->file);
3747   RETM m = match_ltag (&tag_err, &inquire->err);
3748   RETM m = match_out_tag (&tag_iomsg, &inquire->iomsg);
3749   RETM m = match_out_tag (&tag_iostat, &inquire->iostat);
3750   RETM m = match_vtag (&tag_exist, &inquire->exist);
3751   RETM m = match_vtag (&tag_opened, &inquire->opened);
3752   RETM m = match_vtag (&tag_named, &inquire->named);
3753   RETM m = match_vtag (&tag_name, &inquire->name);
3754   RETM m = match_out_tag (&tag_number, &inquire->number);
3755   RETM m = match_vtag (&tag_s_access, &inquire->access);
3756   RETM m = match_vtag (&tag_sequential, &inquire->sequential);
3757   RETM m = match_vtag (&tag_direct, &inquire->direct);
3758   RETM m = match_vtag (&tag_s_form, &inquire->form);
3759   RETM m = match_vtag (&tag_formatted, &inquire->formatted);
3760   RETM m = match_vtag (&tag_unformatted, &inquire->unformatted);
3761   RETM m = match_out_tag (&tag_s_recl, &inquire->recl);
3762   RETM m = match_out_tag (&tag_nextrec, &inquire->nextrec);
3763   RETM m = match_vtag (&tag_s_blank, &inquire->blank);
3764   RETM m = match_vtag (&tag_s_position, &inquire->position);
3765   RETM m = match_vtag (&tag_s_action, &inquire->action);
3766   RETM m = match_vtag (&tag_read, &inquire->read);
3767   RETM m = match_vtag (&tag_write, &inquire->write);
3768   RETM m = match_vtag (&tag_readwrite, &inquire->readwrite);
3769   RETM m = match_vtag (&tag_s_async, &inquire->asynchronous);
3770   RETM m = match_vtag (&tag_s_delim, &inquire->delim);
3771   RETM m = match_vtag (&tag_s_decimal, &inquire->decimal);
3772   RETM m = match_vtag (&tag_size, &inquire->size);
3773   RETM m = match_vtag (&tag_s_encoding, &inquire->encoding);
3774   RETM m = match_vtag (&tag_s_round, &inquire->round);
3775   RETM m = match_vtag (&tag_s_sign, &inquire->sign);
3776   RETM m = match_vtag (&tag_s_pad, &inquire->pad);
3777   RETM m = match_vtag (&tag_iolength, &inquire->iolength);
3778   RETM m = match_vtag (&tag_convert, &inquire->convert);
3779   RETM m = match_out_tag (&tag_strm_out, &inquire->strm_pos);
3780   RETM m = match_vtag (&tag_pending, &inquire->pending);
3781   RETM m = match_vtag (&tag_id, &inquire->id);
3782   RETM return MATCH_NO;
3783 }
3784
3785 #undef RETM
3786
3787
3788 match
3789 gfc_match_inquire (void)
3790 {
3791   gfc_inquire *inquire;
3792   gfc_code *code;
3793   match m;
3794   locus loc;
3795
3796   m = gfc_match_char ('(');
3797   if (m == MATCH_NO)
3798     return m;
3799
3800   inquire = XCNEW (gfc_inquire);
3801
3802   loc = gfc_current_locus;
3803
3804   m = match_inquire_element (inquire);
3805   if (m == MATCH_ERROR)
3806     goto cleanup;
3807   if (m == MATCH_NO)
3808     {
3809       m = gfc_match_expr (&inquire->unit);
3810       if (m == MATCH_ERROR)
3811         goto cleanup;
3812       if (m == MATCH_NO)
3813         goto syntax;
3814     }
3815
3816   /* See if we have the IOLENGTH form of the inquire statement.  */
3817   if (inquire->iolength != NULL)
3818     {
3819       if (gfc_match_char (')') != MATCH_YES)
3820         goto syntax;
3821
3822       m = match_io_list (M_INQUIRE, &code);
3823       if (m == MATCH_ERROR)
3824         goto cleanup;
3825       if (m == MATCH_NO)
3826         goto syntax;
3827
3828       new_st.op = EXEC_IOLENGTH;
3829       new_st.expr1 = inquire->iolength;
3830       new_st.ext.inquire = inquire;
3831
3832       if (gfc_pure (NULL))
3833         {
3834           gfc_free_statements (code);
3835           gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3836           return MATCH_ERROR;
3837         }
3838
3839       new_st.block = gfc_get_code ();
3840       new_st.block->op = EXEC_IOLENGTH;
3841       terminate_io (code);
3842       new_st.block->next = code;
3843       return MATCH_YES;
3844     }
3845
3846   /* At this point, we have the non-IOLENGTH inquire statement.  */
3847   for (;;)
3848     {
3849       if (gfc_match_char (')') == MATCH_YES)
3850         break;
3851       if (gfc_match_char (',') != MATCH_YES)
3852         goto syntax;
3853
3854       m = match_inquire_element (inquire);
3855       if (m == MATCH_ERROR)
3856         goto cleanup;
3857       if (m == MATCH_NO)
3858         goto syntax;
3859
3860       if (inquire->iolength != NULL)
3861         {
3862           gfc_error ("IOLENGTH tag invalid in INQUIRE statement at %C");
3863           goto cleanup;
3864         }
3865     }
3866
3867   if (gfc_match_eos () != MATCH_YES)
3868     goto syntax;
3869
3870   if (inquire->unit != NULL && inquire->file != NULL)
3871     {
3872       gfc_error ("INQUIRE statement at %L cannot contain both FILE and "
3873                  "UNIT specifiers", &loc);
3874       goto cleanup;
3875     }
3876
3877   if (inquire->unit == NULL && inquire->file == NULL)
3878     {
3879       gfc_error ("INQUIRE statement at %L requires either FILE or "
3880                  "UNIT specifier", &loc);
3881       goto cleanup;
3882     }
3883
3884   if (gfc_pure (NULL))
3885     {
3886       gfc_error ("INQUIRE statement not allowed in PURE procedure at %C");
3887       goto cleanup;
3888     }
3889   
3890   if (inquire->id != NULL && inquire->pending == NULL)
3891     {
3892       gfc_error ("INQUIRE statement at %L requires a PENDING= specifier with "
3893                  "the ID= specifier", &loc);
3894       goto cleanup;
3895     }
3896
3897   new_st.op = EXEC_INQUIRE;
3898   new_st.ext.inquire = inquire;
3899   return MATCH_YES;
3900
3901 syntax:
3902   gfc_syntax_error (ST_INQUIRE);
3903
3904 cleanup:
3905   gfc_free_inquire (inquire);
3906   return MATCH_ERROR;
3907 }
3908
3909
3910 /* Resolve everything in a gfc_inquire structure.  */
3911
3912 gfc_try
3913 gfc_resolve_inquire (gfc_inquire *inquire)
3914 {
3915   RESOLVE_TAG (&tag_unit, inquire->unit);
3916   RESOLVE_TAG (&tag_file, inquire->file);
3917   RESOLVE_TAG (&tag_iomsg, inquire->iomsg);
3918   RESOLVE_TAG (&tag_iostat, inquire->iostat);
3919   RESOLVE_TAG (&tag_exist, inquire->exist);
3920   RESOLVE_TAG (&tag_opened, inquire->opened);
3921   RESOLVE_TAG (&tag_number, inquire->number);
3922   RESOLVE_TAG (&tag_named, inquire->named);
3923   RESOLVE_TAG (&tag_name, inquire->name);
3924   RESOLVE_TAG (&tag_s_access, inquire->access);
3925   RESOLVE_TAG (&tag_sequential, inquire->sequential);
3926   RESOLVE_TAG (&tag_direct, inquire->direct);
3927   RESOLVE_TAG (&tag_s_form, inquire->form);
3928   RESOLVE_TAG (&tag_formatted, inquire->formatted);
3929   RESOLVE_TAG (&tag_unformatted, inquire->unformatted);
3930   RESOLVE_TAG (&tag_s_recl, inquire->recl);
3931   RESOLVE_TAG (&tag_nextrec, inquire->nextrec);
3932   RESOLVE_TAG (&tag_s_blank, inquire->blank);
3933   RESOLVE_TAG (&tag_s_position, inquire->position);
3934   RESOLVE_TAG (&tag_s_action, inquire->action);
3935   RESOLVE_TAG (&tag_read, inquire->read);
3936   RESOLVE_TAG (&tag_write, inquire->write);
3937   RESOLVE_TAG (&tag_readwrite, inquire->readwrite);
3938   RESOLVE_TAG (&tag_s_delim, inquire->delim);
3939   RESOLVE_TAG (&tag_s_pad, inquire->pad);
3940   RESOLVE_TAG (&tag_s_encoding, inquire->encoding);
3941   RESOLVE_TAG (&tag_s_round, inquire->round);
3942   RESOLVE_TAG (&tag_iolength, inquire->iolength);
3943   RESOLVE_TAG (&tag_convert, inquire->convert);
3944   RESOLVE_TAG (&tag_strm_out, inquire->strm_pos);
3945   RESOLVE_TAG (&tag_s_async, inquire->asynchronous);
3946   RESOLVE_TAG (&tag_s_sign, inquire->sign);
3947   RESOLVE_TAG (&tag_s_round, inquire->round);
3948   RESOLVE_TAG (&tag_pending, inquire->pending);
3949   RESOLVE_TAG (&tag_size, inquire->size);
3950   RESOLVE_TAG (&tag_id, inquire->id);
3951
3952   if (gfc_reference_st_label (inquire->err, ST_LABEL_TARGET) == FAILURE)
3953     return FAILURE;
3954
3955   return SUCCESS;
3956 }
3957
3958
3959 void
3960 gfc_free_wait (gfc_wait *wait)
3961 {
3962   if (wait == NULL)
3963     return;
3964
3965   gfc_free_expr (wait->unit);
3966   gfc_free_expr (wait->iostat);
3967   gfc_free_expr (wait->iomsg);
3968   gfc_free_expr (wait->id);
3969 }
3970
3971
3972 gfc_try
3973 gfc_resolve_wait (gfc_wait *wait)
3974 {
3975   RESOLVE_TAG (&tag_unit, wait->unit);
3976   RESOLVE_TAG (&tag_iomsg, wait->iomsg);
3977   RESOLVE_TAG (&tag_iostat, wait->iostat);
3978   RESOLVE_TAG (&tag_id, wait->id);
3979
3980   if (gfc_reference_st_label (wait->err, ST_LABEL_TARGET) == FAILURE)
3981     return FAILURE;
3982   
3983   if (gfc_reference_st_label (wait->end, ST_LABEL_TARGET) == FAILURE)
3984     return FAILURE;
3985
3986   return SUCCESS;
3987 }
3988
3989 /* Match an element of a WAIT statement.  */
3990
3991 #define RETM   if (m != MATCH_NO) return m;
3992
3993 static match
3994 match_wait_element (gfc_wait *wait)
3995 {
3996   match m;
3997
3998   m = match_etag (&tag_unit, &wait->unit);
3999   RETM m = match_ltag (&tag_err, &wait->err);
4000   RETM m = match_ltag (&tag_end, &wait->eor);
4001   RETM m = match_ltag (&tag_eor, &wait->end);
4002   RETM m = match_out_tag (&tag_iomsg, &wait->iomsg);
4003   RETM m = match_out_tag (&tag_iostat, &wait->iostat);
4004   RETM m = match_etag (&tag_id, &wait->id);
4005   RETM return MATCH_NO;
4006 }
4007
4008 #undef RETM
4009
4010
4011 match
4012 gfc_match_wait (void)
4013 {
4014   gfc_wait *wait;
4015   match m;
4016   locus loc;
4017
4018   m = gfc_match_char ('(');
4019   if (m == MATCH_NO)
4020     return m;
4021
4022   wait = XCNEW (gfc_wait);
4023
4024   loc = gfc_current_locus;
4025
4026   m = match_wait_element (wait);
4027   if (m == MATCH_ERROR)
4028     goto cleanup;
4029   if (m == MATCH_NO)
4030     {
4031       m = gfc_match_expr (&wait->unit);
4032       if (m == MATCH_ERROR)
4033         goto cleanup;
4034       if (m == MATCH_NO)
4035         goto syntax;
4036     }
4037
4038   for (;;)
4039     {
4040       if (gfc_match_char (')') == MATCH_YES)
4041         break;
4042       if (gfc_match_char (',') != MATCH_YES)
4043         goto syntax;
4044
4045       m = match_wait_element (wait);
4046       if (m == MATCH_ERROR)
4047         goto cleanup;
4048       if (m == MATCH_NO)
4049         goto syntax;
4050     }
4051
4052   if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: WAIT at %C "
4053           "not allowed in Fortran 95") == FAILURE)
4054     goto cleanup;
4055
4056   if (gfc_pure (NULL))
4057     {
4058       gfc_error ("WAIT statement not allowed in PURE procedure at %C");
4059       goto cleanup;
4060     }
4061
4062   new_st.op = EXEC_WAIT;
4063   new_st.ext.wait = wait;
4064
4065   return MATCH_YES;
4066
4067 syntax:
4068   gfc_syntax_error (ST_WAIT);
4069
4070 cleanup:
4071   gfc_free_wait (wait);
4072   return MATCH_ERROR;
4073 }