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