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