[Fortran] ICE in gfc_typenode_for_spec PR93603
[official-gcc.git] / gcc / fortran / match.c
blob9c2ec41c49c620caa865ee16b8de460fb2c0a837
1 /* Matching subroutines in all sizes, shapes and colors.
2 Copyright (C) 2000-2020 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "options.h"
25 #include "gfortran.h"
26 #include "match.h"
27 #include "parse.h"
29 int gfc_matching_ptr_assignment = 0;
30 int gfc_matching_procptr_assignment = 0;
31 bool gfc_matching_prefix = false;
33 /* Stack of SELECT TYPE statements. */
34 gfc_select_type_stack *select_type_stack = NULL;
36 /* List of type parameter expressions. */
37 gfc_actual_arglist *type_param_spec_list;
39 /* For debugging and diagnostic purposes. Return the textual representation
40 of the intrinsic operator OP. */
41 const char *
42 gfc_op2string (gfc_intrinsic_op op)
44 switch (op)
46 case INTRINSIC_UPLUS:
47 case INTRINSIC_PLUS:
48 return "+";
50 case INTRINSIC_UMINUS:
51 case INTRINSIC_MINUS:
52 return "-";
54 case INTRINSIC_POWER:
55 return "**";
56 case INTRINSIC_CONCAT:
57 return "//";
58 case INTRINSIC_TIMES:
59 return "*";
60 case INTRINSIC_DIVIDE:
61 return "/";
63 case INTRINSIC_AND:
64 return ".and.";
65 case INTRINSIC_OR:
66 return ".or.";
67 case INTRINSIC_EQV:
68 return ".eqv.";
69 case INTRINSIC_NEQV:
70 return ".neqv.";
72 case INTRINSIC_EQ_OS:
73 return ".eq.";
74 case INTRINSIC_EQ:
75 return "==";
76 case INTRINSIC_NE_OS:
77 return ".ne.";
78 case INTRINSIC_NE:
79 return "/=";
80 case INTRINSIC_GE_OS:
81 return ".ge.";
82 case INTRINSIC_GE:
83 return ">=";
84 case INTRINSIC_LE_OS:
85 return ".le.";
86 case INTRINSIC_LE:
87 return "<=";
88 case INTRINSIC_LT_OS:
89 return ".lt.";
90 case INTRINSIC_LT:
91 return "<";
92 case INTRINSIC_GT_OS:
93 return ".gt.";
94 case INTRINSIC_GT:
95 return ">";
96 case INTRINSIC_NOT:
97 return ".not.";
99 case INTRINSIC_ASSIGN:
100 return "=";
102 case INTRINSIC_PARENTHESES:
103 return "parens";
105 case INTRINSIC_NONE:
106 return "none";
108 /* DTIO */
109 case INTRINSIC_FORMATTED:
110 return "formatted";
111 case INTRINSIC_UNFORMATTED:
112 return "unformatted";
114 default:
115 break;
118 gfc_internal_error ("gfc_op2string(): Bad code");
119 /* Not reached. */
123 /******************** Generic matching subroutines ************************/
125 /* Matches a member separator. With standard FORTRAN this is '%', but with
126 DEC structures we must carefully match dot ('.').
127 Because operators are spelled ".op.", a dotted string such as "x.y.z..."
128 can be either a component reference chain or a combination of binary
129 operations.
130 There is no real way to win because the string may be grammatically
131 ambiguous. The following rules help avoid ambiguities - they match
132 some behavior of other (older) compilers. If the rules here are changed
133 the test cases should be updated. If the user has problems with these rules
134 they probably deserve the consequences. Consider "x.y.z":
135 (1) If any user defined operator ".y." exists, this is always y(x,z)
136 (even if ".y." is the wrong type and/or x has a member y).
137 (2) Otherwise if x has a member y, and y is itself a derived type,
138 this is (x->y)->z, even if an intrinsic operator exists which
139 can handle (x,z).
140 (3) If x has no member y or (x->y) is not a derived type but ".y."
141 is an intrinsic operator (such as ".eq."), this is y(x,z).
142 (4) Lastly if there is no operator ".y." and x has no member "y", it is an
143 error.
144 It is worth noting that the logic here does not support mixed use of member
145 accessors within a single string. That is, even if x has component y and y
146 has component z, the following are all syntax errors:
147 "x%y.z" "x.y%z" "(x.y).z" "(x%y)%z"
150 match
151 gfc_match_member_sep(gfc_symbol *sym)
153 char name[GFC_MAX_SYMBOL_LEN + 1];
154 locus dot_loc, start_loc;
155 gfc_intrinsic_op iop;
156 match m;
157 gfc_symbol *tsym;
158 gfc_component *c = NULL;
160 /* What a relief: '%' is an unambiguous member separator. */
161 if (gfc_match_char ('%') == MATCH_YES)
162 return MATCH_YES;
164 /* Beware ye who enter here. */
165 if (!flag_dec_structure || !sym)
166 return MATCH_NO;
168 tsym = NULL;
170 /* We may be given either a derived type variable or the derived type
171 declaration itself (which actually contains the components);
172 we need the latter to search for components. */
173 if (gfc_fl_struct (sym->attr.flavor))
174 tsym = sym;
175 else if (gfc_bt_struct (sym->ts.type))
176 tsym = sym->ts.u.derived;
178 iop = INTRINSIC_NONE;
179 name[0] = '\0';
180 m = MATCH_NO;
182 /* If we have to reject come back here later. */
183 start_loc = gfc_current_locus;
185 /* Look for a component access next. */
186 if (gfc_match_char ('.') != MATCH_YES)
187 return MATCH_NO;
189 /* If we accept, come back here. */
190 dot_loc = gfc_current_locus;
192 /* Try to match a symbol name following the dot. */
193 if (gfc_match_name (name) != MATCH_YES)
195 gfc_error ("Expected structure component or operator name "
196 "after '.' at %C");
197 goto error;
200 /* If no dot follows we have "x.y" which should be a component access. */
201 if (gfc_match_char ('.') != MATCH_YES)
202 goto yes;
204 /* Now we have a string "x.y.z" which could be a nested member access
205 (x->y)->z or a binary operation y on x and z. */
207 /* First use any user-defined operators ".y." */
208 if (gfc_find_uop (name, sym->ns) != NULL)
209 goto no;
211 /* Match accesses to existing derived-type components for
212 derived-type vars: "x.y.z" = (x->y)->z */
213 c = gfc_find_component(tsym, name, false, true, NULL);
214 if (c && (gfc_bt_struct (c->ts.type) || c->ts.type == BT_CLASS))
215 goto yes;
217 /* If y is not a component or has no members, try intrinsic operators. */
218 gfc_current_locus = start_loc;
219 if (gfc_match_intrinsic_op (&iop) != MATCH_YES)
221 /* If ".y." is not an intrinsic operator but y was a valid non-
222 structure component, match and leave the trailing dot to be
223 dealt with later. */
224 if (c)
225 goto yes;
227 gfc_error ("%qs is neither a defined operator nor a "
228 "structure component in dotted string at %C", name);
229 goto error;
232 /* .y. is an intrinsic operator, overriding any possible member access. */
233 goto no;
235 /* Return keeping the current locus consistent with the match result. */
236 error:
237 m = MATCH_ERROR;
239 gfc_current_locus = start_loc;
240 return m;
241 yes:
242 gfc_current_locus = dot_loc;
243 return MATCH_YES;
247 /* This function scans the current statement counting the opened and closed
248 parenthesis to make sure they are balanced. */
250 match
251 gfc_match_parens (void)
253 locus old_loc, where;
254 int count;
255 gfc_instring instring;
256 gfc_char_t c, quote;
258 old_loc = gfc_current_locus;
259 count = 0;
260 instring = NONSTRING;
261 quote = ' ';
263 for (;;)
265 if (count > 0)
266 where = gfc_current_locus;
267 c = gfc_next_char_literal (instring);
268 if (c == '\n')
269 break;
270 if (quote == ' ' && ((c == '\'') || (c == '"')))
272 quote = c;
273 instring = INSTRING_WARN;
274 continue;
276 if (quote != ' ' && c == quote)
278 quote = ' ';
279 instring = NONSTRING;
280 continue;
283 if (c == '(' && quote == ' ')
285 count++;
287 if (c == ')' && quote == ' ')
289 count--;
290 where = gfc_current_locus;
294 gfc_current_locus = old_loc;
296 if (count != 0)
298 gfc_error ("Missing %qs in statement at or before %L",
299 count > 0? ")":"(", &where);
300 return MATCH_ERROR;
303 return MATCH_YES;
307 /* See if the next character is a special character that has
308 escaped by a \ via the -fbackslash option. */
310 match
311 gfc_match_special_char (gfc_char_t *res)
313 int len, i;
314 gfc_char_t c, n;
315 match m;
317 m = MATCH_YES;
319 switch ((c = gfc_next_char_literal (INSTRING_WARN)))
321 case 'a':
322 *res = '\a';
323 break;
324 case 'b':
325 *res = '\b';
326 break;
327 case 't':
328 *res = '\t';
329 break;
330 case 'f':
331 *res = '\f';
332 break;
333 case 'n':
334 *res = '\n';
335 break;
336 case 'r':
337 *res = '\r';
338 break;
339 case 'v':
340 *res = '\v';
341 break;
342 case '\\':
343 *res = '\\';
344 break;
345 case '0':
346 *res = '\0';
347 break;
349 case 'x':
350 case 'u':
351 case 'U':
352 /* Hexadecimal form of wide characters. */
353 len = (c == 'x' ? 2 : (c == 'u' ? 4 : 8));
354 n = 0;
355 for (i = 0; i < len; i++)
357 char buf[2] = { '\0', '\0' };
359 c = gfc_next_char_literal (INSTRING_WARN);
360 if (!gfc_wide_fits_in_byte (c)
361 || !gfc_check_digit ((unsigned char) c, 16))
362 return MATCH_NO;
364 buf[0] = (unsigned char) c;
365 n = n << 4;
366 n += strtol (buf, NULL, 16);
368 *res = n;
369 break;
371 default:
372 /* Unknown backslash codes are simply not expanded. */
373 m = MATCH_NO;
374 break;
377 return m;
381 /* In free form, match at least one space. Always matches in fixed
382 form. */
384 match
385 gfc_match_space (void)
387 locus old_loc;
388 char c;
390 if (gfc_current_form == FORM_FIXED)
391 return MATCH_YES;
393 old_loc = gfc_current_locus;
395 c = gfc_next_ascii_char ();
396 if (!gfc_is_whitespace (c))
398 gfc_current_locus = old_loc;
399 return MATCH_NO;
402 gfc_gobble_whitespace ();
404 return MATCH_YES;
408 /* Match an end of statement. End of statement is optional
409 whitespace, followed by a ';' or '\n' or comment '!'. If a
410 semicolon is found, we continue to eat whitespace and semicolons. */
412 match
413 gfc_match_eos (void)
415 locus old_loc;
416 int flag;
417 char c;
419 flag = 0;
421 for (;;)
423 old_loc = gfc_current_locus;
424 gfc_gobble_whitespace ();
426 c = gfc_next_ascii_char ();
427 switch (c)
429 case '!':
432 c = gfc_next_ascii_char ();
434 while (c != '\n');
436 /* Fall through. */
438 case '\n':
439 return MATCH_YES;
441 case ';':
442 flag = 1;
443 continue;
446 break;
449 gfc_current_locus = old_loc;
450 return (flag) ? MATCH_YES : MATCH_NO;
454 /* Match a literal integer on the input, setting the value on
455 MATCH_YES. Literal ints occur in kind-parameters as well as
456 old-style character length specifications. If cnt is non-NULL it
457 will be set to the number of digits. */
459 match
460 gfc_match_small_literal_int (int *value, int *cnt)
462 locus old_loc;
463 char c;
464 int i, j;
466 old_loc = gfc_current_locus;
468 *value = -1;
469 gfc_gobble_whitespace ();
470 c = gfc_next_ascii_char ();
471 if (cnt)
472 *cnt = 0;
474 if (!ISDIGIT (c))
476 gfc_current_locus = old_loc;
477 return MATCH_NO;
480 i = c - '0';
481 j = 1;
483 for (;;)
485 old_loc = gfc_current_locus;
486 c = gfc_next_ascii_char ();
488 if (!ISDIGIT (c))
489 break;
491 i = 10 * i + c - '0';
492 j++;
494 if (i > 99999999)
496 gfc_error ("Integer too large at %C");
497 return MATCH_ERROR;
501 gfc_current_locus = old_loc;
503 *value = i;
504 if (cnt)
505 *cnt = j;
506 return MATCH_YES;
510 /* Match a small, constant integer expression, like in a kind
511 statement. On MATCH_YES, 'value' is set. */
513 match
514 gfc_match_small_int (int *value)
516 gfc_expr *expr;
517 match m;
518 int i;
520 m = gfc_match_expr (&expr);
521 if (m != MATCH_YES)
522 return m;
524 if (gfc_extract_int (expr, &i, 1))
525 m = MATCH_ERROR;
526 gfc_free_expr (expr);
528 *value = i;
529 return m;
533 /* This function is the same as the gfc_match_small_int, except that
534 we're keeping the pointer to the expr. This function could just be
535 removed and the previously mentioned one modified, though all calls
536 to it would have to be modified then (and there were a number of
537 them). Return MATCH_ERROR if fail to extract the int; otherwise,
538 return the result of gfc_match_expr(). The expr (if any) that was
539 matched is returned in the parameter expr. */
541 match
542 gfc_match_small_int_expr (int *value, gfc_expr **expr)
544 match m;
545 int i;
547 m = gfc_match_expr (expr);
548 if (m != MATCH_YES)
549 return m;
551 if (gfc_extract_int (*expr, &i, 1))
552 m = MATCH_ERROR;
554 *value = i;
555 return m;
559 /* Matches a statement label. Uses gfc_match_small_literal_int() to
560 do most of the work. */
562 match
563 gfc_match_st_label (gfc_st_label **label)
565 locus old_loc;
566 match m;
567 int i, cnt;
569 old_loc = gfc_current_locus;
571 m = gfc_match_small_literal_int (&i, &cnt);
572 if (m != MATCH_YES)
573 return m;
575 if (cnt > 5)
577 gfc_error ("Too many digits in statement label at %C");
578 goto cleanup;
581 if (i == 0)
583 gfc_error ("Statement label at %C is zero");
584 goto cleanup;
587 *label = gfc_get_st_label (i);
588 return MATCH_YES;
590 cleanup:
592 gfc_current_locus = old_loc;
593 return MATCH_ERROR;
597 /* Match and validate a label associated with a named IF, DO or SELECT
598 statement. If the symbol does not have the label attribute, we add
599 it. We also make sure the symbol does not refer to another
600 (active) block. A matched label is pointed to by gfc_new_block. */
602 match
603 gfc_match_label (void)
605 char name[GFC_MAX_SYMBOL_LEN + 1];
606 match m;
608 gfc_new_block = NULL;
610 m = gfc_match (" %n :", name);
611 if (m != MATCH_YES)
612 return m;
614 if (gfc_get_symbol (name, NULL, &gfc_new_block))
616 gfc_error ("Label name %qs at %C is ambiguous", name);
617 return MATCH_ERROR;
620 if (gfc_new_block->attr.flavor == FL_LABEL)
622 gfc_error ("Duplicate construct label %qs at %C", name);
623 return MATCH_ERROR;
626 if (!gfc_add_flavor (&gfc_new_block->attr, FL_LABEL,
627 gfc_new_block->name, NULL))
628 return MATCH_ERROR;
630 return MATCH_YES;
634 /* See if the current input looks like a name of some sort. Modifies
635 the passed buffer which must be GFC_MAX_SYMBOL_LEN+1 bytes long.
636 Note that options.c restricts max_identifier_length to not more
637 than GFC_MAX_SYMBOL_LEN. */
639 match
640 gfc_match_name (char *buffer)
642 locus old_loc;
643 int i;
644 char c;
646 old_loc = gfc_current_locus;
647 gfc_gobble_whitespace ();
649 c = gfc_next_ascii_char ();
650 if (!(ISALPHA (c) || (c == '_' && flag_allow_leading_underscore)))
652 /* Special cases for unary minus and plus, which allows for a sensible
653 error message for code of the form 'c = exp(-a*b) )' where an
654 extra ')' appears at the end of statement. */
655 if (!gfc_error_flag_test () && c != '(' && c != '-' && c != '+')
656 gfc_error ("Invalid character in name at %C");
657 gfc_current_locus = old_loc;
658 return MATCH_NO;
661 i = 0;
665 buffer[i++] = c;
667 if (i > gfc_option.max_identifier_length)
669 gfc_error ("Name at %C is too long");
670 return MATCH_ERROR;
673 old_loc = gfc_current_locus;
674 c = gfc_next_ascii_char ();
676 while (ISALNUM (c) || c == '_' || (flag_dollar_ok && c == '$'));
678 if (c == '$' && !flag_dollar_ok)
680 gfc_fatal_error ("Invalid character %<$%> at %L. Use %<-fdollar-ok%> to "
681 "allow it as an extension", &old_loc);
682 return MATCH_ERROR;
685 buffer[i] = '\0';
686 gfc_current_locus = old_loc;
688 return MATCH_YES;
692 /* Match a symbol on the input. Modifies the pointer to the symbol
693 pointer if successful. */
695 match
696 gfc_match_sym_tree (gfc_symtree **matched_symbol, int host_assoc)
698 char buffer[GFC_MAX_SYMBOL_LEN + 1];
699 match m;
701 m = gfc_match_name (buffer);
702 if (m != MATCH_YES)
703 return m;
705 if (host_assoc)
706 return (gfc_get_ha_sym_tree (buffer, matched_symbol))
707 ? MATCH_ERROR : MATCH_YES;
709 if (gfc_get_sym_tree (buffer, NULL, matched_symbol, false))
710 return MATCH_ERROR;
712 return MATCH_YES;
716 match
717 gfc_match_symbol (gfc_symbol **matched_symbol, int host_assoc)
719 gfc_symtree *st;
720 match m;
722 m = gfc_match_sym_tree (&st, host_assoc);
724 if (m == MATCH_YES)
726 if (st)
727 *matched_symbol = st->n.sym;
728 else
729 *matched_symbol = NULL;
731 else
732 *matched_symbol = NULL;
733 return m;
737 /* Match an intrinsic operator. Returns an INTRINSIC enum. While matching,
738 we always find INTRINSIC_PLUS before INTRINSIC_UPLUS. We work around this
739 in matchexp.c. */
741 match
742 gfc_match_intrinsic_op (gfc_intrinsic_op *result)
744 locus orig_loc = gfc_current_locus;
745 char ch;
747 gfc_gobble_whitespace ();
748 ch = gfc_next_ascii_char ();
749 switch (ch)
751 case '+':
752 /* Matched "+". */
753 *result = INTRINSIC_PLUS;
754 return MATCH_YES;
756 case '-':
757 /* Matched "-". */
758 *result = INTRINSIC_MINUS;
759 return MATCH_YES;
761 case '=':
762 if (gfc_next_ascii_char () == '=')
764 /* Matched "==". */
765 *result = INTRINSIC_EQ;
766 return MATCH_YES;
768 break;
770 case '<':
771 if (gfc_peek_ascii_char () == '=')
773 /* Matched "<=". */
774 gfc_next_ascii_char ();
775 *result = INTRINSIC_LE;
776 return MATCH_YES;
778 /* Matched "<". */
779 *result = INTRINSIC_LT;
780 return MATCH_YES;
782 case '>':
783 if (gfc_peek_ascii_char () == '=')
785 /* Matched ">=". */
786 gfc_next_ascii_char ();
787 *result = INTRINSIC_GE;
788 return MATCH_YES;
790 /* Matched ">". */
791 *result = INTRINSIC_GT;
792 return MATCH_YES;
794 case '*':
795 if (gfc_peek_ascii_char () == '*')
797 /* Matched "**". */
798 gfc_next_ascii_char ();
799 *result = INTRINSIC_POWER;
800 return MATCH_YES;
802 /* Matched "*". */
803 *result = INTRINSIC_TIMES;
804 return MATCH_YES;
806 case '/':
807 ch = gfc_peek_ascii_char ();
808 if (ch == '=')
810 /* Matched "/=". */
811 gfc_next_ascii_char ();
812 *result = INTRINSIC_NE;
813 return MATCH_YES;
815 else if (ch == '/')
817 /* Matched "//". */
818 gfc_next_ascii_char ();
819 *result = INTRINSIC_CONCAT;
820 return MATCH_YES;
822 /* Matched "/". */
823 *result = INTRINSIC_DIVIDE;
824 return MATCH_YES;
826 case '.':
827 ch = gfc_next_ascii_char ();
828 switch (ch)
830 case 'a':
831 if (gfc_next_ascii_char () == 'n'
832 && gfc_next_ascii_char () == 'd'
833 && gfc_next_ascii_char () == '.')
835 /* Matched ".and.". */
836 *result = INTRINSIC_AND;
837 return MATCH_YES;
839 break;
841 case 'e':
842 if (gfc_next_ascii_char () == 'q')
844 ch = gfc_next_ascii_char ();
845 if (ch == '.')
847 /* Matched ".eq.". */
848 *result = INTRINSIC_EQ_OS;
849 return MATCH_YES;
851 else if (ch == 'v')
853 if (gfc_next_ascii_char () == '.')
855 /* Matched ".eqv.". */
856 *result = INTRINSIC_EQV;
857 return MATCH_YES;
861 break;
863 case 'g':
864 ch = gfc_next_ascii_char ();
865 if (ch == 'e')
867 if (gfc_next_ascii_char () == '.')
869 /* Matched ".ge.". */
870 *result = INTRINSIC_GE_OS;
871 return MATCH_YES;
874 else if (ch == 't')
876 if (gfc_next_ascii_char () == '.')
878 /* Matched ".gt.". */
879 *result = INTRINSIC_GT_OS;
880 return MATCH_YES;
883 break;
885 case 'l':
886 ch = gfc_next_ascii_char ();
887 if (ch == 'e')
889 if (gfc_next_ascii_char () == '.')
891 /* Matched ".le.". */
892 *result = INTRINSIC_LE_OS;
893 return MATCH_YES;
896 else if (ch == 't')
898 if (gfc_next_ascii_char () == '.')
900 /* Matched ".lt.". */
901 *result = INTRINSIC_LT_OS;
902 return MATCH_YES;
905 break;
907 case 'n':
908 ch = gfc_next_ascii_char ();
909 if (ch == 'e')
911 ch = gfc_next_ascii_char ();
912 if (ch == '.')
914 /* Matched ".ne.". */
915 *result = INTRINSIC_NE_OS;
916 return MATCH_YES;
918 else if (ch == 'q')
920 if (gfc_next_ascii_char () == 'v'
921 && gfc_next_ascii_char () == '.')
923 /* Matched ".neqv.". */
924 *result = INTRINSIC_NEQV;
925 return MATCH_YES;
929 else if (ch == 'o')
931 if (gfc_next_ascii_char () == 't'
932 && gfc_next_ascii_char () == '.')
934 /* Matched ".not.". */
935 *result = INTRINSIC_NOT;
936 return MATCH_YES;
939 break;
941 case 'o':
942 if (gfc_next_ascii_char () == 'r'
943 && gfc_next_ascii_char () == '.')
945 /* Matched ".or.". */
946 *result = INTRINSIC_OR;
947 return MATCH_YES;
949 break;
951 case 'x':
952 if (gfc_next_ascii_char () == 'o'
953 && gfc_next_ascii_char () == 'r'
954 && gfc_next_ascii_char () == '.')
956 if (!gfc_notify_std (GFC_STD_LEGACY, ".XOR. operator at %C"))
957 return MATCH_ERROR;
958 /* Matched ".xor." - equivalent to ".neqv.". */
959 *result = INTRINSIC_NEQV;
960 return MATCH_YES;
962 break;
964 default:
965 break;
967 break;
969 default:
970 break;
973 gfc_current_locus = orig_loc;
974 return MATCH_NO;
978 /* Match a loop control phrase:
980 <LVALUE> = <EXPR>, <EXPR> [, <EXPR> ]
982 If the final integer expression is not present, a constant unity
983 expression is returned. We don't return MATCH_ERROR until after
984 the equals sign is seen. */
986 match
987 gfc_match_iterator (gfc_iterator *iter, int init_flag)
989 char name[GFC_MAX_SYMBOL_LEN + 1];
990 gfc_expr *var, *e1, *e2, *e3;
991 locus start;
992 match m;
994 e1 = e2 = e3 = NULL;
996 /* Match the start of an iterator without affecting the symbol table. */
998 start = gfc_current_locus;
999 m = gfc_match (" %n =", name);
1000 gfc_current_locus = start;
1002 if (m != MATCH_YES)
1003 return MATCH_NO;
1005 m = gfc_match_variable (&var, 0);
1006 if (m != MATCH_YES)
1007 return MATCH_NO;
1009 if (var->symtree->n.sym->attr.dimension)
1011 gfc_error ("Loop variable at %C cannot be an array");
1012 goto cleanup;
1015 /* F2008, C617 & C565. */
1016 if (var->symtree->n.sym->attr.codimension)
1018 gfc_error ("Loop variable at %C cannot be a coarray");
1019 goto cleanup;
1022 if (var->ref != NULL)
1024 gfc_error ("Loop variable at %C cannot be a sub-component");
1025 goto cleanup;
1028 gfc_match_char ('=');
1030 var->symtree->n.sym->attr.implied_index = 1;
1032 m = init_flag ? gfc_match_init_expr (&e1) : gfc_match_expr (&e1);
1033 if (m == MATCH_NO)
1034 goto syntax;
1035 if (m == MATCH_ERROR)
1036 goto cleanup;
1038 if (gfc_match_char (',') != MATCH_YES)
1039 goto syntax;
1041 m = init_flag ? gfc_match_init_expr (&e2) : gfc_match_expr (&e2);
1042 if (m == MATCH_NO)
1043 goto syntax;
1044 if (m == MATCH_ERROR)
1045 goto cleanup;
1047 if (gfc_match_char (',') != MATCH_YES)
1049 e3 = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
1050 goto done;
1053 m = init_flag ? gfc_match_init_expr (&e3) : gfc_match_expr (&e3);
1054 if (m == MATCH_ERROR)
1055 goto cleanup;
1056 if (m == MATCH_NO)
1058 gfc_error ("Expected a step value in iterator at %C");
1059 goto cleanup;
1062 done:
1063 iter->var = var;
1064 iter->start = e1;
1065 iter->end = e2;
1066 iter->step = e3;
1067 return MATCH_YES;
1069 syntax:
1070 gfc_error ("Syntax error in iterator at %C");
1072 cleanup:
1073 gfc_free_expr (e1);
1074 gfc_free_expr (e2);
1075 gfc_free_expr (e3);
1077 return MATCH_ERROR;
1081 /* Tries to match the next non-whitespace character on the input.
1082 This subroutine does not return MATCH_ERROR. */
1084 match
1085 gfc_match_char (char c)
1087 locus where;
1089 where = gfc_current_locus;
1090 gfc_gobble_whitespace ();
1092 if (gfc_next_ascii_char () == c)
1093 return MATCH_YES;
1095 gfc_current_locus = where;
1096 return MATCH_NO;
1100 /* General purpose matching subroutine. The target string is a
1101 scanf-like format string in which spaces correspond to arbitrary
1102 whitespace (including no whitespace), characters correspond to
1103 themselves. The %-codes are:
1105 %% Literal percent sign
1106 %e Expression, pointer to a pointer is set
1107 %s Symbol, pointer to the symbol is set
1108 %n Name, character buffer is set to name
1109 %t Matches end of statement.
1110 %o Matches an intrinsic operator, returned as an INTRINSIC enum.
1111 %l Matches a statement label
1112 %v Matches a variable expression (an lvalue)
1113 % Matches a required space (in free form) and optional spaces. */
1115 match
1116 gfc_match (const char *target, ...)
1118 gfc_st_label **label;
1119 int matches, *ip;
1120 locus old_loc;
1121 va_list argp;
1122 char c, *np;
1123 match m, n;
1124 void **vp;
1125 const char *p;
1127 old_loc = gfc_current_locus;
1128 va_start (argp, target);
1129 m = MATCH_NO;
1130 matches = 0;
1131 p = target;
1133 loop:
1134 c = *p++;
1135 switch (c)
1137 case ' ':
1138 gfc_gobble_whitespace ();
1139 goto loop;
1140 case '\0':
1141 m = MATCH_YES;
1142 break;
1144 case '%':
1145 c = *p++;
1146 switch (c)
1148 case 'e':
1149 vp = va_arg (argp, void **);
1150 n = gfc_match_expr ((gfc_expr **) vp);
1151 if (n != MATCH_YES)
1153 m = n;
1154 goto not_yes;
1157 matches++;
1158 goto loop;
1160 case 'v':
1161 vp = va_arg (argp, void **);
1162 n = gfc_match_variable ((gfc_expr **) vp, 0);
1163 if (n != MATCH_YES)
1165 m = n;
1166 goto not_yes;
1169 matches++;
1170 goto loop;
1172 case 's':
1173 vp = va_arg (argp, void **);
1174 n = gfc_match_symbol ((gfc_symbol **) vp, 0);
1175 if (n != MATCH_YES)
1177 m = n;
1178 goto not_yes;
1181 matches++;
1182 goto loop;
1184 case 'n':
1185 np = va_arg (argp, char *);
1186 n = gfc_match_name (np);
1187 if (n != MATCH_YES)
1189 m = n;
1190 goto not_yes;
1193 matches++;
1194 goto loop;
1196 case 'l':
1197 label = va_arg (argp, gfc_st_label **);
1198 n = gfc_match_st_label (label);
1199 if (n != MATCH_YES)
1201 m = n;
1202 goto not_yes;
1205 matches++;
1206 goto loop;
1208 case 'o':
1209 ip = va_arg (argp, int *);
1210 n = gfc_match_intrinsic_op ((gfc_intrinsic_op *) ip);
1211 if (n != MATCH_YES)
1213 m = n;
1214 goto not_yes;
1217 matches++;
1218 goto loop;
1220 case 't':
1221 if (gfc_match_eos () != MATCH_YES)
1223 m = MATCH_NO;
1224 goto not_yes;
1226 goto loop;
1228 case ' ':
1229 if (gfc_match_space () == MATCH_YES)
1230 goto loop;
1231 m = MATCH_NO;
1232 goto not_yes;
1234 case '%':
1235 break; /* Fall through to character matcher. */
1237 default:
1238 gfc_internal_error ("gfc_match(): Bad match code %c", c);
1240 /* FALLTHRU */
1242 default:
1244 /* gfc_next_ascii_char converts characters to lower-case, so we shouldn't
1245 expect an upper case character here! */
1246 gcc_assert (TOLOWER (c) == c);
1248 if (c == gfc_next_ascii_char ())
1249 goto loop;
1250 break;
1253 not_yes:
1254 va_end (argp);
1256 if (m != MATCH_YES)
1258 /* Clean up after a failed match. */
1259 gfc_current_locus = old_loc;
1260 va_start (argp, target);
1262 p = target;
1263 for (; matches > 0; matches--)
1265 while (*p++ != '%');
1267 switch (*p++)
1269 case '%':
1270 matches++;
1271 break; /* Skip. */
1273 /* Matches that don't have to be undone */
1274 case 'o':
1275 case 'l':
1276 case 'n':
1277 case 's':
1278 (void) va_arg (argp, void **);
1279 break;
1281 case 'e':
1282 case 'v':
1283 vp = va_arg (argp, void **);
1284 gfc_free_expr ((struct gfc_expr *)*vp);
1285 *vp = NULL;
1286 break;
1290 va_end (argp);
1293 return m;
1297 /*********************** Statement level matching **********************/
1299 /* Matches the start of a program unit, which is the program keyword
1300 followed by an obligatory symbol. */
1302 match
1303 gfc_match_program (void)
1305 gfc_symbol *sym;
1306 match m;
1308 m = gfc_match ("% %s%t", &sym);
1310 if (m == MATCH_NO)
1312 gfc_error ("Invalid form of PROGRAM statement at %C");
1313 m = MATCH_ERROR;
1316 if (m == MATCH_ERROR)
1317 return m;
1319 if (!gfc_add_flavor (&sym->attr, FL_PROGRAM, sym->name, NULL))
1320 return MATCH_ERROR;
1322 gfc_new_block = sym;
1324 return MATCH_YES;
1328 /* Match a simple assignment statement. */
1330 match
1331 gfc_match_assignment (void)
1333 gfc_expr *lvalue, *rvalue;
1334 locus old_loc;
1335 match m;
1337 old_loc = gfc_current_locus;
1339 lvalue = NULL;
1340 m = gfc_match (" %v =", &lvalue);
1341 if (m != MATCH_YES)
1343 gfc_current_locus = old_loc;
1344 gfc_free_expr (lvalue);
1345 return MATCH_NO;
1348 rvalue = NULL;
1349 m = gfc_match (" %e%t", &rvalue);
1351 if (lvalue->expr_type == EXPR_CONSTANT)
1353 /* This clobbers %len and %kind. */
1354 m = MATCH_ERROR;
1355 gfc_error ("Assignment to a constant expression at %C");
1358 if (m != MATCH_YES)
1360 gfc_current_locus = old_loc;
1361 gfc_free_expr (lvalue);
1362 gfc_free_expr (rvalue);
1363 return m;
1366 gfc_set_sym_referenced (lvalue->symtree->n.sym);
1368 new_st.op = EXEC_ASSIGN;
1369 new_st.expr1 = lvalue;
1370 new_st.expr2 = rvalue;
1372 gfc_check_do_variable (lvalue->symtree);
1374 if (lvalue->ts.type == BT_CLASS)
1375 gfc_find_vtab (&rvalue->ts);
1377 return MATCH_YES;
1381 /* Match a pointer assignment statement. */
1383 match
1384 gfc_match_pointer_assignment (void)
1386 gfc_expr *lvalue, *rvalue;
1387 locus old_loc;
1388 match m;
1390 old_loc = gfc_current_locus;
1392 lvalue = rvalue = NULL;
1393 gfc_matching_ptr_assignment = 0;
1394 gfc_matching_procptr_assignment = 0;
1396 m = gfc_match (" %v =>", &lvalue);
1397 if (m != MATCH_YES)
1399 m = MATCH_NO;
1400 goto cleanup;
1403 if (lvalue->symtree->n.sym->attr.proc_pointer
1404 || gfc_is_proc_ptr_comp (lvalue))
1405 gfc_matching_procptr_assignment = 1;
1406 else
1407 gfc_matching_ptr_assignment = 1;
1409 m = gfc_match (" %e%t", &rvalue);
1410 gfc_matching_ptr_assignment = 0;
1411 gfc_matching_procptr_assignment = 0;
1412 if (m != MATCH_YES)
1413 goto cleanup;
1415 new_st.op = EXEC_POINTER_ASSIGN;
1416 new_st.expr1 = lvalue;
1417 new_st.expr2 = rvalue;
1419 return MATCH_YES;
1421 cleanup:
1422 gfc_current_locus = old_loc;
1423 gfc_free_expr (lvalue);
1424 gfc_free_expr (rvalue);
1425 return m;
1429 /* We try to match an easy arithmetic IF statement. This only happens
1430 when just after having encountered a simple IF statement. This code
1431 is really duplicate with parts of the gfc_match_if code, but this is
1432 *much* easier. */
1434 static match
1435 match_arithmetic_if (void)
1437 gfc_st_label *l1, *l2, *l3;
1438 gfc_expr *expr;
1439 match m;
1441 m = gfc_match (" ( %e ) %l , %l , %l%t", &expr, &l1, &l2, &l3);
1442 if (m != MATCH_YES)
1443 return m;
1445 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1446 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1447 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1449 gfc_free_expr (expr);
1450 return MATCH_ERROR;
1453 if (!gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
1454 "Arithmetic IF statement at %C"))
1455 return MATCH_ERROR;
1457 new_st.op = EXEC_ARITHMETIC_IF;
1458 new_st.expr1 = expr;
1459 new_st.label1 = l1;
1460 new_st.label2 = l2;
1461 new_st.label3 = l3;
1463 return MATCH_YES;
1467 /* The IF statement is a bit of a pain. First of all, there are three
1468 forms of it, the simple IF, the IF that starts a block and the
1469 arithmetic IF.
1471 There is a problem with the simple IF and that is the fact that we
1472 only have a single level of undo information on symbols. What this
1473 means is for a simple IF, we must re-match the whole IF statement
1474 multiple times in order to guarantee that the symbol table ends up
1475 in the proper state. */
1477 static match match_simple_forall (void);
1478 static match match_simple_where (void);
1480 match
1481 gfc_match_if (gfc_statement *if_type)
1483 gfc_expr *expr;
1484 gfc_st_label *l1, *l2, *l3;
1485 locus old_loc, old_loc2;
1486 gfc_code *p;
1487 match m, n;
1489 n = gfc_match_label ();
1490 if (n == MATCH_ERROR)
1491 return n;
1493 old_loc = gfc_current_locus;
1495 m = gfc_match (" if ", &expr);
1496 if (m != MATCH_YES)
1497 return m;
1499 if (gfc_match_char ('(') != MATCH_YES)
1501 gfc_error ("Missing %<(%> in IF-expression at %C");
1502 return MATCH_ERROR;
1505 m = gfc_match ("%e", &expr);
1506 if (m != MATCH_YES)
1507 return m;
1509 old_loc2 = gfc_current_locus;
1510 gfc_current_locus = old_loc;
1512 if (gfc_match_parens () == MATCH_ERROR)
1513 return MATCH_ERROR;
1515 gfc_current_locus = old_loc2;
1517 if (gfc_match_char (')') != MATCH_YES)
1519 gfc_error ("Syntax error in IF-expression at %C");
1520 gfc_free_expr (expr);
1521 return MATCH_ERROR;
1524 m = gfc_match (" %l , %l , %l%t", &l1, &l2, &l3);
1526 if (m == MATCH_YES)
1528 if (n == MATCH_YES)
1530 gfc_error ("Block label not appropriate for arithmetic IF "
1531 "statement at %C");
1532 gfc_free_expr (expr);
1533 return MATCH_ERROR;
1536 if (!gfc_reference_st_label (l1, ST_LABEL_TARGET)
1537 || !gfc_reference_st_label (l2, ST_LABEL_TARGET)
1538 || !gfc_reference_st_label (l3, ST_LABEL_TARGET))
1540 gfc_free_expr (expr);
1541 return MATCH_ERROR;
1544 if (!gfc_notify_std (GFC_STD_F95_OBS | GFC_STD_F2018_DEL,
1545 "Arithmetic IF statement at %C"))
1546 return MATCH_ERROR;
1548 new_st.op = EXEC_ARITHMETIC_IF;
1549 new_st.expr1 = expr;
1550 new_st.label1 = l1;
1551 new_st.label2 = l2;
1552 new_st.label3 = l3;
1554 *if_type = ST_ARITHMETIC_IF;
1555 return MATCH_YES;
1558 if (gfc_match (" then%t") == MATCH_YES)
1560 new_st.op = EXEC_IF;
1561 new_st.expr1 = expr;
1562 *if_type = ST_IF_BLOCK;
1563 return MATCH_YES;
1566 if (n == MATCH_YES)
1568 gfc_error ("Block label is not appropriate for IF statement at %C");
1569 gfc_free_expr (expr);
1570 return MATCH_ERROR;
1573 /* At this point the only thing left is a simple IF statement. At
1574 this point, n has to be MATCH_NO, so we don't have to worry about
1575 re-matching a block label. From what we've got so far, try
1576 matching an assignment. */
1578 *if_type = ST_SIMPLE_IF;
1580 m = gfc_match_assignment ();
1581 if (m == MATCH_YES)
1582 goto got_match;
1584 gfc_free_expr (expr);
1585 gfc_undo_symbols ();
1586 gfc_current_locus = old_loc;
1588 /* m can be MATCH_NO or MATCH_ERROR, here. For MATCH_ERROR, a mangled
1589 assignment was found. For MATCH_NO, continue to call the various
1590 matchers. */
1591 if (m == MATCH_ERROR)
1592 return MATCH_ERROR;
1594 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1596 m = gfc_match_pointer_assignment ();
1597 if (m == MATCH_YES)
1598 goto got_match;
1600 gfc_free_expr (expr);
1601 gfc_undo_symbols ();
1602 gfc_current_locus = old_loc;
1604 gfc_match (" if ( %e ) ", &expr); /* Guaranteed to match. */
1606 /* Look at the next keyword to see which matcher to call. Matching
1607 the keyword doesn't affect the symbol table, so we don't have to
1608 restore between tries. */
1610 #define match(string, subr, statement) \
1611 if (gfc_match (string) == MATCH_YES) { m = subr(); goto got_match; }
1613 gfc_clear_error ();
1615 match ("allocate", gfc_match_allocate, ST_ALLOCATE)
1616 match ("assign", gfc_match_assign, ST_LABEL_ASSIGNMENT)
1617 match ("backspace", gfc_match_backspace, ST_BACKSPACE)
1618 match ("call", gfc_match_call, ST_CALL)
1619 match ("change team", gfc_match_change_team, ST_CHANGE_TEAM)
1620 match ("close", gfc_match_close, ST_CLOSE)
1621 match ("continue", gfc_match_continue, ST_CONTINUE)
1622 match ("cycle", gfc_match_cycle, ST_CYCLE)
1623 match ("deallocate", gfc_match_deallocate, ST_DEALLOCATE)
1624 match ("end file", gfc_match_endfile, ST_END_FILE)
1625 match ("end team", gfc_match_end_team, ST_END_TEAM)
1626 match ("error stop", gfc_match_error_stop, ST_ERROR_STOP)
1627 match ("event post", gfc_match_event_post, ST_EVENT_POST)
1628 match ("event wait", gfc_match_event_wait, ST_EVENT_WAIT)
1629 match ("exit", gfc_match_exit, ST_EXIT)
1630 match ("fail image", gfc_match_fail_image, ST_FAIL_IMAGE)
1631 match ("flush", gfc_match_flush, ST_FLUSH)
1632 match ("forall", match_simple_forall, ST_FORALL)
1633 match ("form team", gfc_match_form_team, ST_FORM_TEAM)
1634 match ("go to", gfc_match_goto, ST_GOTO)
1635 match ("if", match_arithmetic_if, ST_ARITHMETIC_IF)
1636 match ("inquire", gfc_match_inquire, ST_INQUIRE)
1637 match ("lock", gfc_match_lock, ST_LOCK)
1638 match ("nullify", gfc_match_nullify, ST_NULLIFY)
1639 match ("open", gfc_match_open, ST_OPEN)
1640 match ("pause", gfc_match_pause, ST_NONE)
1641 match ("print", gfc_match_print, ST_WRITE)
1642 match ("read", gfc_match_read, ST_READ)
1643 match ("return", gfc_match_return, ST_RETURN)
1644 match ("rewind", gfc_match_rewind, ST_REWIND)
1645 match ("stop", gfc_match_stop, ST_STOP)
1646 match ("wait", gfc_match_wait, ST_WAIT)
1647 match ("sync all", gfc_match_sync_all, ST_SYNC_CALL);
1648 match ("sync images", gfc_match_sync_images, ST_SYNC_IMAGES);
1649 match ("sync memory", gfc_match_sync_memory, ST_SYNC_MEMORY);
1650 match ("sync team", gfc_match_sync_team, ST_SYNC_TEAM)
1651 match ("unlock", gfc_match_unlock, ST_UNLOCK)
1652 match ("where", match_simple_where, ST_WHERE)
1653 match ("write", gfc_match_write, ST_WRITE)
1655 if (flag_dec)
1656 match ("type", gfc_match_print, ST_WRITE)
1658 /* All else has failed, so give up. See if any of the matchers has
1659 stored an error message of some sort. */
1660 if (!gfc_error_check ())
1661 gfc_error ("Syntax error in IF-clause after %C");
1663 gfc_free_expr (expr);
1664 return MATCH_ERROR;
1666 got_match:
1667 if (m == MATCH_NO)
1668 gfc_error ("Syntax error in IF-clause after %C");
1669 if (m != MATCH_YES)
1671 gfc_free_expr (expr);
1672 return MATCH_ERROR;
1675 /* At this point, we've matched the single IF and the action clause
1676 is in new_st. Rearrange things so that the IF statement appears
1677 in new_st. */
1679 p = gfc_get_code (EXEC_IF);
1680 p->next = XCNEW (gfc_code);
1681 *p->next = new_st;
1682 p->next->loc = gfc_current_locus;
1684 p->expr1 = expr;
1686 gfc_clear_new_st ();
1688 new_st.op = EXEC_IF;
1689 new_st.block = p;
1691 return MATCH_YES;
1694 #undef match
1697 /* Match an ELSE statement. */
1699 match
1700 gfc_match_else (void)
1702 char name[GFC_MAX_SYMBOL_LEN + 1];
1704 if (gfc_match_eos () == MATCH_YES)
1705 return MATCH_YES;
1707 if (gfc_match_name (name) != MATCH_YES
1708 || gfc_current_block () == NULL
1709 || gfc_match_eos () != MATCH_YES)
1711 gfc_error ("Invalid character(s) in ELSE statement after %C");
1712 return MATCH_ERROR;
1715 if (strcmp (name, gfc_current_block ()->name) != 0)
1717 gfc_error ("Label %qs at %C doesn't match IF label %qs",
1718 name, gfc_current_block ()->name);
1719 return MATCH_ERROR;
1722 return MATCH_YES;
1726 /* Match an ELSE IF statement. */
1728 match
1729 gfc_match_elseif (void)
1731 char name[GFC_MAX_SYMBOL_LEN + 1];
1732 gfc_expr *expr, *then;
1733 locus where;
1734 match m;
1736 if (gfc_match_char ('(') != MATCH_YES)
1738 gfc_error ("Missing %<(%> in ELSE IF expression at %C");
1739 return MATCH_ERROR;
1742 m = gfc_match (" %e ", &expr);
1743 if (m != MATCH_YES)
1744 return m;
1746 if (gfc_match_char (')') != MATCH_YES)
1748 gfc_error ("Missing %<)%> in ELSE IF expression at %C");
1749 goto cleanup;
1752 m = gfc_match (" then ", &then);
1754 where = gfc_current_locus;
1756 if (m == MATCH_YES && (gfc_match_eos () == MATCH_YES
1757 || (gfc_current_block ()
1758 && gfc_match_name (name) == MATCH_YES)))
1759 goto done;
1761 if (gfc_match_eos () == MATCH_YES)
1763 gfc_error ("Missing THEN in ELSE IF statement after %L", &where);
1764 goto cleanup;
1767 if (gfc_match_name (name) != MATCH_YES
1768 || gfc_current_block () == NULL
1769 || gfc_match_eos () != MATCH_YES)
1771 gfc_error ("Syntax error in ELSE IF statement after %L", &where);
1772 goto cleanup;
1775 if (strcmp (name, gfc_current_block ()->name) != 0)
1777 gfc_error ("Label %qs after %L doesn't match IF label %qs",
1778 name, &where, gfc_current_block ()->name);
1779 goto cleanup;
1782 if (m != MATCH_YES)
1783 return m;
1785 done:
1786 new_st.op = EXEC_IF;
1787 new_st.expr1 = expr;
1788 return MATCH_YES;
1790 cleanup:
1791 gfc_free_expr (expr);
1792 return MATCH_ERROR;
1796 /* Free a gfc_iterator structure. */
1798 void
1799 gfc_free_iterator (gfc_iterator *iter, int flag)
1802 if (iter == NULL)
1803 return;
1805 gfc_free_expr (iter->var);
1806 gfc_free_expr (iter->start);
1807 gfc_free_expr (iter->end);
1808 gfc_free_expr (iter->step);
1810 if (flag)
1811 free (iter);
1815 /* Match a CRITICAL statement. */
1816 match
1817 gfc_match_critical (void)
1819 gfc_st_label *label = NULL;
1821 if (gfc_match_label () == MATCH_ERROR)
1822 return MATCH_ERROR;
1824 if (gfc_match (" critical") != MATCH_YES)
1825 return MATCH_NO;
1827 if (gfc_match_st_label (&label) == MATCH_ERROR)
1828 return MATCH_ERROR;
1830 if (gfc_match_eos () != MATCH_YES)
1832 gfc_syntax_error (ST_CRITICAL);
1833 return MATCH_ERROR;
1836 if (gfc_pure (NULL))
1838 gfc_error ("Image control statement CRITICAL at %C in PURE procedure");
1839 return MATCH_ERROR;
1842 if (gfc_find_state (COMP_DO_CONCURRENT))
1844 gfc_error ("Image control statement CRITICAL at %C in DO CONCURRENT "
1845 "block");
1846 return MATCH_ERROR;
1849 gfc_unset_implicit_pure (NULL);
1851 if (!gfc_notify_std (GFC_STD_F2008, "CRITICAL statement at %C"))
1852 return MATCH_ERROR;
1854 if (flag_coarray == GFC_FCOARRAY_NONE)
1856 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
1857 "enable");
1858 return MATCH_ERROR;
1861 if (gfc_find_state (COMP_CRITICAL))
1863 gfc_error ("Nested CRITICAL block at %C");
1864 return MATCH_ERROR;
1867 new_st.op = EXEC_CRITICAL;
1869 if (label != NULL
1870 && !gfc_reference_st_label (label, ST_LABEL_TARGET))
1871 return MATCH_ERROR;
1873 return MATCH_YES;
1877 /* Match a BLOCK statement. */
1879 match
1880 gfc_match_block (void)
1882 match m;
1884 if (gfc_match_label () == MATCH_ERROR)
1885 return MATCH_ERROR;
1887 if (gfc_match (" block") != MATCH_YES)
1888 return MATCH_NO;
1890 /* For this to be a correct BLOCK statement, the line must end now. */
1891 m = gfc_match_eos ();
1892 if (m == MATCH_ERROR)
1893 return MATCH_ERROR;
1894 if (m == MATCH_NO)
1895 return MATCH_NO;
1897 return MATCH_YES;
1901 /* Match an ASSOCIATE statement. */
1903 match
1904 gfc_match_associate (void)
1906 if (gfc_match_label () == MATCH_ERROR)
1907 return MATCH_ERROR;
1909 if (gfc_match (" associate") != MATCH_YES)
1910 return MATCH_NO;
1912 /* Match the association list. */
1913 if (gfc_match_char ('(') != MATCH_YES)
1915 gfc_error ("Expected association list at %C");
1916 return MATCH_ERROR;
1918 new_st.ext.block.assoc = NULL;
1919 while (true)
1921 gfc_association_list* newAssoc = gfc_get_association_list ();
1922 gfc_association_list* a;
1924 /* Match the next association. */
1925 if (gfc_match (" %n =>", newAssoc->name) != MATCH_YES)
1927 gfc_error ("Expected association at %C");
1928 goto assocListError;
1931 if (gfc_match (" %e", &newAssoc->target) != MATCH_YES)
1933 /* Have another go, allowing for procedure pointer selectors. */
1934 gfc_matching_procptr_assignment = 1;
1935 if (gfc_match (" %e", &newAssoc->target) != MATCH_YES)
1937 gfc_error ("Invalid association target at %C");
1938 goto assocListError;
1940 gfc_matching_procptr_assignment = 0;
1942 newAssoc->where = gfc_current_locus;
1944 /* Check that the current name is not yet in the list. */
1945 for (a = new_st.ext.block.assoc; a; a = a->next)
1946 if (!strcmp (a->name, newAssoc->name))
1948 gfc_error ("Duplicate name %qs in association at %C",
1949 newAssoc->name);
1950 goto assocListError;
1953 /* The target expression must not be coindexed. */
1954 if (gfc_is_coindexed (newAssoc->target))
1956 gfc_error ("Association target at %C must not be coindexed");
1957 goto assocListError;
1960 /* The target expression cannot be a BOZ literal constant. */
1961 if (newAssoc->target->ts.type == BT_BOZ)
1963 gfc_error ("Association target at %L cannot be a BOZ literal "
1964 "constant", &newAssoc->target->where);
1965 goto assocListError;
1968 /* The `variable' field is left blank for now; because the target is not
1969 yet resolved, we can't use gfc_has_vector_subscript to determine it
1970 for now. This is set during resolution. */
1972 /* Put it into the list. */
1973 newAssoc->next = new_st.ext.block.assoc;
1974 new_st.ext.block.assoc = newAssoc;
1976 /* Try next one or end if closing parenthesis is found. */
1977 gfc_gobble_whitespace ();
1978 if (gfc_peek_char () == ')')
1979 break;
1980 if (gfc_match_char (',') != MATCH_YES)
1982 gfc_error ("Expected %<)%> or %<,%> at %C");
1983 return MATCH_ERROR;
1986 continue;
1988 assocListError:
1989 free (newAssoc);
1990 goto error;
1992 if (gfc_match_char (')') != MATCH_YES)
1994 /* This should never happen as we peek above. */
1995 gcc_unreachable ();
1998 if (gfc_match_eos () != MATCH_YES)
2000 gfc_error ("Junk after ASSOCIATE statement at %C");
2001 goto error;
2004 return MATCH_YES;
2006 error:
2007 gfc_free_association_list (new_st.ext.block.assoc);
2008 return MATCH_ERROR;
2012 /* Match a Fortran 2003 derived-type-spec (F03:R455), which is just the name of
2013 an accessible derived type. */
2015 static match
2016 match_derived_type_spec (gfc_typespec *ts)
2018 char name[GFC_MAX_SYMBOL_LEN + 1];
2019 locus old_locus;
2020 gfc_symbol *derived, *der_type;
2021 match m = MATCH_YES;
2022 gfc_actual_arglist *decl_type_param_list = NULL;
2023 bool is_pdt_template = false;
2025 old_locus = gfc_current_locus;
2027 if (gfc_match ("%n", name) != MATCH_YES)
2029 gfc_current_locus = old_locus;
2030 return MATCH_NO;
2033 gfc_find_symbol (name, NULL, 1, &derived);
2035 /* Match the PDT spec list, if there. */
2036 if (derived && derived->attr.flavor == FL_PROCEDURE)
2038 gfc_find_symbol (gfc_dt_upper_string (name), NULL, 1, &der_type);
2039 is_pdt_template = der_type
2040 && der_type->attr.flavor == FL_DERIVED
2041 && der_type->attr.pdt_template;
2044 if (is_pdt_template)
2045 m = gfc_match_actual_arglist (1, &decl_type_param_list, true);
2047 if (m == MATCH_ERROR)
2049 gfc_free_actual_arglist (decl_type_param_list);
2050 return m;
2053 if (derived && derived->attr.flavor == FL_PROCEDURE && derived->attr.generic)
2054 derived = gfc_find_dt_in_generic (derived);
2056 /* If this is a PDT, find the specific instance. */
2057 if (m == MATCH_YES && is_pdt_template)
2059 gfc_namespace *old_ns;
2061 old_ns = gfc_current_ns;
2062 while (gfc_current_ns && gfc_current_ns->parent)
2063 gfc_current_ns = gfc_current_ns->parent;
2065 if (type_param_spec_list)
2066 gfc_free_actual_arglist (type_param_spec_list);
2067 m = gfc_get_pdt_instance (decl_type_param_list, &der_type,
2068 &type_param_spec_list);
2069 gfc_free_actual_arglist (decl_type_param_list);
2071 if (m != MATCH_YES)
2072 return m;
2073 derived = der_type;
2074 gcc_assert (!derived->attr.pdt_template && derived->attr.pdt_type);
2075 gfc_set_sym_referenced (derived);
2077 gfc_current_ns = old_ns;
2080 if (derived && derived->attr.flavor == FL_DERIVED)
2082 ts->type = BT_DERIVED;
2083 ts->u.derived = derived;
2084 return MATCH_YES;
2087 gfc_current_locus = old_locus;
2088 return MATCH_NO;
2092 /* Match a Fortran 2003 type-spec (F03:R401). This is similar to
2093 gfc_match_decl_type_spec() from decl.c, with the following exceptions:
2094 It only includes the intrinsic types from the Fortran 2003 standard
2095 (thus, neither BYTE nor forms like REAL*4 are allowed). Additionally,
2096 the implicit_flag is not needed, so it was removed. Derived types are
2097 identified by their name alone. */
2099 match
2100 gfc_match_type_spec (gfc_typespec *ts)
2102 match m;
2103 locus old_locus;
2104 char c, name[GFC_MAX_SYMBOL_LEN + 1];
2106 gfc_clear_ts (ts);
2107 gfc_gobble_whitespace ();
2108 old_locus = gfc_current_locus;
2110 /* If c isn't [a-z], then return immediately. */
2111 c = gfc_peek_ascii_char ();
2112 if (!ISALPHA(c))
2113 return MATCH_NO;
2115 type_param_spec_list = NULL;
2117 if (match_derived_type_spec (ts) == MATCH_YES)
2119 /* Enforce F03:C401. */
2120 if (ts->u.derived->attr.abstract)
2122 gfc_error ("Derived type %qs at %L may not be ABSTRACT",
2123 ts->u.derived->name, &old_locus);
2124 return MATCH_ERROR;
2126 return MATCH_YES;
2129 if (gfc_match ("integer") == MATCH_YES)
2131 ts->type = BT_INTEGER;
2132 ts->kind = gfc_default_integer_kind;
2133 goto kind_selector;
2136 if (gfc_match ("double precision") == MATCH_YES)
2138 ts->type = BT_REAL;
2139 ts->kind = gfc_default_double_kind;
2140 return MATCH_YES;
2143 if (gfc_match ("complex") == MATCH_YES)
2145 ts->type = BT_COMPLEX;
2146 ts->kind = gfc_default_complex_kind;
2147 goto kind_selector;
2150 if (gfc_match ("character") == MATCH_YES)
2152 ts->type = BT_CHARACTER;
2154 m = gfc_match_char_spec (ts);
2156 if (m == MATCH_NO)
2157 m = MATCH_YES;
2159 return m;
2162 /* REAL is a real pain because it can be a type, intrinsic subprogram,
2163 or list item in a type-list of an OpenMP reduction clause. Need to
2164 differentiate REAL([KIND]=scalar-int-initialization-expr) from
2165 REAL(A,[KIND]) and REAL(KIND,A). Logically, when this code was
2166 written the use of LOGICAL as a type-spec or intrinsic subprogram
2167 was overlooked. */
2169 m = gfc_match (" %n", name);
2170 if (m == MATCH_YES
2171 && (strcmp (name, "real") == 0 || strcmp (name, "logical") == 0))
2173 char c;
2174 gfc_expr *e;
2175 locus where;
2177 if (*name == 'r')
2179 ts->type = BT_REAL;
2180 ts->kind = gfc_default_real_kind;
2182 else
2184 ts->type = BT_LOGICAL;
2185 ts->kind = gfc_default_logical_kind;
2188 gfc_gobble_whitespace ();
2190 /* Prevent REAL*4, etc. */
2191 c = gfc_peek_ascii_char ();
2192 if (c == '*')
2194 gfc_error ("Invalid type-spec at %C");
2195 return MATCH_ERROR;
2198 /* Found leading colon in REAL::, a trailing ')' in for example
2199 TYPE IS (REAL), or REAL, for an OpenMP list-item. */
2200 if (c == ':' || c == ')' || (flag_openmp && c == ','))
2201 return MATCH_YES;
2203 /* Found something other than the opening '(' in REAL(... */
2204 if (c != '(')
2205 return MATCH_NO;
2206 else
2207 gfc_next_char (); /* Burn the '('. */
2209 /* Look for the optional KIND=. */
2210 where = gfc_current_locus;
2211 m = gfc_match ("%n", name);
2212 if (m == MATCH_YES)
2214 gfc_gobble_whitespace ();
2215 c = gfc_next_char ();
2216 if (c == '=')
2218 if (strcmp(name, "a") == 0 || strcmp(name, "l") == 0)
2219 return MATCH_NO;
2220 else if (strcmp(name, "kind") == 0)
2221 goto found;
2222 else
2223 return MATCH_ERROR;
2225 else
2226 gfc_current_locus = where;
2228 else
2229 gfc_current_locus = where;
2231 found:
2233 m = gfc_match_init_expr (&e);
2234 if (m == MATCH_NO || m == MATCH_ERROR)
2235 return MATCH_NO;
2237 /* If a comma appears, it is an intrinsic subprogram. */
2238 gfc_gobble_whitespace ();
2239 c = gfc_peek_ascii_char ();
2240 if (c == ',')
2242 gfc_free_expr (e);
2243 return MATCH_NO;
2246 /* If ')' appears, we have REAL(initialization-expr), here check for
2247 a scalar integer initialization-expr and valid kind parameter. */
2248 if (c == ')')
2250 if (e->ts.type != BT_INTEGER || e->rank > 0)
2252 gfc_free_expr (e);
2253 return MATCH_NO;
2256 if (e->expr_type != EXPR_CONSTANT)
2257 goto ohno;
2259 gfc_next_char (); /* Burn the ')'. */
2260 ts->kind = (int) mpz_get_si (e->value.integer);
2261 if (gfc_validate_kind (ts->type, ts->kind , true) == -1)
2263 gfc_error ("Invalid type-spec at %C");
2264 return MATCH_ERROR;
2267 gfc_free_expr (e);
2269 return MATCH_YES;
2273 ohno:
2275 /* If a type is not matched, simply return MATCH_NO. */
2276 gfc_current_locus = old_locus;
2277 return MATCH_NO;
2279 kind_selector:
2281 gfc_gobble_whitespace ();
2283 /* This prevents INTEGER*4, etc. */
2284 if (gfc_peek_ascii_char () == '*')
2286 gfc_error ("Invalid type-spec at %C");
2287 return MATCH_ERROR;
2290 m = gfc_match_kind_spec (ts, false);
2292 /* No kind specifier found. */
2293 if (m == MATCH_NO)
2294 m = MATCH_YES;
2296 return m;
2300 /******************** FORALL subroutines ********************/
2302 /* Free a list of FORALL iterators. */
2304 void
2305 gfc_free_forall_iterator (gfc_forall_iterator *iter)
2307 gfc_forall_iterator *next;
2309 while (iter)
2311 next = iter->next;
2312 gfc_free_expr (iter->var);
2313 gfc_free_expr (iter->start);
2314 gfc_free_expr (iter->end);
2315 gfc_free_expr (iter->stride);
2316 free (iter);
2317 iter = next;
2322 /* Match an iterator as part of a FORALL statement. The format is:
2324 <var> = <start>:<end>[:<stride>]
2326 On MATCH_NO, the caller tests for the possibility that there is a
2327 scalar mask expression. */
2329 static match
2330 match_forall_iterator (gfc_forall_iterator **result)
2332 gfc_forall_iterator *iter;
2333 locus where;
2334 match m;
2336 where = gfc_current_locus;
2337 iter = XCNEW (gfc_forall_iterator);
2339 m = gfc_match_expr (&iter->var);
2340 if (m != MATCH_YES)
2341 goto cleanup;
2343 if (gfc_match_char ('=') != MATCH_YES
2344 || iter->var->expr_type != EXPR_VARIABLE)
2346 m = MATCH_NO;
2347 goto cleanup;
2350 m = gfc_match_expr (&iter->start);
2351 if (m != MATCH_YES)
2352 goto cleanup;
2354 if (gfc_match_char (':') != MATCH_YES)
2355 goto syntax;
2357 m = gfc_match_expr (&iter->end);
2358 if (m == MATCH_NO)
2359 goto syntax;
2360 if (m == MATCH_ERROR)
2361 goto cleanup;
2363 if (gfc_match_char (':') == MATCH_NO)
2364 iter->stride = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2365 else
2367 m = gfc_match_expr (&iter->stride);
2368 if (m == MATCH_NO)
2369 goto syntax;
2370 if (m == MATCH_ERROR)
2371 goto cleanup;
2374 /* Mark the iteration variable's symbol as used as a FORALL index. */
2375 iter->var->symtree->n.sym->forall_index = true;
2377 *result = iter;
2378 return MATCH_YES;
2380 syntax:
2381 gfc_error ("Syntax error in FORALL iterator at %C");
2382 m = MATCH_ERROR;
2384 cleanup:
2386 gfc_current_locus = where;
2387 gfc_free_forall_iterator (iter);
2388 return m;
2392 /* Match the header of a FORALL statement. */
2394 static match
2395 match_forall_header (gfc_forall_iterator **phead, gfc_expr **mask)
2397 gfc_forall_iterator *head, *tail, *new_iter;
2398 gfc_expr *msk;
2399 match m;
2401 gfc_gobble_whitespace ();
2403 head = tail = NULL;
2404 msk = NULL;
2406 if (gfc_match_char ('(') != MATCH_YES)
2407 return MATCH_NO;
2409 m = match_forall_iterator (&new_iter);
2410 if (m == MATCH_ERROR)
2411 goto cleanup;
2412 if (m == MATCH_NO)
2413 goto syntax;
2415 head = tail = new_iter;
2417 for (;;)
2419 if (gfc_match_char (',') != MATCH_YES)
2420 break;
2422 m = match_forall_iterator (&new_iter);
2423 if (m == MATCH_ERROR)
2424 goto cleanup;
2426 if (m == MATCH_YES)
2428 tail->next = new_iter;
2429 tail = new_iter;
2430 continue;
2433 /* Have to have a mask expression. */
2435 m = gfc_match_expr (&msk);
2436 if (m == MATCH_NO)
2437 goto syntax;
2438 if (m == MATCH_ERROR)
2439 goto cleanup;
2441 break;
2444 if (gfc_match_char (')') == MATCH_NO)
2445 goto syntax;
2447 *phead = head;
2448 *mask = msk;
2449 return MATCH_YES;
2451 syntax:
2452 gfc_syntax_error (ST_FORALL);
2454 cleanup:
2455 gfc_free_expr (msk);
2456 gfc_free_forall_iterator (head);
2458 return MATCH_ERROR;
2461 /* Match the rest of a simple FORALL statement that follows an
2462 IF statement. */
2464 static match
2465 match_simple_forall (void)
2467 gfc_forall_iterator *head;
2468 gfc_expr *mask;
2469 gfc_code *c;
2470 match m;
2472 mask = NULL;
2473 head = NULL;
2474 c = NULL;
2476 m = match_forall_header (&head, &mask);
2478 if (m == MATCH_NO)
2479 goto syntax;
2480 if (m != MATCH_YES)
2481 goto cleanup;
2483 m = gfc_match_assignment ();
2485 if (m == MATCH_ERROR)
2486 goto cleanup;
2487 if (m == MATCH_NO)
2489 m = gfc_match_pointer_assignment ();
2490 if (m == MATCH_ERROR)
2491 goto cleanup;
2492 if (m == MATCH_NO)
2493 goto syntax;
2496 c = XCNEW (gfc_code);
2497 *c = new_st;
2498 c->loc = gfc_current_locus;
2500 if (gfc_match_eos () != MATCH_YES)
2501 goto syntax;
2503 gfc_clear_new_st ();
2504 new_st.op = EXEC_FORALL;
2505 new_st.expr1 = mask;
2506 new_st.ext.forall_iterator = head;
2507 new_st.block = gfc_get_code (EXEC_FORALL);
2508 new_st.block->next = c;
2510 return MATCH_YES;
2512 syntax:
2513 gfc_syntax_error (ST_FORALL);
2515 cleanup:
2516 gfc_free_forall_iterator (head);
2517 gfc_free_expr (mask);
2519 return MATCH_ERROR;
2523 /* Match a FORALL statement. */
2525 match
2526 gfc_match_forall (gfc_statement *st)
2528 gfc_forall_iterator *head;
2529 gfc_expr *mask;
2530 gfc_code *c;
2531 match m0, m;
2533 head = NULL;
2534 mask = NULL;
2535 c = NULL;
2537 m0 = gfc_match_label ();
2538 if (m0 == MATCH_ERROR)
2539 return MATCH_ERROR;
2541 m = gfc_match (" forall");
2542 if (m != MATCH_YES)
2543 return m;
2545 m = match_forall_header (&head, &mask);
2546 if (m == MATCH_ERROR)
2547 goto cleanup;
2548 if (m == MATCH_NO)
2549 goto syntax;
2551 if (gfc_match_eos () == MATCH_YES)
2553 *st = ST_FORALL_BLOCK;
2554 new_st.op = EXEC_FORALL;
2555 new_st.expr1 = mask;
2556 new_st.ext.forall_iterator = head;
2557 return MATCH_YES;
2560 m = gfc_match_assignment ();
2561 if (m == MATCH_ERROR)
2562 goto cleanup;
2563 if (m == MATCH_NO)
2565 m = gfc_match_pointer_assignment ();
2566 if (m == MATCH_ERROR)
2567 goto cleanup;
2568 if (m == MATCH_NO)
2569 goto syntax;
2572 c = XCNEW (gfc_code);
2573 *c = new_st;
2574 c->loc = gfc_current_locus;
2576 gfc_clear_new_st ();
2577 new_st.op = EXEC_FORALL;
2578 new_st.expr1 = mask;
2579 new_st.ext.forall_iterator = head;
2580 new_st.block = gfc_get_code (EXEC_FORALL);
2581 new_st.block->next = c;
2583 *st = ST_FORALL;
2584 return MATCH_YES;
2586 syntax:
2587 gfc_syntax_error (ST_FORALL);
2589 cleanup:
2590 gfc_free_forall_iterator (head);
2591 gfc_free_expr (mask);
2592 gfc_free_statements (c);
2593 return MATCH_NO;
2597 /* Match a DO statement. */
2599 match
2600 gfc_match_do (void)
2602 gfc_iterator iter, *ip;
2603 locus old_loc;
2604 gfc_st_label *label;
2605 match m;
2607 old_loc = gfc_current_locus;
2609 memset (&iter, '\0', sizeof (gfc_iterator));
2610 label = NULL;
2612 m = gfc_match_label ();
2613 if (m == MATCH_ERROR)
2614 return m;
2616 if (gfc_match (" do") != MATCH_YES)
2617 return MATCH_NO;
2619 m = gfc_match_st_label (&label);
2620 if (m == MATCH_ERROR)
2621 goto cleanup;
2623 /* Match an infinite DO, make it like a DO WHILE(.TRUE.). */
2625 if (gfc_match_eos () == MATCH_YES)
2627 iter.end = gfc_get_logical_expr (gfc_default_logical_kind, NULL, true);
2628 new_st.op = EXEC_DO_WHILE;
2629 goto done;
2632 /* Match an optional comma, if no comma is found, a space is obligatory. */
2633 if (gfc_match_char (',') != MATCH_YES && gfc_match ("% ") != MATCH_YES)
2634 return MATCH_NO;
2636 /* Check for balanced parens. */
2638 if (gfc_match_parens () == MATCH_ERROR)
2639 return MATCH_ERROR;
2641 if (gfc_match (" concurrent") == MATCH_YES)
2643 gfc_forall_iterator *head;
2644 gfc_expr *mask;
2646 if (!gfc_notify_std (GFC_STD_F2008, "DO CONCURRENT construct at %C"))
2647 return MATCH_ERROR;
2650 mask = NULL;
2651 head = NULL;
2652 m = match_forall_header (&head, &mask);
2654 if (m == MATCH_NO)
2655 return m;
2656 if (m == MATCH_ERROR)
2657 goto concurr_cleanup;
2659 if (gfc_match_eos () != MATCH_YES)
2660 goto concurr_cleanup;
2662 if (label != NULL
2663 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2664 goto concurr_cleanup;
2666 new_st.label1 = label;
2667 new_st.op = EXEC_DO_CONCURRENT;
2668 new_st.expr1 = mask;
2669 new_st.ext.forall_iterator = head;
2671 return MATCH_YES;
2673 concurr_cleanup:
2674 gfc_syntax_error (ST_DO);
2675 gfc_free_expr (mask);
2676 gfc_free_forall_iterator (head);
2677 return MATCH_ERROR;
2680 /* See if we have a DO WHILE. */
2681 if (gfc_match (" while ( %e )%t", &iter.end) == MATCH_YES)
2683 new_st.op = EXEC_DO_WHILE;
2684 goto done;
2687 /* The abortive DO WHILE may have done something to the symbol
2688 table, so we start over. */
2689 gfc_undo_symbols ();
2690 gfc_current_locus = old_loc;
2692 gfc_match_label (); /* This won't error. */
2693 gfc_match (" do "); /* This will work. */
2695 gfc_match_st_label (&label); /* Can't error out. */
2696 gfc_match_char (','); /* Optional comma. */
2698 m = gfc_match_iterator (&iter, 0);
2699 if (m == MATCH_NO)
2700 return MATCH_NO;
2701 if (m == MATCH_ERROR)
2702 goto cleanup;
2704 iter.var->symtree->n.sym->attr.implied_index = 0;
2705 gfc_check_do_variable (iter.var->symtree);
2707 if (gfc_match_eos () != MATCH_YES)
2709 gfc_syntax_error (ST_DO);
2710 goto cleanup;
2713 new_st.op = EXEC_DO;
2715 done:
2716 if (label != NULL
2717 && !gfc_reference_st_label (label, ST_LABEL_DO_TARGET))
2718 goto cleanup;
2720 new_st.label1 = label;
2722 if (new_st.op == EXEC_DO_WHILE)
2723 new_st.expr1 = iter.end;
2724 else
2726 new_st.ext.iterator = ip = gfc_get_iterator ();
2727 *ip = iter;
2730 return MATCH_YES;
2732 cleanup:
2733 gfc_free_iterator (&iter, 0);
2735 return MATCH_ERROR;
2739 /* Match an EXIT or CYCLE statement. */
2741 static match
2742 match_exit_cycle (gfc_statement st, gfc_exec_op op)
2744 gfc_state_data *p, *o;
2745 gfc_symbol *sym;
2746 match m;
2747 int cnt;
2749 if (gfc_match_eos () == MATCH_YES)
2750 sym = NULL;
2751 else
2753 char name[GFC_MAX_SYMBOL_LEN + 1];
2754 gfc_symtree* stree;
2756 m = gfc_match ("% %n%t", name);
2757 if (m == MATCH_ERROR)
2758 return MATCH_ERROR;
2759 if (m == MATCH_NO)
2761 gfc_syntax_error (st);
2762 return MATCH_ERROR;
2765 /* Find the corresponding symbol. If there's a BLOCK statement
2766 between here and the label, it is not in gfc_current_ns but a parent
2767 namespace! */
2768 stree = gfc_find_symtree_in_proc (name, gfc_current_ns);
2769 if (!stree)
2771 gfc_error ("Name %qs in %s statement at %C is unknown",
2772 name, gfc_ascii_statement (st));
2773 return MATCH_ERROR;
2776 sym = stree->n.sym;
2777 if (sym->attr.flavor != FL_LABEL)
2779 gfc_error ("Name %qs in %s statement at %C is not a construct name",
2780 name, gfc_ascii_statement (st));
2781 return MATCH_ERROR;
2785 /* Find the loop specified by the label (or lack of a label). */
2786 for (o = NULL, p = gfc_state_stack; p; p = p->previous)
2787 if (o == NULL && p->state == COMP_OMP_STRUCTURED_BLOCK)
2788 o = p;
2789 else if (p->state == COMP_CRITICAL)
2791 gfc_error("%s statement at %C leaves CRITICAL construct",
2792 gfc_ascii_statement (st));
2793 return MATCH_ERROR;
2795 else if (p->state == COMP_DO_CONCURRENT
2796 && (op == EXEC_EXIT || (sym && sym != p->sym)))
2798 /* F2008, C821 & C845. */
2799 gfc_error("%s statement at %C leaves DO CONCURRENT construct",
2800 gfc_ascii_statement (st));
2801 return MATCH_ERROR;
2803 else if ((sym && sym == p->sym)
2804 || (!sym && (p->state == COMP_DO
2805 || p->state == COMP_DO_CONCURRENT)))
2806 break;
2808 if (p == NULL)
2810 if (sym == NULL)
2811 gfc_error ("%s statement at %C is not within a construct",
2812 gfc_ascii_statement (st));
2813 else
2814 gfc_error ("%s statement at %C is not within construct %qs",
2815 gfc_ascii_statement (st), sym->name);
2817 return MATCH_ERROR;
2820 /* Special checks for EXIT from non-loop constructs. */
2821 switch (p->state)
2823 case COMP_DO:
2824 case COMP_DO_CONCURRENT:
2825 break;
2827 case COMP_CRITICAL:
2828 /* This is already handled above. */
2829 gcc_unreachable ();
2831 case COMP_ASSOCIATE:
2832 case COMP_BLOCK:
2833 case COMP_IF:
2834 case COMP_SELECT:
2835 case COMP_SELECT_TYPE:
2836 case COMP_SELECT_RANK:
2837 gcc_assert (sym);
2838 if (op == EXEC_CYCLE)
2840 gfc_error ("CYCLE statement at %C is not applicable to non-loop"
2841 " construct %qs", sym->name);
2842 return MATCH_ERROR;
2844 gcc_assert (op == EXEC_EXIT);
2845 if (!gfc_notify_std (GFC_STD_F2008, "EXIT statement with no"
2846 " do-construct-name at %C"))
2847 return MATCH_ERROR;
2848 break;
2850 default:
2851 gfc_error ("%s statement at %C is not applicable to construct %qs",
2852 gfc_ascii_statement (st), sym->name);
2853 return MATCH_ERROR;
2856 if (o != NULL)
2858 gfc_error (is_oacc (p)
2859 ? G_("%s statement at %C leaving OpenACC structured block")
2860 : G_("%s statement at %C leaving OpenMP structured block"),
2861 gfc_ascii_statement (st));
2862 return MATCH_ERROR;
2865 for (o = p, cnt = 0; o->state == COMP_DO && o->previous != NULL; cnt++)
2866 o = o->previous;
2867 if (cnt > 0
2868 && o != NULL
2869 && o->state == COMP_OMP_STRUCTURED_BLOCK
2870 && (o->head->op == EXEC_OACC_LOOP
2871 || o->head->op == EXEC_OACC_PARALLEL_LOOP
2872 || o->head->op == EXEC_OACC_SERIAL_LOOP))
2874 int collapse = 1;
2875 gcc_assert (o->head->next != NULL
2876 && (o->head->next->op == EXEC_DO
2877 || o->head->next->op == EXEC_DO_WHILE)
2878 && o->previous != NULL
2879 && o->previous->tail->op == o->head->op);
2880 if (o->previous->tail->ext.omp_clauses != NULL
2881 && o->previous->tail->ext.omp_clauses->collapse > 1)
2882 collapse = o->previous->tail->ext.omp_clauses->collapse;
2883 if (st == ST_EXIT && cnt <= collapse)
2885 gfc_error ("EXIT statement at %C terminating !$ACC LOOP loop");
2886 return MATCH_ERROR;
2888 if (st == ST_CYCLE && cnt < collapse)
2890 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2891 " !$ACC LOOP loop");
2892 return MATCH_ERROR;
2895 if (cnt > 0
2896 && o != NULL
2897 && (o->state == COMP_OMP_STRUCTURED_BLOCK)
2898 && (o->head->op == EXEC_OMP_DO
2899 || o->head->op == EXEC_OMP_PARALLEL_DO
2900 || o->head->op == EXEC_OMP_SIMD
2901 || o->head->op == EXEC_OMP_DO_SIMD
2902 || o->head->op == EXEC_OMP_PARALLEL_DO_SIMD))
2904 int count = 1;
2905 gcc_assert (o->head->next != NULL
2906 && (o->head->next->op == EXEC_DO
2907 || o->head->next->op == EXEC_DO_WHILE)
2908 && o->previous != NULL
2909 && o->previous->tail->op == o->head->op);
2910 if (o->previous->tail->ext.omp_clauses != NULL)
2912 if (o->previous->tail->ext.omp_clauses->collapse > 1)
2913 count = o->previous->tail->ext.omp_clauses->collapse;
2914 if (o->previous->tail->ext.omp_clauses->orderedc)
2915 count = o->previous->tail->ext.omp_clauses->orderedc;
2917 if (st == ST_EXIT && cnt <= count)
2919 gfc_error ("EXIT statement at %C terminating !$OMP DO loop");
2920 return MATCH_ERROR;
2922 if (st == ST_CYCLE && cnt < count)
2924 gfc_error ("CYCLE statement at %C to non-innermost collapsed"
2925 " !$OMP DO loop");
2926 return MATCH_ERROR;
2930 /* Save the first statement in the construct - needed by the backend. */
2931 new_st.ext.which_construct = p->construct;
2933 new_st.op = op;
2935 return MATCH_YES;
2939 /* Match the EXIT statement. */
2941 match
2942 gfc_match_exit (void)
2944 return match_exit_cycle (ST_EXIT, EXEC_EXIT);
2948 /* Match the CYCLE statement. */
2950 match
2951 gfc_match_cycle (void)
2953 return match_exit_cycle (ST_CYCLE, EXEC_CYCLE);
2957 /* Match a stop-code after an (ERROR) STOP or PAUSE statement. The
2958 requirements for a stop-code differ in the standards.
2960 Fortran 95 has
2962 R840 stop-stmt is STOP [ stop-code ]
2963 R841 stop-code is scalar-char-constant
2964 or digit [ digit [ digit [ digit [ digit ] ] ] ]
2966 Fortran 2003 matches Fortran 95 except R840 and R841 are now R849 and R850.
2967 Fortran 2008 has
2969 R855 stop-stmt is STOP [ stop-code ]
2970 R856 allstop-stmt is ALL STOP [ stop-code ]
2971 R857 stop-code is scalar-default-char-constant-expr
2972 or scalar-int-constant-expr
2974 For free-form source code, all standards contain a statement of the form:
2976 A blank shall be used to separate names, constants, or labels from
2977 adjacent keywords, names, constants, or labels.
2979 A stop-code is not a name, constant, or label. So, under Fortran 95 and 2003,
2981 STOP123
2983 is valid, but it is invalid Fortran 2008. */
2985 static match
2986 gfc_match_stopcode (gfc_statement st)
2988 gfc_expr *e = NULL;
2989 match m;
2990 bool f95, f03, f08;
2992 /* Set f95 for -std=f95. */
2993 f95 = (gfc_option.allow_std == GFC_STD_OPT_F95);
2995 /* Set f03 for -std=f2003. */
2996 f03 = (gfc_option.allow_std == GFC_STD_OPT_F03);
2998 /* Set f08 for -std=f2008. */
2999 f08 = (gfc_option.allow_std == GFC_STD_OPT_F08);
3001 /* Look for a blank between STOP and the stop-code for F2008 or later. */
3002 if (gfc_current_form != FORM_FIXED && !(f95 || f03))
3004 char c = gfc_peek_ascii_char ();
3006 /* Look for end-of-statement. There is no stop-code. */
3007 if (c == '\n' || c == '!' || c == ';')
3008 goto done;
3010 if (c != ' ')
3012 gfc_error ("Blank required in %s statement near %C",
3013 gfc_ascii_statement (st));
3014 return MATCH_ERROR;
3018 if (gfc_match_eos () != MATCH_YES)
3020 int stopcode;
3021 locus old_locus;
3023 /* First look for the F95 or F2003 digit [...] construct. */
3024 old_locus = gfc_current_locus;
3025 m = gfc_match_small_int (&stopcode);
3026 if (m == MATCH_YES && (f95 || f03))
3028 if (stopcode < 0)
3030 gfc_error ("STOP code at %C cannot be negative");
3031 return MATCH_ERROR;
3034 if (stopcode > 99999)
3036 gfc_error ("STOP code at %C contains too many digits");
3037 return MATCH_ERROR;
3041 /* Reset the locus and now load gfc_expr. */
3042 gfc_current_locus = old_locus;
3043 m = gfc_match_expr (&e);
3044 if (m == MATCH_ERROR)
3045 goto cleanup;
3046 if (m == MATCH_NO)
3047 goto syntax;
3049 if (gfc_match_eos () != MATCH_YES)
3050 goto syntax;
3053 if (gfc_pure (NULL))
3055 if (st == ST_ERROR_STOP)
3057 if (!gfc_notify_std (GFC_STD_F2018, "%s statement at %C in PURE "
3058 "procedure", gfc_ascii_statement (st)))
3059 goto cleanup;
3061 else
3063 gfc_error ("%s statement not allowed in PURE procedure at %C",
3064 gfc_ascii_statement (st));
3065 goto cleanup;
3069 gfc_unset_implicit_pure (NULL);
3071 if (st == ST_STOP && gfc_find_state (COMP_CRITICAL))
3073 gfc_error ("Image control statement STOP at %C in CRITICAL block");
3074 goto cleanup;
3076 if (st == ST_STOP && gfc_find_state (COMP_DO_CONCURRENT))
3078 gfc_error ("Image control statement STOP at %C in DO CONCURRENT block");
3079 goto cleanup;
3082 if (e != NULL)
3084 if (!gfc_simplify_expr (e, 0))
3085 goto cleanup;
3087 /* Test for F95 and F2003 style STOP stop-code. */
3088 if (e->expr_type != EXPR_CONSTANT && (f95 || f03))
3090 gfc_error ("STOP code at %L must be a scalar CHARACTER constant "
3091 "or digit[digit[digit[digit[digit]]]]", &e->where);
3092 goto cleanup;
3095 /* Use the machinery for an initialization expression to reduce the
3096 stop-code to a constant. */
3097 gfc_reduce_init_expr (e);
3099 /* Test for F2008 style STOP stop-code. */
3100 if (e->expr_type != EXPR_CONSTANT && f08)
3102 gfc_error ("STOP code at %L must be a scalar default CHARACTER or "
3103 "INTEGER constant expression", &e->where);
3104 goto cleanup;
3107 if (!(e->ts.type == BT_CHARACTER || e->ts.type == BT_INTEGER))
3109 gfc_error ("STOP code at %L must be either INTEGER or CHARACTER type",
3110 &e->where);
3111 goto cleanup;
3114 if (e->rank != 0)
3116 gfc_error ("STOP code at %L must be scalar", &e->where);
3117 goto cleanup;
3120 if (e->ts.type == BT_CHARACTER
3121 && e->ts.kind != gfc_default_character_kind)
3123 gfc_error ("STOP code at %L must be default character KIND=%d",
3124 &e->where, (int) gfc_default_character_kind);
3125 goto cleanup;
3128 if (e->ts.type == BT_INTEGER && e->ts.kind != gfc_default_integer_kind)
3130 gfc_error ("STOP code at %L must be default integer KIND=%d",
3131 &e->where, (int) gfc_default_integer_kind);
3132 goto cleanup;
3136 done:
3138 switch (st)
3140 case ST_STOP:
3141 new_st.op = EXEC_STOP;
3142 break;
3143 case ST_ERROR_STOP:
3144 new_st.op = EXEC_ERROR_STOP;
3145 break;
3146 case ST_PAUSE:
3147 new_st.op = EXEC_PAUSE;
3148 break;
3149 default:
3150 gcc_unreachable ();
3153 new_st.expr1 = e;
3154 new_st.ext.stop_code = -1;
3156 return MATCH_YES;
3158 syntax:
3159 gfc_syntax_error (st);
3161 cleanup:
3163 gfc_free_expr (e);
3164 return MATCH_ERROR;
3168 /* Match the (deprecated) PAUSE statement. */
3170 match
3171 gfc_match_pause (void)
3173 match m;
3175 m = gfc_match_stopcode (ST_PAUSE);
3176 if (m == MATCH_YES)
3178 if (!gfc_notify_std (GFC_STD_F95_DEL, "PAUSE statement at %C"))
3179 m = MATCH_ERROR;
3181 return m;
3185 /* Match the STOP statement. */
3187 match
3188 gfc_match_stop (void)
3190 return gfc_match_stopcode (ST_STOP);
3194 /* Match the ERROR STOP statement. */
3196 match
3197 gfc_match_error_stop (void)
3199 if (!gfc_notify_std (GFC_STD_F2008, "ERROR STOP statement at %C"))
3200 return MATCH_ERROR;
3202 return gfc_match_stopcode (ST_ERROR_STOP);
3205 /* Match EVENT POST/WAIT statement. Syntax:
3206 EVENT POST ( event-variable [, sync-stat-list] )
3207 EVENT WAIT ( event-variable [, wait-spec-list] )
3208 with
3209 wait-spec-list is sync-stat-list or until-spec
3210 until-spec is UNTIL_COUNT = scalar-int-expr
3211 sync-stat is STAT= or ERRMSG=. */
3213 static match
3214 event_statement (gfc_statement st)
3216 match m;
3217 gfc_expr *tmp, *eventvar, *until_count, *stat, *errmsg;
3218 bool saw_until_count, saw_stat, saw_errmsg;
3220 tmp = eventvar = until_count = stat = errmsg = NULL;
3221 saw_until_count = saw_stat = saw_errmsg = false;
3223 if (gfc_pure (NULL))
3225 gfc_error ("Image control statement EVENT %s at %C in PURE procedure",
3226 st == ST_EVENT_POST ? "POST" : "WAIT");
3227 return MATCH_ERROR;
3230 gfc_unset_implicit_pure (NULL);
3232 if (flag_coarray == GFC_FCOARRAY_NONE)
3234 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3235 return MATCH_ERROR;
3238 if (gfc_find_state (COMP_CRITICAL))
3240 gfc_error ("Image control statement EVENT %s at %C in CRITICAL block",
3241 st == ST_EVENT_POST ? "POST" : "WAIT");
3242 return MATCH_ERROR;
3245 if (gfc_find_state (COMP_DO_CONCURRENT))
3247 gfc_error ("Image control statement EVENT %s at %C in DO CONCURRENT "
3248 "block", st == ST_EVENT_POST ? "POST" : "WAIT");
3249 return MATCH_ERROR;
3252 if (gfc_match_char ('(') != MATCH_YES)
3253 goto syntax;
3255 if (gfc_match ("%e", &eventvar) != MATCH_YES)
3256 goto syntax;
3257 m = gfc_match_char (',');
3258 if (m == MATCH_ERROR)
3259 goto syntax;
3260 if (m == MATCH_NO)
3262 m = gfc_match_char (')');
3263 if (m == MATCH_YES)
3264 goto done;
3265 goto syntax;
3268 for (;;)
3270 m = gfc_match (" stat = %v", &tmp);
3271 if (m == MATCH_ERROR)
3272 goto syntax;
3273 if (m == MATCH_YES)
3275 if (saw_stat)
3277 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
3278 goto cleanup;
3280 stat = tmp;
3281 saw_stat = true;
3283 m = gfc_match_char (',');
3284 if (m == MATCH_YES)
3285 continue;
3287 tmp = NULL;
3288 break;
3291 m = gfc_match (" errmsg = %v", &tmp);
3292 if (m == MATCH_ERROR)
3293 goto syntax;
3294 if (m == MATCH_YES)
3296 if (saw_errmsg)
3298 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
3299 goto cleanup;
3301 errmsg = tmp;
3302 saw_errmsg = true;
3304 m = gfc_match_char (',');
3305 if (m == MATCH_YES)
3306 continue;
3308 tmp = NULL;
3309 break;
3312 m = gfc_match (" until_count = %e", &tmp);
3313 if (m == MATCH_ERROR || st == ST_EVENT_POST)
3314 goto syntax;
3315 if (m == MATCH_YES)
3317 if (saw_until_count)
3319 gfc_error ("Redundant UNTIL_COUNT tag found at %L",
3320 &tmp->where);
3321 goto cleanup;
3323 until_count = tmp;
3324 saw_until_count = true;
3326 m = gfc_match_char (',');
3327 if (m == MATCH_YES)
3328 continue;
3330 tmp = NULL;
3331 break;
3334 break;
3337 if (m == MATCH_ERROR)
3338 goto syntax;
3340 if (gfc_match (" )%t") != MATCH_YES)
3341 goto syntax;
3343 done:
3344 switch (st)
3346 case ST_EVENT_POST:
3347 new_st.op = EXEC_EVENT_POST;
3348 break;
3349 case ST_EVENT_WAIT:
3350 new_st.op = EXEC_EVENT_WAIT;
3351 break;
3352 default:
3353 gcc_unreachable ();
3356 new_st.expr1 = eventvar;
3357 new_st.expr2 = stat;
3358 new_st.expr3 = errmsg;
3359 new_st.expr4 = until_count;
3361 return MATCH_YES;
3363 syntax:
3364 gfc_syntax_error (st);
3366 cleanup:
3367 if (until_count != tmp)
3368 gfc_free_expr (until_count);
3369 if (errmsg != tmp)
3370 gfc_free_expr (errmsg);
3371 if (stat != tmp)
3372 gfc_free_expr (stat);
3374 gfc_free_expr (tmp);
3375 gfc_free_expr (eventvar);
3377 return MATCH_ERROR;
3382 match
3383 gfc_match_event_post (void)
3385 if (!gfc_notify_std (GFC_STD_F2018, "EVENT POST statement at %C"))
3386 return MATCH_ERROR;
3388 return event_statement (ST_EVENT_POST);
3392 match
3393 gfc_match_event_wait (void)
3395 if (!gfc_notify_std (GFC_STD_F2018, "EVENT WAIT statement at %C"))
3396 return MATCH_ERROR;
3398 return event_statement (ST_EVENT_WAIT);
3402 /* Match a FAIL IMAGE statement. */
3404 match
3405 gfc_match_fail_image (void)
3407 if (!gfc_notify_std (GFC_STD_F2018, "FAIL IMAGE statement at %C"))
3408 return MATCH_ERROR;
3410 if (gfc_match_char ('(') == MATCH_YES)
3411 goto syntax;
3413 new_st.op = EXEC_FAIL_IMAGE;
3415 return MATCH_YES;
3417 syntax:
3418 gfc_syntax_error (ST_FAIL_IMAGE);
3420 return MATCH_ERROR;
3423 /* Match a FORM TEAM statement. */
3425 match
3426 gfc_match_form_team (void)
3428 match m;
3429 gfc_expr *teamid,*team;
3431 if (!gfc_notify_std (GFC_STD_F2018, "FORM TEAM statement at %C"))
3432 return MATCH_ERROR;
3434 if (gfc_match_char ('(') == MATCH_NO)
3435 goto syntax;
3437 new_st.op = EXEC_FORM_TEAM;
3439 if (gfc_match ("%e", &teamid) != MATCH_YES)
3440 goto syntax;
3441 m = gfc_match_char (',');
3442 if (m == MATCH_ERROR)
3443 goto syntax;
3444 if (gfc_match ("%e", &team) != MATCH_YES)
3445 goto syntax;
3447 m = gfc_match_char (')');
3448 if (m == MATCH_NO)
3449 goto syntax;
3451 new_st.expr1 = teamid;
3452 new_st.expr2 = team;
3454 return MATCH_YES;
3456 syntax:
3457 gfc_syntax_error (ST_FORM_TEAM);
3459 return MATCH_ERROR;
3462 /* Match a CHANGE TEAM statement. */
3464 match
3465 gfc_match_change_team (void)
3467 match m;
3468 gfc_expr *team;
3470 if (!gfc_notify_std (GFC_STD_F2018, "CHANGE TEAM statement at %C"))
3471 return MATCH_ERROR;
3473 if (gfc_match_char ('(') == MATCH_NO)
3474 goto syntax;
3476 new_st.op = EXEC_CHANGE_TEAM;
3478 if (gfc_match ("%e", &team) != MATCH_YES)
3479 goto syntax;
3481 m = gfc_match_char (')');
3482 if (m == MATCH_NO)
3483 goto syntax;
3485 new_st.expr1 = team;
3487 return MATCH_YES;
3489 syntax:
3490 gfc_syntax_error (ST_CHANGE_TEAM);
3492 return MATCH_ERROR;
3495 /* Match a END TEAM statement. */
3497 match
3498 gfc_match_end_team (void)
3500 if (!gfc_notify_std (GFC_STD_F2018, "END TEAM statement at %C"))
3501 return MATCH_ERROR;
3503 if (gfc_match_char ('(') == MATCH_YES)
3504 goto syntax;
3506 new_st.op = EXEC_END_TEAM;
3508 return MATCH_YES;
3510 syntax:
3511 gfc_syntax_error (ST_END_TEAM);
3513 return MATCH_ERROR;
3516 /* Match a SYNC TEAM statement. */
3518 match
3519 gfc_match_sync_team (void)
3521 match m;
3522 gfc_expr *team;
3524 if (!gfc_notify_std (GFC_STD_F2018, "SYNC TEAM statement at %C"))
3525 return MATCH_ERROR;
3527 if (gfc_match_char ('(') == MATCH_NO)
3528 goto syntax;
3530 new_st.op = EXEC_SYNC_TEAM;
3532 if (gfc_match ("%e", &team) != MATCH_YES)
3533 goto syntax;
3535 m = gfc_match_char (')');
3536 if (m == MATCH_NO)
3537 goto syntax;
3539 new_st.expr1 = team;
3541 return MATCH_YES;
3543 syntax:
3544 gfc_syntax_error (ST_SYNC_TEAM);
3546 return MATCH_ERROR;
3549 /* Match LOCK/UNLOCK statement. Syntax:
3550 LOCK ( lock-variable [ , lock-stat-list ] )
3551 UNLOCK ( lock-variable [ , sync-stat-list ] )
3552 where lock-stat is ACQUIRED_LOCK or sync-stat
3553 and sync-stat is STAT= or ERRMSG=. */
3555 static match
3556 lock_unlock_statement (gfc_statement st)
3558 match m;
3559 gfc_expr *tmp, *lockvar, *acq_lock, *stat, *errmsg;
3560 bool saw_acq_lock, saw_stat, saw_errmsg;
3562 tmp = lockvar = acq_lock = stat = errmsg = NULL;
3563 saw_acq_lock = saw_stat = saw_errmsg = false;
3565 if (gfc_pure (NULL))
3567 gfc_error ("Image control statement %s at %C in PURE procedure",
3568 st == ST_LOCK ? "LOCK" : "UNLOCK");
3569 return MATCH_ERROR;
3572 gfc_unset_implicit_pure (NULL);
3574 if (flag_coarray == GFC_FCOARRAY_NONE)
3576 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable");
3577 return MATCH_ERROR;
3580 if (gfc_find_state (COMP_CRITICAL))
3582 gfc_error ("Image control statement %s at %C in CRITICAL block",
3583 st == ST_LOCK ? "LOCK" : "UNLOCK");
3584 return MATCH_ERROR;
3587 if (gfc_find_state (COMP_DO_CONCURRENT))
3589 gfc_error ("Image control statement %s at %C in DO CONCURRENT block",
3590 st == ST_LOCK ? "LOCK" : "UNLOCK");
3591 return MATCH_ERROR;
3594 if (gfc_match_char ('(') != MATCH_YES)
3595 goto syntax;
3597 if (gfc_match ("%e", &lockvar) != MATCH_YES)
3598 goto syntax;
3599 m = gfc_match_char (',');
3600 if (m == MATCH_ERROR)
3601 goto syntax;
3602 if (m == MATCH_NO)
3604 m = gfc_match_char (')');
3605 if (m == MATCH_YES)
3606 goto done;
3607 goto syntax;
3610 for (;;)
3612 m = gfc_match (" stat = %v", &tmp);
3613 if (m == MATCH_ERROR)
3614 goto syntax;
3615 if (m == MATCH_YES)
3617 if (saw_stat)
3619 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
3620 goto cleanup;
3622 stat = tmp;
3623 saw_stat = true;
3625 m = gfc_match_char (',');
3626 if (m == MATCH_YES)
3627 continue;
3629 tmp = NULL;
3630 break;
3633 m = gfc_match (" errmsg = %v", &tmp);
3634 if (m == MATCH_ERROR)
3635 goto syntax;
3636 if (m == MATCH_YES)
3638 if (saw_errmsg)
3640 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
3641 goto cleanup;
3643 errmsg = tmp;
3644 saw_errmsg = true;
3646 m = gfc_match_char (',');
3647 if (m == MATCH_YES)
3648 continue;
3650 tmp = NULL;
3651 break;
3654 m = gfc_match (" acquired_lock = %v", &tmp);
3655 if (m == MATCH_ERROR || st == ST_UNLOCK)
3656 goto syntax;
3657 if (m == MATCH_YES)
3659 if (saw_acq_lock)
3661 gfc_error ("Redundant ACQUIRED_LOCK tag found at %L",
3662 &tmp->where);
3663 goto cleanup;
3665 acq_lock = tmp;
3666 saw_acq_lock = true;
3668 m = gfc_match_char (',');
3669 if (m == MATCH_YES)
3670 continue;
3672 tmp = NULL;
3673 break;
3676 break;
3679 if (m == MATCH_ERROR)
3680 goto syntax;
3682 if (gfc_match (" )%t") != MATCH_YES)
3683 goto syntax;
3685 done:
3686 switch (st)
3688 case ST_LOCK:
3689 new_st.op = EXEC_LOCK;
3690 break;
3691 case ST_UNLOCK:
3692 new_st.op = EXEC_UNLOCK;
3693 break;
3694 default:
3695 gcc_unreachable ();
3698 new_st.expr1 = lockvar;
3699 new_st.expr2 = stat;
3700 new_st.expr3 = errmsg;
3701 new_st.expr4 = acq_lock;
3703 return MATCH_YES;
3705 syntax:
3706 gfc_syntax_error (st);
3708 cleanup:
3709 if (acq_lock != tmp)
3710 gfc_free_expr (acq_lock);
3711 if (errmsg != tmp)
3712 gfc_free_expr (errmsg);
3713 if (stat != tmp)
3714 gfc_free_expr (stat);
3716 gfc_free_expr (tmp);
3717 gfc_free_expr (lockvar);
3719 return MATCH_ERROR;
3723 match
3724 gfc_match_lock (void)
3726 if (!gfc_notify_std (GFC_STD_F2008, "LOCK statement at %C"))
3727 return MATCH_ERROR;
3729 return lock_unlock_statement (ST_LOCK);
3733 match
3734 gfc_match_unlock (void)
3736 if (!gfc_notify_std (GFC_STD_F2008, "UNLOCK statement at %C"))
3737 return MATCH_ERROR;
3739 return lock_unlock_statement (ST_UNLOCK);
3743 /* Match SYNC ALL/IMAGES/MEMORY statement. Syntax:
3744 SYNC ALL [(sync-stat-list)]
3745 SYNC MEMORY [(sync-stat-list)]
3746 SYNC IMAGES (image-set [, sync-stat-list] )
3747 with sync-stat is int-expr or *. */
3749 static match
3750 sync_statement (gfc_statement st)
3752 match m;
3753 gfc_expr *tmp, *imageset, *stat, *errmsg;
3754 bool saw_stat, saw_errmsg;
3756 tmp = imageset = stat = errmsg = NULL;
3757 saw_stat = saw_errmsg = false;
3759 if (gfc_pure (NULL))
3761 gfc_error ("Image control statement SYNC at %C in PURE procedure");
3762 return MATCH_ERROR;
3765 gfc_unset_implicit_pure (NULL);
3767 if (!gfc_notify_std (GFC_STD_F2008, "SYNC statement at %C"))
3768 return MATCH_ERROR;
3770 if (flag_coarray == GFC_FCOARRAY_NONE)
3772 gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to "
3773 "enable");
3774 return MATCH_ERROR;
3777 if (gfc_find_state (COMP_CRITICAL))
3779 gfc_error ("Image control statement SYNC at %C in CRITICAL block");
3780 return MATCH_ERROR;
3783 if (gfc_find_state (COMP_DO_CONCURRENT))
3785 gfc_error ("Image control statement SYNC at %C in DO CONCURRENT block");
3786 return MATCH_ERROR;
3789 if (gfc_match_eos () == MATCH_YES)
3791 if (st == ST_SYNC_IMAGES)
3792 goto syntax;
3793 goto done;
3796 if (gfc_match_char ('(') != MATCH_YES)
3797 goto syntax;
3799 if (st == ST_SYNC_IMAGES)
3801 /* Denote '*' as imageset == NULL. */
3802 m = gfc_match_char ('*');
3803 if (m == MATCH_ERROR)
3804 goto syntax;
3805 if (m == MATCH_NO)
3807 if (gfc_match ("%e", &imageset) != MATCH_YES)
3808 goto syntax;
3810 m = gfc_match_char (',');
3811 if (m == MATCH_ERROR)
3812 goto syntax;
3813 if (m == MATCH_NO)
3815 m = gfc_match_char (')');
3816 if (m == MATCH_YES)
3817 goto done;
3818 goto syntax;
3822 for (;;)
3824 m = gfc_match (" stat = %v", &tmp);
3825 if (m == MATCH_ERROR)
3826 goto syntax;
3827 if (m == MATCH_YES)
3829 if (saw_stat)
3831 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
3832 goto cleanup;
3834 stat = tmp;
3835 saw_stat = true;
3837 if (gfc_match_char (',') == MATCH_YES)
3838 continue;
3840 tmp = NULL;
3841 break;
3844 m = gfc_match (" errmsg = %v", &tmp);
3845 if (m == MATCH_ERROR)
3846 goto syntax;
3847 if (m == MATCH_YES)
3849 if (saw_errmsg)
3851 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
3852 goto cleanup;
3854 errmsg = tmp;
3855 saw_errmsg = true;
3857 if (gfc_match_char (',') == MATCH_YES)
3858 continue;
3860 tmp = NULL;
3861 break;
3864 break;
3867 if (gfc_match (" )%t") != MATCH_YES)
3868 goto syntax;
3870 done:
3871 switch (st)
3873 case ST_SYNC_ALL:
3874 new_st.op = EXEC_SYNC_ALL;
3875 break;
3876 case ST_SYNC_IMAGES:
3877 new_st.op = EXEC_SYNC_IMAGES;
3878 break;
3879 case ST_SYNC_MEMORY:
3880 new_st.op = EXEC_SYNC_MEMORY;
3881 break;
3882 default:
3883 gcc_unreachable ();
3886 new_st.expr1 = imageset;
3887 new_st.expr2 = stat;
3888 new_st.expr3 = errmsg;
3890 return MATCH_YES;
3892 syntax:
3893 gfc_syntax_error (st);
3895 cleanup:
3896 if (stat != tmp)
3897 gfc_free_expr (stat);
3898 if (errmsg != tmp)
3899 gfc_free_expr (errmsg);
3901 gfc_free_expr (tmp);
3902 gfc_free_expr (imageset);
3904 return MATCH_ERROR;
3908 /* Match SYNC ALL statement. */
3910 match
3911 gfc_match_sync_all (void)
3913 return sync_statement (ST_SYNC_ALL);
3917 /* Match SYNC IMAGES statement. */
3919 match
3920 gfc_match_sync_images (void)
3922 return sync_statement (ST_SYNC_IMAGES);
3926 /* Match SYNC MEMORY statement. */
3928 match
3929 gfc_match_sync_memory (void)
3931 return sync_statement (ST_SYNC_MEMORY);
3935 /* Match a CONTINUE statement. */
3937 match
3938 gfc_match_continue (void)
3940 if (gfc_match_eos () != MATCH_YES)
3942 gfc_syntax_error (ST_CONTINUE);
3943 return MATCH_ERROR;
3946 new_st.op = EXEC_CONTINUE;
3947 return MATCH_YES;
3951 /* Match the (deprecated) ASSIGN statement. */
3953 match
3954 gfc_match_assign (void)
3956 gfc_expr *expr;
3957 gfc_st_label *label;
3959 if (gfc_match (" %l", &label) == MATCH_YES)
3961 if (!gfc_reference_st_label (label, ST_LABEL_UNKNOWN))
3962 return MATCH_ERROR;
3963 if (gfc_match (" to %v%t", &expr) == MATCH_YES)
3965 if (!gfc_notify_std (GFC_STD_F95_DEL, "ASSIGN statement at %C"))
3966 return MATCH_ERROR;
3968 expr->symtree->n.sym->attr.assign = 1;
3970 new_st.op = EXEC_LABEL_ASSIGN;
3971 new_st.label1 = label;
3972 new_st.expr1 = expr;
3973 return MATCH_YES;
3976 return MATCH_NO;
3980 /* Match the GO TO statement. As a computed GOTO statement is
3981 matched, it is transformed into an equivalent SELECT block. No
3982 tree is necessary, and the resulting jumps-to-jumps are
3983 specifically optimized away by the back end. */
3985 match
3986 gfc_match_goto (void)
3988 gfc_code *head, *tail;
3989 gfc_expr *expr;
3990 gfc_case *cp;
3991 gfc_st_label *label;
3992 int i;
3993 match m;
3995 if (gfc_match (" %l%t", &label) == MATCH_YES)
3997 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
3998 return MATCH_ERROR;
4000 new_st.op = EXEC_GOTO;
4001 new_st.label1 = label;
4002 return MATCH_YES;
4005 /* The assigned GO TO statement. */
4007 if (gfc_match_variable (&expr, 0) == MATCH_YES)
4009 if (!gfc_notify_std (GFC_STD_F95_DEL, "Assigned GOTO statement at %C"))
4010 return MATCH_ERROR;
4012 new_st.op = EXEC_GOTO;
4013 new_st.expr1 = expr;
4015 if (gfc_match_eos () == MATCH_YES)
4016 return MATCH_YES;
4018 /* Match label list. */
4019 gfc_match_char (',');
4020 if (gfc_match_char ('(') != MATCH_YES)
4022 gfc_syntax_error (ST_GOTO);
4023 return MATCH_ERROR;
4025 head = tail = NULL;
4029 m = gfc_match_st_label (&label);
4030 if (m != MATCH_YES)
4031 goto syntax;
4033 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
4034 goto cleanup;
4036 if (head == NULL)
4037 head = tail = gfc_get_code (EXEC_GOTO);
4038 else
4040 tail->block = gfc_get_code (EXEC_GOTO);
4041 tail = tail->block;
4044 tail->label1 = label;
4046 while (gfc_match_char (',') == MATCH_YES);
4048 if (gfc_match (")%t") != MATCH_YES)
4049 goto syntax;
4051 if (head == NULL)
4053 gfc_error ("Statement label list in GOTO at %C cannot be empty");
4054 goto syntax;
4056 new_st.block = head;
4058 return MATCH_YES;
4061 /* Last chance is a computed GO TO statement. */
4062 if (gfc_match_char ('(') != MATCH_YES)
4064 gfc_syntax_error (ST_GOTO);
4065 return MATCH_ERROR;
4068 head = tail = NULL;
4069 i = 1;
4073 m = gfc_match_st_label (&label);
4074 if (m != MATCH_YES)
4075 goto syntax;
4077 if (!gfc_reference_st_label (label, ST_LABEL_TARGET))
4078 goto cleanup;
4080 if (head == NULL)
4081 head = tail = gfc_get_code (EXEC_SELECT);
4082 else
4084 tail->block = gfc_get_code (EXEC_SELECT);
4085 tail = tail->block;
4088 cp = gfc_get_case ();
4089 cp->low = cp->high = gfc_get_int_expr (gfc_default_integer_kind,
4090 NULL, i++);
4092 tail->ext.block.case_list = cp;
4094 tail->next = gfc_get_code (EXEC_GOTO);
4095 tail->next->label1 = label;
4097 while (gfc_match_char (',') == MATCH_YES);
4099 if (gfc_match_char (')') != MATCH_YES)
4100 goto syntax;
4102 if (head == NULL)
4104 gfc_error ("Statement label list in GOTO at %C cannot be empty");
4105 goto syntax;
4108 /* Get the rest of the statement. */
4109 gfc_match_char (',');
4111 if (gfc_match (" %e%t", &expr) != MATCH_YES)
4112 goto syntax;
4114 if (!gfc_notify_std (GFC_STD_F95_OBS, "Computed GOTO at %C"))
4115 return MATCH_ERROR;
4117 /* At this point, a computed GOTO has been fully matched and an
4118 equivalent SELECT statement constructed. */
4120 new_st.op = EXEC_SELECT;
4121 new_st.expr1 = NULL;
4123 /* Hack: For a "real" SELECT, the expression is in expr. We put
4124 it in expr2 so we can distinguish then and produce the correct
4125 diagnostics. */
4126 new_st.expr2 = expr;
4127 new_st.block = head;
4128 return MATCH_YES;
4130 syntax:
4131 gfc_syntax_error (ST_GOTO);
4132 cleanup:
4133 gfc_free_statements (head);
4134 return MATCH_ERROR;
4138 /* Frees a list of gfc_alloc structures. */
4140 void
4141 gfc_free_alloc_list (gfc_alloc *p)
4143 gfc_alloc *q;
4145 for (; p; p = q)
4147 q = p->next;
4148 gfc_free_expr (p->expr);
4149 free (p);
4154 /* Match an ALLOCATE statement. */
4156 match
4157 gfc_match_allocate (void)
4159 gfc_alloc *head, *tail;
4160 gfc_expr *stat, *errmsg, *tmp, *source, *mold;
4161 gfc_typespec ts;
4162 gfc_symbol *sym;
4163 match m;
4164 locus old_locus, deferred_locus, assumed_locus;
4165 bool saw_stat, saw_errmsg, saw_source, saw_mold, saw_deferred, b1, b2, b3;
4166 bool saw_unlimited = false, saw_assumed = false;
4168 head = tail = NULL;
4169 stat = errmsg = source = mold = tmp = NULL;
4170 saw_stat = saw_errmsg = saw_source = saw_mold = saw_deferred = false;
4172 if (gfc_match_char ('(') != MATCH_YES)
4174 gfc_syntax_error (ST_ALLOCATE);
4175 return MATCH_ERROR;
4178 /* Match an optional type-spec. */
4179 old_locus = gfc_current_locus;
4180 m = gfc_match_type_spec (&ts);
4181 if (m == MATCH_ERROR)
4182 goto cleanup;
4183 else if (m == MATCH_NO)
4185 char name[GFC_MAX_SYMBOL_LEN + 3];
4187 if (gfc_match ("%n :: ", name) == MATCH_YES)
4189 gfc_error ("Error in type-spec at %L", &old_locus);
4190 goto cleanup;
4193 ts.type = BT_UNKNOWN;
4195 else
4197 /* Needed for the F2008:C631 check below. */
4198 assumed_locus = gfc_current_locus;
4200 if (gfc_match (" :: ") == MATCH_YES)
4202 if (!gfc_notify_std (GFC_STD_F2003, "typespec in ALLOCATE at %L",
4203 &old_locus))
4204 goto cleanup;
4206 if (ts.deferred)
4208 gfc_error ("Type-spec at %L cannot contain a deferred "
4209 "type parameter", &old_locus);
4210 goto cleanup;
4213 if (ts.type == BT_CHARACTER)
4215 if (!ts.u.cl->length)
4216 saw_assumed = true;
4217 else
4218 ts.u.cl->length_from_typespec = true;
4221 if (type_param_spec_list
4222 && gfc_spec_list_type (type_param_spec_list, NULL)
4223 == SPEC_DEFERRED)
4225 gfc_error ("The type parameter spec list in the type-spec at "
4226 "%L cannot contain DEFERRED parameters", &old_locus);
4227 goto cleanup;
4230 else
4232 ts.type = BT_UNKNOWN;
4233 gfc_current_locus = old_locus;
4237 for (;;)
4239 if (head == NULL)
4240 head = tail = gfc_get_alloc ();
4241 else
4243 tail->next = gfc_get_alloc ();
4244 tail = tail->next;
4247 m = gfc_match_variable (&tail->expr, 0);
4248 if (m == MATCH_NO)
4249 goto syntax;
4250 if (m == MATCH_ERROR)
4251 goto cleanup;
4253 if (tail->expr->expr_type == EXPR_CONSTANT)
4255 gfc_error ("Unexpected constant at %C");
4256 goto cleanup;
4259 if (gfc_check_do_variable (tail->expr->symtree))
4260 goto cleanup;
4262 bool impure = gfc_impure_variable (tail->expr->symtree->n.sym);
4263 if (impure && gfc_pure (NULL))
4265 gfc_error ("Bad allocate-object at %C for a PURE procedure");
4266 goto cleanup;
4269 if (impure)
4270 gfc_unset_implicit_pure (NULL);
4272 /* F2008:C631 (R626) A type-param-value in a type-spec shall be an
4273 asterisk if and only if each allocate-object is a dummy argument
4274 for which the corresponding type parameter is assumed. */
4275 if (saw_assumed
4276 && (tail->expr->ts.deferred
4277 || (tail->expr->ts.u.cl && tail->expr->ts.u.cl->length)
4278 || tail->expr->symtree->n.sym->attr.dummy == 0))
4280 gfc_error ("Incompatible allocate-object at %C for CHARACTER "
4281 "type-spec at %L", &assumed_locus);
4282 goto cleanup;
4285 if (tail->expr->ts.deferred)
4287 saw_deferred = true;
4288 deferred_locus = tail->expr->where;
4291 if (gfc_find_state (COMP_DO_CONCURRENT)
4292 || gfc_find_state (COMP_CRITICAL))
4294 gfc_ref *ref;
4295 bool coarray = tail->expr->symtree->n.sym->attr.codimension;
4296 for (ref = tail->expr->ref; ref; ref = ref->next)
4297 if (ref->type == REF_COMPONENT)
4298 coarray = ref->u.c.component->attr.codimension;
4300 if (coarray && gfc_find_state (COMP_DO_CONCURRENT))
4302 gfc_error ("ALLOCATE of coarray at %C in DO CONCURRENT block");
4303 goto cleanup;
4305 if (coarray && gfc_find_state (COMP_CRITICAL))
4307 gfc_error ("ALLOCATE of coarray at %C in CRITICAL block");
4308 goto cleanup;
4312 /* Check for F08:C628. */
4313 sym = tail->expr->symtree->n.sym;
4314 b1 = !(tail->expr->ref
4315 && (tail->expr->ref->type == REF_COMPONENT
4316 || tail->expr->ref->type == REF_ARRAY));
4317 if (sym && sym->ts.type == BT_CLASS && sym->attr.class_ok)
4318 b2 = !(CLASS_DATA (sym)->attr.allocatable
4319 || CLASS_DATA (sym)->attr.class_pointer);
4320 else
4321 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4322 || sym->attr.proc_pointer);
4323 b3 = sym && sym->ns && sym->ns->proc_name
4324 && (sym->ns->proc_name->attr.allocatable
4325 || sym->ns->proc_name->attr.pointer
4326 || sym->ns->proc_name->attr.proc_pointer);
4327 if (b1 && b2 && !b3)
4329 gfc_error ("Allocate-object at %L is neither a data pointer "
4330 "nor an allocatable variable", &tail->expr->where);
4331 goto cleanup;
4334 /* The ALLOCATE statement had an optional typespec. Check the
4335 constraints. */
4336 if (ts.type != BT_UNKNOWN)
4338 /* Enforce F03:C624. */
4339 if (!gfc_type_compatible (&tail->expr->ts, &ts))
4341 gfc_error ("Type of entity at %L is type incompatible with "
4342 "typespec", &tail->expr->where);
4343 goto cleanup;
4346 /* Enforce F03:C627. */
4347 if (ts.kind != tail->expr->ts.kind && !UNLIMITED_POLY (tail->expr))
4349 gfc_error ("Kind type parameter for entity at %L differs from "
4350 "the kind type parameter of the typespec",
4351 &tail->expr->where);
4352 goto cleanup;
4356 if (tail->expr->ts.type == BT_DERIVED)
4357 tail->expr->ts.u.derived = gfc_use_derived (tail->expr->ts.u.derived);
4359 if (type_param_spec_list)
4360 tail->expr->param_list = gfc_copy_actual_arglist (type_param_spec_list);
4362 saw_unlimited = saw_unlimited | UNLIMITED_POLY (tail->expr);
4364 if (gfc_peek_ascii_char () == '(' && !sym->attr.dimension)
4366 gfc_error ("Shape specification for allocatable scalar at %C");
4367 goto cleanup;
4370 if (gfc_match_char (',') != MATCH_YES)
4371 break;
4373 alloc_opt_list:
4375 m = gfc_match (" stat = %v", &tmp);
4376 if (m == MATCH_ERROR)
4377 goto cleanup;
4378 if (m == MATCH_YES)
4380 /* Enforce C630. */
4381 if (saw_stat)
4383 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
4384 goto cleanup;
4387 stat = tmp;
4388 tmp = NULL;
4389 saw_stat = true;
4391 if (stat->expr_type == EXPR_CONSTANT)
4393 gfc_error ("STAT tag at %L cannot be a constant", &stat->where);
4394 goto cleanup;
4397 if (gfc_check_do_variable (stat->symtree))
4398 goto cleanup;
4400 if (gfc_match_char (',') == MATCH_YES)
4401 goto alloc_opt_list;
4404 m = gfc_match (" errmsg = %v", &tmp);
4405 if (m == MATCH_ERROR)
4406 goto cleanup;
4407 if (m == MATCH_YES)
4409 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG tag at %L", &tmp->where))
4410 goto cleanup;
4412 /* Enforce C630. */
4413 if (saw_errmsg)
4415 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
4416 goto cleanup;
4419 errmsg = tmp;
4420 tmp = NULL;
4421 saw_errmsg = true;
4423 if (gfc_match_char (',') == MATCH_YES)
4424 goto alloc_opt_list;
4427 m = gfc_match (" source = %e", &tmp);
4428 if (m == MATCH_ERROR)
4429 goto cleanup;
4430 if (m == MATCH_YES)
4432 if (!gfc_notify_std (GFC_STD_F2003, "SOURCE tag at %L", &tmp->where))
4433 goto cleanup;
4435 /* Enforce C630. */
4436 if (saw_source)
4438 gfc_error ("Redundant SOURCE tag found at %L", &tmp->where);
4439 goto cleanup;
4442 /* The next 2 conditionals check C631. */
4443 if (ts.type != BT_UNKNOWN)
4445 gfc_error ("SOURCE tag at %L conflicts with the typespec at %L",
4446 &tmp->where, &old_locus);
4447 goto cleanup;
4450 if (head->next
4451 && !gfc_notify_std (GFC_STD_F2008, "SOURCE tag at %L"
4452 " with more than a single allocate object",
4453 &tmp->where))
4454 goto cleanup;
4456 source = tmp;
4457 tmp = NULL;
4458 saw_source = true;
4460 if (gfc_match_char (',') == MATCH_YES)
4461 goto alloc_opt_list;
4464 m = gfc_match (" mold = %e", &tmp);
4465 if (m == MATCH_ERROR)
4466 goto cleanup;
4467 if (m == MATCH_YES)
4469 if (!gfc_notify_std (GFC_STD_F2008, "MOLD tag at %L", &tmp->where))
4470 goto cleanup;
4472 /* Check F08:C636. */
4473 if (saw_mold)
4475 gfc_error ("Redundant MOLD tag found at %L", &tmp->where);
4476 goto cleanup;
4479 /* Check F08:C637. */
4480 if (ts.type != BT_UNKNOWN)
4482 gfc_error ("MOLD tag at %L conflicts with the typespec at %L",
4483 &tmp->where, &old_locus);
4484 goto cleanup;
4487 mold = tmp;
4488 tmp = NULL;
4489 saw_mold = true;
4490 mold->mold = 1;
4492 if (gfc_match_char (',') == MATCH_YES)
4493 goto alloc_opt_list;
4496 gfc_gobble_whitespace ();
4498 if (gfc_peek_char () == ')')
4499 break;
4502 if (gfc_match (" )%t") != MATCH_YES)
4503 goto syntax;
4505 /* Check F08:C637. */
4506 if (source && mold)
4508 gfc_error ("MOLD tag at %L conflicts with SOURCE tag at %L",
4509 &mold->where, &source->where);
4510 goto cleanup;
4513 /* Check F03:C623, */
4514 if (saw_deferred && ts.type == BT_UNKNOWN && !source && !mold)
4516 gfc_error ("Allocate-object at %L with a deferred type parameter "
4517 "requires either a type-spec or SOURCE tag or a MOLD tag",
4518 &deferred_locus);
4519 goto cleanup;
4522 /* Check F03:C625, */
4523 if (saw_unlimited && ts.type == BT_UNKNOWN && !source && !mold)
4525 for (tail = head; tail; tail = tail->next)
4527 if (UNLIMITED_POLY (tail->expr))
4528 gfc_error ("Unlimited polymorphic allocate-object at %L "
4529 "requires either a type-spec or SOURCE tag "
4530 "or a MOLD tag", &tail->expr->where);
4532 goto cleanup;
4535 new_st.op = EXEC_ALLOCATE;
4536 new_st.expr1 = stat;
4537 new_st.expr2 = errmsg;
4538 if (source)
4539 new_st.expr3 = source;
4540 else
4541 new_st.expr3 = mold;
4542 new_st.ext.alloc.list = head;
4543 new_st.ext.alloc.ts = ts;
4545 if (type_param_spec_list)
4546 gfc_free_actual_arglist (type_param_spec_list);
4548 return MATCH_YES;
4550 syntax:
4551 gfc_syntax_error (ST_ALLOCATE);
4553 cleanup:
4554 gfc_free_expr (errmsg);
4555 gfc_free_expr (source);
4556 gfc_free_expr (stat);
4557 gfc_free_expr (mold);
4558 if (tmp && tmp->expr_type) gfc_free_expr (tmp);
4559 gfc_free_alloc_list (head);
4560 if (type_param_spec_list)
4561 gfc_free_actual_arglist (type_param_spec_list);
4562 return MATCH_ERROR;
4566 /* Match a NULLIFY statement. A NULLIFY statement is transformed into
4567 a set of pointer assignments to intrinsic NULL(). */
4569 match
4570 gfc_match_nullify (void)
4572 gfc_code *tail;
4573 gfc_expr *e, *p;
4574 match m;
4576 tail = NULL;
4578 if (gfc_match_char ('(') != MATCH_YES)
4579 goto syntax;
4581 for (;;)
4583 m = gfc_match_variable (&p, 0);
4584 if (m == MATCH_ERROR)
4585 goto cleanup;
4586 if (m == MATCH_NO)
4587 goto syntax;
4589 if (gfc_check_do_variable (p->symtree))
4590 goto cleanup;
4592 /* F2008, C1242. */
4593 if (gfc_is_coindexed (p))
4595 gfc_error ("Pointer object at %C shall not be coindexed");
4596 goto cleanup;
4599 /* Check for valid array pointer object. Bounds remapping is not
4600 allowed with NULLIFY. */
4601 if (p->ref)
4603 gfc_ref *remap = p->ref;
4604 for (; remap; remap = remap->next)
4605 if (!remap->next && remap->type == REF_ARRAY
4606 && remap->u.ar.type != AR_FULL)
4607 break;
4608 if (remap)
4610 gfc_error ("NULLIFY does not allow bounds remapping for "
4611 "pointer object at %C");
4612 goto cleanup;
4616 /* build ' => NULL() '. */
4617 e = gfc_get_null_expr (&gfc_current_locus);
4619 /* Chain to list. */
4620 if (tail == NULL)
4622 tail = &new_st;
4623 tail->op = EXEC_POINTER_ASSIGN;
4625 else
4627 tail->next = gfc_get_code (EXEC_POINTER_ASSIGN);
4628 tail = tail->next;
4631 tail->expr1 = p;
4632 tail->expr2 = e;
4634 if (gfc_match (" )%t") == MATCH_YES)
4635 break;
4636 if (gfc_match_char (',') != MATCH_YES)
4637 goto syntax;
4640 return MATCH_YES;
4642 syntax:
4643 gfc_syntax_error (ST_NULLIFY);
4645 cleanup:
4646 gfc_free_statements (new_st.next);
4647 new_st.next = NULL;
4648 gfc_free_expr (new_st.expr1);
4649 new_st.expr1 = NULL;
4650 gfc_free_expr (new_st.expr2);
4651 new_st.expr2 = NULL;
4652 return MATCH_ERROR;
4656 /* Match a DEALLOCATE statement. */
4658 match
4659 gfc_match_deallocate (void)
4661 gfc_alloc *head, *tail;
4662 gfc_expr *stat, *errmsg, *tmp;
4663 gfc_symbol *sym;
4664 match m;
4665 bool saw_stat, saw_errmsg, b1, b2;
4667 head = tail = NULL;
4668 stat = errmsg = tmp = NULL;
4669 saw_stat = saw_errmsg = false;
4671 if (gfc_match_char ('(') != MATCH_YES)
4672 goto syntax;
4674 for (;;)
4676 if (head == NULL)
4677 head = tail = gfc_get_alloc ();
4678 else
4680 tail->next = gfc_get_alloc ();
4681 tail = tail->next;
4684 m = gfc_match_variable (&tail->expr, 0);
4685 if (m == MATCH_ERROR)
4686 goto cleanup;
4687 if (m == MATCH_NO)
4688 goto syntax;
4690 if (tail->expr->expr_type == EXPR_CONSTANT)
4692 gfc_error ("Unexpected constant at %C");
4693 goto cleanup;
4696 if (gfc_check_do_variable (tail->expr->symtree))
4697 goto cleanup;
4699 sym = tail->expr->symtree->n.sym;
4701 bool impure = gfc_impure_variable (sym);
4702 if (impure && gfc_pure (NULL))
4704 gfc_error ("Illegal allocate-object at %C for a PURE procedure");
4705 goto cleanup;
4708 if (impure)
4709 gfc_unset_implicit_pure (NULL);
4711 if (gfc_is_coarray (tail->expr)
4712 && gfc_find_state (COMP_DO_CONCURRENT))
4714 gfc_error ("DEALLOCATE of coarray at %C in DO CONCURRENT block");
4715 goto cleanup;
4718 if (gfc_is_coarray (tail->expr)
4719 && gfc_find_state (COMP_CRITICAL))
4721 gfc_error ("DEALLOCATE of coarray at %C in CRITICAL block");
4722 goto cleanup;
4725 /* FIXME: disable the checking on derived types. */
4726 b1 = !(tail->expr->ref
4727 && (tail->expr->ref->type == REF_COMPONENT
4728 || tail->expr->ref->type == REF_ARRAY));
4729 if (sym && sym->ts.type == BT_CLASS)
4730 b2 = !(CLASS_DATA (sym) && (CLASS_DATA (sym)->attr.allocatable
4731 || CLASS_DATA (sym)->attr.class_pointer));
4732 else
4733 b2 = sym && !(sym->attr.allocatable || sym->attr.pointer
4734 || sym->attr.proc_pointer);
4735 if (b1 && b2)
4737 gfc_error ("Allocate-object at %C is not a nonprocedure pointer "
4738 "nor an allocatable variable");
4739 goto cleanup;
4742 if (gfc_match_char (',') != MATCH_YES)
4743 break;
4745 dealloc_opt_list:
4747 m = gfc_match (" stat = %v", &tmp);
4748 if (m == MATCH_ERROR)
4749 goto cleanup;
4750 if (m == MATCH_YES)
4752 if (saw_stat)
4754 gfc_error ("Redundant STAT tag found at %L", &tmp->where);
4755 gfc_free_expr (tmp);
4756 goto cleanup;
4759 stat = tmp;
4760 saw_stat = true;
4762 if (gfc_check_do_variable (stat->symtree))
4763 goto cleanup;
4765 if (gfc_match_char (',') == MATCH_YES)
4766 goto dealloc_opt_list;
4769 m = gfc_match (" errmsg = %v", &tmp);
4770 if (m == MATCH_ERROR)
4771 goto cleanup;
4772 if (m == MATCH_YES)
4774 if (!gfc_notify_std (GFC_STD_F2003, "ERRMSG at %L", &tmp->where))
4775 goto cleanup;
4777 if (saw_errmsg)
4779 gfc_error ("Redundant ERRMSG tag found at %L", &tmp->where);
4780 gfc_free_expr (tmp);
4781 goto cleanup;
4784 errmsg = tmp;
4785 saw_errmsg = true;
4787 if (gfc_match_char (',') == MATCH_YES)
4788 goto dealloc_opt_list;
4791 gfc_gobble_whitespace ();
4793 if (gfc_peek_char () == ')')
4794 break;
4797 if (gfc_match (" )%t") != MATCH_YES)
4798 goto syntax;
4800 new_st.op = EXEC_DEALLOCATE;
4801 new_st.expr1 = stat;
4802 new_st.expr2 = errmsg;
4803 new_st.ext.alloc.list = head;
4805 return MATCH_YES;
4807 syntax:
4808 gfc_syntax_error (ST_DEALLOCATE);
4810 cleanup:
4811 gfc_free_expr (errmsg);
4812 gfc_free_expr (stat);
4813 gfc_free_alloc_list (head);
4814 return MATCH_ERROR;
4818 /* Match a RETURN statement. */
4820 match
4821 gfc_match_return (void)
4823 gfc_expr *e;
4824 match m;
4825 gfc_compile_state s;
4827 e = NULL;
4829 if (gfc_find_state (COMP_CRITICAL))
4831 gfc_error ("Image control statement RETURN at %C in CRITICAL block");
4832 return MATCH_ERROR;
4835 if (gfc_find_state (COMP_DO_CONCURRENT))
4837 gfc_error ("Image control statement RETURN at %C in DO CONCURRENT block");
4838 return MATCH_ERROR;
4841 if (gfc_match_eos () == MATCH_YES)
4842 goto done;
4844 if (!gfc_find_state (COMP_SUBROUTINE))
4846 gfc_error ("Alternate RETURN statement at %C is only allowed within "
4847 "a SUBROUTINE");
4848 goto cleanup;
4851 if (gfc_current_form == FORM_FREE)
4853 /* The following are valid, so we can't require a blank after the
4854 RETURN keyword:
4855 return+1
4856 return(1) */
4857 char c = gfc_peek_ascii_char ();
4858 if (ISALPHA (c) || ISDIGIT (c))
4859 return MATCH_NO;
4862 m = gfc_match (" %e%t", &e);
4863 if (m == MATCH_YES)
4864 goto done;
4865 if (m == MATCH_ERROR)
4866 goto cleanup;
4868 gfc_syntax_error (ST_RETURN);
4870 cleanup:
4871 gfc_free_expr (e);
4872 return MATCH_ERROR;
4874 done:
4875 gfc_enclosing_unit (&s);
4876 if (s == COMP_PROGRAM
4877 && !gfc_notify_std (GFC_STD_GNU, "RETURN statement in "
4878 "main program at %C"))
4879 return MATCH_ERROR;
4881 new_st.op = EXEC_RETURN;
4882 new_st.expr1 = e;
4884 return MATCH_YES;
4888 /* Match the call of a type-bound procedure, if CALL%var has already been
4889 matched and var found to be a derived-type variable. */
4891 static match
4892 match_typebound_call (gfc_symtree* varst)
4894 gfc_expr* base;
4895 match m;
4897 base = gfc_get_expr ();
4898 base->expr_type = EXPR_VARIABLE;
4899 base->symtree = varst;
4900 base->where = gfc_current_locus;
4901 gfc_set_sym_referenced (varst->n.sym);
4903 m = gfc_match_varspec (base, 0, true, true);
4904 if (m == MATCH_NO)
4905 gfc_error ("Expected component reference at %C");
4906 if (m != MATCH_YES)
4908 gfc_free_expr (base);
4909 return MATCH_ERROR;
4912 if (gfc_match_eos () != MATCH_YES)
4914 gfc_error ("Junk after CALL at %C");
4915 gfc_free_expr (base);
4916 return MATCH_ERROR;
4919 if (base->expr_type == EXPR_COMPCALL)
4920 new_st.op = EXEC_COMPCALL;
4921 else if (base->expr_type == EXPR_PPC)
4922 new_st.op = EXEC_CALL_PPC;
4923 else
4925 gfc_error ("Expected type-bound procedure or procedure pointer component "
4926 "at %C");
4927 gfc_free_expr (base);
4928 return MATCH_ERROR;
4930 new_st.expr1 = base;
4932 return MATCH_YES;
4936 /* Match a CALL statement. The tricky part here are possible
4937 alternate return specifiers. We handle these by having all
4938 "subroutines" actually return an integer via a register that gives
4939 the return number. If the call specifies alternate returns, we
4940 generate code for a SELECT statement whose case clauses contain
4941 GOTOs to the various labels. */
4943 match
4944 gfc_match_call (void)
4946 char name[GFC_MAX_SYMBOL_LEN + 1];
4947 gfc_actual_arglist *a, *arglist;
4948 gfc_case *new_case;
4949 gfc_symbol *sym;
4950 gfc_symtree *st;
4951 gfc_code *c;
4952 match m;
4953 int i;
4955 arglist = NULL;
4957 m = gfc_match ("% %n", name);
4958 if (m == MATCH_NO)
4959 goto syntax;
4960 if (m != MATCH_YES)
4961 return m;
4963 if (gfc_get_ha_sym_tree (name, &st))
4964 return MATCH_ERROR;
4966 sym = st->n.sym;
4968 /* If this is a variable of derived-type, it probably starts a type-bound
4969 procedure call. */
4970 if ((sym->attr.flavor != FL_PROCEDURE
4971 || gfc_is_function_return_value (sym, gfc_current_ns))
4972 && (sym->ts.type == BT_DERIVED || sym->ts.type == BT_CLASS))
4973 return match_typebound_call (st);
4975 /* If it does not seem to be callable (include functions so that the
4976 right association is made. They are thrown out in resolution.)
4977 ... */
4978 if (!sym->attr.generic
4979 && !sym->attr.subroutine
4980 && !sym->attr.function)
4982 if (!(sym->attr.external && !sym->attr.referenced))
4984 /* ...create a symbol in this scope... */
4985 if (sym->ns != gfc_current_ns
4986 && gfc_get_sym_tree (name, NULL, &st, false) == 1)
4987 return MATCH_ERROR;
4989 if (sym != st->n.sym)
4990 sym = st->n.sym;
4993 /* ...and then to try to make the symbol into a subroutine. */
4994 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
4995 return MATCH_ERROR;
4998 gfc_set_sym_referenced (sym);
5000 if (gfc_match_eos () != MATCH_YES)
5002 m = gfc_match_actual_arglist (1, &arglist);
5003 if (m == MATCH_NO)
5004 goto syntax;
5005 if (m == MATCH_ERROR)
5006 goto cleanup;
5008 if (gfc_match_eos () != MATCH_YES)
5009 goto syntax;
5012 /* Walk the argument list looking for invalid BOZ. */
5013 for (a = arglist; a; a = a->next)
5014 if (a->expr && a->expr->ts.type == BT_BOZ)
5016 gfc_error ("A BOZ literal constant at %L cannot appear as an actual "
5017 "argument in a subroutine reference", &a->expr->where);
5018 goto cleanup;
5022 /* If any alternate return labels were found, construct a SELECT
5023 statement that will jump to the right place. */
5025 i = 0;
5026 for (a = arglist; a; a = a->next)
5027 if (a->expr == NULL)
5029 i = 1;
5030 break;
5033 if (i)
5035 gfc_symtree *select_st;
5036 gfc_symbol *select_sym;
5037 char name[GFC_MAX_SYMBOL_LEN + 1];
5039 new_st.next = c = gfc_get_code (EXEC_SELECT);
5040 sprintf (name, "_result_%s", sym->name);
5041 gfc_get_ha_sym_tree (name, &select_st); /* Can't fail. */
5043 select_sym = select_st->n.sym;
5044 select_sym->ts.type = BT_INTEGER;
5045 select_sym->ts.kind = gfc_default_integer_kind;
5046 gfc_set_sym_referenced (select_sym);
5047 c->expr1 = gfc_get_expr ();
5048 c->expr1->expr_type = EXPR_VARIABLE;
5049 c->expr1->symtree = select_st;
5050 c->expr1->ts = select_sym->ts;
5051 c->expr1->where = gfc_current_locus;
5053 i = 0;
5054 for (a = arglist; a; a = a->next)
5056 if (a->expr != NULL)
5057 continue;
5059 if (!gfc_reference_st_label (a->label, ST_LABEL_TARGET))
5060 continue;
5062 i++;
5064 c->block = gfc_get_code (EXEC_SELECT);
5065 c = c->block;
5067 new_case = gfc_get_case ();
5068 new_case->high = gfc_get_int_expr (gfc_default_integer_kind, NULL, i);
5069 new_case->low = new_case->high;
5070 c->ext.block.case_list = new_case;
5072 c->next = gfc_get_code (EXEC_GOTO);
5073 c->next->label1 = a->label;
5077 new_st.op = EXEC_CALL;
5078 new_st.symtree = st;
5079 new_st.ext.actual = arglist;
5081 return MATCH_YES;
5083 syntax:
5084 gfc_syntax_error (ST_CALL);
5086 cleanup:
5087 gfc_free_actual_arglist (arglist);
5088 return MATCH_ERROR;
5092 /* Given a name, return a pointer to the common head structure,
5093 creating it if it does not exist. If FROM_MODULE is nonzero, we
5094 mangle the name so that it doesn't interfere with commons defined
5095 in the using namespace.
5096 TODO: Add to global symbol tree. */
5098 gfc_common_head *
5099 gfc_get_common (const char *name, int from_module)
5101 gfc_symtree *st;
5102 static int serial = 0;
5103 char mangled_name[GFC_MAX_SYMBOL_LEN + 1];
5105 if (from_module)
5107 /* A use associated common block is only needed to correctly layout
5108 the variables it contains. */
5109 snprintf (mangled_name, GFC_MAX_SYMBOL_LEN, "_%d_%s", serial++, name);
5110 st = gfc_new_symtree (&gfc_current_ns->common_root, mangled_name);
5112 else
5114 st = gfc_find_symtree (gfc_current_ns->common_root, name);
5116 if (st == NULL)
5117 st = gfc_new_symtree (&gfc_current_ns->common_root, name);
5120 if (st->n.common == NULL)
5122 st->n.common = gfc_get_common_head ();
5123 st->n.common->where = gfc_current_locus;
5124 strcpy (st->n.common->name, name);
5127 return st->n.common;
5131 /* Match a common block name. */
5133 match match_common_name (char *name)
5135 match m;
5137 if (gfc_match_char ('/') == MATCH_NO)
5139 name[0] = '\0';
5140 return MATCH_YES;
5143 if (gfc_match_char ('/') == MATCH_YES)
5145 name[0] = '\0';
5146 return MATCH_YES;
5149 m = gfc_match_name (name);
5151 if (m == MATCH_ERROR)
5152 return MATCH_ERROR;
5153 if (m == MATCH_YES && gfc_match_char ('/') == MATCH_YES)
5154 return MATCH_YES;
5156 gfc_error ("Syntax error in common block name at %C");
5157 return MATCH_ERROR;
5161 /* Match a COMMON statement. */
5163 match
5164 gfc_match_common (void)
5166 gfc_symbol *sym, **head, *tail, *other;
5167 char name[GFC_MAX_SYMBOL_LEN + 1];
5168 gfc_common_head *t;
5169 gfc_array_spec *as;
5170 gfc_equiv *e1, *e2;
5171 match m;
5172 char c;
5174 /* COMMON has been matched. In free form source code, the next character
5175 needs to be whitespace or '/'. Check that here. Fixed form source
5176 code needs to be checked below. */
5177 c = gfc_peek_ascii_char ();
5178 if (gfc_current_form == FORM_FREE && !gfc_is_whitespace (c) && c != '/')
5179 return MATCH_NO;
5181 as = NULL;
5183 for (;;)
5185 m = match_common_name (name);
5186 if (m == MATCH_ERROR)
5187 goto cleanup;
5189 if (name[0] == '\0')
5191 t = &gfc_current_ns->blank_common;
5192 if (t->head == NULL)
5193 t->where = gfc_current_locus;
5195 else
5197 t = gfc_get_common (name, 0);
5199 head = &t->head;
5201 if (*head == NULL)
5202 tail = NULL;
5203 else
5205 tail = *head;
5206 while (tail->common_next)
5207 tail = tail->common_next;
5210 /* Grab the list of symbols. */
5211 for (;;)
5213 m = gfc_match_symbol (&sym, 0);
5214 if (m == MATCH_ERROR)
5215 goto cleanup;
5216 if (m == MATCH_NO)
5217 goto syntax;
5219 /* See if we know the current common block is bind(c), and if
5220 so, then see if we can check if the symbol is (which it'll
5221 need to be). This can happen if the bind(c) attr stmt was
5222 applied to the common block, and the variable(s) already
5223 defined, before declaring the common block. */
5224 if (t->is_bind_c == 1)
5226 if (sym->ts.type != BT_UNKNOWN && sym->ts.is_c_interop != 1)
5228 /* If we find an error, just print it and continue,
5229 cause it's just semantic, and we can see if there
5230 are more errors. */
5231 gfc_error_now ("Variable %qs at %L in common block %qs "
5232 "at %C must be declared with a C "
5233 "interoperable kind since common block "
5234 "%qs is bind(c)",
5235 sym->name, &(sym->declared_at), t->name,
5236 t->name);
5239 if (sym->attr.is_bind_c == 1)
5240 gfc_error_now ("Variable %qs in common block %qs at %C cannot "
5241 "be bind(c) since it is not global", sym->name,
5242 t->name);
5245 if (sym->attr.in_common)
5247 gfc_error ("Symbol %qs at %C is already in a COMMON block",
5248 sym->name);
5249 goto cleanup;
5252 if (((sym->value != NULL && sym->value->expr_type != EXPR_NULL)
5253 || sym->attr.data) && gfc_current_state () != COMP_BLOCK_DATA)
5255 if (!gfc_notify_std (GFC_STD_GNU, "Initialized symbol %qs at "
5256 "%C can only be COMMON in BLOCK DATA",
5257 sym->name))
5258 goto cleanup;
5261 /* Deal with an optional array specification after the
5262 symbol name. */
5263 m = gfc_match_array_spec (&as, true, true);
5264 if (m == MATCH_ERROR)
5265 goto cleanup;
5267 if (m == MATCH_YES)
5269 if (as->type != AS_EXPLICIT)
5271 gfc_error ("Array specification for symbol %qs in COMMON "
5272 "at %C must be explicit", sym->name);
5273 goto cleanup;
5276 if (!gfc_add_dimension (&sym->attr, sym->name, NULL))
5277 goto cleanup;
5279 if (sym->attr.pointer)
5281 gfc_error ("Symbol %qs in COMMON at %C cannot be a "
5282 "POINTER array", sym->name);
5283 goto cleanup;
5286 sym->as = as;
5287 as = NULL;
5291 /* Add the in_common attribute, but ignore the reported errors
5292 if any, and continue matching. */
5293 gfc_add_in_common (&sym->attr, sym->name, NULL);
5295 sym->common_block = t;
5296 sym->common_block->refs++;
5298 if (tail != NULL)
5299 tail->common_next = sym;
5300 else
5301 *head = sym;
5303 tail = sym;
5305 sym->common_head = t;
5307 /* Check to see if the symbol is already in an equivalence group.
5308 If it is, set the other members as being in common. */
5309 if (sym->attr.in_equivalence)
5311 for (e1 = gfc_current_ns->equiv; e1; e1 = e1->next)
5313 for (e2 = e1; e2; e2 = e2->eq)
5314 if (e2->expr->symtree->n.sym == sym)
5315 goto equiv_found;
5317 continue;
5319 equiv_found:
5321 for (e2 = e1; e2; e2 = e2->eq)
5323 other = e2->expr->symtree->n.sym;
5324 if (other->common_head
5325 && other->common_head != sym->common_head)
5327 gfc_error ("Symbol %qs, in COMMON block %qs at "
5328 "%C is being indirectly equivalenced to "
5329 "another COMMON block %qs",
5330 sym->name, sym->common_head->name,
5331 other->common_head->name);
5332 goto cleanup;
5334 other->attr.in_common = 1;
5335 other->common_head = t;
5341 gfc_gobble_whitespace ();
5342 if (gfc_match_eos () == MATCH_YES)
5343 goto done;
5344 c = gfc_peek_ascii_char ();
5345 if (c == '/')
5346 break;
5347 if (c != ',')
5349 /* In Fixed form source code, gfortran can end up here for an
5350 expression of the form COMMONI = RHS. This may not be an
5351 error, so return MATCH_NO. */
5352 if (gfc_current_form == FORM_FIXED && c == '=')
5354 gfc_free_array_spec (as);
5355 return MATCH_NO;
5357 goto syntax;
5359 else
5360 gfc_match_char (',');
5362 gfc_gobble_whitespace ();
5363 if (gfc_peek_ascii_char () == '/')
5364 break;
5368 done:
5369 return MATCH_YES;
5371 syntax:
5372 gfc_syntax_error (ST_COMMON);
5374 cleanup:
5375 gfc_free_array_spec (as);
5376 return MATCH_ERROR;
5380 /* Match a BLOCK DATA program unit. */
5382 match
5383 gfc_match_block_data (void)
5385 char name[GFC_MAX_SYMBOL_LEN + 1];
5386 gfc_symbol *sym;
5387 match m;
5389 if (!gfc_notify_std (GFC_STD_F2018_OBS, "BLOCK DATA construct at %L",
5390 &gfc_current_locus))
5391 return MATCH_ERROR;
5393 if (gfc_match_eos () == MATCH_YES)
5395 gfc_new_block = NULL;
5396 return MATCH_YES;
5399 m = gfc_match ("% %n%t", name);
5400 if (m != MATCH_YES)
5401 return MATCH_ERROR;
5403 if (gfc_get_symbol (name, NULL, &sym))
5404 return MATCH_ERROR;
5406 if (!gfc_add_flavor (&sym->attr, FL_BLOCK_DATA, sym->name, NULL))
5407 return MATCH_ERROR;
5409 gfc_new_block = sym;
5411 return MATCH_YES;
5415 /* Free a namelist structure. */
5417 void
5418 gfc_free_namelist (gfc_namelist *name)
5420 gfc_namelist *n;
5422 for (; name; name = n)
5424 n = name->next;
5425 free (name);
5430 /* Free an OpenMP namelist structure. */
5432 void
5433 gfc_free_omp_namelist (gfc_omp_namelist *name)
5435 gfc_omp_namelist *n;
5437 for (; name; name = n)
5439 gfc_free_expr (name->expr);
5440 if (name->udr)
5442 if (name->udr->combiner)
5443 gfc_free_statement (name->udr->combiner);
5444 if (name->udr->initializer)
5445 gfc_free_statement (name->udr->initializer);
5446 free (name->udr);
5448 n = name->next;
5449 free (name);
5454 /* Match a NAMELIST statement. */
5456 match
5457 gfc_match_namelist (void)
5459 gfc_symbol *group_name, *sym;
5460 gfc_namelist *nl;
5461 match m, m2;
5463 m = gfc_match (" / %s /", &group_name);
5464 if (m == MATCH_NO)
5465 goto syntax;
5466 if (m == MATCH_ERROR)
5467 goto error;
5469 for (;;)
5471 if (group_name->ts.type != BT_UNKNOWN)
5473 gfc_error ("Namelist group name %qs at %C already has a basic "
5474 "type of %s", group_name->name,
5475 gfc_typename (&group_name->ts));
5476 return MATCH_ERROR;
5479 if (group_name->attr.flavor == FL_NAMELIST
5480 && group_name->attr.use_assoc
5481 && !gfc_notify_std (GFC_STD_GNU, "Namelist group name %qs "
5482 "at %C already is USE associated and can"
5483 "not be respecified.", group_name->name))
5484 return MATCH_ERROR;
5486 if (group_name->attr.flavor != FL_NAMELIST
5487 && !gfc_add_flavor (&group_name->attr, FL_NAMELIST,
5488 group_name->name, NULL))
5489 return MATCH_ERROR;
5491 for (;;)
5493 m = gfc_match_symbol (&sym, 1);
5494 if (m == MATCH_NO)
5495 goto syntax;
5496 if (m == MATCH_ERROR)
5497 goto error;
5499 if (sym->attr.in_namelist == 0
5500 && !gfc_add_in_namelist (&sym->attr, sym->name, NULL))
5501 goto error;
5503 /* Use gfc_error_check here, rather than goto error, so that
5504 these are the only errors for the next two lines. */
5505 if (sym->as && sym->as->type == AS_ASSUMED_SIZE)
5507 gfc_error ("Assumed size array %qs in namelist %qs at "
5508 "%C is not allowed", sym->name, group_name->name);
5509 gfc_error_check ();
5512 nl = gfc_get_namelist ();
5513 nl->sym = sym;
5514 sym->refs++;
5516 if (group_name->namelist == NULL)
5517 group_name->namelist = group_name->namelist_tail = nl;
5518 else
5520 group_name->namelist_tail->next = nl;
5521 group_name->namelist_tail = nl;
5524 if (gfc_match_eos () == MATCH_YES)
5525 goto done;
5527 m = gfc_match_char (',');
5529 if (gfc_match_char ('/') == MATCH_YES)
5531 m2 = gfc_match (" %s /", &group_name);
5532 if (m2 == MATCH_YES)
5533 break;
5534 if (m2 == MATCH_ERROR)
5535 goto error;
5536 goto syntax;
5539 if (m != MATCH_YES)
5540 goto syntax;
5544 done:
5545 return MATCH_YES;
5547 syntax:
5548 gfc_syntax_error (ST_NAMELIST);
5550 error:
5551 return MATCH_ERROR;
5555 /* Match a MODULE statement. */
5557 match
5558 gfc_match_module (void)
5560 match m;
5562 m = gfc_match (" %s%t", &gfc_new_block);
5563 if (m != MATCH_YES)
5564 return m;
5566 if (!gfc_add_flavor (&gfc_new_block->attr, FL_MODULE,
5567 gfc_new_block->name, NULL))
5568 return MATCH_ERROR;
5570 return MATCH_YES;
5574 /* Free equivalence sets and lists. Recursively is the easiest way to
5575 do this. */
5577 void
5578 gfc_free_equiv_until (gfc_equiv *eq, gfc_equiv *stop)
5580 if (eq == stop)
5581 return;
5583 gfc_free_equiv (eq->eq);
5584 gfc_free_equiv_until (eq->next, stop);
5585 gfc_free_expr (eq->expr);
5586 free (eq);
5590 void
5591 gfc_free_equiv (gfc_equiv *eq)
5593 gfc_free_equiv_until (eq, NULL);
5597 /* Match an EQUIVALENCE statement. */
5599 match
5600 gfc_match_equivalence (void)
5602 gfc_equiv *eq, *set, *tail;
5603 gfc_ref *ref;
5604 gfc_symbol *sym;
5605 match m;
5606 gfc_common_head *common_head = NULL;
5607 bool common_flag;
5608 int cnt;
5609 char c;
5611 /* EQUIVALENCE has been matched. After gobbling any possible whitespace,
5612 the next character needs to be '('. Check that here, and return
5613 MATCH_NO for a variable of the form equivalencej. */
5614 gfc_gobble_whitespace ();
5615 c = gfc_peek_ascii_char ();
5616 if (c != '(')
5617 return MATCH_NO;
5619 tail = NULL;
5621 for (;;)
5623 eq = gfc_get_equiv ();
5624 if (tail == NULL)
5625 tail = eq;
5627 eq->next = gfc_current_ns->equiv;
5628 gfc_current_ns->equiv = eq;
5630 if (gfc_match_char ('(') != MATCH_YES)
5631 goto syntax;
5633 set = eq;
5634 common_flag = FALSE;
5635 cnt = 0;
5637 for (;;)
5639 m = gfc_match_equiv_variable (&set->expr);
5640 if (m == MATCH_ERROR)
5641 goto cleanup;
5642 if (m == MATCH_NO)
5643 goto syntax;
5645 /* count the number of objects. */
5646 cnt++;
5648 if (gfc_match_char ('%') == MATCH_YES)
5650 gfc_error ("Derived type component %C is not a "
5651 "permitted EQUIVALENCE member");
5652 goto cleanup;
5655 for (ref = set->expr->ref; ref; ref = ref->next)
5656 if (ref->type == REF_ARRAY && ref->u.ar.type == AR_SECTION)
5658 gfc_error ("Array reference in EQUIVALENCE at %C cannot "
5659 "be an array section");
5660 goto cleanup;
5663 sym = set->expr->symtree->n.sym;
5665 if (!gfc_add_in_equivalence (&sym->attr, sym->name, NULL))
5666 goto cleanup;
5668 if (sym->attr.in_common)
5670 common_flag = TRUE;
5671 common_head = sym->common_head;
5674 if (gfc_match_char (')') == MATCH_YES)
5675 break;
5677 if (gfc_match_char (',') != MATCH_YES)
5678 goto syntax;
5680 set->eq = gfc_get_equiv ();
5681 set = set->eq;
5684 if (cnt < 2)
5686 gfc_error ("EQUIVALENCE at %C requires two or more objects");
5687 goto cleanup;
5690 /* If one of the members of an equivalence is in common, then
5691 mark them all as being in common. Before doing this, check
5692 that members of the equivalence group are not in different
5693 common blocks. */
5694 if (common_flag)
5695 for (set = eq; set; set = set->eq)
5697 sym = set->expr->symtree->n.sym;
5698 if (sym->common_head && sym->common_head != common_head)
5700 gfc_error ("Attempt to indirectly overlap COMMON "
5701 "blocks %s and %s by EQUIVALENCE at %C",
5702 sym->common_head->name, common_head->name);
5703 goto cleanup;
5705 sym->attr.in_common = 1;
5706 sym->common_head = common_head;
5709 if (gfc_match_eos () == MATCH_YES)
5710 break;
5711 if (gfc_match_char (',') != MATCH_YES)
5713 gfc_error ("Expecting a comma in EQUIVALENCE at %C");
5714 goto cleanup;
5718 if (!gfc_notify_std (GFC_STD_F2018_OBS, "EQUIVALENCE statement at %C"))
5719 return MATCH_ERROR;
5721 return MATCH_YES;
5723 syntax:
5724 gfc_syntax_error (ST_EQUIVALENCE);
5726 cleanup:
5727 eq = tail->next;
5728 tail->next = NULL;
5730 gfc_free_equiv (gfc_current_ns->equiv);
5731 gfc_current_ns->equiv = eq;
5733 return MATCH_ERROR;
5737 /* Check that a statement function is not recursive. This is done by looking
5738 for the statement function symbol(sym) by looking recursively through its
5739 expression(e). If a reference to sym is found, true is returned.
5740 12.5.4 requires that any variable of function that is implicitly typed
5741 shall have that type confirmed by any subsequent type declaration. The
5742 implicit typing is conveniently done here. */
5743 static bool
5744 recursive_stmt_fcn (gfc_expr *, gfc_symbol *);
5746 static bool
5747 check_stmt_fcn (gfc_expr *e, gfc_symbol *sym, int *f ATTRIBUTE_UNUSED)
5750 if (e == NULL)
5751 return false;
5753 switch (e->expr_type)
5755 case EXPR_FUNCTION:
5756 if (e->symtree == NULL)
5757 return false;
5759 /* Check the name before testing for nested recursion! */
5760 if (sym->name == e->symtree->n.sym->name)
5761 return true;
5763 /* Catch recursion via other statement functions. */
5764 if (e->symtree->n.sym->attr.proc == PROC_ST_FUNCTION
5765 && e->symtree->n.sym->value
5766 && recursive_stmt_fcn (e->symtree->n.sym->value, sym))
5767 return true;
5769 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5770 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5772 break;
5774 case EXPR_VARIABLE:
5775 if (e->symtree && sym->name == e->symtree->n.sym->name)
5776 return true;
5778 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
5779 gfc_set_default_type (e->symtree->n.sym, 0, NULL);
5780 break;
5782 default:
5783 break;
5786 return false;
5790 static bool
5791 recursive_stmt_fcn (gfc_expr *e, gfc_symbol *sym)
5793 return gfc_traverse_expr (e, sym, check_stmt_fcn, 0);
5797 /* Match a statement function declaration. It is so easy to match
5798 non-statement function statements with a MATCH_ERROR as opposed to
5799 MATCH_NO that we suppress error message in most cases. */
5801 match
5802 gfc_match_st_function (void)
5804 gfc_error_buffer old_error;
5805 gfc_symbol *sym;
5806 gfc_expr *expr;
5807 match m;
5808 char name[GFC_MAX_SYMBOL_LEN + 1];
5809 locus old_locus;
5810 bool fcn;
5811 gfc_formal_arglist *ptr;
5813 /* Read the possible statement function name, and then check to see if
5814 a symbol is already present in the namespace. Record if it is a
5815 function and whether it has been referenced. */
5816 fcn = false;
5817 ptr = NULL;
5818 old_locus = gfc_current_locus;
5819 m = gfc_match_name (name);
5820 if (m == MATCH_YES)
5822 gfc_find_symbol (name, NULL, 1, &sym);
5823 if (sym && sym->attr.function && !sym->attr.referenced)
5825 fcn = true;
5826 ptr = sym->formal;
5830 gfc_current_locus = old_locus;
5831 m = gfc_match_symbol (&sym, 0);
5832 if (m != MATCH_YES)
5833 return m;
5835 gfc_push_error (&old_error);
5837 if (!gfc_add_procedure (&sym->attr, PROC_ST_FUNCTION, sym->name, NULL))
5838 goto undo_error;
5840 if (gfc_match_formal_arglist (sym, 1, 0) != MATCH_YES)
5841 goto undo_error;
5843 m = gfc_match (" = %e%t", &expr);
5844 if (m == MATCH_NO)
5845 goto undo_error;
5847 gfc_free_error (&old_error);
5849 if (m == MATCH_ERROR)
5850 return m;
5852 if (recursive_stmt_fcn (expr, sym))
5854 gfc_error ("Statement function at %L is recursive", &expr->where);
5855 return MATCH_ERROR;
5858 if (fcn && ptr != sym->formal)
5860 gfc_error ("Statement function %qs at %L conflicts with function name",
5861 sym->name, &expr->where);
5862 return MATCH_ERROR;
5865 sym->value = expr;
5867 if ((gfc_current_state () == COMP_FUNCTION
5868 || gfc_current_state () == COMP_SUBROUTINE)
5869 && gfc_state_stack->previous->state == COMP_INTERFACE)
5871 gfc_error ("Statement function at %L cannot appear within an INTERFACE",
5872 &expr->where);
5873 return MATCH_ERROR;
5876 if (!gfc_notify_std (GFC_STD_F95_OBS, "Statement function at %C"))
5877 return MATCH_ERROR;
5879 return MATCH_YES;
5881 undo_error:
5882 gfc_pop_error (&old_error);
5883 return MATCH_NO;
5887 /* Match an assignment to a pointer function (F2008). This could, in
5888 general be ambiguous with a statement function. In this implementation
5889 it remains so if it is the first statement after the specification
5890 block. */
5892 match
5893 gfc_match_ptr_fcn_assign (void)
5895 gfc_error_buffer old_error;
5896 locus old_loc;
5897 gfc_symbol *sym;
5898 gfc_expr *expr;
5899 match m;
5900 char name[GFC_MAX_SYMBOL_LEN + 1];
5902 old_loc = gfc_current_locus;
5903 m = gfc_match_name (name);
5904 if (m != MATCH_YES)
5905 return m;
5907 gfc_find_symbol (name, NULL, 1, &sym);
5908 if (sym && sym->attr.flavor != FL_PROCEDURE)
5909 return MATCH_NO;
5911 gfc_push_error (&old_error);
5913 if (sym && sym->attr.function)
5914 goto match_actual_arglist;
5916 gfc_current_locus = old_loc;
5917 m = gfc_match_symbol (&sym, 0);
5918 if (m != MATCH_YES)
5919 return m;
5921 if (!gfc_add_procedure (&sym->attr, PROC_UNKNOWN, sym->name, NULL))
5922 goto undo_error;
5924 match_actual_arglist:
5925 gfc_current_locus = old_loc;
5926 m = gfc_match (" %e", &expr);
5927 if (m != MATCH_YES)
5928 goto undo_error;
5930 new_st.op = EXEC_ASSIGN;
5931 new_st.expr1 = expr;
5932 expr = NULL;
5934 m = gfc_match (" = %e%t", &expr);
5935 if (m != MATCH_YES)
5936 goto undo_error;
5938 new_st.expr2 = expr;
5939 return MATCH_YES;
5941 undo_error:
5942 gfc_pop_error (&old_error);
5943 return MATCH_NO;
5947 /***************** SELECT CASE subroutines ******************/
5949 /* Free a single case structure. */
5951 static void
5952 free_case (gfc_case *p)
5954 if (p->low == p->high)
5955 p->high = NULL;
5956 gfc_free_expr (p->low);
5957 gfc_free_expr (p->high);
5958 free (p);
5962 /* Free a list of case structures. */
5964 void
5965 gfc_free_case_list (gfc_case *p)
5967 gfc_case *q;
5969 for (; p; p = q)
5971 q = p->next;
5972 free_case (p);
5977 /* Match a single case selector. Combining the requirements of F08:C830
5978 and F08:C832 (R838) means that the case-value must have either CHARACTER,
5979 INTEGER, or LOGICAL type. */
5981 static match
5982 match_case_selector (gfc_case **cp)
5984 gfc_case *c;
5985 match m;
5987 c = gfc_get_case ();
5988 c->where = gfc_current_locus;
5990 if (gfc_match_char (':') == MATCH_YES)
5992 m = gfc_match_init_expr (&c->high);
5993 if (m == MATCH_NO)
5994 goto need_expr;
5995 if (m == MATCH_ERROR)
5996 goto cleanup;
5998 if (c->high->ts.type != BT_LOGICAL && c->high->ts.type != BT_INTEGER
5999 && c->high->ts.type != BT_CHARACTER)
6001 gfc_error ("Expression in CASE selector at %L cannot be %s",
6002 &c->high->where, gfc_typename (&c->high->ts));
6003 goto cleanup;
6006 else
6008 m = gfc_match_init_expr (&c->low);
6009 if (m == MATCH_ERROR)
6010 goto cleanup;
6011 if (m == MATCH_NO)
6012 goto need_expr;
6014 if (c->low->ts.type != BT_LOGICAL && c->low->ts.type != BT_INTEGER
6015 && c->low->ts.type != BT_CHARACTER)
6017 gfc_error ("Expression in CASE selector at %L cannot be %s",
6018 &c->low->where, gfc_typename (&c->low->ts));
6019 goto cleanup;
6022 /* If we're not looking at a ':' now, make a range out of a single
6023 target. Else get the upper bound for the case range. */
6024 if (gfc_match_char (':') != MATCH_YES)
6025 c->high = c->low;
6026 else
6028 m = gfc_match_init_expr (&c->high);
6029 if (m == MATCH_ERROR)
6030 goto cleanup;
6031 /* MATCH_NO is fine. It's OK if nothing is there! */
6035 *cp = c;
6036 return MATCH_YES;
6038 need_expr:
6039 gfc_error ("Expected initialization expression in CASE at %C");
6041 cleanup:
6042 free_case (c);
6043 return MATCH_ERROR;
6047 /* Match the end of a case statement. */
6049 static match
6050 match_case_eos (void)
6052 char name[GFC_MAX_SYMBOL_LEN + 1];
6053 match m;
6055 if (gfc_match_eos () == MATCH_YES)
6056 return MATCH_YES;
6058 /* If the case construct doesn't have a case-construct-name, we
6059 should have matched the EOS. */
6060 if (!gfc_current_block ())
6061 return MATCH_NO;
6063 gfc_gobble_whitespace ();
6065 m = gfc_match_name (name);
6066 if (m != MATCH_YES)
6067 return m;
6069 if (strcmp (name, gfc_current_block ()->name) != 0)
6071 gfc_error ("Expected block name %qs of SELECT construct at %C",
6072 gfc_current_block ()->name);
6073 return MATCH_ERROR;
6076 return gfc_match_eos ();
6080 /* Match a SELECT statement. */
6082 match
6083 gfc_match_select (void)
6085 gfc_expr *expr;
6086 match m;
6088 m = gfc_match_label ();
6089 if (m == MATCH_ERROR)
6090 return m;
6092 m = gfc_match (" select case ( %e )%t", &expr);
6093 if (m != MATCH_YES)
6094 return m;
6096 new_st.op = EXEC_SELECT;
6097 new_st.expr1 = expr;
6099 return MATCH_YES;
6103 /* Transfer the selector typespec to the associate name. */
6105 static void
6106 copy_ts_from_selector_to_associate (gfc_expr *associate, gfc_expr *selector)
6108 gfc_ref *ref;
6109 gfc_symbol *assoc_sym;
6110 int rank = 0;
6112 assoc_sym = associate->symtree->n.sym;
6114 /* At this stage the expression rank and arrayspec dimensions have
6115 not been completely sorted out. We must get the expr2->rank
6116 right here, so that the correct class container is obtained. */
6117 ref = selector->ref;
6118 while (ref && ref->next)
6119 ref = ref->next;
6121 if (selector->ts.type == BT_CLASS && CLASS_DATA (selector)->as
6122 && CLASS_DATA (selector)->as->type == AS_ASSUMED_RANK)
6124 assoc_sym->attr.dimension = 1;
6125 assoc_sym->as = gfc_copy_array_spec (CLASS_DATA (selector)->as);
6126 goto build_class_sym;
6128 else if (selector->ts.type == BT_CLASS && CLASS_DATA (selector)->as
6129 && ref && ref->type == REF_ARRAY)
6131 /* Ensure that the array reference type is set. We cannot use
6132 gfc_resolve_expr at this point, so the usable parts of
6133 resolve.c(resolve_array_ref) are employed to do it. */
6134 if (ref->u.ar.type == AR_UNKNOWN)
6136 ref->u.ar.type = AR_ELEMENT;
6137 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
6138 if (ref->u.ar.dimen_type[i] == DIMEN_RANGE
6139 || ref->u.ar.dimen_type[i] == DIMEN_VECTOR
6140 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
6141 && ref->u.ar.start[i] && ref->u.ar.start[i]->rank))
6143 ref->u.ar.type = AR_SECTION;
6144 break;
6148 if (ref->u.ar.type == AR_FULL)
6149 selector->rank = CLASS_DATA (selector)->as->rank;
6150 else if (ref->u.ar.type == AR_SECTION)
6151 selector->rank = ref->u.ar.dimen;
6152 else
6153 selector->rank = 0;
6155 rank = selector->rank;
6158 if (rank)
6160 for (int i = 0; i < ref->u.ar.dimen + ref->u.ar.codimen; i++)
6161 if (ref->u.ar.dimen_type[i] == DIMEN_ELEMENT
6162 || (ref->u.ar.dimen_type[i] == DIMEN_UNKNOWN
6163 && ref->u.ar.end[i] == NULL
6164 && ref->u.ar.stride[i] == NULL))
6165 rank--;
6167 if (rank)
6169 assoc_sym->attr.dimension = 1;
6170 assoc_sym->as = gfc_get_array_spec ();
6171 assoc_sym->as->rank = rank;
6172 assoc_sym->as->type = AS_DEFERRED;
6174 else
6175 assoc_sym->as = NULL;
6177 else
6178 assoc_sym->as = NULL;
6180 build_class_sym:
6181 if (selector->ts.type == BT_CLASS)
6183 /* The correct class container has to be available. */
6184 assoc_sym->ts.type = BT_CLASS;
6185 assoc_sym->ts.u.derived = CLASS_DATA (selector)->ts.u.derived;
6186 assoc_sym->attr.pointer = 1;
6187 gfc_build_class_symbol (&assoc_sym->ts, &assoc_sym->attr, &assoc_sym->as);
6192 /* Push the current selector onto the SELECT TYPE stack. */
6194 static void
6195 select_type_push (gfc_symbol *sel)
6197 gfc_select_type_stack *top = gfc_get_select_type_stack ();
6198 top->selector = sel;
6199 top->tmp = NULL;
6200 top->prev = select_type_stack;
6202 select_type_stack = top;
6206 /* Set the temporary for the current intrinsic SELECT TYPE selector. */
6208 static gfc_symtree *
6209 select_intrinsic_set_tmp (gfc_typespec *ts)
6211 char name[GFC_MAX_SYMBOL_LEN];
6212 gfc_symtree *tmp;
6213 HOST_WIDE_INT charlen = 0;
6214 gfc_symbol *selector = select_type_stack->selector;
6215 gfc_symbol *sym;
6217 if (ts->type == BT_CLASS || ts->type == BT_DERIVED)
6218 return NULL;
6220 if (selector->ts.type == BT_CLASS && !selector->attr.class_ok)
6221 return NULL;
6223 /* Case value == NULL corresponds to SELECT TYPE cases otherwise
6224 the values correspond to SELECT rank cases. */
6225 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length
6226 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
6227 charlen = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
6229 if (ts->type != BT_CHARACTER)
6230 sprintf (name, "__tmp_%s_%d", gfc_basic_typename (ts->type),
6231 ts->kind);
6232 else
6233 snprintf (name, sizeof (name),
6234 "__tmp_%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
6235 gfc_basic_typename (ts->type), charlen, ts->kind);
6237 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
6238 sym = tmp->n.sym;
6239 gfc_add_type (sym, ts, NULL);
6241 /* Copy across the array spec to the selector. */
6242 if (selector->ts.type == BT_CLASS
6243 && (CLASS_DATA (selector)->attr.dimension
6244 || CLASS_DATA (selector)->attr.codimension))
6246 sym->attr.pointer = 1;
6247 sym->attr.dimension = CLASS_DATA (selector)->attr.dimension;
6248 sym->attr.codimension = CLASS_DATA (selector)->attr.codimension;
6249 sym->as = gfc_copy_array_spec (CLASS_DATA (selector)->as);
6252 gfc_set_sym_referenced (sym);
6253 gfc_add_flavor (&sym->attr, FL_VARIABLE, name, NULL);
6254 sym->attr.select_type_temporary = 1;
6256 return tmp;
6260 /* Set up a temporary for the current TYPE IS / CLASS IS branch . */
6262 static void
6263 select_type_set_tmp (gfc_typespec *ts)
6265 char name[GFC_MAX_SYMBOL_LEN];
6266 gfc_symtree *tmp = NULL;
6267 gfc_symbol *selector = select_type_stack->selector;
6268 gfc_symbol *sym;
6270 if (!ts)
6272 select_type_stack->tmp = NULL;
6273 return;
6276 tmp = select_intrinsic_set_tmp (ts);
6278 if (tmp == NULL)
6280 if (!ts->u.derived)
6281 return;
6283 if (ts->type == BT_CLASS)
6284 sprintf (name, "__tmp_class_%s", ts->u.derived->name);
6285 else
6286 sprintf (name, "__tmp_type_%s", ts->u.derived->name);
6288 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
6289 sym = tmp->n.sym;
6290 gfc_add_type (sym, ts, NULL);
6292 if (selector->ts.type == BT_CLASS && selector->attr.class_ok)
6294 sym->attr.pointer
6295 = CLASS_DATA (selector)->attr.class_pointer;
6297 /* Copy across the array spec to the selector. */
6298 if (CLASS_DATA (selector)->attr.dimension
6299 || CLASS_DATA (selector)->attr.codimension)
6301 sym->attr.dimension
6302 = CLASS_DATA (selector)->attr.dimension;
6303 sym->attr.codimension
6304 = CLASS_DATA (selector)->attr.codimension;
6305 sym->as
6306 = gfc_copy_array_spec (CLASS_DATA (selector)->as);
6310 gfc_set_sym_referenced (sym);
6311 gfc_add_flavor (&sym->attr, FL_VARIABLE, name, NULL);
6312 sym->attr.select_type_temporary = 1;
6314 if (ts->type == BT_CLASS)
6315 gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
6317 else
6318 sym = tmp->n.sym;
6321 /* Add an association for it, so the rest of the parser knows it is
6322 an associate-name. The target will be set during resolution. */
6323 sym->assoc = gfc_get_association_list ();
6324 sym->assoc->dangling = 1;
6325 sym->assoc->st = tmp;
6327 select_type_stack->tmp = tmp;
6331 /* Match a SELECT TYPE statement. */
6333 match
6334 gfc_match_select_type (void)
6336 gfc_expr *expr1, *expr2 = NULL;
6337 match m;
6338 char name[GFC_MAX_SYMBOL_LEN];
6339 bool class_array;
6340 gfc_symbol *sym;
6341 gfc_namespace *ns = gfc_current_ns;
6343 m = gfc_match_label ();
6344 if (m == MATCH_ERROR)
6345 return m;
6347 m = gfc_match (" select type ( ");
6348 if (m != MATCH_YES)
6349 return m;
6351 if (gfc_current_state() == COMP_MODULE
6352 || gfc_current_state() == COMP_SUBMODULE)
6354 gfc_error ("SELECT TYPE at %C cannot appear in this scope");
6355 return MATCH_ERROR;
6358 gfc_current_ns = gfc_build_block_ns (ns);
6359 m = gfc_match (" %n => %e", name, &expr2);
6360 if (m == MATCH_YES)
6362 expr1 = gfc_get_expr ();
6363 expr1->expr_type = EXPR_VARIABLE;
6364 expr1->where = expr2->where;
6365 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
6367 m = MATCH_ERROR;
6368 goto cleanup;
6371 sym = expr1->symtree->n.sym;
6372 if (expr2->ts.type == BT_UNKNOWN)
6373 sym->attr.untyped = 1;
6374 else
6375 copy_ts_from_selector_to_associate (expr1, expr2);
6377 sym->attr.flavor = FL_VARIABLE;
6378 sym->attr.referenced = 1;
6379 sym->attr.class_ok = 1;
6381 else
6383 m = gfc_match (" %e ", &expr1);
6384 if (m != MATCH_YES)
6386 std::swap (ns, gfc_current_ns);
6387 gfc_free_namespace (ns);
6388 return m;
6392 m = gfc_match (" )%t");
6393 if (m != MATCH_YES)
6395 gfc_error ("parse error in SELECT TYPE statement at %C");
6396 goto cleanup;
6399 /* This ghastly expression seems to be needed to distinguish a CLASS
6400 array, which can have a reference, from other expressions that
6401 have references, such as derived type components, and are not
6402 allowed by the standard.
6403 TODO: see if it is sufficient to exclude component and substring
6404 references. */
6405 class_array = (expr1->expr_type == EXPR_VARIABLE
6406 && expr1->ts.type == BT_CLASS
6407 && CLASS_DATA (expr1)
6408 && (strcmp (CLASS_DATA (expr1)->name, "_data") == 0)
6409 && (CLASS_DATA (expr1)->attr.dimension
6410 || CLASS_DATA (expr1)->attr.codimension)
6411 && expr1->ref
6412 && expr1->ref->type == REF_ARRAY
6413 && expr1->ref->u.ar.type == AR_FULL
6414 && expr1->ref->next == NULL);
6416 /* Check for F03:C811 (F08:C835). */
6417 if (!expr2 && (expr1->expr_type != EXPR_VARIABLE
6418 || (!class_array && expr1->ref != NULL)))
6420 gfc_error ("Selector in SELECT TYPE at %C is not a named variable; "
6421 "use associate-name=>");
6422 m = MATCH_ERROR;
6423 goto cleanup;
6426 new_st.op = EXEC_SELECT_TYPE;
6427 new_st.expr1 = expr1;
6428 new_st.expr2 = expr2;
6429 new_st.ext.block.ns = gfc_current_ns;
6431 select_type_push (expr1->symtree->n.sym);
6432 gfc_current_ns = ns;
6434 return MATCH_YES;
6436 cleanup:
6437 gfc_free_expr (expr1);
6438 gfc_free_expr (expr2);
6439 gfc_undo_symbols ();
6440 std::swap (ns, gfc_current_ns);
6441 gfc_free_namespace (ns);
6442 return m;
6446 /* Set the temporary for the current intrinsic SELECT RANK selector. */
6448 static void
6449 select_rank_set_tmp (gfc_typespec *ts, int *case_value)
6451 char name[2 * GFC_MAX_SYMBOL_LEN];
6452 char tname[GFC_MAX_SYMBOL_LEN];
6453 gfc_symtree *tmp;
6454 gfc_symbol *selector = select_type_stack->selector;
6455 gfc_symbol *sym;
6456 gfc_symtree *st;
6457 HOST_WIDE_INT charlen = 0;
6459 if (case_value == NULL)
6460 return;
6462 if (ts->type == BT_CHARACTER && ts->u.cl && ts->u.cl->length
6463 && ts->u.cl->length->expr_type == EXPR_CONSTANT)
6464 charlen = gfc_mpz_get_hwi (ts->u.cl->length->value.integer);
6466 if (ts->type == BT_CLASS)
6467 sprintf (tname, "class_%s", ts->u.derived->name);
6468 else if (ts->type == BT_DERIVED)
6469 sprintf (tname, "type_%s", ts->u.derived->name);
6470 else if (ts->type != BT_CHARACTER)
6471 sprintf (tname, "%s_%d", gfc_basic_typename (ts->type), ts->kind);
6472 else
6473 sprintf (tname, "%s_" HOST_WIDE_INT_PRINT_DEC "_%d",
6474 gfc_basic_typename (ts->type), charlen, ts->kind);
6476 /* Case value == NULL corresponds to SELECT TYPE cases otherwise
6477 the values correspond to SELECT rank cases. */
6478 if (*case_value >=0)
6479 sprintf (name, "__tmp_%s_rank_%d", tname, *case_value);
6480 else
6481 sprintf (name, "__tmp_%s_rank_m%d", tname, -*case_value);
6483 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
6484 if (st)
6485 return;
6487 gfc_get_sym_tree (name, gfc_current_ns, &tmp, false);
6488 sym = tmp->n.sym;
6489 gfc_add_type (sym, ts, NULL);
6491 /* Copy across the array spec to the selector. */
6492 if (selector->ts.type == BT_CLASS)
6494 sym->ts.u.derived = CLASS_DATA (selector)->ts.u.derived;
6495 sym->attr.pointer = CLASS_DATA (selector)->attr.pointer;
6496 sym->attr.allocatable = CLASS_DATA (selector)->attr.allocatable;
6497 sym->attr.target = CLASS_DATA (selector)->attr.target;
6498 sym->attr.class_ok = 0;
6499 if (case_value && *case_value != 0)
6501 sym->attr.dimension = 1;
6502 sym->as = gfc_copy_array_spec (CLASS_DATA (selector)->as);
6503 if (*case_value > 0)
6505 sym->as->type = AS_DEFERRED;
6506 sym->as->rank = *case_value;
6508 else if (*case_value == -1)
6510 sym->as->type = AS_ASSUMED_SIZE;
6511 sym->as->rank = 1;
6515 else
6517 sym->attr.pointer = selector->attr.pointer;
6518 sym->attr.allocatable = selector->attr.allocatable;
6519 sym->attr.target = selector->attr.target;
6520 if (case_value && *case_value != 0)
6522 sym->attr.dimension = 1;
6523 sym->as = gfc_copy_array_spec (selector->as);
6524 if (*case_value > 0)
6526 sym->as->type = AS_DEFERRED;
6527 sym->as->rank = *case_value;
6529 else if (*case_value == -1)
6531 sym->as->type = AS_ASSUMED_SIZE;
6532 sym->as->rank = 1;
6537 gfc_set_sym_referenced (sym);
6538 gfc_add_flavor (&sym->attr, FL_VARIABLE, name, NULL);
6539 sym->attr.select_type_temporary = 1;
6540 if (case_value)
6541 sym->attr.select_rank_temporary = 1;
6543 if (ts->type == BT_CLASS)
6544 gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
6546 /* Add an association for it, so the rest of the parser knows it is
6547 an associate-name. The target will be set during resolution. */
6548 sym->assoc = gfc_get_association_list ();
6549 sym->assoc->dangling = 1;
6550 sym->assoc->st = tmp;
6552 select_type_stack->tmp = tmp;
6556 /* Match a SELECT RANK statement. */
6558 match
6559 gfc_match_select_rank (void)
6561 gfc_expr *expr1, *expr2 = NULL;
6562 match m;
6563 char name[GFC_MAX_SYMBOL_LEN];
6564 gfc_symbol *sym, *sym2;
6565 gfc_namespace *ns = gfc_current_ns;
6566 gfc_array_spec *as = NULL;
6568 m = gfc_match_label ();
6569 if (m == MATCH_ERROR)
6570 return m;
6572 m = gfc_match (" select rank ( ");
6573 if (m != MATCH_YES)
6574 return m;
6576 if (!gfc_notify_std (GFC_STD_F2018, "SELECT RANK statement at %C"))
6577 return MATCH_NO;
6579 gfc_current_ns = gfc_build_block_ns (ns);
6580 m = gfc_match (" %n => %e", name, &expr2);
6581 if (m == MATCH_YES)
6583 expr1 = gfc_get_expr ();
6584 expr1->expr_type = EXPR_VARIABLE;
6585 expr1->where = expr2->where;
6586 expr1->ref = gfc_copy_ref (expr2->ref);
6587 if (gfc_get_sym_tree (name, NULL, &expr1->symtree, false))
6589 m = MATCH_ERROR;
6590 goto cleanup;
6593 sym = expr1->symtree->n.sym;
6595 if (expr2->symtree)
6597 sym2 = expr2->symtree->n.sym;
6598 as = sym2->ts.type == BT_CLASS ? CLASS_DATA (sym2)->as : sym2->as;
6601 if (expr2->expr_type != EXPR_VARIABLE
6602 || !(as && as->type == AS_ASSUMED_RANK))
6604 gfc_error ("The SELECT RANK selector at %C must be an assumed "
6605 "rank variable");
6606 m = MATCH_ERROR;
6607 goto cleanup;
6610 if (expr2->ts.type == BT_CLASS)
6612 copy_ts_from_selector_to_associate (expr1, expr2);
6614 sym->attr.flavor = FL_VARIABLE;
6615 sym->attr.referenced = 1;
6616 sym->attr.class_ok = 1;
6617 CLASS_DATA (sym)->attr.allocatable = CLASS_DATA (sym2)->attr.allocatable;
6618 CLASS_DATA (sym)->attr.pointer = CLASS_DATA (sym2)->attr.pointer;
6619 CLASS_DATA (sym)->attr.target = CLASS_DATA (sym2)->attr.target;
6620 sym->attr.pointer = 1;
6622 else
6624 sym->ts = sym2->ts;
6625 sym->as = gfc_copy_array_spec (sym2->as);
6626 sym->attr.dimension = 1;
6628 sym->attr.flavor = FL_VARIABLE;
6629 sym->attr.referenced = 1;
6630 sym->attr.class_ok = sym2->attr.class_ok;
6631 sym->attr.allocatable = sym2->attr.allocatable;
6632 sym->attr.pointer = sym2->attr.pointer;
6633 sym->attr.target = sym2->attr.target;
6636 else
6638 m = gfc_match (" %e ", &expr1);
6640 if (m != MATCH_YES)
6642 std::swap (ns, gfc_current_ns);
6643 gfc_free_namespace (ns);
6644 return m;
6647 if (expr1->symtree)
6649 sym = expr1->symtree->n.sym;
6650 as = sym->ts.type == BT_CLASS ? CLASS_DATA (sym)->as : sym->as;
6653 if (expr1->expr_type != EXPR_VARIABLE
6654 || !(as && as->type == AS_ASSUMED_RANK))
6656 gfc_error("The SELECT RANK selector at %C must be an assumed "
6657 "rank variable");
6658 m = MATCH_ERROR;
6659 goto cleanup;
6663 m = gfc_match (" )%t");
6664 if (m != MATCH_YES)
6666 gfc_error ("parse error in SELECT RANK statement at %C");
6667 goto cleanup;
6670 new_st.op = EXEC_SELECT_RANK;
6671 new_st.expr1 = expr1;
6672 new_st.expr2 = expr2;
6673 new_st.ext.block.ns = gfc_current_ns;
6675 select_type_push (expr1->symtree->n.sym);
6676 gfc_current_ns = ns;
6678 return MATCH_YES;
6680 cleanup:
6681 gfc_free_expr (expr1);
6682 gfc_free_expr (expr2);
6683 gfc_undo_symbols ();
6684 std::swap (ns, gfc_current_ns);
6685 gfc_free_namespace (ns);
6686 return m;
6690 /* Match a CASE statement. */
6692 match
6693 gfc_match_case (void)
6695 gfc_case *c, *head, *tail;
6696 match m;
6698 head = tail = NULL;
6700 if (gfc_current_state () != COMP_SELECT)
6702 gfc_error ("Unexpected CASE statement at %C");
6703 return MATCH_ERROR;
6706 if (gfc_match ("% default") == MATCH_YES)
6708 m = match_case_eos ();
6709 if (m == MATCH_NO)
6710 goto syntax;
6711 if (m == MATCH_ERROR)
6712 goto cleanup;
6714 new_st.op = EXEC_SELECT;
6715 c = gfc_get_case ();
6716 c->where = gfc_current_locus;
6717 new_st.ext.block.case_list = c;
6718 return MATCH_YES;
6721 if (gfc_match_char ('(') != MATCH_YES)
6722 goto syntax;
6724 for (;;)
6726 if (match_case_selector (&c) == MATCH_ERROR)
6727 goto cleanup;
6729 if (head == NULL)
6730 head = c;
6731 else
6732 tail->next = c;
6734 tail = c;
6736 if (gfc_match_char (')') == MATCH_YES)
6737 break;
6738 if (gfc_match_char (',') != MATCH_YES)
6739 goto syntax;
6742 m = match_case_eos ();
6743 if (m == MATCH_NO)
6744 goto syntax;
6745 if (m == MATCH_ERROR)
6746 goto cleanup;
6748 new_st.op = EXEC_SELECT;
6749 new_st.ext.block.case_list = head;
6751 return MATCH_YES;
6753 syntax:
6754 gfc_error ("Syntax error in CASE specification at %C");
6756 cleanup:
6757 gfc_free_case_list (head); /* new_st is cleaned up in parse.c. */
6758 return MATCH_ERROR;
6762 /* Match a TYPE IS statement. */
6764 match
6765 gfc_match_type_is (void)
6767 gfc_case *c = NULL;
6768 match m;
6770 if (gfc_current_state () != COMP_SELECT_TYPE)
6772 gfc_error ("Unexpected TYPE IS statement at %C");
6773 return MATCH_ERROR;
6776 if (gfc_match_char ('(') != MATCH_YES)
6777 goto syntax;
6779 c = gfc_get_case ();
6780 c->where = gfc_current_locus;
6782 m = gfc_match_type_spec (&c->ts);
6783 if (m == MATCH_NO)
6784 goto syntax;
6785 if (m == MATCH_ERROR)
6786 goto cleanup;
6788 if (gfc_match_char (')') != MATCH_YES)
6789 goto syntax;
6791 m = match_case_eos ();
6792 if (m == MATCH_NO)
6793 goto syntax;
6794 if (m == MATCH_ERROR)
6795 goto cleanup;
6797 new_st.op = EXEC_SELECT_TYPE;
6798 new_st.ext.block.case_list = c;
6800 if (c->ts.type == BT_DERIVED && c->ts.u.derived
6801 && (c->ts.u.derived->attr.sequence
6802 || c->ts.u.derived->attr.is_bind_c))
6804 gfc_error ("The type-spec shall not specify a sequence derived "
6805 "type or a type with the BIND attribute in SELECT "
6806 "TYPE at %C [F2003:C815]");
6807 return MATCH_ERROR;
6810 if (c->ts.type == BT_DERIVED
6811 && c->ts.u.derived && c->ts.u.derived->attr.pdt_type
6812 && gfc_spec_list_type (type_param_spec_list, c->ts.u.derived)
6813 != SPEC_ASSUMED)
6815 gfc_error ("All the LEN type parameters in the TYPE IS statement "
6816 "at %C must be ASSUMED");
6817 return MATCH_ERROR;
6820 /* Create temporary variable. */
6821 select_type_set_tmp (&c->ts);
6823 return MATCH_YES;
6825 syntax:
6826 gfc_error ("Syntax error in TYPE IS specification at %C");
6828 cleanup:
6829 if (c != NULL)
6830 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
6831 return MATCH_ERROR;
6835 /* Match a CLASS IS or CLASS DEFAULT statement. */
6837 match
6838 gfc_match_class_is (void)
6840 gfc_case *c = NULL;
6841 match m;
6843 if (gfc_current_state () != COMP_SELECT_TYPE)
6844 return MATCH_NO;
6846 if (gfc_match ("% default") == MATCH_YES)
6848 m = match_case_eos ();
6849 if (m == MATCH_NO)
6850 goto syntax;
6851 if (m == MATCH_ERROR)
6852 goto cleanup;
6854 new_st.op = EXEC_SELECT_TYPE;
6855 c = gfc_get_case ();
6856 c->where = gfc_current_locus;
6857 c->ts.type = BT_UNKNOWN;
6858 new_st.ext.block.case_list = c;
6859 select_type_set_tmp (NULL);
6860 return MATCH_YES;
6863 m = gfc_match ("% is");
6864 if (m == MATCH_NO)
6865 goto syntax;
6866 if (m == MATCH_ERROR)
6867 goto cleanup;
6869 if (gfc_match_char ('(') != MATCH_YES)
6870 goto syntax;
6872 c = gfc_get_case ();
6873 c->where = gfc_current_locus;
6875 m = match_derived_type_spec (&c->ts);
6876 if (m == MATCH_NO)
6877 goto syntax;
6878 if (m == MATCH_ERROR)
6879 goto cleanup;
6881 if (c->ts.type == BT_DERIVED)
6882 c->ts.type = BT_CLASS;
6884 if (gfc_match_char (')') != MATCH_YES)
6885 goto syntax;
6887 m = match_case_eos ();
6888 if (m == MATCH_NO)
6889 goto syntax;
6890 if (m == MATCH_ERROR)
6891 goto cleanup;
6893 new_st.op = EXEC_SELECT_TYPE;
6894 new_st.ext.block.case_list = c;
6896 /* Create temporary variable. */
6897 select_type_set_tmp (&c->ts);
6899 return MATCH_YES;
6901 syntax:
6902 gfc_error ("Syntax error in CLASS IS specification at %C");
6904 cleanup:
6905 if (c != NULL)
6906 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
6907 return MATCH_ERROR;
6911 /* Match a RANK statement. */
6913 match
6914 gfc_match_rank_is (void)
6916 gfc_case *c = NULL;
6917 match m;
6918 int case_value;
6920 if (gfc_current_state () != COMP_SELECT_RANK)
6922 gfc_error ("Unexpected RANK statement at %C");
6923 return MATCH_ERROR;
6926 if (gfc_match ("% default") == MATCH_YES)
6928 m = match_case_eos ();
6929 if (m == MATCH_NO)
6930 goto syntax;
6931 if (m == MATCH_ERROR)
6932 goto cleanup;
6934 new_st.op = EXEC_SELECT_RANK;
6935 c = gfc_get_case ();
6936 c->ts.type = BT_UNKNOWN;
6937 c->where = gfc_current_locus;
6938 new_st.ext.block.case_list = c;
6939 select_type_stack->tmp = NULL;
6940 return MATCH_YES;
6943 if (gfc_match_char ('(') != MATCH_YES)
6944 goto syntax;
6946 c = gfc_get_case ();
6947 c->where = gfc_current_locus;
6948 c->ts = select_type_stack->selector->ts;
6950 m = gfc_match_expr (&c->low);
6951 if (m == MATCH_NO)
6953 if (gfc_match_char ('*') == MATCH_YES)
6954 c->low = gfc_get_int_expr (gfc_default_integer_kind,
6955 NULL, -1);
6956 else
6957 goto syntax;
6959 case_value = -1;
6961 else if (m == MATCH_YES)
6963 /* F2018: R1150 */
6964 if (c->low->expr_type != EXPR_CONSTANT
6965 || c->low->ts.type != BT_INTEGER
6966 || c->low->rank)
6968 gfc_error ("The SELECT RANK CASE expression at %C must be a "
6969 "scalar, integer constant");
6970 goto cleanup;
6973 case_value = (int) mpz_get_si (c->low->value.integer);
6974 /* F2018: C1151 */
6975 if ((case_value < 0) || (case_value > GFC_MAX_DIMENSIONS))
6977 gfc_error ("The value of the SELECT RANK CASE expression at "
6978 "%C must not be less than zero or greater than %d",
6979 GFC_MAX_DIMENSIONS);
6980 goto cleanup;
6983 else
6984 goto cleanup;
6986 if (gfc_match_char (')') != MATCH_YES)
6987 goto syntax;
6989 m = match_case_eos ();
6990 if (m == MATCH_NO)
6991 goto syntax;
6992 if (m == MATCH_ERROR)
6993 goto cleanup;
6995 new_st.op = EXEC_SELECT_RANK;
6996 new_st.ext.block.case_list = c;
6998 /* Create temporary variable. Recycle the select type code. */
6999 select_rank_set_tmp (&c->ts, &case_value);
7001 return MATCH_YES;
7003 syntax:
7004 gfc_error ("Syntax error in RANK specification at %C");
7006 cleanup:
7007 if (c != NULL)
7008 gfc_free_case_list (c); /* new_st is cleaned up in parse.c. */
7009 return MATCH_ERROR;
7012 /********************* WHERE subroutines ********************/
7014 /* Match the rest of a simple WHERE statement that follows an IF statement.
7017 static match
7018 match_simple_where (void)
7020 gfc_expr *expr;
7021 gfc_code *c;
7022 match m;
7024 m = gfc_match (" ( %e )", &expr);
7025 if (m != MATCH_YES)
7026 return m;
7028 m = gfc_match_assignment ();
7029 if (m == MATCH_NO)
7030 goto syntax;
7031 if (m == MATCH_ERROR)
7032 goto cleanup;
7034 if (gfc_match_eos () != MATCH_YES)
7035 goto syntax;
7037 c = gfc_get_code (EXEC_WHERE);
7038 c->expr1 = expr;
7040 c->next = XCNEW (gfc_code);
7041 *c->next = new_st;
7042 c->next->loc = gfc_current_locus;
7043 gfc_clear_new_st ();
7045 new_st.op = EXEC_WHERE;
7046 new_st.block = c;
7048 return MATCH_YES;
7050 syntax:
7051 gfc_syntax_error (ST_WHERE);
7053 cleanup:
7054 gfc_free_expr (expr);
7055 return MATCH_ERROR;
7059 /* Match a WHERE statement. */
7061 match
7062 gfc_match_where (gfc_statement *st)
7064 gfc_expr *expr;
7065 match m0, m;
7066 gfc_code *c;
7068 m0 = gfc_match_label ();
7069 if (m0 == MATCH_ERROR)
7070 return m0;
7072 m = gfc_match (" where ( %e )", &expr);
7073 if (m != MATCH_YES)
7074 return m;
7076 if (gfc_match_eos () == MATCH_YES)
7078 *st = ST_WHERE_BLOCK;
7079 new_st.op = EXEC_WHERE;
7080 new_st.expr1 = expr;
7081 return MATCH_YES;
7084 m = gfc_match_assignment ();
7085 if (m == MATCH_NO)
7086 gfc_syntax_error (ST_WHERE);
7088 if (m != MATCH_YES)
7090 gfc_free_expr (expr);
7091 return MATCH_ERROR;
7094 /* We've got a simple WHERE statement. */
7095 *st = ST_WHERE;
7096 c = gfc_get_code (EXEC_WHERE);
7097 c->expr1 = expr;
7099 /* Put in the assignment. It will not be processed by add_statement, so we
7100 need to copy the location here. */
7102 c->next = XCNEW (gfc_code);
7103 *c->next = new_st;
7104 c->next->loc = gfc_current_locus;
7105 gfc_clear_new_st ();
7107 new_st.op = EXEC_WHERE;
7108 new_st.block = c;
7110 return MATCH_YES;
7114 /* Match an ELSEWHERE statement. We leave behind a WHERE node in
7115 new_st if successful. */
7117 match
7118 gfc_match_elsewhere (void)
7120 char name[GFC_MAX_SYMBOL_LEN + 1];
7121 gfc_expr *expr;
7122 match m;
7124 if (gfc_current_state () != COMP_WHERE)
7126 gfc_error ("ELSEWHERE statement at %C not enclosed in WHERE block");
7127 return MATCH_ERROR;
7130 expr = NULL;
7132 if (gfc_match_char ('(') == MATCH_YES)
7134 m = gfc_match_expr (&expr);
7135 if (m == MATCH_NO)
7136 goto syntax;
7137 if (m == MATCH_ERROR)
7138 return MATCH_ERROR;
7140 if (gfc_match_char (')') != MATCH_YES)
7141 goto syntax;
7144 if (gfc_match_eos () != MATCH_YES)
7146 /* Only makes sense if we have a where-construct-name. */
7147 if (!gfc_current_block ())
7149 m = MATCH_ERROR;
7150 goto cleanup;
7152 /* Better be a name at this point. */
7153 m = gfc_match_name (name);
7154 if (m == MATCH_NO)
7155 goto syntax;
7156 if (m == MATCH_ERROR)
7157 goto cleanup;
7159 if (gfc_match_eos () != MATCH_YES)
7160 goto syntax;
7162 if (strcmp (name, gfc_current_block ()->name) != 0)
7164 gfc_error ("Label %qs at %C doesn't match WHERE label %qs",
7165 name, gfc_current_block ()->name);
7166 goto cleanup;
7170 new_st.op = EXEC_WHERE;
7171 new_st.expr1 = expr;
7172 return MATCH_YES;
7174 syntax:
7175 gfc_syntax_error (ST_ELSEWHERE);
7177 cleanup:
7178 gfc_free_expr (expr);
7179 return MATCH_ERROR;