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