PR 78534 Revert r244011
[official-gcc.git] / gcc / fortran / match.c
blobea9d315d7cf6206b6606b6b4fc9f6d37fc2093b1
1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
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.
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.
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/>. */
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"
29 int gfc_matching_ptr_assignment = 0;
30 int gfc_matching_procptr_assignment = 0;
31 bool gfc_matching_prefix = false;
33 /* Stack of SELECT TYPE statements. */
34 gfc_select_type_stack *select_type_stack = NULL;
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)
41 switch (op)
43 case INTRINSIC_UPLUS:
44 case INTRINSIC_PLUS:
45 return "+";
47 case INTRINSIC_UMINUS:
48 case INTRINSIC_MINUS:
49 return "-";
51 case INTRINSIC_POWER:
52 return "**";
53 case INTRINSIC_CONCAT:
54 return "//";
55 case INTRINSIC_TIMES:
56 return "*";
57 case INTRINSIC_DIVIDE:
58 return "/";
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.";
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.";
96 case INTRINSIC_ASSIGN:
97 return "=";
99 case INTRINSIC_PARENTHESES:
100 return "parens";
102 case INTRINSIC_NONE:
103 return "none";
105 /* DTIO */
106 case INTRINSIC_FORMATTED:
107 return "formatted";
108 case INTRINSIC_UNFORMATTED:
109 return "unformatted";
111 default:
112 break;
115 gfc_internal_error ("gfc_op2string(): Bad code");
116 /* Not reached. */
120 /******************** Generic matching subroutines ************************/
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"
147 match
148 gfc_match_member_sep(gfc_symbol *sym)
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;
157 /* What a relief: '%' is an unambiguous member separator. */
158 if (gfc_match_char ('%') == MATCH_YES)
159 return MATCH_YES;
161 /* Beware ye who enter here. */
162 if (!flag_dec_structure || !sym)
163 return MATCH_NO;
165 tsym = NULL;
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;
175 iop = INTRINSIC_NONE;
176 name[0] = '\0';
177 m = MATCH_NO;
179 /* If we have to reject come back here later. */
180 start_loc = gfc_current_locus;
182 /* Look for a component access next. */
183 if (gfc_match_char ('.') != MATCH_YES)
184 return MATCH_NO;
186 /* If we accept, come back here. */
187 dot_loc = gfc_current_locus;
189 /* Try to match a symbol name following the dot. */
190 if (gfc_match_name (name) != MATCH_YES)
192 gfc_error ("Expected structure component or operator name "
193 "after '.' at %C");
194 goto error;
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;
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. */
204 /* First use any user-defined operators ".y." */
205 if (gfc_find_uop (name, sym->ns) != NULL)
206 goto no;
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;
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)
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;
224 gfc_error ("'%s' is neither a defined operator nor a "
225 "structure component in dotted string at %C", name);
226 goto error;
229 /* .y. is an intrinsic operator, overriding any possible member access. */
230 goto no;
232 /* Return keeping the current locus consistent with the match result. */
233 error:
234 m = MATCH_ERROR;
236 gfc_current_locus = start_loc;
237 return m;
238 yes:
239 gfc_current_locus = dot_loc;
240 return MATCH_YES;
244 /* This function scans the current statement counting the opened and closed
245 parenthesis to make sure they are balanced. */
247 match
248 gfc_match_parens (void)
250 locus old_loc, where;
251 int count;
252 gfc_instring instring;
253 gfc_char_t c, quote;
255 old_loc = gfc_current_locus;
256 count = 0;
257 instring = NONSTRING;
258 quote = ' ';
260 for (;;)
262 c = gfc_next_char_literal (instring);
263 if (c == '\n')
264 break;
265 if (quote == ' ' && ((c == '\'') || (c == '"')))
267 quote = c;
268 instring = INSTRING_WARN;
269 continue;
271 if (quote != ' ' && c == quote)
273 quote = ' ';
274 instring = NONSTRING;
275 continue;
278 if (c == '(' && quote == ' ')
280 count++;
281 where = gfc_current_locus;
283 if (c == ')' && quote == ' ')
285 count--;
286 where = gfc_current_locus;
290 gfc_current_locus = old_loc;
292 if (count > 0)
294 gfc_error ("Missing %<)%> in statement at or before %L", &where);
295 return MATCH_ERROR;
297 if (count < 0)
299 gfc_error ("Missing %<(%> in statement at or before %L", &where);
300 return MATCH_ERROR;
303 return MATCH_YES;
307 /* See if the next character is a special character that has
308 escaped by a \ via the -fbackslash option. */
310 match
311 gfc_match_special_char (gfc_char_t *res)
313 int len, i;
314 gfc_char_t c, n;
315 match m;
317 m = MATCH_YES;
319 switch ((c = gfc_next_char_literal (INSTRING_WARN)))
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;
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++)
357 char buf[2] = { '\0', '\0' };
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;
364 buf[0] = (unsigned char) c;
365 n = n << 4;
366 n += strtol (buf, NULL, 16);
368 *res = n;
369 break;
371 default:
372 /* Unknown backslash codes are simply not expanded. */
373 m = MATCH_NO;
374 break;
377 return m;
381 /* In free form, match at least one space. Always matches in fixed
382 form. */
384 match
385 gfc_match_space (void)
387 locus old_loc;
388 char c;
390 if (gfc_current_form == FORM_FIXED)
391 return MATCH_YES;
393 old_loc = gfc_current_locus;
395 c = gfc_next_ascii_char ();
396 if (!gfc_is_whitespace (c))
398 gfc_current_locus = old_loc;
399 return MATCH_NO;
402 gfc_gobble_whitespace ();
404 return MATCH_YES;
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. */
412 match
413 gfc_match_eos (void)
415 locus old_loc;
416 int flag;
417 char c;
419 flag = 0;
421 for (;;)
423 old_loc = gfc_current_locus;
424 gfc_gobble_whitespace ();
426 c = gfc_next_ascii_char ();
427 switch (c)
429 case '!':
432 c = gfc_next_ascii_char ();
434 while (c != '\n');
436 /* Fall through. */
438 case '\n':
439 return MATCH_YES;
441 case ';':
442 flag = 1;
443 continue;
446 break;
449 gfc_current_locus = old_loc;
450 return (flag) ? MATCH_YES : MATCH_NO;
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. */
459 match
460 gfc_match_small_literal_int (int *value, int *cnt)
462 locus old_loc;
463 char c;
464 int i, j;
466 old_loc = gfc_current_locus;
468 *value = -1;
469 gfc_gobble_whitespace ();
470 c = gfc_next_ascii_char ();
471 if (cnt)
472 *cnt = 0;
474 if (!ISDIGIT (c))
476 gfc_current_locus = old_loc;
477 return MATCH_NO;
480 i = c - '0';
481 j = 1;
483 for (;;)
485 old_loc = gfc_current_locus;
486 c = gfc_next_ascii_char ();
488 if (!ISDIGIT (c))
489 break;
491 i = 10 * i + c - '0';
492 j++;
494 if (i > 99999999)
496 gfc_error ("Integer too large at %C");
497 return MATCH_ERROR;
501 gfc_current_locus = old_loc;
503 *value = i;
504 if (cnt)
505 *cnt = j;
506 return MATCH_YES;
510 /* Match a small, constant integer expression, like in a kind
511 statement. On MATCH_YES, 'value' is set. */
513 match
514 gfc_match_small_int (int *value)
516 gfc_expr *expr;
517 const char *p;
518 match m;
519 int i;
521 m = gfc_match_expr (&expr);
522 if (m != MATCH_YES)
523 return m;
525 p = gfc_extract_int (expr, &i);
526 gfc_free_expr (expr);
528 if (p != NULL)
530 gfc_error (p);
531 m = MATCH_ERROR;
534 *value = i;
535 return m;
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. */
547 match
548 gfc_match_small_int_expr (int *value, gfc_expr **expr)
550 const char *p;
551 match m;
552 int i;
554 m = gfc_match_expr (expr);
555 if (m != MATCH_YES)
556 return m;
558 p = gfc_extract_int (*expr, &i);
560 if (p != NULL)
562 gfc_error (p);
563 m = MATCH_ERROR;
566 *value = i;
567 return m;
571 /* Matches a statement label. Uses gfc_match_small_literal_int() to
572 do most of the work. */
574 match
575 gfc_match_st_label (gfc_st_label **label)
577 locus old_loc;
578 match m;
579 int i, cnt;
581 old_loc = gfc_current_locus;
583 m = gfc_match_small_literal_int (&i, &cnt);
584 if (m != MATCH_YES)
585 return m;
587 if (cnt > 5)
589 gfc_error ("Too many digits in statement label at %C");
590 goto cleanup;
593 if (i == 0)
595 gfc_error ("Statement label at %C is zero");
596 goto cleanup;
599 *label = gfc_get_st_label (i);
600 return MATCH_YES;
602 cleanup:
604 gfc_current_locus = old_loc;
605 return MATCH_ERROR;
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. */
614 match
615 gfc_match_label (void)
617 char name[GFC_MAX_SYMBOL_LEN + 1];
618 match m;
620 gfc_new_block = NULL;
622 m = gfc_match (" %n :", name);
623 if (m != MATCH_YES)
624 return m;
626 if (gfc_get_symbol (name, NULL, &gfc_new_block))
628 gfc_error ("Label name %qs at %C is ambiguous", name);
629 return MATCH_ERROR;
632 if (gfc_new_block->attr.flavor == FL_LABEL)
634 gfc_error ("Duplicate construct label %qs at %C", name);
635 return MATCH_ERROR;
638 if (!gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
639 gfc_new_block->name, NULL))
640 return MATCH_ERROR;
642 return MATCH_YES;
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. */
651 match
652 gfc_match_name (char *buffer)
654 locus old_loc;
655 int i;
656 char c;
658 old_loc = gfc_current_locus;
659 gfc_gobble_whitespace ();
661 c = gfc_next_ascii_char ();
662 if (!(ISALPHA (c) || (c == '_' && flag_allow_leading_underscore)))
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;
673 i = 0;
677 buffer[i++] = c;
679 if (i > gfc_option.max_identifier_length)
681 gfc_error ("Name at %C is too long");
682 return MATCH_ERROR;
685 old_loc = gfc_current_locus;
686 c = gfc_next_ascii_char ();
688 while (ISALNUM (c) || c == '_' || (flag_dollar_ok && c == '$'));
690 if (c == '$' && !flag_dollar_ok)
692 gfc_fatal_error ("Invalid character %<$%> at %L. Use %<-fdollar-ok%> to "
693 "allow it as an extension", &old_loc);
694 return MATCH_ERROR;
697 buffer[i] = '\0';
698 gfc_current_locus = old_loc;
700 return MATCH_YES;
704 /* Match a symbol on the input. Modifies the pointer to the symbol
705 pointer if successful. */
707 match
708 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
710 char buffer[GFC_MAX_SYMBOL_LEN + 1];
711 match m;
713 m = gfc_match_name (buffer);
714 if (m != MATCH_YES)
715 return m;
717 if (host_assoc)
718 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
719 ? MATCH_ERROR : MATCH_YES;
721 if (gfc_get_sym_tree (buffer, NULL, matched_symbol, false))
722 return MATCH_ERROR;
724 return MATCH_YES;
728 match
729 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
731 gfc_symtree *st;
732 match m;
734 m = gfc_match_sym_tree (&st, host_assoc);
736 if (m == MATCH_YES)
738 if (st)
739 *matched_symbol = st->n.sym;
740 else
741 *matched_symbol = NULL;
743 else
744 *matched_symbol = NULL;
745 return m;
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. */
753 match
754 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
756 locus orig_loc = gfc_current_locus;
757 char ch;
759 gfc_gobble_whitespace ();
760 ch = gfc_next_ascii_char ();
761 switch (ch)
763 case '+':
764 /* Matched "+". */
765 *result = INTRINSIC_PLUS;
766 return MATCH_YES;
768 case '-':
769 /* Matched "-". */
770 *result = INTRINSIC_MINUS;
771 return MATCH_YES;
773 case '=':
774 if (gfc_next_ascii_char () == '=')
776 /* Matched "==". */
777 *result = INTRINSIC_EQ;
778 return MATCH_YES;
780 break;
782 case '<':
783 if (gfc_peek_ascii_char () == '=')
785 /* Matched "<=". */
786 gfc_next_ascii_char ();
787 *result = INTRINSIC_LE;
788 return MATCH_YES;
790 /* Matched "<". */
791 *result = INTRINSIC_LT;
792 return MATCH_YES;
794 case '>':
795 if (gfc_peek_ascii_char () == '=')
797 /* Matched ">=". */
798 gfc_next_ascii_char ();
799 *result = INTRINSIC_GE;
800 return MATCH_YES;
802 /* Matched ">". */
803 *result = INTRINSIC_GT;
804 return MATCH_YES;
806 case '*':
807 if (gfc_peek_ascii_char () == '*')
809 /* Matched "**". */
810 gfc_next_ascii_char ();
811 *result = INTRINSIC_POWER;
812 return MATCH_YES;
814 /* Matched "*". */
815 *result = INTRINSIC_TIMES;
816 return MATCH_YES;
818 case '/':
819 ch = gfc_peek_ascii_char ();
820 if (ch == '=')
822 /* Matched "/=". */
823 gfc_next_ascii_char ();
824 *result = INTRINSIC_NE;
825 return MATCH_YES;
827 else if (ch == '/')
829 /* Matched "//". */
830 gfc_next_ascii_char ();
831 *result = INTRINSIC_CONCAT;
832 return MATCH_YES;
834 /* Matched "/". */
835 *result = INTRINSIC_DIVIDE;
836 return MATCH_YES;
838 case '.':
839 ch = gfc_next_ascii_char ();
840 switch (ch)
842 case 'a':
843 if (gfc_next_ascii_char () == 'n'
844 && gfc_next_ascii_char () == 'd'
845 && gfc_next_ascii_char () == '.')
847 /* Matched ".and.". */
848 *result = INTRINSIC_AND;
849 return MATCH_YES;
851 break;
853 case 'e':
854 if (gfc_next_ascii_char () == 'q')
856 ch = gfc_next_ascii_char ();
857 if (ch == '.')
859 /* Matched ".eq.". */
860 *result = INTRINSIC_EQ_OS;
861 return MATCH_YES;
863 else if (ch == 'v')
865 if (gfc_next_ascii_char () == '.')
867 /* Matched ".eqv.". */
868 *result = INTRINSIC_EQV;
869 return MATCH_YES;
873 break;
875 case 'g':
876 ch = gfc_next_ascii_char ();
877 if (ch == 'e')
879 if (gfc_next_ascii_char () == '.')
881 /* Matched ".ge.". */
882 *result = INTRINSIC_GE_OS;
883 return MATCH_YES;
886 else if (ch == 't')
888 if (gfc_next_ascii_char () == '.')
890 /* Matched ".gt.". */
891 *result = INTRINSIC_GT_OS;
892 return MATCH_YES;
895 break;
897 case 'l':
898 ch = gfc_next_ascii_char ();
899 if (ch == 'e')
901 if (gfc_next_ascii_char () == '.')
903 /* Matched ".le.". */
904 *result = INTRINSIC_LE_OS;
905 return MATCH_YES;
908 else if (ch == 't')
910 if (gfc_next_ascii_char () == '.')
912 /* Matched ".lt.". */
913 *result = INTRINSIC_LT_OS;
914 return MATCH_YES;
917 break;
919 case 'n':
920 ch = gfc_next_ascii_char ();
921 if (ch == 'e')
923 ch = gfc_next_ascii_char ();
924 if (ch == '.')
926 /* Matched ".ne.". */
927 *result = INTRINSIC_NE_OS;
928 return MATCH_YES;
930 else if (ch == 'q')
932 if (gfc_next_ascii_char () == 'v'
933 && gfc_next_ascii_char () == '.')
935 /* Matched ".neqv.". */
936 *result = INTRINSIC_NEQV;
937 return MATCH_YES;
941 else if (ch == 'o')
943 if (gfc_next_ascii_char () == 't'
944 && gfc_next_ascii_char () == '.')
946 /* Matched ".not.". */
947 *result = INTRINSIC_NOT;
948 return MATCH_YES;
951 break;
953 case 'o':
954 if (gfc_next_ascii_char () == 'r'
955 && gfc_next_ascii_char () == '.')
957 /* Matched ".or.". */
958 *result = INTRINSIC_OR;
959 return MATCH_YES;
961 break;
963 case 'x':
964 if (gfc_next_ascii_char () == 'o'
965 && gfc_next_ascii_char () == 'r'
966 && gfc_next_ascii_char () == '.')
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;
974 break;
976 default:
977 break;
979 break;
981 default:
982 break;
985 gfc_current_locus = orig_loc;
986 return MATCH_NO;
990 /* Match a loop control phrase:
992 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
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. */
998 match
999 gfc_match_iterator (gfc_iterator *iter, int init_flag)
1001 char name[GFC_MAX_SYMBOL_LEN + 1];
1002 gfc_expr *var, *e1, *e2, *e3;
1003 locus start;
1004 match m;
1006 e1 = e2 = e3 = NULL;
1008 /* Match the start of an iterator without affecting the symbol table. */
1010 start = gfc_current_locus;
1011 m = gfc_match (" %n =", name);
1012 gfc_current_locus = start;
1014 if (m != MATCH_YES)
1015 return MATCH_NO;
1017 m = gfc_match_variable (&var, 0);
1018 if (m != MATCH_YES)
1019 return MATCH_NO;
1021 if (var->symtree->n.sym->attr.dimension)
1023 gfc_error ("Loop variable at %C cannot be an array");
1024 goto cleanup;
1027 /* F2008, C617 & C565. */
1028 if (var->symtree->n.sym->attr.codimension)
1030 gfc_error ("Loop variable at %C cannot be a coarray");
1031 goto cleanup;
1034 if (var->ref != NULL)
1036 gfc_error ("Loop variable at %C cannot be a sub-component");
1037 goto cleanup;
1040 gfc_match_char ('=');
1042 var->symtree->n.sym->attr.implied_index = 1;
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;
1050 if (gfc_match_char (',') != MATCH_YES)
1051 goto syntax;
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;
1059 if (gfc_match_char (',') != MATCH_YES)
1061 e3 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
1062 goto done;
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)
1070 gfc_error ("Expected a step value in iterator at %C");
1071 goto cleanup;
1074 done:
1075 iter->var = var;
1076 iter->start = e1;
1077 iter->end = e2;
1078 iter->step = e3;
1079 return MATCH_YES;
1081 syntax:
1082 gfc_error ("Syntax error in iterator at %C");
1084 cleanup:
1085 gfc_free_expr (e1);
1086 gfc_free_expr (e2);
1087 gfc_free_expr (e3);
1089 return MATCH_ERROR;
1093 /* Tries to match the next non-whitespace character on the input.
1094 This subroutine does not return MATCH_ERROR. */
1096 match
1097 gfc_match_char (char c)
1099 locus where;
1101 where = gfc_current_locus;
1102 gfc_gobble_whitespace ();
1104 if (gfc_next_ascii_char () == c)
1105 return MATCH_YES;
1107 gfc_current_locus = where;
1108 return MATCH_NO;
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:
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. */
1127 match
1128 gfc_match (const char *target, ...)
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;
1139 old_loc = gfc_current_locus;
1140 va_start (argp, target);
1141 m = MATCH_NO;
1142 matches = 0;
1143 p = target;
1145 loop:
1146 c = *p++;
1147 switch (c)
1149 case ' ':
1150 gfc_gobble_whitespace ();
1151 goto loop;
1152 case '\0':
1153 m = MATCH_YES;
1154 break;
1156 case '%':
1157 c = *p++;
1158 switch (c)
1160 case 'e':
1161 vp = va_arg (argp, void **);
1162 n = gfc_match_expr ((gfc_expr **) vp);
1163 if (n != MATCH_YES)
1165 m = n;
1166 goto not_yes;
1169 matches++;
1170 goto loop;
1172 case 'v':
1173 vp = va_arg (argp, void **);
1174 n = gfc_match_variable ((gfc_expr **) vp, 0);
1175 if (n != MATCH_YES)
1177 m = n;
1178 goto not_yes;
1181 matches++;
1182 goto loop;
1184 case 's':
1185 vp = va_arg (argp, void **);
1186 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1187 if (n != MATCH_YES)
1189 m = n;
1190 goto not_yes;
1193 matches++;
1194 goto loop;
1196 case 'n':
1197 np = va_arg (argp, char *);
1198 n = gfc_match_name (np);
1199 if (n != MATCH_YES)
1201 m = n;
1202 goto not_yes;
1205 matches++;
1206 goto loop;
1208 case 'l':
1209 label = va_arg (argp, gfc_st_label **);
1210 n = gfc_match_st_label (label);
1211 if (n != MATCH_YES)
1213 m = n;
1214 goto not_yes;
1217 matches++;
1218 goto loop;
1220 case 'o':
1221 ip = va_arg (argp, int *);
1222 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1223 if (n != MATCH_YES)
1225 m = n;
1226 goto not_yes;
1229 matches++;
1230 goto loop;
1232 case 't':
1233 if (gfc_match_eos () != MATCH_YES)
1235 m = MATCH_NO;
1236 goto not_yes;
1238 goto loop;
1240 case ' ':
1241 if (gfc_match_space () == MATCH_YES)
1242 goto loop;
1243 m = MATCH_NO;
1244 goto not_yes;
1246 case '%':
1247 break; /* Fall through to character matcher. */
1249 default:
1250 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1253 default:
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);
1259 if (c == gfc_next_ascii_char ())
1260 goto loop;
1261 break;
1264 not_yes:
1265 va_end (argp);
1267 if (m != MATCH_YES)
1269 /* Clean up after a failed match. */
1270 gfc_current_locus = old_loc;
1271 va_start (argp, target);
1273 p = target;
1274 for (; matches > 0; matches--)
1276 while (*p++ != '%');
1278 switch (*p++)
1280 case '%':
1281 matches++;
1282 break; /* Skip. */
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;
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;
1301 va_end (argp);
1304 return m;
1308 /*********************** Statement level matching **********************/
1310 /* Matches the start of a program unit, which is the program keyword
1311 followed by an obligatory symbol. */
1313 match
1314 gfc_match_program (void)
1316 gfc_symbol *sym;
1317 match m;
1319 m = gfc_match ("% %s%t", &sym);
1321 if (m == MATCH_NO)
1323 gfc_error ("Invalid form of PROGRAM statement at %C");
1324 m = MATCH_ERROR;
1327 if (m == MATCH_ERROR)
1328 return m;
1330 if (!gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL))
1331 return MATCH_ERROR;
1333 gfc_new_block = sym;
1335 return MATCH_YES;
1339 /* Match a simple assignment statement. */
1341 match
1342 gfc_match_assignment (void)
1344 gfc_expr *lvalue, *rvalue;
1345 locus old_loc;
1346 match m;
1348 old_loc = gfc_current_locus;
1350 lvalue = NULL;
1351 m = gfc_match (" %v =", &lvalue);
1352 if (m != MATCH_YES)
1354 gfc_current_locus = old_loc;
1355 gfc_free_expr (lvalue);
1356 return MATCH_NO;
1359 rvalue = NULL;
1360 m = gfc_match (" %e%t", &rvalue);
1361 if (m != MATCH_YES)
1363 gfc_current_locus = old_loc;
1364 gfc_free_expr (lvalue);
1365 gfc_free_expr (rvalue);
1366 return m;
1369 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1371 new_st.op = EXEC_ASSIGN;
1372 new_st.expr1 = lvalue;
1373 new_st.expr2 = rvalue;
1375 gfc_check_do_variable (lvalue->symtree);
1377 return MATCH_YES;
1381 /* Match a pointer assignment statement. */
1383 match
1384 gfc_match_pointer_assignment (void)
1386 gfc_expr *lvalue, *rvalue;
1387 locus old_loc;
1388 match m;
1390 old_loc = gfc_current_locus;
1392 lvalue = rvalue = NULL;
1393 gfc_matching_ptr_assignment = 0;
1394 gfc_matching_procptr_assignment = 0;
1396 m = gfc_match (" %v =>", &lvalue);
1397 if (m != MATCH_YES)
1399 m = MATCH_NO;
1400 goto cleanup;
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;
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;
1415 new_st.op = EXEC_POINTER_ASSIGN;
1416 new_st.expr1 = lvalue;
1417 new_st.expr2 = rvalue;
1419 return MATCH_YES;
1421 cleanup:
1422 gfc_current_locus = old_loc;
1423 gfc_free_expr (lvalue);
1424 gfc_free_expr (rvalue);
1425 return m;
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. */
1434 static match
1435 match_arithmetic_if (void)
1437 gfc_st_label *l1, *l2, *l3;
1438 gfc_expr *expr;
1439 match m;
1441 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1442 if (m != MATCH_YES)
1443 return m;
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))
1449 gfc_free_expr (expr);
1450 return MATCH_ERROR;
1453 if (!gfc_notify_std (GFC_STD_F95_OBS, "Arithmetic IF statement at %C"))
1454 return MATCH_ERROR;
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;
1462 return MATCH_YES;
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.
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. */
1476 static match match_simple_forall (void);
1477 static match match_simple_where (void);
1479 match
1480 gfc_match_if (gfc_statement *if_type)
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;
1488 n = gfc_match_label ();
1489 if (n == MATCH_ERROR)
1490 return n;
1492 old_loc = gfc_current_locus;
1494 m = gfc_match (" if ( %e", &expr);
1495 if (m != MATCH_YES)
1496 return m;
1498 old_loc2 = gfc_current_locus;
1499 gfc_current_locus = old_loc;
1501 if (gfc_match_parens () == MATCH_ERROR)
1502 return MATCH_ERROR;
1504 gfc_current_locus = old_loc2;
1506 if (gfc_match_char (')') != MATCH_YES)
1508 gfc_error ("Syntax error in IF-expression at %C");
1509 gfc_free_expr (expr);
1510 return MATCH_ERROR;
1513 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1515 if (m == MATCH_YES)
1517 if (n == MATCH_YES)
1519 gfc_error ("Block label not appropriate for arithmetic IF "
1520 "statement at %C");
1521 gfc_free_expr (expr);
1522 return MATCH_ERROR;
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))
1529 gfc_free_expr (expr);
1530 return MATCH_ERROR;
1533 if (!gfc_notify_std (GFC_STD_F95_OBS, "Arithmetic IF statement at %C"))
1534 return MATCH_ERROR;
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;
1542 *if_type = ST_ARITHMETIC_IF;
1543 return MATCH_YES;
1546 if (gfc_match (" then%t") == MATCH_YES)
1548 new_st.op = EXEC_IF;
1549 new_st.expr1 = expr;
1550 *if_type = ST_IF_BLOCK;
1551 return MATCH_YES;
1554 if (n == MATCH_YES)
1556 gfc_error ("Block label is not appropriate for IF statement at %C");
1557 gfc_free_expr (expr);
1558 return MATCH_ERROR;
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. */
1566 *if_type = ST_SIMPLE_IF;
1568 m = gfc_match_assignment ();
1569 if (m == MATCH_YES)
1570 goto got_match;
1572 gfc_free_expr (expr);
1573 gfc_undo_symbols ();
1574 gfc_current_locus = old_loc;
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;
1582 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1584 m = gfc_match_pointer_assignment ();
1585 if (m == MATCH_YES)
1586 goto got_match;
1588 gfc_free_expr (expr);
1589 gfc_undo_symbols ();
1590 gfc_current_locus = old_loc;
1592 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
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. */
1598 #define match(string, subr, statement) \
1599 if (gfc_match (string) == MATCH_YES) { m = subr(); goto got_match; }
1601 gfc_clear_error ();
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)
1638 if (flag_dec)
1639 match ("type", gfc_match_print, ST_WRITE)
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)
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;
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");
1659 gfc_free_expr (expr);
1660 return MATCH_ERROR;
1662 got_match:
1663 if (m == MATCH_NO)
1664 gfc_error ("Syntax error in IF-clause at %C");
1665 if (m != MATCH_YES)
1667 gfc_free_expr (expr);
1668 return MATCH_ERROR;
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. */
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;
1680 p->expr1 = expr;
1682 gfc_clear_new_st ();
1684 new_st.op = EXEC_IF;
1685 new_st.block = p;
1687 return MATCH_YES;
1690 #undef match
1693 /* Match an ELSE statement. */
1695 match
1696 gfc_match_else (void)
1698 char name[GFC_MAX_SYMBOL_LEN + 1];
1700 if (gfc_match_eos () == MATCH_YES)
1701 return MATCH_YES;
1703 if (gfc_match_name (name) != MATCH_YES
1704 || gfc_current_block () == NULL
1705 || gfc_match_eos () != MATCH_YES)
1707 gfc_error ("Unexpected junk after ELSE statement at %C");
1708 return MATCH_ERROR;
1711 if (strcmp (name, gfc_current_block ()->name) != 0)
1713 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1714 name, gfc_current_block ()->name);
1715 return MATCH_ERROR;
1718 return MATCH_YES;
1722 /* Match an ELSE IF statement. */
1724 match
1725 gfc_match_elseif (void)
1727 char name[GFC_MAX_SYMBOL_LEN + 1];
1728 gfc_expr *expr;
1729 match m;
1731 m = gfc_match (" ( %e ) then", &expr);
1732 if (m != MATCH_YES)
1733 return m;
1735 if (gfc_match_eos () == MATCH_YES)
1736 goto done;
1738 if (gfc_match_name (name) != MATCH_YES
1739 || gfc_current_block () == NULL
1740 || gfc_match_eos () != MATCH_YES)
1742 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1743 goto cleanup;
1746 if (strcmp (name, gfc_current_block ()->name) != 0)
1748 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1749 name, gfc_current_block ()->name);
1750 goto cleanup;
1753 done:
1754 new_st.op = EXEC_IF;
1755 new_st.expr1 = expr;
1756 return MATCH_YES;
1758 cleanup:
1759 gfc_free_expr (expr);
1760 return MATCH_ERROR;
1764 /* Free a gfc_iterator structure. */
1766 void
1767 gfc_free_iterator (gfc_iterator *iter, int flag)
1770 if (iter == NULL)
1771 return;
1773 gfc_free_expr (iter->var);
1774 gfc_free_expr (iter->start);
1775 gfc_free_expr (iter->end);
1776 gfc_free_expr (iter->step);
1778 if (flag)
1779 free (iter);
1783 /* Match a CRITICAL statement. */
1784 match
1785 gfc_match_critical (void)
1787 gfc_st_label *label = NULL;
1789 if (gfc_match_label () == MATCH_ERROR)
1790 return MATCH_ERROR;
1792 if (gfc_match (" critical") != MATCH_YES)
1793 return MATCH_NO;
1795 if (gfc_match_st_label (&label) == MATCH_ERROR)
1796 return MATCH_ERROR;
1798 if (gfc_match_eos () != MATCH_YES)
1800 gfc_syntax_error (ST_CRITICAL);
1801 return MATCH_ERROR;
1804 if (gfc_pure (NULL))
1806 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1807 return MATCH_ERROR;
1810 if (gfc_find_state (COMP_DO_CONCURRENT))
1812 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1813 "block");
1814 return MATCH_ERROR;
1817 gfc_unset_implicit_pure (NULL);
1819 if (!gfc_notify_std (GFC_STD_F2008, "CRITICAL statement at %C"))
1820 return MATCH_ERROR;
1822 if (flag_coarray == GFC_FCOARRAY_NONE)
1824 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
1825 "enable");
1826 return MATCH_ERROR;
1829 if (gfc_find_state (COMP_CRITICAL))
1831 gfc_error ("Nested CRITICAL block at %C");
1832 return MATCH_ERROR;
1835 new_st.op = EXEC_CRITICAL;
1837 if (label != NULL
1838 && !gfc_reference_st_label (label, ST_LABEL_TARGET))
1839 return MATCH_ERROR;
1841 return MATCH_YES;
1845 /* Match a BLOCK statement. */
1847 match
1848 gfc_match_block (void)
1850 match m;
1852 if (gfc_match_label () == MATCH_ERROR)
1853 return MATCH_ERROR;
1855 if (gfc_match (" block") != MATCH_YES)
1856 return MATCH_NO;
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;
1865 return MATCH_YES;
1869 /* Match an ASSOCIATE statement. */
1871 match
1872 gfc_match_associate (void)
1874 if (gfc_match_label () == MATCH_ERROR)
1875 return MATCH_ERROR;
1877 if (gfc_match (" associate") != MATCH_YES)
1878 return MATCH_NO;
1880 /* Match the association list. */
1881 if (gfc_match_char ('(') != MATCH_YES)
1883 gfc_error ("Expected association list at %C");
1884 return MATCH_ERROR;
1886 new_st.ext.block.assoc = NULL;
1887 while (true)
1889 gfc_association_list* newAssoc = gfc_get_association_list ();
1890 gfc_association_list* a;
1892 /* Match the next association. */
1893 if (gfc_match (" %n => %e", newAssoc->name, &newAssoc->target)
1894 != MATCH_YES)
1896 gfc_error ("Expected association at %C");
1897 goto assocListError;
1899 newAssoc->where = gfc_current_locus;
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))
1905 gfc_error ("Duplicate name %qs in association at %C",
1906 newAssoc->name);
1907 goto assocListError;
1910 /* The target expression must not be coindexed. */
1911 if (gfc_is_coindexed (newAssoc->target))
1913 gfc_error ("Association target at %C must not be coindexed");
1914 goto assocListError;
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. */
1921 /* Put it into the list. */
1922 newAssoc->next = new_st.ext.block.assoc;
1923 new_st.ext.block.assoc = newAssoc;
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)
1931 gfc_error ("Expected %<)%> or %<,%> at %C");
1932 return MATCH_ERROR;
1935 continue;
1937 assocListError:
1938 free (newAssoc);
1939 goto error;
1941 if (gfc_match_char (')') != MATCH_YES)
1943 /* This should never happen as we peek above. */
1944 gcc_unreachable ();
1947 if (gfc_match_eos () != MATCH_YES)
1949 gfc_error ("Junk after ASSOCIATE statement at %C");
1950 goto error;
1953 return MATCH_YES;
1955 error:
1956 gfc_free_association_list (new_st.ext.block.assoc);
1957 return MATCH_ERROR;
1961 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
1962 an accessible derived type. */
1964 static match
1965 match_derived_type_spec (gfc_typespec *ts)
1967 char name[GFC_MAX_SYMBOL_LEN + 1];
1968 locus old_locus;
1969 gfc_symbol *derived;
1971 old_locus = gfc_current_locus;
1973 if (gfc_match ("%n", name) != MATCH_YES)
1975 gfc_current_locus = old_locus;
1976 return MATCH_NO;
1979 gfc_find_symbol (name, NULL, 1, &derived);
1981 if (derived && derived->attr.flavor == FL_PROCEDURE && derived->attr.generic)
1982 derived = gfc_find_dt_in_generic (derived);
1984 if (derived && derived->attr.flavor == FL_DERIVED)
1986 ts->type = BT_DERIVED;
1987 ts->u.derived = derived;
1988 return MATCH_YES;
1991 gfc_current_locus = old_locus;
1992 return MATCH_NO;
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. */
2003 match
2004 gfc_match_type_spec (gfc_typespec *ts)
2006 match m;
2007 locus old_locus;
2008 char name[GFC_MAX_SYMBOL_LEN + 1];
2010 gfc_clear_ts (ts);
2011 gfc_gobble_whitespace ();
2012 old_locus = gfc_current_locus;
2014 if (match_derived_type_spec (ts) == MATCH_YES)
2016 /* Enforce F03:C401. */
2017 if (ts->u.derived->attr.abstract)
2019 gfc_error ("Derived type %qs at %L may not be ABSTRACT",
2020 ts->u.derived->name, &old_locus);
2021 return MATCH_ERROR;
2023 return MATCH_YES;
2026 if (gfc_match ("integer") == MATCH_YES)
2028 ts->type = BT_INTEGER;
2029 ts->kind = gfc_default_integer_kind;
2030 goto kind_selector;
2033 if (gfc_match ("double precision") == MATCH_YES)
2035 ts->type = BT_REAL;
2036 ts->kind = gfc_default_double_kind;
2037 return MATCH_YES;
2040 if (gfc_match ("complex") == MATCH_YES)
2042 ts->type = BT_COMPLEX;
2043 ts->kind = gfc_default_complex_kind;
2044 goto kind_selector;
2047 if (gfc_match ("character") == MATCH_YES)
2049 ts->type = BT_CHARACTER;
2051 m = gfc_match_char_spec (ts);
2053 if (m == MATCH_NO)
2054 m = MATCH_YES;
2056 return m;
2059 if (gfc_match ("logical") == MATCH_YES)
2061 ts->type = BT_LOGICAL;
2062 ts->kind = gfc_default_logical_kind;
2063 goto kind_selector;
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). */
2071 m = gfc_match (" %n", name);
2072 if (m == MATCH_YES && strcmp (name, "real") == 0)
2074 char c;
2075 gfc_expr *e;
2076 locus where;
2078 ts->type = BT_REAL;
2079 ts->kind = gfc_default_real_kind;
2081 gfc_gobble_whitespace ();
2083 /* Prevent REAL*4, etc. */
2084 c = gfc_peek_ascii_char ();
2085 if (c == '*')
2087 gfc_error ("Invalid type-spec at %C");
2088 return MATCH_ERROR;
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;
2096 /* Found something other than the opening '(' in REAL(... */
2097 if (c != '(')
2098 return MATCH_NO;
2099 else
2100 gfc_next_char (); /* Burn the '('. */
2102 /* Look for the optional KIND=. */
2103 where = gfc_current_locus;
2104 m = gfc_match ("%n", name);
2105 if (m == MATCH_YES)
2107 gfc_gobble_whitespace ();
2108 c = gfc_next_char ();
2109 if (c == '=')
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;
2118 else
2119 gfc_current_locus = where;
2121 else
2122 gfc_current_locus = where;
2124 found:
2126 m = gfc_match_init_expr (&e);
2127 if (m == MATCH_NO || m == MATCH_ERROR)
2128 return MATCH_NO;
2130 /* If a comma appears, it is an intrinsic subprogram. */
2131 gfc_gobble_whitespace ();
2132 c = gfc_peek_ascii_char ();
2133 if (c == ',')
2135 gfc_free_expr (e);
2136 return MATCH_NO;
2139 /* If ')' appears, we have REAL(initialization-expr), here check for
2140 a scalar integer initialization-expr and valid kind parameter. */
2141 if (c == ')')
2143 if (e->ts.type != BT_INTEGER || e->rank > 0)
2145 gfc_free_expr (e);
2146 return MATCH_NO;
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)
2153 gfc_error ("Invalid type-spec at %C");
2154 return MATCH_ERROR;
2157 gfc_free_expr (e);
2159 return MATCH_YES;
2163 /* If a type is not matched, simply return MATCH_NO. */
2164 gfc_current_locus = old_locus;
2165 return MATCH_NO;
2167 kind_selector:
2169 gfc_gobble_whitespace ();
2171 /* This prevents INTEGER*4, etc. */
2172 if (gfc_peek_ascii_char () == '*')
2174 gfc_error ("Invalid type-spec at %C");
2175 return MATCH_ERROR;
2178 m = gfc_match_kind_spec (ts, false);
2180 /* No kind specifier found. */
2181 if (m == MATCH_NO)
2182 m = MATCH_YES;
2184 return m;
2188 /******************** FORALL subroutines ********************/
2190 /* Free a list of FORALL iterators. */
2192 void
2193 gfc_free_forall_iterator (gfc_forall_iterator *iter)
2195 gfc_forall_iterator *next;
2197 while (iter)
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;
2210 /* Match an iterator as part of a FORALL statement. The format is:
2212 <var> = <start>:<end>[:<stride>]
2214 On MATCH_NO, the caller tests for the possibility that there is a
2215 scalar mask expression. */
2217 static match
2218 match_forall_iterator (gfc_forall_iterator **result)
2220 gfc_forall_iterator *iter;
2221 locus where;
2222 match m;
2224 where = gfc_current_locus;
2225 iter = XCNEW (gfc_forall_iterator);
2227 m = gfc_match_expr (&iter->var);
2228 if (m != MATCH_YES)
2229 goto cleanup;
2231 if (gfc_match_char ('=') != MATCH_YES
2232 || iter->var->expr_type != EXPR_VARIABLE)
2234 m = MATCH_NO;
2235 goto cleanup;
2238 m = gfc_match_expr (&iter->start);
2239 if (m != MATCH_YES)
2240 goto cleanup;
2242 if (gfc_match_char (':') != MATCH_YES)
2243 goto syntax;
2245 m = gfc_match_expr (&iter->end);
2246 if (m == MATCH_NO)
2247 goto syntax;
2248 if (m == MATCH_ERROR)
2249 goto cleanup;
2251 if (gfc_match_char (':') == MATCH_NO)
2252 iter->stride = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2253 else
2255 m = gfc_match_expr (&iter->stride);
2256 if (m == MATCH_NO)
2257 goto syntax;
2258 if (m == MATCH_ERROR)
2259 goto cleanup;
2262 /* Mark the iteration variable's symbol as used as a FORALL index. */
2263 iter->var->symtree->n.sym->forall_index = true;
2265 *result = iter;
2266 return MATCH_YES;
2268 syntax:
2269 gfc_error ("Syntax error in FORALL iterator at %C");
2270 m = MATCH_ERROR;
2272 cleanup:
2274 gfc_current_locus = where;
2275 gfc_free_forall_iterator (iter);
2276 return m;
2280 /* Match the header of a FORALL statement. */
2282 static match
2283 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
2285 gfc_forall_iterator *head, *tail, *new_iter;
2286 gfc_expr *msk;
2287 match m;
2289 gfc_gobble_whitespace ();
2291 head = tail = NULL;
2292 msk = NULL;
2294 if (gfc_match_char ('(') != MATCH_YES)
2295 return MATCH_NO;
2297 m = match_forall_iterator (&new_iter);
2298 if (m == MATCH_ERROR)
2299 goto cleanup;
2300 if (m == MATCH_NO)
2301 goto syntax;
2303 head = tail = new_iter;
2305 for (;;)
2307 if (gfc_match_char (',') != MATCH_YES)
2308 break;
2310 m = match_forall_iterator (&new_iter);
2311 if (m == MATCH_ERROR)
2312 goto cleanup;
2314 if (m == MATCH_YES)
2316 tail->next = new_iter;
2317 tail = new_iter;
2318 continue;
2321 /* Have to have a mask expression. */
2323 m = gfc_match_expr (&msk);
2324 if (m == MATCH_NO)
2325 goto syntax;
2326 if (m == MATCH_ERROR)
2327 goto cleanup;
2329 break;
2332 if (gfc_match_char (')') == MATCH_NO)
2333 goto syntax;
2335 *phead = head;
2336 *mask = msk;
2337 return MATCH_YES;
2339 syntax:
2340 gfc_syntax_error (ST_FORALL);
2342 cleanup:
2343 gfc_free_expr (msk);
2344 gfc_free_forall_iterator (head);
2346 return MATCH_ERROR;
2349 /* Match the rest of a simple FORALL statement that follows an
2350 IF statement. */
2352 static match
2353 match_simple_forall (void)
2355 gfc_forall_iterator *head;
2356 gfc_expr *mask;
2357 gfc_code *c;
2358 match m;
2360 mask = NULL;
2361 head = NULL;
2362 c = NULL;
2364 m = match_forall_header (&head, &mask);
2366 if (m == MATCH_NO)
2367 goto syntax;
2368 if (m != MATCH_YES)
2369 goto cleanup;
2371 m = gfc_match_assignment ();
2373 if (m == MATCH_ERROR)
2374 goto cleanup;
2375 if (m == MATCH_NO)
2377 m = gfc_match_pointer_assignment ();
2378 if (m == MATCH_ERROR)
2379 goto cleanup;
2380 if (m == MATCH_NO)
2381 goto syntax;
2384 c = XCNEW (gfc_code);
2385 *c = new_st;
2386 c->loc = gfc_current_locus;
2388 if (gfc_match_eos () != MATCH_YES)
2389 goto syntax;
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;
2398 return MATCH_YES;
2400 syntax:
2401 gfc_syntax_error (ST_FORALL);
2403 cleanup:
2404 gfc_free_forall_iterator (head);
2405 gfc_free_expr (mask);
2407 return MATCH_ERROR;
2411 /* Match a FORALL statement. */
2413 match
2414 gfc_match_forall (gfc_statement *st)
2416 gfc_forall_iterator *head;
2417 gfc_expr *mask;
2418 gfc_code *c;
2419 match m0, m;
2421 head = NULL;
2422 mask = NULL;
2423 c = NULL;
2425 m0 = gfc_match_label ();
2426 if (m0 == MATCH_ERROR)
2427 return MATCH_ERROR;
2429 m = gfc_match (" forall");
2430 if (m != MATCH_YES)
2431 return m;
2433 m = match_forall_header (&head, &mask);
2434 if (m == MATCH_ERROR)
2435 goto cleanup;
2436 if (m == MATCH_NO)
2437 goto syntax;
2439 if (gfc_match_eos () == MATCH_YES)
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;
2448 m = gfc_match_assignment ();
2449 if (m == MATCH_ERROR)
2450 goto cleanup;
2451 if (m == MATCH_NO)
2453 m = gfc_match_pointer_assignment ();
2454 if (m == MATCH_ERROR)
2455 goto cleanup;
2456 if (m == MATCH_NO)
2457 goto syntax;
2460 c = XCNEW (gfc_code);
2461 *c = new_st;
2462 c->loc = gfc_current_locus;
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;
2471 *st = ST_FORALL;
2472 return MATCH_YES;
2474 syntax:
2475 gfc_syntax_error (ST_FORALL);
2477 cleanup:
2478 gfc_free_forall_iterator (head);
2479 gfc_free_expr (mask);
2480 gfc_free_statements (c);
2481 return MATCH_NO;
2485 /* Match a DO statement. */
2487 match
2488 gfc_match_do (void)
2490 gfc_iterator iter, *ip;
2491 locus old_loc;
2492 gfc_st_label *label;
2493 match m;
2495 old_loc = gfc_current_locus;
2497 label = NULL;
2498 iter.var = iter.start = iter.end = iter.step = NULL;
2500 m = gfc_match_label ();
2501 if (m == MATCH_ERROR)
2502 return m;
2504 if (gfc_match (" do") != MATCH_YES)
2505 return MATCH_NO;
2507 m = gfc_match_st_label (&label);
2508 if (m == MATCH_ERROR)
2509 goto cleanup;
2511 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2513 if (gfc_match_eos () == MATCH_YES)
2515 iter.end = gfc_get_logical_expr (gfc_default_logical_kind, NULL, true);
2516 new_st.op = EXEC_DO_WHILE;
2517 goto done;
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;
2524 /* Check for balanced parens. */
2526 if (gfc_match_parens () == MATCH_ERROR)
2527 return MATCH_ERROR;
2529 if (gfc_match (" concurrent") == MATCH_YES)
2531 gfc_forall_iterator *head;
2532 gfc_expr *mask;
2534 if (!gfc_notify_std (GFC_STD_F2008, "DO CONCURRENT construct at %C"))
2535 return MATCH_ERROR;
2538 mask = NULL;
2539 head = NULL;
2540 m = match_forall_header (&head, &mask);
2542 if (m == MATCH_NO)
2543 return m;
2544 if (m == MATCH_ERROR)
2545 goto concurr_cleanup;
2547 if (gfc_match_eos () != MATCH_YES)
2548 goto concurr_cleanup;
2550 if (label != NULL
2551 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2552 goto concurr_cleanup;
2554 new_st.label1 = label;
2555 new_st.op = EXEC_DO_CONCURRENT;
2556 new_st.expr1 = mask;
2557 new_st.ext.forall_iterator = head;
2559 return MATCH_YES;
2561 concurr_cleanup:
2562 gfc_syntax_error (ST_DO);
2563 gfc_free_expr (mask);
2564 gfc_free_forall_iterator (head);
2565 return MATCH_ERROR;
2568 /* See if we have a DO WHILE. */
2569 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
2571 new_st.op = EXEC_DO_WHILE;
2572 goto done;
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;
2580 gfc_match_label (); /* This won't error. */
2581 gfc_match (" do "); /* This will work. */
2583 gfc_match_st_label (&label); /* Can't error out. */
2584 gfc_match_char (','); /* Optional comma. */
2586 m = gfc_match_iterator (&iter, 0);
2587 if (m == MATCH_NO)
2588 return MATCH_NO;
2589 if (m == MATCH_ERROR)
2590 goto cleanup;
2592 iter.var->symtree->n.sym->attr.implied_index = 0;
2593 gfc_check_do_variable (iter.var->symtree);
2595 if (gfc_match_eos () != MATCH_YES)
2597 gfc_syntax_error (ST_DO);
2598 goto cleanup;
2601 new_st.op = EXEC_DO;
2603 done:
2604 if (label != NULL
2605 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2606 goto cleanup;
2608 new_st.label1 = label;
2610 if (new_st.op == EXEC_DO_WHILE)
2611 new_st.expr1 = iter.end;
2612 else
2614 new_st.ext.iterator = ip = gfc_get_iterator ();
2615 *ip = iter;
2618 return MATCH_YES;
2620 cleanup:
2621 gfc_free_iterator (&iter, 0);
2623 return MATCH_ERROR;
2627 /* Match an EXIT or CYCLE statement. */
2629 static match
2630 match_exit_cycle (gfc_statement st, gfc_exec_op op)
2632 gfc_state_data *p, *o;
2633 gfc_symbol *sym;
2634 match m;
2635 int cnt;
2637 if (gfc_match_eos () == MATCH_YES)
2638 sym = NULL;
2639 else
2641 char name[GFC_MAX_SYMBOL_LEN + 1];
2642 gfc_symtree* stree;
2644 m = gfc_match ("% %n%t", name);
2645 if (m == MATCH_ERROR)
2646 return MATCH_ERROR;
2647 if (m == MATCH_NO)
2649 gfc_syntax_error (st);
2650 return MATCH_ERROR;
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)
2659 gfc_error ("Name %qs in %s statement at %C is unknown",
2660 name, gfc_ascii_statement (st));
2661 return MATCH_ERROR;
2664 sym = stree->n.sym;
2665 if (sym->attr.flavor != FL_LABEL)
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;
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)
2679 gfc_error("%s statement at %C leaves CRITICAL construct",
2680 gfc_ascii_statement (st));
2681 return MATCH_ERROR;
2683 else if (p->state == COMP_DO_CONCURRENT
2684 && (op == EXEC_EXIT || (sym && sym != p->sym)))
2686 /* F2008, C821 & C845. */
2687 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2688 gfc_ascii_statement (st));
2689 return MATCH_ERROR;
2691 else if ((sym && sym == p->sym)
2692 || (!sym && (p->state == COMP_DO
2693 || p->state == COMP_DO_CONCURRENT)))
2694 break;
2696 if (p == NULL)
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);
2705 return MATCH_ERROR;
2708 /* Special checks for EXIT from non-loop constructs. */
2709 switch (p->state)
2711 case COMP_DO:
2712 case COMP_DO_CONCURRENT:
2713 break;
2715 case COMP_CRITICAL:
2716 /* This is already handled above. */
2717 gcc_unreachable ();
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)
2727 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2728 " construct %qs", sym->name);
2729 return MATCH_ERROR;
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;
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;
2743 if (o != NULL)
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;
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))
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)
2771 gfc_error ("EXIT statement at %C terminating !$ACC LOOP loop");
2772 return MATCH_ERROR;
2774 if (st == ST_CYCLE && cnt < collapse)
2776 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2777 " !$ACC LOOP loop");
2778 return MATCH_ERROR;
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))
2790 int count = 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)
2798 if (o->previous->tail->ext.omp_clauses->collapse > 1)
2799 count = o->previous->tail->ext.omp_clauses->collapse;
2800 if (o->previous->tail->ext.omp_clauses->orderedc)
2801 count = o->previous->tail->ext.omp_clauses->orderedc;
2803 if (st == ST_EXIT && cnt <= count)
2805 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2806 return MATCH_ERROR;
2808 if (st == ST_CYCLE && cnt < count)
2810 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2811 " !$OMP DO loop");
2812 return MATCH_ERROR;
2816 /* Save the first statement in the construct - needed by the backend. */
2817 new_st.ext.which_construct = p->construct;
2819 new_st.op = op;
2821 return MATCH_YES;
2825 /* Match the EXIT statement. */
2827 match
2828 gfc_match_exit (void)
2830 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
2834 /* Match the CYCLE statement. */
2836 match
2837 gfc_match_cycle (void)
2839 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
2843 /* Match a stop-code after an (ERROR) STOP or PAUSE statement. The
2844 requirements for a stop-code differ in the standards.
2846 Fortran 95 has
2848 R840 stop-stmt is STOP [ stop-code ]
2849 R841 stop-code is scalar-char-constant
2850 or digit [ digit [ digit [ digit [ digit ] ] ] ]
2852 Fortran 2003 matches Fortran 95 except R840 and R841 are now R849 and R850.
2853 Fortran 2008 has
2855 R855 stop-stmt is STOP [ stop-code ]
2856 R856 allstop-stmt is ALL STOP [ stop-code ]
2857 R857 stop-code is scalar-default-char-constant-expr
2858 or scalar-int-constant-expr
2860 For free-form source code, all standards contain a statement of the form:
2862 A blank shall be used to separate names, constants, or labels from
2863 adjacent keywords, names, constants, or labels.
2865 A stop-code is not a name, constant, or label. So, under Fortran 95 and 2003,
2867 STOP123
2869 is valid, but it is invalid Fortran 2008. */
2871 static match
2872 gfc_match_stopcode (gfc_statement st)
2874 gfc_expr *e = NULL;
2875 match m;
2876 bool f95, f03;
2878 /* Set f95 for -std=f95. */
2879 f95 = gfc_option.allow_std == (GFC_STD_F95_OBS | GFC_STD_F95 | GFC_STD_F77
2880 | GFC_STD_F2008_OBS);
2882 /* Set f03 for -std=f2003. */
2883 f03 = gfc_option.allow_std == (GFC_STD_F95_OBS | GFC_STD_F95 | GFC_STD_F77
2884 | GFC_STD_F2008_OBS | GFC_STD_F2003);
2886 /* Look for a blank between STOP and the stop-code for F2008 or later. */
2887 if (gfc_current_form != FORM_FIXED && !(f95 || f03))
2889 char c = gfc_peek_ascii_char ();
2891 /* Look for end-of-statement. There is no stop-code. */
2892 if (c == '\n' || c == '!' || c == ';')
2893 goto done;
2895 if (c != ' ')
2897 gfc_error ("Blank required in %s statement near %C",
2898 gfc_ascii_statement (st));
2899 return MATCH_ERROR;
2903 if (gfc_match_eos () != MATCH_YES)
2905 int stopcode;
2906 locus old_locus;
2908 /* First look for the F95 or F2003 digit [...] construct. */
2909 old_locus = gfc_current_locus;
2910 m = gfc_match_small_int (&stopcode);
2911 if (m == MATCH_YES && (f95 || f03))
2913 if (stopcode < 0)
2915 gfc_error ("STOP code at %C cannot be negative");
2916 return MATCH_ERROR;
2919 if (stopcode > 99999)
2921 gfc_error ("STOP code at %C contains too many digits");
2922 return MATCH_ERROR;
2926 /* Reset the locus and now load gfc_expr. */
2927 gfc_current_locus = old_locus;
2928 m = gfc_match_expr (&e);
2929 if (m == MATCH_ERROR)
2930 goto cleanup;
2931 if (m == MATCH_NO)
2932 goto syntax;
2934 if (gfc_match_eos () != MATCH_YES)
2935 goto syntax;
2938 if (gfc_pure (NULL))
2940 if (st == ST_ERROR_STOP)
2942 if (!gfc_notify_std (GFC_STD_F2015, "%s statement at %C in PURE "
2943 "procedure", gfc_ascii_statement (st)))
2944 goto cleanup;
2946 else
2948 gfc_error ("%s statement not allowed in PURE procedure at %C",
2949 gfc_ascii_statement (st));
2950 goto cleanup;
2954 gfc_unset_implicit_pure (NULL);
2956 if (st == ST_STOP && gfc_find_state (COMP_CRITICAL))
2958 gfc_error ("Image control statement STOP at %C in CRITICAL block");
2959 goto cleanup;
2961 if (st == ST_STOP && gfc_find_state (COMP_DO_CONCURRENT))
2963 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
2964 goto cleanup;
2967 if (e != NULL)
2969 gfc_simplify_expr (e, 0);
2971 /* Test for F95 and F2003 style STOP stop-code. */
2972 if (e->expr_type != EXPR_CONSTANT && (f95 || f03))
2974 gfc_error ("STOP code at %L must be a scalar CHARACTER constant or "
2975 "digit[digit[digit[digit[digit]]]]", &e->where);
2976 goto cleanup;
2979 /* Use the machinery for an initialization expression to reduce the
2980 stop-code to a constant. */
2981 gfc_init_expr_flag = true;
2982 gfc_reduce_init_expr (e);
2983 gfc_init_expr_flag = false;
2985 if (!(e->ts.type == BT_CHARACTER || e->ts.type == BT_INTEGER))
2987 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
2988 &e->where);
2989 goto cleanup;
2992 if (e->rank != 0)
2994 gfc_error ("STOP code at %L must be scalar", &e->where);
2995 goto cleanup;
2998 if (e->ts.type == BT_CHARACTER
2999 && e->ts.kind != gfc_default_character_kind)
3001 gfc_error ("STOP code at %L must be default character KIND=%d",
3002 &e->where, (int) gfc_default_character_kind);
3003 goto cleanup;
3006 if (e->ts.type == BT_INTEGER && e->ts.kind != gfc_default_integer_kind)
3008 gfc_error ("STOP code at %L must be default integer KIND=%d",
3009 &e->where, (int) gfc_default_integer_kind);
3010 goto cleanup;
3014 done:
3016 switch (st)
3018 case ST_STOP:
3019 new_st.op = EXEC_STOP;
3020 break;
3021 case ST_ERROR_STOP:
3022 new_st.op = EXEC_ERROR_STOP;
3023 break;
3024 case ST_PAUSE:
3025 new_st.op = EXEC_PAUSE;
3026 break;
3027 default:
3028 gcc_unreachable ();
3031 new_st.expr1 = e;
3032 new_st.ext.stop_code = -1;
3034 return MATCH_YES;
3036 syntax:
3037 gfc_syntax_error (st);
3039 cleanup:
3041 gfc_free_expr (e);
3042 return MATCH_ERROR;
3046 /* Match the (deprecated) PAUSE statement. */
3048 match
3049 gfc_match_pause (void)
3051 match m;
3053 m = gfc_match_stopcode (ST_PAUSE);
3054 if (m == MATCH_YES)
3056 if (!gfc_notify_std (GFC_STD_F95_DEL, "PAUSE statement at %C"))
3057 m = MATCH_ERROR;
3059 return m;
3063 /* Match the STOP statement. */
3065 match
3066 gfc_match_stop (void)
3068 return gfc_match_stopcode (ST_STOP);
3072 /* Match the ERROR STOP statement. */
3074 match
3075 gfc_match_error_stop (void)
3077 if (!gfc_notify_std (GFC_STD_F2008, "ERROR STOP statement at %C"))
3078 return MATCH_ERROR;
3080 return gfc_match_stopcode (ST_ERROR_STOP);
3083 /* Match EVENT POST/WAIT statement. Syntax:
3084 EVENT POST ( event-variable [, sync-stat-list] )
3085 EVENT WAIT ( event-variable [, wait-spec-list] )
3086 with
3087 wait-spec-list is sync-stat-list or until-spec
3088 until-spec is UNTIL_COUNT = scalar-int-expr
3089 sync-stat is STAT= or ERRMSG=. */
3091 static match
3092 event_statement (gfc_statement st)
3094 match m;
3095 gfc_expr *tmp, *eventvar, *until_count, *stat, *errmsg;
3096 bool saw_until_count, saw_stat, saw_errmsg;
3098 tmp = eventvar = until_count = stat = errmsg = NULL;
3099 saw_until_count = saw_stat = saw_errmsg = false;
3101 if (gfc_pure (NULL))
3103 gfc_error ("Image control statement EVENT %s at %C in PURE procedure",
3104 st == ST_EVENT_POST ? "POST" : "WAIT");
3105 return MATCH_ERROR;
3108 gfc_unset_implicit_pure (NULL);
3110 if (flag_coarray == GFC_FCOARRAY_NONE)
3112 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3113 return MATCH_ERROR;
3116 if (gfc_find_state (COMP_CRITICAL))
3118 gfc_error ("Image control statement EVENT %s at %C in CRITICAL block",
3119 st == ST_EVENT_POST ? "POST" : "WAIT");
3120 return MATCH_ERROR;
3123 if (gfc_find_state (COMP_DO_CONCURRENT))
3125 gfc_error ("Image control statement EVENT %s at %C in DO CONCURRENT "
3126 "block", st == ST_EVENT_POST ? "POST" : "WAIT");
3127 return MATCH_ERROR;
3130 if (gfc_match_char ('(') != MATCH_YES)
3131 goto syntax;
3133 if (gfc_match ("%e", &eventvar) != MATCH_YES)
3134 goto syntax;
3135 m = gfc_match_char (',');
3136 if (m == MATCH_ERROR)
3137 goto syntax;
3138 if (m == MATCH_NO)
3140 m = gfc_match_char (')');
3141 if (m == MATCH_YES)
3142 goto done;
3143 goto syntax;
3146 for (;;)
3148 m = gfc_match (" stat = %v", &tmp);
3149 if (m == MATCH_ERROR)
3150 goto syntax;
3151 if (m == MATCH_YES)
3153 if (saw_stat)
3155 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3156 goto cleanup;
3158 stat = tmp;
3159 saw_stat = true;
3161 m = gfc_match_char (',');
3162 if (m == MATCH_YES)
3163 continue;
3165 tmp = NULL;
3166 break;
3169 m = gfc_match (" errmsg = %v", &tmp);
3170 if (m == MATCH_ERROR)
3171 goto syntax;
3172 if (m == MATCH_YES)
3174 if (saw_errmsg)
3176 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3177 goto cleanup;
3179 errmsg = tmp;
3180 saw_errmsg = true;
3182 m = gfc_match_char (',');
3183 if (m == MATCH_YES)
3184 continue;
3186 tmp = NULL;
3187 break;
3190 m = gfc_match (" until_count = %e", &tmp);
3191 if (m == MATCH_ERROR || st == ST_EVENT_POST)
3192 goto syntax;
3193 if (m == MATCH_YES)
3195 if (saw_until_count)
3197 gfc_error ("Redundant UNTIL_COUNT tag found at %L ",
3198 &tmp->where);
3199 goto cleanup;
3201 until_count = tmp;
3202 saw_until_count = true;
3204 m = gfc_match_char (',');
3205 if (m == MATCH_YES)
3206 continue;
3208 tmp = NULL;
3209 break;
3212 break;
3215 if (m == MATCH_ERROR)
3216 goto syntax;
3218 if (gfc_match (" )%t") != MATCH_YES)
3219 goto syntax;
3221 done:
3222 switch (st)
3224 case ST_EVENT_POST:
3225 new_st.op = EXEC_EVENT_POST;
3226 break;
3227 case ST_EVENT_WAIT:
3228 new_st.op = EXEC_EVENT_WAIT;
3229 break;
3230 default:
3231 gcc_unreachable ();
3234 new_st.expr1 = eventvar;
3235 new_st.expr2 = stat;
3236 new_st.expr3 = errmsg;
3237 new_st.expr4 = until_count;
3239 return MATCH_YES;
3241 syntax:
3242 gfc_syntax_error (st);
3244 cleanup:
3245 if (until_count != tmp)
3246 gfc_free_expr (until_count);
3247 if (errmsg != tmp)
3248 gfc_free_expr (errmsg);
3249 if (stat != tmp)
3250 gfc_free_expr (stat);
3252 gfc_free_expr (tmp);
3253 gfc_free_expr (eventvar);
3255 return MATCH_ERROR;
3260 match
3261 gfc_match_event_post (void)
3263 if (!gfc_notify_std (GFC_STD_F2008_TS, "EVENT POST statement at %C"))
3264 return MATCH_ERROR;
3266 return event_statement (ST_EVENT_POST);
3270 match
3271 gfc_match_event_wait (void)
3273 if (!gfc_notify_std (GFC_STD_F2008_TS, "EVENT WAIT statement at %C"))
3274 return MATCH_ERROR;
3276 return event_statement (ST_EVENT_WAIT);
3280 /* Match LOCK/UNLOCK statement. Syntax:
3281 LOCK ( lock-variable [ , lock-stat-list ] )
3282 UNLOCK ( lock-variable [ , sync-stat-list ] )
3283 where lock-stat is ACQUIRED_LOCK or sync-stat
3284 and sync-stat is STAT= or ERRMSG=. */
3286 static match
3287 lock_unlock_statement (gfc_statement st)
3289 match m;
3290 gfc_expr *tmp, *lockvar, *acq_lock, *stat, *errmsg;
3291 bool saw_acq_lock, saw_stat, saw_errmsg;
3293 tmp = lockvar = acq_lock = stat = errmsg = NULL;
3294 saw_acq_lock = saw_stat = saw_errmsg = false;
3296 if (gfc_pure (NULL))
3298 gfc_error ("Image control statement %s at %C in PURE procedure",
3299 st == ST_LOCK ? "LOCK" : "UNLOCK");
3300 return MATCH_ERROR;
3303 gfc_unset_implicit_pure (NULL);
3305 if (flag_coarray == GFC_FCOARRAY_NONE)
3307 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3308 return MATCH_ERROR;
3311 if (gfc_find_state (COMP_CRITICAL))
3313 gfc_error ("Image control statement %s at %C in CRITICAL block",
3314 st == ST_LOCK ? "LOCK" : "UNLOCK");
3315 return MATCH_ERROR;
3318 if (gfc_find_state (COMP_DO_CONCURRENT))
3320 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
3321 st == ST_LOCK ? "LOCK" : "UNLOCK");
3322 return MATCH_ERROR;
3325 if (gfc_match_char ('(') != MATCH_YES)
3326 goto syntax;
3328 if (gfc_match ("%e", &lockvar) != MATCH_YES)
3329 goto syntax;
3330 m = gfc_match_char (',');
3331 if (m == MATCH_ERROR)
3332 goto syntax;
3333 if (m == MATCH_NO)
3335 m = gfc_match_char (')');
3336 if (m == MATCH_YES)
3337 goto done;
3338 goto syntax;
3341 for (;;)
3343 m = gfc_match (" stat = %v", &tmp);
3344 if (m == MATCH_ERROR)
3345 goto syntax;
3346 if (m == MATCH_YES)
3348 if (saw_stat)
3350 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3351 goto cleanup;
3353 stat = tmp;
3354 saw_stat = true;
3356 m = gfc_match_char (',');
3357 if (m == MATCH_YES)
3358 continue;
3360 tmp = NULL;
3361 break;
3364 m = gfc_match (" errmsg = %v", &tmp);
3365 if (m == MATCH_ERROR)
3366 goto syntax;
3367 if (m == MATCH_YES)
3369 if (saw_errmsg)
3371 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3372 goto cleanup;
3374 errmsg = tmp;
3375 saw_errmsg = true;
3377 m = gfc_match_char (',');
3378 if (m == MATCH_YES)
3379 continue;
3381 tmp = NULL;
3382 break;
3385 m = gfc_match (" acquired_lock = %v", &tmp);
3386 if (m == MATCH_ERROR || st == ST_UNLOCK)
3387 goto syntax;
3388 if (m == MATCH_YES)
3390 if (saw_acq_lock)
3392 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L ",
3393 &tmp->where);
3394 goto cleanup;
3396 acq_lock = tmp;
3397 saw_acq_lock = true;
3399 m = gfc_match_char (',');
3400 if (m == MATCH_YES)
3401 continue;
3403 tmp = NULL;
3404 break;
3407 break;
3410 if (m == MATCH_ERROR)
3411 goto syntax;
3413 if (gfc_match (" )%t") != MATCH_YES)
3414 goto syntax;
3416 done:
3417 switch (st)
3419 case ST_LOCK:
3420 new_st.op = EXEC_LOCK;
3421 break;
3422 case ST_UNLOCK:
3423 new_st.op = EXEC_UNLOCK;
3424 break;
3425 default:
3426 gcc_unreachable ();
3429 new_st.expr1 = lockvar;
3430 new_st.expr2 = stat;
3431 new_st.expr3 = errmsg;
3432 new_st.expr4 = acq_lock;
3434 return MATCH_YES;
3436 syntax:
3437 gfc_syntax_error (st);
3439 cleanup:
3440 if (acq_lock != tmp)
3441 gfc_free_expr (acq_lock);
3442 if (errmsg != tmp)
3443 gfc_free_expr (errmsg);
3444 if (stat != tmp)
3445 gfc_free_expr (stat);
3447 gfc_free_expr (tmp);
3448 gfc_free_expr (lockvar);
3450 return MATCH_ERROR;
3454 match
3455 gfc_match_lock (void)
3457 if (!gfc_notify_std (GFC_STD_F2008, "LOCK statement at %C"))
3458 return MATCH_ERROR;
3460 return lock_unlock_statement (ST_LOCK);
3464 match
3465 gfc_match_unlock (void)
3467 if (!gfc_notify_std (GFC_STD_F2008, "UNLOCK statement at %C"))
3468 return MATCH_ERROR;
3470 return lock_unlock_statement (ST_UNLOCK);
3474 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
3475 SYNC ALL [(sync-stat-list)]
3476 SYNC MEMORY [(sync-stat-list)]
3477 SYNC IMAGES (image-set [, sync-stat-list] )
3478 with sync-stat is int-expr or *. */
3480 static match
3481 sync_statement (gfc_statement st)
3483 match m;
3484 gfc_expr *tmp, *imageset, *stat, *errmsg;
3485 bool saw_stat, saw_errmsg;
3487 tmp = imageset = stat = errmsg = NULL;
3488 saw_stat = saw_errmsg = false;
3490 if (gfc_pure (NULL))
3492 gfc_error ("Image control statement SYNC at %C in PURE procedure");
3493 return MATCH_ERROR;
3496 gfc_unset_implicit_pure (NULL);
3498 if (!gfc_notify_std (GFC_STD_F2008, "SYNC statement at %C"))
3499 return MATCH_ERROR;
3501 if (flag_coarray == GFC_FCOARRAY_NONE)
3503 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
3504 "enable");
3505 return MATCH_ERROR;
3508 if (gfc_find_state (COMP_CRITICAL))
3510 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
3511 return MATCH_ERROR;
3514 if (gfc_find_state (COMP_DO_CONCURRENT))
3516 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
3517 return MATCH_ERROR;
3520 if (gfc_match_eos () == MATCH_YES)
3522 if (st == ST_SYNC_IMAGES)
3523 goto syntax;
3524 goto done;
3527 if (gfc_match_char ('(') != MATCH_YES)
3528 goto syntax;
3530 if (st == ST_SYNC_IMAGES)
3532 /* Denote '*' as imageset == NULL. */
3533 m = gfc_match_char ('*');
3534 if (m == MATCH_ERROR)
3535 goto syntax;
3536 if (m == MATCH_NO)
3538 if (gfc_match ("%e", &imageset) != MATCH_YES)
3539 goto syntax;
3541 m = gfc_match_char (',');
3542 if (m == MATCH_ERROR)
3543 goto syntax;
3544 if (m == MATCH_NO)
3546 m = gfc_match_char (')');
3547 if (m == MATCH_YES)
3548 goto done;
3549 goto syntax;
3553 for (;;)
3555 m = gfc_match (" stat = %v", &tmp);
3556 if (m == MATCH_ERROR)
3557 goto syntax;
3558 if (m == MATCH_YES)
3560 if (saw_stat)
3562 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3563 goto cleanup;
3565 stat = tmp;
3566 saw_stat = true;
3568 if (gfc_match_char (',') == MATCH_YES)
3569 continue;
3571 tmp = NULL;
3572 break;
3575 m = gfc_match (" errmsg = %v", &tmp);
3576 if (m == MATCH_ERROR)
3577 goto syntax;
3578 if (m == MATCH_YES)
3580 if (saw_errmsg)
3582 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3583 goto cleanup;
3585 errmsg = tmp;
3586 saw_errmsg = true;
3588 if (gfc_match_char (',') == MATCH_YES)
3589 continue;
3591 tmp = NULL;
3592 break;
3595 break;
3598 if (gfc_match (" )%t") != MATCH_YES)
3599 goto syntax;
3601 done:
3602 switch (st)
3604 case ST_SYNC_ALL:
3605 new_st.op = EXEC_SYNC_ALL;
3606 break;
3607 case ST_SYNC_IMAGES:
3608 new_st.op = EXEC_SYNC_IMAGES;
3609 break;
3610 case ST_SYNC_MEMORY:
3611 new_st.op = EXEC_SYNC_MEMORY;
3612 break;
3613 default:
3614 gcc_unreachable ();
3617 new_st.expr1 = imageset;
3618 new_st.expr2 = stat;
3619 new_st.expr3 = errmsg;
3621 return MATCH_YES;
3623 syntax:
3624 gfc_syntax_error (st);
3626 cleanup:
3627 if (stat != tmp)
3628 gfc_free_expr (stat);
3629 if (errmsg != tmp)
3630 gfc_free_expr (errmsg);
3632 gfc_free_expr (tmp);
3633 gfc_free_expr (imageset);
3635 return MATCH_ERROR;
3639 /* Match SYNC ALL statement. */
3641 match
3642 gfc_match_sync_all (void)
3644 return sync_statement (ST_SYNC_ALL);
3648 /* Match SYNC IMAGES statement. */
3650 match
3651 gfc_match_sync_images (void)
3653 return sync_statement (ST_SYNC_IMAGES);
3657 /* Match SYNC MEMORY statement. */
3659 match
3660 gfc_match_sync_memory (void)
3662 return sync_statement (ST_SYNC_MEMORY);
3666 /* Match a CONTINUE statement. */
3668 match
3669 gfc_match_continue (void)
3671 if (gfc_match_eos () != MATCH_YES)
3673 gfc_syntax_error (ST_CONTINUE);
3674 return MATCH_ERROR;
3677 new_st.op = EXEC_CONTINUE;
3678 return MATCH_YES;
3682 /* Match the (deprecated) ASSIGN statement. */
3684 match
3685 gfc_match_assign (void)
3687 gfc_expr *expr;
3688 gfc_st_label *label;
3690 if (gfc_match (" %l", &label) == MATCH_YES)
3692 if (!gfc_reference_st_label (label, ST_LABEL_UNKNOWN))
3693 return MATCH_ERROR;
3694 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
3696 if (!gfc_notify_std (GFC_STD_F95_DEL, "ASSIGN statement at %C"))
3697 return MATCH_ERROR;
3699 expr->symtree->n.sym->attr.assign = 1;
3701 new_st.op = EXEC_LABEL_ASSIGN;
3702 new_st.label1 = label;
3703 new_st.expr1 = expr;
3704 return MATCH_YES;
3707 return MATCH_NO;
3711 /* Match the GO TO statement. As a computed GOTO statement is
3712 matched, it is transformed into an equivalent SELECT block. No
3713 tree is necessary, and the resulting jumps-to-jumps are
3714 specifically optimized away by the back end. */
3716 match
3717 gfc_match_goto (void)
3719 gfc_code *head, *tail;
3720 gfc_expr *expr;
3721 gfc_case *cp;
3722 gfc_st_label *label;
3723 int i;
3724 match m;
3726 if (gfc_match (" %l%t", &label) == MATCH_YES)
3728 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3729 return MATCH_ERROR;
3731 new_st.op = EXEC_GOTO;
3732 new_st.label1 = label;
3733 return MATCH_YES;
3736 /* The assigned GO TO statement. */
3738 if (gfc_match_variable (&expr, 0) == MATCH_YES)
3740 if (!gfc_notify_std (GFC_STD_F95_DEL, "Assigned GOTO statement at %C"))
3741 return MATCH_ERROR;
3743 new_st.op = EXEC_GOTO;
3744 new_st.expr1 = expr;
3746 if (gfc_match_eos () == MATCH_YES)
3747 return MATCH_YES;
3749 /* Match label list. */
3750 gfc_match_char (',');
3751 if (gfc_match_char ('(') != MATCH_YES)
3753 gfc_syntax_error (ST_GOTO);
3754 return MATCH_ERROR;
3756 head = tail = NULL;
3760 m = gfc_match_st_label (&label);
3761 if (m != MATCH_YES)
3762 goto syntax;
3764 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3765 goto cleanup;
3767 if (head == NULL)
3768 head = tail = gfc_get_code (EXEC_GOTO);
3769 else
3771 tail->block = gfc_get_code (EXEC_GOTO);
3772 tail = tail->block;
3775 tail->label1 = label;
3777 while (gfc_match_char (',') == MATCH_YES);
3779 if (gfc_match (")%t") != MATCH_YES)
3780 goto syntax;
3782 if (head == NULL)
3784 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3785 goto syntax;
3787 new_st.block = head;
3789 return MATCH_YES;
3792 /* Last chance is a computed GO TO statement. */
3793 if (gfc_match_char ('(') != MATCH_YES)
3795 gfc_syntax_error (ST_GOTO);
3796 return MATCH_ERROR;
3799 head = tail = NULL;
3800 i = 1;
3804 m = gfc_match_st_label (&label);
3805 if (m != MATCH_YES)
3806 goto syntax;
3808 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3809 goto cleanup;
3811 if (head == NULL)
3812 head = tail = gfc_get_code (EXEC_SELECT);
3813 else
3815 tail->block = gfc_get_code (EXEC_SELECT);
3816 tail = tail->block;
3819 cp = gfc_get_case ();
3820 cp->low = cp->high = gfc_get_int_expr (gfc_default_integer_kind,
3821 NULL, i++);
3823 tail->ext.block.case_list = cp;
3825 tail->next = gfc_get_code (EXEC_GOTO);
3826 tail->next->label1 = label;
3828 while (gfc_match_char (',') == MATCH_YES);
3830 if (gfc_match_char (')') != MATCH_YES)
3831 goto syntax;
3833 if (head == NULL)
3835 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3836 goto syntax;
3839 /* Get the rest of the statement. */
3840 gfc_match_char (',');
3842 if (gfc_match (" %e%t", &expr) != MATCH_YES)
3843 goto syntax;
3845 if (!gfc_notify_std (GFC_STD_F95_OBS, "Computed GOTO at %C"))
3846 return MATCH_ERROR;
3848 /* At this point, a computed GOTO has been fully matched and an
3849 equivalent SELECT statement constructed. */
3851 new_st.op = EXEC_SELECT;
3852 new_st.expr1 = NULL;
3854 /* Hack: For a "real" SELECT, the expression is in expr. We put
3855 it in expr2 so we can distinguish then and produce the correct
3856 diagnostics. */
3857 new_st.expr2 = expr;
3858 new_st.block = head;
3859 return MATCH_YES;
3861 syntax:
3862 gfc_syntax_error (ST_GOTO);
3863 cleanup:
3864 gfc_free_statements (head);
3865 return MATCH_ERROR;
3869 /* Frees a list of gfc_alloc structures. */
3871 void
3872 gfc_free_alloc_list (gfc_alloc *p)
3874 gfc_alloc *q;
3876 for (; p; p = q)
3878 q = p->next;
3879 gfc_free_expr (p->expr);
3880 free (p);
3885 /* Match an ALLOCATE statement. */
3887 match
3888 gfc_match_allocate (void)
3890 gfc_alloc *head, *tail;
3891 gfc_expr *stat, *errmsg, *tmp, *source, *mold;
3892 gfc_typespec ts;
3893 gfc_symbol *sym;
3894 match m;
3895 locus old_locus, deferred_locus;
3896 bool saw_stat, saw_errmsg, saw_source, saw_mold, saw_deferred, b1, b2, b3;
3897 bool saw_unlimited = false;
3899 head = tail = NULL;
3900 stat = errmsg = source = mold = tmp = NULL;
3901 saw_stat = saw_errmsg = saw_source = saw_mold = saw_deferred = false;
3903 if (gfc_match_char ('(') != MATCH_YES)
3904 goto syntax;
3906 /* Match an optional type-spec. */
3907 old_locus = gfc_current_locus;
3908 m = gfc_match_type_spec (&ts);
3909 if (m == MATCH_ERROR)
3910 goto cleanup;
3911 else if (m == MATCH_NO)
3913 char name[GFC_MAX_SYMBOL_LEN + 3];
3915 if (gfc_match ("%n :: ", name) == MATCH_YES)
3917 gfc_error ("Error in type-spec at %L", &old_locus);
3918 goto cleanup;
3921 ts.type = BT_UNKNOWN;
3923 else
3925 if (gfc_match (" :: ") == MATCH_YES)
3927 if (!gfc_notify_std (GFC_STD_F2003, "typespec in ALLOCATE at %L",
3928 &old_locus))
3929 goto cleanup;
3931 if (ts.deferred)
3933 gfc_error ("Type-spec at %L cannot contain a deferred "
3934 "type parameter", &old_locus);
3935 goto cleanup;
3938 if (ts.type == BT_CHARACTER)
3939 ts.u.cl->length_from_typespec = true;
3941 else
3943 ts.type = BT_UNKNOWN;
3944 gfc_current_locus = old_locus;
3948 for (;;)
3950 if (head == NULL)
3951 head = tail = gfc_get_alloc ();
3952 else
3954 tail->next = gfc_get_alloc ();
3955 tail = tail->next;
3958 m = gfc_match_variable (&tail->expr, 0);
3959 if (m == MATCH_NO)
3960 goto syntax;
3961 if (m == MATCH_ERROR)
3962 goto cleanup;
3964 if (gfc_check_do_variable (tail->expr->symtree))
3965 goto cleanup;
3967 bool impure = gfc_impure_variable (tail->expr->symtree->n.sym);
3968 if (impure && gfc_pure (NULL))
3970 gfc_error ("Bad allocate-object at %C for a PURE procedure");
3971 goto cleanup;
3974 if (impure)
3975 gfc_unset_implicit_pure (NULL);
3977 if (tail->expr->ts.deferred)
3979 saw_deferred = true;
3980 deferred_locus = tail->expr->where;
3983 if (gfc_find_state (COMP_DO_CONCURRENT)
3984 || gfc_find_state (COMP_CRITICAL))
3986 gfc_ref *ref;
3987 bool coarray = tail->expr->symtree->n.sym->attr.codimension;
3988 for (ref = tail->expr->ref; ref; ref = ref->next)
3989 if (ref->type == REF_COMPONENT)
3990 coarray = ref->u.c.component->attr.codimension;
3992 if (coarray && gfc_find_state (COMP_DO_CONCURRENT))
3994 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
3995 goto cleanup;
3997 if (coarray && gfc_find_state (COMP_CRITICAL))
3999 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
4000 goto cleanup;
4004 /* Check for F08:C628. */
4005 sym = tail->expr->symtree->n.sym;
4006 b1 = !(tail->expr->ref
4007 && (tail->expr->ref->type == REF_COMPONENT
4008 || tail->expr->ref->type == REF_ARRAY));
4009 if (sym && sym->ts.type == BT_CLASS && sym->attr.class_ok)
4010 b2 = !(CLASS_DATA (sym)->attr.allocatable
4011 || CLASS_DATA (sym)->attr.class_pointer);
4012 else
4013 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4014 || sym->attr.proc_pointer);
4015 b3 = sym && sym->ns && sym->ns->proc_name
4016 && (sym->ns->proc_name->attr.allocatable
4017 || sym->ns->proc_name->attr.pointer
4018 || sym->ns->proc_name->attr.proc_pointer);
4019 if (b1 && b2 && !b3)
4021 gfc_error ("Allocate-object at %L is neither a data pointer "
4022 "nor an allocatable variable", &tail->expr->where);
4023 goto cleanup;
4026 /* The ALLOCATE statement had an optional typespec. Check the
4027 constraints. */
4028 if (ts.type != BT_UNKNOWN)
4030 /* Enforce F03:C624. */
4031 if (!gfc_type_compatible (&tail->expr->ts, &ts))
4033 gfc_error ("Type of entity at %L is type incompatible with "
4034 "typespec", &tail->expr->where);
4035 goto cleanup;
4038 /* Enforce F03:C627. */
4039 if (ts.kind != tail->expr->ts.kind && !UNLIMITED_POLY (tail->expr))
4041 gfc_error ("Kind type parameter for entity at %L differs from "
4042 "the kind type parameter of the typespec",
4043 &tail->expr->where);
4044 goto cleanup;
4048 if (tail->expr->ts.type == BT_DERIVED)
4049 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
4051 saw_unlimited = saw_unlimited | UNLIMITED_POLY (tail->expr);
4053 if (gfc_peek_ascii_char () == '(' && !sym->attr.dimension)
4055 gfc_error ("Shape specification for allocatable scalar at %C");
4056 goto cleanup;
4059 if (gfc_match_char (',') != MATCH_YES)
4060 break;
4062 alloc_opt_list:
4064 m = gfc_match (" stat = %v", &tmp);
4065 if (m == MATCH_ERROR)
4066 goto cleanup;
4067 if (m == MATCH_YES)
4069 /* Enforce C630. */
4070 if (saw_stat)
4072 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
4073 goto cleanup;
4076 stat = tmp;
4077 tmp = NULL;
4078 saw_stat = true;
4080 if (gfc_check_do_variable (stat->symtree))
4081 goto cleanup;
4083 if (gfc_match_char (',') == MATCH_YES)
4084 goto alloc_opt_list;
4087 m = gfc_match (" errmsg = %v", &tmp);
4088 if (m == MATCH_ERROR)
4089 goto cleanup;
4090 if (m == MATCH_YES)
4092 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG tag at %L", &tmp->where))
4093 goto cleanup;
4095 /* Enforce C630. */
4096 if (saw_errmsg)
4098 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
4099 goto cleanup;
4102 errmsg = tmp;
4103 tmp = NULL;
4104 saw_errmsg = true;
4106 if (gfc_match_char (',') == MATCH_YES)
4107 goto alloc_opt_list;
4110 m = gfc_match (" source = %e", &tmp);
4111 if (m == MATCH_ERROR)
4112 goto cleanup;
4113 if (m == MATCH_YES)
4115 if (!gfc_notify_std (GFC_STD_F2003, "SOURCE tag at %L", &tmp->where))
4116 goto cleanup;
4118 /* Enforce C630. */
4119 if (saw_source)
4121 gfc_error ("Redundant SOURCE tag found at %L ", &tmp->where);
4122 goto cleanup;
4125 /* The next 2 conditionals check C631. */
4126 if (ts.type != BT_UNKNOWN)
4128 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
4129 &tmp->where, &old_locus);
4130 goto cleanup;
4133 if (head->next
4134 && !gfc_notify_std (GFC_STD_F2008, "SOURCE tag at %L"
4135 " with more than a single allocate object",
4136 &tmp->where))
4137 goto cleanup;
4139 source = tmp;
4140 tmp = NULL;
4141 saw_source = true;
4143 if (gfc_match_char (',') == MATCH_YES)
4144 goto alloc_opt_list;
4147 m = gfc_match (" mold = %e", &tmp);
4148 if (m == MATCH_ERROR)
4149 goto cleanup;
4150 if (m == MATCH_YES)
4152 if (!gfc_notify_std (GFC_STD_F2008, "MOLD tag at %L", &tmp->where))
4153 goto cleanup;
4155 /* Check F08:C636. */
4156 if (saw_mold)
4158 gfc_error ("Redundant MOLD tag found at %L ", &tmp->where);
4159 goto cleanup;
4162 /* Check F08:C637. */
4163 if (ts.type != BT_UNKNOWN)
4165 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
4166 &tmp->where, &old_locus);
4167 goto cleanup;
4170 mold = tmp;
4171 tmp = NULL;
4172 saw_mold = true;
4173 mold->mold = 1;
4175 if (gfc_match_char (',') == MATCH_YES)
4176 goto alloc_opt_list;
4179 gfc_gobble_whitespace ();
4181 if (gfc_peek_char () == ')')
4182 break;
4185 if (gfc_match (" )%t") != MATCH_YES)
4186 goto syntax;
4188 /* Check F08:C637. */
4189 if (source && mold)
4191 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
4192 &mold->where, &source->where);
4193 goto cleanup;
4196 /* Check F03:C623, */
4197 if (saw_deferred && ts.type == BT_UNKNOWN && !source && !mold)
4199 gfc_error ("Allocate-object at %L with a deferred type parameter "
4200 "requires either a type-spec or SOURCE tag or a MOLD tag",
4201 &deferred_locus);
4202 goto cleanup;
4205 /* Check F03:C625, */
4206 if (saw_unlimited && ts.type == BT_UNKNOWN && !source && !mold)
4208 for (tail = head; tail; tail = tail->next)
4210 if (UNLIMITED_POLY (tail->expr))
4211 gfc_error ("Unlimited polymorphic allocate-object at %L "
4212 "requires either a type-spec or SOURCE tag "
4213 "or a MOLD tag", &tail->expr->where);
4215 goto cleanup;
4218 new_st.op = EXEC_ALLOCATE;
4219 new_st.expr1 = stat;
4220 new_st.expr2 = errmsg;
4221 if (source)
4222 new_st.expr3 = source;
4223 else
4224 new_st.expr3 = mold;
4225 new_st.ext.alloc.list = head;
4226 new_st.ext.alloc.ts = ts;
4228 return MATCH_YES;
4230 syntax:
4231 gfc_syntax_error (ST_ALLOCATE);
4233 cleanup:
4234 gfc_free_expr (errmsg);
4235 gfc_free_expr (source);
4236 gfc_free_expr (stat);
4237 gfc_free_expr (mold);
4238 if (tmp && tmp->expr_type) gfc_free_expr (tmp);
4239 gfc_free_alloc_list (head);
4240 return MATCH_ERROR;
4244 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
4245 a set of pointer assignments to intrinsic NULL(). */
4247 match
4248 gfc_match_nullify (void)
4250 gfc_code *tail;
4251 gfc_expr *e, *p;
4252 match m;
4254 tail = NULL;
4256 if (gfc_match_char ('(') != MATCH_YES)
4257 goto syntax;
4259 for (;;)
4261 m = gfc_match_variable (&p, 0);
4262 if (m == MATCH_ERROR)
4263 goto cleanup;
4264 if (m == MATCH_NO)
4265 goto syntax;
4267 if (gfc_check_do_variable (p->symtree))
4268 goto cleanup;
4270 /* F2008, C1242. */
4271 if (gfc_is_coindexed (p))
4273 gfc_error ("Pointer object at %C shall not be coindexed");
4274 goto cleanup;
4277 /* build ' => NULL() '. */
4278 e = gfc_get_null_expr (&gfc_current_locus);
4280 /* Chain to list. */
4281 if (tail == NULL)
4283 tail = &new_st;
4284 tail->op = EXEC_POINTER_ASSIGN;
4286 else
4288 tail->next = gfc_get_code (EXEC_POINTER_ASSIGN);
4289 tail = tail->next;
4292 tail->expr1 = p;
4293 tail->expr2 = e;
4295 if (gfc_match (" )%t") == MATCH_YES)
4296 break;
4297 if (gfc_match_char (',') != MATCH_YES)
4298 goto syntax;
4301 return MATCH_YES;
4303 syntax:
4304 gfc_syntax_error (ST_NULLIFY);
4306 cleanup:
4307 gfc_free_statements (new_st.next);
4308 new_st.next = NULL;
4309 gfc_free_expr (new_st.expr1);
4310 new_st.expr1 = NULL;
4311 gfc_free_expr (new_st.expr2);
4312 new_st.expr2 = NULL;
4313 return MATCH_ERROR;
4317 /* Match a DEALLOCATE statement. */
4319 match
4320 gfc_match_deallocate (void)
4322 gfc_alloc *head, *tail;
4323 gfc_expr *stat, *errmsg, *tmp;
4324 gfc_symbol *sym;
4325 match m;
4326 bool saw_stat, saw_errmsg, b1, b2;
4328 head = tail = NULL;
4329 stat = errmsg = tmp = NULL;
4330 saw_stat = saw_errmsg = false;
4332 if (gfc_match_char ('(') != MATCH_YES)
4333 goto syntax;
4335 for (;;)
4337 if (head == NULL)
4338 head = tail = gfc_get_alloc ();
4339 else
4341 tail->next = gfc_get_alloc ();
4342 tail = tail->next;
4345 m = gfc_match_variable (&tail->expr, 0);
4346 if (m == MATCH_ERROR)
4347 goto cleanup;
4348 if (m == MATCH_NO)
4349 goto syntax;
4351 if (gfc_check_do_variable (tail->expr->symtree))
4352 goto cleanup;
4354 sym = tail->expr->symtree->n.sym;
4356 bool impure = gfc_impure_variable (sym);
4357 if (impure && gfc_pure (NULL))
4359 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
4360 goto cleanup;
4363 if (impure)
4364 gfc_unset_implicit_pure (NULL);
4366 if (gfc_is_coarray (tail->expr)
4367 && gfc_find_state (COMP_DO_CONCURRENT))
4369 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
4370 goto cleanup;
4373 if (gfc_is_coarray (tail->expr)
4374 && gfc_find_state (COMP_CRITICAL))
4376 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
4377 goto cleanup;
4380 /* FIXME: disable the checking on derived types. */
4381 b1 = !(tail->expr->ref
4382 && (tail->expr->ref->type == REF_COMPONENT
4383 || tail->expr->ref->type == REF_ARRAY));
4384 if (sym && sym->ts.type == BT_CLASS)
4385 b2 = !(CLASS_DATA (sym)->attr.allocatable
4386 || CLASS_DATA (sym)->attr.class_pointer);
4387 else
4388 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4389 || sym->attr.proc_pointer);
4390 if (b1 && b2)
4392 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
4393 "nor an allocatable variable");
4394 goto cleanup;
4397 if (gfc_match_char (',') != MATCH_YES)
4398 break;
4400 dealloc_opt_list:
4402 m = gfc_match (" stat = %v", &tmp);
4403 if (m == MATCH_ERROR)
4404 goto cleanup;
4405 if (m == MATCH_YES)
4407 if (saw_stat)
4409 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
4410 gfc_free_expr (tmp);
4411 goto cleanup;
4414 stat = tmp;
4415 saw_stat = true;
4417 if (gfc_check_do_variable (stat->symtree))
4418 goto cleanup;
4420 if (gfc_match_char (',') == MATCH_YES)
4421 goto dealloc_opt_list;
4424 m = gfc_match (" errmsg = %v", &tmp);
4425 if (m == MATCH_ERROR)
4426 goto cleanup;
4427 if (m == MATCH_YES)
4429 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG at %L", &tmp->where))
4430 goto cleanup;
4432 if (saw_errmsg)
4434 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
4435 gfc_free_expr (tmp);
4436 goto cleanup;
4439 errmsg = tmp;
4440 saw_errmsg = true;
4442 if (gfc_match_char (',') == MATCH_YES)
4443 goto dealloc_opt_list;
4446 gfc_gobble_whitespace ();
4448 if (gfc_peek_char () == ')')
4449 break;
4452 if (gfc_match (" )%t") != MATCH_YES)
4453 goto syntax;
4455 new_st.op = EXEC_DEALLOCATE;
4456 new_st.expr1 = stat;
4457 new_st.expr2 = errmsg;
4458 new_st.ext.alloc.list = head;
4460 return MATCH_YES;
4462 syntax:
4463 gfc_syntax_error (ST_DEALLOCATE);
4465 cleanup:
4466 gfc_free_expr (errmsg);
4467 gfc_free_expr (stat);
4468 gfc_free_alloc_list (head);
4469 return MATCH_ERROR;
4473 /* Match a RETURN statement. */
4475 match
4476 gfc_match_return (void)
4478 gfc_expr *e;
4479 match m;
4480 gfc_compile_state s;
4482 e = NULL;
4484 if (gfc_find_state (COMP_CRITICAL))
4486 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
4487 return MATCH_ERROR;
4490 if (gfc_find_state (COMP_DO_CONCURRENT))
4492 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
4493 return MATCH_ERROR;
4496 if (gfc_match_eos () == MATCH_YES)
4497 goto done;
4499 if (!gfc_find_state (COMP_SUBROUTINE))
4501 gfc_error ("Alternate RETURN statement at %C is only allowed within "
4502 "a SUBROUTINE");
4503 goto cleanup;
4506 if (gfc_current_form == FORM_FREE)
4508 /* The following are valid, so we can't require a blank after the
4509 RETURN keyword:
4510 return+1
4511 return(1) */
4512 char c = gfc_peek_ascii_char ();
4513 if (ISALPHA (c) || ISDIGIT (c))
4514 return MATCH_NO;
4517 m = gfc_match (" %e%t", &e);
4518 if (m == MATCH_YES)
4519 goto done;
4520 if (m == MATCH_ERROR)
4521 goto cleanup;
4523 gfc_syntax_error (ST_RETURN);
4525 cleanup:
4526 gfc_free_expr (e);
4527 return MATCH_ERROR;
4529 done:
4530 gfc_enclosing_unit (&s);
4531 if (s == COMP_PROGRAM
4532 && !gfc_notify_std (GFC_STD_GNU, "RETURN statement in "
4533 "main program at %C"))
4534 return MATCH_ERROR;
4536 new_st.op = EXEC_RETURN;
4537 new_st.expr1 = e;
4539 return MATCH_YES;
4543 /* Match the call of a type-bound procedure, if CALL%var has already been
4544 matched and var found to be a derived-type variable. */
4546 static match
4547 match_typebound_call (gfc_symtree* varst)
4549 gfc_expr* base;
4550 match m;
4552 base = gfc_get_expr ();
4553 base->expr_type = EXPR_VARIABLE;
4554 base->symtree = varst;
4555 base->where = gfc_current_locus;
4556 gfc_set_sym_referenced (varst->n.sym);
4558 m = gfc_match_varspec (base, 0, true, true);
4559 if (m == MATCH_NO)
4560 gfc_error ("Expected component reference at %C");
4561 if (m != MATCH_YES)
4563 gfc_free_expr (base);
4564 return MATCH_ERROR;
4567 if (gfc_match_eos () != MATCH_YES)
4569 gfc_error ("Junk after CALL at %C");
4570 gfc_free_expr (base);
4571 return MATCH_ERROR;
4574 if (base->expr_type == EXPR_COMPCALL)
4575 new_st.op = EXEC_COMPCALL;
4576 else if (base->expr_type == EXPR_PPC)
4577 new_st.op = EXEC_CALL_PPC;
4578 else
4580 gfc_error ("Expected type-bound procedure or procedure pointer component "
4581 "at %C");
4582 gfc_free_expr (base);
4583 return MATCH_ERROR;
4585 new_st.expr1 = base;
4587 return MATCH_YES;
4591 /* Match a CALL statement. The tricky part here are possible
4592 alternate return specifiers. We handle these by having all
4593 "subroutines" actually return an integer via a register that gives
4594 the return number. If the call specifies alternate returns, we
4595 generate code for a SELECT statement whose case clauses contain
4596 GOTOs to the various labels. */
4598 match
4599 gfc_match_call (void)
4601 char name[GFC_MAX_SYMBOL_LEN + 1];
4602 gfc_actual_arglist *a, *arglist;
4603 gfc_case *new_case;
4604 gfc_symbol *sym;
4605 gfc_symtree *st;
4606 gfc_code *c;
4607 match m;
4608 int i;
4610 arglist = NULL;
4612 m = gfc_match ("% %n", name);
4613 if (m == MATCH_NO)
4614 goto syntax;
4615 if (m != MATCH_YES)
4616 return m;
4618 if (gfc_get_ha_sym_tree (name, &st))
4619 return MATCH_ERROR;
4621 sym = st->n.sym;
4623 /* If this is a variable of derived-type, it probably starts a type-bound
4624 procedure call. */
4625 if ((sym->attr.flavor != FL_PROCEDURE
4626 || gfc_is_function_return_value (sym, gfc_current_ns))
4627 && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS))
4628 return match_typebound_call (st);
4630 /* If it does not seem to be callable (include functions so that the
4631 right association is made. They are thrown out in resolution.)
4632 ... */
4633 if (!sym->attr.generic
4634 && !sym->attr.subroutine
4635 && !sym->attr.function)
4637 if (!(sym->attr.external && !sym->attr.referenced))
4639 /* ...create a symbol in this scope... */
4640 if (sym->ns != gfc_current_ns
4641 && gfc_get_sym_tree (name, NULL, &st, false) == 1)
4642 return MATCH_ERROR;
4644 if (sym != st->n.sym)
4645 sym = st->n.sym;
4648 /* ...and then to try to make the symbol into a subroutine. */
4649 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
4650 return MATCH_ERROR;
4653 gfc_set_sym_referenced (sym);
4655 if (gfc_match_eos () != MATCH_YES)
4657 m = gfc_match_actual_arglist (1, &arglist);
4658 if (m == MATCH_NO)
4659 goto syntax;
4660 if (m == MATCH_ERROR)
4661 goto cleanup;
4663 if (gfc_match_eos () != MATCH_YES)
4664 goto syntax;
4667 /* If any alternate return labels were found, construct a SELECT
4668 statement that will jump to the right place. */
4670 i = 0;
4671 for (a = arglist; a; a = a->next)
4672 if (a->expr == NULL)
4674 i = 1;
4675 break;
4678 if (i)
4680 gfc_symtree *select_st;
4681 gfc_symbol *select_sym;
4682 char name[GFC_MAX_SYMBOL_LEN + 1];
4684 new_st.next = c = gfc_get_code (EXEC_SELECT);
4685 sprintf (name, "_result_%s", sym->name);
4686 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
4688 select_sym = select_st->n.sym;
4689 select_sym->ts.type = BT_INTEGER;
4690 select_sym->ts.kind = gfc_default_integer_kind;
4691 gfc_set_sym_referenced (select_sym);
4692 c->expr1 = gfc_get_expr ();
4693 c->expr1->expr_type = EXPR_VARIABLE;
4694 c->expr1->symtree = select_st;
4695 c->expr1->ts = select_sym->ts;
4696 c->expr1->where = gfc_current_locus;
4698 i = 0;
4699 for (a = arglist; a; a = a->next)
4701 if (a->expr != NULL)
4702 continue;
4704 if (!gfc_reference_st_label (a->label, ST_LABEL_TARGET))
4705 continue;
4707 i++;
4709 c->block = gfc_get_code (EXEC_SELECT);
4710 c = c->block;
4712 new_case = gfc_get_case ();
4713 new_case->high = gfc_get_int_expr (gfc_default_integer_kind, NULL, i);
4714 new_case->low = new_case->high;
4715 c->ext.block.case_list = new_case;
4717 c->next = gfc_get_code (EXEC_GOTO);
4718 c->next->label1 = a->label;
4722 new_st.op = EXEC_CALL;
4723 new_st.symtree = st;
4724 new_st.ext.actual = arglist;
4726 return MATCH_YES;
4728 syntax:
4729 gfc_syntax_error (ST_CALL);
4731 cleanup:
4732 gfc_free_actual_arglist (arglist);
4733 return MATCH_ERROR;
4737 /* Given a name, return a pointer to the common head structure,
4738 creating it if it does not exist. If FROM_MODULE is nonzero, we
4739 mangle the name so that it doesn't interfere with commons defined
4740 in the using namespace.
4741 TODO: Add to global symbol tree. */
4743 gfc_common_head *
4744 gfc_get_common (const char *name, int from_module)
4746 gfc_symtree *st;
4747 static int serial = 0;
4748 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
4750 if (from_module)
4752 /* A use associated common block is only needed to correctly layout
4753 the variables it contains. */
4754 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
4755 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
4757 else
4759 st = gfc_find_symtree (gfc_current_ns->common_root, name);
4761 if (st == NULL)
4762 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
4765 if (st->n.common == NULL)
4767 st->n.common = gfc_get_common_head ();
4768 st->n.common->where = gfc_current_locus;
4769 strcpy (st->n.common->name, name);
4772 return st->n.common;
4776 /* Match a common block name. */
4778 match match_common_name (char *name)
4780 match m;
4782 if (gfc_match_char ('/') == MATCH_NO)
4784 name[0] = '\0';
4785 return MATCH_YES;
4788 if (gfc_match_char ('/') == MATCH_YES)
4790 name[0] = '\0';
4791 return MATCH_YES;
4794 m = gfc_match_name (name);
4796 if (m == MATCH_ERROR)
4797 return MATCH_ERROR;
4798 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
4799 return MATCH_YES;
4801 gfc_error ("Syntax error in common block name at %C");
4802 return MATCH_ERROR;
4806 /* Match a COMMON statement. */
4808 match
4809 gfc_match_common (void)
4811 gfc_symbol *sym, **head, *tail, *other;
4812 char name[GFC_MAX_SYMBOL_LEN + 1];
4813 gfc_common_head *t;
4814 gfc_array_spec *as;
4815 gfc_equiv *e1, *e2;
4816 match m;
4818 as = NULL;
4820 for (;;)
4822 m = match_common_name (name);
4823 if (m == MATCH_ERROR)
4824 goto cleanup;
4826 if (name[0] == '\0')
4828 t = &gfc_current_ns->blank_common;
4829 if (t->head == NULL)
4830 t->where = gfc_current_locus;
4832 else
4834 t = gfc_get_common (name, 0);
4836 head = &t->head;
4838 if (*head == NULL)
4839 tail = NULL;
4840 else
4842 tail = *head;
4843 while (tail->common_next)
4844 tail = tail->common_next;
4847 /* Grab the list of symbols. */
4848 for (;;)
4850 m = gfc_match_symbol (&sym, 0);
4851 if (m == MATCH_ERROR)
4852 goto cleanup;
4853 if (m == MATCH_NO)
4854 goto syntax;
4856 /* See if we know the current common block is bind(c), and if
4857 so, then see if we can check if the symbol is (which it'll
4858 need to be). This can happen if the bind(c) attr stmt was
4859 applied to the common block, and the variable(s) already
4860 defined, before declaring the common block. */
4861 if (t->is_bind_c == 1)
4863 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
4865 /* If we find an error, just print it and continue,
4866 cause it's just semantic, and we can see if there
4867 are more errors. */
4868 gfc_error_now ("Variable %qs at %L in common block %qs "
4869 "at %C must be declared with a C "
4870 "interoperable kind since common block "
4871 "%qs is bind(c)",
4872 sym->name, &(sym->declared_at), t->name,
4873 t->name);
4876 if (sym->attr.is_bind_c == 1)
4877 gfc_error_now ("Variable %qs in common block %qs at %C can not "
4878 "be bind(c) since it is not global", sym->name,
4879 t->name);
4882 if (sym->attr.in_common)
4884 gfc_error ("Symbol %qs at %C is already in a COMMON block",
4885 sym->name);
4886 goto cleanup;
4889 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
4890 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
4892 if (!gfc_notify_std (GFC_STD_GNU, "Initialized symbol %qs at "
4893 "%C can only be COMMON in BLOCK DATA",
4894 sym->name))
4895 goto cleanup;
4898 /* Deal with an optional array specification after the
4899 symbol name. */
4900 m = gfc_match_array_spec (&as, true, true);
4901 if (m == MATCH_ERROR)
4902 goto cleanup;
4904 if (m == MATCH_YES)
4906 if (as->type != AS_EXPLICIT)
4908 gfc_error ("Array specification for symbol %qs in COMMON "
4909 "at %C must be explicit", sym->name);
4910 goto cleanup;
4913 if (!gfc_add_dimension (&sym->attr, sym->name, NULL))
4914 goto cleanup;
4916 if (sym->attr.pointer)
4918 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
4919 "POINTER array", sym->name);
4920 goto cleanup;
4923 sym->as = as;
4924 as = NULL;
4928 /* Add the in_common attribute, but ignore the reported errors
4929 if any, and continue matching. */
4930 gfc_add_in_common (&sym->attr, sym->name, NULL);
4932 sym->common_block = t;
4933 sym->common_block->refs++;
4935 if (tail != NULL)
4936 tail->common_next = sym;
4937 else
4938 *head = sym;
4940 tail = sym;
4942 sym->common_head = t;
4944 /* Check to see if the symbol is already in an equivalence group.
4945 If it is, set the other members as being in common. */
4946 if (sym->attr.in_equivalence)
4948 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
4950 for (e2 = e1; e2; e2 = e2->eq)
4951 if (e2->expr->symtree->n.sym == sym)
4952 goto equiv_found;
4954 continue;
4956 equiv_found:
4958 for (e2 = e1; e2; e2 = e2->eq)
4960 other = e2->expr->symtree->n.sym;
4961 if (other->common_head
4962 && other->common_head != sym->common_head)
4964 gfc_error ("Symbol %qs, in COMMON block %qs at "
4965 "%C is being indirectly equivalenced to "
4966 "another COMMON block %qs",
4967 sym->name, sym->common_head->name,
4968 other->common_head->name);
4969 goto cleanup;
4971 other->attr.in_common = 1;
4972 other->common_head = t;
4978 gfc_gobble_whitespace ();
4979 if (gfc_match_eos () == MATCH_YES)
4980 goto done;
4981 if (gfc_peek_ascii_char () == '/')
4982 break;
4983 if (gfc_match_char (',') != MATCH_YES)
4984 goto syntax;
4985 gfc_gobble_whitespace ();
4986 if (gfc_peek_ascii_char () == '/')
4987 break;
4991 done:
4992 return MATCH_YES;
4994 syntax:
4995 gfc_syntax_error (ST_COMMON);
4997 cleanup:
4998 gfc_free_array_spec (as);
4999 return MATCH_ERROR;
5003 /* Match a BLOCK DATA program unit. */
5005 match
5006 gfc_match_block_data (void)
5008 char name[GFC_MAX_SYMBOL_LEN + 1];
5009 gfc_symbol *sym;
5010 match m;
5012 if (gfc_match_eos () == MATCH_YES)
5014 gfc_new_block = NULL;
5015 return MATCH_YES;
5018 m = gfc_match ("% %n%t", name);
5019 if (m != MATCH_YES)
5020 return MATCH_ERROR;
5022 if (gfc_get_symbol (name, NULL, &sym))
5023 return MATCH_ERROR;
5025 if (!gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL))
5026 return MATCH_ERROR;
5028 gfc_new_block = sym;
5030 return MATCH_YES;
5034 /* Free a namelist structure. */
5036 void
5037 gfc_free_namelist (gfc_namelist *name)
5039 gfc_namelist *n;
5041 for (; name; name = n)
5043 n = name->next;
5044 free (name);
5049 /* Free an OpenMP namelist structure. */
5051 void
5052 gfc_free_omp_namelist (gfc_omp_namelist *name)
5054 gfc_omp_namelist *n;
5056 for (; name; name = n)
5058 gfc_free_expr (name->expr);
5059 if (name->udr)
5061 if (name->udr->combiner)
5062 gfc_free_statement (name->udr->combiner);
5063 if (name->udr->initializer)
5064 gfc_free_statement (name->udr->initializer);
5065 free (name->udr);
5067 n = name->next;
5068 free (name);
5073 /* Match a NAMELIST statement. */
5075 match
5076 gfc_match_namelist (void)
5078 gfc_symbol *group_name, *sym;
5079 gfc_namelist *nl;
5080 match m, m2;
5082 m = gfc_match (" / %s /", &group_name);
5083 if (m == MATCH_NO)
5084 goto syntax;
5085 if (m == MATCH_ERROR)
5086 goto error;
5088 for (;;)
5090 if (group_name->ts.type != BT_UNKNOWN)
5092 gfc_error ("Namelist group name %qs at %C already has a basic "
5093 "type of %s", group_name->name,
5094 gfc_typename (&group_name->ts));
5095 return MATCH_ERROR;
5098 if (group_name->attr.flavor == FL_NAMELIST
5099 && group_name->attr.use_assoc
5100 && !gfc_notify_std (GFC_STD_GNU, "Namelist group name %qs "
5101 "at %C already is USE associated and can"
5102 "not be respecified.", group_name->name))
5103 return MATCH_ERROR;
5105 if (group_name->attr.flavor != FL_NAMELIST
5106 && !gfc_add_flavor (&group_name->attr, FL_NAMELIST,
5107 group_name->name, NULL))
5108 return MATCH_ERROR;
5110 for (;;)
5112 m = gfc_match_symbol (&sym, 1);
5113 if (m == MATCH_NO)
5114 goto syntax;
5115 if (m == MATCH_ERROR)
5116 goto error;
5118 if (sym->attr.in_namelist == 0
5119 && !gfc_add_in_namelist (&sym->attr, sym->name, NULL))
5120 goto error;
5122 /* Use gfc_error_check here, rather than goto error, so that
5123 these are the only errors for the next two lines. */
5124 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
5126 gfc_error ("Assumed size array %qs in namelist %qs at "
5127 "%C is not allowed", sym->name, group_name->name);
5128 gfc_error_check ();
5131 nl = gfc_get_namelist ();
5132 nl->sym = sym;
5133 sym->refs++;
5135 if (group_name->namelist == NULL)
5136 group_name->namelist = group_name->namelist_tail = nl;
5137 else
5139 group_name->namelist_tail->next = nl;
5140 group_name->namelist_tail = nl;
5143 if (gfc_match_eos () == MATCH_YES)
5144 goto done;
5146 m = gfc_match_char (',');
5148 if (gfc_match_char ('/') == MATCH_YES)
5150 m2 = gfc_match (" %s /", &group_name);
5151 if (m2 == MATCH_YES)
5152 break;
5153 if (m2 == MATCH_ERROR)
5154 goto error;
5155 goto syntax;
5158 if (m != MATCH_YES)
5159 goto syntax;
5163 done:
5164 return MATCH_YES;
5166 syntax:
5167 gfc_syntax_error (ST_NAMELIST);
5169 error:
5170 return MATCH_ERROR;
5174 /* Match a MODULE statement. */
5176 match
5177 gfc_match_module (void)
5179 match m;
5181 m = gfc_match (" %s%t", &gfc_new_block);
5182 if (m != MATCH_YES)
5183 return m;
5185 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
5186 gfc_new_block->name, NULL))
5187 return MATCH_ERROR;
5189 return MATCH_YES;
5193 /* Free equivalence sets and lists. Recursively is the easiest way to
5194 do this. */
5196 void
5197 gfc_free_equiv_until (gfc_equiv *eq, gfc_equiv *stop)
5199 if (eq == stop)
5200 return;
5202 gfc_free_equiv (eq->eq);
5203 gfc_free_equiv_until (eq->next, stop);
5204 gfc_free_expr (eq->expr);
5205 free (eq);
5209 void
5210 gfc_free_equiv (gfc_equiv *eq)
5212 gfc_free_equiv_until (eq, NULL);
5216 /* Match an EQUIVALENCE statement. */
5218 match
5219 gfc_match_equivalence (void)
5221 gfc_equiv *eq, *set, *tail;
5222 gfc_ref *ref;
5223 gfc_symbol *sym;
5224 match m;
5225 gfc_common_head *common_head = NULL;
5226 bool common_flag;
5227 int cnt;
5229 tail = NULL;
5231 for (;;)
5233 eq = gfc_get_equiv ();
5234 if (tail == NULL)
5235 tail = eq;
5237 eq->next = gfc_current_ns->equiv;
5238 gfc_current_ns->equiv = eq;
5240 if (gfc_match_char ('(') != MATCH_YES)
5241 goto syntax;
5243 set = eq;
5244 common_flag = FALSE;
5245 cnt = 0;
5247 for (;;)
5249 m = gfc_match_equiv_variable (&set->expr);
5250 if (m == MATCH_ERROR)
5251 goto cleanup;
5252 if (m == MATCH_NO)
5253 goto syntax;
5255 /* count the number of objects. */
5256 cnt++;
5258 if (gfc_match_char ('%') == MATCH_YES)
5260 gfc_error ("Derived type component %C is not a "
5261 "permitted EQUIVALENCE member");
5262 goto cleanup;
5265 for (ref = set->expr->ref; ref; ref = ref->next)
5266 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
5268 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
5269 "be an array section");
5270 goto cleanup;
5273 sym = set->expr->symtree->n.sym;
5275 if (!gfc_add_in_equivalence (&sym->attr, sym->name, NULL))
5276 goto cleanup;
5278 if (sym->attr.in_common)
5280 common_flag = TRUE;
5281 common_head = sym->common_head;
5284 if (gfc_match_char (')') == MATCH_YES)
5285 break;
5287 if (gfc_match_char (',') != MATCH_YES)
5288 goto syntax;
5290 set->eq = gfc_get_equiv ();
5291 set = set->eq;
5294 if (cnt < 2)
5296 gfc_error ("EQUIVALENCE at %C requires two or more objects");
5297 goto cleanup;
5300 /* If one of the members of an equivalence is in common, then
5301 mark them all as being in common. Before doing this, check
5302 that members of the equivalence group are not in different
5303 common blocks. */
5304 if (common_flag)
5305 for (set = eq; set; set = set->eq)
5307 sym = set->expr->symtree->n.sym;
5308 if (sym->common_head && sym->common_head != common_head)
5310 gfc_error ("Attempt to indirectly overlap COMMON "
5311 "blocks %s and %s by EQUIVALENCE at %C",
5312 sym->common_head->name, common_head->name);
5313 goto cleanup;
5315 sym->attr.in_common = 1;
5316 sym->common_head = common_head;
5319 if (gfc_match_eos () == MATCH_YES)
5320 break;
5321 if (gfc_match_char (',') != MATCH_YES)
5323 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
5324 goto cleanup;
5328 return MATCH_YES;
5330 syntax:
5331 gfc_syntax_error (ST_EQUIVALENCE);
5333 cleanup:
5334 eq = tail->next;
5335 tail->next = NULL;
5337 gfc_free_equiv (gfc_current_ns->equiv);
5338 gfc_current_ns->equiv = eq;
5340 return MATCH_ERROR;
5344 /* Check that a statement function is not recursive. This is done by looking
5345 for the statement function symbol(sym) by looking recursively through its
5346 expression(e). If a reference to sym is found, true is returned.
5347 12.5.4 requires that any variable of function that is implicitly typed
5348 shall have that type confirmed by any subsequent type declaration. The
5349 implicit typing is conveniently done here. */
5350 static bool
5351 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
5353 static bool
5354 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5357 if (e == NULL)
5358 return false;
5360 switch (e->expr_type)
5362 case EXPR_FUNCTION:
5363 if (e->symtree == NULL)
5364 return false;
5366 /* Check the name before testing for nested recursion! */
5367 if (sym->name == e->symtree->n.sym->name)
5368 return true;
5370 /* Catch recursion via other statement functions. */
5371 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
5372 && e->symtree->n.sym->value
5373 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
5374 return true;
5376 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5377 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5379 break;
5381 case EXPR_VARIABLE:
5382 if (e->symtree && sym->name == e->symtree->n.sym->name)
5383 return true;
5385 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5386 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5387 break;
5389 default:
5390 break;
5393 return false;
5397 static bool
5398 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
5400 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
5404 /* Match a statement function declaration. It is so easy to match
5405 non-statement function statements with a MATCH_ERROR as opposed to
5406 MATCH_NO that we suppress error message in most cases. */
5408 match
5409 gfc_match_st_function (void)
5411 gfc_error_buffer old_error;
5412 gfc_symbol *sym;
5413 gfc_expr *expr;
5414 match m;
5416 m = gfc_match_symbol (&sym, 0);
5417 if (m != MATCH_YES)
5418 return m;
5420 gfc_push_error (&old_error);
5422 if (!gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION, sym->name, NULL))
5423 goto undo_error;
5425 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
5426 goto undo_error;
5428 m = gfc_match (" = %e%t", &expr);
5429 if (m == MATCH_NO)
5430 goto undo_error;
5432 gfc_free_error (&old_error);
5434 if (m == MATCH_ERROR)
5435 return m;
5437 if (recursive_stmt_fcn (expr, sym))
5439 gfc_error ("Statement function at %L is recursive", &expr->where);
5440 return MATCH_ERROR;
5443 sym->value = expr;
5445 if ((gfc_current_state () == COMP_FUNCTION
5446 || gfc_current_state () == COMP_SUBROUTINE)
5447 && gfc_state_stack->previous->state == COMP_INTERFACE)
5449 gfc_error ("Statement function at %L cannot appear within an INTERFACE",
5450 &expr->where);
5451 return MATCH_ERROR;
5454 if (!gfc_notify_std (GFC_STD_F95_OBS, "Statement function at %C"))
5455 return MATCH_ERROR;
5457 return MATCH_YES;
5459 undo_error:
5460 gfc_pop_error (&old_error);
5461 return MATCH_NO;
5465 /* Match an assignment to a pointer function (F2008). This could, in
5466 general be ambiguous with a statement function. In this implementation
5467 it remains so if it is the first statement after the specification
5468 block. */
5470 match
5471 gfc_match_ptr_fcn_assign (void)
5473 gfc_error_buffer old_error;
5474 locus old_loc;
5475 gfc_symbol *sym;
5476 gfc_expr *expr;
5477 match m;
5478 char name[GFC_MAX_SYMBOL_LEN + 1];
5480 old_loc = gfc_current_locus;
5481 m = gfc_match_name (name);
5482 if (m != MATCH_YES)
5483 return m;
5485 gfc_find_symbol (name, NULL, 1, &sym);
5486 if (sym && sym->attr.flavor != FL_PROCEDURE)
5487 return MATCH_NO;
5489 gfc_push_error (&old_error);
5491 if (sym && sym->attr.function)
5492 goto match_actual_arglist;
5494 gfc_current_locus = old_loc;
5495 m = gfc_match_symbol (&sym, 0);
5496 if (m != MATCH_YES)
5497 return m;
5499 if (!gfc_add_procedure (&sym->attr, PROC_UNKNOWN, sym->name, NULL))
5500 goto undo_error;
5502 match_actual_arglist:
5503 gfc_current_locus = old_loc;
5504 m = gfc_match (" %e", &expr);
5505 if (m != MATCH_YES)
5506 goto undo_error;
5508 new_st.op = EXEC_ASSIGN;
5509 new_st.expr1 = expr;
5510 expr = NULL;
5512 m = gfc_match (" = %e%t", &expr);
5513 if (m != MATCH_YES)
5514 goto undo_error;
5516 new_st.expr2 = expr;
5517 return MATCH_YES;
5519 undo_error:
5520 gfc_pop_error (&old_error);
5521 return MATCH_NO;
5525 /***************** SELECT CASE subroutines ******************/
5527 /* Free a single case structure. */
5529 static void
5530 free_case (gfc_case *p)
5532 if (p->low == p->high)
5533 p->high = NULL;
5534 gfc_free_expr (p->low);
5535 gfc_free_expr (p->high);
5536 free (p);
5540 /* Free a list of case structures. */
5542 void
5543 gfc_free_case_list (gfc_case *p)
5545 gfc_case *q;
5547 for (; p; p = q)
5549 q = p->next;
5550 free_case (p);
5555 /* Match a single case selector. Combining the requirements of F08:C830
5556 and F08:C832 (R838) means that the case-value must have either CHARACTER,
5557 INTEGER, or LOGICAL type. */
5559 static match
5560 match_case_selector (gfc_case **cp)
5562 gfc_case *c;
5563 match m;
5565 c = gfc_get_case ();
5566 c->where = gfc_current_locus;
5568 if (gfc_match_char (':') == MATCH_YES)
5570 m = gfc_match_init_expr (&c->high);
5571 if (m == MATCH_NO)
5572 goto need_expr;
5573 if (m == MATCH_ERROR)
5574 goto cleanup;
5576 if (c->high->ts.type != BT_LOGICAL && c->high->ts.type != BT_INTEGER
5577 && c->high->ts.type != BT_CHARACTER)
5579 gfc_error ("Expression in CASE selector at %L cannot be %s",
5580 &c->high->where, gfc_typename (&c->high->ts));
5581 goto cleanup;
5584 else
5586 m = gfc_match_init_expr (&c->low);
5587 if (m == MATCH_ERROR)
5588 goto cleanup;
5589 if (m == MATCH_NO)
5590 goto need_expr;
5592 if (c->low->ts.type != BT_LOGICAL && c->low->ts.type != BT_INTEGER
5593 && c->low->ts.type != BT_CHARACTER)
5595 gfc_error ("Expression in CASE selector at %L cannot be %s",
5596 &c->low->where, gfc_typename (&c->low->ts));
5597 goto cleanup;
5600 /* If we're not looking at a ':' now, make a range out of a single
5601 target. Else get the upper bound for the case range. */
5602 if (gfc_match_char (':') != MATCH_YES)
5603 c->high = c->low;
5604 else
5606 m = gfc_match_init_expr (&c->high);
5607 if (m == MATCH_ERROR)
5608 goto cleanup;
5609 /* MATCH_NO is fine. It's OK if nothing is there! */
5613 *cp = c;
5614 return MATCH_YES;
5616 need_expr:
5617 gfc_error ("Expected initialization expression in CASE at %C");
5619 cleanup:
5620 free_case (c);
5621 return MATCH_ERROR;
5625 /* Match the end of a case statement. */
5627 static match
5628 match_case_eos (void)
5630 char name[GFC_MAX_SYMBOL_LEN + 1];
5631 match m;
5633 if (gfc_match_eos () == MATCH_YES)
5634 return MATCH_YES;
5636 /* If the case construct doesn't have a case-construct-name, we
5637 should have matched the EOS. */
5638 if (!gfc_current_block ())
5639 return MATCH_NO;
5641 gfc_gobble_whitespace ();
5643 m = gfc_match_name (name);
5644 if (m != MATCH_YES)
5645 return m;
5647 if (strcmp (name, gfc_current_block ()->name) != 0)
5649 gfc_error ("Expected block name %qs of SELECT construct at %C",
5650 gfc_current_block ()->name);
5651 return MATCH_ERROR;
5654 return gfc_match_eos ();
5658 /* Match a SELECT statement. */
5660 match
5661 gfc_match_select (void)
5663 gfc_expr *expr;
5664 match m;
5666 m = gfc_match_label ();
5667 if (m == MATCH_ERROR)
5668 return m;
5670 m = gfc_match (" select case ( %e )%t", &expr);
5671 if (m != MATCH_YES)
5672 return m;
5674 new_st.op = EXEC_SELECT;
5675 new_st.expr1 = expr;
5677 return MATCH_YES;
5681 /* Transfer the selector typespec to the associate name. */
5683 static void
5684 copy_ts_from_selector_to_associate (gfc_expr *associate, gfc_expr *selector)
5686 gfc_ref *ref;
5687 gfc_symbol *assoc_sym;
5689 assoc_sym = associate->symtree->n.sym;
5691 /* At this stage the expression rank and arrayspec dimensions have
5692 not been completely sorted out. We must get the expr2->rank
5693 right here, so that the correct class container is obtained. */
5694 ref = selector->ref;
5695 while (ref && ref->next)
5696 ref = ref->next;
5698 if (selector->ts.type == BT_CLASS && CLASS_DATA (selector)->as
5699 && ref && ref->type == REF_ARRAY)
5701 /* Ensure that the array reference type is set. We cannot use
5702 gfc_resolve_expr at this point, so the usable parts of
5703 resolve.c(resolve_array_ref) are employed to do it. */
5704 if (ref->u.ar.type == AR_UNKNOWN)
5706 ref->u.ar.type = AR_ELEMENT;
5707 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
5708 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5709 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR
5710 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
5711 && ref->u.ar.start[i] && ref->u.ar.start[i]->rank))
5713 ref->u.ar.type = AR_SECTION;
5714 break;
5718 if (ref->u.ar.type == AR_FULL)
5719 selector->rank = CLASS_DATA (selector)->as->rank;
5720 else if (ref->u.ar.type == AR_SECTION)
5721 selector->rank = ref->u.ar.dimen;
5722 else
5723 selector->rank = 0;
5726 if (selector->rank)
5728 assoc_sym->attr.dimension = 1;
5729 assoc_sym->as = gfc_get_array_spec ();
5730 assoc_sym->as->rank = selector->rank;
5731 assoc_sym->as->type = AS_DEFERRED;
5733 else
5734 assoc_sym->as = NULL;
5736 if (selector->ts.type == BT_CLASS)
5738 /* The correct class container has to be available. */
5739 assoc_sym->ts.type = BT_CLASS;
5740 assoc_sym->ts.u.derived = CLASS_DATA (selector)->ts.u.derived;
5741 assoc_sym->attr.pointer = 1;
5742 gfc_build_class_symbol (&assoc_sym->ts, &assoc_sym->attr, &assoc_sym->as);
5747 /* Push the current selector onto the SELECT TYPE stack. */
5749 static void
5750 select_type_push (gfc_symbol *sel)
5752 gfc_select_type_stack *top = gfc_get_select_type_stack ();
5753 top->selector = sel;
5754 top->tmp = NULL;
5755 top->prev = select_type_stack;
5757 select_type_stack = top;
5761 /* Set the temporary for the current intrinsic SELECT TYPE selector. */
5763 static gfc_symtree *
5764 select_intrinsic_set_tmp (gfc_typespec *ts)
5766 char name[GFC_MAX_SYMBOL_LEN];
5767 gfc_symtree *tmp;
5768 int charlen = 0;
5770 if (ts->type == BT_CLASS || ts->type == BT_DERIVED)
5771 return NULL;
5773 if (select_type_stack->selector->ts.type == BT_CLASS
5774 && !select_type_stack->selector->attr.class_ok)
5775 return NULL;
5777 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length
5778 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
5779 charlen = mpz_get_si (ts->u.cl->length->value.integer);
5781 if (ts->type != BT_CHARACTER)
5782 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (ts->type),
5783 ts->kind);
5784 else
5785 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (ts->type),
5786 charlen, ts->kind);
5788 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
5789 gfc_add_type (tmp->n.sym, ts, NULL);
5791 /* Copy across the array spec to the selector. */
5792 if (select_type_stack->selector->ts.type == BT_CLASS
5793 && (CLASS_DATA (select_type_stack->selector)->attr.dimension
5794 || CLASS_DATA (select_type_stack->selector)->attr.codimension))
5796 tmp->n.sym->attr.pointer = 1;
5797 tmp->n.sym->attr.dimension
5798 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
5799 tmp->n.sym->attr.codimension
5800 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
5801 tmp->n.sym->as
5802 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
5805 gfc_set_sym_referenced (tmp->n.sym);
5806 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
5807 tmp->n.sym->attr.select_type_temporary = 1;
5809 return tmp;
5813 /* Set up a temporary for the current TYPE IS / CLASS IS branch . */
5815 static void
5816 select_type_set_tmp (gfc_typespec *ts)
5818 char name[GFC_MAX_SYMBOL_LEN];
5819 gfc_symtree *tmp = NULL;
5821 if (!ts)
5823 select_type_stack->tmp = NULL;
5824 return;
5827 tmp = select_intrinsic_set_tmp (ts);
5829 if (tmp == NULL)
5831 if (!ts->u.derived)
5832 return;
5834 if (ts->type == BT_CLASS)
5835 sprintf (name, "__tmp_class_%s", ts->u.derived->name);
5836 else
5837 sprintf (name, "__tmp_type_%s", ts->u.derived->name);
5838 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
5839 gfc_add_type (tmp->n.sym, ts, NULL);
5841 if (select_type_stack->selector->ts.type == BT_CLASS
5842 && select_type_stack->selector->attr.class_ok)
5844 tmp->n.sym->attr.pointer
5845 = CLASS_DATA (select_type_stack->selector)->attr.class_pointer;
5847 /* Copy across the array spec to the selector. */
5848 if (CLASS_DATA (select_type_stack->selector)->attr.dimension
5849 || CLASS_DATA (select_type_stack->selector)->attr.codimension)
5851 tmp->n.sym->attr.dimension
5852 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
5853 tmp->n.sym->attr.codimension
5854 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
5855 tmp->n.sym->as
5856 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
5860 gfc_set_sym_referenced (tmp->n.sym);
5861 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
5862 tmp->n.sym->attr.select_type_temporary = 1;
5864 if (ts->type == BT_CLASS)
5865 gfc_build_class_symbol (&tmp->n.sym->ts, &tmp->n.sym->attr,
5866 &tmp->n.sym->as);
5869 /* Add an association for it, so the rest of the parser knows it is
5870 an associate-name. The target will be set during resolution. */
5871 tmp->n.sym->assoc = gfc_get_association_list ();
5872 tmp->n.sym->assoc->dangling = 1;
5873 tmp->n.sym->assoc->st = tmp;
5875 select_type_stack->tmp = tmp;
5879 /* Match a SELECT TYPE statement. */
5881 match
5882 gfc_match_select_type (void)
5884 gfc_expr *expr1, *expr2 = NULL;
5885 match m;
5886 char name[GFC_MAX_SYMBOL_LEN];
5887 bool class_array;
5888 gfc_symbol *sym;
5889 gfc_namespace *ns = gfc_current_ns;
5891 m = gfc_match_label ();
5892 if (m == MATCH_ERROR)
5893 return m;
5895 m = gfc_match (" select type ( ");
5896 if (m != MATCH_YES)
5897 return m;
5899 gfc_current_ns = gfc_build_block_ns (ns);
5900 m = gfc_match (" %n => %e", name, &expr2);
5901 if (m == MATCH_YES)
5903 expr1 = gfc_get_expr ();
5904 expr1->expr_type = EXPR_VARIABLE;
5905 expr1->where = expr2->where;
5906 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
5908 m = MATCH_ERROR;
5909 goto cleanup;
5912 sym = expr1->symtree->n.sym;
5913 if (expr2->ts.type == BT_UNKNOWN)
5914 sym->attr.untyped = 1;
5915 else
5916 copy_ts_from_selector_to_associate (expr1, expr2);
5918 sym->attr.flavor = FL_VARIABLE;
5919 sym->attr.referenced = 1;
5920 sym->attr.class_ok = 1;
5922 else
5924 m = gfc_match (" %e ", &expr1);
5925 if (m != MATCH_YES)
5927 std::swap (ns, gfc_current_ns);
5928 gfc_free_namespace (ns);
5929 return m;
5933 m = gfc_match (" )%t");
5934 if (m != MATCH_YES)
5936 gfc_error ("parse error in SELECT TYPE statement at %C");
5937 goto cleanup;
5940 /* This ghastly expression seems to be needed to distinguish a CLASS
5941 array, which can have a reference, from other expressions that
5942 have references, such as derived type components, and are not
5943 allowed by the standard.
5944 TODO: see if it is sufficient to exclude component and substring
5945 references. */
5946 class_array = (expr1->expr_type == EXPR_VARIABLE
5947 && expr1->ts.type == BT_CLASS
5948 && CLASS_DATA (expr1)
5949 && (strcmp (CLASS_DATA (expr1)->name, "_data") == 0)
5950 && (CLASS_DATA (expr1)->attr.dimension
5951 || CLASS_DATA (expr1)->attr.codimension)
5952 && expr1->ref
5953 && expr1->ref->type == REF_ARRAY
5954 && expr1->ref->next == NULL);
5956 /* Check for F03:C811. */
5957 if (!expr2 && (expr1->expr_type != EXPR_VARIABLE
5958 || (!class_array && expr1->ref != NULL)))
5960 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
5961 "use associate-name=>");
5962 m = MATCH_ERROR;
5963 goto cleanup;
5966 new_st.op = EXEC_SELECT_TYPE;
5967 new_st.expr1 = expr1;
5968 new_st.expr2 = expr2;
5969 new_st.ext.block.ns = gfc_current_ns;
5971 select_type_push (expr1->symtree->n.sym);
5972 gfc_current_ns = ns;
5974 return MATCH_YES;
5976 cleanup:
5977 gfc_free_expr (expr1);
5978 gfc_free_expr (expr2);
5979 gfc_undo_symbols ();
5980 std::swap (ns, gfc_current_ns);
5981 gfc_free_namespace (ns);
5982 return m;
5986 /* Match a CASE statement. */
5988 match
5989 gfc_match_case (void)
5991 gfc_case *c, *head, *tail;
5992 match m;
5994 head = tail = NULL;
5996 if (gfc_current_state () != COMP_SELECT)
5998 gfc_error ("Unexpected CASE statement at %C");
5999 return MATCH_ERROR;
6002 if (gfc_match ("% default") == MATCH_YES)
6004 m = match_case_eos ();
6005 if (m == MATCH_NO)
6006 goto syntax;
6007 if (m == MATCH_ERROR)
6008 goto cleanup;
6010 new_st.op = EXEC_SELECT;
6011 c = gfc_get_case ();
6012 c->where = gfc_current_locus;
6013 new_st.ext.block.case_list = c;
6014 return MATCH_YES;
6017 if (gfc_match_char ('(') != MATCH_YES)
6018 goto syntax;
6020 for (;;)
6022 if (match_case_selector (&c) == MATCH_ERROR)
6023 goto cleanup;
6025 if (head == NULL)
6026 head = c;
6027 else
6028 tail->next = c;
6030 tail = c;
6032 if (gfc_match_char (')') == MATCH_YES)
6033 break;
6034 if (gfc_match_char (',') != MATCH_YES)
6035 goto syntax;
6038 m = match_case_eos ();
6039 if (m == MATCH_NO)
6040 goto syntax;
6041 if (m == MATCH_ERROR)
6042 goto cleanup;
6044 new_st.op = EXEC_SELECT;
6045 new_st.ext.block.case_list = head;
6047 return MATCH_YES;
6049 syntax:
6050 gfc_error ("Syntax error in CASE specification at %C");
6052 cleanup:
6053 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
6054 return MATCH_ERROR;
6058 /* Match a TYPE IS statement. */
6060 match
6061 gfc_match_type_is (void)
6063 gfc_case *c = NULL;
6064 match m;
6066 if (gfc_current_state () != COMP_SELECT_TYPE)
6068 gfc_error ("Unexpected TYPE IS statement at %C");
6069 return MATCH_ERROR;
6072 if (gfc_match_char ('(') != MATCH_YES)
6073 goto syntax;
6075 c = gfc_get_case ();
6076 c->where = gfc_current_locus;
6078 m = gfc_match_type_spec (&c->ts);
6079 if (m == MATCH_NO)
6080 goto syntax;
6081 if (m == MATCH_ERROR)
6082 goto cleanup;
6084 if (gfc_match_char (')') != MATCH_YES)
6085 goto syntax;
6087 m = match_case_eos ();
6088 if (m == MATCH_NO)
6089 goto syntax;
6090 if (m == MATCH_ERROR)
6091 goto cleanup;
6093 new_st.op = EXEC_SELECT_TYPE;
6094 new_st.ext.block.case_list = c;
6096 if (c->ts.type == BT_DERIVED && c->ts.u.derived
6097 && (c->ts.u.derived->attr.sequence
6098 || c->ts.u.derived->attr.is_bind_c))
6100 gfc_error ("The type-spec shall not specify a sequence derived "
6101 "type or a type with the BIND attribute in SELECT "
6102 "TYPE at %C [F2003:C815]");
6103 return MATCH_ERROR;
6106 /* Create temporary variable. */
6107 select_type_set_tmp (&c->ts);
6109 return MATCH_YES;
6111 syntax:
6112 gfc_error ("Syntax error in TYPE IS specification at %C");
6114 cleanup:
6115 if (c != NULL)
6116 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
6117 return MATCH_ERROR;
6121 /* Match a CLASS IS or CLASS DEFAULT statement. */
6123 match
6124 gfc_match_class_is (void)
6126 gfc_case *c = NULL;
6127 match m;
6129 if (gfc_current_state () != COMP_SELECT_TYPE)
6130 return MATCH_NO;
6132 if (gfc_match ("% default") == MATCH_YES)
6134 m = match_case_eos ();
6135 if (m == MATCH_NO)
6136 goto syntax;
6137 if (m == MATCH_ERROR)
6138 goto cleanup;
6140 new_st.op = EXEC_SELECT_TYPE;
6141 c = gfc_get_case ();
6142 c->where = gfc_current_locus;
6143 c->ts.type = BT_UNKNOWN;
6144 new_st.ext.block.case_list = c;
6145 select_type_set_tmp (NULL);
6146 return MATCH_YES;
6149 m = gfc_match ("% is");
6150 if (m == MATCH_NO)
6151 goto syntax;
6152 if (m == MATCH_ERROR)
6153 goto cleanup;
6155 if (gfc_match_char ('(') != MATCH_YES)
6156 goto syntax;
6158 c = gfc_get_case ();
6159 c->where = gfc_current_locus;
6161 m = match_derived_type_spec (&c->ts);
6162 if (m == MATCH_NO)
6163 goto syntax;
6164 if (m == MATCH_ERROR)
6165 goto cleanup;
6167 if (c->ts.type == BT_DERIVED)
6168 c->ts.type = BT_CLASS;
6170 if (gfc_match_char (')') != MATCH_YES)
6171 goto syntax;
6173 m = match_case_eos ();
6174 if (m == MATCH_NO)
6175 goto syntax;
6176 if (m == MATCH_ERROR)
6177 goto cleanup;
6179 new_st.op = EXEC_SELECT_TYPE;
6180 new_st.ext.block.case_list = c;
6182 /* Create temporary variable. */
6183 select_type_set_tmp (&c->ts);
6185 return MATCH_YES;
6187 syntax:
6188 gfc_error ("Syntax error in CLASS IS specification at %C");
6190 cleanup:
6191 if (c != NULL)
6192 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
6193 return MATCH_ERROR;
6197 /********************* WHERE subroutines ********************/
6199 /* Match the rest of a simple WHERE statement that follows an IF statement.
6202 static match
6203 match_simple_where (void)
6205 gfc_expr *expr;
6206 gfc_code *c;
6207 match m;
6209 m = gfc_match (" ( %e )", &expr);
6210 if (m != MATCH_YES)
6211 return m;
6213 m = gfc_match_assignment ();
6214 if (m == MATCH_NO)
6215 goto syntax;
6216 if (m == MATCH_ERROR)
6217 goto cleanup;
6219 if (gfc_match_eos () != MATCH_YES)
6220 goto syntax;
6222 c = gfc_get_code (EXEC_WHERE);
6223 c->expr1 = expr;
6225 c->next = XCNEW (gfc_code);
6226 *c->next = new_st;
6227 c->next->loc = gfc_current_locus;
6228 gfc_clear_new_st ();
6230 new_st.op = EXEC_WHERE;
6231 new_st.block = c;
6233 return MATCH_YES;
6235 syntax:
6236 gfc_syntax_error (ST_WHERE);
6238 cleanup:
6239 gfc_free_expr (expr);
6240 return MATCH_ERROR;
6244 /* Match a WHERE statement. */
6246 match
6247 gfc_match_where (gfc_statement *st)
6249 gfc_expr *expr;
6250 match m0, m;
6251 gfc_code *c;
6253 m0 = gfc_match_label ();
6254 if (m0 == MATCH_ERROR)
6255 return m0;
6257 m = gfc_match (" where ( %e )", &expr);
6258 if (m != MATCH_YES)
6259 return m;
6261 if (gfc_match_eos () == MATCH_YES)
6263 *st = ST_WHERE_BLOCK;
6264 new_st.op = EXEC_WHERE;
6265 new_st.expr1 = expr;
6266 return MATCH_YES;
6269 m = gfc_match_assignment ();
6270 if (m == MATCH_NO)
6271 gfc_syntax_error (ST_WHERE);
6273 if (m != MATCH_YES)
6275 gfc_free_expr (expr);
6276 return MATCH_ERROR;
6279 /* We've got a simple WHERE statement. */
6280 *st = ST_WHERE;
6281 c = gfc_get_code (EXEC_WHERE);
6282 c->expr1 = expr;
6284 /* Put in the assignment. It will not be processed by add_statement, so we
6285 need to copy the location here. */
6287 c->next = XCNEW (gfc_code);
6288 *c->next = new_st;
6289 c->next->loc = gfc_current_locus;
6290 gfc_clear_new_st ();
6292 new_st.op = EXEC_WHERE;
6293 new_st.block = c;
6295 return MATCH_YES;
6299 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
6300 new_st if successful. */
6302 match
6303 gfc_match_elsewhere (void)
6305 char name[GFC_MAX_SYMBOL_LEN + 1];
6306 gfc_expr *expr;
6307 match m;
6309 if (gfc_current_state () != COMP_WHERE)
6311 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
6312 return MATCH_ERROR;
6315 expr = NULL;
6317 if (gfc_match_char ('(') == MATCH_YES)
6319 m = gfc_match_expr (&expr);
6320 if (m == MATCH_NO)
6321 goto syntax;
6322 if (m == MATCH_ERROR)
6323 return MATCH_ERROR;
6325 if (gfc_match_char (')') != MATCH_YES)
6326 goto syntax;
6329 if (gfc_match_eos () != MATCH_YES)
6331 /* Only makes sense if we have a where-construct-name. */
6332 if (!gfc_current_block ())
6334 m = MATCH_ERROR;
6335 goto cleanup;
6337 /* Better be a name at this point. */
6338 m = gfc_match_name (name);
6339 if (m == MATCH_NO)
6340 goto syntax;
6341 if (m == MATCH_ERROR)
6342 goto cleanup;
6344 if (gfc_match_eos () != MATCH_YES)
6345 goto syntax;
6347 if (strcmp (name, gfc_current_block ()->name) != 0)
6349 gfc_error ("Label %qs at %C doesn't match WHERE label %qs",
6350 name, gfc_current_block ()->name);
6351 goto cleanup;
6355 new_st.op = EXEC_WHERE;
6356 new_st.expr1 = expr;
6357 return MATCH_YES;
6359 syntax:
6360 gfc_syntax_error (ST_ELSEWHERE);
6362 cleanup:
6363 gfc_free_expr (expr);
6364 return MATCH_ERROR;