Extend fold_vec_perm to handle VLA vector_cst.
[official-gcc.git] / gcc / fortran / match.cc
blobba23bcd969236be97aa8343497d4fe3dbcfeee32
1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000-2023 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.
458 When gobble_ws is false, do not skip over leading blanks. */
460 match
461 gfc_match_small_literal_int (int *value, int *cnt, bool gobble_ws)
463 locus old_loc;
464 char c;
465 int i, j;
467 old_loc = gfc_current_locus;
469 *value = -1;
470 if (gobble_ws)
471 gfc_gobble_whitespace ();
472 c = gfc_next_ascii_char ();
473 if (cnt)
474 *cnt = 0;
476 if (!ISDIGIT (c))
478 gfc_current_locus = old_loc;
479 return MATCH_NO;
482 i = c - '0';
483 j = 1;
485 for (;;)
487 old_loc = gfc_current_locus;
488 c = gfc_next_ascii_char ();
490 if (!ISDIGIT (c))
491 break;
493 i = 10 * i + c - '0';
494 j++;
496 if (i > 99999999)
498 gfc_error ("Integer too large at %C");
499 return MATCH_ERROR;
503 gfc_current_locus = old_loc;
505 *value = i;
506 if (cnt)
507 *cnt = j;
508 return MATCH_YES;
512 /* Match a small, constant integer expression, like in a kind
513 statement. On MATCH_YES, 'value' is set. */
515 match
516 gfc_match_small_int (int *value)
518 gfc_expr *expr;
519 match m;
520 int i;
522 m = gfc_match_expr (&expr);
523 if (m != MATCH_YES)
524 return m;
526 if (gfc_extract_int (expr, &i, 1))
527 m = MATCH_ERROR;
528 gfc_free_expr (expr);
530 *value = i;
531 return m;
535 /* Matches a statement label. Uses gfc_match_small_literal_int() to
536 do most of the work. */
538 match
539 gfc_match_st_label (gfc_st_label **label)
541 locus old_loc;
542 match m;
543 int i, cnt;
545 old_loc = gfc_current_locus;
547 m = gfc_match_small_literal_int (&i, &cnt);
548 if (m != MATCH_YES)
549 return m;
551 if (cnt > 5)
553 gfc_error ("Too many digits in statement label at %C");
554 goto cleanup;
557 if (i == 0)
559 gfc_error ("Statement label at %C is zero");
560 goto cleanup;
563 *label = gfc_get_st_label (i);
564 return MATCH_YES;
566 cleanup:
568 gfc_current_locus = old_loc;
569 return MATCH_ERROR;
573 /* Match and validate a label associated with a named IF, DO or SELECT
574 statement. If the symbol does not have the label attribute, we add
575 it. We also make sure the symbol does not refer to another
576 (active) block. A matched label is pointed to by gfc_new_block. */
578 static match
579 gfc_match_label (void)
581 char name[GFC_MAX_SYMBOL_LEN + 1];
582 match m;
584 gfc_new_block = NULL;
586 m = gfc_match (" %n :", name);
587 if (m != MATCH_YES)
588 return m;
590 if (gfc_get_symbol (name, NULL, &gfc_new_block))
592 gfc_error ("Label name %qs at %C is ambiguous", name);
593 return MATCH_ERROR;
596 if (gfc_new_block->attr.flavor == FL_LABEL)
598 gfc_error ("Duplicate construct label %qs at %C", name);
599 return MATCH_ERROR;
602 if (!gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
603 gfc_new_block->name, NULL))
604 return MATCH_ERROR;
606 return MATCH_YES;
610 /* See if the current input looks like a name of some sort. Modifies
611 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
612 Note that options.cc restricts max_identifier_length to not more
613 than GFC_MAX_SYMBOL_LEN.
614 When gobble_ws is false, do not skip over leading blanks. */
616 match
617 gfc_match_name (char *buffer, bool gobble_ws)
619 locus old_loc;
620 int i;
621 char c;
623 old_loc = gfc_current_locus;
624 if (gobble_ws)
625 gfc_gobble_whitespace ();
627 c = gfc_next_ascii_char ();
628 if (!(ISALPHA (c) || (c == '_' && flag_allow_leading_underscore)))
630 /* Special cases for unary minus and plus, which allows for a sensible
631 error message for code of the form 'c = exp(-a*b) )' where an
632 extra ')' appears at the end of statement. */
633 if (!gfc_error_flag_test () && c != '(' && c != '-' && c != '+')
634 gfc_error ("Invalid character in name at %C");
635 gfc_current_locus = old_loc;
636 return MATCH_NO;
639 i = 0;
643 buffer[i++] = c;
645 if (i > gfc_option.max_identifier_length)
647 gfc_error ("Name at %C is too long");
648 return MATCH_ERROR;
651 old_loc = gfc_current_locus;
652 c = gfc_next_ascii_char ();
654 while (ISALNUM (c) || c == '_' || (flag_dollar_ok && c == '$'));
656 if (c == '$' && !flag_dollar_ok)
658 gfc_fatal_error ("Invalid character %<$%> at %L. Use %<-fdollar-ok%> to "
659 "allow it as an extension", &old_loc);
660 return MATCH_ERROR;
663 buffer[i] = '\0';
664 gfc_current_locus = old_loc;
666 return MATCH_YES;
670 /* Match a symbol on the input. Modifies the pointer to the symbol
671 pointer if successful. */
673 match
674 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
676 char buffer[GFC_MAX_SYMBOL_LEN + 1];
677 match m;
679 m = gfc_match_name (buffer);
680 if (m != MATCH_YES)
681 return m;
683 if (host_assoc)
684 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
685 ? MATCH_ERROR : MATCH_YES;
687 if (gfc_get_sym_tree (buffer, NULL, matched_symbol, false))
688 return MATCH_ERROR;
690 return MATCH_YES;
694 match
695 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
697 gfc_symtree *st;
698 match m;
700 m = gfc_match_sym_tree (&st, host_assoc);
702 if (m == MATCH_YES)
704 if (st)
705 *matched_symbol = st->n.sym;
706 else
707 *matched_symbol = NULL;
709 else
710 *matched_symbol = NULL;
711 return m;
715 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
716 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
717 in matchexp.cc. */
719 match
720 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
722 locus orig_loc = gfc_current_locus;
723 char ch;
725 gfc_gobble_whitespace ();
726 ch = gfc_next_ascii_char ();
727 switch (ch)
729 case '+':
730 /* Matched "+". */
731 *result = INTRINSIC_PLUS;
732 return MATCH_YES;
734 case '-':
735 /* Matched "-". */
736 *result = INTRINSIC_MINUS;
737 return MATCH_YES;
739 case '=':
740 if (gfc_next_ascii_char () == '=')
742 /* Matched "==". */
743 *result = INTRINSIC_EQ;
744 return MATCH_YES;
746 break;
748 case '<':
749 if (gfc_peek_ascii_char () == '=')
751 /* Matched "<=". */
752 gfc_next_ascii_char ();
753 *result = INTRINSIC_LE;
754 return MATCH_YES;
756 /* Matched "<". */
757 *result = INTRINSIC_LT;
758 return MATCH_YES;
760 case '>':
761 if (gfc_peek_ascii_char () == '=')
763 /* Matched ">=". */
764 gfc_next_ascii_char ();
765 *result = INTRINSIC_GE;
766 return MATCH_YES;
768 /* Matched ">". */
769 *result = INTRINSIC_GT;
770 return MATCH_YES;
772 case '*':
773 if (gfc_peek_ascii_char () == '*')
775 /* Matched "**". */
776 gfc_next_ascii_char ();
777 *result = INTRINSIC_POWER;
778 return MATCH_YES;
780 /* Matched "*". */
781 *result = INTRINSIC_TIMES;
782 return MATCH_YES;
784 case '/':
785 ch = gfc_peek_ascii_char ();
786 if (ch == '=')
788 /* Matched "/=". */
789 gfc_next_ascii_char ();
790 *result = INTRINSIC_NE;
791 return MATCH_YES;
793 else if (ch == '/')
795 /* Matched "//". */
796 gfc_next_ascii_char ();
797 *result = INTRINSIC_CONCAT;
798 return MATCH_YES;
800 /* Matched "/". */
801 *result = INTRINSIC_DIVIDE;
802 return MATCH_YES;
804 case '.':
805 ch = gfc_next_ascii_char ();
806 switch (ch)
808 case 'a':
809 if (gfc_next_ascii_char () == 'n'
810 && gfc_next_ascii_char () == 'd'
811 && gfc_next_ascii_char () == '.')
813 /* Matched ".and.". */
814 *result = INTRINSIC_AND;
815 return MATCH_YES;
817 break;
819 case 'e':
820 if (gfc_next_ascii_char () == 'q')
822 ch = gfc_next_ascii_char ();
823 if (ch == '.')
825 /* Matched ".eq.". */
826 *result = INTRINSIC_EQ_OS;
827 return MATCH_YES;
829 else if (ch == 'v')
831 if (gfc_next_ascii_char () == '.')
833 /* Matched ".eqv.". */
834 *result = INTRINSIC_EQV;
835 return MATCH_YES;
839 break;
841 case 'g':
842 ch = gfc_next_ascii_char ();
843 if (ch == 'e')
845 if (gfc_next_ascii_char () == '.')
847 /* Matched ".ge.". */
848 *result = INTRINSIC_GE_OS;
849 return MATCH_YES;
852 else if (ch == 't')
854 if (gfc_next_ascii_char () == '.')
856 /* Matched ".gt.". */
857 *result = INTRINSIC_GT_OS;
858 return MATCH_YES;
861 break;
863 case 'l':
864 ch = gfc_next_ascii_char ();
865 if (ch == 'e')
867 if (gfc_next_ascii_char () == '.')
869 /* Matched ".le.". */
870 *result = INTRINSIC_LE_OS;
871 return MATCH_YES;
874 else if (ch == 't')
876 if (gfc_next_ascii_char () == '.')
878 /* Matched ".lt.". */
879 *result = INTRINSIC_LT_OS;
880 return MATCH_YES;
883 break;
885 case 'n':
886 ch = gfc_next_ascii_char ();
887 if (ch == 'e')
889 ch = gfc_next_ascii_char ();
890 if (ch == '.')
892 /* Matched ".ne.". */
893 *result = INTRINSIC_NE_OS;
894 return MATCH_YES;
896 else if (ch == 'q')
898 if (gfc_next_ascii_char () == 'v'
899 && gfc_next_ascii_char () == '.')
901 /* Matched ".neqv.". */
902 *result = INTRINSIC_NEQV;
903 return MATCH_YES;
907 else if (ch == 'o')
909 if (gfc_next_ascii_char () == 't'
910 && gfc_next_ascii_char () == '.')
912 /* Matched ".not.". */
913 *result = INTRINSIC_NOT;
914 return MATCH_YES;
917 break;
919 case 'o':
920 if (gfc_next_ascii_char () == 'r'
921 && gfc_next_ascii_char () == '.')
923 /* Matched ".or.". */
924 *result = INTRINSIC_OR;
925 return MATCH_YES;
927 break;
929 case 'x':
930 if (gfc_next_ascii_char () == 'o'
931 && gfc_next_ascii_char () == 'r'
932 && gfc_next_ascii_char () == '.')
934 if (!gfc_notify_std (GFC_STD_LEGACY, ".XOR. operator at %C"))
935 return MATCH_ERROR;
936 /* Matched ".xor." - equivalent to ".neqv.". */
937 *result = INTRINSIC_NEQV;
938 return MATCH_YES;
940 break;
942 default:
943 break;
945 break;
947 default:
948 break;
951 gfc_current_locus = orig_loc;
952 return MATCH_NO;
956 /* Match a loop control phrase:
958 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
960 If the final integer expression is not present, a constant unity
961 expression is returned. We don't return MATCH_ERROR until after
962 the equals sign is seen. */
964 match
965 gfc_match_iterator (gfc_iterator *iter, int init_flag)
967 char name[GFC_MAX_SYMBOL_LEN + 1];
968 gfc_expr *var, *e1, *e2, *e3;
969 locus start;
970 match m;
972 e1 = e2 = e3 = NULL;
974 /* Match the start of an iterator without affecting the symbol table. */
976 start = gfc_current_locus;
977 m = gfc_match (" %n =", name);
978 gfc_current_locus = start;
980 if (m != MATCH_YES)
981 return MATCH_NO;
983 m = gfc_match_variable (&var, 0);
984 if (m != MATCH_YES)
985 return MATCH_NO;
987 if (var->symtree->n.sym->attr.dimension)
989 gfc_error ("Loop variable at %C cannot be an array");
990 goto cleanup;
993 /* F2008, C617 & C565. */
994 if (var->symtree->n.sym->attr.codimension)
996 gfc_error ("Loop variable at %C cannot be a coarray");
997 goto cleanup;
1000 if (var->ref != NULL)
1002 gfc_error ("Loop variable at %C cannot be a sub-component");
1003 goto cleanup;
1006 gfc_match_char ('=');
1008 var->symtree->n.sym->attr.implied_index = 1;
1010 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
1011 if (m == MATCH_NO)
1012 goto syntax;
1013 if (m == MATCH_ERROR)
1014 goto cleanup;
1016 if (gfc_match_char (',') != MATCH_YES)
1017 goto syntax;
1019 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
1020 if (m == MATCH_NO)
1021 goto syntax;
1022 if (m == MATCH_ERROR)
1023 goto cleanup;
1025 if (gfc_match_char (',') != MATCH_YES)
1027 e3 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
1028 goto done;
1031 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
1032 if (m == MATCH_ERROR)
1033 goto cleanup;
1034 if (m == MATCH_NO)
1036 gfc_error ("Expected a step value in iterator at %C");
1037 goto cleanup;
1040 done:
1041 iter->var = var;
1042 iter->start = e1;
1043 iter->end = e2;
1044 iter->step = e3;
1045 return MATCH_YES;
1047 syntax:
1048 gfc_error ("Syntax error in iterator at %C");
1050 cleanup:
1051 gfc_free_expr (e1);
1052 gfc_free_expr (e2);
1053 gfc_free_expr (e3);
1055 return MATCH_ERROR;
1059 /* Tries to match the next non-whitespace character on the input.
1060 This subroutine does not return MATCH_ERROR.
1061 When gobble_ws is false, do not skip over leading blanks. */
1063 match
1064 gfc_match_char (char c, bool gobble_ws)
1066 locus where;
1068 where = gfc_current_locus;
1069 if (gobble_ws)
1070 gfc_gobble_whitespace ();
1072 if (gfc_next_ascii_char () == c)
1073 return MATCH_YES;
1075 gfc_current_locus = where;
1076 return MATCH_NO;
1080 /* General purpose matching subroutine. The target string is a
1081 scanf-like format string in which spaces correspond to arbitrary
1082 whitespace (including no whitespace), characters correspond to
1083 themselves. The %-codes are:
1085 %% Literal percent sign
1086 %e Expression, pointer to a pointer is set
1087 %s Symbol, pointer to the symbol is set (host_assoc = 0)
1088 %S Symbol, pointer to the symbol is set (host_assoc = 1)
1089 %n Name, character buffer is set to name
1090 %t Matches end of statement.
1091 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1092 %l Matches a statement label
1093 %v Matches a variable expression (an lvalue, except function references
1094 having a data pointer result)
1095 % Matches a required space (in free form) and optional spaces. */
1097 match
1098 gfc_match (const char *target, ...)
1100 gfc_st_label **label;
1101 int matches, *ip;
1102 locus old_loc;
1103 va_list argp;
1104 char c, *np;
1105 match m, n;
1106 void **vp;
1107 const char *p;
1109 old_loc = gfc_current_locus;
1110 va_start (argp, target);
1111 m = MATCH_NO;
1112 matches = 0;
1113 p = target;
1115 loop:
1116 c = *p++;
1117 switch (c)
1119 case ' ':
1120 gfc_gobble_whitespace ();
1121 goto loop;
1122 case '\0':
1123 m = MATCH_YES;
1124 break;
1126 case '%':
1127 c = *p++;
1128 switch (c)
1130 case 'e':
1131 vp = va_arg (argp, void **);
1132 n = gfc_match_expr ((gfc_expr **) vp);
1133 if (n != MATCH_YES)
1135 m = n;
1136 goto not_yes;
1139 matches++;
1140 goto loop;
1142 case 'v':
1143 vp = va_arg (argp, void **);
1144 n = gfc_match_variable ((gfc_expr **) vp, 0);
1145 if (n != MATCH_YES)
1147 m = n;
1148 goto not_yes;
1151 matches++;
1152 goto loop;
1154 case 's':
1155 case 'S':
1156 vp = va_arg (argp, void **);
1157 n = gfc_match_symbol ((gfc_symbol **) vp, c == 'S');
1158 if (n != MATCH_YES)
1160 m = n;
1161 goto not_yes;
1164 matches++;
1165 goto loop;
1167 case 'n':
1168 np = va_arg (argp, char *);
1169 n = gfc_match_name (np);
1170 if (n != MATCH_YES)
1172 m = n;
1173 goto not_yes;
1176 matches++;
1177 goto loop;
1179 case 'l':
1180 label = va_arg (argp, gfc_st_label **);
1181 n = gfc_match_st_label (label);
1182 if (n != MATCH_YES)
1184 m = n;
1185 goto not_yes;
1188 matches++;
1189 goto loop;
1191 case 'o':
1192 ip = va_arg (argp, int *);
1193 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1194 if (n != MATCH_YES)
1196 m = n;
1197 goto not_yes;
1200 matches++;
1201 goto loop;
1203 case 't':
1204 if (gfc_match_eos () != MATCH_YES)
1206 m = MATCH_NO;
1207 goto not_yes;
1209 goto loop;
1211 case ' ':
1212 if (gfc_match_space () == MATCH_YES)
1213 goto loop;
1214 m = MATCH_NO;
1215 goto not_yes;
1217 case '%':
1218 break; /* Fall through to character matcher. */
1220 default:
1221 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1223 /* FALLTHRU */
1225 default:
1227 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1228 expect an upper case character here! */
1229 gcc_assert (TOLOWER (c) == c);
1231 if (c == gfc_next_ascii_char ())
1232 goto loop;
1233 break;
1236 not_yes:
1237 va_end (argp);
1239 if (m != MATCH_YES)
1241 /* Clean up after a failed match. */
1242 gfc_current_locus = old_loc;
1243 va_start (argp, target);
1245 p = target;
1246 for (; matches > 0; matches--)
1248 while (*p++ != '%');
1250 switch (*p++)
1252 case '%':
1253 matches++;
1254 break; /* Skip. */
1256 /* Matches that don't have to be undone */
1257 case 'o':
1258 case 'l':
1259 case 'n':
1260 case 's':
1261 (void) va_arg (argp, void **);
1262 break;
1264 case 'e':
1265 case 'v':
1266 vp = va_arg (argp, void **);
1267 gfc_free_expr ((struct gfc_expr *)*vp);
1268 *vp = NULL;
1269 break;
1273 va_end (argp);
1276 return m;
1280 /*********************** Statement level matching **********************/
1282 /* Matches the start of a program unit, which is the program keyword
1283 followed by an obligatory symbol. */
1285 match
1286 gfc_match_program (void)
1288 gfc_symbol *sym;
1289 match m;
1291 m = gfc_match ("% %s%t", &sym);
1293 if (m == MATCH_NO)
1295 gfc_error ("Invalid form of PROGRAM statement at %C");
1296 m = MATCH_ERROR;
1299 if (m == MATCH_ERROR)
1300 return m;
1302 if (!gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL))
1303 return MATCH_ERROR;
1305 gfc_new_block = sym;
1307 return MATCH_YES;
1311 /* Match a simple assignment statement. */
1313 match
1314 gfc_match_assignment (void)
1316 gfc_expr *lvalue, *rvalue;
1317 locus old_loc;
1318 match m;
1320 old_loc = gfc_current_locus;
1322 lvalue = NULL;
1323 m = gfc_match (" %v =", &lvalue);
1324 if (m != MATCH_YES)
1326 gfc_current_locus = old_loc;
1327 gfc_free_expr (lvalue);
1328 return MATCH_NO;
1331 rvalue = NULL;
1332 m = gfc_match (" %e%t", &rvalue);
1334 if (m == MATCH_YES
1335 && rvalue->ts.type == BT_BOZ
1336 && lvalue->ts.type == BT_CLASS)
1338 m = MATCH_ERROR;
1339 gfc_error ("BOZ literal constant at %L is neither a DATA statement "
1340 "value nor an actual argument of INT/REAL/DBLE/CMPLX "
1341 "intrinsic subprogram", &rvalue->where);
1344 if (lvalue->expr_type == EXPR_CONSTANT)
1346 /* This clobbers %len and %kind. */
1347 m = MATCH_ERROR;
1348 gfc_error ("Assignment to a constant expression at %C");
1351 if (m != MATCH_YES)
1353 gfc_current_locus = old_loc;
1354 gfc_free_expr (lvalue);
1355 gfc_free_expr (rvalue);
1356 return m;
1359 if (!lvalue->symtree)
1361 gfc_free_expr (lvalue);
1362 gfc_free_expr (rvalue);
1363 return MATCH_ERROR;
1367 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1369 new_st.op = EXEC_ASSIGN;
1370 new_st.expr1 = lvalue;
1371 new_st.expr2 = rvalue;
1373 gfc_check_do_variable (lvalue->symtree);
1375 return MATCH_YES;
1379 /* Match a pointer assignment statement. */
1381 match
1382 gfc_match_pointer_assignment (void)
1384 gfc_expr *lvalue, *rvalue;
1385 locus old_loc;
1386 match m;
1388 old_loc = gfc_current_locus;
1390 lvalue = rvalue = NULL;
1391 gfc_matching_ptr_assignment = 0;
1392 gfc_matching_procptr_assignment = 0;
1394 m = gfc_match (" %v =>", &lvalue);
1395 if (m != MATCH_YES || !lvalue->symtree)
1397 m = MATCH_NO;
1398 goto cleanup;
1401 if (lvalue->symtree->n.sym->attr.proc_pointer
1402 || gfc_is_proc_ptr_comp (lvalue))
1403 gfc_matching_procptr_assignment = 1;
1404 else
1405 gfc_matching_ptr_assignment = 1;
1407 m = gfc_match (" %e%t", &rvalue);
1408 gfc_matching_ptr_assignment = 0;
1409 gfc_matching_procptr_assignment = 0;
1410 if (m != MATCH_YES)
1411 goto cleanup;
1413 new_st.op = EXEC_POINTER_ASSIGN;
1414 new_st.expr1 = lvalue;
1415 new_st.expr2 = rvalue;
1417 return MATCH_YES;
1419 cleanup:
1420 gfc_current_locus = old_loc;
1421 gfc_free_expr (lvalue);
1422 gfc_free_expr (rvalue);
1423 return m;
1427 /* We try to match an easy arithmetic IF statement. This only happens
1428 when just after having encountered a simple IF statement. This code
1429 is really duplicate with parts of the gfc_match_if code, but this is
1430 *much* easier. */
1432 static match
1433 match_arithmetic_if (void)
1435 gfc_st_label *l1, *l2, *l3;
1436 gfc_expr *expr;
1437 match m;
1439 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1440 if (m != MATCH_YES)
1441 return m;
1443 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1444 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1445 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1447 gfc_free_expr (expr);
1448 return MATCH_ERROR;
1451 if (!gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
1452 "Arithmetic IF statement at %C"))
1453 return MATCH_ERROR;
1455 new_st.op = EXEC_ARITHMETIC_IF;
1456 new_st.expr1 = expr;
1457 new_st.label1 = l1;
1458 new_st.label2 = l2;
1459 new_st.label3 = l3;
1461 return MATCH_YES;
1465 /* The IF statement is a bit of a pain. First of all, there are three
1466 forms of it, the simple IF, the IF that starts a block and the
1467 arithmetic IF.
1469 There is a problem with the simple IF and that is the fact that we
1470 only have a single level of undo information on symbols. What this
1471 means is for a simple IF, we must re-match the whole IF statement
1472 multiple times in order to guarantee that the symbol table ends up
1473 in the proper state. */
1475 static match match_simple_forall (void);
1476 static match match_simple_where (void);
1478 match
1479 gfc_match_if (gfc_statement *if_type)
1481 gfc_expr *expr;
1482 gfc_st_label *l1, *l2, *l3;
1483 locus old_loc, old_loc2;
1484 gfc_code *p;
1485 match m, n;
1487 n = gfc_match_label ();
1488 if (n == MATCH_ERROR)
1489 return n;
1491 old_loc = gfc_current_locus;
1493 m = gfc_match (" if ", &expr);
1494 if (m != MATCH_YES)
1495 return m;
1497 if (gfc_match_char ('(') != MATCH_YES)
1499 gfc_error ("Missing %<(%> in IF-expression at %C");
1500 return MATCH_ERROR;
1503 m = gfc_match ("%e", &expr);
1504 if (m != MATCH_YES)
1505 return m;
1507 old_loc2 = gfc_current_locus;
1508 gfc_current_locus = old_loc;
1510 if (gfc_match_parens () == MATCH_ERROR)
1511 return MATCH_ERROR;
1513 gfc_current_locus = old_loc2;
1515 if (gfc_match_char (')') != MATCH_YES)
1517 gfc_error ("Syntax error in IF-expression at %C");
1518 gfc_free_expr (expr);
1519 return MATCH_ERROR;
1522 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1524 if (m == MATCH_YES)
1526 if (n == MATCH_YES)
1528 gfc_error ("Block label not appropriate for arithmetic IF "
1529 "statement at %C");
1530 gfc_free_expr (expr);
1531 return MATCH_ERROR;
1534 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1535 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1536 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1538 gfc_free_expr (expr);
1539 return MATCH_ERROR;
1542 if (!gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
1543 "Arithmetic IF statement at %C"))
1544 return MATCH_ERROR;
1546 new_st.op = EXEC_ARITHMETIC_IF;
1547 new_st.expr1 = expr;
1548 new_st.label1 = l1;
1549 new_st.label2 = l2;
1550 new_st.label3 = l3;
1552 *if_type = ST_ARITHMETIC_IF;
1553 return MATCH_YES;
1556 if (gfc_match (" then%t") == MATCH_YES)
1558 new_st.op = EXEC_IF;
1559 new_st.expr1 = expr;
1560 *if_type = ST_IF_BLOCK;
1561 return MATCH_YES;
1564 if (n == MATCH_YES)
1566 gfc_error ("Block label is not appropriate for IF statement at %C");
1567 gfc_free_expr (expr);
1568 return MATCH_ERROR;
1571 /* At this point the only thing left is a simple IF statement. At
1572 this point, n has to be MATCH_NO, so we don't have to worry about
1573 re-matching a block label. From what we've got so far, try
1574 matching an assignment. */
1576 *if_type = ST_SIMPLE_IF;
1578 m = gfc_match_assignment ();
1579 if (m == MATCH_YES)
1580 goto got_match;
1582 gfc_free_expr (expr);
1583 gfc_undo_symbols ();
1584 gfc_current_locus = old_loc;
1586 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1587 assignment was found. For MATCH_NO, continue to call the various
1588 matchers. */
1589 if (m == MATCH_ERROR)
1590 return MATCH_ERROR;
1592 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1594 m = gfc_match_pointer_assignment ();
1595 if (m == MATCH_YES)
1596 goto got_match;
1598 gfc_free_expr (expr);
1599 gfc_undo_symbols ();
1600 gfc_current_locus = old_loc;
1602 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1604 /* Look at the next keyword to see which matcher to call. Matching
1605 the keyword doesn't affect the symbol table, so we don't have to
1606 restore between tries. */
1608 #define match(string, subr, statement) \
1609 if (gfc_match (string) == MATCH_YES) { m = subr(); goto got_match; }
1611 gfc_clear_error ();
1613 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1614 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1615 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1616 match ("call", gfc_match_call, ST_CALL)
1617 match ("change% team", gfc_match_change_team, ST_CHANGE_TEAM)
1618 match ("close", gfc_match_close, ST_CLOSE)
1619 match ("continue", gfc_match_continue, ST_CONTINUE)
1620 match ("cycle", gfc_match_cycle, ST_CYCLE)
1621 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1622 match ("end file", gfc_match_endfile, ST_END_FILE)
1623 match ("end team", gfc_match_end_team, ST_END_TEAM)
1624 match ("error% stop", gfc_match_error_stop, ST_ERROR_STOP)
1625 match ("event% post", gfc_match_event_post, ST_EVENT_POST)
1626 match ("event% wait", gfc_match_event_wait, ST_EVENT_WAIT)
1627 match ("exit", gfc_match_exit, ST_EXIT)
1628 match ("fail% image", gfc_match_fail_image, ST_FAIL_IMAGE)
1629 match ("flush", gfc_match_flush, ST_FLUSH)
1630 match ("forall", match_simple_forall, ST_FORALL)
1631 match ("form% team", gfc_match_form_team, ST_FORM_TEAM)
1632 match ("go to", gfc_match_goto, ST_GOTO)
1633 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1634 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1635 match ("lock", gfc_match_lock, ST_LOCK)
1636 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1637 match ("open", gfc_match_open, ST_OPEN)
1638 match ("pause", gfc_match_pause, ST_NONE)
1639 match ("print", gfc_match_print, ST_WRITE)
1640 match ("read", gfc_match_read, ST_READ)
1641 match ("return", gfc_match_return, ST_RETURN)
1642 match ("rewind", gfc_match_rewind, ST_REWIND)
1643 match ("stop", gfc_match_stop, ST_STOP)
1644 match ("wait", gfc_match_wait, ST_WAIT)
1645 match ("sync% all", gfc_match_sync_all, ST_SYNC_CALL);
1646 match ("sync% images", gfc_match_sync_images, ST_SYNC_IMAGES);
1647 match ("sync% memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
1648 match ("sync% team", gfc_match_sync_team, ST_SYNC_TEAM)
1649 match ("unlock", gfc_match_unlock, ST_UNLOCK)
1650 match ("where", match_simple_where, ST_WHERE)
1651 match ("write", gfc_match_write, ST_WRITE)
1653 if (flag_dec)
1654 match ("type", gfc_match_print, ST_WRITE)
1656 /* All else has failed, so give up. See if any of the matchers has
1657 stored an error message of some sort. */
1658 if (!gfc_error_check ())
1659 gfc_error ("Syntax error in IF-clause after %C");
1661 gfc_free_expr (expr);
1662 return MATCH_ERROR;
1664 got_match:
1665 if (m == MATCH_NO)
1666 gfc_error ("Syntax error in IF-clause after %C");
1667 if (m != MATCH_YES)
1669 gfc_free_expr (expr);
1670 return MATCH_ERROR;
1673 /* At this point, we've matched the single IF and the action clause
1674 is in new_st. Rearrange things so that the IF statement appears
1675 in new_st. */
1677 p = gfc_get_code (EXEC_IF);
1678 p->next = XCNEW (gfc_code);
1679 *p->next = new_st;
1680 p->next->loc = gfc_current_locus;
1682 p->expr1 = expr;
1684 gfc_clear_new_st ();
1686 new_st.op = EXEC_IF;
1687 new_st.block = p;
1689 return MATCH_YES;
1692 #undef match
1695 /* Match an ELSE statement. */
1697 match
1698 gfc_match_else (void)
1700 char name[GFC_MAX_SYMBOL_LEN + 1];
1702 if (gfc_match_eos () == MATCH_YES)
1703 return MATCH_YES;
1705 if (gfc_match_name (name) != MATCH_YES
1706 || gfc_current_block () == NULL
1707 || gfc_match_eos () != MATCH_YES)
1709 gfc_error ("Invalid character(s) in ELSE statement after %C");
1710 return MATCH_ERROR;
1713 if (strcmp (name, gfc_current_block ()->name) != 0)
1715 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1716 name, gfc_current_block ()->name);
1717 return MATCH_ERROR;
1720 return MATCH_YES;
1724 /* Match an ELSE IF statement. */
1726 match
1727 gfc_match_elseif (void)
1729 char name[GFC_MAX_SYMBOL_LEN + 1];
1730 gfc_expr *expr, *then;
1731 locus where;
1732 match m;
1734 if (gfc_match_char ('(') != MATCH_YES)
1736 gfc_error ("Missing %<(%> in ELSE IF expression at %C");
1737 return MATCH_ERROR;
1740 m = gfc_match (" %e ", &expr);
1741 if (m != MATCH_YES)
1742 return m;
1744 if (gfc_match_char (')') != MATCH_YES)
1746 gfc_error ("Missing %<)%> in ELSE IF expression at %C");
1747 goto cleanup;
1750 m = gfc_match (" then ", &then);
1752 where = gfc_current_locus;
1754 if (m == MATCH_YES && (gfc_match_eos () == MATCH_YES
1755 || (gfc_current_block ()
1756 && gfc_match_name (name) == MATCH_YES)))
1757 goto done;
1759 if (gfc_match_eos () == MATCH_YES)
1761 gfc_error ("Missing THEN in ELSE IF statement after %L", &where);
1762 goto cleanup;
1765 if (gfc_match_name (name) != MATCH_YES
1766 || gfc_current_block () == NULL
1767 || gfc_match_eos () != MATCH_YES)
1769 gfc_error ("Syntax error in ELSE IF statement after %L", &where);
1770 goto cleanup;
1773 if (strcmp (name, gfc_current_block ()->name) != 0)
1775 gfc_error ("Label %qs after %L doesn't match IF label %qs",
1776 name, &where, gfc_current_block ()->name);
1777 goto cleanup;
1780 if (m != MATCH_YES)
1781 return m;
1783 done:
1784 new_st.op = EXEC_IF;
1785 new_st.expr1 = expr;
1786 return MATCH_YES;
1788 cleanup:
1789 gfc_free_expr (expr);
1790 return MATCH_ERROR;
1794 /* Free a gfc_iterator structure. */
1796 void
1797 gfc_free_iterator (gfc_iterator *iter, int flag)
1800 if (iter == NULL)
1801 return;
1803 gfc_free_expr (iter->var);
1804 gfc_free_expr (iter->start);
1805 gfc_free_expr (iter->end);
1806 gfc_free_expr (iter->step);
1808 if (flag)
1809 free (iter);
1813 /* Match a CRITICAL statement. */
1814 match
1815 gfc_match_critical (void)
1817 gfc_st_label *label = NULL;
1819 if (gfc_match_label () == MATCH_ERROR)
1820 return MATCH_ERROR;
1822 if (gfc_match (" critical") != MATCH_YES)
1823 return MATCH_NO;
1825 if (gfc_match_st_label (&label) == MATCH_ERROR)
1826 return MATCH_ERROR;
1828 if (gfc_match_eos () != MATCH_YES)
1830 gfc_syntax_error (ST_CRITICAL);
1831 return MATCH_ERROR;
1834 if (gfc_pure (NULL))
1836 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1837 return MATCH_ERROR;
1840 if (gfc_find_state (COMP_DO_CONCURRENT))
1842 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1843 "block");
1844 return MATCH_ERROR;
1847 gfc_unset_implicit_pure (NULL);
1849 if (!gfc_notify_std (GFC_STD_F2008, "CRITICAL statement at %C"))
1850 return MATCH_ERROR;
1852 if (flag_coarray == GFC_FCOARRAY_NONE)
1854 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
1855 "enable");
1856 return MATCH_ERROR;
1859 if (gfc_find_state (COMP_CRITICAL))
1861 gfc_error ("Nested CRITICAL block at %C");
1862 return MATCH_ERROR;
1865 new_st.op = EXEC_CRITICAL;
1867 if (label != NULL
1868 && !gfc_reference_st_label (label, ST_LABEL_TARGET))
1869 return MATCH_ERROR;
1871 return MATCH_YES;
1875 /* Match a BLOCK statement. */
1877 match
1878 gfc_match_block (void)
1880 match m;
1882 if (gfc_match_label () == MATCH_ERROR)
1883 return MATCH_ERROR;
1885 if (gfc_match (" block") != MATCH_YES)
1886 return MATCH_NO;
1888 /* For this to be a correct BLOCK statement, the line must end now. */
1889 m = gfc_match_eos ();
1890 if (m == MATCH_ERROR)
1891 return MATCH_ERROR;
1892 if (m == MATCH_NO)
1893 return MATCH_NO;
1895 return MATCH_YES;
1899 /* Match an ASSOCIATE statement. */
1901 match
1902 gfc_match_associate (void)
1904 if (gfc_match_label () == MATCH_ERROR)
1905 return MATCH_ERROR;
1907 if (gfc_match (" associate") != MATCH_YES)
1908 return MATCH_NO;
1910 /* Match the association list. */
1911 if (gfc_match_char ('(') != MATCH_YES)
1913 gfc_error ("Expected association list at %C");
1914 return MATCH_ERROR;
1916 new_st.ext.block.assoc = NULL;
1917 while (true)
1919 gfc_association_list* newAssoc = gfc_get_association_list ();
1920 gfc_association_list* a;
1922 /* Match the next association. */
1923 if (gfc_match (" %n =>", newAssoc->name) != MATCH_YES)
1925 gfc_error ("Expected association at %C");
1926 goto assocListError;
1929 if (gfc_match (" %e", &newAssoc->target) != MATCH_YES)
1931 /* Have another go, allowing for procedure pointer selectors. */
1932 gfc_matching_procptr_assignment = 1;
1933 if (gfc_match (" %e", &newAssoc->target) != MATCH_YES)
1935 gfc_error ("Invalid association target at %C");
1936 goto assocListError;
1938 gfc_matching_procptr_assignment = 0;
1940 newAssoc->where = gfc_current_locus;
1942 /* Check that the current name is not yet in the list. */
1943 for (a = new_st.ext.block.assoc; a; a = a->next)
1944 if (!strcmp (a->name, newAssoc->name))
1946 gfc_error ("Duplicate name %qs in association at %C",
1947 newAssoc->name);
1948 goto assocListError;
1951 /* The target expression must not be coindexed. */
1952 if (gfc_is_coindexed (newAssoc->target))
1954 gfc_error ("Association target at %C must not be coindexed");
1955 goto assocListError;
1958 /* The target expression cannot be a BOZ literal constant. */
1959 if (newAssoc->target->ts.type == BT_BOZ)
1961 gfc_error ("Association target at %L cannot be a BOZ literal "
1962 "constant", &newAssoc->target->where);
1963 goto assocListError;
1966 /* The `variable' field is left blank for now; because the target is not
1967 yet resolved, we can't use gfc_has_vector_subscript to determine it
1968 for now. This is set during resolution. */
1970 /* Put it into the list. */
1971 newAssoc->next = new_st.ext.block.assoc;
1972 new_st.ext.block.assoc = newAssoc;
1974 /* Try next one or end if closing parenthesis is found. */
1975 gfc_gobble_whitespace ();
1976 if (gfc_peek_char () == ')')
1977 break;
1978 if (gfc_match_char (',') != MATCH_YES)
1980 gfc_error ("Expected %<)%> or %<,%> at %C");
1981 return MATCH_ERROR;
1984 continue;
1986 assocListError:
1987 free (newAssoc);
1988 goto error;
1990 if (gfc_match_char (')') != MATCH_YES)
1992 /* This should never happen as we peek above. */
1993 gcc_unreachable ();
1996 if (gfc_match_eos () != MATCH_YES)
1998 gfc_error ("Junk after ASSOCIATE statement at %C");
1999 goto error;
2002 return MATCH_YES;
2004 error:
2005 gfc_free_association_list (new_st.ext.block.assoc);
2006 return MATCH_ERROR;
2010 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
2011 an accessible derived type. */
2013 static match
2014 match_derived_type_spec (gfc_typespec *ts)
2016 char name[GFC_MAX_SYMBOL_LEN + 1];
2017 locus old_locus;
2018 gfc_symbol *derived, *der_type;
2019 match m = MATCH_YES;
2020 gfc_actual_arglist *decl_type_param_list = NULL;
2021 bool is_pdt_template = false;
2023 old_locus = gfc_current_locus;
2025 if (gfc_match ("%n", name) != MATCH_YES)
2027 gfc_current_locus = old_locus;
2028 return MATCH_NO;
2031 gfc_find_symbol (name, NULL, 1, &derived);
2033 /* Match the PDT spec list, if there. */
2034 if (derived && derived->attr.flavor == FL_PROCEDURE)
2036 gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &der_type);
2037 is_pdt_template = der_type
2038 && der_type->attr.flavor == FL_DERIVED
2039 && der_type->attr.pdt_template;
2042 if (is_pdt_template)
2043 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
2045 if (m == MATCH_ERROR)
2047 gfc_free_actual_arglist (decl_type_param_list);
2048 return m;
2051 if (derived && derived->attr.flavor == FL_PROCEDURE && derived->attr.generic)
2052 derived = gfc_find_dt_in_generic (derived);
2054 /* If this is a PDT, find the specific instance. */
2055 if (m == MATCH_YES && is_pdt_template)
2057 gfc_namespace *old_ns;
2059 old_ns = gfc_current_ns;
2060 while (gfc_current_ns && gfc_current_ns->parent)
2061 gfc_current_ns = gfc_current_ns->parent;
2063 if (type_param_spec_list)
2064 gfc_free_actual_arglist (type_param_spec_list);
2065 m = gfc_get_pdt_instance (decl_type_param_list, &der_type,
2066 &type_param_spec_list);
2067 gfc_free_actual_arglist (decl_type_param_list);
2069 if (m != MATCH_YES)
2070 return m;
2071 derived = der_type;
2072 gcc_assert (!derived->attr.pdt_template && derived->attr.pdt_type);
2073 gfc_set_sym_referenced (derived);
2075 gfc_current_ns = old_ns;
2078 if (derived && derived->attr.flavor == FL_DERIVED)
2080 ts->type = BT_DERIVED;
2081 ts->u.derived = derived;
2082 return MATCH_YES;
2085 gfc_current_locus = old_locus;
2086 return MATCH_NO;
2090 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
2091 gfc_match_decl_type_spec() from decl.cc, with the following exceptions:
2092 It only includes the intrinsic types from the Fortran 2003 standard
2093 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
2094 the implicit_flag is not needed, so it was removed. Derived types are
2095 identified by their name alone. */
2097 match
2098 gfc_match_type_spec (gfc_typespec *ts)
2100 match m;
2101 locus old_locus;
2102 char c, name[GFC_MAX_SYMBOL_LEN + 1];
2104 gfc_clear_ts (ts);
2105 gfc_gobble_whitespace ();
2106 old_locus = gfc_current_locus;
2108 /* If c isn't [a-z], then return immediately. */
2109 c = gfc_peek_ascii_char ();
2110 if (!ISALPHA(c))
2111 return MATCH_NO;
2113 type_param_spec_list = NULL;
2115 if (match_derived_type_spec (ts) == MATCH_YES)
2117 /* Enforce F03:C401. */
2118 if (ts->u.derived->attr.abstract)
2120 gfc_error ("Derived type %qs at %L may not be ABSTRACT",
2121 ts->u.derived->name, &old_locus);
2122 return MATCH_ERROR;
2124 return MATCH_YES;
2127 if (gfc_match ("integer") == MATCH_YES)
2129 ts->type = BT_INTEGER;
2130 ts->kind = gfc_default_integer_kind;
2131 goto kind_selector;
2134 if (gfc_match ("double precision") == MATCH_YES)
2136 ts->type = BT_REAL;
2137 ts->kind = gfc_default_double_kind;
2138 return MATCH_YES;
2141 if (gfc_match ("complex") == MATCH_YES)
2143 ts->type = BT_COMPLEX;
2144 ts->kind = gfc_default_complex_kind;
2145 goto kind_selector;
2148 if (gfc_match ("character") == MATCH_YES)
2150 ts->type = BT_CHARACTER;
2152 m = gfc_match_char_spec (ts);
2154 if (m == MATCH_NO)
2155 m = MATCH_YES;
2157 return m;
2160 /* REAL is a real pain because it can be a type, intrinsic subprogram,
2161 or list item in a type-list of an OpenMP reduction clause. Need to
2162 differentiate REAL([KIND]=scalar-int-initialization-expr) from
2163 REAL(A,[KIND]) and REAL(KIND,A). Logically, when this code was
2164 written the use of LOGICAL as a type-spec or intrinsic subprogram
2165 was overlooked. */
2167 m = gfc_match (" %n", name);
2168 if (m == MATCH_YES
2169 && (strcmp (name, "real") == 0 || strcmp (name, "logical") == 0))
2171 char c;
2172 gfc_expr *e;
2173 locus where;
2175 if (*name == 'r')
2177 ts->type = BT_REAL;
2178 ts->kind = gfc_default_real_kind;
2180 else
2182 ts->type = BT_LOGICAL;
2183 ts->kind = gfc_default_logical_kind;
2186 gfc_gobble_whitespace ();
2188 /* Prevent REAL*4, etc. */
2189 c = gfc_peek_ascii_char ();
2190 if (c == '*')
2192 gfc_error ("Invalid type-spec at %C");
2193 return MATCH_ERROR;
2196 /* Found leading colon in REAL::, a trailing ')' in for example
2197 TYPE IS (REAL), or REAL, for an OpenMP list-item. */
2198 if (c == ':' || c == ')' || (flag_openmp && c == ','))
2199 return MATCH_YES;
2201 /* Found something other than the opening '(' in REAL(... */
2202 if (c != '(')
2203 return MATCH_NO;
2204 else
2205 gfc_next_char (); /* Burn the '('. */
2207 /* Look for the optional KIND=. */
2208 where = gfc_current_locus;
2209 m = gfc_match ("%n", name);
2210 if (m == MATCH_YES)
2212 gfc_gobble_whitespace ();
2213 c = gfc_next_char ();
2214 if (c == '=')
2216 if (strcmp(name, "a") == 0 || strcmp(name, "l") == 0)
2217 return MATCH_NO;
2218 else if (strcmp(name, "kind") == 0)
2219 goto found;
2220 else
2221 return MATCH_ERROR;
2223 else
2224 gfc_current_locus = where;
2226 else
2227 gfc_current_locus = where;
2229 found:
2231 m = gfc_match_expr (&e);
2232 if (m == MATCH_NO || m == MATCH_ERROR)
2233 return m;
2235 /* If a comma appears, it is an intrinsic subprogram. */
2236 gfc_gobble_whitespace ();
2237 c = gfc_peek_ascii_char ();
2238 if (c == ',')
2240 gfc_free_expr (e);
2241 return MATCH_NO;
2244 /* If ')' appears, we have REAL(initialization-expr), here check for
2245 a scalar integer initialization-expr and valid kind parameter. */
2246 if (c == ')')
2248 bool ok = true;
2249 if (e->expr_type != EXPR_CONSTANT && e->expr_type != EXPR_VARIABLE)
2250 ok = gfc_reduce_init_expr (e);
2251 if (!ok || e->ts.type != BT_INTEGER || e->rank > 0)
2253 gfc_free_expr (e);
2254 return MATCH_NO;
2257 if (e->expr_type != EXPR_CONSTANT)
2258 goto ohno;
2260 gfc_next_char (); /* Burn the ')'. */
2261 ts->kind = (int) mpz_get_si (e->value.integer);
2262 if (gfc_validate_kind (ts->type, ts->kind , true) == -1)
2264 gfc_error ("Invalid type-spec at %C");
2265 return MATCH_ERROR;
2268 gfc_free_expr (e);
2270 return MATCH_YES;
2274 ohno:
2276 /* If a type is not matched, simply return MATCH_NO. */
2277 gfc_current_locus = old_locus;
2278 return MATCH_NO;
2280 kind_selector:
2282 gfc_gobble_whitespace ();
2284 /* This prevents INTEGER*4, etc. */
2285 if (gfc_peek_ascii_char () == '*')
2287 gfc_error ("Invalid type-spec at %C");
2288 return MATCH_ERROR;
2291 m = gfc_match_kind_spec (ts, false);
2293 /* No kind specifier found. */
2294 if (m == MATCH_NO)
2295 m = MATCH_YES;
2297 return m;
2301 /******************** FORALL subroutines ********************/
2303 /* Free a list of FORALL iterators. */
2305 void
2306 gfc_free_forall_iterator (gfc_forall_iterator *iter)
2308 gfc_forall_iterator *next;
2310 while (iter)
2312 next = iter->next;
2313 gfc_free_expr (iter->var);
2314 gfc_free_expr (iter->start);
2315 gfc_free_expr (iter->end);
2316 gfc_free_expr (iter->stride);
2317 free (iter);
2318 iter = next;
2323 /* Match an iterator as part of a FORALL statement. The format is:
2325 <var> = <start>:<end>[:<stride>]
2327 On MATCH_NO, the caller tests for the possibility that there is a
2328 scalar mask expression. */
2330 static match
2331 match_forall_iterator (gfc_forall_iterator **result)
2333 gfc_forall_iterator *iter;
2334 locus where;
2335 match m;
2337 where = gfc_current_locus;
2338 iter = XCNEW (gfc_forall_iterator);
2340 m = gfc_match_expr (&iter->var);
2341 if (m != MATCH_YES)
2342 goto cleanup;
2344 if (gfc_match_char ('=') != MATCH_YES
2345 || iter->var->expr_type != EXPR_VARIABLE)
2347 m = MATCH_NO;
2348 goto cleanup;
2351 m = gfc_match_expr (&iter->start);
2352 if (m != MATCH_YES)
2353 goto cleanup;
2355 if (gfc_match_char (':') != MATCH_YES)
2356 goto syntax;
2358 m = gfc_match_expr (&iter->end);
2359 if (m == MATCH_NO)
2360 goto syntax;
2361 if (m == MATCH_ERROR)
2362 goto cleanup;
2364 if (gfc_match_char (':') == MATCH_NO)
2365 iter->stride = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2366 else
2368 m = gfc_match_expr (&iter->stride);
2369 if (m == MATCH_NO)
2370 goto syntax;
2371 if (m == MATCH_ERROR)
2372 goto cleanup;
2375 /* Mark the iteration variable's symbol as used as a FORALL index. */
2376 iter->var->symtree->n.sym->forall_index = true;
2378 *result = iter;
2379 return MATCH_YES;
2381 syntax:
2382 gfc_error ("Syntax error in FORALL iterator at %C");
2383 m = MATCH_ERROR;
2385 cleanup:
2387 gfc_current_locus = where;
2388 gfc_free_forall_iterator (iter);
2389 return m;
2393 /* Match the header of a FORALL statement. */
2395 static match
2396 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
2398 gfc_forall_iterator *head, *tail, *new_iter;
2399 gfc_expr *msk;
2400 match m;
2402 gfc_gobble_whitespace ();
2404 head = tail = NULL;
2405 msk = NULL;
2407 if (gfc_match_char ('(') != MATCH_YES)
2408 return MATCH_NO;
2410 m = match_forall_iterator (&new_iter);
2411 if (m == MATCH_ERROR)
2412 goto cleanup;
2413 if (m == MATCH_NO)
2414 goto syntax;
2416 head = tail = new_iter;
2418 for (;;)
2420 if (gfc_match_char (',') != MATCH_YES)
2421 break;
2423 m = match_forall_iterator (&new_iter);
2424 if (m == MATCH_ERROR)
2425 goto cleanup;
2427 if (m == MATCH_YES)
2429 tail->next = new_iter;
2430 tail = new_iter;
2431 continue;
2434 /* Have to have a mask expression. */
2436 m = gfc_match_expr (&msk);
2437 if (m == MATCH_NO)
2438 goto syntax;
2439 if (m == MATCH_ERROR)
2440 goto cleanup;
2442 break;
2445 if (gfc_match_char (')') == MATCH_NO)
2446 goto syntax;
2448 *phead = head;
2449 *mask = msk;
2450 return MATCH_YES;
2452 syntax:
2453 gfc_syntax_error (ST_FORALL);
2455 cleanup:
2456 gfc_free_expr (msk);
2457 gfc_free_forall_iterator (head);
2459 return MATCH_ERROR;
2462 /* Match the rest of a simple FORALL statement that follows an
2463 IF statement. */
2465 static match
2466 match_simple_forall (void)
2468 gfc_forall_iterator *head;
2469 gfc_expr *mask;
2470 gfc_code *c;
2471 match m;
2473 mask = NULL;
2474 head = NULL;
2475 c = NULL;
2477 m = match_forall_header (&head, &mask);
2479 if (m == MATCH_NO)
2480 goto syntax;
2481 if (m != MATCH_YES)
2482 goto cleanup;
2484 m = gfc_match_assignment ();
2486 if (m == MATCH_ERROR)
2487 goto cleanup;
2488 if (m == MATCH_NO)
2490 m = gfc_match_pointer_assignment ();
2491 if (m == MATCH_ERROR)
2492 goto cleanup;
2493 if (m == MATCH_NO)
2494 goto syntax;
2497 c = XCNEW (gfc_code);
2498 *c = new_st;
2499 c->loc = gfc_current_locus;
2501 if (gfc_match_eos () != MATCH_YES)
2502 goto syntax;
2504 gfc_clear_new_st ();
2505 new_st.op = EXEC_FORALL;
2506 new_st.expr1 = mask;
2507 new_st.ext.forall_iterator = head;
2508 new_st.block = gfc_get_code (EXEC_FORALL);
2509 new_st.block->next = c;
2511 return MATCH_YES;
2513 syntax:
2514 gfc_syntax_error (ST_FORALL);
2516 cleanup:
2517 gfc_free_forall_iterator (head);
2518 gfc_free_expr (mask);
2520 return MATCH_ERROR;
2524 /* Match a FORALL statement. */
2526 match
2527 gfc_match_forall (gfc_statement *st)
2529 gfc_forall_iterator *head;
2530 gfc_expr *mask;
2531 gfc_code *c;
2532 match m0, m;
2534 head = NULL;
2535 mask = NULL;
2536 c = NULL;
2538 m0 = gfc_match_label ();
2539 if (m0 == MATCH_ERROR)
2540 return MATCH_ERROR;
2542 m = gfc_match (" forall");
2543 if (m != MATCH_YES)
2544 return m;
2546 m = match_forall_header (&head, &mask);
2547 if (m == MATCH_ERROR)
2548 goto cleanup;
2549 if (m == MATCH_NO)
2550 goto syntax;
2552 if (gfc_match_eos () == MATCH_YES)
2554 *st = ST_FORALL_BLOCK;
2555 new_st.op = EXEC_FORALL;
2556 new_st.expr1 = mask;
2557 new_st.ext.forall_iterator = head;
2558 return MATCH_YES;
2561 m = gfc_match_assignment ();
2562 if (m == MATCH_ERROR)
2563 goto cleanup;
2564 if (m == MATCH_NO)
2566 m = gfc_match_pointer_assignment ();
2567 if (m == MATCH_ERROR)
2568 goto cleanup;
2569 if (m == MATCH_NO)
2570 goto syntax;
2573 c = XCNEW (gfc_code);
2574 *c = new_st;
2575 c->loc = gfc_current_locus;
2577 gfc_clear_new_st ();
2578 new_st.op = EXEC_FORALL;
2579 new_st.expr1 = mask;
2580 new_st.ext.forall_iterator = head;
2581 new_st.block = gfc_get_code (EXEC_FORALL);
2582 new_st.block->next = c;
2584 *st = ST_FORALL;
2585 return MATCH_YES;
2587 syntax:
2588 gfc_syntax_error (ST_FORALL);
2590 cleanup:
2591 gfc_free_forall_iterator (head);
2592 gfc_free_expr (mask);
2593 gfc_free_statements (c);
2594 return MATCH_NO;
2598 /* Match a DO statement. */
2600 match
2601 gfc_match_do (void)
2603 gfc_iterator iter, *ip;
2604 locus old_loc;
2605 gfc_st_label *label;
2606 match m;
2608 old_loc = gfc_current_locus;
2610 memset (&iter, '\0', sizeof (gfc_iterator));
2611 label = NULL;
2613 m = gfc_match_label ();
2614 if (m == MATCH_ERROR)
2615 return m;
2617 if (gfc_match (" do") != MATCH_YES)
2618 return MATCH_NO;
2620 m = gfc_match_st_label (&label);
2621 if (m == MATCH_ERROR)
2622 goto cleanup;
2624 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2626 if (gfc_match_eos () == MATCH_YES)
2628 iter.end = gfc_get_logical_expr (gfc_default_logical_kind, NULL, true);
2629 new_st.op = EXEC_DO_WHILE;
2630 goto done;
2633 /* Match an optional comma, if no comma is found, a space is obligatory. */
2634 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
2635 return MATCH_NO;
2637 /* Check for balanced parens. */
2639 if (gfc_match_parens () == MATCH_ERROR)
2640 return MATCH_ERROR;
2642 if (gfc_match (" concurrent") == MATCH_YES)
2644 gfc_forall_iterator *head;
2645 gfc_expr *mask;
2647 if (!gfc_notify_std (GFC_STD_F2008, "DO CONCURRENT construct at %C"))
2648 return MATCH_ERROR;
2651 mask = NULL;
2652 head = NULL;
2653 m = match_forall_header (&head, &mask);
2655 if (m == MATCH_NO)
2656 return m;
2657 if (m == MATCH_ERROR)
2658 goto concurr_cleanup;
2660 if (gfc_match_eos () != MATCH_YES)
2661 goto concurr_cleanup;
2663 if (label != NULL
2664 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2665 goto concurr_cleanup;
2667 new_st.label1 = label;
2668 new_st.op = EXEC_DO_CONCURRENT;
2669 new_st.expr1 = mask;
2670 new_st.ext.forall_iterator = head;
2672 return MATCH_YES;
2674 concurr_cleanup:
2675 gfc_syntax_error (ST_DO);
2676 gfc_free_expr (mask);
2677 gfc_free_forall_iterator (head);
2678 return MATCH_ERROR;
2681 /* See if we have a DO WHILE. */
2682 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
2684 new_st.op = EXEC_DO_WHILE;
2685 goto done;
2688 /* The abortive DO WHILE may have done something to the symbol
2689 table, so we start over. */
2690 gfc_undo_symbols ();
2691 gfc_current_locus = old_loc;
2693 gfc_match_label (); /* This won't error. */
2694 gfc_match (" do "); /* This will work. */
2696 gfc_match_st_label (&label); /* Can't error out. */
2697 gfc_match_char (','); /* Optional comma. */
2699 m = gfc_match_iterator (&iter, 0);
2700 if (m == MATCH_NO)
2701 return MATCH_NO;
2702 if (m == MATCH_ERROR)
2703 goto cleanup;
2705 iter.var->symtree->n.sym->attr.implied_index = 0;
2706 gfc_check_do_variable (iter.var->symtree);
2708 if (gfc_match_eos () != MATCH_YES)
2710 gfc_syntax_error (ST_DO);
2711 goto cleanup;
2714 new_st.op = EXEC_DO;
2716 done:
2717 if (label != NULL
2718 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2719 goto cleanup;
2721 new_st.label1 = label;
2723 if (new_st.op == EXEC_DO_WHILE)
2724 new_st.expr1 = iter.end;
2725 else
2727 new_st.ext.iterator = ip = gfc_get_iterator ();
2728 *ip = iter;
2731 return MATCH_YES;
2733 cleanup:
2734 gfc_free_iterator (&iter, 0);
2736 return MATCH_ERROR;
2740 /* Match an EXIT or CYCLE statement. */
2742 static match
2743 match_exit_cycle (gfc_statement st, gfc_exec_op op)
2745 gfc_state_data *p, *o;
2746 gfc_symbol *sym;
2747 match m;
2748 int cnt;
2750 if (gfc_match_eos () == MATCH_YES)
2751 sym = NULL;
2752 else
2754 char name[GFC_MAX_SYMBOL_LEN + 1];
2755 gfc_symtree* stree;
2757 m = gfc_match ("% %n%t", name);
2758 if (m == MATCH_ERROR)
2759 return MATCH_ERROR;
2760 if (m == MATCH_NO)
2762 gfc_syntax_error (st);
2763 return MATCH_ERROR;
2766 /* Find the corresponding symbol. If there's a BLOCK statement
2767 between here and the label, it is not in gfc_current_ns but a parent
2768 namespace! */
2769 stree = gfc_find_symtree_in_proc (name, gfc_current_ns);
2770 if (!stree)
2772 gfc_error ("Name %qs in %s statement at %C is unknown",
2773 name, gfc_ascii_statement (st));
2774 return MATCH_ERROR;
2777 sym = stree->n.sym;
2778 if (sym->attr.flavor != FL_LABEL)
2780 gfc_error ("Name %qs in %s statement at %C is not a construct name",
2781 name, gfc_ascii_statement (st));
2782 return MATCH_ERROR;
2786 /* Find the loop specified by the label (or lack of a label). */
2787 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
2788 if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
2789 o = p;
2790 else if (p->state == COMP_CRITICAL)
2792 gfc_error("%s statement at %C leaves CRITICAL construct",
2793 gfc_ascii_statement (st));
2794 return MATCH_ERROR;
2796 else if (p->state == COMP_DO_CONCURRENT
2797 && (op == EXEC_EXIT || (sym && sym != p->sym)))
2799 /* F2008, C821 & C845. */
2800 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2801 gfc_ascii_statement (st));
2802 return MATCH_ERROR;
2804 else if ((sym && sym == p->sym)
2805 || (!sym && (p->state == COMP_DO
2806 || p->state == COMP_DO_CONCURRENT)))
2807 break;
2809 if (p == NULL)
2811 if (sym == NULL)
2812 gfc_error ("%s statement at %C is not within a construct",
2813 gfc_ascii_statement (st));
2814 else
2815 gfc_error ("%s statement at %C is not within construct %qs",
2816 gfc_ascii_statement (st), sym->name);
2818 return MATCH_ERROR;
2821 /* Special checks for EXIT from non-loop constructs. */
2822 switch (p->state)
2824 case COMP_DO:
2825 case COMP_DO_CONCURRENT:
2826 break;
2828 case COMP_CRITICAL:
2829 /* This is already handled above. */
2830 gcc_unreachable ();
2832 case COMP_ASSOCIATE:
2833 case COMP_BLOCK:
2834 case COMP_IF:
2835 case COMP_SELECT:
2836 case COMP_SELECT_TYPE:
2837 case COMP_SELECT_RANK:
2838 gcc_assert (sym);
2839 if (op == EXEC_CYCLE)
2841 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2842 " construct %qs", sym->name);
2843 return MATCH_ERROR;
2845 gcc_assert (op == EXEC_EXIT);
2846 if (!gfc_notify_std (GFC_STD_F2008, "EXIT statement with no"
2847 " do-construct-name at %C"))
2848 return MATCH_ERROR;
2849 break;
2851 default:
2852 gfc_error ("%s statement at %C is not applicable to construct %qs",
2853 gfc_ascii_statement (st), sym->name);
2854 return MATCH_ERROR;
2857 if (o != NULL)
2859 gfc_error (is_oacc (p)
2860 ? G_("%s statement at %C leaving OpenACC structured block")
2861 : G_("%s statement at %C leaving OpenMP structured block"),
2862 gfc_ascii_statement (st));
2863 return MATCH_ERROR;
2866 for (o = p, cnt = 0; o->state == COMP_DO && o->previous != NULL; cnt++)
2867 o = o->previous;
2869 int count = 1;
2870 if (cnt > 0
2871 && o != NULL
2872 && o->state == COMP_OMP_STRUCTURED_BLOCK)
2873 switch (o->head->op)
2875 case EXEC_OACC_LOOP:
2876 case EXEC_OACC_KERNELS_LOOP:
2877 case EXEC_OACC_PARALLEL_LOOP:
2878 case EXEC_OACC_SERIAL_LOOP:
2879 gcc_assert (o->head->next != NULL
2880 && (o->head->next->op == EXEC_DO
2881 || o->head->next->op == EXEC_DO_WHILE)
2882 && o->previous != NULL
2883 && o->previous->tail->op == o->head->op);
2884 if (o->previous->tail->ext.omp_clauses != NULL)
2886 /* Both collapsed and tiled loops are lowered the same way, but are
2887 not compatible. In gfc_trans_omp_do, the tile is prioritized. */
2888 if (o->previous->tail->ext.omp_clauses->tile_list)
2890 count = 0;
2891 gfc_expr_list *el
2892 = o->previous->tail->ext.omp_clauses->tile_list;
2893 for ( ; el; el = el->next)
2894 ++count;
2896 else if (o->previous->tail->ext.omp_clauses->collapse > 1)
2897 count = o->previous->tail->ext.omp_clauses->collapse;
2899 if (st == ST_EXIT && cnt <= count)
2901 gfc_error ("EXIT statement at %C terminating !$ACC LOOP loop");
2902 return MATCH_ERROR;
2904 if (st == ST_CYCLE && cnt < count)
2906 gfc_error (o->previous->tail->ext.omp_clauses->tile_list
2907 ? G_("CYCLE statement at %C to non-innermost tiled "
2908 "!$ACC LOOP loop")
2909 : G_("CYCLE statement at %C to non-innermost collapsed "
2910 "!$ACC LOOP loop"));
2911 return MATCH_ERROR;
2913 break;
2914 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2915 case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
2916 case EXEC_OMP_TARGET_SIMD:
2917 case EXEC_OMP_TASKLOOP_SIMD:
2918 case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
2919 case EXEC_OMP_MASTER_TASKLOOP_SIMD:
2920 case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
2921 case EXEC_OMP_MASKED_TASKLOOP_SIMD:
2922 case EXEC_OMP_PARALLEL_DO_SIMD:
2923 case EXEC_OMP_DISTRIBUTE_SIMD:
2924 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
2925 case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
2926 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
2927 case EXEC_OMP_LOOP:
2928 case EXEC_OMP_PARALLEL_LOOP:
2929 case EXEC_OMP_TEAMS_LOOP:
2930 case EXEC_OMP_TARGET_PARALLEL_LOOP:
2931 case EXEC_OMP_TARGET_TEAMS_LOOP:
2932 case EXEC_OMP_DO:
2933 case EXEC_OMP_PARALLEL_DO:
2934 case EXEC_OMP_SIMD:
2935 case EXEC_OMP_DO_SIMD:
2936 case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
2937 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
2938 case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
2939 case EXEC_OMP_TARGET_PARALLEL_DO:
2940 case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
2942 gcc_assert (o->head->next != NULL
2943 && (o->head->next->op == EXEC_DO
2944 || o->head->next->op == EXEC_DO_WHILE)
2945 && o->previous != NULL
2946 && o->previous->tail->op == o->head->op);
2947 if (o->previous->tail->ext.omp_clauses != NULL)
2949 if (o->previous->tail->ext.omp_clauses->collapse > 1)
2950 count = o->previous->tail->ext.omp_clauses->collapse;
2951 if (o->previous->tail->ext.omp_clauses->orderedc)
2952 count = o->previous->tail->ext.omp_clauses->orderedc;
2954 if (st == ST_EXIT && cnt <= count)
2956 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2957 return MATCH_ERROR;
2959 if (st == ST_CYCLE && cnt < count)
2961 gfc_error ("CYCLE statement at %C to non-innermost collapsed "
2962 "!$OMP DO loop");
2963 return MATCH_ERROR;
2965 break;
2966 default:
2967 break;
2970 /* Save the first statement in the construct - needed by the backend. */
2971 new_st.ext.which_construct = p->construct;
2973 new_st.op = op;
2975 return MATCH_YES;
2979 /* Match the EXIT statement. */
2981 match
2982 gfc_match_exit (void)
2984 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
2988 /* Match the CYCLE statement. */
2990 match
2991 gfc_match_cycle (void)
2993 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
2997 /* Match a stop-code after an (ERROR) STOP or PAUSE statement. The
2998 requirements for a stop-code differ in the standards.
3000 Fortran 95 has
3002 R840 stop-stmt is STOP [ stop-code ]
3003 R841 stop-code is scalar-char-constant
3004 or digit [ digit [ digit [ digit [ digit ] ] ] ]
3006 Fortran 2003 matches Fortran 95 except R840 and R841 are now R849 and R850.
3007 Fortran 2008 has
3009 R855 stop-stmt is STOP [ stop-code ]
3010 R856 allstop-stmt is ALL STOP [ stop-code ]
3011 R857 stop-code is scalar-default-char-constant-expr
3012 or scalar-int-constant-expr
3013 Fortran 2018 has
3015 R1160 stop-stmt is STOP [ stop-code ] [ , QUIET = scalar-logical-expr]
3016 R1161 error-stop-stmt is
3017 ERROR STOP [ stop-code ] [ , QUIET = scalar-logical-expr]
3018 R1162 stop-code is scalar-default-char-expr
3019 or scalar-int-expr
3021 For free-form source code, all standards contain a statement of the form:
3023 A blank shall be used to separate names, constants, or labels from
3024 adjacent keywords, names, constants, or labels.
3026 A stop-code is not a name, constant, or label. So, under Fortran 95 and 2003,
3028 STOP123
3030 is valid, but it is invalid Fortran 2008. */
3032 static match
3033 gfc_match_stopcode (gfc_statement st)
3035 gfc_expr *e = NULL;
3036 gfc_expr *quiet = NULL;
3037 match m;
3038 bool f95, f03, f08;
3039 char c;
3041 /* Set f95 for -std=f95. */
3042 f95 = (gfc_option.allow_std == GFC_STD_OPT_F95);
3044 /* Set f03 for -std=f2003. */
3045 f03 = (gfc_option.allow_std == GFC_STD_OPT_F03);
3047 /* Set f08 for -std=f2008. */
3048 f08 = (gfc_option.allow_std == GFC_STD_OPT_F08);
3050 /* Plain STOP statement? */
3051 if (gfc_match_eos () == MATCH_YES)
3052 goto checks;
3054 /* Look for a blank between STOP and the stop-code for F2008 or later.
3055 But allow for F2018's ,QUIET= specifier. */
3056 c = gfc_peek_ascii_char ();
3058 if (gfc_current_form != FORM_FIXED && !(f95 || f03) && c != ',')
3060 /* Look for end-of-statement. There is no stop-code. */
3061 if (c == '\n' || c == '!' || c == ';')
3062 goto done;
3064 if (c != ' ')
3066 gfc_error ("Blank required in %s statement near %C",
3067 gfc_ascii_statement (st));
3068 return MATCH_ERROR;
3072 if (c == ' ')
3074 gfc_gobble_whitespace ();
3075 c = gfc_peek_ascii_char ();
3077 if (c != ',')
3079 int stopcode;
3080 locus old_locus;
3082 /* First look for the F95 or F2003 digit [...] construct. */
3083 old_locus = gfc_current_locus;
3084 m = gfc_match_small_int (&stopcode);
3085 if (m == MATCH_YES && (f95 || f03))
3087 if (stopcode < 0)
3089 gfc_error ("STOP code at %C cannot be negative");
3090 return MATCH_ERROR;
3093 if (stopcode > 99999)
3095 gfc_error ("STOP code at %C contains too many digits");
3096 return MATCH_ERROR;
3100 /* Reset the locus and now load gfc_expr. */
3101 gfc_current_locus = old_locus;
3102 m = gfc_match_expr (&e);
3103 if (m == MATCH_ERROR)
3104 goto cleanup;
3105 if (m == MATCH_NO)
3106 goto syntax;
3109 if (gfc_match (" , quiet = %e", &quiet) == MATCH_YES)
3111 if (!gfc_notify_std (GFC_STD_F2018, "QUIET= specifier for %s at %L",
3112 gfc_ascii_statement (st), &quiet->where))
3113 goto cleanup;
3116 if (gfc_match_eos () != MATCH_YES)
3117 goto syntax;
3119 checks:
3121 if (gfc_pure (NULL))
3123 if (st == ST_ERROR_STOP)
3125 if (!gfc_notify_std (GFC_STD_F2018, "%s statement at %C in PURE "
3126 "procedure", gfc_ascii_statement (st)))
3127 goto cleanup;
3129 else
3131 gfc_error ("%s statement not allowed in PURE procedure at %C",
3132 gfc_ascii_statement (st));
3133 goto cleanup;
3137 gfc_unset_implicit_pure (NULL);
3139 if (st == ST_STOP && gfc_find_state (COMP_CRITICAL))
3141 gfc_error ("Image control statement STOP at %C in CRITICAL block");
3142 goto cleanup;
3144 if (st == ST_STOP && gfc_find_state (COMP_DO_CONCURRENT))
3146 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
3147 goto cleanup;
3150 if (e != NULL)
3152 if (!gfc_simplify_expr (e, 0))
3153 goto cleanup;
3155 /* Test for F95 and F2003 style STOP stop-code. */
3156 if (e->expr_type != EXPR_CONSTANT && (f95 || f03))
3158 gfc_error ("STOP code at %L must be a scalar CHARACTER constant "
3159 "or digit[digit[digit[digit[digit]]]]", &e->where);
3160 goto cleanup;
3163 /* Use the machinery for an initialization expression to reduce the
3164 stop-code to a constant. */
3165 gfc_reduce_init_expr (e);
3167 /* Test for F2008 style STOP stop-code. */
3168 if (e->expr_type != EXPR_CONSTANT && f08)
3170 gfc_error ("STOP code at %L must be a scalar default CHARACTER or "
3171 "INTEGER constant expression", &e->where);
3172 goto cleanup;
3175 if (!(e->ts.type == BT_CHARACTER || e->ts.type == BT_INTEGER))
3177 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
3178 &e->where);
3179 goto cleanup;
3182 if (e->rank != 0)
3184 gfc_error ("STOP code at %L must be scalar", &e->where);
3185 goto cleanup;
3188 if (e->ts.type == BT_CHARACTER
3189 && e->ts.kind != gfc_default_character_kind)
3191 gfc_error ("STOP code at %L must be default character KIND=%d",
3192 &e->where, (int) gfc_default_character_kind);
3193 goto cleanup;
3196 if (e->ts.type == BT_INTEGER && e->ts.kind != gfc_default_integer_kind
3197 && !gfc_notify_std (GFC_STD_F2018,
3198 "STOP code at %L must be default integer KIND=%d",
3199 &e->where, (int) gfc_default_integer_kind))
3200 goto cleanup;
3203 if (quiet != NULL)
3205 if (!gfc_simplify_expr (quiet, 0))
3206 goto cleanup;
3208 if (quiet->rank != 0)
3210 gfc_error ("QUIET specifier at %L must be a scalar LOGICAL",
3211 &quiet->where);
3212 goto cleanup;
3216 done:
3218 switch (st)
3220 case ST_STOP:
3221 new_st.op = EXEC_STOP;
3222 break;
3223 case ST_ERROR_STOP:
3224 new_st.op = EXEC_ERROR_STOP;
3225 break;
3226 case ST_PAUSE:
3227 new_st.op = EXEC_PAUSE;
3228 break;
3229 default:
3230 gcc_unreachable ();
3233 new_st.expr1 = e;
3234 new_st.expr2 = quiet;
3235 new_st.ext.stop_code = -1;
3237 return MATCH_YES;
3239 syntax:
3240 gfc_syntax_error (st);
3242 cleanup:
3244 gfc_free_expr (e);
3245 gfc_free_expr (quiet);
3246 return MATCH_ERROR;
3250 /* Match the (deprecated) PAUSE statement. */
3252 match
3253 gfc_match_pause (void)
3255 match m;
3257 m = gfc_match_stopcode (ST_PAUSE);
3258 if (m == MATCH_YES)
3260 if (!gfc_notify_std (GFC_STD_F95_DEL, "PAUSE statement at %C"))
3261 m = MATCH_ERROR;
3263 return m;
3267 /* Match the STOP statement. */
3269 match
3270 gfc_match_stop (void)
3272 return gfc_match_stopcode (ST_STOP);
3276 /* Match the ERROR STOP statement. */
3278 match
3279 gfc_match_error_stop (void)
3281 if (!gfc_notify_std (GFC_STD_F2008, "ERROR STOP statement at %C"))
3282 return MATCH_ERROR;
3284 return gfc_match_stopcode (ST_ERROR_STOP);
3287 /* Match EVENT POST/WAIT statement. Syntax:
3288 EVENT POST ( event-variable [, sync-stat-list] )
3289 EVENT WAIT ( event-variable [, wait-spec-list] )
3290 with
3291 wait-spec-list is sync-stat-list or until-spec
3292 until-spec is UNTIL_COUNT = scalar-int-expr
3293 sync-stat is STAT= or ERRMSG=. */
3295 static match
3296 event_statement (gfc_statement st)
3298 match m;
3299 gfc_expr *tmp, *eventvar, *until_count, *stat, *errmsg;
3300 bool saw_until_count, saw_stat, saw_errmsg;
3302 tmp = eventvar = until_count = stat = errmsg = NULL;
3303 saw_until_count = saw_stat = saw_errmsg = false;
3305 if (gfc_pure (NULL))
3307 gfc_error ("Image control statement EVENT %s at %C in PURE procedure",
3308 st == ST_EVENT_POST ? "POST" : "WAIT");
3309 return MATCH_ERROR;
3312 gfc_unset_implicit_pure (NULL);
3314 if (flag_coarray == GFC_FCOARRAY_NONE)
3316 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3317 return MATCH_ERROR;
3320 if (gfc_find_state (COMP_CRITICAL))
3322 gfc_error ("Image control statement EVENT %s at %C in CRITICAL block",
3323 st == ST_EVENT_POST ? "POST" : "WAIT");
3324 return MATCH_ERROR;
3327 if (gfc_find_state (COMP_DO_CONCURRENT))
3329 gfc_error ("Image control statement EVENT %s at %C in DO CONCURRENT "
3330 "block", st == ST_EVENT_POST ? "POST" : "WAIT");
3331 return MATCH_ERROR;
3334 if (gfc_match_char ('(') != MATCH_YES)
3335 goto syntax;
3337 if (gfc_match ("%e", &eventvar) != MATCH_YES)
3338 goto syntax;
3339 m = gfc_match_char (',');
3340 if (m == MATCH_ERROR)
3341 goto syntax;
3342 if (m == MATCH_NO)
3344 m = gfc_match_char (')');
3345 if (m == MATCH_YES)
3346 goto done;
3347 goto syntax;
3350 for (;;)
3352 m = gfc_match (" stat = %v", &tmp);
3353 if (m == MATCH_ERROR)
3354 goto syntax;
3355 if (m == MATCH_YES)
3357 if (saw_stat)
3359 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
3360 goto cleanup;
3362 stat = tmp;
3363 saw_stat = true;
3365 m = gfc_match_char (',');
3366 if (m == MATCH_YES)
3367 continue;
3369 tmp = NULL;
3370 break;
3373 m = gfc_match (" errmsg = %v", &tmp);
3374 if (m == MATCH_ERROR)
3375 goto syntax;
3376 if (m == MATCH_YES)
3378 if (saw_errmsg)
3380 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
3381 goto cleanup;
3383 errmsg = tmp;
3384 saw_errmsg = true;
3386 m = gfc_match_char (',');
3387 if (m == MATCH_YES)
3388 continue;
3390 tmp = NULL;
3391 break;
3394 m = gfc_match (" until_count = %e", &tmp);
3395 if (m == MATCH_ERROR || st == ST_EVENT_POST)
3396 goto syntax;
3397 if (m == MATCH_YES)
3399 if (saw_until_count)
3401 gfc_error ("Redundant UNTIL_COUNT tag found at %L",
3402 &tmp->where);
3403 goto cleanup;
3405 until_count = tmp;
3406 saw_until_count = true;
3408 m = gfc_match_char (',');
3409 if (m == MATCH_YES)
3410 continue;
3412 tmp = NULL;
3413 break;
3416 break;
3419 if (m == MATCH_ERROR)
3420 goto syntax;
3422 if (gfc_match (" )%t") != MATCH_YES)
3423 goto syntax;
3425 done:
3426 switch (st)
3428 case ST_EVENT_POST:
3429 new_st.op = EXEC_EVENT_POST;
3430 break;
3431 case ST_EVENT_WAIT:
3432 new_st.op = EXEC_EVENT_WAIT;
3433 break;
3434 default:
3435 gcc_unreachable ();
3438 new_st.expr1 = eventvar;
3439 new_st.expr2 = stat;
3440 new_st.expr3 = errmsg;
3441 new_st.expr4 = until_count;
3443 return MATCH_YES;
3445 syntax:
3446 gfc_syntax_error (st);
3448 cleanup:
3449 if (until_count != tmp)
3450 gfc_free_expr (until_count);
3451 if (errmsg != tmp)
3452 gfc_free_expr (errmsg);
3453 if (stat != tmp)
3454 gfc_free_expr (stat);
3456 gfc_free_expr (tmp);
3457 gfc_free_expr (eventvar);
3459 return MATCH_ERROR;
3464 match
3465 gfc_match_event_post (void)
3467 if (!gfc_notify_std (GFC_STD_F2018, "EVENT POST statement at %C"))
3468 return MATCH_ERROR;
3470 return event_statement (ST_EVENT_POST);
3474 match
3475 gfc_match_event_wait (void)
3477 if (!gfc_notify_std (GFC_STD_F2018, "EVENT WAIT statement at %C"))
3478 return MATCH_ERROR;
3480 return event_statement (ST_EVENT_WAIT);
3484 /* Match a FAIL IMAGE statement. */
3486 match
3487 gfc_match_fail_image (void)
3489 if (!gfc_notify_std (GFC_STD_F2018, "FAIL IMAGE statement at %C"))
3490 return MATCH_ERROR;
3492 if (gfc_match_char ('(') == MATCH_YES)
3493 goto syntax;
3495 new_st.op = EXEC_FAIL_IMAGE;
3497 return MATCH_YES;
3499 syntax:
3500 gfc_syntax_error (ST_FAIL_IMAGE);
3502 return MATCH_ERROR;
3505 /* Match a FORM TEAM statement. */
3507 match
3508 gfc_match_form_team (void)
3510 match m;
3511 gfc_expr *teamid,*team;
3513 if (!gfc_notify_std (GFC_STD_F2018, "FORM TEAM statement at %C"))
3514 return MATCH_ERROR;
3516 if (gfc_match_char ('(') == MATCH_NO)
3517 goto syntax;
3519 new_st.op = EXEC_FORM_TEAM;
3521 if (gfc_match ("%e", &teamid) != MATCH_YES)
3522 goto syntax;
3523 m = gfc_match_char (',');
3524 if (m == MATCH_ERROR)
3525 goto syntax;
3526 if (gfc_match ("%e", &team) != MATCH_YES)
3527 goto syntax;
3529 m = gfc_match_char (')');
3530 if (m == MATCH_NO)
3531 goto syntax;
3533 new_st.expr1 = teamid;
3534 new_st.expr2 = team;
3536 return MATCH_YES;
3538 syntax:
3539 gfc_syntax_error (ST_FORM_TEAM);
3541 return MATCH_ERROR;
3544 /* Match a CHANGE TEAM statement. */
3546 match
3547 gfc_match_change_team (void)
3549 match m;
3550 gfc_expr *team;
3552 if (!gfc_notify_std (GFC_STD_F2018, "CHANGE TEAM statement at %C"))
3553 return MATCH_ERROR;
3555 if (gfc_match_char ('(') == MATCH_NO)
3556 goto syntax;
3558 new_st.op = EXEC_CHANGE_TEAM;
3560 if (gfc_match ("%e", &team) != MATCH_YES)
3561 goto syntax;
3563 m = gfc_match_char (')');
3564 if (m == MATCH_NO)
3565 goto syntax;
3567 new_st.expr1 = team;
3569 return MATCH_YES;
3571 syntax:
3572 gfc_syntax_error (ST_CHANGE_TEAM);
3574 return MATCH_ERROR;
3577 /* Match a END TEAM statement. */
3579 match
3580 gfc_match_end_team (void)
3582 if (!gfc_notify_std (GFC_STD_F2018, "END TEAM statement at %C"))
3583 return MATCH_ERROR;
3585 if (gfc_match_char ('(') == MATCH_YES)
3586 goto syntax;
3588 new_st.op = EXEC_END_TEAM;
3590 return MATCH_YES;
3592 syntax:
3593 gfc_syntax_error (ST_END_TEAM);
3595 return MATCH_ERROR;
3598 /* Match a SYNC TEAM statement. */
3600 match
3601 gfc_match_sync_team (void)
3603 match m;
3604 gfc_expr *team;
3606 if (!gfc_notify_std (GFC_STD_F2018, "SYNC TEAM statement at %C"))
3607 return MATCH_ERROR;
3609 if (gfc_match_char ('(') == MATCH_NO)
3610 goto syntax;
3612 new_st.op = EXEC_SYNC_TEAM;
3614 if (gfc_match ("%e", &team) != MATCH_YES)
3615 goto syntax;
3617 m = gfc_match_char (')');
3618 if (m == MATCH_NO)
3619 goto syntax;
3621 new_st.expr1 = team;
3623 return MATCH_YES;
3625 syntax:
3626 gfc_syntax_error (ST_SYNC_TEAM);
3628 return MATCH_ERROR;
3631 /* Match LOCK/UNLOCK statement. Syntax:
3632 LOCK ( lock-variable [ , lock-stat-list ] )
3633 UNLOCK ( lock-variable [ , sync-stat-list ] )
3634 where lock-stat is ACQUIRED_LOCK or sync-stat
3635 and sync-stat is STAT= or ERRMSG=. */
3637 static match
3638 lock_unlock_statement (gfc_statement st)
3640 match m;
3641 gfc_expr *tmp, *lockvar, *acq_lock, *stat, *errmsg;
3642 bool saw_acq_lock, saw_stat, saw_errmsg;
3644 tmp = lockvar = acq_lock = stat = errmsg = NULL;
3645 saw_acq_lock = saw_stat = saw_errmsg = false;
3647 if (gfc_pure (NULL))
3649 gfc_error ("Image control statement %s at %C in PURE procedure",
3650 st == ST_LOCK ? "LOCK" : "UNLOCK");
3651 return MATCH_ERROR;
3654 gfc_unset_implicit_pure (NULL);
3656 if (flag_coarray == GFC_FCOARRAY_NONE)
3658 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3659 return MATCH_ERROR;
3662 if (gfc_find_state (COMP_CRITICAL))
3664 gfc_error ("Image control statement %s at %C in CRITICAL block",
3665 st == ST_LOCK ? "LOCK" : "UNLOCK");
3666 return MATCH_ERROR;
3669 if (gfc_find_state (COMP_DO_CONCURRENT))
3671 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
3672 st == ST_LOCK ? "LOCK" : "UNLOCK");
3673 return MATCH_ERROR;
3676 if (gfc_match_char ('(') != MATCH_YES)
3677 goto syntax;
3679 if (gfc_match ("%e", &lockvar) != MATCH_YES)
3680 goto syntax;
3681 m = gfc_match_char (',');
3682 if (m == MATCH_ERROR)
3683 goto syntax;
3684 if (m == MATCH_NO)
3686 m = gfc_match_char (')');
3687 if (m == MATCH_YES)
3688 goto done;
3689 goto syntax;
3692 for (;;)
3694 m = gfc_match (" stat = %v", &tmp);
3695 if (m == MATCH_ERROR)
3696 goto syntax;
3697 if (m == MATCH_YES)
3699 if (saw_stat)
3701 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
3702 goto cleanup;
3704 stat = tmp;
3705 saw_stat = true;
3707 m = gfc_match_char (',');
3708 if (m == MATCH_YES)
3709 continue;
3711 tmp = NULL;
3712 break;
3715 m = gfc_match (" errmsg = %v", &tmp);
3716 if (m == MATCH_ERROR)
3717 goto syntax;
3718 if (m == MATCH_YES)
3720 if (saw_errmsg)
3722 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
3723 goto cleanup;
3725 errmsg = tmp;
3726 saw_errmsg = true;
3728 m = gfc_match_char (',');
3729 if (m == MATCH_YES)
3730 continue;
3732 tmp = NULL;
3733 break;
3736 m = gfc_match (" acquired_lock = %v", &tmp);
3737 if (m == MATCH_ERROR || st == ST_UNLOCK)
3738 goto syntax;
3739 if (m == MATCH_YES)
3741 if (saw_acq_lock)
3743 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L",
3744 &tmp->where);
3745 goto cleanup;
3747 acq_lock = tmp;
3748 saw_acq_lock = true;
3750 m = gfc_match_char (',');
3751 if (m == MATCH_YES)
3752 continue;
3754 tmp = NULL;
3755 break;
3758 break;
3761 if (m == MATCH_ERROR)
3762 goto syntax;
3764 if (gfc_match (" )%t") != MATCH_YES)
3765 goto syntax;
3767 done:
3768 switch (st)
3770 case ST_LOCK:
3771 new_st.op = EXEC_LOCK;
3772 break;
3773 case ST_UNLOCK:
3774 new_st.op = EXEC_UNLOCK;
3775 break;
3776 default:
3777 gcc_unreachable ();
3780 new_st.expr1 = lockvar;
3781 new_st.expr2 = stat;
3782 new_st.expr3 = errmsg;
3783 new_st.expr4 = acq_lock;
3785 return MATCH_YES;
3787 syntax:
3788 gfc_syntax_error (st);
3790 cleanup:
3791 if (acq_lock != tmp)
3792 gfc_free_expr (acq_lock);
3793 if (errmsg != tmp)
3794 gfc_free_expr (errmsg);
3795 if (stat != tmp)
3796 gfc_free_expr (stat);
3798 gfc_free_expr (tmp);
3799 gfc_free_expr (lockvar);
3801 return MATCH_ERROR;
3805 match
3806 gfc_match_lock (void)
3808 if (!gfc_notify_std (GFC_STD_F2008, "LOCK statement at %C"))
3809 return MATCH_ERROR;
3811 return lock_unlock_statement (ST_LOCK);
3815 match
3816 gfc_match_unlock (void)
3818 if (!gfc_notify_std (GFC_STD_F2008, "UNLOCK statement at %C"))
3819 return MATCH_ERROR;
3821 return lock_unlock_statement (ST_UNLOCK);
3825 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
3826 SYNC ALL [(sync-stat-list)]
3827 SYNC MEMORY [(sync-stat-list)]
3828 SYNC IMAGES (image-set [, sync-stat-list] )
3829 with sync-stat is int-expr or *. */
3831 static match
3832 sync_statement (gfc_statement st)
3834 match m;
3835 gfc_expr *tmp, *imageset, *stat, *errmsg;
3836 bool saw_stat, saw_errmsg;
3838 tmp = imageset = stat = errmsg = NULL;
3839 saw_stat = saw_errmsg = false;
3841 if (gfc_pure (NULL))
3843 gfc_error ("Image control statement SYNC at %C in PURE procedure");
3844 return MATCH_ERROR;
3847 gfc_unset_implicit_pure (NULL);
3849 if (!gfc_notify_std (GFC_STD_F2008, "SYNC statement at %C"))
3850 return MATCH_ERROR;
3852 if (flag_coarray == GFC_FCOARRAY_NONE)
3854 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
3855 "enable");
3856 return MATCH_ERROR;
3859 if (gfc_find_state (COMP_CRITICAL))
3861 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
3862 return MATCH_ERROR;
3865 if (gfc_find_state (COMP_DO_CONCURRENT))
3867 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
3868 return MATCH_ERROR;
3871 if (gfc_match_eos () == MATCH_YES)
3873 if (st == ST_SYNC_IMAGES)
3874 goto syntax;
3875 goto done;
3878 if (gfc_match_char ('(') != MATCH_YES)
3879 goto syntax;
3881 if (st == ST_SYNC_IMAGES)
3883 /* Denote '*' as imageset == NULL. */
3884 m = gfc_match_char ('*');
3885 if (m == MATCH_ERROR)
3886 goto syntax;
3887 if (m == MATCH_NO)
3889 if (gfc_match ("%e", &imageset) != MATCH_YES)
3890 goto syntax;
3892 m = gfc_match_char (',');
3893 if (m == MATCH_ERROR)
3894 goto syntax;
3895 if (m == MATCH_NO)
3897 m = gfc_match_char (')');
3898 if (m == MATCH_YES)
3899 goto done;
3900 goto syntax;
3904 for (;;)
3906 m = gfc_match (" stat = %e", &tmp);
3907 if (m == MATCH_ERROR)
3908 goto syntax;
3909 if (m == MATCH_YES)
3911 if (saw_stat)
3913 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
3914 goto cleanup;
3916 stat = tmp;
3917 saw_stat = true;
3919 if (gfc_match_char (',') == MATCH_YES)
3920 continue;
3922 tmp = NULL;
3923 break;
3926 m = gfc_match (" errmsg = %e", &tmp);
3927 if (m == MATCH_ERROR)
3928 goto syntax;
3929 if (m == MATCH_YES)
3931 if (saw_errmsg)
3933 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
3934 goto cleanup;
3936 errmsg = tmp;
3937 saw_errmsg = true;
3939 if (gfc_match_char (',') == MATCH_YES)
3940 continue;
3942 tmp = NULL;
3943 break;
3946 break;
3949 if (gfc_match (" )%t") != MATCH_YES)
3950 goto syntax;
3952 done:
3953 switch (st)
3955 case ST_SYNC_ALL:
3956 new_st.op = EXEC_SYNC_ALL;
3957 break;
3958 case ST_SYNC_IMAGES:
3959 new_st.op = EXEC_SYNC_IMAGES;
3960 break;
3961 case ST_SYNC_MEMORY:
3962 new_st.op = EXEC_SYNC_MEMORY;
3963 break;
3964 default:
3965 gcc_unreachable ();
3968 new_st.expr1 = imageset;
3969 new_st.expr2 = stat;
3970 new_st.expr3 = errmsg;
3972 return MATCH_YES;
3974 syntax:
3975 gfc_syntax_error (st);
3977 cleanup:
3978 if (stat != tmp)
3979 gfc_free_expr (stat);
3980 if (errmsg != tmp)
3981 gfc_free_expr (errmsg);
3983 gfc_free_expr (tmp);
3984 gfc_free_expr (imageset);
3986 return MATCH_ERROR;
3990 /* Match SYNC ALL statement. */
3992 match
3993 gfc_match_sync_all (void)
3995 return sync_statement (ST_SYNC_ALL);
3999 /* Match SYNC IMAGES statement. */
4001 match
4002 gfc_match_sync_images (void)
4004 return sync_statement (ST_SYNC_IMAGES);
4008 /* Match SYNC MEMORY statement. */
4010 match
4011 gfc_match_sync_memory (void)
4013 return sync_statement (ST_SYNC_MEMORY);
4017 /* Match a CONTINUE statement. */
4019 match
4020 gfc_match_continue (void)
4022 if (gfc_match_eos () != MATCH_YES)
4024 gfc_syntax_error (ST_CONTINUE);
4025 return MATCH_ERROR;
4028 new_st.op = EXEC_CONTINUE;
4029 return MATCH_YES;
4033 /* Match the (deprecated) ASSIGN statement. */
4035 match
4036 gfc_match_assign (void)
4038 gfc_expr *expr;
4039 gfc_st_label *label;
4041 if (gfc_match (" %l", &label) == MATCH_YES)
4043 if (!gfc_reference_st_label (label, ST_LABEL_UNKNOWN))
4044 return MATCH_ERROR;
4045 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
4047 if (!gfc_notify_std (GFC_STD_F95_DEL, "ASSIGN statement at %C"))
4048 return MATCH_ERROR;
4050 expr->symtree->n.sym->attr.assign = 1;
4052 new_st.op = EXEC_LABEL_ASSIGN;
4053 new_st.label1 = label;
4054 new_st.expr1 = expr;
4055 return MATCH_YES;
4058 return MATCH_NO;
4062 /* Match the GO TO statement. As a computed GOTO statement is
4063 matched, it is transformed into an equivalent SELECT block. No
4064 tree is necessary, and the resulting jumps-to-jumps are
4065 specifically optimized away by the back end. */
4067 match
4068 gfc_match_goto (void)
4070 gfc_code *head, *tail;
4071 gfc_expr *expr;
4072 gfc_case *cp;
4073 gfc_st_label *label;
4074 int i;
4075 match m;
4077 if (gfc_match (" %l%t", &label) == MATCH_YES)
4079 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
4080 return MATCH_ERROR;
4082 new_st.op = EXEC_GOTO;
4083 new_st.label1 = label;
4084 return MATCH_YES;
4087 /* The assigned GO TO statement. */
4089 if (gfc_match_variable (&expr, 0) == MATCH_YES)
4091 if (!gfc_notify_std (GFC_STD_F95_DEL, "Assigned GOTO statement at %C"))
4092 return MATCH_ERROR;
4094 new_st.op = EXEC_GOTO;
4095 new_st.expr1 = expr;
4097 if (gfc_match_eos () == MATCH_YES)
4098 return MATCH_YES;
4100 /* Match label list. */
4101 gfc_match_char (',');
4102 if (gfc_match_char ('(') != MATCH_YES)
4104 gfc_syntax_error (ST_GOTO);
4105 return MATCH_ERROR;
4107 head = tail = NULL;
4111 m = gfc_match_st_label (&label);
4112 if (m != MATCH_YES)
4113 goto syntax;
4115 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
4116 goto cleanup;
4118 if (head == NULL)
4119 head = tail = gfc_get_code (EXEC_GOTO);
4120 else
4122 tail->block = gfc_get_code (EXEC_GOTO);
4123 tail = tail->block;
4126 tail->label1 = label;
4128 while (gfc_match_char (',') == MATCH_YES);
4130 if (gfc_match (" )%t") != MATCH_YES)
4131 goto syntax;
4133 if (head == NULL)
4135 gfc_error ("Statement label list in GOTO at %C cannot be empty");
4136 goto syntax;
4138 new_st.block = head;
4140 return MATCH_YES;
4143 /* Last chance is a computed GO TO statement. */
4144 if (gfc_match_char ('(') != MATCH_YES)
4146 gfc_syntax_error (ST_GOTO);
4147 return MATCH_ERROR;
4150 head = tail = NULL;
4151 i = 1;
4155 m = gfc_match_st_label (&label);
4156 if (m != MATCH_YES)
4157 goto syntax;
4159 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
4160 goto cleanup;
4162 if (head == NULL)
4163 head = tail = gfc_get_code (EXEC_SELECT);
4164 else
4166 tail->block = gfc_get_code (EXEC_SELECT);
4167 tail = tail->block;
4170 cp = gfc_get_case ();
4171 cp->low = cp->high = gfc_get_int_expr (gfc_default_integer_kind,
4172 NULL, i++);
4174 tail->ext.block.case_list = cp;
4176 tail->next = gfc_get_code (EXEC_GOTO);
4177 tail->next->label1 = label;
4179 while (gfc_match_char (',') == MATCH_YES);
4181 if (gfc_match_char (')') != MATCH_YES)
4182 goto syntax;
4184 if (head == NULL)
4186 gfc_error ("Statement label list in GOTO at %C cannot be empty");
4187 goto syntax;
4190 /* Get the rest of the statement. */
4191 gfc_match_char (',');
4193 if (gfc_match (" %e%t", &expr) != MATCH_YES)
4194 goto syntax;
4196 if (!gfc_notify_std (GFC_STD_F95_OBS, "Computed GOTO at %C"))
4197 return MATCH_ERROR;
4199 /* At this point, a computed GOTO has been fully matched and an
4200 equivalent SELECT statement constructed. */
4202 new_st.op = EXEC_SELECT;
4203 new_st.expr1 = NULL;
4205 /* Hack: For a "real" SELECT, the expression is in expr. We put
4206 it in expr2 so we can distinguish then and produce the correct
4207 diagnostics. */
4208 new_st.expr2 = expr;
4209 new_st.block = head;
4210 return MATCH_YES;
4212 syntax:
4213 gfc_syntax_error (ST_GOTO);
4214 cleanup:
4215 gfc_free_statements (head);
4216 return MATCH_ERROR;
4220 /* Frees a list of gfc_alloc structures. */
4222 void
4223 gfc_free_alloc_list (gfc_alloc *p)
4225 gfc_alloc *q;
4227 for (; p; p = q)
4229 q = p->next;
4230 gfc_free_expr (p->expr);
4231 free (p);
4236 /* Match an ALLOCATE statement. */
4238 match
4239 gfc_match_allocate (void)
4241 gfc_alloc *head, *tail;
4242 gfc_expr *stat, *errmsg, *tmp, *source, *mold;
4243 gfc_typespec ts;
4244 gfc_symbol *sym;
4245 match m;
4246 locus old_locus, deferred_locus, assumed_locus;
4247 bool saw_stat, saw_errmsg, saw_source, saw_mold, saw_deferred, b1, b2, b3;
4248 bool saw_unlimited = false, saw_assumed = false;
4250 head = tail = NULL;
4251 stat = errmsg = source = mold = tmp = NULL;
4252 saw_stat = saw_errmsg = saw_source = saw_mold = saw_deferred = false;
4254 if (gfc_match_char ('(') != MATCH_YES)
4256 gfc_syntax_error (ST_ALLOCATE);
4257 return MATCH_ERROR;
4260 /* Match an optional type-spec. */
4261 old_locus = gfc_current_locus;
4262 m = gfc_match_type_spec (&ts);
4263 if (m == MATCH_ERROR)
4264 goto cleanup;
4265 else if (m == MATCH_NO)
4267 char name[GFC_MAX_SYMBOL_LEN + 3];
4269 if (gfc_match ("%n :: ", name) == MATCH_YES)
4271 gfc_error ("Error in type-spec at %L", &old_locus);
4272 goto cleanup;
4275 ts.type = BT_UNKNOWN;
4277 else
4279 /* Needed for the F2008:C631 check below. */
4280 assumed_locus = gfc_current_locus;
4282 if (gfc_match (" :: ") == MATCH_YES)
4284 if (!gfc_notify_std (GFC_STD_F2003, "typespec in ALLOCATE at %L",
4285 &old_locus))
4286 goto cleanup;
4288 if (ts.deferred)
4290 gfc_error ("Type-spec at %L cannot contain a deferred "
4291 "type parameter", &old_locus);
4292 goto cleanup;
4295 if (ts.type == BT_CHARACTER)
4297 if (!ts.u.cl->length)
4298 saw_assumed = true;
4299 else
4300 ts.u.cl->length_from_typespec = true;
4303 if (type_param_spec_list
4304 && gfc_spec_list_type (type_param_spec_list, NULL)
4305 == SPEC_DEFERRED)
4307 gfc_error ("The type parameter spec list in the type-spec at "
4308 "%L cannot contain DEFERRED parameters", &old_locus);
4309 goto cleanup;
4312 else
4314 ts.type = BT_UNKNOWN;
4315 gfc_current_locus = old_locus;
4319 for (;;)
4321 if (head == NULL)
4322 head = tail = gfc_get_alloc ();
4323 else
4325 tail->next = gfc_get_alloc ();
4326 tail = tail->next;
4329 m = gfc_match_variable (&tail->expr, 0);
4330 if (m == MATCH_NO)
4331 goto syntax;
4332 if (m == MATCH_ERROR)
4333 goto cleanup;
4335 if (tail->expr->expr_type == EXPR_CONSTANT)
4337 gfc_error ("Unexpected constant at %C");
4338 goto cleanup;
4341 if (gfc_check_do_variable (tail->expr->symtree))
4342 goto cleanup;
4344 bool impure = gfc_impure_variable (tail->expr->symtree->n.sym);
4345 if (impure && gfc_pure (NULL))
4347 gfc_error ("Bad allocate-object at %C for a PURE procedure");
4348 goto cleanup;
4351 if (impure)
4352 gfc_unset_implicit_pure (NULL);
4354 /* F2008:C631 (R626) A type-param-value in a type-spec shall be an
4355 asterisk if and only if each allocate-object is a dummy argument
4356 for which the corresponding type parameter is assumed. */
4357 if (saw_assumed
4358 && (tail->expr->ts.deferred
4359 || (tail->expr->ts.u.cl && tail->expr->ts.u.cl->length)
4360 || tail->expr->symtree->n.sym->attr.dummy == 0))
4362 gfc_error ("Incompatible allocate-object at %C for CHARACTER "
4363 "type-spec at %L", &assumed_locus);
4364 goto cleanup;
4367 if (tail->expr->ts.deferred)
4369 saw_deferred = true;
4370 deferred_locus = tail->expr->where;
4373 if (gfc_find_state (COMP_DO_CONCURRENT)
4374 || gfc_find_state (COMP_CRITICAL))
4376 gfc_ref *ref;
4377 bool coarray = tail->expr->symtree->n.sym->attr.codimension;
4378 for (ref = tail->expr->ref; ref; ref = ref->next)
4379 if (ref->type == REF_COMPONENT)
4380 coarray = ref->u.c.component->attr.codimension;
4382 if (coarray && gfc_find_state (COMP_DO_CONCURRENT))
4384 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
4385 goto cleanup;
4387 if (coarray && gfc_find_state (COMP_CRITICAL))
4389 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
4390 goto cleanup;
4394 /* Check for F08:C628. */
4395 sym = tail->expr->symtree->n.sym;
4396 b1 = !(tail->expr->ref
4397 && (tail->expr->ref->type == REF_COMPONENT
4398 || tail->expr->ref->type == REF_ARRAY));
4399 if (sym && sym->ts.type == BT_CLASS && sym->attr.class_ok)
4400 b2 = !(CLASS_DATA (sym)->attr.allocatable
4401 || CLASS_DATA (sym)->attr.class_pointer);
4402 else
4403 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4404 || sym->attr.proc_pointer);
4405 b3 = sym && sym->ns && sym->ns->proc_name
4406 && (sym->ns->proc_name->attr.allocatable
4407 || sym->ns->proc_name->attr.pointer
4408 || sym->ns->proc_name->attr.proc_pointer);
4409 if (b1 && b2 && !b3)
4411 gfc_error ("Allocate-object at %L is neither a data pointer "
4412 "nor an allocatable variable", &tail->expr->where);
4413 goto cleanup;
4416 /* The ALLOCATE statement had an optional typespec. Check the
4417 constraints. */
4418 if (ts.type != BT_UNKNOWN)
4420 /* Enforce F03:C624. */
4421 if (!gfc_type_compatible (&tail->expr->ts, &ts))
4423 gfc_error ("Type of entity at %L is type incompatible with "
4424 "typespec", &tail->expr->where);
4425 goto cleanup;
4428 /* Enforce F03:C627. */
4429 if (ts.kind != tail->expr->ts.kind && !UNLIMITED_POLY (tail->expr))
4431 gfc_error ("Kind type parameter for entity at %L differs from "
4432 "the kind type parameter of the typespec",
4433 &tail->expr->where);
4434 goto cleanup;
4438 if (tail->expr->ts.type == BT_DERIVED)
4439 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
4441 if (type_param_spec_list)
4442 tail->expr->param_list = gfc_copy_actual_arglist (type_param_spec_list);
4444 saw_unlimited = saw_unlimited | UNLIMITED_POLY (tail->expr);
4446 if (gfc_peek_ascii_char () == '(' && !sym->attr.dimension)
4448 gfc_error ("Shape specification for allocatable scalar at %C");
4449 goto cleanup;
4452 if (gfc_match_char (',') != MATCH_YES)
4453 break;
4455 alloc_opt_list:
4457 m = gfc_match (" stat = %e", &tmp);
4458 if (m == MATCH_ERROR)
4459 goto cleanup;
4460 if (m == MATCH_YES)
4462 /* Enforce C630. */
4463 if (saw_stat)
4465 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
4466 goto cleanup;
4469 stat = tmp;
4470 tmp = NULL;
4471 saw_stat = true;
4473 if (stat->expr_type == EXPR_CONSTANT)
4475 gfc_error ("STAT tag at %L cannot be a constant", &stat->where);
4476 goto cleanup;
4479 if (gfc_check_do_variable (stat->symtree))
4480 goto cleanup;
4482 if (gfc_match_char (',') == MATCH_YES)
4483 goto alloc_opt_list;
4486 m = gfc_match (" errmsg = %e", &tmp);
4487 if (m == MATCH_ERROR)
4488 goto cleanup;
4489 if (m == MATCH_YES)
4491 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG tag at %L", &tmp->where))
4492 goto cleanup;
4494 /* Enforce C630. */
4495 if (saw_errmsg)
4497 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
4498 goto cleanup;
4501 errmsg = tmp;
4502 tmp = NULL;
4503 saw_errmsg = true;
4505 if (gfc_match_char (',') == MATCH_YES)
4506 goto alloc_opt_list;
4509 m = gfc_match (" source = %e", &tmp);
4510 if (m == MATCH_ERROR)
4511 goto cleanup;
4512 if (m == MATCH_YES)
4514 if (!gfc_notify_std (GFC_STD_F2003, "SOURCE tag at %L", &tmp->where))
4515 goto cleanup;
4517 /* Enforce C630. */
4518 if (saw_source)
4520 gfc_error ("Redundant SOURCE tag found at %L", &tmp->where);
4521 goto cleanup;
4524 /* The next 2 conditionals check C631. */
4525 if (ts.type != BT_UNKNOWN)
4527 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
4528 &tmp->where, &old_locus);
4529 goto cleanup;
4532 if (head->next
4533 && !gfc_notify_std (GFC_STD_F2008, "SOURCE tag at %L"
4534 " with more than a single allocate object",
4535 &tmp->where))
4536 goto cleanup;
4538 source = tmp;
4539 tmp = NULL;
4540 saw_source = true;
4542 if (gfc_match_char (',') == MATCH_YES)
4543 goto alloc_opt_list;
4546 m = gfc_match (" mold = %e", &tmp);
4547 if (m == MATCH_ERROR)
4548 goto cleanup;
4549 if (m == MATCH_YES)
4551 if (!gfc_notify_std (GFC_STD_F2008, "MOLD tag at %L", &tmp->where))
4552 goto cleanup;
4554 /* Check F08:C636. */
4555 if (saw_mold)
4557 gfc_error ("Redundant MOLD tag found at %L", &tmp->where);
4558 goto cleanup;
4561 /* Check F08:C637. */
4562 if (ts.type != BT_UNKNOWN)
4564 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
4565 &tmp->where, &old_locus);
4566 goto cleanup;
4569 mold = tmp;
4570 tmp = NULL;
4571 saw_mold = true;
4572 mold->mold = 1;
4574 if (gfc_match_char (',') == MATCH_YES)
4575 goto alloc_opt_list;
4578 gfc_gobble_whitespace ();
4580 if (gfc_peek_char () == ')')
4581 break;
4584 if (gfc_match (" )%t") != MATCH_YES)
4585 goto syntax;
4587 /* Check F08:C637. */
4588 if (source && mold)
4590 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
4591 &mold->where, &source->where);
4592 goto cleanup;
4595 /* Check F03:C623, */
4596 if (saw_deferred && ts.type == BT_UNKNOWN && !source && !mold)
4598 gfc_error ("Allocate-object at %L with a deferred type parameter "
4599 "requires either a type-spec or SOURCE tag or a MOLD tag",
4600 &deferred_locus);
4601 goto cleanup;
4604 /* Check F03:C625, */
4605 if (saw_unlimited && ts.type == BT_UNKNOWN && !source && !mold)
4607 for (tail = head; tail; tail = tail->next)
4609 if (UNLIMITED_POLY (tail->expr))
4610 gfc_error ("Unlimited polymorphic allocate-object at %L "
4611 "requires either a type-spec or SOURCE tag "
4612 "or a MOLD tag", &tail->expr->where);
4614 goto cleanup;
4617 new_st.op = EXEC_ALLOCATE;
4618 new_st.expr1 = stat;
4619 new_st.expr2 = errmsg;
4620 if (source)
4621 new_st.expr3 = source;
4622 else
4623 new_st.expr3 = mold;
4624 new_st.ext.alloc.list = head;
4625 new_st.ext.alloc.ts = ts;
4627 if (type_param_spec_list)
4628 gfc_free_actual_arglist (type_param_spec_list);
4630 return MATCH_YES;
4632 syntax:
4633 gfc_syntax_error (ST_ALLOCATE);
4635 cleanup:
4636 gfc_free_expr (errmsg);
4637 gfc_free_expr (source);
4638 gfc_free_expr (stat);
4639 gfc_free_expr (mold);
4640 if (tmp && tmp->expr_type) gfc_free_expr (tmp);
4641 gfc_free_alloc_list (head);
4642 if (type_param_spec_list)
4643 gfc_free_actual_arglist (type_param_spec_list);
4644 return MATCH_ERROR;
4648 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
4649 a set of pointer assignments to intrinsic NULL(). */
4651 match
4652 gfc_match_nullify (void)
4654 gfc_code *tail;
4655 gfc_expr *e, *p;
4656 match m;
4658 tail = NULL;
4660 if (gfc_match_char ('(') != MATCH_YES)
4661 goto syntax;
4663 for (;;)
4665 m = gfc_match_variable (&p, 0);
4666 if (m == MATCH_ERROR)
4667 goto cleanup;
4668 if (m == MATCH_NO)
4669 goto syntax;
4671 if (gfc_check_do_variable (p->symtree))
4672 goto cleanup;
4674 /* F2008, C1242. */
4675 if (gfc_is_coindexed (p))
4677 gfc_error ("Pointer object at %C shall not be coindexed");
4678 goto cleanup;
4681 /* Check for valid array pointer object. Bounds remapping is not
4682 allowed with NULLIFY. */
4683 if (p->ref)
4685 gfc_ref *remap = p->ref;
4686 for (; remap; remap = remap->next)
4687 if (!remap->next && remap->type == REF_ARRAY
4688 && remap->u.ar.type != AR_FULL)
4689 break;
4690 if (remap)
4692 gfc_error ("NULLIFY does not allow bounds remapping for "
4693 "pointer object at %C");
4694 goto cleanup;
4698 /* build ' => NULL() '. */
4699 e = gfc_get_null_expr (&gfc_current_locus);
4701 /* Chain to list. */
4702 if (tail == NULL)
4704 tail = &new_st;
4705 tail->op = EXEC_POINTER_ASSIGN;
4707 else
4709 tail->next = gfc_get_code (EXEC_POINTER_ASSIGN);
4710 tail = tail->next;
4713 tail->expr1 = p;
4714 tail->expr2 = e;
4716 if (gfc_match (" )%t") == MATCH_YES)
4717 break;
4718 if (gfc_match_char (',') != MATCH_YES)
4719 goto syntax;
4722 return MATCH_YES;
4724 syntax:
4725 gfc_syntax_error (ST_NULLIFY);
4727 cleanup:
4728 gfc_free_statements (new_st.next);
4729 new_st.next = NULL;
4730 gfc_free_expr (new_st.expr1);
4731 new_st.expr1 = NULL;
4732 gfc_free_expr (new_st.expr2);
4733 new_st.expr2 = NULL;
4734 return MATCH_ERROR;
4738 /* Match a DEALLOCATE statement. */
4740 match
4741 gfc_match_deallocate (void)
4743 gfc_alloc *head, *tail;
4744 gfc_expr *stat, *errmsg, *tmp;
4745 gfc_symbol *sym;
4746 match m;
4747 bool saw_stat, saw_errmsg, b1, b2;
4749 head = tail = NULL;
4750 stat = errmsg = tmp = NULL;
4751 saw_stat = saw_errmsg = false;
4753 if (gfc_match_char ('(') != MATCH_YES)
4754 goto syntax;
4756 for (;;)
4758 if (head == NULL)
4759 head = tail = gfc_get_alloc ();
4760 else
4762 tail->next = gfc_get_alloc ();
4763 tail = tail->next;
4766 m = gfc_match_variable (&tail->expr, 0);
4767 if (m == MATCH_ERROR)
4768 goto cleanup;
4769 if (m == MATCH_NO)
4770 goto syntax;
4772 if (tail->expr->expr_type == EXPR_CONSTANT)
4774 gfc_error ("Unexpected constant at %C");
4775 goto cleanup;
4778 if (gfc_check_do_variable (tail->expr->symtree))
4779 goto cleanup;
4781 sym = tail->expr->symtree->n.sym;
4783 bool impure = gfc_impure_variable (sym);
4784 if (impure && gfc_pure (NULL))
4786 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
4787 goto cleanup;
4790 if (impure)
4791 gfc_unset_implicit_pure (NULL);
4793 if (gfc_is_coarray (tail->expr)
4794 && gfc_find_state (COMP_DO_CONCURRENT))
4796 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
4797 goto cleanup;
4800 if (gfc_is_coarray (tail->expr)
4801 && gfc_find_state (COMP_CRITICAL))
4803 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
4804 goto cleanup;
4807 /* FIXME: disable the checking on derived types. */
4808 b1 = !(tail->expr->ref
4809 && (tail->expr->ref->type == REF_COMPONENT
4810 || tail->expr->ref->type == REF_ARRAY));
4811 if (sym && sym->ts.type == BT_CLASS)
4812 b2 = !(CLASS_DATA (sym) && (CLASS_DATA (sym)->attr.allocatable
4813 || CLASS_DATA (sym)->attr.class_pointer));
4814 else
4815 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4816 || sym->attr.proc_pointer);
4817 if (b1 && b2)
4819 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
4820 "nor an allocatable variable");
4821 goto cleanup;
4824 if (gfc_match_char (',') != MATCH_YES)
4825 break;
4827 dealloc_opt_list:
4829 m = gfc_match (" stat = %e", &tmp);
4830 if (m == MATCH_ERROR)
4831 goto cleanup;
4832 if (m == MATCH_YES)
4834 if (saw_stat)
4836 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
4837 gfc_free_expr (tmp);
4838 goto cleanup;
4841 stat = tmp;
4842 saw_stat = true;
4844 if (gfc_check_do_variable (stat->symtree))
4845 goto cleanup;
4847 if (gfc_match_char (',') == MATCH_YES)
4848 goto dealloc_opt_list;
4851 m = gfc_match (" errmsg = %e", &tmp);
4852 if (m == MATCH_ERROR)
4853 goto cleanup;
4854 if (m == MATCH_YES)
4856 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG at %L", &tmp->where))
4857 goto cleanup;
4859 if (saw_errmsg)
4861 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
4862 gfc_free_expr (tmp);
4863 goto cleanup;
4866 errmsg = tmp;
4867 saw_errmsg = true;
4869 if (gfc_match_char (',') == MATCH_YES)
4870 goto dealloc_opt_list;
4873 gfc_gobble_whitespace ();
4875 if (gfc_peek_char () == ')')
4876 break;
4879 if (gfc_match (" )%t") != MATCH_YES)
4880 goto syntax;
4882 new_st.op = EXEC_DEALLOCATE;
4883 new_st.expr1 = stat;
4884 new_st.expr2 = errmsg;
4885 new_st.ext.alloc.list = head;
4887 return MATCH_YES;
4889 syntax:
4890 gfc_syntax_error (ST_DEALLOCATE);
4892 cleanup:
4893 gfc_free_expr (errmsg);
4894 gfc_free_expr (stat);
4895 gfc_free_alloc_list (head);
4896 return MATCH_ERROR;
4900 /* Match a RETURN statement. */
4902 match
4903 gfc_match_return (void)
4905 gfc_expr *e;
4906 match m;
4907 gfc_compile_state s;
4909 e = NULL;
4911 if (gfc_find_state (COMP_CRITICAL))
4913 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
4914 return MATCH_ERROR;
4917 if (gfc_find_state (COMP_DO_CONCURRENT))
4919 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
4920 return MATCH_ERROR;
4923 if (gfc_match_eos () == MATCH_YES)
4924 goto done;
4926 if (!gfc_find_state (COMP_SUBROUTINE))
4928 gfc_error ("Alternate RETURN statement at %C is only allowed within "
4929 "a SUBROUTINE");
4930 goto cleanup;
4933 if (gfc_current_form == FORM_FREE)
4935 /* The following are valid, so we can't require a blank after the
4936 RETURN keyword:
4937 return+1
4938 return(1) */
4939 char c = gfc_peek_ascii_char ();
4940 if (ISALPHA (c) || ISDIGIT (c))
4941 return MATCH_NO;
4944 m = gfc_match (" %e%t", &e);
4945 if (m == MATCH_YES)
4946 goto done;
4947 if (m == MATCH_ERROR)
4948 goto cleanup;
4950 gfc_syntax_error (ST_RETURN);
4952 cleanup:
4953 gfc_free_expr (e);
4954 return MATCH_ERROR;
4956 done:
4957 gfc_enclosing_unit (&s);
4958 if (s == COMP_PROGRAM
4959 && !gfc_notify_std (GFC_STD_GNU, "RETURN statement in "
4960 "main program at %C"))
4961 return MATCH_ERROR;
4963 new_st.op = EXEC_RETURN;
4964 new_st.expr1 = e;
4966 return MATCH_YES;
4970 /* Match the call of a type-bound procedure, if CALL%var has already been
4971 matched and var found to be a derived-type variable. */
4973 static match
4974 match_typebound_call (gfc_symtree* varst)
4976 gfc_expr* base;
4977 match m;
4979 base = gfc_get_expr ();
4980 base->expr_type = EXPR_VARIABLE;
4981 base->symtree = varst;
4982 base->where = gfc_current_locus;
4983 gfc_set_sym_referenced (varst->n.sym);
4985 m = gfc_match_varspec (base, 0, true, true);
4986 if (m == MATCH_NO)
4987 gfc_error ("Expected component reference at %C");
4988 if (m != MATCH_YES)
4990 gfc_free_expr (base);
4991 return MATCH_ERROR;
4994 if (gfc_match_eos () != MATCH_YES)
4996 gfc_error ("Junk after CALL at %C");
4997 gfc_free_expr (base);
4998 return MATCH_ERROR;
5001 if (base->expr_type == EXPR_COMPCALL)
5002 new_st.op = EXEC_COMPCALL;
5003 else if (base->expr_type == EXPR_PPC)
5004 new_st.op = EXEC_CALL_PPC;
5005 else
5007 gfc_error ("Expected type-bound procedure or procedure pointer component "
5008 "at %C");
5009 gfc_free_expr (base);
5010 return MATCH_ERROR;
5012 new_st.expr1 = base;
5014 return MATCH_YES;
5018 /* Match a CALL statement. The tricky part here are possible
5019 alternate return specifiers. We handle these by having all
5020 "subroutines" actually return an integer via a register that gives
5021 the return number. If the call specifies alternate returns, we
5022 generate code for a SELECT statement whose case clauses contain
5023 GOTOs to the various labels. */
5025 match
5026 gfc_match_call (void)
5028 char name[GFC_MAX_SYMBOL_LEN + 1];
5029 gfc_actual_arglist *a, *arglist;
5030 gfc_case *new_case;
5031 gfc_symbol *sym;
5032 gfc_symtree *st;
5033 gfc_code *c;
5034 match m;
5035 int i;
5037 arglist = NULL;
5039 m = gfc_match ("% %n", name);
5040 if (m == MATCH_NO)
5041 goto syntax;
5042 if (m != MATCH_YES)
5043 return m;
5045 if (gfc_get_ha_sym_tree (name, &st))
5046 return MATCH_ERROR;
5048 sym = st->n.sym;
5050 /* If this is a variable of derived-type, it probably starts a type-bound
5051 procedure call. Associate variable targets have to be resolved for the
5052 target type. */
5053 if (((sym->attr.flavor != FL_PROCEDURE
5054 || gfc_is_function_return_value (sym, gfc_current_ns))
5055 && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS))
5057 (sym->assoc && sym->assoc->target
5058 && gfc_resolve_expr (sym->assoc->target)
5059 && (sym->assoc->target->ts.type == BT_DERIVED
5060 || sym->assoc->target->ts.type == BT_CLASS)))
5061 return match_typebound_call (st);
5063 /* If it does not seem to be callable (include functions so that the
5064 right association is made. They are thrown out in resolution.)
5065 ... */
5066 if (!sym->attr.generic
5067 && !sym->attr.subroutine
5068 && !sym->attr.function)
5070 if (!(sym->attr.external && !sym->attr.referenced))
5072 /* ...create a symbol in this scope... */
5073 if (sym->ns != gfc_current_ns
5074 && gfc_get_sym_tree (name, NULL, &st, false) == 1)
5075 return MATCH_ERROR;
5077 if (sym != st->n.sym)
5078 sym = st->n.sym;
5081 /* ...and then to try to make the symbol into a subroutine. */
5082 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
5083 return MATCH_ERROR;
5086 gfc_set_sym_referenced (sym);
5088 if (gfc_match_eos () != MATCH_YES)
5090 m = gfc_match_actual_arglist (1, &arglist);
5091 if (m == MATCH_NO)
5092 goto syntax;
5093 if (m == MATCH_ERROR)
5094 goto cleanup;
5096 if (gfc_match_eos () != MATCH_YES)
5097 goto syntax;
5100 /* Walk the argument list looking for invalid BOZ. */
5101 for (a = arglist; a; a = a->next)
5102 if (a->expr && a->expr->ts.type == BT_BOZ)
5104 gfc_error ("A BOZ literal constant at %L cannot appear as an actual "
5105 "argument in a subroutine reference", &a->expr->where);
5106 goto cleanup;
5110 /* If any alternate return labels were found, construct a SELECT
5111 statement that will jump to the right place. */
5113 i = 0;
5114 for (a = arglist; a; a = a->next)
5115 if (a->expr == NULL)
5117 i = 1;
5118 break;
5121 if (i)
5123 gfc_symtree *select_st;
5124 gfc_symbol *select_sym;
5125 char name[GFC_MAX_SYMBOL_LEN + 1];
5127 new_st.next = c = gfc_get_code (EXEC_SELECT);
5128 sprintf (name, "_result_%s", sym->name);
5129 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
5131 select_sym = select_st->n.sym;
5132 select_sym->ts.type = BT_INTEGER;
5133 select_sym->ts.kind = gfc_default_integer_kind;
5134 gfc_set_sym_referenced (select_sym);
5135 c->expr1 = gfc_get_expr ();
5136 c->expr1->expr_type = EXPR_VARIABLE;
5137 c->expr1->symtree = select_st;
5138 c->expr1->ts = select_sym->ts;
5139 c->expr1->where = gfc_current_locus;
5141 i = 0;
5142 for (a = arglist; a; a = a->next)
5144 if (a->expr != NULL)
5145 continue;
5147 if (!gfc_reference_st_label (a->label, ST_LABEL_TARGET))
5148 continue;
5150 i++;
5152 c->block = gfc_get_code (EXEC_SELECT);
5153 c = c->block;
5155 new_case = gfc_get_case ();
5156 new_case->high = gfc_get_int_expr (gfc_default_integer_kind, NULL, i);
5157 new_case->low = new_case->high;
5158 c->ext.block.case_list = new_case;
5160 c->next = gfc_get_code (EXEC_GOTO);
5161 c->next->label1 = a->label;
5165 new_st.op = EXEC_CALL;
5166 new_st.symtree = st;
5167 new_st.ext.actual = arglist;
5169 return MATCH_YES;
5171 syntax:
5172 gfc_syntax_error (ST_CALL);
5174 cleanup:
5175 gfc_free_actual_arglist (arglist);
5176 return MATCH_ERROR;
5180 /* Given a name, return a pointer to the common head structure,
5181 creating it if it does not exist. If FROM_MODULE is nonzero, we
5182 mangle the name so that it doesn't interfere with commons defined
5183 in the using namespace.
5184 TODO: Add to global symbol tree. */
5186 gfc_common_head *
5187 gfc_get_common (const char *name, int from_module)
5189 gfc_symtree *st;
5190 static int serial = 0;
5191 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
5193 if (from_module)
5195 /* A use associated common block is only needed to correctly layout
5196 the variables it contains. */
5197 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
5198 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
5200 else
5202 st = gfc_find_symtree (gfc_current_ns->common_root, name);
5204 if (st == NULL)
5205 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
5208 if (st->n.common == NULL)
5210 st->n.common = gfc_get_common_head ();
5211 st->n.common->where = gfc_current_locus;
5212 strcpy (st->n.common->name, name);
5215 return st->n.common;
5219 /* Match a common block name. */
5221 match
5222 gfc_match_common_name (char *name)
5224 match m;
5226 if (gfc_match_char ('/') == MATCH_NO)
5228 name[0] = '\0';
5229 return MATCH_YES;
5232 if (gfc_match_char ('/') == MATCH_YES)
5234 name[0] = '\0';
5235 return MATCH_YES;
5238 m = gfc_match_name (name);
5240 if (m == MATCH_ERROR)
5241 return MATCH_ERROR;
5242 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
5243 return MATCH_YES;
5245 gfc_error ("Syntax error in common block name at %C");
5246 return MATCH_ERROR;
5250 /* Match a COMMON statement. */
5252 match
5253 gfc_match_common (void)
5255 gfc_symbol *sym, **head, *tail, *other;
5256 char name[GFC_MAX_SYMBOL_LEN + 1];
5257 gfc_common_head *t;
5258 gfc_array_spec *as;
5259 gfc_equiv *e1, *e2;
5260 match m;
5261 char c;
5263 /* COMMON has been matched. In free form source code, the next character
5264 needs to be whitespace or '/'. Check that here. Fixed form source
5265 code needs to be checked below. */
5266 c = gfc_peek_ascii_char ();
5267 if (gfc_current_form == FORM_FREE && !gfc_is_whitespace (c) && c != '/')
5268 return MATCH_NO;
5270 as = NULL;
5272 for (;;)
5274 m = gfc_match_common_name (name);
5275 if (m == MATCH_ERROR)
5276 goto cleanup;
5278 if (name[0] == '\0')
5280 t = &gfc_current_ns->blank_common;
5281 if (t->head == NULL)
5282 t->where = gfc_current_locus;
5284 else
5286 t = gfc_get_common (name, 0);
5288 head = &t->head;
5290 if (*head == NULL)
5291 tail = NULL;
5292 else
5294 tail = *head;
5295 while (tail->common_next)
5296 tail = tail->common_next;
5299 /* Grab the list of symbols. */
5300 for (;;)
5302 m = gfc_match_symbol (&sym, 0);
5303 if (m == MATCH_ERROR)
5304 goto cleanup;
5305 if (m == MATCH_NO)
5306 goto syntax;
5308 /* See if we know the current common block is bind(c), and if
5309 so, then see if we can check if the symbol is (which it'll
5310 need to be). This can happen if the bind(c) attr stmt was
5311 applied to the common block, and the variable(s) already
5312 defined, before declaring the common block. */
5313 if (t->is_bind_c == 1)
5315 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
5317 /* If we find an error, just print it and continue,
5318 cause it's just semantic, and we can see if there
5319 are more errors. */
5320 gfc_error_now ("Variable %qs at %L in common block %qs "
5321 "at %C must be declared with a C "
5322 "interoperable kind since common block "
5323 "%qs is bind(c)",
5324 sym->name, &(sym->declared_at), t->name,
5325 t->name);
5328 if (sym->attr.is_bind_c == 1)
5329 gfc_error_now ("Variable %qs in common block %qs at %C cannot "
5330 "be bind(c) since it is not global", sym->name,
5331 t->name);
5334 if (sym->attr.in_common)
5336 gfc_error ("Symbol %qs at %C is already in a COMMON block",
5337 sym->name);
5338 goto cleanup;
5341 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
5342 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
5344 if (!gfc_notify_std (GFC_STD_GNU, "Initialized symbol %qs at "
5345 "%C can only be COMMON in BLOCK DATA",
5346 sym->name))
5347 goto cleanup;
5350 /* F2018:R874: common-block-object is variable-name [ (array-spec) ]
5351 F2018:C8121: A variable-name shall not be a name made accessible
5352 by use association. */
5353 if (sym->attr.use_assoc)
5355 gfc_error ("Symbol %qs at %C is USE associated from module %qs "
5356 "and cannot occur in COMMON", sym->name, sym->module);
5357 goto cleanup;
5360 /* Deal with an optional array specification after the
5361 symbol name. */
5362 m = gfc_match_array_spec (&as, true, true);
5363 if (m == MATCH_ERROR)
5364 goto cleanup;
5366 if (m == MATCH_YES)
5368 if (as->type != AS_EXPLICIT)
5370 gfc_error ("Array specification for symbol %qs in COMMON "
5371 "at %C must be explicit", sym->name);
5372 goto cleanup;
5375 if (as->corank)
5377 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
5378 "coarray", sym->name);
5379 goto cleanup;
5382 if (!gfc_add_dimension (&sym->attr, sym->name, NULL))
5383 goto cleanup;
5385 if (sym->attr.pointer)
5387 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
5388 "POINTER array", sym->name);
5389 goto cleanup;
5392 sym->as = as;
5393 as = NULL;
5397 /* Add the in_common attribute, but ignore the reported errors
5398 if any, and continue matching. */
5399 gfc_add_in_common (&sym->attr, sym->name, NULL);
5401 sym->common_block = t;
5402 sym->common_block->refs++;
5404 if (tail != NULL)
5405 tail->common_next = sym;
5406 else
5407 *head = sym;
5409 tail = sym;
5411 sym->common_head = t;
5413 /* Check to see if the symbol is already in an equivalence group.
5414 If it is, set the other members as being in common. */
5415 if (sym->attr.in_equivalence)
5417 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
5419 for (e2 = e1; e2; e2 = e2->eq)
5420 if (e2->expr->symtree->n.sym == sym)
5421 goto equiv_found;
5423 continue;
5425 equiv_found:
5427 for (e2 = e1; e2; e2 = e2->eq)
5429 other = e2->expr->symtree->n.sym;
5430 if (other->common_head
5431 && other->common_head != sym->common_head)
5433 gfc_error ("Symbol %qs, in COMMON block %qs at "
5434 "%C is being indirectly equivalenced to "
5435 "another COMMON block %qs",
5436 sym->name, sym->common_head->name,
5437 other->common_head->name);
5438 goto cleanup;
5440 other->attr.in_common = 1;
5441 other->common_head = t;
5447 gfc_gobble_whitespace ();
5448 if (gfc_match_eos () == MATCH_YES)
5449 goto done;
5450 c = gfc_peek_ascii_char ();
5451 if (c == '/')
5452 break;
5453 if (c != ',')
5455 /* In Fixed form source code, gfortran can end up here for an
5456 expression of the form COMMONI = RHS. This may not be an
5457 error, so return MATCH_NO. */
5458 if (gfc_current_form == FORM_FIXED && c == '=')
5460 gfc_free_array_spec (as);
5461 return MATCH_NO;
5463 goto syntax;
5465 else
5466 gfc_match_char (',');
5468 gfc_gobble_whitespace ();
5469 if (gfc_peek_ascii_char () == '/')
5470 break;
5474 done:
5475 return MATCH_YES;
5477 syntax:
5478 gfc_syntax_error (ST_COMMON);
5480 cleanup:
5481 gfc_free_array_spec (as);
5482 return MATCH_ERROR;
5486 /* Match a BLOCK DATA program unit. */
5488 match
5489 gfc_match_block_data (void)
5491 char name[GFC_MAX_SYMBOL_LEN + 1];
5492 gfc_symbol *sym;
5493 match m;
5495 if (!gfc_notify_std (GFC_STD_F2018_OBS, "BLOCK DATA construct at %L",
5496 &gfc_current_locus))
5497 return MATCH_ERROR;
5499 if (gfc_match_eos () == MATCH_YES)
5501 gfc_new_block = NULL;
5502 return MATCH_YES;
5505 m = gfc_match ("% %n%t", name);
5506 if (m != MATCH_YES)
5507 return MATCH_ERROR;
5509 if (gfc_get_symbol (name, NULL, &sym))
5510 return MATCH_ERROR;
5512 if (!gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL))
5513 return MATCH_ERROR;
5515 gfc_new_block = sym;
5517 return MATCH_YES;
5521 /* Free a namelist structure. */
5523 void
5524 gfc_free_namelist (gfc_namelist *name)
5526 gfc_namelist *n;
5528 for (; name; name = n)
5530 n = name->next;
5531 free (name);
5536 /* Free an OpenMP namelist structure. */
5538 void
5539 gfc_free_omp_namelist (gfc_omp_namelist *name, bool free_ns,
5540 bool free_align_allocator,
5541 bool free_mem_traits_space)
5543 gfc_omp_namelist *n;
5545 for (; name; name = n)
5547 gfc_free_expr (name->expr);
5548 if (free_align_allocator)
5549 gfc_free_expr (name->u.align);
5550 else if (free_mem_traits_space)
5551 { } /* name->u.memspace_sym: shall not call gfc_free_symbol here. */
5552 if (free_ns)
5553 gfc_free_namespace (name->u2.ns);
5554 else if (free_align_allocator)
5555 gfc_free_expr (name->u2.allocator);
5556 else if (free_mem_traits_space)
5557 { } /* name->u2.traits_sym: shall not call gfc_free_symbol here. */
5558 else if (name->u2.udr)
5560 if (name->u2.udr->combiner)
5561 gfc_free_statement (name->u2.udr->combiner);
5562 if (name->u2.udr->initializer)
5563 gfc_free_statement (name->u2.udr->initializer);
5564 free (name->u2.udr);
5566 n = name->next;
5567 free (name);
5572 /* Match a NAMELIST statement. */
5574 match
5575 gfc_match_namelist (void)
5577 gfc_symbol *group_name, *sym;
5578 gfc_namelist *nl;
5579 match m, m2;
5581 m = gfc_match (" / %s /", &group_name);
5582 if (m == MATCH_NO)
5583 goto syntax;
5584 if (m == MATCH_ERROR)
5585 goto error;
5587 for (;;)
5589 if (group_name->ts.type != BT_UNKNOWN)
5591 gfc_error ("Namelist group name %qs at %C already has a basic "
5592 "type of %s", group_name->name,
5593 gfc_typename (&group_name->ts));
5594 return MATCH_ERROR;
5597 if (group_name->attr.flavor == FL_NAMELIST
5598 && group_name->attr.use_assoc
5599 && !gfc_notify_std (GFC_STD_GNU, "Namelist group name %qs "
5600 "at %C already is USE associated and can"
5601 "not be respecified.", group_name->name))
5602 return MATCH_ERROR;
5604 if (group_name->attr.flavor != FL_NAMELIST
5605 && !gfc_add_flavor (&group_name->attr, FL_NAMELIST,
5606 group_name->name, NULL))
5607 return MATCH_ERROR;
5609 for (;;)
5611 m = gfc_match_symbol (&sym, 1);
5612 if (m == MATCH_NO)
5613 goto syntax;
5614 if (m == MATCH_ERROR)
5615 goto error;
5617 if (sym->ts.type == BT_UNKNOWN)
5619 if (gfc_current_ns->seen_implicit_none)
5621 /* It is required that members of a namelist be declared
5622 before the namelist. We check this by checking if the
5623 symbol has a defined type for IMPLICIT NONE. */
5624 gfc_error ("Symbol %qs in namelist %qs at %C must be "
5625 "declared before the namelist is declared.",
5626 sym->name, group_name->name);
5627 gfc_error_check ();
5629 else
5631 /* Before the symbol is given an implicit type, check to
5632 see if the symbol is already available in the namespace,
5633 possibly through host association. Importantly, the
5634 symbol may be a user defined type. */
5636 gfc_symbol *tmp;
5638 gfc_find_symbol (sym->name, NULL, 1, &tmp);
5639 if (tmp && tmp->attr.generic
5640 && (tmp = gfc_find_dt_in_generic (tmp)))
5642 if (tmp->attr.flavor == FL_DERIVED)
5644 gfc_error ("Derived type %qs at %L conflicts with "
5645 "namelist object %qs at %C",
5646 tmp->name, &tmp->declared_at, sym->name);
5647 goto error;
5651 /* Set type of the symbol to its implicit default type. It is
5652 not allowed to set it later to any other type. */
5653 gfc_set_default_type (sym, 0, gfc_current_ns);
5656 if (sym->attr.in_namelist == 0
5657 && !gfc_add_in_namelist (&sym->attr, sym->name, NULL))
5658 goto error;
5660 /* Use gfc_error_check here, rather than goto error, so that
5661 these are the only errors for the next two lines. */
5662 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
5664 gfc_error ("Assumed size array %qs in namelist %qs at "
5665 "%C is not allowed", sym->name, group_name->name);
5666 gfc_error_check ();
5669 nl = gfc_get_namelist ();
5670 nl->sym = sym;
5671 sym->refs++;
5673 if (group_name->namelist == NULL)
5674 group_name->namelist = group_name->namelist_tail = nl;
5675 else
5677 group_name->namelist_tail->next = nl;
5678 group_name->namelist_tail = nl;
5681 if (gfc_match_eos () == MATCH_YES)
5682 goto done;
5684 m = gfc_match_char (',');
5686 if (gfc_match_char ('/') == MATCH_YES)
5688 m2 = gfc_match (" %s /", &group_name);
5689 if (m2 == MATCH_YES)
5690 break;
5691 if (m2 == MATCH_ERROR)
5692 goto error;
5693 goto syntax;
5696 if (m != MATCH_YES)
5697 goto syntax;
5701 done:
5702 return MATCH_YES;
5704 syntax:
5705 gfc_syntax_error (ST_NAMELIST);
5707 error:
5708 return MATCH_ERROR;
5712 /* Match a MODULE statement. */
5714 match
5715 gfc_match_module (void)
5717 match m;
5719 m = gfc_match (" %s%t", &gfc_new_block);
5720 if (m != MATCH_YES)
5721 return m;
5723 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
5724 gfc_new_block->name, NULL))
5725 return MATCH_ERROR;
5727 return MATCH_YES;
5731 /* Free equivalence sets and lists. Recursively is the easiest way to
5732 do this. */
5734 void
5735 gfc_free_equiv_until (gfc_equiv *eq, gfc_equiv *stop)
5737 if (eq == stop)
5738 return;
5740 gfc_free_equiv (eq->eq);
5741 gfc_free_equiv_until (eq->next, stop);
5742 gfc_free_expr (eq->expr);
5743 free (eq);
5747 void
5748 gfc_free_equiv (gfc_equiv *eq)
5750 gfc_free_equiv_until (eq, NULL);
5754 /* Match an EQUIVALENCE statement. */
5756 match
5757 gfc_match_equivalence (void)
5759 gfc_equiv *eq, *set, *tail;
5760 gfc_ref *ref;
5761 gfc_symbol *sym;
5762 match m;
5763 gfc_common_head *common_head = NULL;
5764 bool common_flag;
5765 int cnt;
5766 char c;
5768 /* EQUIVALENCE has been matched. After gobbling any possible whitespace,
5769 the next character needs to be '('. Check that here, and return
5770 MATCH_NO for a variable of the form equivalence. */
5771 gfc_gobble_whitespace ();
5772 c = gfc_peek_ascii_char ();
5773 if (c != '(')
5774 return MATCH_NO;
5776 tail = NULL;
5778 for (;;)
5780 eq = gfc_get_equiv ();
5781 if (tail == NULL)
5782 tail = eq;
5784 eq->next = gfc_current_ns->equiv;
5785 gfc_current_ns->equiv = eq;
5787 if (gfc_match_char ('(') != MATCH_YES)
5788 goto syntax;
5790 set = eq;
5791 common_flag = FALSE;
5792 cnt = 0;
5794 for (;;)
5796 m = gfc_match_equiv_variable (&set->expr);
5797 if (m == MATCH_ERROR)
5798 goto cleanup;
5799 if (m == MATCH_NO)
5800 goto syntax;
5802 /* count the number of objects. */
5803 cnt++;
5805 if (gfc_match_char ('%') == MATCH_YES)
5807 gfc_error ("Derived type component %C is not a "
5808 "permitted EQUIVALENCE member");
5809 goto cleanup;
5812 for (ref = set->expr->ref; ref; ref = ref->next)
5813 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
5815 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
5816 "be an array section");
5817 goto cleanup;
5820 sym = set->expr->symtree->n.sym;
5822 if (!gfc_add_in_equivalence (&sym->attr, sym->name, NULL))
5823 goto cleanup;
5824 if (sym->ts.type == BT_CLASS
5825 && CLASS_DATA (sym)
5826 && !gfc_add_in_equivalence (&CLASS_DATA (sym)->attr,
5827 sym->name, NULL))
5828 goto cleanup;
5830 if (sym->attr.in_common)
5832 common_flag = TRUE;
5833 common_head = sym->common_head;
5836 if (gfc_match_char (')') == MATCH_YES)
5837 break;
5839 if (gfc_match_char (',') != MATCH_YES)
5840 goto syntax;
5842 set->eq = gfc_get_equiv ();
5843 set = set->eq;
5846 if (cnt < 2)
5848 gfc_error ("EQUIVALENCE at %C requires two or more objects");
5849 goto cleanup;
5852 /* If one of the members of an equivalence is in common, then
5853 mark them all as being in common. Before doing this, check
5854 that members of the equivalence group are not in different
5855 common blocks. */
5856 if (common_flag)
5857 for (set = eq; set; set = set->eq)
5859 sym = set->expr->symtree->n.sym;
5860 if (sym->common_head && sym->common_head != common_head)
5862 gfc_error ("Attempt to indirectly overlap COMMON "
5863 "blocks %s and %s by EQUIVALENCE at %C",
5864 sym->common_head->name, common_head->name);
5865 goto cleanup;
5867 sym->attr.in_common = 1;
5868 sym->common_head = common_head;
5871 if (gfc_match_eos () == MATCH_YES)
5872 break;
5873 if (gfc_match_char (',') != MATCH_YES)
5875 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
5876 goto cleanup;
5880 if (!gfc_notify_std (GFC_STD_F2018_OBS, "EQUIVALENCE statement at %C"))
5881 return MATCH_ERROR;
5883 return MATCH_YES;
5885 syntax:
5886 gfc_syntax_error (ST_EQUIVALENCE);
5888 cleanup:
5889 eq = tail->next;
5890 tail->next = NULL;
5892 gfc_free_equiv (gfc_current_ns->equiv);
5893 gfc_current_ns->equiv = eq;
5895 return MATCH_ERROR;
5899 /* Check that a statement function is not recursive. This is done by looking
5900 for the statement function symbol(sym) by looking recursively through its
5901 expression(e). If a reference to sym is found, true is returned.
5902 12.5.4 requires that any variable of function that is implicitly typed
5903 shall have that type confirmed by any subsequent type declaration. The
5904 implicit typing is conveniently done here. */
5905 static bool
5906 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
5908 static bool
5909 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5912 if (e == NULL)
5913 return false;
5915 switch (e->expr_type)
5917 case EXPR_FUNCTION:
5918 if (e->symtree == NULL)
5919 return false;
5921 /* Check the name before testing for nested recursion! */
5922 if (sym->name == e->symtree->n.sym->name)
5923 return true;
5925 /* Catch recursion via other statement functions. */
5926 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
5927 && e->symtree->n.sym->value
5928 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
5929 return true;
5931 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5932 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5934 break;
5936 case EXPR_VARIABLE:
5937 if (e->symtree && sym->name == e->symtree->n.sym->name)
5938 return true;
5940 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5941 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5942 break;
5944 default:
5945 break;
5948 return false;
5952 static bool
5953 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
5955 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
5959 /* Check for invalid uses of statement function dummy arguments in body. */
5961 static bool
5962 chk_stmt_fcn_body (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5964 gfc_formal_arglist *formal;
5966 if (e == NULL || e->symtree == NULL || e->expr_type != EXPR_FUNCTION)
5967 return false;
5969 for (formal = sym->formal; formal; formal = formal->next)
5971 if (formal->sym == e->symtree->n.sym)
5973 gfc_error ("Invalid use of statement function argument at %L",
5974 &e->where);
5975 return true;
5979 return false;
5983 /* Match a statement function declaration. It is so easy to match
5984 non-statement function statements with a MATCH_ERROR as opposed to
5985 MATCH_NO that we suppress error message in most cases. */
5987 match
5988 gfc_match_st_function (void)
5990 gfc_error_buffer old_error;
5991 gfc_symbol *sym;
5992 gfc_expr *expr;
5993 match m;
5994 char name[GFC_MAX_SYMBOL_LEN + 1];
5995 locus old_locus;
5996 bool fcn;
5997 gfc_formal_arglist *ptr;
5999 /* Read the possible statement function name, and then check to see if
6000 a symbol is already present in the namespace. Record if it is a
6001 function and whether it has been referenced. */
6002 fcn = false;
6003 ptr = NULL;
6004 old_locus = gfc_current_locus;
6005 m = gfc_match_name (name);
6006 if (m == MATCH_YES)
6008 gfc_find_symbol (name, NULL, 1, &sym);
6009 if (sym && sym->attr.function && !sym->attr.referenced)
6011 fcn = true;
6012 ptr = sym->formal;
6016 gfc_current_locus = old_locus;
6017 m = gfc_match_symbol (&sym, 0);
6018 if (m != MATCH_YES)
6019 return m;
6021 gfc_push_error (&old_error);
6023 if (!gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION, sym->name, NULL))
6024 goto undo_error;
6026 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
6027 goto undo_error;
6029 m = gfc_match (" = %e%t", &expr);
6030 if (m == MATCH_NO)
6031 goto undo_error;
6033 gfc_free_error (&old_error);
6035 if (m == MATCH_ERROR)
6036 return m;
6038 if (recursive_stmt_fcn (expr, sym))
6040 gfc_error ("Statement function at %L is recursive", &expr->where);
6041 return MATCH_ERROR;
6044 if (fcn && ptr != sym->formal)
6046 gfc_error ("Statement function %qs at %L conflicts with function name",
6047 sym->name, &expr->where);
6048 return MATCH_ERROR;
6051 if (gfc_traverse_expr (expr, sym, chk_stmt_fcn_body, 0))
6052 return MATCH_ERROR;
6054 sym->value = expr;
6056 if ((gfc_current_state () == COMP_FUNCTION
6057 || gfc_current_state () == COMP_SUBROUTINE)
6058 && gfc_state_stack->previous->state == COMP_INTERFACE)
6060 gfc_error ("Statement function at %L cannot appear within an INTERFACE",
6061 &expr->where);
6062 return MATCH_ERROR;
6065 if (!gfc_notify_std (GFC_STD_F95_OBS, "Statement function at %C"))
6066 return MATCH_ERROR;
6068 return MATCH_YES;
6070 undo_error:
6071 gfc_pop_error (&old_error);
6072 return MATCH_NO;
6076 /* Match an assignment to a pointer function (F2008). This could, in
6077 general be ambiguous with a statement function. In this implementation
6078 it remains so if it is the first statement after the specification
6079 block. */
6081 match
6082 gfc_match_ptr_fcn_assign (void)
6084 gfc_error_buffer old_error;
6085 locus old_loc;
6086 gfc_symbol *sym;
6087 gfc_expr *expr;
6088 match m;
6089 char name[GFC_MAX_SYMBOL_LEN + 1];
6091 old_loc = gfc_current_locus;
6092 m = gfc_match_name (name);
6093 if (m != MATCH_YES)
6094 return m;
6096 gfc_find_symbol (name, NULL, 1, &sym);
6097 if (sym && sym->attr.flavor != FL_PROCEDURE)
6098 return MATCH_NO;
6100 gfc_push_error (&old_error);
6102 if (sym && sym->attr.function)
6103 goto match_actual_arglist;
6105 gfc_current_locus = old_loc;
6106 m = gfc_match_symbol (&sym, 0);
6107 if (m != MATCH_YES)
6108 return m;
6110 if (!gfc_add_procedure (&sym->attr, PROC_UNKNOWN, sym->name, NULL))
6111 goto undo_error;
6113 match_actual_arglist:
6114 gfc_current_locus = old_loc;
6115 m = gfc_match (" %e", &expr);
6116 if (m != MATCH_YES)
6117 goto undo_error;
6119 new_st.op = EXEC_ASSIGN;
6120 new_st.expr1 = expr;
6121 expr = NULL;
6123 m = gfc_match (" = %e%t", &expr);
6124 if (m != MATCH_YES)
6125 goto undo_error;
6127 new_st.expr2 = expr;
6128 return MATCH_YES;
6130 undo_error:
6131 gfc_pop_error (&old_error);
6132 return MATCH_NO;
6136 /***************** SELECT CASE subroutines ******************/
6138 /* Free a single case structure. */
6140 static void
6141 free_case (gfc_case *p)
6143 if (p->low == p->high)
6144 p->high = NULL;
6145 gfc_free_expr (p->low);
6146 gfc_free_expr (p->high);
6147 free (p);
6151 /* Free a list of case structures. */
6153 void
6154 gfc_free_case_list (gfc_case *p)
6156 gfc_case *q;
6158 for (; p; p = q)
6160 q = p->next;
6161 free_case (p);
6166 /* Match a single case selector. Combining the requirements of F08:C830
6167 and F08:C832 (R838) means that the case-value must have either CHARACTER,
6168 INTEGER, or LOGICAL type. */
6170 static match
6171 match_case_selector (gfc_case **cp)
6173 gfc_case *c;
6174 match m;
6176 c = gfc_get_case ();
6177 c->where = gfc_current_locus;
6179 if (gfc_match_char (':') == MATCH_YES)
6181 m = gfc_match_init_expr (&c->high);
6182 if (m == MATCH_NO)
6183 goto need_expr;
6184 if (m == MATCH_ERROR)
6185 goto cleanup;
6187 if (c->high->ts.type != BT_LOGICAL && c->high->ts.type != BT_INTEGER
6188 && c->high->ts.type != BT_CHARACTER)
6190 gfc_error ("Expression in CASE selector at %L cannot be %s",
6191 &c->high->where, gfc_typename (&c->high->ts));
6192 goto cleanup;
6195 else
6197 m = gfc_match_init_expr (&c->low);
6198 if (m == MATCH_ERROR)
6199 goto cleanup;
6200 if (m == MATCH_NO)
6201 goto need_expr;
6203 if (c->low->ts.type != BT_LOGICAL && c->low->ts.type != BT_INTEGER
6204 && c->low->ts.type != BT_CHARACTER)
6206 gfc_error ("Expression in CASE selector at %L cannot be %s",
6207 &c->low->where, gfc_typename (&c->low->ts));
6208 goto cleanup;
6211 /* If we're not looking at a ':' now, make a range out of a single
6212 target. Else get the upper bound for the case range. */
6213 if (gfc_match_char (':') != MATCH_YES)
6214 c->high = c->low;
6215 else
6217 m = gfc_match_init_expr (&c->high);
6218 if (m == MATCH_ERROR)
6219 goto cleanup;
6220 if (m == MATCH_YES
6221 && c->high->ts.type != BT_LOGICAL
6222 && c->high->ts.type != BT_INTEGER
6223 && c->high->ts.type != BT_CHARACTER)
6225 gfc_error ("Expression in CASE selector at %L cannot be %s",
6226 &c->high->where, gfc_typename (c->high));
6227 goto cleanup;
6229 /* MATCH_NO is fine. It's OK if nothing is there! */
6233 if (c->low && c->low->rank != 0)
6235 gfc_error ("Expression in CASE selector at %L must be scalar",
6236 &c->low->where);
6237 goto cleanup;
6239 if (c->high && c->high->rank != 0)
6241 gfc_error ("Expression in CASE selector at %L must be scalar",
6242 &c->high->where);
6243 goto cleanup;
6246 *cp = c;
6247 return MATCH_YES;
6249 need_expr:
6250 gfc_error ("Expected initialization expression in CASE at %C");
6252 cleanup:
6253 free_case (c);
6254 return MATCH_ERROR;
6258 /* Match the end of a case statement. */
6260 static match
6261 match_case_eos (void)
6263 char name[GFC_MAX_SYMBOL_LEN + 1];
6264 match m;
6266 if (gfc_match_eos () == MATCH_YES)
6267 return MATCH_YES;
6269 /* If the case construct doesn't have a case-construct-name, we
6270 should have matched the EOS. */
6271 if (!gfc_current_block ())
6272 return MATCH_NO;
6274 gfc_gobble_whitespace ();
6276 m = gfc_match_name (name);
6277 if (m != MATCH_YES)
6278 return m;
6280 if (strcmp (name, gfc_current_block ()->name) != 0)
6282 gfc_error ("Expected block name %qs of SELECT construct at %C",
6283 gfc_current_block ()->name);
6284 return MATCH_ERROR;
6287 return gfc_match_eos ();
6291 /* Match a SELECT statement. */
6293 match
6294 gfc_match_select (void)
6296 gfc_expr *expr;
6297 match m;
6299 m = gfc_match_label ();
6300 if (m == MATCH_ERROR)
6301 return m;
6303 m = gfc_match (" select case ( %e )%t", &expr);
6304 if (m != MATCH_YES)
6305 return m;
6307 new_st.op = EXEC_SELECT;
6308 new_st.expr1 = expr;
6310 return MATCH_YES;
6314 /* Transfer the selector typespec to the associate name. */
6316 static void
6317 copy_ts_from_selector_to_associate (gfc_expr *associate, gfc_expr *selector)
6319 gfc_ref *ref;
6320 gfc_symbol *assoc_sym;
6321 int rank = 0;
6323 assoc_sym = associate->symtree->n.sym;
6325 /* At this stage the expression rank and arrayspec dimensions have
6326 not been completely sorted out. We must get the expr2->rank
6327 right here, so that the correct class container is obtained. */
6328 ref = selector->ref;
6329 while (ref && ref->next)
6330 ref = ref->next;
6332 if (selector->ts.type == BT_CLASS
6333 && CLASS_DATA (selector)
6334 && CLASS_DATA (selector)->as
6335 && CLASS_DATA (selector)->as->type == AS_ASSUMED_RANK)
6337 assoc_sym->attr.dimension = 1;
6338 assoc_sym->as = gfc_copy_array_spec (CLASS_DATA (selector)->as);
6339 goto build_class_sym;
6341 else if (selector->ts.type == BT_CLASS
6342 && CLASS_DATA (selector)
6343 && CLASS_DATA (selector)->as
6344 && ref && ref->type == REF_ARRAY)
6346 /* Ensure that the array reference type is set. We cannot use
6347 gfc_resolve_expr at this point, so the usable parts of
6348 resolve.cc(resolve_array_ref) are employed to do it. */
6349 if (ref->u.ar.type == AR_UNKNOWN)
6351 ref->u.ar.type = AR_ELEMENT;
6352 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
6353 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
6354 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR
6355 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
6356 && ref->u.ar.start[i] && ref->u.ar.start[i]->rank))
6358 ref->u.ar.type = AR_SECTION;
6359 break;
6363 if (ref->u.ar.type == AR_FULL)
6364 selector->rank = CLASS_DATA (selector)->as->rank;
6365 else if (ref->u.ar.type == AR_SECTION)
6366 selector->rank = ref->u.ar.dimen;
6367 else
6368 selector->rank = 0;
6370 rank = selector->rank;
6373 if (rank)
6375 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
6376 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT
6377 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
6378 && ref->u.ar.end[i] == NULL
6379 && ref->u.ar.stride[i] == NULL))
6380 rank--;
6382 if (rank)
6384 assoc_sym->attr.dimension = 1;
6385 assoc_sym->as = gfc_get_array_spec ();
6386 assoc_sym->as->rank = rank;
6387 assoc_sym->as->type = AS_DEFERRED;
6389 else
6390 assoc_sym->as = NULL;
6392 else
6393 assoc_sym->as = NULL;
6395 build_class_sym:
6396 if (selector->ts.type == BT_CLASS)
6398 /* The correct class container has to be available. */
6399 assoc_sym->ts.type = BT_CLASS;
6400 assoc_sym->ts.u.derived = CLASS_DATA (selector)
6401 ? CLASS_DATA (selector)->ts.u.derived : selector->ts.u.derived;
6402 assoc_sym->attr.pointer = 1;
6403 gfc_build_class_symbol (&assoc_sym->ts, &assoc_sym->attr, &assoc_sym->as);
6408 /* Build the associate name */
6409 static int
6410 build_associate_name (const char *name, gfc_expr **e1, gfc_expr **e2)
6412 gfc_expr *expr1 = *e1;
6413 gfc_expr *expr2 = *e2;
6414 gfc_symbol *sym;
6416 /* For the case where the associate name is already an associate name. */
6417 if (!expr2)
6418 expr2 = expr1;
6419 expr1 = gfc_get_expr ();
6420 expr1->expr_type = EXPR_VARIABLE;
6421 expr1->where = expr2->where;
6422 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
6423 return 1;
6425 sym = expr1->symtree->n.sym;
6426 if (expr2->ts.type == BT_UNKNOWN)
6427 sym->attr.untyped = 1;
6428 else
6429 copy_ts_from_selector_to_associate (expr1, expr2);
6431 sym->attr.flavor = FL_VARIABLE;
6432 sym->attr.referenced = 1;
6433 sym->attr.class_ok = 1;
6435 *e1 = expr1;
6436 *e2 = expr2;
6437 return 0;
6441 /* Push the current selector onto the SELECT TYPE stack. */
6443 static void
6444 select_type_push (gfc_symbol *sel)
6446 gfc_select_type_stack *top = gfc_get_select_type_stack ();
6447 top->selector = sel;
6448 top->tmp = NULL;
6449 top->prev = select_type_stack;
6451 select_type_stack = top;
6455 /* Set the temporary for the current intrinsic SELECT TYPE selector. */
6457 static gfc_symtree *
6458 select_intrinsic_set_tmp (gfc_typespec *ts)
6460 char name[GFC_MAX_SYMBOL_LEN];
6461 gfc_symtree *tmp;
6462 HOST_WIDE_INT charlen = 0;
6463 gfc_symbol *selector = select_type_stack->selector;
6464 gfc_symbol *sym;
6466 if (ts->type == BT_CLASS || ts->type == BT_DERIVED)
6467 return NULL;
6469 if (selector->ts.type == BT_CLASS && !selector->attr.class_ok)
6470 return NULL;
6472 /* Case value == NULL corresponds to SELECT TYPE cases otherwise
6473 the values correspond to SELECT rank cases. */
6474 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length
6475 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
6476 charlen = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
6478 if (ts->type != BT_CHARACTER)
6479 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (ts->type),
6480 ts->kind);
6481 else
6482 snprintf (name, sizeof (name),
6483 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
6484 gfc_basic_typename (ts->type), charlen, ts->kind);
6486 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
6487 sym = tmp->n.sym;
6488 gfc_add_type (sym, ts, NULL);
6490 /* Copy across the array spec to the selector. */
6491 if (selector->ts.type == BT_CLASS
6492 && (CLASS_DATA (selector)->attr.dimension
6493 || CLASS_DATA (selector)->attr.codimension))
6495 sym->attr.pointer = 1;
6496 sym->attr.dimension = CLASS_DATA (selector)->attr.dimension;
6497 sym->attr.codimension = CLASS_DATA (selector)->attr.codimension;
6498 sym->as = gfc_copy_array_spec (CLASS_DATA (selector)->as);
6501 gfc_set_sym_referenced (sym);
6502 gfc_add_flavor (&sym->attr, FL_VARIABLE, name, NULL);
6503 sym->attr.select_type_temporary = 1;
6505 return tmp;
6509 /* Set up a temporary for the current TYPE IS / CLASS IS branch . */
6511 static void
6512 select_type_set_tmp (gfc_typespec *ts)
6514 char name[GFC_MAX_SYMBOL_LEN + 12 + 1];
6515 gfc_symtree *tmp = NULL;
6516 gfc_symbol *selector = select_type_stack->selector;
6517 gfc_symbol *sym;
6519 if (!ts)
6521 select_type_stack->tmp = NULL;
6522 return;
6525 tmp = select_intrinsic_set_tmp (ts);
6527 if (tmp == NULL)
6529 if (!ts->u.derived)
6530 return;
6532 if (ts->type == BT_CLASS)
6533 sprintf (name, "__tmp_class_%s", ts->u.derived->name);
6534 else
6535 sprintf (name, "__tmp_type_%s", ts->u.derived->name);
6537 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
6538 sym = tmp->n.sym;
6539 gfc_add_type (sym, ts, NULL);
6541 if (selector->ts.type == BT_CLASS && selector->attr.class_ok
6542 && selector->ts.u.derived && CLASS_DATA (selector))
6544 sym->attr.pointer
6545 = CLASS_DATA (selector)->attr.class_pointer;
6547 /* Copy across the array spec to the selector. */
6548 if (CLASS_DATA (selector)->attr.dimension
6549 || CLASS_DATA (selector)->attr.codimension)
6551 sym->attr.dimension
6552 = CLASS_DATA (selector)->attr.dimension;
6553 sym->attr.codimension
6554 = CLASS_DATA (selector)->attr.codimension;
6555 if (CLASS_DATA (selector)->as->type != AS_EXPLICIT)
6556 sym->as = gfc_copy_array_spec (CLASS_DATA (selector)->as);
6557 else
6559 sym->as = gfc_get_array_spec();
6560 sym->as->rank = CLASS_DATA (selector)->as->rank;
6561 sym->as->type = AS_DEFERRED;
6566 gfc_set_sym_referenced (sym);
6567 gfc_add_flavor (&sym->attr, FL_VARIABLE, name, NULL);
6568 sym->attr.select_type_temporary = 1;
6570 if (ts->type == BT_CLASS)
6571 gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
6573 else
6574 sym = tmp->n.sym;
6577 /* Add an association for it, so the rest of the parser knows it is
6578 an associate-name. The target will be set during resolution. */
6579 sym->assoc = gfc_get_association_list ();
6580 sym->assoc->dangling = 1;
6581 sym->assoc->st = tmp;
6583 select_type_stack->tmp = tmp;
6587 /* Match a SELECT TYPE statement. */
6589 match
6590 gfc_match_select_type (void)
6592 gfc_expr *expr1, *expr2 = NULL;
6593 match m;
6594 char name[GFC_MAX_SYMBOL_LEN + 1];
6595 bool class_array;
6596 gfc_namespace *ns = gfc_current_ns;
6598 m = gfc_match_label ();
6599 if (m == MATCH_ERROR)
6600 return m;
6602 m = gfc_match (" select type ( ");
6603 if (m != MATCH_YES)
6604 return m;
6606 if (gfc_current_state() == COMP_MODULE
6607 || gfc_current_state() == COMP_SUBMODULE)
6609 gfc_error ("SELECT TYPE at %C cannot appear in this scope");
6610 return MATCH_ERROR;
6613 gfc_current_ns = gfc_build_block_ns (ns);
6614 m = gfc_match (" %n => %e", name, &expr2);
6615 if (m == MATCH_YES)
6617 if (build_associate_name (name, &expr1, &expr2))
6619 m = MATCH_ERROR;
6620 goto cleanup;
6623 else
6625 m = gfc_match (" %e ", &expr1);
6626 if (m != MATCH_YES)
6628 std::swap (ns, gfc_current_ns);
6629 gfc_free_namespace (ns);
6630 return m;
6634 m = gfc_match (" )%t");
6635 if (m != MATCH_YES)
6637 gfc_error ("parse error in SELECT TYPE statement at %C");
6638 goto cleanup;
6641 /* This ghastly expression seems to be needed to distinguish a CLASS
6642 array, which can have a reference, from other expressions that
6643 have references, such as derived type components, and are not
6644 allowed by the standard.
6645 TODO: see if it is sufficient to exclude component and substring
6646 references. */
6647 class_array = (expr1->expr_type == EXPR_VARIABLE
6648 && expr1->ts.type == BT_CLASS
6649 && CLASS_DATA (expr1)
6650 && (strcmp (CLASS_DATA (expr1)->name, "_data") == 0)
6651 && (CLASS_DATA (expr1)->attr.dimension
6652 || CLASS_DATA (expr1)->attr.codimension)
6653 && expr1->ref
6654 && expr1->ref->type == REF_ARRAY
6655 && expr1->ref->u.ar.type == AR_FULL
6656 && expr1->ref->next == NULL);
6658 /* Check for F03:C811 (F08:C835). */
6659 if (!expr2 && (expr1->expr_type != EXPR_VARIABLE
6660 || (!class_array && expr1->ref != NULL)))
6662 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
6663 "use associate-name=>");
6664 m = MATCH_ERROR;
6665 goto cleanup;
6668 /* Prevent an existing associate name from reuse here by pushing expr1 to
6669 expr2 and building a new associate name. */
6670 if (!expr2 && expr1->symtree->n.sym->assoc
6671 && !expr1->symtree->n.sym->attr.select_type_temporary
6672 && !expr1->symtree->n.sym->attr.select_rank_temporary
6673 && build_associate_name (expr1->symtree->n.sym->name, &expr1, &expr2))
6675 m = MATCH_ERROR;
6676 goto cleanup;
6679 new_st.op = EXEC_SELECT_TYPE;
6680 new_st.expr1 = expr1;
6681 new_st.expr2 = expr2;
6682 new_st.ext.block.ns = gfc_current_ns;
6684 select_type_push (expr1->symtree->n.sym);
6685 gfc_current_ns = ns;
6687 return MATCH_YES;
6689 cleanup:
6690 gfc_free_expr (expr1);
6691 gfc_free_expr (expr2);
6692 gfc_undo_symbols ();
6693 std::swap (ns, gfc_current_ns);
6694 gfc_free_namespace (ns);
6695 return m;
6699 /* Set the temporary for the current intrinsic SELECT RANK selector. */
6701 static void
6702 select_rank_set_tmp (gfc_typespec *ts, int *case_value)
6704 char name[2 * GFC_MAX_SYMBOL_LEN];
6705 char tname[GFC_MAX_SYMBOL_LEN + 7];
6706 gfc_symtree *tmp;
6707 gfc_symbol *selector = select_type_stack->selector;
6708 gfc_symbol *sym;
6709 gfc_symtree *st;
6710 HOST_WIDE_INT charlen = 0;
6712 if (case_value == NULL)
6713 return;
6715 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length
6716 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
6717 charlen = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
6719 if (ts->type == BT_CLASS)
6720 sprintf (tname, "class_%s", ts->u.derived->name);
6721 else if (ts->type == BT_DERIVED)
6722 sprintf (tname, "type_%s", ts->u.derived->name);
6723 else if (ts->type != BT_CHARACTER)
6724 sprintf (tname, "%s_%d", gfc_basic_typename (ts->type), ts->kind);
6725 else
6726 sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
6727 gfc_basic_typename (ts->type), charlen, ts->kind);
6729 /* Case value == NULL corresponds to SELECT TYPE cases otherwise
6730 the values correspond to SELECT rank cases. */
6731 if (*case_value >=0)
6732 sprintf (name, "__tmp_%s_rank_%d", tname, *case_value);
6733 else
6734 sprintf (name, "__tmp_%s_rank_m%d", tname, -*case_value);
6736 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
6737 if (st)
6738 return;
6740 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
6741 sym = tmp->n.sym;
6742 gfc_add_type (sym, ts, NULL);
6744 /* Copy across the array spec to the selector. */
6745 if (selector->ts.type == BT_CLASS)
6747 sym->ts.u.derived = CLASS_DATA (selector)->ts.u.derived;
6748 sym->attr.pointer = CLASS_DATA (selector)->attr.pointer;
6749 sym->attr.allocatable = CLASS_DATA (selector)->attr.allocatable;
6750 sym->attr.target = CLASS_DATA (selector)->attr.target;
6751 sym->attr.class_ok = 0;
6752 if (case_value && *case_value != 0)
6754 sym->attr.dimension = 1;
6755 sym->as = gfc_copy_array_spec (CLASS_DATA (selector)->as);
6756 if (*case_value > 0)
6758 sym->as->type = AS_DEFERRED;
6759 sym->as->rank = *case_value;
6761 else if (*case_value == -1)
6763 sym->as->type = AS_ASSUMED_SIZE;
6764 sym->as->rank = 1;
6768 else
6770 sym->attr.pointer = selector->attr.pointer;
6771 sym->attr.allocatable = selector->attr.allocatable;
6772 sym->attr.target = selector->attr.target;
6773 if (case_value && *case_value != 0)
6775 sym->attr.dimension = 1;
6776 sym->as = gfc_copy_array_spec (selector->as);
6777 if (*case_value > 0)
6779 sym->as->type = AS_DEFERRED;
6780 sym->as->rank = *case_value;
6782 else if (*case_value == -1)
6784 sym->as->type = AS_ASSUMED_SIZE;
6785 sym->as->rank = 1;
6790 gfc_set_sym_referenced (sym);
6791 gfc_add_flavor (&sym->attr, FL_VARIABLE, name, NULL);
6792 sym->attr.select_type_temporary = 1;
6793 if (case_value)
6794 sym->attr.select_rank_temporary = 1;
6796 if (ts->type == BT_CLASS)
6797 gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
6799 /* Add an association for it, so the rest of the parser knows it is
6800 an associate-name. The target will be set during resolution. */
6801 sym->assoc = gfc_get_association_list ();
6802 sym->assoc->dangling = 1;
6803 sym->assoc->st = tmp;
6805 select_type_stack->tmp = tmp;
6809 /* Match a SELECT RANK statement. */
6811 match
6812 gfc_match_select_rank (void)
6814 gfc_expr *expr1, *expr2 = NULL;
6815 match m;
6816 char name[GFC_MAX_SYMBOL_LEN + 1];
6817 gfc_symbol *sym, *sym2;
6818 gfc_namespace *ns = gfc_current_ns;
6819 gfc_array_spec *as = NULL;
6821 m = gfc_match_label ();
6822 if (m == MATCH_ERROR)
6823 return m;
6825 m = gfc_match (" select% rank ( ");
6826 if (m != MATCH_YES)
6827 return m;
6829 if (!gfc_notify_std (GFC_STD_F2018, "SELECT RANK statement at %C"))
6830 return MATCH_NO;
6832 gfc_current_ns = gfc_build_block_ns (ns);
6833 m = gfc_match (" %n => %e", name, &expr2);
6835 if (m == MATCH_YES)
6837 /* If expr2 corresponds to an implicitly typed variable, then the
6838 actual type of the variable may not have been set. Set it here. */
6839 if (!gfc_current_ns->seen_implicit_none
6840 && expr2->expr_type == EXPR_VARIABLE
6841 && expr2->ts.type == BT_UNKNOWN
6842 && expr2->symtree && expr2->symtree->n.sym)
6844 gfc_set_default_type (expr2->symtree->n.sym, 0, gfc_current_ns);
6845 expr2->ts.type = expr2->symtree->n.sym->ts.type;
6848 expr1 = gfc_get_expr ();
6849 expr1->expr_type = EXPR_VARIABLE;
6850 expr1->where = expr2->where;
6851 expr1->ref = gfc_copy_ref (expr2->ref);
6852 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
6854 m = MATCH_ERROR;
6855 goto cleanup;
6858 sym = expr1->symtree->n.sym;
6860 if (expr2->symtree)
6862 sym2 = expr2->symtree->n.sym;
6863 as = (sym2->ts.type == BT_CLASS
6864 && CLASS_DATA (sym2)) ? CLASS_DATA (sym2)->as : sym2->as;
6867 if (expr2->expr_type != EXPR_VARIABLE
6868 || !(as && as->type == AS_ASSUMED_RANK))
6870 gfc_error ("The SELECT RANK selector at %C must be an assumed "
6871 "rank variable");
6872 m = MATCH_ERROR;
6873 goto cleanup;
6876 if (expr2->ts.type == BT_CLASS && CLASS_DATA (sym2))
6878 copy_ts_from_selector_to_associate (expr1, expr2);
6880 sym->attr.flavor = FL_VARIABLE;
6881 sym->attr.referenced = 1;
6882 sym->attr.class_ok = 1;
6883 CLASS_DATA (sym)->attr.allocatable = CLASS_DATA (sym2)->attr.allocatable;
6884 CLASS_DATA (sym)->attr.pointer = CLASS_DATA (sym2)->attr.pointer;
6885 CLASS_DATA (sym)->attr.target = CLASS_DATA (sym2)->attr.target;
6886 sym->attr.pointer = 1;
6888 else
6890 sym->ts = sym2->ts;
6891 sym->as = gfc_copy_array_spec (sym2->as);
6892 sym->attr.dimension = 1;
6894 sym->attr.flavor = FL_VARIABLE;
6895 sym->attr.referenced = 1;
6896 sym->attr.class_ok = sym2->attr.class_ok;
6897 sym->attr.allocatable = sym2->attr.allocatable;
6898 sym->attr.pointer = sym2->attr.pointer;
6899 sym->attr.target = sym2->attr.target;
6902 else
6904 m = gfc_match (" %e ", &expr1);
6906 if (m != MATCH_YES)
6908 gfc_undo_symbols ();
6909 std::swap (ns, gfc_current_ns);
6910 gfc_free_namespace (ns);
6911 return m;
6914 if (expr1->symtree)
6916 sym = expr1->symtree->n.sym;
6917 as = (sym->ts.type == BT_CLASS
6918 && CLASS_DATA (sym)) ? CLASS_DATA (sym)->as : sym->as;
6921 if (expr1->expr_type != EXPR_VARIABLE
6922 || !(as && as->type == AS_ASSUMED_RANK))
6924 gfc_error("The SELECT RANK selector at %C must be an assumed "
6925 "rank variable");
6926 m = MATCH_ERROR;
6927 goto cleanup;
6931 m = gfc_match (" )%t");
6932 if (m != MATCH_YES)
6934 gfc_error ("parse error in SELECT RANK statement at %C");
6935 goto cleanup;
6938 new_st.op = EXEC_SELECT_RANK;
6939 new_st.expr1 = expr1;
6940 new_st.expr2 = expr2;
6941 new_st.ext.block.ns = gfc_current_ns;
6943 select_type_push (expr1->symtree->n.sym);
6944 gfc_current_ns = ns;
6946 return MATCH_YES;
6948 cleanup:
6949 gfc_free_expr (expr1);
6950 gfc_free_expr (expr2);
6951 gfc_undo_symbols ();
6952 std::swap (ns, gfc_current_ns);
6953 gfc_free_namespace (ns);
6954 return m;
6958 /* Match a CASE statement. */
6960 match
6961 gfc_match_case (void)
6963 gfc_case *c, *head, *tail;
6964 match m;
6966 head = tail = NULL;
6968 if (gfc_current_state () != COMP_SELECT)
6970 gfc_error ("Unexpected CASE statement at %C");
6971 return MATCH_ERROR;
6974 if (gfc_match ("% default") == MATCH_YES)
6976 m = match_case_eos ();
6977 if (m == MATCH_NO)
6978 goto syntax;
6979 if (m == MATCH_ERROR)
6980 goto cleanup;
6982 new_st.op = EXEC_SELECT;
6983 c = gfc_get_case ();
6984 c->where = gfc_current_locus;
6985 new_st.ext.block.case_list = c;
6986 return MATCH_YES;
6989 if (gfc_match_char ('(') != MATCH_YES)
6990 goto syntax;
6992 for (;;)
6994 if (match_case_selector (&c) == MATCH_ERROR)
6995 goto cleanup;
6997 if (head == NULL)
6998 head = c;
6999 else
7000 tail->next = c;
7002 tail = c;
7004 if (gfc_match_char (')') == MATCH_YES)
7005 break;
7006 if (gfc_match_char (',') != MATCH_YES)
7007 goto syntax;
7010 m = match_case_eos ();
7011 if (m == MATCH_NO)
7012 goto syntax;
7013 if (m == MATCH_ERROR)
7014 goto cleanup;
7016 new_st.op = EXEC_SELECT;
7017 new_st.ext.block.case_list = head;
7019 return MATCH_YES;
7021 syntax:
7022 gfc_error ("Syntax error in CASE specification at %C");
7024 cleanup:
7025 gfc_free_case_list (head); /* new_st is cleaned up in parse.cc. */
7026 return MATCH_ERROR;
7030 /* Match a TYPE IS statement. */
7032 match
7033 gfc_match_type_is (void)
7035 gfc_case *c = NULL;
7036 match m;
7038 if (gfc_current_state () != COMP_SELECT_TYPE)
7040 gfc_error ("Unexpected TYPE IS statement at %C");
7041 return MATCH_ERROR;
7044 if (gfc_match_char ('(') != MATCH_YES)
7045 goto syntax;
7047 c = gfc_get_case ();
7048 c->where = gfc_current_locus;
7050 m = gfc_match_type_spec (&c->ts);
7051 if (m == MATCH_NO)
7052 goto syntax;
7053 if (m == MATCH_ERROR)
7054 goto cleanup;
7056 if (gfc_match_char (')') != MATCH_YES)
7057 goto syntax;
7059 m = match_case_eos ();
7060 if (m == MATCH_NO)
7061 goto syntax;
7062 if (m == MATCH_ERROR)
7063 goto cleanup;
7065 new_st.op = EXEC_SELECT_TYPE;
7066 new_st.ext.block.case_list = c;
7068 if (c->ts.type == BT_DERIVED && c->ts.u.derived
7069 && (c->ts.u.derived->attr.sequence
7070 || c->ts.u.derived->attr.is_bind_c))
7072 gfc_error ("The type-spec shall not specify a sequence derived "
7073 "type or a type with the BIND attribute in SELECT "
7074 "TYPE at %C [F2003:C815]");
7075 return MATCH_ERROR;
7078 if (c->ts.type == BT_DERIVED
7079 && c->ts.u.derived && c->ts.u.derived->attr.pdt_type
7080 && gfc_spec_list_type (type_param_spec_list, c->ts.u.derived)
7081 != SPEC_ASSUMED)
7083 gfc_error ("All the LEN type parameters in the TYPE IS statement "
7084 "at %C must be ASSUMED");
7085 return MATCH_ERROR;
7088 /* Create temporary variable. */
7089 select_type_set_tmp (&c->ts);
7091 return MATCH_YES;
7093 syntax:
7094 gfc_error ("Syntax error in TYPE IS specification at %C");
7096 cleanup:
7097 if (c != NULL)
7098 gfc_free_case_list (c); /* new_st is cleaned up in parse.cc. */
7099 return MATCH_ERROR;
7103 /* Match a CLASS IS or CLASS DEFAULT statement. */
7105 match
7106 gfc_match_class_is (void)
7108 gfc_case *c = NULL;
7109 match m;
7111 if (gfc_current_state () != COMP_SELECT_TYPE)
7112 return MATCH_NO;
7114 if (gfc_match ("% default") == MATCH_YES)
7116 m = match_case_eos ();
7117 if (m == MATCH_NO)
7118 goto syntax;
7119 if (m == MATCH_ERROR)
7120 goto cleanup;
7122 new_st.op = EXEC_SELECT_TYPE;
7123 c = gfc_get_case ();
7124 c->where = gfc_current_locus;
7125 c->ts.type = BT_UNKNOWN;
7126 new_st.ext.block.case_list = c;
7127 select_type_set_tmp (NULL);
7128 return MATCH_YES;
7131 m = gfc_match ("% is");
7132 if (m == MATCH_NO)
7133 goto syntax;
7134 if (m == MATCH_ERROR)
7135 goto cleanup;
7137 if (gfc_match_char ('(') != MATCH_YES)
7138 goto syntax;
7140 c = gfc_get_case ();
7141 c->where = gfc_current_locus;
7143 m = match_derived_type_spec (&c->ts);
7144 if (m == MATCH_NO)
7145 goto syntax;
7146 if (m == MATCH_ERROR)
7147 goto cleanup;
7149 if (c->ts.type == BT_DERIVED)
7150 c->ts.type = BT_CLASS;
7152 if (gfc_match_char (')') != MATCH_YES)
7153 goto syntax;
7155 m = match_case_eos ();
7156 if (m == MATCH_NO)
7157 goto syntax;
7158 if (m == MATCH_ERROR)
7159 goto cleanup;
7161 new_st.op = EXEC_SELECT_TYPE;
7162 new_st.ext.block.case_list = c;
7164 /* Create temporary variable. */
7165 select_type_set_tmp (&c->ts);
7167 return MATCH_YES;
7169 syntax:
7170 gfc_error ("Syntax error in CLASS IS specification at %C");
7172 cleanup:
7173 if (c != NULL)
7174 gfc_free_case_list (c); /* new_st is cleaned up in parse.cc. */
7175 return MATCH_ERROR;
7179 /* Match a RANK statement. */
7181 match
7182 gfc_match_rank_is (void)
7184 gfc_case *c = NULL;
7185 match m;
7186 int case_value;
7188 if (gfc_current_state () != COMP_SELECT_RANK)
7190 gfc_error ("Unexpected RANK statement at %C");
7191 return MATCH_ERROR;
7194 if (gfc_match ("% default") == MATCH_YES)
7196 m = match_case_eos ();
7197 if (m == MATCH_NO)
7198 goto syntax;
7199 if (m == MATCH_ERROR)
7200 goto cleanup;
7202 new_st.op = EXEC_SELECT_RANK;
7203 c = gfc_get_case ();
7204 c->ts.type = BT_UNKNOWN;
7205 c->where = gfc_current_locus;
7206 new_st.ext.block.case_list = c;
7207 select_type_stack->tmp = NULL;
7208 return MATCH_YES;
7211 if (gfc_match_char ('(') != MATCH_YES)
7212 goto syntax;
7214 c = gfc_get_case ();
7215 c->where = gfc_current_locus;
7216 c->ts = select_type_stack->selector->ts;
7218 m = gfc_match_expr (&c->low);
7219 if (m == MATCH_NO)
7221 if (gfc_match_char ('*') == MATCH_YES)
7222 c->low = gfc_get_int_expr (gfc_default_integer_kind,
7223 NULL, -1);
7224 else
7225 goto syntax;
7227 case_value = -1;
7229 else if (m == MATCH_YES)
7231 /* F2018: R1150 */
7232 if (c->low->expr_type != EXPR_CONSTANT
7233 || c->low->ts.type != BT_INTEGER
7234 || c->low->rank)
7236 gfc_error ("The SELECT RANK CASE expression at %C must be a "
7237 "scalar, integer constant");
7238 goto cleanup;
7241 case_value = (int) mpz_get_si (c->low->value.integer);
7242 /* F2018: C1151 */
7243 if ((case_value < 0) || (case_value > GFC_MAX_DIMENSIONS))
7245 gfc_error ("The value of the SELECT RANK CASE expression at "
7246 "%C must not be less than zero or greater than %d",
7247 GFC_MAX_DIMENSIONS);
7248 goto cleanup;
7251 else
7252 goto cleanup;
7254 if (gfc_match_char (')') != MATCH_YES)
7255 goto syntax;
7257 m = match_case_eos ();
7258 if (m == MATCH_NO)
7259 goto syntax;
7260 if (m == MATCH_ERROR)
7261 goto cleanup;
7263 new_st.op = EXEC_SELECT_RANK;
7264 new_st.ext.block.case_list = c;
7266 /* Create temporary variable. Recycle the select type code. */
7267 select_rank_set_tmp (&c->ts, &case_value);
7269 return MATCH_YES;
7271 syntax:
7272 gfc_error ("Syntax error in RANK specification at %C");
7274 cleanup:
7275 if (c != NULL)
7276 gfc_free_case_list (c); /* new_st is cleaned up in parse.cc. */
7277 return MATCH_ERROR;
7280 /********************* WHERE subroutines ********************/
7282 /* Match the rest of a simple WHERE statement that follows an IF statement.
7285 static match
7286 match_simple_where (void)
7288 gfc_expr *expr;
7289 gfc_code *c;
7290 match m;
7292 m = gfc_match (" ( %e )", &expr);
7293 if (m != MATCH_YES)
7294 return m;
7296 m = gfc_match_assignment ();
7297 if (m == MATCH_NO)
7298 goto syntax;
7299 if (m == MATCH_ERROR)
7300 goto cleanup;
7302 if (gfc_match_eos () != MATCH_YES)
7303 goto syntax;
7305 c = gfc_get_code (EXEC_WHERE);
7306 c->expr1 = expr;
7308 c->next = XCNEW (gfc_code);
7309 *c->next = new_st;
7310 c->next->loc = gfc_current_locus;
7311 gfc_clear_new_st ();
7313 new_st.op = EXEC_WHERE;
7314 new_st.block = c;
7316 return MATCH_YES;
7318 syntax:
7319 gfc_syntax_error (ST_WHERE);
7321 cleanup:
7322 gfc_free_expr (expr);
7323 return MATCH_ERROR;
7327 /* Match a WHERE statement. */
7329 match
7330 gfc_match_where (gfc_statement *st)
7332 gfc_expr *expr;
7333 match m0, m;
7334 gfc_code *c;
7336 m0 = gfc_match_label ();
7337 if (m0 == MATCH_ERROR)
7338 return m0;
7340 m = gfc_match (" where ( %e )", &expr);
7341 if (m != MATCH_YES)
7342 return m;
7344 if (gfc_match_eos () == MATCH_YES)
7346 *st = ST_WHERE_BLOCK;
7347 new_st.op = EXEC_WHERE;
7348 new_st.expr1 = expr;
7349 return MATCH_YES;
7352 m = gfc_match_assignment ();
7353 if (m == MATCH_NO)
7354 gfc_syntax_error (ST_WHERE);
7356 if (m != MATCH_YES)
7358 gfc_free_expr (expr);
7359 return MATCH_ERROR;
7362 /* We've got a simple WHERE statement. */
7363 *st = ST_WHERE;
7364 c = gfc_get_code (EXEC_WHERE);
7365 c->expr1 = expr;
7367 /* Put in the assignment. It will not be processed by add_statement, so we
7368 need to copy the location here. */
7370 c->next = XCNEW (gfc_code);
7371 *c->next = new_st;
7372 c->next->loc = gfc_current_locus;
7373 gfc_clear_new_st ();
7375 new_st.op = EXEC_WHERE;
7376 new_st.block = c;
7378 return MATCH_YES;
7382 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
7383 new_st if successful. */
7385 match
7386 gfc_match_elsewhere (void)
7388 char name[GFC_MAX_SYMBOL_LEN + 1];
7389 gfc_expr *expr;
7390 match m;
7392 if (gfc_current_state () != COMP_WHERE)
7394 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
7395 return MATCH_ERROR;
7398 expr = NULL;
7400 if (gfc_match_char ('(') == MATCH_YES)
7402 m = gfc_match_expr (&expr);
7403 if (m == MATCH_NO)
7404 goto syntax;
7405 if (m == MATCH_ERROR)
7406 return MATCH_ERROR;
7408 if (gfc_match_char (')') != MATCH_YES)
7409 goto syntax;
7412 if (gfc_match_eos () != MATCH_YES)
7414 /* Only makes sense if we have a where-construct-name. */
7415 if (!gfc_current_block ())
7417 m = MATCH_ERROR;
7418 goto cleanup;
7420 /* Better be a name at this point. */
7421 m = gfc_match_name (name);
7422 if (m == MATCH_NO)
7423 goto syntax;
7424 if (m == MATCH_ERROR)
7425 goto cleanup;
7427 if (gfc_match_eos () != MATCH_YES)
7428 goto syntax;
7430 if (strcmp (name, gfc_current_block ()->name) != 0)
7432 gfc_error ("Label %qs at %C doesn't match WHERE label %qs",
7433 name, gfc_current_block ()->name);
7434 goto cleanup;
7438 new_st.op = EXEC_WHERE;
7439 new_st.expr1 = expr;
7440 return MATCH_YES;
7442 syntax:
7443 gfc_syntax_error (ST_ELSEWHERE);
7445 cleanup:
7446 gfc_free_expr (expr);
7447 return MATCH_ERROR;