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