Daily bump.
[official-gcc.git] / gcc / fortran / match.c
blob2bf21434a42a017ef93c52b5384ca21d771a5eaf
1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000-2021 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 /* List of type parameter expressions. */
37 gfc_actual_arglist *type_param_spec_list;
39 /* For debugging and diagnostic purposes. Return the textual representation
40 of the intrinsic operator OP. */
41 const char *
42 gfc_op2string (gfc_intrinsic_op op)
44 switch (op)
46 case INTRINSIC_UPLUS:
47 case INTRINSIC_PLUS:
48 return "+";
50 case INTRINSIC_UMINUS:
51 case INTRINSIC_MINUS:
52 return "-";
54 case INTRINSIC_POWER:
55 return "**";
56 case INTRINSIC_CONCAT:
57 return "//";
58 case INTRINSIC_TIMES:
59 return "*";
60 case INTRINSIC_DIVIDE:
61 return "/";
63 case INTRINSIC_AND:
64 return ".and.";
65 case INTRINSIC_OR:
66 return ".or.";
67 case INTRINSIC_EQV:
68 return ".eqv.";
69 case INTRINSIC_NEQV:
70 return ".neqv.";
72 case INTRINSIC_EQ_OS:
73 return ".eq.";
74 case INTRINSIC_EQ:
75 return "==";
76 case INTRINSIC_NE_OS:
77 return ".ne.";
78 case INTRINSIC_NE:
79 return "/=";
80 case INTRINSIC_GE_OS:
81 return ".ge.";
82 case INTRINSIC_GE:
83 return ">=";
84 case INTRINSIC_LE_OS:
85 return ".le.";
86 case INTRINSIC_LE:
87 return "<=";
88 case INTRINSIC_LT_OS:
89 return ".lt.";
90 case INTRINSIC_LT:
91 return "<";
92 case INTRINSIC_GT_OS:
93 return ".gt.";
94 case INTRINSIC_GT:
95 return ">";
96 case INTRINSIC_NOT:
97 return ".not.";
99 case INTRINSIC_ASSIGN:
100 return "=";
102 case INTRINSIC_PARENTHESES:
103 return "parens";
105 case INTRINSIC_NONE:
106 return "none";
108 /* DTIO */
109 case INTRINSIC_FORMATTED:
110 return "formatted";
111 case INTRINSIC_UNFORMATTED:
112 return "unformatted";
114 default:
115 break;
118 gfc_internal_error ("gfc_op2string(): Bad code");
119 /* Not reached. */
123 /******************** Generic matching subroutines ************************/
125 /* Matches a member separator. With standard FORTRAN this is '%', but with
126 DEC structures we must carefully match dot ('.').
127 Because operators are spelled ".op.", a dotted string such as "x.y.z..."
128 can be either a component reference chain or a combination of binary
129 operations.
130 There is no real way to win because the string may be grammatically
131 ambiguous. The following rules help avoid ambiguities - they match
132 some behavior of other (older) compilers. If the rules here are changed
133 the test cases should be updated. If the user has problems with these rules
134 they probably deserve the consequences. Consider "x.y.z":
135 (1) If any user defined operator ".y." exists, this is always y(x,z)
136 (even if ".y." is the wrong type and/or x has a member y).
137 (2) Otherwise if x has a member y, and y is itself a derived type,
138 this is (x->y)->z, even if an intrinsic operator exists which
139 can handle (x,z).
140 (3) If x has no member y or (x->y) is not a derived type but ".y."
141 is an intrinsic operator (such as ".eq."), this is y(x,z).
142 (4) Lastly if there is no operator ".y." and x has no member "y", it is an
143 error.
144 It is worth noting that the logic here does not support mixed use of member
145 accessors within a single string. That is, even if x has component y and y
146 has component z, the following are all syntax errors:
147 "x%y.z" "x.y%z" "(x.y).z" "(x%y)%z"
150 match
151 gfc_match_member_sep(gfc_symbol *sym)
153 char name[GFC_MAX_SYMBOL_LEN + 1];
154 locus dot_loc, start_loc;
155 gfc_intrinsic_op iop;
156 match m;
157 gfc_symbol *tsym;
158 gfc_component *c = NULL;
160 /* What a relief: '%' is an unambiguous member separator. */
161 if (gfc_match_char ('%') == MATCH_YES)
162 return MATCH_YES;
164 /* Beware ye who enter here. */
165 if (!flag_dec_structure || !sym)
166 return MATCH_NO;
168 tsym = NULL;
170 /* We may be given either a derived type variable or the derived type
171 declaration itself (which actually contains the components);
172 we need the latter to search for components. */
173 if (gfc_fl_struct (sym->attr.flavor))
174 tsym = sym;
175 else if (gfc_bt_struct (sym->ts.type))
176 tsym = sym->ts.u.derived;
178 iop = INTRINSIC_NONE;
179 name[0] = '\0';
180 m = MATCH_NO;
182 /* If we have to reject come back here later. */
183 start_loc = gfc_current_locus;
185 /* Look for a component access next. */
186 if (gfc_match_char ('.') != MATCH_YES)
187 return MATCH_NO;
189 /* If we accept, come back here. */
190 dot_loc = gfc_current_locus;
192 /* Try to match a symbol name following the dot. */
193 if (gfc_match_name (name) != MATCH_YES)
195 gfc_error ("Expected structure component or operator name "
196 "after '.' at %C");
197 goto error;
200 /* If no dot follows we have "x.y" which should be a component access. */
201 if (gfc_match_char ('.') != MATCH_YES)
202 goto yes;
204 /* Now we have a string "x.y.z" which could be a nested member access
205 (x->y)->z or a binary operation y on x and z. */
207 /* First use any user-defined operators ".y." */
208 if (gfc_find_uop (name, sym->ns) != NULL)
209 goto no;
211 /* Match accesses to existing derived-type components for
212 derived-type vars: "x.y.z" = (x->y)->z */
213 c = gfc_find_component(tsym, name, false, true, NULL);
214 if (c && (gfc_bt_struct (c->ts.type) || c->ts.type == BT_CLASS))
215 goto yes;
217 /* If y is not a component or has no members, try intrinsic operators. */
218 gfc_current_locus = start_loc;
219 if (gfc_match_intrinsic_op (&iop) != MATCH_YES)
221 /* If ".y." is not an intrinsic operator but y was a valid non-
222 structure component, match and leave the trailing dot to be
223 dealt with later. */
224 if (c)
225 goto yes;
227 gfc_error ("%qs is neither a defined operator nor a "
228 "structure component in dotted string at %C", name);
229 goto error;
232 /* .y. is an intrinsic operator, overriding any possible member access. */
233 goto no;
235 /* Return keeping the current locus consistent with the match result. */
236 error:
237 m = MATCH_ERROR;
239 gfc_current_locus = start_loc;
240 return m;
241 yes:
242 gfc_current_locus = dot_loc;
243 return MATCH_YES;
247 /* This function scans the current statement counting the opened and closed
248 parenthesis to make sure they are balanced. */
250 match
251 gfc_match_parens (void)
253 locus old_loc, where;
254 int count;
255 gfc_instring instring;
256 gfc_char_t c, quote;
258 old_loc = gfc_current_locus;
259 count = 0;
260 instring = NONSTRING;
261 quote = ' ';
263 for (;;)
265 if (count > 0)
266 where = gfc_current_locus;
267 c = gfc_next_char_literal (instring);
268 if (c == '\n')
269 break;
270 if (quote == ' ' && ((c == '\'') || (c == '"')))
272 quote = c;
273 instring = INSTRING_WARN;
274 continue;
276 if (quote != ' ' && c == quote)
278 quote = ' ';
279 instring = NONSTRING;
280 continue;
283 if (c == '(' && quote == ' ')
285 count++;
287 if (c == ')' && quote == ' ')
289 count--;
290 where = gfc_current_locus;
294 gfc_current_locus = old_loc;
296 if (count != 0)
298 gfc_error ("Missing %qs in statement at or before %L",
299 count > 0? ")":"(", &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 /* Matches a statement label. Uses gfc_match_small_literal_int() to
534 do most of the work. */
536 match
537 gfc_match_st_label (gfc_st_label **label)
539 locus old_loc;
540 match m;
541 int i, cnt;
543 old_loc = gfc_current_locus;
545 m = gfc_match_small_literal_int (&i, &cnt);
546 if (m != MATCH_YES)
547 return m;
549 if (cnt > 5)
551 gfc_error ("Too many digits in statement label at %C");
552 goto cleanup;
555 if (i == 0)
557 gfc_error ("Statement label at %C is zero");
558 goto cleanup;
561 *label = gfc_get_st_label (i);
562 return MATCH_YES;
564 cleanup:
566 gfc_current_locus = old_loc;
567 return MATCH_ERROR;
571 /* Match and validate a label associated with a named IF, DO or SELECT
572 statement. If the symbol does not have the label attribute, we add
573 it. We also make sure the symbol does not refer to another
574 (active) block. A matched label is pointed to by gfc_new_block. */
576 static match
577 gfc_match_label (void)
579 char name[GFC_MAX_SYMBOL_LEN + 1];
580 match m;
582 gfc_new_block = NULL;
584 m = gfc_match (" %n :", name);
585 if (m != MATCH_YES)
586 return m;
588 if (gfc_get_symbol (name, NULL, &gfc_new_block))
590 gfc_error ("Label name %qs at %C is ambiguous", name);
591 return MATCH_ERROR;
594 if (gfc_new_block->attr.flavor == FL_LABEL)
596 gfc_error ("Duplicate construct label %qs at %C", name);
597 return MATCH_ERROR;
600 if (!gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
601 gfc_new_block->name, NULL))
602 return MATCH_ERROR;
604 return MATCH_YES;
608 /* See if the current input looks like a name of some sort. Modifies
609 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
610 Note that options.c restricts max_identifier_length to not more
611 than GFC_MAX_SYMBOL_LEN. */
613 match
614 gfc_match_name (char *buffer)
616 locus old_loc;
617 int i;
618 char c;
620 old_loc = gfc_current_locus;
621 gfc_gobble_whitespace ();
623 c = gfc_next_ascii_char ();
624 if (!(ISALPHA (c) || (c == '_' && flag_allow_leading_underscore)))
626 /* Special cases for unary minus and plus, which allows for a sensible
627 error message for code of the form 'c = exp(-a*b) )' where an
628 extra ')' appears at the end of statement. */
629 if (!gfc_error_flag_test () && c != '(' && c != '-' && c != '+')
630 gfc_error ("Invalid character in name at %C");
631 gfc_current_locus = old_loc;
632 return MATCH_NO;
635 i = 0;
639 buffer[i++] = c;
641 if (i > gfc_option.max_identifier_length)
643 gfc_error ("Name at %C is too long");
644 return MATCH_ERROR;
647 old_loc = gfc_current_locus;
648 c = gfc_next_ascii_char ();
650 while (ISALNUM (c) || c == '_' || (flag_dollar_ok && c == '$'));
652 if (c == '$' && !flag_dollar_ok)
654 gfc_fatal_error ("Invalid character %<$%> at %L. Use %<-fdollar-ok%> to "
655 "allow it as an extension", &old_loc);
656 return MATCH_ERROR;
659 buffer[i] = '\0';
660 gfc_current_locus = old_loc;
662 return MATCH_YES;
666 /* Match a symbol on the input. Modifies the pointer to the symbol
667 pointer if successful. */
669 match
670 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
672 char buffer[GFC_MAX_SYMBOL_LEN + 1];
673 match m;
675 m = gfc_match_name (buffer);
676 if (m != MATCH_YES)
677 return m;
679 if (host_assoc)
680 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
681 ? MATCH_ERROR : MATCH_YES;
683 if (gfc_get_sym_tree (buffer, NULL, matched_symbol, false))
684 return MATCH_ERROR;
686 return MATCH_YES;
690 match
691 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
693 gfc_symtree *st;
694 match m;
696 m = gfc_match_sym_tree (&st, host_assoc);
698 if (m == MATCH_YES)
700 if (st)
701 *matched_symbol = st->n.sym;
702 else
703 *matched_symbol = NULL;
705 else
706 *matched_symbol = NULL;
707 return m;
711 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
712 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
713 in matchexp.c. */
715 match
716 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
718 locus orig_loc = gfc_current_locus;
719 char ch;
721 gfc_gobble_whitespace ();
722 ch = gfc_next_ascii_char ();
723 switch (ch)
725 case '+':
726 /* Matched "+". */
727 *result = INTRINSIC_PLUS;
728 return MATCH_YES;
730 case '-':
731 /* Matched "-". */
732 *result = INTRINSIC_MINUS;
733 return MATCH_YES;
735 case '=':
736 if (gfc_next_ascii_char () == '=')
738 /* Matched "==". */
739 *result = INTRINSIC_EQ;
740 return MATCH_YES;
742 break;
744 case '<':
745 if (gfc_peek_ascii_char () == '=')
747 /* Matched "<=". */
748 gfc_next_ascii_char ();
749 *result = INTRINSIC_LE;
750 return MATCH_YES;
752 /* Matched "<". */
753 *result = INTRINSIC_LT;
754 return MATCH_YES;
756 case '>':
757 if (gfc_peek_ascii_char () == '=')
759 /* Matched ">=". */
760 gfc_next_ascii_char ();
761 *result = INTRINSIC_GE;
762 return MATCH_YES;
764 /* Matched ">". */
765 *result = INTRINSIC_GT;
766 return MATCH_YES;
768 case '*':
769 if (gfc_peek_ascii_char () == '*')
771 /* Matched "**". */
772 gfc_next_ascii_char ();
773 *result = INTRINSIC_POWER;
774 return MATCH_YES;
776 /* Matched "*". */
777 *result = INTRINSIC_TIMES;
778 return MATCH_YES;
780 case '/':
781 ch = gfc_peek_ascii_char ();
782 if (ch == '=')
784 /* Matched "/=". */
785 gfc_next_ascii_char ();
786 *result = INTRINSIC_NE;
787 return MATCH_YES;
789 else if (ch == '/')
791 /* Matched "//". */
792 gfc_next_ascii_char ();
793 *result = INTRINSIC_CONCAT;
794 return MATCH_YES;
796 /* Matched "/". */
797 *result = INTRINSIC_DIVIDE;
798 return MATCH_YES;
800 case '.':
801 ch = gfc_next_ascii_char ();
802 switch (ch)
804 case 'a':
805 if (gfc_next_ascii_char () == 'n'
806 && gfc_next_ascii_char () == 'd'
807 && gfc_next_ascii_char () == '.')
809 /* Matched ".and.". */
810 *result = INTRINSIC_AND;
811 return MATCH_YES;
813 break;
815 case 'e':
816 if (gfc_next_ascii_char () == 'q')
818 ch = gfc_next_ascii_char ();
819 if (ch == '.')
821 /* Matched ".eq.". */
822 *result = INTRINSIC_EQ_OS;
823 return MATCH_YES;
825 else if (ch == 'v')
827 if (gfc_next_ascii_char () == '.')
829 /* Matched ".eqv.". */
830 *result = INTRINSIC_EQV;
831 return MATCH_YES;
835 break;
837 case 'g':
838 ch = gfc_next_ascii_char ();
839 if (ch == 'e')
841 if (gfc_next_ascii_char () == '.')
843 /* Matched ".ge.". */
844 *result = INTRINSIC_GE_OS;
845 return MATCH_YES;
848 else if (ch == 't')
850 if (gfc_next_ascii_char () == '.')
852 /* Matched ".gt.". */
853 *result = INTRINSIC_GT_OS;
854 return MATCH_YES;
857 break;
859 case 'l':
860 ch = gfc_next_ascii_char ();
861 if (ch == 'e')
863 if (gfc_next_ascii_char () == '.')
865 /* Matched ".le.". */
866 *result = INTRINSIC_LE_OS;
867 return MATCH_YES;
870 else if (ch == 't')
872 if (gfc_next_ascii_char () == '.')
874 /* Matched ".lt.". */
875 *result = INTRINSIC_LT_OS;
876 return MATCH_YES;
879 break;
881 case 'n':
882 ch = gfc_next_ascii_char ();
883 if (ch == 'e')
885 ch = gfc_next_ascii_char ();
886 if (ch == '.')
888 /* Matched ".ne.". */
889 *result = INTRINSIC_NE_OS;
890 return MATCH_YES;
892 else if (ch == 'q')
894 if (gfc_next_ascii_char () == 'v'
895 && gfc_next_ascii_char () == '.')
897 /* Matched ".neqv.". */
898 *result = INTRINSIC_NEQV;
899 return MATCH_YES;
903 else if (ch == 'o')
905 if (gfc_next_ascii_char () == 't'
906 && gfc_next_ascii_char () == '.')
908 /* Matched ".not.". */
909 *result = INTRINSIC_NOT;
910 return MATCH_YES;
913 break;
915 case 'o':
916 if (gfc_next_ascii_char () == 'r'
917 && gfc_next_ascii_char () == '.')
919 /* Matched ".or.". */
920 *result = INTRINSIC_OR;
921 return MATCH_YES;
923 break;
925 case 'x':
926 if (gfc_next_ascii_char () == 'o'
927 && gfc_next_ascii_char () == 'r'
928 && gfc_next_ascii_char () == '.')
930 if (!gfc_notify_std (GFC_STD_LEGACY, ".XOR. operator at %C"))
931 return MATCH_ERROR;
932 /* Matched ".xor." - equivalent to ".neqv.". */
933 *result = INTRINSIC_NEQV;
934 return MATCH_YES;
936 break;
938 default:
939 break;
941 break;
943 default:
944 break;
947 gfc_current_locus = orig_loc;
948 return MATCH_NO;
952 /* Match a loop control phrase:
954 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
956 If the final integer expression is not present, a constant unity
957 expression is returned. We don't return MATCH_ERROR until after
958 the equals sign is seen. */
960 match
961 gfc_match_iterator (gfc_iterator *iter, int init_flag)
963 char name[GFC_MAX_SYMBOL_LEN + 1];
964 gfc_expr *var, *e1, *e2, *e3;
965 locus start;
966 match m;
968 e1 = e2 = e3 = NULL;
970 /* Match the start of an iterator without affecting the symbol table. */
972 start = gfc_current_locus;
973 m = gfc_match (" %n =", name);
974 gfc_current_locus = start;
976 if (m != MATCH_YES)
977 return MATCH_NO;
979 m = gfc_match_variable (&var, 0);
980 if (m != MATCH_YES)
981 return MATCH_NO;
983 if (var->symtree->n.sym->attr.dimension)
985 gfc_error ("Loop variable at %C cannot be an array");
986 goto cleanup;
989 /* F2008, C617 & C565. */
990 if (var->symtree->n.sym->attr.codimension)
992 gfc_error ("Loop variable at %C cannot be a coarray");
993 goto cleanup;
996 if (var->ref != NULL)
998 gfc_error ("Loop variable at %C cannot be a sub-component");
999 goto cleanup;
1002 gfc_match_char ('=');
1004 var->symtree->n.sym->attr.implied_index = 1;
1006 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
1007 if (m == MATCH_NO)
1008 goto syntax;
1009 if (m == MATCH_ERROR)
1010 goto cleanup;
1012 if (gfc_match_char (',') != MATCH_YES)
1013 goto syntax;
1015 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
1016 if (m == MATCH_NO)
1017 goto syntax;
1018 if (m == MATCH_ERROR)
1019 goto cleanup;
1021 if (gfc_match_char (',') != MATCH_YES)
1023 e3 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
1024 goto done;
1027 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
1028 if (m == MATCH_ERROR)
1029 goto cleanup;
1030 if (m == MATCH_NO)
1032 gfc_error ("Expected a step value in iterator at %C");
1033 goto cleanup;
1036 done:
1037 iter->var = var;
1038 iter->start = e1;
1039 iter->end = e2;
1040 iter->step = e3;
1041 return MATCH_YES;
1043 syntax:
1044 gfc_error ("Syntax error in iterator at %C");
1046 cleanup:
1047 gfc_free_expr (e1);
1048 gfc_free_expr (e2);
1049 gfc_free_expr (e3);
1051 return MATCH_ERROR;
1055 /* Tries to match the next non-whitespace character on the input.
1056 This subroutine does not return MATCH_ERROR. */
1058 match
1059 gfc_match_char (char c)
1061 locus where;
1063 where = gfc_current_locus;
1064 gfc_gobble_whitespace ();
1066 if (gfc_next_ascii_char () == c)
1067 return MATCH_YES;
1069 gfc_current_locus = where;
1070 return MATCH_NO;
1074 /* General purpose matching subroutine. The target string is a
1075 scanf-like format string in which spaces correspond to arbitrary
1076 whitespace (including no whitespace), characters correspond to
1077 themselves. The %-codes are:
1079 %% Literal percent sign
1080 %e Expression, pointer to a pointer is set
1081 %s Symbol, pointer to the symbol is set
1082 %n Name, character buffer is set to name
1083 %t Matches end of statement.
1084 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1085 %l Matches a statement label
1086 %v Matches a variable expression (an lvalue, except function references
1087 having a data pointer result)
1088 % Matches a required space (in free form) and optional spaces. */
1090 match
1091 gfc_match (const char *target, ...)
1093 gfc_st_label **label;
1094 int matches, *ip;
1095 locus old_loc;
1096 va_list argp;
1097 char c, *np;
1098 match m, n;
1099 void **vp;
1100 const char *p;
1102 old_loc = gfc_current_locus;
1103 va_start (argp, target);
1104 m = MATCH_NO;
1105 matches = 0;
1106 p = target;
1108 loop:
1109 c = *p++;
1110 switch (c)
1112 case ' ':
1113 gfc_gobble_whitespace ();
1114 goto loop;
1115 case '\0':
1116 m = MATCH_YES;
1117 break;
1119 case '%':
1120 c = *p++;
1121 switch (c)
1123 case 'e':
1124 vp = va_arg (argp, void **);
1125 n = gfc_match_expr ((gfc_expr **) vp);
1126 if (n != MATCH_YES)
1128 m = n;
1129 goto not_yes;
1132 matches++;
1133 goto loop;
1135 case 'v':
1136 vp = va_arg (argp, void **);
1137 n = gfc_match_variable ((gfc_expr **) vp, 0);
1138 if (n != MATCH_YES)
1140 m = n;
1141 goto not_yes;
1144 matches++;
1145 goto loop;
1147 case 's':
1148 vp = va_arg (argp, void **);
1149 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1150 if (n != MATCH_YES)
1152 m = n;
1153 goto not_yes;
1156 matches++;
1157 goto loop;
1159 case 'n':
1160 np = va_arg (argp, char *);
1161 n = gfc_match_name (np);
1162 if (n != MATCH_YES)
1164 m = n;
1165 goto not_yes;
1168 matches++;
1169 goto loop;
1171 case 'l':
1172 label = va_arg (argp, gfc_st_label **);
1173 n = gfc_match_st_label (label);
1174 if (n != MATCH_YES)
1176 m = n;
1177 goto not_yes;
1180 matches++;
1181 goto loop;
1183 case 'o':
1184 ip = va_arg (argp, int *);
1185 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1186 if (n != MATCH_YES)
1188 m = n;
1189 goto not_yes;
1192 matches++;
1193 goto loop;
1195 case 't':
1196 if (gfc_match_eos () != MATCH_YES)
1198 m = MATCH_NO;
1199 goto not_yes;
1201 goto loop;
1203 case ' ':
1204 if (gfc_match_space () == MATCH_YES)
1205 goto loop;
1206 m = MATCH_NO;
1207 goto not_yes;
1209 case '%':
1210 break; /* Fall through to character matcher. */
1212 default:
1213 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1215 /* FALLTHRU */
1217 default:
1219 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1220 expect an upper case character here! */
1221 gcc_assert (TOLOWER (c) == c);
1223 if (c == gfc_next_ascii_char ())
1224 goto loop;
1225 break;
1228 not_yes:
1229 va_end (argp);
1231 if (m != MATCH_YES)
1233 /* Clean up after a failed match. */
1234 gfc_current_locus = old_loc;
1235 va_start (argp, target);
1237 p = target;
1238 for (; matches > 0; matches--)
1240 while (*p++ != '%');
1242 switch (*p++)
1244 case '%':
1245 matches++;
1246 break; /* Skip. */
1248 /* Matches that don't have to be undone */
1249 case 'o':
1250 case 'l':
1251 case 'n':
1252 case 's':
1253 (void) va_arg (argp, void **);
1254 break;
1256 case 'e':
1257 case 'v':
1258 vp = va_arg (argp, void **);
1259 gfc_free_expr ((struct gfc_expr *)*vp);
1260 *vp = NULL;
1261 break;
1265 va_end (argp);
1268 return m;
1272 /*********************** Statement level matching **********************/
1274 /* Matches the start of a program unit, which is the program keyword
1275 followed by an obligatory symbol. */
1277 match
1278 gfc_match_program (void)
1280 gfc_symbol *sym;
1281 match m;
1283 m = gfc_match ("% %s%t", &sym);
1285 if (m == MATCH_NO)
1287 gfc_error ("Invalid form of PROGRAM statement at %C");
1288 m = MATCH_ERROR;
1291 if (m == MATCH_ERROR)
1292 return m;
1294 if (!gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL))
1295 return MATCH_ERROR;
1297 gfc_new_block = sym;
1299 return MATCH_YES;
1303 /* Match a simple assignment statement. */
1305 match
1306 gfc_match_assignment (void)
1308 gfc_expr *lvalue, *rvalue;
1309 locus old_loc;
1310 match m;
1312 old_loc = gfc_current_locus;
1314 lvalue = NULL;
1315 m = gfc_match (" %v =", &lvalue);
1316 if (m != MATCH_YES)
1318 gfc_current_locus = old_loc;
1319 gfc_free_expr (lvalue);
1320 return MATCH_NO;
1323 rvalue = NULL;
1324 m = gfc_match (" %e%t", &rvalue);
1326 if (m == MATCH_YES
1327 && rvalue->ts.type == BT_BOZ
1328 && lvalue->ts.type == BT_CLASS)
1330 m = MATCH_ERROR;
1331 gfc_error ("BOZ literal constant at %L is neither a DATA statement "
1332 "value nor an actual argument of INT/REAL/DBLE/CMPLX "
1333 "intrinsic subprogram", &rvalue->where);
1336 if (lvalue->expr_type == EXPR_CONSTANT)
1338 /* This clobbers %len and %kind. */
1339 m = MATCH_ERROR;
1340 gfc_error ("Assignment to a constant expression at %C");
1343 if (m != MATCH_YES)
1345 gfc_current_locus = old_loc;
1346 gfc_free_expr (lvalue);
1347 gfc_free_expr (rvalue);
1348 return m;
1351 if (!lvalue->symtree)
1353 gfc_free_expr (lvalue);
1354 gfc_free_expr (rvalue);
1355 return MATCH_ERROR;
1359 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1361 new_st.op = EXEC_ASSIGN;
1362 new_st.expr1 = lvalue;
1363 new_st.expr2 = rvalue;
1365 gfc_check_do_variable (lvalue->symtree);
1367 return MATCH_YES;
1371 /* Match a pointer assignment statement. */
1373 match
1374 gfc_match_pointer_assignment (void)
1376 gfc_expr *lvalue, *rvalue;
1377 locus old_loc;
1378 match m;
1380 old_loc = gfc_current_locus;
1382 lvalue = rvalue = NULL;
1383 gfc_matching_ptr_assignment = 0;
1384 gfc_matching_procptr_assignment = 0;
1386 m = gfc_match (" %v =>", &lvalue);
1387 if (m != MATCH_YES || !lvalue->symtree)
1389 m = MATCH_NO;
1390 goto cleanup;
1393 if (lvalue->symtree->n.sym->attr.proc_pointer
1394 || gfc_is_proc_ptr_comp (lvalue))
1395 gfc_matching_procptr_assignment = 1;
1396 else
1397 gfc_matching_ptr_assignment = 1;
1399 m = gfc_match (" %e%t", &rvalue);
1400 gfc_matching_ptr_assignment = 0;
1401 gfc_matching_procptr_assignment = 0;
1402 if (m != MATCH_YES)
1403 goto cleanup;
1405 new_st.op = EXEC_POINTER_ASSIGN;
1406 new_st.expr1 = lvalue;
1407 new_st.expr2 = rvalue;
1409 return MATCH_YES;
1411 cleanup:
1412 gfc_current_locus = old_loc;
1413 gfc_free_expr (lvalue);
1414 gfc_free_expr (rvalue);
1415 return m;
1419 /* We try to match an easy arithmetic IF statement. This only happens
1420 when just after having encountered a simple IF statement. This code
1421 is really duplicate with parts of the gfc_match_if code, but this is
1422 *much* easier. */
1424 static match
1425 match_arithmetic_if (void)
1427 gfc_st_label *l1, *l2, *l3;
1428 gfc_expr *expr;
1429 match m;
1431 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1432 if (m != MATCH_YES)
1433 return m;
1435 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1436 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1437 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1439 gfc_free_expr (expr);
1440 return MATCH_ERROR;
1443 if (!gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
1444 "Arithmetic IF statement at %C"))
1445 return MATCH_ERROR;
1447 new_st.op = EXEC_ARITHMETIC_IF;
1448 new_st.expr1 = expr;
1449 new_st.label1 = l1;
1450 new_st.label2 = l2;
1451 new_st.label3 = l3;
1453 return MATCH_YES;
1457 /* The IF statement is a bit of a pain. First of all, there are three
1458 forms of it, the simple IF, the IF that starts a block and the
1459 arithmetic IF.
1461 There is a problem with the simple IF and that is the fact that we
1462 only have a single level of undo information on symbols. What this
1463 means is for a simple IF, we must re-match the whole IF statement
1464 multiple times in order to guarantee that the symbol table ends up
1465 in the proper state. */
1467 static match match_simple_forall (void);
1468 static match match_simple_where (void);
1470 match
1471 gfc_match_if (gfc_statement *if_type)
1473 gfc_expr *expr;
1474 gfc_st_label *l1, *l2, *l3;
1475 locus old_loc, old_loc2;
1476 gfc_code *p;
1477 match m, n;
1479 n = gfc_match_label ();
1480 if (n == MATCH_ERROR)
1481 return n;
1483 old_loc = gfc_current_locus;
1485 m = gfc_match (" if ", &expr);
1486 if (m != MATCH_YES)
1487 return m;
1489 if (gfc_match_char ('(') != MATCH_YES)
1491 gfc_error ("Missing %<(%> in IF-expression at %C");
1492 return MATCH_ERROR;
1495 m = gfc_match ("%e", &expr);
1496 if (m != MATCH_YES)
1497 return m;
1499 old_loc2 = gfc_current_locus;
1500 gfc_current_locus = old_loc;
1502 if (gfc_match_parens () == MATCH_ERROR)
1503 return MATCH_ERROR;
1505 gfc_current_locus = old_loc2;
1507 if (gfc_match_char (')') != MATCH_YES)
1509 gfc_error ("Syntax error in IF-expression at %C");
1510 gfc_free_expr (expr);
1511 return MATCH_ERROR;
1514 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1516 if (m == MATCH_YES)
1518 if (n == MATCH_YES)
1520 gfc_error ("Block label not appropriate for arithmetic IF "
1521 "statement at %C");
1522 gfc_free_expr (expr);
1523 return MATCH_ERROR;
1526 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1527 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1528 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1530 gfc_free_expr (expr);
1531 return MATCH_ERROR;
1534 if (!gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
1535 "Arithmetic IF statement at %C"))
1536 return MATCH_ERROR;
1538 new_st.op = EXEC_ARITHMETIC_IF;
1539 new_st.expr1 = expr;
1540 new_st.label1 = l1;
1541 new_st.label2 = l2;
1542 new_st.label3 = l3;
1544 *if_type = ST_ARITHMETIC_IF;
1545 return MATCH_YES;
1548 if (gfc_match (" then%t") == MATCH_YES)
1550 new_st.op = EXEC_IF;
1551 new_st.expr1 = expr;
1552 *if_type = ST_IF_BLOCK;
1553 return MATCH_YES;
1556 if (n == MATCH_YES)
1558 gfc_error ("Block label is not appropriate for IF statement at %C");
1559 gfc_free_expr (expr);
1560 return MATCH_ERROR;
1563 /* At this point the only thing left is a simple IF statement. At
1564 this point, n has to be MATCH_NO, so we don't have to worry about
1565 re-matching a block label. From what we've got so far, try
1566 matching an assignment. */
1568 *if_type = ST_SIMPLE_IF;
1570 m = gfc_match_assignment ();
1571 if (m == MATCH_YES)
1572 goto got_match;
1574 gfc_free_expr (expr);
1575 gfc_undo_symbols ();
1576 gfc_current_locus = old_loc;
1578 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1579 assignment was found. For MATCH_NO, continue to call the various
1580 matchers. */
1581 if (m == MATCH_ERROR)
1582 return MATCH_ERROR;
1584 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1586 m = gfc_match_pointer_assignment ();
1587 if (m == MATCH_YES)
1588 goto got_match;
1590 gfc_free_expr (expr);
1591 gfc_undo_symbols ();
1592 gfc_current_locus = old_loc;
1594 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1596 /* Look at the next keyword to see which matcher to call. Matching
1597 the keyword doesn't affect the symbol table, so we don't have to
1598 restore between tries. */
1600 #define match(string, subr, statement) \
1601 if (gfc_match (string) == MATCH_YES) { m = subr(); goto got_match; }
1603 gfc_clear_error ();
1605 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1606 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1607 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1608 match ("call", gfc_match_call, ST_CALL)
1609 match ("change team", gfc_match_change_team, ST_CHANGE_TEAM)
1610 match ("close", gfc_match_close, ST_CLOSE)
1611 match ("continue", gfc_match_continue, ST_CONTINUE)
1612 match ("cycle", gfc_match_cycle, ST_CYCLE)
1613 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1614 match ("end file", gfc_match_endfile, ST_END_FILE)
1615 match ("end team", gfc_match_end_team, ST_END_TEAM)
1616 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP)
1617 match ("event post", gfc_match_event_post, ST_EVENT_POST)
1618 match ("event wait", gfc_match_event_wait, ST_EVENT_WAIT)
1619 match ("exit", gfc_match_exit, ST_EXIT)
1620 match ("fail image", gfc_match_fail_image, ST_FAIL_IMAGE)
1621 match ("flush", gfc_match_flush, ST_FLUSH)
1622 match ("forall", match_simple_forall, ST_FORALL)
1623 match ("form team", gfc_match_form_team, ST_FORM_TEAM)
1624 match ("go to", gfc_match_goto, ST_GOTO)
1625 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1626 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1627 match ("lock", gfc_match_lock, ST_LOCK)
1628 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1629 match ("open", gfc_match_open, ST_OPEN)
1630 match ("pause", gfc_match_pause, ST_NONE)
1631 match ("print", gfc_match_print, ST_WRITE)
1632 match ("read", gfc_match_read, ST_READ)
1633 match ("return", gfc_match_return, ST_RETURN)
1634 match ("rewind", gfc_match_rewind, ST_REWIND)
1635 match ("stop", gfc_match_stop, ST_STOP)
1636 match ("wait", gfc_match_wait, ST_WAIT)
1637 match ("sync all", gfc_match_sync_all, ST_SYNC_CALL);
1638 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
1639 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
1640 match ("sync team", gfc_match_sync_team, ST_SYNC_TEAM)
1641 match ("unlock", gfc_match_unlock, ST_UNLOCK)
1642 match ("where", match_simple_where, ST_WHERE)
1643 match ("write", gfc_match_write, ST_WRITE)
1645 if (flag_dec)
1646 match ("type", gfc_match_print, ST_WRITE)
1648 /* All else has failed, so give up. See if any of the matchers has
1649 stored an error message of some sort. */
1650 if (!gfc_error_check ())
1651 gfc_error ("Syntax error in IF-clause after %C");
1653 gfc_free_expr (expr);
1654 return MATCH_ERROR;
1656 got_match:
1657 if (m == MATCH_NO)
1658 gfc_error ("Syntax error in IF-clause after %C");
1659 if (m != MATCH_YES)
1661 gfc_free_expr (expr);
1662 return MATCH_ERROR;
1665 /* At this point, we've matched the single IF and the action clause
1666 is in new_st. Rearrange things so that the IF statement appears
1667 in new_st. */
1669 p = gfc_get_code (EXEC_IF);
1670 p->next = XCNEW (gfc_code);
1671 *p->next = new_st;
1672 p->next->loc = gfc_current_locus;
1674 p->expr1 = expr;
1676 gfc_clear_new_st ();
1678 new_st.op = EXEC_IF;
1679 new_st.block = p;
1681 return MATCH_YES;
1684 #undef match
1687 /* Match an ELSE statement. */
1689 match
1690 gfc_match_else (void)
1692 char name[GFC_MAX_SYMBOL_LEN + 1];
1694 if (gfc_match_eos () == MATCH_YES)
1695 return MATCH_YES;
1697 if (gfc_match_name (name) != MATCH_YES
1698 || gfc_current_block () == NULL
1699 || gfc_match_eos () != MATCH_YES)
1701 gfc_error ("Invalid character(s) in ELSE statement after %C");
1702 return MATCH_ERROR;
1705 if (strcmp (name, gfc_current_block ()->name) != 0)
1707 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1708 name, gfc_current_block ()->name);
1709 return MATCH_ERROR;
1712 return MATCH_YES;
1716 /* Match an ELSE IF statement. */
1718 match
1719 gfc_match_elseif (void)
1721 char name[GFC_MAX_SYMBOL_LEN + 1];
1722 gfc_expr *expr, *then;
1723 locus where;
1724 match m;
1726 if (gfc_match_char ('(') != MATCH_YES)
1728 gfc_error ("Missing %<(%> in ELSE IF expression at %C");
1729 return MATCH_ERROR;
1732 m = gfc_match (" %e ", &expr);
1733 if (m != MATCH_YES)
1734 return m;
1736 if (gfc_match_char (')') != MATCH_YES)
1738 gfc_error ("Missing %<)%> in ELSE IF expression at %C");
1739 goto cleanup;
1742 m = gfc_match (" then ", &then);
1744 where = gfc_current_locus;
1746 if (m == MATCH_YES && (gfc_match_eos () == MATCH_YES
1747 || (gfc_current_block ()
1748 && gfc_match_name (name) == MATCH_YES)))
1749 goto done;
1751 if (gfc_match_eos () == MATCH_YES)
1753 gfc_error ("Missing THEN in ELSE IF statement after %L", &where);
1754 goto cleanup;
1757 if (gfc_match_name (name) != MATCH_YES
1758 || gfc_current_block () == NULL
1759 || gfc_match_eos () != MATCH_YES)
1761 gfc_error ("Syntax error in ELSE IF statement after %L", &where);
1762 goto cleanup;
1765 if (strcmp (name, gfc_current_block ()->name) != 0)
1767 gfc_error ("Label %qs after %L doesn't match IF label %qs",
1768 name, &where, gfc_current_block ()->name);
1769 goto cleanup;
1772 if (m != MATCH_YES)
1773 return m;
1775 done:
1776 new_st.op = EXEC_IF;
1777 new_st.expr1 = expr;
1778 return MATCH_YES;
1780 cleanup:
1781 gfc_free_expr (expr);
1782 return MATCH_ERROR;
1786 /* Free a gfc_iterator structure. */
1788 void
1789 gfc_free_iterator (gfc_iterator *iter, int flag)
1792 if (iter == NULL)
1793 return;
1795 gfc_free_expr (iter->var);
1796 gfc_free_expr (iter->start);
1797 gfc_free_expr (iter->end);
1798 gfc_free_expr (iter->step);
1800 if (flag)
1801 free (iter);
1805 /* Match a CRITICAL statement. */
1806 match
1807 gfc_match_critical (void)
1809 gfc_st_label *label = NULL;
1811 if (gfc_match_label () == MATCH_ERROR)
1812 return MATCH_ERROR;
1814 if (gfc_match (" critical") != MATCH_YES)
1815 return MATCH_NO;
1817 if (gfc_match_st_label (&label) == MATCH_ERROR)
1818 return MATCH_ERROR;
1820 if (gfc_match_eos () != MATCH_YES)
1822 gfc_syntax_error (ST_CRITICAL);
1823 return MATCH_ERROR;
1826 if (gfc_pure (NULL))
1828 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1829 return MATCH_ERROR;
1832 if (gfc_find_state (COMP_DO_CONCURRENT))
1834 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1835 "block");
1836 return MATCH_ERROR;
1839 gfc_unset_implicit_pure (NULL);
1841 if (!gfc_notify_std (GFC_STD_F2008, "CRITICAL statement at %C"))
1842 return MATCH_ERROR;
1844 if (flag_coarray == GFC_FCOARRAY_NONE)
1846 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
1847 "enable");
1848 return MATCH_ERROR;
1851 if (gfc_find_state (COMP_CRITICAL))
1853 gfc_error ("Nested CRITICAL block at %C");
1854 return MATCH_ERROR;
1857 new_st.op = EXEC_CRITICAL;
1859 if (label != NULL
1860 && !gfc_reference_st_label (label, ST_LABEL_TARGET))
1861 return MATCH_ERROR;
1863 return MATCH_YES;
1867 /* Match a BLOCK statement. */
1869 match
1870 gfc_match_block (void)
1872 match m;
1874 if (gfc_match_label () == MATCH_ERROR)
1875 return MATCH_ERROR;
1877 if (gfc_match (" block") != MATCH_YES)
1878 return MATCH_NO;
1880 /* For this to be a correct BLOCK statement, the line must end now. */
1881 m = gfc_match_eos ();
1882 if (m == MATCH_ERROR)
1883 return MATCH_ERROR;
1884 if (m == MATCH_NO)
1885 return MATCH_NO;
1887 return MATCH_YES;
1891 /* Match an ASSOCIATE statement. */
1893 match
1894 gfc_match_associate (void)
1896 if (gfc_match_label () == MATCH_ERROR)
1897 return MATCH_ERROR;
1899 if (gfc_match (" associate") != MATCH_YES)
1900 return MATCH_NO;
1902 /* Match the association list. */
1903 if (gfc_match_char ('(') != MATCH_YES)
1905 gfc_error ("Expected association list at %C");
1906 return MATCH_ERROR;
1908 new_st.ext.block.assoc = NULL;
1909 while (true)
1911 gfc_association_list* newAssoc = gfc_get_association_list ();
1912 gfc_association_list* a;
1914 /* Match the next association. */
1915 if (gfc_match (" %n =>", newAssoc->name) != MATCH_YES)
1917 gfc_error ("Expected association at %C");
1918 goto assocListError;
1921 if (gfc_match (" %e", &newAssoc->target) != MATCH_YES)
1923 /* Have another go, allowing for procedure pointer selectors. */
1924 gfc_matching_procptr_assignment = 1;
1925 if (gfc_match (" %e", &newAssoc->target) != MATCH_YES)
1927 gfc_error ("Invalid association target at %C");
1928 goto assocListError;
1930 gfc_matching_procptr_assignment = 0;
1932 newAssoc->where = gfc_current_locus;
1934 /* Check that the current name is not yet in the list. */
1935 for (a = new_st.ext.block.assoc; a; a = a->next)
1936 if (!strcmp (a->name, newAssoc->name))
1938 gfc_error ("Duplicate name %qs in association at %C",
1939 newAssoc->name);
1940 goto assocListError;
1943 /* The target expression must not be coindexed. */
1944 if (gfc_is_coindexed (newAssoc->target))
1946 gfc_error ("Association target at %C must not be coindexed");
1947 goto assocListError;
1950 /* The target expression cannot be a BOZ literal constant. */
1951 if (newAssoc->target->ts.type == BT_BOZ)
1953 gfc_error ("Association target at %L cannot be a BOZ literal "
1954 "constant", &newAssoc->target->where);
1955 goto assocListError;
1958 /* The `variable' field is left blank for now; because the target is not
1959 yet resolved, we can't use gfc_has_vector_subscript to determine it
1960 for now. This is set during resolution. */
1962 /* Put it into the list. */
1963 newAssoc->next = new_st.ext.block.assoc;
1964 new_st.ext.block.assoc = newAssoc;
1966 /* Try next one or end if closing parenthesis is found. */
1967 gfc_gobble_whitespace ();
1968 if (gfc_peek_char () == ')')
1969 break;
1970 if (gfc_match_char (',') != MATCH_YES)
1972 gfc_error ("Expected %<)%> or %<,%> at %C");
1973 return MATCH_ERROR;
1976 continue;
1978 assocListError:
1979 free (newAssoc);
1980 goto error;
1982 if (gfc_match_char (')') != MATCH_YES)
1984 /* This should never happen as we peek above. */
1985 gcc_unreachable ();
1988 if (gfc_match_eos () != MATCH_YES)
1990 gfc_error ("Junk after ASSOCIATE statement at %C");
1991 goto error;
1994 return MATCH_YES;
1996 error:
1997 gfc_free_association_list (new_st.ext.block.assoc);
1998 return MATCH_ERROR;
2002 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
2003 an accessible derived type. */
2005 static match
2006 match_derived_type_spec (gfc_typespec *ts)
2008 char name[GFC_MAX_SYMBOL_LEN + 1];
2009 locus old_locus;
2010 gfc_symbol *derived, *der_type;
2011 match m = MATCH_YES;
2012 gfc_actual_arglist *decl_type_param_list = NULL;
2013 bool is_pdt_template = false;
2015 old_locus = gfc_current_locus;
2017 if (gfc_match ("%n", name) != MATCH_YES)
2019 gfc_current_locus = old_locus;
2020 return MATCH_NO;
2023 gfc_find_symbol (name, NULL, 1, &derived);
2025 /* Match the PDT spec list, if there. */
2026 if (derived && derived->attr.flavor == FL_PROCEDURE)
2028 gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &der_type);
2029 is_pdt_template = der_type
2030 && der_type->attr.flavor == FL_DERIVED
2031 && der_type->attr.pdt_template;
2034 if (is_pdt_template)
2035 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
2037 if (m == MATCH_ERROR)
2039 gfc_free_actual_arglist (decl_type_param_list);
2040 return m;
2043 if (derived && derived->attr.flavor == FL_PROCEDURE && derived->attr.generic)
2044 derived = gfc_find_dt_in_generic (derived);
2046 /* If this is a PDT, find the specific instance. */
2047 if (m == MATCH_YES && is_pdt_template)
2049 gfc_namespace *old_ns;
2051 old_ns = gfc_current_ns;
2052 while (gfc_current_ns && gfc_current_ns->parent)
2053 gfc_current_ns = gfc_current_ns->parent;
2055 if (type_param_spec_list)
2056 gfc_free_actual_arglist (type_param_spec_list);
2057 m = gfc_get_pdt_instance (decl_type_param_list, &der_type,
2058 &type_param_spec_list);
2059 gfc_free_actual_arglist (decl_type_param_list);
2061 if (m != MATCH_YES)
2062 return m;
2063 derived = der_type;
2064 gcc_assert (!derived->attr.pdt_template && derived->attr.pdt_type);
2065 gfc_set_sym_referenced (derived);
2067 gfc_current_ns = old_ns;
2070 if (derived && derived->attr.flavor == FL_DERIVED)
2072 ts->type = BT_DERIVED;
2073 ts->u.derived = derived;
2074 return MATCH_YES;
2077 gfc_current_locus = old_locus;
2078 return MATCH_NO;
2082 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
2083 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
2084 It only includes the intrinsic types from the Fortran 2003 standard
2085 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
2086 the implicit_flag is not needed, so it was removed. Derived types are
2087 identified by their name alone. */
2089 match
2090 gfc_match_type_spec (gfc_typespec *ts)
2092 match m;
2093 locus old_locus;
2094 char c, name[GFC_MAX_SYMBOL_LEN + 1];
2096 gfc_clear_ts (ts);
2097 gfc_gobble_whitespace ();
2098 old_locus = gfc_current_locus;
2100 /* If c isn't [a-z], then return immediately. */
2101 c = gfc_peek_ascii_char ();
2102 if (!ISALPHA(c))
2103 return MATCH_NO;
2105 type_param_spec_list = NULL;
2107 if (match_derived_type_spec (ts) == MATCH_YES)
2109 /* Enforce F03:C401. */
2110 if (ts->u.derived->attr.abstract)
2112 gfc_error ("Derived type %qs at %L may not be ABSTRACT",
2113 ts->u.derived->name, &old_locus);
2114 return MATCH_ERROR;
2116 return MATCH_YES;
2119 if (gfc_match ("integer") == MATCH_YES)
2121 ts->type = BT_INTEGER;
2122 ts->kind = gfc_default_integer_kind;
2123 goto kind_selector;
2126 if (gfc_match ("double precision") == MATCH_YES)
2128 ts->type = BT_REAL;
2129 ts->kind = gfc_default_double_kind;
2130 return MATCH_YES;
2133 if (gfc_match ("complex") == MATCH_YES)
2135 ts->type = BT_COMPLEX;
2136 ts->kind = gfc_default_complex_kind;
2137 goto kind_selector;
2140 if (gfc_match ("character") == MATCH_YES)
2142 ts->type = BT_CHARACTER;
2144 m = gfc_match_char_spec (ts);
2146 if (m == MATCH_NO)
2147 m = MATCH_YES;
2149 return m;
2152 /* REAL is a real pain because it can be a type, intrinsic subprogram,
2153 or list item in a type-list of an OpenMP reduction clause. Need to
2154 differentiate REAL([KIND]=scalar-int-initialization-expr) from
2155 REAL(A,[KIND]) and REAL(KIND,A). Logically, when this code was
2156 written the use of LOGICAL as a type-spec or intrinsic subprogram
2157 was overlooked. */
2159 m = gfc_match (" %n", name);
2160 if (m == MATCH_YES
2161 && (strcmp (name, "real") == 0 || strcmp (name, "logical") == 0))
2163 char c;
2164 gfc_expr *e;
2165 locus where;
2167 if (*name == 'r')
2169 ts->type = BT_REAL;
2170 ts->kind = gfc_default_real_kind;
2172 else
2174 ts->type = BT_LOGICAL;
2175 ts->kind = gfc_default_logical_kind;
2178 gfc_gobble_whitespace ();
2180 /* Prevent REAL*4, etc. */
2181 c = gfc_peek_ascii_char ();
2182 if (c == '*')
2184 gfc_error ("Invalid type-spec at %C");
2185 return MATCH_ERROR;
2188 /* Found leading colon in REAL::, a trailing ')' in for example
2189 TYPE IS (REAL), or REAL, for an OpenMP list-item. */
2190 if (c == ':' || c == ')' || (flag_openmp && c == ','))
2191 return MATCH_YES;
2193 /* Found something other than the opening '(' in REAL(... */
2194 if (c != '(')
2195 return MATCH_NO;
2196 else
2197 gfc_next_char (); /* Burn the '('. */
2199 /* Look for the optional KIND=. */
2200 where = gfc_current_locus;
2201 m = gfc_match ("%n", name);
2202 if (m == MATCH_YES)
2204 gfc_gobble_whitespace ();
2205 c = gfc_next_char ();
2206 if (c == '=')
2208 if (strcmp(name, "a") == 0 || strcmp(name, "l") == 0)
2209 return MATCH_NO;
2210 else if (strcmp(name, "kind") == 0)
2211 goto found;
2212 else
2213 return MATCH_ERROR;
2215 else
2216 gfc_current_locus = where;
2218 else
2219 gfc_current_locus = where;
2221 found:
2223 m = gfc_match_expr (&e);
2224 if (m == MATCH_NO || m == MATCH_ERROR)
2225 return m;
2227 /* If a comma appears, it is an intrinsic subprogram. */
2228 gfc_gobble_whitespace ();
2229 c = gfc_peek_ascii_char ();
2230 if (c == ',')
2232 gfc_free_expr (e);
2233 return MATCH_NO;
2236 /* If ')' appears, we have REAL(initialization-expr), here check for
2237 a scalar integer initialization-expr and valid kind parameter. */
2238 if (c == ')')
2240 bool ok = true;
2241 if (e->expr_type != EXPR_CONSTANT && e->expr_type != EXPR_VARIABLE)
2242 ok = gfc_reduce_init_expr (e);
2243 if (!ok || e->ts.type != BT_INTEGER || e->rank > 0)
2245 gfc_free_expr (e);
2246 return MATCH_NO;
2249 if (e->expr_type != EXPR_CONSTANT)
2250 goto ohno;
2252 gfc_next_char (); /* Burn the ')'. */
2253 ts->kind = (int) mpz_get_si (e->value.integer);
2254 if (gfc_validate_kind (ts->type, ts->kind , true) == -1)
2256 gfc_error ("Invalid type-spec at %C");
2257 return MATCH_ERROR;
2260 gfc_free_expr (e);
2262 return MATCH_YES;
2266 ohno:
2268 /* If a type is not matched, simply return MATCH_NO. */
2269 gfc_current_locus = old_locus;
2270 return MATCH_NO;
2272 kind_selector:
2274 gfc_gobble_whitespace ();
2276 /* This prevents INTEGER*4, etc. */
2277 if (gfc_peek_ascii_char () == '*')
2279 gfc_error ("Invalid type-spec at %C");
2280 return MATCH_ERROR;
2283 m = gfc_match_kind_spec (ts, false);
2285 /* No kind specifier found. */
2286 if (m == MATCH_NO)
2287 m = MATCH_YES;
2289 return m;
2293 /******************** FORALL subroutines ********************/
2295 /* Free a list of FORALL iterators. */
2297 void
2298 gfc_free_forall_iterator (gfc_forall_iterator *iter)
2300 gfc_forall_iterator *next;
2302 while (iter)
2304 next = iter->next;
2305 gfc_free_expr (iter->var);
2306 gfc_free_expr (iter->start);
2307 gfc_free_expr (iter->end);
2308 gfc_free_expr (iter->stride);
2309 free (iter);
2310 iter = next;
2315 /* Match an iterator as part of a FORALL statement. The format is:
2317 <var> = <start>:<end>[:<stride>]
2319 On MATCH_NO, the caller tests for the possibility that there is a
2320 scalar mask expression. */
2322 static match
2323 match_forall_iterator (gfc_forall_iterator **result)
2325 gfc_forall_iterator *iter;
2326 locus where;
2327 match m;
2329 where = gfc_current_locus;
2330 iter = XCNEW (gfc_forall_iterator);
2332 m = gfc_match_expr (&iter->var);
2333 if (m != MATCH_YES)
2334 goto cleanup;
2336 if (gfc_match_char ('=') != MATCH_YES
2337 || iter->var->expr_type != EXPR_VARIABLE)
2339 m = MATCH_NO;
2340 goto cleanup;
2343 m = gfc_match_expr (&iter->start);
2344 if (m != MATCH_YES)
2345 goto cleanup;
2347 if (gfc_match_char (':') != MATCH_YES)
2348 goto syntax;
2350 m = gfc_match_expr (&iter->end);
2351 if (m == MATCH_NO)
2352 goto syntax;
2353 if (m == MATCH_ERROR)
2354 goto cleanup;
2356 if (gfc_match_char (':') == MATCH_NO)
2357 iter->stride = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2358 else
2360 m = gfc_match_expr (&iter->stride);
2361 if (m == MATCH_NO)
2362 goto syntax;
2363 if (m == MATCH_ERROR)
2364 goto cleanup;
2367 /* Mark the iteration variable's symbol as used as a FORALL index. */
2368 iter->var->symtree->n.sym->forall_index = true;
2370 *result = iter;
2371 return MATCH_YES;
2373 syntax:
2374 gfc_error ("Syntax error in FORALL iterator at %C");
2375 m = MATCH_ERROR;
2377 cleanup:
2379 gfc_current_locus = where;
2380 gfc_free_forall_iterator (iter);
2381 return m;
2385 /* Match the header of a FORALL statement. */
2387 static match
2388 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
2390 gfc_forall_iterator *head, *tail, *new_iter;
2391 gfc_expr *msk;
2392 match m;
2394 gfc_gobble_whitespace ();
2396 head = tail = NULL;
2397 msk = NULL;
2399 if (gfc_match_char ('(') != MATCH_YES)
2400 return MATCH_NO;
2402 m = match_forall_iterator (&new_iter);
2403 if (m == MATCH_ERROR)
2404 goto cleanup;
2405 if (m == MATCH_NO)
2406 goto syntax;
2408 head = tail = new_iter;
2410 for (;;)
2412 if (gfc_match_char (',') != MATCH_YES)
2413 break;
2415 m = match_forall_iterator (&new_iter);
2416 if (m == MATCH_ERROR)
2417 goto cleanup;
2419 if (m == MATCH_YES)
2421 tail->next = new_iter;
2422 tail = new_iter;
2423 continue;
2426 /* Have to have a mask expression. */
2428 m = gfc_match_expr (&msk);
2429 if (m == MATCH_NO)
2430 goto syntax;
2431 if (m == MATCH_ERROR)
2432 goto cleanup;
2434 break;
2437 if (gfc_match_char (')') == MATCH_NO)
2438 goto syntax;
2440 *phead = head;
2441 *mask = msk;
2442 return MATCH_YES;
2444 syntax:
2445 gfc_syntax_error (ST_FORALL);
2447 cleanup:
2448 gfc_free_expr (msk);
2449 gfc_free_forall_iterator (head);
2451 return MATCH_ERROR;
2454 /* Match the rest of a simple FORALL statement that follows an
2455 IF statement. */
2457 static match
2458 match_simple_forall (void)
2460 gfc_forall_iterator *head;
2461 gfc_expr *mask;
2462 gfc_code *c;
2463 match m;
2465 mask = NULL;
2466 head = NULL;
2467 c = NULL;
2469 m = match_forall_header (&head, &mask);
2471 if (m == MATCH_NO)
2472 goto syntax;
2473 if (m != MATCH_YES)
2474 goto cleanup;
2476 m = gfc_match_assignment ();
2478 if (m == MATCH_ERROR)
2479 goto cleanup;
2480 if (m == MATCH_NO)
2482 m = gfc_match_pointer_assignment ();
2483 if (m == MATCH_ERROR)
2484 goto cleanup;
2485 if (m == MATCH_NO)
2486 goto syntax;
2489 c = XCNEW (gfc_code);
2490 *c = new_st;
2491 c->loc = gfc_current_locus;
2493 if (gfc_match_eos () != MATCH_YES)
2494 goto syntax;
2496 gfc_clear_new_st ();
2497 new_st.op = EXEC_FORALL;
2498 new_st.expr1 = mask;
2499 new_st.ext.forall_iterator = head;
2500 new_st.block = gfc_get_code (EXEC_FORALL);
2501 new_st.block->next = c;
2503 return MATCH_YES;
2505 syntax:
2506 gfc_syntax_error (ST_FORALL);
2508 cleanup:
2509 gfc_free_forall_iterator (head);
2510 gfc_free_expr (mask);
2512 return MATCH_ERROR;
2516 /* Match a FORALL statement. */
2518 match
2519 gfc_match_forall (gfc_statement *st)
2521 gfc_forall_iterator *head;
2522 gfc_expr *mask;
2523 gfc_code *c;
2524 match m0, m;
2526 head = NULL;
2527 mask = NULL;
2528 c = NULL;
2530 m0 = gfc_match_label ();
2531 if (m0 == MATCH_ERROR)
2532 return MATCH_ERROR;
2534 m = gfc_match (" forall");
2535 if (m != MATCH_YES)
2536 return m;
2538 m = match_forall_header (&head, &mask);
2539 if (m == MATCH_ERROR)
2540 goto cleanup;
2541 if (m == MATCH_NO)
2542 goto syntax;
2544 if (gfc_match_eos () == MATCH_YES)
2546 *st = ST_FORALL_BLOCK;
2547 new_st.op = EXEC_FORALL;
2548 new_st.expr1 = mask;
2549 new_st.ext.forall_iterator = head;
2550 return MATCH_YES;
2553 m = gfc_match_assignment ();
2554 if (m == MATCH_ERROR)
2555 goto cleanup;
2556 if (m == MATCH_NO)
2558 m = gfc_match_pointer_assignment ();
2559 if (m == MATCH_ERROR)
2560 goto cleanup;
2561 if (m == MATCH_NO)
2562 goto syntax;
2565 c = XCNEW (gfc_code);
2566 *c = new_st;
2567 c->loc = gfc_current_locus;
2569 gfc_clear_new_st ();
2570 new_st.op = EXEC_FORALL;
2571 new_st.expr1 = mask;
2572 new_st.ext.forall_iterator = head;
2573 new_st.block = gfc_get_code (EXEC_FORALL);
2574 new_st.block->next = c;
2576 *st = ST_FORALL;
2577 return MATCH_YES;
2579 syntax:
2580 gfc_syntax_error (ST_FORALL);
2582 cleanup:
2583 gfc_free_forall_iterator (head);
2584 gfc_free_expr (mask);
2585 gfc_free_statements (c);
2586 return MATCH_NO;
2590 /* Match a DO statement. */
2592 match
2593 gfc_match_do (void)
2595 gfc_iterator iter, *ip;
2596 locus old_loc;
2597 gfc_st_label *label;
2598 match m;
2600 old_loc = gfc_current_locus;
2602 memset (&iter, '\0', sizeof (gfc_iterator));
2603 label = NULL;
2605 m = gfc_match_label ();
2606 if (m == MATCH_ERROR)
2607 return m;
2609 if (gfc_match (" do") != MATCH_YES)
2610 return MATCH_NO;
2612 m = gfc_match_st_label (&label);
2613 if (m == MATCH_ERROR)
2614 goto cleanup;
2616 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2618 if (gfc_match_eos () == MATCH_YES)
2620 iter.end = gfc_get_logical_expr (gfc_default_logical_kind, NULL, true);
2621 new_st.op = EXEC_DO_WHILE;
2622 goto done;
2625 /* Match an optional comma, if no comma is found, a space is obligatory. */
2626 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
2627 return MATCH_NO;
2629 /* Check for balanced parens. */
2631 if (gfc_match_parens () == MATCH_ERROR)
2632 return MATCH_ERROR;
2634 if (gfc_match (" concurrent") == MATCH_YES)
2636 gfc_forall_iterator *head;
2637 gfc_expr *mask;
2639 if (!gfc_notify_std (GFC_STD_F2008, "DO CONCURRENT construct at %C"))
2640 return MATCH_ERROR;
2643 mask = NULL;
2644 head = NULL;
2645 m = match_forall_header (&head, &mask);
2647 if (m == MATCH_NO)
2648 return m;
2649 if (m == MATCH_ERROR)
2650 goto concurr_cleanup;
2652 if (gfc_match_eos () != MATCH_YES)
2653 goto concurr_cleanup;
2655 if (label != NULL
2656 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2657 goto concurr_cleanup;
2659 new_st.label1 = label;
2660 new_st.op = EXEC_DO_CONCURRENT;
2661 new_st.expr1 = mask;
2662 new_st.ext.forall_iterator = head;
2664 return MATCH_YES;
2666 concurr_cleanup:
2667 gfc_syntax_error (ST_DO);
2668 gfc_free_expr (mask);
2669 gfc_free_forall_iterator (head);
2670 return MATCH_ERROR;
2673 /* See if we have a DO WHILE. */
2674 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
2676 new_st.op = EXEC_DO_WHILE;
2677 goto done;
2680 /* The abortive DO WHILE may have done something to the symbol
2681 table, so we start over. */
2682 gfc_undo_symbols ();
2683 gfc_current_locus = old_loc;
2685 gfc_match_label (); /* This won't error. */
2686 gfc_match (" do "); /* This will work. */
2688 gfc_match_st_label (&label); /* Can't error out. */
2689 gfc_match_char (','); /* Optional comma. */
2691 m = gfc_match_iterator (&iter, 0);
2692 if (m == MATCH_NO)
2693 return MATCH_NO;
2694 if (m == MATCH_ERROR)
2695 goto cleanup;
2697 iter.var->symtree->n.sym->attr.implied_index = 0;
2698 gfc_check_do_variable (iter.var->symtree);
2700 if (gfc_match_eos () != MATCH_YES)
2702 gfc_syntax_error (ST_DO);
2703 goto cleanup;
2706 new_st.op = EXEC_DO;
2708 done:
2709 if (label != NULL
2710 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2711 goto cleanup;
2713 new_st.label1 = label;
2715 if (new_st.op == EXEC_DO_WHILE)
2716 new_st.expr1 = iter.end;
2717 else
2719 new_st.ext.iterator = ip = gfc_get_iterator ();
2720 *ip = iter;
2723 return MATCH_YES;
2725 cleanup:
2726 gfc_free_iterator (&iter, 0);
2728 return MATCH_ERROR;
2732 /* Match an EXIT or CYCLE statement. */
2734 static match
2735 match_exit_cycle (gfc_statement st, gfc_exec_op op)
2737 gfc_state_data *p, *o;
2738 gfc_symbol *sym;
2739 match m;
2740 int cnt;
2742 if (gfc_match_eos () == MATCH_YES)
2743 sym = NULL;
2744 else
2746 char name[GFC_MAX_SYMBOL_LEN + 1];
2747 gfc_symtree* stree;
2749 m = gfc_match ("% %n%t", name);
2750 if (m == MATCH_ERROR)
2751 return MATCH_ERROR;
2752 if (m == MATCH_NO)
2754 gfc_syntax_error (st);
2755 return MATCH_ERROR;
2758 /* Find the corresponding symbol. If there's a BLOCK statement
2759 between here and the label, it is not in gfc_current_ns but a parent
2760 namespace! */
2761 stree = gfc_find_symtree_in_proc (name, gfc_current_ns);
2762 if (!stree)
2764 gfc_error ("Name %qs in %s statement at %C is unknown",
2765 name, gfc_ascii_statement (st));
2766 return MATCH_ERROR;
2769 sym = stree->n.sym;
2770 if (sym->attr.flavor != FL_LABEL)
2772 gfc_error ("Name %qs in %s statement at %C is not a construct name",
2773 name, gfc_ascii_statement (st));
2774 return MATCH_ERROR;
2778 /* Find the loop specified by the label (or lack of a label). */
2779 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
2780 if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
2781 o = p;
2782 else if (p->state == COMP_CRITICAL)
2784 gfc_error("%s statement at %C leaves CRITICAL construct",
2785 gfc_ascii_statement (st));
2786 return MATCH_ERROR;
2788 else if (p->state == COMP_DO_CONCURRENT
2789 && (op == EXEC_EXIT || (sym && sym != p->sym)))
2791 /* F2008, C821 & C845. */
2792 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2793 gfc_ascii_statement (st));
2794 return MATCH_ERROR;
2796 else if ((sym && sym == p->sym)
2797 || (!sym && (p->state == COMP_DO
2798 || p->state == COMP_DO_CONCURRENT)))
2799 break;
2801 if (p == NULL)
2803 if (sym == NULL)
2804 gfc_error ("%s statement at %C is not within a construct",
2805 gfc_ascii_statement (st));
2806 else
2807 gfc_error ("%s statement at %C is not within construct %qs",
2808 gfc_ascii_statement (st), sym->name);
2810 return MATCH_ERROR;
2813 /* Special checks for EXIT from non-loop constructs. */
2814 switch (p->state)
2816 case COMP_DO:
2817 case COMP_DO_CONCURRENT:
2818 break;
2820 case COMP_CRITICAL:
2821 /* This is already handled above. */
2822 gcc_unreachable ();
2824 case COMP_ASSOCIATE:
2825 case COMP_BLOCK:
2826 case COMP_IF:
2827 case COMP_SELECT:
2828 case COMP_SELECT_TYPE:
2829 case COMP_SELECT_RANK:
2830 gcc_assert (sym);
2831 if (op == EXEC_CYCLE)
2833 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2834 " construct %qs", sym->name);
2835 return MATCH_ERROR;
2837 gcc_assert (op == EXEC_EXIT);
2838 if (!gfc_notify_std (GFC_STD_F2008, "EXIT statement with no"
2839 " do-construct-name at %C"))
2840 return MATCH_ERROR;
2841 break;
2843 default:
2844 gfc_error ("%s statement at %C is not applicable to construct %qs",
2845 gfc_ascii_statement (st), sym->name);
2846 return MATCH_ERROR;
2849 if (o != NULL)
2851 gfc_error (is_oacc (p)
2852 ? G_("%s statement at %C leaving OpenACC structured block")
2853 : G_("%s statement at %C leaving OpenMP structured block"),
2854 gfc_ascii_statement (st));
2855 return MATCH_ERROR;
2858 for (o = p, cnt = 0; o->state == COMP_DO && o->previous != NULL; cnt++)
2859 o = o->previous;
2860 if (cnt > 0
2861 && o != NULL
2862 && o->state == COMP_OMP_STRUCTURED_BLOCK
2863 && (o->head->op == EXEC_OACC_LOOP
2864 || o->head->op == EXEC_OACC_KERNELS_LOOP
2865 || o->head->op == EXEC_OACC_PARALLEL_LOOP
2866 || o->head->op == EXEC_OACC_SERIAL_LOOP))
2868 int collapse = 1;
2869 gcc_assert (o->head->next != NULL
2870 && (o->head->next->op == EXEC_DO
2871 || o->head->next->op == EXEC_DO_WHILE)
2872 && o->previous != NULL
2873 && o->previous->tail->op == o->head->op);
2874 if (o->previous->tail->ext.omp_clauses != NULL)
2876 /* Both collapsed and tiled loops are lowered the same way, but are not
2877 compatible. In gfc_trans_omp_do, the tile is prioritized. */
2878 if (o->previous->tail->ext.omp_clauses->tile_list)
2880 collapse = 0;
2881 gfc_expr_list *el = o->previous->tail->ext.omp_clauses->tile_list;
2882 for ( ; el; el = el->next)
2883 ++collapse;
2885 else if (o->previous->tail->ext.omp_clauses->collapse > 1)
2886 collapse = o->previous->tail->ext.omp_clauses->collapse;
2888 if (st == ST_EXIT && cnt <= collapse)
2890 gfc_error ("EXIT statement at %C terminating !$ACC LOOP loop");
2891 return MATCH_ERROR;
2893 if (st == ST_CYCLE && cnt < collapse)
2895 gfc_error (o->previous->tail->ext.omp_clauses->tile_list
2896 ? G_("CYCLE statement at %C to non-innermost tiled"
2897 " !$ACC LOOP loop")
2898 : G_("CYCLE statement at %C to non-innermost collapsed"
2899 " !$ACC LOOP loop"));
2900 return MATCH_ERROR;
2903 if (cnt > 0
2904 && o != NULL
2905 && (o->state == COMP_OMP_STRUCTURED_BLOCK)
2906 && (o->head->op == EXEC_OMP_DO
2907 || o->head->op == EXEC_OMP_PARALLEL_DO
2908 || o->head->op == EXEC_OMP_SIMD
2909 || o->head->op == EXEC_OMP_DO_SIMD
2910 || o->head->op == EXEC_OMP_PARALLEL_DO_SIMD))
2912 int count = 1;
2913 gcc_assert (o->head->next != NULL
2914 && (o->head->next->op == EXEC_DO
2915 || o->head->next->op == EXEC_DO_WHILE)
2916 && o->previous != NULL
2917 && o->previous->tail->op == o->head->op);
2918 if (o->previous->tail->ext.omp_clauses != NULL)
2920 if (o->previous->tail->ext.omp_clauses->collapse > 1)
2921 count = o->previous->tail->ext.omp_clauses->collapse;
2922 if (o->previous->tail->ext.omp_clauses->orderedc)
2923 count = o->previous->tail->ext.omp_clauses->orderedc;
2925 if (st == ST_EXIT && cnt <= count)
2927 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2928 return MATCH_ERROR;
2930 if (st == ST_CYCLE && cnt < count)
2932 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2933 " !$OMP DO loop");
2934 return MATCH_ERROR;
2938 /* Save the first statement in the construct - needed by the backend. */
2939 new_st.ext.which_construct = p->construct;
2941 new_st.op = op;
2943 return MATCH_YES;
2947 /* Match the EXIT statement. */
2949 match
2950 gfc_match_exit (void)
2952 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
2956 /* Match the CYCLE statement. */
2958 match
2959 gfc_match_cycle (void)
2961 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
2965 /* Match a stop-code after an (ERROR) STOP or PAUSE statement. The
2966 requirements for a stop-code differ in the standards.
2968 Fortran 95 has
2970 R840 stop-stmt is STOP [ stop-code ]
2971 R841 stop-code is scalar-char-constant
2972 or digit [ digit [ digit [ digit [ digit ] ] ] ]
2974 Fortran 2003 matches Fortran 95 except R840 and R841 are now R849 and R850.
2975 Fortran 2008 has
2977 R855 stop-stmt is STOP [ stop-code ]
2978 R856 allstop-stmt is ALL STOP [ stop-code ]
2979 R857 stop-code is scalar-default-char-constant-expr
2980 or scalar-int-constant-expr
2982 For free-form source code, all standards contain a statement of the form:
2984 A blank shall be used to separate names, constants, or labels from
2985 adjacent keywords, names, constants, or labels.
2987 A stop-code is not a name, constant, or label. So, under Fortran 95 and 2003,
2989 STOP123
2991 is valid, but it is invalid Fortran 2008. */
2993 static match
2994 gfc_match_stopcode (gfc_statement st)
2996 gfc_expr *e = NULL;
2997 match m;
2998 bool f95, f03, f08;
3000 /* Set f95 for -std=f95. */
3001 f95 = (gfc_option.allow_std == GFC_STD_OPT_F95);
3003 /* Set f03 for -std=f2003. */
3004 f03 = (gfc_option.allow_std == GFC_STD_OPT_F03);
3006 /* Set f08 for -std=f2008. */
3007 f08 = (gfc_option.allow_std == GFC_STD_OPT_F08);
3009 /* Look for a blank between STOP and the stop-code for F2008 or later. */
3010 if (gfc_current_form != FORM_FIXED && !(f95 || f03))
3012 char c = gfc_peek_ascii_char ();
3014 /* Look for end-of-statement. There is no stop-code. */
3015 if (c == '\n' || c == '!' || c == ';')
3016 goto done;
3018 if (c != ' ')
3020 gfc_error ("Blank required in %s statement near %C",
3021 gfc_ascii_statement (st));
3022 return MATCH_ERROR;
3026 if (gfc_match_eos () != MATCH_YES)
3028 int stopcode;
3029 locus old_locus;
3031 /* First look for the F95 or F2003 digit [...] construct. */
3032 old_locus = gfc_current_locus;
3033 m = gfc_match_small_int (&stopcode);
3034 if (m == MATCH_YES && (f95 || f03))
3036 if (stopcode < 0)
3038 gfc_error ("STOP code at %C cannot be negative");
3039 return MATCH_ERROR;
3042 if (stopcode > 99999)
3044 gfc_error ("STOP code at %C contains too many digits");
3045 return MATCH_ERROR;
3049 /* Reset the locus and now load gfc_expr. */
3050 gfc_current_locus = old_locus;
3051 m = gfc_match_expr (&e);
3052 if (m == MATCH_ERROR)
3053 goto cleanup;
3054 if (m == MATCH_NO)
3055 goto syntax;
3057 if (gfc_match_eos () != MATCH_YES)
3058 goto syntax;
3061 if (gfc_pure (NULL))
3063 if (st == ST_ERROR_STOP)
3065 if (!gfc_notify_std (GFC_STD_F2018, "%s statement at %C in PURE "
3066 "procedure", gfc_ascii_statement (st)))
3067 goto cleanup;
3069 else
3071 gfc_error ("%s statement not allowed in PURE procedure at %C",
3072 gfc_ascii_statement (st));
3073 goto cleanup;
3077 gfc_unset_implicit_pure (NULL);
3079 if (st == ST_STOP && gfc_find_state (COMP_CRITICAL))
3081 gfc_error ("Image control statement STOP at %C in CRITICAL block");
3082 goto cleanup;
3084 if (st == ST_STOP && gfc_find_state (COMP_DO_CONCURRENT))
3086 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
3087 goto cleanup;
3090 if (e != NULL)
3092 if (!gfc_simplify_expr (e, 0))
3093 goto cleanup;
3095 /* Test for F95 and F2003 style STOP stop-code. */
3096 if (e->expr_type != EXPR_CONSTANT && (f95 || f03))
3098 gfc_error ("STOP code at %L must be a scalar CHARACTER constant "
3099 "or digit[digit[digit[digit[digit]]]]", &e->where);
3100 goto cleanup;
3103 /* Use the machinery for an initialization expression to reduce the
3104 stop-code to a constant. */
3105 gfc_reduce_init_expr (e);
3107 /* Test for F2008 style STOP stop-code. */
3108 if (e->expr_type != EXPR_CONSTANT && f08)
3110 gfc_error ("STOP code at %L must be a scalar default CHARACTER or "
3111 "INTEGER constant expression", &e->where);
3112 goto cleanup;
3115 if (!(e->ts.type == BT_CHARACTER || e->ts.type == BT_INTEGER))
3117 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
3118 &e->where);
3119 goto cleanup;
3122 if (e->rank != 0)
3124 gfc_error ("STOP code at %L must be scalar", &e->where);
3125 goto cleanup;
3128 if (e->ts.type == BT_CHARACTER
3129 && e->ts.kind != gfc_default_character_kind)
3131 gfc_error ("STOP code at %L must be default character KIND=%d",
3132 &e->where, (int) gfc_default_character_kind);
3133 goto cleanup;
3136 if (e->ts.type == BT_INTEGER && e->ts.kind != gfc_default_integer_kind)
3138 gfc_error ("STOP code at %L must be default integer KIND=%d",
3139 &e->where, (int) gfc_default_integer_kind);
3140 goto cleanup;
3144 done:
3146 switch (st)
3148 case ST_STOP:
3149 new_st.op = EXEC_STOP;
3150 break;
3151 case ST_ERROR_STOP:
3152 new_st.op = EXEC_ERROR_STOP;
3153 break;
3154 case ST_PAUSE:
3155 new_st.op = EXEC_PAUSE;
3156 break;
3157 default:
3158 gcc_unreachable ();
3161 new_st.expr1 = e;
3162 new_st.ext.stop_code = -1;
3164 return MATCH_YES;
3166 syntax:
3167 gfc_syntax_error (st);
3169 cleanup:
3171 gfc_free_expr (e);
3172 return MATCH_ERROR;
3176 /* Match the (deprecated) PAUSE statement. */
3178 match
3179 gfc_match_pause (void)
3181 match m;
3183 m = gfc_match_stopcode (ST_PAUSE);
3184 if (m == MATCH_YES)
3186 if (!gfc_notify_std (GFC_STD_F95_DEL, "PAUSE statement at %C"))
3187 m = MATCH_ERROR;
3189 return m;
3193 /* Match the STOP statement. */
3195 match
3196 gfc_match_stop (void)
3198 return gfc_match_stopcode (ST_STOP);
3202 /* Match the ERROR STOP statement. */
3204 match
3205 gfc_match_error_stop (void)
3207 if (!gfc_notify_std (GFC_STD_F2008, "ERROR STOP statement at %C"))
3208 return MATCH_ERROR;
3210 return gfc_match_stopcode (ST_ERROR_STOP);
3213 /* Match EVENT POST/WAIT statement. Syntax:
3214 EVENT POST ( event-variable [, sync-stat-list] )
3215 EVENT WAIT ( event-variable [, wait-spec-list] )
3216 with
3217 wait-spec-list is sync-stat-list or until-spec
3218 until-spec is UNTIL_COUNT = scalar-int-expr
3219 sync-stat is STAT= or ERRMSG=. */
3221 static match
3222 event_statement (gfc_statement st)
3224 match m;
3225 gfc_expr *tmp, *eventvar, *until_count, *stat, *errmsg;
3226 bool saw_until_count, saw_stat, saw_errmsg;
3228 tmp = eventvar = until_count = stat = errmsg = NULL;
3229 saw_until_count = saw_stat = saw_errmsg = false;
3231 if (gfc_pure (NULL))
3233 gfc_error ("Image control statement EVENT %s at %C in PURE procedure",
3234 st == ST_EVENT_POST ? "POST" : "WAIT");
3235 return MATCH_ERROR;
3238 gfc_unset_implicit_pure (NULL);
3240 if (flag_coarray == GFC_FCOARRAY_NONE)
3242 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3243 return MATCH_ERROR;
3246 if (gfc_find_state (COMP_CRITICAL))
3248 gfc_error ("Image control statement EVENT %s at %C in CRITICAL block",
3249 st == ST_EVENT_POST ? "POST" : "WAIT");
3250 return MATCH_ERROR;
3253 if (gfc_find_state (COMP_DO_CONCURRENT))
3255 gfc_error ("Image control statement EVENT %s at %C in DO CONCURRENT "
3256 "block", st == ST_EVENT_POST ? "POST" : "WAIT");
3257 return MATCH_ERROR;
3260 if (gfc_match_char ('(') != MATCH_YES)
3261 goto syntax;
3263 if (gfc_match ("%e", &eventvar) != MATCH_YES)
3264 goto syntax;
3265 m = gfc_match_char (',');
3266 if (m == MATCH_ERROR)
3267 goto syntax;
3268 if (m == MATCH_NO)
3270 m = gfc_match_char (')');
3271 if (m == MATCH_YES)
3272 goto done;
3273 goto syntax;
3276 for (;;)
3278 m = gfc_match (" stat = %v", &tmp);
3279 if (m == MATCH_ERROR)
3280 goto syntax;
3281 if (m == MATCH_YES)
3283 if (saw_stat)
3285 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
3286 goto cleanup;
3288 stat = tmp;
3289 saw_stat = true;
3291 m = gfc_match_char (',');
3292 if (m == MATCH_YES)
3293 continue;
3295 tmp = NULL;
3296 break;
3299 m = gfc_match (" errmsg = %v", &tmp);
3300 if (m == MATCH_ERROR)
3301 goto syntax;
3302 if (m == MATCH_YES)
3304 if (saw_errmsg)
3306 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
3307 goto cleanup;
3309 errmsg = tmp;
3310 saw_errmsg = true;
3312 m = gfc_match_char (',');
3313 if (m == MATCH_YES)
3314 continue;
3316 tmp = NULL;
3317 break;
3320 m = gfc_match (" until_count = %e", &tmp);
3321 if (m == MATCH_ERROR || st == ST_EVENT_POST)
3322 goto syntax;
3323 if (m == MATCH_YES)
3325 if (saw_until_count)
3327 gfc_error ("Redundant UNTIL_COUNT tag found at %L",
3328 &tmp->where);
3329 goto cleanup;
3331 until_count = tmp;
3332 saw_until_count = true;
3334 m = gfc_match_char (',');
3335 if (m == MATCH_YES)
3336 continue;
3338 tmp = NULL;
3339 break;
3342 break;
3345 if (m == MATCH_ERROR)
3346 goto syntax;
3348 if (gfc_match (" )%t") != MATCH_YES)
3349 goto syntax;
3351 done:
3352 switch (st)
3354 case ST_EVENT_POST:
3355 new_st.op = EXEC_EVENT_POST;
3356 break;
3357 case ST_EVENT_WAIT:
3358 new_st.op = EXEC_EVENT_WAIT;
3359 break;
3360 default:
3361 gcc_unreachable ();
3364 new_st.expr1 = eventvar;
3365 new_st.expr2 = stat;
3366 new_st.expr3 = errmsg;
3367 new_st.expr4 = until_count;
3369 return MATCH_YES;
3371 syntax:
3372 gfc_syntax_error (st);
3374 cleanup:
3375 if (until_count != tmp)
3376 gfc_free_expr (until_count);
3377 if (errmsg != tmp)
3378 gfc_free_expr (errmsg);
3379 if (stat != tmp)
3380 gfc_free_expr (stat);
3382 gfc_free_expr (tmp);
3383 gfc_free_expr (eventvar);
3385 return MATCH_ERROR;
3390 match
3391 gfc_match_event_post (void)
3393 if (!gfc_notify_std (GFC_STD_F2018, "EVENT POST statement at %C"))
3394 return MATCH_ERROR;
3396 return event_statement (ST_EVENT_POST);
3400 match
3401 gfc_match_event_wait (void)
3403 if (!gfc_notify_std (GFC_STD_F2018, "EVENT WAIT statement at %C"))
3404 return MATCH_ERROR;
3406 return event_statement (ST_EVENT_WAIT);
3410 /* Match a FAIL IMAGE statement. */
3412 match
3413 gfc_match_fail_image (void)
3415 if (!gfc_notify_std (GFC_STD_F2018, "FAIL IMAGE statement at %C"))
3416 return MATCH_ERROR;
3418 if (gfc_match_char ('(') == MATCH_YES)
3419 goto syntax;
3421 new_st.op = EXEC_FAIL_IMAGE;
3423 return MATCH_YES;
3425 syntax:
3426 gfc_syntax_error (ST_FAIL_IMAGE);
3428 return MATCH_ERROR;
3431 /* Match a FORM TEAM statement. */
3433 match
3434 gfc_match_form_team (void)
3436 match m;
3437 gfc_expr *teamid,*team;
3439 if (!gfc_notify_std (GFC_STD_F2018, "FORM TEAM statement at %C"))
3440 return MATCH_ERROR;
3442 if (gfc_match_char ('(') == MATCH_NO)
3443 goto syntax;
3445 new_st.op = EXEC_FORM_TEAM;
3447 if (gfc_match ("%e", &teamid) != MATCH_YES)
3448 goto syntax;
3449 m = gfc_match_char (',');
3450 if (m == MATCH_ERROR)
3451 goto syntax;
3452 if (gfc_match ("%e", &team) != MATCH_YES)
3453 goto syntax;
3455 m = gfc_match_char (')');
3456 if (m == MATCH_NO)
3457 goto syntax;
3459 new_st.expr1 = teamid;
3460 new_st.expr2 = team;
3462 return MATCH_YES;
3464 syntax:
3465 gfc_syntax_error (ST_FORM_TEAM);
3467 return MATCH_ERROR;
3470 /* Match a CHANGE TEAM statement. */
3472 match
3473 gfc_match_change_team (void)
3475 match m;
3476 gfc_expr *team;
3478 if (!gfc_notify_std (GFC_STD_F2018, "CHANGE TEAM statement at %C"))
3479 return MATCH_ERROR;
3481 if (gfc_match_char ('(') == MATCH_NO)
3482 goto syntax;
3484 new_st.op = EXEC_CHANGE_TEAM;
3486 if (gfc_match ("%e", &team) != MATCH_YES)
3487 goto syntax;
3489 m = gfc_match_char (')');
3490 if (m == MATCH_NO)
3491 goto syntax;
3493 new_st.expr1 = team;
3495 return MATCH_YES;
3497 syntax:
3498 gfc_syntax_error (ST_CHANGE_TEAM);
3500 return MATCH_ERROR;
3503 /* Match a END TEAM statement. */
3505 match
3506 gfc_match_end_team (void)
3508 if (!gfc_notify_std (GFC_STD_F2018, "END TEAM statement at %C"))
3509 return MATCH_ERROR;
3511 if (gfc_match_char ('(') == MATCH_YES)
3512 goto syntax;
3514 new_st.op = EXEC_END_TEAM;
3516 return MATCH_YES;
3518 syntax:
3519 gfc_syntax_error (ST_END_TEAM);
3521 return MATCH_ERROR;
3524 /* Match a SYNC TEAM statement. */
3526 match
3527 gfc_match_sync_team (void)
3529 match m;
3530 gfc_expr *team;
3532 if (!gfc_notify_std (GFC_STD_F2018, "SYNC TEAM statement at %C"))
3533 return MATCH_ERROR;
3535 if (gfc_match_char ('(') == MATCH_NO)
3536 goto syntax;
3538 new_st.op = EXEC_SYNC_TEAM;
3540 if (gfc_match ("%e", &team) != MATCH_YES)
3541 goto syntax;
3543 m = gfc_match_char (')');
3544 if (m == MATCH_NO)
3545 goto syntax;
3547 new_st.expr1 = team;
3549 return MATCH_YES;
3551 syntax:
3552 gfc_syntax_error (ST_SYNC_TEAM);
3554 return MATCH_ERROR;
3557 /* Match LOCK/UNLOCK statement. Syntax:
3558 LOCK ( lock-variable [ , lock-stat-list ] )
3559 UNLOCK ( lock-variable [ , sync-stat-list ] )
3560 where lock-stat is ACQUIRED_LOCK or sync-stat
3561 and sync-stat is STAT= or ERRMSG=. */
3563 static match
3564 lock_unlock_statement (gfc_statement st)
3566 match m;
3567 gfc_expr *tmp, *lockvar, *acq_lock, *stat, *errmsg;
3568 bool saw_acq_lock, saw_stat, saw_errmsg;
3570 tmp = lockvar = acq_lock = stat = errmsg = NULL;
3571 saw_acq_lock = saw_stat = saw_errmsg = false;
3573 if (gfc_pure (NULL))
3575 gfc_error ("Image control statement %s at %C in PURE procedure",
3576 st == ST_LOCK ? "LOCK" : "UNLOCK");
3577 return MATCH_ERROR;
3580 gfc_unset_implicit_pure (NULL);
3582 if (flag_coarray == GFC_FCOARRAY_NONE)
3584 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3585 return MATCH_ERROR;
3588 if (gfc_find_state (COMP_CRITICAL))
3590 gfc_error ("Image control statement %s at %C in CRITICAL block",
3591 st == ST_LOCK ? "LOCK" : "UNLOCK");
3592 return MATCH_ERROR;
3595 if (gfc_find_state (COMP_DO_CONCURRENT))
3597 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
3598 st == ST_LOCK ? "LOCK" : "UNLOCK");
3599 return MATCH_ERROR;
3602 if (gfc_match_char ('(') != MATCH_YES)
3603 goto syntax;
3605 if (gfc_match ("%e", &lockvar) != MATCH_YES)
3606 goto syntax;
3607 m = gfc_match_char (',');
3608 if (m == MATCH_ERROR)
3609 goto syntax;
3610 if (m == MATCH_NO)
3612 m = gfc_match_char (')');
3613 if (m == MATCH_YES)
3614 goto done;
3615 goto syntax;
3618 for (;;)
3620 m = gfc_match (" stat = %v", &tmp);
3621 if (m == MATCH_ERROR)
3622 goto syntax;
3623 if (m == MATCH_YES)
3625 if (saw_stat)
3627 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
3628 goto cleanup;
3630 stat = tmp;
3631 saw_stat = true;
3633 m = gfc_match_char (',');
3634 if (m == MATCH_YES)
3635 continue;
3637 tmp = NULL;
3638 break;
3641 m = gfc_match (" errmsg = %v", &tmp);
3642 if (m == MATCH_ERROR)
3643 goto syntax;
3644 if (m == MATCH_YES)
3646 if (saw_errmsg)
3648 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
3649 goto cleanup;
3651 errmsg = tmp;
3652 saw_errmsg = true;
3654 m = gfc_match_char (',');
3655 if (m == MATCH_YES)
3656 continue;
3658 tmp = NULL;
3659 break;
3662 m = gfc_match (" acquired_lock = %v", &tmp);
3663 if (m == MATCH_ERROR || st == ST_UNLOCK)
3664 goto syntax;
3665 if (m == MATCH_YES)
3667 if (saw_acq_lock)
3669 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L",
3670 &tmp->where);
3671 goto cleanup;
3673 acq_lock = tmp;
3674 saw_acq_lock = true;
3676 m = gfc_match_char (',');
3677 if (m == MATCH_YES)
3678 continue;
3680 tmp = NULL;
3681 break;
3684 break;
3687 if (m == MATCH_ERROR)
3688 goto syntax;
3690 if (gfc_match (" )%t") != MATCH_YES)
3691 goto syntax;
3693 done:
3694 switch (st)
3696 case ST_LOCK:
3697 new_st.op = EXEC_LOCK;
3698 break;
3699 case ST_UNLOCK:
3700 new_st.op = EXEC_UNLOCK;
3701 break;
3702 default:
3703 gcc_unreachable ();
3706 new_st.expr1 = lockvar;
3707 new_st.expr2 = stat;
3708 new_st.expr3 = errmsg;
3709 new_st.expr4 = acq_lock;
3711 return MATCH_YES;
3713 syntax:
3714 gfc_syntax_error (st);
3716 cleanup:
3717 if (acq_lock != tmp)
3718 gfc_free_expr (acq_lock);
3719 if (errmsg != tmp)
3720 gfc_free_expr (errmsg);
3721 if (stat != tmp)
3722 gfc_free_expr (stat);
3724 gfc_free_expr (tmp);
3725 gfc_free_expr (lockvar);
3727 return MATCH_ERROR;
3731 match
3732 gfc_match_lock (void)
3734 if (!gfc_notify_std (GFC_STD_F2008, "LOCK statement at %C"))
3735 return MATCH_ERROR;
3737 return lock_unlock_statement (ST_LOCK);
3741 match
3742 gfc_match_unlock (void)
3744 if (!gfc_notify_std (GFC_STD_F2008, "UNLOCK statement at %C"))
3745 return MATCH_ERROR;
3747 return lock_unlock_statement (ST_UNLOCK);
3751 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
3752 SYNC ALL [(sync-stat-list)]
3753 SYNC MEMORY [(sync-stat-list)]
3754 SYNC IMAGES (image-set [, sync-stat-list] )
3755 with sync-stat is int-expr or *. */
3757 static match
3758 sync_statement (gfc_statement st)
3760 match m;
3761 gfc_expr *tmp, *imageset, *stat, *errmsg;
3762 bool saw_stat, saw_errmsg;
3764 tmp = imageset = stat = errmsg = NULL;
3765 saw_stat = saw_errmsg = false;
3767 if (gfc_pure (NULL))
3769 gfc_error ("Image control statement SYNC at %C in PURE procedure");
3770 return MATCH_ERROR;
3773 gfc_unset_implicit_pure (NULL);
3775 if (!gfc_notify_std (GFC_STD_F2008, "SYNC statement at %C"))
3776 return MATCH_ERROR;
3778 if (flag_coarray == GFC_FCOARRAY_NONE)
3780 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
3781 "enable");
3782 return MATCH_ERROR;
3785 if (gfc_find_state (COMP_CRITICAL))
3787 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
3788 return MATCH_ERROR;
3791 if (gfc_find_state (COMP_DO_CONCURRENT))
3793 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
3794 return MATCH_ERROR;
3797 if (gfc_match_eos () == MATCH_YES)
3799 if (st == ST_SYNC_IMAGES)
3800 goto syntax;
3801 goto done;
3804 if (gfc_match_char ('(') != MATCH_YES)
3805 goto syntax;
3807 if (st == ST_SYNC_IMAGES)
3809 /* Denote '*' as imageset == NULL. */
3810 m = gfc_match_char ('*');
3811 if (m == MATCH_ERROR)
3812 goto syntax;
3813 if (m == MATCH_NO)
3815 if (gfc_match ("%e", &imageset) != MATCH_YES)
3816 goto syntax;
3818 m = gfc_match_char (',');
3819 if (m == MATCH_ERROR)
3820 goto syntax;
3821 if (m == MATCH_NO)
3823 m = gfc_match_char (')');
3824 if (m == MATCH_YES)
3825 goto done;
3826 goto syntax;
3830 for (;;)
3832 m = gfc_match (" stat = %e", &tmp);
3833 if (m == MATCH_ERROR)
3834 goto syntax;
3835 if (m == MATCH_YES)
3837 if (saw_stat)
3839 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
3840 goto cleanup;
3842 stat = tmp;
3843 saw_stat = true;
3845 if (gfc_match_char (',') == MATCH_YES)
3846 continue;
3848 tmp = NULL;
3849 break;
3852 m = gfc_match (" errmsg = %e", &tmp);
3853 if (m == MATCH_ERROR)
3854 goto syntax;
3855 if (m == MATCH_YES)
3857 if (saw_errmsg)
3859 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
3860 goto cleanup;
3862 errmsg = tmp;
3863 saw_errmsg = true;
3865 if (gfc_match_char (',') == MATCH_YES)
3866 continue;
3868 tmp = NULL;
3869 break;
3872 break;
3875 if (gfc_match (" )%t") != MATCH_YES)
3876 goto syntax;
3878 done:
3879 switch (st)
3881 case ST_SYNC_ALL:
3882 new_st.op = EXEC_SYNC_ALL;
3883 break;
3884 case ST_SYNC_IMAGES:
3885 new_st.op = EXEC_SYNC_IMAGES;
3886 break;
3887 case ST_SYNC_MEMORY:
3888 new_st.op = EXEC_SYNC_MEMORY;
3889 break;
3890 default:
3891 gcc_unreachable ();
3894 new_st.expr1 = imageset;
3895 new_st.expr2 = stat;
3896 new_st.expr3 = errmsg;
3898 return MATCH_YES;
3900 syntax:
3901 gfc_syntax_error (st);
3903 cleanup:
3904 if (stat != tmp)
3905 gfc_free_expr (stat);
3906 if (errmsg != tmp)
3907 gfc_free_expr (errmsg);
3909 gfc_free_expr (tmp);
3910 gfc_free_expr (imageset);
3912 return MATCH_ERROR;
3916 /* Match SYNC ALL statement. */
3918 match
3919 gfc_match_sync_all (void)
3921 return sync_statement (ST_SYNC_ALL);
3925 /* Match SYNC IMAGES statement. */
3927 match
3928 gfc_match_sync_images (void)
3930 return sync_statement (ST_SYNC_IMAGES);
3934 /* Match SYNC MEMORY statement. */
3936 match
3937 gfc_match_sync_memory (void)
3939 return sync_statement (ST_SYNC_MEMORY);
3943 /* Match a CONTINUE statement. */
3945 match
3946 gfc_match_continue (void)
3948 if (gfc_match_eos () != MATCH_YES)
3950 gfc_syntax_error (ST_CONTINUE);
3951 return MATCH_ERROR;
3954 new_st.op = EXEC_CONTINUE;
3955 return MATCH_YES;
3959 /* Match the (deprecated) ASSIGN statement. */
3961 match
3962 gfc_match_assign (void)
3964 gfc_expr *expr;
3965 gfc_st_label *label;
3967 if (gfc_match (" %l", &label) == MATCH_YES)
3969 if (!gfc_reference_st_label (label, ST_LABEL_UNKNOWN))
3970 return MATCH_ERROR;
3971 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
3973 if (!gfc_notify_std (GFC_STD_F95_DEL, "ASSIGN statement at %C"))
3974 return MATCH_ERROR;
3976 expr->symtree->n.sym->attr.assign = 1;
3978 new_st.op = EXEC_LABEL_ASSIGN;
3979 new_st.label1 = label;
3980 new_st.expr1 = expr;
3981 return MATCH_YES;
3984 return MATCH_NO;
3988 /* Match the GO TO statement. As a computed GOTO statement is
3989 matched, it is transformed into an equivalent SELECT block. No
3990 tree is necessary, and the resulting jumps-to-jumps are
3991 specifically optimized away by the back end. */
3993 match
3994 gfc_match_goto (void)
3996 gfc_code *head, *tail;
3997 gfc_expr *expr;
3998 gfc_case *cp;
3999 gfc_st_label *label;
4000 int i;
4001 match m;
4003 if (gfc_match (" %l%t", &label) == MATCH_YES)
4005 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
4006 return MATCH_ERROR;
4008 new_st.op = EXEC_GOTO;
4009 new_st.label1 = label;
4010 return MATCH_YES;
4013 /* The assigned GO TO statement. */
4015 if (gfc_match_variable (&expr, 0) == MATCH_YES)
4017 if (!gfc_notify_std (GFC_STD_F95_DEL, "Assigned GOTO statement at %C"))
4018 return MATCH_ERROR;
4020 new_st.op = EXEC_GOTO;
4021 new_st.expr1 = expr;
4023 if (gfc_match_eos () == MATCH_YES)
4024 return MATCH_YES;
4026 /* Match label list. */
4027 gfc_match_char (',');
4028 if (gfc_match_char ('(') != MATCH_YES)
4030 gfc_syntax_error (ST_GOTO);
4031 return MATCH_ERROR;
4033 head = tail = NULL;
4037 m = gfc_match_st_label (&label);
4038 if (m != MATCH_YES)
4039 goto syntax;
4041 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
4042 goto cleanup;
4044 if (head == NULL)
4045 head = tail = gfc_get_code (EXEC_GOTO);
4046 else
4048 tail->block = gfc_get_code (EXEC_GOTO);
4049 tail = tail->block;
4052 tail->label1 = label;
4054 while (gfc_match_char (',') == MATCH_YES);
4056 if (gfc_match (" )%t") != MATCH_YES)
4057 goto syntax;
4059 if (head == NULL)
4061 gfc_error ("Statement label list in GOTO at %C cannot be empty");
4062 goto syntax;
4064 new_st.block = head;
4066 return MATCH_YES;
4069 /* Last chance is a computed GO TO statement. */
4070 if (gfc_match_char ('(') != MATCH_YES)
4072 gfc_syntax_error (ST_GOTO);
4073 return MATCH_ERROR;
4076 head = tail = NULL;
4077 i = 1;
4081 m = gfc_match_st_label (&label);
4082 if (m != MATCH_YES)
4083 goto syntax;
4085 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
4086 goto cleanup;
4088 if (head == NULL)
4089 head = tail = gfc_get_code (EXEC_SELECT);
4090 else
4092 tail->block = gfc_get_code (EXEC_SELECT);
4093 tail = tail->block;
4096 cp = gfc_get_case ();
4097 cp->low = cp->high = gfc_get_int_expr (gfc_default_integer_kind,
4098 NULL, i++);
4100 tail->ext.block.case_list = cp;
4102 tail->next = gfc_get_code (EXEC_GOTO);
4103 tail->next->label1 = label;
4105 while (gfc_match_char (',') == MATCH_YES);
4107 if (gfc_match_char (')') != MATCH_YES)
4108 goto syntax;
4110 if (head == NULL)
4112 gfc_error ("Statement label list in GOTO at %C cannot be empty");
4113 goto syntax;
4116 /* Get the rest of the statement. */
4117 gfc_match_char (',');
4119 if (gfc_match (" %e%t", &expr) != MATCH_YES)
4120 goto syntax;
4122 if (!gfc_notify_std (GFC_STD_F95_OBS, "Computed GOTO at %C"))
4123 return MATCH_ERROR;
4125 /* At this point, a computed GOTO has been fully matched and an
4126 equivalent SELECT statement constructed. */
4128 new_st.op = EXEC_SELECT;
4129 new_st.expr1 = NULL;
4131 /* Hack: For a "real" SELECT, the expression is in expr. We put
4132 it in expr2 so we can distinguish then and produce the correct
4133 diagnostics. */
4134 new_st.expr2 = expr;
4135 new_st.block = head;
4136 return MATCH_YES;
4138 syntax:
4139 gfc_syntax_error (ST_GOTO);
4140 cleanup:
4141 gfc_free_statements (head);
4142 return MATCH_ERROR;
4146 /* Frees a list of gfc_alloc structures. */
4148 void
4149 gfc_free_alloc_list (gfc_alloc *p)
4151 gfc_alloc *q;
4153 for (; p; p = q)
4155 q = p->next;
4156 gfc_free_expr (p->expr);
4157 free (p);
4162 /* Match an ALLOCATE statement. */
4164 match
4165 gfc_match_allocate (void)
4167 gfc_alloc *head, *tail;
4168 gfc_expr *stat, *errmsg, *tmp, *source, *mold;
4169 gfc_typespec ts;
4170 gfc_symbol *sym;
4171 match m;
4172 locus old_locus, deferred_locus, assumed_locus;
4173 bool saw_stat, saw_errmsg, saw_source, saw_mold, saw_deferred, b1, b2, b3;
4174 bool saw_unlimited = false, saw_assumed = false;
4176 head = tail = NULL;
4177 stat = errmsg = source = mold = tmp = NULL;
4178 saw_stat = saw_errmsg = saw_source = saw_mold = saw_deferred = false;
4180 if (gfc_match_char ('(') != MATCH_YES)
4182 gfc_syntax_error (ST_ALLOCATE);
4183 return MATCH_ERROR;
4186 /* Match an optional type-spec. */
4187 old_locus = gfc_current_locus;
4188 m = gfc_match_type_spec (&ts);
4189 if (m == MATCH_ERROR)
4190 goto cleanup;
4191 else if (m == MATCH_NO)
4193 char name[GFC_MAX_SYMBOL_LEN + 3];
4195 if (gfc_match ("%n :: ", name) == MATCH_YES)
4197 gfc_error ("Error in type-spec at %L", &old_locus);
4198 goto cleanup;
4201 ts.type = BT_UNKNOWN;
4203 else
4205 /* Needed for the F2008:C631 check below. */
4206 assumed_locus = gfc_current_locus;
4208 if (gfc_match (" :: ") == MATCH_YES)
4210 if (!gfc_notify_std (GFC_STD_F2003, "typespec in ALLOCATE at %L",
4211 &old_locus))
4212 goto cleanup;
4214 if (ts.deferred)
4216 gfc_error ("Type-spec at %L cannot contain a deferred "
4217 "type parameter", &old_locus);
4218 goto cleanup;
4221 if (ts.type == BT_CHARACTER)
4223 if (!ts.u.cl->length)
4224 saw_assumed = true;
4225 else
4226 ts.u.cl->length_from_typespec = true;
4229 if (type_param_spec_list
4230 && gfc_spec_list_type (type_param_spec_list, NULL)
4231 == SPEC_DEFERRED)
4233 gfc_error ("The type parameter spec list in the type-spec at "
4234 "%L cannot contain DEFERRED parameters", &old_locus);
4235 goto cleanup;
4238 else
4240 ts.type = BT_UNKNOWN;
4241 gfc_current_locus = old_locus;
4245 for (;;)
4247 if (head == NULL)
4248 head = tail = gfc_get_alloc ();
4249 else
4251 tail->next = gfc_get_alloc ();
4252 tail = tail->next;
4255 m = gfc_match_variable (&tail->expr, 0);
4256 if (m == MATCH_NO)
4257 goto syntax;
4258 if (m == MATCH_ERROR)
4259 goto cleanup;
4261 if (tail->expr->expr_type == EXPR_CONSTANT)
4263 gfc_error ("Unexpected constant at %C");
4264 goto cleanup;
4267 if (gfc_check_do_variable (tail->expr->symtree))
4268 goto cleanup;
4270 bool impure = gfc_impure_variable (tail->expr->symtree->n.sym);
4271 if (impure && gfc_pure (NULL))
4273 gfc_error ("Bad allocate-object at %C for a PURE procedure");
4274 goto cleanup;
4277 if (impure)
4278 gfc_unset_implicit_pure (NULL);
4280 /* F2008:C631 (R626) A type-param-value in a type-spec shall be an
4281 asterisk if and only if each allocate-object is a dummy argument
4282 for which the corresponding type parameter is assumed. */
4283 if (saw_assumed
4284 && (tail->expr->ts.deferred
4285 || (tail->expr->ts.u.cl && tail->expr->ts.u.cl->length)
4286 || tail->expr->symtree->n.sym->attr.dummy == 0))
4288 gfc_error ("Incompatible allocate-object at %C for CHARACTER "
4289 "type-spec at %L", &assumed_locus);
4290 goto cleanup;
4293 if (tail->expr->ts.deferred)
4295 saw_deferred = true;
4296 deferred_locus = tail->expr->where;
4299 if (gfc_find_state (COMP_DO_CONCURRENT)
4300 || gfc_find_state (COMP_CRITICAL))
4302 gfc_ref *ref;
4303 bool coarray = tail->expr->symtree->n.sym->attr.codimension;
4304 for (ref = tail->expr->ref; ref; ref = ref->next)
4305 if (ref->type == REF_COMPONENT)
4306 coarray = ref->u.c.component->attr.codimension;
4308 if (coarray && gfc_find_state (COMP_DO_CONCURRENT))
4310 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
4311 goto cleanup;
4313 if (coarray && gfc_find_state (COMP_CRITICAL))
4315 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
4316 goto cleanup;
4320 /* Check for F08:C628. */
4321 sym = tail->expr->symtree->n.sym;
4322 b1 = !(tail->expr->ref
4323 && (tail->expr->ref->type == REF_COMPONENT
4324 || tail->expr->ref->type == REF_ARRAY));
4325 if (sym && sym->ts.type == BT_CLASS && sym->attr.class_ok)
4326 b2 = !(CLASS_DATA (sym)->attr.allocatable
4327 || CLASS_DATA (sym)->attr.class_pointer);
4328 else
4329 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4330 || sym->attr.proc_pointer);
4331 b3 = sym && sym->ns && sym->ns->proc_name
4332 && (sym->ns->proc_name->attr.allocatable
4333 || sym->ns->proc_name->attr.pointer
4334 || sym->ns->proc_name->attr.proc_pointer);
4335 if (b1 && b2 && !b3)
4337 gfc_error ("Allocate-object at %L is neither a data pointer "
4338 "nor an allocatable variable", &tail->expr->where);
4339 goto cleanup;
4342 /* The ALLOCATE statement had an optional typespec. Check the
4343 constraints. */
4344 if (ts.type != BT_UNKNOWN)
4346 /* Enforce F03:C624. */
4347 if (!gfc_type_compatible (&tail->expr->ts, &ts))
4349 gfc_error ("Type of entity at %L is type incompatible with "
4350 "typespec", &tail->expr->where);
4351 goto cleanup;
4354 /* Enforce F03:C627. */
4355 if (ts.kind != tail->expr->ts.kind && !UNLIMITED_POLY (tail->expr))
4357 gfc_error ("Kind type parameter for entity at %L differs from "
4358 "the kind type parameter of the typespec",
4359 &tail->expr->where);
4360 goto cleanup;
4364 if (tail->expr->ts.type == BT_DERIVED)
4365 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
4367 if (type_param_spec_list)
4368 tail->expr->param_list = gfc_copy_actual_arglist (type_param_spec_list);
4370 saw_unlimited = saw_unlimited | UNLIMITED_POLY (tail->expr);
4372 if (gfc_peek_ascii_char () == '(' && !sym->attr.dimension)
4374 gfc_error ("Shape specification for allocatable scalar at %C");
4375 goto cleanup;
4378 if (gfc_match_char (',') != MATCH_YES)
4379 break;
4381 alloc_opt_list:
4383 m = gfc_match (" stat = %e", &tmp);
4384 if (m == MATCH_ERROR)
4385 goto cleanup;
4386 if (m == MATCH_YES)
4388 /* Enforce C630. */
4389 if (saw_stat)
4391 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
4392 goto cleanup;
4395 stat = tmp;
4396 tmp = NULL;
4397 saw_stat = true;
4399 if (stat->expr_type == EXPR_CONSTANT)
4401 gfc_error ("STAT tag at %L cannot be a constant", &stat->where);
4402 goto cleanup;
4405 if (gfc_check_do_variable (stat->symtree))
4406 goto cleanup;
4408 if (gfc_match_char (',') == MATCH_YES)
4409 goto alloc_opt_list;
4412 m = gfc_match (" errmsg = %e", &tmp);
4413 if (m == MATCH_ERROR)
4414 goto cleanup;
4415 if (m == MATCH_YES)
4417 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG tag at %L", &tmp->where))
4418 goto cleanup;
4420 /* Enforce C630. */
4421 if (saw_errmsg)
4423 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
4424 goto cleanup;
4427 errmsg = tmp;
4428 tmp = NULL;
4429 saw_errmsg = true;
4431 if (gfc_match_char (',') == MATCH_YES)
4432 goto alloc_opt_list;
4435 m = gfc_match (" source = %e", &tmp);
4436 if (m == MATCH_ERROR)
4437 goto cleanup;
4438 if (m == MATCH_YES)
4440 if (!gfc_notify_std (GFC_STD_F2003, "SOURCE tag at %L", &tmp->where))
4441 goto cleanup;
4443 /* Enforce C630. */
4444 if (saw_source)
4446 gfc_error ("Redundant SOURCE tag found at %L", &tmp->where);
4447 goto cleanup;
4450 /* The next 2 conditionals check C631. */
4451 if (ts.type != BT_UNKNOWN)
4453 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
4454 &tmp->where, &old_locus);
4455 goto cleanup;
4458 if (head->next
4459 && !gfc_notify_std (GFC_STD_F2008, "SOURCE tag at %L"
4460 " with more than a single allocate object",
4461 &tmp->where))
4462 goto cleanup;
4464 source = tmp;
4465 tmp = NULL;
4466 saw_source = true;
4468 if (gfc_match_char (',') == MATCH_YES)
4469 goto alloc_opt_list;
4472 m = gfc_match (" mold = %e", &tmp);
4473 if (m == MATCH_ERROR)
4474 goto cleanup;
4475 if (m == MATCH_YES)
4477 if (!gfc_notify_std (GFC_STD_F2008, "MOLD tag at %L", &tmp->where))
4478 goto cleanup;
4480 /* Check F08:C636. */
4481 if (saw_mold)
4483 gfc_error ("Redundant MOLD tag found at %L", &tmp->where);
4484 goto cleanup;
4487 /* Check F08:C637. */
4488 if (ts.type != BT_UNKNOWN)
4490 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
4491 &tmp->where, &old_locus);
4492 goto cleanup;
4495 mold = tmp;
4496 tmp = NULL;
4497 saw_mold = true;
4498 mold->mold = 1;
4500 if (gfc_match_char (',') == MATCH_YES)
4501 goto alloc_opt_list;
4504 gfc_gobble_whitespace ();
4506 if (gfc_peek_char () == ')')
4507 break;
4510 if (gfc_match (" )%t") != MATCH_YES)
4511 goto syntax;
4513 /* Check F08:C637. */
4514 if (source && mold)
4516 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
4517 &mold->where, &source->where);
4518 goto cleanup;
4521 /* Check F03:C623, */
4522 if (saw_deferred && ts.type == BT_UNKNOWN && !source && !mold)
4524 gfc_error ("Allocate-object at %L with a deferred type parameter "
4525 "requires either a type-spec or SOURCE tag or a MOLD tag",
4526 &deferred_locus);
4527 goto cleanup;
4530 /* Check F03:C625, */
4531 if (saw_unlimited && ts.type == BT_UNKNOWN && !source && !mold)
4533 for (tail = head; tail; tail = tail->next)
4535 if (UNLIMITED_POLY (tail->expr))
4536 gfc_error ("Unlimited polymorphic allocate-object at %L "
4537 "requires either a type-spec or SOURCE tag "
4538 "or a MOLD tag", &tail->expr->where);
4540 goto cleanup;
4543 new_st.op = EXEC_ALLOCATE;
4544 new_st.expr1 = stat;
4545 new_st.expr2 = errmsg;
4546 if (source)
4547 new_st.expr3 = source;
4548 else
4549 new_st.expr3 = mold;
4550 new_st.ext.alloc.list = head;
4551 new_st.ext.alloc.ts = ts;
4553 if (type_param_spec_list)
4554 gfc_free_actual_arglist (type_param_spec_list);
4556 return MATCH_YES;
4558 syntax:
4559 gfc_syntax_error (ST_ALLOCATE);
4561 cleanup:
4562 gfc_free_expr (errmsg);
4563 gfc_free_expr (source);
4564 gfc_free_expr (stat);
4565 gfc_free_expr (mold);
4566 if (tmp && tmp->expr_type) gfc_free_expr (tmp);
4567 gfc_free_alloc_list (head);
4568 if (type_param_spec_list)
4569 gfc_free_actual_arglist (type_param_spec_list);
4570 return MATCH_ERROR;
4574 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
4575 a set of pointer assignments to intrinsic NULL(). */
4577 match
4578 gfc_match_nullify (void)
4580 gfc_code *tail;
4581 gfc_expr *e, *p;
4582 match m;
4584 tail = NULL;
4586 if (gfc_match_char ('(') != MATCH_YES)
4587 goto syntax;
4589 for (;;)
4591 m = gfc_match_variable (&p, 0);
4592 if (m == MATCH_ERROR)
4593 goto cleanup;
4594 if (m == MATCH_NO)
4595 goto syntax;
4597 if (gfc_check_do_variable (p->symtree))
4598 goto cleanup;
4600 /* F2008, C1242. */
4601 if (gfc_is_coindexed (p))
4603 gfc_error ("Pointer object at %C shall not be coindexed");
4604 goto cleanup;
4607 /* Check for valid array pointer object. Bounds remapping is not
4608 allowed with NULLIFY. */
4609 if (p->ref)
4611 gfc_ref *remap = p->ref;
4612 for (; remap; remap = remap->next)
4613 if (!remap->next && remap->type == REF_ARRAY
4614 && remap->u.ar.type != AR_FULL)
4615 break;
4616 if (remap)
4618 gfc_error ("NULLIFY does not allow bounds remapping for "
4619 "pointer object at %C");
4620 goto cleanup;
4624 /* build ' => NULL() '. */
4625 e = gfc_get_null_expr (&gfc_current_locus);
4627 /* Chain to list. */
4628 if (tail == NULL)
4630 tail = &new_st;
4631 tail->op = EXEC_POINTER_ASSIGN;
4633 else
4635 tail->next = gfc_get_code (EXEC_POINTER_ASSIGN);
4636 tail = tail->next;
4639 tail->expr1 = p;
4640 tail->expr2 = e;
4642 if (gfc_match (" )%t") == MATCH_YES)
4643 break;
4644 if (gfc_match_char (',') != MATCH_YES)
4645 goto syntax;
4648 return MATCH_YES;
4650 syntax:
4651 gfc_syntax_error (ST_NULLIFY);
4653 cleanup:
4654 gfc_free_statements (new_st.next);
4655 new_st.next = NULL;
4656 gfc_free_expr (new_st.expr1);
4657 new_st.expr1 = NULL;
4658 gfc_free_expr (new_st.expr2);
4659 new_st.expr2 = NULL;
4660 return MATCH_ERROR;
4664 /* Match a DEALLOCATE statement. */
4666 match
4667 gfc_match_deallocate (void)
4669 gfc_alloc *head, *tail;
4670 gfc_expr *stat, *errmsg, *tmp;
4671 gfc_symbol *sym;
4672 match m;
4673 bool saw_stat, saw_errmsg, b1, b2;
4675 head = tail = NULL;
4676 stat = errmsg = tmp = NULL;
4677 saw_stat = saw_errmsg = false;
4679 if (gfc_match_char ('(') != MATCH_YES)
4680 goto syntax;
4682 for (;;)
4684 if (head == NULL)
4685 head = tail = gfc_get_alloc ();
4686 else
4688 tail->next = gfc_get_alloc ();
4689 tail = tail->next;
4692 m = gfc_match_variable (&tail->expr, 0);
4693 if (m == MATCH_ERROR)
4694 goto cleanup;
4695 if (m == MATCH_NO)
4696 goto syntax;
4698 if (tail->expr->expr_type == EXPR_CONSTANT)
4700 gfc_error ("Unexpected constant at %C");
4701 goto cleanup;
4704 if (gfc_check_do_variable (tail->expr->symtree))
4705 goto cleanup;
4707 sym = tail->expr->symtree->n.sym;
4709 bool impure = gfc_impure_variable (sym);
4710 if (impure && gfc_pure (NULL))
4712 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
4713 goto cleanup;
4716 if (impure)
4717 gfc_unset_implicit_pure (NULL);
4719 if (gfc_is_coarray (tail->expr)
4720 && gfc_find_state (COMP_DO_CONCURRENT))
4722 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
4723 goto cleanup;
4726 if (gfc_is_coarray (tail->expr)
4727 && gfc_find_state (COMP_CRITICAL))
4729 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
4730 goto cleanup;
4733 /* FIXME: disable the checking on derived types. */
4734 b1 = !(tail->expr->ref
4735 && (tail->expr->ref->type == REF_COMPONENT
4736 || tail->expr->ref->type == REF_ARRAY));
4737 if (sym && sym->ts.type == BT_CLASS)
4738 b2 = !(CLASS_DATA (sym) && (CLASS_DATA (sym)->attr.allocatable
4739 || CLASS_DATA (sym)->attr.class_pointer));
4740 else
4741 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4742 || sym->attr.proc_pointer);
4743 if (b1 && b2)
4745 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
4746 "nor an allocatable variable");
4747 goto cleanup;
4750 if (gfc_match_char (',') != MATCH_YES)
4751 break;
4753 dealloc_opt_list:
4755 m = gfc_match (" stat = %e", &tmp);
4756 if (m == MATCH_ERROR)
4757 goto cleanup;
4758 if (m == MATCH_YES)
4760 if (saw_stat)
4762 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
4763 gfc_free_expr (tmp);
4764 goto cleanup;
4767 stat = tmp;
4768 saw_stat = true;
4770 if (gfc_check_do_variable (stat->symtree))
4771 goto cleanup;
4773 if (gfc_match_char (',') == MATCH_YES)
4774 goto dealloc_opt_list;
4777 m = gfc_match (" errmsg = %e", &tmp);
4778 if (m == MATCH_ERROR)
4779 goto cleanup;
4780 if (m == MATCH_YES)
4782 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG at %L", &tmp->where))
4783 goto cleanup;
4785 if (saw_errmsg)
4787 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
4788 gfc_free_expr (tmp);
4789 goto cleanup;
4792 errmsg = tmp;
4793 saw_errmsg = true;
4795 if (gfc_match_char (',') == MATCH_YES)
4796 goto dealloc_opt_list;
4799 gfc_gobble_whitespace ();
4801 if (gfc_peek_char () == ')')
4802 break;
4805 if (gfc_match (" )%t") != MATCH_YES)
4806 goto syntax;
4808 new_st.op = EXEC_DEALLOCATE;
4809 new_st.expr1 = stat;
4810 new_st.expr2 = errmsg;
4811 new_st.ext.alloc.list = head;
4813 return MATCH_YES;
4815 syntax:
4816 gfc_syntax_error (ST_DEALLOCATE);
4818 cleanup:
4819 gfc_free_expr (errmsg);
4820 gfc_free_expr (stat);
4821 gfc_free_alloc_list (head);
4822 return MATCH_ERROR;
4826 /* Match a RETURN statement. */
4828 match
4829 gfc_match_return (void)
4831 gfc_expr *e;
4832 match m;
4833 gfc_compile_state s;
4835 e = NULL;
4837 if (gfc_find_state (COMP_CRITICAL))
4839 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
4840 return MATCH_ERROR;
4843 if (gfc_find_state (COMP_DO_CONCURRENT))
4845 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
4846 return MATCH_ERROR;
4849 if (gfc_match_eos () == MATCH_YES)
4850 goto done;
4852 if (!gfc_find_state (COMP_SUBROUTINE))
4854 gfc_error ("Alternate RETURN statement at %C is only allowed within "
4855 "a SUBROUTINE");
4856 goto cleanup;
4859 if (gfc_current_form == FORM_FREE)
4861 /* The following are valid, so we can't require a blank after the
4862 RETURN keyword:
4863 return+1
4864 return(1) */
4865 char c = gfc_peek_ascii_char ();
4866 if (ISALPHA (c) || ISDIGIT (c))
4867 return MATCH_NO;
4870 m = gfc_match (" %e%t", &e);
4871 if (m == MATCH_YES)
4872 goto done;
4873 if (m == MATCH_ERROR)
4874 goto cleanup;
4876 gfc_syntax_error (ST_RETURN);
4878 cleanup:
4879 gfc_free_expr (e);
4880 return MATCH_ERROR;
4882 done:
4883 gfc_enclosing_unit (&s);
4884 if (s == COMP_PROGRAM
4885 && !gfc_notify_std (GFC_STD_GNU, "RETURN statement in "
4886 "main program at %C"))
4887 return MATCH_ERROR;
4889 new_st.op = EXEC_RETURN;
4890 new_st.expr1 = e;
4892 return MATCH_YES;
4896 /* Match the call of a type-bound procedure, if CALL%var has already been
4897 matched and var found to be a derived-type variable. */
4899 static match
4900 match_typebound_call (gfc_symtree* varst)
4902 gfc_expr* base;
4903 match m;
4905 base = gfc_get_expr ();
4906 base->expr_type = EXPR_VARIABLE;
4907 base->symtree = varst;
4908 base->where = gfc_current_locus;
4909 gfc_set_sym_referenced (varst->n.sym);
4911 m = gfc_match_varspec (base, 0, true, true);
4912 if (m == MATCH_NO)
4913 gfc_error ("Expected component reference at %C");
4914 if (m != MATCH_YES)
4916 gfc_free_expr (base);
4917 return MATCH_ERROR;
4920 if (gfc_match_eos () != MATCH_YES)
4922 gfc_error ("Junk after CALL at %C");
4923 gfc_free_expr (base);
4924 return MATCH_ERROR;
4927 if (base->expr_type == EXPR_COMPCALL)
4928 new_st.op = EXEC_COMPCALL;
4929 else if (base->expr_type == EXPR_PPC)
4930 new_st.op = EXEC_CALL_PPC;
4931 else
4933 gfc_error ("Expected type-bound procedure or procedure pointer component "
4934 "at %C");
4935 gfc_free_expr (base);
4936 return MATCH_ERROR;
4938 new_st.expr1 = base;
4940 return MATCH_YES;
4944 /* Match a CALL statement. The tricky part here are possible
4945 alternate return specifiers. We handle these by having all
4946 "subroutines" actually return an integer via a register that gives
4947 the return number. If the call specifies alternate returns, we
4948 generate code for a SELECT statement whose case clauses contain
4949 GOTOs to the various labels. */
4951 match
4952 gfc_match_call (void)
4954 char name[GFC_MAX_SYMBOL_LEN + 1];
4955 gfc_actual_arglist *a, *arglist;
4956 gfc_case *new_case;
4957 gfc_symbol *sym;
4958 gfc_symtree *st;
4959 gfc_code *c;
4960 match m;
4961 int i;
4963 arglist = NULL;
4965 m = gfc_match ("% %n", name);
4966 if (m == MATCH_NO)
4967 goto syntax;
4968 if (m != MATCH_YES)
4969 return m;
4971 if (gfc_get_ha_sym_tree (name, &st))
4972 return MATCH_ERROR;
4974 sym = st->n.sym;
4976 /* If this is a variable of derived-type, it probably starts a type-bound
4977 procedure call. Associate variable targets have to be resolved for the
4978 target type. */
4979 if (((sym->attr.flavor != FL_PROCEDURE
4980 || gfc_is_function_return_value (sym, gfc_current_ns))
4981 && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS))
4983 (sym->assoc && sym->assoc->target
4984 && gfc_resolve_expr (sym->assoc->target)
4985 && (sym->assoc->target->ts.type == BT_DERIVED
4986 || sym->assoc->target->ts.type == BT_CLASS)))
4987 return match_typebound_call (st);
4989 /* If it does not seem to be callable (include functions so that the
4990 right association is made. They are thrown out in resolution.)
4991 ... */
4992 if (!sym->attr.generic
4993 && !sym->attr.subroutine
4994 && !sym->attr.function)
4996 if (!(sym->attr.external && !sym->attr.referenced))
4998 /* ...create a symbol in this scope... */
4999 if (sym->ns != gfc_current_ns
5000 && gfc_get_sym_tree (name, NULL, &st, false) == 1)
5001 return MATCH_ERROR;
5003 if (sym != st->n.sym)
5004 sym = st->n.sym;
5007 /* ...and then to try to make the symbol into a subroutine. */
5008 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
5009 return MATCH_ERROR;
5012 gfc_set_sym_referenced (sym);
5014 if (gfc_match_eos () != MATCH_YES)
5016 m = gfc_match_actual_arglist (1, &arglist);
5017 if (m == MATCH_NO)
5018 goto syntax;
5019 if (m == MATCH_ERROR)
5020 goto cleanup;
5022 if (gfc_match_eos () != MATCH_YES)
5023 goto syntax;
5026 /* Walk the argument list looking for invalid BOZ. */
5027 for (a = arglist; a; a = a->next)
5028 if (a->expr && a->expr->ts.type == BT_BOZ)
5030 gfc_error ("A BOZ literal constant at %L cannot appear as an actual "
5031 "argument in a subroutine reference", &a->expr->where);
5032 goto cleanup;
5036 /* If any alternate return labels were found, construct a SELECT
5037 statement that will jump to the right place. */
5039 i = 0;
5040 for (a = arglist; a; a = a->next)
5041 if (a->expr == NULL)
5043 i = 1;
5044 break;
5047 if (i)
5049 gfc_symtree *select_st;
5050 gfc_symbol *select_sym;
5051 char name[GFC_MAX_SYMBOL_LEN + 1];
5053 new_st.next = c = gfc_get_code (EXEC_SELECT);
5054 sprintf (name, "_result_%s", sym->name);
5055 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
5057 select_sym = select_st->n.sym;
5058 select_sym->ts.type = BT_INTEGER;
5059 select_sym->ts.kind = gfc_default_integer_kind;
5060 gfc_set_sym_referenced (select_sym);
5061 c->expr1 = gfc_get_expr ();
5062 c->expr1->expr_type = EXPR_VARIABLE;
5063 c->expr1->symtree = select_st;
5064 c->expr1->ts = select_sym->ts;
5065 c->expr1->where = gfc_current_locus;
5067 i = 0;
5068 for (a = arglist; a; a = a->next)
5070 if (a->expr != NULL)
5071 continue;
5073 if (!gfc_reference_st_label (a->label, ST_LABEL_TARGET))
5074 continue;
5076 i++;
5078 c->block = gfc_get_code (EXEC_SELECT);
5079 c = c->block;
5081 new_case = gfc_get_case ();
5082 new_case->high = gfc_get_int_expr (gfc_default_integer_kind, NULL, i);
5083 new_case->low = new_case->high;
5084 c->ext.block.case_list = new_case;
5086 c->next = gfc_get_code (EXEC_GOTO);
5087 c->next->label1 = a->label;
5091 new_st.op = EXEC_CALL;
5092 new_st.symtree = st;
5093 new_st.ext.actual = arglist;
5095 return MATCH_YES;
5097 syntax:
5098 gfc_syntax_error (ST_CALL);
5100 cleanup:
5101 gfc_free_actual_arglist (arglist);
5102 return MATCH_ERROR;
5106 /* Given a name, return a pointer to the common head structure,
5107 creating it if it does not exist. If FROM_MODULE is nonzero, we
5108 mangle the name so that it doesn't interfere with commons defined
5109 in the using namespace.
5110 TODO: Add to global symbol tree. */
5112 gfc_common_head *
5113 gfc_get_common (const char *name, int from_module)
5115 gfc_symtree *st;
5116 static int serial = 0;
5117 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
5119 if (from_module)
5121 /* A use associated common block is only needed to correctly layout
5122 the variables it contains. */
5123 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
5124 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
5126 else
5128 st = gfc_find_symtree (gfc_current_ns->common_root, name);
5130 if (st == NULL)
5131 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
5134 if (st->n.common == NULL)
5136 st->n.common = gfc_get_common_head ();
5137 st->n.common->where = gfc_current_locus;
5138 strcpy (st->n.common->name, name);
5141 return st->n.common;
5145 /* Match a common block name. */
5147 match
5148 gfc_match_common_name (char *name)
5150 match m;
5152 if (gfc_match_char ('/') == MATCH_NO)
5154 name[0] = '\0';
5155 return MATCH_YES;
5158 if (gfc_match_char ('/') == MATCH_YES)
5160 name[0] = '\0';
5161 return MATCH_YES;
5164 m = gfc_match_name (name);
5166 if (m == MATCH_ERROR)
5167 return MATCH_ERROR;
5168 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
5169 return MATCH_YES;
5171 gfc_error ("Syntax error in common block name at %C");
5172 return MATCH_ERROR;
5176 /* Match a COMMON statement. */
5178 match
5179 gfc_match_common (void)
5181 gfc_symbol *sym, **head, *tail, *other;
5182 char name[GFC_MAX_SYMBOL_LEN + 1];
5183 gfc_common_head *t;
5184 gfc_array_spec *as;
5185 gfc_equiv *e1, *e2;
5186 match m;
5187 char c;
5189 /* COMMON has been matched. In free form source code, the next character
5190 needs to be whitespace or '/'. Check that here. Fixed form source
5191 code needs to be checked below. */
5192 c = gfc_peek_ascii_char ();
5193 if (gfc_current_form == FORM_FREE && !gfc_is_whitespace (c) && c != '/')
5194 return MATCH_NO;
5196 as = NULL;
5198 for (;;)
5200 m = gfc_match_common_name (name);
5201 if (m == MATCH_ERROR)
5202 goto cleanup;
5204 if (name[0] == '\0')
5206 t = &gfc_current_ns->blank_common;
5207 if (t->head == NULL)
5208 t->where = gfc_current_locus;
5210 else
5212 t = gfc_get_common (name, 0);
5214 head = &t->head;
5216 if (*head == NULL)
5217 tail = NULL;
5218 else
5220 tail = *head;
5221 while (tail->common_next)
5222 tail = tail->common_next;
5225 /* Grab the list of symbols. */
5226 for (;;)
5228 m = gfc_match_symbol (&sym, 0);
5229 if (m == MATCH_ERROR)
5230 goto cleanup;
5231 if (m == MATCH_NO)
5232 goto syntax;
5234 /* See if we know the current common block is bind(c), and if
5235 so, then see if we can check if the symbol is (which it'll
5236 need to be). This can happen if the bind(c) attr stmt was
5237 applied to the common block, and the variable(s) already
5238 defined, before declaring the common block. */
5239 if (t->is_bind_c == 1)
5241 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
5243 /* If we find an error, just print it and continue,
5244 cause it's just semantic, and we can see if there
5245 are more errors. */
5246 gfc_error_now ("Variable %qs at %L in common block %qs "
5247 "at %C must be declared with a C "
5248 "interoperable kind since common block "
5249 "%qs is bind(c)",
5250 sym->name, &(sym->declared_at), t->name,
5251 t->name);
5254 if (sym->attr.is_bind_c == 1)
5255 gfc_error_now ("Variable %qs in common block %qs at %C cannot "
5256 "be bind(c) since it is not global", sym->name,
5257 t->name);
5260 if (sym->attr.in_common)
5262 gfc_error ("Symbol %qs at %C is already in a COMMON block",
5263 sym->name);
5264 goto cleanup;
5267 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
5268 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
5270 if (!gfc_notify_std (GFC_STD_GNU, "Initialized symbol %qs at "
5271 "%C can only be COMMON in BLOCK DATA",
5272 sym->name))
5273 goto cleanup;
5276 /* Deal with an optional array specification after the
5277 symbol name. */
5278 m = gfc_match_array_spec (&as, true, true);
5279 if (m == MATCH_ERROR)
5280 goto cleanup;
5282 if (m == MATCH_YES)
5284 if (as->type != AS_EXPLICIT)
5286 gfc_error ("Array specification for symbol %qs in COMMON "
5287 "at %C must be explicit", sym->name);
5288 goto cleanup;
5291 if (as->corank)
5293 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
5294 "coarray", sym->name);
5295 goto cleanup;
5298 if (!gfc_add_dimension (&sym->attr, sym->name, NULL))
5299 goto cleanup;
5301 if (sym->attr.pointer)
5303 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
5304 "POINTER array", sym->name);
5305 goto cleanup;
5308 sym->as = as;
5309 as = NULL;
5313 /* Add the in_common attribute, but ignore the reported errors
5314 if any, and continue matching. */
5315 gfc_add_in_common (&sym->attr, sym->name, NULL);
5317 sym->common_block = t;
5318 sym->common_block->refs++;
5320 if (tail != NULL)
5321 tail->common_next = sym;
5322 else
5323 *head = sym;
5325 tail = sym;
5327 sym->common_head = t;
5329 /* Check to see if the symbol is already in an equivalence group.
5330 If it is, set the other members as being in common. */
5331 if (sym->attr.in_equivalence)
5333 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
5335 for (e2 = e1; e2; e2 = e2->eq)
5336 if (e2->expr->symtree->n.sym == sym)
5337 goto equiv_found;
5339 continue;
5341 equiv_found:
5343 for (e2 = e1; e2; e2 = e2->eq)
5345 other = e2->expr->symtree->n.sym;
5346 if (other->common_head
5347 && other->common_head != sym->common_head)
5349 gfc_error ("Symbol %qs, in COMMON block %qs at "
5350 "%C is being indirectly equivalenced to "
5351 "another COMMON block %qs",
5352 sym->name, sym->common_head->name,
5353 other->common_head->name);
5354 goto cleanup;
5356 other->attr.in_common = 1;
5357 other->common_head = t;
5363 gfc_gobble_whitespace ();
5364 if (gfc_match_eos () == MATCH_YES)
5365 goto done;
5366 c = gfc_peek_ascii_char ();
5367 if (c == '/')
5368 break;
5369 if (c != ',')
5371 /* In Fixed form source code, gfortran can end up here for an
5372 expression of the form COMMONI = RHS. This may not be an
5373 error, so return MATCH_NO. */
5374 if (gfc_current_form == FORM_FIXED && c == '=')
5376 gfc_free_array_spec (as);
5377 return MATCH_NO;
5379 goto syntax;
5381 else
5382 gfc_match_char (',');
5384 gfc_gobble_whitespace ();
5385 if (gfc_peek_ascii_char () == '/')
5386 break;
5390 done:
5391 return MATCH_YES;
5393 syntax:
5394 gfc_syntax_error (ST_COMMON);
5396 cleanup:
5397 gfc_free_array_spec (as);
5398 return MATCH_ERROR;
5402 /* Match a BLOCK DATA program unit. */
5404 match
5405 gfc_match_block_data (void)
5407 char name[GFC_MAX_SYMBOL_LEN + 1];
5408 gfc_symbol *sym;
5409 match m;
5411 if (!gfc_notify_std (GFC_STD_F2018_OBS, "BLOCK DATA construct at %L",
5412 &gfc_current_locus))
5413 return MATCH_ERROR;
5415 if (gfc_match_eos () == MATCH_YES)
5417 gfc_new_block = NULL;
5418 return MATCH_YES;
5421 m = gfc_match ("% %n%t", name);
5422 if (m != MATCH_YES)
5423 return MATCH_ERROR;
5425 if (gfc_get_symbol (name, NULL, &sym))
5426 return MATCH_ERROR;
5428 if (!gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL))
5429 return MATCH_ERROR;
5431 gfc_new_block = sym;
5433 return MATCH_YES;
5437 /* Free a namelist structure. */
5439 void
5440 gfc_free_namelist (gfc_namelist *name)
5442 gfc_namelist *n;
5444 for (; name; name = n)
5446 n = name->next;
5447 free (name);
5452 /* Free an OpenMP namelist structure. */
5454 void
5455 gfc_free_omp_namelist (gfc_omp_namelist *name, bool free_ns)
5457 gfc_omp_namelist *n;
5459 for (; name; name = n)
5461 gfc_free_expr (name->expr);
5462 if (free_ns)
5463 gfc_free_namespace (name->u2.ns);
5464 else if (name->u2.udr)
5466 if (name->u2.udr->combiner)
5467 gfc_free_statement (name->u2.udr->combiner);
5468 if (name->u2.udr->initializer)
5469 gfc_free_statement (name->u2.udr->initializer);
5470 free (name->u2.udr);
5472 n = name->next;
5473 free (name);
5478 /* Match a NAMELIST statement. */
5480 match
5481 gfc_match_namelist (void)
5483 gfc_symbol *group_name, *sym;
5484 gfc_namelist *nl;
5485 match m, m2;
5487 m = gfc_match (" / %s /", &group_name);
5488 if (m == MATCH_NO)
5489 goto syntax;
5490 if (m == MATCH_ERROR)
5491 goto error;
5493 for (;;)
5495 if (group_name->ts.type != BT_UNKNOWN)
5497 gfc_error ("Namelist group name %qs at %C already has a basic "
5498 "type of %s", group_name->name,
5499 gfc_typename (&group_name->ts));
5500 return MATCH_ERROR;
5503 if (group_name->attr.flavor == FL_NAMELIST
5504 && group_name->attr.use_assoc
5505 && !gfc_notify_std (GFC_STD_GNU, "Namelist group name %qs "
5506 "at %C already is USE associated and can"
5507 "not be respecified.", group_name->name))
5508 return MATCH_ERROR;
5510 if (group_name->attr.flavor != FL_NAMELIST
5511 && !gfc_add_flavor (&group_name->attr, FL_NAMELIST,
5512 group_name->name, NULL))
5513 return MATCH_ERROR;
5515 for (;;)
5517 m = gfc_match_symbol (&sym, 1);
5518 if (m == MATCH_NO)
5519 goto syntax;
5520 if (m == MATCH_ERROR)
5521 goto error;
5523 if (sym->ts.type == BT_UNKNOWN)
5525 if (gfc_current_ns->seen_implicit_none)
5527 /* It is required that members of a namelist be declared
5528 before the namelist. We check this by checking if the
5529 symbol has a defined type for IMPLICIT NONE. */
5530 gfc_error ("Symbol %qs in namelist %qs at %C must be "
5531 "declared before the namelist is declared.",
5532 sym->name, group_name->name);
5533 gfc_error_check ();
5535 else
5536 /* If the type is not set already, we set it here to the
5537 implicit default type. It is not allowed to set it
5538 later to any other type. */
5539 gfc_set_default_type (sym, 0, gfc_current_ns);
5541 if (sym->attr.in_namelist == 0
5542 && !gfc_add_in_namelist (&sym->attr, sym->name, NULL))
5543 goto error;
5545 /* Use gfc_error_check here, rather than goto error, so that
5546 these are the only errors for the next two lines. */
5547 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
5549 gfc_error ("Assumed size array %qs in namelist %qs at "
5550 "%C is not allowed", sym->name, group_name->name);
5551 gfc_error_check ();
5554 nl = gfc_get_namelist ();
5555 nl->sym = sym;
5556 sym->refs++;
5558 if (group_name->namelist == NULL)
5559 group_name->namelist = group_name->namelist_tail = nl;
5560 else
5562 group_name->namelist_tail->next = nl;
5563 group_name->namelist_tail = nl;
5566 if (gfc_match_eos () == MATCH_YES)
5567 goto done;
5569 m = gfc_match_char (',');
5571 if (gfc_match_char ('/') == MATCH_YES)
5573 m2 = gfc_match (" %s /", &group_name);
5574 if (m2 == MATCH_YES)
5575 break;
5576 if (m2 == MATCH_ERROR)
5577 goto error;
5578 goto syntax;
5581 if (m != MATCH_YES)
5582 goto syntax;
5586 done:
5587 return MATCH_YES;
5589 syntax:
5590 gfc_syntax_error (ST_NAMELIST);
5592 error:
5593 return MATCH_ERROR;
5597 /* Match a MODULE statement. */
5599 match
5600 gfc_match_module (void)
5602 match m;
5604 m = gfc_match (" %s%t", &gfc_new_block);
5605 if (m != MATCH_YES)
5606 return m;
5608 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
5609 gfc_new_block->name, NULL))
5610 return MATCH_ERROR;
5612 return MATCH_YES;
5616 /* Free equivalence sets and lists. Recursively is the easiest way to
5617 do this. */
5619 void
5620 gfc_free_equiv_until (gfc_equiv *eq, gfc_equiv *stop)
5622 if (eq == stop)
5623 return;
5625 gfc_free_equiv (eq->eq);
5626 gfc_free_equiv_until (eq->next, stop);
5627 gfc_free_expr (eq->expr);
5628 free (eq);
5632 void
5633 gfc_free_equiv (gfc_equiv *eq)
5635 gfc_free_equiv_until (eq, NULL);
5639 /* Match an EQUIVALENCE statement. */
5641 match
5642 gfc_match_equivalence (void)
5644 gfc_equiv *eq, *set, *tail;
5645 gfc_ref *ref;
5646 gfc_symbol *sym;
5647 match m;
5648 gfc_common_head *common_head = NULL;
5649 bool common_flag;
5650 int cnt;
5651 char c;
5653 /* EQUIVALENCE has been matched. After gobbling any possible whitespace,
5654 the next character needs to be '('. Check that here, and return
5655 MATCH_NO for a variable of the form equivalencej. */
5656 gfc_gobble_whitespace ();
5657 c = gfc_peek_ascii_char ();
5658 if (c != '(')
5659 return MATCH_NO;
5661 tail = NULL;
5663 for (;;)
5665 eq = gfc_get_equiv ();
5666 if (tail == NULL)
5667 tail = eq;
5669 eq->next = gfc_current_ns->equiv;
5670 gfc_current_ns->equiv = eq;
5672 if (gfc_match_char ('(') != MATCH_YES)
5673 goto syntax;
5675 set = eq;
5676 common_flag = FALSE;
5677 cnt = 0;
5679 for (;;)
5681 m = gfc_match_equiv_variable (&set->expr);
5682 if (m == MATCH_ERROR)
5683 goto cleanup;
5684 if (m == MATCH_NO)
5685 goto syntax;
5687 /* count the number of objects. */
5688 cnt++;
5690 if (gfc_match_char ('%') == MATCH_YES)
5692 gfc_error ("Derived type component %C is not a "
5693 "permitted EQUIVALENCE member");
5694 goto cleanup;
5697 for (ref = set->expr->ref; ref; ref = ref->next)
5698 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
5700 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
5701 "be an array section");
5702 goto cleanup;
5705 sym = set->expr->symtree->n.sym;
5707 if (!gfc_add_in_equivalence (&sym->attr, sym->name, NULL))
5708 goto cleanup;
5709 if (sym->ts.type == BT_CLASS
5710 && CLASS_DATA (sym)
5711 && !gfc_add_in_equivalence (&CLASS_DATA (sym)->attr,
5712 sym->name, NULL))
5713 goto cleanup;
5715 if (sym->attr.in_common)
5717 common_flag = TRUE;
5718 common_head = sym->common_head;
5721 if (gfc_match_char (')') == MATCH_YES)
5722 break;
5724 if (gfc_match_char (',') != MATCH_YES)
5725 goto syntax;
5727 set->eq = gfc_get_equiv ();
5728 set = set->eq;
5731 if (cnt < 2)
5733 gfc_error ("EQUIVALENCE at %C requires two or more objects");
5734 goto cleanup;
5737 /* If one of the members of an equivalence is in common, then
5738 mark them all as being in common. Before doing this, check
5739 that members of the equivalence group are not in different
5740 common blocks. */
5741 if (common_flag)
5742 for (set = eq; set; set = set->eq)
5744 sym = set->expr->symtree->n.sym;
5745 if (sym->common_head && sym->common_head != common_head)
5747 gfc_error ("Attempt to indirectly overlap COMMON "
5748 "blocks %s and %s by EQUIVALENCE at %C",
5749 sym->common_head->name, common_head->name);
5750 goto cleanup;
5752 sym->attr.in_common = 1;
5753 sym->common_head = common_head;
5756 if (gfc_match_eos () == MATCH_YES)
5757 break;
5758 if (gfc_match_char (',') != MATCH_YES)
5760 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
5761 goto cleanup;
5765 if (!gfc_notify_std (GFC_STD_F2018_OBS, "EQUIVALENCE statement at %C"))
5766 return MATCH_ERROR;
5768 return MATCH_YES;
5770 syntax:
5771 gfc_syntax_error (ST_EQUIVALENCE);
5773 cleanup:
5774 eq = tail->next;
5775 tail->next = NULL;
5777 gfc_free_equiv (gfc_current_ns->equiv);
5778 gfc_current_ns->equiv = eq;
5780 return MATCH_ERROR;
5784 /* Check that a statement function is not recursive. This is done by looking
5785 for the statement function symbol(sym) by looking recursively through its
5786 expression(e). If a reference to sym is found, true is returned.
5787 12.5.4 requires that any variable of function that is implicitly typed
5788 shall have that type confirmed by any subsequent type declaration. The
5789 implicit typing is conveniently done here. */
5790 static bool
5791 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
5793 static bool
5794 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5797 if (e == NULL)
5798 return false;
5800 switch (e->expr_type)
5802 case EXPR_FUNCTION:
5803 if (e->symtree == NULL)
5804 return false;
5806 /* Check the name before testing for nested recursion! */
5807 if (sym->name == e->symtree->n.sym->name)
5808 return true;
5810 /* Catch recursion via other statement functions. */
5811 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
5812 && e->symtree->n.sym->value
5813 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
5814 return true;
5816 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5817 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5819 break;
5821 case EXPR_VARIABLE:
5822 if (e->symtree && sym->name == e->symtree->n.sym->name)
5823 return true;
5825 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5826 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5827 break;
5829 default:
5830 break;
5833 return false;
5837 static bool
5838 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
5840 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
5844 /* Match a statement function declaration. It is so easy to match
5845 non-statement function statements with a MATCH_ERROR as opposed to
5846 MATCH_NO that we suppress error message in most cases. */
5848 match
5849 gfc_match_st_function (void)
5851 gfc_error_buffer old_error;
5852 gfc_symbol *sym;
5853 gfc_expr *expr;
5854 match m;
5855 char name[GFC_MAX_SYMBOL_LEN + 1];
5856 locus old_locus;
5857 bool fcn;
5858 gfc_formal_arglist *ptr;
5860 /* Read the possible statement function name, and then check to see if
5861 a symbol is already present in the namespace. Record if it is a
5862 function and whether it has been referenced. */
5863 fcn = false;
5864 ptr = NULL;
5865 old_locus = gfc_current_locus;
5866 m = gfc_match_name (name);
5867 if (m == MATCH_YES)
5869 gfc_find_symbol (name, NULL, 1, &sym);
5870 if (sym && sym->attr.function && !sym->attr.referenced)
5872 fcn = true;
5873 ptr = sym->formal;
5877 gfc_current_locus = old_locus;
5878 m = gfc_match_symbol (&sym, 0);
5879 if (m != MATCH_YES)
5880 return m;
5882 gfc_push_error (&old_error);
5884 if (!gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION, sym->name, NULL))
5885 goto undo_error;
5887 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
5888 goto undo_error;
5890 m = gfc_match (" = %e%t", &expr);
5891 if (m == MATCH_NO)
5892 goto undo_error;
5894 gfc_free_error (&old_error);
5896 if (m == MATCH_ERROR)
5897 return m;
5899 if (recursive_stmt_fcn (expr, sym))
5901 gfc_error ("Statement function at %L is recursive", &expr->where);
5902 return MATCH_ERROR;
5905 if (fcn && ptr != sym->formal)
5907 gfc_error ("Statement function %qs at %L conflicts with function name",
5908 sym->name, &expr->where);
5909 return MATCH_ERROR;
5912 sym->value = expr;
5914 if ((gfc_current_state () == COMP_FUNCTION
5915 || gfc_current_state () == COMP_SUBROUTINE)
5916 && gfc_state_stack->previous->state == COMP_INTERFACE)
5918 gfc_error ("Statement function at %L cannot appear within an INTERFACE",
5919 &expr->where);
5920 return MATCH_ERROR;
5923 if (!gfc_notify_std (GFC_STD_F95_OBS, "Statement function at %C"))
5924 return MATCH_ERROR;
5926 return MATCH_YES;
5928 undo_error:
5929 gfc_pop_error (&old_error);
5930 return MATCH_NO;
5934 /* Match an assignment to a pointer function (F2008). This could, in
5935 general be ambiguous with a statement function. In this implementation
5936 it remains so if it is the first statement after the specification
5937 block. */
5939 match
5940 gfc_match_ptr_fcn_assign (void)
5942 gfc_error_buffer old_error;
5943 locus old_loc;
5944 gfc_symbol *sym;
5945 gfc_expr *expr;
5946 match m;
5947 char name[GFC_MAX_SYMBOL_LEN + 1];
5949 old_loc = gfc_current_locus;
5950 m = gfc_match_name (name);
5951 if (m != MATCH_YES)
5952 return m;
5954 gfc_find_symbol (name, NULL, 1, &sym);
5955 if (sym && sym->attr.flavor != FL_PROCEDURE)
5956 return MATCH_NO;
5958 gfc_push_error (&old_error);
5960 if (sym && sym->attr.function)
5961 goto match_actual_arglist;
5963 gfc_current_locus = old_loc;
5964 m = gfc_match_symbol (&sym, 0);
5965 if (m != MATCH_YES)
5966 return m;
5968 if (!gfc_add_procedure (&sym->attr, PROC_UNKNOWN, sym->name, NULL))
5969 goto undo_error;
5971 match_actual_arglist:
5972 gfc_current_locus = old_loc;
5973 m = gfc_match (" %e", &expr);
5974 if (m != MATCH_YES)
5975 goto undo_error;
5977 new_st.op = EXEC_ASSIGN;
5978 new_st.expr1 = expr;
5979 expr = NULL;
5981 m = gfc_match (" = %e%t", &expr);
5982 if (m != MATCH_YES)
5983 goto undo_error;
5985 new_st.expr2 = expr;
5986 return MATCH_YES;
5988 undo_error:
5989 gfc_pop_error (&old_error);
5990 return MATCH_NO;
5994 /***************** SELECT CASE subroutines ******************/
5996 /* Free a single case structure. */
5998 static void
5999 free_case (gfc_case *p)
6001 if (p->low == p->high)
6002 p->high = NULL;
6003 gfc_free_expr (p->low);
6004 gfc_free_expr (p->high);
6005 free (p);
6009 /* Free a list of case structures. */
6011 void
6012 gfc_free_case_list (gfc_case *p)
6014 gfc_case *q;
6016 for (; p; p = q)
6018 q = p->next;
6019 free_case (p);
6024 /* Match a single case selector. Combining the requirements of F08:C830
6025 and F08:C832 (R838) means that the case-value must have either CHARACTER,
6026 INTEGER, or LOGICAL type. */
6028 static match
6029 match_case_selector (gfc_case **cp)
6031 gfc_case *c;
6032 match m;
6034 c = gfc_get_case ();
6035 c->where = gfc_current_locus;
6037 if (gfc_match_char (':') == MATCH_YES)
6039 m = gfc_match_init_expr (&c->high);
6040 if (m == MATCH_NO)
6041 goto need_expr;
6042 if (m == MATCH_ERROR)
6043 goto cleanup;
6045 if (c->high->ts.type != BT_LOGICAL && c->high->ts.type != BT_INTEGER
6046 && c->high->ts.type != BT_CHARACTER)
6048 gfc_error ("Expression in CASE selector at %L cannot be %s",
6049 &c->high->where, gfc_typename (&c->high->ts));
6050 goto cleanup;
6053 else
6055 m = gfc_match_init_expr (&c->low);
6056 if (m == MATCH_ERROR)
6057 goto cleanup;
6058 if (m == MATCH_NO)
6059 goto need_expr;
6061 if (c->low->ts.type != BT_LOGICAL && c->low->ts.type != BT_INTEGER
6062 && c->low->ts.type != BT_CHARACTER)
6064 gfc_error ("Expression in CASE selector at %L cannot be %s",
6065 &c->low->where, gfc_typename (&c->low->ts));
6066 goto cleanup;
6069 /* If we're not looking at a ':' now, make a range out of a single
6070 target. Else get the upper bound for the case range. */
6071 if (gfc_match_char (':') != MATCH_YES)
6072 c->high = c->low;
6073 else
6075 m = gfc_match_init_expr (&c->high);
6076 if (m == MATCH_ERROR)
6077 goto cleanup;
6078 /* MATCH_NO is fine. It's OK if nothing is there! */
6082 *cp = c;
6083 return MATCH_YES;
6085 need_expr:
6086 gfc_error ("Expected initialization expression in CASE at %C");
6088 cleanup:
6089 free_case (c);
6090 return MATCH_ERROR;
6094 /* Match the end of a case statement. */
6096 static match
6097 match_case_eos (void)
6099 char name[GFC_MAX_SYMBOL_LEN + 1];
6100 match m;
6102 if (gfc_match_eos () == MATCH_YES)
6103 return MATCH_YES;
6105 /* If the case construct doesn't have a case-construct-name, we
6106 should have matched the EOS. */
6107 if (!gfc_current_block ())
6108 return MATCH_NO;
6110 gfc_gobble_whitespace ();
6112 m = gfc_match_name (name);
6113 if (m != MATCH_YES)
6114 return m;
6116 if (strcmp (name, gfc_current_block ()->name) != 0)
6118 gfc_error ("Expected block name %qs of SELECT construct at %C",
6119 gfc_current_block ()->name);
6120 return MATCH_ERROR;
6123 return gfc_match_eos ();
6127 /* Match a SELECT statement. */
6129 match
6130 gfc_match_select (void)
6132 gfc_expr *expr;
6133 match m;
6135 m = gfc_match_label ();
6136 if (m == MATCH_ERROR)
6137 return m;
6139 m = gfc_match (" select case ( %e )%t", &expr);
6140 if (m != MATCH_YES)
6141 return m;
6143 new_st.op = EXEC_SELECT;
6144 new_st.expr1 = expr;
6146 return MATCH_YES;
6150 /* Transfer the selector typespec to the associate name. */
6152 static void
6153 copy_ts_from_selector_to_associate (gfc_expr *associate, gfc_expr *selector)
6155 gfc_ref *ref;
6156 gfc_symbol *assoc_sym;
6157 int rank = 0;
6159 assoc_sym = associate->symtree->n.sym;
6161 /* At this stage the expression rank and arrayspec dimensions have
6162 not been completely sorted out. We must get the expr2->rank
6163 right here, so that the correct class container is obtained. */
6164 ref = selector->ref;
6165 while (ref && ref->next)
6166 ref = ref->next;
6168 if (selector->ts.type == BT_CLASS
6169 && CLASS_DATA (selector)
6170 && CLASS_DATA (selector)->as
6171 && CLASS_DATA (selector)->as->type == AS_ASSUMED_RANK)
6173 assoc_sym->attr.dimension = 1;
6174 assoc_sym->as = gfc_copy_array_spec (CLASS_DATA (selector)->as);
6175 goto build_class_sym;
6177 else if (selector->ts.type == BT_CLASS
6178 && CLASS_DATA (selector)
6179 && CLASS_DATA (selector)->as
6180 && ref && ref->type == REF_ARRAY)
6182 /* Ensure that the array reference type is set. We cannot use
6183 gfc_resolve_expr at this point, so the usable parts of
6184 resolve.c(resolve_array_ref) are employed to do it. */
6185 if (ref->u.ar.type == AR_UNKNOWN)
6187 ref->u.ar.type = AR_ELEMENT;
6188 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
6189 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
6190 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR
6191 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
6192 && ref->u.ar.start[i] && ref->u.ar.start[i]->rank))
6194 ref->u.ar.type = AR_SECTION;
6195 break;
6199 if (ref->u.ar.type == AR_FULL)
6200 selector->rank = CLASS_DATA (selector)->as->rank;
6201 else if (ref->u.ar.type == AR_SECTION)
6202 selector->rank = ref->u.ar.dimen;
6203 else
6204 selector->rank = 0;
6206 rank = selector->rank;
6209 if (rank)
6211 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
6212 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT
6213 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
6214 && ref->u.ar.end[i] == NULL
6215 && ref->u.ar.stride[i] == NULL))
6216 rank--;
6218 if (rank)
6220 assoc_sym->attr.dimension = 1;
6221 assoc_sym->as = gfc_get_array_spec ();
6222 assoc_sym->as->rank = rank;
6223 assoc_sym->as->type = AS_DEFERRED;
6225 else
6226 assoc_sym->as = NULL;
6228 else
6229 assoc_sym->as = NULL;
6231 build_class_sym:
6232 if (selector->ts.type == BT_CLASS)
6234 /* The correct class container has to be available. */
6235 assoc_sym->ts.type = BT_CLASS;
6236 assoc_sym->ts.u.derived = CLASS_DATA (selector)
6237 ? CLASS_DATA (selector)->ts.u.derived : selector->ts.u.derived;
6238 assoc_sym->attr.pointer = 1;
6239 gfc_build_class_symbol (&assoc_sym->ts, &assoc_sym->attr, &assoc_sym->as);
6244 /* Push the current selector onto the SELECT TYPE stack. */
6246 static void
6247 select_type_push (gfc_symbol *sel)
6249 gfc_select_type_stack *top = gfc_get_select_type_stack ();
6250 top->selector = sel;
6251 top->tmp = NULL;
6252 top->prev = select_type_stack;
6254 select_type_stack = top;
6258 /* Set the temporary for the current intrinsic SELECT TYPE selector. */
6260 static gfc_symtree *
6261 select_intrinsic_set_tmp (gfc_typespec *ts)
6263 char name[GFC_MAX_SYMBOL_LEN];
6264 gfc_symtree *tmp;
6265 HOST_WIDE_INT charlen = 0;
6266 gfc_symbol *selector = select_type_stack->selector;
6267 gfc_symbol *sym;
6269 if (ts->type == BT_CLASS || ts->type == BT_DERIVED)
6270 return NULL;
6272 if (selector->ts.type == BT_CLASS && !selector->attr.class_ok)
6273 return NULL;
6275 /* Case value == NULL corresponds to SELECT TYPE cases otherwise
6276 the values correspond to SELECT rank cases. */
6277 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length
6278 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
6279 charlen = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
6281 if (ts->type != BT_CHARACTER)
6282 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (ts->type),
6283 ts->kind);
6284 else
6285 snprintf (name, sizeof (name),
6286 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
6287 gfc_basic_typename (ts->type), charlen, ts->kind);
6289 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
6290 sym = tmp->n.sym;
6291 gfc_add_type (sym, ts, NULL);
6293 /* Copy across the array spec to the selector. */
6294 if (selector->ts.type == BT_CLASS
6295 && (CLASS_DATA (selector)->attr.dimension
6296 || CLASS_DATA (selector)->attr.codimension))
6298 sym->attr.pointer = 1;
6299 sym->attr.dimension = CLASS_DATA (selector)->attr.dimension;
6300 sym->attr.codimension = CLASS_DATA (selector)->attr.codimension;
6301 sym->as = gfc_copy_array_spec (CLASS_DATA (selector)->as);
6304 gfc_set_sym_referenced (sym);
6305 gfc_add_flavor (&sym->attr, FL_VARIABLE, name, NULL);
6306 sym->attr.select_type_temporary = 1;
6308 return tmp;
6312 /* Set up a temporary for the current TYPE IS / CLASS IS branch . */
6314 static void
6315 select_type_set_tmp (gfc_typespec *ts)
6317 char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
6318 gfc_symtree *tmp = NULL;
6319 gfc_symbol *selector = select_type_stack->selector;
6320 gfc_symbol *sym;
6322 if (!ts)
6324 select_type_stack->tmp = NULL;
6325 return;
6328 tmp = select_intrinsic_set_tmp (ts);
6330 if (tmp == NULL)
6332 if (!ts->u.derived)
6333 return;
6335 if (ts->type == BT_CLASS)
6336 sprintf (name, "__tmp_class_%s", ts->u.derived->name);
6337 else
6338 sprintf (name, "__tmp_type_%s", ts->u.derived->name);
6340 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
6341 sym = tmp->n.sym;
6342 gfc_add_type (sym, ts, NULL);
6344 if (selector->ts.type == BT_CLASS && selector->attr.class_ok)
6346 sym->attr.pointer
6347 = CLASS_DATA (selector)->attr.class_pointer;
6349 /* Copy across the array spec to the selector. */
6350 if (CLASS_DATA (selector)->attr.dimension
6351 || CLASS_DATA (selector)->attr.codimension)
6353 sym->attr.dimension
6354 = CLASS_DATA (selector)->attr.dimension;
6355 sym->attr.codimension
6356 = CLASS_DATA (selector)->attr.codimension;
6357 if (CLASS_DATA (selector)->as->type != AS_EXPLICIT)
6358 sym->as = gfc_copy_array_spec (CLASS_DATA (selector)->as);
6359 else
6361 sym->as = gfc_get_array_spec();
6362 sym->as->rank = CLASS_DATA (selector)->as->rank;
6363 sym->as->type = AS_DEFERRED;
6368 gfc_set_sym_referenced (sym);
6369 gfc_add_flavor (&sym->attr, FL_VARIABLE, name, NULL);
6370 sym->attr.select_type_temporary = 1;
6372 if (ts->type == BT_CLASS)
6373 gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
6375 else
6376 sym = tmp->n.sym;
6379 /* Add an association for it, so the rest of the parser knows it is
6380 an associate-name. The target will be set during resolution. */
6381 sym->assoc = gfc_get_association_list ();
6382 sym->assoc->dangling = 1;
6383 sym->assoc->st = tmp;
6385 select_type_stack->tmp = tmp;
6389 /* Match a SELECT TYPE statement. */
6391 match
6392 gfc_match_select_type (void)
6394 gfc_expr *expr1, *expr2 = NULL;
6395 match m;
6396 char name[GFC_MAX_SYMBOL_LEN + 1];
6397 bool class_array;
6398 gfc_symbol *sym;
6399 gfc_namespace *ns = gfc_current_ns;
6401 m = gfc_match_label ();
6402 if (m == MATCH_ERROR)
6403 return m;
6405 m = gfc_match (" select type ( ");
6406 if (m != MATCH_YES)
6407 return m;
6409 if (gfc_current_state() == COMP_MODULE
6410 || gfc_current_state() == COMP_SUBMODULE)
6412 gfc_error ("SELECT TYPE at %C cannot appear in this scope");
6413 return MATCH_ERROR;
6416 gfc_current_ns = gfc_build_block_ns (ns);
6417 m = gfc_match (" %n => %e", name, &expr2);
6418 if (m == MATCH_YES)
6420 expr1 = gfc_get_expr ();
6421 expr1->expr_type = EXPR_VARIABLE;
6422 expr1->where = expr2->where;
6423 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
6425 m = MATCH_ERROR;
6426 goto cleanup;
6429 sym = expr1->symtree->n.sym;
6430 if (expr2->ts.type == BT_UNKNOWN)
6431 sym->attr.untyped = 1;
6432 else
6433 copy_ts_from_selector_to_associate (expr1, expr2);
6435 sym->attr.flavor = FL_VARIABLE;
6436 sym->attr.referenced = 1;
6437 sym->attr.class_ok = 1;
6439 else
6441 m = gfc_match (" %e ", &expr1);
6442 if (m != MATCH_YES)
6444 std::swap (ns, gfc_current_ns);
6445 gfc_free_namespace (ns);
6446 return m;
6450 m = gfc_match (" )%t");
6451 if (m != MATCH_YES)
6453 gfc_error ("parse error in SELECT TYPE statement at %C");
6454 goto cleanup;
6457 /* This ghastly expression seems to be needed to distinguish a CLASS
6458 array, which can have a reference, from other expressions that
6459 have references, such as derived type components, and are not
6460 allowed by the standard.
6461 TODO: see if it is sufficient to exclude component and substring
6462 references. */
6463 class_array = (expr1->expr_type == EXPR_VARIABLE
6464 && expr1->ts.type == BT_CLASS
6465 && CLASS_DATA (expr1)
6466 && (strcmp (CLASS_DATA (expr1)->name, "_data") == 0)
6467 && (CLASS_DATA (expr1)->attr.dimension
6468 || CLASS_DATA (expr1)->attr.codimension)
6469 && expr1->ref
6470 && expr1->ref->type == REF_ARRAY
6471 && expr1->ref->u.ar.type == AR_FULL
6472 && expr1->ref->next == NULL);
6474 /* Check for F03:C811 (F08:C835). */
6475 if (!expr2 && (expr1->expr_type != EXPR_VARIABLE
6476 || (!class_array && expr1->ref != NULL)))
6478 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
6479 "use associate-name=>");
6480 m = MATCH_ERROR;
6481 goto cleanup;
6484 new_st.op = EXEC_SELECT_TYPE;
6485 new_st.expr1 = expr1;
6486 new_st.expr2 = expr2;
6487 new_st.ext.block.ns = gfc_current_ns;
6489 select_type_push (expr1->symtree->n.sym);
6490 gfc_current_ns = ns;
6492 return MATCH_YES;
6494 cleanup:
6495 gfc_free_expr (expr1);
6496 gfc_free_expr (expr2);
6497 gfc_undo_symbols ();
6498 std::swap (ns, gfc_current_ns);
6499 gfc_free_namespace (ns);
6500 return m;
6504 /* Set the temporary for the current intrinsic SELECT RANK selector. */
6506 static void
6507 select_rank_set_tmp (gfc_typespec *ts, int *case_value)
6509 char name[2 * GFC_MAX_SYMBOL_LEN];
6510 char tname[GFC_MAX_SYMBOL_LEN + 7];
6511 gfc_symtree *tmp;
6512 gfc_symbol *selector = select_type_stack->selector;
6513 gfc_symbol *sym;
6514 gfc_symtree *st;
6515 HOST_WIDE_INT charlen = 0;
6517 if (case_value == NULL)
6518 return;
6520 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length
6521 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
6522 charlen = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
6524 if (ts->type == BT_CLASS)
6525 sprintf (tname, "class_%s", ts->u.derived->name);
6526 else if (ts->type == BT_DERIVED)
6527 sprintf (tname, "type_%s", ts->u.derived->name);
6528 else if (ts->type != BT_CHARACTER)
6529 sprintf (tname, "%s_%d", gfc_basic_typename (ts->type), ts->kind);
6530 else
6531 sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
6532 gfc_basic_typename (ts->type), charlen, ts->kind);
6534 /* Case value == NULL corresponds to SELECT TYPE cases otherwise
6535 the values correspond to SELECT rank cases. */
6536 if (*case_value >=0)
6537 sprintf (name, "__tmp_%s_rank_%d", tname, *case_value);
6538 else
6539 sprintf (name, "__tmp_%s_rank_m%d", tname, -*case_value);
6541 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
6542 if (st)
6543 return;
6545 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
6546 sym = tmp->n.sym;
6547 gfc_add_type (sym, ts, NULL);
6549 /* Copy across the array spec to the selector. */
6550 if (selector->ts.type == BT_CLASS)
6552 sym->ts.u.derived = CLASS_DATA (selector)->ts.u.derived;
6553 sym->attr.pointer = CLASS_DATA (selector)->attr.pointer;
6554 sym->attr.allocatable = CLASS_DATA (selector)->attr.allocatable;
6555 sym->attr.target = CLASS_DATA (selector)->attr.target;
6556 sym->attr.class_ok = 0;
6557 if (case_value && *case_value != 0)
6559 sym->attr.dimension = 1;
6560 sym->as = gfc_copy_array_spec (CLASS_DATA (selector)->as);
6561 if (*case_value > 0)
6563 sym->as->type = AS_DEFERRED;
6564 sym->as->rank = *case_value;
6566 else if (*case_value == -1)
6568 sym->as->type = AS_ASSUMED_SIZE;
6569 sym->as->rank = 1;
6573 else
6575 sym->attr.pointer = selector->attr.pointer;
6576 sym->attr.allocatable = selector->attr.allocatable;
6577 sym->attr.target = selector->attr.target;
6578 if (case_value && *case_value != 0)
6580 sym->attr.dimension = 1;
6581 sym->as = gfc_copy_array_spec (selector->as);
6582 if (*case_value > 0)
6584 sym->as->type = AS_DEFERRED;
6585 sym->as->rank = *case_value;
6587 else if (*case_value == -1)
6589 sym->as->type = AS_ASSUMED_SIZE;
6590 sym->as->rank = 1;
6595 gfc_set_sym_referenced (sym);
6596 gfc_add_flavor (&sym->attr, FL_VARIABLE, name, NULL);
6597 sym->attr.select_type_temporary = 1;
6598 if (case_value)
6599 sym->attr.select_rank_temporary = 1;
6601 if (ts->type == BT_CLASS)
6602 gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
6604 /* Add an association for it, so the rest of the parser knows it is
6605 an associate-name. The target will be set during resolution. */
6606 sym->assoc = gfc_get_association_list ();
6607 sym->assoc->dangling = 1;
6608 sym->assoc->st = tmp;
6610 select_type_stack->tmp = tmp;
6614 /* Match a SELECT RANK statement. */
6616 match
6617 gfc_match_select_rank (void)
6619 gfc_expr *expr1, *expr2 = NULL;
6620 match m;
6621 char name[GFC_MAX_SYMBOL_LEN + 1];
6622 gfc_symbol *sym, *sym2;
6623 gfc_namespace *ns = gfc_current_ns;
6624 gfc_array_spec *as = NULL;
6626 m = gfc_match_label ();
6627 if (m == MATCH_ERROR)
6628 return m;
6630 m = gfc_match (" select rank ( ");
6631 if (m != MATCH_YES)
6632 return m;
6634 if (!gfc_notify_std (GFC_STD_F2018, "SELECT RANK statement at %C"))
6635 return MATCH_NO;
6637 gfc_current_ns = gfc_build_block_ns (ns);
6638 m = gfc_match (" %n => %e", name, &expr2);
6639 if (m == MATCH_YES)
6641 expr1 = gfc_get_expr ();
6642 expr1->expr_type = EXPR_VARIABLE;
6643 expr1->where = expr2->where;
6644 expr1->ref = gfc_copy_ref (expr2->ref);
6645 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
6647 m = MATCH_ERROR;
6648 goto cleanup;
6651 sym = expr1->symtree->n.sym;
6653 if (expr2->symtree)
6655 sym2 = expr2->symtree->n.sym;
6656 as = (sym2->ts.type == BT_CLASS
6657 && CLASS_DATA (sym2)) ? CLASS_DATA (sym2)->as : sym2->as;
6660 if (expr2->expr_type != EXPR_VARIABLE
6661 || !(as && as->type == AS_ASSUMED_RANK))
6663 gfc_error ("The SELECT RANK selector at %C must be an assumed "
6664 "rank variable");
6665 m = MATCH_ERROR;
6666 goto cleanup;
6669 if (expr2->ts.type == BT_CLASS && CLASS_DATA (sym2))
6671 copy_ts_from_selector_to_associate (expr1, expr2);
6673 sym->attr.flavor = FL_VARIABLE;
6674 sym->attr.referenced = 1;
6675 sym->attr.class_ok = 1;
6676 CLASS_DATA (sym)->attr.allocatable = CLASS_DATA (sym2)->attr.allocatable;
6677 CLASS_DATA (sym)->attr.pointer = CLASS_DATA (sym2)->attr.pointer;
6678 CLASS_DATA (sym)->attr.target = CLASS_DATA (sym2)->attr.target;
6679 sym->attr.pointer = 1;
6681 else
6683 sym->ts = sym2->ts;
6684 sym->as = gfc_copy_array_spec (sym2->as);
6685 sym->attr.dimension = 1;
6687 sym->attr.flavor = FL_VARIABLE;
6688 sym->attr.referenced = 1;
6689 sym->attr.class_ok = sym2->attr.class_ok;
6690 sym->attr.allocatable = sym2->attr.allocatable;
6691 sym->attr.pointer = sym2->attr.pointer;
6692 sym->attr.target = sym2->attr.target;
6695 else
6697 m = gfc_match (" %e ", &expr1);
6699 if (m != MATCH_YES)
6701 gfc_undo_symbols ();
6702 std::swap (ns, gfc_current_ns);
6703 gfc_free_namespace (ns);
6704 return m;
6707 if (expr1->symtree)
6709 sym = expr1->symtree->n.sym;
6710 as = (sym->ts.type == BT_CLASS
6711 && CLASS_DATA (sym)) ? CLASS_DATA (sym)->as : sym->as;
6714 if (expr1->expr_type != EXPR_VARIABLE
6715 || !(as && as->type == AS_ASSUMED_RANK))
6717 gfc_error("The SELECT RANK selector at %C must be an assumed "
6718 "rank variable");
6719 m = MATCH_ERROR;
6720 goto cleanup;
6724 m = gfc_match (" )%t");
6725 if (m != MATCH_YES)
6727 gfc_error ("parse error in SELECT RANK statement at %C");
6728 goto cleanup;
6731 new_st.op = EXEC_SELECT_RANK;
6732 new_st.expr1 = expr1;
6733 new_st.expr2 = expr2;
6734 new_st.ext.block.ns = gfc_current_ns;
6736 select_type_push (expr1->symtree->n.sym);
6737 gfc_current_ns = ns;
6739 return MATCH_YES;
6741 cleanup:
6742 gfc_free_expr (expr1);
6743 gfc_free_expr (expr2);
6744 gfc_undo_symbols ();
6745 std::swap (ns, gfc_current_ns);
6746 gfc_free_namespace (ns);
6747 return m;
6751 /* Match a CASE statement. */
6753 match
6754 gfc_match_case (void)
6756 gfc_case *c, *head, *tail;
6757 match m;
6759 head = tail = NULL;
6761 if (gfc_current_state () != COMP_SELECT)
6763 gfc_error ("Unexpected CASE statement at %C");
6764 return MATCH_ERROR;
6767 if (gfc_match ("% default") == MATCH_YES)
6769 m = match_case_eos ();
6770 if (m == MATCH_NO)
6771 goto syntax;
6772 if (m == MATCH_ERROR)
6773 goto cleanup;
6775 new_st.op = EXEC_SELECT;
6776 c = gfc_get_case ();
6777 c->where = gfc_current_locus;
6778 new_st.ext.block.case_list = c;
6779 return MATCH_YES;
6782 if (gfc_match_char ('(') != MATCH_YES)
6783 goto syntax;
6785 for (;;)
6787 if (match_case_selector (&c) == MATCH_ERROR)
6788 goto cleanup;
6790 if (head == NULL)
6791 head = c;
6792 else
6793 tail->next = c;
6795 tail = c;
6797 if (gfc_match_char (')') == MATCH_YES)
6798 break;
6799 if (gfc_match_char (',') != MATCH_YES)
6800 goto syntax;
6803 m = match_case_eos ();
6804 if (m == MATCH_NO)
6805 goto syntax;
6806 if (m == MATCH_ERROR)
6807 goto cleanup;
6809 new_st.op = EXEC_SELECT;
6810 new_st.ext.block.case_list = head;
6812 return MATCH_YES;
6814 syntax:
6815 gfc_error ("Syntax error in CASE specification at %C");
6817 cleanup:
6818 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
6819 return MATCH_ERROR;
6823 /* Match a TYPE IS statement. */
6825 match
6826 gfc_match_type_is (void)
6828 gfc_case *c = NULL;
6829 match m;
6831 if (gfc_current_state () != COMP_SELECT_TYPE)
6833 gfc_error ("Unexpected TYPE IS statement at %C");
6834 return MATCH_ERROR;
6837 if (gfc_match_char ('(') != MATCH_YES)
6838 goto syntax;
6840 c = gfc_get_case ();
6841 c->where = gfc_current_locus;
6843 m = gfc_match_type_spec (&c->ts);
6844 if (m == MATCH_NO)
6845 goto syntax;
6846 if (m == MATCH_ERROR)
6847 goto cleanup;
6849 if (gfc_match_char (')') != MATCH_YES)
6850 goto syntax;
6852 m = match_case_eos ();
6853 if (m == MATCH_NO)
6854 goto syntax;
6855 if (m == MATCH_ERROR)
6856 goto cleanup;
6858 new_st.op = EXEC_SELECT_TYPE;
6859 new_st.ext.block.case_list = c;
6861 if (c->ts.type == BT_DERIVED && c->ts.u.derived
6862 && (c->ts.u.derived->attr.sequence
6863 || c->ts.u.derived->attr.is_bind_c))
6865 gfc_error ("The type-spec shall not specify a sequence derived "
6866 "type or a type with the BIND attribute in SELECT "
6867 "TYPE at %C [F2003:C815]");
6868 return MATCH_ERROR;
6871 if (c->ts.type == BT_DERIVED
6872 && c->ts.u.derived && c->ts.u.derived->attr.pdt_type
6873 && gfc_spec_list_type (type_param_spec_list, c->ts.u.derived)
6874 != SPEC_ASSUMED)
6876 gfc_error ("All the LEN type parameters in the TYPE IS statement "
6877 "at %C must be ASSUMED");
6878 return MATCH_ERROR;
6881 /* Create temporary variable. */
6882 select_type_set_tmp (&c->ts);
6884 return MATCH_YES;
6886 syntax:
6887 gfc_error ("Syntax error in TYPE IS specification at %C");
6889 cleanup:
6890 if (c != NULL)
6891 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
6892 return MATCH_ERROR;
6896 /* Match a CLASS IS or CLASS DEFAULT statement. */
6898 match
6899 gfc_match_class_is (void)
6901 gfc_case *c = NULL;
6902 match m;
6904 if (gfc_current_state () != COMP_SELECT_TYPE)
6905 return MATCH_NO;
6907 if (gfc_match ("% default") == MATCH_YES)
6909 m = match_case_eos ();
6910 if (m == MATCH_NO)
6911 goto syntax;
6912 if (m == MATCH_ERROR)
6913 goto cleanup;
6915 new_st.op = EXEC_SELECT_TYPE;
6916 c = gfc_get_case ();
6917 c->where = gfc_current_locus;
6918 c->ts.type = BT_UNKNOWN;
6919 new_st.ext.block.case_list = c;
6920 select_type_set_tmp (NULL);
6921 return MATCH_YES;
6924 m = gfc_match ("% is");
6925 if (m == MATCH_NO)
6926 goto syntax;
6927 if (m == MATCH_ERROR)
6928 goto cleanup;
6930 if (gfc_match_char ('(') != MATCH_YES)
6931 goto syntax;
6933 c = gfc_get_case ();
6934 c->where = gfc_current_locus;
6936 m = match_derived_type_spec (&c->ts);
6937 if (m == MATCH_NO)
6938 goto syntax;
6939 if (m == MATCH_ERROR)
6940 goto cleanup;
6942 if (c->ts.type == BT_DERIVED)
6943 c->ts.type = BT_CLASS;
6945 if (gfc_match_char (')') != MATCH_YES)
6946 goto syntax;
6948 m = match_case_eos ();
6949 if (m == MATCH_NO)
6950 goto syntax;
6951 if (m == MATCH_ERROR)
6952 goto cleanup;
6954 new_st.op = EXEC_SELECT_TYPE;
6955 new_st.ext.block.case_list = c;
6957 /* Create temporary variable. */
6958 select_type_set_tmp (&c->ts);
6960 return MATCH_YES;
6962 syntax:
6963 gfc_error ("Syntax error in CLASS IS specification at %C");
6965 cleanup:
6966 if (c != NULL)
6967 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
6968 return MATCH_ERROR;
6972 /* Match a RANK statement. */
6974 match
6975 gfc_match_rank_is (void)
6977 gfc_case *c = NULL;
6978 match m;
6979 int case_value;
6981 if (gfc_current_state () != COMP_SELECT_RANK)
6983 gfc_error ("Unexpected RANK statement at %C");
6984 return MATCH_ERROR;
6987 if (gfc_match ("% default") == MATCH_YES)
6989 m = match_case_eos ();
6990 if (m == MATCH_NO)
6991 goto syntax;
6992 if (m == MATCH_ERROR)
6993 goto cleanup;
6995 new_st.op = EXEC_SELECT_RANK;
6996 c = gfc_get_case ();
6997 c->ts.type = BT_UNKNOWN;
6998 c->where = gfc_current_locus;
6999 new_st.ext.block.case_list = c;
7000 select_type_stack->tmp = NULL;
7001 return MATCH_YES;
7004 if (gfc_match_char ('(') != MATCH_YES)
7005 goto syntax;
7007 c = gfc_get_case ();
7008 c->where = gfc_current_locus;
7009 c->ts = select_type_stack->selector->ts;
7011 m = gfc_match_expr (&c->low);
7012 if (m == MATCH_NO)
7014 if (gfc_match_char ('*') == MATCH_YES)
7015 c->low = gfc_get_int_expr (gfc_default_integer_kind,
7016 NULL, -1);
7017 else
7018 goto syntax;
7020 case_value = -1;
7022 else if (m == MATCH_YES)
7024 /* F2018: R1150 */
7025 if (c->low->expr_type != EXPR_CONSTANT
7026 || c->low->ts.type != BT_INTEGER
7027 || c->low->rank)
7029 gfc_error ("The SELECT RANK CASE expression at %C must be a "
7030 "scalar, integer constant");
7031 goto cleanup;
7034 case_value = (int) mpz_get_si (c->low->value.integer);
7035 /* F2018: C1151 */
7036 if ((case_value < 0) || (case_value > GFC_MAX_DIMENSIONS))
7038 gfc_error ("The value of the SELECT RANK CASE expression at "
7039 "%C must not be less than zero or greater than %d",
7040 GFC_MAX_DIMENSIONS);
7041 goto cleanup;
7044 else
7045 goto cleanup;
7047 if (gfc_match_char (')') != MATCH_YES)
7048 goto syntax;
7050 m = match_case_eos ();
7051 if (m == MATCH_NO)
7052 goto syntax;
7053 if (m == MATCH_ERROR)
7054 goto cleanup;
7056 new_st.op = EXEC_SELECT_RANK;
7057 new_st.ext.block.case_list = c;
7059 /* Create temporary variable. Recycle the select type code. */
7060 select_rank_set_tmp (&c->ts, &case_value);
7062 return MATCH_YES;
7064 syntax:
7065 gfc_error ("Syntax error in RANK specification at %C");
7067 cleanup:
7068 if (c != NULL)
7069 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
7070 return MATCH_ERROR;
7073 /********************* WHERE subroutines ********************/
7075 /* Match the rest of a simple WHERE statement that follows an IF statement.
7078 static match
7079 match_simple_where (void)
7081 gfc_expr *expr;
7082 gfc_code *c;
7083 match m;
7085 m = gfc_match (" ( %e )", &expr);
7086 if (m != MATCH_YES)
7087 return m;
7089 m = gfc_match_assignment ();
7090 if (m == MATCH_NO)
7091 goto syntax;
7092 if (m == MATCH_ERROR)
7093 goto cleanup;
7095 if (gfc_match_eos () != MATCH_YES)
7096 goto syntax;
7098 c = gfc_get_code (EXEC_WHERE);
7099 c->expr1 = expr;
7101 c->next = XCNEW (gfc_code);
7102 *c->next = new_st;
7103 c->next->loc = gfc_current_locus;
7104 gfc_clear_new_st ();
7106 new_st.op = EXEC_WHERE;
7107 new_st.block = c;
7109 return MATCH_YES;
7111 syntax:
7112 gfc_syntax_error (ST_WHERE);
7114 cleanup:
7115 gfc_free_expr (expr);
7116 return MATCH_ERROR;
7120 /* Match a WHERE statement. */
7122 match
7123 gfc_match_where (gfc_statement *st)
7125 gfc_expr *expr;
7126 match m0, m;
7127 gfc_code *c;
7129 m0 = gfc_match_label ();
7130 if (m0 == MATCH_ERROR)
7131 return m0;
7133 m = gfc_match (" where ( %e )", &expr);
7134 if (m != MATCH_YES)
7135 return m;
7137 if (gfc_match_eos () == MATCH_YES)
7139 *st = ST_WHERE_BLOCK;
7140 new_st.op = EXEC_WHERE;
7141 new_st.expr1 = expr;
7142 return MATCH_YES;
7145 m = gfc_match_assignment ();
7146 if (m == MATCH_NO)
7147 gfc_syntax_error (ST_WHERE);
7149 if (m != MATCH_YES)
7151 gfc_free_expr (expr);
7152 return MATCH_ERROR;
7155 /* We've got a simple WHERE statement. */
7156 *st = ST_WHERE;
7157 c = gfc_get_code (EXEC_WHERE);
7158 c->expr1 = expr;
7160 /* Put in the assignment. It will not be processed by add_statement, so we
7161 need to copy the location here. */
7163 c->next = XCNEW (gfc_code);
7164 *c->next = new_st;
7165 c->next->loc = gfc_current_locus;
7166 gfc_clear_new_st ();
7168 new_st.op = EXEC_WHERE;
7169 new_st.block = c;
7171 return MATCH_YES;
7175 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
7176 new_st if successful. */
7178 match
7179 gfc_match_elsewhere (void)
7181 char name[GFC_MAX_SYMBOL_LEN + 1];
7182 gfc_expr *expr;
7183 match m;
7185 if (gfc_current_state () != COMP_WHERE)
7187 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
7188 return MATCH_ERROR;
7191 expr = NULL;
7193 if (gfc_match_char ('(') == MATCH_YES)
7195 m = gfc_match_expr (&expr);
7196 if (m == MATCH_NO)
7197 goto syntax;
7198 if (m == MATCH_ERROR)
7199 return MATCH_ERROR;
7201 if (gfc_match_char (')') != MATCH_YES)
7202 goto syntax;
7205 if (gfc_match_eos () != MATCH_YES)
7207 /* Only makes sense if we have a where-construct-name. */
7208 if (!gfc_current_block ())
7210 m = MATCH_ERROR;
7211 goto cleanup;
7213 /* Better be a name at this point. */
7214 m = gfc_match_name (name);
7215 if (m == MATCH_NO)
7216 goto syntax;
7217 if (m == MATCH_ERROR)
7218 goto cleanup;
7220 if (gfc_match_eos () != MATCH_YES)
7221 goto syntax;
7223 if (strcmp (name, gfc_current_block ()->name) != 0)
7225 gfc_error ("Label %qs at %C doesn't match WHERE label %qs",
7226 name, gfc_current_block ()->name);
7227 goto cleanup;
7231 new_st.op = EXEC_WHERE;
7232 new_st.expr1 = expr;
7233 return MATCH_YES;
7235 syntax:
7236 gfc_syntax_error (ST_ELSEWHERE);
7238 cleanup:
7239 gfc_free_expr (expr);
7240 return MATCH_ERROR;