* de.po: Update.
[official-gcc.git] / gcc / fortran / match.c
blobfc37f227e8a68dae3a903603d7560325a1de0167
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 match m;
518 int i;
520 m = gfc_match_expr (&expr);
521 if (m != MATCH_YES)
522 return m;
524 if (gfc_extract_int (expr, &i, 1))
525 m = MATCH_ERROR;
526 gfc_free_expr (expr);
528 *value = i;
529 return m;
533 /* This function is the same as the gfc_match_small_int, except that
534 we're keeping the pointer to the expr. This function could just be
535 removed and the previously mentioned one modified, though all calls
536 to it would have to be modified then (and there were a number of
537 them). Return MATCH_ERROR if fail to extract the int; otherwise,
538 return the result of gfc_match_expr(). The expr (if any) that was
539 matched is returned in the parameter expr. */
541 match
542 gfc_match_small_int_expr (int *value, gfc_expr **expr)
544 match m;
545 int i;
547 m = gfc_match_expr (expr);
548 if (m != MATCH_YES)
549 return m;
551 if (gfc_extract_int (*expr, &i, 1))
552 m = MATCH_ERROR;
554 *value = i;
555 return m;
559 /* Matches a statement label. Uses gfc_match_small_literal_int() to
560 do most of the work. */
562 match
563 gfc_match_st_label (gfc_st_label **label)
565 locus old_loc;
566 match m;
567 int i, cnt;
569 old_loc = gfc_current_locus;
571 m = gfc_match_small_literal_int (&i, &cnt);
572 if (m != MATCH_YES)
573 return m;
575 if (cnt > 5)
577 gfc_error ("Too many digits in statement label at %C");
578 goto cleanup;
581 if (i == 0)
583 gfc_error ("Statement label at %C is zero");
584 goto cleanup;
587 *label = gfc_get_st_label (i);
588 return MATCH_YES;
590 cleanup:
592 gfc_current_locus = old_loc;
593 return MATCH_ERROR;
597 /* Match and validate a label associated with a named IF, DO or SELECT
598 statement. If the symbol does not have the label attribute, we add
599 it. We also make sure the symbol does not refer to another
600 (active) block. A matched label is pointed to by gfc_new_block. */
602 match
603 gfc_match_label (void)
605 char name[GFC_MAX_SYMBOL_LEN + 1];
606 match m;
608 gfc_new_block = NULL;
610 m = gfc_match (" %n :", name);
611 if (m != MATCH_YES)
612 return m;
614 if (gfc_get_symbol (name, NULL, &gfc_new_block))
616 gfc_error ("Label name %qs at %C is ambiguous", name);
617 return MATCH_ERROR;
620 if (gfc_new_block->attr.flavor == FL_LABEL)
622 gfc_error ("Duplicate construct label %qs at %C", name);
623 return MATCH_ERROR;
626 if (!gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
627 gfc_new_block->name, NULL))
628 return MATCH_ERROR;
630 return MATCH_YES;
634 /* See if the current input looks like a name of some sort. Modifies
635 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
636 Note that options.c restricts max_identifier_length to not more
637 than GFC_MAX_SYMBOL_LEN. */
639 match
640 gfc_match_name (char *buffer)
642 locus old_loc;
643 int i;
644 char c;
646 old_loc = gfc_current_locus;
647 gfc_gobble_whitespace ();
649 c = gfc_next_ascii_char ();
650 if (!(ISALPHA (c) || (c == '_' && flag_allow_leading_underscore)))
652 /* Special cases for unary minus and plus, which allows for a sensible
653 error message for code of the form 'c = exp(-a*b) )' where an
654 extra ')' appears at the end of statement. */
655 if (!gfc_error_flag_test () && c != '(' && c != '-' && c != '+')
656 gfc_error ("Invalid character in name at %C");
657 gfc_current_locus = old_loc;
658 return MATCH_NO;
661 i = 0;
665 buffer[i++] = c;
667 if (i > gfc_option.max_identifier_length)
669 gfc_error ("Name at %C is too long");
670 return MATCH_ERROR;
673 old_loc = gfc_current_locus;
674 c = gfc_next_ascii_char ();
676 while (ISALNUM (c) || c == '_' || (flag_dollar_ok && c == '$'));
678 if (c == '$' && !flag_dollar_ok)
680 gfc_fatal_error ("Invalid character %<$%> at %L. Use %<-fdollar-ok%> to "
681 "allow it as an extension", &old_loc);
682 return MATCH_ERROR;
685 buffer[i] = '\0';
686 gfc_current_locus = old_loc;
688 return MATCH_YES;
692 /* Match a symbol on the input. Modifies the pointer to the symbol
693 pointer if successful. */
695 match
696 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
698 char buffer[GFC_MAX_SYMBOL_LEN + 1];
699 match m;
701 m = gfc_match_name (buffer);
702 if (m != MATCH_YES)
703 return m;
705 if (host_assoc)
706 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
707 ? MATCH_ERROR : MATCH_YES;
709 if (gfc_get_sym_tree (buffer, NULL, matched_symbol, false))
710 return MATCH_ERROR;
712 return MATCH_YES;
716 match
717 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
719 gfc_symtree *st;
720 match m;
722 m = gfc_match_sym_tree (&st, host_assoc);
724 if (m == MATCH_YES)
726 if (st)
727 *matched_symbol = st->n.sym;
728 else
729 *matched_symbol = NULL;
731 else
732 *matched_symbol = NULL;
733 return m;
737 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
738 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
739 in matchexp.c. */
741 match
742 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
744 locus orig_loc = gfc_current_locus;
745 char ch;
747 gfc_gobble_whitespace ();
748 ch = gfc_next_ascii_char ();
749 switch (ch)
751 case '+':
752 /* Matched "+". */
753 *result = INTRINSIC_PLUS;
754 return MATCH_YES;
756 case '-':
757 /* Matched "-". */
758 *result = INTRINSIC_MINUS;
759 return MATCH_YES;
761 case '=':
762 if (gfc_next_ascii_char () == '=')
764 /* Matched "==". */
765 *result = INTRINSIC_EQ;
766 return MATCH_YES;
768 break;
770 case '<':
771 if (gfc_peek_ascii_char () == '=')
773 /* Matched "<=". */
774 gfc_next_ascii_char ();
775 *result = INTRINSIC_LE;
776 return MATCH_YES;
778 /* Matched "<". */
779 *result = INTRINSIC_LT;
780 return MATCH_YES;
782 case '>':
783 if (gfc_peek_ascii_char () == '=')
785 /* Matched ">=". */
786 gfc_next_ascii_char ();
787 *result = INTRINSIC_GE;
788 return MATCH_YES;
790 /* Matched ">". */
791 *result = INTRINSIC_GT;
792 return MATCH_YES;
794 case '*':
795 if (gfc_peek_ascii_char () == '*')
797 /* Matched "**". */
798 gfc_next_ascii_char ();
799 *result = INTRINSIC_POWER;
800 return MATCH_YES;
802 /* Matched "*". */
803 *result = INTRINSIC_TIMES;
804 return MATCH_YES;
806 case '/':
807 ch = gfc_peek_ascii_char ();
808 if (ch == '=')
810 /* Matched "/=". */
811 gfc_next_ascii_char ();
812 *result = INTRINSIC_NE;
813 return MATCH_YES;
815 else if (ch == '/')
817 /* Matched "//". */
818 gfc_next_ascii_char ();
819 *result = INTRINSIC_CONCAT;
820 return MATCH_YES;
822 /* Matched "/". */
823 *result = INTRINSIC_DIVIDE;
824 return MATCH_YES;
826 case '.':
827 ch = gfc_next_ascii_char ();
828 switch (ch)
830 case 'a':
831 if (gfc_next_ascii_char () == 'n'
832 && gfc_next_ascii_char () == 'd'
833 && gfc_next_ascii_char () == '.')
835 /* Matched ".and.". */
836 *result = INTRINSIC_AND;
837 return MATCH_YES;
839 break;
841 case 'e':
842 if (gfc_next_ascii_char () == 'q')
844 ch = gfc_next_ascii_char ();
845 if (ch == '.')
847 /* Matched ".eq.". */
848 *result = INTRINSIC_EQ_OS;
849 return MATCH_YES;
851 else if (ch == 'v')
853 if (gfc_next_ascii_char () == '.')
855 /* Matched ".eqv.". */
856 *result = INTRINSIC_EQV;
857 return MATCH_YES;
861 break;
863 case 'g':
864 ch = gfc_next_ascii_char ();
865 if (ch == 'e')
867 if (gfc_next_ascii_char () == '.')
869 /* Matched ".ge.". */
870 *result = INTRINSIC_GE_OS;
871 return MATCH_YES;
874 else if (ch == 't')
876 if (gfc_next_ascii_char () == '.')
878 /* Matched ".gt.". */
879 *result = INTRINSIC_GT_OS;
880 return MATCH_YES;
883 break;
885 case 'l':
886 ch = gfc_next_ascii_char ();
887 if (ch == 'e')
889 if (gfc_next_ascii_char () == '.')
891 /* Matched ".le.". */
892 *result = INTRINSIC_LE_OS;
893 return MATCH_YES;
896 else if (ch == 't')
898 if (gfc_next_ascii_char () == '.')
900 /* Matched ".lt.". */
901 *result = INTRINSIC_LT_OS;
902 return MATCH_YES;
905 break;
907 case 'n':
908 ch = gfc_next_ascii_char ();
909 if (ch == 'e')
911 ch = gfc_next_ascii_char ();
912 if (ch == '.')
914 /* Matched ".ne.". */
915 *result = INTRINSIC_NE_OS;
916 return MATCH_YES;
918 else if (ch == 'q')
920 if (gfc_next_ascii_char () == 'v'
921 && gfc_next_ascii_char () == '.')
923 /* Matched ".neqv.". */
924 *result = INTRINSIC_NEQV;
925 return MATCH_YES;
929 else if (ch == 'o')
931 if (gfc_next_ascii_char () == 't'
932 && gfc_next_ascii_char () == '.')
934 /* Matched ".not.". */
935 *result = INTRINSIC_NOT;
936 return MATCH_YES;
939 break;
941 case 'o':
942 if (gfc_next_ascii_char () == 'r'
943 && gfc_next_ascii_char () == '.')
945 /* Matched ".or.". */
946 *result = INTRINSIC_OR;
947 return MATCH_YES;
949 break;
951 case 'x':
952 if (gfc_next_ascii_char () == 'o'
953 && gfc_next_ascii_char () == 'r'
954 && gfc_next_ascii_char () == '.')
956 if (!gfc_notify_std (GFC_STD_LEGACY, ".XOR. operator at %C"))
957 return MATCH_ERROR;
958 /* Matched ".xor." - equivalent to ".neqv.". */
959 *result = INTRINSIC_NEQV;
960 return MATCH_YES;
962 break;
964 default:
965 break;
967 break;
969 default:
970 break;
973 gfc_current_locus = orig_loc;
974 return MATCH_NO;
978 /* Match a loop control phrase:
980 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
982 If the final integer expression is not present, a constant unity
983 expression is returned. We don't return MATCH_ERROR until after
984 the equals sign is seen. */
986 match
987 gfc_match_iterator (gfc_iterator *iter, int init_flag)
989 char name[GFC_MAX_SYMBOL_LEN + 1];
990 gfc_expr *var, *e1, *e2, *e3;
991 locus start;
992 match m;
994 e1 = e2 = e3 = NULL;
996 /* Match the start of an iterator without affecting the symbol table. */
998 start = gfc_current_locus;
999 m = gfc_match (" %n =", name);
1000 gfc_current_locus = start;
1002 if (m != MATCH_YES)
1003 return MATCH_NO;
1005 m = gfc_match_variable (&var, 0);
1006 if (m != MATCH_YES)
1007 return MATCH_NO;
1009 if (var->symtree->n.sym->attr.dimension)
1011 gfc_error ("Loop variable at %C cannot be an array");
1012 goto cleanup;
1015 /* F2008, C617 & C565. */
1016 if (var->symtree->n.sym->attr.codimension)
1018 gfc_error ("Loop variable at %C cannot be a coarray");
1019 goto cleanup;
1022 if (var->ref != NULL)
1024 gfc_error ("Loop variable at %C cannot be a sub-component");
1025 goto cleanup;
1028 gfc_match_char ('=');
1030 var->symtree->n.sym->attr.implied_index = 1;
1032 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
1033 if (m == MATCH_NO)
1034 goto syntax;
1035 if (m == MATCH_ERROR)
1036 goto cleanup;
1038 if (gfc_match_char (',') != MATCH_YES)
1039 goto syntax;
1041 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
1042 if (m == MATCH_NO)
1043 goto syntax;
1044 if (m == MATCH_ERROR)
1045 goto cleanup;
1047 if (gfc_match_char (',') != MATCH_YES)
1049 e3 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
1050 goto done;
1053 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
1054 if (m == MATCH_ERROR)
1055 goto cleanup;
1056 if (m == MATCH_NO)
1058 gfc_error ("Expected a step value in iterator at %C");
1059 goto cleanup;
1062 done:
1063 iter->var = var;
1064 iter->start = e1;
1065 iter->end = e2;
1066 iter->step = e3;
1067 return MATCH_YES;
1069 syntax:
1070 gfc_error ("Syntax error in iterator at %C");
1072 cleanup:
1073 gfc_free_expr (e1);
1074 gfc_free_expr (e2);
1075 gfc_free_expr (e3);
1077 return MATCH_ERROR;
1081 /* Tries to match the next non-whitespace character on the input.
1082 This subroutine does not return MATCH_ERROR. */
1084 match
1085 gfc_match_char (char c)
1087 locus where;
1089 where = gfc_current_locus;
1090 gfc_gobble_whitespace ();
1092 if (gfc_next_ascii_char () == c)
1093 return MATCH_YES;
1095 gfc_current_locus = where;
1096 return MATCH_NO;
1100 /* General purpose matching subroutine. The target string is a
1101 scanf-like format string in which spaces correspond to arbitrary
1102 whitespace (including no whitespace), characters correspond to
1103 themselves. The %-codes are:
1105 %% Literal percent sign
1106 %e Expression, pointer to a pointer is set
1107 %s Symbol, pointer to the symbol is set
1108 %n Name, character buffer is set to name
1109 %t Matches end of statement.
1110 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1111 %l Matches a statement label
1112 %v Matches a variable expression (an lvalue)
1113 % Matches a required space (in free form) and optional spaces. */
1115 match
1116 gfc_match (const char *target, ...)
1118 gfc_st_label **label;
1119 int matches, *ip;
1120 locus old_loc;
1121 va_list argp;
1122 char c, *np;
1123 match m, n;
1124 void **vp;
1125 const char *p;
1127 old_loc = gfc_current_locus;
1128 va_start (argp, target);
1129 m = MATCH_NO;
1130 matches = 0;
1131 p = target;
1133 loop:
1134 c = *p++;
1135 switch (c)
1137 case ' ':
1138 gfc_gobble_whitespace ();
1139 goto loop;
1140 case '\0':
1141 m = MATCH_YES;
1142 break;
1144 case '%':
1145 c = *p++;
1146 switch (c)
1148 case 'e':
1149 vp = va_arg (argp, void **);
1150 n = gfc_match_expr ((gfc_expr **) vp);
1151 if (n != MATCH_YES)
1153 m = n;
1154 goto not_yes;
1157 matches++;
1158 goto loop;
1160 case 'v':
1161 vp = va_arg (argp, void **);
1162 n = gfc_match_variable ((gfc_expr **) vp, 0);
1163 if (n != MATCH_YES)
1165 m = n;
1166 goto not_yes;
1169 matches++;
1170 goto loop;
1172 case 's':
1173 vp = va_arg (argp, void **);
1174 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1175 if (n != MATCH_YES)
1177 m = n;
1178 goto not_yes;
1181 matches++;
1182 goto loop;
1184 case 'n':
1185 np = va_arg (argp, char *);
1186 n = gfc_match_name (np);
1187 if (n != MATCH_YES)
1189 m = n;
1190 goto not_yes;
1193 matches++;
1194 goto loop;
1196 case 'l':
1197 label = va_arg (argp, gfc_st_label **);
1198 n = gfc_match_st_label (label);
1199 if (n != MATCH_YES)
1201 m = n;
1202 goto not_yes;
1205 matches++;
1206 goto loop;
1208 case 'o':
1209 ip = va_arg (argp, int *);
1210 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1211 if (n != MATCH_YES)
1213 m = n;
1214 goto not_yes;
1217 matches++;
1218 goto loop;
1220 case 't':
1221 if (gfc_match_eos () != MATCH_YES)
1223 m = MATCH_NO;
1224 goto not_yes;
1226 goto loop;
1228 case ' ':
1229 if (gfc_match_space () == MATCH_YES)
1230 goto loop;
1231 m = MATCH_NO;
1232 goto not_yes;
1234 case '%':
1235 break; /* Fall through to character matcher. */
1237 default:
1238 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1241 default:
1243 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1244 expect an upper case character here! */
1245 gcc_assert (TOLOWER (c) == c);
1247 if (c == gfc_next_ascii_char ())
1248 goto loop;
1249 break;
1252 not_yes:
1253 va_end (argp);
1255 if (m != MATCH_YES)
1257 /* Clean up after a failed match. */
1258 gfc_current_locus = old_loc;
1259 va_start (argp, target);
1261 p = target;
1262 for (; matches > 0; matches--)
1264 while (*p++ != '%');
1266 switch (*p++)
1268 case '%':
1269 matches++;
1270 break; /* Skip. */
1272 /* Matches that don't have to be undone */
1273 case 'o':
1274 case 'l':
1275 case 'n':
1276 case 's':
1277 (void) va_arg (argp, void **);
1278 break;
1280 case 'e':
1281 case 'v':
1282 vp = va_arg (argp, void **);
1283 gfc_free_expr ((struct gfc_expr *)*vp);
1284 *vp = NULL;
1285 break;
1289 va_end (argp);
1292 return m;
1296 /*********************** Statement level matching **********************/
1298 /* Matches the start of a program unit, which is the program keyword
1299 followed by an obligatory symbol. */
1301 match
1302 gfc_match_program (void)
1304 gfc_symbol *sym;
1305 match m;
1307 m = gfc_match ("% %s%t", &sym);
1309 if (m == MATCH_NO)
1311 gfc_error ("Invalid form of PROGRAM statement at %C");
1312 m = MATCH_ERROR;
1315 if (m == MATCH_ERROR)
1316 return m;
1318 if (!gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL))
1319 return MATCH_ERROR;
1321 gfc_new_block = sym;
1323 return MATCH_YES;
1327 /* Match a simple assignment statement. */
1329 match
1330 gfc_match_assignment (void)
1332 gfc_expr *lvalue, *rvalue;
1333 locus old_loc;
1334 match m;
1336 old_loc = gfc_current_locus;
1338 lvalue = NULL;
1339 m = gfc_match (" %v =", &lvalue);
1340 if (m != MATCH_YES)
1342 gfc_current_locus = old_loc;
1343 gfc_free_expr (lvalue);
1344 return MATCH_NO;
1347 rvalue = NULL;
1348 m = gfc_match (" %e%t", &rvalue);
1349 if (m != MATCH_YES)
1351 gfc_current_locus = old_loc;
1352 gfc_free_expr (lvalue);
1353 gfc_free_expr (rvalue);
1354 return m;
1357 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1359 new_st.op = EXEC_ASSIGN;
1360 new_st.expr1 = lvalue;
1361 new_st.expr2 = rvalue;
1363 gfc_check_do_variable (lvalue->symtree);
1365 return MATCH_YES;
1369 /* Match a pointer assignment statement. */
1371 match
1372 gfc_match_pointer_assignment (void)
1374 gfc_expr *lvalue, *rvalue;
1375 locus old_loc;
1376 match m;
1378 old_loc = gfc_current_locus;
1380 lvalue = rvalue = NULL;
1381 gfc_matching_ptr_assignment = 0;
1382 gfc_matching_procptr_assignment = 0;
1384 m = gfc_match (" %v =>", &lvalue);
1385 if (m != MATCH_YES)
1387 m = MATCH_NO;
1388 goto cleanup;
1391 if (lvalue->symtree->n.sym->attr.proc_pointer
1392 || gfc_is_proc_ptr_comp (lvalue))
1393 gfc_matching_procptr_assignment = 1;
1394 else
1395 gfc_matching_ptr_assignment = 1;
1397 m = gfc_match (" %e%t", &rvalue);
1398 gfc_matching_ptr_assignment = 0;
1399 gfc_matching_procptr_assignment = 0;
1400 if (m != MATCH_YES)
1401 goto cleanup;
1403 new_st.op = EXEC_POINTER_ASSIGN;
1404 new_st.expr1 = lvalue;
1405 new_st.expr2 = rvalue;
1407 return MATCH_YES;
1409 cleanup:
1410 gfc_current_locus = old_loc;
1411 gfc_free_expr (lvalue);
1412 gfc_free_expr (rvalue);
1413 return m;
1417 /* We try to match an easy arithmetic IF statement. This only happens
1418 when just after having encountered a simple IF statement. This code
1419 is really duplicate with parts of the gfc_match_if code, but this is
1420 *much* easier. */
1422 static match
1423 match_arithmetic_if (void)
1425 gfc_st_label *l1, *l2, *l3;
1426 gfc_expr *expr;
1427 match m;
1429 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1430 if (m != MATCH_YES)
1431 return m;
1433 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1434 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1435 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1437 gfc_free_expr (expr);
1438 return MATCH_ERROR;
1441 if (!gfc_notify_std (GFC_STD_F95_OBS, "Arithmetic IF statement at %C"))
1442 return MATCH_ERROR;
1444 new_st.op = EXEC_ARITHMETIC_IF;
1445 new_st.expr1 = expr;
1446 new_st.label1 = l1;
1447 new_st.label2 = l2;
1448 new_st.label3 = l3;
1450 return MATCH_YES;
1454 /* The IF statement is a bit of a pain. First of all, there are three
1455 forms of it, the simple IF, the IF that starts a block and the
1456 arithmetic IF.
1458 There is a problem with the simple IF and that is the fact that we
1459 only have a single level of undo information on symbols. What this
1460 means is for a simple IF, we must re-match the whole IF statement
1461 multiple times in order to guarantee that the symbol table ends up
1462 in the proper state. */
1464 static match match_simple_forall (void);
1465 static match match_simple_where (void);
1467 match
1468 gfc_match_if (gfc_statement *if_type)
1470 gfc_expr *expr;
1471 gfc_st_label *l1, *l2, *l3;
1472 locus old_loc, old_loc2;
1473 gfc_code *p;
1474 match m, n;
1476 n = gfc_match_label ();
1477 if (n == MATCH_ERROR)
1478 return n;
1480 old_loc = gfc_current_locus;
1482 m = gfc_match (" if ( %e", &expr);
1483 if (m != MATCH_YES)
1484 return m;
1486 old_loc2 = gfc_current_locus;
1487 gfc_current_locus = old_loc;
1489 if (gfc_match_parens () == MATCH_ERROR)
1490 return MATCH_ERROR;
1492 gfc_current_locus = old_loc2;
1494 if (gfc_match_char (')') != MATCH_YES)
1496 gfc_error ("Syntax error in IF-expression at %C");
1497 gfc_free_expr (expr);
1498 return MATCH_ERROR;
1501 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1503 if (m == MATCH_YES)
1505 if (n == MATCH_YES)
1507 gfc_error ("Block label not appropriate for arithmetic IF "
1508 "statement at %C");
1509 gfc_free_expr (expr);
1510 return MATCH_ERROR;
1513 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1514 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1515 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1517 gfc_free_expr (expr);
1518 return MATCH_ERROR;
1521 if (!gfc_notify_std (GFC_STD_F95_OBS, "Arithmetic IF statement at %C"))
1522 return MATCH_ERROR;
1524 new_st.op = EXEC_ARITHMETIC_IF;
1525 new_st.expr1 = expr;
1526 new_st.label1 = l1;
1527 new_st.label2 = l2;
1528 new_st.label3 = l3;
1530 *if_type = ST_ARITHMETIC_IF;
1531 return MATCH_YES;
1534 if (gfc_match (" then%t") == MATCH_YES)
1536 new_st.op = EXEC_IF;
1537 new_st.expr1 = expr;
1538 *if_type = ST_IF_BLOCK;
1539 return MATCH_YES;
1542 if (n == MATCH_YES)
1544 gfc_error ("Block label is not appropriate for IF statement at %C");
1545 gfc_free_expr (expr);
1546 return MATCH_ERROR;
1549 /* At this point the only thing left is a simple IF statement. At
1550 this point, n has to be MATCH_NO, so we don't have to worry about
1551 re-matching a block label. From what we've got so far, try
1552 matching an assignment. */
1554 *if_type = ST_SIMPLE_IF;
1556 m = gfc_match_assignment ();
1557 if (m == MATCH_YES)
1558 goto got_match;
1560 gfc_free_expr (expr);
1561 gfc_undo_symbols ();
1562 gfc_current_locus = old_loc;
1564 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1565 assignment was found. For MATCH_NO, continue to call the various
1566 matchers. */
1567 if (m == MATCH_ERROR)
1568 return MATCH_ERROR;
1570 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1572 m = gfc_match_pointer_assignment ();
1573 if (m == MATCH_YES)
1574 goto got_match;
1576 gfc_free_expr (expr);
1577 gfc_undo_symbols ();
1578 gfc_current_locus = old_loc;
1580 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1582 /* Look at the next keyword to see which matcher to call. Matching
1583 the keyword doesn't affect the symbol table, so we don't have to
1584 restore between tries. */
1586 #define match(string, subr, statement) \
1587 if (gfc_match (string) == MATCH_YES) { m = subr(); goto got_match; }
1589 gfc_clear_error ();
1591 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1592 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1593 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1594 match ("call", gfc_match_call, ST_CALL)
1595 match ("close", gfc_match_close, ST_CLOSE)
1596 match ("continue", gfc_match_continue, ST_CONTINUE)
1597 match ("cycle", gfc_match_cycle, ST_CYCLE)
1598 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1599 match ("end file", gfc_match_endfile, ST_END_FILE)
1600 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP)
1601 match ("event post", gfc_match_event_post, ST_EVENT_POST)
1602 match ("event wait", gfc_match_event_wait, ST_EVENT_WAIT)
1603 match ("exit", gfc_match_exit, ST_EXIT)
1604 match ("flush", gfc_match_flush, ST_FLUSH)
1605 match ("forall", match_simple_forall, ST_FORALL)
1606 match ("go to", gfc_match_goto, ST_GOTO)
1607 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1608 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1609 match ("lock", gfc_match_lock, ST_LOCK)
1610 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1611 match ("open", gfc_match_open, ST_OPEN)
1612 match ("pause", gfc_match_pause, ST_NONE)
1613 match ("print", gfc_match_print, ST_WRITE)
1614 match ("read", gfc_match_read, ST_READ)
1615 match ("return", gfc_match_return, ST_RETURN)
1616 match ("rewind", gfc_match_rewind, ST_REWIND)
1617 match ("stop", gfc_match_stop, ST_STOP)
1618 match ("wait", gfc_match_wait, ST_WAIT)
1619 match ("sync all", gfc_match_sync_all, ST_SYNC_CALL);
1620 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
1621 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
1622 match ("unlock", gfc_match_unlock, ST_UNLOCK)
1623 match ("where", match_simple_where, ST_WHERE)
1624 match ("write", gfc_match_write, ST_WRITE)
1626 if (flag_dec)
1627 match ("type", gfc_match_print, ST_WRITE)
1629 /* The gfc_match_assignment() above may have returned a MATCH_NO
1630 where the assignment was to a named constant. Check that
1631 special case here. */
1632 m = gfc_match_assignment ();
1633 if (m == MATCH_NO)
1635 gfc_error ("Cannot assign to a named constant at %C");
1636 gfc_free_expr (expr);
1637 gfc_undo_symbols ();
1638 gfc_current_locus = old_loc;
1639 return MATCH_ERROR;
1642 /* All else has failed, so give up. See if any of the matchers has
1643 stored an error message of some sort. */
1644 if (!gfc_error_check ())
1645 gfc_error ("Unclassifiable statement in IF-clause at %C");
1647 gfc_free_expr (expr);
1648 return MATCH_ERROR;
1650 got_match:
1651 if (m == MATCH_NO)
1652 gfc_error ("Syntax error in IF-clause at %C");
1653 if (m != MATCH_YES)
1655 gfc_free_expr (expr);
1656 return MATCH_ERROR;
1659 /* At this point, we've matched the single IF and the action clause
1660 is in new_st. Rearrange things so that the IF statement appears
1661 in new_st. */
1663 p = gfc_get_code (EXEC_IF);
1664 p->next = XCNEW (gfc_code);
1665 *p->next = new_st;
1666 p->next->loc = gfc_current_locus;
1668 p->expr1 = expr;
1670 gfc_clear_new_st ();
1672 new_st.op = EXEC_IF;
1673 new_st.block = p;
1675 return MATCH_YES;
1678 #undef match
1681 /* Match an ELSE statement. */
1683 match
1684 gfc_match_else (void)
1686 char name[GFC_MAX_SYMBOL_LEN + 1];
1688 if (gfc_match_eos () == MATCH_YES)
1689 return MATCH_YES;
1691 if (gfc_match_name (name) != MATCH_YES
1692 || gfc_current_block () == NULL
1693 || gfc_match_eos () != MATCH_YES)
1695 gfc_error ("Unexpected junk after ELSE statement at %C");
1696 return MATCH_ERROR;
1699 if (strcmp (name, gfc_current_block ()->name) != 0)
1701 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1702 name, gfc_current_block ()->name);
1703 return MATCH_ERROR;
1706 return MATCH_YES;
1710 /* Match an ELSE IF statement. */
1712 match
1713 gfc_match_elseif (void)
1715 char name[GFC_MAX_SYMBOL_LEN + 1];
1716 gfc_expr *expr;
1717 match m;
1719 m = gfc_match (" ( %e ) then", &expr);
1720 if (m != MATCH_YES)
1721 return m;
1723 if (gfc_match_eos () == MATCH_YES)
1724 goto done;
1726 if (gfc_match_name (name) != MATCH_YES
1727 || gfc_current_block () == NULL
1728 || gfc_match_eos () != MATCH_YES)
1730 gfc_error ("Unexpected junk after ELSE IF statement at %C");
1731 goto cleanup;
1734 if (strcmp (name, gfc_current_block ()->name) != 0)
1736 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1737 name, gfc_current_block ()->name);
1738 goto cleanup;
1741 done:
1742 new_st.op = EXEC_IF;
1743 new_st.expr1 = expr;
1744 return MATCH_YES;
1746 cleanup:
1747 gfc_free_expr (expr);
1748 return MATCH_ERROR;
1752 /* Free a gfc_iterator structure. */
1754 void
1755 gfc_free_iterator (gfc_iterator *iter, int flag)
1758 if (iter == NULL)
1759 return;
1761 gfc_free_expr (iter->var);
1762 gfc_free_expr (iter->start);
1763 gfc_free_expr (iter->end);
1764 gfc_free_expr (iter->step);
1766 if (flag)
1767 free (iter);
1771 /* Match a CRITICAL statement. */
1772 match
1773 gfc_match_critical (void)
1775 gfc_st_label *label = NULL;
1777 if (gfc_match_label () == MATCH_ERROR)
1778 return MATCH_ERROR;
1780 if (gfc_match (" critical") != MATCH_YES)
1781 return MATCH_NO;
1783 if (gfc_match_st_label (&label) == MATCH_ERROR)
1784 return MATCH_ERROR;
1786 if (gfc_match_eos () != MATCH_YES)
1788 gfc_syntax_error (ST_CRITICAL);
1789 return MATCH_ERROR;
1792 if (gfc_pure (NULL))
1794 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1795 return MATCH_ERROR;
1798 if (gfc_find_state (COMP_DO_CONCURRENT))
1800 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1801 "block");
1802 return MATCH_ERROR;
1805 gfc_unset_implicit_pure (NULL);
1807 if (!gfc_notify_std (GFC_STD_F2008, "CRITICAL statement at %C"))
1808 return MATCH_ERROR;
1810 if (flag_coarray == GFC_FCOARRAY_NONE)
1812 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
1813 "enable");
1814 return MATCH_ERROR;
1817 if (gfc_find_state (COMP_CRITICAL))
1819 gfc_error ("Nested CRITICAL block at %C");
1820 return MATCH_ERROR;
1823 new_st.op = EXEC_CRITICAL;
1825 if (label != NULL
1826 && !gfc_reference_st_label (label, ST_LABEL_TARGET))
1827 return MATCH_ERROR;
1829 return MATCH_YES;
1833 /* Match a BLOCK statement. */
1835 match
1836 gfc_match_block (void)
1838 match m;
1840 if (gfc_match_label () == MATCH_ERROR)
1841 return MATCH_ERROR;
1843 if (gfc_match (" block") != MATCH_YES)
1844 return MATCH_NO;
1846 /* For this to be a correct BLOCK statement, the line must end now. */
1847 m = gfc_match_eos ();
1848 if (m == MATCH_ERROR)
1849 return MATCH_ERROR;
1850 if (m == MATCH_NO)
1851 return MATCH_NO;
1853 return MATCH_YES;
1857 /* Match an ASSOCIATE statement. */
1859 match
1860 gfc_match_associate (void)
1862 if (gfc_match_label () == MATCH_ERROR)
1863 return MATCH_ERROR;
1865 if (gfc_match (" associate") != MATCH_YES)
1866 return MATCH_NO;
1868 /* Match the association list. */
1869 if (gfc_match_char ('(') != MATCH_YES)
1871 gfc_error ("Expected association list at %C");
1872 return MATCH_ERROR;
1874 new_st.ext.block.assoc = NULL;
1875 while (true)
1877 gfc_association_list* newAssoc = gfc_get_association_list ();
1878 gfc_association_list* a;
1880 /* Match the next association. */
1881 if (gfc_match (" %n => %e", newAssoc->name, &newAssoc->target)
1882 != MATCH_YES)
1884 gfc_error ("Expected association at %C");
1885 goto assocListError;
1887 newAssoc->where = gfc_current_locus;
1889 /* Check that the current name is not yet in the list. */
1890 for (a = new_st.ext.block.assoc; a; a = a->next)
1891 if (!strcmp (a->name, newAssoc->name))
1893 gfc_error ("Duplicate name %qs in association at %C",
1894 newAssoc->name);
1895 goto assocListError;
1898 /* The target expression must not be coindexed. */
1899 if (gfc_is_coindexed (newAssoc->target))
1901 gfc_error ("Association target at %C must not be coindexed");
1902 goto assocListError;
1905 /* The `variable' field is left blank for now; because the target is not
1906 yet resolved, we can't use gfc_has_vector_subscript to determine it
1907 for now. This is set during resolution. */
1909 /* Put it into the list. */
1910 newAssoc->next = new_st.ext.block.assoc;
1911 new_st.ext.block.assoc = newAssoc;
1913 /* Try next one or end if closing parenthesis is found. */
1914 gfc_gobble_whitespace ();
1915 if (gfc_peek_char () == ')')
1916 break;
1917 if (gfc_match_char (',') != MATCH_YES)
1919 gfc_error ("Expected %<)%> or %<,%> at %C");
1920 return MATCH_ERROR;
1923 continue;
1925 assocListError:
1926 free (newAssoc);
1927 goto error;
1929 if (gfc_match_char (')') != MATCH_YES)
1931 /* This should never happen as we peek above. */
1932 gcc_unreachable ();
1935 if (gfc_match_eos () != MATCH_YES)
1937 gfc_error ("Junk after ASSOCIATE statement at %C");
1938 goto error;
1941 return MATCH_YES;
1943 error:
1944 gfc_free_association_list (new_st.ext.block.assoc);
1945 return MATCH_ERROR;
1949 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
1950 an accessible derived type. */
1952 static match
1953 match_derived_type_spec (gfc_typespec *ts)
1955 char name[GFC_MAX_SYMBOL_LEN + 1];
1956 locus old_locus;
1957 gfc_symbol *derived;
1959 old_locus = gfc_current_locus;
1961 if (gfc_match ("%n", name) != MATCH_YES)
1963 gfc_current_locus = old_locus;
1964 return MATCH_NO;
1967 gfc_find_symbol (name, NULL, 1, &derived);
1969 if (derived && derived->attr.flavor == FL_PROCEDURE && derived->attr.generic)
1970 derived = gfc_find_dt_in_generic (derived);
1972 if (derived && derived->attr.flavor == FL_DERIVED)
1974 ts->type = BT_DERIVED;
1975 ts->u.derived = derived;
1976 return MATCH_YES;
1979 gfc_current_locus = old_locus;
1980 return MATCH_NO;
1984 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
1985 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
1986 It only includes the intrinsic types from the Fortran 2003 standard
1987 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
1988 the implicit_flag is not needed, so it was removed. Derived types are
1989 identified by their name alone. */
1991 match
1992 gfc_match_type_spec (gfc_typespec *ts)
1994 match m;
1995 locus old_locus;
1996 char name[GFC_MAX_SYMBOL_LEN + 1];
1998 gfc_clear_ts (ts);
1999 gfc_gobble_whitespace ();
2000 old_locus = gfc_current_locus;
2002 if (match_derived_type_spec (ts) == MATCH_YES)
2004 /* Enforce F03:C401. */
2005 if (ts->u.derived->attr.abstract)
2007 gfc_error ("Derived type %qs at %L may not be ABSTRACT",
2008 ts->u.derived->name, &old_locus);
2009 return MATCH_ERROR;
2011 return MATCH_YES;
2014 if (gfc_match ("integer") == MATCH_YES)
2016 ts->type = BT_INTEGER;
2017 ts->kind = gfc_default_integer_kind;
2018 goto kind_selector;
2021 if (gfc_match ("double precision") == MATCH_YES)
2023 ts->type = BT_REAL;
2024 ts->kind = gfc_default_double_kind;
2025 return MATCH_YES;
2028 if (gfc_match ("complex") == MATCH_YES)
2030 ts->type = BT_COMPLEX;
2031 ts->kind = gfc_default_complex_kind;
2032 goto kind_selector;
2035 if (gfc_match ("character") == MATCH_YES)
2037 ts->type = BT_CHARACTER;
2039 m = gfc_match_char_spec (ts);
2041 if (m == MATCH_NO)
2042 m = MATCH_YES;
2044 return m;
2047 if (gfc_match ("logical") == MATCH_YES)
2049 ts->type = BT_LOGICAL;
2050 ts->kind = gfc_default_logical_kind;
2051 goto kind_selector;
2054 /* REAL is a real pain because it can be a type, intrinsic subprogram,
2055 or list item in a type-list of an OpenMP reduction clause. Need to
2056 differentiate REAL([KIND]=scalar-int-initialization-expr) from
2057 REAL(A,[KIND]) and REAL(KIND,A). */
2059 m = gfc_match (" %n", name);
2060 if (m == MATCH_YES && strcmp (name, "real") == 0)
2062 char c;
2063 gfc_expr *e;
2064 locus where;
2066 ts->type = BT_REAL;
2067 ts->kind = gfc_default_real_kind;
2069 gfc_gobble_whitespace ();
2071 /* Prevent REAL*4, etc. */
2072 c = gfc_peek_ascii_char ();
2073 if (c == '*')
2075 gfc_error ("Invalid type-spec at %C");
2076 return MATCH_ERROR;
2079 /* Found leading colon in REAL::, a trailing ')' in for example
2080 TYPE IS (REAL), or REAL, for an OpenMP list-item. */
2081 if (c == ':' || c == ')' || (flag_openmp && c == ','))
2082 return MATCH_YES;
2084 /* Found something other than the opening '(' in REAL(... */
2085 if (c != '(')
2086 return MATCH_NO;
2087 else
2088 gfc_next_char (); /* Burn the '('. */
2090 /* Look for the optional KIND=. */
2091 where = gfc_current_locus;
2092 m = gfc_match ("%n", name);
2093 if (m == MATCH_YES)
2095 gfc_gobble_whitespace ();
2096 c = gfc_next_char ();
2097 if (c == '=')
2099 if (strcmp(name, "a") == 0)
2100 return MATCH_NO;
2101 else if (strcmp(name, "kind") == 0)
2102 goto found;
2103 else
2104 return MATCH_ERROR;
2106 else
2107 gfc_current_locus = where;
2109 else
2110 gfc_current_locus = where;
2112 found:
2114 m = gfc_match_init_expr (&e);
2115 if (m == MATCH_NO || m == MATCH_ERROR)
2116 return MATCH_NO;
2118 /* If a comma appears, it is an intrinsic subprogram. */
2119 gfc_gobble_whitespace ();
2120 c = gfc_peek_ascii_char ();
2121 if (c == ',')
2123 gfc_free_expr (e);
2124 return MATCH_NO;
2127 /* If ')' appears, we have REAL(initialization-expr), here check for
2128 a scalar integer initialization-expr and valid kind parameter. */
2129 if (c == ')')
2131 if (e->ts.type != BT_INTEGER || e->rank > 0)
2133 gfc_free_expr (e);
2134 return MATCH_NO;
2137 gfc_next_char (); /* Burn the ')'. */
2138 ts->kind = (int) mpz_get_si (e->value.integer);
2139 if (gfc_validate_kind (BT_REAL, ts->kind , true) == -1)
2141 gfc_error ("Invalid type-spec at %C");
2142 return MATCH_ERROR;
2145 gfc_free_expr (e);
2147 return MATCH_YES;
2151 /* If a type is not matched, simply return MATCH_NO. */
2152 gfc_current_locus = old_locus;
2153 return MATCH_NO;
2155 kind_selector:
2157 gfc_gobble_whitespace ();
2159 /* This prevents INTEGER*4, etc. */
2160 if (gfc_peek_ascii_char () == '*')
2162 gfc_error ("Invalid type-spec at %C");
2163 return MATCH_ERROR;
2166 m = gfc_match_kind_spec (ts, false);
2168 /* No kind specifier found. */
2169 if (m == MATCH_NO)
2170 m = MATCH_YES;
2172 return m;
2176 /******************** FORALL subroutines ********************/
2178 /* Free a list of FORALL iterators. */
2180 void
2181 gfc_free_forall_iterator (gfc_forall_iterator *iter)
2183 gfc_forall_iterator *next;
2185 while (iter)
2187 next = iter->next;
2188 gfc_free_expr (iter->var);
2189 gfc_free_expr (iter->start);
2190 gfc_free_expr (iter->end);
2191 gfc_free_expr (iter->stride);
2192 free (iter);
2193 iter = next;
2198 /* Match an iterator as part of a FORALL statement. The format is:
2200 <var> = <start>:<end>[:<stride>]
2202 On MATCH_NO, the caller tests for the possibility that there is a
2203 scalar mask expression. */
2205 static match
2206 match_forall_iterator (gfc_forall_iterator **result)
2208 gfc_forall_iterator *iter;
2209 locus where;
2210 match m;
2212 where = gfc_current_locus;
2213 iter = XCNEW (gfc_forall_iterator);
2215 m = gfc_match_expr (&iter->var);
2216 if (m != MATCH_YES)
2217 goto cleanup;
2219 if (gfc_match_char ('=') != MATCH_YES
2220 || iter->var->expr_type != EXPR_VARIABLE)
2222 m = MATCH_NO;
2223 goto cleanup;
2226 m = gfc_match_expr (&iter->start);
2227 if (m != MATCH_YES)
2228 goto cleanup;
2230 if (gfc_match_char (':') != MATCH_YES)
2231 goto syntax;
2233 m = gfc_match_expr (&iter->end);
2234 if (m == MATCH_NO)
2235 goto syntax;
2236 if (m == MATCH_ERROR)
2237 goto cleanup;
2239 if (gfc_match_char (':') == MATCH_NO)
2240 iter->stride = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2241 else
2243 m = gfc_match_expr (&iter->stride);
2244 if (m == MATCH_NO)
2245 goto syntax;
2246 if (m == MATCH_ERROR)
2247 goto cleanup;
2250 /* Mark the iteration variable's symbol as used as a FORALL index. */
2251 iter->var->symtree->n.sym->forall_index = true;
2253 *result = iter;
2254 return MATCH_YES;
2256 syntax:
2257 gfc_error ("Syntax error in FORALL iterator at %C");
2258 m = MATCH_ERROR;
2260 cleanup:
2262 gfc_current_locus = where;
2263 gfc_free_forall_iterator (iter);
2264 return m;
2268 /* Match the header of a FORALL statement. */
2270 static match
2271 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
2273 gfc_forall_iterator *head, *tail, *new_iter;
2274 gfc_expr *msk;
2275 match m;
2277 gfc_gobble_whitespace ();
2279 head = tail = NULL;
2280 msk = NULL;
2282 if (gfc_match_char ('(') != MATCH_YES)
2283 return MATCH_NO;
2285 m = match_forall_iterator (&new_iter);
2286 if (m == MATCH_ERROR)
2287 goto cleanup;
2288 if (m == MATCH_NO)
2289 goto syntax;
2291 head = tail = new_iter;
2293 for (;;)
2295 if (gfc_match_char (',') != MATCH_YES)
2296 break;
2298 m = match_forall_iterator (&new_iter);
2299 if (m == MATCH_ERROR)
2300 goto cleanup;
2302 if (m == MATCH_YES)
2304 tail->next = new_iter;
2305 tail = new_iter;
2306 continue;
2309 /* Have to have a mask expression. */
2311 m = gfc_match_expr (&msk);
2312 if (m == MATCH_NO)
2313 goto syntax;
2314 if (m == MATCH_ERROR)
2315 goto cleanup;
2317 break;
2320 if (gfc_match_char (')') == MATCH_NO)
2321 goto syntax;
2323 *phead = head;
2324 *mask = msk;
2325 return MATCH_YES;
2327 syntax:
2328 gfc_syntax_error (ST_FORALL);
2330 cleanup:
2331 gfc_free_expr (msk);
2332 gfc_free_forall_iterator (head);
2334 return MATCH_ERROR;
2337 /* Match the rest of a simple FORALL statement that follows an
2338 IF statement. */
2340 static match
2341 match_simple_forall (void)
2343 gfc_forall_iterator *head;
2344 gfc_expr *mask;
2345 gfc_code *c;
2346 match m;
2348 mask = NULL;
2349 head = NULL;
2350 c = NULL;
2352 m = match_forall_header (&head, &mask);
2354 if (m == MATCH_NO)
2355 goto syntax;
2356 if (m != MATCH_YES)
2357 goto cleanup;
2359 m = gfc_match_assignment ();
2361 if (m == MATCH_ERROR)
2362 goto cleanup;
2363 if (m == MATCH_NO)
2365 m = gfc_match_pointer_assignment ();
2366 if (m == MATCH_ERROR)
2367 goto cleanup;
2368 if (m == MATCH_NO)
2369 goto syntax;
2372 c = XCNEW (gfc_code);
2373 *c = new_st;
2374 c->loc = gfc_current_locus;
2376 if (gfc_match_eos () != MATCH_YES)
2377 goto syntax;
2379 gfc_clear_new_st ();
2380 new_st.op = EXEC_FORALL;
2381 new_st.expr1 = mask;
2382 new_st.ext.forall_iterator = head;
2383 new_st.block = gfc_get_code (EXEC_FORALL);
2384 new_st.block->next = c;
2386 return MATCH_YES;
2388 syntax:
2389 gfc_syntax_error (ST_FORALL);
2391 cleanup:
2392 gfc_free_forall_iterator (head);
2393 gfc_free_expr (mask);
2395 return MATCH_ERROR;
2399 /* Match a FORALL statement. */
2401 match
2402 gfc_match_forall (gfc_statement *st)
2404 gfc_forall_iterator *head;
2405 gfc_expr *mask;
2406 gfc_code *c;
2407 match m0, m;
2409 head = NULL;
2410 mask = NULL;
2411 c = NULL;
2413 m0 = gfc_match_label ();
2414 if (m0 == MATCH_ERROR)
2415 return MATCH_ERROR;
2417 m = gfc_match (" forall");
2418 if (m != MATCH_YES)
2419 return m;
2421 m = match_forall_header (&head, &mask);
2422 if (m == MATCH_ERROR)
2423 goto cleanup;
2424 if (m == MATCH_NO)
2425 goto syntax;
2427 if (gfc_match_eos () == MATCH_YES)
2429 *st = ST_FORALL_BLOCK;
2430 new_st.op = EXEC_FORALL;
2431 new_st.expr1 = mask;
2432 new_st.ext.forall_iterator = head;
2433 return MATCH_YES;
2436 m = gfc_match_assignment ();
2437 if (m == MATCH_ERROR)
2438 goto cleanup;
2439 if (m == MATCH_NO)
2441 m = gfc_match_pointer_assignment ();
2442 if (m == MATCH_ERROR)
2443 goto cleanup;
2444 if (m == MATCH_NO)
2445 goto syntax;
2448 c = XCNEW (gfc_code);
2449 *c = new_st;
2450 c->loc = gfc_current_locus;
2452 gfc_clear_new_st ();
2453 new_st.op = EXEC_FORALL;
2454 new_st.expr1 = mask;
2455 new_st.ext.forall_iterator = head;
2456 new_st.block = gfc_get_code (EXEC_FORALL);
2457 new_st.block->next = c;
2459 *st = ST_FORALL;
2460 return MATCH_YES;
2462 syntax:
2463 gfc_syntax_error (ST_FORALL);
2465 cleanup:
2466 gfc_free_forall_iterator (head);
2467 gfc_free_expr (mask);
2468 gfc_free_statements (c);
2469 return MATCH_NO;
2473 /* Match a DO statement. */
2475 match
2476 gfc_match_do (void)
2478 gfc_iterator iter, *ip;
2479 locus old_loc;
2480 gfc_st_label *label;
2481 match m;
2483 old_loc = gfc_current_locus;
2485 label = NULL;
2486 iter.var = iter.start = iter.end = iter.step = NULL;
2488 m = gfc_match_label ();
2489 if (m == MATCH_ERROR)
2490 return m;
2492 if (gfc_match (" do") != MATCH_YES)
2493 return MATCH_NO;
2495 m = gfc_match_st_label (&label);
2496 if (m == MATCH_ERROR)
2497 goto cleanup;
2499 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2501 if (gfc_match_eos () == MATCH_YES)
2503 iter.end = gfc_get_logical_expr (gfc_default_logical_kind, NULL, true);
2504 new_st.op = EXEC_DO_WHILE;
2505 goto done;
2508 /* Match an optional comma, if no comma is found, a space is obligatory. */
2509 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
2510 return MATCH_NO;
2512 /* Check for balanced parens. */
2514 if (gfc_match_parens () == MATCH_ERROR)
2515 return MATCH_ERROR;
2517 if (gfc_match (" concurrent") == MATCH_YES)
2519 gfc_forall_iterator *head;
2520 gfc_expr *mask;
2522 if (!gfc_notify_std (GFC_STD_F2008, "DO CONCURRENT construct at %C"))
2523 return MATCH_ERROR;
2526 mask = NULL;
2527 head = NULL;
2528 m = match_forall_header (&head, &mask);
2530 if (m == MATCH_NO)
2531 return m;
2532 if (m == MATCH_ERROR)
2533 goto concurr_cleanup;
2535 if (gfc_match_eos () != MATCH_YES)
2536 goto concurr_cleanup;
2538 if (label != NULL
2539 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2540 goto concurr_cleanup;
2542 new_st.label1 = label;
2543 new_st.op = EXEC_DO_CONCURRENT;
2544 new_st.expr1 = mask;
2545 new_st.ext.forall_iterator = head;
2547 return MATCH_YES;
2549 concurr_cleanup:
2550 gfc_syntax_error (ST_DO);
2551 gfc_free_expr (mask);
2552 gfc_free_forall_iterator (head);
2553 return MATCH_ERROR;
2556 /* See if we have a DO WHILE. */
2557 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
2559 new_st.op = EXEC_DO_WHILE;
2560 goto done;
2563 /* The abortive DO WHILE may have done something to the symbol
2564 table, so we start over. */
2565 gfc_undo_symbols ();
2566 gfc_current_locus = old_loc;
2568 gfc_match_label (); /* This won't error. */
2569 gfc_match (" do "); /* This will work. */
2571 gfc_match_st_label (&label); /* Can't error out. */
2572 gfc_match_char (','); /* Optional comma. */
2574 m = gfc_match_iterator (&iter, 0);
2575 if (m == MATCH_NO)
2576 return MATCH_NO;
2577 if (m == MATCH_ERROR)
2578 goto cleanup;
2580 iter.var->symtree->n.sym->attr.implied_index = 0;
2581 gfc_check_do_variable (iter.var->symtree);
2583 if (gfc_match_eos () != MATCH_YES)
2585 gfc_syntax_error (ST_DO);
2586 goto cleanup;
2589 new_st.op = EXEC_DO;
2591 done:
2592 if (label != NULL
2593 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2594 goto cleanup;
2596 new_st.label1 = label;
2598 if (new_st.op == EXEC_DO_WHILE)
2599 new_st.expr1 = iter.end;
2600 else
2602 new_st.ext.iterator = ip = gfc_get_iterator ();
2603 *ip = iter;
2606 return MATCH_YES;
2608 cleanup:
2609 gfc_free_iterator (&iter, 0);
2611 return MATCH_ERROR;
2615 /* Match an EXIT or CYCLE statement. */
2617 static match
2618 match_exit_cycle (gfc_statement st, gfc_exec_op op)
2620 gfc_state_data *p, *o;
2621 gfc_symbol *sym;
2622 match m;
2623 int cnt;
2625 if (gfc_match_eos () == MATCH_YES)
2626 sym = NULL;
2627 else
2629 char name[GFC_MAX_SYMBOL_LEN + 1];
2630 gfc_symtree* stree;
2632 m = gfc_match ("% %n%t", name);
2633 if (m == MATCH_ERROR)
2634 return MATCH_ERROR;
2635 if (m == MATCH_NO)
2637 gfc_syntax_error (st);
2638 return MATCH_ERROR;
2641 /* Find the corresponding symbol. If there's a BLOCK statement
2642 between here and the label, it is not in gfc_current_ns but a parent
2643 namespace! */
2644 stree = gfc_find_symtree_in_proc (name, gfc_current_ns);
2645 if (!stree)
2647 gfc_error ("Name %qs in %s statement at %C is unknown",
2648 name, gfc_ascii_statement (st));
2649 return MATCH_ERROR;
2652 sym = stree->n.sym;
2653 if (sym->attr.flavor != FL_LABEL)
2655 gfc_error ("Name %qs in %s statement at %C is not a construct name",
2656 name, gfc_ascii_statement (st));
2657 return MATCH_ERROR;
2661 /* Find the loop specified by the label (or lack of a label). */
2662 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
2663 if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
2664 o = p;
2665 else if (p->state == COMP_CRITICAL)
2667 gfc_error("%s statement at %C leaves CRITICAL construct",
2668 gfc_ascii_statement (st));
2669 return MATCH_ERROR;
2671 else if (p->state == COMP_DO_CONCURRENT
2672 && (op == EXEC_EXIT || (sym && sym != p->sym)))
2674 /* F2008, C821 & C845. */
2675 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2676 gfc_ascii_statement (st));
2677 return MATCH_ERROR;
2679 else if ((sym && sym == p->sym)
2680 || (!sym && (p->state == COMP_DO
2681 || p->state == COMP_DO_CONCURRENT)))
2682 break;
2684 if (p == NULL)
2686 if (sym == NULL)
2687 gfc_error ("%s statement at %C is not within a construct",
2688 gfc_ascii_statement (st));
2689 else
2690 gfc_error ("%s statement at %C is not within construct %qs",
2691 gfc_ascii_statement (st), sym->name);
2693 return MATCH_ERROR;
2696 /* Special checks for EXIT from non-loop constructs. */
2697 switch (p->state)
2699 case COMP_DO:
2700 case COMP_DO_CONCURRENT:
2701 break;
2703 case COMP_CRITICAL:
2704 /* This is already handled above. */
2705 gcc_unreachable ();
2707 case COMP_ASSOCIATE:
2708 case COMP_BLOCK:
2709 case COMP_IF:
2710 case COMP_SELECT:
2711 case COMP_SELECT_TYPE:
2712 gcc_assert (sym);
2713 if (op == EXEC_CYCLE)
2715 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2716 " construct %qs", sym->name);
2717 return MATCH_ERROR;
2719 gcc_assert (op == EXEC_EXIT);
2720 if (!gfc_notify_std (GFC_STD_F2008, "EXIT statement with no"
2721 " do-construct-name at %C"))
2722 return MATCH_ERROR;
2723 break;
2725 default:
2726 gfc_error ("%s statement at %C is not applicable to construct %qs",
2727 gfc_ascii_statement (st), sym->name);
2728 return MATCH_ERROR;
2731 if (o != NULL)
2733 gfc_error (is_oacc (p)
2734 ? G_("%s statement at %C leaving OpenACC structured block")
2735 : G_("%s statement at %C leaving OpenMP structured block"),
2736 gfc_ascii_statement (st));
2737 return MATCH_ERROR;
2740 for (o = p, cnt = 0; o->state == COMP_DO && o->previous != NULL; cnt++)
2741 o = o->previous;
2742 if (cnt > 0
2743 && o != NULL
2744 && o->state == COMP_OMP_STRUCTURED_BLOCK
2745 && (o->head->op == EXEC_OACC_LOOP
2746 || o->head->op == EXEC_OACC_PARALLEL_LOOP))
2748 int collapse = 1;
2749 gcc_assert (o->head->next != NULL
2750 && (o->head->next->op == EXEC_DO
2751 || o->head->next->op == EXEC_DO_WHILE)
2752 && o->previous != NULL
2753 && o->previous->tail->op == o->head->op);
2754 if (o->previous->tail->ext.omp_clauses != NULL
2755 && o->previous->tail->ext.omp_clauses->collapse > 1)
2756 collapse = o->previous->tail->ext.omp_clauses->collapse;
2757 if (st == ST_EXIT && cnt <= collapse)
2759 gfc_error ("EXIT statement at %C terminating !$ACC LOOP loop");
2760 return MATCH_ERROR;
2762 if (st == ST_CYCLE && cnt < collapse)
2764 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2765 " !$ACC LOOP loop");
2766 return MATCH_ERROR;
2769 if (cnt > 0
2770 && o != NULL
2771 && (o->state == COMP_OMP_STRUCTURED_BLOCK)
2772 && (o->head->op == EXEC_OMP_DO
2773 || o->head->op == EXEC_OMP_PARALLEL_DO
2774 || o->head->op == EXEC_OMP_SIMD
2775 || o->head->op == EXEC_OMP_DO_SIMD
2776 || o->head->op == EXEC_OMP_PARALLEL_DO_SIMD))
2778 int count = 1;
2779 gcc_assert (o->head->next != NULL
2780 && (o->head->next->op == EXEC_DO
2781 || o->head->next->op == EXEC_DO_WHILE)
2782 && o->previous != NULL
2783 && o->previous->tail->op == o->head->op);
2784 if (o->previous->tail->ext.omp_clauses != NULL)
2786 if (o->previous->tail->ext.omp_clauses->collapse > 1)
2787 count = o->previous->tail->ext.omp_clauses->collapse;
2788 if (o->previous->tail->ext.omp_clauses->orderedc)
2789 count = o->previous->tail->ext.omp_clauses->orderedc;
2791 if (st == ST_EXIT && cnt <= count)
2793 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2794 return MATCH_ERROR;
2796 if (st == ST_CYCLE && cnt < count)
2798 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2799 " !$OMP DO loop");
2800 return MATCH_ERROR;
2804 /* Save the first statement in the construct - needed by the backend. */
2805 new_st.ext.which_construct = p->construct;
2807 new_st.op = op;
2809 return MATCH_YES;
2813 /* Match the EXIT statement. */
2815 match
2816 gfc_match_exit (void)
2818 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
2822 /* Match the CYCLE statement. */
2824 match
2825 gfc_match_cycle (void)
2827 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
2831 /* Match a stop-code after an (ERROR) STOP or PAUSE statement. The
2832 requirements for a stop-code differ in the standards.
2834 Fortran 95 has
2836 R840 stop-stmt is STOP [ stop-code ]
2837 R841 stop-code is scalar-char-constant
2838 or digit [ digit [ digit [ digit [ digit ] ] ] ]
2840 Fortran 2003 matches Fortran 95 except R840 and R841 are now R849 and R850.
2841 Fortran 2008 has
2843 R855 stop-stmt is STOP [ stop-code ]
2844 R856 allstop-stmt is ALL STOP [ stop-code ]
2845 R857 stop-code is scalar-default-char-constant-expr
2846 or scalar-int-constant-expr
2848 For free-form source code, all standards contain a statement of the form:
2850 A blank shall be used to separate names, constants, or labels from
2851 adjacent keywords, names, constants, or labels.
2853 A stop-code is not a name, constant, or label. So, under Fortran 95 and 2003,
2855 STOP123
2857 is valid, but it is invalid Fortran 2008. */
2859 static match
2860 gfc_match_stopcode (gfc_statement st)
2862 gfc_expr *e = NULL;
2863 match m;
2864 bool f95, f03;
2866 /* Set f95 for -std=f95. */
2867 f95 = gfc_option.allow_std == (GFC_STD_F95_OBS | GFC_STD_F95 | GFC_STD_F77
2868 | GFC_STD_F2008_OBS);
2870 /* Set f03 for -std=f2003. */
2871 f03 = gfc_option.allow_std == (GFC_STD_F95_OBS | GFC_STD_F95 | GFC_STD_F77
2872 | GFC_STD_F2008_OBS | GFC_STD_F2003);
2874 /* Look for a blank between STOP and the stop-code for F2008 or later. */
2875 if (gfc_current_form != FORM_FIXED && !(f95 || f03))
2877 char c = gfc_peek_ascii_char ();
2879 /* Look for end-of-statement. There is no stop-code. */
2880 if (c == '\n' || c == '!' || c == ';')
2881 goto done;
2883 if (c != ' ')
2885 gfc_error ("Blank required in %s statement near %C",
2886 gfc_ascii_statement (st));
2887 return MATCH_ERROR;
2891 if (gfc_match_eos () != MATCH_YES)
2893 int stopcode;
2894 locus old_locus;
2896 /* First look for the F95 or F2003 digit [...] construct. */
2897 old_locus = gfc_current_locus;
2898 m = gfc_match_small_int (&stopcode);
2899 if (m == MATCH_YES && (f95 || f03))
2901 if (stopcode < 0)
2903 gfc_error ("STOP code at %C cannot be negative");
2904 return MATCH_ERROR;
2907 if (stopcode > 99999)
2909 gfc_error ("STOP code at %C contains too many digits");
2910 return MATCH_ERROR;
2914 /* Reset the locus and now load gfc_expr. */
2915 gfc_current_locus = old_locus;
2916 m = gfc_match_expr (&e);
2917 if (m == MATCH_ERROR)
2918 goto cleanup;
2919 if (m == MATCH_NO)
2920 goto syntax;
2922 if (gfc_match_eos () != MATCH_YES)
2923 goto syntax;
2926 if (gfc_pure (NULL))
2928 if (st == ST_ERROR_STOP)
2930 if (!gfc_notify_std (GFC_STD_F2015, "%s statement at %C in PURE "
2931 "procedure", gfc_ascii_statement (st)))
2932 goto cleanup;
2934 else
2936 gfc_error ("%s statement not allowed in PURE procedure at %C",
2937 gfc_ascii_statement (st));
2938 goto cleanup;
2942 gfc_unset_implicit_pure (NULL);
2944 if (st == ST_STOP && gfc_find_state (COMP_CRITICAL))
2946 gfc_error ("Image control statement STOP at %C in CRITICAL block");
2947 goto cleanup;
2949 if (st == ST_STOP && gfc_find_state (COMP_DO_CONCURRENT))
2951 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
2952 goto cleanup;
2955 if (e != NULL)
2957 gfc_simplify_expr (e, 0);
2959 /* Test for F95 and F2003 style STOP stop-code. */
2960 if (e->expr_type != EXPR_CONSTANT && (f95 || f03))
2962 gfc_error ("STOP code at %L must be a scalar CHARACTER constant or "
2963 "digit[digit[digit[digit[digit]]]]", &e->where);
2964 goto cleanup;
2967 /* Use the machinery for an initialization expression to reduce the
2968 stop-code to a constant. */
2969 gfc_init_expr_flag = true;
2970 gfc_reduce_init_expr (e);
2971 gfc_init_expr_flag = false;
2973 if (!(e->ts.type == BT_CHARACTER || e->ts.type == BT_INTEGER))
2975 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
2976 &e->where);
2977 goto cleanup;
2980 if (e->rank != 0)
2982 gfc_error ("STOP code at %L must be scalar", &e->where);
2983 goto cleanup;
2986 if (e->ts.type == BT_CHARACTER
2987 && e->ts.kind != gfc_default_character_kind)
2989 gfc_error ("STOP code at %L must be default character KIND=%d",
2990 &e->where, (int) gfc_default_character_kind);
2991 goto cleanup;
2994 if (e->ts.type == BT_INTEGER && e->ts.kind != gfc_default_integer_kind)
2996 gfc_error ("STOP code at %L must be default integer KIND=%d",
2997 &e->where, (int) gfc_default_integer_kind);
2998 goto cleanup;
3002 done:
3004 switch (st)
3006 case ST_STOP:
3007 new_st.op = EXEC_STOP;
3008 break;
3009 case ST_ERROR_STOP:
3010 new_st.op = EXEC_ERROR_STOP;
3011 break;
3012 case ST_PAUSE:
3013 new_st.op = EXEC_PAUSE;
3014 break;
3015 default:
3016 gcc_unreachable ();
3019 new_st.expr1 = e;
3020 new_st.ext.stop_code = -1;
3022 return MATCH_YES;
3024 syntax:
3025 gfc_syntax_error (st);
3027 cleanup:
3029 gfc_free_expr (e);
3030 return MATCH_ERROR;
3034 /* Match the (deprecated) PAUSE statement. */
3036 match
3037 gfc_match_pause (void)
3039 match m;
3041 m = gfc_match_stopcode (ST_PAUSE);
3042 if (m == MATCH_YES)
3044 if (!gfc_notify_std (GFC_STD_F95_DEL, "PAUSE statement at %C"))
3045 m = MATCH_ERROR;
3047 return m;
3051 /* Match the STOP statement. */
3053 match
3054 gfc_match_stop (void)
3056 return gfc_match_stopcode (ST_STOP);
3060 /* Match the ERROR STOP statement. */
3062 match
3063 gfc_match_error_stop (void)
3065 if (!gfc_notify_std (GFC_STD_F2008, "ERROR STOP statement at %C"))
3066 return MATCH_ERROR;
3068 return gfc_match_stopcode (ST_ERROR_STOP);
3071 /* Match EVENT POST/WAIT statement. Syntax:
3072 EVENT POST ( event-variable [, sync-stat-list] )
3073 EVENT WAIT ( event-variable [, wait-spec-list] )
3074 with
3075 wait-spec-list is sync-stat-list or until-spec
3076 until-spec is UNTIL_COUNT = scalar-int-expr
3077 sync-stat is STAT= or ERRMSG=. */
3079 static match
3080 event_statement (gfc_statement st)
3082 match m;
3083 gfc_expr *tmp, *eventvar, *until_count, *stat, *errmsg;
3084 bool saw_until_count, saw_stat, saw_errmsg;
3086 tmp = eventvar = until_count = stat = errmsg = NULL;
3087 saw_until_count = saw_stat = saw_errmsg = false;
3089 if (gfc_pure (NULL))
3091 gfc_error ("Image control statement EVENT %s at %C in PURE procedure",
3092 st == ST_EVENT_POST ? "POST" : "WAIT");
3093 return MATCH_ERROR;
3096 gfc_unset_implicit_pure (NULL);
3098 if (flag_coarray == GFC_FCOARRAY_NONE)
3100 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3101 return MATCH_ERROR;
3104 if (gfc_find_state (COMP_CRITICAL))
3106 gfc_error ("Image control statement EVENT %s at %C in CRITICAL block",
3107 st == ST_EVENT_POST ? "POST" : "WAIT");
3108 return MATCH_ERROR;
3111 if (gfc_find_state (COMP_DO_CONCURRENT))
3113 gfc_error ("Image control statement EVENT %s at %C in DO CONCURRENT "
3114 "block", st == ST_EVENT_POST ? "POST" : "WAIT");
3115 return MATCH_ERROR;
3118 if (gfc_match_char ('(') != MATCH_YES)
3119 goto syntax;
3121 if (gfc_match ("%e", &eventvar) != MATCH_YES)
3122 goto syntax;
3123 m = gfc_match_char (',');
3124 if (m == MATCH_ERROR)
3125 goto syntax;
3126 if (m == MATCH_NO)
3128 m = gfc_match_char (')');
3129 if (m == MATCH_YES)
3130 goto done;
3131 goto syntax;
3134 for (;;)
3136 m = gfc_match (" stat = %v", &tmp);
3137 if (m == MATCH_ERROR)
3138 goto syntax;
3139 if (m == MATCH_YES)
3141 if (saw_stat)
3143 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3144 goto cleanup;
3146 stat = tmp;
3147 saw_stat = true;
3149 m = gfc_match_char (',');
3150 if (m == MATCH_YES)
3151 continue;
3153 tmp = NULL;
3154 break;
3157 m = gfc_match (" errmsg = %v", &tmp);
3158 if (m == MATCH_ERROR)
3159 goto syntax;
3160 if (m == MATCH_YES)
3162 if (saw_errmsg)
3164 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3165 goto cleanup;
3167 errmsg = tmp;
3168 saw_errmsg = true;
3170 m = gfc_match_char (',');
3171 if (m == MATCH_YES)
3172 continue;
3174 tmp = NULL;
3175 break;
3178 m = gfc_match (" until_count = %e", &tmp);
3179 if (m == MATCH_ERROR || st == ST_EVENT_POST)
3180 goto syntax;
3181 if (m == MATCH_YES)
3183 if (saw_until_count)
3185 gfc_error ("Redundant UNTIL_COUNT tag found at %L ",
3186 &tmp->where);
3187 goto cleanup;
3189 until_count = tmp;
3190 saw_until_count = true;
3192 m = gfc_match_char (',');
3193 if (m == MATCH_YES)
3194 continue;
3196 tmp = NULL;
3197 break;
3200 break;
3203 if (m == MATCH_ERROR)
3204 goto syntax;
3206 if (gfc_match (" )%t") != MATCH_YES)
3207 goto syntax;
3209 done:
3210 switch (st)
3212 case ST_EVENT_POST:
3213 new_st.op = EXEC_EVENT_POST;
3214 break;
3215 case ST_EVENT_WAIT:
3216 new_st.op = EXEC_EVENT_WAIT;
3217 break;
3218 default:
3219 gcc_unreachable ();
3222 new_st.expr1 = eventvar;
3223 new_st.expr2 = stat;
3224 new_st.expr3 = errmsg;
3225 new_st.expr4 = until_count;
3227 return MATCH_YES;
3229 syntax:
3230 gfc_syntax_error (st);
3232 cleanup:
3233 if (until_count != tmp)
3234 gfc_free_expr (until_count);
3235 if (errmsg != tmp)
3236 gfc_free_expr (errmsg);
3237 if (stat != tmp)
3238 gfc_free_expr (stat);
3240 gfc_free_expr (tmp);
3241 gfc_free_expr (eventvar);
3243 return MATCH_ERROR;
3248 match
3249 gfc_match_event_post (void)
3251 if (!gfc_notify_std (GFC_STD_F2008_TS, "EVENT POST statement at %C"))
3252 return MATCH_ERROR;
3254 return event_statement (ST_EVENT_POST);
3258 match
3259 gfc_match_event_wait (void)
3261 if (!gfc_notify_std (GFC_STD_F2008_TS, "EVENT WAIT statement at %C"))
3262 return MATCH_ERROR;
3264 return event_statement (ST_EVENT_WAIT);
3268 /* Match LOCK/UNLOCK statement. Syntax:
3269 LOCK ( lock-variable [ , lock-stat-list ] )
3270 UNLOCK ( lock-variable [ , sync-stat-list ] )
3271 where lock-stat is ACQUIRED_LOCK or sync-stat
3272 and sync-stat is STAT= or ERRMSG=. */
3274 static match
3275 lock_unlock_statement (gfc_statement st)
3277 match m;
3278 gfc_expr *tmp, *lockvar, *acq_lock, *stat, *errmsg;
3279 bool saw_acq_lock, saw_stat, saw_errmsg;
3281 tmp = lockvar = acq_lock = stat = errmsg = NULL;
3282 saw_acq_lock = saw_stat = saw_errmsg = false;
3284 if (gfc_pure (NULL))
3286 gfc_error ("Image control statement %s at %C in PURE procedure",
3287 st == ST_LOCK ? "LOCK" : "UNLOCK");
3288 return MATCH_ERROR;
3291 gfc_unset_implicit_pure (NULL);
3293 if (flag_coarray == GFC_FCOARRAY_NONE)
3295 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3296 return MATCH_ERROR;
3299 if (gfc_find_state (COMP_CRITICAL))
3301 gfc_error ("Image control statement %s at %C in CRITICAL block",
3302 st == ST_LOCK ? "LOCK" : "UNLOCK");
3303 return MATCH_ERROR;
3306 if (gfc_find_state (COMP_DO_CONCURRENT))
3308 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
3309 st == ST_LOCK ? "LOCK" : "UNLOCK");
3310 return MATCH_ERROR;
3313 if (gfc_match_char ('(') != MATCH_YES)
3314 goto syntax;
3316 if (gfc_match ("%e", &lockvar) != MATCH_YES)
3317 goto syntax;
3318 m = gfc_match_char (',');
3319 if (m == MATCH_ERROR)
3320 goto syntax;
3321 if (m == MATCH_NO)
3323 m = gfc_match_char (')');
3324 if (m == MATCH_YES)
3325 goto done;
3326 goto syntax;
3329 for (;;)
3331 m = gfc_match (" stat = %v", &tmp);
3332 if (m == MATCH_ERROR)
3333 goto syntax;
3334 if (m == MATCH_YES)
3336 if (saw_stat)
3338 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3339 goto cleanup;
3341 stat = tmp;
3342 saw_stat = true;
3344 m = gfc_match_char (',');
3345 if (m == MATCH_YES)
3346 continue;
3348 tmp = NULL;
3349 break;
3352 m = gfc_match (" errmsg = %v", &tmp);
3353 if (m == MATCH_ERROR)
3354 goto syntax;
3355 if (m == MATCH_YES)
3357 if (saw_errmsg)
3359 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3360 goto cleanup;
3362 errmsg = tmp;
3363 saw_errmsg = true;
3365 m = gfc_match_char (',');
3366 if (m == MATCH_YES)
3367 continue;
3369 tmp = NULL;
3370 break;
3373 m = gfc_match (" acquired_lock = %v", &tmp);
3374 if (m == MATCH_ERROR || st == ST_UNLOCK)
3375 goto syntax;
3376 if (m == MATCH_YES)
3378 if (saw_acq_lock)
3380 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L ",
3381 &tmp->where);
3382 goto cleanup;
3384 acq_lock = tmp;
3385 saw_acq_lock = true;
3387 m = gfc_match_char (',');
3388 if (m == MATCH_YES)
3389 continue;
3391 tmp = NULL;
3392 break;
3395 break;
3398 if (m == MATCH_ERROR)
3399 goto syntax;
3401 if (gfc_match (" )%t") != MATCH_YES)
3402 goto syntax;
3404 done:
3405 switch (st)
3407 case ST_LOCK:
3408 new_st.op = EXEC_LOCK;
3409 break;
3410 case ST_UNLOCK:
3411 new_st.op = EXEC_UNLOCK;
3412 break;
3413 default:
3414 gcc_unreachable ();
3417 new_st.expr1 = lockvar;
3418 new_st.expr2 = stat;
3419 new_st.expr3 = errmsg;
3420 new_st.expr4 = acq_lock;
3422 return MATCH_YES;
3424 syntax:
3425 gfc_syntax_error (st);
3427 cleanup:
3428 if (acq_lock != tmp)
3429 gfc_free_expr (acq_lock);
3430 if (errmsg != tmp)
3431 gfc_free_expr (errmsg);
3432 if (stat != tmp)
3433 gfc_free_expr (stat);
3435 gfc_free_expr (tmp);
3436 gfc_free_expr (lockvar);
3438 return MATCH_ERROR;
3442 match
3443 gfc_match_lock (void)
3445 if (!gfc_notify_std (GFC_STD_F2008, "LOCK statement at %C"))
3446 return MATCH_ERROR;
3448 return lock_unlock_statement (ST_LOCK);
3452 match
3453 gfc_match_unlock (void)
3455 if (!gfc_notify_std (GFC_STD_F2008, "UNLOCK statement at %C"))
3456 return MATCH_ERROR;
3458 return lock_unlock_statement (ST_UNLOCK);
3462 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
3463 SYNC ALL [(sync-stat-list)]
3464 SYNC MEMORY [(sync-stat-list)]
3465 SYNC IMAGES (image-set [, sync-stat-list] )
3466 with sync-stat is int-expr or *. */
3468 static match
3469 sync_statement (gfc_statement st)
3471 match m;
3472 gfc_expr *tmp, *imageset, *stat, *errmsg;
3473 bool saw_stat, saw_errmsg;
3475 tmp = imageset = stat = errmsg = NULL;
3476 saw_stat = saw_errmsg = false;
3478 if (gfc_pure (NULL))
3480 gfc_error ("Image control statement SYNC at %C in PURE procedure");
3481 return MATCH_ERROR;
3484 gfc_unset_implicit_pure (NULL);
3486 if (!gfc_notify_std (GFC_STD_F2008, "SYNC statement at %C"))
3487 return MATCH_ERROR;
3489 if (flag_coarray == GFC_FCOARRAY_NONE)
3491 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
3492 "enable");
3493 return MATCH_ERROR;
3496 if (gfc_find_state (COMP_CRITICAL))
3498 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
3499 return MATCH_ERROR;
3502 if (gfc_find_state (COMP_DO_CONCURRENT))
3504 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
3505 return MATCH_ERROR;
3508 if (gfc_match_eos () == MATCH_YES)
3510 if (st == ST_SYNC_IMAGES)
3511 goto syntax;
3512 goto done;
3515 if (gfc_match_char ('(') != MATCH_YES)
3516 goto syntax;
3518 if (st == ST_SYNC_IMAGES)
3520 /* Denote '*' as imageset == NULL. */
3521 m = gfc_match_char ('*');
3522 if (m == MATCH_ERROR)
3523 goto syntax;
3524 if (m == MATCH_NO)
3526 if (gfc_match ("%e", &imageset) != MATCH_YES)
3527 goto syntax;
3529 m = gfc_match_char (',');
3530 if (m == MATCH_ERROR)
3531 goto syntax;
3532 if (m == MATCH_NO)
3534 m = gfc_match_char (')');
3535 if (m == MATCH_YES)
3536 goto done;
3537 goto syntax;
3541 for (;;)
3543 m = gfc_match (" stat = %v", &tmp);
3544 if (m == MATCH_ERROR)
3545 goto syntax;
3546 if (m == MATCH_YES)
3548 if (saw_stat)
3550 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
3551 goto cleanup;
3553 stat = tmp;
3554 saw_stat = true;
3556 if (gfc_match_char (',') == MATCH_YES)
3557 continue;
3559 tmp = NULL;
3560 break;
3563 m = gfc_match (" errmsg = %v", &tmp);
3564 if (m == MATCH_ERROR)
3565 goto syntax;
3566 if (m == MATCH_YES)
3568 if (saw_errmsg)
3570 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
3571 goto cleanup;
3573 errmsg = tmp;
3574 saw_errmsg = true;
3576 if (gfc_match_char (',') == MATCH_YES)
3577 continue;
3579 tmp = NULL;
3580 break;
3583 break;
3586 if (gfc_match (" )%t") != MATCH_YES)
3587 goto syntax;
3589 done:
3590 switch (st)
3592 case ST_SYNC_ALL:
3593 new_st.op = EXEC_SYNC_ALL;
3594 break;
3595 case ST_SYNC_IMAGES:
3596 new_st.op = EXEC_SYNC_IMAGES;
3597 break;
3598 case ST_SYNC_MEMORY:
3599 new_st.op = EXEC_SYNC_MEMORY;
3600 break;
3601 default:
3602 gcc_unreachable ();
3605 new_st.expr1 = imageset;
3606 new_st.expr2 = stat;
3607 new_st.expr3 = errmsg;
3609 return MATCH_YES;
3611 syntax:
3612 gfc_syntax_error (st);
3614 cleanup:
3615 if (stat != tmp)
3616 gfc_free_expr (stat);
3617 if (errmsg != tmp)
3618 gfc_free_expr (errmsg);
3620 gfc_free_expr (tmp);
3621 gfc_free_expr (imageset);
3623 return MATCH_ERROR;
3627 /* Match SYNC ALL statement. */
3629 match
3630 gfc_match_sync_all (void)
3632 return sync_statement (ST_SYNC_ALL);
3636 /* Match SYNC IMAGES statement. */
3638 match
3639 gfc_match_sync_images (void)
3641 return sync_statement (ST_SYNC_IMAGES);
3645 /* Match SYNC MEMORY statement. */
3647 match
3648 gfc_match_sync_memory (void)
3650 return sync_statement (ST_SYNC_MEMORY);
3654 /* Match a CONTINUE statement. */
3656 match
3657 gfc_match_continue (void)
3659 if (gfc_match_eos () != MATCH_YES)
3661 gfc_syntax_error (ST_CONTINUE);
3662 return MATCH_ERROR;
3665 new_st.op = EXEC_CONTINUE;
3666 return MATCH_YES;
3670 /* Match the (deprecated) ASSIGN statement. */
3672 match
3673 gfc_match_assign (void)
3675 gfc_expr *expr;
3676 gfc_st_label *label;
3678 if (gfc_match (" %l", &label) == MATCH_YES)
3680 if (!gfc_reference_st_label (label, ST_LABEL_UNKNOWN))
3681 return MATCH_ERROR;
3682 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
3684 if (!gfc_notify_std (GFC_STD_F95_DEL, "ASSIGN statement at %C"))
3685 return MATCH_ERROR;
3687 expr->symtree->n.sym->attr.assign = 1;
3689 new_st.op = EXEC_LABEL_ASSIGN;
3690 new_st.label1 = label;
3691 new_st.expr1 = expr;
3692 return MATCH_YES;
3695 return MATCH_NO;
3699 /* Match the GO TO statement. As a computed GOTO statement is
3700 matched, it is transformed into an equivalent SELECT block. No
3701 tree is necessary, and the resulting jumps-to-jumps are
3702 specifically optimized away by the back end. */
3704 match
3705 gfc_match_goto (void)
3707 gfc_code *head, *tail;
3708 gfc_expr *expr;
3709 gfc_case *cp;
3710 gfc_st_label *label;
3711 int i;
3712 match m;
3714 if (gfc_match (" %l%t", &label) == MATCH_YES)
3716 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3717 return MATCH_ERROR;
3719 new_st.op = EXEC_GOTO;
3720 new_st.label1 = label;
3721 return MATCH_YES;
3724 /* The assigned GO TO statement. */
3726 if (gfc_match_variable (&expr, 0) == MATCH_YES)
3728 if (!gfc_notify_std (GFC_STD_F95_DEL, "Assigned GOTO statement at %C"))
3729 return MATCH_ERROR;
3731 new_st.op = EXEC_GOTO;
3732 new_st.expr1 = expr;
3734 if (gfc_match_eos () == MATCH_YES)
3735 return MATCH_YES;
3737 /* Match label list. */
3738 gfc_match_char (',');
3739 if (gfc_match_char ('(') != MATCH_YES)
3741 gfc_syntax_error (ST_GOTO);
3742 return MATCH_ERROR;
3744 head = tail = NULL;
3748 m = gfc_match_st_label (&label);
3749 if (m != MATCH_YES)
3750 goto syntax;
3752 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3753 goto cleanup;
3755 if (head == NULL)
3756 head = tail = gfc_get_code (EXEC_GOTO);
3757 else
3759 tail->block = gfc_get_code (EXEC_GOTO);
3760 tail = tail->block;
3763 tail->label1 = label;
3765 while (gfc_match_char (',') == MATCH_YES);
3767 if (gfc_match (")%t") != MATCH_YES)
3768 goto syntax;
3770 if (head == NULL)
3772 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3773 goto syntax;
3775 new_st.block = head;
3777 return MATCH_YES;
3780 /* Last chance is a computed GO TO statement. */
3781 if (gfc_match_char ('(') != MATCH_YES)
3783 gfc_syntax_error (ST_GOTO);
3784 return MATCH_ERROR;
3787 head = tail = NULL;
3788 i = 1;
3792 m = gfc_match_st_label (&label);
3793 if (m != MATCH_YES)
3794 goto syntax;
3796 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3797 goto cleanup;
3799 if (head == NULL)
3800 head = tail = gfc_get_code (EXEC_SELECT);
3801 else
3803 tail->block = gfc_get_code (EXEC_SELECT);
3804 tail = tail->block;
3807 cp = gfc_get_case ();
3808 cp->low = cp->high = gfc_get_int_expr (gfc_default_integer_kind,
3809 NULL, i++);
3811 tail->ext.block.case_list = cp;
3813 tail->next = gfc_get_code (EXEC_GOTO);
3814 tail->next->label1 = label;
3816 while (gfc_match_char (',') == MATCH_YES);
3818 if (gfc_match_char (')') != MATCH_YES)
3819 goto syntax;
3821 if (head == NULL)
3823 gfc_error ("Statement label list in GOTO at %C cannot be empty");
3824 goto syntax;
3827 /* Get the rest of the statement. */
3828 gfc_match_char (',');
3830 if (gfc_match (" %e%t", &expr) != MATCH_YES)
3831 goto syntax;
3833 if (!gfc_notify_std (GFC_STD_F95_OBS, "Computed GOTO at %C"))
3834 return MATCH_ERROR;
3836 /* At this point, a computed GOTO has been fully matched and an
3837 equivalent SELECT statement constructed. */
3839 new_st.op = EXEC_SELECT;
3840 new_st.expr1 = NULL;
3842 /* Hack: For a "real" SELECT, the expression is in expr. We put
3843 it in expr2 so we can distinguish then and produce the correct
3844 diagnostics. */
3845 new_st.expr2 = expr;
3846 new_st.block = head;
3847 return MATCH_YES;
3849 syntax:
3850 gfc_syntax_error (ST_GOTO);
3851 cleanup:
3852 gfc_free_statements (head);
3853 return MATCH_ERROR;
3857 /* Frees a list of gfc_alloc structures. */
3859 void
3860 gfc_free_alloc_list (gfc_alloc *p)
3862 gfc_alloc *q;
3864 for (; p; p = q)
3866 q = p->next;
3867 gfc_free_expr (p->expr);
3868 free (p);
3873 /* Match an ALLOCATE statement. */
3875 match
3876 gfc_match_allocate (void)
3878 gfc_alloc *head, *tail;
3879 gfc_expr *stat, *errmsg, *tmp, *source, *mold;
3880 gfc_typespec ts;
3881 gfc_symbol *sym;
3882 match m;
3883 locus old_locus, deferred_locus;
3884 bool saw_stat, saw_errmsg, saw_source, saw_mold, saw_deferred, b1, b2, b3;
3885 bool saw_unlimited = false;
3887 head = tail = NULL;
3888 stat = errmsg = source = mold = tmp = NULL;
3889 saw_stat = saw_errmsg = saw_source = saw_mold = saw_deferred = false;
3891 if (gfc_match_char ('(') != MATCH_YES)
3892 goto syntax;
3894 /* Match an optional type-spec. */
3895 old_locus = gfc_current_locus;
3896 m = gfc_match_type_spec (&ts);
3897 if (m == MATCH_ERROR)
3898 goto cleanup;
3899 else if (m == MATCH_NO)
3901 char name[GFC_MAX_SYMBOL_LEN + 3];
3903 if (gfc_match ("%n :: ", name) == MATCH_YES)
3905 gfc_error ("Error in type-spec at %L", &old_locus);
3906 goto cleanup;
3909 ts.type = BT_UNKNOWN;
3911 else
3913 if (gfc_match (" :: ") == MATCH_YES)
3915 if (!gfc_notify_std (GFC_STD_F2003, "typespec in ALLOCATE at %L",
3916 &old_locus))
3917 goto cleanup;
3919 if (ts.deferred)
3921 gfc_error ("Type-spec at %L cannot contain a deferred "
3922 "type parameter", &old_locus);
3923 goto cleanup;
3926 if (ts.type == BT_CHARACTER)
3927 ts.u.cl->length_from_typespec = true;
3929 else
3931 ts.type = BT_UNKNOWN;
3932 gfc_current_locus = old_locus;
3936 for (;;)
3938 if (head == NULL)
3939 head = tail = gfc_get_alloc ();
3940 else
3942 tail->next = gfc_get_alloc ();
3943 tail = tail->next;
3946 m = gfc_match_variable (&tail->expr, 0);
3947 if (m == MATCH_NO)
3948 goto syntax;
3949 if (m == MATCH_ERROR)
3950 goto cleanup;
3952 if (gfc_check_do_variable (tail->expr->symtree))
3953 goto cleanup;
3955 bool impure = gfc_impure_variable (tail->expr->symtree->n.sym);
3956 if (impure && gfc_pure (NULL))
3958 gfc_error ("Bad allocate-object at %C for a PURE procedure");
3959 goto cleanup;
3962 if (impure)
3963 gfc_unset_implicit_pure (NULL);
3965 if (tail->expr->ts.deferred)
3967 saw_deferred = true;
3968 deferred_locus = tail->expr->where;
3971 if (gfc_find_state (COMP_DO_CONCURRENT)
3972 || gfc_find_state (COMP_CRITICAL))
3974 gfc_ref *ref;
3975 bool coarray = tail->expr->symtree->n.sym->attr.codimension;
3976 for (ref = tail->expr->ref; ref; ref = ref->next)
3977 if (ref->type == REF_COMPONENT)
3978 coarray = ref->u.c.component->attr.codimension;
3980 if (coarray && gfc_find_state (COMP_DO_CONCURRENT))
3982 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
3983 goto cleanup;
3985 if (coarray && gfc_find_state (COMP_CRITICAL))
3987 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
3988 goto cleanup;
3992 /* Check for F08:C628. */
3993 sym = tail->expr->symtree->n.sym;
3994 b1 = !(tail->expr->ref
3995 && (tail->expr->ref->type == REF_COMPONENT
3996 || tail->expr->ref->type == REF_ARRAY));
3997 if (sym && sym->ts.type == BT_CLASS && sym->attr.class_ok)
3998 b2 = !(CLASS_DATA (sym)->attr.allocatable
3999 || CLASS_DATA (sym)->attr.class_pointer);
4000 else
4001 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4002 || sym->attr.proc_pointer);
4003 b3 = sym && sym->ns && sym->ns->proc_name
4004 && (sym->ns->proc_name->attr.allocatable
4005 || sym->ns->proc_name->attr.pointer
4006 || sym->ns->proc_name->attr.proc_pointer);
4007 if (b1 && b2 && !b3)
4009 gfc_error ("Allocate-object at %L is neither a data pointer "
4010 "nor an allocatable variable", &tail->expr->where);
4011 goto cleanup;
4014 /* The ALLOCATE statement had an optional typespec. Check the
4015 constraints. */
4016 if (ts.type != BT_UNKNOWN)
4018 /* Enforce F03:C624. */
4019 if (!gfc_type_compatible (&tail->expr->ts, &ts))
4021 gfc_error ("Type of entity at %L is type incompatible with "
4022 "typespec", &tail->expr->where);
4023 goto cleanup;
4026 /* Enforce F03:C627. */
4027 if (ts.kind != tail->expr->ts.kind && !UNLIMITED_POLY (tail->expr))
4029 gfc_error ("Kind type parameter for entity at %L differs from "
4030 "the kind type parameter of the typespec",
4031 &tail->expr->where);
4032 goto cleanup;
4036 if (tail->expr->ts.type == BT_DERIVED)
4037 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
4039 saw_unlimited = saw_unlimited | UNLIMITED_POLY (tail->expr);
4041 if (gfc_peek_ascii_char () == '(' && !sym->attr.dimension)
4043 gfc_error ("Shape specification for allocatable scalar at %C");
4044 goto cleanup;
4047 if (gfc_match_char (',') != MATCH_YES)
4048 break;
4050 alloc_opt_list:
4052 m = gfc_match (" stat = %v", &tmp);
4053 if (m == MATCH_ERROR)
4054 goto cleanup;
4055 if (m == MATCH_YES)
4057 /* Enforce C630. */
4058 if (saw_stat)
4060 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
4061 goto cleanup;
4064 stat = tmp;
4065 tmp = NULL;
4066 saw_stat = true;
4068 if (gfc_check_do_variable (stat->symtree))
4069 goto cleanup;
4071 if (gfc_match_char (',') == MATCH_YES)
4072 goto alloc_opt_list;
4075 m = gfc_match (" errmsg = %v", &tmp);
4076 if (m == MATCH_ERROR)
4077 goto cleanup;
4078 if (m == MATCH_YES)
4080 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG tag at %L", &tmp->where))
4081 goto cleanup;
4083 /* Enforce C630. */
4084 if (saw_errmsg)
4086 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
4087 goto cleanup;
4090 errmsg = tmp;
4091 tmp = NULL;
4092 saw_errmsg = true;
4094 if (gfc_match_char (',') == MATCH_YES)
4095 goto alloc_opt_list;
4098 m = gfc_match (" source = %e", &tmp);
4099 if (m == MATCH_ERROR)
4100 goto cleanup;
4101 if (m == MATCH_YES)
4103 if (!gfc_notify_std (GFC_STD_F2003, "SOURCE tag at %L", &tmp->where))
4104 goto cleanup;
4106 /* Enforce C630. */
4107 if (saw_source)
4109 gfc_error ("Redundant SOURCE tag found at %L ", &tmp->where);
4110 goto cleanup;
4113 /* The next 2 conditionals check C631. */
4114 if (ts.type != BT_UNKNOWN)
4116 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
4117 &tmp->where, &old_locus);
4118 goto cleanup;
4121 if (head->next
4122 && !gfc_notify_std (GFC_STD_F2008, "SOURCE tag at %L"
4123 " with more than a single allocate object",
4124 &tmp->where))
4125 goto cleanup;
4127 source = tmp;
4128 tmp = NULL;
4129 saw_source = true;
4131 if (gfc_match_char (',') == MATCH_YES)
4132 goto alloc_opt_list;
4135 m = gfc_match (" mold = %e", &tmp);
4136 if (m == MATCH_ERROR)
4137 goto cleanup;
4138 if (m == MATCH_YES)
4140 if (!gfc_notify_std (GFC_STD_F2008, "MOLD tag at %L", &tmp->where))
4141 goto cleanup;
4143 /* Check F08:C636. */
4144 if (saw_mold)
4146 gfc_error ("Redundant MOLD tag found at %L ", &tmp->where);
4147 goto cleanup;
4150 /* Check F08:C637. */
4151 if (ts.type != BT_UNKNOWN)
4153 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
4154 &tmp->where, &old_locus);
4155 goto cleanup;
4158 mold = tmp;
4159 tmp = NULL;
4160 saw_mold = true;
4161 mold->mold = 1;
4163 if (gfc_match_char (',') == MATCH_YES)
4164 goto alloc_opt_list;
4167 gfc_gobble_whitespace ();
4169 if (gfc_peek_char () == ')')
4170 break;
4173 if (gfc_match (" )%t") != MATCH_YES)
4174 goto syntax;
4176 /* Check F08:C637. */
4177 if (source && mold)
4179 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
4180 &mold->where, &source->where);
4181 goto cleanup;
4184 /* Check F03:C623, */
4185 if (saw_deferred && ts.type == BT_UNKNOWN && !source && !mold)
4187 gfc_error ("Allocate-object at %L with a deferred type parameter "
4188 "requires either a type-spec or SOURCE tag or a MOLD tag",
4189 &deferred_locus);
4190 goto cleanup;
4193 /* Check F03:C625, */
4194 if (saw_unlimited && ts.type == BT_UNKNOWN && !source && !mold)
4196 for (tail = head; tail; tail = tail->next)
4198 if (UNLIMITED_POLY (tail->expr))
4199 gfc_error ("Unlimited polymorphic allocate-object at %L "
4200 "requires either a type-spec or SOURCE tag "
4201 "or a MOLD tag", &tail->expr->where);
4203 goto cleanup;
4206 new_st.op = EXEC_ALLOCATE;
4207 new_st.expr1 = stat;
4208 new_st.expr2 = errmsg;
4209 if (source)
4210 new_st.expr3 = source;
4211 else
4212 new_st.expr3 = mold;
4213 new_st.ext.alloc.list = head;
4214 new_st.ext.alloc.ts = ts;
4216 return MATCH_YES;
4218 syntax:
4219 gfc_syntax_error (ST_ALLOCATE);
4221 cleanup:
4222 gfc_free_expr (errmsg);
4223 gfc_free_expr (source);
4224 gfc_free_expr (stat);
4225 gfc_free_expr (mold);
4226 if (tmp && tmp->expr_type) gfc_free_expr (tmp);
4227 gfc_free_alloc_list (head);
4228 return MATCH_ERROR;
4232 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
4233 a set of pointer assignments to intrinsic NULL(). */
4235 match
4236 gfc_match_nullify (void)
4238 gfc_code *tail;
4239 gfc_expr *e, *p;
4240 match m;
4242 tail = NULL;
4244 if (gfc_match_char ('(') != MATCH_YES)
4245 goto syntax;
4247 for (;;)
4249 m = gfc_match_variable (&p, 0);
4250 if (m == MATCH_ERROR)
4251 goto cleanup;
4252 if (m == MATCH_NO)
4253 goto syntax;
4255 if (gfc_check_do_variable (p->symtree))
4256 goto cleanup;
4258 /* F2008, C1242. */
4259 if (gfc_is_coindexed (p))
4261 gfc_error ("Pointer object at %C shall not be coindexed");
4262 goto cleanup;
4265 /* build ' => NULL() '. */
4266 e = gfc_get_null_expr (&gfc_current_locus);
4268 /* Chain to list. */
4269 if (tail == NULL)
4271 tail = &new_st;
4272 tail->op = EXEC_POINTER_ASSIGN;
4274 else
4276 tail->next = gfc_get_code (EXEC_POINTER_ASSIGN);
4277 tail = tail->next;
4280 tail->expr1 = p;
4281 tail->expr2 = e;
4283 if (gfc_match (" )%t") == MATCH_YES)
4284 break;
4285 if (gfc_match_char (',') != MATCH_YES)
4286 goto syntax;
4289 return MATCH_YES;
4291 syntax:
4292 gfc_syntax_error (ST_NULLIFY);
4294 cleanup:
4295 gfc_free_statements (new_st.next);
4296 new_st.next = NULL;
4297 gfc_free_expr (new_st.expr1);
4298 new_st.expr1 = NULL;
4299 gfc_free_expr (new_st.expr2);
4300 new_st.expr2 = NULL;
4301 return MATCH_ERROR;
4305 /* Match a DEALLOCATE statement. */
4307 match
4308 gfc_match_deallocate (void)
4310 gfc_alloc *head, *tail;
4311 gfc_expr *stat, *errmsg, *tmp;
4312 gfc_symbol *sym;
4313 match m;
4314 bool saw_stat, saw_errmsg, b1, b2;
4316 head = tail = NULL;
4317 stat = errmsg = tmp = NULL;
4318 saw_stat = saw_errmsg = false;
4320 if (gfc_match_char ('(') != MATCH_YES)
4321 goto syntax;
4323 for (;;)
4325 if (head == NULL)
4326 head = tail = gfc_get_alloc ();
4327 else
4329 tail->next = gfc_get_alloc ();
4330 tail = tail->next;
4333 m = gfc_match_variable (&tail->expr, 0);
4334 if (m == MATCH_ERROR)
4335 goto cleanup;
4336 if (m == MATCH_NO)
4337 goto syntax;
4339 if (gfc_check_do_variable (tail->expr->symtree))
4340 goto cleanup;
4342 sym = tail->expr->symtree->n.sym;
4344 bool impure = gfc_impure_variable (sym);
4345 if (impure && gfc_pure (NULL))
4347 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
4348 goto cleanup;
4351 if (impure)
4352 gfc_unset_implicit_pure (NULL);
4354 if (gfc_is_coarray (tail->expr)
4355 && gfc_find_state (COMP_DO_CONCURRENT))
4357 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
4358 goto cleanup;
4361 if (gfc_is_coarray (tail->expr)
4362 && gfc_find_state (COMP_CRITICAL))
4364 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
4365 goto cleanup;
4368 /* FIXME: disable the checking on derived types. */
4369 b1 = !(tail->expr->ref
4370 && (tail->expr->ref->type == REF_COMPONENT
4371 || tail->expr->ref->type == REF_ARRAY));
4372 if (sym && sym->ts.type == BT_CLASS)
4373 b2 = !(CLASS_DATA (sym)->attr.allocatable
4374 || CLASS_DATA (sym)->attr.class_pointer);
4375 else
4376 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4377 || sym->attr.proc_pointer);
4378 if (b1 && b2)
4380 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
4381 "nor an allocatable variable");
4382 goto cleanup;
4385 if (gfc_match_char (',') != MATCH_YES)
4386 break;
4388 dealloc_opt_list:
4390 m = gfc_match (" stat = %v", &tmp);
4391 if (m == MATCH_ERROR)
4392 goto cleanup;
4393 if (m == MATCH_YES)
4395 if (saw_stat)
4397 gfc_error ("Redundant STAT tag found at %L ", &tmp->where);
4398 gfc_free_expr (tmp);
4399 goto cleanup;
4402 stat = tmp;
4403 saw_stat = true;
4405 if (gfc_check_do_variable (stat->symtree))
4406 goto cleanup;
4408 if (gfc_match_char (',') == MATCH_YES)
4409 goto dealloc_opt_list;
4412 m = gfc_match (" errmsg = %v", &tmp);
4413 if (m == MATCH_ERROR)
4414 goto cleanup;
4415 if (m == MATCH_YES)
4417 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG at %L", &tmp->where))
4418 goto cleanup;
4420 if (saw_errmsg)
4422 gfc_error ("Redundant ERRMSG tag found at %L ", &tmp->where);
4423 gfc_free_expr (tmp);
4424 goto cleanup;
4427 errmsg = tmp;
4428 saw_errmsg = true;
4430 if (gfc_match_char (',') == MATCH_YES)
4431 goto dealloc_opt_list;
4434 gfc_gobble_whitespace ();
4436 if (gfc_peek_char () == ')')
4437 break;
4440 if (gfc_match (" )%t") != MATCH_YES)
4441 goto syntax;
4443 new_st.op = EXEC_DEALLOCATE;
4444 new_st.expr1 = stat;
4445 new_st.expr2 = errmsg;
4446 new_st.ext.alloc.list = head;
4448 return MATCH_YES;
4450 syntax:
4451 gfc_syntax_error (ST_DEALLOCATE);
4453 cleanup:
4454 gfc_free_expr (errmsg);
4455 gfc_free_expr (stat);
4456 gfc_free_alloc_list (head);
4457 return MATCH_ERROR;
4461 /* Match a RETURN statement. */
4463 match
4464 gfc_match_return (void)
4466 gfc_expr *e;
4467 match m;
4468 gfc_compile_state s;
4470 e = NULL;
4472 if (gfc_find_state (COMP_CRITICAL))
4474 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
4475 return MATCH_ERROR;
4478 if (gfc_find_state (COMP_DO_CONCURRENT))
4480 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
4481 return MATCH_ERROR;
4484 if (gfc_match_eos () == MATCH_YES)
4485 goto done;
4487 if (!gfc_find_state (COMP_SUBROUTINE))
4489 gfc_error ("Alternate RETURN statement at %C is only allowed within "
4490 "a SUBROUTINE");
4491 goto cleanup;
4494 if (gfc_current_form == FORM_FREE)
4496 /* The following are valid, so we can't require a blank after the
4497 RETURN keyword:
4498 return+1
4499 return(1) */
4500 char c = gfc_peek_ascii_char ();
4501 if (ISALPHA (c) || ISDIGIT (c))
4502 return MATCH_NO;
4505 m = gfc_match (" %e%t", &e);
4506 if (m == MATCH_YES)
4507 goto done;
4508 if (m == MATCH_ERROR)
4509 goto cleanup;
4511 gfc_syntax_error (ST_RETURN);
4513 cleanup:
4514 gfc_free_expr (e);
4515 return MATCH_ERROR;
4517 done:
4518 gfc_enclosing_unit (&s);
4519 if (s == COMP_PROGRAM
4520 && !gfc_notify_std (GFC_STD_GNU, "RETURN statement in "
4521 "main program at %C"))
4522 return MATCH_ERROR;
4524 new_st.op = EXEC_RETURN;
4525 new_st.expr1 = e;
4527 return MATCH_YES;
4531 /* Match the call of a type-bound procedure, if CALL%var has already been
4532 matched and var found to be a derived-type variable. */
4534 static match
4535 match_typebound_call (gfc_symtree* varst)
4537 gfc_expr* base;
4538 match m;
4540 base = gfc_get_expr ();
4541 base->expr_type = EXPR_VARIABLE;
4542 base->symtree = varst;
4543 base->where = gfc_current_locus;
4544 gfc_set_sym_referenced (varst->n.sym);
4546 m = gfc_match_varspec (base, 0, true, true);
4547 if (m == MATCH_NO)
4548 gfc_error ("Expected component reference at %C");
4549 if (m != MATCH_YES)
4551 gfc_free_expr (base);
4552 return MATCH_ERROR;
4555 if (gfc_match_eos () != MATCH_YES)
4557 gfc_error ("Junk after CALL at %C");
4558 gfc_free_expr (base);
4559 return MATCH_ERROR;
4562 if (base->expr_type == EXPR_COMPCALL)
4563 new_st.op = EXEC_COMPCALL;
4564 else if (base->expr_type == EXPR_PPC)
4565 new_st.op = EXEC_CALL_PPC;
4566 else
4568 gfc_error ("Expected type-bound procedure or procedure pointer component "
4569 "at %C");
4570 gfc_free_expr (base);
4571 return MATCH_ERROR;
4573 new_st.expr1 = base;
4575 return MATCH_YES;
4579 /* Match a CALL statement. The tricky part here are possible
4580 alternate return specifiers. We handle these by having all
4581 "subroutines" actually return an integer via a register that gives
4582 the return number. If the call specifies alternate returns, we
4583 generate code for a SELECT statement whose case clauses contain
4584 GOTOs to the various labels. */
4586 match
4587 gfc_match_call (void)
4589 char name[GFC_MAX_SYMBOL_LEN + 1];
4590 gfc_actual_arglist *a, *arglist;
4591 gfc_case *new_case;
4592 gfc_symbol *sym;
4593 gfc_symtree *st;
4594 gfc_code *c;
4595 match m;
4596 int i;
4598 arglist = NULL;
4600 m = gfc_match ("% %n", name);
4601 if (m == MATCH_NO)
4602 goto syntax;
4603 if (m != MATCH_YES)
4604 return m;
4606 if (gfc_get_ha_sym_tree (name, &st))
4607 return MATCH_ERROR;
4609 sym = st->n.sym;
4611 /* If this is a variable of derived-type, it probably starts a type-bound
4612 procedure call. */
4613 if ((sym->attr.flavor != FL_PROCEDURE
4614 || gfc_is_function_return_value (sym, gfc_current_ns))
4615 && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS))
4616 return match_typebound_call (st);
4618 /* If it does not seem to be callable (include functions so that the
4619 right association is made. They are thrown out in resolution.)
4620 ... */
4621 if (!sym->attr.generic
4622 && !sym->attr.subroutine
4623 && !sym->attr.function)
4625 if (!(sym->attr.external && !sym->attr.referenced))
4627 /* ...create a symbol in this scope... */
4628 if (sym->ns != gfc_current_ns
4629 && gfc_get_sym_tree (name, NULL, &st, false) == 1)
4630 return MATCH_ERROR;
4632 if (sym != st->n.sym)
4633 sym = st->n.sym;
4636 /* ...and then to try to make the symbol into a subroutine. */
4637 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
4638 return MATCH_ERROR;
4641 gfc_set_sym_referenced (sym);
4643 if (gfc_match_eos () != MATCH_YES)
4645 m = gfc_match_actual_arglist (1, &arglist);
4646 if (m == MATCH_NO)
4647 goto syntax;
4648 if (m == MATCH_ERROR)
4649 goto cleanup;
4651 if (gfc_match_eos () != MATCH_YES)
4652 goto syntax;
4655 /* If any alternate return labels were found, construct a SELECT
4656 statement that will jump to the right place. */
4658 i = 0;
4659 for (a = arglist; a; a = a->next)
4660 if (a->expr == NULL)
4662 i = 1;
4663 break;
4666 if (i)
4668 gfc_symtree *select_st;
4669 gfc_symbol *select_sym;
4670 char name[GFC_MAX_SYMBOL_LEN + 1];
4672 new_st.next = c = gfc_get_code (EXEC_SELECT);
4673 sprintf (name, "_result_%s", sym->name);
4674 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
4676 select_sym = select_st->n.sym;
4677 select_sym->ts.type = BT_INTEGER;
4678 select_sym->ts.kind = gfc_default_integer_kind;
4679 gfc_set_sym_referenced (select_sym);
4680 c->expr1 = gfc_get_expr ();
4681 c->expr1->expr_type = EXPR_VARIABLE;
4682 c->expr1->symtree = select_st;
4683 c->expr1->ts = select_sym->ts;
4684 c->expr1->where = gfc_current_locus;
4686 i = 0;
4687 for (a = arglist; a; a = a->next)
4689 if (a->expr != NULL)
4690 continue;
4692 if (!gfc_reference_st_label (a->label, ST_LABEL_TARGET))
4693 continue;
4695 i++;
4697 c->block = gfc_get_code (EXEC_SELECT);
4698 c = c->block;
4700 new_case = gfc_get_case ();
4701 new_case->high = gfc_get_int_expr (gfc_default_integer_kind, NULL, i);
4702 new_case->low = new_case->high;
4703 c->ext.block.case_list = new_case;
4705 c->next = gfc_get_code (EXEC_GOTO);
4706 c->next->label1 = a->label;
4710 new_st.op = EXEC_CALL;
4711 new_st.symtree = st;
4712 new_st.ext.actual = arglist;
4714 return MATCH_YES;
4716 syntax:
4717 gfc_syntax_error (ST_CALL);
4719 cleanup:
4720 gfc_free_actual_arglist (arglist);
4721 return MATCH_ERROR;
4725 /* Given a name, return a pointer to the common head structure,
4726 creating it if it does not exist. If FROM_MODULE is nonzero, we
4727 mangle the name so that it doesn't interfere with commons defined
4728 in the using namespace.
4729 TODO: Add to global symbol tree. */
4731 gfc_common_head *
4732 gfc_get_common (const char *name, int from_module)
4734 gfc_symtree *st;
4735 static int serial = 0;
4736 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
4738 if (from_module)
4740 /* A use associated common block is only needed to correctly layout
4741 the variables it contains. */
4742 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
4743 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
4745 else
4747 st = gfc_find_symtree (gfc_current_ns->common_root, name);
4749 if (st == NULL)
4750 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
4753 if (st->n.common == NULL)
4755 st->n.common = gfc_get_common_head ();
4756 st->n.common->where = gfc_current_locus;
4757 strcpy (st->n.common->name, name);
4760 return st->n.common;
4764 /* Match a common block name. */
4766 match match_common_name (char *name)
4768 match m;
4770 if (gfc_match_char ('/') == MATCH_NO)
4772 name[0] = '\0';
4773 return MATCH_YES;
4776 if (gfc_match_char ('/') == MATCH_YES)
4778 name[0] = '\0';
4779 return MATCH_YES;
4782 m = gfc_match_name (name);
4784 if (m == MATCH_ERROR)
4785 return MATCH_ERROR;
4786 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
4787 return MATCH_YES;
4789 gfc_error ("Syntax error in common block name at %C");
4790 return MATCH_ERROR;
4794 /* Match a COMMON statement. */
4796 match
4797 gfc_match_common (void)
4799 gfc_symbol *sym, **head, *tail, *other;
4800 char name[GFC_MAX_SYMBOL_LEN + 1];
4801 gfc_common_head *t;
4802 gfc_array_spec *as;
4803 gfc_equiv *e1, *e2;
4804 match m;
4806 as = NULL;
4808 for (;;)
4810 m = match_common_name (name);
4811 if (m == MATCH_ERROR)
4812 goto cleanup;
4814 if (name[0] == '\0')
4816 t = &gfc_current_ns->blank_common;
4817 if (t->head == NULL)
4818 t->where = gfc_current_locus;
4820 else
4822 t = gfc_get_common (name, 0);
4824 head = &t->head;
4826 if (*head == NULL)
4827 tail = NULL;
4828 else
4830 tail = *head;
4831 while (tail->common_next)
4832 tail = tail->common_next;
4835 /* Grab the list of symbols. */
4836 for (;;)
4838 m = gfc_match_symbol (&sym, 0);
4839 if (m == MATCH_ERROR)
4840 goto cleanup;
4841 if (m == MATCH_NO)
4842 goto syntax;
4844 /* See if we know the current common block is bind(c), and if
4845 so, then see if we can check if the symbol is (which it'll
4846 need to be). This can happen if the bind(c) attr stmt was
4847 applied to the common block, and the variable(s) already
4848 defined, before declaring the common block. */
4849 if (t->is_bind_c == 1)
4851 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
4853 /* If we find an error, just print it and continue,
4854 cause it's just semantic, and we can see if there
4855 are more errors. */
4856 gfc_error_now ("Variable %qs at %L in common block %qs "
4857 "at %C must be declared with a C "
4858 "interoperable kind since common block "
4859 "%qs is bind(c)",
4860 sym->name, &(sym->declared_at), t->name,
4861 t->name);
4864 if (sym->attr.is_bind_c == 1)
4865 gfc_error_now ("Variable %qs in common block %qs at %C can not "
4866 "be bind(c) since it is not global", sym->name,
4867 t->name);
4870 if (sym->attr.in_common)
4872 gfc_error ("Symbol %qs at %C is already in a COMMON block",
4873 sym->name);
4874 goto cleanup;
4877 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
4878 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
4880 if (!gfc_notify_std (GFC_STD_GNU, "Initialized symbol %qs at "
4881 "%C can only be COMMON in BLOCK DATA",
4882 sym->name))
4883 goto cleanup;
4886 /* Deal with an optional array specification after the
4887 symbol name. */
4888 m = gfc_match_array_spec (&as, true, true);
4889 if (m == MATCH_ERROR)
4890 goto cleanup;
4892 if (m == MATCH_YES)
4894 if (as->type != AS_EXPLICIT)
4896 gfc_error ("Array specification for symbol %qs in COMMON "
4897 "at %C must be explicit", sym->name);
4898 goto cleanup;
4901 if (!gfc_add_dimension (&sym->attr, sym->name, NULL))
4902 goto cleanup;
4904 if (sym->attr.pointer)
4906 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
4907 "POINTER array", sym->name);
4908 goto cleanup;
4911 sym->as = as;
4912 as = NULL;
4916 /* Add the in_common attribute, but ignore the reported errors
4917 if any, and continue matching. */
4918 gfc_add_in_common (&sym->attr, sym->name, NULL);
4920 sym->common_block = t;
4921 sym->common_block->refs++;
4923 if (tail != NULL)
4924 tail->common_next = sym;
4925 else
4926 *head = sym;
4928 tail = sym;
4930 sym->common_head = t;
4932 /* Check to see if the symbol is already in an equivalence group.
4933 If it is, set the other members as being in common. */
4934 if (sym->attr.in_equivalence)
4936 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
4938 for (e2 = e1; e2; e2 = e2->eq)
4939 if (e2->expr->symtree->n.sym == sym)
4940 goto equiv_found;
4942 continue;
4944 equiv_found:
4946 for (e2 = e1; e2; e2 = e2->eq)
4948 other = e2->expr->symtree->n.sym;
4949 if (other->common_head
4950 && other->common_head != sym->common_head)
4952 gfc_error ("Symbol %qs, in COMMON block %qs at "
4953 "%C is being indirectly equivalenced to "
4954 "another COMMON block %qs",
4955 sym->name, sym->common_head->name,
4956 other->common_head->name);
4957 goto cleanup;
4959 other->attr.in_common = 1;
4960 other->common_head = t;
4966 gfc_gobble_whitespace ();
4967 if (gfc_match_eos () == MATCH_YES)
4968 goto done;
4969 if (gfc_peek_ascii_char () == '/')
4970 break;
4971 if (gfc_match_char (',') != MATCH_YES)
4972 goto syntax;
4973 gfc_gobble_whitespace ();
4974 if (gfc_peek_ascii_char () == '/')
4975 break;
4979 done:
4980 return MATCH_YES;
4982 syntax:
4983 gfc_syntax_error (ST_COMMON);
4985 cleanup:
4986 gfc_free_array_spec (as);
4987 return MATCH_ERROR;
4991 /* Match a BLOCK DATA program unit. */
4993 match
4994 gfc_match_block_data (void)
4996 char name[GFC_MAX_SYMBOL_LEN + 1];
4997 gfc_symbol *sym;
4998 match m;
5000 if (gfc_match_eos () == MATCH_YES)
5002 gfc_new_block = NULL;
5003 return MATCH_YES;
5006 m = gfc_match ("% %n%t", name);
5007 if (m != MATCH_YES)
5008 return MATCH_ERROR;
5010 if (gfc_get_symbol (name, NULL, &sym))
5011 return MATCH_ERROR;
5013 if (!gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL))
5014 return MATCH_ERROR;
5016 gfc_new_block = sym;
5018 return MATCH_YES;
5022 /* Free a namelist structure. */
5024 void
5025 gfc_free_namelist (gfc_namelist *name)
5027 gfc_namelist *n;
5029 for (; name; name = n)
5031 n = name->next;
5032 free (name);
5037 /* Free an OpenMP namelist structure. */
5039 void
5040 gfc_free_omp_namelist (gfc_omp_namelist *name)
5042 gfc_omp_namelist *n;
5044 for (; name; name = n)
5046 gfc_free_expr (name->expr);
5047 if (name->udr)
5049 if (name->udr->combiner)
5050 gfc_free_statement (name->udr->combiner);
5051 if (name->udr->initializer)
5052 gfc_free_statement (name->udr->initializer);
5053 free (name->udr);
5055 n = name->next;
5056 free (name);
5061 /* Match a NAMELIST statement. */
5063 match
5064 gfc_match_namelist (void)
5066 gfc_symbol *group_name, *sym;
5067 gfc_namelist *nl;
5068 match m, m2;
5070 m = gfc_match (" / %s /", &group_name);
5071 if (m == MATCH_NO)
5072 goto syntax;
5073 if (m == MATCH_ERROR)
5074 goto error;
5076 for (;;)
5078 if (group_name->ts.type != BT_UNKNOWN)
5080 gfc_error ("Namelist group name %qs at %C already has a basic "
5081 "type of %s", group_name->name,
5082 gfc_typename (&group_name->ts));
5083 return MATCH_ERROR;
5086 if (group_name->attr.flavor == FL_NAMELIST
5087 && group_name->attr.use_assoc
5088 && !gfc_notify_std (GFC_STD_GNU, "Namelist group name %qs "
5089 "at %C already is USE associated and can"
5090 "not be respecified.", group_name->name))
5091 return MATCH_ERROR;
5093 if (group_name->attr.flavor != FL_NAMELIST
5094 && !gfc_add_flavor (&group_name->attr, FL_NAMELIST,
5095 group_name->name, NULL))
5096 return MATCH_ERROR;
5098 for (;;)
5100 m = gfc_match_symbol (&sym, 1);
5101 if (m == MATCH_NO)
5102 goto syntax;
5103 if (m == MATCH_ERROR)
5104 goto error;
5106 if (sym->attr.in_namelist == 0
5107 && !gfc_add_in_namelist (&sym->attr, sym->name, NULL))
5108 goto error;
5110 /* Use gfc_error_check here, rather than goto error, so that
5111 these are the only errors for the next two lines. */
5112 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
5114 gfc_error ("Assumed size array %qs in namelist %qs at "
5115 "%C is not allowed", sym->name, group_name->name);
5116 gfc_error_check ();
5119 nl = gfc_get_namelist ();
5120 nl->sym = sym;
5121 sym->refs++;
5123 if (group_name->namelist == NULL)
5124 group_name->namelist = group_name->namelist_tail = nl;
5125 else
5127 group_name->namelist_tail->next = nl;
5128 group_name->namelist_tail = nl;
5131 if (gfc_match_eos () == MATCH_YES)
5132 goto done;
5134 m = gfc_match_char (',');
5136 if (gfc_match_char ('/') == MATCH_YES)
5138 m2 = gfc_match (" %s /", &group_name);
5139 if (m2 == MATCH_YES)
5140 break;
5141 if (m2 == MATCH_ERROR)
5142 goto error;
5143 goto syntax;
5146 if (m != MATCH_YES)
5147 goto syntax;
5151 done:
5152 return MATCH_YES;
5154 syntax:
5155 gfc_syntax_error (ST_NAMELIST);
5157 error:
5158 return MATCH_ERROR;
5162 /* Match a MODULE statement. */
5164 match
5165 gfc_match_module (void)
5167 match m;
5169 m = gfc_match (" %s%t", &gfc_new_block);
5170 if (m != MATCH_YES)
5171 return m;
5173 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
5174 gfc_new_block->name, NULL))
5175 return MATCH_ERROR;
5177 return MATCH_YES;
5181 /* Free equivalence sets and lists. Recursively is the easiest way to
5182 do this. */
5184 void
5185 gfc_free_equiv_until (gfc_equiv *eq, gfc_equiv *stop)
5187 if (eq == stop)
5188 return;
5190 gfc_free_equiv (eq->eq);
5191 gfc_free_equiv_until (eq->next, stop);
5192 gfc_free_expr (eq->expr);
5193 free (eq);
5197 void
5198 gfc_free_equiv (gfc_equiv *eq)
5200 gfc_free_equiv_until (eq, NULL);
5204 /* Match an EQUIVALENCE statement. */
5206 match
5207 gfc_match_equivalence (void)
5209 gfc_equiv *eq, *set, *tail;
5210 gfc_ref *ref;
5211 gfc_symbol *sym;
5212 match m;
5213 gfc_common_head *common_head = NULL;
5214 bool common_flag;
5215 int cnt;
5217 tail = NULL;
5219 for (;;)
5221 eq = gfc_get_equiv ();
5222 if (tail == NULL)
5223 tail = eq;
5225 eq->next = gfc_current_ns->equiv;
5226 gfc_current_ns->equiv = eq;
5228 if (gfc_match_char ('(') != MATCH_YES)
5229 goto syntax;
5231 set = eq;
5232 common_flag = FALSE;
5233 cnt = 0;
5235 for (;;)
5237 m = gfc_match_equiv_variable (&set->expr);
5238 if (m == MATCH_ERROR)
5239 goto cleanup;
5240 if (m == MATCH_NO)
5241 goto syntax;
5243 /* count the number of objects. */
5244 cnt++;
5246 if (gfc_match_char ('%') == MATCH_YES)
5248 gfc_error ("Derived type component %C is not a "
5249 "permitted EQUIVALENCE member");
5250 goto cleanup;
5253 for (ref = set->expr->ref; ref; ref = ref->next)
5254 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
5256 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
5257 "be an array section");
5258 goto cleanup;
5261 sym = set->expr->symtree->n.sym;
5263 if (!gfc_add_in_equivalence (&sym->attr, sym->name, NULL))
5264 goto cleanup;
5266 if (sym->attr.in_common)
5268 common_flag = TRUE;
5269 common_head = sym->common_head;
5272 if (gfc_match_char (')') == MATCH_YES)
5273 break;
5275 if (gfc_match_char (',') != MATCH_YES)
5276 goto syntax;
5278 set->eq = gfc_get_equiv ();
5279 set = set->eq;
5282 if (cnt < 2)
5284 gfc_error ("EQUIVALENCE at %C requires two or more objects");
5285 goto cleanup;
5288 /* If one of the members of an equivalence is in common, then
5289 mark them all as being in common. Before doing this, check
5290 that members of the equivalence group are not in different
5291 common blocks. */
5292 if (common_flag)
5293 for (set = eq; set; set = set->eq)
5295 sym = set->expr->symtree->n.sym;
5296 if (sym->common_head && sym->common_head != common_head)
5298 gfc_error ("Attempt to indirectly overlap COMMON "
5299 "blocks %s and %s by EQUIVALENCE at %C",
5300 sym->common_head->name, common_head->name);
5301 goto cleanup;
5303 sym->attr.in_common = 1;
5304 sym->common_head = common_head;
5307 if (gfc_match_eos () == MATCH_YES)
5308 break;
5309 if (gfc_match_char (',') != MATCH_YES)
5311 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
5312 goto cleanup;
5316 return MATCH_YES;
5318 syntax:
5319 gfc_syntax_error (ST_EQUIVALENCE);
5321 cleanup:
5322 eq = tail->next;
5323 tail->next = NULL;
5325 gfc_free_equiv (gfc_current_ns->equiv);
5326 gfc_current_ns->equiv = eq;
5328 return MATCH_ERROR;
5332 /* Check that a statement function is not recursive. This is done by looking
5333 for the statement function symbol(sym) by looking recursively through its
5334 expression(e). If a reference to sym is found, true is returned.
5335 12.5.4 requires that any variable of function that is implicitly typed
5336 shall have that type confirmed by any subsequent type declaration. The
5337 implicit typing is conveniently done here. */
5338 static bool
5339 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
5341 static bool
5342 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5345 if (e == NULL)
5346 return false;
5348 switch (e->expr_type)
5350 case EXPR_FUNCTION:
5351 if (e->symtree == NULL)
5352 return false;
5354 /* Check the name before testing for nested recursion! */
5355 if (sym->name == e->symtree->n.sym->name)
5356 return true;
5358 /* Catch recursion via other statement functions. */
5359 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
5360 && e->symtree->n.sym->value
5361 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
5362 return true;
5364 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5365 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5367 break;
5369 case EXPR_VARIABLE:
5370 if (e->symtree && sym->name == e->symtree->n.sym->name)
5371 return true;
5373 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5374 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5375 break;
5377 default:
5378 break;
5381 return false;
5385 static bool
5386 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
5388 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
5392 /* Match a statement function declaration. It is so easy to match
5393 non-statement function statements with a MATCH_ERROR as opposed to
5394 MATCH_NO that we suppress error message in most cases. */
5396 match
5397 gfc_match_st_function (void)
5399 gfc_error_buffer old_error;
5400 gfc_symbol *sym;
5401 gfc_expr *expr;
5402 match m;
5404 m = gfc_match_symbol (&sym, 0);
5405 if (m != MATCH_YES)
5406 return m;
5408 gfc_push_error (&old_error);
5410 if (!gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION, sym->name, NULL))
5411 goto undo_error;
5413 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
5414 goto undo_error;
5416 m = gfc_match (" = %e%t", &expr);
5417 if (m == MATCH_NO)
5418 goto undo_error;
5420 gfc_free_error (&old_error);
5422 if (m == MATCH_ERROR)
5423 return m;
5425 if (recursive_stmt_fcn (expr, sym))
5427 gfc_error ("Statement function at %L is recursive", &expr->where);
5428 return MATCH_ERROR;
5431 sym->value = expr;
5433 if ((gfc_current_state () == COMP_FUNCTION
5434 || gfc_current_state () == COMP_SUBROUTINE)
5435 && gfc_state_stack->previous->state == COMP_INTERFACE)
5437 gfc_error ("Statement function at %L cannot appear within an INTERFACE",
5438 &expr->where);
5439 return MATCH_ERROR;
5442 if (!gfc_notify_std (GFC_STD_F95_OBS, "Statement function at %C"))
5443 return MATCH_ERROR;
5445 return MATCH_YES;
5447 undo_error:
5448 gfc_pop_error (&old_error);
5449 return MATCH_NO;
5453 /* Match an assignment to a pointer function (F2008). This could, in
5454 general be ambiguous with a statement function. In this implementation
5455 it remains so if it is the first statement after the specification
5456 block. */
5458 match
5459 gfc_match_ptr_fcn_assign (void)
5461 gfc_error_buffer old_error;
5462 locus old_loc;
5463 gfc_symbol *sym;
5464 gfc_expr *expr;
5465 match m;
5466 char name[GFC_MAX_SYMBOL_LEN + 1];
5468 old_loc = gfc_current_locus;
5469 m = gfc_match_name (name);
5470 if (m != MATCH_YES)
5471 return m;
5473 gfc_find_symbol (name, NULL, 1, &sym);
5474 if (sym && sym->attr.flavor != FL_PROCEDURE)
5475 return MATCH_NO;
5477 gfc_push_error (&old_error);
5479 if (sym && sym->attr.function)
5480 goto match_actual_arglist;
5482 gfc_current_locus = old_loc;
5483 m = gfc_match_symbol (&sym, 0);
5484 if (m != MATCH_YES)
5485 return m;
5487 if (!gfc_add_procedure (&sym->attr, PROC_UNKNOWN, sym->name, NULL))
5488 goto undo_error;
5490 match_actual_arglist:
5491 gfc_current_locus = old_loc;
5492 m = gfc_match (" %e", &expr);
5493 if (m != MATCH_YES)
5494 goto undo_error;
5496 new_st.op = EXEC_ASSIGN;
5497 new_st.expr1 = expr;
5498 expr = NULL;
5500 m = gfc_match (" = %e%t", &expr);
5501 if (m != MATCH_YES)
5502 goto undo_error;
5504 new_st.expr2 = expr;
5505 return MATCH_YES;
5507 undo_error:
5508 gfc_pop_error (&old_error);
5509 return MATCH_NO;
5513 /***************** SELECT CASE subroutines ******************/
5515 /* Free a single case structure. */
5517 static void
5518 free_case (gfc_case *p)
5520 if (p->low == p->high)
5521 p->high = NULL;
5522 gfc_free_expr (p->low);
5523 gfc_free_expr (p->high);
5524 free (p);
5528 /* Free a list of case structures. */
5530 void
5531 gfc_free_case_list (gfc_case *p)
5533 gfc_case *q;
5535 for (; p; p = q)
5537 q = p->next;
5538 free_case (p);
5543 /* Match a single case selector. Combining the requirements of F08:C830
5544 and F08:C832 (R838) means that the case-value must have either CHARACTER,
5545 INTEGER, or LOGICAL type. */
5547 static match
5548 match_case_selector (gfc_case **cp)
5550 gfc_case *c;
5551 match m;
5553 c = gfc_get_case ();
5554 c->where = gfc_current_locus;
5556 if (gfc_match_char (':') == MATCH_YES)
5558 m = gfc_match_init_expr (&c->high);
5559 if (m == MATCH_NO)
5560 goto need_expr;
5561 if (m == MATCH_ERROR)
5562 goto cleanup;
5564 if (c->high->ts.type != BT_LOGICAL && c->high->ts.type != BT_INTEGER
5565 && c->high->ts.type != BT_CHARACTER)
5567 gfc_error ("Expression in CASE selector at %L cannot be %s",
5568 &c->high->where, gfc_typename (&c->high->ts));
5569 goto cleanup;
5572 else
5574 m = gfc_match_init_expr (&c->low);
5575 if (m == MATCH_ERROR)
5576 goto cleanup;
5577 if (m == MATCH_NO)
5578 goto need_expr;
5580 if (c->low->ts.type != BT_LOGICAL && c->low->ts.type != BT_INTEGER
5581 && c->low->ts.type != BT_CHARACTER)
5583 gfc_error ("Expression in CASE selector at %L cannot be %s",
5584 &c->low->where, gfc_typename (&c->low->ts));
5585 goto cleanup;
5588 /* If we're not looking at a ':' now, make a range out of a single
5589 target. Else get the upper bound for the case range. */
5590 if (gfc_match_char (':') != MATCH_YES)
5591 c->high = c->low;
5592 else
5594 m = gfc_match_init_expr (&c->high);
5595 if (m == MATCH_ERROR)
5596 goto cleanup;
5597 /* MATCH_NO is fine. It's OK if nothing is there! */
5601 *cp = c;
5602 return MATCH_YES;
5604 need_expr:
5605 gfc_error ("Expected initialization expression in CASE at %C");
5607 cleanup:
5608 free_case (c);
5609 return MATCH_ERROR;
5613 /* Match the end of a case statement. */
5615 static match
5616 match_case_eos (void)
5618 char name[GFC_MAX_SYMBOL_LEN + 1];
5619 match m;
5621 if (gfc_match_eos () == MATCH_YES)
5622 return MATCH_YES;
5624 /* If the case construct doesn't have a case-construct-name, we
5625 should have matched the EOS. */
5626 if (!gfc_current_block ())
5627 return MATCH_NO;
5629 gfc_gobble_whitespace ();
5631 m = gfc_match_name (name);
5632 if (m != MATCH_YES)
5633 return m;
5635 if (strcmp (name, gfc_current_block ()->name) != 0)
5637 gfc_error ("Expected block name %qs of SELECT construct at %C",
5638 gfc_current_block ()->name);
5639 return MATCH_ERROR;
5642 return gfc_match_eos ();
5646 /* Match a SELECT statement. */
5648 match
5649 gfc_match_select (void)
5651 gfc_expr *expr;
5652 match m;
5654 m = gfc_match_label ();
5655 if (m == MATCH_ERROR)
5656 return m;
5658 m = gfc_match (" select case ( %e )%t", &expr);
5659 if (m != MATCH_YES)
5660 return m;
5662 new_st.op = EXEC_SELECT;
5663 new_st.expr1 = expr;
5665 return MATCH_YES;
5669 /* Transfer the selector typespec to the associate name. */
5671 static void
5672 copy_ts_from_selector_to_associate (gfc_expr *associate, gfc_expr *selector)
5674 gfc_ref *ref;
5675 gfc_symbol *assoc_sym;
5677 assoc_sym = associate->symtree->n.sym;
5679 /* At this stage the expression rank and arrayspec dimensions have
5680 not been completely sorted out. We must get the expr2->rank
5681 right here, so that the correct class container is obtained. */
5682 ref = selector->ref;
5683 while (ref && ref->next)
5684 ref = ref->next;
5686 if (selector->ts.type == BT_CLASS && CLASS_DATA (selector)->as
5687 && ref && ref->type == REF_ARRAY)
5689 /* Ensure that the array reference type is set. We cannot use
5690 gfc_resolve_expr at this point, so the usable parts of
5691 resolve.c(resolve_array_ref) are employed to do it. */
5692 if (ref->u.ar.type == AR_UNKNOWN)
5694 ref->u.ar.type = AR_ELEMENT;
5695 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
5696 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
5697 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR
5698 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
5699 && ref->u.ar.start[i] && ref->u.ar.start[i]->rank))
5701 ref->u.ar.type = AR_SECTION;
5702 break;
5706 if (ref->u.ar.type == AR_FULL)
5707 selector->rank = CLASS_DATA (selector)->as->rank;
5708 else if (ref->u.ar.type == AR_SECTION)
5709 selector->rank = ref->u.ar.dimen;
5710 else
5711 selector->rank = 0;
5714 if (selector->rank)
5716 assoc_sym->attr.dimension = 1;
5717 assoc_sym->as = gfc_get_array_spec ();
5718 assoc_sym->as->rank = selector->rank;
5719 assoc_sym->as->type = AS_DEFERRED;
5721 else
5722 assoc_sym->as = NULL;
5724 if (selector->ts.type == BT_CLASS)
5726 /* The correct class container has to be available. */
5727 assoc_sym->ts.type = BT_CLASS;
5728 assoc_sym->ts.u.derived = CLASS_DATA (selector)->ts.u.derived;
5729 assoc_sym->attr.pointer = 1;
5730 gfc_build_class_symbol (&assoc_sym->ts, &assoc_sym->attr, &assoc_sym->as);
5735 /* Push the current selector onto the SELECT TYPE stack. */
5737 static void
5738 select_type_push (gfc_symbol *sel)
5740 gfc_select_type_stack *top = gfc_get_select_type_stack ();
5741 top->selector = sel;
5742 top->tmp = NULL;
5743 top->prev = select_type_stack;
5745 select_type_stack = top;
5749 /* Set the temporary for the current intrinsic SELECT TYPE selector. */
5751 static gfc_symtree *
5752 select_intrinsic_set_tmp (gfc_typespec *ts)
5754 char name[GFC_MAX_SYMBOL_LEN];
5755 gfc_symtree *tmp;
5756 int charlen = 0;
5758 if (ts->type == BT_CLASS || ts->type == BT_DERIVED)
5759 return NULL;
5761 if (select_type_stack->selector->ts.type == BT_CLASS
5762 && !select_type_stack->selector->attr.class_ok)
5763 return NULL;
5765 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length
5766 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
5767 charlen = mpz_get_si (ts->u.cl->length->value.integer);
5769 if (ts->type != BT_CHARACTER)
5770 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (ts->type),
5771 ts->kind);
5772 else
5773 sprintf (name, "__tmp_%s_%d_%d", gfc_basic_typename (ts->type),
5774 charlen, ts->kind);
5776 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
5777 gfc_add_type (tmp->n.sym, ts, NULL);
5779 /* Copy across the array spec to the selector. */
5780 if (select_type_stack->selector->ts.type == BT_CLASS
5781 && (CLASS_DATA (select_type_stack->selector)->attr.dimension
5782 || CLASS_DATA (select_type_stack->selector)->attr.codimension))
5784 tmp->n.sym->attr.pointer = 1;
5785 tmp->n.sym->attr.dimension
5786 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
5787 tmp->n.sym->attr.codimension
5788 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
5789 tmp->n.sym->as
5790 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
5793 gfc_set_sym_referenced (tmp->n.sym);
5794 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
5795 tmp->n.sym->attr.select_type_temporary = 1;
5797 return tmp;
5801 /* Set up a temporary for the current TYPE IS / CLASS IS branch . */
5803 static void
5804 select_type_set_tmp (gfc_typespec *ts)
5806 char name[GFC_MAX_SYMBOL_LEN];
5807 gfc_symtree *tmp = NULL;
5809 if (!ts)
5811 select_type_stack->tmp = NULL;
5812 return;
5815 tmp = select_intrinsic_set_tmp (ts);
5817 if (tmp == NULL)
5819 if (!ts->u.derived)
5820 return;
5822 if (ts->type == BT_CLASS)
5823 sprintf (name, "__tmp_class_%s", ts->u.derived->name);
5824 else
5825 sprintf (name, "__tmp_type_%s", ts->u.derived->name);
5826 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
5827 gfc_add_type (tmp->n.sym, ts, NULL);
5829 if (select_type_stack->selector->ts.type == BT_CLASS
5830 && select_type_stack->selector->attr.class_ok)
5832 tmp->n.sym->attr.pointer
5833 = CLASS_DATA (select_type_stack->selector)->attr.class_pointer;
5835 /* Copy across the array spec to the selector. */
5836 if (CLASS_DATA (select_type_stack->selector)->attr.dimension
5837 || CLASS_DATA (select_type_stack->selector)->attr.codimension)
5839 tmp->n.sym->attr.dimension
5840 = CLASS_DATA (select_type_stack->selector)->attr.dimension;
5841 tmp->n.sym->attr.codimension
5842 = CLASS_DATA (select_type_stack->selector)->attr.codimension;
5843 tmp->n.sym->as
5844 = gfc_copy_array_spec (CLASS_DATA (select_type_stack->selector)->as);
5848 gfc_set_sym_referenced (tmp->n.sym);
5849 gfc_add_flavor (&tmp->n.sym->attr, FL_VARIABLE, name, NULL);
5850 tmp->n.sym->attr.select_type_temporary = 1;
5852 if (ts->type == BT_CLASS)
5853 gfc_build_class_symbol (&tmp->n.sym->ts, &tmp->n.sym->attr,
5854 &tmp->n.sym->as);
5857 /* Add an association for it, so the rest of the parser knows it is
5858 an associate-name. The target will be set during resolution. */
5859 tmp->n.sym->assoc = gfc_get_association_list ();
5860 tmp->n.sym->assoc->dangling = 1;
5861 tmp->n.sym->assoc->st = tmp;
5863 select_type_stack->tmp = tmp;
5867 /* Match a SELECT TYPE statement. */
5869 match
5870 gfc_match_select_type (void)
5872 gfc_expr *expr1, *expr2 = NULL;
5873 match m;
5874 char name[GFC_MAX_SYMBOL_LEN];
5875 bool class_array;
5876 gfc_symbol *sym;
5877 gfc_namespace *ns = gfc_current_ns;
5879 m = gfc_match_label ();
5880 if (m == MATCH_ERROR)
5881 return m;
5883 m = gfc_match (" select type ( ");
5884 if (m != MATCH_YES)
5885 return m;
5887 gfc_current_ns = gfc_build_block_ns (ns);
5888 m = gfc_match (" %n => %e", name, &expr2);
5889 if (m == MATCH_YES)
5891 expr1 = gfc_get_expr ();
5892 expr1->expr_type = EXPR_VARIABLE;
5893 expr1->where = expr2->where;
5894 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
5896 m = MATCH_ERROR;
5897 goto cleanup;
5900 sym = expr1->symtree->n.sym;
5901 if (expr2->ts.type == BT_UNKNOWN)
5902 sym->attr.untyped = 1;
5903 else
5904 copy_ts_from_selector_to_associate (expr1, expr2);
5906 sym->attr.flavor = FL_VARIABLE;
5907 sym->attr.referenced = 1;
5908 sym->attr.class_ok = 1;
5910 else
5912 m = gfc_match (" %e ", &expr1);
5913 if (m != MATCH_YES)
5915 std::swap (ns, gfc_current_ns);
5916 gfc_free_namespace (ns);
5917 return m;
5921 m = gfc_match (" )%t");
5922 if (m != MATCH_YES)
5924 gfc_error ("parse error in SELECT TYPE statement at %C");
5925 goto cleanup;
5928 /* This ghastly expression seems to be needed to distinguish a CLASS
5929 array, which can have a reference, from other expressions that
5930 have references, such as derived type components, and are not
5931 allowed by the standard.
5932 TODO: see if it is sufficient to exclude component and substring
5933 references. */
5934 class_array = (expr1->expr_type == EXPR_VARIABLE
5935 && expr1->ts.type == BT_CLASS
5936 && CLASS_DATA (expr1)
5937 && (strcmp (CLASS_DATA (expr1)->name, "_data") == 0)
5938 && (CLASS_DATA (expr1)->attr.dimension
5939 || CLASS_DATA (expr1)->attr.codimension)
5940 && expr1->ref
5941 && expr1->ref->type == REF_ARRAY
5942 && expr1->ref->next == NULL);
5944 /* Check for F03:C811. */
5945 if (!expr2 && (expr1->expr_type != EXPR_VARIABLE
5946 || (!class_array && expr1->ref != NULL)))
5948 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
5949 "use associate-name=>");
5950 m = MATCH_ERROR;
5951 goto cleanup;
5954 new_st.op = EXEC_SELECT_TYPE;
5955 new_st.expr1 = expr1;
5956 new_st.expr2 = expr2;
5957 new_st.ext.block.ns = gfc_current_ns;
5959 select_type_push (expr1->symtree->n.sym);
5960 gfc_current_ns = ns;
5962 return MATCH_YES;
5964 cleanup:
5965 gfc_free_expr (expr1);
5966 gfc_free_expr (expr2);
5967 gfc_undo_symbols ();
5968 std::swap (ns, gfc_current_ns);
5969 gfc_free_namespace (ns);
5970 return m;
5974 /* Match a CASE statement. */
5976 match
5977 gfc_match_case (void)
5979 gfc_case *c, *head, *tail;
5980 match m;
5982 head = tail = NULL;
5984 if (gfc_current_state () != COMP_SELECT)
5986 gfc_error ("Unexpected CASE statement at %C");
5987 return MATCH_ERROR;
5990 if (gfc_match ("% default") == MATCH_YES)
5992 m = match_case_eos ();
5993 if (m == MATCH_NO)
5994 goto syntax;
5995 if (m == MATCH_ERROR)
5996 goto cleanup;
5998 new_st.op = EXEC_SELECT;
5999 c = gfc_get_case ();
6000 c->where = gfc_current_locus;
6001 new_st.ext.block.case_list = c;
6002 return MATCH_YES;
6005 if (gfc_match_char ('(') != MATCH_YES)
6006 goto syntax;
6008 for (;;)
6010 if (match_case_selector (&c) == MATCH_ERROR)
6011 goto cleanup;
6013 if (head == NULL)
6014 head = c;
6015 else
6016 tail->next = c;
6018 tail = c;
6020 if (gfc_match_char (')') == MATCH_YES)
6021 break;
6022 if (gfc_match_char (',') != MATCH_YES)
6023 goto syntax;
6026 m = match_case_eos ();
6027 if (m == MATCH_NO)
6028 goto syntax;
6029 if (m == MATCH_ERROR)
6030 goto cleanup;
6032 new_st.op = EXEC_SELECT;
6033 new_st.ext.block.case_list = head;
6035 return MATCH_YES;
6037 syntax:
6038 gfc_error ("Syntax error in CASE specification at %C");
6040 cleanup:
6041 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
6042 return MATCH_ERROR;
6046 /* Match a TYPE IS statement. */
6048 match
6049 gfc_match_type_is (void)
6051 gfc_case *c = NULL;
6052 match m;
6054 if (gfc_current_state () != COMP_SELECT_TYPE)
6056 gfc_error ("Unexpected TYPE IS statement at %C");
6057 return MATCH_ERROR;
6060 if (gfc_match_char ('(') != MATCH_YES)
6061 goto syntax;
6063 c = gfc_get_case ();
6064 c->where = gfc_current_locus;
6066 m = gfc_match_type_spec (&c->ts);
6067 if (m == MATCH_NO)
6068 goto syntax;
6069 if (m == MATCH_ERROR)
6070 goto cleanup;
6072 if (gfc_match_char (')') != MATCH_YES)
6073 goto syntax;
6075 m = match_case_eos ();
6076 if (m == MATCH_NO)
6077 goto syntax;
6078 if (m == MATCH_ERROR)
6079 goto cleanup;
6081 new_st.op = EXEC_SELECT_TYPE;
6082 new_st.ext.block.case_list = c;
6084 if (c->ts.type == BT_DERIVED && c->ts.u.derived
6085 && (c->ts.u.derived->attr.sequence
6086 || c->ts.u.derived->attr.is_bind_c))
6088 gfc_error ("The type-spec shall not specify a sequence derived "
6089 "type or a type with the BIND attribute in SELECT "
6090 "TYPE at %C [F2003:C815]");
6091 return MATCH_ERROR;
6094 /* Create temporary variable. */
6095 select_type_set_tmp (&c->ts);
6097 return MATCH_YES;
6099 syntax:
6100 gfc_error ("Syntax error in TYPE IS specification at %C");
6102 cleanup:
6103 if (c != NULL)
6104 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
6105 return MATCH_ERROR;
6109 /* Match a CLASS IS or CLASS DEFAULT statement. */
6111 match
6112 gfc_match_class_is (void)
6114 gfc_case *c = NULL;
6115 match m;
6117 if (gfc_current_state () != COMP_SELECT_TYPE)
6118 return MATCH_NO;
6120 if (gfc_match ("% default") == MATCH_YES)
6122 m = match_case_eos ();
6123 if (m == MATCH_NO)
6124 goto syntax;
6125 if (m == MATCH_ERROR)
6126 goto cleanup;
6128 new_st.op = EXEC_SELECT_TYPE;
6129 c = gfc_get_case ();
6130 c->where = gfc_current_locus;
6131 c->ts.type = BT_UNKNOWN;
6132 new_st.ext.block.case_list = c;
6133 select_type_set_tmp (NULL);
6134 return MATCH_YES;
6137 m = gfc_match ("% is");
6138 if (m == MATCH_NO)
6139 goto syntax;
6140 if (m == MATCH_ERROR)
6141 goto cleanup;
6143 if (gfc_match_char ('(') != MATCH_YES)
6144 goto syntax;
6146 c = gfc_get_case ();
6147 c->where = gfc_current_locus;
6149 m = match_derived_type_spec (&c->ts);
6150 if (m == MATCH_NO)
6151 goto syntax;
6152 if (m == MATCH_ERROR)
6153 goto cleanup;
6155 if (c->ts.type == BT_DERIVED)
6156 c->ts.type = BT_CLASS;
6158 if (gfc_match_char (')') != MATCH_YES)
6159 goto syntax;
6161 m = match_case_eos ();
6162 if (m == MATCH_NO)
6163 goto syntax;
6164 if (m == MATCH_ERROR)
6165 goto cleanup;
6167 new_st.op = EXEC_SELECT_TYPE;
6168 new_st.ext.block.case_list = c;
6170 /* Create temporary variable. */
6171 select_type_set_tmp (&c->ts);
6173 return MATCH_YES;
6175 syntax:
6176 gfc_error ("Syntax error in CLASS IS specification at %C");
6178 cleanup:
6179 if (c != NULL)
6180 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
6181 return MATCH_ERROR;
6185 /********************* WHERE subroutines ********************/
6187 /* Match the rest of a simple WHERE statement that follows an IF statement.
6190 static match
6191 match_simple_where (void)
6193 gfc_expr *expr;
6194 gfc_code *c;
6195 match m;
6197 m = gfc_match (" ( %e )", &expr);
6198 if (m != MATCH_YES)
6199 return m;
6201 m = gfc_match_assignment ();
6202 if (m == MATCH_NO)
6203 goto syntax;
6204 if (m == MATCH_ERROR)
6205 goto cleanup;
6207 if (gfc_match_eos () != MATCH_YES)
6208 goto syntax;
6210 c = gfc_get_code (EXEC_WHERE);
6211 c->expr1 = expr;
6213 c->next = XCNEW (gfc_code);
6214 *c->next = new_st;
6215 c->next->loc = gfc_current_locus;
6216 gfc_clear_new_st ();
6218 new_st.op = EXEC_WHERE;
6219 new_st.block = c;
6221 return MATCH_YES;
6223 syntax:
6224 gfc_syntax_error (ST_WHERE);
6226 cleanup:
6227 gfc_free_expr (expr);
6228 return MATCH_ERROR;
6232 /* Match a WHERE statement. */
6234 match
6235 gfc_match_where (gfc_statement *st)
6237 gfc_expr *expr;
6238 match m0, m;
6239 gfc_code *c;
6241 m0 = gfc_match_label ();
6242 if (m0 == MATCH_ERROR)
6243 return m0;
6245 m = gfc_match (" where ( %e )", &expr);
6246 if (m != MATCH_YES)
6247 return m;
6249 if (gfc_match_eos () == MATCH_YES)
6251 *st = ST_WHERE_BLOCK;
6252 new_st.op = EXEC_WHERE;
6253 new_st.expr1 = expr;
6254 return MATCH_YES;
6257 m = gfc_match_assignment ();
6258 if (m == MATCH_NO)
6259 gfc_syntax_error (ST_WHERE);
6261 if (m != MATCH_YES)
6263 gfc_free_expr (expr);
6264 return MATCH_ERROR;
6267 /* We've got a simple WHERE statement. */
6268 *st = ST_WHERE;
6269 c = gfc_get_code (EXEC_WHERE);
6270 c->expr1 = expr;
6272 /* Put in the assignment. It will not be processed by add_statement, so we
6273 need to copy the location here. */
6275 c->next = XCNEW (gfc_code);
6276 *c->next = new_st;
6277 c->next->loc = gfc_current_locus;
6278 gfc_clear_new_st ();
6280 new_st.op = EXEC_WHERE;
6281 new_st.block = c;
6283 return MATCH_YES;
6287 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
6288 new_st if successful. */
6290 match
6291 gfc_match_elsewhere (void)
6293 char name[GFC_MAX_SYMBOL_LEN + 1];
6294 gfc_expr *expr;
6295 match m;
6297 if (gfc_current_state () != COMP_WHERE)
6299 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
6300 return MATCH_ERROR;
6303 expr = NULL;
6305 if (gfc_match_char ('(') == MATCH_YES)
6307 m = gfc_match_expr (&expr);
6308 if (m == MATCH_NO)
6309 goto syntax;
6310 if (m == MATCH_ERROR)
6311 return MATCH_ERROR;
6313 if (gfc_match_char (')') != MATCH_YES)
6314 goto syntax;
6317 if (gfc_match_eos () != MATCH_YES)
6319 /* Only makes sense if we have a where-construct-name. */
6320 if (!gfc_current_block ())
6322 m = MATCH_ERROR;
6323 goto cleanup;
6325 /* Better be a name at this point. */
6326 m = gfc_match_name (name);
6327 if (m == MATCH_NO)
6328 goto syntax;
6329 if (m == MATCH_ERROR)
6330 goto cleanup;
6332 if (gfc_match_eos () != MATCH_YES)
6333 goto syntax;
6335 if (strcmp (name, gfc_current_block ()->name) != 0)
6337 gfc_error ("Label %qs at %C doesn't match WHERE label %qs",
6338 name, gfc_current_block ()->name);
6339 goto cleanup;
6343 new_st.op = EXEC_WHERE;
6344 new_st.expr1 = expr;
6345 return MATCH_YES;
6347 syntax:
6348 gfc_syntax_error (ST_ELSEWHERE);
6350 cleanup:
6351 gfc_free_expr (expr);
6352 return MATCH_ERROR;