check.c (positive_check): Add new function checking constant for being greater then...
[gcc.git] / gcc / fortran / match.c
1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000-2017 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
29 int gfc_matching_ptr_assignment = 0;
30 int gfc_matching_procptr_assignment = 0;
31 bool gfc_matching_prefix = false;
32
33 /* Stack of SELECT TYPE statements. */
34 gfc_select_type_stack *select_type_stack = NULL;
35
36 /* For debugging and diagnostic purposes. Return the textual representation
37 of the intrinsic operator OP. */
38 const char *
39 gfc_op2string (gfc_intrinsic_op op)
40 {
41 switch (op)
42 {
43 case INTRINSIC_UPLUS:
44 case INTRINSIC_PLUS:
45 return "+";
46
47 case INTRINSIC_UMINUS:
48 case INTRINSIC_MINUS:
49 return "-";
50
51 case INTRINSIC_POWER:
52 return "**";
53 case INTRINSIC_CONCAT:
54 return "//";
55 case INTRINSIC_TIMES:
56 return "*";
57 case INTRINSIC_DIVIDE:
58 return "/";
59
60 case INTRINSIC_AND:
61 return ".and.";
62 case INTRINSIC_OR:
63 return ".or.";
64 case INTRINSIC_EQV:
65 return ".eqv.";
66 case INTRINSIC_NEQV:
67 return ".neqv.";
68
69 case INTRINSIC_EQ_OS:
70 return ".eq.";
71 case INTRINSIC_EQ:
72 return "==";
73 case INTRINSIC_NE_OS:
74 return ".ne.";
75 case INTRINSIC_NE:
76 return "/=";
77 case INTRINSIC_GE_OS:
78 return ".ge.";
79 case INTRINSIC_GE:
80 return ">=";
81 case INTRINSIC_LE_OS:
82 return ".le.";
83 case INTRINSIC_LE:
84 return "<=";
85 case INTRINSIC_LT_OS:
86 return ".lt.";
87 case INTRINSIC_LT:
88 return "<";
89 case INTRINSIC_GT_OS:
90 return ".gt.";
91 case INTRINSIC_GT:
92 return ">";
93 case INTRINSIC_NOT:
94 return ".not.";
95
96 case INTRINSIC_ASSIGN:
97 return "=";
98
99 case INTRINSIC_PARENTHESES:
100 return "parens";
101
102 case INTRINSIC_NONE:
103 return "none";
104
105 /* DTIO */
106 case INTRINSIC_FORMATTED:
107 return "formatted";
108 case INTRINSIC_UNFORMATTED:
109 return "unformatted";
110
111 default:
112 break;
113 }
114
115 gfc_internal_error ("gfc_op2string(): Bad code");
116 /* Not reached. */
117 }
118
119
120 /******************** Generic matching subroutines ************************/
121
122 /* Matches a member separator. With standard FORTRAN this is '%', but with
123 DEC structures we must carefully match dot ('.').
124 Because operators are spelled ".op.", a dotted string such as "x.y.z..."
125 can be either a component reference chain or a combination of binary
126 operations.
127 There is no real way to win because the string may be grammatically
128 ambiguous. The following rules help avoid ambiguities - they match
129 some behavior of other (older) compilers. If the rules here are changed
130 the test cases should be updated. If the user has problems with these rules
131 they probably deserve the consequences. Consider "x.y.z":
132 (1) If any user defined operator ".y." exists, this is always y(x,z)
133 (even if ".y." is the wrong type and/or x has a member y).
134 (2) Otherwise if x has a member y, and y is itself a derived type,
135 this is (x->y)->z, even if an intrinsic operator exists which
136 can handle (x,z).
137 (3) If x has no member y or (x->y) is not a derived type but ".y."
138 is an intrinsic operator (such as ".eq."), this is y(x,z).
139 (4) Lastly if there is no operator ".y." and x has no member "y", it is an
140 error.
141 It is worth noting that the logic here does not support mixed use of member
142 accessors within a single string. That is, even if x has component y and y
143 has component z, the following are all syntax errors:
144 "x%y.z" "x.y%z" "(x.y).z" "(x%y)%z"
145 */
146
147 match
148 gfc_match_member_sep(gfc_symbol *sym)
149 {
150 char name[GFC_MAX_SYMBOL_LEN + 1];
151 locus dot_loc, start_loc;
152 gfc_intrinsic_op iop;
153 match m;
154 gfc_symbol *tsym;
155 gfc_component *c = NULL;
156
157 /* What a relief: '%' is an unambiguous member separator. */
158 if (gfc_match_char ('%') == MATCH_YES)
159 return MATCH_YES;
160
161 /* Beware ye who enter here. */
162 if (!flag_dec_structure || !sym)
163 return MATCH_NO;
164
165 tsym = NULL;
166
167 /* We may be given either a derived type variable or the derived type
168 declaration itself (which actually contains the components);
169 we need the latter to search for components. */
170 if (gfc_fl_struct (sym->attr.flavor))
171 tsym = sym;
172 else if (gfc_bt_struct (sym->ts.type))
173 tsym = sym->ts.u.derived;
174
175 iop = INTRINSIC_NONE;
176 name[0] = '\0';
177 m = MATCH_NO;
178
179 /* If we have to reject come back here later. */
180 start_loc = gfc_current_locus;
181
182 /* Look for a component access next. */
183 if (gfc_match_char ('.') != MATCH_YES)
184 return MATCH_NO;
185
186 /* If we accept, come back here. */
187 dot_loc = gfc_current_locus;
188
189 /* Try to match a symbol name following the dot. */
190 if (gfc_match_name (name) != MATCH_YES)
191 {
192 gfc_error ("Expected structure component or operator name "
193 "after '.' at %C");
194 goto error;
195 }
196
197 /* If no dot follows we have "x.y" which should be a component access. */
198 if (gfc_match_char ('.') != MATCH_YES)
199 goto yes;
200
201 /* Now we have a string "x.y.z" which could be a nested member access
202 (x->y)->z or a binary operation y on x and z. */
203
204 /* First use any user-defined operators ".y." */
205 if (gfc_find_uop (name, sym->ns) != NULL)
206 goto no;
207
208 /* Match accesses to existing derived-type components for
209 derived-type vars: "x.y.z" = (x->y)->z */
210 c = gfc_find_component(tsym, name, false, true, NULL);
211 if (c && (gfc_bt_struct (c->ts.type) || c->ts.type == BT_CLASS))
212 goto yes;
213
214 /* If y is not a component or has no members, try intrinsic operators. */
215 gfc_current_locus = start_loc;
216 if (gfc_match_intrinsic_op (&iop) != MATCH_YES)
217 {
218 /* If ".y." is not an intrinsic operator but y was a valid non-
219 structure component, match and leave the trailing dot to be
220 dealt with later. */
221 if (c)
222 goto yes;
223
224 gfc_error ("'%s' is neither a defined operator nor a "
225 "structure component in dotted string at %C", name);
226 goto error;
227 }
228
229 /* .y. is an intrinsic operator, overriding any possible member access. */
230 goto no;
231
232 /* Return keeping the current locus consistent with the match result. */
233 error:
234 m = MATCH_ERROR;
235 no:
236 gfc_current_locus = start_loc;
237 return m;
238 yes:
239 gfc_current_locus = dot_loc;
240 return MATCH_YES;
241 }
242
243
244 /* This function scans the current statement counting the opened and closed
245 parenthesis to make sure they are balanced. */
246
247 match
248 gfc_match_parens (void)
249 {
250 locus old_loc, where;
251 int count;
252 gfc_instring instring;
253 gfc_char_t c, quote;
254
255 old_loc = gfc_current_locus;
256 count = 0;
257 instring = NONSTRING;
258 quote = ' ';
259
260 for (;;)
261 {
262 c = gfc_next_char_literal (instring);
263 if (c == '\n')
264 break;
265 if (quote == ' ' && ((c == '\'') || (c == '"')))
266 {
267 quote = c;
268 instring = INSTRING_WARN;
269 continue;
270 }
271 if (quote != ' ' && c == quote)
272 {
273 quote = ' ';
274 instring = NONSTRING;
275 continue;
276 }
277
278 if (c == '(' && quote == ' ')
279 {
280 count++;
281 where = gfc_current_locus;
282 }
283 if (c == ')' && quote == ' ')
284 {
285 count--;
286 where = gfc_current_locus;
287 }
288 }
289
290 gfc_current_locus = old_loc;
291
292 if (count > 0)
293 {
294 gfc_error ("Missing %<)%> in statement at or before %L", &where);
295 return MATCH_ERROR;
296 }
297 if (count < 0)
298 {
299 gfc_error ("Missing %<(%> in statement at or before %L", &where);
300 return MATCH_ERROR;
301 }
302
303 return MATCH_YES;
304 }
305
306
307 /* See if the next character is a special character that has
308 escaped by a \ via the -fbackslash option. */
309
310 match
311 gfc_match_special_char (gfc_char_t *res)
312 {
313 int len, i;
314 gfc_char_t c, n;
315 match m;
316
317 m = MATCH_YES;
318
319 switch ((c = gfc_next_char_literal (INSTRING_WARN)))
320 {
321 case 'a':
322 *res = '\a';
323 break;
324 case 'b':
325 *res = '\b';
326 break;
327 case 't':
328 *res = '\t';
329 break;
330 case 'f':
331 *res = '\f';
332 break;
333 case 'n':
334 *res = '\n';
335 break;
336 case 'r':
337 *res = '\r';
338 break;
339 case 'v':
340 *res = '\v';
341 break;
342 case '\\':
343 *res = '\\';
344 break;
345 case '0':
346 *res = '\0';
347 break;
348
349 case 'x':
350 case 'u':
351 case 'U':
352 /* Hexadecimal form of wide characters. */
353 len = (c == 'x' ? 2 : (c == 'u' ? 4 : 8));
354 n = 0;
355 for (i = 0; i < len; i++)
356 {
357 char buf[2] = { '\0', '\0' };
358
359 c = gfc_next_char_literal (INSTRING_WARN);
360 if (!gfc_wide_fits_in_byte (c)
361 || !gfc_check_digit ((unsigned char) c, 16))
362 return MATCH_NO;
363
364 buf[0] = (unsigned char) c;
365 n = n << 4;
366 n += strtol (buf, NULL, 16);
367 }
368 *res = n;
369 break;
370
371 default:
372 /* Unknown backslash codes are simply not expanded. */
373 m = MATCH_NO;
374 break;
375 }
376
377 return m;
378 }
379
380
381 /* In free form, match at least one space. Always matches in fixed
382 form. */
383
384 match
385 gfc_match_space (void)
386 {
387 locus old_loc;
388 char c;
389
390 if (gfc_current_form == FORM_FIXED)
391 return MATCH_YES;
392
393 old_loc = gfc_current_locus;
394
395 c = gfc_next_ascii_char ();
396 if (!gfc_is_whitespace (c))
397 {
398 gfc_current_locus = old_loc;
399 return MATCH_NO;
400 }
401
402 gfc_gobble_whitespace ();
403
404 return MATCH_YES;
405 }
406
407
408 /* Match an end of statement. End of statement is optional
409 whitespace, followed by a ';' or '\n' or comment '!'. If a
410 semicolon is found, we continue to eat whitespace and semicolons. */
411
412 match
413 gfc_match_eos (void)
414 {
415 locus old_loc;
416 int flag;
417 char c;
418
419 flag = 0;
420
421 for (;;)
422 {
423 old_loc = gfc_current_locus;
424 gfc_gobble_whitespace ();
425
426 c = gfc_next_ascii_char ();
427 switch (c)
428 {
429 case '!':
430 do
431 {
432 c = gfc_next_ascii_char ();
433 }
434 while (c != '\n');
435
436 /* Fall through. */
437
438 case '\n':
439 return MATCH_YES;
440
441 case ';':
442 flag = 1;
443 continue;
444 }
445
446 break;
447 }
448
449 gfc_current_locus = old_loc;
450 return (flag) ? MATCH_YES : MATCH_NO;
451 }
452
453
454 /* Match a literal integer on the input, setting the value on
455 MATCH_YES. Literal ints occur in kind-parameters as well as
456 old-style character length specifications. If cnt is non-NULL it
457 will be set to the number of digits. */
458
459 match
460 gfc_match_small_literal_int (int *value, int *cnt)
461 {
462 locus old_loc;
463 char c;
464 int i, j;
465
466 old_loc = gfc_current_locus;
467
468 *value = -1;
469 gfc_gobble_whitespace ();
470 c = gfc_next_ascii_char ();
471 if (cnt)
472 *cnt = 0;
473
474 if (!ISDIGIT (c))
475 {
476 gfc_current_locus = old_loc;
477 return MATCH_NO;
478 }
479
480 i = c - '0';
481 j = 1;
482
483 for (;;)
484 {
485 old_loc = gfc_current_locus;
486 c = gfc_next_ascii_char ();
487
488 if (!ISDIGIT (c))
489 break;
490
491 i = 10 * i + c - '0';
492 j++;
493
494 if (i > 99999999)
495 {
496 gfc_error ("Integer too large at %C");
497 return MATCH_ERROR;
498 }
499 }
500
501 gfc_current_locus = old_loc;
502
503 *value = i;
504 if (cnt)
505 *cnt = j;
506 return MATCH_YES;
507 }
508
509
510 /* Match a small, constant integer expression, like in a kind
511 statement. On MATCH_YES, 'value' is set. */
512
513 match
514 gfc_match_small_int (int *value)
515 {
516 gfc_expr *expr;
517 match m;
518 int i;
519
520 m = gfc_match_expr (&expr);
521 if (m != MATCH_YES)
522 return m;
523
524 if (gfc_extract_int (expr, &i, 1))
525 m = MATCH_ERROR;
526 gfc_free_expr (expr);
527
528 *value = i;
529 return m;
530 }
531
532
533 /* This function is the same as the gfc_match_small_int, except that
534 we're keeping the pointer to the expr. This function could just be
535 removed and the previously mentioned one modified, though all calls
536 to it would have to be modified then (and there were a number of
537 them). Return MATCH_ERROR if fail to extract the int; otherwise,
538 return the result of gfc_match_expr(). The expr (if any) that was
539 matched is returned in the parameter expr. */
540
541 match
542 gfc_match_small_int_expr (int *value, gfc_expr **expr)
543 {
544 match m;
545 int i;
546
547 m = gfc_match_expr (expr);
548 if (m != MATCH_YES)
549 return m;
550
551 if (gfc_extract_int (*expr, &i, 1))
552 m = MATCH_ERROR;
553
554 *value = i;
555 return m;
556 }
557
558
559 /* Matches a statement label. Uses gfc_match_small_literal_int() to
560 do most of the work. */
561
562 match
563 gfc_match_st_label (gfc_st_label **label)
564 {
565 locus old_loc;
566 match m;
567 int i, cnt;
568
569 old_loc = gfc_current_locus;
570
571 m = gfc_match_small_literal_int (&i, &cnt);
572 if (m != MATCH_YES)
573 return m;
574
575 if (cnt > 5)
576 {
577 gfc_error ("Too many digits in statement label at %C");
578 goto cleanup;
579 }
580
581 if (i == 0)
582 {
583 gfc_error ("Statement label at %C is zero");
584 goto cleanup;
585 }
586
587 *label = gfc_get_st_label (i);
588 return MATCH_YES;
589
590 cleanup:
591
592 gfc_current_locus = old_loc;
593 return MATCH_ERROR;
594 }
595
596
597 /* Match and validate a label associated with a named IF, DO or SELECT
598 statement. If the symbol does not have the label attribute, we add
599 it. We also make sure the symbol does not refer to another
600 (active) block. A matched label is pointed to by gfc_new_block. */
601
602 match
603 gfc_match_label (void)
604 {
605 char name[GFC_MAX_SYMBOL_LEN + 1];
606 match m;
607
608 gfc_new_block = NULL;
609
610 m = gfc_match (" %n :", name);
611 if (m != MATCH_YES)
612 return m;
613
614 if (gfc_get_symbol (name, NULL, &gfc_new_block))
615 {
616 gfc_error ("Label name %qs at %C is ambiguous", name);
617 return MATCH_ERROR;
618 }
619
620 if (gfc_new_block->attr.flavor == FL_LABEL)
621 {
622 gfc_error ("Duplicate construct label %qs at %C", name);
623 return MATCH_ERROR;
624 }
625
626 if (!gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
627 gfc_new_block->name, NULL))
628 return MATCH_ERROR;
629
630 return MATCH_YES;
631 }
632
633
634 /* See if the current input looks like a name of some sort. Modifies
635 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
636 Note that options.c restricts max_identifier_length to not more
637 than GFC_MAX_SYMBOL_LEN. */
638
639 match
640 gfc_match_name (char *buffer)
641 {
642 locus old_loc;
643 int i;
644 char c;
645
646 old_loc = gfc_current_locus;
647 gfc_gobble_whitespace ();
648
649 c = gfc_next_ascii_char ();
650 if (!(ISALPHA (c) || (c == '_' && flag_allow_leading_underscore)))
651 {
652 /* Special cases for unary minus and plus, which allows for a sensible
653 error message for code of the form 'c = exp(-a*b) )' where an
654 extra ')' appears at the end of statement. */
655 if (!gfc_error_flag_test () && c != '(' && c != '-' && c != '+')
656 gfc_error ("Invalid character in name at %C");
657 gfc_current_locus = old_loc;
658 return MATCH_NO;
659 }
660
661 i = 0;
662
663 do
664 {
665 buffer[i++] = c;
666
667 if (i > gfc_option.max_identifier_length)
668 {
669 gfc_error ("Name at %C is too long");
670 return MATCH_ERROR;
671 }
672
673 old_loc = gfc_current_locus;
674 c = gfc_next_ascii_char ();
675 }
676 while (ISALNUM (c) || c == '_' || (flag_dollar_ok && c == '$'));
677
678 if (c == '$' && !flag_dollar_ok)
679 {
680 gfc_fatal_error ("Invalid character %<$%> at %L. Use %<-fdollar-ok%> to "
681 "allow it as an extension", &old_loc);
682 return MATCH_ERROR;
683 }
684
685 buffer[i] = '\0';
686 gfc_current_locus = old_loc;
687
688 return MATCH_YES;
689 }
690
691
692 /* Match a symbol on the input. Modifies the pointer to the symbol
693 pointer if successful. */
694
695 match
696 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
697 {
698 char buffer[GFC_MAX_SYMBOL_LEN + 1];
699 match m;
700
701 m = gfc_match_name (buffer);
702 if (m != MATCH_YES)
703 return m;
704
705 if (host_assoc)
706 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
707 ? MATCH_ERROR : MATCH_YES;
708
709 if (gfc_get_sym_tree (buffer, NULL, matched_symbol, false))
710 return MATCH_ERROR;
711
712 return MATCH_YES;
713 }
714
715
716 match
717 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
718 {
719 gfc_symtree *st;
720 match m;
721
722 m = gfc_match_sym_tree (&st, host_assoc);
723
724 if (m == MATCH_YES)
725 {
726 if (st)
727 *matched_symbol = st->n.sym;
728 else
729 *matched_symbol = NULL;
730 }
731 else
732 *matched_symbol = NULL;
733 return m;
734 }
735
736
737 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
738 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
739 in matchexp.c. */
740
741 match
742 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
743 {
744 locus orig_loc = gfc_current_locus;
745 char ch;
746
747 gfc_gobble_whitespace ();
748 ch = gfc_next_ascii_char ();
749 switch (ch)
750 {
751 case '+':
752 /* Matched "+". */
753 *result = INTRINSIC_PLUS;
754 return MATCH_YES;
755
756 case '-':
757 /* Matched "-". */
758 *result = INTRINSIC_MINUS;
759 return MATCH_YES;
760
761 case '=':
762 if (gfc_next_ascii_char () == '=')
763 {
764 /* Matched "==". */
765 *result = INTRINSIC_EQ;
766 return MATCH_YES;
767 }
768 break;
769
770 case '<':
771 if (gfc_peek_ascii_char () == '=')
772 {
773 /* Matched "<=". */
774 gfc_next_ascii_char ();
775 *result = INTRINSIC_LE;
776 return MATCH_YES;
777 }
778 /* Matched "<". */
779 *result = INTRINSIC_LT;
780 return MATCH_YES;
781
782 case '>':
783 if (gfc_peek_ascii_char () == '=')
784 {
785 /* Matched ">=". */
786 gfc_next_ascii_char ();
787 *result = INTRINSIC_GE;
788 return MATCH_YES;
789 }
790 /* Matched ">". */
791 *result = INTRINSIC_GT;
792 return MATCH_YES;
793
794 case '*':
795 if (gfc_peek_ascii_char () == '*')
796 {
797 /* Matched "**". */
798 gfc_next_ascii_char ();
799 *result = INTRINSIC_POWER;
800 return MATCH_YES;
801 }
802 /* Matched "*". */
803 *result = INTRINSIC_TIMES;
804 return MATCH_YES;
805
806 case '/':
807 ch = gfc_peek_ascii_char ();
808 if (ch == '=')
809 {
810 /* Matched "/=". */
811 gfc_next_ascii_char ();
812 *result = INTRINSIC_NE;
813 return MATCH_YES;
814 }
815 else if (ch == '/')
816 {
817 /* Matched "//". */
818 gfc_next_ascii_char ();
819 *result = INTRINSIC_CONCAT;
820 return MATCH_YES;
821 }
822 /* Matched "/". */
823 *result = INTRINSIC_DIVIDE;
824 return MATCH_YES;
825
826 case '.':
827 ch = gfc_next_ascii_char ();
828 switch (ch)
829 {
830 case 'a':
831 if (gfc_next_ascii_char () == 'n'
832 && gfc_next_ascii_char () == 'd'
833 && gfc_next_ascii_char () == '.')
834 {
835 /* Matched ".and.". */
836 *result = INTRINSIC_AND;
837 return MATCH_YES;
838 }
839 break;
840
841 case 'e':
842 if (gfc_next_ascii_char () == 'q')
843 {
844 ch = gfc_next_ascii_char ();
845 if (ch == '.')
846 {
847 /* Matched ".eq.". */
848 *result = INTRINSIC_EQ_OS;
849 return MATCH_YES;
850 }
851 else if (ch == 'v')
852 {
853 if (gfc_next_ascii_char () == '.')
854 {
855 /* Matched ".eqv.". */
856 *result = INTRINSIC_EQV;
857 return MATCH_YES;
858 }
859 }
860 }
861 break;
862
863 case 'g':
864 ch = gfc_next_ascii_char ();
865 if (ch == 'e')
866 {
867 if (gfc_next_ascii_char () == '.')
868 {
869 /* Matched ".ge.". */
870 *result = INTRINSIC_GE_OS;
871 return MATCH_YES;
872 }
873 }
874 else if (ch == 't')
875 {
876 if (gfc_next_ascii_char () == '.')
877 {
878 /* Matched ".gt.". */
879 *result = INTRINSIC_GT_OS;
880 return MATCH_YES;
881 }
882 }
883 break;
884
885 case 'l':
886 ch = gfc_next_ascii_char ();
887 if (ch == 'e')
888 {
889 if (gfc_next_ascii_char () == '.')
890 {
891 /* Matched ".le.". */
892 *result = INTRINSIC_LE_OS;
893 return MATCH_YES;
894 }
895 }
896 else if (ch == 't')
897 {
898 if (gfc_next_ascii_char () == '.')
899 {
900 /* Matched ".lt.". */
901 *result = INTRINSIC_LT_OS;
902 return MATCH_YES;
903 }
904 }
905 break;
906
907 case 'n':
908 ch = gfc_next_ascii_char ();
909 if (ch == 'e')
910 {
911 ch = gfc_next_ascii_char ();
912 if (ch == '.')
913 {
914 /* Matched ".ne.". */
915 *result = INTRINSIC_NE_OS;
916 return MATCH_YES;
917 }
918 else if (ch == 'q')
919 {
920 if (gfc_next_ascii_char () == 'v'
921 && gfc_next_ascii_char () == '.')
922 {
923 /* Matched ".neqv.". */
924 *result = INTRINSIC_NEQV;
925 return MATCH_YES;
926 }
927 }
928 }
929 else if (ch == 'o')
930 {
931 if (gfc_next_ascii_char () == 't'
932 && gfc_next_ascii_char () == '.')
933 {
934 /* Matched ".not.". */
935 *result = INTRINSIC_NOT;
936 return MATCH_YES;
937 }
938 }
939 break;
940
941 case 'o':
942 if (gfc_next_ascii_char () == 'r'
943 && gfc_next_ascii_char () == '.')
944 {
945 /* Matched ".or.". */
946 *result = INTRINSIC_OR;
947 return MATCH_YES;
948 }
949 break;
950
951 case 'x':
952 if (gfc_next_ascii_char () == 'o'
953 && gfc_next_ascii_char () == 'r'
954 && gfc_next_ascii_char () == '.')
955 {
956 if (!gfc_notify_std (GFC_STD_LEGACY, ".XOR. operator at %C"))
957 return MATCH_ERROR;
958 /* Matched ".xor." - equivalent to ".neqv.". */
959 *result = INTRINSIC_NEQV;
960 return MATCH_YES;
961 }
962 break;
963
964 default:
965 break;
966 }
967 break;
968
969 default:
970 break;
971 }
972
973 gfc_current_locus = orig_loc;
974 return MATCH_NO;
975 }
976
977
978 /* Match a loop control phrase:
979
980 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
981
982 If the final integer expression is not present, a constant unity
983 expression is returned. We don't return MATCH_ERROR until after
984 the equals sign is seen. */
985
986 match
987 gfc_match_iterator (gfc_iterator *iter, int init_flag)
988 {
989 char name[GFC_MAX_SYMBOL_LEN + 1];
990 gfc_expr *var, *e1, *e2, *e3;
991 locus start;
992 match m;
993
994 e1 = e2 = e3 = NULL;
995
996 /* Match the start of an iterator without affecting the symbol table. */
997
998 start = gfc_current_locus;
999 m = gfc_match (" %n =", name);
1000 gfc_current_locus = start;
1001
1002 if (m != MATCH_YES)
1003 return MATCH_NO;
1004
1005 m = gfc_match_variable (&var, 0);
1006 if (m != MATCH_YES)
1007 return MATCH_NO;
1008
1009 if (var->symtree->n.sym->attr.dimension)
1010 {
1011 gfc_error ("Loop variable at %C cannot be an array");
1012 goto cleanup;
1013 }
1014
1015 /* F2008, C617 & C565. */
1016 if (var->symtree->n.sym->attr.codimension)
1017 {
1018 gfc_error ("Loop variable at %C cannot be a coarray");
1019 goto cleanup;
1020 }
1021
1022 if (var->ref != NULL)
1023 {
1024 gfc_error ("Loop variable at %C cannot be a sub-component");
1025 goto cleanup;
1026 }
1027
1028 gfc_match_char ('=');
1029
1030 var->symtree->n.sym->attr.implied_index = 1;
1031
1032 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
1033 if (m == MATCH_NO)
1034 goto syntax;
1035 if (m == MATCH_ERROR)
1036 goto cleanup;
1037
1038 if (gfc_match_char (',') != MATCH_YES)
1039 goto syntax;
1040
1041 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
1042 if (m == MATCH_NO)
1043 goto syntax;
1044 if (m == MATCH_ERROR)
1045 goto cleanup;
1046
1047 if (gfc_match_char (',') != MATCH_YES)
1048 {
1049 e3 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
1050 goto done;
1051 }
1052
1053 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
1054 if (m == MATCH_ERROR)
1055 goto cleanup;
1056 if (m == MATCH_NO)
1057 {
1058 gfc_error ("Expected a step value in iterator at %C");
1059 goto cleanup;
1060 }
1061
1062 done:
1063 iter->var = var;
1064 iter->start = e1;
1065 iter->end = e2;
1066 iter->step = e3;
1067 return MATCH_YES;
1068
1069 syntax:
1070 gfc_error ("Syntax error in iterator at %C");
1071
1072 cleanup:
1073 gfc_free_expr (e1);
1074 gfc_free_expr (e2);
1075 gfc_free_expr (e3);
1076
1077 return MATCH_ERROR;
1078 }
1079
1080
1081 /* Tries to match the next non-whitespace character on the input.
1082 This subroutine does not return MATCH_ERROR. */
1083
1084 match
1085 gfc_match_char (char c)
1086 {
1087 locus where;
1088
1089 where = gfc_current_locus;
1090 gfc_gobble_whitespace ();
1091
1092 if (gfc_next_ascii_char () == c)
1093 return MATCH_YES;
1094
1095 gfc_current_locus = where;
1096 return MATCH_NO;
1097 }
1098
1099
1100 /* General purpose matching subroutine. The target string is a
1101 scanf-like format string in which spaces correspond to arbitrary
1102 whitespace (including no whitespace), characters correspond to
1103 themselves. The %-codes are:
1104
1105 %% Literal percent sign
1106 %e Expression, pointer to a pointer is set
1107 %s Symbol, pointer to the symbol is set
1108 %n Name, character buffer is set to name
1109 %t Matches end of statement.
1110 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1111 %l Matches a statement label
1112 %v Matches a variable expression (an lvalue)
1113 % Matches a required space (in free form) and optional spaces. */
1114
1115 match
1116 gfc_match (const char *target, ...)
1117 {
1118 gfc_st_label **label;
1119 int matches, *ip;
1120 locus old_loc;
1121 va_list argp;
1122 char c, *np;
1123 match m, n;
1124 void **vp;
1125 const char *p;
1126
1127 old_loc = gfc_current_locus;
1128 va_start (argp, target);
1129 m = MATCH_NO;
1130 matches = 0;
1131 p = target;
1132
1133 loop:
1134 c = *p++;
1135 switch (c)
1136 {
1137 case ' ':
1138 gfc_gobble_whitespace ();
1139 goto loop;
1140 case '\0':
1141 m = MATCH_YES;
1142 break;
1143
1144 case '%':
1145 c = *p++;
1146 switch (c)
1147 {
1148 case 'e':
1149 vp = va_arg (argp, void **);
1150 n = gfc_match_expr ((gfc_expr **) vp);
1151 if (n != MATCH_YES)
1152 {
1153 m = n;
1154 goto not_yes;
1155 }
1156
1157 matches++;
1158 goto loop;
1159
1160 case 'v':
1161 vp = va_arg (argp, void **);
1162 n = gfc_match_variable ((gfc_expr **) vp, 0);
1163 if (n != MATCH_YES)
1164 {
1165 m = n;
1166 goto not_yes;
1167 }
1168
1169 matches++;
1170 goto loop;
1171
1172 case 's':
1173 vp = va_arg (argp, void **);
1174 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1175 if (n != MATCH_YES)
1176 {
1177 m = n;
1178 goto not_yes;
1179 }
1180
1181 matches++;
1182 goto loop;
1183
1184 case 'n':
1185 np = va_arg (argp, char *);
1186 n = gfc_match_name (np);
1187 if (n != MATCH_YES)
1188 {
1189 m = n;
1190 goto not_yes;
1191 }
1192
1193 matches++;
1194 goto loop;
1195
1196 case 'l':
1197 label = va_arg (argp, gfc_st_label **);
1198 n = gfc_match_st_label (label);
1199 if (n != MATCH_YES)
1200 {
1201 m = n;
1202 goto not_yes;
1203 }
1204
1205 matches++;
1206 goto loop;
1207
1208 case 'o':
1209 ip = va_arg (argp, int *);
1210 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1211 if (n != MATCH_YES)
1212 {
1213 m = n;
1214 goto not_yes;
1215 }
1216
1217 matches++;
1218 goto loop;
1219
1220 case 't':
1221 if (gfc_match_eos () != MATCH_YES)
1222 {
1223 m = MATCH_NO;
1224 goto not_yes;
1225 }
1226 goto loop;
1227
1228 case ' ':
1229 if (gfc_match_space () == MATCH_YES)
1230 goto loop;
1231 m = MATCH_NO;
1232 goto not_yes;
1233
1234 case '%':
1235 break; /* Fall through to character matcher. */
1236
1237 default:
1238 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1239 }
1240
1241 default:
1242
1243 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1244 expect an upper case character here! */
1245 gcc_assert (TOLOWER (c) == c);
1246
1247 if (c == gfc_next_ascii_char ())
1248 goto loop;
1249 break;
1250 }
1251
1252 not_yes:
1253 va_end (argp);
1254
1255 if (m != MATCH_YES)
1256 {
1257 /* Clean up after a failed match. */
1258 gfc_current_locus = old_loc;
1259 va_start (argp, target);
1260
1261 p = target;
1262 for (; matches > 0; matches--)
1263 {
1264 while (*p++ != '%');
1265
1266 switch (*p++)
1267 {
1268 case '%':
1269 matches++;
1270 break; /* Skip. */
1271
1272 /* Matches that don't have to be undone */
1273 case 'o':
1274 case 'l':
1275 case 'n':
1276 case 's':
1277 (void) va_arg (argp, void **);
1278 break;
1279
1280 case 'e':
1281 case 'v':
1282 vp = va_arg (argp, void **);
1283 gfc_free_expr ((struct gfc_expr *)*vp);
1284 *vp = NULL;
1285 break;
1286 }
1287 }
1288
1289 va_end (argp);
1290 }
1291
1292 return m;
1293 }
1294
1295
1296 /*********************** Statement level matching **********************/
1297
1298 /* Matches the start of a program unit, which is the program keyword
1299 followed by an obligatory symbol. */
1300
1301 match
1302 gfc_match_program (void)
1303 {
1304 gfc_symbol *sym;
1305 match m;
1306
1307 m = gfc_match ("% %s%t", &sym);
1308
1309 if (m == MATCH_NO)
1310 {
1311 gfc_error ("Invalid form of PROGRAM statement at %C");
1312 m = MATCH_ERROR;
1313 }
1314
1315 if (m == MATCH_ERROR)
1316 return m;
1317
1318 if (!gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL))
1319 return MATCH_ERROR;
1320
1321 gfc_new_block = sym;
1322
1323 return MATCH_YES;
1324 }
1325
1326
1327 /* Match a simple assignment statement. */
1328
1329 match
1330 gfc_match_assignment (void)
1331 {
1332 gfc_expr *lvalue, *rvalue;
1333 locus old_loc;
1334 match m;
1335
1336 old_loc = gfc_current_locus;
1337
1338 lvalue = NULL;
1339 m = gfc_match (" %v =", &lvalue);
1340 if (m != MATCH_YES)
1341 {
1342 gfc_current_locus = old_loc;
1343 gfc_free_expr (lvalue);
1344 return MATCH_NO;
1345 }
1346
1347 rvalue = NULL;
1348 m = gfc_match (" %e%t", &rvalue);
1349 if (m != MATCH_YES)
1350 {
1351 gfc_current_locus = old_loc;
1352 gfc_free_expr (lvalue);
1353 gfc_free_expr (rvalue);
1354 return m;
1355 }
1356
1357 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1358
1359 new_st.op = EXEC_ASSIGN;
1360 new_st.expr1 = lvalue;
1361 new_st.expr2 = rvalue;
1362
1363 gfc_check_do_variable (lvalue->symtree);
1364
1365 return MATCH_YES;
1366 }
1367
1368
1369 /* Match a pointer assignment statement. */
1370
1371 match
1372 gfc_match_pointer_assignment (void)
1373 {
1374 gfc_expr *lvalue, *rvalue;
1375 locus old_loc;
1376 match m;
1377
1378 old_loc = gfc_current_locus;
1379
1380 lvalue = rvalue = NULL;
1381 gfc_matching_ptr_assignment = 0;
1382 gfc_matching_procptr_assignment = 0;
1383
1384 m = gfc_match (" %v =>", &lvalue);
1385 if (m != MATCH_YES)
1386 {
1387 m = MATCH_NO;
1388 goto cleanup;
1389 }
1390
1391 if (lvalue->symtree->n.sym->attr.proc_pointer
1392 || gfc_is_proc_ptr_comp (lvalue))
1393 gfc_matching_procptr_assignment = 1;
1394 else
1395 gfc_matching_ptr_assignment = 1;
1396
1397 m = gfc_match (" %e%t", &rvalue);
1398 gfc_matching_ptr_assignment = 0;
1399 gfc_matching_procptr_assignment = 0;
1400 if (m != MATCH_YES)
1401 goto cleanup;
1402
1403 new_st.op = EXEC_POINTER_ASSIGN;
1404 new_st.expr1 = lvalue;
1405 new_st.expr2 = rvalue;
1406
1407 return MATCH_YES;
1408
1409 cleanup:
1410 gfc_current_locus = old_loc;
1411 gfc_free_expr (lvalue);
1412 gfc_free_expr (rvalue);
1413 return m;
1414 }
1415
1416
1417 /* We try to match an easy arithmetic IF statement. This only happens
1418 when just after having encountered a simple IF statement. This code
1419 is really duplicate with parts of the gfc_match_if code, but this is
1420 *much* easier. */
1421
1422 static match
1423 match_arithmetic_if (void)
1424 {
1425 gfc_st_label *l1, *l2, *l3;
1426 gfc_expr *expr;
1427 match m;
1428
1429 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1430 if (m != MATCH_YES)
1431 return m;
1432
1433 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1434 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1435 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1436 {
1437 gfc_free_expr (expr);
1438 return MATCH_ERROR;
1439 }
1440
1441 if (!gfc_notify_std (GFC_STD_F95_OBS, "Arithmetic IF statement at %C"))
1442 return MATCH_ERROR;
1443
1444 new_st.op = EXEC_ARITHMETIC_IF;
1445 new_st.expr1 = expr;
1446 new_st.label1 = l1;
1447 new_st.label2 = l2;
1448 new_st.label3 = l3;
1449
1450 return MATCH_YES;
1451 }
1452
1453
1454 /* The IF statement is a bit of a pain. First of all, there are three
1455 forms of it, the simple IF, the IF that starts a block and the
1456 arithmetic IF.
1457
1458 There is a problem with the simple IF and that is the fact that we
1459 only have a single level of undo information on symbols. What this
1460 means is for a simple IF, we must re-match the whole IF statement
1461 multiple times in order to guarantee that the symbol table ends up
1462 in the proper state. */
1463
1464 static match match_simple_forall (void);
1465 static match match_simple_where (void);
1466
1467 match
1468 gfc_match_if (gfc_statement *if_type)
1469 {
1470 gfc_expr *expr;
1471 gfc_st_label *l1, *l2, *l3;
1472 locus old_loc, old_loc2;
1473 gfc_code *p;
1474 match m, n;
1475
1476 n = gfc_match_label ();
1477 if (n == MATCH_ERROR)
1478 return n;
1479
1480 old_loc = gfc_current_locus;
1481
1482 m = gfc_match (" if ( %e", &expr);
1483 if (m != MATCH_YES)
1484 return m;
1485
1486 old_loc2 = gfc_current_locus;
1487 gfc_current_locus = old_loc;
1488
1489 if (gfc_match_parens () == MATCH_ERROR)
1490 return MATCH_ERROR;
1491
1492 gfc_current_locus = old_loc2;
1493
1494 if (gfc_match_char (')') != MATCH_YES)
1495 {
1496 gfc_error ("Syntax error in IF-expression at %C");
1497 gfc_free_expr (expr);
1498 return MATCH_ERROR;
1499 }
1500
1501 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1502
1503 if (m == MATCH_YES)
1504 {
1505 if (n == MATCH_YES)
1506 {
1507 gfc_error ("Block label not appropriate for arithmetic IF "
1508 "statement at %C");
1509 gfc_free_expr (expr);
1510 return MATCH_ERROR;
1511 }
1512
1513 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1514 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1515 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1516 {
1517 gfc_free_expr (expr);
1518 return MATCH_ERROR;
1519 }
1520
1521 if (!gfc_notify_std (GFC_STD_F95_OBS, "Arithmetic IF statement at %C"))
1522 return MATCH_ERROR;
1523
1524 new_st.op = EXEC_ARITHMETIC_IF;
1525 new_st.expr1 = expr;
1526 new_st.label1 = l1;
1527 new_st.label2 = l2;
1528 new_st.label3 = l3;
1529
1530 *if_type = ST_ARITHMETIC_IF;
1531 return MATCH_YES;
1532 }
1533
1534 if (gfc_match (" then%t") == MATCH_YES)
1535 {
1536 new_st.op = EXEC_IF;
1537 new_st.expr1 = expr;
1538 *if_type = ST_IF_BLOCK;
1539 return MATCH_YES;
1540 }
1541
1542 if (n == MATCH_YES)
1543 {
1544 gfc_error ("Block label is not appropriate for IF statement at %C");
1545 gfc_free_expr (expr);
1546 return MATCH_ERROR;
1547 }
1548
1549 /* At this point the only thing left is a simple IF statement. At
1550 this point, n has to be MATCH_NO, so we don't have to worry about
1551 re-matching a block label. From what we've got so far, try
1552 matching an assignment. */
1553
1554 *if_type = ST_SIMPLE_IF;
1555
1556 m = gfc_match_assignment ();
1557 if (m == MATCH_YES)
1558 goto got_match;
1559
1560 gfc_free_expr (expr);
1561 gfc_undo_symbols ();
1562 gfc_current_locus = old_loc;
1563
1564 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1565 assignment was found. For MATCH_NO, continue to call the various
1566 matchers. */
1567 if (m == MATCH_ERROR)
1568 return MATCH_ERROR;
1569
1570 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1571
1572 m = gfc_match_pointer_assignment ();
1573 if (m == MATCH_YES)
1574 goto got_match;
1575
1576 gfc_free_expr (expr);
1577 gfc_undo_symbols ();
1578 gfc_current_locus = old_loc;
1579
1580 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1581
1582 /* Look at the next keyword to see which matcher to call. Matching
1583 the keyword doesn't affect the symbol table, so we don't have to
1584 restore between tries. */
1585
1586 #define match(string, subr, statement) \
1587 if (gfc_match (string) == MATCH_YES) { m = subr(); goto got_match; }
1588
1589 gfc_clear_error ();
1590
1591 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1592 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1593 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1594 match ("call", gfc_match_call, ST_CALL)
1595 match ("close", gfc_match_close, ST_CLOSE)
1596 match ("continue", gfc_match_continue, ST_CONTINUE)
1597 match ("cycle", gfc_match_cycle, ST_CYCLE)
1598 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1599 match ("end file", gfc_match_endfile, ST_END_FILE)
1600 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP)
1601 match ("event post", gfc_match_event_post, ST_EVENT_POST)
1602 match ("event wait", gfc_match_event_wait, ST_EVENT_WAIT)
1603 match ("exit", gfc_match_exit, ST_EXIT)
1604 match ("fail image", gfc_match_fail_image, ST_FAIL_IMAGE)
1605 match ("flush", gfc_match_flush, ST_FLUSH)
1606 match ("forall", match_simple_forall, ST_FORALL)
1607 match ("go to", gfc_match_goto, ST_GOTO)
1608 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1609 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1610 match ("lock", gfc_match_lock, ST_LOCK)
1611 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1612 match ("open", gfc_match_open, ST_OPEN)
1613 match ("pause", gfc_match_pause, ST_NONE)
1614 match ("print", gfc_match_print, ST_WRITE)
1615 match ("read", gfc_match_read, ST_READ)
1616 match ("return", gfc_match_return, ST_RETURN)
1617 match ("rewind", gfc_match_rewind, ST_REWIND)
1618 match ("stop", gfc_match_stop, ST_STOP)
1619 match ("wait", gfc_match_wait, ST_WAIT)
1620 match ("sync all", gfc_match_sync_all, ST_SYNC_CALL);
1621 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
1622 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
1623 match ("unlock", gfc_match_unlock, ST_UNLOCK)
1624 match ("where", match_simple_where, ST_WHERE)
1625 match ("write", gfc_match_write, ST_WRITE)
1626
1627 if (flag_dec)
1628 match ("type", gfc_match_print, ST_WRITE)
1629
1630 /* The gfc_match_assignment() above may have returned a MATCH_NO
1631 where the assignment was to a named constant. Check that
1632 special case here. */
1633 m = gfc_match_assignment ();
1634 if (m == MATCH_NO)
1635 {
1636 gfc_error ("Cannot assign to a named constant at %C");
1637 gfc_free_expr (expr);
1638 gfc_undo_symbols ();
1639 gfc_current_locus = old_loc;
1640 return MATCH_ERROR;
1641 }
1642
1643 /* All else has failed, so give up. See if any of the matchers has
1644 stored an error message of some sort. */
1645 if (!gfc_error_check ())
1646 gfc_error ("Unclassifiable statement in IF-clause at %C");
1647
1648 gfc_free_expr (expr);
1649 return MATCH_ERROR;
1650
1651 got_match:
1652 if (m == MATCH_NO)
1653 gfc_error ("Syntax error in IF-clause at %C");
1654 if (m != MATCH_YES)
1655 {
1656 gfc_free_expr (expr);
1657 return MATCH_ERROR;
1658 }
1659
1660 /* At this point, we've matched the single IF and the action clause
1661 is in new_st. Rearrange things so that the IF statement appears
1662 in new_st. */
1663
1664 p = gfc_get_code (EXEC_IF);
1665 p->next = XCNEW (gfc_code);
1666 *p->next = new_st;
1667 p->next->loc = gfc_current_locus;
1668
1669 p->expr1 = expr;
1670
1671 gfc_clear_new_st ();
1672
1673 new_st.op = EXEC_IF;
1674 new_st.block = p;
1675
1676 return MATCH_YES;
1677 }
1678
1679 #undef match
1680
1681
1682 /* Match an ELSE statement. */
1683
1684 match
1685 gfc_match_else (void)
1686 {
1687 char name[GFC_MAX_SYMBOL_LEN + 1];
1688
1689 if (gfc_match_eos () == MATCH_YES)
1690 return MATCH_YES;
1691
1692 if (gfc_match_name (name) != MATCH_YES
1693 || gfc_current_block () == NULL
1694 || gfc_match_eos () != MATCH_YES)
1695 {
1696 gfc_error ("Unexpected junk after ELSE statement at %C");
1697 return MATCH_ERROR;
1698 }
1699
1700 if (strcmp (name, gfc_current_block ()->name) != 0)
1701 {
1702 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1703 name, gfc_current_block ()->name);
1704 return MATCH_ERROR;
1705 }
1706
1707 return MATCH_YES;
1708 }
1709
1710
1711 /* Match an ELSE IF statement. */
1712
1713 match
1714 gfc_match_elseif (void)
1715 {
1716 char name[GFC_MAX_SYMBOL_LEN + 1];
1717 gfc_expr *expr;
1718 match m;
1719
1720 m = gfc_match (" ( %e ) then", &expr);
1721 if (m != MATCH_YES)
1722 return m;
1723
1724 if (gfc_match_eos () == MATCH_YES)
1725 goto done;
1726
1727 if (gfc_match_name (name) != MATCH_YES
1728 || gfc_current_block () == NULL
1729 || gfc_match_eos () != MATCH_YES)
1730 {
1731 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1732 goto cleanup;
1733 }
1734
1735 if (strcmp (name, gfc_current_block ()->name) != 0)
1736 {
1737 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1738 name, gfc_current_block ()->name);
1739 goto cleanup;
1740 }
1741
1742 done:
1743 new_st.op = EXEC_IF;
1744 new_st.expr1 = expr;
1745 return MATCH_YES;
1746
1747 cleanup:
1748 gfc_free_expr (expr);
1749 return MATCH_ERROR;
1750 }
1751
1752
1753 /* Free a gfc_iterator structure. */
1754
1755 void
1756 gfc_free_iterator (gfc_iterator *iter, int flag)
1757 {
1758
1759 if (iter == NULL)
1760 return;
1761
1762 gfc_free_expr (iter->var);
1763 gfc_free_expr (iter->start);
1764 gfc_free_expr (iter->end);
1765 gfc_free_expr (iter->step);
1766
1767 if (flag)
1768 free (iter);
1769 }
1770
1771
1772 /* Match a CRITICAL statement. */
1773 match
1774 gfc_match_critical (void)
1775 {
1776 gfc_st_label *label = NULL;
1777
1778 if (gfc_match_label () == MATCH_ERROR)
1779 return MATCH_ERROR;
1780
1781 if (gfc_match (" critical") != MATCH_YES)
1782 return MATCH_NO;
1783
1784 if (gfc_match_st_label (&label) == MATCH_ERROR)
1785 return MATCH_ERROR;
1786
1787 if (gfc_match_eos () != MATCH_YES)
1788 {
1789 gfc_syntax_error (ST_CRITICAL);
1790 return MATCH_ERROR;
1791 }
1792
1793 if (gfc_pure (NULL))
1794 {
1795 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1796 return MATCH_ERROR;
1797 }
1798
1799 if (gfc_find_state (COMP_DO_CONCURRENT))
1800 {
1801 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1802 "block");
1803 return MATCH_ERROR;
1804 }
1805
1806 gfc_unset_implicit_pure (NULL);
1807
1808 if (!gfc_notify_std (GFC_STD_F2008, "CRITICAL statement at %C"))
1809 return MATCH_ERROR;
1810
1811 if (flag_coarray == GFC_FCOARRAY_NONE)
1812 {
1813 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
1814 "enable");
1815 return MATCH_ERROR;
1816 }
1817
1818 if (gfc_find_state (COMP_CRITICAL))
1819 {
1820 gfc_error ("Nested CRITICAL block at %C");
1821 return MATCH_ERROR;
1822 }
1823
1824 new_st.op = EXEC_CRITICAL;
1825
1826 if (label != NULL
1827 && !gfc_reference_st_label (label, ST_LABEL_TARGET))
1828 return MATCH_ERROR;
1829
1830 return MATCH_YES;
1831 }
1832
1833
1834 /* Match a BLOCK statement. */
1835
1836 match
1837 gfc_match_block (void)
1838 {
1839 match m;
1840
1841 if (gfc_match_label () == MATCH_ERROR)
1842 return MATCH_ERROR;
1843
1844 if (gfc_match (" block") != MATCH_YES)
1845 return MATCH_NO;
1846
1847 /* For this to be a correct BLOCK statement, the line must end now. */
1848 m = gfc_match_eos ();
1849 if (m == MATCH_ERROR)
1850 return MATCH_ERROR;
1851 if (m == MATCH_NO)
1852 return MATCH_NO;
1853
1854 return MATCH_YES;
1855 }
1856
1857
1858 /* Match an ASSOCIATE statement. */
1859
1860 match
1861 gfc_match_associate (void)
1862 {
1863 if (gfc_match_label () == MATCH_ERROR)
1864 return MATCH_ERROR;
1865
1866 if (gfc_match (" associate") != MATCH_YES)
1867 return MATCH_NO;
1868
1869 /* Match the association list. */
1870 if (gfc_match_char ('(') != MATCH_YES)
1871 {
1872 gfc_error ("Expected association list at %C");
1873 return MATCH_ERROR;
1874 }
1875 new_st.ext.block.assoc = NULL;
1876 while (true)
1877 {
1878 gfc_association_list* newAssoc = gfc_get_association_list ();
1879 gfc_association_list* a;
1880
1881 /* Match the next association. */
1882 if (gfc_match (" %n => %e", newAssoc->name, &newAssoc->target)
1883 != MATCH_YES)
1884 {
1885 gfc_error ("Expected association at %C");
1886 goto assocListError;
1887 }
1888 newAssoc->where = gfc_current_locus;
1889
1890 /* Check that the current name is not yet in the list. */
1891 for (a = new_st.ext.block.assoc; a; a = a->next)
1892 if (!strcmp (a->name, newAssoc->name))
1893 {
1894 gfc_error ("Duplicate name %qs in association at %C",
1895 newAssoc->name);
1896 goto assocListError;
1897 }
1898
1899 /* The target expression must not be coindexed. */
1900 if (gfc_is_coindexed (newAssoc->target))
1901 {
1902 gfc_error ("Association target at %C must not be coindexed");
1903 goto assocListError;
1904 }
1905
1906 /* The `variable' field is left blank for now; because the target is not
1907 yet resolved, we can't use gfc_has_vector_subscript to determine it
1908 for now. This is set during resolution. */
1909
1910 /* Put it into the list. */
1911 newAssoc->next = new_st.ext.block.assoc;
1912 new_st.ext.block.assoc = newAssoc;
1913
1914 /* Try next one or end if closing parenthesis is found. */
1915 gfc_gobble_whitespace ();
1916 if (gfc_peek_char () == ')')
1917 break;
1918 if (gfc_match_char (',') != MATCH_YES)
1919 {
1920 gfc_error ("Expected %<)%> or %<,%> at %C");
1921 return MATCH_ERROR;
1922 }
1923
1924 continue;
1925
1926 assocListError:
1927 free (newAssoc);
1928 goto error;
1929 }
1930 if (gfc_match_char (')') != MATCH_YES)
1931 {
1932 /* This should never happen as we peek above. */
1933 gcc_unreachable ();
1934 }
1935
1936 if (gfc_match_eos () != MATCH_YES)
1937 {
1938 gfc_error ("Junk after ASSOCIATE statement at %C");
1939 goto error;
1940 }
1941
1942 return MATCH_YES;
1943
1944 error:
1945 gfc_free_association_list (new_st.ext.block.assoc);
1946 return MATCH_ERROR;
1947 }
1948
1949
1950 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
1951 an accessible derived type. */
1952
1953 static match
1954 match_derived_type_spec (gfc_typespec *ts)
1955 {
1956 char name[GFC_MAX_SYMBOL_LEN + 1];
1957 locus old_locus;
1958 gfc_symbol *derived;
1959
1960 old_locus = gfc_current_locus;
1961
1962 if (gfc_match ("%n", name) != MATCH_YES)
1963 {
1964 gfc_current_locus = old_locus;
1965 return MATCH_NO;
1966 }
1967
1968 gfc_find_symbol (name, NULL, 1, &derived);
1969
1970 if (derived && derived->attr.flavor == FL_PROCEDURE && derived->attr.generic)
1971 derived = gfc_find_dt_in_generic (derived);
1972
1973 if (derived && derived->attr.flavor == FL_DERIVED)
1974 {
1975 ts->type = BT_DERIVED;
1976 ts->u.derived = derived;
1977 return MATCH_YES;
1978 }
1979
1980 gfc_current_locus = old_locus;
1981 return MATCH_NO;
1982 }
1983
1984
1985 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
1986 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
1987 It only includes the intrinsic types from the Fortran 2003 standard
1988 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
1989 the implicit_flag is not needed, so it was removed. Derived types are
1990 identified by their name alone. */
1991
1992 match
1993 gfc_match_type_spec (gfc_typespec *ts)
1994 {
1995 match m;
1996 locus old_locus;
1997 char name[GFC_MAX_SYMBOL_LEN + 1];
1998
1999 gfc_clear_ts (ts);
2000 gfc_gobble_whitespace ();
2001 old_locus = gfc_current_locus;
2002
2003 if (match_derived_type_spec (ts) == MATCH_YES)
2004 {
2005 /* Enforce F03:C401. */
2006 if (ts->u.derived->attr.abstract)
2007 {
2008 gfc_error ("Derived type %qs at %L may not be ABSTRACT",
2009 ts->u.derived->name, &old_locus);
2010 return MATCH_ERROR;
2011 }
2012 return MATCH_YES;
2013 }
2014
2015 if (gfc_match ("integer") == MATCH_YES)
2016 {
2017 ts->type = BT_INTEGER;
2018 ts->kind = gfc_default_integer_kind;
2019 goto kind_selector;
2020 }
2021
2022 if (gfc_match ("double precision") == MATCH_YES)
2023 {
2024 ts->type = BT_REAL;
2025 ts->kind = gfc_default_double_kind;
2026 return MATCH_YES;
2027 }
2028
2029 if (gfc_match ("complex") == MATCH_YES)
2030 {
2031 ts->type = BT_COMPLEX;
2032 ts->kind = gfc_default_complex_kind;
2033 goto kind_selector;
2034 }
2035
2036 if (gfc_match ("character") == MATCH_YES)
2037 {
2038 ts->type = BT_CHARACTER;
2039
2040 m = gfc_match_char_spec (ts);
2041
2042 if (m == MATCH_NO)
2043 m = MATCH_YES;
2044
2045 return m;
2046 }
2047
2048 if (gfc_match ("logical") == MATCH_YES)
2049 {
2050 ts->type = BT_LOGICAL;
2051 ts->kind = gfc_default_logical_kind;
2052 goto kind_selector;
2053 }
2054
2055 /* REAL is a real pain because it can be a type, intrinsic subprogram,
2056 or list item in a type-list of an OpenMP reduction clause. Need to
2057 differentiate REAL([KIND]=scalar-int-initialization-expr) from
2058 REAL(A,[KIND]) and REAL(KIND,A). */
2059
2060 m = gfc_match (" %n", name);
2061 if (m == MATCH_YES && strcmp (name, "real") == 0)
2062 {
2063 char c;
2064 gfc_expr *e;
2065 locus where;
2066
2067 ts->type = BT_REAL;
2068 ts->kind = gfc_default_real_kind;
2069
2070 gfc_gobble_whitespace ();
2071
2072 /* Prevent REAL*4, etc. */
2073 c = gfc_peek_ascii_char ();
2074 if (c == '*')
2075 {
2076 gfc_error ("Invalid type-spec at %C");
2077 return MATCH_ERROR;
2078 }
2079
2080 /* Found leading colon in REAL::, a trailing ')' in for example
2081 TYPE IS (REAL), or REAL, for an OpenMP list-item. */
2082 if (c == ':' || c == ')' || (flag_openmp && c == ','))
2083 return MATCH_YES;
2084
2085 /* Found something other than the opening '(' in REAL(... */
2086 if (c != '(')
2087 return MATCH_NO;
2088 else
2089 gfc_next_char (); /* Burn the '('. */
2090
2091 /* Look for the optional KIND=. */
2092 where = gfc_current_locus;
2093 m = gfc_match ("%n", name);
2094 if (m == MATCH_YES)
2095 {
2096 gfc_gobble_whitespace ();
2097 c = gfc_next_char ();
2098 if (c == '=')
2099 {
2100 if (strcmp(name, "a") == 0)
2101 return MATCH_NO;
2102 else if (strcmp(name, "kind") == 0)
2103 goto found;
2104 else
2105 return MATCH_ERROR;
2106 }
2107 else
2108 gfc_current_locus = where;
2109 }
2110 else
2111 gfc_current_locus = where;
2112
2113 found:
2114
2115 m = gfc_match_init_expr (&e);
2116 if (m == MATCH_NO || m == MATCH_ERROR)
2117 return MATCH_NO;
2118
2119 /* If a comma appears, it is an intrinsic subprogram. */
2120 gfc_gobble_whitespace ();
2121 c = gfc_peek_ascii_char ();
2122 if (c == ',')
2123 {
2124 gfc_free_expr (e);
2125 return MATCH_NO;
2126 }
2127
2128 /* If ')' appears, we have REAL(initialization-expr), here check for
2129 a scalar integer initialization-expr and valid kind parameter. */
2130 if (c == ')')
2131 {
2132 if (e->ts.type != BT_INTEGER || e->rank > 0)
2133 {
2134 gfc_free_expr (e);
2135 return MATCH_NO;
2136 }
2137
2138 gfc_next_char (); /* Burn the ')'. */
2139 ts->kind = (int) mpz_get_si (e->value.integer);
2140 if (gfc_validate_kind (BT_REAL, ts->kind , true) == -1)
2141 {
2142 gfc_error ("Invalid type-spec at %C");
2143 return MATCH_ERROR;
2144 }
2145
2146 gfc_free_expr (e);
2147
2148 return MATCH_YES;
2149 }
2150 }
2151
2152 /* If a type is not matched, simply return MATCH_NO. */
2153 gfc_current_locus = old_locus;
2154 return MATCH_NO;
2155
2156 kind_selector:
2157
2158 gfc_gobble_whitespace ();
2159
2160 /* This prevents INTEGER*4, etc. */
2161 if (gfc_peek_ascii_char () == '*')
2162 {
2163 gfc_error ("Invalid type-spec at %C");
2164 return MATCH_ERROR;
2165 }
2166
2167 m = gfc_match_kind_spec (ts, false);
2168
2169 /* No kind specifier found. */
2170 if (m == MATCH_NO)
2171 m = MATCH_YES;
2172
2173 return m;
2174 }
2175
2176
2177 /******************** FORALL subroutines ********************/
2178
2179 /* Free a list of FORALL iterators. */
2180
2181 void
2182 gfc_free_forall_iterator (gfc_forall_iterator *iter)
2183 {
2184 gfc_forall_iterator *next;
2185
2186 while (iter)
2187 {
2188 next = iter->next;
2189 gfc_free_expr (iter->var);
2190 gfc_free_expr (iter->start);
2191 gfc_free_expr (iter->end);
2192 gfc_free_expr (iter->stride);
2193 free (iter);
2194 iter = next;
2195 }
2196 }
2197
2198
2199 /* Match an iterator as part of a FORALL statement. The format is:
2200
2201 <var> = <start>:<end>[:<stride>]
2202
2203 On MATCH_NO, the caller tests for the possibility that there is a
2204 scalar mask expression. */
2205
2206 static match
2207 match_forall_iterator (gfc_forall_iterator **result)
2208 {
2209 gfc_forall_iterator *iter;
2210 locus where;
2211 match m;
2212
2213 where = gfc_current_locus;
2214 iter = XCNEW (gfc_forall_iterator);
2215
2216 m = gfc_match_expr (&iter->var);
2217 if (m != MATCH_YES)
2218 goto cleanup;
2219
2220 if (gfc_match_char ('=') != MATCH_YES
2221 || iter->var->expr_type != EXPR_VARIABLE)
2222 {
2223 m = MATCH_NO;
2224 goto cleanup;
2225 }
2226
2227 m = gfc_match_expr (&iter->start);
2228 if (m != MATCH_YES)
2229 goto cleanup;
2230
2231 if (gfc_match_char (':') != MATCH_YES)
2232 goto syntax;
2233
2234 m = gfc_match_expr (&iter->end);
2235 if (m == MATCH_NO)
2236 goto syntax;
2237 if (m == MATCH_ERROR)
2238 goto cleanup;
2239
2240 if (gfc_match_char (':') == MATCH_NO)
2241 iter->stride = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2242 else
2243 {
2244 m = gfc_match_expr (&iter->stride);
2245 if (m == MATCH_NO)
2246 goto syntax;
2247 if (m == MATCH_ERROR)
2248 goto cleanup;
2249 }
2250
2251 /* Mark the iteration variable's symbol as used as a FORALL index. */
2252 iter->var->symtree->n.sym->forall_index = true;
2253
2254 *result = iter;
2255 return MATCH_YES;
2256
2257 syntax:
2258 gfc_error ("Syntax error in FORALL iterator at %C");
2259 m = MATCH_ERROR;
2260
2261 cleanup:
2262
2263 gfc_current_locus = where;
2264 gfc_free_forall_iterator (iter);
2265 return m;
2266 }
2267
2268
2269 /* Match the header of a FORALL statement. */
2270
2271 static match
2272 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
2273 {
2274 gfc_forall_iterator *head, *tail, *new_iter;
2275 gfc_expr *msk;
2276 match m;
2277
2278 gfc_gobble_whitespace ();
2279
2280 head = tail = NULL;
2281 msk = NULL;
2282
2283 if (gfc_match_char ('(') != MATCH_YES)
2284 return MATCH_NO;
2285
2286 m = match_forall_iterator (&new_iter);
2287 if (m == MATCH_ERROR)
2288 goto cleanup;
2289 if (m == MATCH_NO)
2290 goto syntax;
2291
2292 head = tail = new_iter;
2293
2294 for (;;)
2295 {
2296 if (gfc_match_char (',') != MATCH_YES)
2297 break;
2298
2299 m = match_forall_iterator (&new_iter);
2300 if (m == MATCH_ERROR)
2301 goto cleanup;
2302
2303 if (m == MATCH_YES)
2304 {
2305 tail->next = new_iter;
2306 tail = new_iter;
2307 continue;
2308 }
2309
2310 /* Have to have a mask expression. */
2311
2312 m = gfc_match_expr (&msk);
2313 if (m == MATCH_NO)
2314 goto syntax;
2315 if (m == MATCH_ERROR)
2316 goto cleanup;
2317
2318 break;
2319 }
2320
2321 if (gfc_match_char (')') == MATCH_NO)
2322 goto syntax;
2323
2324 *phead = head;
2325 *mask = msk;
2326 return MATCH_YES;
2327
2328 syntax:
2329 gfc_syntax_error (ST_FORALL);
2330
2331 cleanup:
2332 gfc_free_expr (msk);
2333 gfc_free_forall_iterator (head);
2334
2335 return MATCH_ERROR;
2336 }
2337
2338 /* Match the rest of a simple FORALL statement that follows an
2339 IF statement. */
2340
2341 static match
2342 match_simple_forall (void)
2343 {
2344 gfc_forall_iterator *head;
2345 gfc_expr *mask;
2346 gfc_code *c;
2347 match m;
2348
2349 mask = NULL;
2350 head = NULL;
2351 c = NULL;
2352
2353 m = match_forall_header (&head, &mask);
2354
2355 if (m == MATCH_NO)
2356 goto syntax;
2357 if (m != MATCH_YES)
2358 goto cleanup;
2359
2360 m = gfc_match_assignment ();
2361
2362 if (m == MATCH_ERROR)
2363 goto cleanup;
2364 if (m == MATCH_NO)
2365 {
2366 m = gfc_match_pointer_assignment ();
2367 if (m == MATCH_ERROR)
2368 goto cleanup;
2369 if (m == MATCH_NO)
2370 goto syntax;
2371 }
2372
2373 c = XCNEW (gfc_code);
2374 *c = new_st;
2375 c->loc = gfc_current_locus;
2376
2377 if (gfc_match_eos () != MATCH_YES)
2378 goto syntax;
2379
2380 gfc_clear_new_st ();
2381 new_st.op = EXEC_FORALL;
2382 new_st.expr1 = mask;
2383 new_st.ext.forall_iterator = head;
2384 new_st.block = gfc_get_code (EXEC_FORALL);
2385 new_st.block->next = c;
2386
2387 return MATCH_YES;
2388
2389 syntax:
2390 gfc_syntax_error (ST_FORALL);
2391
2392 cleanup:
2393 gfc_free_forall_iterator (head);
2394 gfc_free_expr (mask);
2395
2396 return MATCH_ERROR;
2397 }
2398
2399
2400 /* Match a FORALL statement. */
2401
2402 match
2403 gfc_match_forall (gfc_statement *st)
2404 {
2405 gfc_forall_iterator *head;
2406 gfc_expr *mask;
2407 gfc_code *c;
2408 match m0, m;
2409
2410 head = NULL;
2411 mask = NULL;
2412 c = NULL;
2413
2414 m0 = gfc_match_label ();
2415 if (m0 == MATCH_ERROR)
2416 return MATCH_ERROR;
2417
2418 m = gfc_match (" forall");
2419 if (m != MATCH_YES)
2420 return m;
2421
2422 m = match_forall_header (&head, &mask);
2423 if (m == MATCH_ERROR)
2424 goto cleanup;
2425 if (m == MATCH_NO)
2426 goto syntax;
2427
2428 if (gfc_match_eos () == MATCH_YES)
2429 {
2430 *st = ST_FORALL_BLOCK;
2431 new_st.op = EXEC_FORALL;
2432 new_st.expr1 = mask;
2433 new_st.ext.forall_iterator = head;
2434 return MATCH_YES;
2435 }
2436
2437 m = gfc_match_assignment ();
2438 if (m == MATCH_ERROR)
2439 goto cleanup;
2440 if (m == MATCH_NO)
2441 {
2442 m = gfc_match_pointer_assignment ();
2443 if (m == MATCH_ERROR)
2444 goto cleanup;
2445 if (m == MATCH_NO)
2446 goto syntax;
2447 }
2448
2449 c = XCNEW (gfc_code);
2450 *c = new_st;
2451 c->loc = gfc_current_locus;
2452
2453 gfc_clear_new_st ();
2454 new_st.op = EXEC_FORALL;
2455 new_st.expr1 = mask;
2456 new_st.ext.forall_iterator = head;
2457 new_st.block = gfc_get_code (EXEC_FORALL);
2458 new_st.block->next = c;
2459
2460 *st = ST_FORALL;
2461 return MATCH_YES;
2462
2463 syntax:
2464 gfc_syntax_error (ST_FORALL);
2465
2466 cleanup:
2467 gfc_free_forall_iterator (head);
2468 gfc_free_expr (mask);
2469 gfc_free_statements (c);
2470 return MATCH_NO;
2471 }
2472
2473
2474 /* Match a DO statement. */
2475
2476 match
2477 gfc_match_do (void)
2478 {
2479 gfc_iterator iter, *ip;
2480 locus old_loc;
2481 gfc_st_label *label;
2482 match m;
2483
2484 old_loc = gfc_current_locus;
2485
2486 label = NULL;
2487 iter.var = iter.start = iter.end = iter.step = NULL;
2488
2489 m = gfc_match_label ();
2490 if (m == MATCH_ERROR)
2491 return m;
2492
2493 if (gfc_match (" do") != MATCH_YES)
2494 return MATCH_NO;
2495
2496 m = gfc_match_st_label (&label);
2497 if (m == MATCH_ERROR)
2498 goto cleanup;
2499
2500 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2501
2502 if (gfc_match_eos () == MATCH_YES)
2503 {
2504 iter.end = gfc_get_logical_expr (gfc_default_logical_kind, NULL, true);
2505 new_st.op = EXEC_DO_WHILE;
2506 goto done;
2507 }
2508
2509 /* Match an optional comma, if no comma is found, a space is obligatory. */
2510 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
2511 return MATCH_NO;
2512
2513 /* Check for balanced parens. */
2514
2515 if (gfc_match_parens () == MATCH_ERROR)
2516 return MATCH_ERROR;
2517
2518 if (gfc_match (" concurrent") == MATCH_YES)
2519 {
2520 gfc_forall_iterator *head;
2521 gfc_expr *mask;
2522
2523 if (!gfc_notify_std (GFC_STD_F2008, "DO CONCURRENT construct at %C"))
2524 return MATCH_ERROR;
2525
2526
2527 mask = NULL;
2528 head = NULL;
2529 m = match_forall_header (&head, &mask);
2530
2531 if (m == MATCH_NO)
2532 return m;
2533 if (m == MATCH_ERROR)
2534 goto concurr_cleanup;
2535
2536 if (gfc_match_eos () != MATCH_YES)
2537 goto concurr_cleanup;
2538
2539 if (label != NULL
2540 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2541 goto concurr_cleanup;
2542
2543 new_st.label1 = label;
2544 new_st.op = EXEC_DO_CONCURRENT;
2545 new_st.expr1 = mask;
2546 new_st.ext.forall_iterator = head;
2547
2548 return MATCH_YES;
2549
2550 concurr_cleanup:
2551 gfc_syntax_error (ST_DO);
2552 gfc_free_expr (mask);
2553 gfc_free_forall_iterator (head);
2554 return MATCH_ERROR;
2555 }
2556
2557 /* See if we have a DO WHILE. */
2558 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
2559 {
2560 new_st.op = EXEC_DO_WHILE;
2561 goto done;
2562 }
2563
2564 /* The abortive DO WHILE may have done something to the symbol
2565 table, so we start over. */
2566 gfc_undo_symbols ();
2567 gfc_current_locus = old_loc;
2568
2569 gfc_match_label (); /* This won't error. */
2570 gfc_match (" do "); /* This will work. */
2571
2572 gfc_match_st_label (&label); /* Can't error out. */
2573 gfc_match_char (','); /* Optional comma. */
2574
2575 m = gfc_match_iterator (&iter, 0);
2576 if (m == MATCH_NO)
2577 return MATCH_NO;
2578 if (m == MATCH_ERROR)
2579 goto cleanup;
2580
2581 iter.var->symtree->n.sym->attr.implied_index = 0;
2582 gfc_check_do_variable (iter.var->symtree);
2583
2584 if (gfc_match_eos () != MATCH_YES)
2585 {
2586 gfc_syntax_error (ST_DO);
2587 goto cleanup;
2588 }
2589
2590 new_st.op = EXEC_DO;
2591
2592 done:
2593 if (label != NULL
2594 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2595 goto cleanup;
2596
2597 new_st.label1 = label;
2598
2599 if (new_st.op == EXEC_DO_WHILE)
2600 new_st.expr1 = iter.end;
2601 else
2602 {
2603 new_st.ext.iterator = ip = gfc_get_iterator ();
2604 *ip = iter;
2605 }
2606
2607 return MATCH_YES;
2608
2609 cleanup:
2610 gfc_free_iterator (&iter, 0);
2611
2612 return MATCH_ERROR;
2613 }
2614
2615
2616 /* Match an EXIT or CYCLE statement. */
2617
2618 static match
2619 match_exit_cycle (gfc_statement st, gfc_exec_op op)
2620 {
2621 gfc_state_data *p, *o;
2622 gfc_symbol *sym;
2623 match m;
2624 int cnt;
2625
2626 if (gfc_match_eos () == MATCH_YES)
2627 sym = NULL;
2628 else
2629 {
2630 char name[GFC_MAX_SYMBOL_LEN + 1];
2631 gfc_symtree* stree;
2632
2633 m = gfc_match ("% %n%t", name);
2634 if (m == MATCH_ERROR)
2635 return MATCH_ERROR;
2636 if (m == MATCH_NO)
2637 {
2638 gfc_syntax_error (st);
2639 return MATCH_ERROR;
2640 }
2641
2642 /* Find the corresponding symbol. If there's a BLOCK statement
2643 between here and the label, it is not in gfc_current_ns but a parent
2644 namespace! */
2645 stree = gfc_find_symtree_in_proc (name, gfc_current_ns);
2646 if (!stree)
2647 {
2648 gfc_error ("Name %qs in %s statement at %C is unknown",
2649 name, gfc_ascii_statement (st));
2650 return MATCH_ERROR;
2651 }
2652
2653 sym = stree->n.sym;
2654 if (sym->attr.flavor != FL_LABEL)
2655 {
2656 gfc_error ("Name %qs in %s statement at %C is not a construct name",
2657 name, gfc_ascii_statement (st));
2658 return MATCH_ERROR;
2659 }
2660 }
2661
2662 /* Find the loop specified by the label (or lack of a label). */
2663 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
2664 if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
2665 o = p;
2666 else if (p->state == COMP_CRITICAL)
2667 {
2668 gfc_error("%s statement at %C leaves CRITICAL construct",
2669 gfc_ascii_statement (st));
2670 return MATCH_ERROR;
2671 }
2672 else if (p->state == COMP_DO_CONCURRENT
2673 && (op == EXEC_EXIT || (sym && sym != p->sym)))
2674 {
2675 /* F2008, C821 & C845. */
2676 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2677 gfc_ascii_statement (st));
2678 return MATCH_ERROR;
2679 }
2680 else if ((sym && sym == p->sym)
2681 || (!sym && (p->state == COMP_DO
2682 || p->state == COMP_DO_CONCURRENT)))
2683 break;
2684
2685 if (p == NULL)
2686 {
2687 if (sym == NULL)
2688 gfc_error ("%s statement at %C is not within a construct",
2689 gfc_ascii_statement (st));
2690 else
2691 gfc_error ("%s statement at %C is not within construct %qs",
2692 gfc_ascii_statement (st), sym->name);
2693
2694 return MATCH_ERROR;
2695 }
2696
2697 /* Special checks for EXIT from non-loop constructs. */
2698 switch (p->state)
2699 {
2700 case COMP_DO:
2701 case COMP_DO_CONCURRENT:
2702 break;
2703
2704 case COMP_CRITICAL:
2705 /* This is already handled above. */
2706 gcc_unreachable ();
2707
2708 case COMP_ASSOCIATE:
2709 case COMP_BLOCK:
2710 case COMP_IF:
2711 case COMP_SELECT:
2712 case COMP_SELECT_TYPE:
2713 gcc_assert (sym);
2714 if (op == EXEC_CYCLE)
2715 {
2716 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2717 " construct %qs", sym->name);
2718 return MATCH_ERROR;
2719 }
2720 gcc_assert (op == EXEC_EXIT);
2721 if (!gfc_notify_std (GFC_STD_F2008, "EXIT statement with no"
2722 " do-construct-name at %C"))
2723 return MATCH_ERROR;
2724 break;
2725
2726 default:
2727 gfc_error ("%s statement at %C is not applicable to construct %qs",
2728 gfc_ascii_statement (st), sym->name);
2729 return MATCH_ERROR;
2730 }
2731
2732 if (o != NULL)
2733 {
2734 gfc_error (is_oacc (p)
2735 ? G_("%s statement at %C leaving OpenACC structured block")
2736 : G_("%s statement at %C leaving OpenMP structured block"),
2737 gfc_ascii_statement (st));
2738 return MATCH_ERROR;
2739 }
2740
2741 for (o = p, cnt = 0; o->state == COMP_DO && o->previous != NULL; cnt++)
2742 o = o->previous;
2743 if (cnt > 0
2744 && o != NULL
2745 && o->state == COMP_OMP_STRUCTURED_BLOCK
2746 && (o->head->op == EXEC_OACC_LOOP
2747 || o->head->op == EXEC_OACC_PARALLEL_LOOP))
2748 {
2749 int collapse = 1;
2750 gcc_assert (o->head->next != NULL
2751 && (o->head->next->op == EXEC_DO
2752 || o->head->next->op == EXEC_DO_WHILE)
2753 && o->previous != NULL
2754 && o->previous->tail->op == o->head->op);
2755 if (o->previous->tail->ext.omp_clauses != NULL
2756 && o->previous->tail->ext.omp_clauses->collapse > 1)
2757 collapse = o->previous->tail->ext.omp_clauses->collapse;
2758 if (st == ST_EXIT && cnt <= collapse)
2759 {
2760 gfc_error ("EXIT statement at %C terminating !$ACC LOOP loop");
2761 return MATCH_ERROR;
2762 }
2763 if (st == ST_CYCLE && cnt < collapse)
2764 {
2765 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2766 " !$ACC LOOP loop");
2767 return MATCH_ERROR;
2768 }
2769 }
2770 if (cnt > 0
2771 && o != NULL
2772 && (o->state == COMP_OMP_STRUCTURED_BLOCK)
2773 && (o->head->op == EXEC_OMP_DO
2774 || o->head->op == EXEC_OMP_PARALLEL_DO
2775 || o->head->op == EXEC_OMP_SIMD
2776 || o->head->op == EXEC_OMP_DO_SIMD
2777 || o->head->op == EXEC_OMP_PARALLEL_DO_SIMD))
2778 {
2779 int count = 1;
2780 gcc_assert (o->head->next != NULL
2781 && (o->head->next->op == EXEC_DO
2782 || o->head->next->op == EXEC_DO_WHILE)
2783 && o->previous != NULL
2784 && o->previous->tail->op == o->head->op);
2785 if (o->previous->tail->ext.omp_clauses != NULL)
2786 {
2787 if (o->previous->tail->ext.omp_clauses->collapse > 1)
2788 count = o->previous->tail->ext.omp_clauses->collapse;
2789 if (o->previous->tail->ext.omp_clauses->orderedc)
2790 count = o->previous->tail->ext.omp_clauses->orderedc;
2791 }
2792 if (st == ST_EXIT && cnt <= count)
2793 {
2794 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2795 return MATCH_ERROR;
2796 }
2797 if (st == ST_CYCLE && cnt < count)
2798 {
2799 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2800 " !$OMP DO loop");
2801 return MATCH_ERROR;
2802 }
2803 }
2804
2805 /* Save the first statement in the construct - needed by the backend. */
2806 new_st.ext.which_construct = p->construct;
2807
2808 new_st.op = op;
2809
2810 return MATCH_YES;
2811 }
2812
2813
2814 /* Match the EXIT statement. */
2815
2816 match
2817 gfc_match_exit (void)
2818 {
2819 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
2820 }
2821
2822
2823 /* Match the CYCLE statement. */
2824
2825 match
2826 gfc_match_cycle (void)
2827 {
2828 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
2829 }
2830
2831
2832 /* Match a stop-code after an (ERROR) STOP or PAUSE statement. The
2833 requirements for a stop-code differ in the standards.
2834
2835 Fortran 95 has
2836
2837 R840 stop-stmt is STOP [ stop-code ]
2838 R841 stop-code is scalar-char-constant
2839 or digit [ digit [ digit [ digit [ digit ] ] ] ]
2840
2841 Fortran 2003 matches Fortran 95 except R840 and R841 are now R849 and R850.
2842 Fortran 2008 has
2843
2844 R855 stop-stmt is STOP [ stop-code ]
2845 R856 allstop-stmt is ALL STOP [ stop-code ]
2846 R857 stop-code is scalar-default-char-constant-expr
2847 or scalar-int-constant-expr
2848
2849 For free-form source code, all standards contain a statement of the form:
2850
2851 A blank shall be used to separate names, constants, or labels from
2852 adjacent keywords, names, constants, or labels.
2853
2854 A stop-code is not a name, constant, or label. So, under Fortran 95 and 2003,
2855
2856 STOP123
2857
2858 is valid, but it is invalid Fortran 2008. */
2859
2860 static match
2861 gfc_match_stopcode (gfc_statement st)
2862 {
2863 gfc_expr *e = NULL;
2864 match m;
2865 bool f95, f03;
2866
2867 /* Set f95 for -std=f95. */
2868 f95 = gfc_option.allow_std == (GFC_STD_F95_OBS | GFC_STD_F95 | GFC_STD_F77
2869 | GFC_STD_F2008_OBS);
2870
2871 /* Set f03 for -std=f2003. */
2872 f03 = gfc_option.allow_std == (GFC_STD_F95_OBS | GFC_STD_F95 | GFC_STD_F77
2873 | GFC_STD_F2008_OBS | GFC_STD_F2003);
2874
2875 /* Look for a blank between STOP and the stop-code for F2008 or later. */
2876 if (gfc_current_form != FORM_FIXED && !(f95 || f03))
2877 {
2878 char c = gfc_peek_ascii_char ();
2879
2880 /* Look for end-of-statement. There is no stop-code. */
2881 if (c == '\n' || c == '!' || c == ';')
2882 goto done;
2883
2884 if (c != ' ')
2885 {
2886 gfc_error ("Blank required in %s statement near %C",
2887 gfc_ascii_statement (st));
2888 return MATCH_ERROR;
2889 }
2890 }
2891
2892 if (gfc_match_eos () != MATCH_YES)
2893 {
2894 int stopcode;
2895 locus old_locus;
2896
2897 /* First look for the F95 or F2003 digit [...] construct. */
2898 old_locus = gfc_current_locus;
2899 m = gfc_match_small_int (&stopcode);
2900 if (m == MATCH_YES && (f95 || f03))
2901 {
2902 if (stopcode < 0)
2903 {
2904 gfc_error ("STOP code at %C cannot be negative");
2905 return MATCH_ERROR;
2906 }
2907
2908 if (stopcode > 99999)
2909 {
2910 gfc_error ("STOP code at %C contains too many digits");
2911 return MATCH_ERROR;
2912 }
2913 }
2914
2915 /* Reset the locus and now load gfc_expr. */
2916 gfc_current_locus = old_locus;
2917 m = gfc_match_expr (&e);
2918 if (m == MATCH_ERROR)
2919 goto cleanup;
2920 if (m == MATCH_NO)
2921 goto syntax;
2922
2923 if (gfc_match_eos () != MATCH_YES)
2924 goto syntax;
2925 }
2926
2927 if (gfc_pure (NULL))
2928 {
2929 if (st == ST_ERROR_STOP)
2930 {
2931 if (!gfc_notify_std (GFC_STD_F2015, "%s statement at %C in PURE "
2932 "procedure", gfc_ascii_statement (st)))
2933 goto cleanup;
2934 }
2935 else
2936 {
2937 gfc_error ("%s statement not allowed in PURE procedure at %C",
2938 gfc_ascii_statement (st));
2939 goto cleanup;
2940 }
2941 }
2942
2943 gfc_unset_implicit_pure (NULL);
2944
2945 if (st == ST_STOP && gfc_find_state (COMP_CRITICAL))
2946 {
2947 gfc_error ("Image control statement STOP at %C in CRITICAL block");
2948 goto cleanup;
2949 }
2950 if (st == ST_STOP && gfc_find_state (COMP_DO_CONCURRENT))
2951 {
2952 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
2953 goto cleanup;
2954 }
2955
2956 if (e != NULL)
2957 {
2958 gfc_simplify_expr (e, 0);
2959
2960 /* Test for F95 and F2003 style STOP stop-code. */
2961 if (e->expr_type != EXPR_CONSTANT && (f95 || f03))
2962 {
2963 gfc_error ("STOP code at %L must be a scalar CHARACTER constant or "
2964 "digit[digit[digit[digit[digit]]]]", &e->where);
2965 goto cleanup;
2966 }
2967
2968 /* Use the machinery for an initialization expression to reduce the
2969 stop-code to a constant. */
2970 gfc_init_expr_flag = true;
2971 gfc_reduce_init_expr (e);
2972 gfc_init_expr_flag = false;
2973
2974 if (!(e->ts.type == BT_CHARACTER || e->ts.type == BT_INTEGER))
2975 {
2976 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
2977 &e->where);
2978 goto cleanup;
2979 }
2980
2981 if (e->rank != 0)
2982 {
2983 gfc_error ("STOP code at %L must be scalar", &e->where);
2984 goto cleanup;
2985 }
2986
2987 if (e->ts.type == BT_CHARACTER
2988 && e->ts.kind != gfc_default_character_kind)
2989 {
2990 gfc_error ("STOP code at %L must be default character KIND=%d",
2991 &e->where, (int) gfc_default_character_kind);
2992 goto cleanup;
2993 }
2994
2995 if (e->ts.type == BT_INTEGER && e->ts.kind != gfc_default_integer_kind)
2996 {
2997 gfc_error ("STOP code at %L must be default integer KIND=%d",
2998 &e->where, (int) gfc_default_integer_kind);
2999 goto cleanup;
3000 }
3001 }
3002
3003 done:
3004
3005 switch (st)
3006 {
3007 case ST_STOP:
3008 new_st.op = EXEC_STOP;
3009 break;
3010 case ST_ERROR_STOP:
3011 new_st.op = EXEC_ERROR_STOP;
3012 break;
3013 case ST_PAUSE:
3014 new_st.op = EXEC_PAUSE;
3015 break;
3016 default:
3017 gcc_unreachable ();
3018 }
3019
3020 new_st.expr1 = e;
3021 new_st.ext.stop_code = -1;
3022
3023 return MATCH_YES;
3024
3025 syntax:
3026 gfc_syntax_error (st);
3027
3028 cleanup:
3029
3030 gfc_free_expr (e);
3031 return MATCH_ERROR;
3032 }
3033
3034
3035 /* Match the (deprecated) PAUSE statement. */
3036
3037 match
3038 gfc_match_pause (void)
3039 {
3040 match m;
3041
3042 m = gfc_match_stopcode (ST_PAUSE);
3043 if (m == MATCH_YES)
3044 {
3045 if (!gfc_notify_std (GFC_STD_F95_DEL, "PAUSE statement at %C"))
3046 m = MATCH_ERROR;
3047 }
3048 return m;
3049 }
3050
3051
3052 /* Match the STOP statement. */
3053
3054 match
3055 gfc_match_stop (void)
3056 {
3057 return gfc_match_stopcode (ST_STOP);
3058 }
3059
3060
3061 /* Match the ERROR STOP statement. */
3062
3063 match
3064 gfc_match_error_stop (void)
3065 {
3066 if (!gfc_notify_std (GFC_STD_F2008, "ERROR STOP statement at %C"))
3067 return MATCH_ERROR;
3068
3069 return gfc_match_stopcode (ST_ERROR_STOP);
3070 }
3071
3072 /* Match EVENT POST/WAIT statement. Syntax:
3073 EVENT POST ( event-variable [, sync-stat-list] )
3074 EVENT WAIT ( event-variable [, wait-spec-list] )
3075 with
3076 wait-spec-list is sync-stat-list or until-spec
3077 until-spec is UNTIL_COUNT = scalar-int-expr
3078 sync-stat is STAT= or ERRMSG=. */
3079
3080 static match
3081 event_statement (gfc_statement st)
3082 {
3083 match m;
3084 gfc_expr *tmp, *eventvar, *until_count, *stat, *errmsg;
3085 bool saw_until_count, saw_stat, saw_errmsg;
3086
3087 tmp = eventvar = until_count = stat = errmsg = NULL;
3088 saw_until_count = saw_stat = saw_errmsg = false;
3089
3090 if (gfc_pure (NULL))
3091 {
3092 gfc_error ("Image control statement EVENT %s at %C in PURE procedure",
3093 st == ST_EVENT_POST ? "POST" : "WAIT");
3094 return MATCH_ERROR;
3095 }
3096
3097 gfc_unset_implicit_pure (NULL);
3098
3099 if (flag_coarray == GFC_FCOARRAY_NONE)
3100 {
3101 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3102 return MATCH_ERROR;
3103 }
3104
3105 if (gfc_find_state (COMP_CRITICAL))
3106 {
3107 gfc_error ("Image control statement EVENT %s at %C in CRITICAL block",
3108 st == ST_EVENT_POST ? "POST" : "WAIT");
3109 return MATCH_ERROR;
3110 }
3111
3112 if (gfc_find_state (COMP_DO_CONCURRENT))
3113 {
3114 gfc_error ("Image control statement EVENT %s at %C in DO CONCURRENT "
3115 "block", st == ST_EVENT_POST ? "POST" : "WAIT");
3116 return MATCH_ERROR;
3117 }
3118
3119 if (gfc_match_char ('(') != MATCH_YES)
3120 goto syntax;
3121
3122 if (gfc_match ("%e", &eventvar) != MATCH_YES)
3123 goto syntax;
3124 m = gfc_match_char (',');
3125 if (m == MATCH_ERROR)
3126 goto syntax;
3127 if (m == MATCH_NO)
3128 {
3129 m = gfc_match_char (')');
3130 if (m == MATCH_YES)
3131 goto done;
3132 goto syntax;
3133 }
3134
3135 for (;;)
3136 {
3137 m = gfc_match (" stat = %v", &tmp);
3138 if (m == MATCH_ERROR)
3139 goto syntax;
3140 if (m == MATCH_YES)
3141 {
3142 if (saw_stat)
3143 {
3144 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3145 goto cleanup;
3146 }
3147 stat = tmp;
3148 saw_stat = true;
3149
3150 m = gfc_match_char (',');
3151 if (m == MATCH_YES)
3152 continue;
3153
3154 tmp = NULL;
3155 break;
3156 }
3157
3158 m = gfc_match (" errmsg = %v", &tmp);
3159 if (m == MATCH_ERROR)
3160 goto syntax;
3161 if (m == MATCH_YES)
3162 {
3163 if (saw_errmsg)
3164 {
3165 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3166 goto cleanup;
3167 }
3168 errmsg = tmp;
3169 saw_errmsg = true;
3170
3171 m = gfc_match_char (',');
3172 if (m == MATCH_YES)
3173 continue;
3174
3175 tmp = NULL;
3176 break;
3177 }
3178
3179 m = gfc_match (" until_count = %e", &tmp);
3180 if (m == MATCH_ERROR || st == ST_EVENT_POST)
3181 goto syntax;
3182 if (m == MATCH_YES)
3183 {
3184 if (saw_until_count)
3185 {
3186 gfc_error ("Redundant UNTIL_COUNT tag found at %L ",
3187 &tmp->where);
3188 goto cleanup;
3189 }
3190 until_count = tmp;
3191 saw_until_count = true;
3192
3193 m = gfc_match_char (',');
3194 if (m == MATCH_YES)
3195 continue;
3196
3197 tmp = NULL;
3198 break;
3199 }
3200
3201 break;
3202 }
3203
3204 if (m == MATCH_ERROR)
3205 goto syntax;
3206
3207 if (gfc_match (" )%t") != MATCH_YES)
3208 goto syntax;
3209
3210 done:
3211 switch (st)
3212 {
3213 case ST_EVENT_POST:
3214 new_st.op = EXEC_EVENT_POST;
3215 break;
3216 case ST_EVENT_WAIT:
3217 new_st.op = EXEC_EVENT_WAIT;
3218 break;
3219 default:
3220 gcc_unreachable ();
3221 }
3222
3223 new_st.expr1 = eventvar;
3224 new_st.expr2 = stat;
3225 new_st.expr3 = errmsg;
3226 new_st.expr4 = until_count;
3227
3228 return MATCH_YES;
3229
3230 syntax:
3231 gfc_syntax_error (st);
3232
3233 cleanup:
3234 if (until_count != tmp)
3235 gfc_free_expr (until_count);
3236 if (errmsg != tmp)
3237 gfc_free_expr (errmsg);
3238 if (stat != tmp)
3239 gfc_free_expr (stat);
3240
3241 gfc_free_expr (tmp);
3242 gfc_free_expr (eventvar);
3243
3244 return MATCH_ERROR;
3245
3246 }
3247
3248
3249 match
3250 gfc_match_event_post (void)
3251 {
3252 if (!gfc_notify_std (GFC_STD_F2008_TS, "EVENT POST statement at %C"))
3253 return MATCH_ERROR;
3254
3255 return event_statement (ST_EVENT_POST);
3256 }
3257
3258
3259 match
3260 gfc_match_event_wait (void)
3261 {
3262 if (!gfc_notify_std (GFC_STD_F2008_TS, "EVENT WAIT statement at %C"))
3263 return MATCH_ERROR;
3264
3265 return event_statement (ST_EVENT_WAIT);
3266 }
3267
3268
3269 /* Match a FAIL IMAGE statement. */
3270
3271 match
3272 gfc_match_fail_image (void)
3273 {
3274 if (!gfc_notify_std (GFC_STD_F2008_TS, "FAIL IMAGE statement at %C"))
3275 return MATCH_ERROR;
3276
3277 if (gfc_match_char ('(') == MATCH_YES)
3278 goto syntax;
3279
3280 new_st.op = EXEC_FAIL_IMAGE;
3281
3282 return MATCH_YES;
3283
3284 syntax:
3285 gfc_syntax_error (ST_FAIL_IMAGE);
3286
3287 return MATCH_ERROR;
3288 }
3289
3290
3291 /* Match LOCK/UNLOCK statement. Syntax:
3292 LOCK ( lock-variable [ , lock-stat-list ] )
3293 UNLOCK ( lock-variable [ , sync-stat-list ] )
3294 where lock-stat is ACQUIRED_LOCK or sync-stat
3295 and sync-stat is STAT= or ERRMSG=. */
3296
3297 static match
3298 lock_unlock_statement (gfc_statement st)
3299 {
3300 match m;
3301 gfc_expr *tmp, *lockvar, *acq_lock, *stat, *errmsg;
3302 bool saw_acq_lock, saw_stat, saw_errmsg;
3303
3304 tmp = lockvar = acq_lock = stat = errmsg = NULL;
3305 saw_acq_lock = saw_stat = saw_errmsg = false;
3306
3307 if (gfc_pure (NULL))
3308 {
3309 gfc_error ("Image control statement %s at %C in PURE procedure",
3310 st == ST_LOCK ? "LOCK" : "UNLOCK");
3311 return MATCH_ERROR;
3312 }
3313
3314 gfc_unset_implicit_pure (NULL);
3315
3316 if (flag_coarray == GFC_FCOARRAY_NONE)
3317 {
3318 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3319 return MATCH_ERROR;
3320 }
3321
3322 if (gfc_find_state (COMP_CRITICAL))
3323 {
3324 gfc_error ("Image control statement %s at %C in CRITICAL block",
3325 st == ST_LOCK ? "LOCK" : "UNLOCK");
3326 return MATCH_ERROR;
3327 }
3328
3329 if (gfc_find_state (COMP_DO_CONCURRENT))
3330 {
3331 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
3332 st == ST_LOCK ? "LOCK" : "UNLOCK");
3333 return MATCH_ERROR;
3334 }
3335
3336 if (gfc_match_char ('(') != MATCH_YES)
3337 goto syntax;
3338
3339 if (gfc_match ("%e", &lockvar) != MATCH_YES)
3340 goto syntax;
3341 m = gfc_match_char (',');
3342 if (m == MATCH_ERROR)
3343 goto syntax;
3344 if (m == MATCH_NO)
3345 {
3346 m = gfc_match_char (')');
3347 if (m == MATCH_YES)
3348 goto done;
3349 goto syntax;
3350 }
3351
3352 for (;;)
3353 {
3354 m = gfc_match (" stat = %v", &tmp);
3355 if (m == MATCH_ERROR)
3356 goto syntax;
3357 if (m == MATCH_YES)
3358 {
3359 if (saw_stat)
3360 {
3361 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3362 goto cleanup;
3363 }
3364 stat = tmp;
3365 saw_stat = true;
3366
3367 m = gfc_match_char (',');
3368 if (m == MATCH_YES)
3369 continue;
3370
3371 tmp = NULL;
3372 break;
3373 }
3374
3375 m = gfc_match (" errmsg = %v", &tmp);
3376 if (m == MATCH_ERROR)
3377 goto syntax;
3378 if (m == MATCH_YES)
3379 {
3380 if (saw_errmsg)
3381 {
3382 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3383 goto cleanup;
3384 }
3385 errmsg = tmp;
3386 saw_errmsg = true;
3387
3388 m = gfc_match_char (',');
3389 if (m == MATCH_YES)
3390 continue;
3391
3392 tmp = NULL;
3393 break;
3394 }
3395
3396 m = gfc_match (" acquired_lock = %v", &tmp);
3397 if (m == MATCH_ERROR || st == ST_UNLOCK)
3398 goto syntax;
3399 if (m == MATCH_YES)
3400 {
3401 if (saw_acq_lock)
3402 {
3403 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L ",
3404 &tmp->where);
3405 goto cleanup;
3406 }
3407 acq_lock = tmp;
3408 saw_acq_lock = true;
3409
3410 m = gfc_match_char (',');
3411 if (m == MATCH_YES)
3412 continue;
3413
3414 tmp = NULL;
3415 break;
3416 }
3417
3418 break;
3419 }
3420
3421 if (m == MATCH_ERROR)
3422 goto syntax;
3423
3424 if (gfc_match (" )%t") != MATCH_YES)
3425 goto syntax;
3426
3427 done:
3428 switch (st)
3429 {
3430 case ST_LOCK:
3431 new_st.op = EXEC_LOCK;
3432 break;
3433 case ST_UNLOCK:
3434 new_st.op = EXEC_UNLOCK;
3435 break;
3436 default:
3437 gcc_unreachable ();
3438 }
3439
3440 new_st.expr1 = lockvar;
3441 new_st.expr2 = stat;
3442 new_st.expr3 = errmsg;
3443 new_st.expr4 = acq_lock;
3444
3445 return MATCH_YES;
3446
3447 syntax:
3448 gfc_syntax_error (st);
3449
3450 cleanup:
3451 if (acq_lock != tmp)
3452 gfc_free_expr (acq_lock);
3453 if (errmsg != tmp)
3454 gfc_free_expr (errmsg);
3455 if (stat != tmp)
3456 gfc_free_expr (stat);
3457
3458 gfc_free_expr (tmp);
3459 gfc_free_expr (lockvar);
3460
3461 return MATCH_ERROR;
3462 }
3463
3464
3465 match
3466 gfc_match_lock (void)
3467 {
3468 if (!gfc_notify_std (GFC_STD_F2008, "LOCK statement at %C"))
3469 return MATCH_ERROR;
3470
3471 return lock_unlock_statement (ST_LOCK);
3472 }
3473
3474
3475 match
3476 gfc_match_unlock (void)
3477 {
3478 if (!gfc_notify_std (GFC_STD_F2008, "UNLOCK statement at %C"))
3479 return MATCH_ERROR;
3480
3481 return lock_unlock_statement (ST_UNLOCK);
3482 }
3483
3484
3485 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
3486 SYNC ALL [(sync-stat-list)]
3487 SYNC MEMORY [(sync-stat-list)]
3488 SYNC IMAGES (image-set [, sync-stat-list] )
3489 with sync-stat is int-expr or *. */
3490
3491 static match
3492 sync_statement (gfc_statement st)
3493 {
3494 match m;
3495 gfc_expr *tmp, *imageset, *stat, *errmsg;
3496 bool saw_stat, saw_errmsg;
3497
3498 tmp = imageset = stat = errmsg = NULL;
3499 saw_stat = saw_errmsg = false;
3500
3501 if (gfc_pure (NULL))
3502 {
3503 gfc_error ("Image control statement SYNC at %C in PURE procedure");
3504 return MATCH_ERROR;
3505 }
3506
3507 gfc_unset_implicit_pure (NULL);
3508
3509 if (!gfc_notify_std (GFC_STD_F2008, "SYNC statement at %C"))
3510 return MATCH_ERROR;
3511
3512 if (flag_coarray == GFC_FCOARRAY_NONE)
3513 {
3514 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
3515 "enable");
3516 return MATCH_ERROR;
3517 }
3518
3519 if (gfc_find_state (COMP_CRITICAL))
3520 {
3521 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
3522 return MATCH_ERROR;
3523 }
3524
3525 if (gfc_find_state (COMP_DO_CONCURRENT))
3526 {
3527 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
3528 return MATCH_ERROR;
3529 }
3530
3531 if (gfc_match_eos () == MATCH_YES)
3532 {
3533 if (st == ST_SYNC_IMAGES)
3534 goto syntax;
3535 goto done;
3536 }
3537
3538 if (gfc_match_char ('(') != MATCH_YES)
3539 goto syntax;
3540
3541 if (st == ST_SYNC_IMAGES)
3542 {
3543 /* Denote '*' as imageset == NULL. */
3544 m = gfc_match_char ('*');
3545 if (m == MATCH_ERROR)
3546 goto syntax;
3547 if (m == MATCH_NO)
3548 {
3549 if (gfc_match ("%e", &imageset) != MATCH_YES)
3550 goto syntax;
3551 }
3552 m = gfc_match_char (',');
3553 if (m == MATCH_ERROR)
3554 goto syntax;
3555 if (m == MATCH_NO)
3556 {
3557 m = gfc_match_char (')');
3558 if (m == MATCH_YES)
3559 goto done;
3560 goto syntax;
3561 }
3562 }
3563
3564 for (;;)
3565 {
3566 m = gfc_match (" stat = %v", &tmp);
3567 if (m == MATCH_ERROR)
3568 goto syntax;
3569 if (m == MATCH_YES)
3570 {
3571 if (saw_stat)
3572 {
3573 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3574 goto cleanup;
3575 }
3576 stat = tmp;
3577 saw_stat = true;
3578
3579 if (gfc_match_char (',') == MATCH_YES)
3580 continue;
3581
3582 tmp = NULL;
3583 break;
3584 }
3585
3586 m = gfc_match (" errmsg = %v", &tmp);
3587 if (m == MATCH_ERROR)
3588 goto syntax;
3589 if (m == MATCH_YES)
3590 {
3591 if (saw_errmsg)
3592 {
3593 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3594 goto cleanup;
3595 }
3596 errmsg = tmp;
3597 saw_errmsg = true;
3598
3599 if (gfc_match_char (',') == MATCH_YES)
3600 continue;
3601
3602 tmp = NULL;
3603 break;
3604 }
3605
3606 break;
3607 }
3608
3609 if (gfc_match (" )%t") != MATCH_YES)
3610 goto syntax;
3611
3612 done:
3613 switch (st)
3614 {
3615 case ST_SYNC_ALL:
3616 new_st.op = EXEC_SYNC_ALL;
3617 break;
3618 case ST_SYNC_IMAGES:
3619 new_st.op = EXEC_SYNC_IMAGES;
3620 break;
3621 case ST_SYNC_MEMORY:
3622 new_st.op = EXEC_SYNC_MEMORY;
3623 break;
3624 default:
3625 gcc_unreachable ();
3626 }
3627
3628 new_st.expr1 = imageset;
3629 new_st.expr2 = stat;
3630 new_st.expr3 = errmsg;
3631
3632 return MATCH_YES;
3633
3634 syntax:
3635 gfc_syntax_error (st);
3636
3637 cleanup:
3638 if (stat != tmp)
3639 gfc_free_expr (stat);
3640 if (errmsg != tmp)
3641 gfc_free_expr (errmsg);
3642
3643 gfc_free_expr (tmp);
3644 gfc_free_expr (imageset);
3645
3646 return MATCH_ERROR;
3647 }
3648
3649
3650 /* Match SYNC ALL statement. */
3651
3652 match
3653 gfc_match_sync_all (void)
3654 {
3655 return sync_statement (ST_SYNC_ALL);
3656 }
3657
3658
3659 /* Match SYNC IMAGES statement. */
3660
3661 match
3662 gfc_match_sync_images (void)
3663 {
3664 return sync_statement (ST_SYNC_IMAGES);
3665 }
3666
3667
3668 /* Match SYNC MEMORY statement. */
3669
3670 match
3671 gfc_match_sync_memory (void)
3672 {
3673 return sync_statement (ST_SYNC_MEMORY);
3674 }
3675
3676
3677 /* Match a CONTINUE statement. */
3678
3679 match
3680 gfc_match_continue (void)
3681 {
3682 if (gfc_match_eos () != MATCH_YES)
3683 {
3684 gfc_syntax_error (ST_CONTINUE);
3685 return MATCH_ERROR;
3686 }
3687
3688 new_st.op = EXEC_CONTINUE;
3689 return MATCH_YES;
3690 }
3691
3692
3693 /* Match the (deprecated) ASSIGN statement. */
3694
3695 match
3696 gfc_match_assign (void)
3697 {
3698 gfc_expr *expr;
3699 gfc_st_label *label;
3700
3701 if (gfc_match (" %l", &label) == MATCH_YES)
3702 {
3703 if (!gfc_reference_st_label (label, ST_LABEL_UNKNOWN))
3704 return MATCH_ERROR;
3705 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
3706 {
3707 if (!gfc_notify_std (GFC_STD_F95_DEL, "ASSIGN statement at %C"))
3708 return MATCH_ERROR;
3709
3710 expr->symtree->n.sym->attr.assign = 1;
3711
3712 new_st.op = EXEC_LABEL_ASSIGN;
3713 new_st.label1 = label;
3714 new_st.expr1 = expr;
3715 return MATCH_YES;
3716 }
3717 }
3718 return MATCH_NO;
3719 }
3720
3721
3722 /* Match the GO TO statement. As a computed GOTO statement is
3723 matched, it is transformed into an equivalent SELECT block. No
3724 tree is necessary, and the resulting jumps-to-jumps are
3725 specifically optimized away by the back end. */
3726
3727 match
3728 gfc_match_goto (void)
3729 {
3730 gfc_code *head, *tail;
3731 gfc_expr *expr;
3732 gfc_case *cp;
3733 gfc_st_label *label;
3734 int i;
3735 match m;
3736
3737 if (gfc_match (" %l%t", &label) == MATCH_YES)
3738 {
3739 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3740 return MATCH_ERROR;
3741
3742 new_st.op = EXEC_GOTO;
3743 new_st.label1 = label;
3744 return MATCH_YES;
3745 }
3746
3747 /* The assigned GO TO statement. */
3748
3749 if (gfc_match_variable (&expr, 0) == MATCH_YES)
3750 {
3751 if (!gfc_notify_std (GFC_STD_F95_DEL, "Assigned GOTO statement at %C"))
3752 return MATCH_ERROR;
3753
3754 new_st.op = EXEC_GOTO;
3755 new_st.expr1 = expr;
3756
3757 if (gfc_match_eos () == MATCH_YES)
3758 return MATCH_YES;
3759
3760 /* Match label list. */
3761 gfc_match_char (',');
3762 if (gfc_match_char ('(') != MATCH_YES)
3763 {
3764 gfc_syntax_error (ST_GOTO);
3765 return MATCH_ERROR;
3766 }
3767 head = tail = NULL;
3768
3769 do
3770 {
3771 m = gfc_match_st_label (&label);
3772 if (m != MATCH_YES)
3773 goto syntax;
3774
3775 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3776 goto cleanup;
3777
3778 if (head == NULL)
3779 head = tail = gfc_get_code (EXEC_GOTO);
3780 else
3781 {
3782 tail->block = gfc_get_code (EXEC_GOTO);
3783 tail = tail->block;
3784 }
3785
3786 tail->label1 = label;
3787 }
3788 while (gfc_match_char (',') == MATCH_YES);
3789
3790 if (gfc_match (")%t") != MATCH_YES)
3791 goto syntax;
3792
3793 if (head == NULL)
3794 {
3795 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3796 goto syntax;
3797 }
3798 new_st.block = head;
3799
3800 return MATCH_YES;
3801 }
3802
3803 /* Last chance is a computed GO TO statement. */
3804 if (gfc_match_char ('(') != MATCH_YES)
3805 {
3806 gfc_syntax_error (ST_GOTO);
3807 return MATCH_ERROR;
3808 }
3809
3810 head = tail = NULL;
3811 i = 1;
3812
3813 do
3814 {
3815 m = gfc_match_st_label (&label);
3816 if (m != MATCH_YES)
3817 goto syntax;
3818
3819 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3820 goto cleanup;
3821
3822 if (head == NULL)
3823 head = tail = gfc_get_code (EXEC_SELECT);
3824 else
3825 {
3826 tail->block = gfc_get_code (EXEC_SELECT);
3827 tail = tail->block;
3828 }
3829
3830 cp = gfc_get_case ();
3831 cp->low = cp->high = gfc_get_int_expr (gfc_default_integer_kind,
3832 NULL, i++);
3833
3834 tail->ext.block.case_list = cp;
3835
3836 tail->next = gfc_get_code (EXEC_GOTO);
3837 tail->next->label1 = label;
3838 }
3839 while (gfc_match_char (',') == MATCH_YES);
3840
3841 if (gfc_match_char (')') != MATCH_YES)
3842 goto syntax;
3843
3844 if (head == NULL)
3845 {
3846 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3847 goto syntax;
3848 }
3849
3850 /* Get the rest of the statement. */
3851 gfc_match_char (',');
3852
3853 if (gfc_match (" %e%t", &expr) != MATCH_YES)
3854 goto syntax;
3855
3856 if (!gfc_notify_std (GFC_STD_F95_OBS, "Computed GOTO at %C"))
3857 return MATCH_ERROR;
3858
3859 /* At this point, a computed GOTO has been fully matched and an
3860 equivalent SELECT statement constructed. */
3861
3862 new_st.op = EXEC_SELECT;
3863 new_st.expr1 = NULL;
3864
3865 /* Hack: For a "real" SELECT, the expression is in expr. We put
3866 it in expr2 so we can distinguish then and produce the correct
3867 diagnostics. */
3868 new_st.expr2 = expr;
3869 new_st.block = head;
3870 return MATCH_YES;
3871
3872 syntax:
3873 gfc_syntax_error (ST_GOTO);
3874 cleanup:
3875 gfc_free_statements (head);
3876 return MATCH_ERROR;
3877 }
3878
3879
3880 /* Frees a list of gfc_alloc structures. */
3881
3882 void
3883 gfc_free_alloc_list (gfc_alloc *p)
3884 {
3885 gfc_alloc *q;
3886
3887 for (; p; p = q)
3888 {
3889 q = p->next;
3890 gfc_free_expr (p->expr);
3891 free (p);
3892 }
3893 }
3894
3895
3896 /* Match an ALLOCATE statement. */
3897
3898 match
3899 gfc_match_allocate (void)
3900 {
3901 gfc_alloc *head, *tail;
3902 gfc_expr *stat, *errmsg, *tmp, *source, *mold;
3903 gfc_typespec ts;
3904 gfc_symbol *sym;
3905 match m;
3906 locus old_locus, deferred_locus;
3907 bool saw_stat, saw_errmsg, saw_source, saw_mold, saw_deferred, b1, b2, b3;
3908 bool saw_unlimited = false;
3909
3910 head = tail = NULL;
3911 stat = errmsg = source = mold = tmp = NULL;
3912 saw_stat = saw_errmsg = saw_source = saw_mold = saw_deferred = false;
3913
3914 if (gfc_match_char ('(') != MATCH_YES)
3915 goto syntax;
3916
3917 /* Match an optional type-spec. */
3918 old_locus = gfc_current_locus;
3919 m = gfc_match_type_spec (&ts);
3920 if (m == MATCH_ERROR)
3921 goto cleanup;
3922 else if (m == MATCH_NO)
3923 {
3924 char name[GFC_MAX_SYMBOL_LEN + 3];
3925
3926 if (gfc_match ("%n :: ", name) == MATCH_YES)
3927 {
3928 gfc_error ("Error in type-spec at %L", &old_locus);
3929 goto cleanup;
3930 }
3931
3932 ts.type = BT_UNKNOWN;
3933 }
3934 else
3935 {
3936 if (gfc_match (" :: ") == MATCH_YES)
3937 {
3938 if (!gfc_notify_std (GFC_STD_F2003, "typespec in ALLOCATE at %L",
3939 &old_locus))
3940 goto cleanup;
3941
3942 if (ts.deferred)
3943 {
3944 gfc_error ("Type-spec at %L cannot contain a deferred "
3945 "type parameter", &old_locus);
3946 goto cleanup;
3947 }
3948
3949 if (ts.type == BT_CHARACTER)
3950 ts.u.cl->length_from_typespec = true;
3951 }
3952 else
3953 {
3954 ts.type = BT_UNKNOWN;
3955 gfc_current_locus = old_locus;
3956 }
3957 }
3958
3959 for (;;)
3960 {
3961 if (head == NULL)
3962 head = tail = gfc_get_alloc ();
3963 else
3964 {
3965 tail->next = gfc_get_alloc ();
3966 tail = tail->next;
3967 }
3968
3969 m = gfc_match_variable (&tail->expr, 0);
3970 if (m == MATCH_NO)
3971 goto syntax;
3972 if (m == MATCH_ERROR)
3973 goto cleanup;
3974
3975 if (gfc_check_do_variable (tail->expr->symtree))
3976 goto cleanup;
3977
3978 bool impure = gfc_impure_variable (tail->expr->symtree->n.sym);
3979 if (impure && gfc_pure (NULL))
3980 {
3981 gfc_error ("Bad allocate-object at %C for a PURE procedure");
3982 goto cleanup;
3983 }
3984
3985 if (impure)
3986 gfc_unset_implicit_pure (NULL);
3987
3988 if (tail->expr->ts.deferred)
3989 {
3990 saw_deferred = true;
3991 deferred_locus = tail->expr->where;
3992 }
3993
3994 if (gfc_find_state (COMP_DO_CONCURRENT)
3995 || gfc_find_state (COMP_CRITICAL))
3996 {
3997 gfc_ref *ref;
3998 bool coarray = tail->expr->symtree->n.sym->attr.codimension;
3999 for (ref = tail->expr->ref; ref; ref = ref->next)
4000 if (ref->type == REF_COMPONENT)
4001 coarray = ref->u.c.component->attr.codimension;
4002
4003 if (coarray && gfc_find_state (COMP_DO_CONCURRENT))
4004 {
4005 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
4006 goto cleanup;
4007 }
4008 if (coarray && gfc_find_state (COMP_CRITICAL))
4009 {
4010 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
4011 goto cleanup;
4012 }
4013 }
4014
4015 /* Check for F08:C628. */
4016 sym = tail->expr->symtree->n.sym;
4017 b1 = !(tail->expr->ref
4018 && (tail->expr->ref->type == REF_COMPONENT
4019 || tail->expr->ref->type == REF_ARRAY));
4020 if (sym && sym->ts.type == BT_CLASS && sym->attr.class_ok)
4021 b2 = !(CLASS_DATA (sym)->attr.allocatable
4022 || CLASS_DATA (sym)->attr.class_pointer);
4023 else
4024 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4025 || sym->attr.proc_pointer);
4026 b3 = sym && sym->ns && sym->ns->proc_name
4027 && (sym->ns->proc_name->attr.allocatable
4028 || sym->ns->proc_name->attr.pointer
4029 || sym->ns->proc_name->attr.proc_pointer);
4030 if (b1 && b2 && !b3)
4031 {
4032 gfc_error ("Allocate-object at %L is neither a data pointer "
4033 "nor an allocatable variable", &tail->expr->where);
4034 goto cleanup;
4035 }
4036
4037 /* The ALLOCATE statement had an optional typespec. Check the
4038 constraints. */
4039 if (ts.type != BT_UNKNOWN)
4040 {
4041 /* Enforce F03:C624. */
4042 if (!gfc_type_compatible (&tail->expr->ts, &ts))
4043 {
4044 gfc_error ("Type of entity at %L is type incompatible with "
4045 "typespec", &tail->expr->where);
4046 goto cleanup;
4047 }
4048
4049 /* Enforce F03:C627. */
4050 if (ts.kind != tail->expr->ts.kind && !UNLIMITED_POLY (tail->expr))
4051 {
4052 gfc_error ("Kind type parameter for entity at %L differs from "
4053 "the kind type parameter of the typespec",
4054 &tail->expr->where);
4055 goto cleanup;
4056 }
4057 }
4058
4059 if (tail->expr->ts.type == BT_DERIVED)
4060 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
4061
4062 saw_unlimited = saw_unlimited | UNLIMITED_POLY (tail->expr);
4063
4064 if (gfc_peek_ascii_char () == '(' && !sym->attr.dimension)
4065 {
4066 gfc_error ("Shape specification for allocatable scalar at %C");
4067 goto cleanup;
4068 }
4069
4070 if (gfc_match_char (',') != MATCH_YES)
4071 break;
4072
4073 alloc_opt_list:
4074
4075 m = gfc_match (" stat = %v", &tmp);
4076 if (m == MATCH_ERROR)
4077 goto cleanup;
4078 if (m == MATCH_YES)
4079 {
4080 /* Enforce C630. */
4081 if (saw_stat)
4082 {
4083 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
4084 goto cleanup;
4085 }
4086
4087 stat = tmp;
4088 tmp = NULL;
4089 saw_stat = true;
4090
4091 if (gfc_check_do_variable (stat->symtree))
4092 goto cleanup;
4093
4094 if (gfc_match_char (',') == MATCH_YES)
4095 goto alloc_opt_list;
4096 }
4097
4098 m = gfc_match (" errmsg = %v", &tmp);
4099 if (m == MATCH_ERROR)
4100 goto cleanup;
4101 if (m == MATCH_YES)
4102 {
4103 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG tag at %L", &tmp->where))
4104 goto cleanup;
4105
4106 /* Enforce C630. */
4107 if (saw_errmsg)
4108 {
4109 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
4110 goto cleanup;
4111 }
4112
4113 errmsg = tmp;
4114 tmp = NULL;
4115 saw_errmsg = true;
4116
4117 if (gfc_match_char (',') == MATCH_YES)
4118 goto alloc_opt_list;
4119 }
4120
4121 m = gfc_match (" source = %e", &tmp);
4122 if (m == MATCH_ERROR)
4123 goto cleanup;
4124 if (m == MATCH_YES)
4125 {
4126 if (!gfc_notify_std (GFC_STD_F2003, "SOURCE tag at %L", &tmp->where))
4127 goto cleanup;
4128
4129 /* Enforce C630. */
4130 if (saw_source)
4131 {
4132 gfc_error ("Redundant SOURCE tag found at %L ", &tmp->where);
4133 goto cleanup;
4134 }
4135
4136 /* The next 2 conditionals check C631. */
4137 if (ts.type != BT_UNKNOWN)
4138 {
4139 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
4140 &tmp->where, &old_locus);
4141 goto cleanup;
4142 }
4143
4144 if (head->next
4145 && !gfc_notify_std (GFC_STD_F2008, "SOURCE tag at %L"
4146 " with more than a single allocate object",
4147 &tmp->where))
4148 goto cleanup;
4149
4150 source = tmp;
4151 tmp = NULL;
4152 saw_source = true;
4153
4154 if (gfc_match_char (',') == MATCH_YES)
4155 goto alloc_opt_list;
4156 }
4157
4158 m = gfc_match (" mold = %e", &tmp);
4159 if (m == MATCH_ERROR)
4160 goto cleanup;
4161 if (m == MATCH_YES)
4162 {
4163 if (!gfc_notify_std (GFC_STD_F2008, "MOLD tag at %L", &tmp->where))
4164 goto cleanup;
4165
4166 /* Check F08:C636. */
4167 if (saw_mold)
4168 {
4169 gfc_error ("Redundant MOLD tag found at %L ", &tmp->where);
4170 goto cleanup;
4171 }
4172
4173 /* Check F08:C637. */
4174 if (ts.type != BT_UNKNOWN)
4175 {
4176 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
4177 &tmp->where, &old_locus);
4178 goto cleanup;
4179 }
4180
4181 mold = tmp;
4182 tmp = NULL;
4183 saw_mold = true;
4184 mold->mold = 1;
4185
4186 if (gfc_match_char (',') == MATCH_YES)
4187 goto alloc_opt_list;
4188 }
4189
4190 gfc_gobble_whitespace ();
4191
4192 if (gfc_peek_char () == ')')
4193 break;
4194 }
4195
4196 if (gfc_match (" )%t") != MATCH_YES)
4197 goto syntax;
4198
4199 /* Check F08:C637. */
4200 if (source && mold)
4201 {
4202 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
4203 &mold->where, &source->where);
4204 goto cleanup;
4205 }
4206
4207 /* Check F03:C623, */
4208 if (saw_deferred && ts.type == BT_UNKNOWN && !source && !mold)
4209 {
4210 gfc_error ("Allocate-object at %L with a deferred type parameter "
4211 "requires either a type-spec or SOURCE tag or a MOLD tag",
4212 &deferred_locus);
4213 goto cleanup;
4214 }
4215
4216 /* Check F03:C625, */
4217 if (saw_unlimited && ts.type == BT_UNKNOWN && !source && !mold)
4218 {
4219 for (tail = head; tail; tail = tail->next)
4220 {
4221 if (UNLIMITED_POLY (tail->expr))
4222 gfc_error ("Unlimited polymorphic allocate-object at %L "
4223 "requires either a type-spec or SOURCE tag "
4224 "or a MOLD tag", &tail->expr->where);
4225 }
4226 goto cleanup;
4227 }
4228
4229 new_st.op = EXEC_ALLOCATE;
4230 new_st.expr1 = stat;
4231 new_st.expr2 = errmsg;
4232 if (source)
4233 new_st.expr3 = source;
4234 else
4235 new_st.expr3 = mold;
4236 new_st.ext.alloc.list = head;
4237 new_st.ext.alloc.ts = ts;
4238
4239 return MATCH_YES;
4240
4241 syntax:
4242 gfc_syntax_error (ST_ALLOCATE);
4243
4244 cleanup:
4245 gfc_free_expr (errmsg);
4246 gfc_free_expr (source);
4247 gfc_free_expr (stat);
4248 gfc_free_expr (mold);
4249 if (tmp && tmp->expr_type) gfc_free_expr (tmp);
4250 gfc_free_alloc_list (head);
4251 return MATCH_ERROR;
4252 }
4253
4254
4255 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
4256 a set of pointer assignments to intrinsic NULL(). */
4257
4258 match
4259 gfc_match_nullify (void)
4260 {
4261 gfc_code *tail;
4262 gfc_expr *e, *p;
4263 match m;
4264
4265 tail = NULL;
4266
4267 if (gfc_match_char ('(') != MATCH_YES)
4268 goto syntax;
4269
4270 for (;;)
4271 {
4272 m = gfc_match_variable (&p, 0);
4273 if (m == MATCH_ERROR)
4274 goto cleanup;
4275 if (m == MATCH_NO)
4276 goto syntax;
4277
4278 if (gfc_check_do_variable (p->symtree))
4279 goto cleanup;
4280
4281 /* F2008, C1242. */
4282 if (gfc_is_coindexed (p))
4283 {
4284 gfc_error ("Pointer object at %C shall not be coindexed");
4285 goto cleanup;
4286 }
4287
4288 /* build ' => NULL() '. */
4289 e = gfc_get_null_expr (&gfc_current_locus);
4290
4291 /* Chain to list. */
4292 if (tail == NULL)
4293 {
4294 tail = &new_st;
4295 tail->op = EXEC_POINTER_ASSIGN;
4296 }
4297 else
4298 {
4299 tail->next = gfc_get_code (EXEC_POINTER_ASSIGN);
4300 tail = tail->next;
4301 }
4302
4303 tail->expr1 = p;
4304 tail->expr2 = e;
4305
4306 if (gfc_match (" )%t") == MATCH_YES)
4307 break;
4308 if (gfc_match_char (',') != MATCH_YES)
4309 goto syntax;
4310 }
4311
4312 return MATCH_YES;
4313
4314 syntax:
4315 gfc_syntax_error (ST_NULLIFY);
4316
4317 cleanup:
4318 gfc_free_statements (new_st.next);
4319 new_st.next = NULL;
4320 gfc_free_expr (new_st.expr1);
4321 new_st.expr1 = NULL;
4322 gfc_free_expr (new_st.expr2);
4323 new_st.expr2 = NULL;
4324 return MATCH_ERROR;
4325 }
4326
4327
4328 /* Match a DEALLOCATE statement. */
4329
4330 match
4331 gfc_match_deallocate (void)
4332 {
4333 gfc_alloc *head, *tail;
4334 gfc_expr *stat, *errmsg, *tmp;
4335 gfc_symbol *sym;
4336 match m;
4337 bool saw_stat, saw_errmsg, b1, b2;
4338
4339 head = tail = NULL;
4340 stat = errmsg = tmp = NULL;
4341 saw_stat = saw_errmsg = false;
4342
4343 if (gfc_match_char ('(') != MATCH_YES)
4344 goto syntax;
4345
4346 for (;;)
4347 {
4348 if (head == NULL)
4349 head = tail = gfc_get_alloc ();
4350 else
4351 {
4352 tail->next = gfc_get_alloc ();
4353 tail = tail->next;
4354 }
4355
4356 m = gfc_match_variable (&tail->expr, 0);
4357 if (m == MATCH_ERROR)
4358 goto cleanup;
4359 if (m == MATCH_NO)
4360 goto syntax;
4361
4362 if (gfc_check_do_variable (tail->expr->symtree))
4363 goto cleanup;
4364
4365 sym = tail->expr->symtree->n.sym;
4366
4367 bool impure = gfc_impure_variable (sym);
4368 if (impure && gfc_pure (NULL))
4369 {
4370 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
4371 goto cleanup;
4372 }
4373
4374 if (impure)
4375 gfc_unset_implicit_pure (NULL);
4376
4377 if (gfc_is_coarray (tail->expr)
4378 && gfc_find_state (COMP_DO_CONCURRENT))
4379 {
4380 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
4381 goto cleanup;
4382 }
4383
4384 if (gfc_is_coarray (tail->expr)
4385 && gfc_find_state (COMP_CRITICAL))
4386 {
4387 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
4388 goto cleanup;
4389 }
4390
4391 /* FIXME: disable the checking on derived types. */
4392 b1 = !(tail->expr->ref
4393 && (tail->expr->ref->type == REF_COMPONENT
4394 || tail->expr->ref->type == REF_ARRAY));
4395 if (sym && sym->ts.type == BT_CLASS)
4396 b2 = !(CLASS_DATA (sym)->attr.allocatable
4397 || CLASS_DATA (sym)->attr.class_pointer);
4398 else
4399 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4400 || sym->attr.proc_pointer);
4401 if (b1 && b2)
4402 {
4403 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
4404 "nor an allocatable variable");
4405 goto cleanup;
4406 }
4407
4408 if (gfc_match_char (',') != MATCH_YES)
4409 break;
4410
4411 dealloc_opt_list:
4412
4413 m = gfc_match (" stat = %v", &tmp);
4414 if (m == MATCH_ERROR)
4415 goto cleanup;
4416 if (m == MATCH_YES)
4417 {
4418 if (saw_stat)
4419 {
4420 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
4421 gfc_free_expr (tmp);
4422 goto cleanup;
4423 }
4424
4425 stat = tmp;
4426 saw_stat = true;
4427
4428 if (gfc_check_do_variable (stat->symtree))
4429 goto cleanup;
4430
4431 if (gfc_match_char (',') == MATCH_YES)
4432 goto dealloc_opt_list;
4433 }
4434
4435 m = gfc_match (" errmsg = %v", &tmp);
4436 if (m == MATCH_ERROR)
4437 goto cleanup;
4438 if (m == MATCH_YES)
4439 {
4440 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG at %L", &tmp->where))
4441 goto cleanup;
4442
4443 if (saw_errmsg)
4444 {
4445 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
4446 gfc_free_expr (tmp);
4447 goto cleanup;
4448 }
4449
4450 errmsg = tmp;
4451 saw_errmsg = true;
4452
4453 if (gfc_match_char (',') == MATCH_YES)
4454 goto dealloc_opt_list;
4455 }
4456
4457 gfc_gobble_whitespace ();
4458
4459 if (gfc_peek_char () == ')')
4460 break;
4461 }
4462
4463 if (gfc_match (" )%t") != MATCH_YES)
4464 goto syntax;
4465
4466 new_st.op = EXEC_DEALLOCATE;
4467 new_st.expr1 = stat;
4468 new_st.expr2 = errmsg;
4469 new_st.ext.alloc.list = head;
4470
4471 return MATCH_YES;
4472
4473 syntax:
4474 gfc_syntax_error (ST_DEALLOCATE);
4475
4476 cleanup:
4477 gfc_free_expr (errmsg);
4478 gfc_free_expr (stat);
4479 gfc_free_alloc_list (head);
4480 return MATCH_ERROR;
4481 }
4482
4483
4484 /* Match a RETURN statement. */
4485
4486 match
4487 gfc_match_return (void)
4488 {
4489 gfc_expr *e;
4490 match m;
4491 gfc_compile_state s;
4492
4493 e = NULL;
4494
4495 if (gfc_find_state (COMP_CRITICAL))
4496 {
4497 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
4498 return MATCH_ERROR;
4499 }
4500
4501 if (gfc_find_state (COMP_DO_CONCURRENT))
4502 {
4503 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
4504 return MATCH_ERROR;
4505 }
4506
4507 if (gfc_match_eos () == MATCH_YES)
4508 goto done;
4509
4510 if (!gfc_find_state (COMP_SUBROUTINE))
4511 {
4512 gfc_error ("Alternate RETURN statement at %C is only allowed within "
4513 "a SUBROUTINE");
4514 goto cleanup;
4515 }
4516
4517 if (gfc_current_form == FORM_FREE)
4518 {
4519 /* The following are valid, so we can't require a blank after the
4520 RETURN keyword:
4521 return+1
4522 return(1) */
4523 char c = gfc_peek_ascii_char ();
4524 if (ISALPHA (c) || ISDIGIT (c))
4525 return MATCH_NO;
4526 }
4527
4528 m = gfc_match (" %e%t", &e);
4529 if (m == MATCH_YES)
4530 goto done;
4531 if (m == MATCH_ERROR)
4532 goto cleanup;
4533
4534 gfc_syntax_error (ST_RETURN);
4535
4536 cleanup:
4537 gfc_free_expr (e);
4538 return MATCH_ERROR;
4539
4540 done:
4541 gfc_enclosing_unit (&s);
4542 if (s == COMP_PROGRAM
4543 && !gfc_notify_std (GFC_STD_GNU, "RETURN statement in "
4544 "main program at %C"))
4545 return MATCH_ERROR;
4546
4547 new_st.op = EXEC_RETURN;
4548 new_st.expr1 = e;
4549
4550 return MATCH_YES;
4551 }
4552
4553
4554 /* Match the call of a type-bound procedure, if CALL%var has already been
4555 matched and var found to be a derived-type variable. */
4556
4557 static match
4558 match_typebound_call (gfc_symtree* varst)
4559 {
4560 gfc_expr* base;
4561 match m;
4562
4563 base = gfc_get_expr ();
4564 base->expr_type = EXPR_VARIABLE;
4565 base->symtree = varst;
4566 base->where = gfc_current_locus;
4567 gfc_set_sym_referenced (varst->n.sym);
4568
4569 m = gfc_match_varspec (base, 0, true, true);
4570 if (m == MATCH_NO)
4571 gfc_error ("Expected component reference at %C");
4572 if (m != MATCH_YES)
4573 {
4574 gfc_free_expr (base);
4575 return MATCH_ERROR;
4576 }
4577
4578 if (gfc_match_eos () != MATCH_YES)
4579 {
4580 gfc_error ("Junk after CALL at %C");
4581 gfc_free_expr (base);
4582 return MATCH_ERROR;
4583 }
4584
4585 if (base->expr_type == EXPR_COMPCALL)
4586 new_st.op = EXEC_COMPCALL;
4587 else if (base->expr_type == EXPR_PPC)
4588 new_st.op = EXEC_CALL_PPC;
4589 else
4590 {
4591 gfc_error ("Expected type-bound procedure or procedure pointer component "
4592 "at %C");
4593 gfc_free_expr (base);
4594 return MATCH_ERROR;
4595 }
4596 new_st.expr1 = base;
4597
4598 return MATCH_YES;
4599 }
4600
4601
4602 /* Match a CALL statement. The tricky part here are possible
4603 alternate return specifiers. We handle these by having all
4604 "subroutines" actually return an integer via a register that gives
4605 the return number. If the call specifies alternate returns, we
4606 generate code for a SELECT statement whose case clauses contain
4607 GOTOs to the various labels. */
4608
4609 match
4610 gfc_match_call (void)
4611 {
4612 char name[GFC_MAX_SYMBOL_LEN + 1];
4613 gfc_actual_arglist *a, *arglist;
4614 gfc_case *new_case;
4615 gfc_symbol *sym;
4616 gfc_symtree *st;
4617 gfc_code *c;
4618 match m;
4619 int i;
4620
4621 arglist = NULL;
4622
4623 m = gfc_match ("% %n", name);
4624 if (m == MATCH_NO)
4625 goto syntax;
4626 if (m != MATCH_YES)
4627 return m;
4628
4629 if (gfc_get_ha_sym_tree (name, &st))
4630 return MATCH_ERROR;
4631
4632 sym = st->n.sym;
4633
4634 /* If this is a variable of derived-type, it probably starts a type-bound
4635 procedure call. */
4636 if ((sym->attr.flavor != FL_PROCEDURE
4637 || gfc_is_function_return_value (sym, gfc_current_ns))
4638 && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS))
4639 return match_typebound_call (st);
4640
4641 /* If it does not seem to be callable (include functions so that the
4642 right association is made. They are thrown out in resolution.)
4643 ... */
4644 if (!sym->attr.generic
4645 && !sym->attr.subroutine
4646 && !sym->attr.function)
4647 {
4648 if (!(sym->attr.external && !sym->attr.referenced))
4649 {
4650 /* ...create a symbol in this scope... */
4651 if (sym->ns != gfc_current_ns
4652 && gfc_get_sym_tree (name, NULL, &st, false) == 1)
4653 return MATCH_ERROR;
4654
4655 if (sym != st->n.sym)
4656 sym = st->n.sym;
4657 }
4658
4659 /* ...and then to try to make the symbol into a subroutine. */
4660 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
4661 return MATCH_ERROR;
4662 }
4663
4664 gfc_set_sym_referenced (sym);
4665
4666 if (gfc_match_eos () != MATCH_YES)
4667 {
4668 m = gfc_match_actual_arglist (1, &arglist);
4669 if (m == MATCH_NO)
4670 goto syntax;
4671 if (m == MATCH_ERROR)
4672 goto cleanup;
4673
4674 if (gfc_match_eos () != MATCH_YES)
4675 goto syntax;
4676 }
4677
4678 /* If any alternate return labels were found, construct a SELECT
4679 statement that will jump to the right place. */
4680
4681 i = 0;
4682 for (a = arglist; a; a = a->next)
4683 if (a->expr == NULL)
4684 {
4685 i = 1;
4686 break;
4687 }
4688
4689 if (i)
4690 {
4691 gfc_symtree *select_st;
4692 gfc_symbol *select_sym;
4693 char name[GFC_MAX_SYMBOL_LEN + 1];
4694
4695 new_st.next = c = gfc_get_code (EXEC_SELECT);
4696 sprintf (name, "_result_%s", sym->name);
4697 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
4698
4699 select_sym = select_st->n.sym;
4700 select_sym->ts.type = BT_INTEGER;
4701 select_sym->ts.kind = gfc_default_integer_kind;
4702 gfc_set_sym_referenced (select_sym);
4703 c->expr1 = gfc_get_expr ();
4704 c->expr1->expr_type = EXPR_VARIABLE;
4705 c->expr1->symtree = select_st;
4706 c->expr1->ts = select_sym->ts;
4707 c->expr1->where = gfc_current_locus;
4708
4709 i = 0;
4710 for (a = arglist; a; a = a->next)
4711 {
4712 if (a->expr != NULL)
4713 continue;
4714
4715 if (!gfc_reference_st_label (a->label, ST_LABEL_TARGET))
4716 continue;
4717
4718 i++;
4719
4720 c->block = gfc_get_code (EXEC_SELECT);
4721 c = c->block;
4722
4723 new_case = gfc_get_case ();
4724 new_case->high = gfc_get_int_expr (gfc_default_integer_kind, NULL, i);
4725 new_case->low = new_case->high;
4726 c->ext.block.case_list = new_case;
4727
4728 c->next = gfc_get_code (EXEC_GOTO);
4729 c->next->label1 = a->label;
4730 }
4731 }
4732
4733 new_st.op = EXEC_CALL;
4734 new_st.symtree = st;
4735 new_st.ext.actual = arglist;
4736
4737 return MATCH_YES;
4738
4739 syntax:
4740 gfc_syntax_error (ST_CALL);
4741
4742 cleanup:
4743 gfc_free_actual_arglist (arglist);
4744 return MATCH_ERROR;
4745 }
4746
4747
4748 /* Given a name, return a pointer to the common head structure,
4749 creating it if it does not exist. If FROM_MODULE is nonzero, we
4750 mangle the name so that it doesn't interfere with commons defined
4751 in the using namespace.
4752 TODO: Add to global symbol tree. */
4753
4754 gfc_common_head *
4755 gfc_get_common (const char *name, int from_module)
4756 {
4757 gfc_symtree *st;
4758 static int serial = 0;
4759 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
4760
4761 if (from_module)
4762 {
4763 /* A use associated common block is only needed to correctly layout
4764 the variables it contains. */
4765 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
4766 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
4767 }
4768 else
4769 {
4770 st = gfc_find_symtree (gfc_current_ns->common_root, name);
4771
4772 if (st == NULL)
4773 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
4774 }
4775
4776 if (st->n.common == NULL)
4777 {
4778 st->n.common = gfc_get_common_head ();
4779 st->n.common->where = gfc_current_locus;
4780 strcpy (st->n.common->name, name);
4781 }
4782
4783 return st->n.common;
4784 }
4785
4786
4787 /* Match a common block name. */
4788
4789 match match_common_name (char *name)
4790 {
4791 match m;
4792
4793 if (gfc_match_char ('/') == MATCH_NO)
4794 {
4795 name[0] = '\0';
4796 return MATCH_YES;
4797 }
4798
4799 if (gfc_match_char ('/') == MATCH_YES)
4800 {
4801 name[0] = '\0';
4802 return MATCH_YES;
4803 }
4804
4805 m = gfc_match_name (name);
4806
4807 if (m == MATCH_ERROR)
4808 return MATCH_ERROR;
4809 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
4810 return MATCH_YES;
4811
4812 gfc_error ("Syntax error in common block name at %C");
4813 return MATCH_ERROR;
4814 }
4815
4816
4817 /* Match a COMMON statement. */
4818
4819 match
4820 gfc_match_common (void)
4821 {
4822 gfc_symbol *sym, **head, *tail, *other;
4823 char name[GFC_MAX_SYMBOL_LEN + 1];
4824 gfc_common_head *t;
4825 gfc_array_spec *as;
4826 gfc_equiv *e1, *e2;
4827 match m;
4828
4829 as = NULL;
4830
4831 for (;;)
4832 {
4833 m = match_common_name (name);
4834 if (m == MATCH_ERROR)
4835 goto cleanup;
4836
4837 if (name[0] == '\0')
4838 {
4839 t = &gfc_current_ns->blank_common;
4840 if (t->head == NULL)
4841 t->where = gfc_current_locus;
4842 }
4843 else
4844 {
4845 t = gfc_get_common (name, 0);
4846 }
4847 head = &t->head;
4848
4849 if (*head == NULL)
4850 tail = NULL;
4851 else
4852 {
4853 tail = *head;
4854 while (tail->common_next)
4855 tail = tail->common_next;
4856 }
4857
4858 /* Grab the list of symbols. */
4859 for (;;)
4860 {
4861 m = gfc_match_symbol (&sym, 0);
4862 if (m == MATCH_ERROR)
4863 goto cleanup;
4864 if (m == MATCH_NO)
4865 goto syntax;
4866
4867 /* See if we know the current common block is bind(c), and if
4868 so, then see if we can check if the symbol is (which it'll
4869 need to be). This can happen if the bind(c) attr stmt was
4870 applied to the common block, and the variable(s) already
4871 defined, before declaring the common block. */
4872 if (t->is_bind_c == 1)
4873 {
4874 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
4875 {
4876 /* If we find an error, just print it and continue,
4877 cause it's just semantic, and we can see if there
4878 are more errors. */
4879 gfc_error_now ("Variable %qs at %L in common block %qs "
4880 "at %C must be declared with a C "
4881 "interoperable kind since common block "
4882 "%qs is bind(c)",
4883 sym->name, &(sym->declared_at), t->name,
4884 t->name);
4885 }
4886
4887 if (sym->attr.is_bind_c == 1)
4888 gfc_error_now ("Variable %qs in common block %qs at %C can not "
4889 "be bind(c) since it is not global", sym->name,
4890 t->name);
4891 }
4892
4893 if (sym->attr.in_common)
4894 {
4895 gfc_error ("Symbol %qs at %C is already in a COMMON block",
4896 sym->name);
4897 goto cleanup;
4898 }
4899
4900 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
4901 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
4902 {
4903 if (!gfc_notify_std (GFC_STD_GNU, "Initialized symbol %qs at "
4904 "%C can only be COMMON in BLOCK DATA",
4905 sym->name))
4906 goto cleanup;
4907 }
4908
4909 /* Deal with an optional array specification after the
4910 symbol name. */
4911 m = gfc_match_array_spec (&as, true, true);
4912 if (m == MATCH_ERROR)
4913 goto cleanup;
4914
4915 if (m == MATCH_YES)
4916 {
4917 if (as->type != AS_EXPLICIT)
4918 {
4919 gfc_error ("Array specification for symbol %qs in COMMON "
4920 "at %C must be explicit", sym->name);
4921 goto cleanup;
4922 }
4923
4924 if (!gfc_add_dimension (&sym->attr, sym->name, NULL))
4925 goto cleanup;
4926
4927 if (sym->attr.pointer)
4928 {
4929 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
4930 "POINTER array", sym->name);
4931 goto cleanup;
4932 }
4933
4934 sym->as = as;
4935 as = NULL;
4936
4937 }
4938
4939 /* Add the in_common attribute, but ignore the reported errors
4940 if any, and continue matching. */
4941 gfc_add_in_common (&sym->attr, sym->name, NULL);
4942
4943 sym->common_block = t;
4944 sym->common_block->refs++;
4945
4946 if (tail != NULL)
4947 tail->common_next = sym;
4948 else
4949 *head = sym;
4950
4951 tail = sym;
4952
4953 sym->common_head = t;
4954
4955 /* Check to see if the symbol is already in an equivalence group.
4956 If it is, set the other members as being in common. */
4957 if (sym->attr.in_equivalence)
4958 {
4959 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
4960 {
4961 for (e2 = e1; e2; e2 = e2->eq)
4962 if (e2->expr->symtree->n.sym == sym)
4963 goto equiv_found;
4964
4965 continue;
4966
4967 equiv_found:
4968
4969 for (e2 = e1; e2; e2 = e2->eq)
4970 {
4971 other = e2->expr->symtree->n.sym;
4972 if (other->common_head
4973 && other->common_head != sym->common_head)
4974 {
4975 gfc_error ("Symbol %qs, in COMMON block %qs at "
4976 "%C is being indirectly equivalenced to "
4977 "another COMMON block %qs",
4978 sym->name, sym->common_head->name,
4979 other->common_head->name);
4980 goto cleanup;
4981 }
4982 other->attr.in_common = 1;
4983 other->common_head = t;
4984 }
4985 }
4986 }
4987
4988
4989 gfc_gobble_whitespace ();
4990 if (gfc_match_eos () == MATCH_YES)
4991 goto done;
4992 if (gfc_peek_ascii_char () == '/')
4993 break;
4994 if (gfc_match_char (',') != MATCH_YES)
4995 goto syntax;
4996 gfc_gobble_whitespace ();
4997 if (gfc_peek_ascii_char () == '/')
4998 break;
4999 }
5000 }
5001
5002 done:
5003 return MATCH_YES;
5004
5005 syntax:
5006 gfc_syntax_error (ST_COMMON);
5007
5008 cleanup:
5009 gfc_free_array_spec (as);
5010 return MATCH_ERROR;
5011 }
5012
5013
5014 /* Match a BLOCK DATA program unit. */
5015
5016 match
5017 gfc_match_block_data (void)
5018 {
5019 char name[GFC_MAX_SYMBOL_LEN + 1];
5020 gfc_symbol *sym;
5021 match m;
5022
5023 if (gfc_match_eos () == MATCH_YES)
5024 {
5025 gfc_new_block = NULL;
5026 return MATCH_YES;
5027 }
5028
5029 m = gfc_match ("% %n%t", name);
5030 if (m != MATCH_YES)
5031 return MATCH_ERROR;
5032
5033 if (gfc_get_symbol (name, NULL, &sym))
5034 return MATCH_ERROR;
5035
5036 if (!gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL))
5037 return MATCH_ERROR;
5038
5039 gfc_new_block = sym;
5040
5041 return MATCH_YES;
5042 }
5043
5044
5045 /* Free a namelist structure. */
5046
5047 void
5048 gfc_free_namelist (gfc_namelist *name)
5049 {
5050 gfc_namelist *n;
5051
5052 for (; name; name = n)
5053 {
5054 n = name->next;
5055 free (name);
5056 }
5057 }
5058
5059
5060 /* Free an OpenMP namelist structure. */
5061
5062 void
5063 gfc_free_omp_namelist (gfc_omp_namelist *name)
5064 {
5065 gfc_omp_namelist *n;
5066
5067 for (; name; name = n)
5068 {
5069 gfc_free_expr (name->expr);
5070 if (name->udr)
5071 {
5072 if (name->udr->combiner)
5073 gfc_free_statement (name->udr->combiner);
5074 if (name->udr->initializer)
5075 gfc_free_statement (name->udr->initializer);
5076 free (name->udr);
5077 }
5078 n = name->next;
5079 free (name);
5080 }
5081 }
5082
5083
5084 /* Match a NAMELIST statement. */
5085
5086 match
5087 gfc_match_namelist (void)
5088 {
5089 gfc_symbol *group_name, *sym;
5090 gfc_namelist *nl;
5091 match m, m2;
5092
5093 m = gfc_match (" / %s /", &group_name);
5094 if (m == MATCH_NO)
5095 goto syntax;
5096 if (m == MATCH_ERROR)
5097 goto error;
5098
5099 for (;;)
5100 {
5101 if (group_name->ts.type != BT_UNKNOWN)
5102 {
5103 gfc_error ("Namelist group name %qs at %C already has a basic "
5104 "type of %s", group_name->name,
5105 gfc_typename (&group_name->ts));
5106 return MATCH_ERROR;
5107 }
5108
5109 if (group_name->attr.flavor == FL_NAMELIST
5110 && group_name->attr.use_assoc
5111 && !gfc_notify_std (GFC_STD_GNU, "Namelist group name %qs "
5112 "at %C already is USE associated and can"
5113 "not be respecified.", group_name->name))
5114 return MATCH_ERROR;
5115
5116 if (group_name->attr.flavor != FL_NAMELIST
5117 && !gfc_add_flavor (&group_name->attr, FL_NAMELIST,
5118 group_name->name, NULL))
5119 return MATCH_ERROR;
5120
5121 for (;;)
5122 {
5123 m = gfc_match_symbol (&sym, 1);
5124 if (m == MATCH_NO)
5125 goto syntax;
5126 if (m == MATCH_ERROR)
5127 goto error;
5128
5129 if (sym->attr.in_namelist == 0
5130 && !gfc_add_in_namelist (&sym->attr, sym->name, NULL))
5131 goto error;
5132
5133 /* Use gfc_error_check here, rather than goto error, so that
5134 these are the only errors for the next two lines. */
5135 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
5136 {
5137 gfc_error ("Assumed size array %qs in namelist %qs at "
5138 "%C is not allowed", sym->name, group_name->name);
5139 gfc_error_check ();
5140 }
5141
5142 nl = gfc_get_namelist ();
5143 nl->sym = sym;
5144 sym->refs++;
5145
5146 if (group_name->namelist == NULL)
5147 group_name->namelist = group_name->namelist_tail = nl;
5148 else
5149 {
5150 group_name->namelist_tail->next = nl;
5151 group_name->namelist_tail = nl;
5152 }
5153
5154 if (gfc_match_eos () == MATCH_YES)
5155 goto done;
5156
5157 m = gfc_match_char (',');
5158
5159 if (gfc_match_char ('/') == MATCH_YES)
5160 {
5161 m2 = gfc_match (" %s /", &group_name);
5162 if (m2 == MATCH_YES)
5163 break;
5164 if (m2 == MATCH_ERROR)
5165 goto error;
5166 goto syntax;
5167 }
5168
5169 if (m != MATCH_YES)
5170 goto syntax;
5171 }
5172 }
5173
5174 done:
5175 return MATCH_YES;
5176
5177 syntax:
5178 gfc_syntax_error (ST_NAMELIST);
5179
5180 error:
5181 return MATCH_ERROR;
5182 }
5183
5184
5185 /* Match a MODULE statement. */
5186
5187 match
5188 gfc_match_module (void)
5189 {
5190 match m;
5191
5192 m = gfc_match (" %s%t", &gfc_new_block);
5193 if (m != MATCH_YES)
5194 return m;
5195
5196 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
5197 gfc_new_block->name, NULL))
5198 return MATCH_ERROR;
5199
5200 return MATCH_YES;
5201 }
5202
5203
5204 /* Free equivalence sets and lists. Recursively is the easiest way to
5205 do this. */
5206
5207 void
5208 gfc_free_equiv_until (gfc_equiv *eq, gfc_equiv *stop)
5209 {
5210 if (eq == stop)
5211 return;
5212
5213 gfc_free_equiv (eq->eq);
5214 gfc_free_equiv_until (eq->next, stop);
5215 gfc_free_expr (eq->expr);
5216 free (eq);
5217 }
5218
5219
5220 void
5221 gfc_free_equiv (gfc_equiv *eq)
5222 {
5223 gfc_free_equiv_until (eq, NULL);
5224 }
5225
5226
5227 /* Match an EQUIVALENCE statement. */
5228
5229 match
5230 gfc_match_equivalence (void)
5231 {
5232 gfc_equiv *eq, *set, *tail;
5233 gfc_ref *ref;
5234 gfc_symbol *sym;
5235 match m;
5236 gfc_common_head *common_head = NULL;
5237 bool common_flag;
5238 int cnt;
5239
5240 tail = NULL;
5241
5242 for (;;)
5243 {
5244 eq = gfc_get_equiv ();
5245 if (tail == NULL)
5246 tail = eq;
5247
5248 eq->next = gfc_current_ns->equiv;
5249 gfc_current_ns->equiv = eq;
5250
5251 if (gfc_match_char ('(') != MATCH_YES)
5252 goto syntax;
5253
5254 set = eq;
5255 common_flag = FALSE;
5256 cnt = 0;
5257
5258 for (;;)
5259 {
5260 m = gfc_match_equiv_variable (&set->expr);
5261 if (m == MATCH_ERROR)
5262 goto cleanup;
5263 if (m == MATCH_NO)
5264 goto syntax;
5265
5266 /* count the number of objects. */
5267 cnt++;
5268
5269 if (gfc_match_char ('%') == MATCH_YES)
5270 {
5271 gfc_error ("Derived type component %C is not a "
5272 "permitted EQUIVALENCE member");
5273 goto cleanup;
5274 }
5275
5276 for (ref = set->expr->ref; ref; ref = ref->next)
5277 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
5278 {
5279 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
5280 "be an array section");
5281 goto cleanup;
5282 }
5283
5284 sym = set->expr->symtree->n.sym;
5285
5286 if (!gfc_add_in_equivalence (&sym->attr, sym->name, NULL))
5287 goto cleanup;
5288
5289 if (sym->attr.in_common)
5290 {
5291 common_flag = TRUE;
5292 common_head = sym->common_head;
5293 }
5294
5295 if (gfc_match_char (')') == MATCH_YES)
5296 break;
5297
5298 if (gfc_match_char (',') != MATCH_YES)
5299 goto syntax;
5300
5301 set->eq = gfc_get_equiv ();
5302 set = set->eq;
5303 }
5304
5305 if (cnt < 2)
5306 {
5307 gfc_error ("EQUIVALENCE at %C requires two or more objects");
5308 goto cleanup;
5309 }
5310
5311 /* If one of the members of an equivalence is in common, then
5312 mark them all as being in common. Before doing this, check
5313 that members of the equivalence group are not in different
5314 common blocks. */
5315 if (common_flag)
5316 for (set = eq; set; set = set->eq)
5317 {
5318 sym = set->expr->symtree->n.sym;
5319 if (sym->common_head && sym->common_head != common_head)
5320 {
5321 gfc_error ("Attempt to indirectly overlap COMMON "
5322 "blocks %s and %s by EQUIVALENCE at %C",
5323 sym->common_head->name, common_head->name);
5324 goto cleanup;
5325 }
5326 sym->attr.in_common = 1;
5327 sym->common_head = common_head;
5328 }
5329
5330 if (gfc_match_eos () == MATCH_YES)
5331 break;
5332 if (gfc_match_char (',') != MATCH_YES)
5333 {
5334 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
5335 goto cleanup;
5336 }
5337 }
5338
5339 return MATCH_YES;
5340
5341 syntax:
5342 gfc_syntax_error (ST_EQUIVALENCE);
5343
5344 cleanup:
5345 eq = tail->next;
5346 tail->next = NULL;
5347
5348 gfc_free_equiv (gfc_current_ns->equiv);
5349 gfc_current_ns->equiv = eq;
5350
5351 return MATCH_ERROR;
5352 }
5353
5354
5355 /* Check that a statement function is not recursive. This is done by looking
5356 for the statement function symbol(sym) by looking recursively through its
5357 expression(e). If a reference to sym is found, true is returned.
5358 12.5.4 requires that any variable of function that is implicitly typed
5359 shall have that type confirmed by any subsequent type declaration. The
5360 implicit typing is conveniently done here. */
5361 static bool
5362 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
5363
5364 static bool
5365 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5366 {
5367
5368 if (e == NULL)
5369 return false;
5370
5371 switch (e->expr_type)
5372 {
5373 case EXPR_FUNCTION:
5374 if (e->symtree == NULL)
5375 return false;
5376
5377 /* Check the name before testing for nested recursion! */
5378 if (sym->name == e->symtree->n.sym->name)
5379 return true;
5380
5381 /* Catch recursion via other statement functions. */
5382 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
5383 && e->symtree->n.sym->value
5384 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
5385 return true;
5386
5387 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5388 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5389
5390 break;
5391
5392 case EXPR_VARIABLE:
5393 if (e->symtree && sym->name == e->symtree->n.sym->name)
5394 return true;
5395
5396 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5397 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5398 break;
5399
5400 default:
5401 break;
5402 }
5403
5404 return false;
5405 }
5406
5407
5408 static bool
5409 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
5410 {
5411 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
5412 }
5413
5414
5415 /* Match a statement function declaration. It is so easy to match
5416 non-statement function statements with a MATCH_ERROR as opposed to
5417 MATCH_NO that we suppress error message in most cases. */
5418
5419 match
5420 gfc_match_st_function (void)
5421 {
5422 gfc_error_buffer old_error;
5423 gfc_symbol *sym;
5424 gfc_expr *expr;
5425 match m;
5426
5427 m = gfc_match_symbol (&sym, 0);
5428 if (m != MATCH_YES)
5429 return m;
5430
5431 gfc_push_error (&old_error);
5432
5433 if (!gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION, sym->name, NULL))
5434 goto undo_error;
5435
5436 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
5437 goto undo_error;
5438
5439 m = gfc_match (" = %e%t", &expr);
5440 if (m == MATCH_NO)
5441 goto undo_error;
5442
5443 gfc_free_error (&old_error);
5444
5445 if (m == MATCH_ERROR)
5446 return m;
5447
5448 if (recursive_stmt_fcn (expr, sym))
5449 {
5450 gfc_error ("Statement function at %L is recursive", &expr->where);
5451 return MATCH_ERROR;
5452 }
5453
5454 sym->value = expr;
5455
5456 if ((gfc_current_state () == COMP_FUNCTION
5457 || gfc_current_state () == COMP_SUBROUTINE)
5458 && gfc_state_stack->previous->state == COMP_INTERFACE)
5459 {
5460 gfc_error ("Statement function at %L cannot appear within an INTERFACE",
5461 &expr->where);
5462 return MATCH_ERROR;
5463 }
5464
5465 if (!gfc_notify_std (GFC_STD_F95_OBS, "Statement function at %C"))
5466 return MATCH_ERROR;
5467
5468 return MATCH_YES;
5469
5470 undo_error:
5471 gfc_pop_error (&old_error);
5472 return MATCH_NO;
5473 }
5474
5475
5476 /* Match an assignment to a pointer function (F2008). This could, in
5477 general be ambiguous with a statement function. In this implementation
5478 it remains so if it is the first statement after the specification
5479 block. */
5480
5481 match
5482 gfc_match_ptr_fcn_assign (void)
5483 {
5484 gfc_error_buffer old_error;
5485 locus old_loc;
5486 gfc_symbol *sym;
5487 gfc_expr *expr;
5488 match m;
5489 char name[GFC_MAX_SYMBOL_LEN + 1];
5490
5491 old_loc = gfc_current_locus;
5492 m = gfc_match_name (name);
5493 if (m != MATCH_YES)
5494 return m;
5495
5496 gfc_find_symbol (name, NULL, 1, &sym);
5497 if (sym && sym->attr.flavor != FL_PROCEDURE)
5498 return MATCH_NO;
5499
5500 gfc_push_error (&old_error);
5501
5502 if (sym && sym->attr.function)
5503 goto match_actual_arglist;
5504
5505 gfc_current_locus = old_loc;
5506 m = gfc_match_symbol (&sym, 0);
5507 if (m != MATCH_YES)
5508 return m;
5509
5510 if (!gfc_add_procedure (&sym->attr, PROC_UNKNOWN, sym->name, NULL))
5511 goto undo_error;
5512
5513 match_actual_arglist:
5514 gfc_current_locus = old_loc;
5515 m = gfc_match (" %e", &expr);
5516 if (m != MATCH_YES)
5517 goto undo_error;
5518
5519 new_st.op = EXEC_ASSIGN;
5520 new_st.expr1 = expr;
5521 expr = NULL;
5522
5523 m = gfc_match (" = %e%t", &expr);
5524 if (m != MATCH_YES)
5525 goto undo_error;
5526
5527 new_st.expr2 = expr;
5528 return MATCH_YES;
5529
5530 undo_error:
5531 gfc_pop_error (&old_error);
5532 return MATCH_NO;
5533 }
5534
5535
5536 /***************** SELECT CASE subroutines ******************/
5537
5538 /* Free a single case structure. */
5539
5540 static void
5541 free_case (gfc_case *p)
5542 {
5543 if (p->low == p->high)
5544 p->high = NULL;
5545 gfc_free_expr (p->low);
5546 gfc_free_expr (p->high);
5547 free (p);
5548 }
5549
5550
5551 /* Free a list of case structures. */
5552
5553 void
5554 gfc_free_case_list (gfc_case *p)
5555 {
5556 gfc_case *q;
5557
5558 for (; p; p = q)
5559 {
5560 q = p->next;
5561 free_case (p);
5562 }
5563 }
5564
5565
5566 /* Match a single case selector. Combining the requirements of F08:C830
5567 and F08:C832 (R838) means that the case-value must have either CHARACTER,
5568 INTEGER, or LOGICAL type. */
5569
5570 static match
5571 match_case_selector (gfc_case **cp)
5572 {
5573 gfc_case *c;
5574 match m;
5575
5576 c = gfc_get_case ();
5577 c->where = gfc_current_locus;
5578
5579 if (gfc_match_char (':') == MATCH_YES)
5580 {
5581 m = gfc_match_init_expr (&c->high);
5582 if (m == MATCH_NO)
5583 goto need_expr;
5584 if (m == MATCH_ERROR)
5585 goto cleanup;
5586
5587 if (c->high->ts.type != BT_LOGICAL && c->high->ts.type != BT_INTEGER
5588 && c->high->ts.type != BT_CHARACTER)
5589 {
5590 gfc_error ("Expression in CASE selector at %L cannot be %s",
5591 &c->high->where, gfc_typename (&c->high->ts));
5592 goto cleanup;
5593 }
5594 }
5595 else
5596 {
5597 m = gfc_match_init_expr (&c->low);
5598 if (m == MATCH_ERROR)
5599 goto cleanup;
5600 if (m == MATCH_NO)
5601 goto need_expr;
5602
5603 if (c->low->ts.type != BT_LOGICAL && c->low->ts.type != BT_INTEGER
5604 && c->low->ts.type != BT_CHARACTER)
5605 {
5606 gfc_error ("Expression in CASE selector at %L cannot be %s",
5607 &c->low->where, gfc_typename (&c->low->ts));
5608 goto cleanup;
5609 }
5610
5611 /* If we're not looking at a ':' now, make a range out of a single
5612 target. Else get the upper bound for the case range. */
5613 if (gfc_match_char (':') != MATCH_YES)
5614 c->high = c->low;
5615 else
5616 {
5617 m = gfc_match_init_expr (&c->high);
5618 if (m == MATCH_ERROR)
5619 goto cleanup;
5620 /* MATCH_NO is fine. It's OK if nothing is there! */
5621 }
5622 }
5623
5624 *cp = c;
5625 return MATCH_YES;
5626
5627 need_expr:
5628 gfc_error ("Expected initialization expression in CASE at %C");
5629
5630 cleanup:
5631 free_case (c);
5632 return MATCH_ERROR;
5633 }
5634
5635
5636 /* Match the end of a case statement. */
5637
5638 static match
5639 match_case_eos (void)
5640 {
5641 char name[GFC_MAX_SYMBOL_LEN + 1];
5642 match m;
5643
5644 if (gfc_match_eos () == MATCH_YES)
5645 return MATCH_YES;
5646
5647 /* If the case construct doesn't have a case-construct-name, we
5648 should have matched the EOS. */
5649 if (!gfc_current_block ())
5650 return MATCH_NO;
5651
5652 gfc_gobble_whitespace ();
5653
5654 m = gfc_match_name (name);
5655 if (m != MATCH_YES)
5656 return m;
5657
5658 if (strcmp (name, gfc_current_block ()->name) != 0)
5659 {
5660 gfc_error ("Expected block name %qs of SELECT construct at %C",
5661 gfc_current_block ()->name);
5662 return MATCH_ERROR;
5663 }
5664
5665 return gfc_match_eos ();
5666 }
5667
5668
5669 /* Match a SELECT statement. */
5670
5671 match
5672 gfc_match_select (void)
5673 {
5674 gfc_expr *expr;
5675 match m;
5676
5677 m = gfc_match_label ();
5678 if (m == MATCH_ERROR)
5679 return m;
5680
5681 m = gfc_match (" select case ( %e )%t", &expr);
5682 if (m != MATCH_YES)
5683 return m;
5684
5685 new_st.op = EXEC_SELECT;
5686 new_st.expr1 = expr;
5687
5688 return MATCH_YES;
5689 }
5690
5691
5692 /* Transfer the selector typespec to the associate name. */
5693
5694 static void
5695 copy_ts_from_selector_to_associate (gfc_expr *associate, gfc_expr *selector)
5696 {
5697 gfc_ref *ref;
5698 gfc_symbol *assoc_sym;
5699
5700 assoc_sym = associate->symtree->n.sym;
5701
5702 /* At this stage the expression rank and arrayspec dimensions have
5703 not been completely sorted out. We must get the expr2->rank
5704 right here, so that the correct class container is obtained. */
5705 ref = selector->ref;
5706 while (ref && ref->next)
5707 ref = ref->next;
5708
5709 if (selector->ts.type == BT_CLASS && CLASS_DATA (selector)->as
5710 && ref && ref->type == REF_ARRAY)
5711 {
5712 /* Ensure that the array reference type is set. We cannot use
5713 gfc_resolve_expr at this point, so the usable parts of
5714 resolve.c(resolve_array_ref) are employed to do it. */
5715 if (ref->u.ar.type == AR_UNKNOWN)
5716 {
5717 ref->u.ar.type = AR_ELEMENT;
5718 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
5719 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5720 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR
5721 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
5722 && ref->u.ar.start[i] && ref->u.ar.start[i]->rank))
5723 {
5724 ref->u.ar.type = AR_SECTION;
5725 break;
5726 }
5727 }
5728
5729 if (ref->u.ar.type == AR_FULL)
5730 selector->rank = CLASS_DATA (selector)->as->rank;
5731 else if (ref->u.ar.type == AR_SECTION)
5732 selector->rank = ref->u.ar.dimen;
5733 else
5734 selector->rank = 0;
5735 }
5736
5737 if (selector->rank)
5738 {
5739 assoc_sym->attr.dimension = 1;
5740 assoc_sym->as = gfc_get_array_spec ();
5741 assoc_sym->as->rank = selector->rank;
5742 assoc_sym->as->type = AS_DEFERRED;
5743 }
5744 else
5745 assoc_sym->as = NULL;
5746
5747 if (selector->ts.type == BT_CLASS)
5748 {
5749 /* The correct class container has to be available. */
5750 assoc_sym->ts.type = BT_CLASS;
5751 assoc_sym->ts.u.derived = CLASS_DATA (selector)->ts.u.derived;
5752 assoc_sym->attr.pointer = 1;
5753 gfc_build_class_symbol (&assoc_sym->ts, &assoc_sym->attr, &assoc_sym->as);
5754 }
5755 }
5756
5757
5758 /* Push the current selector onto the SELECT TYPE stack. */
5759
5760 static void
5761 select_type_push (gfc_symbol *sel)
5762 {
5763 gfc_select_type_stack *top = gfc_get_select_type_stack ();
5764 top->selector = sel;
5765 top->tmp = NULL;
5766 top->prev = select_type_stack;
5767
5768 select_type_stack = top;
5769 }
5770
5771
5772 /* Set the temporary for the current intrinsic SELECT TYPE selector. */
5773
5774 static gfc_symtree *
5775 select_intrinsic_set_tmp (gfc_typespec *ts)
5776 {
5777 char name[GFC_MAX_SYMBOL_LEN];
5778 gfc_symtree *tmp;
5779 int charlen = 0;
5780
5781 if (ts->type == BT_CLASS || ts->type == BT_DERIVED)
5782 return NULL;
5783
5784 if (select_type_stack->selector->ts.type == BT_CLASS
5785 && !select_type_stack->selector->attr.class_ok)
5786 return NULL;
5787
5788 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length
5789 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
5790 charlen = mpz_get_si (ts->u.cl->length->value.integer);
5791
5792 if (ts->type != BT_CHARACTER)
5793 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (ts->type),
5794 ts->kind);
5795 else
5796 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (ts->type),
5797 charlen, ts->kind);
5798
5799 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
5800 gfc_add_type (tmp->n.sym, ts, NULL);
5801
5802 /* Copy across the array spec to the selector. */
5803 if (select_type_stack->selector->ts.type == BT_CLASS
5804 && (CLASS_DATA (select_type_stack->selector)->attr.dimension
5805 || CLASS_DATA (select_type_stack->selector)->attr.codimension))
5806 {
5807 tmp->n.sym->attr.pointer = 1;
5808 tmp->n.sym->attr.dimension
5809 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
5810 tmp->n.sym->attr.codimension
5811 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
5812 tmp->n.sym->as
5813 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
5814 }
5815
5816 gfc_set_sym_referenced (tmp->n.sym);
5817 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
5818 tmp->n.sym->attr.select_type_temporary = 1;
5819
5820 return tmp;
5821 }
5822
5823
5824 /* Set up a temporary for the current TYPE IS / CLASS IS branch . */
5825
5826 static void
5827 select_type_set_tmp (gfc_typespec *ts)
5828 {
5829 char name[GFC_MAX_SYMBOL_LEN];
5830 gfc_symtree *tmp = NULL;
5831
5832 if (!ts)
5833 {
5834 select_type_stack->tmp = NULL;
5835 return;
5836 }
5837
5838 tmp = select_intrinsic_set_tmp (ts);
5839
5840 if (tmp == NULL)
5841 {
5842 if (!ts->u.derived)
5843 return;
5844
5845 if (ts->type == BT_CLASS)
5846 sprintf (name, "__tmp_class_%s", ts->u.derived->name);
5847 else
5848 sprintf (name, "__tmp_type_%s", ts->u.derived->name);
5849 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
5850 gfc_add_type (tmp->n.sym, ts, NULL);
5851
5852 if (select_type_stack->selector->ts.type == BT_CLASS
5853 && select_type_stack->selector->attr.class_ok)
5854 {
5855 tmp->n.sym->attr.pointer
5856 = CLASS_DATA (select_type_stack->selector)->attr.class_pointer;
5857
5858 /* Copy across the array spec to the selector. */
5859 if (CLASS_DATA (select_type_stack->selector)->attr.dimension
5860 || CLASS_DATA (select_type_stack->selector)->attr.codimension)
5861 {
5862 tmp->n.sym->attr.dimension
5863 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
5864 tmp->n.sym->attr.codimension
5865 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
5866 tmp->n.sym->as
5867 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
5868 }
5869 }
5870
5871 gfc_set_sym_referenced (tmp->n.sym);
5872 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
5873 tmp->n.sym->attr.select_type_temporary = 1;
5874
5875 if (ts->type == BT_CLASS)
5876 gfc_build_class_symbol (&tmp->n.sym->ts, &tmp->n.sym->attr,
5877 &tmp->n.sym->as);
5878 }
5879
5880 /* Add an association for it, so the rest of the parser knows it is
5881 an associate-name. The target will be set during resolution. */
5882 tmp->n.sym->assoc = gfc_get_association_list ();
5883 tmp->n.sym->assoc->dangling = 1;
5884 tmp->n.sym->assoc->st = tmp;
5885
5886 select_type_stack->tmp = tmp;
5887 }
5888
5889
5890 /* Match a SELECT TYPE statement. */
5891
5892 match
5893 gfc_match_select_type (void)
5894 {
5895 gfc_expr *expr1, *expr2 = NULL;
5896 match m;
5897 char name[GFC_MAX_SYMBOL_LEN];
5898 bool class_array;
5899 gfc_symbol *sym;
5900 gfc_namespace *ns = gfc_current_ns;
5901
5902 m = gfc_match_label ();
5903 if (m == MATCH_ERROR)
5904 return m;
5905
5906 m = gfc_match (" select type ( ");
5907 if (m != MATCH_YES)
5908 return m;
5909
5910 gfc_current_ns = gfc_build_block_ns (ns);
5911 m = gfc_match (" %n => %e", name, &expr2);
5912 if (m == MATCH_YES)
5913 {
5914 expr1 = gfc_get_expr ();
5915 expr1->expr_type = EXPR_VARIABLE;
5916 expr1->where = expr2->where;
5917 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
5918 {
5919 m = MATCH_ERROR;
5920 goto cleanup;
5921 }
5922
5923 sym = expr1->symtree->n.sym;
5924 if (expr2->ts.type == BT_UNKNOWN)
5925 sym->attr.untyped = 1;
5926 else
5927 copy_ts_from_selector_to_associate (expr1, expr2);
5928
5929 sym->attr.flavor = FL_VARIABLE;
5930 sym->attr.referenced = 1;
5931 sym->attr.class_ok = 1;
5932 }
5933 else
5934 {
5935 m = gfc_match (" %e ", &expr1);
5936 if (m != MATCH_YES)
5937 {
5938 std::swap (ns, gfc_current_ns);
5939 gfc_free_namespace (ns);
5940 return m;
5941 }
5942 }
5943
5944 m = gfc_match (" )%t");
5945 if (m != MATCH_YES)
5946 {
5947 gfc_error ("parse error in SELECT TYPE statement at %C");
5948 goto cleanup;
5949 }
5950
5951 /* This ghastly expression seems to be needed to distinguish a CLASS
5952 array, which can have a reference, from other expressions that
5953 have references, such as derived type components, and are not
5954 allowed by the standard.
5955 TODO: see if it is sufficient to exclude component and substring
5956 references. */
5957 class_array = (expr1->expr_type == EXPR_VARIABLE
5958 && expr1->ts.type == BT_CLASS
5959 && CLASS_DATA (expr1)
5960 && (strcmp (CLASS_DATA (expr1)->name, "_data") == 0)
5961 && (CLASS_DATA (expr1)->attr.dimension
5962 || CLASS_DATA (expr1)->attr.codimension)
5963 && expr1->ref
5964 && expr1->ref->type == REF_ARRAY
5965 && expr1->ref->next == NULL);
5966
5967 /* Check for F03:C811. */
5968 if (!expr2 && (expr1->expr_type != EXPR_VARIABLE
5969 || (!class_array && expr1->ref != NULL)))
5970 {
5971 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
5972 "use associate-name=>");
5973 m = MATCH_ERROR;
5974 goto cleanup;
5975 }
5976
5977 new_st.op = EXEC_SELECT_TYPE;
5978 new_st.expr1 = expr1;
5979 new_st.expr2 = expr2;
5980 new_st.ext.block.ns = gfc_current_ns;
5981
5982 select_type_push (expr1->symtree->n.sym);
5983 gfc_current_ns = ns;
5984
5985 return MATCH_YES;
5986
5987 cleanup:
5988 gfc_free_expr (expr1);
5989 gfc_free_expr (expr2);
5990 gfc_undo_symbols ();
5991 std::swap (ns, gfc_current_ns);
5992 gfc_free_namespace (ns);
5993 return m;
5994 }
5995
5996
5997 /* Match a CASE statement. */
5998
5999 match
6000 gfc_match_case (void)
6001 {
6002 gfc_case *c, *head, *tail;
6003 match m;
6004
6005 head = tail = NULL;
6006
6007 if (gfc_current_state () != COMP_SELECT)
6008 {
6009 gfc_error ("Unexpected CASE statement at %C");
6010 return MATCH_ERROR;
6011 }
6012
6013 if (gfc_match ("% default") == MATCH_YES)
6014 {
6015 m = match_case_eos ();
6016 if (m == MATCH_NO)
6017 goto syntax;
6018 if (m == MATCH_ERROR)
6019 goto cleanup;
6020
6021 new_st.op = EXEC_SELECT;
6022 c = gfc_get_case ();
6023 c->where = gfc_current_locus;
6024 new_st.ext.block.case_list = c;
6025 return MATCH_YES;
6026 }
6027
6028 if (gfc_match_char ('(') != MATCH_YES)
6029 goto syntax;
6030
6031 for (;;)
6032 {
6033 if (match_case_selector (&c) == MATCH_ERROR)
6034 goto cleanup;
6035
6036 if (head == NULL)
6037 head = c;
6038 else
6039 tail->next = c;
6040
6041 tail = c;
6042
6043 if (gfc_match_char (')') == MATCH_YES)
6044 break;
6045 if (gfc_match_char (',') != MATCH_YES)
6046 goto syntax;
6047 }
6048
6049 m = match_case_eos ();
6050 if (m == MATCH_NO)
6051 goto syntax;
6052 if (m == MATCH_ERROR)
6053 goto cleanup;
6054
6055 new_st.op = EXEC_SELECT;
6056 new_st.ext.block.case_list = head;
6057
6058 return MATCH_YES;
6059
6060 syntax:
6061 gfc_error ("Syntax error in CASE specification at %C");
6062
6063 cleanup:
6064 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
6065 return MATCH_ERROR;
6066 }
6067
6068
6069 /* Match a TYPE IS statement. */
6070
6071 match
6072 gfc_match_type_is (void)
6073 {
6074 gfc_case *c = NULL;
6075 match m;
6076
6077 if (gfc_current_state () != COMP_SELECT_TYPE)
6078 {
6079 gfc_error ("Unexpected TYPE IS statement at %C");
6080 return MATCH_ERROR;
6081 }
6082
6083 if (gfc_match_char ('(') != MATCH_YES)
6084 goto syntax;
6085
6086 c = gfc_get_case ();
6087 c->where = gfc_current_locus;
6088
6089 m = gfc_match_type_spec (&c->ts);
6090 if (m == MATCH_NO)
6091 goto syntax;
6092 if (m == MATCH_ERROR)
6093 goto cleanup;
6094
6095 if (gfc_match_char (')') != MATCH_YES)
6096 goto syntax;
6097
6098 m = match_case_eos ();
6099 if (m == MATCH_NO)
6100 goto syntax;
6101 if (m == MATCH_ERROR)
6102 goto cleanup;
6103
6104 new_st.op = EXEC_SELECT_TYPE;
6105 new_st.ext.block.case_list = c;
6106
6107 if (c->ts.type == BT_DERIVED && c->ts.u.derived
6108 && (c->ts.u.derived->attr.sequence
6109 || c->ts.u.derived->attr.is_bind_c))
6110 {
6111 gfc_error ("The type-spec shall not specify a sequence derived "
6112 "type or a type with the BIND attribute in SELECT "
6113 "TYPE at %C [F2003:C815]");
6114 return MATCH_ERROR;
6115 }
6116
6117 /* Create temporary variable. */
6118 select_type_set_tmp (&c->ts);
6119
6120 return MATCH_YES;
6121
6122 syntax:
6123 gfc_error ("Syntax error in TYPE IS specification at %C");
6124
6125 cleanup:
6126 if (c != NULL)
6127 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
6128 return MATCH_ERROR;
6129 }
6130
6131
6132 /* Match a CLASS IS or CLASS DEFAULT statement. */
6133
6134 match
6135 gfc_match_class_is (void)
6136 {
6137 gfc_case *c = NULL;
6138 match m;
6139
6140 if (gfc_current_state () != COMP_SELECT_TYPE)
6141 return MATCH_NO;
6142
6143 if (gfc_match ("% default") == MATCH_YES)
6144 {
6145 m = match_case_eos ();
6146 if (m == MATCH_NO)
6147 goto syntax;
6148 if (m == MATCH_ERROR)
6149 goto cleanup;
6150
6151 new_st.op = EXEC_SELECT_TYPE;
6152 c = gfc_get_case ();
6153 c->where = gfc_current_locus;
6154 c->ts.type = BT_UNKNOWN;
6155 new_st.ext.block.case_list = c;
6156 select_type_set_tmp (NULL);
6157 return MATCH_YES;
6158 }
6159
6160 m = gfc_match ("% is");
6161 if (m == MATCH_NO)
6162 goto syntax;
6163 if (m == MATCH_ERROR)
6164 goto cleanup;
6165
6166 if (gfc_match_char ('(') != MATCH_YES)
6167 goto syntax;
6168
6169 c = gfc_get_case ();
6170 c->where = gfc_current_locus;
6171
6172 m = match_derived_type_spec (&c->ts);
6173 if (m == MATCH_NO)
6174 goto syntax;
6175 if (m == MATCH_ERROR)
6176 goto cleanup;
6177
6178 if (c->ts.type == BT_DERIVED)
6179 c->ts.type = BT_CLASS;
6180
6181 if (gfc_match_char (')') != MATCH_YES)
6182 goto syntax;
6183
6184 m = match_case_eos ();
6185 if (m == MATCH_NO)
6186 goto syntax;
6187 if (m == MATCH_ERROR)
6188 goto cleanup;
6189
6190 new_st.op = EXEC_SELECT_TYPE;
6191 new_st.ext.block.case_list = c;
6192
6193 /* Create temporary variable. */
6194 select_type_set_tmp (&c->ts);
6195
6196 return MATCH_YES;
6197
6198 syntax:
6199 gfc_error ("Syntax error in CLASS IS specification at %C");
6200
6201 cleanup:
6202 if (c != NULL)
6203 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
6204 return MATCH_ERROR;
6205 }
6206
6207
6208 /********************* WHERE subroutines ********************/
6209
6210 /* Match the rest of a simple WHERE statement that follows an IF statement.
6211 */
6212
6213 static match
6214 match_simple_where (void)
6215 {
6216 gfc_expr *expr;
6217 gfc_code *c;
6218 match m;
6219
6220 m = gfc_match (" ( %e )", &expr);
6221 if (m != MATCH_YES)
6222 return m;
6223
6224 m = gfc_match_assignment ();
6225 if (m == MATCH_NO)
6226 goto syntax;
6227 if (m == MATCH_ERROR)
6228 goto cleanup;
6229
6230 if (gfc_match_eos () != MATCH_YES)
6231 goto syntax;
6232
6233 c = gfc_get_code (EXEC_WHERE);
6234 c->expr1 = expr;
6235
6236 c->next = XCNEW (gfc_code);
6237 *c->next = new_st;
6238 c->next->loc = gfc_current_locus;
6239 gfc_clear_new_st ();
6240
6241 new_st.op = EXEC_WHERE;
6242 new_st.block = c;
6243
6244 return MATCH_YES;
6245
6246 syntax:
6247 gfc_syntax_error (ST_WHERE);
6248
6249 cleanup:
6250 gfc_free_expr (expr);
6251 return MATCH_ERROR;
6252 }
6253
6254
6255 /* Match a WHERE statement. */
6256
6257 match
6258 gfc_match_where (gfc_statement *st)
6259 {
6260 gfc_expr *expr;
6261 match m0, m;
6262 gfc_code *c;
6263
6264 m0 = gfc_match_label ();
6265 if (m0 == MATCH_ERROR)
6266 return m0;
6267
6268 m = gfc_match (" where ( %e )", &expr);
6269 if (m != MATCH_YES)
6270 return m;
6271
6272 if (gfc_match_eos () == MATCH_YES)
6273 {
6274 *st = ST_WHERE_BLOCK;
6275 new_st.op = EXEC_WHERE;
6276 new_st.expr1 = expr;
6277 return MATCH_YES;
6278 }
6279
6280 m = gfc_match_assignment ();
6281 if (m == MATCH_NO)
6282 gfc_syntax_error (ST_WHERE);
6283
6284 if (m != MATCH_YES)
6285 {
6286 gfc_free_expr (expr);
6287 return MATCH_ERROR;
6288 }
6289
6290 /* We've got a simple WHERE statement. */
6291 *st = ST_WHERE;
6292 c = gfc_get_code (EXEC_WHERE);
6293 c->expr1 = expr;
6294
6295 /* Put in the assignment. It will not be processed by add_statement, so we
6296 need to copy the location here. */
6297
6298 c->next = XCNEW (gfc_code);
6299 *c->next = new_st;
6300 c->next->loc = gfc_current_locus;
6301 gfc_clear_new_st ();
6302
6303 new_st.op = EXEC_WHERE;
6304 new_st.block = c;
6305
6306 return MATCH_YES;
6307 }
6308
6309
6310 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
6311 new_st if successful. */
6312
6313 match
6314 gfc_match_elsewhere (void)
6315 {
6316 char name[GFC_MAX_SYMBOL_LEN + 1];
6317 gfc_expr *expr;
6318 match m;
6319
6320 if (gfc_current_state () != COMP_WHERE)
6321 {
6322 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
6323 return MATCH_ERROR;
6324 }
6325
6326 expr = NULL;
6327
6328 if (gfc_match_char ('(') == MATCH_YES)
6329 {
6330 m = gfc_match_expr (&expr);
6331 if (m == MATCH_NO)
6332 goto syntax;
6333 if (m == MATCH_ERROR)
6334 return MATCH_ERROR;
6335
6336 if (gfc_match_char (')') != MATCH_YES)
6337 goto syntax;
6338 }
6339
6340 if (gfc_match_eos () != MATCH_YES)
6341 {
6342 /* Only makes sense if we have a where-construct-name. */
6343 if (!gfc_current_block ())
6344 {
6345 m = MATCH_ERROR;
6346 goto cleanup;
6347 }
6348 /* Better be a name at this point. */
6349 m = gfc_match_name (name);
6350 if (m == MATCH_NO)
6351 goto syntax;
6352 if (m == MATCH_ERROR)
6353 goto cleanup;
6354
6355 if (gfc_match_eos () != MATCH_YES)
6356 goto syntax;
6357
6358 if (strcmp (name, gfc_current_block ()->name) != 0)
6359 {
6360 gfc_error ("Label %qs at %C doesn't match WHERE label %qs",
6361 name, gfc_current_block ()->name);
6362 goto cleanup;
6363 }
6364 }
6365
6366 new_st.op = EXEC_WHERE;
6367 new_st.expr1 = expr;
6368 return MATCH_YES;
6369
6370 syntax:
6371 gfc_syntax_error (ST_ELSEWHERE);
6372
6373 cleanup:
6374 gfc_free_expr (expr);
6375 return MATCH_ERROR;
6376 }