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