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