OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [gcc/] [fortran/] [io.c] - Blame information for rev 20

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.