]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/fortran/match.c
tree-core.h: Include symtab.h.
[thirdparty/gcc.git] / gcc / fortran / match.c
CommitLineData
6de9cd9a 1/* Matching subroutines in all sizes, shapes and colors.
5624e564 2 Copyright (C) 2000-2015 Free Software Foundation, Inc.
6de9cd9a
DN
3 Contributed by Andy Vaught
4
9fc4d79b 5This file is part of GCC.
6de9cd9a 6
9fc4d79b
TS
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
d234d788 9Software Foundation; either version 3, or (at your option) any later
9fc4d79b 10version.
6de9cd9a 11
9fc4d79b
TS
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
6de9cd9a
DN
16
17You should have received a copy of the GNU General Public License
d234d788
NC
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
6de9cd9a 20
6de9cd9a
DN
21#include "config.h"
22#include "system.h"
953bee7c 23#include "coretypes.h"
6de9cd9a 24#include "flags.h"
6de9cd9a
DN
25#include "gfortran.h"
26#include "match.h"
27#include "parse.h"
40e23961 28#include "alias.h"
62603fae 29#include "tree.h"
d8a2d370 30#include "stringpool.h"
6de9cd9a 31
837c4b78 32int gfc_matching_ptr_assignment = 0;
8fb74da4 33int gfc_matching_procptr_assignment = 0;
3df684e2 34bool gfc_matching_prefix = false;
6de9cd9a 35
7431bf06
JW
36/* Stack of SELECT TYPE statements. */
37gfc_select_type_stack *select_type_stack = NULL;
cf2b3c22 38
ba3ba492
RS
39/* For debugging and diagnostic purposes. Return the textual representation
40 of the intrinsic operator OP. */
41const char *
42gfc_op2string (gfc_intrinsic_op op)
43{
44 switch (op)
45 {
46 case INTRINSIC_UPLUS:
47 case INTRINSIC_PLUS:
48 return "+";
49
50 case INTRINSIC_UMINUS:
51 case INTRINSIC_MINUS:
52 return "-";
53
54 case INTRINSIC_POWER:
55 return "**";
56 case INTRINSIC_CONCAT:
57 return "//";
58 case INTRINSIC_TIMES:
59 return "*";
60 case INTRINSIC_DIVIDE:
61 return "/";
62
63 case INTRINSIC_AND:
64 return ".and.";
65 case INTRINSIC_OR:
66 return ".or.";
67 case INTRINSIC_EQV:
68 return ".eqv.";
69 case INTRINSIC_NEQV:
70 return ".neqv.";
71
72 case INTRINSIC_EQ_OS:
73 return ".eq.";
74 case INTRINSIC_EQ:
75 return "==";
76 case INTRINSIC_NE_OS:
77 return ".ne.";
78 case INTRINSIC_NE:
79 return "/=";
80 case INTRINSIC_GE_OS:
81 return ".ge.";
82 case INTRINSIC_GE:
83 return ">=";
84 case INTRINSIC_LE_OS:
85 return ".le.";
86 case INTRINSIC_LE:
87 return "<=";
88 case INTRINSIC_LT_OS:
89 return ".lt.";
90 case INTRINSIC_LT:
91 return "<";
92 case INTRINSIC_GT_OS:
93 return ".gt.";
94 case INTRINSIC_GT:
95 return ">";
96 case INTRINSIC_NOT:
97 return ".not.";
98
99 case INTRINSIC_ASSIGN:
100 return "=";
101
102 case INTRINSIC_PARENTHESES:
103 return "parens";
104
898344a9
SK
105 case INTRINSIC_NONE:
106 return "none";
107
ba3ba492
RS
108 default:
109 break;
110 }
111
112 gfc_internal_error ("gfc_op2string(): Bad code");
113 /* Not reached. */
114}
115
6de9cd9a
DN
116
117/******************** Generic matching subroutines ************************/
118
f9b9fb82
JD
119/* This function scans the current statement counting the opened and closed
120 parenthesis to make sure they are balanced. */
121
122match
123gfc_match_parens (void)
124{
125 locus old_loc, where;
696abb30
JD
126 int count;
127 gfc_instring instring;
8fc541d3 128 gfc_char_t c, quote;
f9b9fb82
JD
129
130 old_loc = gfc_current_locus;
131 count = 0;
696abb30 132 instring = NONSTRING;
f9b9fb82
JD
133 quote = ' ';
134
135 for (;;)
136 {
137 c = gfc_next_char_literal (instring);
138 if (c == '\n')
139 break;
140 if (quote == ' ' && ((c == '\'') || (c == '"')))
141 {
8fc541d3 142 quote = c;
696abb30 143 instring = INSTRING_WARN;
f9b9fb82
JD
144 continue;
145 }
146 if (quote != ' ' && c == quote)
147 {
148 quote = ' ';
696abb30 149 instring = NONSTRING;
f9b9fb82
JD
150 continue;
151 }
152
153 if (c == '(' && quote == ' ')
154 {
155 count++;
156 where = gfc_current_locus;
157 }
158 if (c == ')' && quote == ' ')
159 {
160 count--;
161 where = gfc_current_locus;
162 }
163 }
164
165 gfc_current_locus = old_loc;
166
167 if (count > 0)
168 {
a4d9b221 169 gfc_error ("Missing %<)%> in statement at or before %L", &where);
f9b9fb82
JD
170 return MATCH_ERROR;
171 }
172 if (count < 0)
173 {
a4d9b221 174 gfc_error ("Missing %<(%> in statement at or before %L", &where);
f9b9fb82
JD
175 return MATCH_ERROR;
176 }
177
178 return MATCH_YES;
179}
180
181
a88a266c
SK
182/* See if the next character is a special character that has
183 escaped by a \ via the -fbackslash option. */
184
185match
8fc541d3 186gfc_match_special_char (gfc_char_t *res)
a88a266c 187{
8fc541d3
FXC
188 int len, i;
189 gfc_char_t c, n;
a88a266c
SK
190 match m;
191
192 m = MATCH_YES;
193
696abb30 194 switch ((c = gfc_next_char_literal (INSTRING_WARN)))
a88a266c
SK
195 {
196 case 'a':
8fc541d3 197 *res = '\a';
a88a266c
SK
198 break;
199 case 'b':
8fc541d3 200 *res = '\b';
a88a266c
SK
201 break;
202 case 't':
8fc541d3 203 *res = '\t';
a88a266c
SK
204 break;
205 case 'f':
8fc541d3 206 *res = '\f';
a88a266c
SK
207 break;
208 case 'n':
8fc541d3 209 *res = '\n';
a88a266c
SK
210 break;
211 case 'r':
8fc541d3 212 *res = '\r';
a88a266c
SK
213 break;
214 case 'v':
8fc541d3 215 *res = '\v';
a88a266c
SK
216 break;
217 case '\\':
8fc541d3 218 *res = '\\';
a88a266c
SK
219 break;
220 case '0':
8fc541d3
FXC
221 *res = '\0';
222 break;
223
224 case 'x':
225 case 'u':
226 case 'U':
227 /* Hexadecimal form of wide characters. */
228 len = (c == 'x' ? 2 : (c == 'u' ? 4 : 8));
229 n = 0;
230 for (i = 0; i < len; i++)
231 {
232 char buf[2] = { '\0', '\0' };
233
696abb30 234 c = gfc_next_char_literal (INSTRING_WARN);
8fc541d3
FXC
235 if (!gfc_wide_fits_in_byte (c)
236 || !gfc_check_digit ((unsigned char) c, 16))
237 return MATCH_NO;
238
239 buf[0] = (unsigned char) c;
240 n = n << 4;
241 n += strtol (buf, NULL, 16);
242 }
243 *res = n;
a88a266c 244 break;
8fc541d3 245
a88a266c
SK
246 default:
247 /* Unknown backslash codes are simply not expanded. */
248 m = MATCH_NO;
249 break;
250 }
251
252 return m;
253}
254
255
6de9cd9a
DN
256/* In free form, match at least one space. Always matches in fixed
257 form. */
258
259match
260gfc_match_space (void)
261{
262 locus old_loc;
8fc541d3 263 char c;
6de9cd9a 264
d4fa05b9 265 if (gfc_current_form == FORM_FIXED)
6de9cd9a
DN
266 return MATCH_YES;
267
63645982 268 old_loc = gfc_current_locus;
6de9cd9a 269
8fc541d3 270 c = gfc_next_ascii_char ();
6de9cd9a
DN
271 if (!gfc_is_whitespace (c))
272 {
63645982 273 gfc_current_locus = old_loc;
6de9cd9a
DN
274 return MATCH_NO;
275 }
276
277 gfc_gobble_whitespace ();
278
279 return MATCH_YES;
280}
281
282
283/* Match an end of statement. End of statement is optional
284 whitespace, followed by a ';' or '\n' or comment '!'. If a
285 semicolon is found, we continue to eat whitespace and semicolons. */
286
287match
288gfc_match_eos (void)
289{
290 locus old_loc;
8fc541d3
FXC
291 int flag;
292 char c;
6de9cd9a
DN
293
294 flag = 0;
295
296 for (;;)
297 {
63645982 298 old_loc = gfc_current_locus;
6de9cd9a
DN
299 gfc_gobble_whitespace ();
300
8fc541d3 301 c = gfc_next_ascii_char ();
6de9cd9a
DN
302 switch (c)
303 {
304 case '!':
305 do
306 {
8fc541d3 307 c = gfc_next_ascii_char ();
6de9cd9a
DN
308 }
309 while (c != '\n');
310
66e4ab31 311 /* Fall through. */
6de9cd9a
DN
312
313 case '\n':
314 return MATCH_YES;
315
316 case ';':
317 flag = 1;
318 continue;
319 }
320
321 break;
322 }
323
63645982 324 gfc_current_locus = old_loc;
6de9cd9a
DN
325 return (flag) ? MATCH_YES : MATCH_NO;
326}
327
328
329/* Match a literal integer on the input, setting the value on
330 MATCH_YES. Literal ints occur in kind-parameters as well as
5cf54585
TS
331 old-style character length specifications. If cnt is non-NULL it
332 will be set to the number of digits. */
6de9cd9a
DN
333
334match
8a8f7eca 335gfc_match_small_literal_int (int *value, int *cnt)
6de9cd9a
DN
336{
337 locus old_loc;
338 char c;
8a8f7eca 339 int i, j;
6de9cd9a 340
63645982 341 old_loc = gfc_current_locus;
6de9cd9a 342
8fc541d3 343 *value = -1;
6de9cd9a 344 gfc_gobble_whitespace ();
8fc541d3 345 c = gfc_next_ascii_char ();
5cf54585
TS
346 if (cnt)
347 *cnt = 0;
6de9cd9a
DN
348
349 if (!ISDIGIT (c))
350 {
63645982 351 gfc_current_locus = old_loc;
6de9cd9a
DN
352 return MATCH_NO;
353 }
354
355 i = c - '0';
8a8f7eca 356 j = 1;
6de9cd9a
DN
357
358 for (;;)
359 {
63645982 360 old_loc = gfc_current_locus;
8fc541d3 361 c = gfc_next_ascii_char ();
6de9cd9a
DN
362
363 if (!ISDIGIT (c))
364 break;
365
366 i = 10 * i + c - '0';
8a8f7eca 367 j++;
6de9cd9a
DN
368
369 if (i > 99999999)
370 {
371 gfc_error ("Integer too large at %C");
372 return MATCH_ERROR;
373 }
374 }
375
63645982 376 gfc_current_locus = old_loc;
6de9cd9a
DN
377
378 *value = i;
5cf54585
TS
379 if (cnt)
380 *cnt = j;
6de9cd9a
DN
381 return MATCH_YES;
382}
383
384
385/* Match a small, constant integer expression, like in a kind
386 statement. On MATCH_YES, 'value' is set. */
387
388match
389gfc_match_small_int (int *value)
390{
391 gfc_expr *expr;
392 const char *p;
393 match m;
394 int i;
395
396 m = gfc_match_expr (&expr);
397 if (m != MATCH_YES)
398 return m;
399
400 p = gfc_extract_int (expr, &i);
401 gfc_free_expr (expr);
402
403 if (p != NULL)
404 {
405 gfc_error (p);
406 m = MATCH_ERROR;
407 }
408
409 *value = i;
410 return m;
411}
412
413
a8b3b0b6
CR
414/* This function is the same as the gfc_match_small_int, except that
415 we're keeping the pointer to the expr. This function could just be
416 removed and the previously mentioned one modified, though all calls
417 to it would have to be modified then (and there were a number of
418 them). Return MATCH_ERROR if fail to extract the int; otherwise,
419 return the result of gfc_match_expr(). The expr (if any) that was
420 matched is returned in the parameter expr. */
421
422match
423gfc_match_small_int_expr (int *value, gfc_expr **expr)
424{
425 const char *p;
426 match m;
427 int i;
428
429 m = gfc_match_expr (expr);
430 if (m != MATCH_YES)
431 return m;
432
433 p = gfc_extract_int (*expr, &i);
434
435 if (p != NULL)
436 {
437 gfc_error (p);
438 m = MATCH_ERROR;
439 }
440
441 *value = i;
442 return m;
443}
444
445
6de9cd9a
DN
446/* Matches a statement label. Uses gfc_match_small_literal_int() to
447 do most of the work. */
448
449match
b251af97 450gfc_match_st_label (gfc_st_label **label)
6de9cd9a
DN
451{
452 locus old_loc;
453 match m;
8a8f7eca 454 int i, cnt;
6de9cd9a 455
63645982 456 old_loc = gfc_current_locus;
6de9cd9a 457
8a8f7eca 458 m = gfc_match_small_literal_int (&i, &cnt);
6de9cd9a
DN
459 if (m != MATCH_YES)
460 return m;
461
8a8f7eca 462 if (cnt > 5)
6de9cd9a 463 {
8a8f7eca
SK
464 gfc_error ("Too many digits in statement label at %C");
465 goto cleanup;
6de9cd9a
DN
466 }
467
a34a91f0 468 if (i == 0)
8a8f7eca
SK
469 {
470 gfc_error ("Statement label at %C is zero");
471 goto cleanup;
472 }
473
474 *label = gfc_get_st_label (i);
475 return MATCH_YES;
476
477cleanup:
478
63645982 479 gfc_current_locus = old_loc;
6de9cd9a
DN
480 return MATCH_ERROR;
481}
482
483
484/* Match and validate a label associated with a named IF, DO or SELECT
485 statement. If the symbol does not have the label attribute, we add
486 it. We also make sure the symbol does not refer to another
487 (active) block. A matched label is pointed to by gfc_new_block. */
488
489match
490gfc_match_label (void)
491{
492 char name[GFC_MAX_SYMBOL_LEN + 1];
6de9cd9a
DN
493 match m;
494
495 gfc_new_block = NULL;
496
497 m = gfc_match (" %n :", name);
498 if (m != MATCH_YES)
499 return m;
500
501 if (gfc_get_symbol (name, NULL, &gfc_new_block))
502 {
a4d9b221 503 gfc_error ("Label name %qs at %C is ambiguous", name);
6de9cd9a
DN
504 return MATCH_ERROR;
505 }
506
cb1d4dce
SK
507 if (gfc_new_block->attr.flavor == FL_LABEL)
508 {
a4d9b221 509 gfc_error ("Duplicate construct label %qs at %C", name);
cb1d4dce
SK
510 return MATCH_ERROR;
511 }
6de9cd9a 512
524af0d6
JB
513 if (!gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
514 gfc_new_block->name, NULL))
cb1d4dce 515 return MATCH_ERROR;
6de9cd9a
DN
516
517 return MATCH_YES;
518}
519
520
6de9cd9a 521/* See if the current input looks like a name of some sort. Modifies
090021e9
BM
522 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
523 Note that options.c restricts max_identifier_length to not more
524 than GFC_MAX_SYMBOL_LEN. */
6de9cd9a
DN
525
526match
527gfc_match_name (char *buffer)
528{
529 locus old_loc;
8fc541d3
FXC
530 int i;
531 char c;
6de9cd9a 532
63645982 533 old_loc = gfc_current_locus;
6de9cd9a
DN
534 gfc_gobble_whitespace ();
535
8fc541d3 536 c = gfc_next_ascii_char ();
c61819ff 537 if (!(ISALPHA (c) || (c == '_' && flag_allow_leading_underscore)))
6de9cd9a 538 {
83eb71f4
SK
539 /* Special cases for unary minus and plus, which allows for a sensible
540 error message for code of the form 'c = exp(-a*b) )' where an
541 extra ')' appears at the end of statement. */
542 if (!gfc_error_flag_test () && c != '(' && c != '-' && c != '+')
b251af97 543 gfc_error ("Invalid character in name at %C");
63645982 544 gfc_current_locus = old_loc;
6de9cd9a
DN
545 return MATCH_NO;
546 }
547
548 i = 0;
549
550 do
551 {
552 buffer[i++] = c;
553
554 if (i > gfc_option.max_identifier_length)
555 {
556 gfc_error ("Name at %C is too long");
557 return MATCH_ERROR;
558 }
559
63645982 560 old_loc = gfc_current_locus;
8fc541d3 561 c = gfc_next_ascii_char ();
6de9cd9a 562 }
c61819ff 563 while (ISALNUM (c) || c == '_' || (flag_dollar_ok && c == '$'));
6de9cd9a 564
c61819ff 565 if (c == '$' && !flag_dollar_ok)
89a5afda 566 {
29e0597e
TB
567 gfc_fatal_error ("Invalid character %<$%> at %L. Use %<-fdollar-ok%> to "
568 "allow it as an extension", &old_loc);
89a5afda
TB
569 return MATCH_ERROR;
570 }
571
6de9cd9a 572 buffer[i] = '\0';
63645982 573 gfc_current_locus = old_loc;
6de9cd9a
DN
574
575 return MATCH_YES;
576}
577
578
579/* Match a symbol on the input. Modifies the pointer to the symbol
580 pointer if successful. */
581
582match
b251af97 583gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
6de9cd9a
DN
584{
585 char buffer[GFC_MAX_SYMBOL_LEN + 1];
586 match m;
587
588 m = gfc_match_name (buffer);
589 if (m != MATCH_YES)
590 return m;
591
592 if (host_assoc)
593 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
66e4ab31 594 ? MATCH_ERROR : MATCH_YES;
6de9cd9a 595
08a6b8e0 596 if (gfc_get_sym_tree (buffer, NULL, matched_symbol, false))
6de9cd9a
DN
597 return MATCH_ERROR;
598
599 return MATCH_YES;
600}
601
602
603match
b251af97 604gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
6de9cd9a
DN
605{
606 gfc_symtree *st;
607 match m;
608
609 m = gfc_match_sym_tree (&st, host_assoc);
610
611 if (m == MATCH_YES)
612 {
613 if (st)
b251af97 614 *matched_symbol = st->n.sym;
6de9cd9a 615 else
b251af97 616 *matched_symbol = NULL;
6de9cd9a 617 }
32cafd73
MH
618 else
619 *matched_symbol = NULL;
6de9cd9a
DN
620 return m;
621}
622
b251af97 623
8b704316
PT
624/* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
625 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
6de9cd9a
DN
626 in matchexp.c. */
627
628match
b251af97 629gfc_match_intrinsic_op (gfc_intrinsic_op *result)
6de9cd9a 630{
f4d8e0d1 631 locus orig_loc = gfc_current_locus;
8fc541d3 632 char ch;
6de9cd9a 633
f4d8e0d1 634 gfc_gobble_whitespace ();
8fc541d3 635 ch = gfc_next_ascii_char ();
f4d8e0d1
RS
636 switch (ch)
637 {
638 case '+':
639 /* Matched "+". */
640 *result = INTRINSIC_PLUS;
641 return MATCH_YES;
6de9cd9a 642
f4d8e0d1
RS
643 case '-':
644 /* Matched "-". */
645 *result = INTRINSIC_MINUS;
646 return MATCH_YES;
6de9cd9a 647
f4d8e0d1 648 case '=':
8fc541d3 649 if (gfc_next_ascii_char () == '=')
f4d8e0d1
RS
650 {
651 /* Matched "==". */
652 *result = INTRINSIC_EQ;
653 return MATCH_YES;
654 }
655 break;
656
657 case '<':
8fc541d3 658 if (gfc_peek_ascii_char () == '=')
f4d8e0d1
RS
659 {
660 /* Matched "<=". */
8fc541d3 661 gfc_next_ascii_char ();
f4d8e0d1
RS
662 *result = INTRINSIC_LE;
663 return MATCH_YES;
664 }
665 /* Matched "<". */
666 *result = INTRINSIC_LT;
667 return MATCH_YES;
668
669 case '>':
8fc541d3 670 if (gfc_peek_ascii_char () == '=')
f4d8e0d1
RS
671 {
672 /* Matched ">=". */
8fc541d3 673 gfc_next_ascii_char ();
f4d8e0d1
RS
674 *result = INTRINSIC_GE;
675 return MATCH_YES;
676 }
677 /* Matched ">". */
678 *result = INTRINSIC_GT;
679 return MATCH_YES;
680
681 case '*':
8fc541d3 682 if (gfc_peek_ascii_char () == '*')
f4d8e0d1
RS
683 {
684 /* Matched "**". */
8fc541d3 685 gfc_next_ascii_char ();
f4d8e0d1
RS
686 *result = INTRINSIC_POWER;
687 return MATCH_YES;
688 }
689 /* Matched "*". */
690 *result = INTRINSIC_TIMES;
691 return MATCH_YES;
692
693 case '/':
8fc541d3 694 ch = gfc_peek_ascii_char ();
f4d8e0d1
RS
695 if (ch == '=')
696 {
697 /* Matched "/=". */
8fc541d3 698 gfc_next_ascii_char ();
f4d8e0d1
RS
699 *result = INTRINSIC_NE;
700 return MATCH_YES;
701 }
702 else if (ch == '/')
703 {
704 /* Matched "//". */
8fc541d3 705 gfc_next_ascii_char ();
f4d8e0d1
RS
706 *result = INTRINSIC_CONCAT;
707 return MATCH_YES;
708 }
709 /* Matched "/". */
710 *result = INTRINSIC_DIVIDE;
711 return MATCH_YES;
712
713 case '.':
8fc541d3 714 ch = gfc_next_ascii_char ();
f4d8e0d1
RS
715 switch (ch)
716 {
717 case 'a':
8fc541d3
FXC
718 if (gfc_next_ascii_char () == 'n'
719 && gfc_next_ascii_char () == 'd'
720 && gfc_next_ascii_char () == '.')
f4d8e0d1
RS
721 {
722 /* Matched ".and.". */
723 *result = INTRINSIC_AND;
724 return MATCH_YES;
725 }
726 break;
727
728 case 'e':
8fc541d3 729 if (gfc_next_ascii_char () == 'q')
f4d8e0d1 730 {
8fc541d3 731 ch = gfc_next_ascii_char ();
f4d8e0d1
RS
732 if (ch == '.')
733 {
734 /* Matched ".eq.". */
735 *result = INTRINSIC_EQ_OS;
736 return MATCH_YES;
737 }
738 else if (ch == 'v')
739 {
8fc541d3 740 if (gfc_next_ascii_char () == '.')
f4d8e0d1
RS
741 {
742 /* Matched ".eqv.". */
743 *result = INTRINSIC_EQV;
744 return MATCH_YES;
745 }
746 }
747 }
748 break;
749
750 case 'g':
8fc541d3 751 ch = gfc_next_ascii_char ();
f4d8e0d1
RS
752 if (ch == 'e')
753 {
8fc541d3 754 if (gfc_next_ascii_char () == '.')
f4d8e0d1
RS
755 {
756 /* Matched ".ge.". */
757 *result = INTRINSIC_GE_OS;
758 return MATCH_YES;
759 }
760 }
761 else if (ch == 't')
762 {
8fc541d3 763 if (gfc_next_ascii_char () == '.')
f4d8e0d1
RS
764 {
765 /* Matched ".gt.". */
766 *result = INTRINSIC_GT_OS;
767 return MATCH_YES;
768 }
769 }
770 break;
771
772 case 'l':
8fc541d3 773 ch = gfc_next_ascii_char ();
f4d8e0d1
RS
774 if (ch == 'e')
775 {
8fc541d3 776 if (gfc_next_ascii_char () == '.')
f4d8e0d1
RS
777 {
778 /* Matched ".le.". */
779 *result = INTRINSIC_LE_OS;
780 return MATCH_YES;
781 }
782 }
783 else if (ch == 't')
784 {
8fc541d3 785 if (gfc_next_ascii_char () == '.')
f4d8e0d1
RS
786 {
787 /* Matched ".lt.". */
788 *result = INTRINSIC_LT_OS;
789 return MATCH_YES;
790 }
791 }
792 break;
793
794 case 'n':
8fc541d3 795 ch = gfc_next_ascii_char ();
f4d8e0d1
RS
796 if (ch == 'e')
797 {
8fc541d3 798 ch = gfc_next_ascii_char ();
f4d8e0d1
RS
799 if (ch == '.')
800 {
801 /* Matched ".ne.". */
802 *result = INTRINSIC_NE_OS;
803 return MATCH_YES;
804 }
805 else if (ch == 'q')
806 {
8fc541d3
FXC
807 if (gfc_next_ascii_char () == 'v'
808 && gfc_next_ascii_char () == '.')
f4d8e0d1
RS
809 {
810 /* Matched ".neqv.". */
811 *result = INTRINSIC_NEQV;
812 return MATCH_YES;
813 }
814 }
815 }
816 else if (ch == 'o')
817 {
8fc541d3
FXC
818 if (gfc_next_ascii_char () == 't'
819 && gfc_next_ascii_char () == '.')
f4d8e0d1
RS
820 {
821 /* Matched ".not.". */
822 *result = INTRINSIC_NOT;
823 return MATCH_YES;
824 }
825 }
826 break;
827
828 case 'o':
8fc541d3
FXC
829 if (gfc_next_ascii_char () == 'r'
830 && gfc_next_ascii_char () == '.')
f4d8e0d1
RS
831 {
832 /* Matched ".or.". */
833 *result = INTRINSIC_OR;
834 return MATCH_YES;
835 }
836 break;
837
838 default:
839 break;
840 }
841 break;
842
843 default:
844 break;
845 }
846
847 gfc_current_locus = orig_loc;
848 return MATCH_NO;
6de9cd9a
DN
849}
850
851
852/* Match a loop control phrase:
853
854 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
855
856 If the final integer expression is not present, a constant unity
857 expression is returned. We don't return MATCH_ERROR until after
858 the equals sign is seen. */
859
860match
b251af97 861gfc_match_iterator (gfc_iterator *iter, int init_flag)
6de9cd9a
DN
862{
863 char name[GFC_MAX_SYMBOL_LEN + 1];
864 gfc_expr *var, *e1, *e2, *e3;
865 locus start;
866 match m;
867
d3a9eea2
TB
868 e1 = e2 = e3 = NULL;
869
b251af97 870 /* Match the start of an iterator without affecting the symbol table. */
6de9cd9a 871
63645982 872 start = gfc_current_locus;
6de9cd9a 873 m = gfc_match (" %n =", name);
63645982 874 gfc_current_locus = start;
6de9cd9a
DN
875
876 if (m != MATCH_YES)
877 return MATCH_NO;
878
879 m = gfc_match_variable (&var, 0);
880 if (m != MATCH_YES)
881 return MATCH_NO;
882
d3a9eea2
TB
883 /* F2008, C617 & C565. */
884 if (var->symtree->n.sym->attr.codimension)
885 {
886 gfc_error ("Loop variable at %C cannot be a coarray");
887 goto cleanup;
888 }
6de9cd9a
DN
889
890 if (var->ref != NULL)
891 {
892 gfc_error ("Loop variable at %C cannot be a sub-component");
893 goto cleanup;
894 }
895
d3a9eea2
TB
896 gfc_match_char ('=');
897
9a3db5a3
PT
898 var->symtree->n.sym->attr.implied_index = 1;
899
6de9cd9a
DN
900 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
901 if (m == MATCH_NO)
902 goto syntax;
903 if (m == MATCH_ERROR)
904 goto cleanup;
905
906 if (gfc_match_char (',') != MATCH_YES)
907 goto syntax;
908
909 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
910 if (m == MATCH_NO)
911 goto syntax;
912 if (m == MATCH_ERROR)
913 goto cleanup;
914
915 if (gfc_match_char (',') != MATCH_YES)
916 {
b7e75771 917 e3 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
6de9cd9a
DN
918 goto done;
919 }
920
921 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
922 if (m == MATCH_ERROR)
923 goto cleanup;
924 if (m == MATCH_NO)
925 {
926 gfc_error ("Expected a step value in iterator at %C");
927 goto cleanup;
928 }
929
930done:
931 iter->var = var;
932 iter->start = e1;
933 iter->end = e2;
934 iter->step = e3;
935 return MATCH_YES;
936
937syntax:
938 gfc_error ("Syntax error in iterator at %C");
939
940cleanup:
941 gfc_free_expr (e1);
942 gfc_free_expr (e2);
943 gfc_free_expr (e3);
944
945 return MATCH_ERROR;
946}
947
948
949/* Tries to match the next non-whitespace character on the input.
950 This subroutine does not return MATCH_ERROR. */
951
952match
953gfc_match_char (char c)
954{
955 locus where;
956
63645982 957 where = gfc_current_locus;
6de9cd9a
DN
958 gfc_gobble_whitespace ();
959
8fc541d3 960 if (gfc_next_ascii_char () == c)
6de9cd9a
DN
961 return MATCH_YES;
962
63645982 963 gfc_current_locus = where;
6de9cd9a
DN
964 return MATCH_NO;
965}
966
967
968/* General purpose matching subroutine. The target string is a
969 scanf-like format string in which spaces correspond to arbitrary
970 whitespace (including no whitespace), characters correspond to
971 themselves. The %-codes are:
972
973 %% Literal percent sign
974 %e Expression, pointer to a pointer is set
975 %s Symbol, pointer to the symbol is set
976 %n Name, character buffer is set to name
977 %t Matches end of statement.
978 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
979 %l Matches a statement label
980 %v Matches a variable expression (an lvalue)
981 % Matches a required space (in free form) and optional spaces. */
982
983match
984gfc_match (const char *target, ...)
985{
986 gfc_st_label **label;
987 int matches, *ip;
988 locus old_loc;
989 va_list argp;
990 char c, *np;
991 match m, n;
992 void **vp;
993 const char *p;
994
63645982 995 old_loc = gfc_current_locus;
6de9cd9a
DN
996 va_start (argp, target);
997 m = MATCH_NO;
998 matches = 0;
999 p = target;
1000
1001loop:
1002 c = *p++;
1003 switch (c)
1004 {
1005 case ' ':
1006 gfc_gobble_whitespace ();
1007 goto loop;
1008 case '\0':
1009 m = MATCH_YES;
1010 break;
1011
1012 case '%':
1013 c = *p++;
1014 switch (c)
1015 {
1016 case 'e':
1017 vp = va_arg (argp, void **);
1018 n = gfc_match_expr ((gfc_expr **) vp);
1019 if (n != MATCH_YES)
1020 {
1021 m = n;
1022 goto not_yes;
1023 }
1024
1025 matches++;
1026 goto loop;
1027
1028 case 'v':
1029 vp = va_arg (argp, void **);
1030 n = gfc_match_variable ((gfc_expr **) vp, 0);
1031 if (n != MATCH_YES)
1032 {
1033 m = n;
1034 goto not_yes;
1035 }
1036
1037 matches++;
1038 goto loop;
1039
1040 case 's':
1041 vp = va_arg (argp, void **);
1042 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1043 if (n != MATCH_YES)
1044 {
1045 m = n;
1046 goto not_yes;
1047 }
1048
1049 matches++;
1050 goto loop;
1051
1052 case 'n':
1053 np = va_arg (argp, char *);
1054 n = gfc_match_name (np);
1055 if (n != MATCH_YES)
1056 {
1057 m = n;
1058 goto not_yes;
1059 }
1060
1061 matches++;
1062 goto loop;
1063
1064 case 'l':
1065 label = va_arg (argp, gfc_st_label **);
a34a91f0 1066 n = gfc_match_st_label (label);
6de9cd9a
DN
1067 if (n != MATCH_YES)
1068 {
1069 m = n;
1070 goto not_yes;
1071 }
1072
1073 matches++;
1074 goto loop;
1075
1076 case 'o':
1077 ip = va_arg (argp, int *);
1078 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1079 if (n != MATCH_YES)
1080 {
1081 m = n;
1082 goto not_yes;
1083 }
1084
1085 matches++;
1086 goto loop;
1087
1088 case 't':
1089 if (gfc_match_eos () != MATCH_YES)
1090 {
1091 m = MATCH_NO;
1092 goto not_yes;
1093 }
1094 goto loop;
1095
1096 case ' ':
1097 if (gfc_match_space () == MATCH_YES)
1098 goto loop;
1099 m = MATCH_NO;
1100 goto not_yes;
1101
1102 case '%':
66e4ab31 1103 break; /* Fall through to character matcher. */
6de9cd9a
DN
1104
1105 default:
1106 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1107 }
1108
1109 default:
befdf741
DK
1110
1111 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1112 expect an upper case character here! */
1113 gcc_assert (TOLOWER (c) == c);
1114
8fc541d3 1115 if (c == gfc_next_ascii_char ())
6de9cd9a
DN
1116 goto loop;
1117 break;
1118 }
1119
1120not_yes:
1121 va_end (argp);
1122
1123 if (m != MATCH_YES)
1124 {
1125 /* Clean up after a failed match. */
63645982 1126 gfc_current_locus = old_loc;
6de9cd9a
DN
1127 va_start (argp, target);
1128
1129 p = target;
1130 for (; matches > 0; matches--)
1131 {
1132 while (*p++ != '%');
1133
1134 switch (*p++)
1135 {
1136 case '%':
1137 matches++;
66e4ab31 1138 break; /* Skip. */
6de9cd9a 1139
6de9cd9a
DN
1140 /* Matches that don't have to be undone */
1141 case 'o':
1142 case 'l':
1143 case 'n':
1144 case 's':
b251af97 1145 (void) va_arg (argp, void **);
6de9cd9a
DN
1146 break;
1147
1148 case 'e':
6de9cd9a 1149 case 'v':
6de9cd9a 1150 vp = va_arg (argp, void **);
ece3f663 1151 gfc_free_expr ((struct gfc_expr *)*vp);
6de9cd9a
DN
1152 *vp = NULL;
1153 break;
1154 }
1155 }
1156
1157 va_end (argp);
1158 }
1159
1160 return m;
1161}
1162
1163
1164/*********************** Statement level matching **********************/
1165
1166/* Matches the start of a program unit, which is the program keyword
e08b5a75 1167 followed by an obligatory symbol. */
6de9cd9a
DN
1168
1169match
1170gfc_match_program (void)
1171{
1172 gfc_symbol *sym;
1173 match m;
1174
6de9cd9a
DN
1175 m = gfc_match ("% %s%t", &sym);
1176
1177 if (m == MATCH_NO)
1178 {
1179 gfc_error ("Invalid form of PROGRAM statement at %C");
1180 m = MATCH_ERROR;
1181 }
1182
1183 if (m == MATCH_ERROR)
1184 return m;
1185
524af0d6 1186 if (!gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL))
6de9cd9a
DN
1187 return MATCH_ERROR;
1188
1189 gfc_new_block = sym;
1190
1191 return MATCH_YES;
1192}
1193
1194
1195/* Match a simple assignment statement. */
1196
1197match
1198gfc_match_assignment (void)
1199{
1200 gfc_expr *lvalue, *rvalue;
1201 locus old_loc;
1202 match m;
1203
63645982 1204 old_loc = gfc_current_locus;
6de9cd9a 1205
5056a350 1206 lvalue = NULL;
6de9cd9a
DN
1207 m = gfc_match (" %v =", &lvalue);
1208 if (m != MATCH_YES)
c9583ed2 1209 {
5056a350
SK
1210 gfc_current_locus = old_loc;
1211 gfc_free_expr (lvalue);
1212 return MATCH_NO;
c9583ed2
TS
1213 }
1214
5056a350 1215 rvalue = NULL;
6de9cd9a
DN
1216 m = gfc_match (" %e%t", &rvalue);
1217 if (m != MATCH_YES)
5056a350
SK
1218 {
1219 gfc_current_locus = old_loc;
1220 gfc_free_expr (lvalue);
1221 gfc_free_expr (rvalue);
1222 return m;
1223 }
6de9cd9a
DN
1224
1225 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1226
1227 new_st.op = EXEC_ASSIGN;
a513927a 1228 new_st.expr1 = lvalue;
6de9cd9a
DN
1229 new_st.expr2 = rvalue;
1230
c9583ed2
TS
1231 gfc_check_do_variable (lvalue->symtree);
1232
6de9cd9a 1233 return MATCH_YES;
6de9cd9a
DN
1234}
1235
1236
1237/* Match a pointer assignment statement. */
1238
1239match
1240gfc_match_pointer_assignment (void)
1241{
1242 gfc_expr *lvalue, *rvalue;
1243 locus old_loc;
1244 match m;
1245
63645982 1246 old_loc = gfc_current_locus;
6de9cd9a
DN
1247
1248 lvalue = rvalue = NULL;
837c4b78 1249 gfc_matching_ptr_assignment = 0;
8fb74da4 1250 gfc_matching_procptr_assignment = 0;
6de9cd9a
DN
1251
1252 m = gfc_match (" %v =>", &lvalue);
1253 if (m != MATCH_YES)
1254 {
1255 m = MATCH_NO;
1256 goto cleanup;
1257 }
1258
713485cc 1259 if (lvalue->symtree->n.sym->attr.proc_pointer
2a573572 1260 || gfc_is_proc_ptr_comp (lvalue))
8fb74da4 1261 gfc_matching_procptr_assignment = 1;
837c4b78
JW
1262 else
1263 gfc_matching_ptr_assignment = 1;
8fb74da4 1264
6de9cd9a 1265 m = gfc_match (" %e%t", &rvalue);
837c4b78 1266 gfc_matching_ptr_assignment = 0;
8fb74da4 1267 gfc_matching_procptr_assignment = 0;
6de9cd9a
DN
1268 if (m != MATCH_YES)
1269 goto cleanup;
1270
1271 new_st.op = EXEC_POINTER_ASSIGN;
a513927a 1272 new_st.expr1 = lvalue;
6de9cd9a
DN
1273 new_st.expr2 = rvalue;
1274
1275 return MATCH_YES;
1276
1277cleanup:
63645982 1278 gfc_current_locus = old_loc;
6de9cd9a
DN
1279 gfc_free_expr (lvalue);
1280 gfc_free_expr (rvalue);
1281 return m;
1282}
1283
1284
43e1c5f7 1285/* We try to match an easy arithmetic IF statement. This only happens
835d64ab
FXC
1286 when just after having encountered a simple IF statement. This code
1287 is really duplicate with parts of the gfc_match_if code, but this is
1288 *much* easier. */
b251af97 1289
f55e72ce 1290static match
835d64ab 1291match_arithmetic_if (void)
43e1c5f7
FXC
1292{
1293 gfc_st_label *l1, *l2, *l3;
1294 gfc_expr *expr;
1295 match m;
1296
1297 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1298 if (m != MATCH_YES)
1299 return m;
1300
524af0d6
JB
1301 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1302 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1303 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
43e1c5f7
FXC
1304 {
1305 gfc_free_expr (expr);
1306 return MATCH_ERROR;
1307 }
1308
524af0d6 1309 if (!gfc_notify_std (GFC_STD_F95_OBS, "Arithmetic IF statement at %C"))
51c3f0f6
FXC
1310 return MATCH_ERROR;
1311
43e1c5f7 1312 new_st.op = EXEC_ARITHMETIC_IF;
a513927a 1313 new_st.expr1 = expr;
79bd1948 1314 new_st.label1 = l1;
43e1c5f7
FXC
1315 new_st.label2 = l2;
1316 new_st.label3 = l3;
1317
1318 return MATCH_YES;
1319}
1320
1321
6de9cd9a
DN
1322/* The IF statement is a bit of a pain. First of all, there are three
1323 forms of it, the simple IF, the IF that starts a block and the
1324 arithmetic IF.
1325
1326 There is a problem with the simple IF and that is the fact that we
1327 only have a single level of undo information on symbols. What this
1328 means is for a simple IF, we must re-match the whole IF statement
1329 multiple times in order to guarantee that the symbol table ends up
1330 in the proper state. */
1331
c874ae73
TS
1332static match match_simple_forall (void);
1333static match match_simple_where (void);
1334
6de9cd9a 1335match
b251af97 1336gfc_match_if (gfc_statement *if_type)
6de9cd9a
DN
1337{
1338 gfc_expr *expr;
1339 gfc_st_label *l1, *l2, *l3;
f9b9fb82 1340 locus old_loc, old_loc2;
6de9cd9a
DN
1341 gfc_code *p;
1342 match m, n;
1343
1344 n = gfc_match_label ();
1345 if (n == MATCH_ERROR)
1346 return n;
1347
63645982 1348 old_loc = gfc_current_locus;
6de9cd9a
DN
1349
1350 m = gfc_match (" if ( %e", &expr);
1351 if (m != MATCH_YES)
1352 return m;
1353
f9b9fb82
JD
1354 old_loc2 = gfc_current_locus;
1355 gfc_current_locus = old_loc;
8b704316 1356
f9b9fb82
JD
1357 if (gfc_match_parens () == MATCH_ERROR)
1358 return MATCH_ERROR;
1359
1360 gfc_current_locus = old_loc2;
1361
6de9cd9a
DN
1362 if (gfc_match_char (')') != MATCH_YES)
1363 {
1364 gfc_error ("Syntax error in IF-expression at %C");
1365 gfc_free_expr (expr);
1366 return MATCH_ERROR;
1367 }
1368
1369 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1370
1371 if (m == MATCH_YES)
1372 {
1373 if (n == MATCH_YES)
1374 {
b251af97
SK
1375 gfc_error ("Block label not appropriate for arithmetic IF "
1376 "statement at %C");
6de9cd9a
DN
1377 gfc_free_expr (expr);
1378 return MATCH_ERROR;
1379 }
1380
524af0d6
JB
1381 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1382 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1383 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
6de9cd9a 1384 {
6de9cd9a
DN
1385 gfc_free_expr (expr);
1386 return MATCH_ERROR;
1387 }
8b704316 1388
524af0d6 1389 if (!gfc_notify_std (GFC_STD_F95_OBS, "Arithmetic IF statement at %C"))
b251af97 1390 return MATCH_ERROR;
6de9cd9a
DN
1391
1392 new_st.op = EXEC_ARITHMETIC_IF;
a513927a 1393 new_st.expr1 = expr;
79bd1948 1394 new_st.label1 = l1;
6de9cd9a
DN
1395 new_st.label2 = l2;
1396 new_st.label3 = l3;
1397
1398 *if_type = ST_ARITHMETIC_IF;
1399 return MATCH_YES;
1400 }
1401
172b8799 1402 if (gfc_match (" then%t") == MATCH_YES)
6de9cd9a
DN
1403 {
1404 new_st.op = EXEC_IF;
a513927a 1405 new_st.expr1 = expr;
6de9cd9a
DN
1406 *if_type = ST_IF_BLOCK;
1407 return MATCH_YES;
1408 }
1409
1410 if (n == MATCH_YES)
1411 {
f9b9fb82 1412 gfc_error ("Block label is not appropriate for IF statement at %C");
6de9cd9a
DN
1413 gfc_free_expr (expr);
1414 return MATCH_ERROR;
1415 }
1416
1417 /* At this point the only thing left is a simple IF statement. At
1418 this point, n has to be MATCH_NO, so we don't have to worry about
1419 re-matching a block label. From what we've got so far, try
1420 matching an assignment. */
1421
1422 *if_type = ST_SIMPLE_IF;
1423
1424 m = gfc_match_assignment ();
1425 if (m == MATCH_YES)
1426 goto got_match;
1427
1428 gfc_free_expr (expr);
1429 gfc_undo_symbols ();
63645982 1430 gfc_current_locus = old_loc;
6de9cd9a 1431
5056a350
SK
1432 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1433 assignment was found. For MATCH_NO, continue to call the various
1434 matchers. */
17bbca74
SK
1435 if (m == MATCH_ERROR)
1436 return MATCH_ERROR;
1437
66e4ab31 1438 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
6de9cd9a
DN
1439
1440 m = gfc_match_pointer_assignment ();
1441 if (m == MATCH_YES)
1442 goto got_match;
1443
1444 gfc_free_expr (expr);
1445 gfc_undo_symbols ();
63645982 1446 gfc_current_locus = old_loc;
6de9cd9a 1447
66e4ab31 1448 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
6de9cd9a
DN
1449
1450 /* Look at the next keyword to see which matcher to call. Matching
1451 the keyword doesn't affect the symbol table, so we don't have to
1452 restore between tries. */
1453
1454#define match(string, subr, statement) \
524af0d6 1455 if (gfc_match (string) == MATCH_YES) { m = subr(); goto got_match; }
6de9cd9a
DN
1456
1457 gfc_clear_error ();
1458
1459 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
5056a350
SK
1460 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1461 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1462 match ("call", gfc_match_call, ST_CALL)
1463 match ("close", gfc_match_close, ST_CLOSE)
1464 match ("continue", gfc_match_continue, ST_CONTINUE)
1465 match ("cycle", gfc_match_cycle, ST_CYCLE)
1466 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1467 match ("end file", gfc_match_endfile, ST_END_FILE)
d0a4a61c 1468 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP)
5056a350
SK
1469 match ("exit", gfc_match_exit, ST_EXIT)
1470 match ("flush", gfc_match_flush, ST_FLUSH)
1471 match ("forall", match_simple_forall, ST_FORALL)
1472 match ("go to", gfc_match_goto, ST_GOTO)
1473 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1474 match ("inquire", gfc_match_inquire, ST_INQUIRE)
5493aa17 1475 match ("lock", gfc_match_lock, ST_LOCK)
5056a350
SK
1476 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1477 match ("open", gfc_match_open, ST_OPEN)
1478 match ("pause", gfc_match_pause, ST_NONE)
1479 match ("print", gfc_match_print, ST_WRITE)
1480 match ("read", gfc_match_read, ST_READ)
1481 match ("return", gfc_match_return, ST_RETURN)
1482 match ("rewind", gfc_match_rewind, ST_REWIND)
1483 match ("stop", gfc_match_stop, ST_STOP)
6f0f0b2e 1484 match ("wait", gfc_match_wait, ST_WAIT)
d0a4a61c
TB
1485 match ("sync all", gfc_match_sync_all, ST_SYNC_CALL);
1486 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
1487 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
5493aa17 1488 match ("unlock", gfc_match_unlock, ST_UNLOCK)
5056a350
SK
1489 match ("where", match_simple_where, ST_WHERE)
1490 match ("write", gfc_match_write, ST_WRITE)
1491
1492 /* The gfc_match_assignment() above may have returned a MATCH_NO
8b704316 1493 where the assignment was to a named constant. Check that
5056a350
SK
1494 special case here. */
1495 m = gfc_match_assignment ();
1496 if (m == MATCH_NO)
1497 {
1498 gfc_error ("Cannot assign to a named constant at %C");
1499 gfc_free_expr (expr);
1500 gfc_undo_symbols ();
1501 gfc_current_locus = old_loc;
1502 return MATCH_ERROR;
1503 }
6de9cd9a
DN
1504
1505 /* All else has failed, so give up. See if any of the matchers has
1506 stored an error message of some sort. */
b5a9fd3e 1507 if (!gfc_error_check ())
6de9cd9a
DN
1508 gfc_error ("Unclassifiable statement in IF-clause at %C");
1509
1510 gfc_free_expr (expr);
1511 return MATCH_ERROR;
1512
1513got_match:
1514 if (m == MATCH_NO)
1515 gfc_error ("Syntax error in IF-clause at %C");
1516 if (m != MATCH_YES)
1517 {
1518 gfc_free_expr (expr);
1519 return MATCH_ERROR;
1520 }
1521
1522 /* At this point, we've matched the single IF and the action clause
1523 is in new_st. Rearrange things so that the IF statement appears
1524 in new_st. */
1525
11e5274a
JW
1526 p = gfc_get_code (EXEC_IF);
1527 p->next = XCNEW (gfc_code);
6de9cd9a 1528 *p->next = new_st;
63645982 1529 p->next->loc = gfc_current_locus;
6de9cd9a 1530
a513927a 1531 p->expr1 = expr;
6de9cd9a
DN
1532
1533 gfc_clear_new_st ();
1534
1535 new_st.op = EXEC_IF;
1536 new_st.block = p;
1537
1538 return MATCH_YES;
1539}
1540
1541#undef match
1542
1543
1544/* Match an ELSE statement. */
1545
1546match
1547gfc_match_else (void)
1548{
1549 char name[GFC_MAX_SYMBOL_LEN + 1];
1550
1551 if (gfc_match_eos () == MATCH_YES)
1552 return MATCH_YES;
1553
1554 if (gfc_match_name (name) != MATCH_YES
1555 || gfc_current_block () == NULL
1556 || gfc_match_eos () != MATCH_YES)
1557 {
1558 gfc_error ("Unexpected junk after ELSE statement at %C");
1559 return MATCH_ERROR;
1560 }
1561
1562 if (strcmp (name, gfc_current_block ()->name) != 0)
1563 {
a4d9b221 1564 gfc_error ("Label %qs at %C doesn't match IF label %qs",
6de9cd9a
DN
1565 name, gfc_current_block ()->name);
1566 return MATCH_ERROR;
1567 }
1568
1569 return MATCH_YES;
1570}
1571
1572
1573/* Match an ELSE IF statement. */
1574
1575match
1576gfc_match_elseif (void)
1577{
1578 char name[GFC_MAX_SYMBOL_LEN + 1];
1579 gfc_expr *expr;
1580 match m;
1581
1582 m = gfc_match (" ( %e ) then", &expr);
1583 if (m != MATCH_YES)
1584 return m;
1585
1586 if (gfc_match_eos () == MATCH_YES)
1587 goto done;
1588
1589 if (gfc_match_name (name) != MATCH_YES
1590 || gfc_current_block () == NULL
1591 || gfc_match_eos () != MATCH_YES)
1592 {
1593 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1594 goto cleanup;
1595 }
1596
1597 if (strcmp (name, gfc_current_block ()->name) != 0)
1598 {
a4d9b221 1599 gfc_error ("Label %qs at %C doesn't match IF label %qs",
6de9cd9a
DN
1600 name, gfc_current_block ()->name);
1601 goto cleanup;
1602 }
1603
1604done:
1605 new_st.op = EXEC_IF;
a513927a 1606 new_st.expr1 = expr;
6de9cd9a
DN
1607 return MATCH_YES;
1608
1609cleanup:
1610 gfc_free_expr (expr);
1611 return MATCH_ERROR;
1612}
1613
1614
1615/* Free a gfc_iterator structure. */
1616
1617void
b251af97 1618gfc_free_iterator (gfc_iterator *iter, int flag)
6de9cd9a 1619{
66e4ab31 1620
6de9cd9a
DN
1621 if (iter == NULL)
1622 return;
1623
1624 gfc_free_expr (iter->var);
1625 gfc_free_expr (iter->start);
1626 gfc_free_expr (iter->end);
1627 gfc_free_expr (iter->step);
1628
1629 if (flag)
cede9502 1630 free (iter);
6de9cd9a
DN
1631}
1632
1633
d0a4a61c
TB
1634/* Match a CRITICAL statement. */
1635match
1636gfc_match_critical (void)
1637{
1638 gfc_st_label *label = NULL;
1639
1640 if (gfc_match_label () == MATCH_ERROR)
1641 return MATCH_ERROR;
1642
1643 if (gfc_match (" critical") != MATCH_YES)
1644 return MATCH_NO;
1645
1646 if (gfc_match_st_label (&label) == MATCH_ERROR)
1647 return MATCH_ERROR;
1648
1649 if (gfc_match_eos () != MATCH_YES)
1650 {
1651 gfc_syntax_error (ST_CRITICAL);
1652 return MATCH_ERROR;
1653 }
1654
1655 if (gfc_pure (NULL))
1656 {
1657 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1658 return MATCH_ERROR;
1659 }
1660
524af0d6 1661 if (gfc_find_state (COMP_DO_CONCURRENT))
8c6a85e3
TB
1662 {
1663 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1664 "block");
1665 return MATCH_ERROR;
1666 }
1667
ccd7751b 1668 gfc_unset_implicit_pure (NULL);
f1f39033 1669
524af0d6 1670 if (!gfc_notify_std (GFC_STD_F2008, "CRITICAL statement at %C"))
d0a4a61c
TB
1671 return MATCH_ERROR;
1672
f19626cf 1673 if (flag_coarray == GFC_FCOARRAY_NONE)
f4d1d50a 1674 {
ddc05d11
TB
1675 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
1676 "enable");
f4d1d50a
TB
1677 return MATCH_ERROR;
1678 }
1679
524af0d6 1680 if (gfc_find_state (COMP_CRITICAL))
d0a4a61c
TB
1681 {
1682 gfc_error ("Nested CRITICAL block at %C");
1683 return MATCH_ERROR;
1684 }
1685
1686 new_st.op = EXEC_CRITICAL;
1687
1688 if (label != NULL
524af0d6 1689 && !gfc_reference_st_label (label, ST_LABEL_TARGET))
d0a4a61c
TB
1690 return MATCH_ERROR;
1691
1692 return MATCH_YES;
1693}
1694
1695
9abe5e56
DK
1696/* Match a BLOCK statement. */
1697
1698match
1699gfc_match_block (void)
1700{
1701 match m;
1702
1703 if (gfc_match_label () == MATCH_ERROR)
1704 return MATCH_ERROR;
1705
1706 if (gfc_match (" block") != MATCH_YES)
1707 return MATCH_NO;
1708
1709 /* For this to be a correct BLOCK statement, the line must end now. */
1710 m = gfc_match_eos ();
1711 if (m == MATCH_ERROR)
1712 return MATCH_ERROR;
1713 if (m == MATCH_NO)
1714 return MATCH_NO;
1715
1716 return MATCH_YES;
1717}
1718
1719
03af1e4c
DK
1720/* Match an ASSOCIATE statement. */
1721
1722match
1723gfc_match_associate (void)
1724{
1725 if (gfc_match_label () == MATCH_ERROR)
1726 return MATCH_ERROR;
1727
1728 if (gfc_match (" associate") != MATCH_YES)
1729 return MATCH_NO;
1730
1731 /* Match the association list. */
1732 if (gfc_match_char ('(') != MATCH_YES)
1733 {
1734 gfc_error ("Expected association list at %C");
1735 return MATCH_ERROR;
1736 }
1737 new_st.ext.block.assoc = NULL;
1738 while (true)
1739 {
1740 gfc_association_list* newAssoc = gfc_get_association_list ();
1741 gfc_association_list* a;
1742
1743 /* Match the next association. */
1744 if (gfc_match (" %n => %e", newAssoc->name, &newAssoc->target)
1745 != MATCH_YES)
1746 {
1747 gfc_error ("Expected association at %C");
1748 goto assocListError;
1749 }
571d54de 1750 newAssoc->where = gfc_current_locus;
03af1e4c
DK
1751
1752 /* Check that the current name is not yet in the list. */
1753 for (a = new_st.ext.block.assoc; a; a = a->next)
1754 if (!strcmp (a->name, newAssoc->name))
1755 {
a4d9b221 1756 gfc_error ("Duplicate name %qs in association at %C",
03af1e4c
DK
1757 newAssoc->name);
1758 goto assocListError;
1759 }
1760
1761 /* The target expression must not be coindexed. */
1762 if (gfc_is_coindexed (newAssoc->target))
1763 {
1764 gfc_error ("Association target at %C must not be coindexed");
1765 goto assocListError;
1766 }
1767
571d54de
DK
1768 /* The `variable' field is left blank for now; because the target is not
1769 yet resolved, we can't use gfc_has_vector_subscript to determine it
8c91ab34 1770 for now. This is set during resolution. */
03af1e4c
DK
1771
1772 /* Put it into the list. */
1773 newAssoc->next = new_st.ext.block.assoc;
1774 new_st.ext.block.assoc = newAssoc;
1775
1776 /* Try next one or end if closing parenthesis is found. */
1777 gfc_gobble_whitespace ();
1778 if (gfc_peek_char () == ')')
1779 break;
1780 if (gfc_match_char (',') != MATCH_YES)
1781 {
a4d9b221 1782 gfc_error ("Expected %<)%> or %<,%> at %C");
03af1e4c
DK
1783 return MATCH_ERROR;
1784 }
1785
1786 continue;
1787
1788assocListError:
cede9502 1789 free (newAssoc);
03af1e4c
DK
1790 goto error;
1791 }
1792 if (gfc_match_char (')') != MATCH_YES)
1793 {
1794 /* This should never happen as we peek above. */
1795 gcc_unreachable ();
1796 }
1797
1798 if (gfc_match_eos () != MATCH_YES)
1799 {
1800 gfc_error ("Junk after ASSOCIATE statement at %C");
1801 goto error;
1802 }
1803
1804 return MATCH_YES;
1805
1806error:
1807 gfc_free_association_list (new_st.ext.block.assoc);
1808 return MATCH_ERROR;
1809}
1810
1811
8c6a85e3
TB
1812/* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
1813 an accessible derived type. */
6de9cd9a 1814
8c6a85e3
TB
1815static match
1816match_derived_type_spec (gfc_typespec *ts)
6de9cd9a 1817{
8c6a85e3 1818 char name[GFC_MAX_SYMBOL_LEN + 1];
8b704316 1819 locus old_locus;
8c6a85e3 1820 gfc_symbol *derived;
6de9cd9a 1821
8c6a85e3 1822 old_locus = gfc_current_locus;
6de9cd9a 1823
8c6a85e3
TB
1824 if (gfc_match ("%n", name) != MATCH_YES)
1825 {
1826 gfc_current_locus = old_locus;
1827 return MATCH_NO;
1828 }
6de9cd9a 1829
8c6a85e3 1830 gfc_find_symbol (name, NULL, 1, &derived);
6de9cd9a 1831
c3f34952
TB
1832 if (derived && derived->attr.flavor == FL_PROCEDURE && derived->attr.generic)
1833 derived = gfc_find_dt_in_generic (derived);
1834
8c6a85e3
TB
1835 if (derived && derived->attr.flavor == FL_DERIVED)
1836 {
1837 ts->type = BT_DERIVED;
1838 ts->u.derived = derived;
1839 return MATCH_YES;
1840 }
6de9cd9a 1841
8b704316 1842 gfc_current_locus = old_locus;
8c6a85e3
TB
1843 return MATCH_NO;
1844}
9b089e05 1845
6de9cd9a 1846
8c6a85e3
TB
1847/* Match a Fortran 2003 type-spec (F03:R401). This is similar to
1848 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
1849 It only includes the intrinsic types from the Fortran 2003 standard
1850 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
1851 the implicit_flag is not needed, so it was removed. Derived types are
1852 identified by their name alone. */
1853
894460a7
TB
1854match
1855gfc_match_type_spec (gfc_typespec *ts)
8c6a85e3
TB
1856{
1857 match m;
1858 locus old_locus;
1859
1860 gfc_clear_ts (ts);
1861 gfc_gobble_whitespace ();
1862 old_locus = gfc_current_locus;
1863
1864 if (match_derived_type_spec (ts) == MATCH_YES)
6de9cd9a 1865 {
8c6a85e3
TB
1866 /* Enforce F03:C401. */
1867 if (ts->u.derived->attr.abstract)
1868 {
a4d9b221 1869 gfc_error ("Derived type %qs at %L may not be ABSTRACT",
8c6a85e3
TB
1870 ts->u.derived->name, &old_locus);
1871 return MATCH_ERROR;
1872 }
1873 return MATCH_YES;
6de9cd9a
DN
1874 }
1875
8c6a85e3
TB
1876 if (gfc_match ("integer") == MATCH_YES)
1877 {
1878 ts->type = BT_INTEGER;
1879 ts->kind = gfc_default_integer_kind;
1880 goto kind_selector;
1881 }
6de9cd9a 1882
8c6a85e3
TB
1883 if (gfc_match ("real") == MATCH_YES)
1884 {
1885 ts->type = BT_REAL;
1886 ts->kind = gfc_default_real_kind;
1887 goto kind_selector;
1888 }
acb388a0 1889
8c6a85e3 1890 if (gfc_match ("double precision") == MATCH_YES)
6de9cd9a 1891 {
8c6a85e3
TB
1892 ts->type = BT_REAL;
1893 ts->kind = gfc_default_double_kind;
1894 return MATCH_YES;
6de9cd9a
DN
1895 }
1896
8c6a85e3
TB
1897 if (gfc_match ("complex") == MATCH_YES)
1898 {
1899 ts->type = BT_COMPLEX;
1900 ts->kind = gfc_default_complex_kind;
1901 goto kind_selector;
1902 }
6de9cd9a 1903
8c6a85e3
TB
1904 if (gfc_match ("character") == MATCH_YES)
1905 {
1906 ts->type = BT_CHARACTER;
6de9cd9a 1907
8c6a85e3 1908 m = gfc_match_char_spec (ts);
6de9cd9a 1909
8c6a85e3
TB
1910 if (m == MATCH_NO)
1911 m = MATCH_YES;
6de9cd9a 1912
8c6a85e3
TB
1913 return m;
1914 }
c9583ed2 1915
8c6a85e3 1916 if (gfc_match ("logical") == MATCH_YES)
6de9cd9a 1917 {
8c6a85e3
TB
1918 ts->type = BT_LOGICAL;
1919 ts->kind = gfc_default_logical_kind;
1920 goto kind_selector;
6de9cd9a
DN
1921 }
1922
8c6a85e3
TB
1923 /* If a type is not matched, simply return MATCH_NO. */
1924 gfc_current_locus = old_locus;
1925 return MATCH_NO;
6de9cd9a 1926
8c6a85e3 1927kind_selector:
6de9cd9a 1928
8c6a85e3
TB
1929 gfc_gobble_whitespace ();
1930 if (gfc_peek_ascii_char () == '*')
6de9cd9a 1931 {
8c6a85e3
TB
1932 gfc_error ("Invalid type-spec at %C");
1933 return MATCH_ERROR;
6de9cd9a
DN
1934 }
1935
8c6a85e3 1936 m = gfc_match_kind_spec (ts, false);
6de9cd9a 1937
8c6a85e3
TB
1938 if (m == MATCH_NO)
1939 m = MATCH_YES; /* No kind specifier found. */
6de9cd9a 1940
8c6a85e3 1941 return m;
6de9cd9a
DN
1942}
1943
1944
8c6a85e3 1945/******************** FORALL subroutines ********************/
6de9cd9a 1946
8c6a85e3
TB
1947/* Free a list of FORALL iterators. */
1948
1949void
1950gfc_free_forall_iterator (gfc_forall_iterator *iter)
6de9cd9a 1951{
8c6a85e3 1952 gfc_forall_iterator *next;
6de9cd9a 1953
8c6a85e3 1954 while (iter)
6de9cd9a 1955 {
8c6a85e3
TB
1956 next = iter->next;
1957 gfc_free_expr (iter->var);
1958 gfc_free_expr (iter->start);
1959 gfc_free_expr (iter->end);
1960 gfc_free_expr (iter->stride);
1961 free (iter);
1962 iter = next;
1963 }
1964}
61b644c2 1965
6de9cd9a 1966
8c6a85e3 1967/* Match an iterator as part of a FORALL statement. The format is:
61b644c2 1968
8c6a85e3 1969 <var> = <start>:<end>[:<stride>]
6de9cd9a 1970
8c6a85e3
TB
1971 On MATCH_NO, the caller tests for the possibility that there is a
1972 scalar mask expression. */
6de9cd9a 1973
8c6a85e3
TB
1974static match
1975match_forall_iterator (gfc_forall_iterator **result)
1976{
1977 gfc_forall_iterator *iter;
1978 locus where;
1979 match m;
6de9cd9a 1980
8c6a85e3
TB
1981 where = gfc_current_locus;
1982 iter = XCNEW (gfc_forall_iterator);
6de9cd9a 1983
8c6a85e3
TB
1984 m = gfc_match_expr (&iter->var);
1985 if (m != MATCH_YES)
1986 goto cleanup;
1987
1988 if (gfc_match_char ('=') != MATCH_YES
1989 || iter->var->expr_type != EXPR_VARIABLE)
e5ca9693 1990 {
8c6a85e3
TB
1991 m = MATCH_NO;
1992 goto cleanup;
1993 }
e5ca9693 1994
8c6a85e3
TB
1995 m = gfc_match_expr (&iter->start);
1996 if (m != MATCH_YES)
1997 goto cleanup;
e5ca9693 1998
8c6a85e3
TB
1999 if (gfc_match_char (':') != MATCH_YES)
2000 goto syntax;
e5ca9693 2001
8c6a85e3
TB
2002 m = gfc_match_expr (&iter->end);
2003 if (m == MATCH_NO)
2004 goto syntax;
2005 if (m == MATCH_ERROR)
2006 goto cleanup;
84fa59a7 2007
8c6a85e3
TB
2008 if (gfc_match_char (':') == MATCH_NO)
2009 iter->stride = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2010 else
6c7a4dfd 2011 {
8c6a85e3
TB
2012 m = gfc_match_expr (&iter->stride);
2013 if (m == MATCH_NO)
2014 goto syntax;
2015 if (m == MATCH_ERROR)
2016 goto cleanup;
6c7a4dfd
JJ
2017 }
2018
8c6a85e3
TB
2019 /* Mark the iteration variable's symbol as used as a FORALL index. */
2020 iter->var->symtree->n.sym->forall_index = true;
6de9cd9a 2021
8c6a85e3 2022 *result = iter;
6de9cd9a 2023 return MATCH_YES;
6de9cd9a 2024
8c6a85e3
TB
2025syntax:
2026 gfc_error ("Syntax error in FORALL iterator at %C");
2027 m = MATCH_ERROR;
6de9cd9a 2028
8c6a85e3 2029cleanup:
6de9cd9a 2030
8c6a85e3
TB
2031 gfc_current_locus = where;
2032 gfc_free_forall_iterator (iter);
2033 return m;
6de9cd9a
DN
2034}
2035
2036
8c6a85e3 2037/* Match the header of a FORALL statement. */
6de9cd9a
DN
2038
2039static match
8c6a85e3 2040match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
6de9cd9a 2041{
8c6a85e3
TB
2042 gfc_forall_iterator *head, *tail, *new_iter;
2043 gfc_expr *msk;
6de9cd9a
DN
2044 match m;
2045
8c6a85e3 2046 gfc_gobble_whitespace ();
6de9cd9a 2047
8c6a85e3
TB
2048 head = tail = NULL;
2049 msk = NULL;
6de9cd9a 2050
8c6a85e3
TB
2051 if (gfc_match_char ('(') != MATCH_YES)
2052 return MATCH_NO;
6d1b0f92 2053
8c6a85e3
TB
2054 m = match_forall_iterator (&new_iter);
2055 if (m == MATCH_ERROR)
2056 goto cleanup;
2057 if (m == MATCH_NO)
2058 goto syntax;
6de9cd9a 2059
8c6a85e3 2060 head = tail = new_iter;
f1f39033 2061
8c6a85e3 2062 for (;;)
d0a4a61c 2063 {
8c6a85e3
TB
2064 if (gfc_match_char (',') != MATCH_YES)
2065 break;
d0a4a61c 2066
8c6a85e3
TB
2067 m = match_forall_iterator (&new_iter);
2068 if (m == MATCH_ERROR)
2069 goto cleanup;
5d2d72cb 2070
8c6a85e3 2071 if (m == MATCH_YES)
5d2d72cb 2072 {
8c6a85e3
TB
2073 tail->next = new_iter;
2074 tail = new_iter;
2075 continue;
6d1b0f92
JD
2076 }
2077
8c6a85e3 2078 /* Have to have a mask expression. */
6d1b0f92 2079
8c6a85e3
TB
2080 m = gfc_match_expr (&msk);
2081 if (m == MATCH_NO)
2082 goto syntax;
2083 if (m == MATCH_ERROR)
2084 goto cleanup;
6d1b0f92 2085
d0a4a61c 2086 break;
d0a4a61c
TB
2087 }
2088
8c6a85e3
TB
2089 if (gfc_match_char (')') == MATCH_NO)
2090 goto syntax;
6de9cd9a 2091
8c6a85e3
TB
2092 *phead = head;
2093 *mask = msk;
6de9cd9a
DN
2094 return MATCH_YES;
2095
2096syntax:
8c6a85e3 2097 gfc_syntax_error (ST_FORALL);
6de9cd9a
DN
2098
2099cleanup:
8c6a85e3
TB
2100 gfc_free_expr (msk);
2101 gfc_free_forall_iterator (head);
6de9cd9a 2102
6de9cd9a
DN
2103 return MATCH_ERROR;
2104}
2105
8b704316 2106/* Match the rest of a simple FORALL statement that follows an
8c6a85e3 2107 IF statement. */
66e4ab31 2108
8c6a85e3
TB
2109static match
2110match_simple_forall (void)
6de9cd9a 2111{
8c6a85e3
TB
2112 gfc_forall_iterator *head;
2113 gfc_expr *mask;
2114 gfc_code *c;
6de9cd9a
DN
2115 match m;
2116
8c6a85e3
TB
2117 mask = NULL;
2118 head = NULL;
2119 c = NULL;
6de9cd9a 2120
8c6a85e3 2121 m = match_forall_header (&head, &mask);
6de9cd9a 2122
8c6a85e3
TB
2123 if (m == MATCH_NO)
2124 goto syntax;
2125 if (m != MATCH_YES)
2126 goto cleanup;
6de9cd9a 2127
8c6a85e3 2128 m = gfc_match_assignment ();
6de9cd9a 2129
8c6a85e3
TB
2130 if (m == MATCH_ERROR)
2131 goto cleanup;
2132 if (m == MATCH_NO)
2133 {
2134 m = gfc_match_pointer_assignment ();
2135 if (m == MATCH_ERROR)
2136 goto cleanup;
2137 if (m == MATCH_NO)
2138 goto syntax;
2139 }
6de9cd9a 2140
11e5274a 2141 c = XCNEW (gfc_code);
8c6a85e3
TB
2142 *c = new_st;
2143 c->loc = gfc_current_locus;
d0a4a61c 2144
8c6a85e3
TB
2145 if (gfc_match_eos () != MATCH_YES)
2146 goto syntax;
d0a4a61c 2147
8c6a85e3
TB
2148 gfc_clear_new_st ();
2149 new_st.op = EXEC_FORALL;
2150 new_st.expr1 = mask;
2151 new_st.ext.forall_iterator = head;
11e5274a 2152 new_st.block = gfc_get_code (EXEC_FORALL);
8c6a85e3 2153 new_st.block->next = c;
d0a4a61c 2154
8c6a85e3 2155 return MATCH_YES;
5493aa17 2156
8c6a85e3
TB
2157syntax:
2158 gfc_syntax_error (ST_FORALL);
5493aa17 2159
8c6a85e3
TB
2160cleanup:
2161 gfc_free_forall_iterator (head);
2162 gfc_free_expr (mask);
5493aa17 2163
8c6a85e3
TB
2164 return MATCH_ERROR;
2165}
5493aa17 2166
5493aa17 2167
8c6a85e3 2168/* Match a FORALL statement. */
5493aa17 2169
8c6a85e3
TB
2170match
2171gfc_match_forall (gfc_statement *st)
2172{
2173 gfc_forall_iterator *head;
2174 gfc_expr *mask;
2175 gfc_code *c;
2176 match m0, m;
5493aa17 2177
8c6a85e3
TB
2178 head = NULL;
2179 mask = NULL;
2180 c = NULL;
5493aa17 2181
8c6a85e3
TB
2182 m0 = gfc_match_label ();
2183 if (m0 == MATCH_ERROR)
2184 return MATCH_ERROR;
2185
2186 m = gfc_match (" forall");
2187 if (m != MATCH_YES)
2188 return m;
2189
2190 m = match_forall_header (&head, &mask);
5493aa17 2191 if (m == MATCH_ERROR)
8c6a85e3 2192 goto cleanup;
5493aa17 2193 if (m == MATCH_NO)
8c6a85e3
TB
2194 goto syntax;
2195
2196 if (gfc_match_eos () == MATCH_YES)
5493aa17 2197 {
8c6a85e3
TB
2198 *st = ST_FORALL_BLOCK;
2199 new_st.op = EXEC_FORALL;
2200 new_st.expr1 = mask;
2201 new_st.ext.forall_iterator = head;
2202 return MATCH_YES;
5493aa17
TB
2203 }
2204
8c6a85e3
TB
2205 m = gfc_match_assignment ();
2206 if (m == MATCH_ERROR)
2207 goto cleanup;
2208 if (m == MATCH_NO)
5493aa17 2209 {
8c6a85e3 2210 m = gfc_match_pointer_assignment ();
5493aa17 2211 if (m == MATCH_ERROR)
8c6a85e3
TB
2212 goto cleanup;
2213 if (m == MATCH_NO)
5493aa17 2214 goto syntax;
8c6a85e3 2215 }
5493aa17 2216
11e5274a 2217 c = XCNEW (gfc_code);
8c6a85e3
TB
2218 *c = new_st;
2219 c->loc = gfc_current_locus;
5493aa17 2220
8c6a85e3
TB
2221 gfc_clear_new_st ();
2222 new_st.op = EXEC_FORALL;
2223 new_st.expr1 = mask;
2224 new_st.ext.forall_iterator = head;
11e5274a 2225 new_st.block = gfc_get_code (EXEC_FORALL);
8c6a85e3 2226 new_st.block->next = c;
5493aa17 2227
8c6a85e3
TB
2228 *st = ST_FORALL;
2229 return MATCH_YES;
5493aa17 2230
8c6a85e3
TB
2231syntax:
2232 gfc_syntax_error (ST_FORALL);
5493aa17 2233
8c6a85e3
TB
2234cleanup:
2235 gfc_free_forall_iterator (head);
2236 gfc_free_expr (mask);
2237 gfc_free_statements (c);
2238 return MATCH_NO;
2239}
5493aa17 2240
5493aa17 2241
8c6a85e3 2242/* Match a DO statement. */
5493aa17 2243
8c6a85e3
TB
2244match
2245gfc_match_do (void)
2246{
2247 gfc_iterator iter, *ip;
2248 locus old_loc;
2249 gfc_st_label *label;
2250 match m;
5493aa17 2251
8c6a85e3
TB
2252 old_loc = gfc_current_locus;
2253
2254 label = NULL;
2255 iter.var = iter.start = iter.end = iter.step = NULL;
5493aa17 2256
8c6a85e3 2257 m = gfc_match_label ();
5493aa17 2258 if (m == MATCH_ERROR)
8c6a85e3 2259 return m;
5493aa17 2260
8c6a85e3
TB
2261 if (gfc_match (" do") != MATCH_YES)
2262 return MATCH_NO;
5493aa17 2263
8c6a85e3
TB
2264 m = gfc_match_st_label (&label);
2265 if (m == MATCH_ERROR)
2266 goto cleanup;
2267
2268 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2269
2270 if (gfc_match_eos () == MATCH_YES)
5493aa17 2271 {
8c6a85e3
TB
2272 iter.end = gfc_get_logical_expr (gfc_default_logical_kind, NULL, true);
2273 new_st.op = EXEC_DO_WHILE;
2274 goto done;
5493aa17
TB
2275 }
2276
8c6a85e3
TB
2277 /* Match an optional comma, if no comma is found, a space is obligatory. */
2278 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
2279 return MATCH_NO;
5493aa17 2280
8c6a85e3 2281 /* Check for balanced parens. */
8b704316 2282
8c6a85e3
TB
2283 if (gfc_match_parens () == MATCH_ERROR)
2284 return MATCH_ERROR;
5493aa17 2285
8c6a85e3
TB
2286 if (gfc_match (" concurrent") == MATCH_YES)
2287 {
2288 gfc_forall_iterator *head;
2289 gfc_expr *mask;
5493aa17 2290
524af0d6 2291 if (!gfc_notify_std (GFC_STD_F2008, "DO CONCURRENT construct at %C"))
8c6a85e3 2292 return MATCH_ERROR;
5493aa17 2293
d0a4a61c 2294
8c6a85e3
TB
2295 mask = NULL;
2296 head = NULL;
2297 m = match_forall_header (&head, &mask);
d0a4a61c 2298
8c6a85e3
TB
2299 if (m == MATCH_NO)
2300 return m;
2301 if (m == MATCH_ERROR)
2302 goto concurr_cleanup;
d0a4a61c 2303
8c6a85e3
TB
2304 if (gfc_match_eos () != MATCH_YES)
2305 goto concurr_cleanup;
d0a4a61c 2306
8c6a85e3 2307 if (label != NULL
524af0d6 2308 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
8c6a85e3 2309 goto concurr_cleanup;
f1f39033 2310
8c6a85e3
TB
2311 new_st.label1 = label;
2312 new_st.op = EXEC_DO_CONCURRENT;
2313 new_st.expr1 = mask;
2314 new_st.ext.forall_iterator = head;
d0a4a61c 2315
8c6a85e3 2316 return MATCH_YES;
f4d1d50a 2317
8c6a85e3
TB
2318concurr_cleanup:
2319 gfc_syntax_error (ST_DO);
2320 gfc_free_expr (mask);
2321 gfc_free_forall_iterator (head);
d0a4a61c
TB
2322 return MATCH_ERROR;
2323 }
5493aa17 2324
8c6a85e3
TB
2325 /* See if we have a DO WHILE. */
2326 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
d0a4a61c 2327 {
8c6a85e3 2328 new_st.op = EXEC_DO_WHILE;
d0a4a61c
TB
2329 goto done;
2330 }
2331
8c6a85e3
TB
2332 /* The abortive DO WHILE may have done something to the symbol
2333 table, so we start over. */
2334 gfc_undo_symbols ();
2335 gfc_current_locus = old_loc;
5493aa17 2336
8c6a85e3
TB
2337 gfc_match_label (); /* This won't error. */
2338 gfc_match (" do "); /* This will work. */
d0a4a61c 2339
8c6a85e3
TB
2340 gfc_match_st_label (&label); /* Can't error out. */
2341 gfc_match_char (','); /* Optional comma. */
d0a4a61c 2342
8c6a85e3
TB
2343 m = gfc_match_iterator (&iter, 0);
2344 if (m == MATCH_NO)
2345 return MATCH_NO;
2346 if (m == MATCH_ERROR)
2347 goto cleanup;
d0a4a61c 2348
8c6a85e3
TB
2349 iter.var->symtree->n.sym->attr.implied_index = 0;
2350 gfc_check_do_variable (iter.var->symtree);
d0a4a61c 2351
8c6a85e3
TB
2352 if (gfc_match_eos () != MATCH_YES)
2353 {
2354 gfc_syntax_error (ST_DO);
2355 goto cleanup;
d0a4a61c
TB
2356 }
2357
8c6a85e3 2358 new_st.op = EXEC_DO;
d0a4a61c
TB
2359
2360done:
8c6a85e3 2361 if (label != NULL
524af0d6 2362 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
8c6a85e3
TB
2363 goto cleanup;
2364
2365 new_st.label1 = label;
2366
2367 if (new_st.op == EXEC_DO_WHILE)
2368 new_st.expr1 = iter.end;
2369 else
d0a4a61c 2370 {
8c6a85e3
TB
2371 new_st.ext.iterator = ip = gfc_get_iterator ();
2372 *ip = iter;
d0a4a61c
TB
2373 }
2374
d0a4a61c
TB
2375 return MATCH_YES;
2376
d0a4a61c 2377cleanup:
8c6a85e3 2378 gfc_free_iterator (&iter, 0);
d0a4a61c
TB
2379
2380 return MATCH_ERROR;
2381}
2382
2383
8c6a85e3 2384/* Match an EXIT or CYCLE statement. */
d0a4a61c 2385
8c6a85e3
TB
2386static match
2387match_exit_cycle (gfc_statement st, gfc_exec_op op)
d0a4a61c 2388{
8c6a85e3
TB
2389 gfc_state_data *p, *o;
2390 gfc_symbol *sym;
2391 match m;
2392 int cnt;
d0a4a61c 2393
8c6a85e3
TB
2394 if (gfc_match_eos () == MATCH_YES)
2395 sym = NULL;
2396 else
2397 {
2398 char name[GFC_MAX_SYMBOL_LEN + 1];
2399 gfc_symtree* stree;
d0a4a61c 2400
8c6a85e3
TB
2401 m = gfc_match ("% %n%t", name);
2402 if (m == MATCH_ERROR)
2403 return MATCH_ERROR;
2404 if (m == MATCH_NO)
2405 {
2406 gfc_syntax_error (st);
2407 return MATCH_ERROR;
2408 }
d0a4a61c 2409
8c6a85e3
TB
2410 /* Find the corresponding symbol. If there's a BLOCK statement
2411 between here and the label, it is not in gfc_current_ns but a parent
2412 namespace! */
2413 stree = gfc_find_symtree_in_proc (name, gfc_current_ns);
2414 if (!stree)
2415 {
a4d9b221 2416 gfc_error ("Name %qs in %s statement at %C is unknown",
8c6a85e3
TB
2417 name, gfc_ascii_statement (st));
2418 return MATCH_ERROR;
2419 }
d0a4a61c 2420
8c6a85e3
TB
2421 sym = stree->n.sym;
2422 if (sym->attr.flavor != FL_LABEL)
2423 {
a4d9b221 2424 gfc_error ("Name %qs in %s statement at %C is not a construct name",
8c6a85e3
TB
2425 name, gfc_ascii_statement (st));
2426 return MATCH_ERROR;
2427 }
2428 }
d0a4a61c 2429
8c6a85e3
TB
2430 /* Find the loop specified by the label (or lack of a label). */
2431 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
2432 if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
2433 o = p;
2434 else if (p->state == COMP_CRITICAL)
2435 {
2436 gfc_error("%s statement at %C leaves CRITICAL construct",
2437 gfc_ascii_statement (st));
2438 return MATCH_ERROR;
2439 }
2440 else if (p->state == COMP_DO_CONCURRENT
2441 && (op == EXEC_EXIT || (sym && sym != p->sym)))
2442 {
2443 /* F2008, C821 & C845. */
2444 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2445 gfc_ascii_statement (st));
2446 return MATCH_ERROR;
2447 }
2448 else if ((sym && sym == p->sym)
2449 || (!sym && (p->state == COMP_DO
2450 || p->state == COMP_DO_CONCURRENT)))
2451 break;
6de9cd9a 2452
8c6a85e3 2453 if (p == NULL)
6de9cd9a 2454 {
8c6a85e3
TB
2455 if (sym == NULL)
2456 gfc_error ("%s statement at %C is not within a construct",
2457 gfc_ascii_statement (st));
2458 else
a4d9b221 2459 gfc_error ("%s statement at %C is not within construct %qs",
8c6a85e3
TB
2460 gfc_ascii_statement (st), sym->name);
2461
6de9cd9a
DN
2462 return MATCH_ERROR;
2463 }
2464
8c6a85e3
TB
2465 /* Special checks for EXIT from non-loop constructs. */
2466 switch (p->state)
2467 {
2468 case COMP_DO:
2469 case COMP_DO_CONCURRENT:
2470 break;
2471
2472 case COMP_CRITICAL:
2473 /* This is already handled above. */
2474 gcc_unreachable ();
2475
2476 case COMP_ASSOCIATE:
2477 case COMP_BLOCK:
2478 case COMP_IF:
2479 case COMP_SELECT:
2480 case COMP_SELECT_TYPE:
2481 gcc_assert (sym);
2482 if (op == EXEC_CYCLE)
2483 {
2484 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
a4d9b221 2485 " construct %qs", sym->name);
8c6a85e3
TB
2486 return MATCH_ERROR;
2487 }
2488 gcc_assert (op == EXEC_EXIT);
524af0d6
JB
2489 if (!gfc_notify_std (GFC_STD_F2008, "EXIT statement with no"
2490 " do-construct-name at %C"))
8c6a85e3
TB
2491 return MATCH_ERROR;
2492 break;
8b704316 2493
8c6a85e3 2494 default:
a4d9b221 2495 gfc_error ("%s statement at %C is not applicable to construct %qs",
8c6a85e3
TB
2496 gfc_ascii_statement (st), sym->name);
2497 return MATCH_ERROR;
2498 }
2499
2500 if (o != NULL)
2501 {
41dbbb37
TS
2502 gfc_error (is_oacc (p)
2503 ? "%s statement at %C leaving OpenACC structured block"
2504 : "%s statement at %C leaving OpenMP structured block",
8c6a85e3
TB
2505 gfc_ascii_statement (st));
2506 return MATCH_ERROR;
2507 }
2508
2509 for (o = p, cnt = 0; o->state == COMP_DO && o->previous != NULL; cnt++)
2510 o = o->previous;
2511 if (cnt > 0
2512 && o != NULL
2513 && o->state == COMP_OMP_STRUCTURED_BLOCK
41dbbb37
TS
2514 && (o->head->op == EXEC_OACC_LOOP
2515 || o->head->op == EXEC_OACC_PARALLEL_LOOP))
2516 {
2517 int collapse = 1;
2518 gcc_assert (o->head->next != NULL
2519 && (o->head->next->op == EXEC_DO
2520 || o->head->next->op == EXEC_DO_WHILE)
2521 && o->previous != NULL
2522 && o->previous->tail->op == o->head->op);
2523 if (o->previous->tail->ext.omp_clauses != NULL
2524 && o->previous->tail->ext.omp_clauses->collapse > 1)
2525 collapse = o->previous->tail->ext.omp_clauses->collapse;
2526 if (st == ST_EXIT && cnt <= collapse)
2527 {
2528 gfc_error ("EXIT statement at %C terminating !$ACC LOOP loop");
2529 return MATCH_ERROR;
2530 }
2531 if (st == ST_CYCLE && cnt < collapse)
2532 {
2533 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2534 " !$ACC LOOP loop");
2535 return MATCH_ERROR;
2536 }
2537 }
2538 if (cnt > 0
2539 && o != NULL
2540 && (o->state == COMP_OMP_STRUCTURED_BLOCK)
8c6a85e3 2541 && (o->head->op == EXEC_OMP_DO
dd2fc525
JJ
2542 || o->head->op == EXEC_OMP_PARALLEL_DO
2543 || o->head->op == EXEC_OMP_SIMD
2544 || o->head->op == EXEC_OMP_DO_SIMD
2545 || o->head->op == EXEC_OMP_PARALLEL_DO_SIMD))
8c6a85e3
TB
2546 {
2547 int collapse = 1;
2548 gcc_assert (o->head->next != NULL
2549 && (o->head->next->op == EXEC_DO
2550 || o->head->next->op == EXEC_DO_WHILE)
2551 && o->previous != NULL
2552 && o->previous->tail->op == o->head->op);
2553 if (o->previous->tail->ext.omp_clauses != NULL
2554 && o->previous->tail->ext.omp_clauses->collapse > 1)
2555 collapse = o->previous->tail->ext.omp_clauses->collapse;
2556 if (st == ST_EXIT && cnt <= collapse)
2557 {
2558 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2559 return MATCH_ERROR;
2560 }
2561 if (st == ST_CYCLE && cnt < collapse)
2562 {
2563 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2564 " !$OMP DO loop");
2565 return MATCH_ERROR;
2566 }
2567 }
2568
2569 /* Save the first statement in the construct - needed by the backend. */
2570 new_st.ext.which_construct = p->construct;
2571
2572 new_st.op = op;
2573
6de9cd9a
DN
2574 return MATCH_YES;
2575}
2576
2577
8c6a85e3 2578/* Match the EXIT statement. */
6de9cd9a
DN
2579
2580match
8c6a85e3 2581gfc_match_exit (void)
6de9cd9a 2582{
8c6a85e3
TB
2583 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
2584}
6de9cd9a 2585
6de9cd9a 2586
8c6a85e3 2587/* Match the CYCLE statement. */
6de9cd9a 2588
8c6a85e3
TB
2589match
2590gfc_match_cycle (void)
2591{
2592 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
6de9cd9a
DN
2593}
2594
2595
2e9cc48c
TB
2596/* Match a number or character constant after an (ERROR) STOP or PAUSE
2597 statement. */
6de9cd9a 2598
8c6a85e3
TB
2599static match
2600gfc_match_stopcode (gfc_statement st)
6de9cd9a 2601{
8c6a85e3 2602 gfc_expr *e;
6de9cd9a
DN
2603 match m;
2604
8c6a85e3
TB
2605 e = NULL;
2606
2607 if (gfc_match_eos () != MATCH_YES)
6de9cd9a 2608 {
8c6a85e3
TB
2609 m = gfc_match_init_expr (&e);
2610 if (m == MATCH_ERROR)
2611 goto cleanup;
2612 if (m == MATCH_NO)
2613 goto syntax;
6de9cd9a 2614
8c6a85e3
TB
2615 if (gfc_match_eos () != MATCH_YES)
2616 goto syntax;
6de9cd9a
DN
2617 }
2618
8c6a85e3 2619 if (gfc_pure (NULL))
6de9cd9a 2620 {
2e9cc48c
TB
2621 if (st == ST_ERROR_STOP)
2622 {
2623 if (!gfc_notify_std (GFC_STD_F2015, "%s statement at %C in PURE "
2624 "procedure", gfc_ascii_statement (st)))
2625 goto cleanup;
2626 }
2627 else
2628 {
2629 gfc_error ("%s statement not allowed in PURE procedure at %C",
2630 gfc_ascii_statement (st));
2631 goto cleanup;
2632 }
8c6a85e3 2633 }
6de9cd9a 2634
ccd7751b 2635 gfc_unset_implicit_pure (NULL);
6de9cd9a 2636
524af0d6 2637 if (st == ST_STOP && gfc_find_state (COMP_CRITICAL))
8c6a85e3
TB
2638 {
2639 gfc_error ("Image control statement STOP at %C in CRITICAL block");
2640 goto cleanup;
2641 }
524af0d6 2642 if (st == ST_STOP && gfc_find_state (COMP_DO_CONCURRENT))
8c6a85e3
TB
2643 {
2644 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
2645 goto cleanup;
2646 }
6de9cd9a 2647
8c6a85e3
TB
2648 if (e != NULL)
2649 {
2650 if (!(e->ts.type == BT_CHARACTER || e->ts.type == BT_INTEGER))
6de9cd9a 2651 {
8c6a85e3
TB
2652 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
2653 &e->where);
2654 goto cleanup;
6de9cd9a 2655 }
6de9cd9a 2656
8c6a85e3 2657 if (e->rank != 0)
6de9cd9a 2658 {
8c6a85e3
TB
2659 gfc_error ("STOP code at %L must be scalar",
2660 &e->where);
2661 goto cleanup;
6de9cd9a 2662 }
6de9cd9a 2663
8c6a85e3
TB
2664 if (e->ts.type == BT_CHARACTER
2665 && e->ts.kind != gfc_default_character_kind)
6de9cd9a 2666 {
8c6a85e3
TB
2667 gfc_error ("STOP code at %L must be default character KIND=%d",
2668 &e->where, (int) gfc_default_character_kind);
2669 goto cleanup;
6de9cd9a 2670 }
6de9cd9a 2671
8c6a85e3
TB
2672 if (e->ts.type == BT_INTEGER
2673 && e->ts.kind != gfc_default_integer_kind)
2674 {
2675 gfc_error ("STOP code at %L must be default integer KIND=%d",
2676 &e->where, (int) gfc_default_integer_kind);
2677 goto cleanup;
2678 }
6de9cd9a
DN
2679 }
2680
8c6a85e3 2681 switch (st)
6de9cd9a 2682 {
8c6a85e3
TB
2683 case ST_STOP:
2684 new_st.op = EXEC_STOP;
2685 break;
2686 case ST_ERROR_STOP:
2687 new_st.op = EXEC_ERROR_STOP;
2688 break;
2689 case ST_PAUSE:
2690 new_st.op = EXEC_PAUSE;
2691 break;
2692 default:
2693 gcc_unreachable ();
6de9cd9a
DN
2694 }
2695
8c6a85e3
TB
2696 new_st.expr1 = e;
2697 new_st.ext.stop_code = -1;
6de9cd9a 2698
6de9cd9a
DN
2699 return MATCH_YES;
2700
2701syntax:
8c6a85e3
TB
2702 gfc_syntax_error (st);
2703
6de9cd9a 2704cleanup:
8c6a85e3
TB
2705
2706 gfc_free_expr (e);
6de9cd9a
DN
2707 return MATCH_ERROR;
2708}
2709
2710
8c6a85e3 2711/* Match the (deprecated) PAUSE statement. */
6de9cd9a 2712
8c6a85e3
TB
2713match
2714gfc_match_pause (void)
6de9cd9a 2715{
8c6a85e3 2716 match m;
6de9cd9a 2717
8c6a85e3
TB
2718 m = gfc_match_stopcode (ST_PAUSE);
2719 if (m == MATCH_YES)
6de9cd9a 2720 {
524af0d6 2721 if (!gfc_notify_std (GFC_STD_F95_DEL, "PAUSE statement at %C"))
8c6a85e3 2722 m = MATCH_ERROR;
6de9cd9a 2723 }
8c6a85e3 2724 return m;
6de9cd9a
DN
2725}
2726
2727
8c6a85e3 2728/* Match the STOP statement. */
cf2b3c22 2729
8c6a85e3
TB
2730match
2731gfc_match_stop (void)
cf2b3c22 2732{
8c6a85e3
TB
2733 return gfc_match_stopcode (ST_STOP);
2734}
cf2b3c22 2735
1fccc6c3 2736
8c6a85e3 2737/* Match the ERROR STOP statement. */
1fccc6c3 2738
8c6a85e3
TB
2739match
2740gfc_match_error_stop (void)
2741{
524af0d6 2742 if (!gfc_notify_std (GFC_STD_F2008, "ERROR STOP statement at %C"))
8c6a85e3 2743 return MATCH_ERROR;
cf2b3c22 2744
8c6a85e3 2745 return gfc_match_stopcode (ST_ERROR_STOP);
cf2b3c22
TB
2746}
2747
2748
8c6a85e3
TB
2749/* Match LOCK/UNLOCK statement. Syntax:
2750 LOCK ( lock-variable [ , lock-stat-list ] )
2751 UNLOCK ( lock-variable [ , sync-stat-list ] )
2752 where lock-stat is ACQUIRED_LOCK or sync-stat
2753 and sync-stat is STAT= or ERRMSG=. */
8234e5e0
SK
2754
2755static match
8c6a85e3 2756lock_unlock_statement (gfc_statement st)
8234e5e0
SK
2757{
2758 match m;
8c6a85e3
TB
2759 gfc_expr *tmp, *lockvar, *acq_lock, *stat, *errmsg;
2760 bool saw_acq_lock, saw_stat, saw_errmsg;
8234e5e0 2761
8c6a85e3
TB
2762 tmp = lockvar = acq_lock = stat = errmsg = NULL;
2763 saw_acq_lock = saw_stat = saw_errmsg = false;
8234e5e0 2764
8c6a85e3 2765 if (gfc_pure (NULL))
1107bd38 2766 {
8c6a85e3
TB
2767 gfc_error ("Image control statement %s at %C in PURE procedure",
2768 st == ST_LOCK ? "LOCK" : "UNLOCK");
2769 return MATCH_ERROR;
1107bd38 2770 }
1107bd38 2771
ccd7751b 2772 gfc_unset_implicit_pure (NULL);
8c6a85e3 2773
f19626cf 2774 if (flag_coarray == GFC_FCOARRAY_NONE)
8234e5e0 2775 {
ddc05d11 2776 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
8c6a85e3 2777 return MATCH_ERROR;
8234e5e0
SK
2778 }
2779
524af0d6 2780 if (gfc_find_state (COMP_CRITICAL))
8234e5e0 2781 {
8c6a85e3
TB
2782 gfc_error ("Image control statement %s at %C in CRITICAL block",
2783 st == ST_LOCK ? "LOCK" : "UNLOCK");
2784 return MATCH_ERROR;
8234e5e0
SK
2785 }
2786
524af0d6 2787 if (gfc_find_state (COMP_DO_CONCURRENT))
8234e5e0 2788 {
8c6a85e3
TB
2789 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
2790 st == ST_LOCK ? "LOCK" : "UNLOCK");
2791 return MATCH_ERROR;
8234e5e0
SK
2792 }
2793
8c6a85e3
TB
2794 if (gfc_match_char ('(') != MATCH_YES)
2795 goto syntax;
2796
2797 if (gfc_match ("%e", &lockvar) != MATCH_YES)
2798 goto syntax;
2799 m = gfc_match_char (',');
2800 if (m == MATCH_ERROR)
2801 goto syntax;
2802 if (m == MATCH_NO)
8234e5e0 2803 {
8c6a85e3
TB
2804 m = gfc_match_char (')');
2805 if (m == MATCH_YES)
2806 goto done;
2807 goto syntax;
8234e5e0
SK
2808 }
2809
8c6a85e3 2810 for (;;)
8234e5e0 2811 {
8c6a85e3
TB
2812 m = gfc_match (" stat = %v", &tmp);
2813 if (m == MATCH_ERROR)
2814 goto syntax;
2815 if (m == MATCH_YES)
2816 {
2817 if (saw_stat)
2818 {
2819 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
2820 goto cleanup;
2821 }
2822 stat = tmp;
2823 saw_stat = true;
1fccc6c3 2824
8c6a85e3
TB
2825 m = gfc_match_char (',');
2826 if (m == MATCH_YES)
2827 continue;
1fccc6c3 2828
8c6a85e3
TB
2829 tmp = NULL;
2830 break;
2831 }
1fccc6c3 2832
8c6a85e3
TB
2833 m = gfc_match (" errmsg = %v", &tmp);
2834 if (m == MATCH_ERROR)
2835 goto syntax;
2836 if (m == MATCH_YES)
2837 {
2838 if (saw_errmsg)
2839 {
2840 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
2841 goto cleanup;
2842 }
2843 errmsg = tmp;
2844 saw_errmsg = true;
8234e5e0 2845
8c6a85e3
TB
2846 m = gfc_match_char (',');
2847 if (m == MATCH_YES)
2848 continue;
8234e5e0 2849
8c6a85e3
TB
2850 tmp = NULL;
2851 break;
2852 }
8234e5e0 2853
8c6a85e3
TB
2854 m = gfc_match (" acquired_lock = %v", &tmp);
2855 if (m == MATCH_ERROR || st == ST_UNLOCK)
2856 goto syntax;
2857 if (m == MATCH_YES)
2858 {
2859 if (saw_acq_lock)
2860 {
2861 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L ",
2862 &tmp->where);
2863 goto cleanup;
2864 }
2865 acq_lock = tmp;
2866 saw_acq_lock = true;
8234e5e0 2867
8c6a85e3
TB
2868 m = gfc_match_char (',');
2869 if (m == MATCH_YES)
2870 continue;
2871
2872 tmp = NULL;
2873 break;
2874 }
2875
2876 break;
2877 }
2878
2879 if (m == MATCH_ERROR)
2880 goto syntax;
2881
2882 if (gfc_match (" )%t") != MATCH_YES)
2883 goto syntax;
2884
2885done:
2886 switch (st)
8234e5e0 2887 {
8c6a85e3
TB
2888 case ST_LOCK:
2889 new_st.op = EXEC_LOCK;
2890 break;
2891 case ST_UNLOCK:
2892 new_st.op = EXEC_UNLOCK;
2893 break;
2894 default:
2895 gcc_unreachable ();
8234e5e0
SK
2896 }
2897
8c6a85e3
TB
2898 new_st.expr1 = lockvar;
2899 new_st.expr2 = stat;
2900 new_st.expr3 = errmsg;
2901 new_st.expr4 = acq_lock;
8234e5e0 2902
8c6a85e3 2903 return MATCH_YES;
8234e5e0 2904
8c6a85e3
TB
2905syntax:
2906 gfc_syntax_error (st);
2907
2908cleanup:
fd2805e1
TB
2909 if (acq_lock != tmp)
2910 gfc_free_expr (acq_lock);
2911 if (errmsg != tmp)
2912 gfc_free_expr (errmsg);
2913 if (stat != tmp)
2914 gfc_free_expr (stat);
2915
8c6a85e3
TB
2916 gfc_free_expr (tmp);
2917 gfc_free_expr (lockvar);
8c6a85e3
TB
2918
2919 return MATCH_ERROR;
8234e5e0
SK
2920}
2921
2922
8c6a85e3
TB
2923match
2924gfc_match_lock (void)
2925{
524af0d6 2926 if (!gfc_notify_std (GFC_STD_F2008, "LOCK statement at %C"))
8c6a85e3
TB
2927 return MATCH_ERROR;
2928
2929 return lock_unlock_statement (ST_LOCK);
2930}
2931
6de9cd9a
DN
2932
2933match
8c6a85e3 2934gfc_match_unlock (void)
6de9cd9a 2935{
524af0d6 2936 if (!gfc_notify_std (GFC_STD_F2008, "UNLOCK statement at %C"))
8c6a85e3 2937 return MATCH_ERROR;
6de9cd9a 2938
8c6a85e3
TB
2939 return lock_unlock_statement (ST_UNLOCK);
2940}
6de9cd9a 2941
6de9cd9a 2942
8c6a85e3
TB
2943/* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
2944 SYNC ALL [(sync-stat-list)]
2945 SYNC MEMORY [(sync-stat-list)]
2946 SYNC IMAGES (image-set [, sync-stat-list] )
2947 with sync-stat is int-expr or *. */
1fccc6c3 2948
8c6a85e3
TB
2949static match
2950sync_statement (gfc_statement st)
2951{
2952 match m;
2953 gfc_expr *tmp, *imageset, *stat, *errmsg;
2954 bool saw_stat, saw_errmsg;
1fccc6c3 2955
8c6a85e3
TB
2956 tmp = imageset = stat = errmsg = NULL;
2957 saw_stat = saw_errmsg = false;
2958
2959 if (gfc_pure (NULL))
2960 {
2961 gfc_error ("Image control statement SYNC at %C in PURE procedure");
2962 return MATCH_ERROR;
1fccc6c3 2963 }
8c6a85e3 2964
ccd7751b 2965 gfc_unset_implicit_pure (NULL);
8c6a85e3 2966
524af0d6 2967 if (!gfc_notify_std (GFC_STD_F2008, "SYNC statement at %C"))
8c6a85e3
TB
2968 return MATCH_ERROR;
2969
f19626cf 2970 if (flag_coarray == GFC_FCOARRAY_NONE)
8234e5e0 2971 {
ddc05d11
TB
2972 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
2973 "enable");
8c6a85e3
TB
2974 return MATCH_ERROR;
2975 }
e69afb29 2976
524af0d6 2977 if (gfc_find_state (COMP_CRITICAL))
8c6a85e3
TB
2978 {
2979 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
2980 return MATCH_ERROR;
8234e5e0
SK
2981 }
2982
524af0d6 2983 if (gfc_find_state (COMP_DO_CONCURRENT))
6de9cd9a 2984 {
8c6a85e3
TB
2985 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
2986 return MATCH_ERROR;
2987 }
6de9cd9a 2988
8c6a85e3
TB
2989 if (gfc_match_eos () == MATCH_YES)
2990 {
2991 if (st == ST_SYNC_IMAGES)
6de9cd9a 2992 goto syntax;
8c6a85e3
TB
2993 goto done;
2994 }
6de9cd9a 2995
8c6a85e3
TB
2996 if (gfc_match_char ('(') != MATCH_YES)
2997 goto syntax;
c9583ed2 2998
8c6a85e3
TB
2999 if (st == ST_SYNC_IMAGES)
3000 {
3001 /* Denote '*' as imageset == NULL. */
3002 m = gfc_match_char ('*');
3003 if (m == MATCH_ERROR)
3004 goto syntax;
3005 if (m == MATCH_NO)
6de9cd9a 3006 {
8c6a85e3
TB
3007 if (gfc_match ("%e", &imageset) != MATCH_YES)
3008 goto syntax;
6de9cd9a 3009 }
8c6a85e3
TB
3010 m = gfc_match_char (',');
3011 if (m == MATCH_ERROR)
3012 goto syntax;
3013 if (m == MATCH_NO)
e69afb29 3014 {
8c6a85e3
TB
3015 m = gfc_match_char (')');
3016 if (m == MATCH_YES)
3017 goto done;
3018 goto syntax;
e69afb29 3019 }
8c6a85e3 3020 }
e69afb29 3021
8c6a85e3
TB
3022 for (;;)
3023 {
3024 m = gfc_match (" stat = %v", &tmp);
3025 if (m == MATCH_ERROR)
3026 goto syntax;
3027 if (m == MATCH_YES)
8234e5e0 3028 {
8c6a85e3 3029 if (saw_stat)
8234e5e0 3030 {
8c6a85e3 3031 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
8234e5e0
SK
3032 goto cleanup;
3033 }
8c6a85e3
TB
3034 stat = tmp;
3035 saw_stat = true;
8234e5e0 3036
8c6a85e3
TB
3037 if (gfc_match_char (',') == MATCH_YES)
3038 continue;
3039
3040 tmp = NULL;
3041 break;
3042 }
3043
3044 m = gfc_match (" errmsg = %v", &tmp);
3045 if (m == MATCH_ERROR)
3046 goto syntax;
3047 if (m == MATCH_YES)
3048 {
3049 if (saw_errmsg)
8234e5e0 3050 {
8c6a85e3 3051 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
8234e5e0
SK
3052 goto cleanup;
3053 }
8c6a85e3
TB
3054 errmsg = tmp;
3055 saw_errmsg = true;
8234e5e0 3056
8c6a85e3
TB
3057 if (gfc_match_char (',') == MATCH_YES)
3058 continue;
3e978d30 3059
8c6a85e3
TB
3060 tmp = NULL;
3061 break;
d59b1dcb
DF
3062 }
3063
6de9cd9a 3064 break;
8c6a85e3 3065 }
6de9cd9a 3066
8c6a85e3
TB
3067 if (gfc_match (" )%t") != MATCH_YES)
3068 goto syntax;
3759634f 3069
8c6a85e3
TB
3070done:
3071 switch (st)
3072 {
3073 case ST_SYNC_ALL:
3074 new_st.op = EXEC_SYNC_ALL;
3075 break;
3076 case ST_SYNC_IMAGES:
3077 new_st.op = EXEC_SYNC_IMAGES;
3078 break;
3079 case ST_SYNC_MEMORY:
3080 new_st.op = EXEC_SYNC_MEMORY;
3081 break;
3082 default:
3083 gcc_unreachable ();
3084 }
3759634f 3085
8c6a85e3
TB
3086 new_st.expr1 = imageset;
3087 new_st.expr2 = stat;
3088 new_st.expr3 = errmsg;
3759634f 3089
8c6a85e3 3090 return MATCH_YES;
3759634f 3091
8c6a85e3
TB
3092syntax:
3093 gfc_syntax_error (st);
8234e5e0 3094
8c6a85e3 3095cleanup:
fd2805e1
TB
3096 if (stat != tmp)
3097 gfc_free_expr (stat);
3098 if (errmsg != tmp)
3099 gfc_free_expr (errmsg);
3100
8c6a85e3
TB
3101 gfc_free_expr (tmp);
3102 gfc_free_expr (imageset);
8234e5e0 3103
8c6a85e3
TB
3104 return MATCH_ERROR;
3105}
8234e5e0 3106
8234e5e0 3107
8c6a85e3 3108/* Match SYNC ALL statement. */
8234e5e0 3109
8c6a85e3
TB
3110match
3111gfc_match_sync_all (void)
3112{
3113 return sync_statement (ST_SYNC_ALL);
3114}
8234e5e0 3115
94bff632 3116
8c6a85e3 3117/* Match SYNC IMAGES statement. */
94bff632 3118
8c6a85e3
TB
3119match
3120gfc_match_sync_images (void)
3121{
3122 return sync_statement (ST_SYNC_IMAGES);
3123}
94bff632 3124
94bff632 3125
8c6a85e3 3126/* Match SYNC MEMORY statement. */
3759634f 3127
8c6a85e3
TB
3128match
3129gfc_match_sync_memory (void)
3130{
3131 return sync_statement (ST_SYNC_MEMORY);
3132}
6de9cd9a 3133
6de9cd9a 3134
8c6a85e3 3135/* Match a CONTINUE statement. */
e69afb29 3136
8c6a85e3
TB
3137match
3138gfc_match_continue (void)
3139{
3140 if (gfc_match_eos () != MATCH_YES)
e69afb29 3141 {
8c6a85e3
TB
3142 gfc_syntax_error (ST_CONTINUE);
3143 return MATCH_ERROR;
e69afb29 3144 }
6de9cd9a 3145
8c6a85e3 3146 new_st.op = EXEC_CONTINUE;
6de9cd9a 3147 return MATCH_YES;
6de9cd9a
DN
3148}
3149
3150
8c6a85e3 3151/* Match the (deprecated) ASSIGN statement. */
6de9cd9a
DN
3152
3153match
8c6a85e3 3154gfc_match_assign (void)
6de9cd9a 3155{
8c6a85e3
TB
3156 gfc_expr *expr;
3157 gfc_st_label *label;
6de9cd9a 3158
8c6a85e3 3159 if (gfc_match (" %l", &label) == MATCH_YES)
6de9cd9a 3160 {
524af0d6 3161 if (!gfc_reference_st_label (label, ST_LABEL_UNKNOWN))
8c6a85e3
TB
3162 return MATCH_ERROR;
3163 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
5aacb11e 3164 {
524af0d6 3165 if (!gfc_notify_std (GFC_STD_F95_DEL, "ASSIGN statement at %C"))
8c6a85e3 3166 return MATCH_ERROR;
5aacb11e 3167
8c6a85e3 3168 expr->symtree->n.sym->attr.assign = 1;
6de9cd9a 3169
8c6a85e3
TB
3170 new_st.op = EXEC_LABEL_ASSIGN;
3171 new_st.label1 = label;
3172 new_st.expr1 = expr;
3173 return MATCH_YES;
6de9cd9a 3174 }
6de9cd9a 3175 }
8c6a85e3 3176 return MATCH_NO;
6de9cd9a
DN
3177}
3178
3179
8c6a85e3
TB
3180/* Match the GO TO statement. As a computed GOTO statement is
3181 matched, it is transformed into an equivalent SELECT block. No
3182 tree is necessary, and the resulting jumps-to-jumps are
3183 specifically optimized away by the back end. */
6de9cd9a
DN
3184
3185match
8c6a85e3 3186gfc_match_goto (void)
6de9cd9a 3187{
8c6a85e3
TB
3188 gfc_code *head, *tail;
3189 gfc_expr *expr;
3190 gfc_case *cp;
3191 gfc_st_label *label;
3192 int i;
6de9cd9a 3193 match m;
6de9cd9a 3194
8c6a85e3 3195 if (gfc_match (" %l%t", &label) == MATCH_YES)
6de9cd9a 3196 {
524af0d6 3197 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
8c6a85e3 3198 return MATCH_ERROR;
6de9cd9a 3199
8c6a85e3
TB
3200 new_st.op = EXEC_GOTO;
3201 new_st.label1 = label;
3202 return MATCH_YES;
3203 }
6de9cd9a 3204
8b704316 3205 /* The assigned GO TO statement. */
c9583ed2 3206
8c6a85e3
TB
3207 if (gfc_match_variable (&expr, 0) == MATCH_YES)
3208 {
524af0d6 3209 if (!gfc_notify_std (GFC_STD_F95_DEL, "Assigned GOTO statement at %C"))
8c6a85e3 3210 return MATCH_ERROR;
cf2b3c22 3211
8c6a85e3
TB
3212 new_st.op = EXEC_GOTO;
3213 new_st.expr1 = expr;
3759634f 3214
8c6a85e3
TB
3215 if (gfc_match_eos () == MATCH_YES)
3216 return MATCH_YES;
f1f39033 3217
8c6a85e3
TB
3218 /* Match label list. */
3219 gfc_match_char (',');
3220 if (gfc_match_char ('(') != MATCH_YES)
3759634f 3221 {
8c6a85e3
TB
3222 gfc_syntax_error (ST_GOTO);
3223 return MATCH_ERROR;
6de9cd9a 3224 }
8c6a85e3 3225 head = tail = NULL;
6de9cd9a 3226
8c6a85e3
TB
3227 do
3228 {
3229 m = gfc_match_st_label (&label);
3230 if (m != MATCH_YES)
3231 goto syntax;
6de9cd9a 3232
524af0d6 3233 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
8c6a85e3 3234 goto cleanup;
3759634f 3235
8c6a85e3 3236 if (head == NULL)
11e5274a 3237 head = tail = gfc_get_code (EXEC_GOTO);
8c6a85e3 3238 else
3759634f 3239 {
11e5274a 3240 tail->block = gfc_get_code (EXEC_GOTO);
8c6a85e3 3241 tail = tail->block;
3759634f
SK
3242 }
3243
8c6a85e3 3244 tail->label1 = label;
8c6a85e3
TB
3245 }
3246 while (gfc_match_char (',') == MATCH_YES);
3759634f 3247
8c6a85e3
TB
3248 if (gfc_match (")%t") != MATCH_YES)
3249 goto syntax;
6de9cd9a 3250
8c6a85e3
TB
3251 if (head == NULL)
3252 {
3253 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3254 goto syntax;
3759634f 3255 }
8c6a85e3 3256 new_st.block = head;
3759634f 3257
8c6a85e3
TB
3258 return MATCH_YES;
3259 }
3759634f 3260
8c6a85e3
TB
3261 /* Last chance is a computed GO TO statement. */
3262 if (gfc_match_char ('(') != MATCH_YES)
3263 {
3264 gfc_syntax_error (ST_GOTO);
3265 return MATCH_ERROR;
3266 }
3759634f 3267
8c6a85e3
TB
3268 head = tail = NULL;
3269 i = 1;
3759634f 3270
8c6a85e3
TB
3271 do
3272 {
3273 m = gfc_match_st_label (&label);
3274 if (m != MATCH_YES)
3275 goto syntax;
3276
524af0d6 3277 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
8c6a85e3
TB
3278 goto cleanup;
3279
3280 if (head == NULL)
11e5274a 3281 head = tail = gfc_get_code (EXEC_SELECT);
8c6a85e3
TB
3282 else
3283 {
11e5274a 3284 tail->block = gfc_get_code (EXEC_SELECT);
8c6a85e3 3285 tail = tail->block;
3759634f
SK
3286 }
3287
8c6a85e3
TB
3288 cp = gfc_get_case ();
3289 cp->low = cp->high = gfc_get_int_expr (gfc_default_integer_kind,
3290 NULL, i++);
3759634f 3291
8c6a85e3
TB
3292 tail->ext.block.case_list = cp;
3293
11e5274a 3294 tail->next = gfc_get_code (EXEC_GOTO);
8c6a85e3 3295 tail->next->label1 = label;
3759634f 3296 }
8c6a85e3 3297 while (gfc_match_char (',') == MATCH_YES);
6de9cd9a 3298
8c6a85e3 3299 if (gfc_match_char (')') != MATCH_YES)
6de9cd9a
DN
3300 goto syntax;
3301
8c6a85e3
TB
3302 if (head == NULL)
3303 {
3304 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3305 goto syntax;
3306 }
6de9cd9a 3307
8c6a85e3
TB
3308 /* Get the rest of the statement. */
3309 gfc_match_char (',');
6de9cd9a 3310
8c6a85e3
TB
3311 if (gfc_match (" %e%t", &expr) != MATCH_YES)
3312 goto syntax;
6de9cd9a 3313
524af0d6 3314 if (!gfc_notify_std (GFC_STD_F95_OBS, "Computed GOTO at %C"))
e2ab8b09
JW
3315 return MATCH_ERROR;
3316
8c6a85e3
TB
3317 /* At this point, a computed GOTO has been fully matched and an
3318 equivalent SELECT statement constructed. */
7f42f27f 3319
8c6a85e3
TB
3320 new_st.op = EXEC_SELECT;
3321 new_st.expr1 = NULL;
6de9cd9a 3322
8c6a85e3
TB
3323 /* Hack: For a "real" SELECT, the expression is in expr. We put
3324 it in expr2 so we can distinguish then and produce the correct
3325 diagnostics. */
3326 new_st.expr2 = expr;
3327 new_st.block = head;
3328 return MATCH_YES;
6de9cd9a 3329
8c6a85e3
TB
3330syntax:
3331 gfc_syntax_error (ST_GOTO);
6de9cd9a 3332cleanup:
8c6a85e3 3333 gfc_free_statements (head);
6de9cd9a 3334 return MATCH_ERROR;
6de9cd9a
DN
3335}
3336
3337
8c6a85e3 3338/* Frees a list of gfc_alloc structures. */
8e1f752a 3339
8c6a85e3
TB
3340void
3341gfc_free_alloc_list (gfc_alloc *p)
8e1f752a 3342{
8c6a85e3 3343 gfc_alloc *q;
8e1f752a 3344
8c6a85e3 3345 for (; p; p = q)
8e1f752a 3346 {
8c6a85e3
TB
3347 q = p->next;
3348 gfc_free_expr (p->expr);
3349 free (p);
8e1f752a 3350 }
8e1f752a
DK
3351}
3352
3353
8c6a85e3 3354/* Match an ALLOCATE statement. */
6de9cd9a
DN
3355
3356match
8c6a85e3 3357gfc_match_allocate (void)
6de9cd9a 3358{
8c6a85e3
TB
3359 gfc_alloc *head, *tail;
3360 gfc_expr *stat, *errmsg, *tmp, *source, *mold;
3361 gfc_typespec ts;
6de9cd9a 3362 gfc_symbol *sym;
6de9cd9a 3363 match m;
8c6a85e3
TB
3364 locus old_locus, deferred_locus;
3365 bool saw_stat, saw_errmsg, saw_source, saw_mold, saw_deferred, b1, b2, b3;
8b704316 3366 bool saw_unlimited = false;
6de9cd9a 3367
8c6a85e3
TB
3368 head = tail = NULL;
3369 stat = errmsg = source = mold = tmp = NULL;
3370 saw_stat = saw_errmsg = saw_source = saw_mold = saw_deferred = false;
6de9cd9a 3371
8c6a85e3 3372 if (gfc_match_char ('(') != MATCH_YES)
6de9cd9a 3373 goto syntax;
6de9cd9a 3374
8c6a85e3
TB
3375 /* Match an optional type-spec. */
3376 old_locus = gfc_current_locus;
894460a7 3377 m = gfc_match_type_spec (&ts);
8c6a85e3
TB
3378 if (m == MATCH_ERROR)
3379 goto cleanup;
3380 else if (m == MATCH_NO)
3381 {
3382 char name[GFC_MAX_SYMBOL_LEN + 3];
6de9cd9a 3383
8c6a85e3
TB
3384 if (gfc_match ("%n :: ", name) == MATCH_YES)
3385 {
3386 gfc_error ("Error in type-spec at %L", &old_locus);
3387 goto cleanup;
3388 }
8e1f752a 3389
8c6a85e3
TB
3390 ts.type = BT_UNKNOWN;
3391 }
3392 else
6291f3ba 3393 {
8c6a85e3 3394 if (gfc_match (" :: ") == MATCH_YES)
eda0ed25 3395 {
524af0d6
JB
3396 if (!gfc_notify_std (GFC_STD_F2003, "typespec in ALLOCATE at %L",
3397 &old_locus))
8c6a85e3 3398 goto cleanup;
6de9cd9a 3399
8c6a85e3
TB
3400 if (ts.deferred)
3401 {
3402 gfc_error ("Type-spec at %L cannot contain a deferred "
3403 "type parameter", &old_locus);
3404 goto cleanup;
3405 }
239b48db
TB
3406
3407 if (ts.type == BT_CHARACTER)
3408 ts.u.cl->length_from_typespec = true;
8c6a85e3
TB
3409 }
3410 else
3411 {
3412 ts.type = BT_UNKNOWN;
3413 gfc_current_locus = old_locus;
eda0ed25 3414 }
6291f3ba 3415 }
8de10a62 3416
8c6a85e3 3417 for (;;)
6de9cd9a 3418 {
8c6a85e3
TB
3419 if (head == NULL)
3420 head = tail = gfc_get_alloc ();
3421 else
3422 {
3423 tail->next = gfc_get_alloc ();
3424 tail = tail->next;
3425 }
3426
3427 m = gfc_match_variable (&tail->expr, 0);
6de9cd9a
DN
3428 if (m == MATCH_NO)
3429 goto syntax;
3430 if (m == MATCH_ERROR)
3431 goto cleanup;
3432
8c6a85e3
TB
3433 if (gfc_check_do_variable (tail->expr->symtree))
3434 goto cleanup;
6de9cd9a 3435
ccd7751b
TB
3436 bool impure = gfc_impure_variable (tail->expr->symtree->n.sym);
3437 if (impure && gfc_pure (NULL))
8c6a85e3
TB
3438 {
3439 gfc_error ("Bad allocate-object at %C for a PURE procedure");
3440 goto cleanup;
3441 }
6de9cd9a 3442
ccd7751b
TB
3443 if (impure)
3444 gfc_unset_implicit_pure (NULL);
6de9cd9a 3445
8c6a85e3
TB
3446 if (tail->expr->ts.deferred)
3447 {
3448 saw_deferred = true;
3449 deferred_locus = tail->expr->where;
3450 }
6de9cd9a 3451
524af0d6
JB
3452 if (gfc_find_state (COMP_DO_CONCURRENT)
3453 || gfc_find_state (COMP_CRITICAL))
8c6a85e3
TB
3454 {
3455 gfc_ref *ref;
3456 bool coarray = tail->expr->symtree->n.sym->attr.codimension;
3457 for (ref = tail->expr->ref; ref; ref = ref->next)
3458 if (ref->type == REF_COMPONENT)
3459 coarray = ref->u.c.component->attr.codimension;
6de9cd9a 3460
524af0d6 3461 if (coarray && gfc_find_state (COMP_DO_CONCURRENT))
8c6a85e3
TB
3462 {
3463 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
3464 goto cleanup;
3465 }
524af0d6 3466 if (coarray && gfc_find_state (COMP_CRITICAL))
8c6a85e3
TB
3467 {
3468 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
3469 goto cleanup;
3470 }
3471 }
6de9cd9a 3472
98cf47d1
JW
3473 /* Check for F08:C628. */
3474 sym = tail->expr->symtree->n.sym;
3475 b1 = !(tail->expr->ref
3476 && (tail->expr->ref->type == REF_COMPONENT
3477 || tail->expr->ref->type == REF_ARRAY));
3478 if (sym && sym->ts.type == BT_CLASS && sym->attr.class_ok)
3479 b2 = !(CLASS_DATA (sym)->attr.allocatable
3480 || CLASS_DATA (sym)->attr.class_pointer);
3481 else
3482 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
3483 || sym->attr.proc_pointer);
3484 b3 = sym && sym->ns && sym->ns->proc_name
3485 && (sym->ns->proc_name->attr.allocatable
3486 || sym->ns->proc_name->attr.pointer
3487 || sym->ns->proc_name->attr.proc_pointer);
3488 if (b1 && b2 && !b3)
3489 {
3490 gfc_error ("Allocate-object at %L is neither a data pointer "
3491 "nor an allocatable variable", &tail->expr->where);
3492 goto cleanup;
3493 }
3494
8c6a85e3
TB
3495 /* The ALLOCATE statement had an optional typespec. Check the
3496 constraints. */
3497 if (ts.type != BT_UNKNOWN)
6de9cd9a 3498 {
8c6a85e3
TB
3499 /* Enforce F03:C624. */
3500 if (!gfc_type_compatible (&tail->expr->ts, &ts))
3501 {
3502 gfc_error ("Type of entity at %L is type incompatible with "
3503 "typespec", &tail->expr->where);
3504 goto cleanup;
3505 }
6de9cd9a 3506
8c6a85e3 3507 /* Enforce F03:C627. */
8b704316 3508 if (ts.kind != tail->expr->ts.kind && !UNLIMITED_POLY (tail->expr))
8c6a85e3
TB
3509 {
3510 gfc_error ("Kind type parameter for entity at %L differs from "
3511 "the kind type parameter of the typespec",
3512 &tail->expr->where);
3513 goto cleanup;
3514 }
3515 }
6de9cd9a 3516
8c6a85e3
TB
3517 if (tail->expr->ts.type == BT_DERIVED)
3518 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
6de9cd9a 3519
8b704316
PT
3520 saw_unlimited = saw_unlimited | UNLIMITED_POLY (tail->expr);
3521
8c6a85e3
TB
3522 if (gfc_peek_ascii_char () == '(' && !sym->attr.dimension)
3523 {
3524 gfc_error ("Shape specification for allocatable scalar at %C");
3525 goto cleanup;
3526 }
6de9cd9a 3527
8c6a85e3
TB
3528 if (gfc_match_char (',') != MATCH_YES)
3529 break;
3530
3531alloc_opt_list:
3532
3533 m = gfc_match (" stat = %v", &tmp);
3534 if (m == MATCH_ERROR)
3535 goto cleanup;
3536 if (m == MATCH_YES)
3537 {
3538 /* Enforce C630. */
3539 if (saw_stat)
3540 {
3541 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3542 goto cleanup;
3543 }
3544
3545 stat = tmp;
3546 tmp = NULL;
3547 saw_stat = true;
3548
3549 if (gfc_check_do_variable (stat->symtree))
3550 goto cleanup;
3551
3552 if (gfc_match_char (',') == MATCH_YES)
3553 goto alloc_opt_list;
3554 }
3555
3556 m = gfc_match (" errmsg = %v", &tmp);
3557 if (m == MATCH_ERROR)
3558 goto cleanup;
3559 if (m == MATCH_YES)
3560 {
524af0d6 3561 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG tag at %L", &tmp->where))
8c6a85e3
TB
3562 goto cleanup;
3563
3564 /* Enforce C630. */
3565 if (saw_errmsg)
3566 {
3567 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3568 goto cleanup;
3569 }
3570
3571 errmsg = tmp;
3572 tmp = NULL;
3573 saw_errmsg = true;
3574
3575 if (gfc_match_char (',') == MATCH_YES)
3576 goto alloc_opt_list;
3577 }
3578
3579 m = gfc_match (" source = %e", &tmp);
3580 if (m == MATCH_ERROR)
3581 goto cleanup;
3582 if (m == MATCH_YES)
3583 {
524af0d6 3584 if (!gfc_notify_std (GFC_STD_F2003, "SOURCE tag at %L", &tmp->where))
8c6a85e3
TB
3585 goto cleanup;
3586
3587 /* Enforce C630. */
3588 if (saw_source)
3589 {
3590 gfc_error ("Redundant SOURCE tag found at %L ", &tmp->where);
3591 goto cleanup;
3592 }
3593
3594 /* The next 2 conditionals check C631. */
3595 if (ts.type != BT_UNKNOWN)
3596 {
fea70c99 3597 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
8c6a85e3
TB
3598 &tmp->where, &old_locus);
3599 goto cleanup;
3600 }
3601
4cb2a867 3602 if (head->next
524af0d6
JB
3603 && !gfc_notify_std (GFC_STD_F2008, "SOURCE tag at %L"
3604 " with more than a single allocate object",
3605 &tmp->where))
4cb2a867 3606 goto cleanup;
8c6a85e3
TB
3607
3608 source = tmp;
3609 tmp = NULL;
3610 saw_source = true;
3611
3612 if (gfc_match_char (',') == MATCH_YES)
3613 goto alloc_opt_list;
6de9cd9a 3614 }
8c6a85e3
TB
3615
3616 m = gfc_match (" mold = %e", &tmp);
3617 if (m == MATCH_ERROR)
3618 goto cleanup;
3619 if (m == MATCH_YES)
3620 {
524af0d6 3621 if (!gfc_notify_std (GFC_STD_F2008, "MOLD tag at %L", &tmp->where))
8c6a85e3
TB
3622 goto cleanup;
3623
3624 /* Check F08:C636. */
3625 if (saw_mold)
3626 {
3627 gfc_error ("Redundant MOLD tag found at %L ", &tmp->where);
3628 goto cleanup;
3629 }
8b704316 3630
8c6a85e3
TB
3631 /* Check F08:C637. */
3632 if (ts.type != BT_UNKNOWN)
3633 {
fea70c99 3634 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
8c6a85e3
TB
3635 &tmp->where, &old_locus);
3636 goto cleanup;
3637 }
3638
3639 mold = tmp;
3640 tmp = NULL;
3641 saw_mold = true;
3642 mold->mold = 1;
3643
3644 if (gfc_match_char (',') == MATCH_YES)
3645 goto alloc_opt_list;
3646 }
3647
3648 gfc_gobble_whitespace ();
3649
3650 if (gfc_peek_char () == ')')
3651 break;
6de9cd9a
DN
3652 }
3653
8c6a85e3
TB
3654 if (gfc_match (" )%t") != MATCH_YES)
3655 goto syntax;
3656
3657 /* Check F08:C637. */
3658 if (source && mold)
3659 {
fea70c99
MLI
3660 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
3661 &mold->where, &source->where);
8c6a85e3
TB
3662 goto cleanup;
3663 }
3664
3665 /* Check F03:C623, */
3666 if (saw_deferred && ts.type == BT_UNKNOWN && !source && !mold)
3667 {
3668 gfc_error ("Allocate-object at %L with a deferred type parameter "
3669 "requires either a type-spec or SOURCE tag or a MOLD tag",
3670 &deferred_locus);
3671 goto cleanup;
3672 }
8b704316
PT
3673
3674 /* Check F03:C625, */
3675 if (saw_unlimited && ts.type == BT_UNKNOWN && !source && !mold)
3676 {
3677 for (tail = head; tail; tail = tail->next)
3678 {
3679 if (UNLIMITED_POLY (tail->expr))
3680 gfc_error ("Unlimited polymorphic allocate-object at %L "
3681 "requires either a type-spec or SOURCE tag "
3682 "or a MOLD tag", &tail->expr->where);
3683 }
3684 goto cleanup;
3685 }
3686
8c6a85e3
TB
3687 new_st.op = EXEC_ALLOCATE;
3688 new_st.expr1 = stat;
3689 new_st.expr2 = errmsg;
3690 if (source)
3691 new_st.expr3 = source;
3692 else
3693 new_st.expr3 = mold;
3694 new_st.ext.alloc.list = head;
3695 new_st.ext.alloc.ts = ts;
6de9cd9a
DN
3696
3697 return MATCH_YES;
3698
3699syntax:
8c6a85e3 3700 gfc_syntax_error (ST_ALLOCATE);
6de9cd9a
DN
3701
3702cleanup:
8c6a85e3
TB
3703 gfc_free_expr (errmsg);
3704 gfc_free_expr (source);
3705 gfc_free_expr (stat);
3706 gfc_free_expr (mold);
3707 if (tmp && tmp->expr_type) gfc_free_expr (tmp);
3708 gfc_free_alloc_list (head);
6de9cd9a
DN
3709 return MATCH_ERROR;
3710}
3711
3712
8c6a85e3
TB
3713/* Match a NULLIFY statement. A NULLIFY statement is transformed into
3714 a set of pointer assignments to intrinsic NULL(). */
9056bd70 3715
8c6a85e3
TB
3716match
3717gfc_match_nullify (void)
9056bd70 3718{
8c6a85e3
TB
3719 gfc_code *tail;
3720 gfc_expr *e, *p;
3721 match m;
9056bd70 3722
8c6a85e3 3723 tail = NULL;
53814b8f 3724
8c6a85e3
TB
3725 if (gfc_match_char ('(') != MATCH_YES)
3726 goto syntax;
9056bd70 3727
8c6a85e3 3728 for (;;)
9056bd70 3729 {
8c6a85e3
TB
3730 m = gfc_match_variable (&p, 0);
3731 if (m == MATCH_ERROR)
3732 goto cleanup;
3733 if (m == MATCH_NO)
3734 goto syntax;
3735
3736 if (gfc_check_do_variable (p->symtree))
3737 goto cleanup;
3738
3739 /* F2008, C1242. */
3740 if (gfc_is_coindexed (p))
3741 {
0c133211 3742 gfc_error ("Pointer object at %C shall not be coindexed");
8c6a85e3
TB
3743 goto cleanup;
3744 }
3745
3746 /* build ' => NULL() '. */
3747 e = gfc_get_null_expr (&gfc_current_locus);
3748
3749 /* Chain to list. */
3750 if (tail == NULL)
11e5274a
JW
3751 {
3752 tail = &new_st;
3753 tail->op = EXEC_POINTER_ASSIGN;
3754 }
8c6a85e3
TB
3755 else
3756 {
11e5274a 3757 tail->next = gfc_get_code (EXEC_POINTER_ASSIGN);
8c6a85e3
TB
3758 tail = tail->next;
3759 }
3760
8c6a85e3
TB
3761 tail->expr1 = p;
3762 tail->expr2 = e;
3763
3764 if (gfc_match (" )%t") == MATCH_YES)
3765 break;
3766 if (gfc_match_char (',') != MATCH_YES)
3767 goto syntax;
9056bd70
TS
3768 }
3769
8c6a85e3
TB
3770 return MATCH_YES;
3771
3772syntax:
3773 gfc_syntax_error (ST_NULLIFY);
3774
3775cleanup:
3776 gfc_free_statements (new_st.next);
3777 new_st.next = NULL;
3778 gfc_free_expr (new_st.expr1);
3779 new_st.expr1 = NULL;
3780 gfc_free_expr (new_st.expr2);
3781 new_st.expr2 = NULL;
3782 return MATCH_ERROR;
9056bd70
TS
3783}
3784
3785
8c6a85e3 3786/* Match a DEALLOCATE statement. */
6de9cd9a 3787
8c6a85e3
TB
3788match
3789gfc_match_deallocate (void)
6de9cd9a 3790{
8c6a85e3
TB
3791 gfc_alloc *head, *tail;
3792 gfc_expr *stat, *errmsg, *tmp;
3793 gfc_symbol *sym;
6de9cd9a 3794 match m;
8c6a85e3 3795 bool saw_stat, saw_errmsg, b1, b2;
6de9cd9a 3796
8c6a85e3
TB
3797 head = tail = NULL;
3798 stat = errmsg = tmp = NULL;
3799 saw_stat = saw_errmsg = false;
6de9cd9a 3800
8c6a85e3
TB
3801 if (gfc_match_char ('(') != MATCH_YES)
3802 goto syntax;
3803
3804 for (;;)
6de9cd9a 3805 {
8c6a85e3
TB
3806 if (head == NULL)
3807 head = tail = gfc_get_alloc ();
3808 else
3809 {
3810 tail->next = gfc_get_alloc ();
3811 tail = tail->next;
3812 }
3813
3814 m = gfc_match_variable (&tail->expr, 0);
3815 if (m == MATCH_ERROR)
3816 goto cleanup;
3817 if (m == MATCH_NO)
3818 goto syntax;
3819
3820 if (gfc_check_do_variable (tail->expr->symtree))
3821 goto cleanup;
3822
3823 sym = tail->expr->symtree->n.sym;
3824
ccd7751b
TB
3825 bool impure = gfc_impure_variable (sym);
3826 if (impure && gfc_pure (NULL))
8c6a85e3
TB
3827 {
3828 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
3829 goto cleanup;
3830 }
3831
ccd7751b
TB
3832 if (impure)
3833 gfc_unset_implicit_pure (NULL);
8c6a85e3
TB
3834
3835 if (gfc_is_coarray (tail->expr)
524af0d6 3836 && gfc_find_state (COMP_DO_CONCURRENT))
8c6a85e3
TB
3837 {
3838 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
3839 goto cleanup;
3840 }
3841
3842 if (gfc_is_coarray (tail->expr)
524af0d6 3843 && gfc_find_state (COMP_CRITICAL))
8c6a85e3
TB
3844 {
3845 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
3846 goto cleanup;
3847 }
3848
3849 /* FIXME: disable the checking on derived types. */
3850 b1 = !(tail->expr->ref
3851 && (tail->expr->ref->type == REF_COMPONENT
3852 || tail->expr->ref->type == REF_ARRAY));
3853 if (sym && sym->ts.type == BT_CLASS)
3854 b2 = !(CLASS_DATA (sym)->attr.allocatable
3855 || CLASS_DATA (sym)->attr.class_pointer);
3856 else
3857 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
3858 || sym->attr.proc_pointer);
3859 if (b1 && b2)
3860 {
3861 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
b59e9071 3862 "nor an allocatable variable");
8c6a85e3
TB
3863 goto cleanup;
3864 }
3865
3866 if (gfc_match_char (',') != MATCH_YES)
3867 break;
3868
3869dealloc_opt_list:
3870
3871 m = gfc_match (" stat = %v", &tmp);
3872 if (m == MATCH_ERROR)
3873 goto cleanup;
3874 if (m == MATCH_YES)
3875 {
3876 if (saw_stat)
3877 {
3878 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3879 gfc_free_expr (tmp);
3880 goto cleanup;
3881 }
3882
3883 stat = tmp;
3884 saw_stat = true;
3885
3886 if (gfc_check_do_variable (stat->symtree))
3887 goto cleanup;
3888
3889 if (gfc_match_char (',') == MATCH_YES)
3890 goto dealloc_opt_list;
3891 }
3892
3893 m = gfc_match (" errmsg = %v", &tmp);
3894 if (m == MATCH_ERROR)
3895 goto cleanup;
3896 if (m == MATCH_YES)
3897 {
524af0d6 3898 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG at %L", &tmp->where))
8c6a85e3
TB
3899 goto cleanup;
3900
3901 if (saw_errmsg)
3902 {
3903 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3904 gfc_free_expr (tmp);
3905 goto cleanup;
3906 }
3907
3908 errmsg = tmp;
3909 saw_errmsg = true;
3910
3911 if (gfc_match_char (',') == MATCH_YES)
3912 goto dealloc_opt_list;
3913 }
3914
3915 gfc_gobble_whitespace ();
3916
3917 if (gfc_peek_char () == ')')
3918 break;
6de9cd9a
DN
3919 }
3920
8c6a85e3
TB
3921 if (gfc_match (" )%t") != MATCH_YES)
3922 goto syntax;
3923
3924 new_st.op = EXEC_DEALLOCATE;
3925 new_st.expr1 = stat;
3926 new_st.expr2 = errmsg;
3927 new_st.ext.alloc.list = head;
3928
3929 return MATCH_YES;
6de9cd9a 3930
8c6a85e3
TB
3931syntax:
3932 gfc_syntax_error (ST_DEALLOCATE);
6de9cd9a 3933
8c6a85e3
TB
3934cleanup:
3935 gfc_free_expr (errmsg);
3936 gfc_free_expr (stat);
3937 gfc_free_alloc_list (head);
6de9cd9a
DN
3938 return MATCH_ERROR;
3939}
3940
3941
8c6a85e3 3942/* Match a RETURN statement. */
6de9cd9a
DN
3943
3944match
8c6a85e3 3945gfc_match_return (void)
6de9cd9a 3946{
8c6a85e3 3947 gfc_expr *e;
6de9cd9a 3948 match m;
8c6a85e3 3949 gfc_compile_state s;
6de9cd9a 3950
8c6a85e3
TB
3951 e = NULL;
3952
524af0d6 3953 if (gfc_find_state (COMP_CRITICAL))
6de9cd9a 3954 {
8c6a85e3
TB
3955 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
3956 return MATCH_ERROR;
6de9cd9a
DN
3957 }
3958
524af0d6 3959 if (gfc_find_state (COMP_DO_CONCURRENT))
6de9cd9a 3960 {
8c6a85e3
TB
3961 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
3962 return MATCH_ERROR;
3963 }
f69ab0e0 3964
8c6a85e3
TB
3965 if (gfc_match_eos () == MATCH_YES)
3966 goto done;
6de9cd9a 3967
524af0d6 3968 if (!gfc_find_state (COMP_SUBROUTINE))
8c6a85e3
TB
3969 {
3970 gfc_error ("Alternate RETURN statement at %C is only allowed within "
3971 "a SUBROUTINE");
3972 goto cleanup;
3973 }
6de9cd9a 3974
8c6a85e3
TB
3975 if (gfc_current_form == FORM_FREE)
3976 {
3977 /* The following are valid, so we can't require a blank after the
3978 RETURN keyword:
3979 return+1
3980 return(1) */
3981 char c = gfc_peek_ascii_char ();
3982 if (ISALPHA (c) || ISDIGIT (c))
3983 return MATCH_NO;
3984 }
6de9cd9a 3985
8c6a85e3
TB
3986 m = gfc_match (" %e%t", &e);
3987 if (m == MATCH_YES)
3988 goto done;
3989 if (m == MATCH_ERROR)
3990 goto cleanup;
6de9cd9a 3991
8c6a85e3 3992 gfc_syntax_error (ST_RETURN);
6de9cd9a 3993
8c6a85e3
TB
3994cleanup:
3995 gfc_free_expr (e);
3996 return MATCH_ERROR;
6de9cd9a 3997
8c6a85e3
TB
3998done:
3999 gfc_enclosing_unit (&s);
4000 if (s == COMP_PROGRAM
524af0d6
JB
4001 && !gfc_notify_std (GFC_STD_GNU, "RETURN statement in "
4002 "main program at %C"))
8c6a85e3 4003 return MATCH_ERROR;
30aabb86 4004
8c6a85e3
TB
4005 new_st.op = EXEC_RETURN;
4006 new_st.expr1 = e;
30aabb86 4007
8c6a85e3
TB
4008 return MATCH_YES;
4009}
30aabb86 4010
30aabb86 4011
8b704316 4012/* Match the call of a type-bound procedure, if CALL%var has already been
8c6a85e3 4013 matched and var found to be a derived-type variable. */
30aabb86 4014
8c6a85e3
TB
4015static match
4016match_typebound_call (gfc_symtree* varst)
4017{
4018 gfc_expr* base;
4019 match m;
30aabb86 4020
8c6a85e3
TB
4021 base = gfc_get_expr ();
4022 base->expr_type = EXPR_VARIABLE;
4023 base->symtree = varst;
4024 base->where = gfc_current_locus;
4025 gfc_set_sym_referenced (varst->n.sym);
8b704316 4026
8c6a85e3
TB
4027 m = gfc_match_varspec (base, 0, true, true);
4028 if (m == MATCH_NO)
4029 gfc_error ("Expected component reference at %C");
4030 if (m != MATCH_YES)
36abe895
TB
4031 {
4032 gfc_free_expr (base);
4033 return MATCH_ERROR;
4034 }
6de9cd9a 4035
8c6a85e3
TB
4036 if (gfc_match_eos () != MATCH_YES)
4037 {
4038 gfc_error ("Junk after CALL at %C");
36abe895 4039 gfc_free_expr (base);
8c6a85e3
TB
4040 return MATCH_ERROR;
4041 }
30aabb86 4042
8c6a85e3
TB
4043 if (base->expr_type == EXPR_COMPCALL)
4044 new_st.op = EXEC_COMPCALL;
4045 else if (base->expr_type == EXPR_PPC)
4046 new_st.op = EXEC_CALL_PPC;
4047 else
4048 {
4049 gfc_error ("Expected type-bound procedure or procedure pointer component "
4050 "at %C");
36abe895 4051 gfc_free_expr (base);
8c6a85e3 4052 return MATCH_ERROR;
6de9cd9a 4053 }
8c6a85e3 4054 new_st.expr1 = base;
6de9cd9a 4055
6de9cd9a 4056 return MATCH_YES;
6de9cd9a
DN
4057}
4058
4059
8c6a85e3
TB
4060/* Match a CALL statement. The tricky part here are possible
4061 alternate return specifiers. We handle these by having all
4062 "subroutines" actually return an integer via a register that gives
4063 the return number. If the call specifies alternate returns, we
4064 generate code for a SELECT statement whose case clauses contain
4065 GOTOs to the various labels. */
6de9cd9a
DN
4066
4067match
8c6a85e3 4068gfc_match_call (void)
6de9cd9a
DN
4069{
4070 char name[GFC_MAX_SYMBOL_LEN + 1];
8c6a85e3
TB
4071 gfc_actual_arglist *a, *arglist;
4072 gfc_case *new_case;
6de9cd9a 4073 gfc_symbol *sym;
8c6a85e3
TB
4074 gfc_symtree *st;
4075 gfc_code *c;
6de9cd9a 4076 match m;
8c6a85e3 4077 int i;
6de9cd9a 4078
8c6a85e3 4079 arglist = NULL;
6de9cd9a 4080
8c6a85e3
TB
4081 m = gfc_match ("% %n", name);
4082 if (m == MATCH_NO)
4083 goto syntax;
6de9cd9a 4084 if (m != MATCH_YES)
8c6a85e3 4085 return m;
6de9cd9a 4086
8c6a85e3 4087 if (gfc_get_ha_sym_tree (name, &st))
6de9cd9a
DN
4088 return MATCH_ERROR;
4089
8c6a85e3 4090 sym = st->n.sym;
6de9cd9a 4091
8c6a85e3
TB
4092 /* If this is a variable of derived-type, it probably starts a type-bound
4093 procedure call. */
4094 if ((sym->attr.flavor != FL_PROCEDURE
4095 || gfc_is_function_return_value (sym, gfc_current_ns))
4096 && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS))
4097 return match_typebound_call (st);
6de9cd9a 4098
8c6a85e3
TB
4099 /* If it does not seem to be callable (include functions so that the
4100 right association is made. They are thrown out in resolution.)
4101 ... */
4102 if (!sym->attr.generic
4103 && !sym->attr.subroutine
4104 && !sym->attr.function)
4105 {
4106 if (!(sym->attr.external && !sym->attr.referenced))
4107 {
4108 /* ...create a symbol in this scope... */
4109 if (sym->ns != gfc_current_ns
4110 && gfc_get_sym_tree (name, NULL, &st, false) == 1)
4111 return MATCH_ERROR;
6de9cd9a 4112
8c6a85e3
TB
4113 if (sym != st->n.sym)
4114 sym = st->n.sym;
4115 }
6de9cd9a 4116
8c6a85e3 4117 /* ...and then to try to make the symbol into a subroutine. */
524af0d6 4118 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
8c6a85e3
TB
4119 return MATCH_ERROR;
4120 }
6de9cd9a 4121
8c6a85e3 4122 gfc_set_sym_referenced (sym);
6de9cd9a 4123
8c6a85e3 4124 if (gfc_match_eos () != MATCH_YES)
6de9cd9a 4125 {
8c6a85e3
TB
4126 m = gfc_match_actual_arglist (1, &arglist);
4127 if (m == MATCH_NO)
4128 goto syntax;
4129 if (m == MATCH_ERROR)
4130 goto cleanup;
4131
4132 if (gfc_match_eos () != MATCH_YES)
4133 goto syntax;
6de9cd9a 4134 }
6de9cd9a 4135
8c6a85e3
TB
4136 /* If any alternate return labels were found, construct a SELECT
4137 statement that will jump to the right place. */
6de9cd9a 4138
8c6a85e3
TB
4139 i = 0;
4140 for (a = arglist; a; a = a->next)
4141 if (a->expr == NULL)
502af491
PCC
4142 {
4143 i = 1;
4144 break;
4145 }
6de9cd9a 4146
8c6a85e3
TB
4147 if (i)
4148 {
4149 gfc_symtree *select_st;
4150 gfc_symbol *select_sym;
4151 char name[GFC_MAX_SYMBOL_LEN + 1];
6de9cd9a 4152
11e5274a 4153 new_st.next = c = gfc_get_code (EXEC_SELECT);
8c6a85e3
TB
4154 sprintf (name, "_result_%s", sym->name);
4155 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
6de9cd9a 4156
8c6a85e3
TB
4157 select_sym = select_st->n.sym;
4158 select_sym->ts.type = BT_INTEGER;
4159 select_sym->ts.kind = gfc_default_integer_kind;
4160 gfc_set_sym_referenced (select_sym);
4161 c->expr1 = gfc_get_expr ();
4162 c->expr1->expr_type = EXPR_VARIABLE;
4163 c->expr1->symtree = select_st;
4164 c->expr1->ts = select_sym->ts;
4165 c->expr1->where = gfc_current_locus;
4166
4167 i = 0;
4168 for (a = arglist; a; a = a->next)
6de9cd9a 4169 {
8c6a85e3
TB
4170 if (a->expr != NULL)
4171 continue;
6de9cd9a 4172
524af0d6 4173 if (!gfc_reference_st_label (a->label, ST_LABEL_TARGET))
8c6a85e3 4174 continue;
e0e85e06 4175
8c6a85e3 4176 i++;
6de9cd9a 4177
11e5274a 4178 c->block = gfc_get_code (EXEC_SELECT);
8c6a85e3 4179 c = c->block;
6de9cd9a 4180
8c6a85e3
TB
4181 new_case = gfc_get_case ();
4182 new_case->high = gfc_get_int_expr (gfc_default_integer_kind, NULL, i);
4183 new_case->low = new_case->high;
4184 c->ext.block.case_list = new_case;
6de9cd9a 4185
11e5274a 4186 c->next = gfc_get_code (EXEC_GOTO);
8c6a85e3
TB
4187 c->next->label1 = a->label;
4188 }
4189 }
e0e85e06 4190
8c6a85e3
TB
4191 new_st.op = EXEC_CALL;
4192 new_st.symtree = st;
4193 new_st.ext.actual = arglist;
6de9cd9a 4194
8c6a85e3 4195 return MATCH_YES;
6de9cd9a 4196
8c6a85e3
TB
4197syntax:
4198 gfc_syntax_error (ST_CALL);
6de9cd9a 4199
8c6a85e3
TB
4200cleanup:
4201 gfc_free_actual_arglist (arglist);
4202 return MATCH_ERROR;
4203}
6de9cd9a 4204
6de9cd9a 4205
8c6a85e3
TB
4206/* Given a name, return a pointer to the common head structure,
4207 creating it if it does not exist. If FROM_MODULE is nonzero, we
8b704316 4208 mangle the name so that it doesn't interfere with commons defined
8c6a85e3
TB
4209 in the using namespace.
4210 TODO: Add to global symbol tree. */
4211
4212gfc_common_head *
4213gfc_get_common (const char *name, int from_module)
4214{
4215 gfc_symtree *st;
4216 static int serial = 0;
4217 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
4218
4219 if (from_module)
4220 {
4221 /* A use associated common block is only needed to correctly layout
4222 the variables it contains. */
4223 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
4224 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
6de9cd9a 4225 }
8c6a85e3
TB
4226 else
4227 {
4228 st = gfc_find_symtree (gfc_current_ns->common_root, name);
6de9cd9a 4229
8c6a85e3
TB
4230 if (st == NULL)
4231 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
4232 }
6de9cd9a 4233
8c6a85e3
TB
4234 if (st->n.common == NULL)
4235 {
4236 st->n.common = gfc_get_common_head ();
4237 st->n.common->where = gfc_current_locus;
4238 strcpy (st->n.common->name, name);
4239 }
6de9cd9a 4240
8c6a85e3 4241 return st->n.common;
6de9cd9a
DN
4242}
4243
4244
8c6a85e3 4245/* Match a common block name. */
6de9cd9a 4246
8c6a85e3 4247match match_common_name (char *name)
6de9cd9a
DN
4248{
4249 match m;
4250
8c6a85e3
TB
4251 if (gfc_match_char ('/') == MATCH_NO)
4252 {
4253 name[0] = '\0';
4254 return MATCH_YES;
4255 }
6de9cd9a 4256
8c6a85e3
TB
4257 if (gfc_match_char ('/') == MATCH_YES)
4258 {
4259 name[0] = '\0';
4260 return MATCH_YES;
4261 }
6de9cd9a 4262
8c6a85e3 4263 m = gfc_match_name (name);
6de9cd9a 4264
8c6a85e3
TB
4265 if (m == MATCH_ERROR)
4266 return MATCH_ERROR;
4267 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
4268 return MATCH_YES;
6de9cd9a 4269
8c6a85e3
TB
4270 gfc_error ("Syntax error in common block name at %C");
4271 return MATCH_ERROR;
31fee91e
MM
4272}
4273
4274
8c6a85e3 4275/* Match a COMMON statement. */
6de9cd9a
DN
4276
4277match
8c6a85e3 4278gfc_match_common (void)
6de9cd9a 4279{
8c6a85e3
TB
4280 gfc_symbol *sym, **head, *tail, *other, *old_blank_common;
4281 char name[GFC_MAX_SYMBOL_LEN + 1];
4282 gfc_common_head *t;
4283 gfc_array_spec *as;
4284 gfc_equiv *e1, *e2;
6de9cd9a
DN
4285 match m;
4286
8c6a85e3
TB
4287 old_blank_common = gfc_current_ns->blank_common.head;
4288 if (old_blank_common)
4289 {
4290 while (old_blank_common->common_next)
4291 old_blank_common = old_blank_common->common_next;
4292 }
4293
4294 as = NULL;
6de9cd9a
DN
4295
4296 for (;;)
4297 {
8c6a85e3
TB
4298 m = match_common_name (name);
4299 if (m == MATCH_ERROR)
4300 goto cleanup;
6de9cd9a 4301
8c6a85e3
TB
4302 if (name[0] == '\0')
4303 {
4304 t = &gfc_current_ns->blank_common;
4305 if (t->head == NULL)
4306 t->where = gfc_current_locus;
4307 }
4308 else
4309 {
4310 t = gfc_get_common (name, 0);
4311 }
4312 head = &t->head;
4313
4314 if (*head == NULL)
4315 tail = NULL;
4316 else
4317 {
4318 tail = *head;
4319 while (tail->common_next)
4320 tail = tail->common_next;
4321 }
6de9cd9a 4322
8c6a85e3 4323 /* Grab the list of symbols. */
6de9cd9a
DN
4324 for (;;)
4325 {
8c6a85e3 4326 m = gfc_match_symbol (&sym, 0);
6de9cd9a
DN
4327 if (m == MATCH_ERROR)
4328 goto cleanup;
4329 if (m == MATCH_NO)
4330 goto syntax;
4331
8c6a85e3
TB
4332 /* Store a ref to the common block for error checking. */
4333 sym->common_block = t;
6f79f4d1 4334 sym->common_block->refs++;
8b704316 4335
8c6a85e3
TB
4336 /* See if we know the current common block is bind(c), and if
4337 so, then see if we can check if the symbol is (which it'll
4338 need to be). This can happen if the bind(c) attr stmt was
4339 applied to the common block, and the variable(s) already
4340 defined, before declaring the common block. */
4341 if (t->is_bind_c == 1)
4342 {
4343 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
4344 {
4345 /* If we find an error, just print it and continue,
4346 cause it's just semantic, and we can see if there
4347 are more errors. */
fea70c99
MLI
4348 gfc_error_now ("Variable %qs at %L in common block %qs "
4349 "at %C must be declared with a C "
4350 "interoperable kind since common block "
4351 "%qs is bind(c)",
4352 sym->name, &(sym->declared_at), t->name,
4353 t->name);
8c6a85e3 4354 }
8b704316 4355
8c6a85e3 4356 if (sym->attr.is_bind_c == 1)
4daa149b
TB
4357 gfc_error_now ("Variable %qs in common block %qs at %C can not "
4358 "be bind(c) since it is not global", sym->name,
4359 t->name);
8c6a85e3 4360 }
8b704316 4361
8c6a85e3 4362 if (sym->attr.in_common)
e8ec07e1 4363 {
c4100eae 4364 gfc_error ("Symbol %qs at %C is already in a COMMON block",
8c6a85e3 4365 sym->name);
e8ec07e1
PT
4366 goto cleanup;
4367 }
4368
8c6a85e3
TB
4369 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
4370 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
30aabb86 4371 {
a4d9b221 4372 if (!gfc_notify_std (GFC_STD_GNU, "Initialized symbol %qs at "
524af0d6
JB
4373 "%C can only be COMMON in BLOCK DATA",
4374 sym->name))
30aabb86 4375 goto cleanup;
8c6a85e3 4376 }
6de9cd9a 4377
524af0d6 4378 if (!gfc_add_in_common (&sym->attr, sym->name, NULL))
8c6a85e3 4379 goto cleanup;
6de9cd9a 4380
8c6a85e3
TB
4381 if (tail != NULL)
4382 tail->common_next = sym;
4383 else
4384 *head = sym;
6de9cd9a 4385
8c6a85e3 4386 tail = sym;
6de9cd9a 4387
8c6a85e3
TB
4388 /* Deal with an optional array specification after the
4389 symbol name. */
4390 m = gfc_match_array_spec (&as, true, true);
4391 if (m == MATCH_ERROR)
4392 goto cleanup;
6de9cd9a 4393
8c6a85e3
TB
4394 if (m == MATCH_YES)
4395 {
4396 if (as->type != AS_EXPLICIT)
4397 {
a4d9b221 4398 gfc_error ("Array specification for symbol %qs in COMMON "
8c6a85e3
TB
4399 "at %C must be explicit", sym->name);
4400 goto cleanup;
4401 }
b251af97 4402
524af0d6 4403 if (!gfc_add_dimension (&sym->attr, sym->name, NULL))
8c6a85e3 4404 goto cleanup;
d68bd5a8 4405
8c6a85e3
TB
4406 if (sym->attr.pointer)
4407 {
a4d9b221 4408 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
8c6a85e3
TB
4409 "POINTER array", sym->name);
4410 goto cleanup;
4411 }
4213f93b 4412
8c6a85e3
TB
4413 sym->as = as;
4414 as = NULL;
4213f93b 4415
8c6a85e3 4416 }
9081e356 4417
8c6a85e3 4418 sym->common_head = t;
4213f93b 4419
8c6a85e3
TB
4420 /* Check to see if the symbol is already in an equivalence group.
4421 If it is, set the other members as being in common. */
4422 if (sym->attr.in_equivalence)
4423 {
4424 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
4425 {
4426 for (e2 = e1; e2; e2 = e2->eq)
4427 if (e2->expr->symtree->n.sym == sym)
4428 goto equiv_found;
4213f93b 4429
8c6a85e3 4430 continue;
d68bd5a8 4431
8c6a85e3 4432 equiv_found:
4213f93b 4433
8c6a85e3
TB
4434 for (e2 = e1; e2; e2 = e2->eq)
4435 {
4436 other = e2->expr->symtree->n.sym;
4437 if (other->common_head
4438 && other->common_head != sym->common_head)
4439 {
a4d9b221 4440 gfc_error ("Symbol %qs, in COMMON block %qs at "
8c6a85e3 4441 "%C is being indirectly equivalenced to "
a4d9b221 4442 "another COMMON block %qs",
8c6a85e3
TB
4443 sym->name, sym->common_head->name,
4444 other->common_head->name);
4445 goto cleanup;
4446 }
4447 other->attr.in_common = 1;
4448 other->common_head = t;
4449 }
4450 }
4451 }
d68bd5a8 4452
4213f93b 4453
8c6a85e3
TB
4454 gfc_gobble_whitespace ();
4455 if (gfc_match_eos () == MATCH_YES)
4456 goto done;
4457 if (gfc_peek_ascii_char () == '/')
4458 break;
4459 if (gfc_match_char (',') != MATCH_YES)
4460 goto syntax;
4461 gfc_gobble_whitespace ();
4462 if (gfc_peek_ascii_char () == '/')
4463 break;
4464 }
4213f93b
PT
4465 }
4466
8c6a85e3
TB
4467done:
4468 return MATCH_YES;
4213f93b 4469
8c6a85e3
TB
4470syntax:
4471 gfc_syntax_error (ST_COMMON);
4213f93b 4472
8c6a85e3 4473cleanup:
8c6a85e3
TB
4474 gfc_free_array_spec (as);
4475 return MATCH_ERROR;
4213f93b
PT
4476}
4477
6de9cd9a 4478
8c6a85e3 4479/* Match a BLOCK DATA program unit. */
6de9cd9a
DN
4480
4481match
8c6a85e3 4482gfc_match_block_data (void)
6de9cd9a 4483{
8c6a85e3 4484 char name[GFC_MAX_SYMBOL_LEN + 1];
6de9cd9a 4485 gfc_symbol *sym;
6de9cd9a
DN
4486 match m;
4487
8c6a85e3
TB
4488 if (gfc_match_eos () == MATCH_YES)
4489 {
4490 gfc_new_block = NULL;
4491 return MATCH_YES;
4492 }
4493
4494 m = gfc_match ("% %n%t", name);
6de9cd9a 4495 if (m != MATCH_YES)
8c6a85e3 4496 return MATCH_ERROR;
6de9cd9a 4497
8c6a85e3
TB
4498 if (gfc_get_symbol (name, NULL, &sym))
4499 return MATCH_ERROR;
6de9cd9a 4500
524af0d6 4501 if (!gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL))
8c6a85e3 4502 return MATCH_ERROR;
6de9cd9a 4503
8c6a85e3 4504 gfc_new_block = sym;
6de9cd9a 4505
8c6a85e3
TB
4506 return MATCH_YES;
4507}
d71b89ca 4508
6de9cd9a 4509
8c6a85e3 4510/* Free a namelist structure. */
4213f93b 4511
8c6a85e3
TB
4512void
4513gfc_free_namelist (gfc_namelist *name)
4514{
4515 gfc_namelist *n;
6de9cd9a 4516
8c6a85e3
TB
4517 for (; name; name = n)
4518 {
4519 n = name->next;
4520 free (name);
4521 }
4522}
e2ab8b09 4523
6de9cd9a 4524
dd2fc525
JJ
4525/* Free an OpenMP namelist structure. */
4526
4527void
4528gfc_free_omp_namelist (gfc_omp_namelist *name)
4529{
4530 gfc_omp_namelist *n;
4531
4532 for (; name; name = n)
4533 {
4534 gfc_free_expr (name->expr);
b46ebd6c
JJ
4535 if (name->udr)
4536 {
4537 if (name->udr->combiner)
4538 gfc_free_statement (name->udr->combiner);
4539 if (name->udr->initializer)
4540 gfc_free_statement (name->udr->initializer);
4541 free (name->udr);
4542 }
dd2fc525
JJ
4543 n = name->next;
4544 free (name);
4545 }
4546}
4547
4548
8c6a85e3 4549/* Match a NAMELIST statement. */
6de9cd9a 4550
8c6a85e3
TB
4551match
4552gfc_match_namelist (void)
4553{
4554 gfc_symbol *group_name, *sym;
4555 gfc_namelist *nl;
4556 match m, m2;
6de9cd9a 4557
8c6a85e3
TB
4558 m = gfc_match (" / %s /", &group_name);
4559 if (m == MATCH_NO)
4560 goto syntax;
4561 if (m == MATCH_ERROR)
4562 goto error;
6de9cd9a 4563
8c6a85e3
TB
4564 for (;;)
4565 {
4566 if (group_name->ts.type != BT_UNKNOWN)
4567 {
a4d9b221 4568 gfc_error ("Namelist group name %qs at %C already has a basic "
8c6a85e3
TB
4569 "type of %s", group_name->name,
4570 gfc_typename (&group_name->ts));
4571 return MATCH_ERROR;
4572 }
6de9cd9a 4573
8c6a85e3
TB
4574 if (group_name->attr.flavor == FL_NAMELIST
4575 && group_name->attr.use_assoc
a4d9b221 4576 && !gfc_notify_std (GFC_STD_GNU, "Namelist group name %qs "
524af0d6
JB
4577 "at %C already is USE associated and can"
4578 "not be respecified.", group_name->name))
8c6a85e3 4579 return MATCH_ERROR;
6de9cd9a 4580
8c6a85e3 4581 if (group_name->attr.flavor != FL_NAMELIST
524af0d6
JB
4582 && !gfc_add_flavor (&group_name->attr, FL_NAMELIST,
4583 group_name->name, NULL))
8c6a85e3 4584 return MATCH_ERROR;
6de9cd9a 4585
8c6a85e3
TB
4586 for (;;)
4587 {
4588 m = gfc_match_symbol (&sym, 1);
4589 if (m == MATCH_NO)
4590 goto syntax;
4591 if (m == MATCH_ERROR)
4592 goto error;
6de9cd9a 4593
8c6a85e3 4594 if (sym->attr.in_namelist == 0
524af0d6 4595 && !gfc_add_in_namelist (&sym->attr, sym->name, NULL))
8c6a85e3 4596 goto error;
6de9cd9a 4597
8c6a85e3
TB
4598 /* Use gfc_error_check here, rather than goto error, so that
4599 these are the only errors for the next two lines. */
4600 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
4601 {
a4d9b221 4602 gfc_error ("Assumed size array %qs in namelist %qs at "
8c6a85e3
TB
4603 "%C is not allowed", sym->name, group_name->name);
4604 gfc_error_check ();
4605 }
6de9cd9a 4606
8c6a85e3
TB
4607 nl = gfc_get_namelist ();
4608 nl->sym = sym;
4609 sym->refs++;
6de9cd9a 4610
8c6a85e3
TB
4611 if (group_name->namelist == NULL)
4612 group_name->namelist = group_name->namelist_tail = nl;
4613 else
4614 {
4615 group_name->namelist_tail->next = nl;
4616 group_name->namelist_tail = nl;
4617 }
6de9cd9a 4618
8c6a85e3
TB
4619 if (gfc_match_eos () == MATCH_YES)
4620 goto done;
6de9cd9a 4621
8c6a85e3 4622 m = gfc_match_char (',');
6de9cd9a 4623
8c6a85e3
TB
4624 if (gfc_match_char ('/') == MATCH_YES)
4625 {
4626 m2 = gfc_match (" %s /", &group_name);
4627 if (m2 == MATCH_YES)
4628 break;
4629 if (m2 == MATCH_ERROR)
4630 goto error;
4631 goto syntax;
4632 }
6de9cd9a 4633
8c6a85e3
TB
4634 if (m != MATCH_YES)
4635 goto syntax;
6de9cd9a
DN
4636 }
4637 }
4638
8c6a85e3 4639done:
6de9cd9a
DN
4640 return MATCH_YES;
4641
8c6a85e3
TB
4642syntax:
4643 gfc_syntax_error (ST_NAMELIST);
6de9cd9a 4644
8c6a85e3 4645error:
6de9cd9a
DN
4646 return MATCH_ERROR;
4647}
4648
4649
8c6a85e3 4650/* Match a MODULE statement. */
6de9cd9a 4651
8c6a85e3
TB
4652match
4653gfc_match_module (void)
6de9cd9a 4654{
6de9cd9a
DN
4655 match m;
4656
8c6a85e3 4657 m = gfc_match (" %s%t", &gfc_new_block);
6de9cd9a
DN
4658 if (m != MATCH_YES)
4659 return m;
4660
524af0d6
JB
4661 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
4662 gfc_new_block->name, NULL))
8c6a85e3 4663 return MATCH_ERROR;
6de9cd9a 4664
8c6a85e3 4665 return MATCH_YES;
6de9cd9a
DN
4666}
4667
4668
8c6a85e3
TB
4669/* Free equivalence sets and lists. Recursively is the easiest way to
4670 do this. */
6de9cd9a 4671
8c6a85e3
TB
4672void
4673gfc_free_equiv_until (gfc_equiv *eq, gfc_equiv *stop)
6de9cd9a 4674{
8c6a85e3
TB
4675 if (eq == stop)
4676 return;
6de9cd9a 4677
8c6a85e3
TB
4678 gfc_free_equiv (eq->eq);
4679 gfc_free_equiv_until (eq->next, stop);
4680 gfc_free_expr (eq->expr);
4681 free (eq);
4682}
6de9cd9a 4683
6de9cd9a 4684
8c6a85e3
TB
4685void
4686gfc_free_equiv (gfc_equiv *eq)
4687{
4688 gfc_free_equiv_until (eq, NULL);
6de9cd9a
DN
4689}
4690
4691
8c6a85e3 4692/* Match an EQUIVALENCE statement. */
7431bf06 4693
8c6a85e3
TB
4694match
4695gfc_match_equivalence (void)
7431bf06 4696{
8c6a85e3
TB
4697 gfc_equiv *eq, *set, *tail;
4698 gfc_ref *ref;
4699 gfc_symbol *sym;
4700 match m;
4701 gfc_common_head *common_head = NULL;
4702 bool common_flag;
4703 int cnt;
7431bf06 4704
8c6a85e3 4705 tail = NULL;
7431bf06 4706
8c6a85e3
TB
4707 for (;;)
4708 {
4709 eq = gfc_get_equiv ();
4710 if (tail == NULL)
4711 tail = eq;
7431bf06 4712
8c6a85e3
TB
4713 eq->next = gfc_current_ns->equiv;
4714 gfc_current_ns->equiv = eq;
7431bf06 4715
8c6a85e3
TB
4716 if (gfc_match_char ('(') != MATCH_YES)
4717 goto syntax;
7431bf06 4718
8c6a85e3
TB
4719 set = eq;
4720 common_flag = FALSE;
4721 cnt = 0;
7431bf06 4722
8c6a85e3
TB
4723 for (;;)
4724 {
4725 m = gfc_match_equiv_variable (&set->expr);
4726 if (m == MATCH_ERROR)
4727 goto cleanup;
4728 if (m == MATCH_NO)
4729 goto syntax;
3e78238a 4730
8c6a85e3
TB
4731 /* count the number of objects. */
4732 cnt++;
4733
4734 if (gfc_match_char ('%') == MATCH_YES)
4735 {
4736 gfc_error ("Derived type component %C is not a "
4737 "permitted EQUIVALENCE member");
4738 goto cleanup;
4739 }
7431bf06 4740
8c6a85e3
TB
4741 for (ref = set->expr->ref; ref; ref = ref->next)
4742 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
4743 {
4744 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
4745 "be an array section");
4746 goto cleanup;
4747 }
7431bf06 4748
8c6a85e3 4749 sym = set->expr->symtree->n.sym;
cf2b3c22 4750
524af0d6 4751 if (!gfc_add_in_equivalence (&sym->attr, sym->name, NULL))
8c6a85e3 4752 goto cleanup;
cf2b3c22 4753
8c6a85e3
TB
4754 if (sym->attr.in_common)
4755 {
4756 common_flag = TRUE;
4757 common_head = sym->common_head;
4758 }
4759
4760 if (gfc_match_char (')') == MATCH_YES)
4761 break;
cf2b3c22 4762
8c6a85e3
TB
4763 if (gfc_match_char (',') != MATCH_YES)
4764 goto syntax;
cf2b3c22 4765
8c6a85e3
TB
4766 set->eq = gfc_get_equiv ();
4767 set = set->eq;
4768 }
93d76687 4769
8c6a85e3 4770 if (cnt < 2)
bc382218 4771 {
8c6a85e3 4772 gfc_error ("EQUIVALENCE at %C requires two or more objects");
bc382218
JW
4773 goto cleanup;
4774 }
cf2b3c22 4775
8c6a85e3
TB
4776 /* If one of the members of an equivalence is in common, then
4777 mark them all as being in common. Before doing this, check
4778 that members of the equivalence group are not in different
4779 common blocks. */
4780 if (common_flag)
4781 for (set = eq; set; set = set->eq)
4782 {
4783 sym = set->expr->symtree->n.sym;
4784 if (sym->common_head && sym->common_head != common_head)
4785 {
4786 gfc_error ("Attempt to indirectly overlap COMMON "
4787 "blocks %s and %s by EQUIVALENCE at %C",
4788 sym->common_head->name, common_head->name);
4789 goto cleanup;
4790 }
4791 sym->attr.in_common = 1;
4792 sym->common_head = common_head;
4793 }
cf2b3c22 4794
8c6a85e3
TB
4795 if (gfc_match_eos () == MATCH_YES)
4796 break;
4797 if (gfc_match_char (',') != MATCH_YES)
4798 {
4799 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
4800 goto cleanup;
4801 }
cf2b3c22
TB
4802 }
4803
8c6a85e3 4804 return MATCH_YES;
cf2b3c22 4805
8c6a85e3
TB
4806syntax:
4807 gfc_syntax_error (ST_EQUIVALENCE);
cf2b3c22 4808
bc382218 4809cleanup:
8c6a85e3
TB
4810 eq = tail->next;
4811 tail->next = NULL;
4812
4813 gfc_free_equiv (gfc_current_ns->equiv);
4814 gfc_current_ns->equiv = eq;
4815
4816 return MATCH_ERROR;
cf2b3c22
TB
4817}
4818
4819
8c6a85e3
TB
4820/* Check that a statement function is not recursive. This is done by looking
4821 for the statement function symbol(sym) by looking recursively through its
8b704316 4822 expression(e). If a reference to sym is found, true is returned.
8c6a85e3
TB
4823 12.5.4 requires that any variable of function that is implicitly typed
4824 shall have that type confirmed by any subsequent type declaration. The
4825 implicit typing is conveniently done here. */
4826static bool
4827recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
6de9cd9a 4828
8c6a85e3
TB
4829static bool
4830check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
6de9cd9a 4831{
6de9cd9a 4832
8c6a85e3
TB
4833 if (e == NULL)
4834 return false;
6de9cd9a 4835
8c6a85e3 4836 switch (e->expr_type)
6de9cd9a 4837 {
8c6a85e3
TB
4838 case EXPR_FUNCTION:
4839 if (e->symtree == NULL)
4840 return false;
6de9cd9a 4841
8c6a85e3
TB
4842 /* Check the name before testing for nested recursion! */
4843 if (sym->name == e->symtree->n.sym->name)
4844 return true;
6de9cd9a 4845
8c6a85e3
TB
4846 /* Catch recursion via other statement functions. */
4847 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
4848 && e->symtree->n.sym->value
4849 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
4850 return true;
6de9cd9a 4851
8c6a85e3
TB
4852 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
4853 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
6de9cd9a 4854
8c6a85e3 4855 break;
6de9cd9a 4856
8c6a85e3
TB
4857 case EXPR_VARIABLE:
4858 if (e->symtree && sym->name == e->symtree->n.sym->name)
4859 return true;
6de9cd9a 4860
8c6a85e3
TB
4861 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
4862 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
4863 break;
6de9cd9a 4864
8c6a85e3
TB
4865 default:
4866 break;
6de9cd9a
DN
4867 }
4868
8c6a85e3
TB
4869 return false;
4870}
6de9cd9a 4871
6de9cd9a 4872
8c6a85e3
TB
4873static bool
4874recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
4875{
4876 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
6de9cd9a
DN
4877}
4878
cf2b3c22 4879
8c6a85e3
TB
4880/* Match a statement function declaration. It is so easy to match
4881 non-statement function statements with a MATCH_ERROR as opposed to
4882 MATCH_NO that we suppress error message in most cases. */
cf2b3c22
TB
4883
4884match
8c6a85e3 4885gfc_match_st_function (void)
cf2b3c22 4886{
fea70c99 4887 gfc_error_buffer old_error;
c4100eae 4888
8c6a85e3
TB
4889 gfc_symbol *sym;
4890 gfc_expr *expr;
cf2b3c22 4891 match m;
cf2b3c22 4892
8c6a85e3
TB
4893 m = gfc_match_symbol (&sym, 0);
4894 if (m != MATCH_YES)
4895 return m;
4896
fea70c99 4897 gfc_push_error (&old_error);
8c6a85e3 4898
524af0d6 4899 if (!gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION, sym->name, NULL))
8c6a85e3
TB
4900 goto undo_error;
4901
4902 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
4903 goto undo_error;
4904
4905 m = gfc_match (" = %e%t", &expr);
4906 if (m == MATCH_NO)
4907 goto undo_error;
4908
fea70c99 4909 gfc_free_error (&old_error);
c4100eae 4910
8c6a85e3
TB
4911 if (m == MATCH_ERROR)
4912 return m;
4913
4914 if (recursive_stmt_fcn (expr, sym))
cf2b3c22 4915 {
8c6a85e3 4916 gfc_error ("Statement function at %L is recursive", &expr->where);
cf2b3c22
TB
4917 return MATCH_ERROR;
4918 }
4919
8c6a85e3 4920 sym->value = expr;
cf2b3c22 4921
524af0d6 4922 if (!gfc_notify_std (GFC_STD_F95_OBS, "Statement function at %C"))
8c6a85e3 4923 return MATCH_ERROR;
cf2b3c22 4924
8c6a85e3 4925 return MATCH_YES;
cf2b3c22 4926
8c6a85e3 4927undo_error:
fea70c99 4928 gfc_pop_error (&old_error);
8c6a85e3
TB
4929 return MATCH_NO;
4930}
cf2b3c22 4931
cf2b3c22 4932
8c6a85e3
TB
4933/***************** SELECT CASE subroutines ******************/
4934
4935/* Free a single case structure. */
4936
4937static void
4938free_case (gfc_case *p)
4939{
4940 if (p->low == p->high)
4941 p->high = NULL;
4942 gfc_free_expr (p->low);
4943 gfc_free_expr (p->high);
4944 free (p);
4945}
cf2b3c22 4946
cf2b3c22 4947
8c6a85e3 4948/* Free a list of case structures. */
cf2b3c22 4949
8c6a85e3
TB
4950void
4951gfc_free_case_list (gfc_case *p)
4952{
4953 gfc_case *q;
cf2b3c22 4954
8c6a85e3
TB
4955 for (; p; p = q)
4956 {
4957 q = p->next;
4958 free_case (p);
4959 }
cf2b3c22
TB
4960}
4961
4962
8c6a85e3 4963/* Match a single case selector. */
cf2b3c22 4964
8c6a85e3
TB
4965static match
4966match_case_selector (gfc_case **cp)
cf2b3c22 4967{
8c6a85e3 4968 gfc_case *c;
cf2b3c22
TB
4969 match m;
4970
8c6a85e3
TB
4971 c = gfc_get_case ();
4972 c->where = gfc_current_locus;
cf2b3c22 4973
8c6a85e3 4974 if (gfc_match_char (':') == MATCH_YES)
cf2b3c22 4975 {
8c6a85e3 4976 m = gfc_match_init_expr (&c->high);
cf2b3c22 4977 if (m == MATCH_NO)
8c6a85e3
TB
4978 goto need_expr;
4979 if (m == MATCH_ERROR)
4980 goto cleanup;
4981 }
4982 else
4983 {
4984 m = gfc_match_init_expr (&c->low);
cf2b3c22
TB
4985 if (m == MATCH_ERROR)
4986 goto cleanup;
8c6a85e3
TB
4987 if (m == MATCH_NO)
4988 goto need_expr;
cf2b3c22 4989
8c6a85e3
TB
4990 /* If we're not looking at a ':' now, make a range out of a single
4991 target. Else get the upper bound for the case range. */
4992 if (gfc_match_char (':') != MATCH_YES)
4993 c->high = c->low;
4994 else
4995 {
4996 m = gfc_match_init_expr (&c->high);
4997 if (m == MATCH_ERROR)
4998 goto cleanup;
4999 /* MATCH_NO is fine. It's OK if nothing is there! */
5000 }
cf2b3c22
TB
5001 }
5002
8c6a85e3
TB
5003 *cp = c;
5004 return MATCH_YES;
cf2b3c22 5005
8c6a85e3
TB
5006need_expr:
5007 gfc_error ("Expected initialization expression in CASE at %C");
cf2b3c22 5008
8c6a85e3
TB
5009cleanup:
5010 free_case (c);
5011 return MATCH_ERROR;
5012}
cf2b3c22 5013
cf2b3c22 5014
8c6a85e3 5015/* Match the end of a case statement. */
cf2b3c22 5016
8c6a85e3
TB
5017static match
5018match_case_eos (void)
5019{
5020 char name[GFC_MAX_SYMBOL_LEN + 1];
5021 match m;
cf2b3c22 5022
8c6a85e3
TB
5023 if (gfc_match_eos () == MATCH_YES)
5024 return MATCH_YES;
cf2b3c22 5025
8c6a85e3
TB
5026 /* If the case construct doesn't have a case-construct-name, we
5027 should have matched the EOS. */
5028 if (!gfc_current_block ())
5029 return MATCH_NO;
cf2b3c22 5030
8c6a85e3 5031 gfc_gobble_whitespace ();
cf2b3c22 5032
8c6a85e3
TB
5033 m = gfc_match_name (name);
5034 if (m != MATCH_YES)
5035 return m;
cf2b3c22 5036
8c6a85e3
TB
5037 if (strcmp (name, gfc_current_block ()->name) != 0)
5038 {
a4d9b221 5039 gfc_error ("Expected block name %qs of SELECT construct at %C",
8c6a85e3
TB
5040 gfc_current_block ()->name);
5041 return MATCH_ERROR;
5042 }
cf2b3c22 5043
8c6a85e3
TB
5044 return gfc_match_eos ();
5045}
cf2b3c22 5046
6de9cd9a 5047
8c6a85e3 5048/* Match a SELECT statement. */
c874ae73 5049
8c6a85e3
TB
5050match
5051gfc_match_select (void)
c874ae73
TS
5052{
5053 gfc_expr *expr;
c874ae73
TS
5054 match m;
5055
8c6a85e3
TB
5056 m = gfc_match_label ();
5057 if (m == MATCH_ERROR)
5058 return m;
5059
5060 m = gfc_match (" select case ( %e )%t", &expr);
c874ae73
TS
5061 if (m != MATCH_YES)
5062 return m;
5063
8c6a85e3
TB
5064 new_st.op = EXEC_SELECT;
5065 new_st.expr1 = expr;
c874ae73 5066
8c6a85e3
TB
5067 return MATCH_YES;
5068}
c874ae73 5069
c874ae73 5070
8f75db9f
PT
5071/* Transfer the selector typespec to the associate name. */
5072
5073static void
5074copy_ts_from_selector_to_associate (gfc_expr *associate, gfc_expr *selector)
5075{
5076 gfc_ref *ref;
5077 gfc_symbol *assoc_sym;
5078
5079 assoc_sym = associate->symtree->n.sym;
5080
8f75db9f
PT
5081 /* At this stage the expression rank and arrayspec dimensions have
5082 not been completely sorted out. We must get the expr2->rank
5083 right here, so that the correct class container is obtained. */
5084 ref = selector->ref;
5085 while (ref && ref->next)
5086 ref = ref->next;
5087
a7a6a027
JW
5088 if (selector->ts.type == BT_CLASS && CLASS_DATA (selector)->as
5089 && ref && ref->type == REF_ARRAY)
8f75db9f 5090 {
e4821cd8
PT
5091 /* Ensure that the array reference type is set. We cannot use
5092 gfc_resolve_expr at this point, so the usable parts of
5093 resolve.c(resolve_array_ref) are employed to do it. */
5094 if (ref->u.ar.type == AR_UNKNOWN)
5095 {
5096 ref->u.ar.type = AR_ELEMENT;
a7a6a027 5097 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
e4821cd8
PT
5098 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5099 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR
5100 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
5101 && ref->u.ar.start[i] && ref->u.ar.start[i]->rank))
5102 {
5103 ref->u.ar.type = AR_SECTION;
5104 break;
5105 }
5106 }
5107
8f75db9f
PT
5108 if (ref->u.ar.type == AR_FULL)
5109 selector->rank = CLASS_DATA (selector)->as->rank;
5110 else if (ref->u.ar.type == AR_SECTION)
5111 selector->rank = ref->u.ar.dimen;
5112 else
5113 selector->rank = 0;
5114 }
5115
a7a6a027 5116 if (selector->rank)
8f75db9f 5117 {
a7a6a027
JW
5118 assoc_sym->attr.dimension = 1;
5119 assoc_sym->as = gfc_get_array_spec ();
5120 assoc_sym->as->rank = selector->rank;
5121 assoc_sym->as->type = AS_DEFERRED;
8f75db9f
PT
5122 }
5123 else
a7a6a027
JW
5124 assoc_sym->as = NULL;
5125
5126 if (selector->ts.type == BT_CLASS)
8f75db9f
PT
5127 {
5128 /* The correct class container has to be available. */
8f75db9f
PT
5129 assoc_sym->ts.type = BT_CLASS;
5130 assoc_sym->ts.u.derived = CLASS_DATA (selector)->ts.u.derived;
5131 assoc_sym->attr.pointer = 1;
9b6da3c7 5132 gfc_build_class_symbol (&assoc_sym->ts, &assoc_sym->attr, &assoc_sym->as);
8f75db9f
PT
5133 }
5134}
5135
5136
8c6a85e3 5137/* Push the current selector onto the SELECT TYPE stack. */
c874ae73 5138
8c6a85e3
TB
5139static void
5140select_type_push (gfc_symbol *sel)
5141{
5142 gfc_select_type_stack *top = gfc_get_select_type_stack ();
5143 top->selector = sel;
5144 top->tmp = NULL;
5145 top->prev = select_type_stack;
c874ae73 5146
8c6a85e3
TB
5147 select_type_stack = top;
5148}
c874ae73 5149
c874ae73 5150
8b704316
PT
5151/* Set the temporary for the current intrinsic SELECT TYPE selector. */
5152
5153static gfc_symtree *
5154select_intrinsic_set_tmp (gfc_typespec *ts)
5155{
5156 char name[GFC_MAX_SYMBOL_LEN];
5157 gfc_symtree *tmp;
5158 int charlen = 0;
5159
5160 if (ts->type == BT_CLASS || ts->type == BT_DERIVED)
5161 return NULL;
5162
5163 if (select_type_stack->selector->ts.type == BT_CLASS
5164 && !select_type_stack->selector->attr.class_ok)
5165 return NULL;
5166
5167 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length
5168 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
5169 charlen = mpz_get_si (ts->u.cl->length->value.integer);
5170
5171 if (ts->type != BT_CHARACTER)
5172 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (ts->type),
5173 ts->kind);
5174 else
5175 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (ts->type),
5176 charlen, ts->kind);
5177
5178 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
5179 gfc_add_type (tmp->n.sym, ts, NULL);
5180
5181 /* Copy across the array spec to the selector. */
5182 if (select_type_stack->selector->ts.type == BT_CLASS
5183 && (CLASS_DATA (select_type_stack->selector)->attr.dimension
5184 || CLASS_DATA (select_type_stack->selector)->attr.codimension))
5185 {
5186 tmp->n.sym->attr.pointer = 1;
5187 tmp->n.sym->attr.dimension
5188 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
5189 tmp->n.sym->attr.codimension
5190 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
5191 tmp->n.sym->as
5192 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
5193 }
5194
5195 gfc_set_sym_referenced (tmp->n.sym);
5196 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
5197 tmp->n.sym->attr.select_type_temporary = 1;
5198
5199 return tmp;
5200}
5201
5202
fca04db3 5203/* Set up a temporary for the current TYPE IS / CLASS IS branch . */
c874ae73 5204
fca04db3
JW
5205static void
5206select_type_set_tmp (gfc_typespec *ts)
8c6a85e3
TB
5207{
5208 char name[GFC_MAX_SYMBOL_LEN];
8b704316 5209 gfc_symtree *tmp = NULL;
8f75db9f 5210
fca04db3 5211 if (!ts)
8c6a85e3 5212 {
fca04db3
JW
5213 select_type_stack->tmp = NULL;
5214 return;
8c6a85e3 5215 }
8c6a85e3 5216
8b704316 5217 tmp = select_intrinsic_set_tmp (ts);
c49ea23d 5218
8b704316 5219 if (tmp == NULL)
c49ea23d 5220 {
4cc70466
PT
5221 if (!ts->u.derived)
5222 return;
5223
8b704316
PT
5224 if (ts->type == BT_CLASS)
5225 sprintf (name, "__tmp_class_%s", ts->u.derived->name);
5226 else
5227 sprintf (name, "__tmp_type_%s", ts->u.derived->name);
5228 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
5229 gfc_add_type (tmp->n.sym, ts, NULL);
fca04db3 5230
8b704316
PT
5231 if (select_type_stack->selector->ts.type == BT_CLASS
5232 && select_type_stack->selector->attr.class_ok)
fca04db3 5233 {
8b704316
PT
5234 tmp->n.sym->attr.pointer
5235 = CLASS_DATA (select_type_stack->selector)->attr.class_pointer;
5236
5237 /* Copy across the array spec to the selector. */
5238 if (CLASS_DATA (select_type_stack->selector)->attr.dimension
5239 || CLASS_DATA (select_type_stack->selector)->attr.codimension)
5240 {
5241 tmp->n.sym->attr.dimension
fca04db3 5242 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
8b704316 5243 tmp->n.sym->attr.codimension
fca04db3 5244 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
8b704316 5245 tmp->n.sym->as
fca04db3 5246 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
8b704316 5247 }
c49ea23d
PT
5248 }
5249
8c6a85e3 5250 gfc_set_sym_referenced (tmp->n.sym);
8c6a85e3 5251 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
7d40e49f 5252 tmp->n.sym->attr.select_type_temporary = 1;
8f75db9f 5253
8c6a85e3 5254 if (ts->type == BT_CLASS)
fca04db3 5255 gfc_build_class_symbol (&tmp->n.sym->ts, &tmp->n.sym->attr,
9b6da3c7 5256 &tmp->n.sym->as);
8b704316 5257 }
8c6a85e3
TB
5258
5259 /* Add an association for it, so the rest of the parser knows it is
5260 an associate-name. The target will be set during resolution. */
5261 tmp->n.sym->assoc = gfc_get_association_list ();
5262 tmp->n.sym->assoc->dangling = 1;
5263 tmp->n.sym->assoc->st = tmp;
5264
5265 select_type_stack->tmp = tmp;
c874ae73
TS
5266}
5267
8b704316 5268
8c6a85e3 5269/* Match a SELECT TYPE statement. */
6de9cd9a
DN
5270
5271match
8c6a85e3 5272gfc_match_select_type (void)
6de9cd9a 5273{
8c6a85e3
TB
5274 gfc_expr *expr1, *expr2 = NULL;
5275 match m;
5276 char name[GFC_MAX_SYMBOL_LEN];
c49ea23d 5277 bool class_array;
8f75db9f 5278 gfc_symbol *sym;
6de9cd9a 5279
8c6a85e3
TB
5280 m = gfc_match_label ();
5281 if (m == MATCH_ERROR)
5282 return m;
6de9cd9a 5283
8c6a85e3 5284 m = gfc_match (" select type ( ");
6de9cd9a
DN
5285 if (m != MATCH_YES)
5286 return m;
5287
8c6a85e3
TB
5288 m = gfc_match (" %n => %e", name, &expr2);
5289 if (m == MATCH_YES)
5290 {
5291 expr1 = gfc_get_expr();
5292 expr1->expr_type = EXPR_VARIABLE;
5293 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
5294 {
5295 m = MATCH_ERROR;
5296 goto cleanup;
5297 }
8f75db9f
PT
5298
5299 sym = expr1->symtree->n.sym;
8c6a85e3 5300 if (expr2->ts.type == BT_UNKNOWN)
8f75db9f 5301 sym->attr.untyped = 1;
8c6a85e3 5302 else
8f75db9f
PT
5303 copy_ts_from_selector_to_associate (expr1, expr2);
5304
5305 sym->attr.flavor = FL_VARIABLE;
5306 sym->attr.referenced = 1;
5307 sym->attr.class_ok = 1;
8c6a85e3
TB
5308 }
5309 else
6de9cd9a 5310 {
8c6a85e3
TB
5311 m = gfc_match (" %e ", &expr1);
5312 if (m != MATCH_YES)
36abe895 5313 return m;
6de9cd9a
DN
5314 }
5315
8c6a85e3 5316 m = gfc_match (" )%t");
6de9cd9a 5317 if (m != MATCH_YES)
a5e52264
MM
5318 {
5319 gfc_error ("parse error in SELECT TYPE statement at %C");
5320 goto cleanup;
5321 }
8c6a85e3 5322
c49ea23d
PT
5323 /* This ghastly expression seems to be needed to distinguish a CLASS
5324 array, which can have a reference, from other expressions that
5325 have references, such as derived type components, and are not
5326 allowed by the standard.
ee3bea0b 5327 TODO: see if it is sufficient to exclude component and substring
c49ea23d
PT
5328 references. */
5329 class_array = expr1->expr_type == EXPR_VARIABLE
ee3bea0b 5330 && expr1->ts.type == BT_CLASS
c49ea23d
PT
5331 && CLASS_DATA (expr1)
5332 && (strcmp (CLASS_DATA (expr1)->name, "_data") == 0)
cd99c23c
TB
5333 && (CLASS_DATA (expr1)->attr.dimension
5334 || CLASS_DATA (expr1)->attr.codimension)
c49ea23d
PT
5335 && expr1->ref
5336 && expr1->ref->type == REF_ARRAY
5337 && expr1->ref->next == NULL;
5338
8c6a85e3 5339 /* Check for F03:C811. */
c49ea23d
PT
5340 if (!expr2 && (expr1->expr_type != EXPR_VARIABLE
5341 || (!class_array && expr1->ref != NULL)))
6de9cd9a 5342 {
8c6a85e3
TB
5343 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
5344 "use associate-name=>");
5345 m = MATCH_ERROR;
5346 goto cleanup;
6de9cd9a
DN
5347 }
5348
8c6a85e3
TB
5349 new_st.op = EXEC_SELECT_TYPE;
5350 new_st.expr1 = expr1;
5351 new_st.expr2 = expr2;
5352 new_st.ext.block.ns = gfc_current_ns;
6de9cd9a 5353
8c6a85e3 5354 select_type_push (expr1->symtree->n.sym);
6de9cd9a
DN
5355
5356 return MATCH_YES;
8b704316 5357
8c6a85e3 5358cleanup:
36abe895
TB
5359 gfc_free_expr (expr1);
5360 gfc_free_expr (expr2);
8c6a85e3 5361 return m;
6de9cd9a
DN
5362}
5363
5364
8c6a85e3 5365/* Match a CASE statement. */
6de9cd9a
DN
5366
5367match
8c6a85e3 5368gfc_match_case (void)
6de9cd9a 5369{
8c6a85e3 5370 gfc_case *c, *head, *tail;
6de9cd9a
DN
5371 match m;
5372
8c6a85e3
TB
5373 head = tail = NULL;
5374
5375 if (gfc_current_state () != COMP_SELECT)
6de9cd9a 5376 {
8c6a85e3 5377 gfc_error ("Unexpected CASE statement at %C");
6de9cd9a
DN
5378 return MATCH_ERROR;
5379 }
5380
8c6a85e3 5381 if (gfc_match ("% default") == MATCH_YES)
6de9cd9a 5382 {
8c6a85e3 5383 m = match_case_eos ();
6de9cd9a
DN
5384 if (m == MATCH_NO)
5385 goto syntax;
5386 if (m == MATCH_ERROR)
8c6a85e3 5387 goto cleanup;
6de9cd9a 5388
8c6a85e3
TB
5389 new_st.op = EXEC_SELECT;
5390 c = gfc_get_case ();
5391 c->where = gfc_current_locus;
5392 new_st.ext.block.case_list = c;
5393 return MATCH_YES;
6de9cd9a
DN
5394 }
5395
8c6a85e3
TB
5396 if (gfc_match_char ('(') != MATCH_YES)
5397 goto syntax;
5398
5399 for (;;)
690af379 5400 {
8c6a85e3 5401 if (match_case_selector (&c) == MATCH_ERROR)
6de9cd9a
DN
5402 goto cleanup;
5403
8c6a85e3
TB
5404 if (head == NULL)
5405 head = c;
5406 else
5407 tail->next = c;
6de9cd9a 5408
8c6a85e3
TB
5409 tail = c;
5410
5411 if (gfc_match_char (')') == MATCH_YES)
5412 break;
5413 if (gfc_match_char (',') != MATCH_YES)
5414 goto syntax;
6de9cd9a
DN
5415 }
5416
8c6a85e3
TB
5417 m = match_case_eos ();
5418 if (m == MATCH_NO)
5419 goto syntax;
5420 if (m == MATCH_ERROR)
5421 goto cleanup;
5422
5423 new_st.op = EXEC_SELECT;
5424 new_st.ext.block.case_list = head;
5425
6de9cd9a
DN
5426 return MATCH_YES;
5427
5428syntax:
8c6a85e3 5429 gfc_error ("Syntax error in CASE specification at %C");
6de9cd9a
DN
5430
5431cleanup:
8c6a85e3 5432 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
6de9cd9a
DN
5433 return MATCH_ERROR;
5434}
5435
5436
8c6a85e3 5437/* Match a TYPE IS statement. */
6de9cd9a 5438
8c6a85e3
TB
5439match
5440gfc_match_type_is (void)
6de9cd9a 5441{
8c6a85e3
TB
5442 gfc_case *c = NULL;
5443 match m;
6de9cd9a 5444
8c6a85e3 5445 if (gfc_current_state () != COMP_SELECT_TYPE)
6de9cd9a 5446 {
8c6a85e3
TB
5447 gfc_error ("Unexpected TYPE IS statement at %C");
5448 return MATCH_ERROR;
6de9cd9a 5449 }
6de9cd9a 5450
8c6a85e3
TB
5451 if (gfc_match_char ('(') != MATCH_YES)
5452 goto syntax;
6de9cd9a 5453
8c6a85e3
TB
5454 c = gfc_get_case ();
5455 c->where = gfc_current_locus;
6de9cd9a 5456
ef701bbe
SK
5457 m = gfc_match_type_spec (&c->ts);
5458 if (m == MATCH_NO)
5459 goto syntax;
5460 if (m == MATCH_ERROR)
6de9cd9a
DN
5461 goto cleanup;
5462
8c6a85e3 5463 if (gfc_match_char (')') != MATCH_YES)
6de9cd9a
DN
5464 goto syntax;
5465
8c6a85e3 5466 m = match_case_eos ();
6de9cd9a
DN
5467 if (m == MATCH_NO)
5468 goto syntax;
5469 if (m == MATCH_ERROR)
5470 goto cleanup;
5471
8c6a85e3
TB
5472 new_st.op = EXEC_SELECT_TYPE;
5473 new_st.ext.block.case_list = c;
6de9cd9a 5474
8b704316
PT
5475 if (c->ts.type == BT_DERIVED && c->ts.u.derived
5476 && (c->ts.u.derived->attr.sequence
5477 || c->ts.u.derived->attr.is_bind_c))
5478 {
5479 gfc_error ("The type-spec shall not specify a sequence derived "
5480 "type or a type with the BIND attribute in SELECT "
5481 "TYPE at %C [F2003:C815]");
5482 return MATCH_ERROR;
5483 }
5484
8c6a85e3
TB
5485 /* Create temporary variable. */
5486 select_type_set_tmp (&c->ts);
31708dc6 5487
6de9cd9a
DN
5488 return MATCH_YES;
5489
5490syntax:
8c6a85e3 5491 gfc_error ("Syntax error in TYPE IS specification at %C");
6de9cd9a
DN
5492
5493cleanup:
8c6a85e3
TB
5494 if (c != NULL)
5495 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
5496 return MATCH_ERROR;
5497}
d68bd5a8 5498
c874ae73 5499
8c6a85e3
TB
5500/* Match a CLASS IS or CLASS DEFAULT statement. */
5501
5502match
5503gfc_match_class_is (void)
5504{
5505 gfc_case *c = NULL;
5506 match m;
5507
5508 if (gfc_current_state () != COMP_SELECT_TYPE)
5509 return MATCH_NO;
5510
5511 if (gfc_match ("% default") == MATCH_YES)
5512 {
5513 m = match_case_eos ();
6de9cd9a
DN
5514 if (m == MATCH_NO)
5515 goto syntax;
5516 if (m == MATCH_ERROR)
5517 goto cleanup;
5518
8c6a85e3
TB
5519 new_st.op = EXEC_SELECT_TYPE;
5520 c = gfc_get_case ();
5521 c->where = gfc_current_locus;
5522 c->ts.type = BT_UNKNOWN;
5523 new_st.ext.block.case_list = c;
5524 select_type_set_tmp (NULL);
5525 return MATCH_YES;
6de9cd9a
DN
5526 }
5527
8c6a85e3
TB
5528 m = gfc_match ("% is");
5529 if (m == MATCH_NO)
6de9cd9a 5530 goto syntax;
8c6a85e3
TB
5531 if (m == MATCH_ERROR)
5532 goto cleanup;
5533
5534 if (gfc_match_char ('(') != MATCH_YES)
5535 goto syntax;
5536
5537 c = gfc_get_case ();
5538 c->where = gfc_current_locus;
5539
ef701bbe
SK
5540 m = match_derived_type_spec (&c->ts);
5541 if (m == MATCH_NO)
5542 goto syntax;
5543 if (m == MATCH_ERROR)
8c6a85e3
TB
5544 goto cleanup;
5545
5546 if (c->ts.type == BT_DERIVED)
5547 c->ts.type = BT_CLASS;
5548
5549 if (gfc_match_char (')') != MATCH_YES)
5550 goto syntax;
5551
5552 m = match_case_eos ();
5553 if (m == MATCH_NO)
5554 goto syntax;
5555 if (m == MATCH_ERROR)
5556 goto cleanup;
5557
5558 new_st.op = EXEC_SELECT_TYPE;
5559 new_st.ext.block.case_list = c;
8b704316 5560
8c6a85e3
TB
5561 /* Create temporary variable. */
5562 select_type_set_tmp (&c->ts);
6de9cd9a 5563
c874ae73
TS
5564 return MATCH_YES;
5565
5566syntax:
8c6a85e3 5567 gfc_error ("Syntax error in CLASS IS specification at %C");
c874ae73
TS
5568
5569cleanup:
8c6a85e3
TB
5570 if (c != NULL)
5571 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
c874ae73
TS
5572 return MATCH_ERROR;
5573}
5574
8c6a85e3
TB
5575
5576/********************* WHERE subroutines ********************/
5577
8b704316 5578/* Match the rest of a simple WHERE statement that follows an IF statement.
8c6a85e3 5579 */
c874ae73
TS
5580
5581static match
8c6a85e3 5582match_simple_where (void)
c874ae73 5583{
8c6a85e3 5584 gfc_expr *expr;
c874ae73
TS
5585 gfc_code *c;
5586 match m;
5587
8c6a85e3 5588 m = gfc_match (" ( %e )", &expr);
c874ae73 5589 if (m != MATCH_YES)
8c6a85e3 5590 return m;
c874ae73
TS
5591
5592 m = gfc_match_assignment ();
8c6a85e3
TB
5593 if (m == MATCH_NO)
5594 goto syntax;
c874ae73
TS
5595 if (m == MATCH_ERROR)
5596 goto cleanup;
c874ae73
TS
5597
5598 if (gfc_match_eos () != MATCH_YES)
5599 goto syntax;
5600
11e5274a 5601 c = gfc_get_code (EXEC_WHERE);
8c6a85e3 5602 c->expr1 = expr;
8c6a85e3 5603
11e5274a 5604 c->next = XCNEW (gfc_code);
8c6a85e3 5605 *c->next = new_st;
c874ae73 5606 gfc_clear_new_st ();
c874ae73 5607
8c6a85e3
TB
5608 new_st.op = EXEC_WHERE;
5609 new_st.block = c;
c874ae73
TS
5610
5611 return MATCH_YES;
5612
5613syntax:
8c6a85e3 5614 gfc_syntax_error (ST_WHERE);
c874ae73
TS
5615
5616cleanup:
8c6a85e3 5617 gfc_free_expr (expr);
c874ae73
TS
5618 return MATCH_ERROR;
5619}
5620
5621
8c6a85e3 5622/* Match a WHERE statement. */
c874ae73
TS
5623
5624match
8c6a85e3 5625gfc_match_where (gfc_statement *st)
c874ae73 5626{
8c6a85e3 5627 gfc_expr *expr;
c874ae73 5628 match m0, m;
8c6a85e3 5629 gfc_code *c;
c874ae73
TS
5630
5631 m0 = gfc_match_label ();
5632 if (m0 == MATCH_ERROR)
8c6a85e3 5633 return m0;
c874ae73 5634
8c6a85e3 5635 m = gfc_match (" where ( %e )", &expr);
c874ae73
TS
5636 if (m != MATCH_YES)
5637 return m;
5638
6de9cd9a
DN
5639 if (gfc_match_eos () == MATCH_YES)
5640 {
8c6a85e3
TB
5641 *st = ST_WHERE_BLOCK;
5642 new_st.op = EXEC_WHERE;
5643 new_st.expr1 = expr;
6de9cd9a
DN
5644 return MATCH_YES;
5645 }
5646
5647 m = gfc_match_assignment ();
6de9cd9a 5648 if (m == MATCH_NO)
8c6a85e3
TB
5649 gfc_syntax_error (ST_WHERE);
5650
5651 if (m != MATCH_YES)
6de9cd9a 5652 {
8c6a85e3
TB
5653 gfc_free_expr (expr);
5654 return MATCH_ERROR;
6de9cd9a
DN
5655 }
5656
8c6a85e3
TB
5657 /* We've got a simple WHERE statement. */
5658 *st = ST_WHERE;
11e5274a 5659 c = gfc_get_code (EXEC_WHERE);
8c6a85e3 5660 c->expr1 = expr;
8c6a85e3 5661
11e5274a 5662 c->next = XCNEW (gfc_code);
8c6a85e3 5663 *c->next = new_st;
6de9cd9a 5664 gfc_clear_new_st ();
6de9cd9a 5665
8c6a85e3
TB
5666 new_st.op = EXEC_WHERE;
5667 new_st.block = c;
5668
5669 return MATCH_YES;
5670}
5671
5672
5673/* Match an ELSEWHERE statement. We leave behind a WHERE node in
5674 new_st if successful. */
5675
5676match
5677gfc_match_elsewhere (void)
5678{
5679 char name[GFC_MAX_SYMBOL_LEN + 1];
5680 gfc_expr *expr;
5681 match m;
5682
5683 if (gfc_current_state () != COMP_WHERE)
5684 {
5685 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
5686 return MATCH_ERROR;
5687 }
5688
5689 expr = NULL;
5690
5691 if (gfc_match_char ('(') == MATCH_YES)
5692 {
5693 m = gfc_match_expr (&expr);
5694 if (m == MATCH_NO)
5695 goto syntax;
5696 if (m == MATCH_ERROR)
5697 return MATCH_ERROR;
5698
5699 if (gfc_match_char (')') != MATCH_YES)
5700 goto syntax;
5701 }
5702
5703 if (gfc_match_eos () != MATCH_YES)
5704 {
5705 /* Only makes sense if we have a where-construct-name. */
5706 if (!gfc_current_block ())
5707 {
5708 m = MATCH_ERROR;
5709 goto cleanup;
5710 }
5711 /* Better be a name at this point. */
5712 m = gfc_match_name (name);
5713 if (m == MATCH_NO)
5714 goto syntax;
5715 if (m == MATCH_ERROR)
5716 goto cleanup;
5717
5718 if (gfc_match_eos () != MATCH_YES)
5719 goto syntax;
5720
5721 if (strcmp (name, gfc_current_block ()->name) != 0)
5722 {
a4d9b221 5723 gfc_error ("Label %qs at %C doesn't match WHERE label %qs",
8c6a85e3
TB
5724 name, gfc_current_block ()->name);
5725 goto cleanup;
5726 }
5727 }
5728
5729 new_st.op = EXEC_WHERE;
5730 new_st.expr1 = expr;
6de9cd9a
DN
5731 return MATCH_YES;
5732
5733syntax:
8c6a85e3 5734 gfc_syntax_error (ST_ELSEWHERE);
6de9cd9a
DN
5735
5736cleanup:
8c6a85e3
TB
5737 gfc_free_expr (expr);
5738 return MATCH_ERROR;
6de9cd9a 5739}