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