Debian Codegraph @

gcc/fortran/match.cc

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